tannerhelland / PhotoDemon

A free portable photo editor focused on pro-grade features, high performance, and maximum usability.
https://photodemon.org
Other
1.38k stars 201 forks source link

[BUG] - Low bit dithered images when exported are larger than their higher bit depth counterparts #562

Closed pixelglade closed 4 months ago

pixelglade commented 4 months ago

Photodemon 9.0, Windows 11

If I open an image exported from another program as a PNG format with the bit depth listed in the Windows Property menu as 8, then dither it with a 16 colour palette using the Map > Palette Map feature and select "restrict palette" in the export dialogue, as either a PNG or GIF the file will end up exporting as larger than the original file although the bit depth is now listed as 4 in the Properties Dialogue when nothing else (file dimensions) has changed.

In general, I have noticed that dithered images don't end up noticeably smaller in size despite this being the one of the primary functions of dithering to reduce colour depth and reduce file size. What am I doing wrong or what is happening behind the scenes to cause the file size to increase instead of reduce?

tannerhelland commented 4 months ago

Hello @pixelglade . Thank you for your comments.

So there are two different ideas being mixed together here:

1) Reducing color depth (count) in an image. 2) Dithering.

Reducing color count in an image will usually - but not always - produce a smaller image file. PNG doesn't actually encode colors directly. Instead, it "pre-filters" them to improve compression. There are different filtering strategies (as you can see in the export options), but by doing things like saving the difference between neighboring pixels instead of raw pixel values, an image may compress relatively equally regardless of its bit-depth.

Dithering, however, almost always makes compression worse. Dithering uses pseudo-random patterns of limited colors to try and imitate a wider variety of colors. Pseudo-random patterns have high entropy and do not compress well. (Ordered dither settings will typically compress better, because of their repeating patterns, but they also make dithering that looks much more artificial.)

If you reduce a PNGs color depth without using dithering, you will likely get the smallest possible file. You might also try using a higher color count than 4-bits per pixel - for example, reducing 256 colors to perhaps 128 or 64 without dithering - to see if that shrinks file size meaningfully without impacting image quality.

Hope that helps!

pixelglade commented 4 months ago

That explanation does help me understand things a bit better. Thanks!