Bug with png images in grayscale + alpha

[email protected] (Daniel Diaz) Thu, 07 Jun 2007 17:54:47 +0200
Newsgroups php.gd.devel,php.gd.bugs
Message-ID <[email protected]>
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