1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/imagbmp.cpp
3 // Purpose: wxImage BMP,ICO and CUR handlers
4 // Author: Robert Roebling, Chris Elliott
6 // Copyright: (c) Robert Roebling, Chris Elliott
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/imagbmp.h"
23 #include "wx/msw/wrapwin.h"
27 #include "wx/bitmap.h"
28 #include "wx/palette.h"
33 #include "wx/filefn.h"
34 #include "wx/wfstream.h"
35 #include "wx/quantize.h"
36 #include "wx/scopeguard.h"
37 #include "wx/anidecod.h"
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
63 #define BI_BITFIELDS 3
66 #define poffset (line * width * 3 + column * 3)
68 bool wxBMPHandler::SaveFile(wxImage
*image
,
69 wxOutputStream
& stream
,
72 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
75 bool wxBMPHandler::SaveDib(wxImage
*image
,
76 wxOutputStream
& stream
,
82 wxCHECK_MSG( image
, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
87 wxLogError(_("BMP: Couldn't save invalid image."));
91 // get the format of the BMP file to save, else use 24bpp
92 unsigned format
= wxBMP_24BPP
;
93 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
94 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
96 wxUint16 bpp
; // # of bits per pixel
97 int palette_size
; // # of color map entries, ie. 2^bpp colors
99 // set the bpp and appropriate palette_size, and do additional checks
100 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
105 else if ( format
== wxBMP_4BPP
)
110 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
111 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
113 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
114 if ((format
== wxBMP_8BPP_PALETTE
)
116 && !image
->HasPalette()
117 #endif // wxUSE_PALETTE
121 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
127 else // you get 24bpp
129 format
= wxBMP_24BPP
;
134 unsigned width
= image
->GetWidth();
135 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
136 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
141 wxUint16 magic
; // format magic, always 'BM'
142 wxUint32 filesize
; // total file size, inc. headers
143 wxUint32 reserved
; // for future use
144 wxUint32 data_offset
; // image data offset in the file
147 wxUint32 bih_size
; // 2nd part's size
148 wxUint32 width
, height
; // bitmap's dimensions
149 wxUint16 planes
; // num of planes
150 wxUint16 bpp
; // bits per pixel
151 wxUint32 compression
; // compression method
152 wxUint32 size_of_bmp
; // size of the bitmap
153 wxUint32 h_res
, v_res
; // image resolution in pixels-per-meter
154 wxUint32 num_clrs
; // number of colors used
155 wxUint32 num_signif_clrs
;// number of significant colors
158 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
160 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
161 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
162 row_width
* image
->GetHeight() );
164 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
166 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
167 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
170 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
174 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
176 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
177 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
178 hdr
.compression
= 0; // RGB uncompressed
179 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
181 // get the resolution from the image options or fall back to 72dpi standard
182 // for the BMP format if not specified
183 wxUint32 hres
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
184 vres
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
185 switch ( image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
) )
188 wxFAIL_MSG( _T("unexpected image resolution units") );
191 case wxIMAGE_RESOLUTION_NONE
:
194 // fall through to convert it to correct units
196 case wxIMAGE_RESOLUTION_INCHES
:
197 // convert resolution in inches to resolution in centimeters
198 hres
= (wxUint32
)(10*mm2inches
*hres
);
199 vres
= (wxUint32
)(10*mm2inches
*vres
);
200 // fall through to convert it to resolution in meters
202 case wxIMAGE_RESOLUTION_CM
:
203 // convert resolution in centimeters to resolution in meters
209 hdr
.h_res
= wxUINT32_SWAP_ON_BE(hres
);
210 hdr
.v_res
= wxUINT32_SWAP_ON_BE(vres
);
211 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
212 hdr
.num_signif_clrs
= 0; // all colors are significant
216 if (// VS: looks ugly but compilers tend to do ugly things with structs,
217 // like aligning hdr.filesize's ofset to dword :(
218 // VZ: we should add padding then...
219 !stream
.Write(&hdr
.magic
, 2) ||
220 !stream
.Write(&hdr
.filesize
, 4) ||
221 !stream
.Write(&hdr
.reserved
, 4) ||
222 !stream
.Write(&hdr
.data_offset
, 4)
226 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
233 !stream
.Write(&hdr
.bih_size
, 4) ||
234 !stream
.Write(&hdr
.width
, 4) ||
235 !stream
.Write(&hdr
.height
, 4) ||
236 !stream
.Write(&hdr
.planes
, 2) ||
237 !stream
.Write(&hdr
.bpp
, 2) ||
238 !stream
.Write(&hdr
.compression
, 4) ||
239 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
240 !stream
.Write(&hdr
.h_res
, 4) ||
241 !stream
.Write(&hdr
.v_res
, 4) ||
242 !stream
.Write(&hdr
.num_clrs
, 4) ||
243 !stream
.Write(&hdr
.num_signif_clrs
, 4)
247 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
252 wxPalette
*palette
= NULL
; // entries for quantized images
253 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
254 wxImage
*q_image
= NULL
; // destination for quantized image
256 // if <24bpp use quantization to reduce colors for *some* of the formats
257 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
258 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
260 // make a new palette and quantize the image
261 if (format
!= wxBMP_8BPP_PALETTE
)
263 q_image
= new wxImage();
265 // I get a delete error using Quantize when desired colors > 236
266 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
267 // fill the destination too, it gives much nicer 4bpp images
268 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
269 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
274 palette
= new wxPalette(image
->GetPalette());
275 #endif // wxUSE_PALETTE
279 unsigned char r
, g
, b
;
280 rgbquad
= new wxUint8
[palette_size
*4];
282 for (i
= 0; i
< palette_size
; i
++)
285 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
286 #endif // wxUSE_PALETTE
295 // make a 256 entry greyscale colormap or 2 entry black & white
296 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
297 (format
== wxBMP_1BPP_BW
) )
299 rgbquad
= new wxUint8
[palette_size
*4];
301 for ( int i
= 0; i
< palette_size
; i
++ )
303 // if 1BPP_BW then the value should be either 0 or 255
304 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
313 // if the colormap was made, then it needs to be written
318 if ( !stream
.Write(rgbquad
, palette_size
*4) )
321 wxLogError(_("BMP: Couldn't write RGB color map."));
325 #endif // wxUSE_PALETTE
333 // pointer to the image data, use quantized if available
334 wxUint8
*data
= (wxUint8
*) image
->GetData();
335 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
337 wxUint8
*buffer
= new wxUint8
[row_width
];
338 memset(buffer
, 0, row_width
);
342 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
344 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
346 for ( x
= 0; x
< width
; x
++ )
348 pixel
= 3*(y
*width
+ x
);
350 buffer
[3*x
] = data
[pixel
+2];
351 buffer
[3*x
+ 1] = data
[pixel
+1];
352 buffer
[3*x
+ 2] = data
[pixel
];
355 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
356 (format
== wxBMP_8BPP_PALETTE
))
358 for (x
= 0; x
< width
; x
++)
360 pixel
= 3*(y
*width
+ x
);
362 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
366 // FIXME: what should this be? use some std palette maybe?
368 #endif // wxUSE_PALETTE
371 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
373 for (x
= 0; x
< width
; x
++)
375 pixel
= 3*(y
*width
+ x
);
376 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
381 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
383 for (x
= 0; x
< width
; x
++)
385 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
388 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
390 for (x
= 0; x
< width
; x
+=2)
392 pixel
= 3*(y
*width
+ x
);
394 // fill buffer, ignore if > width
396 buffer
[x
/2] = (wxUint8
)(
397 ((wxUint8
)palette
->GetPixel(data
[pixel
],
399 data
[pixel
+2]) << 4) |
402 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
406 // FIXME: what should this be? use some std palette maybe?
408 #endif // wxUSE_PALETTE
411 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
413 for (x
= 0; x
< width
; x
+=8)
415 pixel
= 3*(y
*width
+ x
);
418 buffer
[x
/8] = (wxUint8
)(
419 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
420 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
421 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
422 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
423 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
424 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
425 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
426 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
428 // FIXME: what should this be? use some std palette maybe?
430 #endif // wxUSE_PALETTE
433 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
435 for (x
= 0; x
< width
; x
+=8)
437 pixel
= 3*(y
*width
+ x
);
439 buffer
[x
/8] = (wxUint8
)(
440 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
441 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
442 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
443 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
444 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
445 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
446 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
447 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
451 if ( !stream
.Write(buffer
, row_width
) )
454 wxLogError(_("BMP: Couldn't write data."));
458 #endif // wxUSE_PALETTE
466 #endif // wxUSE_PALETTE
475 static void Free(BMPPalette
* pal
) { delete [] pal
; }
477 unsigned char r
, g
, b
;
480 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
481 int bpp
, int ncolors
, int comp
,
482 wxFileOffset bmpOffset
, wxInputStream
& stream
,
483 bool verbose
, bool IsBmp
, bool hasPalette
)
485 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0, amask
= 0;
486 int rshift
= 0, gshift
= 0, bshift
= 0, ashift
= 0;
487 int rbits
= 0, gbits
= 0, bbits
= 0;
493 // allocate space for palette if needed:
498 cmap
= new BMPPalette
[ncolors
];
502 wxLogError(_("BMP: Couldn't allocate memory."));
511 wxON_BLOCK_EXIT1(&BMPPalette::Free
, cmap
);
513 // destroy existing here instead of:
515 image
->Create(width
, height
);
517 unsigned char *ptr
= image
->GetData();
522 wxLogError( _("BMP: Couldn't allocate memory.") );
526 unsigned char *alpha
;
529 // tell the image to allocate an alpha buffer
531 alpha
= image
->GetAlpha();
535 wxLogError(_("BMP: Couldn't allocate memory."));
544 // Reading the palette, if it exists:
545 if ( bpp
< 16 && ncolors
!= 0 )
547 unsigned char* r
= new unsigned char[ncolors
];
548 unsigned char* g
= new unsigned char[ncolors
];
549 unsigned char* b
= new unsigned char[ncolors
];
550 for (int j
= 0; j
< ncolors
; j
++)
554 stream
.Read(bbuf
, 4);
565 //used in reading .ico file mask
568 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
573 // Set the palette for the wxImage
574 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
575 #endif // wxUSE_PALETTE
581 else if ( bpp
== 16 || bpp
== 32 )
583 if ( comp
== BI_BITFIELDS
)
586 stream
.Read(dbuf
, 4 * 3);
587 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
588 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
589 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
590 // find shift amount (Least significant bit of mask)
591 for (bit
= bpp
-1; bit
>=0; bit
--)
593 if (bmask
& (1 << bit
))
595 if (gmask
& (1 << bit
))
597 if (rmask
& (1 << bit
))
600 // Find number of bits in mask (MSB-LSB+1)
601 for (bit
= 0; bit
< bpp
; bit
++)
603 if (bmask
& (1 << bit
))
604 bbits
= bit
-bshift
+1;
605 if (gmask
& (1 << bit
))
606 gbits
= bit
-gshift
+1;
607 if (rmask
& (1 << bit
))
608 rbits
= bit
-rshift
+1;
611 else if ( bpp
== 16 )
623 else if ( bpp
== 32 )
641 * Reading the image data
644 stream
.SeekI(bmpOffset
); // else icon, just carry on
646 unsigned char *data
= ptr
;
648 /* set the whole image to the background color */
649 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
651 for (int i
= 0; i
< width
* height
; i
++)
660 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
662 /* BMPs are stored upside down */
663 for ( int line
= (height
- 1); line
>= 0; line
-- )
666 for ( int column
= 0; column
< width
; )
671 aByte
= stream
.GetC();
674 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
676 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
677 ptr
[poffset
] = cmap
[index
].r
;
678 ptr
[poffset
+ 1] = cmap
[index
].g
;
679 ptr
[poffset
+ 2] = cmap
[index
].b
;
685 if ( comp
== BI_RLE4
)
689 aByte
= stream
.GetC();
697 else if ( aByte
== 1 )
702 else if ( aByte
== 2 )
704 aByte
= stream
.GetC();
706 linepos
= column
* bpp
/ 4;
707 aByte
= stream
.GetC();
708 line
-= aByte
; // upside down
712 int absolute
= aByte
;
715 for (int k
= 0; k
< absolute
; k
++)
720 aByte
= stream
.GetC();
721 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
722 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
724 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
725 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
726 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
731 if ( readBytes
& 0x01 )
732 aByte
= stream
.GetC();
738 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
739 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
741 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
743 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
744 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
745 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
754 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
756 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
759 ptr
[poffset
] = cmap
[index
].r
;
760 ptr
[poffset
+ 1] = cmap
[index
].g
;
761 ptr
[poffset
+ 2] = cmap
[index
].b
;
768 if ( comp
== BI_RLE8
)
772 aByte
= stream
.GetC();
777 /* column = width; */
779 else if ( aByte
== 1 )
784 else if ( aByte
== 2 )
786 aByte
= stream
.GetC();
788 linepos
= column
* bpp
/ 8;
789 aByte
= stream
.GetC();
794 int absolute
= aByte
;
795 for (int k
= 0; k
< absolute
; k
++)
798 aByte
= stream
.GetC();
799 ptr
[poffset
] = cmap
[aByte
].r
;
800 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
801 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
804 if ( absolute
& 0x01 )
805 aByte
= stream
.GetC();
810 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
812 ptr
[poffset
] = cmap
[aByte
].r
;
813 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
814 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
822 ptr
[poffset
] = cmap
[aByte
].r
;
823 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
824 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
826 // linepos += size; seems to be wrong, RR
830 else if ( bpp
== 24 )
832 stream
.Read(bbuf
, 3);
834 ptr
[poffset
] = (unsigned char)bbuf
[2];
835 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
836 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
839 else if ( bpp
== 16 )
842 stream
.Read(&aWord
, 2);
843 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
845 /* use the masks and calculated amonut of shift
846 to retrieve the color data out of the word. Then
847 shift it left by (8 - number of bits) such that
848 the image has the proper dynamic range */
849 temp
= (unsigned char)((aWord
& rmask
) >> rshift
<< (8-rbits
));
851 temp
= (unsigned char)((aWord
& gmask
) >> gshift
<< (8-gbits
));
852 ptr
[poffset
+ 1] = temp
;
853 temp
= (unsigned char)((aWord
& bmask
) >> bshift
<< (8-bbits
));
854 ptr
[poffset
+ 2] = temp
;
860 stream
.Read(&aDword
, 4);
861 aDword
= wxINT32_SWAP_ON_BE(aDword
);
863 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
865 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
866 ptr
[poffset
+ 1] = temp
;
867 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
868 ptr
[poffset
+ 2] = temp
;
871 temp
= (unsigned char)((aDword
& amask
) >> ashift
);
872 alpha
[line
* width
+ column
] = temp
;
877 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
879 stream
.Read(&aByte
, 1);
886 image
->SetMask(false);
888 const wxStreamError err
= stream
.GetLastError();
889 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
892 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
893 bool verbose
, bool IsBmp
)
899 wxFileOffset offset
= 0; // keep gcc quiet
902 // read the header off the .BMP format file
904 offset
= stream
.TellI();
905 if (offset
== wxInvalidOffset
)
908 stream
.Read(bbuf
, 2);
909 stream
.Read(dbuf
, 16);
913 stream
.Read(dbuf
, 4);
916 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
918 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
920 stream
.Read(dbuf
, 4 * 2);
921 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
922 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
923 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
928 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
931 if ( height
> 32767 )
934 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
938 stream
.Read(&aWord
, 2);
941 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
943 stream
.Read(&aWord
, 2);
944 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
945 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
948 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
952 stream
.Read(dbuf
, 4 * 4);
953 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
954 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
955 comp
!= BI_BITFIELDS
)
958 wxLogError( _("DIB Header: Unknown encoding in file.") );
962 stream
.Read(dbuf
, 4 * 2);
964 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
967 /* some more sanity checks */
968 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
969 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
970 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
973 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
977 //read DIB; this is the BMP image or the XOR part of an icon image
978 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
979 verbose
, IsBmp
, true) )
982 wxLogError( _("Error in reading image DIB.") );
988 //read Icon mask which is monochrome
989 //there is no palette, so we will create one
991 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
992 verbose
, IsBmp
, false) )
995 wxLogError( _("ICO: Error in reading mask DIB.") );
998 image
->SetMaskFromImage(mask
, 255, 255, 255);
1002 // the resolution in the bitmap header is in meters, convert to centimeters
1003 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT
, wxIMAGE_RESOLUTION_CM
);
1004 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, dbuf
[2]/100);
1005 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, dbuf
[3]/100);
1010 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1011 bool verbose
, int WXUNUSED(index
))
1013 // Read a single DIB fom the file:
1014 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
1017 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
1019 unsigned char hdr
[2];
1021 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1024 // do we have the BMP file signature?
1025 return hdr
[0] == 'B' && hdr
[1] == 'M';
1028 #endif // wxUSE_STREAMS
1032 //-----------------------------------------------------------------------------
1034 //-----------------------------------------------------------------------------
1036 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
1042 wxUint8 bWidth
; // Width of the image
1043 wxUint8 bHeight
; // Height of the image (times 2)
1044 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
1045 wxUint8 bReserved
; // Reserved
1047 // these two are different in icons and cursors:
1049 wxUint16 wPlanes
; // Color Planes or XHotSpot
1050 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
1052 wxUint32 dwBytesInRes
; // how many bytes in this resource?
1053 wxUint32 dwImageOffset
; // where in the file is this image
1058 wxUint16 idReserved
; // Reserved
1059 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1060 wxUint16 idCount
; // how many images?
1064 bool wxICOHandler::SaveFile(wxImage
*image
,
1065 wxOutputStream
& stream
,
1069 //sanity check; icon must be less than 127 pixels high and 255 wide
1070 if ( image
->GetHeight () > 127 )
1073 wxLogError(_("ICO: Image too tall for an icon."));
1076 if ( image
->GetWidth () > 255 )
1079 wxLogError(_("ICO: Image too wide for an icon."));
1083 const int images
= 1; // only generate one image
1085 // VS: This is a hack of sort - since ICO and CUR files are almost
1086 // identical, we have all the meat in wxICOHandler and check for
1087 // the actual (handler) type when the code has to distinguish between
1089 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1091 // write a header, (ICONDIR)
1092 // Calculate the header size
1093 wxUint32 offset
= 3 * sizeof(wxUint16
);
1096 IconDir
.idReserved
= 0;
1097 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1098 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1099 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1100 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1101 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1102 if ( !stream
.IsOk() )
1105 wxLogError(_("ICO: Error writing the image file!"));
1109 // for each iamage write a description ICONDIRENTRY:
1110 ICONDIRENTRY icondirentry
;
1111 for (int img
= 0; img
< images
; img
++)
1115 if ( image
->HasMask() )
1117 // make another image with black/white:
1118 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1120 // now we need to change the masked regions to black:
1121 unsigned char r
= image
->GetMaskRed();
1122 unsigned char g
= image
->GetMaskGreen();
1123 unsigned char b
= image
->GetMaskBlue();
1124 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1126 // Go round and apply black to the masked bits:
1128 for (i
= 0; i
< mask
.GetWidth(); i
++)
1130 for (j
= 0; j
< mask
.GetHeight(); j
++)
1132 if ((r
== mask
.GetRed(i
, j
)) &&
1133 (g
== mask
.GetGreen(i
, j
))&&
1134 (b
== mask
.GetBlue(i
, j
)) )
1135 image
->SetRGB(i
, j
, 0, 0, 0 );
1142 // just make a black mask all over:
1143 mask
= image
->Copy();
1145 for (i
= 0; i
< mask
.GetWidth(); i
++)
1146 for (j
= 0; j
< mask
.GetHeight(); j
++)
1147 mask
.SetRGB(i
, j
, 0, 0, 0 );
1149 // Set the formats for image and mask
1150 // (Windows never saves with more than 8 colors):
1151 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1153 // monochome bitmap:
1154 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1156 bool IsMask
= false;
1158 //calculate size and offset of image and mask
1159 wxCountingOutputStream cStream
;
1160 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1164 wxLogError(_("ICO: Error writing the image file!"));
1169 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1173 wxLogError(_("ICO: Error writing the image file!"));
1176 wxUint32 Size
= cStream
.GetSize();
1178 // wxCountingOutputStream::IsOk() always returns true for now and this
1179 // "if" provokes VC++ warnings in optimized build
1181 if ( !cStream
.Ok() )
1184 wxLogError(_("ICO: Error writing the image file!"));
1189 offset
= offset
+ sizeof(ICONDIRENTRY
);
1191 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1192 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1193 icondirentry
.bColorCount
= 0;
1194 icondirentry
.bReserved
= 0;
1195 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1196 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1197 if ( type
== 2 /*CUR*/)
1199 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1200 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1201 image
->GetWidth() / 2;
1202 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1203 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1204 image
->GetHeight() / 2;
1206 // actually write the values of the hot spot here:
1207 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1208 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1210 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1211 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1213 // increase size to allow for the data written:
1217 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1218 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1219 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1220 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1221 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1222 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1223 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1224 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1225 if ( !stream
.IsOk() )
1228 wxLogError(_("ICO: Error writing the image file!"));
1232 // actually save it:
1234 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1238 wxLogError(_("ICO: Error writing the image file!"));
1243 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1247 wxLogError(_("ICO: Error writing the image file!"));
1251 } // end of for loop
1256 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1257 bool verbose
, int index
)
1260 return DoLoadFile(image
, stream
, verbose
, index
);
1263 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1264 bool WXUNUSED(verbose
), int index
)
1266 bool bResult
wxDUMMY_INITIALIZE(false);
1271 wxFileOffset iPos
= stream
.TellI();
1272 stream
.Read(&IconDir
, sizeof(IconDir
));
1273 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1274 // nType is 1 for Icons, 2 for Cursors:
1275 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1277 // loop round the icons and choose the best one:
1278 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1279 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1282 int iSel
= wxNOT_FOUND
;
1284 for (int i
= 0; i
< nIcons
; i
++ )
1286 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1287 // bHeight and bColorCount are wxUint8
1288 if ( pCurrentEntry
->bWidth
>= wMax
)
1290 // see if we have more colors, ==0 indicates > 8bpp:
1291 if ( pCurrentEntry
->bColorCount
== 0 )
1292 pCurrentEntry
->bColorCount
= 255;
1293 if ( pCurrentEntry
->bColorCount
>= colmax
)
1296 wMax
= pCurrentEntry
->bWidth
;
1297 colmax
= pCurrentEntry
->bColorCount
;
1305 // VS: Note that we *have* to run the loop above even if index != -1, because
1306 // it reads ICONDIRENTRies.
1310 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1312 wxLogError(_("ICO: Invalid icon index."));
1317 // seek to selected icon:
1318 pCurrentEntry
= pIconDirEntry
+ iSel
;
1319 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1320 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1321 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1322 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1324 // it is a cursor, so let's set the hotspot:
1325 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1326 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1329 delete[] pIconDirEntry
;
1333 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1336 wxFileOffset iPos
= stream
.TellI();
1338 stream
.Read(&IconDir
, sizeof(IconDir
));
1339 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1344 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1347 unsigned char hdr
[4];
1348 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1351 // hdr[2] is one for an icon and two for a cursor
1352 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1355 #endif // wxUSE_STREAMS
1358 //-----------------------------------------------------------------------------
1360 //-----------------------------------------------------------------------------
1362 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1366 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1369 unsigned char hdr
[4];
1370 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1373 // hdr[2] is one for an icon and two for a cursor
1374 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1377 #endif // wxUSE_STREAMS
1379 //-----------------------------------------------------------------------------
1381 //-----------------------------------------------------------------------------
1383 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1387 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1388 bool WXUNUSED(verbose
), int index
)
1390 wxANIDecoder decoder
;
1391 if (!decoder
.Load(stream
))
1394 return decoder
.ConvertToImage(index
!= -1 ? (size_t)index
: 0, image
);
1397 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1400 return decod
.CanRead(stream
);
1403 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1405 wxANIDecoder decoder
;
1406 if (!decoder
.Load(stream
))
1409 return decoder
.GetFrameCount();
1412 #endif // wxUSE_STREAMS
1414 #endif // wxUSE_ICO_CUR
1416 #endif // wxUSE_IMAGE