Search in sources :

Example 1 with PNGWriter

use of it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.

the class CustomByteIndexImageTypesTest method testCustomIndexedImage.

@Test
public void testCustomIndexedImage() throws Exception {
    byte[] colors = new byte[ncolors];
    for (int i = 0; i < ncolors; i++) {
        colors[i] = (byte) i;
    }
    int nbits;
    if (ncolors <= 2) {
        nbits = 1;
    } else {
        nbits = (int) Math.ceil(Math.log(ncolors) / Math.log(2));
        if ((nbits & (nbits - 1)) != 0) {
            int nextPower = (int) (Math.floor(Math.log(nbits) / Math.log(2)) + 1);
            nbits = (int) Math.pow(2, nextPower);
        }
    }
    IndexColorModel icm = new IndexColorModel(nbits, ncolors, colors, colors, colors);
    SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, size, size, nbits);
    int pixelsPerByte = 8 / nbits;
    int bytesPerRow = (int) Math.max(1, Math.ceil(1d * size / pixelsPerByte));
    int bytes = bytesPerRow * size;
    DataBufferByte dataBuffer = new DataBufferByte(bytes);
    WritableRaster wr = (WritableRaster) Raster.createWritableRaster(sm, dataBuffer, new Point(0, 0));
    BufferedImage bi = new BufferedImage(icm, wr, false, null);
    Graphics2D graphics = bi.createGraphics();
    graphics.setColor(Color.BLACK);
    graphics.fillRect(0, 0, 16, 32);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(16, 0, 16, 32);
    graphics.dispose();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    float quality = 5f / 9 - 1;
    new PNGWriter().writePNG(bi, bos, -quality, FilterType.FILTER_NONE);
    BufferedImage read = ImageIO.read(new ByteArrayInputStream(bos.toByteArray()));
    ImageAssert.assertImagesEqual(bi, read);
    // now using imagewriter interface
    ImageWriter writer = new PNGImageWriterSPI().createWriterInstance();
    writer.setOutput(bos);
    ImageWriteParam wp = writer.getDefaultWriteParam();
    wp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wp.setCompressionQuality(-quality);
    writer.write(null, new IIOImage(bi, null, null), wp);
    writer.dispose();
    ImageAssert.assertImagesEqual(bi, ImageIO.read(new ByteArrayInputStream(bos.toByteArray())));
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ImageWriter(javax.imageio.ImageWriter) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataBufferByte(java.awt.image.DataBufferByte) ImageWriteParam(javax.imageio.ImageWriteParam) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ByteArrayInputStream(java.io.ByteArrayInputStream) WritableRaster(java.awt.image.WritableRaster) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) IndexColorModel(java.awt.image.IndexColorModel) Test(org.junit.Test)

Example 2 with PNGWriter

use of it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.

the class PNGWriterTest method testWriter.

@Test
public void testWriter() {
    PNGWriter writer = new PNGWriter();
    OutputStream out = null;
    try {
        // read test image
        BufferedImage read = ImageIO.read(TestData.file(this, "sample.jpeg"));
        File pngOut = TestData.temp(this, "test.png", true);
        out = new FileOutputStream(pngOut);
        writer.writePNG(read, out, 1, FilterType.FILTER_NONE);
        BufferedImage test = ImageIO.read(pngOut);
        assertNotNull(test);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
            out = null;
        }
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) IOException(java.io.IOException) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with PNGWriter

use of it.geosolutions.imageio.plugins.png.PNGWriter 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)

Example 4 with PNGWriter

use of it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.

the class BufferedImageChildTest method testSubImage.

