Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6788458

PNGImageReader ignores tRNS chunk while reading non-indexed RGB/Gray images

XMLWordPrintable

    • b13
    • generic
    • generic

      A DESCRIPTION OF THE REQUEST :
      ImageIO.read() ignores the tRNS chunk in 8-bit PNGs which are not indexed. This means that loading a PNG of color type 2 which has a transparent color via ImageIO.read() will result in a BufferedImage which is opaque. According to the PNG standard, tRNS is an ancillary chunk, so a compliant PNG decoder is free to ignore it, which is why I'm not reporting this as an ImageIO bug.

      JUSTIFICATION :
      The PNG decoder used by Toolkit.createImage() does respect the tRNS chunk and produces an Image with the correct transparency, but it returns an Image which is not also a BufferedImage, which means we then have to spend extra time converting the image. ImageIO returns BufferedImages, so it would be nice if this kind of image could be decoded directly.

      Additionally, ImageIO is the only PNG decoder I've been able to find which doesn't honor the tRNS chunk, which means that this behavior, while not strictly speaking incorrect, is annoying and unexpected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The transparent pixels in the resulting image should be transparent.
      ACTUAL -
      The transparent pixels in the resulting image were opaque.

      ---------- BEGIN SOURCE ----------
      import javax.imageio.*;
      import javax.swing.*;
      import java.io.File;
      import java.awt.Toolkit;
      import java.net.URL;
      import java.awt.Image;
       
      public class Test {
        public static void main(final String[] args) throws Exception {
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              try {
                final Image img = ImageIO.read(new File(args[0]));
      // final Image img = Toolkit.getDefaultToolkit().createImage(new URL("file://" + args[0]));
       
                final JFrame frame = new JFrame();
                final JLabel label = new JLabel(new ImageIcon(img));
       
                frame.add(label);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
              }
              catch (Exception e) {
                e.printStackTrace();
              }
            }
          });
        }
      }
      ---------- END SOURCE ----------

            jdv Jayathirth D V
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: