Search in sources :

Example 41 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class ImageTypeSpecifierTest method test4430991.

private static void test4430991() {
    ImageTypeSpecifier itspecifier;
    itspecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    try {
        itspecifier.createBufferedImage(Integer.MAX_VALUE, Integer.MAX_VALUE);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
    try {
        itspecifier.getSampleModel(Integer.MAX_VALUE, Integer.MAX_VALUE);
        fail("Failed to get IAE!");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 42 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class WBMPPluginTest method test.

public boolean test() throws IIOException, IOException {
    ir.reset();
    iw.reset();
    String[] suffixes = iw.getOriginatingProvider().getFileSuffixes();
    IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOImage iio_img = new IIOImage(img, null, md);
    System.out.println("Image type " + img.getType());
    String fname = "test" + img.getType() + "." + suffixes[0];
    iw.setOutput(ImageIO.createImageOutputStream(new FileOutputStream(new File(fname))));
    System.out.print("write image ... ");
    iw.write(iio_img);
    System.out.println("OK");
    System.out.print("read image ... ");
    byte[] ba_image = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
    ir.setInput(ImageIO.createImageInputStream(new FileInputStream(new File(fname))));
    BufferedImage res = ir.read(0);
    System.out.println("OK");
    System.out.print("compare images ... ");
    boolean r = compare(img, res);
    System.out.println(r ? "OK" : "FAILED");
    return r;
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) FileInputStream(java.io.FileInputStream) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage)

Example 43 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class WbmpBigDestinationTest method main.

public static void main(String[] args) {
    try {
        BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_BINARY);
        Graphics2D g = src.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 100, 100);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next();
        if (iw == null) {
            throw new RuntimeException("No writer available. Test failed.");
        }
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        iw.write(src);
        byte[] data = baos.toByteArray();
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(format).next();
        ir.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(data)));
        Iterator specifiers = ir.getImageTypes(0);
        ImageTypeSpecifier typeSpecifier = null;
        if (specifiers.hasNext()) {
            typeSpecifier = (ImageTypeSpecifier) specifiers.next();
        }
        ImageReadParam param = new ImageReadParam();
        BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
        param.setDestination(dst);
        ir.read(0, param);
        checkResults(src, dst);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D)

Example 44 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project jdk8u_jdk by JetBrains.

the class PngOutputTypeTest method doTest.

public void doTest() throws IOException {
    /*
         * This test verifies that png images with color type RGB or RGBA
         * are decoded as buffered  image of some standard type.
         *
         * So we need to be sure that image provided by
         * user has required color type - RGB or RGBA
         */
    if (!checkImageType()) {
        System.out.println("Test IGNORED!");
        return;
    }
    def = reader.read(0);
    System.out.println("Default image type: " + def.getType());
    if (def == null || def.getType() == BufferedImage.TYPE_CUSTOM) {
        throw new RuntimeException("Test FAILED!");
    }
    raw_type = reader.getRawImageType(0);
    ImageReadParam param = reader.getDefaultReadParam();
    param.setDestinationType(raw_type);
    System.out.println("Reading with raw image type...");
    raw = reader.read(0, param);
    System.out.println("Type of raw image is " + raw.getType());
    compare(def, raw);
    Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
    while (types.hasNext()) {
        ImageTypeSpecifier t = types.next();
        System.out.println("Test type: " + t);
        param.setDestinationType(t);
        BufferedImage img = reader.read(0, param);
        System.out.println("Result type: " + img.getType());
        compare(def, img);
        System.out.println("Done.\n");
    }
    System.out.println("Test PASSED.");
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage)

Example 45 with ImageTypeSpecifier

use of javax.imageio.ImageTypeSpecifier in project poi by apache.

the class BitmapImageRenderer method readImage.

/**
     * Read the image data via ImageIO and optionally try to workaround metadata errors.
     * The resulting image is of image type {@link BufferedImage#TYPE_INT_ARGB}
     *
     * @param data the data stream
     * @param contentType the content type
     * @return the bufferedImage or null, if there was no image reader for this content type
     * @throws IOException thrown if there was an error while processing the image
     */
