use of it.geosolutions.imageio.plugins.png.PNGImageWriterSPI 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.PNGImageWriterSPI 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 it.geosolutions.imageio.plugins.png.PNGImageWriterSPI 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.PNGImageWriterSPI 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())));
}
use of it.geosolutions.imageio.plugins.png.PNGImageWriterSPI 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())));
}
Aggregations