Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/internal/image/WinBMPFileFormat.d @ 49:7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 12:59:54 +0100 |
parents | ddbfe84d86df |
children |
rev | line source |
---|---|
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.WinBMPFileFormat; | |
14 | |
15 import org.eclipse.swt.internal.image.FileFormat; | |
16 import org.eclipse.swt.graphics.PaletteData; | |
17 import org.eclipse.swt.graphics.Point; | |
18 import org.eclipse.swt.graphics.RGB; | |
19 import java.io.ByteArrayOutputStream; | |
20 import org.eclipse.swt.SWT; | |
21 import java.lang.all; | |
22 | |
23 | |
24 final class WinBMPFileFormat : FileFormat { | |
25 | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
26 static const int BMPFileHeaderSize = 14; |
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
27 static const int BMPHeaderFixedSize = 40; |
25 | 28 int importantColors; |
29 Point pelsPerMeter; | |
30 | |
31 public this(){ | |
32 pelsPerMeter = new Point(0, 0); | |
33 } | |
34 | |
35 /** | |
36 * Compress numBytes bytes of image data from src, storing in dest | |
37 * (starting at 0), using the technique specified by comp. | |
38 * If last is true, this indicates the last line of the image. | |
39 * Answer the size of the compressed data. | |
40 */ | |
41 int compress(int comp, byte[] src, int srcOffset, int numBytes, byte[] dest, bool last) { | |
42 if (comp is 1) { // BMP_RLE8_COMPRESSION | |
43 return compressRLE8Data(src, srcOffset, numBytes, dest, last); | |
44 } | |
45 if (comp is 2) { // BMP_RLE4_COMPRESSION | |
46 return compressRLE4Data(src, srcOffset, numBytes, dest, last); | |
47 } | |
48 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
49 return 0; | |
50 } | |
51 int compressRLE4Data(byte[] src, int srcOffset, int numBytes, byte[] dest, bool last) { | |
52 int sp = srcOffset, end = srcOffset + numBytes, dp = 0; | |
53 int size = 0, left, i, n; | |
54 byte theByte; | |
55 while (sp < end) { | |
56 /* find two consecutive bytes that are the same in the next 128 */ | |
57 left = end - sp - 1; | |
58 if (left > 127) | |
59 left = 127; | |
60 for (n = 0; n < left; n++) { | |
61 if (src[sp + n] is src[sp + n + 1]) | |
62 break; | |
63 } | |
64 /* if there is only one more byte in the scan line, include it */ | |
65 if (n < 127 && n is left) | |
66 n++; | |
67 /* store the intervening data */ | |
68 switch (n) { | |
69 case 0: | |
70 break; | |
71 case 1: /* handled separately because 0,2 is a command */ | |
72 dest[dp] = 2; dp++; /* 1 byte is 2 pixels */ | |
73 dest[dp] = src[sp]; | |
74 dp++; sp++; | |
75 size += 2; | |
76 break; | |
77 default: | |
78 dest[dp] = 0; dp++; | |
79 dest[dp] = cast(byte)(n + n); dp++; /* n bytes = n*2 pixels */ | |
80 for (i = n; i > 0; i--) { | |
81 dest[dp] = src[sp]; | |
82 dp++; sp++; | |
83 } | |
84 size += 2 + n; | |
85 if ((n & 1) !is 0) { /* pad to word */ | |
86 dest[dp] = 0; | |
87 dp++; | |
88 size++; | |
89 } | |
90 break; | |
91 } | |
92 /* find the length of the next run (up to 127) and store it */ | |
93 left = end - sp; | |
94 if (left > 0) { | |
95 if (left > 127) | |
96 left = 127; | |
97 theByte = src[sp]; | |
98 for (n = 1; n < left; n++) { | |
99 if (src[sp + n] !is theByte) | |
100 break; | |
101 } | |
102 dest[dp] = cast(byte)(n + n); dp++; /* n bytes = n*2 pixels */ | |
103 dest[dp] = theByte; dp++; | |
104 sp += n; | |
105 size += 2; | |
106 } | |
107 } | |
108 | |
109 /* store the end of line or end of bitmap codes */ | |
110 dest[dp] = 0; dp++; | |
111 if (last) { | |
112 dest[dp] = 1; dp++; | |
113 } else { | |
114 dest[dp] = 0; dp++; | |
115 } | |
116 size += 2; | |
117 | |
118 return size; | |
119 } | |
120 int compressRLE8Data(byte[] src, int srcOffset, int numBytes, byte[] dest, bool last) { | |
121 int sp = srcOffset, end = srcOffset + numBytes, dp = 0; | |
122 int size = 0, left, i, n; | |
123 byte theByte; | |
124 while (sp < end) { | |
125 /* find two consecutive bytes that are the same in the next 256 */ | |
126 left = end - sp - 1; | |
127 if (left > 254) | |
128 left = 254; | |
129 for (n = 0; n < left; n++) { | |
130 if (src[sp + n] is src[sp + n + 1]) | |
131 break; | |
132 } | |
133 /* if there is only one more byte in the scan line, include it */ | |
134 if (n is left) | |
135 n++; | |
136 /* store the intervening data */ | |
137 switch (n) { | |
138 case 0: | |
139 break; | |
140 case 2: /* handled separately because 0,2 is a command */ | |
141 dest[dp] = 1; dp++; | |
142 dest[dp] = src[sp]; | |
143 dp++; sp++; | |
144 size += 2; | |
145 /* don't break, fall through */ | |
146 case 1: /* handled separately because 0,1 is a command */ | |
147 dest[dp] = 1; dp++; | |
148 dest[dp] = src[sp]; | |
149 dp++; sp++; | |
150 size += 2; | |
151 break; | |
152 default: | |
153 dest[dp] = 0; dp++; | |
154 dest[dp] = cast(byte)n; dp++; | |
155 for (i = n; i > 0; i--) { | |
156 dest[dp] = src[sp]; | |
157 dp++; sp++; | |
158 } | |
159 size += 2 + n; | |
160 if ((n & 1) !is 0) { /* pad to word */ | |
161 dest[dp] = 0; | |
162 dp++; | |
163 size++; | |
164 } | |
165 break; | |
166 } | |
167 /* find the length of the next run (up to 255) and store it */ | |
168 left = end - sp; | |
169 if (left > 0) { | |
170 if (left > 255) | |
171 left = 255; | |
172 theByte = src[sp]; | |
173 for (n = 1; n < left; n++) { | |
174 if (src[sp + n] !is theByte) | |
175 break; | |
176 } | |
177 dest[dp] = cast(byte)n; dp++; | |
178 dest[dp] = theByte; dp++; | |
179 sp += n; | |
180 size += 2; | |
181 } | |
182 } | |
183 | |
184 /* store the end of line or end of bitmap codes */ | |
185 dest[dp] = 0; dp++; | |
186 if (last) { | |
187 dest[dp] = 1; dp++; | |
188 } else { | |
189 dest[dp] = 0; dp++; | |
190 } | |
191 size += 2; | |
192 | |
193 return size; | |
194 } | |
195 void decompressData(byte[] src, byte[] dest, int stride, int cmp) { | |
196 if (cmp is 1) { // BMP_RLE8_COMPRESSION | |
197 if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0) | |
198 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
199 return; | |
200 } | |
201 if (cmp is 2) { // BMP_RLE4_COMPRESSION | |
202 if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0) | |
203 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
204 return; | |
205 } | |
206 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
207 } | |
208 int decompressRLE4Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { | |
209 int sp = 0; | |
210 int se = numBytes; | |
211 int dp = 0; | |
212 int de = destSize; | |
213 int x = 0, y = 0; | |
214 while (sp < se) { | |
215 int len = src[sp] & 0xFF; | |
216 sp++; | |
217 if (len is 0) { | |
218 len = src[sp] & 0xFF; | |
219 sp++; | |
220 switch (len) { | |
221 case 0: /* end of line */ | |
222 y++; | |
223 x = 0; | |
224 dp = y * stride; | |
225 if (dp > de) | |
226 return -1; | |
227 break; | |
228 case 1: /* end of bitmap */ | |
229 return 1; | |
230 case 2: /* delta */ | |
231 x += src[sp] & 0xFF; | |
232 sp++; | |
233 y += src[sp] & 0xFF; | |
234 sp++; | |
235 dp = y * stride + x / 2; | |
236 if (dp > de) | |
237 return -1; | |
238 break; | |
239 default: /* absolute mode run */ | |
240 if ((len & 1) !is 0) /* odd run lengths not currently supported */ | |
241 return -1; | |
242 x += len; | |
243 len = len / 2; | |
244 if (len > (se - sp)) | |
245 return -1; | |
246 if (len > (de - dp)) | |
247 return -1; | |
248 for (int i = 0; i < len; i++) { | |
249 dest[dp] = src[sp]; | |
250 dp++; | |
251 sp++; | |
252 } | |
253 if ((sp & 1) !is 0) | |
254 sp++; /* word align sp? */ | |
255 break; | |
256 } | |
257 } else { | |
258 if ((len & 1) !is 0) | |
259 return -1; | |
260 x += len; | |
261 len = len / 2; | |
262 byte theByte = src[sp]; | |
263 sp++; | |
264 if (len > (de - dp)) | |
265 return -1; | |
266 for (int i = 0; i < len; i++) { | |
267 dest[dp] = theByte; | |
268 dp++; | |
269 } | |
270 } | |
271 } | |
272 return 1; | |
273 } | |
274 int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { | |
275 int sp = 0; | |
276 int se = numBytes; | |
277 int dp = 0; | |
278 int de = destSize; | |
279 int x = 0, y = 0; | |
280 while (sp < se) { | |
281 int len = src[sp] & 0xFF; | |
282 sp++; | |
283 if (len is 0) { | |
284 len = src[sp] & 0xFF; | |
285 sp++; | |
286 switch (len) { | |
287 case 0: /* end of line */ | |
288 y++; | |
289 x = 0; | |
290 dp = y * stride; | |
291 if (dp > de) | |
292 return -1; | |
293 break; | |
294 case 1: /* end of bitmap */ | |
295 return 1; | |
296 case 2: /* delta */ | |
297 x += src[sp] & 0xFF; | |
298 sp++; | |
299 y += src[sp] & 0xFF; | |
300 sp++; | |
301 dp = y * stride + x; | |
302 if (dp > de) | |
303 return -1; | |
304 break; | |
305 default: /* absolute mode run */ | |
306 if (len > (se - sp)) | |
307 return -1; | |
308 if (len > (de - dp)) | |
309 return -1; | |
310 for (int i = 0; i < len; i++) { | |
311 dest[dp] = src[sp]; | |
312 dp++; | |
313 sp++; | |
314 } | |
315 if ((sp & 1) !is 0) | |
316 sp++; /* word align sp? */ | |
317 x += len; | |
318 break; | |
319 } | |
320 } else { | |
321 byte theByte = src[sp]; | |
322 sp++; | |
323 if (len > (de - dp)) | |
324 return -1; | |
325 for (int i = 0; i < len; i++) { | |
326 dest[dp] = theByte; | |
327 dp++; | |
328 } | |
329 x += len; | |
330 } | |
331 } | |
332 return 1; | |
333 } | |
334 override bool isFileFormat(LEDataInputStream stream) { | |
335 try { | |
336 byte[] header = new byte[18]; | |
337 stream.read(header); | |
338 stream.unread(header); | |
339 int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); | |
340 return header[0] is 0x42 && header[1] is 0x4D && infoHeaderSize >= BMPHeaderFixedSize; | |
341 } catch (Exception e) { | |
342 return false; | |
343 } | |
344 } | |
345 byte[] loadData(byte[] infoHeader) { | |
346 int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); | |
347 int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); | |
348 int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); | |
349 int stride = (width * bitCount + 7) / 8; | |
350 stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple | |
351 byte[] data = loadData(infoHeader, stride); | |
352 flipScanLines(data, stride, height); | |
353 return data; | |
354 } | |
355 byte[] loadData(byte[] infoHeader, int stride) { | |
356 int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); | |
357 if (height < 0) height = -height; | |
358 int dataSize = height * stride; | |
359 byte[] data = new byte[dataSize]; | |
360 int cmp = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); | |
361 if (cmp is 0 || cmp is 3) { // BMP_NO_COMPRESSION | |
362 try { | |
363 if (inputStream.read(data) !is dataSize) | |
364 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
365 } catch (IOException e) { | |
366 SWT.error(SWT.ERROR_IO, e); | |
367 } | |
368 } else { | |
369 int compressedSize = (infoHeader[20] & 0xFF) | ((infoHeader[21] & 0xFF) << 8) | ((infoHeader[22] & 0xFF) << 16) | ((infoHeader[23] & 0xFF) << 24); | |
370 byte[] compressed = new byte[compressedSize]; | |
371 try { | |
372 if (inputStream.read(compressed) !is compressedSize) | |
373 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
374 } catch (IOException e) { | |
375 SWT.error(SWT.ERROR_IO, e); | |
376 } | |
377 decompressData(compressed, data, stride, cmp); | |
378 } | |
379 return data; | |
380 } | |
381 int[] loadFileHeader() { | |
382 int[] header = new int[5]; | |
383 try { | |
384 header[0] = inputStream.readShort(); | |
385 header[1] = inputStream.readInt(); | |
386 header[2] = inputStream.readShort(); | |
387 header[3] = inputStream.readShort(); | |
388 header[4] = inputStream.readInt(); | |
389 } catch (IOException e) { | |
390 SWT.error(SWT.ERROR_IO, e); | |
391 } | |
392 if (header[0] !is 0x4D42) | |
393 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
394 return header; | |
395 } | |
396 override ImageData[] loadFromByteStream() { | |
397 int[] fileHeader = loadFileHeader(); | |
398 byte[] infoHeader = new byte[BMPHeaderFixedSize]; | |
399 try { | |
400 inputStream.read(infoHeader); | |
401 } catch (Exception e) { | |
402 SWT.error(SWT.ERROR_IO, e); | |
403 } | |
404 int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); | |
405 int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); | |
406 if (height < 0) height = -height; | |
407 int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); | |
408 this.compression = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); | |
409 PaletteData palette = loadPalette(infoHeader); | |
410 if (inputStream.getPosition() < fileHeader[4]) { | |
411 // Seek to the specified offset | |
412 try { | |
413 inputStream.skip(fileHeader[4] - inputStream.getPosition()); | |
414 } catch (IOException e) { | |
415 SWT.error(SWT.ERROR_IO, e); | |
416 } | |
417 } | |
418 byte[] data = loadData(infoHeader); | |
419 this.importantColors = (infoHeader[36] & 0xFF) | ((infoHeader[37] & 0xFF) << 8) | ((infoHeader[38] & 0xFF) << 16) | ((infoHeader[39] & 0xFF) << 24); | |
420 int xPelsPerMeter = (infoHeader[24] & 0xFF) | ((infoHeader[25] & 0xFF) << 8) | ((infoHeader[26] & 0xFF) << 16) | ((infoHeader[27] & 0xFF) << 24); | |
421 int yPelsPerMeter = (infoHeader[28] & 0xFF) | ((infoHeader[29] & 0xFF) << 8) | ((infoHeader[30] & 0xFF) << 16) | ((infoHeader[31] & 0xFF) << 24); | |
422 this.pelsPerMeter = new Point(xPelsPerMeter, yPelsPerMeter); | |
423 int type = (this.compression is 1 /*BMP_RLE8_COMPRESSION*/) || (this.compression is 2 /*BMP_RLE4_COMPRESSION*/) ? SWT.IMAGE_BMP_RLE : SWT.IMAGE_BMP; | |
424 return [ | |
425 ImageData.internal_new( | |
426 width, | |
427 height, | |
428 bitCount, | |
429 palette, | |
430 4, | |
431 data, | |
432 0, | |
433 null, | |
434 null, | |
435 -1, | |
436 -1, | |
437 type, | |
438 0, | |
439 0, | |
440 0, | |
441 0) | |
442 ]; | |
443 } | |
444 PaletteData loadPalette(byte[] infoHeader) { | |
445 int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); | |
446 if (depth <= 8) { | |
447 int numColors = (infoHeader[32] & 0xFF) | ((infoHeader[33] & 0xFF) << 8) | ((infoHeader[34] & 0xFF) << 16) | ((infoHeader[35] & 0xFF) << 24); | |
448 if (numColors is 0) { | |
449 numColors = 1 << depth; | |
450 } else { | |
451 if (numColors > 256) | |
452 numColors = 256; | |
453 } | |
454 byte[] buf = new byte[numColors * 4]; | |
455 try { | |
456 if (inputStream.read(buf) !is buf.length) | |
457 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
458 } catch (IOException e) { | |
459 SWT.error(SWT.ERROR_IO, e); | |
460 } | |
461 return paletteFromBytes(buf, numColors); | |
462 } | |
463 if (depth is 16) { | |
464 if (this.compression is 3) { | |
465 try { | |
466 return new PaletteData(inputStream.readInt(), inputStream.readInt(), inputStream.readInt()); | |
467 } catch (IOException e) { | |
468 SWT.error(SWT.ERROR_IO, e); | |
469 } | |
470 } | |
471 return new PaletteData(0x7C00, 0x3E0, 0x1F); | |
472 } | |
473 if (depth is 24) return new PaletteData(0xFF, 0xFF00, 0xFF0000); | |
474 if (this.compression is 3) { | |
475 try { | |
476 return new PaletteData(inputStream.readInt(), inputStream.readInt(), inputStream.readInt()); | |
477 } catch (IOException e) { | |
478 SWT.error(SWT.ERROR_IO, e); | |
479 } | |
480 } | |
481 return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); | |
482 } | |
483 PaletteData paletteFromBytes(byte[] bytes, int numColors) { | |
484 int bytesOffset = 0; | |
485 RGB[] colors = new RGB[numColors]; | |
486 for (int i = 0; i < numColors; i++) { | |
487 colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, | |
488 bytes[bytesOffset + 1] & 0xFF, | |
489 bytes[bytesOffset] & 0xFF); | |
490 bytesOffset += 4; | |
491 } | |
492 return new PaletteData(colors); | |
493 } | |
494 /** | |
495 * Answer a byte array containing the BMP representation of | |
496 * the given device independent palette. | |
497 */ | |
498 static byte[] paletteToBytes(PaletteData pal) { | |
499 int n = pal.colors is null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); | |
500 byte[] bytes = new byte[n * 4]; | |
501 int offset = 0; | |
502 for (int i = 0; i < n; i++) { | |
503 RGB col = pal.colors[i]; | |
504 bytes[offset] = cast(byte)col.blue; | |
505 bytes[offset + 1] = cast(byte)col.green; | |
506 bytes[offset + 2] = cast(byte)col.red; | |
507 offset += 4; | |
508 } | |
509 return bytes; | |
510 } | |
511 /** | |
512 * Unload the given image's data into the given byte stream | |
513 * using the given compression strategy. | |
514 * Answer the number of bytes written. | |
515 */ | |
516 int unloadData(ImageData image, OutputStream ostr, int comp) { | |
517 int totalSize = 0; | |
518 try { | |
519 if (comp is 0) | |
520 return unloadDataNoCompression(image, ostr); | |
521 int bpl = (image.width * image.depth + 7) / 8; | |
522 int bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes | |
523 int imageBpl = image.bytesPerLine; | |
524 // Compression can actually take twice as much space, in worst case | |
525 byte[] buf = new byte[bmpBpl * 2]; | |
526 int srcOffset = imageBpl * (image.height - 1); // Start at last line | |
527 byte[] data = image.data; | |
528 totalSize = 0; | |
529 byte[] buf2 = new byte[32768]; | |
530 int buf2Offset = 0; | |
531 for (int y = image.height - 1; y >= 0; y--) { | |
532 int lineSize = compress(comp, data, srcOffset, bpl, buf, y is 0); | |
533 if (buf2Offset + lineSize > buf2.length) { | |
534 ostr.write(buf2, 0, buf2Offset); | |
535 buf2Offset = 0; | |
536 } | |
537 System.arraycopy(buf, 0, buf2, buf2Offset, lineSize); | |
538 buf2Offset += lineSize; | |
539 totalSize += lineSize; | |
540 srcOffset -= imageBpl; | |
541 } | |
542 if (buf2Offset > 0) | |
543 ostr.write(buf2, 0, buf2Offset); | |
544 } catch (IOException e) { | |
545 SWT.error(SWT.ERROR_IO, e); | |
546 } | |
547 return totalSize; | |
548 } | |
549 /** | |
550 * Prepare the given image's data for unloading into a byte stream | |
551 * using no compression strategy. | |
552 * Answer the number of bytes written. | |
553 */ | |
554 int unloadDataNoCompression(ImageData image, OutputStream ostr) { | |
555 int bmpBpl = 0; | |
556 try { | |
557 int bpl = (image.width * image.depth + 7) / 8; | |
558 bmpBpl = (bpl + 3) / 4 * 4; // BMP pads scanlines to multiples of 4 bytes | |
559 int linesPerBuf = 32678 / bmpBpl; | |
560 byte[] buf = new byte[linesPerBuf * bmpBpl]; | |
561 byte[] data = image.data; | |
562 int imageBpl = image.bytesPerLine; | |
563 int dataIndex = imageBpl * (image.height - 1); // Start at last line | |
564 if (image.depth is 16) { | |
565 for (int y = 0; y < image.height; y += linesPerBuf) { | |
566 int count = image.height - y; | |
567 if (linesPerBuf < count) count = linesPerBuf; | |
568 int bufOffset = 0; | |
569 for (int i = 0; i < count; i++) { | |
570 for (int wIndex = 0; wIndex < bpl; wIndex += 2) { | |
571 buf[bufOffset + wIndex + 1] = data[dataIndex + wIndex + 1]; | |
572 buf[bufOffset + wIndex] = data[dataIndex + wIndex]; | |
573 } | |
574 bufOffset += bmpBpl; | |
575 dataIndex -= imageBpl; | |
576 } | |
577 ostr.write(buf, 0, bufOffset); | |
578 } | |
579 } else { | |
580 for (int y = 0; y < image.height; y += linesPerBuf) { | |
581 int tmp = image.height - y; | |
582 int count = tmp < linesPerBuf ? tmp : linesPerBuf; | |
583 int bufOffset = 0; | |
584 for (int i = 0; i < count; i++) { | |
585 System.arraycopy(data, dataIndex, buf, bufOffset, bpl); | |
586 bufOffset += bmpBpl; | |
587 dataIndex -= imageBpl; | |
588 } | |
589 ostr.write(buf, 0, bufOffset); | |
590 } | |
591 } | |
592 } catch (IOException e) { | |
593 SWT.error(SWT.ERROR_IO, e); | |
594 } | |
595 return bmpBpl * image.height; | |
596 } | |
597 /** | |
598 * Unload a DeviceIndependentImage using Windows .BMP format into the given | |
599 * byte stream. | |
600 */ | |
601 override void unloadIntoByteStream(ImageLoader loader) { | |
602 ImageData image = loader.data[0]; | |
603 byte[] rgbs; | |
604 int numCols; | |
605 if (!((image.depth is 1) || (image.depth is 4) || (image.depth is 8) || | |
606 (image.depth is 16) || (image.depth is 24) || (image.depth is 32))) | |
607 SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); | |
608 int comp = this.compression; | |
609 if (!((comp is 0) || ((comp is 1) && (image.depth is 8)) || | |
610 ((comp is 2) && (image.depth is 4)))) | |
611 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
612 PaletteData pal = image.palette; | |
613 if ((image.depth is 16) || (image.depth is 24) || (image.depth is 32)) { | |
614 if (!pal.isDirect) | |
615 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
616 numCols = 0; | |
617 rgbs = null; | |
618 } else { | |
619 if (pal.isDirect) | |
620 SWT.error(SWT.ERROR_INVALID_IMAGE); | |
621 numCols = pal.colors.length; | |
622 rgbs = paletteToBytes(pal); | |
623 } | |
624 // Fill in file header, except for bfsize, which is done later. | |
625 int headersSize = BMPFileHeaderSize + BMPHeaderFixedSize; | |
626 int[] fileHeader = new int[5]; | |
627 fileHeader[0] = 0x4D42; // Signature | |
628 fileHeader[1] = 0; // File size - filled in later | |
629 fileHeader[2] = 0; // Reserved 1 | |
630 fileHeader[3] = 0; // Reserved 2 | |
631 fileHeader[4] = headersSize; // Offset to data | |
632 if (rgbs !is null) { | |
633 fileHeader[4] += rgbs.length; | |
634 } | |
635 | |
636 // Prepare data. This is done first so we don't have to try to rewind | |
637 // the stream and fill in the details later. | |
638 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); | |
639 unloadData(image, ostr, comp); | |
640 byte[] data = ostr.toByteArray(); | |
641 | |
642 // Calculate file size | |
643 fileHeader[1] = fileHeader[4] + data.length; | |
644 | |
645 // Write the headers | |
646 try { | |
647 outputStream.writeShort(fileHeader[0]); | |
648 outputStream.writeInt(fileHeader[1]); | |
649 outputStream.writeShort(fileHeader[2]); | |
650 outputStream.writeShort(fileHeader[3]); | |
651 outputStream.writeInt(fileHeader[4]); | |
652 } catch (IOException e) { | |
653 SWT.error(SWT.ERROR_IO, e); | |
654 } | |
655 try { | |
656 outputStream.writeInt(BMPHeaderFixedSize); | |
657 outputStream.writeInt(image.width); | |
658 outputStream.writeInt(image.height); | |
659 outputStream.writeShort(1); | |
660 outputStream.writeShort(cast(short)image.depth); | |
661 outputStream.writeInt(comp); | |
662 outputStream.writeInt(data.length); | |
663 outputStream.writeInt(pelsPerMeter.x); | |
664 outputStream.writeInt(pelsPerMeter.y); | |
665 outputStream.writeInt(numCols); | |
666 outputStream.writeInt(importantColors); | |
667 } catch (IOException e) { | |
668 SWT.error(SWT.ERROR_IO, e); | |
669 } | |
670 | |
671 // Unload palette | |
672 if (numCols > 0) { | |
673 try { | |
674 outputStream.write(rgbs); | |
675 } catch (IOException e) { | |
676 SWT.error(SWT.ERROR_IO, e); | |
677 } | |
678 } | |
679 | |
680 // Unload the data | |
681 try { | |
682 outputStream.write(data); | |
683 } catch (IOException e) { | |
684 SWT.error(SWT.ERROR_IO, e); | |
685 } | |
686 } | |
687 void flipScanLines(byte[] data, int stride, int height) { | |
688 int i1 = 0; | |
689 int i2 = (height - 1) * stride; | |
690 for (int i = 0; i < height / 2; i++) { | |
691 for (int index = 0; index < stride; index++) { | |
692 byte b = data[index + i1]; | |
693 data[index + i1] = data[index + i2]; | |
694 data[index + i2] = b; | |
695 } | |
696 i1 += stride; | |
697 i2 -= stride; | |
698 } | |
699 } | |
700 | |
701 } |