use of javax.imageio.IIOImage 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 javax.imageio.IIOImage 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.IIOImage 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.IIOImage in project imageio-ext by geosolutions-it.
the class TIFFBaseJPEGCompressor method encode.
public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
if (this.JPEGWriter == null) {
throw new IIOException("JPEG writer has not been initialized!");
}
if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
}
// Set the stream.
ImageOutputStream ios;
// usingCodecLib && !writeAbbreviatedStream
long initialStreamPosition;
if (usingCodecLib && !writeAbbreviatedStream) {
ios = stream;
initialStreamPosition = stream.getStreamPosition();
} else {
// is using a stream on the native side which cannot be reset.
if (baos == null) {
baos = new IIOByteArrayOutputStream();
} else {
baos.reset();
}
ios = new MemoryCacheImageOutputStream(baos);
initialStreamPosition = 0L;
}
JPEGWriter.setOutput(ios);
// Create a DataBuffer.
DataBufferByte dbb;
if (off == 0 || usingCodecLib) {
dbb = new DataBufferByte(b, b.length);
} else {
//
// Workaround for bug in core Java Image I/O JPEG
// ImageWriter which cannot handle non-zero offsets.
//
int bytesPerSegment = scanlineStride * height;
byte[] btmp = new byte[bytesPerSegment];
System.arraycopy(b, off, btmp, 0, bytesPerSegment);
dbb = new DataBufferByte(btmp, bytesPerSegment);
off = 0;
}
// Set up the ColorSpace.
int[] offsets;
ColorSpace cs;
if (bitsPerSample.length == 3) {
offsets = new int[] { off, off + 1, off + 2 };
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
} else {
offsets = new int[] { off };
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
}
// Create the ColorModel.
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
// Create the SampleModel.
SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
// Create the WritableRaster.
WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
// Create the BufferedImage.
BufferedImage bi = new BufferedImage(cm, wras, false, null);
// Get the pruned JPEG image metadata (may be null).
IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
// Compress the image into the output stream.
int compDataLength;
if (usingCodecLib && !writeAbbreviatedStream) {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
} else {
if (writeAbbreviatedStream) {
// Write abbreviated JPEG stream
// First write the tables-only data.
JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
ios.flush();
// Rewind to the beginning of the byte array.
baos.reset();
// Write the abbreviated image data.
IIOImage image = new IIOImage(bi, null, imageMetadata);
JPEGWriter.writeToSequence(image, JPEGParam);
JPEGWriter.endWriteSequence();
} else {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
}
compDataLength = baos.size();
baos.writeTo(stream);
baos.reset();
}
return compDataLength;
}
use of javax.imageio.IIOImage 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"));
}
}
}
Aggregations