Search in sources :

Example 6 with IIOException

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

the class GIFImageReader method read.

public BufferedImage read(int imageIndex, ImageReadParam param) throws IIOException {
    if (stream == null) {
        throw new IllegalStateException("Input not set!");
    }
    checkIndex(imageIndex);
    int index = locateImage(imageIndex);
    if (index != imageIndex) {
        throw new IndexOutOfBoundsException("imageIndex out of bounds!");
    }
    clearAbortRequest();
    readMetadata();
    // A null ImageReadParam means we use the default
    if (param == null) {
        param = getDefaultReadParam();
    }
    // Initialize the destination image
    Iterator imageTypes = getImageTypes(imageIndex);
    this.theImage = getDestination(param, imageTypes, imageMetadata.imageWidth, imageMetadata.imageHeight);
    this.theTile = theImage.getWritableTile(0, 0);
    this.width = imageMetadata.imageWidth;
    this.height = imageMetadata.imageHeight;
    this.streamX = 0;
    this.streamY = 0;
    this.rowsDone = 0;
    this.interlacePass = 0;
    // Get source region, taking subsampling offsets into account,
    // and clipping against the true source bounds
    this.sourceRegion = new Rectangle(0, 0, 0, 0);
    this.destinationRegion = new Rectangle(0, 0, 0, 0);
    computeRegions(param, width, height, theImage, sourceRegion, destinationRegion);
    this.destinationOffset = new Point(destinationRegion.x, destinationRegion.y);
    this.sourceXSubsampling = param.getSourceXSubsampling();
    this.sourceYSubsampling = param.getSourceYSubsampling();
    this.sourceMinProgressivePass = Math.max(param.getSourceMinProgressivePass(), 0);
    this.sourceMaxProgressivePass = Math.min(param.getSourceMaxProgressivePass(), 3);
    this.destY = destinationRegion.y + (streamY - sourceRegion.y) / sourceYSubsampling;
    computeDecodeThisRow();
    // Inform IIOReadProgressListeners of start of image
    processImageStarted(imageIndex);
    startPass(0);
    this.rowBuf = new byte[width];
    try {
        // Read and decode the image data, fill in theImage
        this.initCodeSize = stream.readUnsignedByte();
        // Read first data block
        this.blockLength = stream.readUnsignedByte();
        int left = blockLength;
        int off = 0;
        while (left > 0) {
            int nbytes = stream.read(block, off, left);
            left -= nbytes;
            off += nbytes;
        }
        this.bitPos = 0;
        this.nextByte = 0;
        this.lastBlockFound = false;
        this.interlacePass = 0;
        // Init 32-bit buffer
        initNext32Bits();
        this.clearCode = 1 << initCodeSize;
        this.eofCode = clearCode + 1;
        int code, oldCode = 0;
        int[] prefix = new int[4096];
        byte[] suffix = new byte[4096];
        byte[] initial = new byte[4096];
        int[] length = new int[4096];
        byte[] string = new byte[4096];
        initializeStringTable(prefix, suffix, initial, length);
        int tableIndex = (1 << initCodeSize) + 2;
        int codeSize = initCodeSize + 1;
        int codeMask = (1 << codeSize) - 1;
        while (!abortRequested()) {
            code = getCode(codeSize, codeMask);
            if (code == clearCode) {
                initializeStringTable(prefix, suffix, initial, length);
                tableIndex = (1 << initCodeSize) + 2;
                codeSize = initCodeSize + 1;
                codeMask = (1 << codeSize) - 1;
                code = getCode(codeSize, codeMask);
                if (code == eofCode) {
                    // Inform IIOReadProgressListeners of end of image
                    processImageComplete();
                    return theImage;
                }
            } else if (code == eofCode) {
                // Inform IIOReadProgressListeners of end of image
                processImageComplete();
                return theImage;
            } else {
                int newSuffixIndex;
                if (code < tableIndex) {
                    newSuffixIndex = code;
                } else {
                    // code == tableIndex
                    newSuffixIndex = oldCode;
                    if (code != tableIndex) {
                        // warning - code out of sequence
                        // possibly data corruption
                        processWarningOccurred("Out-of-sequence code!");
                    }
                }
                int ti = tableIndex;
                int oc = oldCode;
                prefix[ti] = oc;
                suffix[ti] = initial[newSuffixIndex];
                initial[ti] = initial[oc];
                length[ti] = length[oc] + 1;
                ++tableIndex;
                if ((tableIndex == (1 << codeSize)) && (tableIndex < 4096)) {
                    ++codeSize;
                    codeMask = (1 << codeSize) - 1;
                }
            }
            // Reverse code
            int c = code;
            int len = length[c];
            for (int i = len - 1; i >= 0; i--) {
                string[i] = suffix[c];
                c = prefix[c];
            }
            outputPixels(string, len);
            oldCode = code;
        }
        processReadAborted();
        return theImage;
    } catch (IOException e) {
        e.printStackTrace();
        throw new IIOException("I/O error reading image!", e);
    }
}
Also used : Iterator(java.util.Iterator) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IOException(java.io.IOException) IIOException(javax.imageio.IIOException) Point(java.awt.Point)

