25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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.WinICOFileFormat;
|
|
14
|
|
15 import org.eclipse.swt.internal.image.FileFormat;
|
|
16 import org.eclipse.swt.graphics.PaletteData;
|
|
17 import org.eclipse.swt.internal.image.WinBMPFileFormat;
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import java.lang.all;
|
|
20
|
|
21
|
|
22 public final class WinICOFileFormat : FileFormat {
|
|
23
|
|
24 byte[] bitInvertData(byte[] data, int startIndex, int endIndex) {
|
|
25 // Destructively bit invert data in the given byte array.
|
|
26 for (int i = startIndex; i < endIndex; i++) {
|
|
27 data[i] = cast(byte)(255 - data[i - startIndex]);
|
|
28 }
|
|
29 return data;
|
|
30 }
|
|
31
|
|
32 static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) {
|
|
33 if (pad is newPad) return data;
|
|
34 int stride = (width * depth + 7) / 8;
|
|
35 int bpl = (stride + (pad - 1)) / pad * pad;
|
|
36 int newBpl = (stride + (newPad - 1)) / newPad * newPad;
|
|
37 byte[] newData = new byte[height * newBpl];
|
|
38 int srcIndex = 0, destIndex = 0;
|
|
39 for (int y = 0; y < height; y++) {
|
|
40 System.arraycopy(data, srcIndex, newData, destIndex, newBpl);
|
|
41 srcIndex += bpl;
|
|
42 destIndex += newBpl;
|
|
43 }
|
|
44 return newData;
|
|
45 }
|
|
46 /**
|
|
47 * Answer the size in bytes of the file representation of the given
|
|
48 * icon
|
|
49 */
|
|
50 int iconSize(ImageData i) {
|
|
51 int shapeDataStride = (i.width * i.depth + 31) / 32 * 4;
|
|
52 int maskDataStride = (i.width + 31) / 32 * 4;
|
|
53 int dataSize = (shapeDataStride + maskDataStride) * i.height;
|
|
54 int paletteSize = i.palette.colors !is null ? i.palette.colors.length * 4 : 0;
|
|
55 return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize;
|
|
56 }
|
|
57 override bool isFileFormat(LEDataInputStream stream) {
|
|
58 try {
|
|
59 byte[] header = new byte[4];
|
|
60 stream.read(header);
|
|
61 stream.unread(header);
|
|
62 return header[0] is 0 && header[1] is 0 && header[2] is 1 && header[3] is 0;
|
|
63 } catch (Exception e) {
|
|
64 return false;
|
|
65 }
|
|
66 }
|
|
67 bool isValidIcon(ImageData i) {
|
|
68 switch (i.depth) {
|
|
69 case 1:
|
|
70 case 4:
|
|
71 case 8:
|
|
72 if (i.palette.isDirect) return false;
|
|
73 int size = i.palette.colors.length;
|
|
74 return size is 2 || size is 16 || size is 32 || size is 256;
|
|
75 case 24:
|
|
76 case 32:
|
|
77 return i.palette.isDirect;
|
|
78 default:
|
|
79 }
|
|
80 return false;
|
|
81 }
|
|
82 int loadFileHeader(LEDataInputStream byteStream) {
|
|
83 int[] fileHeader = new int[3];
|
|
84 try {
|
|
85 fileHeader[0] = byteStream.readShort();
|
|
86 fileHeader[1] = byteStream.readShort();
|
|
87 fileHeader[2] = byteStream.readShort();
|
|
88 } catch (IOException e) {
|
|
89 SWT.error(SWT.ERROR_IO, e);
|
|
90 }
|
|
91 if ((fileHeader[0] !is 0) || (fileHeader[1] !is 1))
|
|
92 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
93 int numIcons = fileHeader[2];
|
|
94 if (numIcons <= 0)
|
|
95 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
96 return numIcons;
|
|
97 }
|
|
98 int loadFileHeader(LEDataInputStream byteStream, bool hasHeader) {
|
|
99 int[] fileHeader = new int[3];
|
|
100 try {
|
|
101 if (hasHeader) {
|
|
102 fileHeader[0] = byteStream.readShort();
|
|
103 fileHeader[1] = byteStream.readShort();
|
|
104 } else {
|
|
105 fileHeader[0] = 0;
|
|
106 fileHeader[1] = 1;
|
|
107 }
|
|
108 fileHeader[2] = byteStream.readShort();
|
|
109 } catch (IOException e) {
|
|
110 SWT.error(SWT.ERROR_IO, e);
|
|
111 }
|
|
112 if ((fileHeader[0] !is 0) || (fileHeader[1] !is 1))
|
|
113 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
114 int numIcons = fileHeader[2];
|
|
115 if (numIcons <= 0)
|
|
116 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
117 return numIcons;
|
|
118 }
|
|
119 override ImageData[] loadFromByteStream() {
|
|
120 int numIcons = loadFileHeader(inputStream);
|
|
121 int[][] headers = loadIconHeaders(numIcons);
|
|
122 ImageData[] icons = new ImageData[headers.length];
|
|
123 for (int i = 0; i < icons.length; i++) {
|
|
124 icons[i] = loadIcon(headers[i]);
|
|
125 }
|
|
126 return icons;
|
|
127 }
|
|
128 /**
|
|
129 * Load one icon from the byte stream.
|
|
130 */
|
|
131 ImageData loadIcon(int[] iconHeader) {
|
|
132 byte[] infoHeader = loadInfoHeader(iconHeader);
|
|
133 WinBMPFileFormat bmpFormat = new WinBMPFileFormat();
|
|
134 bmpFormat.inputStream = inputStream;
|
|
135 PaletteData palette = bmpFormat.loadPalette(infoHeader);
|
|
136 byte[] shapeData = bmpFormat.loadData(infoHeader);
|
|
137 int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24);
|
|
138 int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24);
|
|
139 if (height < 0) height = -height;
|
|
140 int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8);
|
|
141 infoHeader[14] = 1;
|
|
142 infoHeader[15] = 0;
|
|
143 byte[] maskData = bmpFormat.loadData(infoHeader);
|
|
144 maskData = convertPad(maskData, width, height, 1, 4, 2);
|
|
145 bitInvertData(maskData, 0, maskData.length);
|
|
146 return ImageData.internal_new(
|
|
147 width,
|
|
148 height,
|
|
149 depth,
|
|
150 palette,
|
|
151 4,
|
|
152 shapeData,
|
|
153 2,
|
|
154 maskData,
|
|
155 null,
|
|
156 -1,
|
|
157 -1,
|
|
158 SWT.IMAGE_ICO,
|
|
159 0,
|
|
160 0,
|
|
161 0,
|
|
162 0);
|
|
163 }
|
|
164 int[][] loadIconHeaders(int numIcons) {
|
|
165 int[][] headers = new int[][]( numIcons, 7 );
|
|
166 try {
|
|
167 for (int i = 0; i < numIcons; i++) {
|
|
168 headers[i][0] = inputStream.read();
|
|
169 headers[i][1] = inputStream.read();
|
|
170 headers[i][2] = inputStream.readShort();
|
|
171 headers[i][3] = inputStream.readShort();
|
|
172 headers[i][4] = inputStream.readShort();
|
|
173 headers[i][5] = inputStream.readInt();
|
|
174 headers[i][6] = inputStream.readInt();
|
|
175 }
|
|
176 } catch (IOException e) {
|
|
177 SWT.error(SWT.ERROR_IO, e);
|
|
178 }
|
|
179 return headers;
|
|
180 }
|
|
181 byte[] loadInfoHeader(int[] iconHeader) {
|
|
182 int width = iconHeader[0];
|
|
183 int height = iconHeader[1];
|
|
184 int numColors = iconHeader[2]; // the number of colors is in the low byte, but the high byte must be 0
|
|
185 if (numColors is 0) numColors = 256; // this is specified: '00' represents '256' (0x100) colors
|
|
186 if ((numColors !is 2) && (numColors !is 8) && (numColors !is 16) &&
|
|
187 (numColors !is 32) && (numColors !is 256))
|
|
188 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
189 if (inputStream.getPosition() < iconHeader[6]) {
|
|
190 // Seek to the specified offset
|
|
191 try {
|
|
192 inputStream.skip(iconHeader[6] - inputStream.getPosition());
|
|
193 } catch (IOException e) {
|
|
194 SWT.error(SWT.ERROR_IO, e);
|
|
195 return null;
|
|
196 }
|
|
197 }
|
|
198 byte[] infoHeader = new byte[WinBMPFileFormat.BMPHeaderFixedSize];
|
|
199 try {
|
|
200 inputStream.read(infoHeader);
|
|
201 } catch (IOException e) {
|
|
202 SWT.error(SWT.ERROR_IO, e);
|
|
203 }
|
|
204 if (((infoHeader[12] & 0xFF) | ((infoHeader[13] & 0xFF) << 8)) !is 1)
|
|
205 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
206 int infoWidth = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24);
|
|
207 int infoHeight = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24);
|
|
208 int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8);
|
|
209 if (height is infoHeight && bitCount is 1) height /= 2;
|
|
210 if (!((width is infoWidth) && (height * 2 is infoHeight) &&
|
|
211 (bitCount is 1 || bitCount is 4 || bitCount is 8 || bitCount is 24 || bitCount is 32)))
|
|
212 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
213 infoHeader[8] = cast(byte)(height & 0xFF);
|
|
214 infoHeader[9] = cast(byte)((height >> 8) & 0xFF);
|
|
215 infoHeader[10] = cast(byte)((height >> 16) & 0xFF);
|
|
216 infoHeader[11] = cast(byte)((height >> 24) & 0xFF);
|
|
217 return infoHeader;
|
|
218 }
|
|
219 /**
|
|
220 * Unload a single icon
|
|
221 */
|
|
222 void unloadIcon(ImageData icon) {
|
|
223 int sizeImage = (((icon.width * icon.depth + 31) / 32 * 4) +
|
|
224 ((icon.width + 31) / 32 * 4)) * icon.height;
|
|
225 try {
|
|
226 outputStream.writeInt(WinBMPFileFormat.BMPHeaderFixedSize);
|
|
227 outputStream.writeInt(icon.width);
|
|
228 outputStream.writeInt(icon.height * 2);
|
|
229 outputStream.writeShort(1);
|
|
230 outputStream.writeShort(cast(short)icon.depth);
|
|
231 outputStream.writeInt(0);
|
|
232 outputStream.writeInt(sizeImage);
|
|
233 outputStream.writeInt(0);
|
|
234 outputStream.writeInt(0);
|
|
235 outputStream.writeInt(icon.palette.colors !is null ? icon.palette.colors.length : 0);
|
|
236 outputStream.writeInt(0);
|
|
237 } catch (IOException e) {
|
|
238 SWT.error(SWT.ERROR_IO, e);
|
|
239 }
|
|
240
|
|
241 byte[] rgbs = WinBMPFileFormat.paletteToBytes(icon.palette);
|
|
242 try {
|
|
243 outputStream.write(rgbs);
|
|
244 } catch (IOException e) {
|
|
245 SWT.error(SWT.ERROR_IO, e);
|
|
246 }
|
|
247 unloadShapeData(icon);
|
|
248 unloadMaskData(icon);
|
|
249 }
|
|
250 /**
|
|
251 * Unload the icon header for the given icon, calculating the offset.
|
|
252 */
|
|
253 void unloadIconHeader(ImageData i) {
|
|
254 int headerSize = 16;
|
|
255 int offset = headerSize + 6;
|
|
256 int iconSize = iconSize(i);
|
|
257 try {
|
|
258 outputStream.write(i.width);
|
|
259 outputStream.write(i.height);
|
|
260 outputStream.writeShort(i.palette.colors !is null ? i.palette.colors.length : 0);
|
|
261 outputStream.writeShort(0);
|
|
262 outputStream.writeShort(0);
|
|
263 outputStream.writeInt(iconSize);
|
|
264 outputStream.writeInt(offset);
|
|
265 } catch (IOException e) {
|
|
266 SWT.error(SWT.ERROR_IO, e);
|
|
267 }
|
|
268 }
|
|
269 override void unloadIntoByteStream(ImageLoader loader) {
|
|
270 /* We do not currently support writing multi-image ico,
|
|
271 * so we use the first image data in the loader's array. */
|
|
272 ImageData image = loader.data[0];
|
|
273 if (!isValidIcon(image))
|
|
274 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
275 try {
|
|
276 outputStream.writeShort(0);
|
|
277 outputStream.writeShort(1);
|
|
278 outputStream.writeShort(1);
|
|
279 } catch (IOException e) {
|
|
280 SWT.error(SWT.ERROR_IO, e);
|
|
281 }
|
|
282 unloadIconHeader(image);
|
|
283 unloadIcon(image);
|
|
284 }
|
|
285 /**
|
|
286 * Unload the mask data for an icon. The data is flipped vertically
|
|
287 * and inverted.
|
|
288 */
|
|
289 void unloadMaskData(ImageData icon) {
|
|
290 ImageData mask = icon.getTransparencyMask();
|
|
291 int bpl = (icon.width + 7) / 8;
|
|
292 int pad = mask.scanlinePad;
|
|
293 int srcBpl = (bpl + pad - 1) / pad * pad;
|
|
294 int destBpl = (bpl + 3) / 4 * 4;
|
|
295 byte[] buf = new byte[destBpl];
|
|
296 int offset = (icon.height - 1) * srcBpl;
|
|
297 byte[] data = mask.data;
|
|
298 try {
|
|
299 for (int i = 0; i < icon.height; i++) {
|
|
300 System.arraycopy(data, offset, buf, 0, bpl);
|
|
301 bitInvertData(buf, 0, bpl);
|
|
302 outputStream.write(buf, 0, destBpl);
|
|
303 offset -= srcBpl;
|
|
304 }
|
|
305 } catch (IOException e) {
|
|
306 SWT.error(SWT.ERROR_IO, e);
|
|
307 }
|
|
308 }
|
|
309 /**
|
|
310 * Unload the shape data for an icon. The data is flipped vertically.
|
|
311 */
|
|
312 void unloadShapeData(ImageData icon) {
|
|
313 int bpl = (icon.width * icon.depth + 7) / 8;
|
|
314 int pad = icon.scanlinePad;
|
|
315 int srcBpl = (bpl + pad - 1) / pad * pad;
|
|
316 int destBpl = (bpl + 3) / 4 * 4;
|
|
317 byte[] buf = new byte[destBpl];
|
|
318 int offset = (icon.height - 1) * srcBpl;
|
|
319 byte[] data = icon.data;
|
|
320 try {
|
|
321 for (int i = 0; i < icon.height; i++) {
|
|
322 System.arraycopy(data, offset, buf, 0, bpl);
|
|
323 outputStream.write(buf, 0, destBpl);
|
|
324 offset -= srcBpl;
|
|
325 }
|
|
326 } catch (IOException e) {
|
|
327 SWT.error(SWT.ERROR_IO, e);
|
|
328 }
|
|
329 }
|
|
330 }
|