use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class GeoTiffVrtTest method write.
/**
* Test Writing capabilities.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void write() throws IOException, FileNotFoundException {
if (!isGDALAvailable) {
return;
}
final File outputFile = TestData.temp(this, "writetest.tif", false);
outputFile.deleteOnExit();
final File inputFile = TestData.file(this, "utmByte.tif.vrt");
ImageReadParam rparam = new ImageReadParam();
rparam.setSourceRegion(new Rectangle(1, 1, 300, 500));
rparam.setSourceSubsampling(1, 2, 0, 0);
ImageReader reader = new VRTImageReaderSpi().createReaderInstance();
reader.setInput(inputFile);
final IIOMetadata metadata = reader.getImageMetadata(0);
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("reader", reader);
pbjImageRead.setParameter("readParam", rparam);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "geotiff");
// ////////////////////////////////////////////////////////////////
// preparing to write
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("Output", outputFile);
pbjImageWrite.setParameter("writer", writer);
pbjImageWrite.setParameter("ImageMetadata", metadata);
pbjImageWrite.setParameter("Transcode", false);
ImageWriteParam param = new ImageWriteParam(Locale.getDefault());
param.setSourceRegion(new Rectangle(10, 10, 100, 100));
param.setSourceSubsampling(2, 1, 0, 0);
pbjImageWrite.setParameter("writeParam", param);
pbjImageWrite.addSource(image);
final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
writer2.dispose();
// ////////////////////////////////////////////////////////////////
// preparing to read again
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
pbjImageReRead.setParameter("Input", outputFile);
pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image2, "geotif2");
else
Assert.assertNotNull(image2.getTiles());
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class CustomByteIndexImageTypesTest method testCustomIndexedImage.
@Test
public void testCustomIndexedImage() throws Exception {
byte[] colors = new byte[ncolors];
for (int i = 0; i < ncolors; i++) {
colors[i] = (byte) i;
}
int nbits;
if (ncolors <= 2) {
nbits = 1;
} else {
nbits = (int) Math.ceil(Math.log(ncolors) / Math.log(2));
if ((nbits & (nbits - 1)) != 0) {
int nextPower = (int) (Math.floor(Math.log(nbits) / Math.log(2)) + 1);
nbits = (int) Math.pow(2, nextPower);
}
}
IndexColorModel icm = new IndexColorModel(nbits, ncolors, colors, colors, colors);
SampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, size, size, nbits);
int pixelsPerByte = 8 / nbits;
int bytesPerRow = (int) Math.max(1, Math.ceil(1d * size / pixelsPerByte));
int bytes = bytesPerRow * size;
DataBufferByte dataBuffer = new DataBufferByte(bytes);
WritableRaster wr = (WritableRaster) Raster.createWritableRaster(sm, dataBuffer, new Point(0, 0));
BufferedImage bi = new BufferedImage(icm, wr, false, null);
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.ImageWriter in project imageio-ext by geosolutions-it.
the class SPITest method testSPI.
@Test
public void testSPI() throws Exception {
boolean found = false;
Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("PNG");
while (it.hasNext()) {
ImageWriter wr = it.next();
if (wr.getOriginatingProvider() instanceof PNGImageWriterSPI) {
found = true;
break;
}
}
// did we find it
assertTrue("Unable to find PNGImageWriterSPI", found);
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class BufferedImageChildTest method testSubImage.
private void testSubImage(int x, int y, int w, int h) throws Exception {
BufferedImage bi = getSample();
// ImageAssert.showImage("Original", 2000, bi);
BufferedImage subimage = bi.getSubimage(x, y, w, h);
// ImageAssert.showImage("Subimage", 2000, subimage);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
float quality = 4f / 9 - 1;
new PNGWriter().writePNG(subimage, bos, -quality, FilterType.FILTER_NONE);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
BufferedImage readBack = ImageIO.read(bis);
// ImageAssert.showImage("ReadBack", 2000, readBack);
ImageAssert.assertImagesEqual(subimage, readBack);
// 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(subimage, null, null), wp);
writer.dispose();
bis = new ByteArrayInputStream(bos.toByteArray());
readBack = ImageIO.read(bis);
// ImageAssert.showImage("ReadBack", 2000, readBack);
ImageAssert.assertImagesEqual(subimage, readBack);
}
use of javax.imageio.ImageWriter in project imageio-ext by geosolutions-it.
the class JP2KKakaduWriteTest method write.
private static synchronized void write(String file, RenderedImage bi, boolean codeStreamOnly, double quality, boolean useJAI, JP2KKakaduImageWriteParam addParam) throws IOException {
writeOperations++;
file += "_Q" + quality + (codeStreamOnly ? ".j2c" : ".jp2");
final ImageOutputStream outputStream = ImageIO.createImageOutputStream(new File(file));
JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
param.setQuality(quality);
param.setWriteCodeStreamOnly(codeStreamOnly);
if (addParam != null) {
param.setSourceRegion(addParam.getSourceRegion());
param.setSourceSubsampling(addParam.getSourceXSubsampling(), addParam.getSourceYSubsampling(), addParam.getSubsamplingXOffset(), addParam.getSubsamplingYOffset());
param.setCLevels(addParam.getCLevels());
param.setQualityLayers(addParam.getQualityLayers());
}
if (!useJAI) {
final ImageWriter writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
// final ImageWriter writer = new
// J2KImageWriterSpi().createWriterInstance();
writer.setOutput(outputStream);
// J2KImageWriteParam ioparam = (J2KImageWriteParam)
// writer.getDefaultWriteParam();
// ioparam.setWriteCodeStreamOnly(true);
// ioparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// ioparam.setCompressionType("JPEG2000");
// ioparam.setCompressionQuality((float)quality);
// ioparam.setEncodingRate((quality)*24);
// writer.write(null, new IIOImage(bi, null, null), ioparam);
writer.write(null, new IIOImage(bi, null, null), param);
writer.dispose();
} else {
final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
final ImageWriter writer = new JP2KKakaduImageWriterSpi().createWriterInstance();
pbjImageWrite.setParameter("writer", writer);
pbjImageWrite.setParameter("output", outputStream);
pbjImageWrite.setParameter("writeParam", param);
pbjImageWrite.addSource(bi);
RenderedOp image = JAI.create("ImageWrite", pbjImageWrite);
}
}
Aggregations