1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage BMP handler
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "imagbmp.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
25 #include "wx/imagbmp.h"
26 #include "wx/bitmap.h"
30 #include "wx/filefn.h"
31 #include "wx/wfstream.h"
33 #include "wx/module.h"
34 #include "wx/quantize.h"
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
59 bool wxBMPHandler::SaveFile(wxImage
*image
,
60 wxOutputStream
& stream
,
63 wxCHECK_MSG( image
, FALSE
, _T("invalid pointer in wxBMPHandler::SaveFile") );
67 if (verbose
) wxLogError(_("BMP: Couldn't save invalid image."));
71 // get the format of the BMP file to save, else use 24bpp
72 unsigned format
= wxBMP_24BPP
;
73 if (image
->HasOption(wxBMP_FORMAT
))
74 format
= image
->GetOptionInt(wxBMP_FORMAT
);
76 unsigned bpp
; // # of bits per pixel
77 int palette_size
; // # of color map entries, ie. 2^bpp colors
79 // set the bpp and appropriate palette_size, and do additional checks
80 if ((format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
))
85 else if (format
== wxBMP_4BPP
)
90 else if ((format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
91 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
))
93 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
94 if ((format
== wxBMP_8BPP_PALETTE
)
96 && !image
->HasPalette()
97 #endif // wxUSE_PALETTE
101 wxLogError(_("BMP: wImage doesn't have own wxPalette."));
107 else // you get 24bpp
109 format
= wxBMP_24BPP
;
114 unsigned width
= image
->GetWidth();
115 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
116 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
121 wxUint16 magic
; // format magic, always 'BM'
122 wxUint32 filesize
; // total file size, inc. headers
123 wxUint32 reserved
; // for future use
124 wxUint32 data_offset
; // image data offset in the file
127 wxUint32 bih_size
; // 2nd part's size
128 wxUint32 width
, height
; // bitmap's dimensions
129 wxUint16 planes
; // num of planes
130 wxUint16 bpp
; // bits per pixel
131 wxUint32 compression
; // compression method
132 wxUint32 size_of_bmp
; // size of the bitmap
133 wxUint32 h_res
, v_res
; // image resolution in dpi
134 wxUint32 num_clrs
; // number of colors used
135 wxUint32 num_signif_clrs
;// number of significant colors
138 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
140 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
141 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
142 row_width
* image
->GetHeight() );
144 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
146 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
147 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
148 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
149 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
150 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
151 hdr
.compression
= 0; // RGB uncompressed
152 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
153 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
154 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
155 hdr
.num_signif_clrs
= 0; // all colors are significant
157 if (// VS: looks ugly but compilers tend to do ugly things with structs,
158 // like aligning hdr.filesize's ofset to dword :(
159 // VZ: we should add padding then...
160 !stream
.Write(&hdr
.magic
, 2) ||
161 !stream
.Write(&hdr
.filesize
, 4) ||
162 !stream
.Write(&hdr
.reserved
, 4) ||
163 !stream
.Write(&hdr
.data_offset
, 4) ||
164 !stream
.Write(&hdr
.bih_size
, 4) ||
165 !stream
.Write(&hdr
.width
, 4) ||
166 !stream
.Write(&hdr
.height
, 4) ||
167 !stream
.Write(&hdr
.planes
, 2) ||
168 !stream
.Write(&hdr
.bpp
, 2) ||
169 !stream
.Write(&hdr
.compression
, 4) ||
170 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
171 !stream
.Write(&hdr
.h_res
, 4) ||
172 !stream
.Write(&hdr
.v_res
, 4) ||
173 !stream
.Write(&hdr
.num_clrs
, 4) ||
174 !stream
.Write(&hdr
.num_signif_clrs
, 4)
178 wxLogError(_("BMP: Couldn't write the file header."));
182 wxPalette
*palette
= NULL
; // entries for quantized images
183 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
184 wxImage
*q_image
= NULL
; // destination for quantized image
186 // if <24bpp use quantization to reduce colors for *some* of the formats
187 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
188 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
))
190 // make a new palette and quantize the image
191 if (format
!= wxBMP_8BPP_PALETTE
)
193 q_image
= new wxImage();
195 // I get a delete error using Quantize when desired colors > 236
196 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
197 // fill the destination too, it gives much nicer 4bpp images
198 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
199 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
204 palette
= new wxPalette(image
->GetPalette());
205 #endif // wxUSE_PALETTE
209 unsigned char r
, g
, b
;
210 rgbquad
= new wxUint8
[palette_size
*4];
212 for (i
=0; i
<palette_size
; i
++)
215 if (!palette
->GetRGB( i
, &r
, &g
, &b
))
216 #endif // wxUSE_PALETTE
225 // make a 256 entry greyscale colormap or 2 entry black & white
226 else if ((format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
227 (format
== wxBMP_1BPP_BW
))
230 rgbquad
= new wxUint8
[palette_size
*4];
232 for (i
=0; i
<palette_size
; i
++)
234 // if 1BPP_BW then just 0 and 255 then exit
235 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
243 // if the colormap was made, then it needs to be written
246 if (!stream
.Write(rgbquad
, palette_size
*4))
249 wxLogError(_("BMP: Couldn't write RGB color map."));
253 #endif // wxUSE_PALETTE
260 // pointer to the image data, use quantized if available
261 wxUint8
*data
= (wxUint8
*) image
->GetData();
262 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
264 wxUint8
*buffer
= new wxUint8
[row_width
];
265 memset(buffer
, 0, row_width
);
269 for (y
= image
->GetHeight() -1 ; y
>= 0; y
--)
271 if (format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
273 for (x
= 0; x
< width
; x
++)
275 pixel
= 3*(y
*width
+ x
);
277 buffer
[3*x
] = data
[pixel
+2];
278 buffer
[3*x
+ 1] = data
[pixel
+1];
279 buffer
[3*x
+ 2] = data
[pixel
];
282 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
283 (format
== wxBMP_8BPP_PALETTE
))
285 for (x
= 0; x
< width
; x
++)
287 pixel
= 3*(y
*width
+ x
);
289 buffer
[x
] = palette
->GetPixel( data
[pixel
],
293 // FIXME: what should this be? use some std palette maybe?
295 #endif // wxUSE_PALETTE
298 else if (format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
300 for (x
= 0; x
< width
; x
++)
302 pixel
= 3*(y
*width
+ x
);
303 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
308 else if (format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
310 for (x
= 0; x
< width
; x
++)
312 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
315 else if (format
== wxBMP_4BPP
) // 4 bpp in color
317 for (x
= 0; x
< width
; x
+=2)
319 pixel
= 3*(y
*width
+ x
);
321 // fill buffer, ignore if > width
324 ((wxUint8
)palette
->GetPixel(data
[pixel
],
326 data
[pixel
+2]) << 4) |
329 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
333 // FIXME: what should this be? use some std palette maybe?
335 #endif // wxUSE_PALETTE
338 else if (format
== wxBMP_1BPP
) // 1 bpp in "color"
340 for (x
= 0; x
< width
; x
+=8)
342 pixel
= 3*(y
*width
+ x
);
345 buffer
[x
/8] = ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
346 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
347 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
348 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
349 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
350 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
351 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
352 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
354 // FIXME: what should this be? use some std palette maybe?
356 #endif // wxUSE_PALETTE
359 else if (format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
361 for (x
= 0; x
< width
; x
+=8)
363 pixel
= 3*(y
*width
+ x
);
366 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
367 ( ((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
368 ( ((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
369 ( ((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
370 ( ((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
371 ( ((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
372 ( ((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
373 ( ((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
377 if (!stream
.Write(buffer
, row_width
))
380 wxLogError(_("BMP: Couldn't write data."));
384 #endif // wxUSE_PALETTE
392 #endif // wxUSE_PALETTE
407 #define BI_BITFIELDS 3
410 #define poffset (line * width * 3 + column * 3)
412 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
414 int rshift
= 0, gshift
= 0, bshift
= 0;
418 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
421 unsigned char r
, g
, b
;
424 off_t start_offset
= stream
.TellI();
425 if (start_offset
== wxInvalidOffset
) start_offset
= 0;
430 * Read the BMP header
433 stream
.Read( bbuf
, 2 );
434 stream
.Read( dbuf
, 4 * 4 );
437 wxInt32 size
= wxINT32_SWAP_ON_BE( dbuf
[0] );
439 wxInt32 offset
= wxINT32_SWAP_ON_BE( dbuf
[2] );
441 stream
.Read(dbuf
, 4 * 2);
442 int width
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
443 int height
= (int)wxINT32_SWAP_ON_BE( dbuf
[1] );
447 wxLogError( _("BMP: Image width > 32767 pixels for file.") );
453 wxLogError( _("BMP: Image height > 32767 pixels for file.") );
457 stream
.Read( &aWord
, 2 );
460 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
462 stream
.Read( &aWord
, 2 );
463 int bpp
= (int)wxUINT16_SWAP_ON_BE( aWord
);
464 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32)
467 wxLogError( _("BMP: Unknown bitdepth in file.") );
471 stream
.Read( dbuf
, 4 * 4 );
472 int comp
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
473 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
476 wxLogError( _("BMP: Unknown encoding in file.") );
480 stream
.Read( dbuf
, 4 * 2 );
481 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
484 /* some more sanity checks */
485 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
486 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
487 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
490 wxLogError( _("BMP: Encoding doesn't match bitdepth.") );
495 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
499 wxLogError( _("BMP: Couldn't allocate memory.") );
506 image
->Create( width
, height
);
507 unsigned char *ptr
= image
->GetData();
511 wxLogError( _("BMP: Couldn't allocate memory.") );
518 * Reading the palette, if it exists.
520 if (bpp
< 16 && ncolors
!= 0)
522 unsigned char* r
= new unsigned char[ncolors
];
523 unsigned char* g
= new unsigned char[ncolors
];
524 unsigned char* b
= new unsigned char[ncolors
];
525 for (int j
= 0; j
< ncolors
; j
++)
527 stream
.Read( bbuf
, 4 );
538 // Set the palette for the wxImage
539 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
540 #endif // wxUSE_PALETTE
546 else if (bpp
== 16 || bpp
== 32)
548 if (comp
== BI_BITFIELDS
)
551 stream
.Read( dbuf
, 4 * 3 );
552 bmask
= wxINT32_SWAP_ON_BE( dbuf
[0] );
553 gmask
= wxINT32_SWAP_ON_BE( dbuf
[1] );
554 rmask
= wxINT32_SWAP_ON_BE( dbuf
[2] );
555 /* find shift amount.. ugly, but i can't think of a better way */
556 for (bit
= 0; bit
< bpp
; bit
++)
558 if (bmask
& (1 << bit
))
560 if (gmask
& (1 << bit
))
562 if (rmask
& (1 << bit
))
587 * Reading the image data
589 stream
.SeekI( start_offset
+ offset
);
590 unsigned char *data
= ptr
;
592 /* set the whole image to the background color */
593 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
595 for (int i
= 0; i
< width
* height
; i
++)
606 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
608 /* BMPs are stored upside down */
609 for (line
= (height
- 1); line
>= 0; line
--)
612 for (column
= 0; column
< width
;)
618 aByte
= stream
.GetC();
622 for (bit
= 0; bit
< 8 && column
< width
; bit
++)
624 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
625 ptr
[poffset
] = cmap
[index
].r
;
626 ptr
[poffset
+ 1] = cmap
[index
].g
;
627 ptr
[poffset
+ 2] = cmap
[index
].b
;
636 wxLogError( _("BMP: Cannot deal with 4bit encoded yet.") );
644 for (nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
646 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
649 ptr
[poffset
] = cmap
[index
].r
;
650 ptr
[poffset
+ 1] = cmap
[index
].g
;
651 ptr
[poffset
+ 2] = cmap
[index
].b
;
662 aByte
= stream
.GetC();
667 /* column = width; */
676 aByte
= stream
.GetC();
678 linepos
= column
* bpp
/ 8;
679 aByte
= stream
.GetC();
684 int absolute
= aByte
;
685 for (int k
= 0; k
< absolute
; k
++)
688 aByte
= stream
.GetC();
689 ptr
[poffset
] = cmap
[aByte
].r
;
690 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
691 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
695 aByte
= stream
.GetC();
700 for (int l
= 0; l
< first
&& column
< width
; l
++)
702 ptr
[poffset
] = cmap
[aByte
].r
;
703 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
704 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
712 ptr
[poffset
] = cmap
[aByte
].r
;
713 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
714 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
716 // linepos += size; seems to be wrong, RR
722 stream
.Read( bbuf
, 3 );
724 ptr
[poffset
] = (unsigned char)bbuf
[2];
725 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
726 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
732 stream
.Read( &aWord
, 2 );
733 aWord
= wxUINT16_SWAP_ON_BE( aWord
);
735 temp
= (aWord
& rmask
) >> rshift
;
737 temp
= (aWord
& gmask
) >> gshift
;
738 ptr
[poffset
+ 1] = temp
;
739 temp
= (aWord
& bmask
) >> bshift
;
740 ptr
[poffset
+ 2] = temp
;
746 stream
.Read( &aDword
, 4 );
747 aDword
= wxINT32_SWAP_ON_BE( aDword
);
749 temp
= (aDword
& rmask
) >> rshift
;
751 temp
= (aDword
& gmask
) >> gshift
;
752 ptr
[poffset
+ 1] = temp
;
753 temp
= (aDword
& bmask
) >> bshift
;
754 ptr
[poffset
+ 2] = temp
;
758 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
760 stream
.Read( &aByte
, 1 );
762 if (stream
.LastError() != wxStream_NOERROR
)
769 image
->SetMask( FALSE
);
774 bool wxBMPHandler::DoCanRead( wxInputStream
& stream
)
776 unsigned char hdr
[2];
779 stream
.SeekI(-2, wxFromCurrent
);
780 return (hdr
[0] == 'B' && hdr
[1] == 'M');
783 #endif // wxUSE_STREAMS
785 #endif // wxUSE_IMAGE