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;
}
}
}
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());
}
}
}
}
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) {
//
}
}
}
}
Aggregations