1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }