// /////////////////////////////////////////////////////////////////
//
// Reformatting this image for png
//
// /////////////////////////////////////////////////////////////////
final MemoryCacheImageOutputStream memOutStream = new MemoryCacheImageOutputStream(output);
final ImageWorker worker = new ImageWorker(image);
final PlanarImage finalImage = (image.getColorModel() instanceof DirectColorModel) ? worker
.forceComponentColorModel().getPlanarImage() : worker.getPlanarImage();
// /////////////////////////////////////////////////////////////////
//
// Getting a writer
//
// /////////////////////////////////////////////////////////////////
final Iterator<ImageWriter> it;
it = ImageIO.getImageWritersByMIMEType(PNGLegendOutputFormat.MIME_TYPE);
ImageWriter writer = null;
if (!it.hasNext()) {
throw new IllegalStateException("No PNG ImageWriter found");
} else {
writer = (ImageWriter) it.next();
}
// /////////////////////////////////////////////////////////////////
//
// Compression is available only on native lib
//
// /////////////////////////////////////////////////////////////////
final ImageWriteParam iwp = writer.getDefaultWriteParam();
if (writer.getClass().getName()
.equals("com.sun.media.imageioimpl.plugins.png.CLibPNGImageWriter")) {
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.75f); // we can control quality here
}
writer.setOutput(memOutStream);
try {
writer.write(null, new IIOImage(finalImage, null, null), iwp);
memOutStream.flush();
// this doesn't close the destination output stream
memOutStream.close();
} finally {
writer.dispose();
}
}