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/anidecod.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxBMPHandler
,wxImageHandler
)
62 #define BI_BITFIELDS 3
65 #define poffset (line * width * 3 + column * 3)
67 bool wxBMPHandler::SaveFile(wxImage
*image
,
68 wxOutputStream
& stream
,
71 return SaveDib(image
, stream
, verbose
, true/*IsBmp*/, false/*IsMask*/);
74 bool wxBMPHandler::SaveDib(wxImage
*image
,
75 wxOutputStream
& stream
,
81 wxCHECK_MSG( image
, false, _T("invalid pointer in wxBMPHandler::SaveFile") );
86 wxLogError(_("BMP: Couldn't save invalid image."));
90 // get the format of the BMP file to save, else use 24bpp
91 unsigned format
= wxBMP_24BPP
;
92 if ( image
->HasOption(wxIMAGE_OPTION_BMP_FORMAT
) )
93 format
= image
->GetOptionInt(wxIMAGE_OPTION_BMP_FORMAT
);
95 wxUint16 bpp
; // # of bits per pixel
96 int palette_size
; // # of color map entries, ie. 2^bpp colors
98 // set the bpp and appropriate palette_size, and do additional checks
99 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_1BPP_BW
) )
104 else if ( format
== wxBMP_4BPP
)
109 else if ( (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_GREY
) ||
110 (format
== wxBMP_8BPP_RED
) || (format
== wxBMP_8BPP_PALETTE
) )
112 // need to set a wxPalette to use this, HOW TO CHECK IF VALID, SIZE?
113 if ((format
== wxBMP_8BPP_PALETTE
)
115 && !image
->HasPalette()
116 #endif // wxUSE_PALETTE
120 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
126 else // you get 24bpp
128 format
= wxBMP_24BPP
;
133 unsigned width
= image
->GetWidth();
134 unsigned row_padding
= (4 - int(width
*bpp
/8.0) % 4) % 4; // # bytes to pad to dword
135 unsigned row_width
= int(width
* bpp
/8.0) + row_padding
; // # of bytes per row
140 wxUint16 magic
; // format magic, always 'BM'
141 wxUint32 filesize
; // total file size, inc. headers
142 wxUint32 reserved
; // for future use
143 wxUint32 data_offset
; // image data offset in the file
146 wxUint32 bih_size
; // 2nd part's size
147 wxUint32 width
, height
; // bitmap's dimensions
148 wxUint16 planes
; // num of planes
149 wxUint16 bpp
; // bits per pixel
150 wxUint32 compression
; // compression method
151 wxUint32 size_of_bmp
; // size of the bitmap
152 wxUint32 h_res
, v_res
; // image resolution in pixels-per-meter
153 wxUint32 num_clrs
; // number of colors used
154 wxUint32 num_signif_clrs
;// number of significant colors
157 wxUint32 hdr_size
= 14/*BitmapHeader*/ + 40/*BitmapInfoHeader*/;
159 hdr
.magic
= wxUINT16_SWAP_ON_BE(0x4D42/*'BM'*/);
160 hdr
.filesize
= wxUINT32_SWAP_ON_BE( hdr_size
+ palette_size
*4 +
161 row_width
* image
->GetHeight() );
163 hdr
.data_offset
= wxUINT32_SWAP_ON_BE(hdr_size
+ palette_size
*4);
165 hdr
.bih_size
= wxUINT32_SWAP_ON_BE(hdr_size
- 14);
166 hdr
.width
= wxUINT32_SWAP_ON_BE(image
->GetWidth());
169 hdr
.height
= wxUINT32_SWAP_ON_BE(image
->GetHeight());
173 hdr
.height
= wxUINT32_SWAP_ON_BE(2 * image
->GetHeight());
175 hdr
.planes
= wxUINT16_SWAP_ON_BE(1); // always 1 plane
176 hdr
.bpp
= wxUINT16_SWAP_ON_BE(bpp
);
177 hdr
.compression
= 0; // RGB uncompressed
178 hdr
.size_of_bmp
= wxUINT32_SWAP_ON_BE(row_width
* image
->GetHeight());
180 // get the resolution from the image options or fall back to 72dpi standard
181 // for the BMP format if not specified
182 wxUint32 hres
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX
),
183 vres
= image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY
);
184 switch ( image
->GetOptionInt(wxIMAGE_OPTION_RESOLUTION
) )
187 wxFAIL_MSG( _T("unexpected image resolution units") );
190 case wxIMAGE_RESOLUTION_NONE
:
193 // fall through to convert it to correct units
195 case wxIMAGE_RESOLUTION_INCHES
:
196 // convert resolution in inches to resolution in centimeters
197 hres
= (wxUint32
)(100*mm2inches
*hres
);
198 vres
= (wxUint32
)(100*mm2inches
*vres
);
199 // fall through to convert it to resolution in meters
201 case wxIMAGE_RESOLUTION_CM
:
202 // convert resolution in centimeters to resolution in meters
208 hdr
.h_res
= wxUINT32_SWAP_ON_BE(hres
);
209 hdr
.v_res
= wxUINT32_SWAP_ON_BE(vres
);
210 hdr
.num_clrs
= wxUINT32_SWAP_ON_BE(palette_size
); // # colors in colormap
211 hdr
.num_signif_clrs
= 0; // all colors are significant
215 if (// VS: looks ugly but compilers tend to do ugly things with structs,
216 // like aligning hdr.filesize's ofset to dword :(
217 // VZ: we should add padding then...
218 !stream
.Write(&hdr
.magic
, 2) ||
219 !stream
.Write(&hdr
.filesize
, 4) ||
220 !stream
.Write(&hdr
.reserved
, 4) ||
221 !stream
.Write(&hdr
.data_offset
, 4)
225 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
232 !stream
.Write(&hdr
.bih_size
, 4) ||
233 !stream
.Write(&hdr
.width
, 4) ||
234 !stream
.Write(&hdr
.height
, 4) ||
235 !stream
.Write(&hdr
.planes
, 2) ||
236 !stream
.Write(&hdr
.bpp
, 2) ||
237 !stream
.Write(&hdr
.compression
, 4) ||
238 !stream
.Write(&hdr
.size_of_bmp
, 4) ||
239 !stream
.Write(&hdr
.h_res
, 4) ||
240 !stream
.Write(&hdr
.v_res
, 4) ||
241 !stream
.Write(&hdr
.num_clrs
, 4) ||
242 !stream
.Write(&hdr
.num_signif_clrs
, 4)
246 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
251 wxPalette
*palette
= NULL
; // entries for quantized images
252 wxUint8
*rgbquad
= NULL
; // for the RGBQUAD bytes for the colormap
253 wxImage
*q_image
= NULL
; // destination for quantized image
255 // if <24bpp use quantization to reduce colors for *some* of the formats
256 if ( (format
== wxBMP_1BPP
) || (format
== wxBMP_4BPP
) ||
257 (format
== wxBMP_8BPP
) || (format
== wxBMP_8BPP_PALETTE
) )
259 // make a new palette and quantize the image
260 if (format
!= wxBMP_8BPP_PALETTE
)
262 q_image
= new wxImage();
264 // I get a delete error using Quantize when desired colors > 236
265 int quantize
= ((palette_size
> 236) ? 236 : palette_size
);
266 // fill the destination too, it gives much nicer 4bpp images
267 wxQuantize::Quantize( *image
, *q_image
, &palette
, quantize
, 0,
268 wxQUANTIZE_FILL_DESTINATION_IMAGE
);
273 palette
= new wxPalette(image
->GetPalette());
274 #endif // wxUSE_PALETTE
278 unsigned char r
, g
, b
;
279 rgbquad
= new wxUint8
[palette_size
*4];
281 for (i
= 0; i
< palette_size
; i
++)
284 if ( !palette
->GetRGB(i
, &r
, &g
, &b
) )
285 #endif // wxUSE_PALETTE
294 // make a 256 entry greyscale colormap or 2 entry black & white
295 else if ( (format
== wxBMP_8BPP_GREY
) || (format
== wxBMP_8BPP_RED
) ||
296 (format
== wxBMP_1BPP_BW
) )
298 rgbquad
= new wxUint8
[palette_size
*4];
300 for ( int i
= 0; i
< palette_size
; i
++ )
302 // if 1BPP_BW then the value should be either 0 or 255
303 wxUint8 c
= (wxUint8
)((i
> 0) && (format
== wxBMP_1BPP_BW
) ? 255 : i
);
312 // if the colormap was made, then it needs to be written
317 if ( !stream
.Write(rgbquad
, palette_size
*4) )
320 wxLogError(_("BMP: Couldn't write RGB color map."));
324 #endif // wxUSE_PALETTE
332 // pointer to the image data, use quantized if available
333 wxUint8
*data
= (wxUint8
*) image
->GetData();
334 if (q_image
) if (q_image
->Ok()) data
= (wxUint8
*) q_image
->GetData();
336 wxUint8
*buffer
= new wxUint8
[row_width
];
337 memset(buffer
, 0, row_width
);
341 for (y
= image
->GetHeight() -1; y
>= 0; y
--)
343 if ( format
== wxBMP_24BPP
) // 3 bytes per pixel red,green,blue
345 for ( x
= 0; x
< width
; x
++ )
347 pixel
= 3*(y
*width
+ x
);
349 buffer
[3*x
] = data
[pixel
+2];
350 buffer
[3*x
+ 1] = data
[pixel
+1];
351 buffer
[3*x
+ 2] = data
[pixel
];
354 else if ((format
== wxBMP_8BPP
) || // 1 byte per pixel in color
355 (format
== wxBMP_8BPP_PALETTE
))
357 for (x
= 0; x
< width
; x
++)
359 pixel
= 3*(y
*width
+ x
);
361 buffer
[x
] = (wxUint8
)palette
->GetPixel( data
[pixel
],
365 // FIXME: what should this be? use some std palette maybe?
367 #endif // wxUSE_PALETTE
370 else if ( format
== wxBMP_8BPP_GREY
) // 1 byte per pix, rgb ave to grey
372 for (x
= 0; x
< width
; x
++)
374 pixel
= 3*(y
*width
+ x
);
375 buffer
[x
] = (wxUint8
)(.299*data
[pixel
] +
380 else if ( format
== wxBMP_8BPP_RED
) // 1 byte per pixel, red as greys
382 for (x
= 0; x
< width
; x
++)
384 buffer
[x
] = (wxUint8
)data
[3*(y
*width
+ x
)];
387 else if ( format
== wxBMP_4BPP
) // 4 bpp in color
389 for (x
= 0; x
< width
; x
+=2)
391 pixel
= 3*(y
*width
+ x
);
393 // fill buffer, ignore if > width
395 buffer
[x
/2] = (wxUint8
)(
396 ((wxUint8
)palette
->GetPixel(data
[pixel
],
398 data
[pixel
+2]) << 4) |
401 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3],
405 // FIXME: what should this be? use some std palette maybe?
407 #endif // wxUSE_PALETTE
410 else if ( format
== wxBMP_1BPP
) // 1 bpp in "color"
412 for (x
= 0; x
< width
; x
+=8)
414 pixel
= 3*(y
*width
+ x
);
417 buffer
[x
/8] = (wxUint8
)(
418 ((wxUint8
)palette
->GetPixel(data
[pixel
], data
[pixel
+1], data
[pixel
+2]) << 7) |
419 (((x
+1) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+3], data
[pixel
+4], data
[pixel
+5]) << 6)) |
420 (((x
+2) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+6], data
[pixel
+7], data
[pixel
+8]) << 5)) |
421 (((x
+3) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+9], data
[pixel
+10], data
[pixel
+11]) << 4)) |
422 (((x
+4) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+12], data
[pixel
+13], data
[pixel
+14]) << 3)) |
423 (((x
+5) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+15], data
[pixel
+16], data
[pixel
+17]) << 2)) |
424 (((x
+6) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+18], data
[pixel
+19], data
[pixel
+20]) << 1)) |
425 (((x
+7) > width
) ? 0 : ((wxUint8
)palette
->GetPixel(data
[pixel
+21], data
[pixel
+22], data
[pixel
+23]) )) );
427 // FIXME: what should this be? use some std palette maybe?
429 #endif // wxUSE_PALETTE
432 else if ( format
== wxBMP_1BPP_BW
) // 1 bpp B&W colormap from red color ONLY
434 for (x
= 0; x
< width
; x
+=8)
436 pixel
= 3*(y
*width
+ x
);
438 buffer
[x
/8] = (wxUint8
)(
439 (((wxUint8
)(data
[pixel
] /128.)) << 7) |
440 (((x
+1) > width
) ? 0 : (((wxUint8
)(data
[pixel
+3] /128.)) << 6)) |
441 (((x
+2) > width
) ? 0 : (((wxUint8
)(data
[pixel
+6] /128.)) << 5)) |
442 (((x
+3) > width
) ? 0 : (((wxUint8
)(data
[pixel
+9] /128.)) << 4)) |
443 (((x
+4) > width
) ? 0 : (((wxUint8
)(data
[pixel
+12]/128.)) << 3)) |
444 (((x
+5) > width
) ? 0 : (((wxUint8
)(data
[pixel
+15]/128.)) << 2)) |
445 (((x
+6) > width
) ? 0 : (((wxUint8
)(data
[pixel
+18]/128.)) << 1)) |
446 (((x
+7) > width
) ? 0 : (((wxUint8
)(data
[pixel
+21]/128.)) )) );
450 if ( !stream
.Write(buffer
, row_width
) )
453 wxLogError(_("BMP: Couldn't write data."));
457 #endif // wxUSE_PALETTE
465 #endif // wxUSE_PALETTE
474 unsigned char r
, g
, b
;
477 bool wxBMPHandler::DoLoadDib(wxImage
* image
, int width
, int height
,
478 int bpp
, int ncolors
, int comp
,
479 wxFileOffset bmpOffset
, wxInputStream
& stream
,
480 bool verbose
, bool IsBmp
, bool hasPalette
)
482 wxInt32 aDword
, rmask
= 0, gmask
= 0, bmask
= 0;
483 int rshift
= 0, gshift
= 0, bshift
= 0;
484 int rbits
= 0, gbits
= 0, bbits
= 0;
490 // allocate space for palette if needed:
495 cmap
= new _cmap
[ncolors
];
499 wxLogError(_("BMP: Couldn't allocate memory."));
506 // destroy existing here instead of:
508 image
->Create(width
, height
);
510 unsigned char *ptr
= image
->GetData();
515 wxLogError( _("BMP: Couldn't allocate memory.") );
521 // Reading the palette, if it exists:
522 if ( bpp
< 16 && ncolors
!= 0 )
524 unsigned char* r
= new unsigned char[ncolors
];
525 unsigned char* g
= new unsigned char[ncolors
];
526 unsigned char* b
= new unsigned char[ncolors
];
527 for (int j
= 0; j
< ncolors
; j
++)
531 stream
.Read(bbuf
, 4);
542 //used in reading .ico file mask
545 b
[j
] = cmap
[j
].b
= ( j
? 255 : 0 );
550 // Set the palette for the wxImage
551 image
->SetPalette(wxPalette(ncolors
, r
, g
, b
));
552 #endif // wxUSE_PALETTE
558 else if ( bpp
== 16 || bpp
== 32 )
560 if ( comp
== BI_BITFIELDS
)
563 stream
.Read(dbuf
, 4 * 3);
564 rmask
= wxINT32_SWAP_ON_BE(dbuf
[0]);
565 gmask
= wxINT32_SWAP_ON_BE(dbuf
[1]);
566 bmask
= wxINT32_SWAP_ON_BE(dbuf
[2]);
567 // find shift amount (Least significant bit of mask)
568 for (bit
= bpp
-1; bit
>=0; bit
--)
570 if (bmask
& (1 << bit
))
572 if (gmask
& (1 << bit
))
574 if (rmask
& (1 << bit
))
577 // Find number of bits in mask (MSB-LSB+1)
578 for (bit
= 0; bit
< bpp
; bit
++)
580 if (bmask
& (1 << bit
))
581 bbits
= bit
-bshift
+1;
582 if (gmask
& (1 << bit
))
583 gbits
= bit
-gshift
+1;
584 if (rmask
& (1 << bit
))
585 rbits
= bit
-rshift
+1;
588 else if ( bpp
== 16 )
600 else if ( bpp
== 32 )
615 * Reading the image data
618 stream
.SeekI(bmpOffset
); // else icon, just carry on
620 unsigned char *data
= ptr
;
622 /* set the whole image to the background color */
623 if ( bpp
< 16 && (comp
== BI_RLE4
|| comp
== BI_RLE8
) )
625 for (int i
= 0; i
< width
* height
; i
++)
634 int linesize
= ((width
* bpp
+ 31) / 32) * 4;
636 /* BMPs are stored upside down */
637 for ( int line
= (height
- 1); line
>= 0; line
-- )
640 for ( int column
= 0; column
< width
; )
645 aByte
= stream
.GetC();
648 for (int bit
= 0; bit
< 8 && column
< width
; bit
++)
650 int index
= ((aByte
& (0x80 >> bit
)) ? 1 : 0);
651 ptr
[poffset
] = cmap
[index
].r
;
652 ptr
[poffset
+ 1] = cmap
[index
].g
;
653 ptr
[poffset
+ 2] = cmap
[index
].b
;
659 if ( comp
== BI_RLE4
)
663 aByte
= stream
.GetC();
671 else if ( aByte
== 1 )
676 else if ( aByte
== 2 )
678 aByte
= stream
.GetC();
680 linepos
= column
* bpp
/ 4;
681 aByte
= stream
.GetC();
682 line
-= aByte
; // upside down
686 int absolute
= aByte
;
689 for (int k
= 0; k
< absolute
; k
++)
694 aByte
= stream
.GetC();
695 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
696 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
698 ptr
[poffset
] = cmap
[nibble
[k%2
]].r
;
699 ptr
[poffset
+ 1] = cmap
[nibble
[k%2
]].g
;
700 ptr
[poffset
+ 2] = cmap
[nibble
[k%2
]].b
;
705 if ( readBytes
& 0x01 )
706 aByte
= stream
.GetC();
712 nibble
[0] = (wxUint8
)( (aByte
& 0xF0) >> 4 ) ;
713 nibble
[1] = (wxUint8
)( aByte
& 0x0F ) ;
715 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
717 ptr
[poffset
] = cmap
[nibble
[l%2
]].r
;
718 ptr
[poffset
+ 1] = cmap
[nibble
[l%2
]].g
;
719 ptr
[poffset
+ 2] = cmap
[nibble
[l%2
]].b
;
728 for (int nibble
= 0; nibble
< 2 && column
< width
; nibble
++)
730 int index
= ((aByte
& (0xF0 >> nibble
* 4)) >> (!nibble
* 4));
733 ptr
[poffset
] = cmap
[index
].r
;
734 ptr
[poffset
+ 1] = cmap
[index
].g
;
735 ptr
[poffset
+ 2] = cmap
[index
].b
;
742 if ( comp
== BI_RLE8
)
746 aByte
= stream
.GetC();
751 /* column = width; */
753 else if ( aByte
== 1 )
758 else if ( aByte
== 2 )
760 aByte
= stream
.GetC();
762 linepos
= column
* bpp
/ 8;
763 aByte
= stream
.GetC();
768 int absolute
= aByte
;
769 for (int k
= 0; k
< absolute
; k
++)
772 aByte
= stream
.GetC();
773 ptr
[poffset
] = cmap
[aByte
].r
;
774 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
775 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
778 if ( absolute
& 0x01 )
779 aByte
= stream
.GetC();
784 for ( int l
= 0; l
< first
&& column
< width
; l
++ )
786 ptr
[poffset
] = cmap
[aByte
].r
;
787 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
788 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
796 ptr
[poffset
] = cmap
[aByte
].r
;
797 ptr
[poffset
+ 1] = cmap
[aByte
].g
;
798 ptr
[poffset
+ 2] = cmap
[aByte
].b
;
800 // linepos += size; seems to be wrong, RR
804 else if ( bpp
== 24 )
806 stream
.Read(bbuf
, 3);
808 ptr
[poffset
] = (unsigned char)bbuf
[2];
809 ptr
[poffset
+ 1] = (unsigned char)bbuf
[1];
810 ptr
[poffset
+ 2] = (unsigned char)bbuf
[0];
813 else if ( bpp
== 16 )
816 stream
.Read(&aWord
, 2);
817 aWord
= wxUINT16_SWAP_ON_BE(aWord
);
819 /* use the masks and calculated amonut of shift
820 to retrieve the color data out of the word. Then
821 shift it left by (8 - number of bits) such that
822 the image has the proper dynamic range */
823 temp
= (unsigned char)((aWord
& rmask
) >> rshift
<< (8-rbits
));
825 temp
= (unsigned char)((aWord
& gmask
) >> gshift
<< (8-gbits
));
826 ptr
[poffset
+ 1] = temp
;
827 temp
= (unsigned char)((aWord
& bmask
) >> bshift
<< (8-bbits
));
828 ptr
[poffset
+ 2] = temp
;
834 stream
.Read(&aDword
, 4);
835 aDword
= wxINT32_SWAP_ON_BE(aDword
);
837 temp
= (unsigned char)((aDword
& rmask
) >> rshift
);
839 temp
= (unsigned char)((aDword
& gmask
) >> gshift
);
840 ptr
[poffset
+ 1] = temp
;
841 temp
= (unsigned char)((aDword
& bmask
) >> bshift
);
842 ptr
[poffset
+ 2] = temp
;
846 while ( (linepos
< linesize
) && (comp
!= 1) && (comp
!= 2) )
848 stream
.Read(&aByte
, 1);
857 image
->SetMask(false);
859 const wxStreamError err
= stream
.GetLastError();
860 return err
== wxSTREAM_NO_ERROR
|| err
== wxSTREAM_EOF
;
863 bool wxBMPHandler::LoadDib(wxImage
*image
, wxInputStream
& stream
,
864 bool verbose
, bool IsBmp
)
870 wxFileOffset offset
= 0; // keep gcc quiet
873 // read the header off the .BMP format file
875 offset
= stream
.TellI();
876 if (offset
== wxInvalidOffset
)
879 stream
.Read(bbuf
, 2);
880 stream
.Read(dbuf
, 16);
884 stream
.Read(dbuf
, 4);
887 wxInt32 size
= wxINT32_SWAP_ON_BE(dbuf
[0]);
889 offset
= offset
+ wxINT32_SWAP_ON_BE(dbuf
[2]);
891 stream
.Read(dbuf
, 4 * 2);
892 int width
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
893 int height
= wxINT32_SWAP_ON_BE((int)dbuf
[1]);
894 if ( !IsBmp
)height
= height
/ 2; // for icons divide by 2
899 wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
902 if ( height
> 32767 )
905 wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
909 stream
.Read(&aWord
, 2);
912 int planes = (int)wxUINT16_SWAP_ON_BE( aWord );
914 stream
.Read(&aWord
, 2);
915 int bpp
= wxUINT16_SWAP_ON_BE((int)aWord
);
916 if ( bpp
!= 1 && bpp
!= 4 && bpp
!= 8 && bpp
!= 16 && bpp
!= 24 && bpp
!= 32 )
919 wxLogError( _("DIB Header: Unknown bitdepth in file.") );
923 stream
.Read(dbuf
, 4 * 4);
924 int comp
= wxINT32_SWAP_ON_BE((int)dbuf
[0]);
925 if ( comp
!= BI_RGB
&& comp
!= BI_RLE4
&& comp
!= BI_RLE8
&&
926 comp
!= BI_BITFIELDS
)
929 wxLogError( _("DIB Header: Unknown encoding in file.") );
933 stream
.Read(dbuf
, 4 * 2);
935 int ncolors
= wxINT32_SWAP_ON_BE( (int)dbuf
[0] );
938 /* some more sanity checks */
939 if (((comp
== BI_RLE4
) && (bpp
!= 4)) ||
940 ((comp
== BI_RLE8
) && (bpp
!= 8)) ||
941 ((comp
== BI_BITFIELDS
) && (bpp
!= 16 && bpp
!= 32)))
944 wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
948 //read DIB; this is the BMP image or the XOR part of an icon image
949 if ( !DoLoadDib(image
, width
, height
, bpp
, ncolors
, comp
, offset
, stream
,
950 verbose
, IsBmp
, true) )
953 wxLogError( _("Error in reading image DIB.") );
959 //read Icon mask which is monochrome
960 //there is no palette, so we will create one
962 if ( !DoLoadDib(&mask
, width
, height
, 1, 2, BI_RGB
, offset
, stream
,
963 verbose
, IsBmp
, false) )
966 wxLogError( _("ICO: Error in reading mask DIB.") );
969 image
->SetMaskFromImage(mask
, 255, 255, 255);
973 // the resolution in the bitmap header is in meters, convert to centimeters
974 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT
, wxIMAGE_RESOLUTION_CM
);
975 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONX
, dbuf
[2]/100);
976 image
->SetOption(wxIMAGE_OPTION_RESOLUTIONY
, dbuf
[3]/100);
981 bool wxBMPHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
982 bool verbose
, int WXUNUSED(index
))
984 // Read a single DIB fom the file:
985 return LoadDib(image
, stream
, verbose
, true/*isBmp*/);
988 bool wxBMPHandler::DoCanRead(wxInputStream
& stream
)
990 unsigned char hdr
[2];
992 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
995 // do we have the BMP file signature?
996 return hdr
[0] == 'B' && hdr
[1] == 'M';
999 #endif // wxUSE_STREAMS
1003 //-----------------------------------------------------------------------------
1005 //-----------------------------------------------------------------------------
1007 IMPLEMENT_DYNAMIC_CLASS(wxICOHandler
, wxBMPHandler
)
1013 wxUint8 bWidth
; // Width of the image
1014 wxUint8 bHeight
; // Height of the image (times 2)
1015 wxUint8 bColorCount
; // Number of colors in image (0 if >=8bpp)
1016 wxUint8 bReserved
; // Reserved
1018 // these two are different in icons and cursors:
1020 wxUint16 wPlanes
; // Color Planes or XHotSpot
1021 wxUint16 wBitCount
; // Bits per pixel or YHotSpot
1023 wxUint32 dwBytesInRes
; // how many bytes in this resource?
1024 wxUint32 dwImageOffset
; // where in the file is this image
1029 wxUint16 idReserved
; // Reserved
1030 wxUint16 idType
; // resource type (1 for icons, 2 for cursors)
1031 wxUint16 idCount
; // how many images?
1035 bool wxICOHandler::SaveFile(wxImage
*image
,
1036 wxOutputStream
& stream
,
1040 //sanity check; icon must be less than 127 pixels high and 255 wide
1041 if ( image
->GetHeight () > 127 )
1044 wxLogError(_("ICO: Image too tall for an icon."));
1047 if ( image
->GetWidth () > 255 )
1050 wxLogError(_("ICO: Image too wide for an icon."));
1054 const int images
= 1; // only generate one image
1056 // VS: This is a hack of sort - since ICO and CUR files are almost
1057 // identical, we have all the meat in wxICOHandler and check for
1058 // the actual (handler) type when the code has to distinguish between
1060 int type
= (this->GetType() == wxBITMAP_TYPE_CUR
) ? 2 : 1;
1062 // write a header, (ICONDIR)
1063 // Calculate the header size
1064 wxUint32 offset
= 3 * sizeof(wxUint16
);
1067 IconDir
.idReserved
= 0;
1068 IconDir
.idType
= wxUINT16_SWAP_ON_BE((wxUint16
)type
);
1069 IconDir
.idCount
= wxUINT16_SWAP_ON_BE((wxUint16
)images
);
1070 stream
.Write(&IconDir
.idReserved
, sizeof(IconDir
.idReserved
));
1071 stream
.Write(&IconDir
.idType
, sizeof(IconDir
.idType
));
1072 stream
.Write(&IconDir
.idCount
, sizeof(IconDir
.idCount
));
1073 if ( !stream
.IsOk() )
1076 wxLogError(_("ICO: Error writing the image file!"));
1080 // for each iamage write a description ICONDIRENTRY:
1081 ICONDIRENTRY icondirentry
;
1082 for (int img
= 0; img
< images
; img
++)
1086 if ( image
->HasMask() )
1088 // make another image with black/white:
1089 mask
= image
->ConvertToMono (image
->GetMaskRed(), image
->GetMaskGreen(), image
->GetMaskBlue() );
1091 // now we need to change the masked regions to black:
1092 unsigned char r
= image
->GetMaskRed();
1093 unsigned char g
= image
->GetMaskGreen();
1094 unsigned char b
= image
->GetMaskBlue();
1095 if ( (r
!= 0) || (g
!= 0) || (b
!= 0) )
1097 // Go round and apply black to the masked bits:
1099 for (i
= 0; i
< mask
.GetWidth(); i
++)
1101 for (j
= 0; j
< mask
.GetHeight(); j
++)
1103 if ((r
== mask
.GetRed(i
, j
)) &&
1104 (g
== mask
.GetGreen(i
, j
))&&
1105 (b
== mask
.GetBlue(i
, j
)) )
1106 image
->SetRGB(i
, j
, 0, 0, 0 );
1113 // just make a black mask all over:
1114 mask
= image
->Copy();
1116 for (i
= 0; i
< mask
.GetWidth(); i
++)
1117 for (j
= 0; j
< mask
.GetHeight(); j
++)
1118 mask
.SetRGB(i
, j
, 0, 0, 0 );
1120 // Set the formats for image and mask
1121 // (Windows never saves with more than 8 colors):
1122 image
->SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_8BPP
);
1124 // monochome bitmap:
1125 mask
.SetOption(wxIMAGE_OPTION_BMP_FORMAT
, wxBMP_1BPP_BW
);
1127 bool IsMask
= false;
1129 //calculate size and offset of image and mask
1130 wxCountingOutputStream cStream
;
1131 bool bResult
= SaveDib(image
, cStream
, verbose
, IsBmp
, IsMask
);
1135 wxLogError(_("ICO: Error writing the image file!"));
1140 bResult
= SaveDib(&mask
, cStream
, verbose
, IsBmp
, IsMask
);
1144 wxLogError(_("ICO: Error writing the image file!"));
1147 wxUint32 Size
= cStream
.GetSize();
1149 // wxCountingOutputStream::IsOk() always returns true for now and this
1150 // "if" provokes VC++ warnings in optimized build
1152 if ( !cStream
.Ok() )
1155 wxLogError(_("ICO: Error writing the image file!"));
1160 offset
= offset
+ sizeof(ICONDIRENTRY
);
1162 icondirentry
.bWidth
= (wxUint8
)image
->GetWidth();
1163 icondirentry
.bHeight
= (wxUint8
)(2 * image
->GetHeight());
1164 icondirentry
.bColorCount
= 0;
1165 icondirentry
.bReserved
= 0;
1166 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE(1);
1167 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE(wxBMP_8BPP
);
1168 if ( type
== 2 /*CUR*/)
1170 int hx
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
) ?
1171 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_X
) :
1172 image
->GetWidth() / 2;
1173 int hy
= image
->HasOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) ?
1174 image
->GetOptionInt(wxIMAGE_OPTION_CUR_HOTSPOT_Y
) :
1175 image
->GetHeight() / 2;
1177 // actually write the values of the hot spot here:
1178 icondirentry
.wPlanes
= wxUINT16_SWAP_ON_BE((wxUint16
)hx
);
1179 icondirentry
.wBitCount
= wxUINT16_SWAP_ON_BE((wxUint16
)hy
);
1181 icondirentry
.dwBytesInRes
= wxUINT32_SWAP_ON_BE(Size
);
1182 icondirentry
.dwImageOffset
= wxUINT32_SWAP_ON_BE(offset
);
1184 // increase size to allow for the data written:
1188 stream
.Write(&icondirentry
.bWidth
, sizeof(icondirentry
.bWidth
));
1189 stream
.Write(&icondirentry
.bHeight
, sizeof(icondirentry
.bHeight
));
1190 stream
.Write(&icondirentry
.bColorCount
, sizeof(icondirentry
.bColorCount
));
1191 stream
.Write(&icondirentry
.bReserved
, sizeof(icondirentry
.bReserved
));
1192 stream
.Write(&icondirentry
.wPlanes
, sizeof(icondirentry
.wPlanes
));
1193 stream
.Write(&icondirentry
.wBitCount
, sizeof(icondirentry
.wBitCount
));
1194 stream
.Write(&icondirentry
.dwBytesInRes
, sizeof(icondirentry
.dwBytesInRes
));
1195 stream
.Write(&icondirentry
.dwImageOffset
, sizeof(icondirentry
.dwImageOffset
));
1196 if ( !stream
.IsOk() )
1199 wxLogError(_("ICO: Error writing the image file!"));
1203 // actually save it:
1205 bResult
= SaveDib(image
, stream
, verbose
, IsBmp
, IsMask
);
1209 wxLogError(_("ICO: Error writing the image file!"));
1214 bResult
= SaveDib(&mask
, stream
, verbose
, IsBmp
, IsMask
);
1218 wxLogError(_("ICO: Error writing the image file!"));
1222 } // end of for loop
1227 bool wxICOHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1228 bool verbose
, int index
)
1231 return DoLoadFile(image
, stream
, verbose
, index
);
1234 bool wxICOHandler::DoLoadFile(wxImage
*image
, wxInputStream
& stream
,
1235 bool WXUNUSED(verbose
), int index
)
1237 bool bResult
wxDUMMY_INITIALIZE(false);
1242 wxFileOffset iPos
= stream
.TellI();
1243 stream
.Read(&IconDir
, sizeof(IconDir
));
1244 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1245 // nType is 1 for Icons, 2 for Cursors:
1246 wxUint16 nType
= wxUINT16_SWAP_ON_BE(IconDir
.idType
);
1248 // loop round the icons and choose the best one:
1249 ICONDIRENTRY
*pIconDirEntry
= new ICONDIRENTRY
[nIcons
];
1250 ICONDIRENTRY
*pCurrentEntry
= pIconDirEntry
;
1253 int iSel
= wxNOT_FOUND
;
1255 for (int i
= 0; i
< nIcons
; i
++ )
1257 stream
.Read(pCurrentEntry
, sizeof(ICONDIRENTRY
));
1258 // bHeight and bColorCount are wxUint8
1259 if ( pCurrentEntry
->bWidth
>= wMax
)
1261 // see if we have more colors, ==0 indicates > 8bpp:
1262 if ( pCurrentEntry
->bColorCount
== 0 )
1263 pCurrentEntry
->bColorCount
= 255;
1264 if ( pCurrentEntry
->bColorCount
>= colmax
)
1267 wMax
= pCurrentEntry
->bWidth
;
1268 colmax
= pCurrentEntry
->bColorCount
;
1276 // VS: Note that we *have* to run the loop above even if index != -1, because
1277 // it reads ICONDIRENTRies.
1281 if ( iSel
== wxNOT_FOUND
|| iSel
< 0 || iSel
>= nIcons
)
1283 wxLogError(_("ICO: Invalid icon index."));
1288 // seek to selected icon:
1289 pCurrentEntry
= pIconDirEntry
+ iSel
;
1290 stream
.SeekI(iPos
+ wxUINT32_SWAP_ON_BE(pCurrentEntry
->dwImageOffset
), wxFromStart
);
1291 bResult
= LoadDib(image
, stream
, true, IsBmp
);
1292 bool bIsCursorType
= (this->GetType() == wxBITMAP_TYPE_CUR
) || (this->GetType() == wxBITMAP_TYPE_ANI
);
1293 if ( bResult
&& bIsCursorType
&& nType
== 2 )
1295 // it is a cursor, so let's set the hotspot:
1296 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wPlanes
));
1297 image
->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y
, wxUINT16_SWAP_ON_BE(pCurrentEntry
->wBitCount
));
1300 delete[] pIconDirEntry
;
1304 int wxICOHandler::GetImageCount(wxInputStream
& stream
)
1307 wxFileOffset iPos
= stream
.TellI();
1309 stream
.Read(&IconDir
, sizeof(IconDir
));
1310 wxUint16 nIcons
= wxUINT16_SWAP_ON_BE(IconDir
.idCount
);
1315 bool wxICOHandler::DoCanRead(wxInputStream
& stream
)
1318 unsigned char hdr
[4];
1319 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1322 // hdr[2] is one for an icon and two for a cursor
1323 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\1' && hdr
[3] == '\0';
1326 #endif // wxUSE_STREAMS
1329 //-----------------------------------------------------------------------------
1331 //-----------------------------------------------------------------------------
1333 IMPLEMENT_DYNAMIC_CLASS(wxCURHandler
, wxICOHandler
)
1337 bool wxCURHandler::DoCanRead(wxInputStream
& stream
)
1340 unsigned char hdr
[4];
1341 if ( !stream
.Read(hdr
, WXSIZEOF(hdr
)) )
1344 // hdr[2] is one for an icon and two for a cursor
1345 return hdr
[0] == '\0' && hdr
[1] == '\0' && hdr
[2] == '\2' && hdr
[3] == '\0';
1348 #endif // wxUSE_STREAMS
1350 //-----------------------------------------------------------------------------
1352 //-----------------------------------------------------------------------------
1354 IMPLEMENT_DYNAMIC_CLASS(wxANIHandler
, wxCURHandler
)
1358 bool wxANIHandler::LoadFile(wxImage
*image
, wxInputStream
& stream
,
1359 bool WXUNUSED(verbose
), int index
)
1361 wxANIDecoder decoder
;
1362 if (!decoder
.Load(stream
))
1365 return decoder
.ConvertToImage(index
!= -1 ? (size_t)index
: 0, image
);
1368 bool wxANIHandler::DoCanRead(wxInputStream
& stream
)
1371 return decod
.CanRead(stream
);
1374 int wxANIHandler::GetImageCount(wxInputStream
& stream
)
1376 wxANIDecoder decoder
;
1377 if (!decoder
.Load(stream
))
1380 return decoder
.GetFrameCount();
1383 #endif // wxUSE_STREAMS
1385 #endif // wxUSE_ICO_CUR
1387 #endif // wxUSE_IMAGE