"lossless" cropping with jpegtran

I have added a new option to jpegtran (part of the Independent JPEG Group's JPEG software) to cut regions out of an image without re-encoding the region. The region retains the exact same data it had in the main image with the restriction that the image can only be cropped along DCT block boundaries. My main use for this feature is modifying digital camera images which come out of the camera already JPEG compressed and need to be rotated and cropped for storage.

Usage is straightforward:

jpegtran -cut WxH+X+Y
Specify a region of the image by size and offset into the image. Negative offsets are relative to the lower and righthand sides. The resulting image will contain at least the named region (constrained to the image size), and may be larger to round up to the nearest DCT block boundary (or multiple thereof, depending on the sampling factors). If the image ends in a partial DCT block that part of the image will be lost, even though it would be possible to include it. Perhaps an enterprising reader will fix that deficiency.

Many thanks to Guido Vollbeding for implementing the lossless rotation in jpegtran. I use it regularly to rotate digital camera images and the code framework made the addition of cropping easy. His Online JPEG Lab will let you test-drive jpegtran from the comfort of your web browser.

Patches

Get a copy of the patches which apply to "release 6b-beta1 of 18-Oct-1997" of the JPEG software. They are small and should be easily applied by hand if you have trouble with the diff.

If you find it more convenient you can download a tarfile of the modified source files.

Future Work and Update

Guido Vollbeding and Tom Lane have reworked my patches extensively for a future release of the IJG code. You can see Guido's enhancements (including a lossless cut & paste) at his extensions of jpegtran page.