Debian Codegraph @

src/com/drew/imaging/png/PngChunkType.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.imaging.png;
22
23 import com.drew.lang.annotations.NotNull;
24
25 import java.io.UnsupportedEncodingException;
26 import java.util.Arrays;
27 import java.util.HashSet;
28 import java.util.Set;
29
30 /**
31 * @author Drew Noakes https://drewnoakes.com
32 */
33 public class PngChunkType
34 {
35 private static final Set<String> _identifiersAllowingMultiples
36 = new HashSet<String>(Arrays.asList("IDAT", "sPLT", "iTXt", "tEXt", "zTXt"));
37
38 //
39 // Standard critical chunks
40 //
41 /**
42 * Denotes a critical {@link PngChunk} that contains basic information about the PNG image.
43 * This must be the first chunk in the data sequence, and may only occur once.
44 * <p>
45 * The format is:
46 * <ul>
47 * <li><b>pixel width</b> 4 bytes, unsigned and greater than zero</li>
48 * <li><b>pixel height</b> 4 bytes, unsigned and greater than zero</li>
49 * <li><b>bit depth</b> 1 byte, number of bits per sample or per palette index (not per pixel)</li>
50 * <li><b>color type</b> 1 byte, maps to {@link PngColorType} enum</li>
51 * <li><b>compression method</b> 1 byte, currently only a value of zero (deflate/inflate) is in the standard</li>
52 * <li><b>filter method</b> 1 byte, currently only a value of zero (adaptive filtering with five basic filter types) is in the standard</li>
53 * <li><b>interlace method</b> 1 byte, indicates the transmission order of image data, currently only 0 (no interlace) and 1 (Adam7 interlace) are in the standard</li>
54 * </ul>
55 */
56 public static final PngChunkType IHDR;
57
58 /**
59 * Denotes a critical {@link PngChunk} that contains palette entries.
60 * This chunk should only appear for a {@link PngColorType} of <code>IndexedColor</code>,
61 * and may only occur once in the PNG data sequence.
62 * <p>
63 * The chunk contains between one and 256 entries, each of three bytes:
64 * <ul>
65 * <li><b>red</b> 1 byte</li>
66 * <li><b>green</b> 1 byte</li>
67 * <li><b>blue</b> 1 byte</li>
68 * </ul>
69 * The number of entries is determined by the chunk length. A chunk length indivisible by three is an error.
70 */
71 public static final PngChunkType PLTE;
72 public static final PngChunkType IDAT;
73 public static final PngChunkType IEND;
74
75 //
76 // Standard ancillary chunks
77 //
78 public static final PngChunkType cHRM;
79 public static final PngChunkType gAMA;
80 public static final PngChunkType iCCP;
81 public static final PngChunkType sBIT;
82 public static final PngChunkType sRGB;
83 public static final PngChunkType bKGD;
84 public static final PngChunkType hIST;
85 public static final PngChunkType tRNS;
86 public static final PngChunkType pHYs;
87 public static final PngChunkType sPLT;
88 public static final PngChunkType tIME;
89 public static final PngChunkType iTXt;
90 public static final PngChunkType eXIf;
91
92 /**
93 * Denotes an ancillary {@link PngChunk} that contains textual data, having first a keyword and then a value.
94 * If multiple text data keywords are needed, then multiple chunks are included in the PNG data stream.
95 * <p>
96 * The format is:
97 * <ul>
98 * <li><b>keyword</b> 1-79 bytes</li>
99 * <li><b>null separator</b> 1 byte (\0)</li>
100 * <li><b>text string</b> 0 or more bytes</li>
101 * </ul>
102 * Text is interpreted according to the Latin-1 character set [ISO-8859-1].
103 * Newlines should be represented by a single linefeed character (0x9).
104 */
105 public static final PngChunkType tEXt;
106 public static final PngChunkType zTXt;
107
108 static {
109 try {
110 IHDR = new PngChunkType("IHDR");
111 PLTE = new PngChunkType("PLTE");
112 IDAT = new PngChunkType("IDAT", true);
113 IEND = new PngChunkType("IEND");
114 cHRM = new PngChunkType("cHRM");
115 gAMA = new PngChunkType("gAMA");
116 iCCP = new PngChunkType("iCCP");
117 sBIT = new PngChunkType("sBIT");
118 sRGB = new PngChunkType("sRGB");
119 bKGD = new PngChunkType("bKGD");
120 hIST = new PngChunkType("hIST");
121 tRNS = new PngChunkType("tRNS");
122 pHYs = new PngChunkType("pHYs");
123 sPLT = new PngChunkType("sPLT", true);
124 tIME = new PngChunkType("tIME");
125 iTXt = new PngChunkType("iTXt", true);
126 tEXt = new PngChunkType("tEXt", true);
127 zTXt = new PngChunkType("zTXt", true);
128 eXIf = new PngChunkType("eXIf");
129 } catch (PngProcessingException e) {
130 throw new IllegalArgumentException(e);
131 }
132 }
133
134 private final byte[] _bytes;
135 private final boolean _multipleAllowed;
136
137 public PngChunkType(@NotNull String identifier) throws PngProcessingException
138 {
139 this(identifier, false);
140 }
141
142 public PngChunkType(@NotNull String identifier, boolean multipleAllowed) throws PngProcessingException
143 {
144 _multipleAllowed = multipleAllowed;
145
146 try {
147 byte[] bytes = identifier.getBytes("ASCII");
148 validateBytes(bytes);
149 _bytes = bytes;
150 } catch (UnsupportedEncodingException e) {
151 throw new IllegalArgumentException("Unable to convert string code to bytes.");
152 }
153 }
154
155 public PngChunkType(@NotNull byte[] bytes) throws PngProcessingException
156 {
157 validateBytes(bytes);
158 _bytes = bytes;
159 _multipleAllowed = _identifiersAllowingMultiples.contains(getIdentifier());
160 }
161
162 private static void validateBytes(byte[] bytes) throws PngProcessingException
163 {
164 if (bytes.length != 4) {
165 throw new PngProcessingException("PNG chunk type identifier must be four bytes in length");
166 }
167
168 for (byte b : bytes) {
169 if (!isValidByte(b)) {
170 throw new PngProcessingException("PNG chunk type identifier may only contain alphabet characters");
171 }
172 }
173 }
174
175 public boolean isCritical()
176 {
177 return isUpperCase(_bytes[0]);
178 }
179
180 public boolean isAncillary()
181 {
182 return !isCritical();
183 }
184
185 public boolean isPrivate()
186 {
187 return isUpperCase(_bytes[1]);
188 }
189
190 public boolean isSafeToCopy()
191 {
192 return isLowerCase(_bytes[3]);
193 }
194
195 public boolean areMultipleAllowed()
196 {
197 return _multipleAllowed;
198 }
199
200 private static boolean isLowerCase(byte b)
201 {
202 return (b & (1 << 5)) != 0;
203 }
204
205 private static boolean isUpperCase(byte b)
206 {
207 return (b & (1 << 5)) == 0;
208 }
209
210 private static boolean isValidByte(byte b)
211 {
212 return (b >= 65 && b <= 90) || (b >= 97 && b <= 122);
213 }
214
215 public String getIdentifier()
216 {
217 try {
218 return new String(_bytes, "ASCII");
219 } catch (UnsupportedEncodingException e) {
220 // The constructor should ensure that we're always able to encode the bytes in ASCII.
221 // noinspection ConstantConditions
222 assert(false);
223 return "Invalid object instance";
224 }
225 }
226
227 @Override
228 public String toString()
229 {
230 return getIdentifier();
231 }
232
233 @Override
234 public boolean equals(Object o)
235 {
236 if (this == o)
237 return true;
238
239 if (o == null || getClass() != o.getClass())
240 return false;
241
242 PngChunkType that = (PngChunkType)o;
243
244 return Arrays.equals(_bytes, that._bytes);
245 }
246
247 @Override
248 public int hashCode()
249 {
250 return Arrays.hashCode(_bytes);
251 }
252 }