Lymphatus / caesium-image-compressor

Caesium is an image compression software that helps you store, send and share digital pictures, supporting JPG, PNG, WebP and TIFF formats. You can quickly reduce the file size (and resolution, if you want) by preserving the overall quality of the image.
https://saerasoft.com/caesium
GNU General Public License v3.0
3.47k stars 209 forks source link

about png compression algorithm #192

Closed medukia closed 1 year ago

medukia commented 1 year ago

I can tweak the compression level to my liking from 0 to 100 using slider (default is 80)

and I assume bit depth is adjusted (from 24 bits to 1 bit) according to colors required for each image to reduce file size.

but sometimes some PNGs are not optimized so well and many details are just omitted. (like complicated photo of nature or street requiring more than 256 colors)

For scanned text or graphs or line arts, this might not be a problem, in that they don't use many colors that even PNG 4 or 8 should suffice and provides almost (or exactly) lossless compression.

how can I make sure the Caesium is smart enough to know how many colors for each image are used and set the best bit depth respectively, to avoid losing details?

checking lossless option isn't the solution because it will make all PNGs 24 bits and it's a huge waste for not color-demanding images like text or graphs requiring only 2 to 4 bits of colors.

Lymphatus commented 1 year ago

I think there's room for improvement for the PNG compression algorithms and I can tweak it to be a little bit smarter. Do you have an example of a complex PNG losing details on compression? I'd like to run some tests on it.

medukia commented 1 year ago

sure. here are examples I think will help.

colorful-original-32 original, 32bits colorful-saesium-compressed-8 compressed, at 90%, 8bits poem-original-32 original, 32bits poem-saesium-8bit compressed, at 90%, 8bits poem-saesium-2bit compressed, at 1%, 2bits

You can notice the nature photo's details of gradient background out of focus are heavily damaged, so are the butterflies' red wings. On the other hand, you can in no way notice any difference between 32bit and 8bit scanned document images although zooming in at 300% or something. and 2bit document still provides quite good quality, hard to notice loss in naked eyes.

So the problem is, in short, that Caesium has difficulty in detecting the type of each images and numbers of colors used to pick the best bit depth, to get the best quality/size ratios. Different type of images gets different compression efficiency at same quality level, which will make it hard to get the expected overall consistency in same settings profile.

Lymphatus commented 1 year ago

I think it's better to use the lossless flag for every PNG that has a lot of details/colors, like the first you provided. Running with the quality slider will always decrement the colors and can be an hard hit on quality. That's how the algorithms are tweaked and I think there's no much room for improvement at the moment**. The "lossy" PNG option is great for images like the second example, where colors are very few.

Another option might be to convert the first example image to something like WebP, which should preserve quality and make it smaller.

** I know I said the opposite on the other comment, but I ran some tests and I could not make much improvements.