-
-
Notifications
You must be signed in to change notification settings - Fork 298
Description
Currently, all PNG images are saved using the same bit depth -- 8-bit by default, or based on the profile once PR #976 is merged. This approach doesn't always result in optimal file sizes.
It would be great to implement smarter per-image bit-depth detection. Some ideas:
- Pure single color images can often be stored as 1-bit without any quality loss.
- Low-detail/filler pages can often be reduced to 1-bit or 2-bit.
- Simple grayscale or limited-color artwork can benefit from palette mode, resulting in smaller files without color/gray shade loss.
At first glance, this seems easy, just check the number of shades used. If an image has fewer than 4 colors or grayscale levels, it should be safe to save it at a matching lower bit depth.
However, in practice, we can’t always trust the apparent bit depth. For example, some images look like they only use 2 colors, but due to anti-aliasing (especially on text), they actually use 256 shades of gray. These images don't need to be stored as full 8-bit grayscale images. 2-bit (4 shades) is often more than enough to retain text smoothness and readability while reducing size.
It's also important to note that not all source images that are genuinely 1-bit should remain 1-bit after processing. Typically, we resize images to match the resolution of the target reading device. resizing it to match the target device resolution can destroy visual detail if it's saved again as 1-bit, especially for high-resolution sources. For example, resizing a 3000×4500 1-bit image to 1000×1500 and saving it again as 1-bit can result in a significant quality drop. 2-bit helps somewhat, but 4-bit tends to preserve visual fidelity much better, especially for actual artwork.
Saving pure color pages in 1-bit is straightforward. The tricky part is accurately detecting when a high-resolution image with only 2 or 4 shades still contain important visual details that could be lost during resizing, as well as determining the optimal bit depth for source images in 8-bit. If anyone has ideas on how to improve detection in these cases, please feel free to share!