In com.lowagie.text.Image#getInstance(Image, Color, boolean), there is
code that tries to detect an image with binary transparency. It assumes
that if the only alpha values are 0 and 255, the color of the first
pixel with transparency 0 is fully transparent throughout the
image. While this works for GIFs and images with pre-multiplied alpha,
it is possible to have a PNG image where the rgb values of fully
transparent pixels differ.

I've attached a patch that ensures that the color values of all
transparent pixels are the same. If they differ, it falls back on using
a full alpha channel.

Thanks,
Chris



*** Image.java.~1.95.~  Wed Jan 26 23:10:50 2005
--- Image.java  Sun Jan 30 00:53:49 2005
***************
*** 576,593 ****
                  }
              }
              else {
                  smask = new byte[w * h];
                  boolean shades = false;
                  for (int j = 0; j < size; j++) {
                      byte alpha = smask[j] = (byte)((pixels[j] >> 24) & 0xff);
!                     if (alpha != 0 && alpha != -1)
!                         shades = true;
!                     if (transparency == null) {
!                         if (alpha == 0) {
!                             transparency = new int[6];
!                             transparency[0] = transparency[1] = (pixels[j] >> 
16) & 0xff;
!                             transparency[2] = transparency[3] = (pixels[j] >> 
8) & 0xff;
!                             transparency[4] = transparency[5] = pixels[j] & 
0xff;
                          }
                      }
                      pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);
--- 576,599 ----
                  }
              }
              else {
+                 int transparentPixel = 0;
                  smask = new byte[w * h];
                  boolean shades = false;
                  for (int j = 0; j < size; j++) {
                      byte alpha = smask[j] = (byte)((pixels[j] >> 24) & 0xff);
!                     if (!shades) {
!                         if (alpha != 0 || alpha != -1) {
!                             shades = true;
!                         } else if (transparency == null) {
!                             if (alpha == 0) {
!                                 transparentPixel = pixels[j] & 0xffffff;
!                                 transparency = new int[6];
!                                 transparency[0] = transparency[1] = 
(transparentPixel >> 16) & 0xff;
!                                 transparency[2] = transparency[3] = 
(transparentPixel >> 8) & 0xff;
!                                 transparency[4] = transparency[5] = 
transparentPixel & 0xff;
!                             }
!                         } else if ((pixels[j] & 0xffffff) != 
transparentPixel) {
!                             shades = true;
                          }
                      }
                      pixelsByte[index++] = (byte) ((pixels[j] >> 16) & 0xff);

Reply via email to