Search in sources :

Example 31 with IIOException

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

the class BMPImageReader method readHeader.

/**
     * Process the image header.
     *
     * @exception IllegalStateException if source stream is not set.
     *
     * @exception IOException if image stream is corrupted.
     *
     * @exception IllegalArgumentException if the image stream does not contain
     *             a BMP image, or if a sample model instance to describe the
     *             image can not be created.
     */
protected void readHeader() throws IOException, IllegalArgumentException {
    if (gotHeader)
        return;
    if (iis == null) {
        throw new IllegalStateException("Input source not set!");
    }
    int profileData = 0, profileSize = 0;
    this.metadata = new BMPMetadata();
    iis.mark();
    // read and check the magic marker
    byte[] marker = new byte[2];
    iis.read(marker);
    if (marker[0] != 0x42 || marker[1] != 0x4d)
        throw new IllegalArgumentException(I18N.getString("BMPImageReader1"));
    // Read file size
    bitmapFileSize = iis.readUnsignedInt();
    // skip the two reserved fields
    iis.skipBytes(4);
    // Offset to the bitmap from the beginning
    bitmapOffset = iis.readUnsignedInt();
    // End File Header
    // Start BitmapCoreHeader
    long size = iis.readUnsignedInt();
    if (size == 12) {
        width = iis.readShort();
        height = iis.readShort();
    } else {
        width = iis.readInt();
        height = iis.readInt();
    }
    metadata.width = width;
    metadata.height = height;
    int planes = iis.readUnsignedShort();
    bitsPerPixel = iis.readUnsignedShort();
    //metadata.colorPlane = planes;
    metadata.bitsPerPixel = (short) bitsPerPixel;
    // As BMP always has 3 rgb bands, except for Version 5,
    // which is bgra
    numBands = 3;
    if (size == 12) {
        // Windows 2.x and OS/2 1.x
        metadata.bmpVersion = VERSION_2;
        // Classify the image type
        if (bitsPerPixel == 1) {
            imageType = VERSION_2_1_BIT;
        } else if (bitsPerPixel == 4) {
            imageType = VERSION_2_4_BIT;
        } else if (bitsPerPixel == 8) {
            imageType = VERSION_2_8_BIT;
        } else if (bitsPerPixel == 24) {
            imageType = VERSION_2_24_BIT;
        }
        // Read in the palette
        int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 3);
        int sizeOfPalette = numberOfEntries * 3;
        palette = new byte[sizeOfPalette];
        iis.readFully(palette, 0, sizeOfPalette);
        metadata.palette = palette;
        metadata.paletteSize = numberOfEntries;
    } else {
        compression = iis.readUnsignedInt();
        imageSize = iis.readUnsignedInt();
        long xPelsPerMeter = iis.readInt();
        long yPelsPerMeter = iis.readInt();
        long colorsUsed = iis.readUnsignedInt();
        long colorsImportant = iis.readUnsignedInt();
        metadata.compression = (int) compression;
        metadata.xPixelsPerMeter = (int) xPelsPerMeter;
        metadata.yPixelsPerMeter = (int) yPelsPerMeter;
        metadata.colorsUsed = (int) colorsUsed;
        metadata.colorsImportant = (int) colorsImportant;
        if (size == 40) {
            // Windows 3.x and Windows NT
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    metadata.bmpVersion = VERSION_3;
                    imageType = VERSION_3_XP_EMBEDDED;
                    break;
                // No compression
                case BI_RGB:
                // 8-bit RLE compression
                case BI_RLE8:
                case // 4-bit RLE compression
                BI_RLE4:
                    // Read in the palette
                    if (bitmapOffset < (size + 14)) {
                        throw new IIOException(I18N.getString("BMPImageReader7"));
                    }
                    int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
                    int sizeOfPalette = numberOfEntries * 4;
                    palette = new byte[sizeOfPalette];
                    iis.readFully(palette, 0, sizeOfPalette);
                    metadata.palette = palette;
                    metadata.paletteSize = numberOfEntries;
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_3_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_3_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_3_8_BIT;
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_3_24_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                        redMask = 0x7C00;
                        greenMask = 0x3E0;
                        // 0x1F;
                        blueMask = (1 << 5) - 1;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                        redMask = 0x00FF0000;
                        greenMask = 0x0000FF00;
                        blueMask = 0x000000FF;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    }
                    metadata.bmpVersion = VERSION_3;
                    break;
                case BI_BITFIELDS:
                    if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                    }
                    // BitsField encoding
                    redMask = (int) iis.readUnsignedInt();
                    greenMask = (int) iis.readUnsignedInt();
                    blueMask = (int) iis.readUnsignedInt();
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    if (colorsUsed != 0) {
                        // there is a palette
                        sizeOfPalette = (int) colorsUsed * 4;
                        palette = new byte[sizeOfPalette];
                        iis.readFully(palette, 0, sizeOfPalette);
                        metadata.palette = palette;
                        metadata.paletteSize = (int) colorsUsed;
                    }
                    metadata.bmpVersion = VERSION_3_NT;
                    break;
                default:
                    throw new IIOException(I18N.getString("BMPImageReader2"));
            }
        } else if (size == 108 || size == 124) {
            // Windows 4.x BMP
            if (size == 108)
                metadata.bmpVersion = VERSION_4;
            else if (size == 124)
                metadata.bmpVersion = VERSION_5;
            // rgb masks, valid only if comp is BI_BITFIELDS
            redMask = (int) iis.readUnsignedInt();
            greenMask = (int) iis.readUnsignedInt();
            blueMask = (int) iis.readUnsignedInt();
            // Only supported for 32bpp BI_RGB argb
            alphaMask = (int) iis.readUnsignedInt();
            long csType = iis.readUnsignedInt();
            int redX = iis.readInt();
            int redY = iis.readInt();
            int redZ = iis.readInt();
            int greenX = iis.readInt();
            int greenY = iis.readInt();
            int greenZ = iis.readInt();
            int blueX = iis.readInt();
            int blueY = iis.readInt();
            int blueZ = iis.readInt();
            long gammaRed = iis.readUnsignedInt();
            long gammaGreen = iis.readUnsignedInt();
            long gammaBlue = iis.readUnsignedInt();
            if (size == 124) {
                metadata.intent = iis.readInt();
                profileData = iis.readInt();
                profileSize = iis.readInt();
                iis.skipBytes(4);
            }
            metadata.colorSpace = (int) csType;
            if (csType == LCS_CALIBRATED_RGB) {
                // All the new fields are valid only for this case
                metadata.redX = redX;
                metadata.redY = redY;
                metadata.redZ = redZ;
                metadata.greenX = greenX;
                metadata.greenY = greenY;
                metadata.greenZ = greenZ;
                metadata.blueX = blueX;
                metadata.blueY = blueY;
                metadata.blueZ = blueZ;
                metadata.gammaRed = (int) gammaRed;
                metadata.gammaGreen = (int) gammaGreen;
                metadata.gammaBlue = (int) gammaBlue;
            }
            // Read in the palette
            int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
            int sizeOfPalette = numberOfEntries * 4;
            palette = new byte[sizeOfPalette];
            iis.readFully(palette, 0, sizeOfPalette);
            metadata.palette = palette;
            metadata.paletteSize = numberOfEntries;
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    if (size == 108) {
                        imageType = VERSION_4_XP_EMBEDDED;
                    } else if (size == 124) {
                        imageType = VERSION_5_XP_EMBEDDED;
                    }
                    break;
                default:
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_4_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_4_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_4_8_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_4_16_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x7C00;
                            greenMask = 0x3E0;
                            blueMask = 0x1F;
                        }
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_4_24_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_4_32_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x00FF0000;
                            greenMask = 0x0000FF00;
                            blueMask = 0x000000FF;
                        }
                    }
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    metadata.alphaMask = alphaMask;
            }
        } else {
            throw new IIOException(I18N.getString("BMPImageReader3"));
        }
    }
    if (height > 0) {
        // bottom up image
        isBottomUp = true;
    } else {
        // top down image
        isBottomUp = false;
        height = Math.abs(height);
    }
    // Reset Image Layout so there's only one tile.
    //Define the color space
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    if (metadata.colorSpace == PROFILE_LINKED || metadata.colorSpace == PROFILE_EMBEDDED) {
        iis.mark();
        iis.skipBytes(profileData - size);
        byte[] profile = new byte[profileSize];
        iis.readFully(profile, 0, profileSize);
        iis.reset();
        try {
            if (metadata.colorSpace == PROFILE_LINKED && isLinkedProfileAllowed() && !isUncOrDevicePath(profile)) {
                String path = new String(profile, "windows-1252");
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(path));
            } else {
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(profile));
            }
        } catch (Exception e) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        }
    }
    if (bitsPerPixel == 0 || compression == BI_JPEG || compression == BI_PNG) {
        // the colorModel and sampleModel will be initialzed
        // by the  reader of embedded image
        colorModel = null;
        sampleModel = null;
    } else if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
        // When number of bitsPerPixel is <= 8, we use IndexColorModel.
        numBands = 1;
        if (bitsPerPixel == 8) {
            int[] bandOffsets = new int[numBands];
            for (int i = 0; i < numBands; i++) {
                bandOffsets[i] = numBands - 1 - i;
            }
            sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        } else {
            // 1 and 4 bit pixels can be stored in a packed format.
            sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerPixel);
        }
        // Create IndexColorModel from the palette.
        byte[] r, g, b;
        if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
            size = palette.length / 3;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < (int) size; i++) {
                off = 3 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        } else {
            size = palette.length / 4;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < size; i++) {
                off = 4 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        }
        if (ImageUtil.isIndicesForGrayscale(r, g, b))
            colorModel = ImageUtil.createColorModel(null, sampleModel);
        else
            colorModel = new IndexColorModel(bitsPerPixel, (int) size, r, g, b);
    } else if (bitsPerPixel == 16) {
        numBands = 3;
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, width, height, new int[] { redMask, greenMask, blueMask });
        colorModel = new DirectColorModel(colorSpace, 16, redMask, greenMask, blueMask, 0, false, DataBuffer.TYPE_USHORT);
    } else if (bitsPerPixel == 32) {
        numBands = alphaMask == 0 ? 3 : 4;
        // The number of bands in the SampleModel is determined by
        // the length of the mask array passed in.
        int[] bitMasks = numBands == 3 ? new int[] { redMask, greenMask, blueMask } : new int[] { redMask, greenMask, blueMask, alphaMask };
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bitMasks);
        colorModel = new DirectColorModel(colorSpace, 32, redMask, greenMask, blueMask, alphaMask, false, DataBuffer.TYPE_INT);
    } else {
        numBands = 3;
        // Create SampleModel
        int[] bandOffsets = new int[numBands];
        for (int i = 0; i < numBands; i++) {
            bandOffsets[i] = numBands - 1 - i;
        }
        sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        colorModel = ImageUtil.createColorModel(colorSpace, sampleModel);
    }
    originalSampleModel = sampleModel;
    originalColorModel = colorModel;
    // Reset to the start of bitmap; then jump to the
    //start of image data
    iis.reset();
    iis.skipBytes(bitmapOffset);
    gotHeader = true;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IIOException(javax.imageio.IIOException) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 32 with IIOException

