Question

I use jpeg library v8d from Independent JPEG Group and I want to change the way jpeg decompression reads and processes data.

In the djpeg main(), only one scanline/row at a time is read and processed in each jpeg_read_scanlines() call. So, to read entire image this functions is called until all lines are read and processed:

  while (cinfo.output_scanline < cinfo.output_height) { 
    num_scanlines = jpeg_read_scanlines(&cinfo, dest_mgr->buffer,
                    dest_mgr->buffer_height); //read and process
    (*dest_mgr->put_pixel_rows) (&cinfo, dest_mgr, num_scanlines); //write to file
  }

But I would like to read the entire image once and store it in the memory and then process the entire image from memory. By reading libjpeg.txt, I found out this is possible: "You can process an entire image in one call if you have it all in memory, but usually it's simplest to process one scanline at a time."

Even though I made some progress, I couldn't make it completely work. I can now read a couple of rows once by increasing pub.buffer_height value and pub.buffer size, but no matter how large pub.buffer_height and pub.buffer are, only a couple of lines are read in each jpeg_read_scanlines() call. Any thoughts on this?

Was it helpful?

Solution

only a couple of lines are read in each jpeg_read_scanlines()

Yes, so you call it in a loop. Here's a loop that grabs one scanline at a time:

unsigned char *rowp[1], *pixdata = ...;
unsigned rowbytes = ..., height = ...;

while (cinfo.output_scanline < height) {
    rowp[0] = pixdata + cinfo.output_scanline * rowbytes;
    jpeg_read_scanlines(&cinfo, rowp, 1);
}

Once the loop exits, you have the entire image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top