-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
6
-
x86
-
windows_xp
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When writing a specific BufferedImage with a 1 bit indexed color model, the ImageIO class writes a file that it fails to read.
I noticed that Microsoft Word cannot read the the procuded PNG files, so I suggest that the problem is rather in the ImageIO.write() method than in the read() method.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the source code below and see the exception. The code simply creates a buffered image, writes it into a byte array and tries to read it back from the byte array.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
ImageIO should only write files that it can also read back. The code should run without problems, and the read image should give the same result when displayed as the original image.
ACTUAL -
ImageIO.read() fails with a javax.imageio.IIOException.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
javax.imageio.IIOException: I/O error reading PNG header!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:297)
at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:621)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1194)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1467)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.incors.swing.text.ImageIOTest.main(ImageIOTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: javax.imageio.IIOException: Bad color type/bit depth combination!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:275)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.imageio.ImageIO;
import java.awt.image.IndexColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
*/
public class ImageIOTest {
public static void main(String[] args) {
try {
IndexColorModel colorModel = new IndexColorModel(1, 2, new int[]{-16777216, 16777215}, 0, true, 1, DataBuffer.TYPE_BYTE);
BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_BINARY, colorModel);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ImageIO.write(image1, "png", byteOut);
byte[] bytes = byteOut.toByteArray();
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
Image image2 = ImageIO.read(byteIn);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Convert the image to a different color model and write the converted image.
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
When writing a specific BufferedImage with a 1 bit indexed color model, the ImageIO class writes a file that it fails to read.
I noticed that Microsoft Word cannot read the the procuded PNG files, so I suggest that the problem is rather in the ImageIO.write() method than in the read() method.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the source code below and see the exception. The code simply creates a buffered image, writes it into a byte array and tries to read it back from the byte array.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
ImageIO should only write files that it can also read back. The code should run without problems, and the read image should give the same result when displayed as the original image.
ACTUAL -
ImageIO.read() fails with a javax.imageio.IIOException.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
javax.imageio.IIOException: I/O error reading PNG header!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:297)
at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:621)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1194)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1467)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.incors.swing.text.ImageIOTest.main(ImageIOTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: javax.imageio.IIOException: Bad color type/bit depth combination!
at com.sun.imageio.plugins.png.PNGImageReader.readHeader(PNGImageReader.java:275)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.imageio.ImageIO;
import java.awt.image.IndexColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
*/
public class ImageIOTest {
public static void main(String[] args) {
try {
IndexColorModel colorModel = new IndexColorModel(1, 2, new int[]{-16777216, 16777215}, 0, true, 1, DataBuffer.TYPE_BYTE);
BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_BINARY, colorModel);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ImageIO.write(image1, "png", byteOut);
byte[] bytes = byteOut.toByteArray();
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
Image image2 = ImageIO.read(byteIn);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Convert the image to a different color model and write the converted image.