Search in sources :

Example 1 with MemoryCacheImageInputStream

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);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 2 with MemoryCacheImageInputStream

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!");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 3 with MemoryCacheImageInputStream

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!");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IIOByteBuffer(javax.imageio.stream.IIOByteBuffer)

Example 4 with MemoryCacheImageInputStream

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);
    }
}
Also used : MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with MemoryCacheImageInputStream

use of javax.imageio.stream.MemoryCacheImageInputStream in project jdk8u_jdk by JetBrains.

the class ItxtUtf8Test method runTest.

public static void runTest(boolean dump, boolean truncate) throws Exception {
    String format = "javax_imageio_png_1.0";
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
    DOMImplementationRegistry registry;
    registry = DOMImplementationRegistry.newInstance();
    DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
    Document doc = impl.createDocument(null, format, null);
    Element root, itxt, entry;
    root = doc.getDocumentElement();
    root.appendChild(itxt = doc.createElement("iTXt"));
    itxt.appendChild(entry = doc.createElement("iTXtEntry"));
    entry.setAttribute("keyword", "verbatim");
    entry.setAttribute("compressionFlag", "false");
    entry.setAttribute("compressionMethod", "0");
    entry.setAttribute("languageTag", "x-circled");
    entry.setAttribute("translatedKeyword", VERBATIM);
    entry.setAttribute("text", TEXT);
    itxt.appendChild(entry = doc.createElement("iTXtEntry"));
    entry.setAttribute("keyword", "compressed");
    entry.setAttribute("compressionFlag", "true");
    entry.setAttribute("compressionMethod", "0");
    entry.setAttribute("languageTag", "x-circled");
    entry.setAttribute("translatedKeyword", COMPRESSED);
    entry.setAttribute("text", TEXT);
    meta.mergeTree(format, root);
    iw.write(new IIOImage(img, null, meta));
    iw.dispose();
    byte[] bytes = os.toByteArray();
    if (dump)
        System.out.write(bytes);
    if (findBytes(VBYTES, bytes) < 0)
        throw new AssertionError("verbatim block not found");
    if (findBytes(CBYTES, bytes) < 0)
        throw new AssertionError("compressed block not found");
    int length = bytes.length;
    if (truncate)
        length = findBytes(VBYTES, bytes) + 32;
    ImageReader ir = ImageIO.getImageReader(iw);
    ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, length);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    meta = ir.getImageMetadata(0);
    Node node = meta.getAsTree(format);
    for (node = node.getFirstChild(); !"iTXt".equals(node.getNodeName()); node = node.getNextSibling()) ;
    boolean verbatimSeen = false, compressedSeen = false;
    for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) {
        entry = (Element) node;
        String keyword = entry.getAttribute("keyword");
        String translatedKeyword = entry.getAttribute("translatedKeyword");
        String text = entry.getAttribute("text");
        if ("verbatim".equals(keyword)) {
            if (verbatimSeen)
                throw new AssertionError("Duplicate");
            verbatimSeen = true;
            if (!VERBATIM.equals(translatedKeyword))
                throw new AssertionError("Wrong translated keyword");
            if (!TEXT.equals(text))
                throw new AssertionError("Wrong text");
        } else if ("compressed".equals(keyword)) {
            if (compressedSeen)
                throw new AssertionError("Duplicate");
            compressedSeen = true;
            if (!COMPRESSED.equals(translatedKeyword))
                throw new AssertionError("Wrong translated keyword");
            if (!TEXT.equals(text))
                throw new AssertionError("Wrong text");
        } else {
            throw new AssertionError("Unexpected keyword");
        }
    }
    if (!(verbatimSeen && compressedSeen))
        throw new AssertionError("Missing chunk");
}
Also used : Element(org.w3c.dom.Element) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) Node(org.w3c.dom.Node) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) DOMImplementation(org.w3c.dom.DOMImplementation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) DOMImplementationRegistry(org.w3c.dom.bootstrap.DOMImplementationRegistry) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Aggregations

MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)48 ImageInputStream (javax.imageio.stream.ImageInputStream)28 ByteArrayInputStream (java.io.ByteArrayInputStream)22 ImageReader (javax.imageio.ImageReader)21 InputStream (java.io.InputStream)18 BufferedImage (java.awt.image.BufferedImage)16 IOException (java.io.IOException)16 FileInputStream (java.io.FileInputStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 EOFException (java.io.EOFException)5 ImageReadParam (javax.imageio.ImageReadParam)5 MemoryCacheImageOutputStream (javax.imageio.stream.MemoryCacheImageOutputStream)5 Point (java.awt.Point)4 Size (org.olat.core.commons.services.image.Size)4 Point2D (java.awt.geom.Point2D)3 ArrayList (java.util.ArrayList)3 IIOImage (javax.imageio.IIOImage)3 ImageWriter (javax.imageio.ImageWriter)3 FileImageInputStream (javax.imageio.stream.FileImageInputStream)3 UnavailableException (javax.servlet.UnavailableException)3