private void parse_IHDR_chunk()

in batik-codec/src/main/java/org/apache/batik/ext/awt/image/codec/png/PNGRed.java [520:714]


    private void parse_IHDR_chunk(PNGChunk chunk) {
        int width  = chunk.getInt4(0);
        int height = chunk.getInt4(4);

        bounds = new Rectangle(0, 0, width, height);

        bitDepth = chunk.getInt1(8);

        int validMask = (1 << 1) | ( 1 << 2 ) | ( 1 << 4 ) | ( 1 << 8 ) | ( 1 << 16 );
        if (( ( 1 << bitDepth ) & validMask ) == 0 ) {
            // bitDepth is not one of { 1, 2, 4, 8, 16 }: Error -- bad bit depth
            String msg = PropertyUtil.getString("PNGImageDecoder3");
            throw new RuntimeException(msg);
        }
        maxOpacity = (1 << bitDepth) - 1;

        colorType = chunk.getInt1(9);
        if ((colorType != PNG_COLOR_GRAY) &&
            (colorType != PNG_COLOR_RGB) &&
            (colorType != PNG_COLOR_PALETTE) &&
            (colorType != PNG_COLOR_GRAY_ALPHA) &&
            (colorType != PNG_COLOR_RGB_ALPHA)) {
            System.out.println(PropertyUtil.getString("PNGImageDecoder4"));
        }

        if ((colorType == PNG_COLOR_RGB) && (bitDepth < 8)) {
            // Error -- RGB images must have 8 or 16 bits
            String msg = PropertyUtil.getString("PNGImageDecoder5");
            throw new RuntimeException(msg);
        }

        if ((colorType == PNG_COLOR_PALETTE) && (bitDepth == 16)) {
            // Error -- palette images must have < 16 bits
            String msg = PropertyUtil.getString("PNGImageDecoder6");
            throw new RuntimeException(msg);
        }

        if ((colorType == PNG_COLOR_GRAY_ALPHA) && (bitDepth < 8)) {
            // Error -- gray/alpha images must have >= 8 bits
            String msg = PropertyUtil.getString("PNGImageDecoder7");
            throw new RuntimeException(msg);
        }

        if ((colorType == PNG_COLOR_RGB_ALPHA) && (bitDepth < 8)) {
            // Error -- RGB/alpha images must have >= 8 bits
            String msg = PropertyUtil.getString("PNGImageDecoder8");
            throw new RuntimeException(msg);
        }

        if (emitProperties) {
            properties.put("color_type", colorTypeNames[colorType]);
        }

        if (generateEncodeParam) {
            if (colorType == PNG_COLOR_PALETTE) {
                encodeParam = new PNGEncodeParam.Palette();
            } else if (colorType == PNG_COLOR_GRAY ||
                       colorType == PNG_COLOR_GRAY_ALPHA) {
                encodeParam = new PNGEncodeParam.Gray();
            } else {
                encodeParam = new PNGEncodeParam.RGB();
            }
            decodeParam.setEncodeParam(encodeParam);
        }

        if (encodeParam != null) {
            encodeParam.setBitDepth(bitDepth);
        }
        if (emitProperties) {
            properties.put("bit_depth", bitDepth);
        }

        if (performGammaCorrection) {
            // Assume file gamma is 1/2.2 unless we get a gAMA chunk
            float gamma = (1.0F/2.2F)*(displayExponent/userExponent);
            if (encodeParam != null) {
                encodeParam.setGamma(gamma);
            }
            if (emitProperties) {
                properties.put("gamma", gamma);
            }
        }

        compressionMethod = chunk.getInt1(10);
        if (compressionMethod != 0) {
            // Error -- only know about compression method 0
            String msg = PropertyUtil.getString("PNGImageDecoder9");
            throw new RuntimeException(msg);
        }

        filterMethod = chunk.getInt1(11);
        if (filterMethod != 0) {
            // Error -- only know about filter method 0
            String msg = PropertyUtil.getString("PNGImageDecoder10");
            throw new RuntimeException(msg);
        }

        interlaceMethod = chunk.getInt1(12);
        if (interlaceMethod == 0) {
            if (encodeParam != null) {
                encodeParam.setInterlacing(false);
            }
            if (emitProperties) {
                properties.put("interlace_method", "None");
            }
        } else if (interlaceMethod == 1) {
            if (encodeParam != null) {
                encodeParam.setInterlacing(true);
            }
            if (emitProperties) {
                properties.put("interlace_method", "Adam7");
            }
        } else {
            // Error -- only know about Adam7 interlacing
            String msg = PropertyUtil.getString("PNGImageDecoder11");
            throw new RuntimeException(msg);
        }

        bytesPerPixel = (bitDepth == 16) ? 2 : 1;

        switch (colorType) {
        case PNG_COLOR_GRAY:
            inputBands = 1;
            outputBands = 1;

            if (output8BitGray && (bitDepth < 8)) {
                postProcess = POST_GRAY_LUT;
            } else if (performGammaCorrection) {
                postProcess = POST_GAMMA;
            } else {
                postProcess = POST_NONE;
            }
            break;

        case PNG_COLOR_RGB:
            inputBands = 3;
            bytesPerPixel *= 3;
            outputBands = 3;

            if (performGammaCorrection) {
                postProcess = POST_GAMMA;
            } else {
                postProcess = POST_NONE;
            }
            break;

        case PNG_COLOR_PALETTE:
            inputBands = 1;
            bytesPerPixel = 1;
            outputBands = expandPalette ? 3 : 1;

            if (expandPalette) {
                postProcess = POST_PALETTE_TO_RGB;
            } else {
                postProcess = POST_NONE;
            }
            break;

        case PNG_COLOR_GRAY_ALPHA:
            inputBands = 2;
            bytesPerPixel *= 2;

            if (suppressAlpha) {
                outputBands = 1;
                postProcess = POST_REMOVE_GRAY_TRANS;
            } else {
                if (performGammaCorrection) {
                    postProcess = POST_GAMMA;
                } else {
                    postProcess = POST_NONE;
                }
                if (expandGrayAlpha) {
                    postProcess |= POST_EXP_MASK;
                    outputBands = 4;
                } else {
                    outputBands = 2;
                }
            }
            break;

        case PNG_COLOR_RGB_ALPHA:
            inputBands = 4;
            bytesPerPixel *= 4;
            outputBands = (!suppressAlpha) ? 4 : 3;

            if (suppressAlpha) {
                postProcess = POST_REMOVE_RGB_TRANS;
            } else if (performGammaCorrection) {
                postProcess = POST_GAMMA;
            } else {
                postProcess = POST_NONE;
            }
            break;
        }
    }