[gdal-dev] lossless jpeg crop in gdal?

Even Rouault even.rouault at spatialys.com
Tue Mar 16 14:06:45 PDT 2021


Matt,

Le 16/03/2021 à 21:45, Matt.Wilkie at yukon.ca a écrit :
>
> I recently learned that *jpegtran* from the JPEG library has the 
> ability to crop and save a new jpeg losslessly:
>
>  [...]
>
> Can GDAL take advantage of this for jpeg-in-geotiff images?
>
to some limited extent, yes. I thought this was documented in 
https://gdal.org/drivers/raster/gtiff.html but I can't find it.

So the most relevant doc is from this comment of the GeoTIFF driver

     // When CreateCopy'ing() from a JPEG dataset, and asking for 
COMPRESS=JPEG,
     // use DCT coefficients (unless other options are incompatible, like
     // strip/tile dimensions, specifying JPEG_QUALITY option, incompatible
     // PHOTOMETRIC with the source colorspace, etc.) to avoid the lossy 
steps
     // involved by decompression/recompression.

Basically, you can do JPEG to JPEG-in-TIFF lossless transcoding if doing:

gdal_translate in.jpg out.tif -co COMPRESS=JPEG [[-co TILED=YES] [-co 
BLOCKXSIZE=...] [-co BLOCKYSIZE=...]]

where the block size should be a multiple of 16.

Most other gdal_translate generic option (like -projwin / -srcwin) or 
GTiff driver creation option are incompatible with the implementation of 
this special mode (subsetting could be in theory possible on boundaries 
of JPEG DCT blocks, but isn't implemented).

If adding "--debug on", you should see a line "GTiff: Using special copy 
mode from a JPEG dataset" when this lossless transcoding mode works. 
Otherwise the generic JPEG decoding / recoding is used.

JPEG-in-TIFF to JPEG-in-TIFF or JPEG lossless transcodingcould also be 
in theory supported, but isn't implemented . With modest effort though 
with some scripting it is possible to extract a JPEG file from a tiled 
JPEG-in-TIFF file by reassembling the TIFF tile content with the 
huffman/quantization tables in the JPEGTABLE TIFF tag.

If someone wants to propose a pull request with a doc addition to the 
GTiff driver doc with the above content, that'd be welcome.

Even

> *Matt Wilkie*
>
> Geomatics Analyst
>
> Environment | Technology, Innovation and Mapping
>
> T 867-667-8133 | _Yukon.ca <http://yukon.ca/>_
>
> /Hours: 08:30-16:30, Tue-Wed: Office, Thu-Fri: Remote./
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
http://www.spatialys.com
My software is free, but my time generally not.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20210316/fea85309/attachment.html>


More information about the gdal-dev mailing list