use of ar.com.hjg.pngj.chunks.ChunksListForWrite in project imageio-ext by geosolutions-it.
the class PNGWriter method writePNG.
public RenderedImage writePNG(RenderedImage image, OutputStream outStream, float quality, FilterType filterType, Map<String, String> text) throws Exception {
// compute the compression level similarly to what the Clib code does
int level = Math.round(9 * (1f - quality));
// get the optimal scanline provider for this image
RenderedImage original = image;
ScanlineProvider scanlines = ScanlineProviderFactory.getProvider(image);
if (scanlines == null) {
throw new IllegalArgumentException("Could not find a scanline extractor for " + original);
}
// encode using the PNGJ library and the GeoServer own scanline providers
ColorModel colorModel = image.getColorModel();
boolean indexed = colorModel instanceof IndexColorModel;
ImageInfo ii = getImageInfo(image, scanlines, colorModel, indexed);
PngWriter pw = new PngWriter(outStream, ii);
pw.setShouldCloseStream(false);
try {
pw.setCompLevel(level);
pw.setFilterType(filterType);
ChunksListForWrite chunkList = pw.getChunksList();
PngMetadata metadata = pw.getMetadata();
if (indexed) {
IndexColorModel icm = (IndexColorModel) colorModel;
PngChunkPLTE palette = metadata.createPLTEChunk();
int ncolors = icm.getMapSize();
palette.setNentries(ncolors);
for (int i = 0; i < ncolors; i++) {
final int red = icm.getRed(i);
final int green = icm.getGreen(i);
final int blue = icm.getBlue(i);
palette.setEntry(i, red, green, blue);
}
if (icm.hasAlpha()) {
PngChunkTRNS transparent = new PngChunkTRNS(ii);
int[] alpha = new int[ncolors];
for (int i = 0; i < ncolors; i++) {
final int a = icm.getAlpha(i);
alpha[i] = a;
}
transparent.setPalletteAlpha(alpha);
chunkList.queue(transparent);
}
}
if (text != null && !text.isEmpty()) {
Iterator<Entry<String, String>> entrySetIterator = text.entrySet().iterator();
while (entrySetIterator.hasNext()) {
Entry<String, String> entrySet = entrySetIterator.next();
metadata.setText(entrySet.getKey(), entrySet.getValue(), true, false);
}
}
// write out the actual image lines
for (int row = 0; row < image.getHeight(); row++) {
pw.writeRow(scanlines);
}
pw.end();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to encode the PNG", e);
throw e;
} finally {
pw.close();
}
return image;
}
Aggregations