use of javax.imageio.ImageWriteParam in project imageio-ext by geosolutions-it.
the class CustomUShortImageTypesTest method testCustomUShortImage.
@Test
public void testCustomUShortImage() throws Exception {
BufferedImage bi = ImageTypeSpecifier.createGrayscale(nbits, DataBuffer.TYPE_USHORT, false).createBufferedImage(size, size);
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();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
float quality = 5f / 9 - 1;
new PNGWriter().writePNG(bi, bos, -quality, FilterType.FILTER_NONE);
BufferedImage read = ImageIO.read(new ByteArrayInputStream(bos.toByteArray()));
ImageAssert.assertImagesEqual(bi, 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())));
}
use of javax.imageio.ImageWriteParam in project imageio-ext by geosolutions-it.
the class PngSuiteImagesTest method roundTripPNGJ.
private void roundTripPNGJ(BufferedImage original, RenderedImage source) throws Exception {
// write the PNG
ByteArrayOutputStream bos = new ByteArrayOutputStream();
float quality = 4f / 9 - 1;
new PNGWriter().writePNG(original, bos, -quality, FilterType.FILTER_NONE);
// write the output to file for eventual visual comparison
byte[] bytes = bos.toByteArray();
writeToFile(new File("./target/roundTripNone", sourceFile.getName()), bytes);
// read it back
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bis);
ImageAssert.assertImagesEqual(original, image);
// 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(original, null, null), wp);
writer.dispose();
ImageAssert.assertImagesEqual(original, ImageIO.read(new ByteArrayInputStream(bos.toByteArray())));
}
use of javax.imageio.ImageWriteParam in project imageio-ext by geosolutions-it.
the class BufferedImageTypesTest method compareImage.
@Test
public void compareImage() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
float quality = 4f / 9 - 1;
new PNGWriter().writePNG(image, bos, -quality, FilterType.FILTER_NONE);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
BufferedImage readBack = ImageIO.read(bis);
boolean success = false;
try {
ImageAssert.assertImagesEqual(image, readBack);
success = true;
} finally {
if (!success) {
ImageIO.write(image, "PNG", new File("./target/" + name + "_expected.png"));
ImageIO.write(readBack, "PNG", new File("./target/" + name + "_actual.png"));
}
}
// 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(image, null, null), wp);
writer.dispose();
bis = new ByteArrayInputStream(bos.toByteArray());
readBack = ImageIO.read(bis);
success = false;
try {
ImageAssert.assertImagesEqual(image, readBack);
success = true;
} finally {
if (!success) {
ImageIO.write(image, "PNG", new File("./target/" + name + "_expected.png"));
ImageIO.write(readBack, "PNG", new File("./target/" + name + "_actual.png"));
}
}
}
use of javax.imageio.ImageWriteParam in project imageio-ext by geosolutions-it.
the class JP2KWriteTest method write_Qstep.
// ////////////////////////////////////////////////////////////////////////
//
// Testing "Qstep" Create Option.
//
// ////////////////////////////////////////////////////////////////////////
@Test
public void write_Qstep() throws IOException, FileNotFoundException {
if (!isJp2KakDriverAvailable)
return;
LOGGER.info("Testing JP2 Write operation with Qstep option setting");
// //
// Preparing input/output files
// //
final File inputFile = TestData.file(this, testFileName);
Assert.assertTrue(inputFile.exists());
// Output files resulting from 3 different values of the same create
// option. In this test, the create option is Qstep.
final float firstQstepParam = 0.2f;
final float secondQstepParam = 1.7f;
final String fileName1 = new StringBuffer("Qstep-").append(Float.toString(firstQstepParam)).append("f-.jp2").toString();
final String fileName2 = new StringBuffer("Qstep-").append(Float.toString(secondQstepParam)).append("f-.jp2").toString();
final String fileName3 = "Qstep-Default-.jp2";
final File outputFile1 = TestData.temp(this, fileName1, deleteTempFilesOnExit);
final File outputFile2 = TestData.temp(this, fileName2, deleteTempFilesOnExit);
final File outputFile3 = TestData.temp(this, fileName3, deleteTempFilesOnExit);
// //
// Preparing to read
// //
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
if (ENABLE_SUBSAMPLING) {
ImageReadParam readParam = new ImageReadParam();
readParam.setSourceSubsampling(4, 4, 0, 0);
pbjImageRead.setParameter("readParam", readParam);
}
pbjImageRead.setParameter("Reader", new JP2GDALKakaduImageReaderSpi().createReaderInstance());
RenderedOp image = JAI.create("ImageRead", pbjImageRead);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (1ST version of the create option test)
//
// ////////////////////////////////////////////////////////////////////
// Setting output and writer
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
pbjImageWrite.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile1));
ImageWriter writer = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Writer", writer);
// Specifying image source to write
pbjImageWrite.addSource(image);
ImageWriteParam param = writer.getDefaultWriteParam();
// Specifying the required create option
((JP2GDALKakaduImageWriteParam) param).setQstep(firstQstepParam);
pbjImageWrite.setParameter("writeParam", param);
// Writing
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (2ND version of the create option test)
//
// ////////////////////////////////////////////////////////////////////
// Setting output and writer
final ParameterBlockJAI pbjImageWrite2 = new ParameterBlockJAI("ImageWrite");
pbjImageWrite2.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile2));
ImageWriter writer2 = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite2.setParameter("Writer", writer2);
// Specifying image source to write
pbjImageWrite2.addSource(image);
ImageWriteParam param2 = writer2.getDefaultWriteParam();
// Specifying the required create option
((JP2GDALKakaduImageWriteParam) param2).setQstep(secondQstepParam);
pbjImageWrite2.setParameter("writeParam", param2);
// Writing
final RenderedOp op2 = JAI.create("ImageWrite", pbjImageWrite2);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (3ND version of the create option - DEFAULT)
//
// ////////////////////////////////////////////////////////////////////
// Setting output and writer
final ParameterBlockJAI pbjImageWrite3 = new ParameterBlockJAI("ImageWrite");
pbjImageWrite3.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile3));
ImageWriter writer3 = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite3.setParameter("Writer", writer3);
// Specifying image source to write
pbjImageWrite3.addSource(image);
ImageWriteParam param3 = writer3.getDefaultWriteParam();
// Specifying the required create option
pbjImageWrite3.setParameter("writeParam", param3);
// Writing
final RenderedOp op3 = JAI.create("ImageWrite", pbjImageWrite3);
}
use of javax.imageio.ImageWriteParam in project imageio-ext by geosolutions-it.
the class JP2KWriteTest method write_ORGtparts.
// ////////////////////////////////////////////////////////////////////////
//
// Testing "ORGtparts" Create Option.
//
// ////////////////////////////////////////////////////////////////////////
@Test
public void write_ORGtparts() throws IOException, FileNotFoundException {
if (!isJp2KakDriverAvailable)
return;
LOGGER.info("Testing JP2 Write operation with ORGtparts option setting");
// //
// Preparing input/output files
// //
final File inputFile = TestData.file(this, testFileName);
Assert.assertTrue(inputFile.exists());
// //
// Preparing to read
// //
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
if (ENABLE_SUBSAMPLING) {
ImageReadParam readParam = new ImageReadParam();
readParam.setSourceSubsampling(8, 8, 0, 0);
pbjImageRead.setParameter("readParam", readParam);
}
pbjImageRead.setParameter("Reader", new JP2GDALKakaduImageReaderSpi().createReaderInstance());
RenderedOp image = JAI.create("ImageRead", pbjImageRead);
// ////////////////////////////////////////////////////////////////////
//
// preparing to write (i-TH version of the create option test)
//
// ////////////////////////////////////////////////////////////////////
final String[] createVersions = { "R", "C", "L", "R|C", "R|L", "L|C", "R|L|C" };
final String[] filenameVersions = { "R", "C", "L", "RC", "RL", "LC", "RLC" };
final int numberOfVersions = createVersions.length;
for (int i = 0; i < numberOfVersions; i++) {
// Output files resulting from different values of the same create
// option. In this test, the create option is ORGtparts.
final String filenameVersion = filenameVersions[i];
final StringBuffer fileName = new StringBuffer("ORGtparts").append(filenameVersion).append("-.jp2");
final File outputFile = TestData.temp(this, fileName.toString(), deleteTempFilesOnExit);
// Setting output and writer
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
pbjImageWrite.setParameter("Output", new FileImageOutputStreamExtImpl(outputFile));
ImageWriter writer = new JP2GDALKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Writer", writer);
// Specifying image source to write
pbjImageWrite.addSource(image);
ImageWriteParam param = writer.getDefaultWriteParam();
// Specifying the required create option
((JP2GDALKakaduImageWriteParam) param).setORGgen_plt("yes");
((JP2GDALKakaduImageWriteParam) param).setLayers(10);
((JP2GDALKakaduImageWriteParam) param).setCorder("LRCP");
((JP2GDALKakaduImageWriteParam) param).setTiling(1024, 1024);
((JP2GDALKakaduImageWriteParam) param).setORGtparts(createVersions[i]);
pbjImageWrite.setParameter("writeParam", param);
// Writing
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
}
}
Aggregations