Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/internal/image/PngIhdrChunk.d @ 120:536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
===D2===
* added [Try]Immutable/Const/Shared templates to work with differenses in D1/D2 instead of version statements
used these templates to work with strict type storage rules of dmd-2.053
* com.ibm.icu now also compilable with D2, but not tested yet
* small fixes
Snippet288 - shared data is in TLS
===Phobos===
* fixed critical bugs in Phobos implemention
completely incorrect segfault prone fromStringz (Linux's port ruthless killer)
terrible, incorrect StringBuffer realization (StyledText killer)
* fixed small bugs as well
Snippet72 - misprint in the snippet
* implemented missed functionality for Phobos
ByteArrayOutputStream implemented (image loading available)
formatting correctly works for all DWT's cases
As a result, folowing snippets now works with Phobos (Snippet### - what is fixed):
Snippet24, 42, 111, 115, 130, 235, 276 - bad string formatting
Snippet48, 282 - crash on image loading
Snippet163, 189, 211, 213, 217, 218, 222 - crash on copy/cut in StyledText
Snippet244 - hang-up
===Tango===
* few changes for the latest Tango trunc-r5661
* few small performance improvments
===General===
* implMissing-s for only one version changed to implMissingInTango/InPhobos
* incorrect calls to Format in toString-s fixed
* fixed loading \uXXXX characters in ResourceBundle
* added good UTF-8 support for StyledText, TextLayout (Win32) and friends
UTF functions revised and tested. It is now in java.nonstandard.*Utf modules
StyledText and TextLayout (Win32) modules revised for UTF-8 support
* removed small diferences in most identical files in *.swt.* folders
*.swt.internal.image, *.swt.events and *.swt.custom are identical in Win32/Linux32
now 179 of 576 (~31%) files in *.swt.* folders are fully identical
* Win32: snippets now have right subsystem, pretty icons and native system style controls
* small fixes in snippets
Snippet44 - it's not Snippet44
Snippet212 - functions work with different images and offsets arrays
Win32: Snippet282 - crash on close if the button has an image
Snippet293 - setGrayed is commented
and others
Win32: As a result, folowing snippets now works
Snippet68 - color doesn't change
Snippet163, 189, 211, 213, 217, 218, 222 - UTF-8 issues (see above)
Snippet193 - no tabel headers
author | Denis Shelomovskij <verylonglogin.reg@gmail.com> |
---|---|
date | Sat, 09 Jul 2011 15:50:20 +0300 |
parents | f589fc20a5f9 |
children |
rev | line source |
---|---|
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 import org.eclipse.swt.SWT; | |
18 import org.eclipse.swt.graphics.PaletteData; | |
19 import org.eclipse.swt.graphics.RGB; | |
20 import org.eclipse.swt.internal.image.PngFileReadState; | |
21 import org.eclipse.swt.internal.image.PngIhdrChunk; | |
22 import org.eclipse.swt.internal.image.PngChunk; | |
23 | |
24 class PngIhdrChunk : PngChunk { | |
25 static const int IHDR_DATA_LENGTH = 13; | |
26 | |
27 static const int WIDTH_DATA_OFFSET = DATA_OFFSET + 0; | |
28 static const int HEIGHT_DATA_OFFSET = DATA_OFFSET + 4; | |
29 static const int BIT_DEPTH_OFFSET = DATA_OFFSET + 8; | |
30 static const int COLOR_TYPE_OFFSET = DATA_OFFSET + 9; | |
31 static const int COMPRESSION_METHOD_OFFSET = DATA_OFFSET + 10; | |
32 static const int FILTER_METHOD_OFFSET = DATA_OFFSET + 11; | |
33 static const int INTERLACE_METHOD_OFFSET = DATA_OFFSET + 12; | |
34 | |
35 static const byte COLOR_TYPE_GRAYSCALE = 0; | |
36 static const byte COLOR_TYPE_RGB = 2; | |
37 static const byte COLOR_TYPE_PALETTE = 3; | |
38 static const byte COLOR_TYPE_GRAYSCALE_WITH_ALPHA = 4; | |
39 static const byte COLOR_TYPE_RGB_WITH_ALPHA = 6; | |
40 | |
41 static const int INTERLACE_METHOD_NONE = 0; | |
42 static const int INTERLACE_METHOD_ADAM7 = 1; | |
43 | |
44 static const int FILTER_NONE = 0; | |
45 static const int FILTER_SUB = 1; | |
46 static const int FILTER_UP = 2; | |
47 static const int FILTER_AVERAGE = 3; | |
48 static const int FILTER_PAETH = 4; | |
49 | |
50 static const byte[] ValidBitDepths = [ cast(byte)1, 2, 4, 8, 16]; | |
51 static const byte[] ValidColorTypes = [ cast(byte)0, 2, 3, 4, 6]; | |
52 | |
53 int width, height; | |
54 byte bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod; | |
55 | |
56 this(int width, int height, byte bitDepth, byte colorType, byte compressionMethod, byte filterMethod, byte interlaceMethod) { | |
57 super(IHDR_DATA_LENGTH); | |
58 setType(TYPE_IHDR); | |
59 setWidth(width); | |
60 setHeight(height); | |
61 setBitDepth(bitDepth); | |
62 setColorType(colorType); | |
63 setCompressionMethod(compressionMethod); | |
64 setFilterMethod(filterMethod); | |
65 setInterlaceMethod(interlaceMethod); | |
66 setCRC(computeCRC()); | |
67 } | |
68 | |
69 /** | |
70 * Construct a PNGChunk using the reference bytes | |
71 * given. | |
72 */ | |
73 this(byte[] reference) { | |
74 super(reference); | |
75 if (reference.length <= IHDR_DATA_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE); | |
76 width = getInt32(WIDTH_DATA_OFFSET); | |
77 height = getInt32(HEIGHT_DATA_OFFSET); | |
78 bitDepth = reference[BIT_DEPTH_OFFSET]; | |
79 colorType = reference[COLOR_TYPE_OFFSET]; | |
80 compressionMethod = reference[COMPRESSION_METHOD_OFFSET]; | |
81 filterMethod = reference[FILTER_METHOD_OFFSET]; | |
82 interlaceMethod = reference[INTERLACE_METHOD_OFFSET]; | |
83 } | |
84 | |
85 override int getChunkType() { | |
86 return CHUNK_IHDR; | |
87 } | |
88 | |
89 /** | |
90 * Get the image's width in pixels. | |
91 */ | |
92 int getWidth() { | |
93 return width; | |
94 } | |
95 | |
96 /** | |
97 * Set the image's width in pixels. | |
98 */ | |
99 void setWidth(int value) { | |
100 setInt32(WIDTH_DATA_OFFSET, value); | |
101 width = value; | |
102 } | |
103 | |
104 /** | |
105 * Get the image's height in pixels. | |
106 */ | |
107 int getHeight() { | |
108 return height; | |
109 } | |
110 | |
111 /** | |
112 * Set the image's height in pixels. | |
113 */ | |
114 void setHeight(int value) { | |
115 setInt32(HEIGHT_DATA_OFFSET, value); | |
116 height = value; | |
117 } | |
118 | |
119 /** | |
120 * Get the image's bit depth. | |
121 * This is limited to the values 1, 2, 4, 8, or 16. | |
122 */ | |
123 byte getBitDepth() { | |
124 return bitDepth; | |
125 } | |
126 | |
127 /** | |
128 * Set the image's bit depth. | |
129 * This is limited to the values 1, 2, 4, 8, or 16. | |
130 */ | |
131 void setBitDepth(byte value) { | |
132 reference[BIT_DEPTH_OFFSET] = value; | |
133 bitDepth = value; | |
134 } | |
135 | |
136 /** | |
137 * Get the image's color type. | |
138 * This is limited to the values: | |
139 * 0 - Grayscale image. | |
140 * 2 - RGB triple. | |
141 * 3 - Palette. | |
142 * 4 - Grayscale with Alpha channel. | |
143 * 6 - RGB with Alpha channel. | |
144 */ | |
145 byte getColorType() { | |
146 return colorType; | |
147 } | |
148 | |
149 /** | |
150 * Set the image's color type. | |
151 * This is limited to the values: | |
152 * 0 - Grayscale image. | |
153 * 2 - RGB triple. | |
154 * 3 - Palette. | |
155 * 4 - Grayscale with Alpha channel. | |
156 * 6 - RGB with Alpha channel. | |
157 */ | |
158 void setColorType(byte value) { | |
159 reference[COLOR_TYPE_OFFSET] = value; | |
160 colorType = value; | |
161 } | |
162 | |
163 /** | |
164 * Get the image's compression method. | |
165 * This value must be 0. | |
166 */ | |
167 byte getCompressionMethod() { | |
168 return compressionMethod; | |
169 } | |
170 | |
171 /** | |
172 * Set the image's compression method. | |
173 * This value must be 0. | |
174 */ | |
175 void setCompressionMethod(byte value) { | |
176 reference[COMPRESSION_METHOD_OFFSET] = value; | |
177 compressionMethod = value; | |
178 } | |
179 | |
180 /** | |
181 * Get the image's filter method. | |
182 * This value must be 0. | |
183 */ | |
184 byte getFilterMethod() { | |
185 return filterMethod; | |
186 } | |
187 | |
188 /** | |
189 * Set the image's filter method. | |
190 * This value must be 0. | |
191 */ | |
192 void setFilterMethod(byte value) { | |
193 reference[FILTER_METHOD_OFFSET] = value; | |
194 filterMethod = value; | |
195 } | |
196 | |
197 /** | |
198 * Get the image's interlace method. | |
199 * This value is limited to: | |
200 * 0 - No interlacing used. | |
201 * 1 - Adam7 interlacing used. | |
202 */ | |
203 byte getInterlaceMethod() { | |
204 return interlaceMethod; | |
205 } | |
206 | |
207 /** | |
208 * Set the image's interlace method. | |
209 * This value is limited to: | |
210 * 0 - No interlacing used. | |
211 * 1 - Adam7 interlacing used. | |
212 */ | |
213 void setInterlaceMethod(byte value) { | |
214 reference[INTERLACE_METHOD_OFFSET] = value; | |
215 interlaceMethod = value; | |
216 } | |
217 | |
218 /** | |
219 * Answer whether the chunk is a valid IHDR chunk. | |
220 */ | |
221 override void validate(PngFileReadState readState, PngIhdrChunk headerChunk) { | |
222 // An IHDR chunk is invalid if any other chunk has | |
223 // been read. | |
224 if (readState.readIHDR | |
225 || readState.readPLTE | |
226 || readState.readIDAT | |
227 || readState.readIEND) | |
228 { | |
229 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
230 } else { | |
231 readState.readIHDR = true; | |
232 } | |
233 | |
234 super.validate(readState, headerChunk); | |
235 | |
236 if (length !is IHDR_DATA_LENGTH) SWT.error(SWT.ERROR_INVALID_IMAGE); | |
237 if (compressionMethod !is 0) SWT.error(SWT.ERROR_INVALID_IMAGE); | |
238 if (interlaceMethod !is INTERLACE_METHOD_NONE && | |
239 interlaceMethod !is INTERLACE_METHOD_ADAM7) { | |
240 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
241 } | |
242 | |
243 bool colorTypeIsValid = false; | |
244 for (int i = 0; i < ValidColorTypes.length; i++) { | |
245 if (ValidColorTypes[i] is colorType) { | |
246 colorTypeIsValid = true; | |
247 break; | |
248 } | |
249 } | |
250 if (!colorTypeIsValid) SWT.error(SWT.ERROR_INVALID_IMAGE); | |
251 | |
252 bool bitDepthIsValid = false; | |
253 for (int i = 0; i < ValidBitDepths.length; i++) { | |
254 if (ValidBitDepths[i] is bitDepth) { | |
255 bitDepthIsValid = true; | |
256 break; | |
257 } | |
258 } | |
259 if (!bitDepthIsValid) SWT.error(SWT.ERROR_INVALID_IMAGE); | |
260 | |
261 if ((colorType is COLOR_TYPE_RGB | |
262 || colorType is COLOR_TYPE_RGB_WITH_ALPHA | |
263 || colorType is COLOR_TYPE_GRAYSCALE_WITH_ALPHA) | |
264 && bitDepth < 8) | |
265 { | |
266 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
267 } | |
268 | |
269 if (colorType is COLOR_TYPE_PALETTE && bitDepth > 8) { | |
270 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
271 } | |
272 } | |
273 | |
274 String getColorTypeString() { | |
275 switch (colorType) { | |
276 case COLOR_TYPE_GRAYSCALE: return "Grayscale"; | |
277 case COLOR_TYPE_RGB: return "RGB"; | |
278 case COLOR_TYPE_PALETTE: return "Palette"; | |
279 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: return "Grayscale with Alpha"; | |
280 case COLOR_TYPE_RGB_WITH_ALPHA: return "RGB with Alpha"; | |
281 default: return "Unknown - " ~ cast(char)colorType; | |
282 } | |
283 } | |
284 | |
285 String getFilterMethodString() { | |
286 switch (filterMethod) { | |
287 case FILTER_NONE: return "None"; | |
288 case FILTER_SUB: return "Sub"; | |
289 case FILTER_UP: return "Up"; | |
290 case FILTER_AVERAGE: return "Average"; | |
291 case FILTER_PAETH: return "Paeth"; | |
292 default: return "Unknown"; | |
293 } | |
294 } | |
295 | |
296 String getInterlaceMethodString() { | |
297 switch (interlaceMethod) { | |
298 case INTERLACE_METHOD_NONE: return "Not Interlaced"; | |
299 case INTERLACE_METHOD_ADAM7: return "Interlaced - ADAM7"; | |
300 default: return "Unknown"; | |
301 } | |
302 } | |
303 | |
304 override String contributeToString() { | |
305 return Format( "\n\tWidth: {}\n\tHeight: {}\n\tBit Depth: {}\n\tColor Type: {}\n\tCompression Method: {}\n\tFilter Method: {}\n\tInterlace Method: {}", | |
306 width, height, bitDepth, getColorTypeString(), compressionMethod, getFilterMethodString(), getInterlaceMethodString() ); | |
307 } | |
308 | |
309 bool getMustHavePalette() { | |
310 return colorType is COLOR_TYPE_PALETTE; | |
311 } | |
312 | |
313 bool getCanHavePalette() { | |
314 return colorType !is COLOR_TYPE_GRAYSCALE && | |
315 colorType !is COLOR_TYPE_GRAYSCALE_WITH_ALPHA; | |
316 } | |
317 | |
318 /** | |
319 * Answer the pixel size in bits based on the color type | |
320 * and bit depth. | |
321 */ | |
322 int getBitsPerPixel() { | |
323 switch (colorType) { | |
324 case COLOR_TYPE_RGB_WITH_ALPHA: | |
325 return 4 * bitDepth; | |
326 case COLOR_TYPE_RGB: | |
327 return 3 * bitDepth; | |
328 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: | |
329 return 2 * bitDepth; | |
330 case COLOR_TYPE_GRAYSCALE: | |
331 case COLOR_TYPE_PALETTE: | |
332 return bitDepth; | |
333 default: | |
334 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
335 return 0; | |
336 } | |
337 } | |
338 | |
339 /** | |
340 * Answer the pixel size in bits based on the color type | |
341 * and bit depth. | |
342 */ | |
343 int getSwtBitsPerPixel() { | |
344 switch (colorType) { | |
345 case COLOR_TYPE_RGB_WITH_ALPHA: | |
346 case COLOR_TYPE_RGB: | |
347 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: | |
348 return 24; | |
349 case COLOR_TYPE_GRAYSCALE: | |
350 case COLOR_TYPE_PALETTE: | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
26
diff
changeset
|
351 return Math.min(cast(int)bitDepth, 8); |
25 | 352 default: |
353 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
354 return 0; | |
355 } | |
356 } | |
357 | |
358 int getFilterByteOffset() { | |
359 if (bitDepth < 8) return 1; | |
360 return getBitsPerPixel() / 8; | |
361 } | |
362 | |
363 bool usesDirectColor() { | |
364 switch (colorType) { | |
365 case COLOR_TYPE_GRAYSCALE: | |
366 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: | |
367 case COLOR_TYPE_RGB: | |
368 case COLOR_TYPE_RGB_WITH_ALPHA: | |
369 return true; | |
370 default: | |
371 return false; | |
372 } | |
373 } | |
374 | |
375 PaletteData createGrayscalePalette() { | |
120
536e43f63c81
Comprehensive update for Win32/Linux32 dmd-2.053/dmd-1.068+Tango-r5661
Denis Shelomovskij <verylonglogin.reg@gmail.com>
parents:
26
diff
changeset
|
376 int depth = Math.min(cast(int)bitDepth, 8); |
25 | 377 int max = (1 << depth) - 1; |
378 int delta = 255 / max; | |
379 int gray = 0; | |
380 RGB[] rgbs = new RGB[max + 1]; | |
381 for (int i = 0; i <= max; i++) { | |
382 rgbs[i] = new RGB(gray, gray, gray); | |
383 gray += delta; | |
384 } | |
385 return new PaletteData(rgbs); | |
386 } | |
387 | |
388 PaletteData getPaletteData() { | |
389 switch (colorType) { | |
390 case COLOR_TYPE_GRAYSCALE: | |
391 return createGrayscalePalette(); | |
392 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: | |
393 case COLOR_TYPE_RGB: | |
394 case COLOR_TYPE_RGB_WITH_ALPHA: | |
395 return new PaletteData(0xFF0000, 0xFF00, 0xFF); | |
396 default: | |
397 return null; | |
398 } | |
399 } | |
400 | |
401 | |
402 | |
403 } |