Bug with png images in grayscale + alpha
| From: | Daniel Diaz | Date: | Thu, 07 Jun 2007 15:54:47 +0000 |
| Subject: | Bug with png images in grayscale + alpha | ||
| Groups: | php.gd.devel php.gd.bugs | ||
| Request: | Send a blank email to php-gd-devel+get-199@lists.php.net to get a copy of this message | ||
Hi,
First, thanks a lot for this wonderful library.
There is a bug in gd_png.c when dealing with a png in grayscale + alpha (color_type == PNG_COLOR_TYPE_GRAY_ALPHA). Tested version is last CVS (gd_png.c v 1.23).
The existing does not make a difference between PNG_COLOR_TYPE_GRAY and PNG_COLOR_TYPE_GRAY_ALPHA. In the PNG_COLOR_TYPE_GRAY_ALPHA mode, row_pointers contain 2 bytes par pixel, (rowbytes = 2 *width) but existing code simply uses the first width bytes (forgetting the width next others). The result is a wrong image : only the left half is loaded in memory (but with wrong gray colors).
I attach an image.png file in this format. If you use:
pngtogd image.png foo.gd
gdtopng foo.gd new-image.png
you will see that new-mage1.png is wrong (see attached file).
A preliminar solution consists in adding the following code to gd_png.c:415 (just before the default:)
case PNG_COLOR_TYPE_GRAY_ALPHA:
for (h = 0; h < height; ++h) {
int boffset = 0;
for (w = 0; w < width; ++w) {
register png_byte idx = row_pointers[h][boffset++];
im->pixels[h][w] = idx;
open[idx] = 0;
// add code to deal with alpha at row_pointer[h][boffset]
// for now we simply ignore it !
boffset++;
}
}
break;
Obviously I here simply ignore alpha bytes (I don't know how alpha channel is handled in libgd, sorry).
DD