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