if (!it.hasNext()) {
throw new IllegalArgumentException("Format not supported: " + format);
}
ImageWriter writer = (ImageWriter) it.next();
ImageOutputStream ioutstream = null;
IIOMetadata meta = writer.getDefaultStreamMetadata(writer.getDefaultWriteParam());
ImageWriteParam param = writer.getDefaultWriteParam();
// DJB: jpeg does not support ARGB (alpha) colour
// this converts the image from ARGB to RGB
// TODO: make this more abstract - it should be smarter for more image
// writer types (Ie. ask the writer what it supports)
// Alternately, make a jpeg writer and png writer, as these are
// mostly what we get from jai!
if (format.equalsIgnoreCase("image/jpeg")) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.9f); // DJB: only do this for jpegs - png freaks when you do this!
meta = writer.getDefaultStreamMetadata(param);
// WritableRaster raster = image.getRaster();
// WritableRaster newRaster = raster.createWritableChild(0, 0, image.getWidth(), image.getHeight(), 0, 0, new int[] {0, 1, 2});
// create a ColorModel that represents the one of the ARGB except the alpha channel:
// DirectColorModel cm = (DirectColorModel)image.getColorModel();
// DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
// now create the new buffer that is used ot write the image:
// BufferedImage rgbBuffer = new BufferedImage(newCM, newRaster, false, null);
BufferedImage curImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = (Graphics2D) curImage.createGraphics();
g.drawImage(image, 0, 0, null);
image = curImage;
ioutstream = ImageIO.createImageOutputStream(outStream);
writer.setOutput(ioutstream);
writer.write(image);
ioutstream.close();
writer.dispose();
return;
}
ioutstream = ImageIO.createImageOutputStream(outStream);
writer.setOutput(ioutstream);
writer.write(meta, new IIOImage(image, null, meta), param);
ioutstream.close();
writer.dispose();
}