use of javax.imageio.stream.MemoryCacheImageInputStream in project jdk8u_jdk by JetBrains.
the class BooleanAttributes method test.
public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
iw.setOutput(ios);
ImageWriteParam param = null;
IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
Source src = new StreamSource(new StringReader(metaXml));
DOMResult dst = new DOMResult();
transform(src, dst);
Document doc = (Document) dst.getNode();
Element node = doc.getDocumentElement();
String metaFormat = node.getNodeName();
// Verify that the default metadata gets formatted correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, false);
meta.mergeTree(metaFormat, node);
// Verify that the merged metadata gets formatte correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, true);
iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
iw.dispose();
ios.close();
ImageReader ir = ImageIO.getImageReader(iw);
byte[] bytes = os.toByteArray();
if (bytes.length == 0)
throw new AssertionError("Zero length image file");
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
ir.setInput(iis);
if (useStreamMeta)
meta = ir.getStreamMetadata();
else
meta = ir.getImageMetadata(0);
// Verify again after writing and re-reading the image
verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project jdk8u_jdk by JetBrains.
the class ImageReaderReadAll method main.
public static void main(String[] argv) {
ImageReader ireader;
ImageReadParam irp;
IIOImage image;
BufferedImage bi;
BufferedImage bi_1;
BufferedImage bi_2;
ireader = new DummyImageReaderImpl(null);
MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(new ByteArrayInputStream(ba));
ireader.setInput(mciis);
irp = new ImageReadParam();
irp.setDestination(new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR));
try {
image = ireader.readAll(0, irp);
bi_1 = ireader.read(0, irp);
bi_2 = ireader.read(0);
} catch (java.io.IOException ee) {
throw new RuntimeException("Unexpected exception: " + ee);
}
bi = (BufferedImage) image.getRenderedImage();
if (bi.getType() != bi_1.getType()) {
throw new RuntimeException("Images have different type!");
}
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project jdk8u_jdk by JetBrains.
the class ReadBytesIIOByteBuffer method main.
public static void main(String[] argv) {
byte[] bar = { 1, 1, 1 };
InputStream is = new ByteArrayInputStream(bar);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
byte[] b = new byte[10];
IIOByteBuffer iiob = new IIOByteBuffer(b, 0, b.length);
try {
iis.readBytes(iiob, -1);
} catch (IndexOutOfBoundsException e) {
return;
} catch (Exception e) {
throw new RuntimeException("Unexpected exception: " + e);
}
throw new RuntimeException("No exception thrown for len < 0!");
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project jdk8u_jdk by JetBrains.
the class ReadFullyTest method main.
public static void main(String[] args) {
try {
byte[] b = { // low low
(byte) 0x11, // low low
(byte) 0x22, // low high
(byte) 0x44, // low high
(byte) 0x99, // high low
(byte) 0xAA, // high low
(byte) 0x33, // high high
(byte) 0xBB, // high high
(byte) 0xCC };
InputStream in = new ByteArrayInputStream(b);
ImageInputStream iin = new MemoryCacheImageInputStream(in);
short[] s = new short[b.length / 2];
char[] c = new char[b.length / 2];
int[] i = new int[b.length / 4];
long[] l = new long[b.length / 8];
float[] f = new float[b.length / 4];
double[] d = new double[b.length / 8];
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(s, 0, s.length);
expect(s[0] & 0xffff, 0x1122);
expect(s[1] & 0xffff, 0x4499);
expect(s[2] & 0xffff, 0xAA33);
expect(s[3] & 0xffff, 0xBBCC);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(s, 0, s.length);
expect(s[0] & 0xffff, 0x2211);
expect(s[1] & 0xffff, 0x9944);
expect(s[2] & 0xffff, 0x33AA);
expect(s[3] & 0xffff, 0xCCBB);
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(c, 0, c.length);
expect(c[0], 0x1122);
expect(c[1], 0x4499);
expect(c[2], 0xAA33);
expect(c[3], 0xBBCC);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(c, 0, c.length);
expect(c[0], 0x2211);
expect(c[1], 0x9944);
expect(c[2], 0x33AA);
expect(c[3], 0xCCBB);
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(i, 0, i.length);
expect(i[0] & 0xffffffff, 0x11224499);
expect(i[1] & 0xffffffff, 0xAA33BBCC);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(i, 0, i.length);
expect(i[0] & 0xffffffff, 0x99442211);
expect(i[1] & 0xffffffff, 0xCCBB33AA);
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(f, 0, f.length);
expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x11224499);
expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xAA33BBCC);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(f, 0, f.length);
expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x99442211);
expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xCCBB33AA);
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(l, 0, l.length);
expect(l[0], 0x11224499AA33BBCCL);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(l, 0, l.length);
expect(l[0], 0xCCBB33AA99442211L);
iin.seek(0L);
iin.setByteOrder(bigEndian);
iin.readFully(d, 0, d.length);
expect(Double.doubleToLongBits(d[0]), 0x11224499AA33BBCCL);
iin.seek(0L);
iin.setByteOrder(littleEndian);
iin.readFully(d, 0, d.length);
expect(Double.doubleToLongBits(d[0]), 0xCCBB33AA99442211L);
} catch (Exception ex) {
throw new RuntimeException("Got exception " + ex);
}
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project zm-mailbox by Zimbra.
the class NativeFormatter method getResizedImageData.
/**
* If the image stored in the {@code MimePart} exceeds the given width,
* shrinks the image and returns the shrunk data. If the
* image width is smaller than {@code maxWidth} or resizing is not supported,
* returns {@code null}.
*/
private static byte[] getResizedImageData(MimePart mp, Integer maxWidth, Integer maxHeight) throws IOException, MessagingException {
ImageReader reader = null;
ImageWriter writer = null;
InputStream in = null;
if (maxWidth == null)
maxWidth = LC.max_image_size_to_resize.intValue();
if (maxHeight == null)
maxHeight = LC.max_image_size_to_resize.intValue();
try {
// Get ImageReader for stream content.
reader = ImageUtil.getImageReader(Mime.getContentType(mp), mp.getFileName());
if (reader == null) {
log.debug("No ImageReader available.");
return null;
}
// Read message content.
in = mp.getInputStream();
reader.setInput(new MemoryCacheImageInputStream(in));
BufferedImage img = reader.read(0);
int width = img.getWidth(), height = img.getHeight();
if (width <= maxWidth && height <= maxHeight) {
log.debug("Image %dx%d is less than max %dx%d. Not resizing.", width, height, maxWidth, maxHeight);
return null;
}
// Resize.
writer = ImageIO.getImageWriter(reader);
if (writer == null) {
log.debug("No ImageWriter available.");
return null;
}
double ratio = Math.min((double) maxWidth / width, (double) maxHeight / height);
width *= ratio;
height *= ratio;
BufferedImage small = ImageUtil.resize(img, width, height);
ByteArrayOutputStream out = new ByteArrayOutputStream();
writer.setOutput(new MemoryCacheImageOutputStream(out));
writer.write(small);
return out.toByteArray();
} finally {
ByteUtil.closeStream(in);
if (reader != null) {
reader.dispose();
}
if (writer != null) {
writer.dispose();
}
}
}
Aggregations