annotate dwt/internal/image/PngChunk.d @ 213:36f5cb12e1a2

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