Search in sources :

Example 91 with ImageWriteParam

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

the class JPEGWriterTest method writerTestComponentsSubsampling.

@Test
public void writerTestComponentsSubsampling() throws IOException {
    if (SKIP_TESTS) {
        LOGGER.warning(ERROR_LIB_MESSAGE);
        assumeTrue(!SKIP_TESTS);
        return;
    }
    final long[] lengths = new long[3];
    final int[] componentSubsampling = new int[] { TJ.SAMP_444, TJ.SAMP_422, TJ.SAMP_420 };
    // test-data
    final File input = TestData.file(this, "testmergb.png");
    assertTrue("Unable to find test data", input.exists() && input.isFile() && input.canRead());
    // get the SPI for writer\
    final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
    assertTrue(it.hasNext());
    TurboJpegImageWriter writer = null;
    while (it.hasNext()) {
        ImageWriterSpi writer_ = it.next().getOriginatingProvider();
        if (writer_ instanceof TurboJpegImageWriterSpi) {
            writer = (TurboJpegImageWriter) writer_.createWriterInstance();
            break;
        }
    }
    assertNotNull("Unable to find TurboJpegImageWriter", writer);
    IIOImage image = new IIOImage(ImageIO.read(input), null, null);
    for (int i = 0; i < 3; i++) {
        // create write param
        ImageWriteParam wParam_ = writer.getDefaultWriteParam();
        assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
        TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
        wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        wParam.setCompressionType("JPEG");
        wParam.setCompressionQuality(.75f);
        wParam.setComponentSubsampling(componentSubsampling[i]);
        // create output file
        final File output = TestData.temp(this, "output.jpeg", false);
        LOGGER.info("output file is " + output);
        writer.setOutput(output);
        writer.write(null, image, wParam);
        writer.dispose();
        assertTrue("Unable to create output file", output.exists() && output.isFile());
        lengths[i] = output.length();
    // output.delete();
    }
    assertEquals(lengths[0], 11604l);
    assertEquals(lengths[1], 9376l);
    assertEquals(lengths[2], 8209l);
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) IIOImage(javax.imageio.IIOImage) Test(org.junit.Test)

Example 92 with ImageWriteParam

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

the class JPEGWriterSpeedTest method testJPEGJDK.

@Test
@Ignore
public void testJPEGJDK() throws FileNotFoundException, IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    assumeTrue(!SKIP_TESTS);
    String fileName = null;
    ImageOutputStream out1 = null;
    try {
        ImageWriterSpi spi = standardSPI;
        fileName = OUTPUT_FOLDER + ((SAMPLE_IMAGE.getSampleModel().getNumBands() == 1) ? "GRAY" : "jdkRGB") + "METAoutput.jpeg";
        final File file = new File(fileName);
        out1 = new FileImageOutputStream(file);
        ImageWriter writer1 = spi.createWriterInstance();
        ImageWriteParam param = writer1.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(0.75f);
        writer1.setOutput(out1);
        writer1.write(null, new IIOImage(SAMPLE_IMAGE, null, null), param);
        out1.close();
        writer1.dispose();
        // Writing loops
        long start = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            // Startup write
            out1 = new FileImageOutputStream(file);
            writer1 = spi.createWriterInstance();
            writer1.setOutput(out1);
            writer1.write(null, new IIOImage(SAMPLE_IMAGE, null, null), param);
            out1.close();
            writer1.dispose();
        }
        long end = System.nanoTime();
        long total = end - start;
        reportTime("JDK", total, LOOP);
    } catch (Throwable t) {
        if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.warning(t.getLocalizedMessage());
        }
    } finally {
        if (out1 != null) {
            try {
                out1.close();
            } catch (Throwable t) {
            // 
            }
        }
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 93 with ImageWriteParam

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

the class JPEGWriterSpeedTest method testJPEGCLIB.

@Test
@Ignore
public void testJPEGCLIB() throws FileNotFoundException, IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    assumeTrue(!SKIP_TESTS);
    String fileName = null;
    ImageOutputStream out1 = null;
    try {
        ImageWriterSpi spi = clibSPI;
        fileName = OUTPUT_FOLDER + ((SAMPLE_IMAGE.getSampleModel().getNumBands() == 1) ? "GRAY" : "RGB") + "CLIBoutput.jpeg";
        final File file = new File(fileName);
        out1 = new FileImageOutputStream(file);
        ImageWriter writer1 = spi.createWriterInstance();
        ImageWriteParam param = writer1.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(0.75f);
        writer1.setOutput(out1);
        writer1.write(null, new IIOImage(SAMPLE_IMAGE, null, null), param);
        out1.close();
        writer1.dispose();
        // Writing loops
        long start = System.nanoTime();
        for (int i = 0; i < LOOP; i++) {
            // Startup write
            out1 = new FileImageOutputStream(file);
            writer1 = spi.createWriterInstance();
            writer1.setOutput(out1);
            writer1.write(null, new IIOImage(SAMPLE_IMAGE, null, null), param);
            out1.close();
            writer1.dispose();
        }
        long end = System.nanoTime();
        long total = end - start;
        reportTime("Clib", total, LOOP);
    } catch (Throwable t) {
        if (LOGGER.isLoggable(Level.WARNING)) {
            LOGGER.warning(t.getLocalizedMessage());
        }
    } finally {
        if (out1 != null) {
            try {
                out1.close();
            } catch (Throwable t) {
            // 
            }
        }
    }
}
Also used : FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) ImageWriter(javax.imageio.ImageWriter) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 94 with ImageWriteParam

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

