Search in sources :

Example 1 with PNGImageWriterSPI

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

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

the class SPITest method testSPI.

@Test
public void testSPI() throws Exception {
    boolean found = false;
    Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("PNG");
    while (it.hasNext()) {
        ImageWriter wr = it.next();
        if (wr.getOriginatingProvider() instanceof PNGImageWriterSPI) {
            found = true;
            break;
        }
    }
    // did we find it
    assertTrue("Unable to find PNGImageWriterSPI", found);
}
Also used : PNGImageWriterSPI(it.geosolutions.imageio.plugins.png.PNGImageWriterSPI) ImageWriter(javax.imageio.ImageWriter) Test(org.junit.Test)

Example 3 with PNGImageWriterSPI

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

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

Example 5 with PNGImageWriterSPI

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

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