libpng
libpng copied to clipboard
Fixes pertaining background color and grayscale conversions
After adding libpng support to my image library, I wrote some test code which generated PNG files with all valid combinations of relevant parameters, and asked libpng to parse each into every compatible format supported by my image library. Some of these combinations produced unexpected results, which seem to be caused by bugs in libpng.
This pull request includes two patches which attempt to rectify these bugs. Please see the individual commits' commit messages for details.
FWIW - the source code for my test suite that identified this bug is here: https://github.com/CyberShadow/ae/blob/master/utils/graphics/libpng.d
Thank you for your use and contribution to libpng.
Yes; the transforms do not always compose correctly. I identified a number of cases (I'm not sure if this is one, probably not because memory format conversions are not a high priority).
The problem is that it is whack-a-mole with the 1.6 (and earlier) implementation. I tried to fix this in 1.7 and believe I got it all right but that's my version of 1.7; this (the transform code) is one place where a complete rewrite is in order (that's how I fixed it.)
As a result it's impossible to review a fix like this as just a one file change (to pngrtran.c). The first step is to come up with a test case using pngstest - i.e. a set of arguments to pngstest that demonstrate the problem or, possibly, since pngstest has no support at present for the swap transforms, to implement a new test program.
Without a test program accepting such a patch is inadvisable. Maybe in a major release if someone is committed to testing everything.
A better patch for libpng 1.6 is to detect the incompatible transforms when they are set and simply png_error out; that is way better than producing a completely valid PNG full of garbage. Clearly since this bug has not been widely reported people who encounter it have already worked round it by doing one or other of the composed transforms themselves.