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