comparison dwt/internal/image/WinICOFileFormat.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 d8635bb48c7c
comparison
equal deleted inserted replaced
33:965ac0a77267 34:5123b17c98ef
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
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.WinICOFileFormat;
12 14
13 15 import dwt.internal.image.FileFormat;
14 import java.io.IOException; 16 import dwt.graphics.PaletteData;
15 17 import dwt.internal.image.WinBMPFileFormat;
16 import dwt.DWT; 18 import dwt.DWT;
17 import dwt.graphics.ImageData; 19 import dwt.dwthelper.utils;
18 import dwt.graphics.ImageLoader; 20
19 import dwt.graphics.PaletteData; 21 import tango.core.Exception;
20 22
21 public final class WinICOFileFormat : FileFormat { 23 public final class WinICOFileFormat : FileFormat {
22 24
23 byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { 25 byte[] bitInvertData(byte[] data, int startIndex, int endIndex) {
24 // Destructively bit invert data in the given byte array. 26 // Destructively bit invert data in the given byte array.
25 for (int i = startIndex; i < endIndex; i++) { 27 for (int i = startIndex; i < endIndex; i++) {
26 data[i] = cast(byte)(255 - data[i - startIndex]); 28 data[i] = cast(byte)(255 - data[i - startIndex]);
27 } 29 }
51 int maskDataStride = (i.width + 31) / 32 * 4; 53 int maskDataStride = (i.width + 31) / 32 * 4;
52 int dataSize = (shapeDataStride + maskDataStride) * i.height; 54 int dataSize = (shapeDataStride + maskDataStride) * i.height;
53 int paletteSize = i.palette.colors !is null ? i.palette.colors.length * 4 : 0; 55 int paletteSize = i.palette.colors !is null ? i.palette.colors.length * 4 : 0;
54 return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize; 56 return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize;
55 } 57 }
56 bool isFileFormat(LEDataInputStream stream) { 58 override bool isFileFormat(LEDataInputStream stream) {
57 try { 59 try {
58 byte[] header = new byte[4]; 60 byte[] header = new byte[4];
59 stream.read(header); 61 stream.read(header);
60 stream.unread(header); 62 stream.unread(header);
61 return header[0] is 0 && header[1] is 0 && header[2] is 1 && header[3] is 0; 63 return header[0] is 0 && header[1] is 0 && header[2] is 1 && header[3] is 0;
72 int size = i.palette.colors.length; 74 int size = i.palette.colors.length;
73 return size is 2 || size is 16 || size is 32 || size is 256; 75 return size is 2 || size is 16 || size is 32 || size is 256;
74 case 24: 76 case 24:
75 case 32: 77 case 32:
76 return i.palette.isDirect; 78 return i.palette.isDirect;
79 default:
77 } 80 }
78 return false; 81 return false;
79 } 82 }
80 int loadFileHeader(LEDataInputStream byteStream) { 83 int loadFileHeader(LEDataInputStream byteStream) {
81 int[] fileHeader = new int[3]; 84 int[] fileHeader = new int[3];
112 int numIcons = fileHeader[2]; 115 int numIcons = fileHeader[2];
113 if (numIcons <= 0) 116 if (numIcons <= 0)
114 DWT.error(DWT.ERROR_INVALID_IMAGE); 117 DWT.error(DWT.ERROR_INVALID_IMAGE);
115 return numIcons; 118 return numIcons;
116 } 119 }
117 ImageData[] loadFromByteStream() { 120 override ImageData[] loadFromByteStream() {
118 int numIcons = loadFileHeader(inputStream); 121 int numIcons = loadFileHeader(inputStream);
119 int[][] headers = loadIconHeaders(numIcons); 122 int[][] headers = loadIconHeaders(numIcons);
120 ImageData[] icons = new ImageData[headers.length]; 123 ImageData[] icons = new ImageData[headers.length];
121 for (int i = 0; i < icons.length; i++) { 124 for (int i = 0; i < icons.length; i++) {
122 icons[i] = loadIcon(headers[i]); 125 icons[i] = loadIcon(headers[i]);
158 0, 161 0,
159 0, 162 0,
160 0); 163 0);
161 } 164 }
162 int[][] loadIconHeaders(int numIcons) { 165 int[][] loadIconHeaders(int numIcons) {
163 int[][] headers = new int[numIcons][7]; 166 int[][] headers = new int[][]( numIcons, 7 );
164 try { 167 try {
165 for (int i = 0; i < numIcons; i++) { 168 for (int i = 0; i < numIcons; i++) {
166 headers[i][0] = inputStream.read(); 169 headers[i][0] = inputStream.read();
167 headers[i][1] = inputStream.read(); 170 headers[i][1] = inputStream.read();
168 headers[i][2] = inputStream.readShort(); 171 headers[i][2] = inputStream.readShort();
233 outputStream.writeInt(icon.palette.colors !is null ? icon.palette.colors.length : 0); 236 outputStream.writeInt(icon.palette.colors !is null ? icon.palette.colors.length : 0);
234 outputStream.writeInt(0); 237 outputStream.writeInt(0);
235 } catch (IOException e) { 238 } catch (IOException e) {
236 DWT.error(DWT.ERROR_IO, e); 239 DWT.error(DWT.ERROR_IO, e);
237 } 240 }
238 241
239 byte[] rgbs = WinBMPFileFormat.paletteToBytes(icon.palette); 242 byte[] rgbs = WinBMPFileFormat.paletteToBytes(icon.palette);
240 try { 243 try {
241 outputStream.write(rgbs); 244 outputStream.write(rgbs);
242 } catch (IOException e) { 245 } catch (IOException e) {
243 DWT.error(DWT.ERROR_IO, e); 246 DWT.error(DWT.ERROR_IO, e);
262 outputStream.writeInt(offset); 265 outputStream.writeInt(offset);
263 } catch (IOException e) { 266 } catch (IOException e) {
264 DWT.error(DWT.ERROR_IO, e); 267 DWT.error(DWT.ERROR_IO, e);
265 } 268 }
266 } 269 }
267 void unloadIntoByteStream(ImageLoader loader) { 270 override void unloadIntoByteStream(ImageLoader loader) {
268 /* We do not currently support writing multi-image ico, 271 /* We do not currently support writing multi-image ico,
269 * so we use the first image data in the loader's array. */ 272 * so we use the first image data in the loader's array. */
270 ImageData image = loader.data[0]; 273 ImageData image = loader.data[0];
271 if (!isValidIcon(image)) 274 if (!isValidIcon(image))
272 DWT.error(DWT.ERROR_INVALID_IMAGE); 275 DWT.error(DWT.ERROR_INVALID_IMAGE);