Search in sources :

Example 6 with PNGImageWriterSPI

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

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

PNGImageWriterSPI (it.geosolutions.imageio.plugins.png.PNGImageWriterSPI)7 ImageWriter (javax.imageio.ImageWriter)7 PNGWriter (it.geosolutions.imageio.plugins.png.PNGWriter)6 BufferedImage (java.awt.image.BufferedImage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IIOImage (javax.imageio.IIOImage)6 ImageWriteParam (javax.imageio.ImageWriteParam)6 Test (org.junit.Test)5 Graphics2D (java.awt.Graphics2D)3 File (java.io.File)2 PNGImageWriter (it.geosolutions.imageio.plugins.png.PNGImageWriter)1 Point (java.awt.Point)1 RenderingHints (java.awt.RenderingHints)1 DataBufferByte (java.awt.image.DataBufferByte)1 IndexColorModel (java.awt.image.IndexColorModel)1 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)1 RenderedImage (java.awt.image.RenderedImage)1 SampleModel (java.awt.image.SampleModel)1 WritableRaster (java.awt.image.WritableRaster)1