Search in sources :

Example 66 with ImageWriter

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

the class GeoTiffVrtTest method write.

/**
 * Test Writing capabilities.
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void write() throws IOException, FileNotFoundException {
    if (!isGDALAvailable) {
        return;
    }
    final File outputFile = TestData.temp(this, "writetest.tif", false);
    outputFile.deleteOnExit();
    final File inputFile = TestData.file(this, "utmByte.tif.vrt");
    ImageReadParam rparam = new ImageReadParam();
    rparam.setSourceRegion(new Rectangle(1, 1, 300, 500));
    rparam.setSourceSubsampling(1, 2, 0, 0);
    ImageReader reader = new VRTImageReaderSpi().createReaderInstance();
    reader.setInput(inputFile);
    final IIOMetadata metadata = reader.getImageMetadata(0);
    final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", inputFile);
    pbjImageRead.setParameter("reader", reader);
    pbjImageRead.setParameter("readParam", rparam);
    final ImageLayout l = new ImageLayout();
    l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image, "geotiff");
    // ////////////////////////////////////////////////////////////////
    // preparing to write
    // ////////////////////////////////////////////////////////////////
    final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
    ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
    pbjImageWrite.setParameter("Output", outputFile);
    pbjImageWrite.setParameter("writer", writer);
    pbjImageWrite.setParameter("ImageMetadata", metadata);
    pbjImageWrite.setParameter("Transcode", false);
    ImageWriteParam param = new ImageWriteParam(Locale.getDefault());
    param.setSourceRegion(new Rectangle(10, 10, 100, 100));
    param.setSourceSubsampling(2, 1, 0, 0);
    pbjImageWrite.setParameter("writeParam", param);
    pbjImageWrite.addSource(image);
    final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
    final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
    writer2.dispose();
    // ////////////////////////////////////////////////////////////////
    // preparing to read again
    // ////////////////////////////////////////////////////////////////
    final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
    pbjImageReRead.setParameter("Input", outputFile);
    pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
    final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image2, "geotif2");
    else
        Assert.assertNotNull(image2.getTiles());
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) GeoTiffImageReaderSpi(it.geosolutions.imageio.plugins.geotiff.GeoTiffImageReaderSpi) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) RenderedOp(javax.media.jai.RenderedOp) GeoTiffImageWriterSpi(it.geosolutions.imageio.plugins.geotiff.GeoTiffImageWriterSpi) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest) Test(org.junit.Test)

Example 67 with ImageWriter

use of javax.imageio.ImageWriter 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 68 with ImageWriter

use of javax.imageio.ImageWriter 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 69 with ImageWriter

use of javax.imageio.ImageWriter 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 70 with ImageWriter

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

the class JP2KKakaduWriteTest method write.

private static synchronized void write(String file, RenderedImage bi, boolean codeStreamOnly, double quality, boolean useJAI, JP2KKakaduImageWriteParam addParam) throws IOException {
    writeOperations++;
    file += "_Q" + quality + (codeStreamOnly ? ".j2c" : ".jp2");
    final ImageOutputStream outputStream = ImageIO.createImageOutputStream(new File(file));
    JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
    param.setQuality(quality);
    param.setWriteCodeStreamOnly(codeStreamOnly);
    if (addParam != null) {
        param.setSourceRegion(addParam.getSourceRegion());
        param.setSourceSubsampling(addParam.getSourceXSubsampling(), addParam.getSourceYSubsampling(), addParam.getSubsamplingXOffset(), addParam.getSubsamplingYOffset());
        param.setCLevels(addParam.getCLevels());
        param.setQualityLayers(addParam.getQualityLayers());
    }
    if (!useJAI) {
        final ImageWriter writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
        // final ImageWriter writer = new
        // J2KImageWriterSpi().createWriterInstance();
        writer.setOutput(outputStream);
        // J2KImageWriteParam ioparam = (J2KImageWriteParam)
        // writer.getDefaultWriteParam();
        // ioparam.setWriteCodeStreamOnly(true);
        // ioparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        // ioparam.setCompressionType("JPEG2000");
        // ioparam.setCompressionQuality((float)quality);
        // ioparam.setEncodingRate((quality)*24);
        // writer.write(null, new IIOImage(bi, null, null), ioparam);
        writer.write(null, new IIOImage(bi, null, null), param);
        writer.dispose();
    } else {
        final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
        final ImageWriter writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
        pbjImageWrite.setParameter("writer", writer);
        pbjImageWrite.setParameter("output", outputStream);
        pbjImageWrite.setParameter("writeParam", param);
        pbjImageWrite.addSource(bi);
        RenderedOp image = JAI.create("ImageWrite", pbjImageWrite);
    }
}
Also used : RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ImageWriter(javax.imageio.ImageWriter) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageWriter (javax.imageio.ImageWriter)171 ImageWriteParam (javax.imageio.ImageWriteParam)103 IIOImage (javax.imageio.IIOImage)92 ImageOutputStream (javax.imageio.stream.ImageOutputStream)85 BufferedImage (java.awt.image.BufferedImage)80 File (java.io.File)73 IOException (java.io.IOException)53 ByteArrayOutputStream (java.io.ByteArrayOutputStream)44 Test (org.junit.Test)37 IIOMetadata (javax.imageio.metadata.IIOMetadata)36 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)30 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)28 RenderedOp (javax.media.jai.RenderedOp)28 ImageReadParam (javax.imageio.ImageReadParam)26 Iterator (java.util.Iterator)25 ImageReader (javax.imageio.ImageReader)24 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Graphics2D (java.awt.Graphics2D)19 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)17