Search in sources :

Example 6 with ImageInputStream

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

the class ValidWbmpTest method main.

public static void main(String[] args) {
    try {
        String[] formats = { "JPEG", "PNG", "BMP" };
        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = img.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.black);
        g.fillRect(10, 10, 80, 80);
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("WBMP").next();
        if (ir == null) {
            throw new RuntimeException("No readers for WBMP format!");
        }
        for (int i = 0; i < formats.length; i++) {
            System.out.println("Test " + formats[i] + " stream...");
            boolean passed = false;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, formats[i], baos);
            baos.close();
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ImageInputStream iis = null;
            iis = ImageIO.createImageInputStream(bais);
            ir.setInput(iis);
            try {
                BufferedImage res = ir.read(0);
            } catch (IIOException e) {
                StackTraceElement[] stack = e.getStackTrace();
                if (ir.getClass().getName().equals(stack[0].getClassName()) && "readHeader".equals(stack[0].getMethodName())) {
                    passed = true;
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
            if (!passed) {
                throw new RuntimeException("Test failed!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IIOException(javax.imageio.IIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOException(javax.imageio.IIOException) Graphics(java.awt.Graphics) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader)

Example 7 with ImageInputStream

use of javax.imageio.stream.ImageInputStream 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)

Example 8 with ImageInputStream

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

the class CrashAfterDispose method main.

public static void main(String[] args) throws IOException {
    InputStream bais = new ByteArrayInputStream(new byte[100]);
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    // find the JPEG reader
    ImageReader reader = null;
    Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
    if (readers.hasNext()) {
        reader = (ImageReader) readers.next();
    } else {
        throw new RuntimeException("Unable to find a reader!");
    }
    // dispose the reader, then poke and prod it... the reader should
    // throw exceptions (which will be caught by this test), but it
    // should not crash the VM
    reader.dispose();
    try {
        reader.setInput(iis);
    } catch (IllegalStateException e) {
    }
    try {
        reader.read(0);
    } catch (IllegalStateException e) {
    }
    try {
        reader.abort();
    } catch (IllegalStateException e) {
    }
    try {
        reader.reset();
    } catch (IllegalStateException e) {
    }
    try {
        reader.dispose();
    } catch (IllegalStateException e) {
    }
    // find the JPEG writer
    ImageWriter writer = null;
    Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
    if (writers.hasNext()) {
        writer = (ImageWriter) writers.next();
    } else {
        throw new RuntimeException("Unable to find a writer!");
    }
    // set up output stream
    OutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    // dispose the writer, then poke and prod it... the writer should
    // throw exceptions (which will be caught by this test), but it
    // should not crash the VM
    writer.dispose();
    try {
        writer.setOutput(ios);
    } catch (IllegalStateException e) {
    }
    try {
        writer.write(bi);
    } catch (IllegalStateException e) {
    }
    try {
        writer.abort();
    } catch (IllegalStateException e) {
    }
    try {
        writer.reset();
    } catch (IllegalStateException e) {
    }
    try {
        writer.dispose();
    } catch (IllegalStateException e) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage)

Example 9 with ImageInputStream

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

the class TruncatedImageWarningTest method main.

public static void main(String[] args) throws IOException {
    String sep = System.getProperty("file.separator");
    String dir = System.getProperty("test.src", ".");
    String filePath = dir + sep + fileName;
    System.out.println("Test file: " + filePath);
    File f = new File(filePath);
    ImageInputStream in = ImageIO.createImageInputStream(f);
    ImageReader reader = ImageIO.getImageReaders(in).next();
    TruncatedImageWarningTest twt = new TruncatedImageWarningTest();
    reader.addIIOReadWarningListener(twt);
    reader.setInput(in);
    reader.read(0);
    if (!twt.receivedWarning) {
        throw new RuntimeException("No expected warning");
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Example 10 with ImageInputStream

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

the class RleEncodingTest method doTest.

private static void doTest(BufferedImage src, String compressionType, int compressionMode) throws IOException {
    ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("bmp").next();
    if (iw == null) {
        throw new RuntimeException("No available writer. Test failed.");
    }
    IIOImage iioImg = new IIOImage(src, null, null);
    ImageWriteParam param = iw.getDefaultWriteParam();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    iw.setOutput(ios);
    System.out.println("Compression Type is " + compressionType);
    System.out.println("Compression Mode is " + compressionMode);
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType(compressionType);
    if (compressionMode != ImageWriteParam.MODE_EXPLICIT) {
        param.setCompressionMode(compressionMode);
    }
    try {
        iw.write(null, iioImg, param);
    } catch (IOException e) {
        int bpp = src.getColorModel().getPixelSize();
        if (compressionMode == ImageWriteParam.MODE_EXPLICIT) {
            if ((compressionType.equals("BI_RLE4") && bpp != 4) || (compressionType.equals("BI_RLE8") && bpp != 8)) {
                System.out.println("Can not encode " + bpp + "bpp image as" + compressionType);
                return;
            } else {
                throw new RuntimeException("Unable to encode " + bpp + "bpp image as " + compressionType + ". Test failed");
            }
        }
    }
    baos.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    BufferedImage dst = ImageIO.read(iis);
    int w = src.getWidth();
    int h = src.getHeight();
    Object dstPixel = dst.getRaster().getDataElements(w / 2, h / 2, null);
    Object srcPixel = src.getRaster().getDataElements(w / 2, h / 2, null);
    if ((src.getColorModel().getRed(srcPixel) != dst.getColorModel().getRed(dstPixel)) || (src.getColorModel().getGreen(srcPixel) != dst.getColorModel().getGreen(dstPixel)) || (src.getColorModel().getBlue(srcPixel) != dst.getColorModel().getBlue(dstPixel)) || (src.getColorModel().getAlpha(srcPixel) != dst.getColorModel().getAlpha(dstPixel))) {
        showPixel(src, w / 2, h / 2);
        showPixel(dst, w / 2, h / 2);
        throw new RuntimeException("Colors are different: " + Integer.toHexString(src.getColorModel().getRGB(srcPixel)) + " and " + Integer.toHexString(dst.getColorModel().getRGB(dstPixel)));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Aggregations

ImageInputStream (javax.imageio.stream.ImageInputStream)60 ImageReader (javax.imageio.ImageReader)32 BufferedImage (java.awt.image.BufferedImage)23 IOException (java.io.IOException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageOutputStream (javax.imageio.stream.ImageOutputStream)9 File (java.io.File)8 InputStream (java.io.InputStream)7 IIOImage (javax.imageio.IIOImage)7 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)7 ImageReadParam (javax.imageio.ImageReadParam)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageWriter (javax.imageio.ImageWriter)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 Iterator (java.util.Iterator)4 IIOException (javax.imageio.IIOException)4 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2