0

In Java, is it expected to be able to have a BufferedImage such that getColorModel().hasAlpha() will return true, but getAlphaRaster() will return null?

I ask because there's a library I'm using (PDFBox specifically, in the PDJpeg class) that breaks on such an image.

In this particular case I'm creating the image very simply using:

BufferedImage bi = ImageIO.read(new FileInputStream("/Users/dan/Downloads/test.png"));

I've attached the particular image that's failing for me below this question.

Is there some sort of parameter I can pass to ImageIO or some kind of transformation I can do to my BufferedImage after it's loaded so that it won't run into this problem?

I'm running Java 1.7.0_40 if it matters.

Stack trace for completeness:

java.lang.NullPointerException
    at java.awt.image.ComponentColorModel.isCompatibleRaster(ComponentColorModel.java:2787)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:629)
    at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.createImageStream(PDJpeg.java:159)
    at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.<init>(PDJpeg.java:133)

enter image description here

Dan
  • 10,990
  • 7
  • 51
  • 80
  • How does it fail? Where is the stack trace? – Harald K Dec 06 '13 at 20:03
  • The specific stack trace is internal to PDFBox so I didn't think it was applicable, but specifically it's a `NullPointerException` the root cause of which is that `bi.getColorModel().hasAlpha()` returns true but `bi.getAlphaRaster()` returns null. – Dan Dec 06 '13 at 21:15

1 Answers1

1

Yes. As the JavaDoc states:

This method assumes that for all ColorModel objects other than IndexColorModel, if the ColorModel supports alpha, there is a separate alpha channel which is stored as the last band of image data. If the image uses an IndexColorModel that has alpha in the lookup table, this method returns null since there is no spatially discrete alpha channel.

Your image is a palette PNG with a transparent index. ImageIO will read this into a BufferedImage with IndexColorModel (ie., no discrete alpha channel).

You can convert the image to a different BufferedImage type (like TYPE_INT_RGB), by creating a blank image of the same size, getting its graphics, and draw the original onto it:

BufferedImage origininal = ...;
BufferedImage copy = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);

Graphics2D g = copy.createGraphics();
try {
    g.drawImage(original, 0, 0, null);
}
finally {
    g.dispose();
}

You can probably also pass the image type as an ImageTypeSpecifier on the ImageReadParam passed to the ImageReader. But it requires quite a bit more code for the reading part.

Harald K
  • 26,314
  • 7
  • 65
  • 111
  • Thanks. This worked. I'm unfortunately far from an expert on image formats and processing. – Dan Dec 09 '13 at 21:00