Search in sources :

Example 1 with PngMetadata

use of ar.com.hjg.pngj.chunks.PngMetadata 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;
}
Also used : PngWriter(ar.com.hjg.pngj.PngWriter) PngMetadata(ar.com.hjg.pngj.chunks.PngMetadata) Entry(java.util.Map.Entry) ChunksListForWrite(ar.com.hjg.pngj.chunks.ChunksListForWrite) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) PngChunkTRNS(ar.com.hjg.pngj.chunks.PngChunkTRNS) RenderedImage(java.awt.image.RenderedImage) ImageInfo(ar.com.hjg.pngj.ImageInfo) PngChunkPLTE(ar.com.hjg.pngj.chunks.PngChunkPLTE) IndexColorModel(java.awt.image.IndexColorModel)

Example 2 with PngMetadata

use of ar.com.hjg.pngj.chunks.PngMetadata in project imageio-ext by geosolutions-it.

the class PNGWriterTest method testTeXt.

@Test
public void testTeXt() throws Exception {
    PNGWriter writer = new PNGWriter();
    OutputStream out = null;
    File pngOut = null;
    final String title = "Title";
    final String description = "Sample Description";
    final String software = "ImageIO-Ext";
    final String author = "Me";
    try {
        // read test image
        BufferedImage read = ImageIO.read(TestData.file(this, "sample.jpeg"));
        pngOut = TestData.temp(this, "test.png", true);
        out = new FileOutputStream(pngOut);
        Map<String, String> textMetadata = new HashMap<String, String>();
        textMetadata.put("Title", title);
        textMetadata.put("Author", author);
        textMetadata.put("Software", software);
        textMetadata.put("Description", description);
        writer.writePNG(read, out, 1, FilterType.FILTER_NONE, textMetadata);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
    BufferedImage test = ImageIO.read(pngOut);
    assertNotNull(test);
    PngReader reader = null;
    try {
        reader = new PngReader(pngOut);
        reader.readSkippingAllRows();
        PngMetadata metadata = reader.getMetadata();
        assertNotNull(metadata);
        assertEquals(title, metadata.getTxtForKey("Title"));
        assertEquals(description, metadata.getTxtForKey("Description"));
        assertEquals(author, metadata.getTxtForKey("Author"));
        assertEquals(software, metadata.getTxtForKey("Software"));
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) IOException(java.io.IOException) PngReader(ar.com.hjg.pngj.PngReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) PngMetadata(ar.com.hjg.pngj.chunks.PngMetadata) Test(org.junit.Test)

Aggregations

PngMetadata (ar.com.hjg.pngj.chunks.PngMetadata)2 ImageInfo (ar.com.hjg.pngj.ImageInfo)1 PngReader (ar.com.hjg.pngj.PngReader)1 PngWriter (ar.com.hjg.pngj.PngWriter)1 ChunksListForWrite (ar.com.hjg.pngj.chunks.ChunksListForWrite)1 PngChunkPLTE (ar.com.hjg.pngj.chunks.PngChunkPLTE)1 PngChunkTRNS (ar.com.hjg.pngj.chunks.PngChunkTRNS)1 PNGWriter (it.geosolutions.imageio.plugins.png.PNGWriter)1 BufferedImage (java.awt.image.BufferedImage)1 ColorModel (java.awt.image.ColorModel)1 IndexColorModel (java.awt.image.IndexColorModel)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Test (org.junit.Test)1