JDK-6388546 : PNG with transparent background doesn't render correctly
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-02-21
  • Updated: 2011-01-19
  • Resolved: 2006-03-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b77Fixed
Related Reports
Relates :  
Description
/*

If you run this app with the attached PNG image ("test.png") in
the same directory you'll see that the lightbulb image's background
is white - not transparent.  However viewing the same image with
Microsoft's tools shows that the image has a transparent background.
As well it should, since it was saved that way using IrFanView 3.98,
a freeware image editor.

*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test {
   JFrame mainFrame = null;

   void initialize(String[] ignore) {
       Icon icon = new ImageIcon("test.png");
       mainFrame = new JFrame("Image should have transparent background");
       Container cp = mainFrame.getContentPane();
       cp.setBackground(Color.green);
       mainFrame.add(new JLabel(icon));
   }

   void show() {
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       mainFrame.pack();
       mainFrame.setVisible(true);
   }

   public static void main(final String[] args) throws Exception {
       Runnable doCreateAndShowGUI = new Runnable() {
           public void run() {
               try {
                   Test app = new Test();
                   app.initialize(args);
                   app.show();
               }
               catch (Exception e) {
                   // TBD log an error
               }
           }
       };
       SwingUtilities.invokeLater(doCreateAndShowGUI);
   }
}

Comments
EVALUATION Test image is 8bpp RGB image (png color type is 2) with tRNS chunk, that defines value of transparent pixel (the white, in case of test image). This image seem perfectly legal according to PNG spec and most of native apps display it correctly. In our case PNG decoder process tRNS chunk correctly but never uses calculated value of transparent color during decoding of RGB raster data. We need to be smarter and set alpha to max transparency for pixels with specified color. BTW, ImageIO is vulnerable to the same problem and even worse. However, fixing ImageIO problems will likely require much bigger change so we probably want to file separate bug on this.
22-02-2006