use of javax.imageio.IIOException 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 33 with IIOException

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

the class PNGImageReader method parse_sPLT_chunk.

private void parse_sPLT_chunk(int chunkLength) throws IOException, IIOException {
    metadata.sPLT_paletteName = readNullTerminatedString("ISO-8859-1", 80);
    chunkLength -= metadata.sPLT_paletteName.length() + 1;
    int sampleDepth = stream.readUnsignedByte();
    metadata.sPLT_sampleDepth = sampleDepth;
    int numEntries = chunkLength / (4 * (sampleDepth / 8) + 2);
    metadata.sPLT_red = new int[numEntries];
    metadata.sPLT_green = new int[numEntries];
    metadata.sPLT_blue = new int[numEntries];
    metadata.sPLT_alpha = new int[numEntries];
    metadata.sPLT_frequency = new int[numEntries];
    if (sampleDepth == 8) {
        for (int i = 0; i < numEntries; i++) {
            metadata.sPLT_red[i] = stream.readUnsignedByte();
            metadata.sPLT_green[i] = stream.readUnsignedByte();
            metadata.sPLT_blue[i] = stream.readUnsignedByte();
            metadata.sPLT_alpha[i] = stream.readUnsignedByte();
            metadata.sPLT_frequency[i] = stream.readUnsignedShort();
        }
    } else if (sampleDepth == 16) {
        for (int i = 0; i < numEntries; i++) {
            metadata.sPLT_red[i] = stream.readUnsignedShort();
            metadata.sPLT_green[i] = stream.readUnsignedShort();
            metadata.sPLT_blue[i] = stream.readUnsignedShort();
            metadata.sPLT_alpha[i] = stream.readUnsignedShort();
            metadata.sPLT_frequency[i] = stream.readUnsignedShort();
        }
    } else {
        throw new IIOException("sPLT sample depth not 8 or 16!");
    }
    metadata.sPLT_present = true;
}
Also used : IIOException(javax.imageio.IIOException) Point(java.awt.Point)

