Mercurial > projects > dwt-mac
comparison dwt/internal/image/PngTrnsChunk.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | e831403a80a9 |
children |
comparison
equal
deleted
inserted
replaced
33:965ac0a77267 | 34:5123b17c98ef |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.internal.image; | 13 module dwt.internal.image.PngTrnsChunk; |
12 | |
13 | 14 |
14 import dwt.DWT; | 15 import dwt.DWT; |
16 import dwt.graphics.PaletteData; | |
15 import dwt.graphics.RGB; | 17 import dwt.graphics.RGB; |
18 import dwt.internal.image.PngChunk; | |
19 import dwt.internal.image.PNGFileFormat; | |
20 import dwt.internal.image.PngFileReadState; | |
21 import dwt.internal.image.PngIhdrChunk; | |
22 import dwt.internal.image.PngPlteChunk; | |
16 | 23 |
17 public class PngTrnsChunk : PngChunk { | 24 public class PngTrnsChunk : PngChunk { |
18 static final int TRANSPARENCY_TYPE_PIXEL = 0; | 25 |
19 static final int TRANSPARENCY_TYPE_ALPHAS = 1; | 26 alias PngChunk.validate validate; |
20 static final int RGB_DATA_LENGTH = 6; | 27 |
21 | 28 static const int TRANSPARENCY_TYPE_PIXEL = 0; |
29 static const int TRANSPARENCY_TYPE_ALPHAS = 1; | |
30 static const int RGB_DATA_LENGTH = 6; | |
31 | |
22 this(RGB rgb) { | 32 this(RGB rgb) { |
23 super(RGB_DATA_LENGTH); | 33 super(RGB_DATA_LENGTH); |
24 setType(TYPE_tRNS); | 34 setType(TYPE_tRNS); |
25 setInt16(DATA_OFFSET, rgb.red); | 35 setInt16(DATA_OFFSET, rgb.red); |
26 setInt16(DATA_OFFSET + 2, rgb.green); | 36 setInt16(DATA_OFFSET + 2, rgb.green); |
27 setInt16(DATA_OFFSET + 4, rgb.blue); | 37 setInt16(DATA_OFFSET + 4, rgb.blue); |
28 setCRC(computeCRC()); | 38 setCRC(computeCRC()); |
29 } | 39 } |
30 | 40 |
31 this(byte[] reference){ | 41 this(byte[] reference){ |
32 super(reference); | 42 super(reference); |
33 } | 43 } |
34 | 44 |
35 int getChunkType() { | 45 override int getChunkType() { |
36 return CHUNK_tRNS; | 46 return CHUNK_tRNS; |
37 } | 47 } |
38 | 48 |
39 void validateLength(PngIhdrChunk header, PngPlteChunk paletteChunk) { | 49 void validateLength(PngIhdrChunk header, PngPlteChunk paletteChunk) { |
40 bool valid; | 50 bool valid; |
41 switch (header.getColorType()) { | 51 switch (header.getColorType()) { |
42 case PngIhdrChunk.COLOR_TYPE_RGB: | 52 case PngIhdrChunk.COLOR_TYPE_RGB: |
43 // Three 2-byte values cast(RGB) | 53 // Three 2-byte values (RGB) |
44 valid = getLength() is 6; | 54 valid = getLength() is 6; |
45 break; | 55 break; |
46 case PngIhdrChunk.COLOR_TYPE_PALETTE: | 56 case PngIhdrChunk.COLOR_TYPE_PALETTE: |
47 // Three 2-byte values cast(RGB) | 57 // Three 2-byte values (RGB) |
48 valid = getLength() <= paletteChunk.getLength(); | 58 valid = getLength() <= paletteChunk.getLength(); |
49 break; | 59 break; |
50 case PngIhdrChunk.COLOR_TYPE_GRAYSCALE: | 60 case PngIhdrChunk.COLOR_TYPE_GRAYSCALE: |
51 // One 2-byte value | 61 // One 2-byte value |
52 valid = getLength() is 2; | 62 valid = getLength() is 2; |
67 */ | 77 */ |
68 void validate(PngFileReadState readState, PngIhdrChunk headerChunk, PngPlteChunk paletteChunk) { | 78 void validate(PngFileReadState readState, PngIhdrChunk headerChunk, PngPlteChunk paletteChunk) { |
69 if (!readState.readIHDR | 79 if (!readState.readIHDR |
70 || (headerChunk.getMustHavePalette() && !readState.readPLTE) | 80 || (headerChunk.getMustHavePalette() && !readState.readPLTE) |
71 || readState.readIDAT | 81 || readState.readIDAT |
72 || readState.readIEND) | 82 || readState.readIEND) |
73 { | 83 { |
74 DWT.error(DWT.ERROR_INVALID_IMAGE); | 84 DWT.error(DWT.ERROR_INVALID_IMAGE); |
75 } else { | 85 } else { |
76 readState.readTRNS = true; | 86 readState.readTRNS = true; |
77 } | 87 } |
78 | 88 |
79 validateLength(headerChunk, paletteChunk); | 89 validateLength(headerChunk, paletteChunk); |
80 | 90 |
81 super.validate(readState, headerChunk); | 91 super.validate(readState, headerChunk); |
82 } | 92 } |
83 | 93 |
84 | 94 |
85 int getTransparencyType(PngIhdrChunk header) { | 95 int getTransparencyType(PngIhdrChunk header) { |
111 int red = ((reference[DATA_OFFSET] & 0xFF) << 8) | 121 int red = ((reference[DATA_OFFSET] & 0xFF) << 8) |
112 | (reference[DATA_OFFSET + 1] & 0xFF); | 122 | (reference[DATA_OFFSET + 1] & 0xFF); |
113 int green = ((reference[DATA_OFFSET + 2] & 0xFF) << 8) | 123 int green = ((reference[DATA_OFFSET + 2] & 0xFF) << 8) |
114 | (reference[DATA_OFFSET + 3] & 0xFF); | 124 | (reference[DATA_OFFSET + 3] & 0xFF); |
115 int blue = ((reference[DATA_OFFSET + 4] & 0xFF) << 8) | 125 int blue = ((reference[DATA_OFFSET + 4] & 0xFF) << 8) |
116 | (reference[DATA_OFFSET + 5] & 0xFF); | 126 | (reference[DATA_OFFSET + 5] & 0xFF); |
117 if (header.getBitDepth() > 8) { | 127 if (header.getBitDepth() > 8) { |
118 red = PNGFileFormat.compress16BitDepthTo8BitDepth(red); | 128 red = PNGFileFormat.compress16BitDepthTo8BitDepth(red); |
119 green = PNGFileFormat.compress16BitDepthTo8BitDepth(green); | 129 green = PNGFileFormat.compress16BitDepthTo8BitDepth(green); |
120 blue = PNGFileFormat.compress16BitDepthTo8BitDepth(blue); | 130 blue = PNGFileFormat.compress16BitDepthTo8BitDepth(blue); |
121 } | 131 } |
122 return (red << 16) | (green << 8) | blue; | 132 return (red << 16) | (green << 8) | blue; |
123 default: | 133 default: |
124 DWT.error(DWT.ERROR_INVALID_IMAGE); | 134 DWT.error(DWT.ERROR_INVALID_IMAGE); |
125 return -1; | 135 return -1; |
126 } | 136 } |
127 } | 137 } |
128 | 138 |
129 /** | 139 /** |
130 * Answer an array of Alpha values that correspond to the | 140 * Answer an array of Alpha values that correspond to the |
131 * colors in the palette. | 141 * colors in the palette. |
132 * This is only valid for the COLOR_TYPE_PALETTE color type. | 142 * This is only valid for the COLOR_TYPE_PALETTE color type. |
133 */ | 143 */ |
134 byte[] getAlphaValues(PngIhdrChunk header, PngPlteChunk paletteChunk) { | 144 byte[] getAlphaValues(PngIhdrChunk header, PngPlteChunk paletteChunk) { |
135 if (header.getColorType() !is PngIhdrChunk.COLOR_TYPE_PALETTE) { | 145 if (header.getColorType() !is PngIhdrChunk.COLOR_TYPE_PALETTE) { |
141 for (i = 0; i < dataLength; i++) { | 151 for (i = 0; i < dataLength; i++) { |
142 alphas[i] = reference[DATA_OFFSET + i]; | 152 alphas[i] = reference[DATA_OFFSET + i]; |
143 } | 153 } |
144 /** | 154 /** |
145 * Any palette entries which do not have a corresponding | 155 * Any palette entries which do not have a corresponding |
146 * alpha value in the tRNS chunk are spec'd to have an | 156 * alpha value in the tRNS chunk are spec'd to have an |
147 * alpha of 255. | 157 * alpha of 255. |
148 */ | 158 */ |
149 for (int j = i; j < alphas.length; j++) { | 159 for (int j = i; j < alphas.length; j++) { |
150 alphas[j] = cast(byte) 255; | 160 alphas[j] = cast(byte) 255; |
151 } | 161 } |