Search in sources :

Example 6 with PNGWriter

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

the class CustomUShortImageTypesTest method testCustomUShortImage.

@Test
public void testCustomUShortImage() throws Exception {
    BufferedImage bi = ImageTypeSpecifier.createGrayscale(nbits, DataBuffer.TYPE_USHORT, false).createBufferedImage(size, size);
    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) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) Test(org.junit.Test)

Example 7 with PNGWriter

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

the class PngSuiteImagesTest method roundTripPNGJ.

private void roundTripPNGJ(BufferedImage original, RenderedImage source) throws Exception {
    // write the PNG
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    float quality = 4f / 9 - 1;
    new PNGWriter().writePNG(original, bos, -quality, FilterType.FILTER_NONE);
    // write the output to file for eventual visual comparison
    byte[] bytes = bos.toByteArray();
    writeToFile(new File("./target/roundTripNone", sourceFile.getName()), bytes);
    // read it back
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BufferedImage image = ImageIO.read(bis);
    ImageAssert.assertImagesEqual(original, image);
    // 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(original, null, null), wp);
    writer.dispose();
    ImageAssert.assertImagesEqual(original, ImageIO.read(new ByteArrayInputStream(bos.toByteArray())));
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 8 with PNGWriter

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

the class BufferedImageTypesTest method compareImage.

@Test
public void compareImage() throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    float quality = 4f / 9 - 1;
    new PNGWriter().writePNG(image, bos, -quality, FilterType.FILTER_NONE);
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    BufferedImage readBack = ImageIO.read(bis);
    boolean success = false;
    try {
        ImageAssert.assertImagesEqual(image, readBack);
        success = true;
    } finally {
        if (!success) {
            ImageIO.write(image, "PNG", new File("./target/" + name + "_expected.png"));
            ImageIO.write(readBack, "PNG", new File("./target/" + name + "_actual.png"));
        }
    }
    // 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(image, null, null), wp);
    writer.dispose();
    bis = new ByteArrayInputStream(bos.toByteArray());
    readBack = ImageIO.read(bis);
    success = false;
    try {
        ImageAssert.assertImagesEqual(image, readBack);
        success = true;
    } finally {
        if (!success) {
            ImageIO.write(image, "PNG", new File("./target/" + name + "_expected.png"));
            ImageIO.write(readBack, "PNG", new File("./target/" + name + "_actual.png"));
        }
    }
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) PNGWriter(it.geosolutions.imageio.plugins.png.PNGWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) 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