Example 34 with IIOException

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

the class PNGImageReader method decodePass.

private void decodePass(int passNum, int xStart, int yStart, int xStep, int yStep, int passWidth, int passHeight) throws IOException {
    if ((passWidth == 0) || (passHeight == 0)) {
        return;
    }
    WritableRaster imRas = theImage.getWritableTile(0, 0);
    int dstMinX = imRas.getMinX();
    int dstMaxX = dstMinX + imRas.getWidth() - 1;
    int dstMinY = imRas.getMinY();
    int dstMaxY = dstMinY + imRas.getHeight() - 1;
    // Determine which pixels will be updated in this pass
    int[] vals = ReaderUtil.computeUpdatedPixels(sourceRegion, destinationOffset, dstMinX, dstMinY, dstMaxX, dstMaxY, sourceXSubsampling, sourceYSubsampling, xStart, yStart, passWidth, passHeight, xStep, yStep);
    int updateMinX = vals[0];
    int updateMinY = vals[1];
    int updateWidth = vals[2];
    int updateXStep = vals[4];
    int updateYStep = vals[5];
    int bitDepth = metadata.IHDR_bitDepth;
    int inputBands = inputBandsForColorType[metadata.IHDR_colorType];
    int bytesPerPixel = (bitDepth == 16) ? 2 : 1;
    bytesPerPixel *= inputBands;
    int bytesPerRow = (inputBands * passWidth * bitDepth + 7) / 8;
    int eltsPerRow = (bitDepth == 16) ? bytesPerRow / 2 : bytesPerRow;
    // If no pixels need updating, just skip the input data
    if (updateWidth == 0) {
        for (int srcY = 0; srcY < passHeight; srcY++) {
            // Update count of pixels read
            updateImageProgress(passWidth);
            // Skip filter byte and the remaining row bytes
            pixelStream.skipBytes(1 + bytesPerRow);
        }
        return;
    }
    // Backwards map from destination pixels
    // (dstX = updateMinX + k*updateXStep)
    // to source pixels (sourceX), and then
    // to offset and skip in passRow (srcX and srcXStep)
    int sourceX = (updateMinX - destinationOffset.x) * sourceXSubsampling + sourceRegion.x;
    int srcX = (sourceX - xStart) / xStep;
    // Compute the step factor in the source
    int srcXStep = updateXStep * sourceXSubsampling / xStep;
    byte[] byteData = null;
    short[] shortData = null;
    byte[] curr = new byte[bytesPerRow];
    byte[] prior = new byte[bytesPerRow];
    // Create a 1-row tall Raster to hold the data
    WritableRaster passRow = createRaster(passWidth, 1, inputBands, eltsPerRow, bitDepth);
    // Create an array suitable for holding one pixel
    int[] ps = passRow.getPixel(0, 0, (int[]) null);
    DataBuffer dataBuffer = passRow.getDataBuffer();
    int type = dataBuffer.getDataType();
    if (type == DataBuffer.TYPE_BYTE) {
        byteData = ((DataBufferByte) dataBuffer).getData();
    } else {
        shortData = ((DataBufferUShort) dataBuffer).getData();
    }
    processPassStarted(theImage, passNum, sourceMinProgressivePass, sourceMaxProgressivePass, updateMinX, updateMinY, updateXStep, updateYStep, destinationBands);
    // Handle source and destination bands
    if (sourceBands != null) {
        passRow = passRow.createWritableChild(0, 0, passRow.getWidth(), 1, 0, 0, sourceBands);
    }
    if (destinationBands != null) {
        imRas = imRas.createWritableChild(0, 0, imRas.getWidth(), imRas.getHeight(), 0, 0, destinationBands);
    }
    // Determine if all of the relevant output bands have the
    // same bit depth as the source data
    boolean adjustBitDepths = false;
    int[] outputSampleSize = imRas.getSampleModel().getSampleSize();
    int numBands = outputSampleSize.length;
    for (int b = 0; b < numBands; b++) {
        if (outputSampleSize[b] != bitDepth) {
            adjustBitDepths = true;
            break;
        }
    }
    // If the bit depths differ, create a lookup table per band to perform
    // the conversion
    int[][] scale = null;
    if (adjustBitDepths) {
        int maxInSample = (1 << bitDepth) - 1;
        int halfMaxInSample = maxInSample / 2;
        scale = new int[numBands][];
        for (int b = 0; b < numBands; b++) {
            int maxOutSample = (1 << outputSampleSize[b]) - 1;
            scale[b] = new int[maxInSample + 1];
            for (int s = 0; s <= maxInSample; s++) {
                scale[b][s] = (s * maxOutSample + halfMaxInSample) / maxInSample;
            }
        }
    }
    // Limit passRow to relevant area for the case where we
    // will can setRect to copy a contiguous span
    boolean useSetRect = srcXStep == 1 && updateXStep == 1 && !adjustBitDepths && (imRas instanceof ByteInterleavedRaster);
    if (useSetRect) {
        passRow = passRow.createWritableChild(srcX, 0, updateWidth, 1, 0, 0, null);
    }
    // Decode the (sub)image row-by-row
    for (int srcY = 0; srcY < passHeight; srcY++) {
        // Update count of pixels read
        updateImageProgress(passWidth);
        // Read the filter type byte and a row of data
        int filter = pixelStream.read();
        try {
            // Swap curr and prior
            byte[] tmp = prior;
            prior = curr;
            curr = tmp;
            pixelStream.readFully(curr, 0, bytesPerRow);
        } catch (java.util.zip.ZipException ze) {
            // TODO - throw a more meaningful exception
            throw ze;
        }
        switch(filter) {
            case PNG_FILTER_NONE:
                break;
            case PNG_FILTER_SUB:
                decodeSubFilter(curr, 0, bytesPerRow, bytesPerPixel);
                break;
            case PNG_FILTER_UP:
                decodeUpFilter(curr, 0, prior, 0, bytesPerRow);
                break;
            case PNG_FILTER_AVERAGE:
                decodeAverageFilter(curr, 0, prior, 0, bytesPerRow, bytesPerPixel);
                break;
            case PNG_FILTER_PAETH:
                decodePaethFilter(curr, 0, prior, 0, bytesPerRow, bytesPerPixel);
                break;
            default:
                throw new IIOException("Unknown row filter type (= " + filter + ")!");
        }
        // Copy data into passRow byte by byte
        if (bitDepth < 16) {
            System.arraycopy(curr, 0, byteData, 0, bytesPerRow);
        } else {
            int idx = 0;
            for (int j = 0; j < eltsPerRow; j++) {
                shortData[j] = (short) ((curr[idx] << 8) | (curr[idx + 1] & 0xff));
                idx += 2;
            }
        }
        // True Y position in source
        int sourceY = srcY * yStep + yStart;
        if ((sourceY >= sourceRegion.y) && (sourceY < sourceRegion.y + sourceRegion.height) && (((sourceY - sourceRegion.y) % sourceYSubsampling) == 0)) {
            int dstY = destinationOffset.y + (sourceY - sourceRegion.y) / sourceYSubsampling;
            if (dstY < dstMinY) {
                continue;
            }
            if (dstY > dstMaxY) {
                break;
            }
            if (useSetRect) {
                imRas.setRect(updateMinX, dstY, passRow);
            } else {
                int newSrcX = srcX;
                for (int dstX = updateMinX; dstX < updateMinX + updateWidth; dstX += updateXStep) {
                    passRow.getPixel(newSrcX, 0, ps);
                    if (adjustBitDepths) {
                        for (int b = 0; b < numBands; b++) {
                            ps[b] = scale[b][ps[b]];
                        }
                    }
                    imRas.setPixel(dstX, dstY, ps);
                    newSrcX += srcXStep;
                }
            }
            processImageUpdate(theImage, updateMinX, dstY, updateWidth, 1, updateXStep, updateYStep, destinationBands);
            // processReadAborted will be called later
            if (abortRequested()) {
                return;
            }
        }
    }
    processPassComplete(theImage);
}
Also used : ByteInterleavedRaster(sun.awt.image.ByteInterleavedRaster) IIOException(javax.imageio.IIOException) Point(java.awt.Point) WritableRaster(java.awt.image.WritableRaster) DataBuffer(java.awt.image.DataBuffer)

