Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mozjpeg doesn't honour non-optimized coding mode of libtiff #398

Open
rouault opened this issue Jun 17, 2021 · 2 comments
Open

mozjpeg doesn't honour non-optimized coding mode of libtiff #398

rouault opened this issue Jun 17, 2021 · 2 comments

Comments

@rouault
Copy link
Contributor

rouault commented Jun 17, 2021

It has been found that in the default mode of libtiff for JPEG compression, that uses default Huffman tables stored in the JpegTables TIFF tag (more details about that tag can be found in the "JPEGTables field" paragraph of https://www.awaresystems.be/imaging/tiff/specification/TIFFTechNote2.txt), and use non-optimized coding for the TIFF tile/strips, mozjpeg still emits Huffman tables in the TIFF tile/strips, which breaks the decoding

This can be reproduced when building libtiff master (https://gitlab.com/libtiff/libtiff) against mozjpeg, after having removed the if () block at https://gitlab.com/libtiff/libtiff/-/blob/master/libtiff/tif_jpeg.c#L1908 (or just removing the line that does sp->jpegtablesmode &= ~JPEGTABLESMODE_HUFF;), and running tiffcp -c jpeg -t in.tif out.tif on the attached file. The bottom of the resulting file will be corrupted when decoding.
in.tif.zip
corrupted_out.tif.zip

libtiff works fine with libjpeg-turbo, so this is a mozjpeg specific behaviour. A workaround has been implemented in https://gitlab.com/libtiff/libtiff/-/merge_requests/254 to make libtiff use optimized coding systemetically, but this isn't fully satisfactory as the user has normally the control on if he wants optimized coding or default Huffman tables.

@kornelski
Copy link
Member

That's a very unfortunate conflict of requirements. I know MozJPEG is supposed to be a drop-in replacement for libjpeg, but OTOH disabling such an essential optimization ruins compression, and defeats the whole point of using MozJPEG.

Is the absence of custom Huffman tables a requirement of the TIFF standard, or is it just a feature in libtiff?

@rouault
Copy link
Contributor Author

rouault commented Jun 17, 2021

Is the absence of custom Huffman tables a requirement of the TIFF standard, or is it just a feature in libtiff?

The TIFF standard allow both custom Huffman tables or standard. The default of libtiff is to use standard, but the user can decide otherwise (the GDAL library that leverages libtiff for example has changed the default behaviour to use custom Huffman tables, and thus isn't affected by this issue in its default mode)

(Another issue with mozjpeg was that it defaults to use progressive scans, which is disallowed by the TIFF standard, but I could change that easily be setting num_scans = 0 and scan_info = NULL, so there is no longer any issue regarding that point)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants