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
) && !image
->HasPalette())
97 wxLogError(_("BMP: wImage doesn't have own wxPalette."));
103 else // you get 24bpp
105 format
= wxBMP_24BPP
;
110 unsigned width
= image
->GetWidth();
111 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
112 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
117 wxUint16 magic
; // format magic, always 'BM'
118 wxUint32 filesize
; // total file size, inc. headers
119 wxUint32 reserved
; // for future use
120 wxUint32 data_offset
; // image data offset in the file
123 wxUint32 bih_size
; // 2nd part's size
124 wxUint32 width
, height
; // bitmap's dimensions
125 wxUint16 planes
; // num of planes
126 wxUint16 bpp
; // bits per pixel
127 wxUint32 compression
; // compression method
128 wxUint32 size_of_bmp
; // size of the bitmap
129 wxUint32 h_res
, v_res
; // image resolution in dpi
130 wxUint32 num_clrs
; // number of colors used
131 wxUint32 num_signif_clrs
;// number of significant colors
134 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
136 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
137 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
138 row_width
* image
->GetHeight() );
140 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
142 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
143 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
144 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
145 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
146 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
147 hdr
.compression
= 0; // RGB uncompressed
148 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
149 hdr
.h_res
= hdr
.v_res
= wxUINT32_SWAP_ON_BE(72); // 72dpi is standard
150 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
151 hdr
.num_signif_clrs
= 0; // all colors are significant
153 if (// VS: looks ugly but compilers tend to do ugly things with structs,
154 // like aligning hdr.filesize's ofset to dword :(
155 // VZ: we should add padding then...
156 !stream
.Write(&hdr
.magic
, 2) ||
157 !stream
.Write(&hdr
.filesize
, 4) ||
158 !stream
.Write(&hdr
.reserved
, 4) ||
159 !stream
.Write(&hdr
.data_offset
, 4) ||
160 !stream
.Write(&hdr
.bih_size
, 4) ||
161 !stream
.Write(&hdr
.width
, 4) ||
162 !stream
.Write(&hdr
.height
, 4) ||
163 !stream
.Write(&hdr
.planes
, 2) ||
164 !stream
.Write(&hdr
.bpp
, 2) ||
165 !stream
.Write(&hdr
.compression
, 4) ||
166 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
167 !stream
.Write(&hdr
.h_res
, 4) ||
168 !stream
.Write(&hdr
.v_res
, 4) ||
169 !stream
.Write(&hdr
.num_clrs
, 4) ||
170 !stream
.Write(&hdr
.num_signif_clrs
, 4)
174 wxLogError(_("BMP: Couldn't write the file header."));
178 wxPalette
*palette
= NULL
; // entries for quantized images
179 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
180 wxImage
*q_image
= NULL
; // destination for quantized image
182 // if <24bpp use quantization to reduce colors for *some* of the formats
183 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
184 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
))
186 // make a new palette and quantize the image
187 if (format
!= wxBMP_8BPP_PALETTE
)
189 q_image
= new wxImage();
191 // I get a delete error using Quantize when desired colors > 236
192 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
193 // fill the destination too, it gives much nicer 4bpp images
194 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
195 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
199 palette
= new wxPalette(image
->GetPalette());
203 unsigned char r
, g
, b
;
204 rgbquad
= new wxUint8
[palette_size
*4];
206 for (i
=0; i
<palette_size
; i
++)
208 if (!palette
->GetRGB( i
, &r
, &g
, &b
)) r
= g
= b
= 0;
216 // make a 256 entry greyscale colormap or 2 entry black & white
217 else if ((format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
218 (format
== wxBMP_1BPP_BW
))
221 rgbquad
= new wxUint8
[palette_size
*4];
223 for (i
=0; i
<palette_size
; i
++)
225 // if 1BPP_BW then just 0 and 255 then exit
226 if (( i
> 0) && (format
== wxBMP_1BPP_BW
)) i
= 255;
234 // if the colormap was made, then it needs to be written
237 if (!stream
.Write(rgbquad
, palette_size
*4))
239 if (verbose
) wxLogError(_("BMP: Couldn't write RGB color map."));
241 if (palette
) delete palette
;
242 if (q_image
) delete q_image
;
248 // pointer to the image data, use quantized if available
249 wxUint8
*data
= (wxUint8
*) image
->GetData();
250 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
252 wxUint8
*buffer
= new wxUint8
[row_width
];
253 memset(buffer
, 0, row_width
);
257 for (y
= image
->GetHeight() -1 ; y
>= 0; y
--)
259 if (format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
261 for (x
= 0; x
< width
; x
++)
263 pixel
= 3*(y
*width
+ x
);
265 buffer
[3*x
] = data
[pixel
+2];
266 buffer
[3*x
+ 1] = data
[pixel
+1];
267 buffer
[3*x
+ 2] = data
[pixel
];
270 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
271 (format
== wxBMP_8BPP_PALETTE
))
273 for (x
= 0; x
< width
; x
++)
275 pixel
= 3*(y
*width
+ x
);
276 buffer
[x
] = palette
->GetPixel( data
[pixel
],
281 else if (format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
283 for (x
= 0; x
< width
; x
++)
285 pixel
= 3*(y
*width
+ x
);
286 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
291 else if (format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
293 for (x
= 0; x
< width
; x
++)
295 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
298 else if (format
== wxBMP_4BPP
) // 4 bpp in color
300 for (x
= 0; x
< width
; x
+=2)
302 pixel
= 3*(y
*width
+ x
);
304 // fill buffer, ignore if > width
306 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 4) |
307 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) ));
310 else if (format
== wxBMP_1BPP
) // 1 bpp in "color"
312 for (x
= 0; x
< width
; x
+=8)
314 pixel
= 3*(y
*width
+ x
);
317 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
318 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
319 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
320 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
321 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
322 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
323 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
324 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) ));
327 else if (format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
329 for (x
= 0; x
< width
; x
+=8)
331 pixel
= 3*(y
*width
+ x
);
334 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
335 ( ((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
336 ( ((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
337 ( ((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
338 ( ((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
339 ( ((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
340 ( ((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
341 ( ((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) ));
345 if (!stream
.Write(buffer
, row_width
))
348 wxLogError(_("BMP: Couldn't write data."));
350 if (palette
) delete palette
;
351 if (q_image
) delete q_image
;
356 if (palette
) delete palette
;
357 if (q_image
) delete q_image
;
371 #define BI_BITFIELDS 3
374 #define poffset (line * width * 3 + column * 3)
376 bool wxBMPHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
, int WXUNUSED(index
) )
378 int rshift
= 0, gshift
= 0, bshift
= 0;
382 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
385 unsigned char r
, g
, b
;
388 off_t start_offset
= stream
.TellI();
389 if (start_offset
== wxInvalidOffset
) start_offset
= 0;
394 * Read the BMP header
397 stream
.Read( bbuf
, 2 );
398 stream
.Read( dbuf
, 4 * 4 );
401 wxInt32 size
= wxINT32_SWAP_ON_BE( dbuf
[0] );
403 wxInt32 offset
= wxINT32_SWAP_ON_BE( dbuf
[2] );
405 stream
.Read(dbuf
, 4 * 2);
406 int width
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
407 int height
= (int)wxINT32_SWAP_ON_BE( dbuf
[1] );
411 wxLogError( _("BMP: Image width > 32767 pixels for file.") );
417 wxLogError( _("BMP: Image height > 32767 pixels for file.") );
421 stream
.Read( &aWord
, 2 );
424 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
426 stream
.Read( &aWord
, 2 );
427 int bpp
= (int)wxUINT16_SWAP_ON_BE( aWord
);
428 if (bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32)
431 wxLogError( _("BMP: Unknown bitdepth in file.") );
435 stream
.Read( dbuf
, 4 * 4 );
436 int comp
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
437 if (comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&& comp
!= BI_BITFIELDS
)
440 wxLogError( _("BMP: Unknown encoding in file.") );
444 stream
.Read( dbuf
, 4 * 2 );
445 int ncolors
= (int)wxINT32_SWAP_ON_BE( dbuf
[0] );
448 /* some more sanity checks */
449 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
450 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
451 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
454 wxLogError( _("BMP: Encoding doesn't match bitdepth.") );
459 cmap
= (struct _cmap
*)malloc(sizeof(struct _cmap
) * ncolors
);
463 wxLogError( _("BMP: Couldn't allocate memory.") );
470 image
->Create( width
, height
);
471 unsigned char *ptr
= image
->GetData();
475 wxLogError( _("BMP: Couldn't allocate memory.") );
482 * Reading the palette, if it exists.
484 if (bpp
< 16 && ncolors
!= 0)
486 unsigned char* r
= new unsigned char[ncolors
];
487 unsigned char* g
= new unsigned char[ncolors
];
488 unsigned char* b
= new unsigned char[ncolors
];
489 for (int j
= 0; j
< ncolors
; j
++)
491 stream
.Read( bbuf
, 4 );
500 // Set the palette for the wxImage
501 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
507 else if (bpp
== 16 || bpp
== 32)
509 if (comp
== BI_BITFIELDS
)
512 stream
.Read( dbuf
, 4 * 3 );
513 bmask
= wxINT32_SWAP_ON_BE( dbuf
[0] );
514 gmask
= wxINT32_SWAP_ON_BE( dbuf
[1] );
515 rmask
= wxINT32_SWAP_ON_BE( dbuf
[2] );
516 /* find shift amount.. ugly, but i can't think of a better way */
517 for (bit
= 0; bit
< bpp
; bit
++)
519 if (bmask
& (1 << bit
))
521 if (gmask
& (1 << bit
))
523 if (rmask
& (1 << bit
))
548 * Reading the image data
550 stream
.SeekI( start_offset
+ offset
);
551 unsigned char *data
= ptr
;
553 /* set the whole image to the background color */
554 if (bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
))
556 for (int i
= 0; i
< width
* height
; i
++)
567 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
569 /* BMPs are stored upside down */
570 for (line
= (height
- 1); line
>= 0; line
--)
573 for (column
= 0; column
< width
;)
579 aByte
= stream
.GetC();
583 for (bit
= 0; bit
< 8 && column
< width
; bit
++)
585 index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
586 ptr
[poffset
] = cmap
[index
].r
;
587 ptr
[poffset
+ 1] = cmap
[index
].g
;
588 ptr
[poffset
+ 2] = cmap
[index
].b
;
597 wxLogError( _("BMP: Cannot deal with 4bit encoded yet.") );
605 for (nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
607 index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
610 ptr
[poffset
] = cmap
[index
].r
;
611 ptr
[poffset
+ 1] = cmap
[index
].g
;
612 ptr
[poffset
+ 2] = cmap
[index
].b
;
623 aByte
= stream
.GetC();
628 /* column = width; */
637 aByte
= stream
.GetC();
639 linepos
= column
* bpp
/ 8;
640 aByte
= stream
.GetC();
645 int absolute
= aByte
;
646 for (int k
= 0; k
< absolute
; k
++)
649 aByte
= stream
.GetC();
650 ptr
[poffset
] = cmap
[aByte
].r
;
651 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
652 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
656 aByte
= stream
.GetC();
661 for (int l
= 0; l
< first
&& column
< width
; l
++)
663 ptr
[poffset
] = cmap
[aByte
].r
;
664 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
665 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
673 ptr
[poffset
] = cmap
[aByte
].r
;
674 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
675 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
677 // linepos += size; seems to be wrong, RR
683 stream
.Read( bbuf
, 3 );
685 ptr
[poffset
] = (unsigned char)bbuf
[2];
686 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
687 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
693 stream
.Read( &aWord
, 2 );
694 aWord
= wxUINT16_SWAP_ON_BE( aWord
);
696 temp
= (aWord
& rmask
) >> rshift
;
698 temp
= (aWord
& gmask
) >> gshift
;
699 ptr
[poffset
+ 1] = temp
;
700 temp
= (aWord
& bmask
) >> bshift
;
701 ptr
[poffset
+ 2] = temp
;
707 stream
.Read( &aDword
, 4 );
708 aDword
= wxINT32_SWAP_ON_BE( aDword
);
710 temp
= (aDword
& rmask
) >> rshift
;
712 temp
= (aDword
& gmask
) >> gshift
;
713 ptr
[poffset
+ 1] = temp
;
714 temp
= (aDword
& bmask
) >> bshift
;
715 ptr
[poffset
+ 2] = temp
;
719 while ((linepos
< linesize
) && (comp
!= 1) && (comp
!= 2))
721 stream
.Read( &aByte
, 1 );
723 if (stream
.LastError() != wxStream_NOERROR
)
730 image
->SetMask( FALSE
);
735 bool wxBMPHandler::DoCanRead( wxInputStream
& stream
)
737 unsigned char hdr
[2];
740 stream
.SeekI(-2, wxFromCurrent
);
741 return (hdr
[0] == 'B' && hdr
[1] == 'M');
744 #endif // wxUSE_STREAMS
746 #endif // wxUSE_IMAGE