Example 35 with IIOException

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

the class PNGImageReader method readHeader.

private void readHeader() throws IIOException {
    if (gotHeader) {
        return;
    }
    if (stream == null) {
        throw new IllegalStateException("Input source not set!");
    }
    try {
        byte[] signature = new byte[8];
        stream.readFully(signature);
        if (signature[0] != (byte) 137 || signature[1] != (byte) 80 || signature[2] != (byte) 78 || signature[3] != (byte) 71 || signature[4] != (byte) 13 || signature[5] != (byte) 10 || signature[6] != (byte) 26 || signature[7] != (byte) 10) {
            throw new IIOException("Bad PNG signature!");
        }
        int IHDR_length = stream.readInt();
        if (IHDR_length != 13) {
            throw new IIOException("Bad length for IHDR chunk!");
        }
        int IHDR_type = stream.readInt();
        if (IHDR_type != IHDR_TYPE) {
            throw new IIOException("Bad type for IHDR chunk!");
        }
        this.metadata = new PNGMetadata();
        int width = stream.readInt();
        int height = stream.readInt();
        // Re-use signature array to bulk-read these unsigned byte values
        stream.readFully(signature, 0, 5);
        int bitDepth = signature[0] & 0xff;
        int colorType = signature[1] & 0xff;
        int compressionMethod = signature[2] & 0xff;
        int filterMethod = signature[3] & 0xff;
        int interlaceMethod = signature[4] & 0xff;
        // Skip IHDR CRC
        stream.skipBytes(4);
        stream.flushBefore(stream.getStreamPosition());
        if (width == 0) {
            throw new IIOException("Image width == 0!");
        }
        if (height == 0) {
            throw new IIOException("Image height == 0!");
        }
        if (bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16) {
            throw new IIOException("Bit depth must be 1, 2, 4, 8, or 16!");
        }
        if (colorType != 0 && colorType != 2 && colorType != 3 && colorType != 4 && colorType != 6) {
            throw new IIOException("Color type must be 0, 2, 3, 4, or 6!");
        }
        if (colorType == PNG_COLOR_PALETTE && bitDepth == 16) {
            throw new IIOException("Bad color type/bit depth combination!");
        }
        if ((colorType == PNG_COLOR_RGB || colorType == PNG_COLOR_RGB_ALPHA || colorType == PNG_COLOR_GRAY_ALPHA) && (bitDepth != 8 && bitDepth != 16)) {
            throw new IIOException("Bad color type/bit depth combination!");
        }
        if (compressionMethod != 0) {
            throw new IIOException("Unknown compression method (not 0)!");
        }
        if (filterMethod != 0) {
            throw new IIOException("Unknown filter method (not 0)!");
        }
        if (interlaceMethod != 0 && interlaceMethod != 1) {
            throw new IIOException("Unknown interlace method (not 0 or 1)!");
        }
        metadata.IHDR_present = true;
        metadata.IHDR_width = width;
        metadata.IHDR_height = height;
        metadata.IHDR_bitDepth = bitDepth;
        metadata.IHDR_colorType = colorType;
        metadata.IHDR_compressionMethod = compressionMethod;
        metadata.IHDR_filterMethod = filterMethod;
        metadata.IHDR_interlaceMethod = interlaceMethod;
        gotHeader = true;
    } catch (IOException e) {
        throw new IIOException("I/O error reading PNG header!", e);
    }
}
Also used : IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) Point(java.awt.Point)

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