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