Mercurial > projects > dwt-win
annotate dwt/internal/image/PngChunk.d @ 212:ab60f3309436
reverted the char[] to String and use the an alias.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 05 May 2008 00:12:38 +0200 |
parents | 1801ddeb8f32 |
children | 36f5cb12e1a2 |
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; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
24 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
25 import tango.core.Exception; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
26 import tango.text.convert.Format; |
31
92c102dd64a3
Added all widgets modules as dummy. Most modules of accessible are equal to the linux version, except Accessible.
Frank Benoit <benoit@tionex.de>
parents:
2
diff
changeset
|
27 import dwt.dwthelper.System; |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
28 import dwt.dwthelper.utils; |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
29 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
30 class PngChunk { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
31 byte[] reference; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
32 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
33 static const int LENGTH_OFFSET = 0; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
34 static const int TYPE_OFFSET = 4; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
35 static const int DATA_OFFSET = 8; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
36 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
37 static const int TYPE_FIELD_LENGTH = 4; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
38 static const int LENGTH_FIELD_LENGTH = 4; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
39 static const int MIN_LENGTH = 12; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
40 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
41 static const int CHUNK_UNKNOWN = -1; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
42 // Critical chunks. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
43 static const int CHUNK_IHDR = 0; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
44 static const int CHUNK_PLTE = 1; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
45 static const int CHUNK_IDAT = 2; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
46 static const int CHUNK_IEND = 3; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
47 // Non-critical chunks. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
48 static const int CHUNK_tRNS = 5; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
49 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 |
80 | 56 private static /+const+/ int[] CRC_TABLE; |
57 private static bool static_this_completed = false; | |
58 private static void static_this() { | |
59 if( static_this_completed ){ | |
60 return; | |
61 } | |
62 synchronized { | |
63 if( static_this_completed ){ | |
64 return; | |
65 } | |
66 CRC_TABLE = new int[256]; | |
67 for (int i = 0; i < 256; i++) { | |
68 CRC_TABLE[i] = i; | |
69 for (int j = 0; j < 8; j++) { | |
70 if ((CRC_TABLE[i] & 0x1) is 0) { | |
71 CRC_TABLE[i] = (CRC_TABLE[i] >> 1) & 0x7FFFFFFF; | |
72 } else { | |
73 CRC_TABLE[i] = 0xEDB88320 ^ ((CRC_TABLE[i] >> 1) & 0x7FFFFFFF); | |
74 } | |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
75 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
76 } |
80 | 77 static_this_completed = true; |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
78 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
79 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
80 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
81 int length; |
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 using the reference bytes |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
85 * given. |
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(byte[] reference) { |
80 | 88 static_this(); |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
89 setReference(reference); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
90 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
|
91 length = getInt32(LENGTH_OFFSET); |
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 |
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 * Construct a PngChunk with the specified number of |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
96 * data bytes. |
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 this(int dataLength) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
99 this(new byte[MIN_LENGTH + dataLength]); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
100 setLength(dataLength); |
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 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
103 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
104 * Get the PngChunk's reference byteArray; |
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 byte[] getReference() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
107 return reference; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
108 } |
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 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
111 * Set the PngChunk's reference byteArray; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
112 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
113 void setReference(byte[] reference) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
114 this.reference = reference; |
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 * Get the 16-bit integer from 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 int getInt16(int offset) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
122 int answer = 0; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
123 answer |= (reference[offset] & 0xFF) << 8; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
124 answer |= (reference[offset + 1] & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
125 return answer; |
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 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
128 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
129 * Set the 16-bit integer in the reference byte |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
130 * array at the given offset. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
131 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
132 void setInt16(int offset, int value) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
133 reference[offset] = cast(byte) ((value >> 8) & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
134 reference[offset + 1] = cast(byte) (value & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
135 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
136 |
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 * Get the 32-bit integer from the reference byte |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
139 * array at the given offset. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
140 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
141 int getInt32(int offset) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
142 int answer = 0; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
143 answer |= (reference[offset] & 0xFF) << 24; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
144 answer |= (reference[offset + 1] & 0xFF) << 16; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
145 answer |= (reference[offset + 2] & 0xFF) << 8; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
146 answer |= (reference[offset + 3] & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
147 return answer; |
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 * Set the 32-bit integer in the reference byte |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
152 * array at the given offset. |
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 void setInt32(int offset, int value) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
155 reference[offset] = cast(byte) ((value >> 24) & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
156 reference[offset + 1] = cast(byte) ((value >> 16) & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
157 reference[offset + 2] = cast(byte) ((value >> 8) & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
158 reference[offset + 3] = cast(byte) (value & 0xFF); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
159 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
160 |
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 * 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
|
163 * This is not the length of the entire chunk. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
164 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
165 int getLength() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
166 return length; |
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 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
169 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
170 * 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
|
171 * This is not the length of the entire chunk. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
172 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
173 void setLength(int value) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
174 setInt32(LENGTH_OFFSET, value); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
175 length = value; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
176 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
177 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
178 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
179 * 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
|
180 * Each byte should be an ASCII character. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
181 * 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
|
182 * 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
|
183 * The third byte must be upper case. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
184 * 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
|
185 * 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
|
186 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
187 byte[] getTypeBytes() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
188 byte[] type = new byte[4]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
189 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
|
190 return type; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
191 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
192 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
193 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
194 * 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
|
195 * Each byte should be an ASCII character. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
196 * 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
|
197 * 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
|
198 * The third byte must be upper case. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
199 * 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
|
200 * 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
|
201 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
202 void setType(byte[] value) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
203 if (value.length !is TYPE_FIELD_LENGTH) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
204 DWT.error (DWT.ERROR_INVALID_ARGUMENT); |
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 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
|
207 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
208 |
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 * Get the chunk's data. |
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 byte[] getData() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
213 int dataLength = getLength(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
214 if (reference.length < MIN_LENGTH + dataLength) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
215 DWT.error (DWT.ERROR_INVALID_RANGE); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
216 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
217 byte[] data = new byte[dataLength]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
218 System.arraycopy(reference, DATA_OFFSET, data, 0, dataLength); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
219 return data; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
220 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
221 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
222 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
223 * Set the chunk's data. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
224 * This method has two side-effects. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
225 * 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
|
226 * of the data array given. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
227 * 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
|
228 * value of the data array given. |
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 void setData(byte[] data) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
231 setLength(data.length); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
232 System.arraycopy(data, 0, reference, DATA_OFFSET, data.length); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
233 setCRC(computeCRC()); |
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 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
237 * Get the CRC value for the chunk's data. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
238 * Ensure that the length field has a good |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
239 * value before making this call. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
240 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
241 int getCRC() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
242 int crcOffset = DATA_OFFSET + getLength(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
243 return getInt32(crcOffset); |
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 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
247 * Set the CRC value for the chunk's data. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
248 * Ensure that the length field has a good |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
249 * value before making this call. |
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 void setCRC(int value) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
252 int crcOffset = DATA_OFFSET + getLength(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
253 setInt32(crcOffset, value); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
254 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
255 |
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 * 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
|
258 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
259 int getSize() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
260 return MIN_LENGTH + getLength(); |
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 * 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
|
265 * whether this value matches the value stored in the |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
266 * chunk. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
267 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
268 bool checkCRC() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
269 int crc = computeCRC(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
270 int storedCRC = getCRC(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
271 return crc is storedCRC; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
272 } |
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 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
275 * Answer the CRC value of chunk's data. |
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 int computeCRC() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
278 int crc = 0xFFFFFFFF; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
279 int start = TYPE_OFFSET; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
280 int stop = DATA_OFFSET + getLength(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
281 for (int i = start; i < stop; i++) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
282 int index = (crc ^ reference[i]) & 0xFF; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
283 crc = CRC_TABLE[index] ^ ((crc >> 8) & 0x00FFFFFF); |
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 return ~crc; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
286 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
287 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
288 bool typeMatchesArray(byte[] array) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
289 for (int i = 0; i < TYPE_FIELD_LENGTH; i++) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
290 if (reference[TYPE_OFFSET + i] !is array[i]){ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
291 return false; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
292 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
293 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
294 return true; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
295 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
296 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
297 bool isCritical() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
298 char c = cast(char) getTypeBytes()[0]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
299 return 'A' <= c && c <= 'Z'; |
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 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
302 int getChunkType() { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
303 if (typeMatchesArray(TYPE_IHDR)) return CHUNK_IHDR; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
304 if (typeMatchesArray(TYPE_PLTE)) return CHUNK_PLTE; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
305 if (typeMatchesArray(TYPE_IDAT)) return CHUNK_IDAT; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
306 if (typeMatchesArray(TYPE_IEND)) return CHUNK_IEND; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
307 if (typeMatchesArray(TYPE_tRNS)) return CHUNK_tRNS; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
308 return CHUNK_UNKNOWN; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
309 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
310 |
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 * 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
|
313 * If unable to read a chunk, return null. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
314 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
315 static PngChunk readNextFromStream(LEDataInputStream stream) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
316 try { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
317 int headerLength = LENGTH_FIELD_LENGTH + TYPE_FIELD_LENGTH; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
318 byte[] headerBytes = new byte[headerLength]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
319 int result = stream.read(headerBytes, 0, headerLength); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
320 stream.unread(headerBytes); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
321 if (result !is headerLength) return null; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
322 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
323 PngChunk tempChunk = new PngChunk(headerBytes); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
324 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
325 int chunkLength = tempChunk.getSize(); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
326 byte[] chunk = new byte[chunkLength]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
327 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
328 result = stream.read(chunk, 0, chunkLength); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
329 if (result !is chunkLength) return null; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
330 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
331 switch (tempChunk.getChunkType()) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
332 case CHUNK_IHDR: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
333 return new PngIhdrChunk(chunk); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
334 case CHUNK_PLTE: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
335 return new PngPlteChunk(chunk); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
336 case CHUNK_IDAT: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
337 return new PngIdatChunk(chunk); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
338 case CHUNK_IEND: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
339 return new PngIendChunk(chunk); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
340 case CHUNK_tRNS: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
341 return new PngTrnsChunk(chunk); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
342 default: |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
343 return new PngChunk(chunk); |
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 } catch (IOException e) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
346 return null; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
347 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
348 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
349 |
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 * Answer whether the chunk is a valid PNG chunk. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
352 */ |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
353 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
354 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
|
355 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
356 byte[] type = getTypeBytes(); |
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 // The third character MUST be upper case. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
359 char c = cast(char) type[2]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
360 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
|
361 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
362 // All characters must be letters. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
363 for (int i = 0; i < TYPE_FIELD_LENGTH; i++) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
364 c = cast(char) type[i]; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
365 if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))) { |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
366 DWT.error(DWT.ERROR_INVALID_IMAGE); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
367 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
368 } |
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 // 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
|
371 if (!checkCRC()) DWT.error(DWT.ERROR_INVALID_IMAGE); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
372 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
373 |
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 * Provided so that subclasses can override and add |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
376 * data to the toString() call. |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
377 */ |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
378 String contributeToString() { |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
379 return ""; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
380 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
381 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
382 /** |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
383 * Returns a string containing a concise, human-readable |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
384 * description of the receiver. |
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 * @return a string representation of the event |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
387 */ |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
388 override public String toString() { |
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
389 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
|
390 getLength(), |
212
ab60f3309436
reverted the char[] to String and use the an alias.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
391 cast(String) getTypeBytes(), |
2
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
392 contributeToString(), |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
393 getCRC()); |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
394 return buffer; |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
395 } |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
396 |
57151e2793a2
More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff
changeset
|
397 } |