use of it.geosolutions.imageio.plugins.png.PNGWriter 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 it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.
the class PNGWriterTest method testWriter.
@Test
public void testWriter() {
PNGWriter writer = new PNGWriter();
OutputStream out = null;
try {
// read test image
BufferedImage read = ImageIO.read(TestData.file(this, "sample.jpeg"));
File pngOut = TestData.temp(this, "test.png", true);
out = new FileOutputStream(pngOut);
writer.writePNG(read, out, 1, FilterType.FILTER_NONE);
BufferedImage test = ImageIO.read(pngOut);
assertNotNull(test);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
out = null;
}
}
}
use of it.geosolutions.imageio.plugins.png.PNGWriter in project imageio-ext by geosolutions-it.
the class PNGWriterTest method testTeXt.
@Test
public void testTeXt() throws Exception {
PNGWriter writer = new PNGWriter();
OutputStream out = null;
File pngOut = null;
final String title = "Title";
final String description = "Sample Description";
final String software = "ImageIO-Ext";
final String author = "Me";
try {
// read test image
BufferedImage read = ImageIO.read(TestData.file(this, "sample.jpeg"));
pngOut = TestData.temp(this, "test.png", true);
out = new FileOutputStream(pngOut);
Map<String, String> textMetadata = new HashMap<String, String>();
textMetadata.put("Title", title);
textMetadata.put("Author", author);
textMetadata.put("Software", software);
textMetadata.put("Description", description);
writer.writePNG(read, out, 1, FilterType.FILTER_NONE, textMetadata);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
BufferedImage test = ImageIO.read(pngOut);
assertNotNull(test);
PngReader reader = null;
try {
reader = new PngReader(pngOut);
reader.readSkippingAllRows();
PngMetadata metadata = reader.getMetadata();
assertNotNull(metadata);
assertEquals(title, metadata.getTxtForKey("Title"));
assertEquals(description, metadata.getTxtForKey("Description"));
assertEquals(author, metadata.getTxtForKey("Author"));
assertEquals(software, metadata.getTxtForKey("Software"));
} finally {
if (reader != null) {
reader.close();
}
}
}
use of it.geosolutions.imageio.plugins.png.PNGWriter 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 it.geosolutions.imageio.plugins.png.PNGWriter 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