I am trying to load a PNG image using the javax.imageio.ImageIO.read() method. However, I want the resulting type to be "BufferedImage.TYPE_4BYTE_ABGR", but it ends up as an indexed image ("BufferedImage.TYPE_BYTE_INDEXED"). Is there any way to load an image as unindexed, when the original image is indexed? There are about 120 images, so it would take too long to make them all unindexed by hand.
Asked
Active
Viewed 1,964 times
0
-
's problem occured when he was trying to copy one type of PNG to another. An example of the code he previously presented can be found here [link](http://stackoverflow.com/questions/5685803/java-exception-exception-in-thread-main-java-lang-classcastexception-b-canno/5686068#5686068) – Boro Apr 16 '11 at 13:23
1 Answers
0
If you're not opposed to using JAI, you can create a rendering chain for the RenderedImage (BufferedImage implements the interface) and add a format operation to the chain:
JAI.create("format",...) operation with a rendering hint with a key of JAI.KEY_REPLACE_INDEX_COLOR_MODEL.
A pure-ImageIO approach would be to create a new BufferedImage of the type you want and draw the one loaded from ImageIO.read into the new BufferedImage:
BufferedImage image = ImageIO.read(inputFile);
BufferedImage convertedImage = new BufferedImage(image.getWidth(),
image.getHeight(), BufferedImage.TYPE_4BYTE_ABRG);
convertedImage.createGraphics().drawRenderedImage(image, null);

Jeff
- 3,669
- 1
- 23
- 33