001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.formats.png.chunks;
018
019import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
020import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
021
022import java.io.ByteArrayInputStream;
023import java.io.IOException;
024
025import org.apache.commons.imaging.ImagingException;
026import org.apache.commons.imaging.formats.png.InterlaceMethod;
027import org.apache.commons.imaging.formats.png.PngColorType;
028
029public final class PngChunkIhdr extends PngChunk {
030
031    private final int width;
032    private final int height;
033    private final int bitDepth;
034    private final PngColorType pngColorType;
035    private final int compressionMethod;
036    private final int filterMethod;
037    private final InterlaceMethod interlaceMethod;
038
039    public PngChunkIhdr(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
040        super(length, chunkType, crc, bytes);
041
042        final ByteArrayInputStream is = new ByteArrayInputStream(bytes);
043        width = read4Bytes("Width", is, "Not a Valid PNG File: IHDR Corrupt", getByteOrder());
044        height = read4Bytes("Height", is, "Not a Valid PNG File: IHDR Corrupt", getByteOrder());
045        bitDepth = readByte("BitDepth", is, "Not a Valid PNG File: IHDR Corrupt");
046        final int type = readByte("ColorType", is, "Not a Valid PNG File: IHDR Corrupt");
047        pngColorType = PngColorType.getColorType(type);
048        if (getPngColorType() == null) {
049            throw new ImagingException("PNG: unknown color type: " + type);
050        }
051        compressionMethod = readByte("CompressionMethod", is, "Not a Valid PNG File: IHDR Corrupt");
052        filterMethod = readByte("FilterMethod", is, "Not a Valid PNG File: IHDR Corrupt");
053        final int method = readByte("InterlaceMethod", is, "Not a Valid PNG File: IHDR Corrupt");
054        if (method < 0 || method >= InterlaceMethod.values().length) {
055            throw new ImagingException("PNG: unknown interlace method: " + method);
056        }
057        interlaceMethod = InterlaceMethod.values()[method];
058    }
059
060    public int getBitDepth() {
061        return bitDepth;
062    }
063
064    public int getCompressionMethod() {
065        return compressionMethod;
066    }
067
068    public int getFilterMethod() {
069        return filterMethod;
070    }
071
072    public int getHeight() {
073        return height;
074    }
075
076    public InterlaceMethod getInterlaceMethod() {
077        return interlaceMethod;
078    }
079
080    public PngColorType getPngColorType() {
081        return pngColorType;
082    }
083
084    public int getWidth() {
085        return width;
086    }
087}