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

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

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9, 10, 11
    • Fix Version/s: 11
    • Component/s: client-libs
    • Subcomponent:
    • Resolved In Build:
      b13
    • CPU:
      generic
    • OS:
      generic

      Description

      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 ----------

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: