Search in sources :

Example 61 with ImageTypeSpecifier

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

the class BMPImageReader method readEmbedded.

/** Decodes the jpeg/png image embedded in the bitmap using any jpeg
     *  ImageIO-style plugin.
     *
     * @param bi The destination <code>BufferedImage</code>.
     * @param bmpParam The <code>ImageReadParam</code> for decoding this
     *          BMP image.  The parameters for subregion, band selection and
     *          subsampling are used in decoding the jpeg image.
     */
private BufferedImage readEmbedded(int type, BufferedImage bi, ImageReadParam bmpParam) throws IOException {
    String format;
    switch(type) {
        case BI_JPEG:
            format = "JPEG";
            break;
        case BI_PNG:
            format = "PNG";
            break;
        default:
            throw new IOException("Unexpected compression type: " + type);
    }
    ImageReader reader = ImageIO.getImageReadersByFormatName(format).next();
    if (reader == null) {
        throw new RuntimeException(I18N.getString("BMPImageReader4") + " " + format);
    }
    // prepare input
    byte[] buff = new byte[(int) imageSize];
    iis.read(buff);
    reader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(buff)));
    if (bi == null) {
        ImageTypeSpecifier embType = reader.getImageTypes(0).next();
        bi = embType.createBufferedImage(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height);
    }
    reader.addIIOReadProgressListener(new EmbeddedProgressAdapter() {

        public void imageProgress(ImageReader source, float percentageDone) {
            processImageProgress(percentageDone);
        }
    });
    reader.addIIOReadUpdateListener(new IIOReadUpdateListener() {

        public void imageUpdate(ImageReader source, BufferedImage theImage, int minX, int minY, int width, int height, int periodX, int periodY, int[] bands) {
            processImageUpdate(theImage, minX, minY, width, height, periodX, periodY, bands);
        }

        public void passComplete(ImageReader source, BufferedImage theImage) {
            processPassComplete(theImage);
        }

        public void passStarted(ImageReader source, BufferedImage theImage, int pass, int minPass, int maxPass, int minX, int minY, int periodX, int periodY, int[] bands) {
            processPassStarted(theImage, pass, minPass, maxPass, minX, minY, periodX, periodY, bands);
        }

        public void thumbnailPassComplete(ImageReader source, BufferedImage thumb) {
        }

        public void thumbnailPassStarted(ImageReader source, BufferedImage thumb, int pass, int minPass, int maxPass, int minX, int minY, int periodX, int periodY, int[] bands) {
        }

        public void thumbnailUpdate(ImageReader source, BufferedImage theThumbnail, int minX, int minY, int width, int height, int periodX, int periodY, int[] bands) {
        }
    });
    reader.addIIOReadWarningListener(new IIOReadWarningListener() {

        public void warningOccurred(ImageReader source, String warning) {
            processWarningOccurred(warning);
        }
    });
    ImageReadParam param = reader.getDefaultReadParam();
    param.setDestination(bi);
    param.setDestinationBands(bmpParam.getDestinationBands());
    param.setDestinationOffset(bmpParam.getDestinationOffset());
    param.setSourceBands(bmpParam.getSourceBands());
    param.setSourceRegion(bmpParam.getSourceRegion());
    param.setSourceSubsampling(bmpParam.getSourceXSubsampling(), bmpParam.getSourceYSubsampling(), bmpParam.getSubsamplingXOffset(), bmpParam.getSubsamplingYOffset());
    reader.read(0, param);
    return bi;
}
Also used : IIOReadWarningListener(javax.imageio.event.IIOReadWarningListener) IIOException(javax.imageio.IIOException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) IIOReadUpdateListener(javax.imageio.event.IIOReadUpdateListener) ImageReadParam(javax.imageio.ImageReadParam) ImageReader(javax.imageio.ImageReader)

Example 62 with ImageTypeSpecifier

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

the class ImageUtil method canEncodeImage.

/** Checks that the provided <code>ImageWriter</code> can encode
     * the provided <code>ColorModel</code> and <code>SampleModel</code>.
     * If not, an <code>IIOException</code> will be thrown.
     * @param writer The provided <code>ImageWriter</code>.
     * @param colorModel The provided <code>ColorModel</code>.
     * @param sampleModel The provided <code>SampleModel</code>.
     * @throws IIOException If the writer cannot encoded the provided image.
     */
public static final void canEncodeImage(ImageWriter writer, ColorModel colorModel, SampleModel sampleModel) throws IIOException {
    ImageTypeSpecifier type = null;
    if (colorModel != null && sampleModel != null)
        type = new ImageTypeSpecifier(colorModel, sampleModel);
    canEncodeImage(writer, type);
}
Also used : ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 63 with ImageTypeSpecifier

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

the class BMPImageReader method getImageTypes.

public Iterator getImageTypes(int imageIndex) throws IOException {
    checkIndex(imageIndex);
    try {
        readHeader();
    } catch (IllegalArgumentException e) {
        throw new IIOException(I18N.getString("BMPImageReader6"), e);
    }
    ArrayList list = new ArrayList(1);
    list.add(new ImageTypeSpecifier(originalColorModel, originalSampleModel));
    return list.iterator();
}
Also used : ArrayList(java.util.ArrayList) IIOException(javax.imageio.IIOException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 64 with ImageTypeSpecifier

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

the class PNGImageReader method getRawImageType.

/*
     * Super class implementation uses first element
     * of image types list as raw image type.
     *
     * Also, super implementation uses first element of this list
     * as default destination type image read param does not specify
     * anything other.
     *
     * However, in case of RGB and RGBA color types, raw image type
     * produces buffered image of custom type. It causes some
     * performance degradation of subsequent rendering operations.
     *
     * To resolve this contradiction we put standard image types
     * at the first positions of image types list (to produce standard
     * images by default) and put raw image type (which is custom)
     * at the last position of this list.
     *
     * After this changes we should override getRawImageType()
     * to return last element of image types list.
     */
public ImageTypeSpecifier getRawImageType(int imageIndex) throws IOException {
    Iterator<ImageTypeSpecifier> types = getImageTypes(imageIndex);
    ImageTypeSpecifier raw = null;
    do {
        raw = types.next();
    } while (types.hasNext());
    return raw;
}
Also used : ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 65 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)88 BufferedImage (java.awt.image.BufferedImage)36 IIOMetadata (javax.imageio.metadata.IIOMetadata)32 ImageWriter (javax.imageio.ImageWriter)29 IIOImage (javax.imageio.IIOImage)23 IOException (java.io.IOException)22 ImageOutputStream (javax.imageio.stream.ImageOutputStream)22 ColorModel (java.awt.image.ColorModel)20 ImageReader (javax.imageio.ImageReader)20 SampleModel (java.awt.image.SampleModel)18 ImageWriteParam (javax.imageio.ImageWriteParam)18 ImageReadParam (javax.imageio.ImageReadParam)16 Rectangle (java.awt.Rectangle)14 File (java.io.File)13 Iterator (java.util.Iterator)12 ColorSpace (java.awt.color.ColorSpace)11 IndexColorModel (java.awt.image.IndexColorModel)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Point (java.awt.Point)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9