25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.image.PngChunk;
|
|
14
|
|
15
|
|
16 import org.eclipse.swt.SWT;
|
|
17 import org.eclipse.swt.internal.image.LEDataInputStream;
|
|
18 import org.eclipse.swt.internal.image.PngFileReadState;
|
|
19 import org.eclipse.swt.internal.image.PngIhdrChunk;
|
|
20 import org.eclipse.swt.internal.image.PngPlteChunk;
|
|
21 import org.eclipse.swt.internal.image.PngIdatChunk;
|
|
22 import org.eclipse.swt.internal.image.PngIendChunk;
|
|
23 import org.eclipse.swt.internal.image.PngTrnsChunk;
|
|
24 import java.lang.all;
|
|
25
|
|
26
|
|
27 class PngChunk {
|
|
28 byte[] reference;
|
|
29
|
|
30 static const int LENGTH_OFFSET = 0;
|
|
31 static const int TYPE_OFFSET = 4;
|
|
32 static const int DATA_OFFSET = 8;
|
|
33
|
|
34 static const int TYPE_FIELD_LENGTH = 4;
|
|
35 static const int LENGTH_FIELD_LENGTH = 4;
|
|
36 static const int MIN_LENGTH = 12;
|
|
37
|
|
38 static const int CHUNK_UNKNOWN = -1;
|
|
39 // Critical chunks.
|
|
40 static const int CHUNK_IHDR = 0;
|
|
41 static const int CHUNK_PLTE = 1;
|
|
42 static const int CHUNK_IDAT = 2;
|
|
43 static const int CHUNK_IEND = 3;
|
|
44 // Non-critical chunks.
|
|
45 static const int CHUNK_tRNS = 5;
|
|
46
|
51
|
47 static /+const+/ byte[] TYPE_IHDR = cast(byte[])"IHDR";//{(byte) 'I', (byte) 'H', (byte) 'D', (byte) 'R'};
|
|
48 static /+const+/ byte[] TYPE_PLTE = cast(byte[])"PLTE";//{(byte) 'P', (byte) 'L', (byte) 'T', (byte) 'E'};
|
|
49 static /+const+/ byte[] TYPE_IDAT = cast(byte[])"IDAT";//{(byte) 'I', (byte) 'D', (byte) 'A', (byte) 'T'};
|
|
50 static /+const+/ byte[] TYPE_IEND = cast(byte[])"IEND";//{(byte) 'I', (byte) 'E', (byte) 'N', (byte) 'D'};
|
|
51 static /+const+/ byte[] TYPE_tRNS = cast(byte[])"tRNS";//{(byte) 't', (byte) 'R', (byte) 'N', (byte) 'S'};
|
25
|
52
|
113
|
53 private static int[] _CRC_TABLE = null;
|
|
54
|
|
55 static int[] CRC_TABLE () {
|
|
56 if (!_CRC_TABLE)
|
|
57 static_this;
|
|
58
|
|
59 return _CRC_TABLE;
|
|
60 }
|
|
61
|
|
62 private static void static_this () {
|
|
63 _CRC_TABLE = new int[256];
|
25
|
64 for (int i = 0; i < 256; i++) {
|
113
|
65 _CRC_TABLE[i] = i;
|
25
|
66 for (int j = 0; j < 8; j++) {
|
113
|
67 if ((_CRC_TABLE[i] & 0x1) is 0) {
|
|
68 _CRC_TABLE[i] = (_CRC_TABLE[i] >> 1) & 0x7FFFFFFF;
|
25
|
69 } else {
|
113
|
70 _CRC_TABLE[i] = 0xEDB88320 ^ ((_CRC_TABLE[i] >> 1) & 0x7FFFFFFF);
|
25
|
71 }
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75
|
|
76 int length;
|
|
77
|
|
78 /**
|
|
79 * Construct a PngChunk using the reference bytes
|
|
80 * given.
|
|
81 */
|
|
82 this(byte[] reference) {
|
|
83 setReference(reference);
|
|
84 if (reference.length < LENGTH_OFFSET + LENGTH_FIELD_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
85 length = getInt32(LENGTH_OFFSET);
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * Construct a PngChunk with the specified number of
|
|
90 * data bytes.
|
|
91 */
|
|
92 this(int dataLength) {
|
|
93 this(new byte[MIN_LENGTH + dataLength]);
|
|
94 setLength(dataLength);
|
|
95 }
|
|
96
|
|
97 /**
|
|
98 * Get the PngChunk's reference byteArray;
|
|
99 */
|
|
100 byte[] getReference() {
|
|
101 return reference;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Set the PngChunk's reference byteArray;
|
|
106 */
|
|
107 void setReference(byte[] reference) {
|
|
108 this.reference = reference;
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * Get the 16-bit integer from the reference byte
|
|
113 * array at the given offset.
|
|
114 */
|
|
115 int getInt16(int offset) {
|
|
116 int answer = 0;
|
|
117 answer |= (reference[offset] & 0xFF) << 8;
|
|
118 answer |= (reference[offset + 1] & 0xFF);
|
|
119 return answer;
|
|
120 }
|
|
121
|
|
122 /**
|
|
123 * Set the 16-bit integer in the reference byte
|
|
124 * array at the given offset.
|
|
125 */
|
|
126 void setInt16(int offset, int value) {
|
|
127 reference[offset] = cast(byte) ((value >> 8) & 0xFF);
|
|
128 reference[offset + 1] = cast(byte) (value & 0xFF);
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * Get the 32-bit integer from the reference byte
|
|
133 * array at the given offset.
|
|
134 */
|
|
135 int getInt32(int offset) {
|
|
136 int answer = 0;
|
|
137 answer |= (reference[offset] & 0xFF) << 24;
|
|
138 answer |= (reference[offset + 1] & 0xFF) << 16;
|
|
139 answer |= (reference[offset + 2] & 0xFF) << 8;
|
|
140 answer |= (reference[offset + 3] & 0xFF);
|
|
141 return answer;
|
|
142 }
|
|
143
|
|
144 /**
|
|
145 * Set the 32-bit integer in the reference byte
|
|
146 * array at the given offset.
|
|
147 */
|
|
148 void setInt32(int offset, int value) {
|
|
149 reference[offset] = cast(byte) ((value >> 24) & 0xFF);
|
|
150 reference[offset + 1] = cast(byte) ((value >> 16) & 0xFF);
|
|
151 reference[offset + 2] = cast(byte) ((value >> 8) & 0xFF);
|
|
152 reference[offset + 3] = cast(byte) (value & 0xFF);
|
|
153 }
|
|
154
|
|
155 /**
|
|
156 * Get the length of the data component of this chunk.
|
|
157 * This is not the length of the entire chunk.
|
|
158 */
|
|
159 int getLength() {
|
|
160 return length;
|
|
161 }
|
|
162
|
|
163 /**
|
|
164 * Set the length of the data component of this chunk.
|
|
165 * This is not the length of the entire chunk.
|
|
166 */
|
|
167 void setLength(int value) {
|
|
168 setInt32(LENGTH_OFFSET, value);
|
|
169 length = value;
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Get the chunk type. This is a four byte value.
|
|
174 * Each byte should be an ASCII character.
|
|
175 * The first byte is upper case if the chunk is critical.
|
|
176 * The second byte is upper case if the chunk is publicly defined.
|
|
177 * The third byte must be upper case.
|
|
178 * The fourth byte is upper case if the chunk is unsafe to copy.
|
|
179 * Public chunk types are defined by the PNG Development Group.
|
|
180 */
|
|
181 byte[] getTypeBytes() {
|
|
182 byte[] type = new byte[4];
|
|
183 System.arraycopy(reference, TYPE_OFFSET, type, 0, TYPE_FIELD_LENGTH);
|
|
184 return type;
|
|
185 }
|
|
186
|
|
187 /**
|
|
188 * Set the chunk type. This is a four byte value.
|
|
189 * Each byte should be an ASCII character.
|
|
190 * The first byte is upper case if the chunk is critical.
|
|
191 * The second byte is upper case if the chunk is publicly defined.
|
|
192 * The third byte must be upper case.
|
|
193 * The fourth byte is upper case if the chunk is unsafe to copy.
|
|
194 * Public chunk types are defined by the PNG Development Group.
|
|
195 */
|
|
196 void setType(byte[] value) {
|
|
197 if (value.length !is TYPE_FIELD_LENGTH) {
|
|
198 SWT.error (SWT.ERROR_INVALID_ARGUMENT);
|
|
199 }
|
|
200 System.arraycopy(value, 0, reference, TYPE_OFFSET, TYPE_FIELD_LENGTH);
|
|
201 }
|
|
202
|
|
203 /**
|
|
204 * Get the chunk's data.
|
|
205 */
|
|
206 byte[] getData() {
|
|
207 int dataLength = getLength();
|
|
208 if (reference.length < MIN_LENGTH + dataLength) {
|
|
209 SWT.error (SWT.ERROR_INVALID_RANGE);
|
|
210 }
|
|
211 byte[] data = new byte[dataLength];
|
|
212 System.arraycopy(reference, DATA_OFFSET, data, 0, dataLength);
|
|
213 return data;
|
|
214 }
|
|
215
|
|
216 /**
|
|
217 * Set the chunk's data.
|
|
218 * This method has two side-effects.
|
|
219 * 1. It will set the length field to be the length
|
|
220 * of the data array given.
|
|
221 * 2. It will set the CRC field to the computed CRC
|
|
222 * value of the data array given.
|
|
223 */
|
|
224 void setData(byte[] data) {
|
|
225 setLength(data.length);
|
|
226 System.arraycopy(data, 0, reference, DATA_OFFSET, data.length);
|
|
227 setCRC(computeCRC());
|
|
228 }
|
|
229
|
|
230 /**
|
|
231 * Get the CRC value for the chunk's data.
|
|
232 * Ensure that the length field has a good
|
|
233 * value before making this call.
|
|
234 */
|
|
235 int getCRC() {
|
|
236 int crcOffset = DATA_OFFSET + getLength();
|
|
237 return getInt32(crcOffset);
|
|
238 }
|
|
239
|
|
240 /**
|
|
241 * Set the CRC value for the chunk's data.
|
|
242 * Ensure that the length field has a good
|
|
243 * value before making this call.
|
|
244 */
|
|
245 void setCRC(int value) {
|
|
246 int crcOffset = DATA_OFFSET + getLength();
|
|
247 setInt32(crcOffset, value);
|
|
248 }
|
|
249
|
|
250 /**
|
|
251 * Get the chunk's total size including the length, type, and crc fields.
|
|
252 */
|
|
253 int getSize() {
|
|
254 return MIN_LENGTH + getLength();
|
|
255 }
|
|
256
|
|
257 /**
|
|
258 * Compute the CRC value for the chunk's data. Answer
|
|
259 * whether this value matches the value stored in the
|
|
260 * chunk.
|
|
261 */
|
|
262 bool checkCRC() {
|
|
263 int crc = computeCRC();
|
|
264 int storedCRC = getCRC();
|
|
265 return crc is storedCRC;
|
|
266 }
|
|
267
|
|
268 /**
|
|
269 * Answer the CRC value of chunk's data.
|
|
270 */
|
|
271 int computeCRC() {
|
|
272 int crc = 0xFFFFFFFF;
|
|
273 int start = TYPE_OFFSET;
|
|
274 int stop = DATA_OFFSET + getLength();
|
|
275 for (int i = start; i < stop; i++) {
|
|
276 int index = (crc ^ reference[i]) & 0xFF;
|
|
277 crc = CRC_TABLE[index] ^ ((crc >> 8) & 0x00FFFFFF);
|
|
278 }
|
|
279 return ~crc;
|
|
280 }
|
|
281
|
|
282 bool typeMatchesArray(byte[] array) {
|
|
283 for (int i = 0; i < TYPE_FIELD_LENGTH; i++) {
|
|
284 if (reference[TYPE_OFFSET + i] !is array[i]){
|
|
285 return false;
|
|
286 }
|
|
287 }
|
|
288 return true;
|
|
289 }
|
|
290
|
|
291 bool isCritical() {
|
|
292 char c = cast(char) getTypeBytes()[0];
|
|
293 return 'A' <= c && c <= 'Z';
|
|
294 }
|
|
295
|
|
296 int getChunkType() {
|
|
297 if (typeMatchesArray(TYPE_IHDR)) return CHUNK_IHDR;
|
|
298 if (typeMatchesArray(TYPE_PLTE)) return CHUNK_PLTE;
|
|
299 if (typeMatchesArray(TYPE_IDAT)) return CHUNK_IDAT;
|
|
300 if (typeMatchesArray(TYPE_IEND)) return CHUNK_IEND;
|
|
301 if (typeMatchesArray(TYPE_tRNS)) return CHUNK_tRNS;
|
|
302 return CHUNK_UNKNOWN;
|
|
303 }
|
|
304
|
|
305 /**
|
|
306 * Read the next PNG chunk from the input stream given.
|
|
307 * If unable to read a chunk, return null.
|
|
308 */
|
|
309 static PngChunk readNextFromStream(LEDataInputStream stream) {
|
|
310 try {
|
|
311 int headerLength = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH;
|
|
312 byte[] headerBytes = new byte[headerLength];
|
|
313 int result = stream.read(headerBytes, 0, headerLength);
|
|
314 stream.unread(headerBytes);
|
|
315 if (result !is headerLength) return null;
|
|
316
|
|
317 PngChunk tempChunk = new PngChunk(headerBytes);
|
|
318
|
|
319 int chunkLength = tempChunk.getSize();
|
|
320 byte[] chunk = new byte[chunkLength];
|
|
321
|
|
322 result = stream.read(chunk, 0, chunkLength);
|
|
323 if (result !is chunkLength) return null;
|
|
324
|
|
325 switch (tempChunk.getChunkType()) {
|
|
326 case CHUNK_IHDR:
|
|
327 return new PngIhdrChunk(chunk);
|
|
328 case CHUNK_PLTE:
|
|
329 return new PngPlteChunk(chunk);
|
|
330 case CHUNK_IDAT:
|
|
331 return new PngIdatChunk(chunk);
|
|
332 case CHUNK_IEND:
|
|
333 return new PngIendChunk(chunk);
|
|
334 case CHUNK_tRNS:
|
|
335 return new PngTrnsChunk(chunk);
|
|
336 default:
|
|
337 return new PngChunk(chunk);
|
|
338 }
|
|
339 } catch (IOException e) {
|
|
340 return null;
|
|
341 }
|
|
342 }
|
|
343
|
|
344 /**
|
|
345 * Answer whether the chunk is a valid PNG chunk.
|
|
346 */
|
|
347 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
|
|
348 if (reference.length < MIN_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
349
|
|
350 byte[] type = getTypeBytes();
|
|
351
|
|
352 // The third character MUST be upper case.
|
|
353 char c = cast(char) type[2];
|
|
354 if (!('A' <= c && c <= 'Z')) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
355
|
|
356 // All characters must be letters.
|
|
357 for (int i = 0; i < TYPE_FIELD_LENGTH; i++) {
|
|
358 c = cast(char) type[i];
|
|
359 if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))) {
|
|
360 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
361 }
|
|
362 }
|
|
363
|
|
364 // The stored CRC must match the data's computed CRC.
|
|
365 if (!checkCRC()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
366 }
|
|
367
|
|
368 /**
|
|
369 * Provided so that subclasses can override and add
|
|
370 * data to the toString() call.
|
|
371 */
|
|
372 String contributeToString() {
|
|
373 return "";
|
|
374 }
|
|
375
|
|
376 /**
|
|
377 * Returns a string containing a concise, human-readable
|
|
378 * description of the receiver.
|
|
379 *
|
|
380 * @return a string representation of the event
|
|
381 */
|
|
382 public override String toString() {
|
|
383 String buffer = Format( "{\n\tLength: {}\n\tType: {}{}\n\tCRC: {:X}\n}",
|
|
384 getLength(),
|
|
385 cast(String) getTypeBytes(),
|
|
386 contributeToString(),
|
|
387 getCRC());
|
|
388 return buffer;
|
|
389 }
|
|
390
|
|
391 }
|