ome / ome-codecs

OME image compression (coder-decoder implementations)
Other
4 stars 13 forks source link

Disable chroma subsampling in JPEGCodec #29

Closed iwbh15 closed 1 year ago

iwbh15 commented 1 year ago

Follow up to PR https://github.com/ome/ome-codecs/pull/26

see https://github.com/ome/ome-codecs/pull/26#issuecomment-1456675017 :

There is another option that has a decisive influence on the Jpeg image quality: 'Chroma Subsampling'. Especially with biological/microscopic images in RGB format (transmitted light or fluorescence) chroma subsampling leads to intolerable image errors. Chroma subsampling should therefore - in my view - be deactivatable in libraries like BioFormats.

Unfortunately, access to the 'Chroma Sampling' property is not configurable via the 'ImageWriteParam' (like e.g. the JpegQuality). Instead, it can be accessed via the 'IIOMetadata'.

Unfortunately, so far have not been able to find a direct and simple description. But it seems to exist in some form in the "JPEG Metadata Format Specification and Usage Notes".

In ImageJ, access to the 'Chroma Sampling' property is already integrated. see https://imagej.nih.gov/ij/notes.html 1.52s 10 December 2019 Thanks to Peter Haub, chroma subsampling is disabled when saving in JPEG format if the Quality setting (Edit>Options>Input/Output) is 90 or higher, resulting in higher quality images but larger file sizes.

For example, chroma subsampling can be disabled with the following macro command: JpegWriter.disableChromaSubsampling(true); By default, 'Chroma Sampling' is disabled in ImageJ when JpegQuality >= 90.

My source at that time was Apache/Shindig. See: https://github.com/imagej/ImageJ/blob/d82104f1c55290af56d5afc29004cf8298055067/ij/plugin/JpegWriter.java#L86 // Disable JPEG chroma subsampling // http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/BaseOptimizer.java // http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/JpegImageUtils.java

There is a test script from Wayne here https://imagej.nih.gov/ij/macros/js/JpegQualityTests.js . It can be called in the ImageJ menu under: Help>Examples>Java Scripts> JPEG Quality Plot

I think it would be worth and important to have the 'Chroma Sampling' property in mind, especially with the current code changes to support variable Jpeg quality.

Unfortunately I lack the overview of ome-codecs to elaborate this hint more concretely.

Here are some further and interesting links: _https://stackoverflow.com/questions/14149739/disable-java-imageio-chroma-subsampling https://stackoverflow.com/questions/72581932/why-is-the-color-of-my-image-changed-after-writing-it-as-a-jpg-file https://github.com/libvips/libvips/issues/538 http://poynton.ca/PDFs/Chroma_subsampling_notation.pdf_

iwbh15 commented 1 year ago

Important Note: The change made in ome.codecs.CodecOptions.java must also be inserted into loci.formats.codec.CodecOptions. Can somebody please create this change in bioformats.

This is because loci.formats.codec.CodecOptions will be wrapped to ome.codecs.CodecOptions.java in WrappedCodec.java (see https://github.com/ome/bioformats/blob/971fb341745e5c7e4b0cbf8c2af59deacc73db75/components/formats-bsd/src/loci/formats/codec/WrappedCodec.java#L139) (called from TiffCompression.java, see https://github.com/ome/bioformats/blob/971fb341745e5c7e4b0cbf8c2af59deacc73db75/components/formats-bsd/src/loci/formats/tiff/TiffCompression.java#L360)

dgault commented 1 year ago

Thanks @iwbh15 for getting this PR opened, we are currently preparing a Bio-Formats 6.14.0 release for next week. Once that is out we we will aim to get this reviewed and tested for a Bio-Formats 7.0.0 which will follow shortly after.