Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PNG export: finalize support for "transparent color" detection...
...when "automatic" output settings are used. (Manual output settings will require additional changes tbd.) With this, I believe PD now boasts the most comprehensive automatic PNG feature detector on the planet. This was a large (and ugly) project and the code likely needs to be refactored to reduce some extremely large functions, but at least everything is working well. Why all this effort? PNGs support multiple mechanisms for storing transparency data. Some color depths support embedding full alpha channels (e.g. "Grayscale + Alpha" and "TrueColor + Alpha" color modes). Other color depths support flagging a single color or luminance value as transparent (e.g. "Grayscale" and "TrueColor" modes can store transparency data this way, allowing for GIF-like transparency in an 8- or 24-bit image). While it's tedious to account for, using this feature can trim down PNG sizes on compatible images. Note also that still other color modes allow you to assign per-index transparency values (e.g. "Palette" color mode allows this, so you can use up to 256 combinations of RGBA data, where "a" can be "any value" - pngquant relies on this to produce its images). PD now performs comprehensive analysis on an image before exporting it, with the goal of determining which combination of features will produce the most compact PNG file. If it can get away with using a palette or a transparent color or a special color mode like grayscale, it will, in any combination. There are a ton of edge-cases to cover, including esoteric ones like "if an image uses all 16.7 million possible colors, *and* it has transparency data, don't use a transparent color flag because no colors are available for flagging" - but I'm pretty sure PD covers those cases completely. The edge-cases are particularly obnoxious for grayscale modes, as 1/2/4/8-bit all have different requirements when trying to figure out if any unused shades are available for use as transparent color flags... but even monochrome images with alpha data should now be handled "ideally", with grayscale+alpha mode only being activated if absolutely necessary due to alpha channel complexity or a lack of unused gray values. This commit also includes a large reorganization of PNG-specific export code which previously resided in the ExportPNG function. The old organization required a ton of extra parameters to be passed between that function and the pdPNG class, and now that we no longer require FreeImage for PNG import/export, it made sense to migrate that functionality directly into pdPNG. (This also allows me to remove a bunch of PNG-specific code from PD's existing "autodetect color mode/depth" export function, and optimize some pathways for PNG needs specifically.)
- Loading branch information