1

I'm using the example for the most part and it does create an image it's just garbled:

unsigned char* readJpeg(JNIEnv* env, libraw_processed_image_t *raw)
{
    // http://sourceforge.net/p/libjpeg-turbo/code/HEAD/tree/trunk/example.c#l109
    // http://stackoverflow.com/questions/5616216/need-help-in-reading-jpeg-file-using-libjpeg

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    int row_stride;     /* physical row width in output buffer */

    cinfo.err = jpeg_std_error(&jerr);

    /* Now we can initialize the JPEG decompression object. */
    jpeg_create_decompress(&cinfo);


    /* Step 2: specify data source (eg, a file) */
    jpeg_mem_src(&cinfo, raw->data, raw->data_size);

    /* Step 3: read file parameters with jpeg_read_header() */
    (void) jpeg_read_header(&cinfo, TRUE);

    /* Step 4: set parameters for decompression */

    /* In this example, we don't need to change any of the defaults set by
    * jpeg_read_header(), so we do nothing here.
    */

    /* Step 5: Start decompressor */

    (void) jpeg_start_decompress(&cinfo);
    /* We can ignore the return value since suspension is not possible
    * with the stdio data source.
    */

    /* We may need to do some setup of our own at this point before reading
    * the data.  After jpeg_start_decompress() we have the correct scaled
    * output image dimensions available, as well as the output colormap
    * if we asked for color quantization.
    * In this example, we need to make an output work buffer of the right size.
    */
    /* JSAMPLEs per row in output buffer */
    row_stride = cinfo.output_width * cinfo.output_components;
    JSAMPROW rowData;
    unsigned char* imageData = new unsigned char[cinfo.output_height * row_stride];
    /* Step 6: while (scan lines remain to be read) */
    /*           jpeg_read_scanlines(...); */

    /* Here we use the library's state variable cinfo.output_scanline as the
    * loop counter, so that we don't have to keep track ourselves.
    */
    __android_log_write(ANDROID_LOG_INFO, "JNI", "Made it to read lines");
    int row = 0;
    while (cinfo.output_scanline < cinfo.output_height)
    {
        rowData = imageData + (row * row_stride);
        jpeg_read_scanlines(&cinfo, &rowData, 1);
        ++row;
    }

    /* Step 7: Finish decompression */

    (void) jpeg_finish_decompress(&cinfo);
    /* We can ignore the return value since suspension is not possible
    * with the stdio data source.
    */

    /* Step 8: Release JPEG decompression object */

    /* This is an important step since it will release a good deal of memory. */
    jpeg_destroy_decompress(&cinfo);

    /* At this point you may want to check to see whether any corrupt-data
    * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
    */

    /* And we're done! */
    return imageData;
}

The image I'm reading loads fine in existing readers. I'm guessing I'm missing some sort of decompress setting, though I though it would be getting those from the header.

Garbled Image

Anthony
  • 7,638
  • 3
  • 38
  • 71
  • 2
    Seems to me that there's some problem with the row stride calculation. Can you debug to see that it's okay? Both in the reading and display functions. – Some programmer dude Oct 23 '13 at 13:28
  • Yah that would make sense, but I logged the the input throughout the program and it worked. Additionally just to check I bumped up the width and got a segment fault, so it is the right size. – Anthony Oct 23 '13 at 19:15
  • Actually this appears to be correct, the cinfo.output_width seems to be off by 18 pixels in every image I've looked at... – Anthony Oct 24 '13 at 07:09
  • Correction, it varies by image dimensions in the range of 18 pixels. The height is also incorrect. Basically the values in cinfo are wrong for some reason... – Anthony Oct 24 '13 at 07:19

1 Answers1

1

I faced similar problem but then later figured out that you have to align the RGB array to 8 byte boundary. check http://atlc.sourceforge.net/bmp.html#_toc381201083

madrag
  • 1,685
  • 1
  • 9
  • 4
  • Thanks for the input, could you go into a little more detail? Taught myself code, so the intricacies of memory management are lost on me at times. – Anthony Oct 23 '13 at 18:57
  • The code provided [here] (http://imagetools.codeplex.com/SourceControl/latest#src/ImageTools/ImageTools.IO.Png/PngDecoder.cs) should help. the function to look out is `CalculateScanlineLength`. You have to align the row length buffer to 8 byte boundary by padding it with zeros. for example, your image is RGB with 10 pixel width, then each row should be allocated with 3*10+(8-((3*10)%8))=32 bytes so if the image is 10x10 pixel then you need 32*10 buffer. – madrag Oct 23 '13 at 23:24
  • 1
    `row_stride = cinfo.output_width * cinfo.output_components; row_stride += (8-(row_stride%8));` should fix the code. also dont forget to zeromemory the `imageData` – madrag Oct 23 '13 at 23:34
  • The suggested line did move the pixels, but just a different misalignment. It's definitely related to the row_stride. When you say zeromemory, do you mean free? – Anthony Oct 24 '13 at 04:28
  • zeromemory is initializing memory with zeros before using it. you have to use `memset` to zeromemory. in the above case you have to call `memset ((void*)image Data, 0, cinfo.output_height * row_stride);` after the new. – madrag Oct 24 '13 at 06:14