private void testSubImage(int x, int y, int w, int h) throws Exception {
    BufferedImage bi = getSample();
    // ImageAssert.showImage("Original", 2000, bi);
    BufferedImage subimage = bi.getSubimage(x, y, w, h);
    // ImageAssert.showImage("Subimage", 2000, subimage);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    float quality = 4f / 9 - 1;
    new PNGWriter().writePNG(subimage, bos, -quality, FilterType.FILTER_NONE);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    BufferedImage readBack = ImageIO.read(bis);
    // ImageAssert.showImage("ReadBack", 2000, readBack);
    ImageAssert.assertImagesEqual(subimage, readBack);
    // now using imagewriter interface
    ImageWriter writer = new PNGImageWriterSPI().createWriterInstance();
    writer.setOutput(bos);
    ImageWriteParam wp = writer.getDefaultWriteParam();
    wp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wp.setCompressionQuality(-quality);
    writer.write(null, new IIOImage(subimage, null, null), wp);
    writer.dispose();
    bis = new ByteArrayInputStream(bos.toByteArray());
    readBack = ImageIO.read(bis);
    // ImageAssert.showImage("ReadBack", 2000, readBack);
    ImageAssert.assertImagesEqual(subimage, readBack);
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ByteArrayInputStream(java.io.ByteArrayInputStream) PNGImageWriter(it.geosolutions.imageio.plugins.png.PNGImageWriter) ImageWriter(javax.imageio.ImageWriter) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 5 with PNGWriter

use of it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.

the class GrayAlpha8bitTest method testGrayAlpha8Bit.

@Test
public void testGrayAlpha8Bit() throws Exception {
    BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_BYTE_GRAY);
    Graphics2D graphics = bi.createGraphics();
    graphics.setColor(Color.BLACK);
    graphics.fillRect(0, 0, 16, 32);
    graphics.setColor(Color.WHITE);
    graphics.fillRect(16, 0, 16, 32);
    graphics.dispose();
    final ImageLayout tempLayout = new ImageLayout(bi);
    tempLayout.unsetValid(ImageLayout.COLOR_MODEL_MASK).unsetValid(ImageLayout.SAMPLE_MODEL_MASK);
    RenderedImage alpha = ConstantDescriptor.create(Float.valueOf(bi.getWidth()), Float.valueOf(bi.getHeight()), new Byte[] { Byte.valueOf((byte) 255) }, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, tempLayout));
    RenderedImage grayAlpha = BandMergeDescriptor.create(bi, alpha, null);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    float quality = 5f / 9 - 1;
    new PNGWriter().writePNG(grayAlpha, bos, -quality, FilterType.FILTER_NONE);
    BufferedImage read = ImageIO.read(new ByteArrayInputStream(bos.toByteArray()));
    BufferedImage gaBuffered = PlanarImage.wrapRenderedImage(grayAlpha).getAsBufferedImage();
    ImageAssert.assertImagesEqual(gaBuffered, read);
    // now using imagewriter interface
    ImageWriter writer = new PNGImageWriterSPI().createWriterInstance();
    writer.setOutput(bos);
    ImageWriteParam wp = writer.getDefaultWriteParam();
    wp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    wp.setCompressionQuality(-quality);
    writer.write(null, new IIOImage(bi, null, null), wp);
    writer.dispose();
    ImageAssert.assertImagesEqual(bi, ImageIO.read(new ByteArrayInputStream(bos.toByteArray())));
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) RenderingHints(java.awt.RenderingHints) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) ByteArrayInputStream(java.io.ByteArrayInputStream) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) RenderedImage(java.awt.image.RenderedImage) ImageLayout(javax.media.jai.ImageLayout) Test(org.junit.Test)

Aggregations

PNGWriter (it.geosolutions.imageio.plugins.png.PNGWriter)8 BufferedImage (java.awt.image.BufferedImage)8 PNGImageWriterSPI (it.geosolutions.imageio.plugins.png.PNGImageWriterSPI)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IIOImage (javax.imageio.IIOImage)6 ImageWriteParam (javax.imageio.ImageWriteParam)6 ImageWriter (javax.imageio.ImageWriter)6 Test (org.junit.Test)6 File (java.io.File)4 Graphics2D (java.awt.Graphics2D)3 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PngReader (ar.com.hjg.pngj.PngReader)1 PngMetadata (ar.com.hjg.pngj.chunks.PngMetadata)1 PNGImageWriter (it.geosolutions.imageio.plugins.png.PNGImageWriter)1 Point (java.awt.Point)1 RenderingHints (java.awt.RenderingHints)1 DataBufferByte (java.awt.image.DataBufferByte)1