An attempt to ImageIO.write from a BufferedImage with type TYPE_USHORT_565_RGB
fails with this exception:
While writing image ./output-u565_rgb.png, caught java.lang.RuntimeException
java.lang.RuntimeException
at com.sun.imageio.plugins.png.PNGMetadata.initialize(PNGMetadata.java:252)
at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:788)
at javax.imageio.ImageWriter.write(ImageWriter.java:591)
at javax.imageio.ImageIO.write(ImageIO.java:1179)
at javax.imageio.ImageIO.write(ImageIO.java:1213)
at headless.bit.imgtest.writeImage(imgtest.java:81)
at headless.bit.imgtest.runTest(imgtest.java:58)
at headless.bit.imgtest.main(imgtest.java:117)
Using all other image types the image is produced, however there are
drawing mistakes in two image types:
TYPE_BYTE_BINARY There is a black line down the right side
of the image.
TYPE_USHORT_555_RGB The colors are completely wrong. Instead
of white and red, it's two shades of dark grey.
The rendering into the BufferedImage is a simple fill, first
filling the background with white, then filling a square in the
middle with red.
The behavior is the same on Solaris 8 and Windows 2k
Test code follows:
/*
* imgtest.java
*
* Created on February 7, 2001, 4:49 PM
*/
// package headless.bit;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
/**
*
* @author David Herron
* @version
*/
public class imgtest extends java.lang.Object {
protected BufferedImage fb = null;
protected int width = 500;
protected int height = 500;
int[] imageTypes = new int[] {
BufferedImage.TYPE_3BYTE_BGR,
BufferedImage.TYPE_4BYTE_ABGR,
BufferedImage.TYPE_BYTE_BINARY,
BufferedImage.TYPE_BYTE_GRAY,
BufferedImage.TYPE_BYTE_INDEXED,
// XXX BufferedImage.TYPE_CUSTOM
BufferedImage.TYPE_INT_ARGB,
BufferedImage.TYPE_INT_ARGB_PRE,
BufferedImage.TYPE_INT_BGR,
BufferedImage.TYPE_INT_RGB,
BufferedImage.TYPE_USHORT_555_RGB,
BufferedImage.TYPE_USHORT_565_RGB,
BufferedImage.TYPE_USHORT_GRAY
};
/** Creates new imgtest */
public imgtest(String args[]) {
}
public void runTest() {
try {
// Construct the BufferedImage, and run the test
for (int itype = 0; itype < imageTypes.length; itype++) {
int type = imageTypes[itype];
if (fb != null) {
fb.flush();
fb = null;
}
fb = new BufferedImage(width, height, type);
render(width, height, (Graphics2D)fb.getGraphics());
writeImage();
}
} catch (Exception e) { }
}
public void render(int w, int h, Graphics2D g2) {
g2.setColor(Color.white);
g2.fillRect(0, 0, w, h);
g2.setColor(Color.red);
g2.fillRect((int)((w * .25)/2),
(int)((h * .25)/2),
(int)(w * .75),
(int)(h * .75));
}
public void writeImage() {
// Write the image out to PNG file
String ifn = null;
try {
String dn = ".";
ifn = dn+"/output-"+getImageTypeString()+".png";
System.out.println("writeImageFromTest " + ifn);
System.out.println(" " + fb.toString());
ImageIO.write((RenderedImage)fb, "png", new File(ifn));
} catch (Exception e) {
System.out.println("While writing image "+ifn+", caught " + e);
e.printStackTrace(System.out);
}
}
/** Get the image type used to create the current frame buffer. */
public int getCurrentFrameBufferType() {
return fb.getType();
}
/** Get a string form for the image type. */
public String getImageTypeString() {
switch (getCurrentFrameBufferType()) {
case BufferedImage.TYPE_3BYTE_BGR: return "3b_bgr";
case BufferedImage.TYPE_4BYTE_ABGR: return "4b_abgr";
case BufferedImage.TYPE_BYTE_BINARY: return "b_binary";
case BufferedImage.TYPE_BYTE_GRAY: return "b_gray";
case BufferedImage.TYPE_BYTE_INDEXED: return "b_indexed";
// XXX BufferedImage.TYPE_CUSTOM
case BufferedImage.TYPE_INT_ARGB: return "i_argb";
case BufferedImage.TYPE_INT_ARGB_PRE: return "i_argb_pre";
case BufferedImage.TYPE_INT_BGR: return "i_bgr";
case BufferedImage.TYPE_INT_RGB: return "i_rgb";
case BufferedImage.TYPE_USHORT_555_RGB: return "u555_rgb";
case BufferedImage.TYPE_USHORT_565_RGB: return "u565_rgb";
case BufferedImage.TYPE_USHORT_GRAY: return "u_gray";
}
return "unk";
}
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
new imgtest(args).runTest();
}
}