bioformats
bioformats copied to clipboard
Efficiency of Jpeg compression in TiffSaver
The jpeg compression does not write blocks of 8x8 pixels and can be improved.
See comment in https://github.com/ome/ome-codecs/pull/29#discussion_r1270301260: "... Tif images saved with JPEG compression (at high quality settings) are larger than the original uncompressed image. ..."
I tested different combinations of JPEG quality, interleaved mode and chroma subsampling and found that the size of the saved tiff files was larger than the original in almost all combinations.
To test a possible improvement, I made a change in loci.formats.tiff.TiffSaver in the writeImage() function. My change forces 8 lines per strip when saving, resulting in jpeg compression in blocks of 8x8 pixels (what is normally used in jpeg). I have tested the different combinations with this modified code. Here is a comparison of the file size in bytes. (All files that are larger than the original are marked in bold.) File used for testing: lena-std.tif (786572 bytes) (available at e.g. https://imagej.nih.gov/ij/images/)
interleaved | jpeg-quality | chroma-subsampling | Unmodified TiffSaver | Modified TiffSaver |
---|---|---|---|---|
no | 0.95 | no | 1668225 | 364087 |
no | 0.95 | yes | 1668225 | 364087 |
no | 0.7 | no | 1121273 | 169081 |
no | 0.7 | yes | 1121273 | 169081 |
yes | 0.95 | no | 1083658 | 202473 |
yes | 0.95 | yes | 914820 | 163099 |
yes | 0.7 | no | 625754 | 89538 |
yes | 0.7 | wC | 599917 | 85053 |
This test clearly shows that the storage of jpeg-compressed tiff files can be improved.
My code change is NOT as a solution. It was only for my testing and is meant as a hint.
I added a short statement in https://github.com/ome/bioformats/blob/d566f70845be514ab9c88d910ae42ab7a5f0bac0/components/formats-bsd/src/loci/formats/tiff/TiffSaver.java#L313C7-L318C64
Original code
...
compression = ifd.getCompression();
tileWidth = (int) ifd.getTileWidth();
tileHeight = (int) ifd.getTileLength();
int tilesPerRow = (int) ifd.getTilesPerRow();
int rowsPerStrip = (int) ifd.getRowsPerStrip()[0];
int stripSize = rowsPerStrip * tileWidth * bytesPerPixel;
...
My version
...
compression = ifd.getCompression();
if (compression.getCodecName().equals("JPEG"))
ifd.putIFDValue(IFD.ROWS_PER_STRIP, new long[] {8});
tileWidth = (int) ifd.getTileWidth();
tileHeight = (int) ifd.getTileLength();
int tilesPerRow = (int) ifd.getTilesPerRow();
int rowsPerStrip = (int) ifd.getRowsPerStrip()[0];
int stripSize = rowsPerStrip * tileWidth * bytesPerPixel;
...