Search in sources :

Example 91 with IIOImage

use of javax.imageio.IIOImage 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 92 with IIOImage

use of javax.imageio.IIOImage 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 93 with IIOImage

use of javax.imageio.IIOImage 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 94 with IIOImage

use of javax.imageio.IIOImage in project imageio-ext by geosolutions-it.

the class TIFFBaseJPEGCompressor method encode.

public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
    if (this.JPEGWriter == null) {
        throw new IIOException("JPEG writer has not been initialized!");
    }
    if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
        throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
    }
    // Set the stream.
    ImageOutputStream ios;
    // usingCodecLib && !writeAbbreviatedStream
    long initialStreamPosition;
    if (usingCodecLib && !writeAbbreviatedStream) {
        ios = stream;
        initialStreamPosition = stream.getStreamPosition();
    } else {
        // is using a stream on the native side which cannot be reset.
        if (baos == null) {
            baos = new IIOByteArrayOutputStream();
        } else {
            baos.reset();
        }
        ios = new MemoryCacheImageOutputStream(baos);
        initialStreamPosition = 0L;
    }
    JPEGWriter.setOutput(ios);
    // Create a DataBuffer.
    DataBufferByte dbb;
    if (off == 0 || usingCodecLib) {
        dbb = new DataBufferByte(b, b.length);
    } else {
        // 
        // Workaround for bug in core Java Image I/O JPEG
        // ImageWriter which cannot handle non-zero offsets.
        // 
        int bytesPerSegment = scanlineStride * height;
        byte[] btmp = new byte[bytesPerSegment];
        System.arraycopy(b, off, btmp, 0, bytesPerSegment);
        dbb = new DataBufferByte(btmp, bytesPerSegment);
        off = 0;
    }
    // Set up the ColorSpace.
    int[] offsets;
    ColorSpace cs;
    if (bitsPerSample.length == 3) {
        offsets = new int[] { off, off + 1, off + 2 };
        cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    } else {
        offsets = new int[] { off };
        cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
    }
    // Create the ColorModel.
    ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    // Create the SampleModel.
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
    // Create the WritableRaster.
    WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
    // Create the BufferedImage.
    BufferedImage bi = new BufferedImage(cm, wras, false, null);
    // Get the pruned JPEG image metadata (may be null).
    IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
    // Compress the image into the output stream.
    int compDataLength;
    if (usingCodecLib && !writeAbbreviatedStream) {
        // Write complete JPEG stream
        JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
    } else {
        if (writeAbbreviatedStream) {
            // Write abbreviated JPEG stream
            // First write the tables-only data.
            JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
            ios.flush();
            // Rewind to the beginning of the byte array.
            baos.reset();
            // Write the abbreviated image data.
            IIOImage image = new IIOImage(bi, null, imageMetadata);
            JPEGWriter.writeToSequence(image, JPEGParam);
            JPEGWriter.endWriteSequence();
        } else {
            // Write complete JPEG stream
            JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
        }
        compDataLength = baos.size();
        baos.writeTo(stream);
        baos.reset();
    }
    return compDataLength;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 95 with IIOImage

use of javax.imageio.IIOImage 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

IIOImage (javax.imageio.IIOImage)123 ImageWriter (javax.imageio.ImageWriter)92 ImageWriteParam (javax.imageio.ImageWriteParam)79 ImageOutputStream (javax.imageio.stream.ImageOutputStream)79 BufferedImage (java.awt.image.BufferedImage)66 File (java.io.File)45 IOException (java.io.IOException)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)25 ByteArrayInputStream (java.io.ByteArrayInputStream)22 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17 ImageReader (javax.imageio.ImageReader)16 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)16 Graphics2D (java.awt.Graphics2D)14 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)14 Test (org.junit.Test)14 FileOutputStream (java.io.FileOutputStream)13 Iterator (java.util.Iterator)11 RenderedImage (java.awt.image.RenderedImage)10