Search in sources :

Example 21 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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 22 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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 23 with ImageWriterSpi

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

Example 24 with ImageWriterSpi

use of javax.imageio.spi.ImageWriterSpi 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 25 with ImageWriterSpi

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

Aggregations

ImageWriterSpi (javax.imageio.spi.ImageWriterSpi)28 ImageWriter (javax.imageio.ImageWriter)13 File (java.io.File)9 IIOImage (javax.imageio.IIOImage)8 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 ImageWriteParam (javax.imageio.ImageWriteParam)6 IIORegistry (javax.imageio.spi.IIORegistry)6 ImageOutputStream (javax.imageio.stream.ImageOutputStream)5 IOException (java.io.IOException)4 BufferedImage (java.awt.image.BufferedImage)3 FileOutputStream (java.io.FileOutputStream)3 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)3 Ignore (org.junit.Ignore)3 ImageOutputStreamAdapter (it.geosolutions.imageio.stream.output.ImageOutputStreamAdapter)2 OutputStream (java.io.OutputStream)2 ImageReader (javax.imageio.ImageReader)2 JPEGImageWriteParam (javax.imageio.plugins.jpeg.JPEGImageWriteParam)2 ImageReaderWriterSpi (javax.imageio.spi.ImageReaderWriterSpi)2