Search in sources :

Example 1 with ImageOutputStreamAdapter

use of it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter in project imageio-ext by geosolutions-it.

the class TestImageOutputStream method test.

@Test
public void test() throws FileNotFoundException, IOException {
    // read test image
    RenderedImage image = JAI.create("ImageRead", TestData.file(this, "sample.jpeg"));
    // try to encode a jpeg
    BufferedImage test = null;
    ImageOutputStream out = null;
    try {
        File jpegOut = TestData.temp(this, "test.jpeg", true);
        out = new ImageOutputStreamAdapter(new FileOutputStream(jpegOut));
        ImageIO.write(image, "JPEG", out);
        test = ImageIO.read(jpegOut);
        Assert.assertNotNull(test);
    } finally {
        if (test != null) {
            test.flush();
            test = null;
        }
    }
    // try to encode a png
    test = null;
    out = null;
    try {
        File pngO = TestData.temp(this, "test.png", true);
        out = new ImageOutputStreamAdapter(new FileOutputStream(pngO));
        ImageIO.write(image, "PNG", out);
        // we should not get here!!!
        Assert.assertNotNull(null);
    } catch (Exception e) {
    } finally {
        if (test != null) {
            test.flush();
            test = null;
        }
    }
    // try to encode a tiff
    test = null;
    out = null;
    try {
        File tiffO = TestData.temp(this, "test.tiff", true);
        out = new ImageOutputStreamAdapter(new FileOutputStream(tiffO));
        ImageIO.write(image, "tiff", out);
        // we should not get here!!!
        Assert.assertNotNull(null);
    } catch (Exception e) {
    } finally {
        if (test != null) {
            test.flush();
            test = null;
        }
    }
    // try to encode a bmp
    test = null;
    out = null;
    try {
        File bmpO = TestData.temp(this, "test.bmp", true);
        out = new ImageOutputStreamAdapter(new FileOutputStream(bmpO));
        ImageIO.write(image, "bmp", out);
        // we should not get here!!!
        Assert.assertNotNull(null);
    } catch (Exception e) {
    } finally {
        if (test != null) {
            test.flush();
            test = null;
        }
    }
    // try to encode a gif
    test = null;
    out = null;
    try {
        File gifO = TestData.temp(this, "test.gif", true);
        out = new ImageOutputStreamAdapter(new FileOutputStream(gifO));
        ImageIO.write(image, "gif", out);
        test = ImageIO.read(gifO);
        Assert.assertNotNull(test);
    } finally {
        if (test != null) {
            test.flush();
            test = null;
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) RenderedImage(java.awt.image.RenderedImage) ImageOutputStreamAdapter(it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 2 with ImageOutputStreamAdapter

use of it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter in project imageio-ext by geosolutions-it.

the class JPEGWriterCompareTest method writeJPEG.

/**
 * Writes outs the image contained into this {@link ImageWorker} as a JPEG using the provided
 * destination , compression and compression rate.
 * <p>
 * The destination object can be anything providing that we have an {@link ImageOutputStreamSpi}
 * that recognizes it.
 *
 * @param destination
 *            where to write the internal {@link #image} as a JPEG.
 * @param compression
 *            algorithm.
 * @param compressionRate
 *            percentage of compression.
 * @param nativeAcc
 *            should we use native acceleration.
 * @return this {@link ImageWorker}.
 * @throws IOException
 *             In case an error occurs during the search for an {@link ImageOutputStream} or
 *             during the eoncding process.
 */
public static final void writeJPEG(final RenderedImage image, final Object destination, final String compression, final float compressionRate, final boolean nativeAcc) throws IOException {
    ImageWriterSpi spi = nativeAcc ? clibSPI : turboSPI;
    ImageWriter writer = spi.createWriterInstance();
    // Compression is available on both lib
    final ImageWriteParam iwp = writer.getDefaultWriteParam();
    final ImageOutputStream outStream = nativeAcc ? new MemoryCacheImageOutputStream((OutputStream) destination) : new ImageOutputStreamAdapter((OutputStream) destination);
    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwp.setCompressionType("JPEG");
    // We can control quality here.
    iwp.setCompressionQuality(compressionRate);
    if (nativeAcc) {
        // Lossy compression.
        iwp.setCompressionType(compression);
    }
    try {
        if (iwp instanceof JPEGImageWriteParam) {
            final JPEGImageWriteParam param = (JPEGImageWriteParam) iwp;
            param.setOptimizeHuffmanTables(true);
            param.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
        }
        writer.setOutput(outStream);
        writer.write(null, new IIOImage(image, null, null), iwp);
    } finally {
        if (writer != null) {
            try {
                writer.dispose();
            } catch (Throwable e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
        if (outStream != null) {
            try {
                ((ImageOutputStream) outStream).close();
            } catch (Throwable e) {
                System.out.println(e.getLocalizedMessage());
            }
        }
    }
}
Also used : MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) ImageWriter(javax.imageio.ImageWriter) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStreamAdapter(it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 3 with ImageOutputStreamAdapter

use of it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter in project imageio-ext by geosolutions-it.

the class JPEGWriterSpeedTest method testJPEGTurbo.

@Test
@Ignore
public void testJPEGTurbo() throws FileNotFoundException, IOException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    assumeTrue(!SKIP_TESTS);
    if (!TurboJpegUtilities.isTurboJpegAvailable()) {
        LOGGER.warning(ERROR_LIB_MESSAGE);
        return;
    }
    ImageWriterSpi spi = turboSPI;
    String fileName = null;
    OutputStream os = null;
    ImageOutputStream out1 = null;
    TurboJpegImageWriteParam param = new TurboJpegImageWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(0.75f);
    try {
        fileName = OUTPUT_FOLDER + ((SAMPLE_IMAGE.getSampleModel().getNumBands() == 1) ? "GRAY" : "RGBTurbo") + INPUT_FILE.getName() + ".jpeg";
        final File file = new File(fileName);
        os = new FileOutputStream(file);
        out1 = new ImageOutputStreamAdapter(os);
        TurboJpegImageWriter writer1 = (TurboJpegImageWriter) spi.createWriterInstance();
        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
            os = new FileOutputStream(file);
            out1 = new ImageOutputStreamAdapter(os);
            writer1 = (TurboJpegImageWriter) 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("Turbo", 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 : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) IIOImage(javax.imageio.IIOImage) FileOutputStream(java.io.FileOutputStream) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) ImageOutputStreamAdapter(it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter) File(java.io.File) ImageOutputStream(javax.imageio.stream.ImageOutputStream) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ImageOutputStreamAdapter (it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter)3 FileOutputStream (java.io.FileOutputStream)3 ImageOutputStream (javax.imageio.stream.ImageOutputStream)3 File (java.io.File)2 OutputStream (java.io.OutputStream)2 IIOImage (javax.imageio.IIOImage)2 ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)2 Test (org.junit.Test)2 BufferedImage (java.awt.image.BufferedImage)1 RenderedImage (java.awt.image.RenderedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ImageWriteParam (javax.imageio.ImageWriteParam)1 ImageWriter (javax.imageio.ImageWriter)1 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)1 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)1 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)1 Ignore (org.junit.Ignore)1