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