the class TIFFWriteTest method readWriteFromFileDirect.

@Test
public void readWriteFromFileDirect() throws IOException {
    // JAI.getDefaultInstance().getTileCache().setMemoryCapacity(512*1024*1024);
    // final TCTool tc= new TCTool((SunTileCache)JAI.getDefaultInstance().getTileCache());
    // new File("c:\\work\\dem30_final.tiff");
    final File inputFile = TestData.file(this, "test.tif");
    final File outputFile = TestData.temp(this, "testw.tif", true);
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 10, 10));
    TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    reader.setInput(new FileImageInputStream(inputFile));
    BufferedImage image = reader.read(0, param);
    // RenderedImage image = ImageReadDescriptor.create(new FileImageInputStream(inputFile),
    // Integer.valueOf(0), false, false, false, null, null, null,
    // reader, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, new ImageLayout().setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512)));
    final TIFFImageWriter writer = (TIFFImageWriter) new TIFFImageWriterSpi().createWriterInstance();
    final ImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault());
    writeParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
    writeParam.setTiling(512, 512, 0, 0);
    // for( int i=0;i<1;i++){
    writer.setOutput(new FileImageOutputStream(outputFile));
    // BUG
    // writer.write(metadata, new IIOImage(image, null, metadata), null);
    writer.addIIOWriteProgressListener(new IIOWriteProgressListener() {

        public void writeAborted(ImageWriter source) {
        // TODO Auto-generated method stub
        }

        public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
            assertTrue(imageIndex >= 0);
            assertTrue(thumbnailIndex == 0);
        }

        public void thumbnailProgress(ImageWriter source, float percentageDone) {
            assertTrue(percentageDone >= 0 && percentageDone <= 100);
        }

        public void thumbnailComplete(ImageWriter source) {
        // TODO Auto-generated method stub
        }

        public void imageStarted(ImageWriter source, int imageIndex) {
            assertTrue(imageIndex >= 0);
            LOGGER.info("imageStarted");
        }

        public void imageProgress(ImageWriter source, float percentageDone) {
            assertTrue(percentageDone >= 0 && percentageDone <= 100);
            LOGGER.info(percentageDone + "%");
        }

        public void imageComplete(ImageWriter source) {
            LOGGER.info("imageComplete");
        }
    });
    writer.write(null, new IIOImage(image, null, null), writeParam);
    // }
    writer.dispose();
    reader.reset();
    reader.setInput(new FileImageInputStream(outputFile));
    image = reader.read(0);
    Assert.assertEquals(10, image.getWidth());
    Assert.assertEquals(10, image.getHeight());
    image.flush();
    image = null;
    reader.dispose();
}
Also used : TIFFImageWriterSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriterSpi) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) TIFFImageWriteParam(it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam) TIFFImageWriter(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriter) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam) ImageWriteParam(javax.imageio.ImageWriteParam) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IIOWriteProgressListener(javax.imageio.event.IIOWriteProgressListener) IIOImage(javax.imageio.IIOImage) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) TIFFImageWriter(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriter) File(java.io.File) Test(org.junit.Test)

Example 95 with ImageWriteParam

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

Aggregations

ImageWriteParam (javax.imageio.ImageWriteParam)120 ImageWriter (javax.imageio.ImageWriter)100 IIOImage (javax.imageio.IIOImage)74 ImageOutputStream (javax.imageio.stream.ImageOutputStream)53 BufferedImage (java.awt.image.BufferedImage)51 File (java.io.File)51 IOException (java.io.IOException)38 Test (org.junit.Test)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ImageReadParam (javax.imageio.ImageReadParam)24 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)23 RenderedOp (javax.media.jai.RenderedOp)23 IIOMetadata (javax.imageio.metadata.IIOMetadata)22 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 Graphics2D (java.awt.Graphics2D)12 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)12 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)12 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)12