// Getting a writer.
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Getting a writer");
}
ImageWriter writer = null;
ImageWriterSpi originatingProvider = null;
// ImageIO
if (nativeAcc) {
if (CLIB_PNG_IMAGE_WRITER_SPI != null) {
// let me check if the native writer can encode this image
if (CLIB_PNG_IMAGE_WRITER_SPI.canEncodeImage(new ImageTypeSpecifier(image))) {
writer = CLIB_PNG_IMAGE_WRITER_SPI.createWriterInstance();
originatingProvider = CLIB_PNG_IMAGE_WRITER_SPI;
} else {
LOGGER.fine("The ImageIO PNG native encode cannot encode this image!");
writer = null;
originatingProvider = null;
}
} else {
LOGGER.fine("Unable to use Native ImageIO PNG writer.");
}
}
// move on with the writer quest
if (!nativeAcc || writer == null) {
final Iterator<ImageWriter> it = ImageIO.getImageWriters(new ImageTypeSpecifier(image),
"PNG");
if (!it.hasNext()) {
throw new IllegalStateException(Errors.format(ErrorKeys.NO_IMAGE_WRITER));
}
while (it.hasNext()) {
writer = it.next();
originatingProvider = writer.getOriginatingProvider();
// check that this is not the native one
if (CLIB_PNG_IMAGE_WRITER_SPI != null
&& originatingProvider.getClass().equals(
CLIB_PNG_IMAGE_WRITER_SPI.getClass())) {
if (it.hasNext()) {
writer = it.next();
originatingProvider = writer.getOriginatingProvider();
} else {
LOGGER.fine("Unable to use PNG writer different than ImageIO CLib one");
}
}
// let me check if the native writer can encode this image (paranoiac checks this was already performed by the ImageIO search
if (originatingProvider.canEncodeImage(new ImageTypeSpecifier(image))) {
break; // leave loop
}
// clean
writer = null;
originatingProvider = null;
}
}
// ok, last resort use the JDK one and reformat the image
if (writer == null) {
List providers = com.sun.media.imageioimpl.common.ImageUtil.getJDKImageReaderWriterSPI(
IIORegistry.getDefaultInstance(), "PNG", false);
if (providers == null || providers.isEmpty()) {
throw new IllegalStateException("Unable to find JDK Png encoder!");
}
originatingProvider = (ImageWriterSpi) providers.get(0);
writer = originatingProvider.createWriterInstance();
// kk, last resort reformat the image
forceComponentColorModel(true, true);
rescaleToBytes();
if (!originatingProvider.canEncodeImage(image)) {
throw new IllegalArgumentException(
"Unable to find a valid PNG Encoder! And believe me, we tried hard!");
}
}
LOGGER.fine("Using ImageIO Writer with SPI: "
+ originatingProvider.getClass().getCanonicalName());
// Getting a stream.
LOGGER.fine("Setting write parameters for this writer");
ImageWriteParam iwp = null;
final ImageOutputStream memOutStream = ImageIOExt.createImageOutputStream(image,
destination);
if (memOutStream == null) {
throw new IIOException(Errors.format(ErrorKeys.NULL_ARGUMENT_$1, "stream"));
}
if (CLIB_PNG_IMAGE_WRITER_SPI != null
&& originatingProvider.getClass().equals(CLIB_PNG_IMAGE_WRITER_SPI.getClass())) {
// Compressing with native.
LOGGER.fine("Writer is native");
iwp = writer.getDefaultWriteParam();
// Define compression mode
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);