Example 7 with IIOException

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

the class GIFImageReader method skipImage.

private boolean skipImage() throws IIOException {
    try {
        while (true) {
            int blockType = stream.readUnsignedByte();
            if (blockType == 0x2c) {
                stream.skipBytes(8);
                int packedFields = stream.readUnsignedByte();
                if ((packedFields & 0x80) != 0) {
                    // Skip color table if any
                    int bits = (packedFields & 0x7) + 1;
                    stream.skipBytes(3 * (1 << bits));
                }
                stream.skipBytes(1);
                int length = 0;
                do {
                    length = stream.readUnsignedByte();
                    stream.skipBytes(length);
                } while (length > 0);
                return true;
            } else if (blockType == 0x3b) {
                return false;
            } else if (blockType == 0x21) {
                int label = stream.readUnsignedByte();
                int length = 0;
                do {
                    length = stream.readUnsignedByte();
                    stream.skipBytes(length);
                } while (length > 0);
            } else if (blockType == 0x0) {
                // EOF
                return false;
            } else {
                int length = 0;
                do {
                    length = stream.readUnsignedByte();
                    stream.skipBytes(length);
                } while (length > 0);
            }
        }
    } catch (EOFException e) {
        return false;
    } catch (IOException e) {
        throw new IIOException("I/O error locating image!", e);
    }
}
Also used : EOFException(java.io.EOFException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) IIOException(javax.imageio.IIOException) Point(java.awt.Point)

Example 8 with IIOException

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

the class GIFImageWriteParam method writeImageDescriptor.

private void writeImageDescriptor(int imageLeftPosition, int imageTopPosition, int imageWidth, int imageHeight, boolean interlaceFlag, boolean sortFlag, int bitsPerPixel, byte[] localColorTable) throws IOException {
    try {
        stream.write(0x2c);
        stream.writeShort((short) imageLeftPosition);
        stream.writeShort((short) imageTopPosition);
        stream.writeShort((short) imageWidth);
        stream.writeShort((short) imageHeight);
        int packedFields = localColorTable != null ? 0x80 : 0x00;
        if (interlaceFlag) {
            packedFields |= 0x40;
        }
        if (sortFlag) {
            packedFields |= 0x8;
        }
        packedFields |= (bitsPerPixel - 1);
        stream.write(packedFields);
        if (localColorTable != null) {
            stream.write(localColorTable);
        }
    } catch (IOException e) {
        throw new IIOException("I/O error writing Image Descriptor!", e);
    }
}
Also used : IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException)

Example 9 with IIOException

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

the class GIFImageWriteParam method writeCommentExtension.

private void writeCommentExtension(GIFWritableImageMetadata im) throws IOException {
    if (im.comments != null) {
        try {
            Iterator iter = im.comments.iterator();
            while (iter.hasNext()) {
                stream.write(0x21);
                stream.write(0xfe);
                writeBlocks((byte[]) iter.next());
                stream.write(0x00);
            }
        } catch (IOException e) {
            throw new IIOException("I/O error writing Comment Extension!", e);
        }
    }
}
Also used : Iterator(java.util.Iterator) IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException)

Example 10 with IIOException

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

the class PNGImageReader method readImage.

