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

Efficiency of Jpeg compression in TiffSaver #4140

Open
iwbh15 opened this issue Jan 12, 2024 · 0 comments
Open

Efficiency of Jpeg compression in TiffSaver #4140

iwbh15 opened this issue Jan 12, 2024 · 0 comments

Comments

@iwbh15
Copy link

iwbh15 commented Jan 12, 2024

The jpeg compression does not write blocks of 8x8 pixels and can be improved.

See comment in ome/ome-codecs#29 (comment):
"... 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;
        ...
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

1 participant