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