View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.png.chunks;
18  
19  import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
20  import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  
25  import org.apache.commons.imaging.ImagingException;
26  import org.apache.commons.imaging.formats.png.InterlaceMethod;
27  import org.apache.commons.imaging.formats.png.PngColorType;
28  
29  public class PngChunkIhdr extends PngChunk {
30  
31      private final int width;
32      private final int height;
33      private final int bitDepth;
34      private final PngColorType pngColorType;
35      private final int compressionMethod;
36      private final int filterMethod;
37      private final InterlaceMethod interlaceMethod;
38  
39      public PngChunkIhdr(final int length, final int chunkType, final int crc, final byte[] bytes) throws ImagingException, IOException {
40          super(length, chunkType, crc, bytes);
41  
42          final ByteArrayInputStream is = new ByteArrayInputStream(bytes);
43          width = read4Bytes("Width", is, "Not a Valid PNG File: IHDR Corrupt", getByteOrder());
44          height = read4Bytes("Height", is, "Not a Valid PNG File: IHDR Corrupt", getByteOrder());
45          bitDepth = readByte("BitDepth", is, "Not a Valid PNG File: IHDR Corrupt");
46          final int type = readByte("ColorType", is, "Not a Valid PNG File: IHDR Corrupt");
47          pngColorType = PngColorType.getColorType(type);
48          if (getPngColorType() == null) {
49              throw new ImagingException("PNG: unknown color type: " + type);
50          }
51          compressionMethod = readByte("CompressionMethod", is, "Not a Valid PNG File: IHDR Corrupt");
52          filterMethod = readByte("FilterMethod", is, "Not a Valid PNG File: IHDR Corrupt");
53          final int method = readByte("InterlaceMethod", is, "Not a Valid PNG File: IHDR Corrupt");
54          if (method < 0 || method >= InterlaceMethod.values().length) {
55              throw new ImagingException("PNG: unknown interlace method: " + method);
56          }
57          interlaceMethod = InterlaceMethod.values()[method];
58      }
59  
60      public int getBitDepth() {
61          return bitDepth;
62      }
63  
64      public int getCompressionMethod() {
65          return compressionMethod;
66      }
67  
68      public int getFilterMethod() {
69          return filterMethod;
70      }
71  
72      public int getHeight() {
73          return height;
74      }
75  
76      public InterlaceMethod getInterlaceMethod() {
77          return interlaceMethod;
78      }
79  
80      public PngColorType getPngColorType() {
81          return pngColorType;
82      }
83  
84      public int getWidth() {
85          return width;
86      }
87  }