21
21
public final class WinBMPFileFormat extends FileFormat {
22
22
static final int BMPFileHeaderSize = 14 ;
23
23
static final int BMPHeaderFixedSize = 40 ;
24
+
25
+ static final int BI_RGB = 0 ;
26
+ static final int BI_RLE8 = 1 ;
27
+ static final int BI_RLE4 = 2 ;
28
+ static final int BI_BITFIELDS = 3 ;
29
+
24
30
int importantColors ;
25
31
Point pelsPerMeter = new Point (0 , 0 );
26
32
@@ -31,10 +37,10 @@ public final class WinBMPFileFormat extends FileFormat {
31
37
* Answer the size of the compressed data.
32
38
*/
33
39
int compress (int comp , byte [] src , int srcOffset , int numBytes , byte [] dest , boolean last ) {
34
- if (comp == 1 ) { // BMP_RLE8_COMPRESSION
40
+ if (comp == BI_RLE8 ) {
35
41
return compressRLE8Data (src , srcOffset , numBytes , dest , last );
36
42
}
37
- if (comp == 2 ) { // BMP_RLE4_COMPRESSION
43
+ if (comp == BI_RLE4 ) {
38
44
return compressRLE4Data (src , srcOffset , numBytes , dest , last );
39
45
}
40
46
SWT .error (SWT .ERROR_INVALID_IMAGE );
@@ -271,12 +277,12 @@ void convertPixelsToBGR(ImageData image, byte[] dest) {
271
277
}
272
278
}
273
279
void decompressData (byte [] src , byte [] dest , int stride , int cmp ) {
274
- if (cmp == 1 ) { // BMP_RLE8_COMPRESSION
280
+ if (cmp == BI_RLE8 ) {
275
281
if (decompressRLE8Data (src , src .length , stride , dest , dest .length ) <= 0 )
276
282
SWT .error (SWT .ERROR_INVALID_IMAGE );
277
283
return ;
278
284
}
279
- if (cmp == 2 ) { // BMP_RLE4_COMPRESSION
285
+ if (cmp == BI_RLE4 ) {
280
286
if (decompressRLE4Data (src , src .length , stride , dest , dest .length ) <= 0 )
281
287
SWT .error (SWT .ERROR_INVALID_IMAGE );
282
288
return ;
@@ -452,7 +458,7 @@ byte[] loadData(byte[] infoHeader, int stride) {
452
458
int dataSize = height * stride ;
453
459
byte [] data = new byte [dataSize ];
454
460
int cmp = (infoHeader [16 ] & 0xFF ) | ((infoHeader [17 ] & 0xFF ) << 8 ) | ((infoHeader [18 ] & 0xFF ) << 16 ) | ((infoHeader [19 ] & 0xFF ) << 24 );
455
- if (cmp == 0 || cmp == 3 ) { // BMP_NO_COMPRESSION
461
+ if (cmp == BI_RGB || cmp == BI_BITFIELDS ) {
456
462
try {
457
463
if (inputStream .read (data ) != dataSize )
458
464
SWT .error (SWT .ERROR_INVALID_IMAGE );
@@ -556,7 +562,7 @@ PaletteData loadPalette(byte[] infoHeader) {
556
562
return paletteFromBytes (buf , numColors );
557
563
}
558
564
if (depth == 16 ) {
559
- if (this .compression == 3 ) {
565
+ if (this .compression == BI_BITFIELDS ) {
560
566
try {
561
567
return new PaletteData (inputStream .readInt (), inputStream .readInt (), inputStream .readInt ());
562
568
} catch (IOException e ) {
@@ -566,9 +572,18 @@ PaletteData loadPalette(byte[] infoHeader) {
566
572
return new PaletteData (0x7C00 , 0x3E0 , 0x1F );
567
573
}
568
574
if (depth == 24 ) return new PaletteData (0xFF , 0xFF00 , 0xFF0000 );
569
- if (this .compression == 3 ) {
575
+ if (this .compression == BI_BITFIELDS ) {
570
576
try {
571
- return new PaletteData (inputStream .readInt (), inputStream .readInt (), inputStream .readInt ());
577
+ /*
578
+ * ImageData is expected to be in big-endian format when
579
+ * (bpp != 16); see 'ImageData.getByteOrder()'. At the same
580
+ * time, 'inputStream' is a 'LEDataInputStream', that is,
581
+ * low-endian. Therefore, masks need to be converted.
582
+ */
583
+ final int maskR = Integer .reverseBytes (inputStream .readInt ());
584
+ final int maskG = Integer .reverseBytes (inputStream .readInt ());
585
+ final int maskB = Integer .reverseBytes (inputStream .readInt ());
586
+ return new PaletteData (maskR , maskG , maskB );
572
587
} catch (IOException e ) {
573
588
SWT .error (SWT .ERROR_IO , e );
574
589
}
0 commit comments