private static BufferedImage readImage(InputStream data, String contentType) throws IOException {
    IOException lastException = null;
    BufferedImage img = null;
    if (data.markSupported()) {
        data.mark(data.available());
    }
    // currently don't use FileCacheImageInputStream,
    // because of the risk of filling the file handles (see #59166)
    ImageInputStream iis = new MemoryCacheImageInputStream(data);
    try {
        iis = new MemoryCacheImageInputStream(data);
        iis.mark();
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        while (img == null && iter.hasNext()) {
            ImageReader reader = iter.next();
            ImageReadParam param = reader.getDefaultReadParam();
            // 0:default mode, 1:fallback mode
            for (int mode = 0; img == null && mode < 3; mode++) {
                lastException = null;
                try {
                    iis.reset();
                } catch (IOException e) {
                    if (data.markSupported()) {
                        data.reset();
                        data.mark(data.available());
                        iis.close();
                        iis = new MemoryCacheImageInputStream(data);
                    } else {
                        // can't restore the input stream, so we need to stop processing here
                        lastException = e;
                        break;
                    }
                }
                iis.mark();
                try {
                    switch(mode) {
                        case 0:
                            reader.setInput(iis, false, true);
                            img = reader.read(0, param);
                            break;
                        case 1:
                            {
                                // try to load picture in gray scale mode
                                // fallback mode for invalid image band metadata
                                // see http://stackoverflow.com/questions/10416378
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                while (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                                        param.setDestinationType(imageTypeSpecifier);
                                        break;
                                    }
                                }
                                reader.setInput(iis, false, true);
                                img = reader.read(0, param);
                                break;
                            }
                        case 2:
                            {
                                // try to load truncated pictures by supplying a BufferedImage
                                // and use the processed data up till the point of error
                                reader.setInput(iis, false, true);
                                int height = reader.getHeight(0);
                                int width = reader.getWidth(0);
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                if (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    img = imageTypeSpecifier.createBufferedImage(width, height);
                                    param.setDestination(img);
                                } else {
                                    lastException = new IOException("unable to load even a truncated version of the image.");
                                    break;
                                }
                                try {
                                    reader.read(0, param);
                                } finally {
                                    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
                                        int y = findTruncatedBlackBox(img, width, height);
                                        if (y < height) {
                                            BufferedImage argbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                                            Graphics2D g = argbImg.createGraphics();
                                            g.clipRect(0, 0, width, y);
                                            g.drawImage(img, 0, 0, null);
                                            g.dispose();
                                            img.flush();
                                            img = argbImg;
                                        }
                                    }
                                }
                                break;
                            }
                    }
                } catch (IOException e) {
                    if (mode < 2) {
                        lastException = e;
                    }
                } catch (RuntimeException e) {
                    if (mode < 2) {
                        lastException = new IOException("ImageIO runtime exception - " + (mode == 0 ? "normal" : "fallback"), e);
                    }
                }
            }
            reader.dispose();
        }
    } finally {
        iis.close();
    }
    // If you don't have an image at the end of all readers
    if (img == null) {
        if (lastException != null) {
            // multiple locations above ...
            throw lastException;
        }
        LOG.log(POILogger.WARN, "Content-type: " + contentType + " is not support. Image ignored.");
        return null;
    }
    // add alpha channel
    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
        BufferedImage argbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = argbImg.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return argbImg;
    }
    return img;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) ImageReadParam(javax.imageio.ImageReadParam) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader)

Aggregations

ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)45 BufferedImage (java.awt.image.BufferedImage)25 IIOMetadata (javax.imageio.metadata.IIOMetadata)20 ImageWriter (javax.imageio.ImageWriter)18 IIOImage (javax.imageio.IIOImage)13 ImageOutputStream (javax.imageio.stream.ImageOutputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageReader (javax.imageio.ImageReader)11 ImageWriteParam (javax.imageio.ImageWriteParam)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 Iterator (java.util.Iterator)9 ColorModel (java.awt.image.ColorModel)7 IOException (java.io.IOException)7 SampleModel (java.awt.image.SampleModel)6 File (java.io.File)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 Rectangle (java.awt.Rectangle)5 ImageReadParam (javax.imageio.ImageReadParam)5 IndexColorModel (java.awt.image.IndexColorModel)4 Node (org.w3c.dom.Node)4