private void readImage(ImageReadParam param) throws IIOException {
    readMetadata();
    int width = metadata.IHDR_width;
    int height = metadata.IHDR_height;
    // Init default values
    sourceXSubsampling = 1;
    sourceYSubsampling = 1;
    sourceMinProgressivePass = 0;
    sourceMaxProgressivePass = 6;
    sourceBands = null;
    destinationBands = null;
    destinationOffset = new Point(0, 0);
    // If an ImageReadParam is available, get values from it
    if (param != null) {
        sourceXSubsampling = param.getSourceXSubsampling();
        sourceYSubsampling = param.getSourceYSubsampling();
        sourceMinProgressivePass = Math.max(param.getSourceMinProgressivePass(), 0);
        sourceMaxProgressivePass = Math.min(param.getSourceMaxProgressivePass(), 6);
        sourceBands = param.getSourceBands();
        destinationBands = param.getDestinationBands();
        destinationOffset = param.getDestinationOffset();
    }
    Inflater inf = null;
    try {
        stream.seek(imageStartPosition);
        Enumeration<InputStream> e = new PNGImageDataEnumeration(stream);
        InputStream is = new SequenceInputStream(e);
        /* InflaterInputStream uses an Inflater instance which consumes
            * native (non-GC visible) resources. This is normally implicitly
            * freed when the stream is closed. However since the
            * InflaterInputStream wraps a client-supplied input stream,
            * we cannot close it.
            * But the app may depend on GC finalization to close the stream.
            * Therefore to ensure timely freeing of native resources we
            * explicitly create the Inflater instance and free its resources
            * when we are done with the InflaterInputStream by calling
            * inf.end();
            */
        inf = new Inflater();
        is = new InflaterInputStream(is, inf);
        is = new BufferedInputStream(is);
        this.pixelStream = new DataInputStream(is);
        /*
             * NB: the PNG spec declares that valid range for width
             * and height is [1, 2^31-1], so here we may fail to allocate
             * a buffer for destination image due to memory limitation.
             *
             * However, the recovery strategy for this case should be
             * defined on the level of application, so we will not
             * try to estimate the required amount of the memory and/or
             * handle OOM in any way.
             */
        theImage = getDestination(param, getImageTypes(0), width, height);
        Rectangle destRegion = new Rectangle(0, 0, 0, 0);
        sourceRegion = new Rectangle(0, 0, 0, 0);
        computeRegions(param, width, height, theImage, sourceRegion, destRegion);
        destinationOffset.setLocation(destRegion.getLocation());
        // At this point the header has been read and we know
        // how many bands are in the image, so perform checking
        // of the read param.
        int colorType = metadata.IHDR_colorType;
        checkReadParamBandSettings(param, inputBandsForColorType[colorType], theImage.getSampleModel().getNumBands());
        processImageStarted(0);
        decodeImage();
        if (abortRequested()) {
            processReadAborted();
        } else {
            processImageComplete();
        }
    } catch (IOException e) {
        throw new IIOException("Error reading PNG image data", e);
    } finally {
        if (inf != null) {
            inf.end();
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SubImageInputStream(com.sun.imageio.plugins.common.SubImageInputStream) SequenceInputStream(java.io.SequenceInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Point(java.awt.Point) SequenceInputStream(java.io.SequenceInputStream) BufferedInputStream(java.io.BufferedInputStream) Inflater(java.util.zip.Inflater)

Aggregations

IIOException (javax.imageio.IIOException)60 Point (java.awt.Point)23 IOException (java.io.IOException)23 BufferedImage (java.awt.image.BufferedImage)12 Rectangle (java.awt.Rectangle)11 IndexColorModel (java.awt.image.IndexColorModel)7 SampleModel (java.awt.image.SampleModel)7 WritableRaster (java.awt.image.WritableRaster)7 Iterator (java.util.Iterator)7 ColorModel (java.awt.image.ColorModel)6 ArrayList (java.util.ArrayList)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageInputStream (javax.imageio.stream.ImageInputStream)6 TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)5 ColorSpace (java.awt.color.ColorSpace)5 ComponentSampleModel (java.awt.image.ComponentSampleModel)5 DataBufferByte (java.awt.image.DataBufferByte)5 ImageReader (javax.imageio.ImageReader)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)4