Mercurial > projects > dwt-mac
comparison dwt/internal/image/OS2BMPFileFormat.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, 2005 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.OS2BMPFileFormat; |
12 | |
13 | |
14 import java.io.ByteArrayOutputStream; | |
15 import java.io.IOException; | |
16 import java.io.OutputStream; | |
17 | 14 |
18 import dwt.DWT; | 15 import dwt.DWT; |
19 import dwt.graphics.ImageData; | 16 import dwt.graphics.ImageData; |
20 import dwt.graphics.ImageLoader; | 17 import dwt.graphics.ImageLoader; |
21 import dwt.graphics.PaletteData; | 18 import dwt.graphics.PaletteData; |
22 import dwt.graphics.RGB; | 19 import dwt.graphics.RGB; |
23 | 20 import dwt.internal.image.LEDataInputStream; |
24 public final class OS2BMPFileFormat : FileFormat { | 21 import dwt.internal.image.FileFormat; |
22 import dwt.dwthelper.ByteArrayOutputStream; | |
23 import dwt.dwthelper.utils; | |
24 | |
25 import tango.core.Exception; | |
26 | |
27 final class OS2BMPFileFormat : FileFormat { | |
25 static final int BMPFileHeaderSize = 14; | 28 static final int BMPFileHeaderSize = 14; |
26 static final int BMPHeaderFixedSize = 12; | 29 static final int BMPHeaderFixedSize = 12; |
27 int width, height, bitCount; | 30 int width, height, bitCount; |
28 | 31 |
29 bool isFileFormat(LEDataInputStream stream) { | 32 override bool isFileFormat(LEDataInputStream stream) { |
30 try { | 33 try { |
31 byte[] header = new byte[18]; | 34 byte[] header = new byte[18]; |
32 stream.read(header); | 35 stream.read(header); |
33 stream.unread(header); | 36 stream.unread(header); |
34 int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); | 37 int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); |
68 } | 71 } |
69 if (header[0] !is 0x4D42) | 72 if (header[0] !is 0x4D42) |
70 DWT.error(DWT.ERROR_INVALID_IMAGE); | 73 DWT.error(DWT.ERROR_INVALID_IMAGE); |
71 return header; | 74 return header; |
72 } | 75 } |
73 ImageData[] loadFromByteStream() { | 76 override ImageData[] loadFromByteStream() { |
74 int[] fileHeader = loadFileHeader(); | 77 int[] fileHeader = loadFileHeader(); |
75 byte[] infoHeader = new byte[BMPHeaderFixedSize]; | 78 byte[] infoHeader = new byte[BMPHeaderFixedSize]; |
76 try { | 79 try { |
77 inputStream.read(infoHeader); | 80 inputStream.read(infoHeader); |
78 } catch (Exception e) { | 81 } catch (Exception e) { |
90 DWT.error(DWT.ERROR_IO, e); | 93 DWT.error(DWT.ERROR_IO, e); |
91 } | 94 } |
92 } | 95 } |
93 byte[] data = loadData(infoHeader); | 96 byte[] data = loadData(infoHeader); |
94 int type = DWT.IMAGE_OS2_BMP; | 97 int type = DWT.IMAGE_OS2_BMP; |
95 return new ImageData[] { | 98 return [ |
96 ImageData.internal_new( | 99 ImageData.internal_new( |
97 width, | 100 width, |
98 height, | 101 height, |
99 bitCount, | 102 bitCount, |
100 palette, | 103 palette, |
108 type, | 111 type, |
109 0, | 112 0, |
110 0, | 113 0, |
111 0, | 114 0, |
112 0) | 115 0) |
113 }; | 116 ]; |
114 } | 117 } |
115 PaletteData loadPalette(byte[] infoHeader) { | 118 PaletteData loadPalette(byte[] infoHeader) { |
116 if (bitCount <= 8) { | 119 if (bitCount <= 8) { |
117 int numColors = 1 << bitCount; | 120 int numColors = 1 << bitCount; |
118 byte[] buf = new byte[numColors * 3]; | 121 byte[] buf = new byte[numColors * 3]; |
155 offset += 3; | 158 offset += 3; |
156 } | 159 } |
157 return bytes; | 160 return bytes; |
158 } | 161 } |
159 /** | 162 /** |
160 * Unload the given image's data into the given byte stream. | 163 * Unload the given image's data into the given byte stream. |
161 * Answer the number of bytes written. | 164 * Answer the number of bytes written. |
162 */ | 165 */ |
163 int unloadData(ImageData image, OutputStream out) { | 166 int unloadData(ImageData image, OutputStream ostr) { |
164 int bmpBpl = 0; | 167 int bmpBpl = 0; |
165 try { | 168 try { |
166 int bpl = (image.width * image.depth + 7) / 8; | 169 int bpl = (image.width * image.depth + 7) / 8; |
167 bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes | 170 bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes |
168 int linesPerBuf = 32678 / bmpBpl; | 171 int linesPerBuf = 32678 / bmpBpl; |
181 buf[bufOffset + wIndex] = data[dataIndex + wIndex]; | 184 buf[bufOffset + wIndex] = data[dataIndex + wIndex]; |
182 } | 185 } |
183 bufOffset += bmpBpl; | 186 bufOffset += bmpBpl; |
184 dataIndex -= imageBpl; | 187 dataIndex -= imageBpl; |
185 } | 188 } |
186 out.write(buf, 0, bufOffset); | 189 ostr.write(buf, 0, bufOffset); |
187 } | 190 } |
188 } else { | 191 } else { |
189 for (int y = 0; y < image.height; y += linesPerBuf) { | 192 for (int y = 0; y < image.height; y += linesPerBuf) { |
190 int tmp = image.height - y; | 193 int tmp = image.height - y; |
191 int count = tmp < linesPerBuf ? tmp : linesPerBuf; | 194 int count = tmp < linesPerBuf ? tmp : linesPerBuf; |
193 for (int i = 0; i < count; i++) { | 196 for (int i = 0; i < count; i++) { |
194 System.arraycopy(data, dataIndex, buf, bufOffset, bpl); | 197 System.arraycopy(data, dataIndex, buf, bufOffset, bpl); |
195 bufOffset += bmpBpl; | 198 bufOffset += bmpBpl; |
196 dataIndex -= imageBpl; | 199 dataIndex -= imageBpl; |
197 } | 200 } |
198 out.write(buf, 0, bufOffset); | 201 ostr.write(buf, 0, bufOffset); |
199 } | 202 } |
200 } | 203 } |
201 } catch (IOException e) { | 204 } catch (IOException e) { |
202 DWT.error(DWT.ERROR_IO, e); | 205 DWT.error(DWT.ERROR_IO, e); |
203 } | 206 } |
205 } | 208 } |
206 /** | 209 /** |
207 * Unload a DeviceIndependentImage using Windows .BMP format into the given | 210 * Unload a DeviceIndependentImage using Windows .BMP format into the given |
208 * byte stream. | 211 * byte stream. |
209 */ | 212 */ |
210 void unloadIntoByteStream(ImageLoader loader) { | 213 override void unloadIntoByteStream(ImageLoader loader) { |
211 ImageData image = loader.data[0]; | 214 ImageData image = loader.data[0]; |
212 byte[] rgbs; | 215 byte[] rgbs; |
213 int numCols; | 216 int numCols; |
214 if (!((image.depth is 1) || (image.depth is 4) || (image.depth is 8) || | 217 if (!((image.depth is 1) || (image.depth is 4) || (image.depth is 8) || |
215 (image.depth is 16) || (image.depth is 24) || (image.depth is 32))) | 218 (image.depth is 16) || (image.depth is 24) || (image.depth is 32))) |
238 fileHeader[4] += rgbs.length; | 241 fileHeader[4] += rgbs.length; |
239 } | 242 } |
240 | 243 |
241 // Prepare data. This is done first so we don't have to try to rewind | 244 // Prepare data. This is done first so we don't have to try to rewind |
242 // the stream and fill in the details later. | 245 // the stream and fill in the details later. |
243 ByteArrayOutputStream out = new ByteArrayOutputStream(); | 246 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); |
244 unloadData(image, out); | 247 unloadData(image, ostr); |
245 byte[] data = out.toByteArray(); | 248 byte[] data = ostr.toByteArray(); |
246 | 249 |
247 // Calculate file size | 250 // Calculate file size |
248 fileHeader[1] = fileHeader[4] + data.length; | 251 fileHeader[1] = fileHeader[4] + data.length; |
249 | 252 |
250 // Write the headers | 253 // Write the headers |
251 try { | 254 try { |
264 outputStream.writeShort(1); | 267 outputStream.writeShort(1); |
265 outputStream.writeShort(cast(short)image.depth); | 268 outputStream.writeShort(cast(short)image.depth); |
266 } catch (IOException e) { | 269 } catch (IOException e) { |
267 DWT.error(DWT.ERROR_IO, e); | 270 DWT.error(DWT.ERROR_IO, e); |
268 } | 271 } |
269 | 272 |
270 // Unload palette | 273 // Unload palette |
271 if (numCols > 0) { | 274 if (numCols > 0) { |
272 try { | 275 try { |
273 outputStream.write(rgbs); | 276 outputStream.write(rgbs); |
274 } catch (IOException e) { | 277 } catch (IOException e) { |