comparison dwt/internal/image/PngIhdrChunk.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.internal.image;
12
13
14 import dwt.DWT;
15 import dwt.graphics.PaletteData;
16 import dwt.graphics.RGB;
17
18 class PngIhdrChunk : PngChunk {
19 static final int IHDR_DATA_LENGTH = 13;
20
21 static final int WIDTH_DATA_OFFSET = DATA_OFFSET + 0;
22 static final int HEIGHT_DATA_OFFSET = DATA_OFFSET + 4;
23 static final int BIT_DEPTH_OFFSET = DATA_OFFSET + 8;
24 static final int COLOR_TYPE_OFFSET = DATA_OFFSET + 9;
25 static final int COMPRESSION_METHOD_OFFSET = DATA_OFFSET + 10;
26 static final int FILTER_METHOD_OFFSET = DATA_OFFSET + 11;
27 static final int INTERLACE_METHOD_OFFSET = DATA_OFFSET + 12;
28
29 static final byte COLOR_TYPE_GRAYSCALE = 0;
30 static final byte COLOR_TYPE_RGB = 2;
31 static final byte COLOR_TYPE_PALETTE = 3;
32 static final byte COLOR_TYPE_GRAYSCALE_WITH_ALPHA = 4;
33 static final byte COLOR_TYPE_RGB_WITH_ALPHA = 6;
34
35 static final int INTERLACE_METHOD_NONE = 0;
36 static final int INTERLACE_METHOD_ADAM7 = 1;
37
38 static final int FILTER_NONE = 0;
39 static final int FILTER_SUB = 1;
40 static final int FILTER_UP = 2;
41 static final int FILTER_AVERAGE = 3;
42 static final int FILTER_PAETH = 4;
43
44 static final byte[] ValidBitDepths = {1, 2, 4, 8, 16};
45 static final byte[] ValidColorTypes = {0, 2, 3, 4, 6};
46
47 int width, height;
48 byte bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
49
50 PngIhdrChunk(int width, int height, byte bitDepth, byte colorType, byte compressionMethod, byte filterMethod, byte interlaceMethod) {
51 super(IHDR_DATA_LENGTH);
52 setType(TYPE_IHDR);
53 setWidth(width);
54 setHeight(height);
55 setBitDepth(bitDepth);
56 setColorType(colorType);
57 setCompressionMethod(compressionMethod);
58 setFilterMethod(filterMethod);
59 setInterlaceMethod(interlaceMethod);
60 setCRC(computeCRC());
61 }
62
63 /**
64 * Construct a PNGChunk using the reference bytes
65 * given.
66 */
67 PngIhdrChunk(byte[] reference) {
68 super(reference);
69 if (reference.length <= IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
70 width = getInt32(WIDTH_DATA_OFFSET);
71 height = getInt32(HEIGHT_DATA_OFFSET);
72 bitDepth = reference[BIT_DEPTH_OFFSET];
73 colorType = reference[COLOR_TYPE_OFFSET];
74 compressionMethod = reference[COMPRESSION_METHOD_OFFSET];
75 filterMethod = reference[FILTER_METHOD_OFFSET];
76 interlaceMethod = reference[INTERLACE_METHOD_OFFSET];
77 }
78
79 int getChunkType() {
80 return CHUNK_IHDR;
81 }
82
83 /**
84 * Get the image's width in pixels.
85 */
86 int getWidth() {
87 return width;
88 }
89
90 /**
91 * Set the image's width in pixels.
92 */
93 void setWidth(int value) {
94 setInt32(WIDTH_DATA_OFFSET, value);
95 width = value;
96 }
97
98 /**
99 * Get the image's height in pixels.
100 */
101 int getHeight() {
102 return height;
103 }
104
105 /**
106 * Set the image's height in pixels.
107 */
108 void setHeight(int value) {
109 setInt32(HEIGHT_DATA_OFFSET, value);
110 height = value;
111 }
112
113 /**
114 * Get the image's bit depth.
115 * This is limited to the values 1, 2, 4, 8, or 16.
116 */
117 byte getBitDepth() {
118 return bitDepth;
119 }
120
121 /**
122 * Set the image's bit depth.
123 * This is limited to the values 1, 2, 4, 8, or 16.
124 */
125 void setBitDepth(byte value) {
126 reference[BIT_DEPTH_OFFSET] = value;
127 bitDepth = value;
128 }
129
130 /**
131 * Get the image's color type.
132 * This is limited to the values:
133 * 0 - Grayscale image.
134 * 2 - RGB triple.
135 * 3 - Palette.
136 * 4 - Grayscale with Alpha channel.
137 * 6 - RGB with Alpha channel.
138 */
139 byte getColorType() {
140 return colorType;
141 }
142
143 /**
144 * Set the image's color type.
145 * This is limited to the values:
146 * 0 - Grayscale image.
147 * 2 - RGB triple.
148 * 3 - Palette.
149 * 4 - Grayscale with Alpha channel.
150 * 6 - RGB with Alpha channel.
151 */
152 void setColorType(byte value) {
153 reference[COLOR_TYPE_OFFSET] = value;
154 colorType = value;
155 }
156
157 /**
158 * Get the image's compression method.
159 * This value must be 0.
160 */
161 byte getCompressionMethod() {
162 return compressionMethod;
163 }
164
165 /**
166 * Set the image's compression method.
167 * This value must be 0.
168 */
169 void setCompressionMethod(byte value) {
170 reference[COMPRESSION_METHOD_OFFSET] = value;
171 compressionMethod = value;
172 }
173
174 /**
175 * Get the image's filter method.
176 * This value must be 0.
177 */
178 byte getFilterMethod() {
179 return filterMethod;
180 }
181
182 /**
183 * Set the image's filter method.
184 * This value must be 0.
185 */
186 void setFilterMethod(byte value) {
187 reference[FILTER_METHOD_OFFSET] = value;
188 filterMethod = value;
189 }
190
191 /**
192 * Get the image's interlace method.
193 * This value is limited to:
194 * 0 - No interlacing used.
195 * 1 - Adam7 interlacing used.
196 */
197 byte getInterlaceMethod() {
198 return interlaceMethod;
199 }
200
201 /**
202 * Set the image's interlace method.
203 * This value is limited to:
204 * 0 - No interlacing used.
205 * 1 - Adam7 interlacing used.
206 */
207 void setInterlaceMethod(byte value) {
208 reference[INTERLACE_METHOD_OFFSET] = value;
209 interlaceMethod = value;
210 }
211
212 /**
213 * Answer whether the chunk is a valid IHDR chunk.
214 */
215 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
216 // An IHDR chunk is invalid if any other chunk has
217 // been read.
218 if (readState.readIHDR
219 || readState.readPLTE
220 || readState.readIDAT
221 || readState.readIEND)
222 {
223 DWT.error(DWT.ERROR_INVALID_IMAGE);
224 } else {
225 readState.readIHDR = true;
226 }
227
228 super.validate(readState, headerChunk);
229
230 if (length !is IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
231 if (compressionMethod !is 0) DWT.error(DWT.ERROR_INVALID_IMAGE);
232 if (interlaceMethod !is INTERLACE_METHOD_NONE &&
233 interlaceMethod !is INTERLACE_METHOD_ADAM7) {
234 DWT.error(DWT.ERROR_INVALID_IMAGE);
235 }
236
237 bool colorTypeIsValid = false;
238 for (int i = 0; i < ValidColorTypes.length; i++) {
239 if (ValidColorTypes[i] is colorType) {
240 colorTypeIsValid = true;
241 break;
242 }
243 }
244 if (!colorTypeIsValid) DWT.error(DWT.ERROR_INVALID_IMAGE);
245
246 bool bitDepthIsValid = false;
247 for (int i = 0; i < ValidBitDepths.length; i++) {
248 if (ValidBitDepths[i] is bitDepth) {
249 bitDepthIsValid = true;
250 break;
251 }
252 }
253 if (!bitDepthIsValid) DWT.error(DWT.ERROR_INVALID_IMAGE);
254
255 if ((colorType is COLOR_TYPE_RGB
256 || colorType is COLOR_TYPE_RGB_WITH_ALPHA
257 || colorType is COLOR_TYPE_GRAYSCALE_WITH_ALPHA)
258 && bitDepth < 8)
259 {
260 DWT.error(DWT.ERROR_INVALID_IMAGE);
261 }
262
263 if (colorType is COLOR_TYPE_PALETTE && bitDepth > 8) {
264 DWT.error(DWT.ERROR_INVALID_IMAGE);
265 }
266 }
267
268 String getColorTypeString() {
269 switch (colorType) {
270 case COLOR_TYPE_GRAYSCALE: return "Grayscale";
271 case COLOR_TYPE_RGB: return "RGB";
272 case COLOR_TYPE_PALETTE: return "Palette";
273 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: return "Grayscale with Alpha";
274 case COLOR_TYPE_RGB_WITH_ALPHA: return "RGB with Alpha";
275 default: return "Unknown - " + colorType;
276 }
277 }
278
279 String getFilterMethodString() {
280 switch (filterMethod) {
281 case FILTER_NONE: return "None";
282 case FILTER_SUB: return "Sub";
283 case FILTER_UP: return "Up";
284 case FILTER_AVERAGE: return "Average";
285 case FILTER_PAETH: return "Paeth";
286 default: return "Unknown";
287 }
288 }
289
290 String getInterlaceMethodString() {
291 switch (interlaceMethod) {
292 case INTERLACE_METHOD_NONE: return "Not Interlaced";
293 case INTERLACE_METHOD_ADAM7: return "Interlaced - ADAM7";
294 default: return "Unknown";
295 }
296 }
297
298 void contributeToString(StringBuffer buffer) {
299 buffer.append("\n\tWidth: ");
300 buffer.append(width);
301 buffer.append("\n\tHeight: ");
302 buffer.append(height);
303 buffer.append("\n\tBit Depth: ");
304 buffer.append(bitDepth);
305 buffer.append("\n\tColor Type: ");
306 buffer.append(getColorTypeString());
307 buffer.append("\n\tCompression Method: ");
308 buffer.append(compressionMethod);
309 buffer.append("\n\tFilter Method: ");
310 buffer.append(getFilterMethodString());
311 buffer.append("\n\tInterlace Method: ");
312 buffer.append(getInterlaceMethodString());
313 }
314
315 bool getMustHavePalette() {
316 return colorType is COLOR_TYPE_PALETTE;
317 }
318
319 bool getCanHavePalette() {
320 return colorType !is COLOR_TYPE_GRAYSCALE &&
321 colorType !is COLOR_TYPE_GRAYSCALE_WITH_ALPHA;
322 }
323
324 /**
325 * Answer the pixel size in bits based on the color type
326 * and bit depth.
327 */
328 int getBitsPerPixel() {
329 switch (colorType) {
330 case COLOR_TYPE_RGB_WITH_ALPHA:
331 return 4 * bitDepth;
332 case COLOR_TYPE_RGB:
333 return 3 * bitDepth;
334 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
335 return 2 * bitDepth;
336 case COLOR_TYPE_GRAYSCALE:
337 case COLOR_TYPE_PALETTE:
338 return bitDepth;
339 default:
340 DWT.error(DWT.ERROR_INVALID_IMAGE);
341 return 0;
342 }
343 }
344
345 /**
346 * Answer the pixel size in bits based on the color type
347 * and bit depth.
348 */
349 int getSwtBitsPerPixel() {
350 switch (colorType) {
351 case COLOR_TYPE_RGB_WITH_ALPHA:
352 case COLOR_TYPE_RGB:
353 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
354 return 24;
355 case COLOR_TYPE_GRAYSCALE:
356 case COLOR_TYPE_PALETTE:
357 return Math.min(bitDepth, 8);
358 default:
359 DWT.error(DWT.ERROR_INVALID_IMAGE);
360 return 0;
361 }
362 }
363
364 int getFilterByteOffset() {
365 if (bitDepth < 8) return 1;
366 return getBitsPerPixel() / 8;
367 }
368
369 bool usesDirectColor() {
370 switch (colorType) {
371 case COLOR_TYPE_GRAYSCALE:
372 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
373 case COLOR_TYPE_RGB:
374 case COLOR_TYPE_RGB_WITH_ALPHA:
375 return true;
376 default:
377 return false;
378 }
379 }
380
381 PaletteData createGrayscalePalette() {
382 int depth = Math.min(bitDepth, 8);
383 int max = (1 << depth) - 1;
384 int delta = 255 / max;
385 int gray = 0;
386 RGB[] rgbs = new RGB[max + 1];
387 for (int i = 0; i <= max; i++) {
388 rgbs[i] = new RGB(gray, gray, gray);
389 gray += delta;
390 }
391 return new PaletteData(rgbs);
392 }
393
394 PaletteData getPaletteData() {
395 switch (colorType) {
396 case COLOR_TYPE_GRAYSCALE:
397 return createGrayscalePalette();
398 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
399 case COLOR_TYPE_RGB:
400 case COLOR_TYPE_RGB_WITH_ALPHA:
401 return new PaletteData(0xFF0000, 0xFF00, 0xFF);
402 default:
403 return null;
404 }
405 }
406
407
408
409 }