tags:

views:

45

answers:

1

Using libjpeg, if possible, I would like to read a row from the middle of a JPEG image without reading all the preceding rows. Can this be done?

+1  A: 

The answer is almost certainly "yes you can, but it will take more effort than you want".

A JPEG image is a stream of markers that contain either information global to the whole compressed image, or information related to specific portions of the image. The compression works by breaking the image into color planes, possibly changing color spaces to one where the color information can be down-sampled, and within each plane operating on 8x8 pixel blocks.

For instance, it is possible to rotate a compressed image by 90 degrees if it is sized such that it is made up of only whole blocks by only transposing the basic blocks and the coefficients inside each block; i.e. without uncompressing, rotating the real image, and recompressing.

Given that, your approach would be to parse the marker stream on the way into the library, passing all the markers that are global to the image, modifying any related to image size, and dropping markers containing coefficients that lie outside your cropping rectangle.

You will likely need to further crop the result if the restriction of cropping to complete basic blocks is too coarse.

What isn't clear to me is whether there is any real win over the alternative, which is to crop the results as it comes out of the library. The library is highly configurable, so you can provide an uncompressed data consumer function that discards all pixels outside your cropping rectangle and only saves pixels you want to keep.

RBerteig
Thanks for your detailed remarks. I think you're right that there would be little advantage over simply processing the output of libjpeg. What I originally had in mind was to keep the image on disk, then load and draw only particular tiles. In the end, I think a better way to do this will be simply to process the source file one row at a time, produce cached tiles on disk, and load these as needed.
Michael Cooper
@Michael Cooper, that makes sense to me. You could use a multi-resolution pyramid, and fill the tiles in on demand, caching back to disk. I believe that is essentially what Adobe Light Room is doing in its thumbnail cache, for instance, with each resolution in a separate file.
RBerteig