ome / ome-codecs

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

Setting jpeg quality in JPEGCodec #26

Closed iwbh15 closed 1 year ago

iwbh15 commented 1 year ago

As a simple way to make the jpeg quality property settable, e.g. for use in QuPath, the approach of this PR uses System.setProperty() and System.getProperty to support adjustable jpeg quality while avoiding passing the 'jpegquality' value from QuPath through the entire class hierarchy from QuPath to Bioformats to ome.codecs.

Related to: https://github.com/ome/bioformats/issues/3370

dgault commented 1 year ago

Thanks @iwbh15 for getting the PR open. I have tagged it for inclusion in our CI builds and tests, I will aim to get a full review of the PR next week.

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#376. See the console output for more details. Possible conflicts:

--conflicts

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#1525. See the console output for more details. Possible conflicts:

--conflicts

iwbh15 commented 1 year ago

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_

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#401. See the console output for more details. Possible conflicts:

--conflicts

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#1553. See the console output for more details. Possible conflicts:

--conflicts

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#402. See the console output for more details. Possible conflicts:

--conflicts

dgault commented 1 year ago

@iwbh15, agreed that having the option to disable the sub sampling similar to ImageJ seems like a good idea here. You should be able to reuse much of the same code from the ImageJ implementation in https://github.com/imagej/ImageJ/blob/master/ij/plugin/JpegWriter.java#L80-L123, perhaps pulling the core of it out into a separate method (https://github.com/ome/bioformats/blob/develop/components/formats-api/src/loci/formats/codec/CodecOptions.java)

Also to handle the configuration of this option it would also be best to do this through the CodecOptions. This would mean opening an accompanying PR to add an additional option and default value to https://github.com/ome/bioformats/blob/develop/components/formats-api/src/loci/formats/codec/CodecOptions.java

Perhaps the Chroma Subsampling could be a handled in a follow up PR after we have got these initial JPEG quality changes merged.

snoopycrimecop commented 1 year ago

Conflicting PR. Removed from build BIOFORMATS-push#403. See the console output for more details. Possible conflicts:

--conflicts Conflict resolved in build BIOFORMATS-push#431. See the console output for more details.

dgault commented 1 year ago

@iwbh15, I have merged in the changes to use CodecOptions and resolved some conflicts with the PR to now use memory cache (https://github.com/ome/ome-codecs/pull/26/). I will do some final testing with these changes and if all looks good we will try and get this merged for the next Bio-Formats release