1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bitmap.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
35 #include "wx/palette.h"
36 #include "wx/dcmemory.h"
42 #include "wx/msw/private.h"
45 #include "wx/msw/dib.h"
48 #include "wx/xpmdecod.h"
50 #ifdef wxHAVE_RAW_BITMAP
51 #include "wx/rawbmp.h"
54 // missing from mingw32 header
56 #define CLR_INVALID ((COLORREF)-1)
57 #endif // no CLR_INVALID
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
67 wxBitmapRefData(const wxBitmapRefData
& data
);
68 virtual ~wxBitmapRefData() { Free(); }
72 // set the mask object to use as the mask, we take ownership of it
73 void SetMask(wxMask
*mask
)
79 // set the HBITMAP to use as the mask
80 void SetMask(HBITMAP hbmpMask
)
82 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
86 wxMask
*GetMask() const { return m_bitmapMask
; }
90 wxPalette m_bitmapPalette
;
91 #endif // wxUSE_PALETTE
97 // this field is solely for error checking: we detect selecting a bitmap
98 // into more than one DC at once or deleting a bitmap still selected into a
99 // DC (both are serious programming errors under Windows)
100 wxDC
*m_selectedInto
;
101 #endif // __WXDEBUG__
104 // when GetRawData() is called for a DDB we need to convert it to a DIB
105 // first to be able to provide direct access to it and we cache that DIB
106 // here and convert it back to DDB when UngetRawData() is called
110 // true if we have alpha transparency info and can be drawn using
114 // true if our HBITMAP is a DIB section, false if it is a DDB
118 // optional mask for transparent drawing
119 wxMask
*m_bitmapMask
;
123 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 // decide whether we should create a DIB or a DDB for the given parameters
145 // NB: we always use DIBs under Windows CE as this is much simpler (even if
146 // also less efficient...) and we obviously can't use them if there is no
147 // DIB support compiled in at all
149 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
151 #define ALWAYS_USE_DIB
153 // no sense in defining wxShouldCreateDIB() as we can't compile code
154 // executed if it is true, so we have to use #if's anyhow
155 #define NEVER_USE_DIB
156 #else // wxUSE_WXDIB && !__WXWINCE__
157 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
159 // here is the logic:
161 // (a) if hdc is specified, the caller explicitly wants DDB
162 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
163 // or less DIBs anyhow)
164 // (c) finally, create DIBs under Win9x even if the depth hasn't been
165 // explicitly specified but the current display depth is 24 or
166 // more and the image is "big", i.e. > 16Mb which is the
167 // theoretical limit for DDBs under Win9x
169 // consequences (all of which seem to make sense):
171 // (i) by default, DDBs are created (depth == -1 usually)
172 // (ii) DIBs can be created by explicitly specifying the depth
173 // (iii) using a DC always forces creating a DDB
177 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
180 #define SOMETIMES_USE_DIB
181 #endif // different DIB usage scenarious
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 wxBitmapRefData::wxBitmapRefData()
190 m_selectedInto
= NULL
;
194 m_hBitmap
= (WXHBITMAP
) NULL
;
203 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
204 : wxGDIImageRefData(data
)
207 m_selectedInto
= NULL
;
210 // can't copy the mask as the other bitmap destroys it
213 wxASSERT_MSG( !data
.m_isDIB
,
214 _T("can't copy bitmap locked for raw access!") );
217 m_hasAlpha
= data
.m_hasAlpha
;
220 void wxBitmapRefData::Free()
222 wxASSERT_MSG( !m_selectedInto
,
223 wxT("deleting bitmap still selected into wxMemoryDC") );
226 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
231 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
233 wxLogLastError(wxT("DeleteObject(hbitmap)"));
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 wxGDIImageRefData
*wxBitmap::CreateData() const
247 return new wxBitmapRefData
;
250 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
252 const wxBitmapRefData
*
253 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
257 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
260 // copy the other bitmap
261 if ( data
->m_hBitmap
)
263 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
264 self
->CopyFromDIB(dib
);
267 #endif // wxUSE_WXDIB
269 // don't copy the bitmap data, but do copy the size, depth, ...
270 self
->m_refData
= new wxBitmapRefData(*data
);
278 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
280 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
281 // it may be either HICON or HCURSOR
282 HICON hicon
= (HICON
)icon
.GetHandle();
285 if ( !::GetIconInfo(hicon
, &iconInfo
) )
287 wxLogLastError(wxT("GetIconInfo"));
292 wxBitmapRefData
*refData
= new wxBitmapRefData
;
295 int w
= icon
.GetWidth(),
296 h
= icon
.GetHeight();
298 refData
->m_width
= w
;
299 refData
->m_height
= h
;
300 refData
->m_depth
= wxDisplayDepth();
302 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
305 // If the icon is 32 bits per pixel then it may have alpha channel data,
306 // although there are some icons that are 32 bpp but have no alpha... So
307 // convert to a DIB and manually check the 4th byte for each pixel.
309 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
310 && bm
.bmBitsPixel
== 32)
312 wxDIB
dib(iconInfo
.hbmColor
);
315 unsigned char* pixels
= dib
.GetData();
316 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
318 if (pixels
[idx
+3] != 0)
320 // If there is an alpha byte that is non-zero then set the
321 // alpha flag and bail out of the loop.
322 refData
->m_hasAlpha
= true;
329 if ( !refData
->m_hasAlpha
)
331 // the mask returned by GetIconInfo() is inverted compared to the usual
333 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
336 // delete the old one now as we don't need it any more
337 ::DeleteObject(iconInfo
.hbmMask
);
348 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
355 return CopyFromIconOrCursor(cursor
);
358 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
365 return CopyFromIconOrCursor(icon
);
368 #ifndef NEVER_USE_DIB
370 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
372 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
374 #ifdef SOMETIMES_USE_DIB
375 HBITMAP hbitmap
= dib
.CreateDDB();
378 #else // ALWAYS_USE_DIB
379 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
380 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
384 wxBitmapRefData
*refData
= new wxBitmapRefData
;
387 refData
->m_width
= dib
.GetWidth();
388 refData
->m_height
= dib
.GetHeight();
389 refData
->m_depth
= dib
.GetDepth();
391 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
394 wxPalette
*palette
= dib
.CreatePalette();
397 refData
->m_bitmapPalette
= *palette
;
401 #endif // wxUSE_PALETTE
406 #endif // NEVER_USE_DIB
408 wxBitmap::~wxBitmap()
412 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
414 #ifndef __WXMICROWIN__
415 wxBitmapRefData
*refData
= new wxBitmapRefData
;
418 refData
->m_width
= width
;
419 refData
->m_height
= height
;
420 refData
->m_depth
= depth
;
425 // we assume that it is in XBM format which is not quite the same as
426 // the format CreateBitmap() wants because the order of bytes in the
428 const size_t bytesPerLine
= (width
+ 7) / 8;
429 const size_t padding
= bytesPerLine
% 2;
430 const size_t len
= height
* ( padding
+ bytesPerLine
);
431 data
= (char *)malloc(len
);
432 const char *src
= bits
;
435 for ( int rows
= 0; rows
< height
; rows
++ )
437 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
439 unsigned char val
= *src
++;
440 unsigned char reversed
= 0;
442 for ( int bits
= 0; bits
< 8; bits
++)
445 reversed
|= (unsigned char)(val
& 0x01);
457 // bits should already be in Windows standard format
458 data
= (char *)bits
; // const_cast is harmless
461 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
464 wxLogLastError(wxT("CreateBitmap"));
472 SetHBITMAP((WXHBITMAP
)hbmp
);
476 wxBitmap::wxBitmap(int w
, int h
, int d
)
478 (void)Create(w
, h
, d
);
481 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
483 (void)Create(w
, h
, dc
);
486 wxBitmap::wxBitmap(const void* data
, long type
, int width
, int height
, int depth
)
488 (void)Create(data
, type
, width
, height
, depth
);
491 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
493 LoadFile(filename
, (int)type
);
496 bool wxBitmap::Create(int width
, int height
, int depth
)
498 return DoCreate(width
, height
, depth
, 0);
501 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
503 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
505 return DoCreate(width
, height
, -1, dc
.GetHDC());
508 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
512 m_refData
= new wxBitmapRefData
;
514 GetBitmapData()->m_width
= w
;
515 GetBitmapData()->m_height
= h
;
517 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
519 #ifndef NEVER_USE_DIB
520 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
524 // create DIBs without alpha channel by default
532 // don't delete the DIB section in dib object dtor
535 GetBitmapData()->m_isDIB
= true;
536 GetBitmapData()->m_depth
= d
;
539 #endif // NEVER_USE_DIB
541 #ifndef ALWAYS_USE_DIB
542 #ifndef __WXMICROWIN__
545 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
548 wxLogLastError(wxT("CreateBitmap"));
551 GetBitmapData()->m_depth
= d
;
553 else // d == 0, create bitmap compatible with the screen
554 #endif // !__WXMICROWIN__
557 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
560 wxLogLastError(wxT("CreateCompatibleBitmap"));
563 GetBitmapData()->m_depth
= wxDisplayDepth();
565 #endif // !ALWAYS_USE_DIB
568 SetHBITMAP((WXHBITMAP
)hbmp
);
575 // ----------------------------------------------------------------------------
576 // wxImage to/from conversions for Microwin
577 // ----------------------------------------------------------------------------
579 // Microwin versions are so different from normal ones that it really doesn't
580 // make sense to use #ifdefs inside the function bodies
581 #ifdef __WXMICROWIN__
583 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
585 // Set this to 1 to experiment with mask code,
586 // which currently doesn't work
589 m_refData
= new wxBitmapRefData();
591 // Initial attempt at a simple-minded implementation.
592 // The bitmap will always be created at the screen depth,
593 // so the 'depth' argument is ignored.
595 HDC hScreenDC
= ::GetDC(NULL
);
596 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
598 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
599 HBITMAP hMaskBitmap
= NULL
;
600 HBITMAP hOldMaskBitmap
= NULL
;
602 unsigned char maskR
= 0;
603 unsigned char maskG
= 0;
604 unsigned char maskB
= 0;
606 // printf("Created bitmap %d\n", (int) hBitmap);
609 ::ReleaseDC(NULL
, hScreenDC
);
612 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
614 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
615 ::ReleaseDC(NULL
, hScreenDC
);
617 // created an mono-bitmap for the possible mask
618 bool hasMask
= image
.HasMask();
623 // FIXME: we should be able to pass bpp = 1, but
624 // GdBlit can't handle a different depth
626 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
628 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
630 maskR
= image
.GetMaskRed();
631 maskG
= image
.GetMaskGreen();
632 maskB
= image
.GetMaskBlue();
640 hScreenDC
= ::GetDC(NULL
);
641 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
642 ::ReleaseDC(NULL
, hScreenDC
);
644 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
652 for (i
= 0; i
< image
.GetWidth(); i
++)
654 for (j
= 0; j
< image
.GetHeight(); j
++)
656 unsigned char red
= image
.GetRed(i
, j
);
657 unsigned char green
= image
.GetGreen(i
, j
);
658 unsigned char blue
= image
.GetBlue(i
, j
);
660 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
664 // scan the bitmap for the transparent colour and set the corresponding
665 // pixels in the mask to BLACK and the rest to WHITE
666 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
667 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
669 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
674 ::SelectObject(hMemDC
, hOldBitmap
);
678 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
681 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
684 SetWidth(image
.GetWidth());
685 SetHeight(image
.GetHeight());
686 SetDepth(screenDepth
);
687 SetHBITMAP( (WXHBITMAP
) hBitmap
);
690 // Copy the palette from the source image
691 SetPalette(image
.GetPalette());
692 #endif // wxUSE_PALETTE
697 wxImage
wxBitmap::ConvertToImage() const
699 // Initial attempt at a simple-minded implementation.
700 // The bitmap will always be created at the screen depth,
701 // so the 'depth' argument is ignored.
702 // TODO: transparency (create a mask image)
706 wxFAIL_MSG( wxT("bitmap is invalid") );
712 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
714 // create an wxImage object
715 int width
= GetWidth();
716 int height
= GetHeight();
717 image
.Create( width
, height
);
718 unsigned char *data
= image
.GetData();
721 wxFAIL_MSG( wxT("could not allocate data for image") );
725 HDC hScreenDC
= ::GetDC(NULL
);
727 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
728 ::ReleaseDC(NULL
, hScreenDC
);
730 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
732 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
735 for (i
= 0; i
< GetWidth(); i
++)
737 for (j
= 0; j
< GetHeight(); j
++)
739 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
740 unsigned char red
= GetRValue(color
);
741 unsigned char green
= GetGValue(color
);
742 unsigned char blue
= GetBValue(color
);
744 image
.SetRGB(i
, j
, red
, green
, blue
);
748 ::SelectObject(hMemDC
, hOldBitmap
);
752 // Copy the palette from the source image
754 image
.SetPalette(* GetPalette());
755 #endif // wxUSE_PALETTE
760 #endif // __WXMICROWIN__
762 // ----------------------------------------------------------------------------
763 // wxImage to/from conversions
764 // ----------------------------------------------------------------------------
766 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
768 return CreateFromImage(image
, depth
, 0);
771 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
773 wxCHECK_MSG( dc
.Ok(), false,
774 _T("invalid HDC in wxBitmap::CreateFromImage()") );
776 return CreateFromImage(image
, -1, dc
.GetHDC());
781 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
783 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
787 // first convert the image to DIB
788 const int h
= image
.GetHeight();
789 const int w
= image
.GetWidth();
796 depth
= dib
.GetDepth(); // Get depth from image if none specified
798 // store the bitmap parameters
799 wxBitmapRefData
*refData
= new wxBitmapRefData
;
800 refData
->m_width
= w
;
801 refData
->m_height
= h
;
802 refData
->m_hasAlpha
= image
.HasAlpha();
807 // next either store DIB as is or create a DDB from it
808 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
810 // are we going to use DIB?
812 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
813 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
815 // don't delete the DIB section in dib object dtor
816 hbitmap
= dib
.Detach();
818 refData
->m_isDIB
= true;
819 refData
->m_depth
= depth
;
821 #ifndef ALWAYS_USE_DIB
822 else // we need to convert DIB to DDB
824 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
826 refData
->m_depth
= depth
;
828 #endif // !ALWAYS_USE_DIB
830 // validate this object
831 SetHBITMAP((WXHBITMAP
)hbitmap
);
833 // finally also set the mask if we have one
834 if ( image
.HasMask() )
836 const size_t len
= 2*((w
+15)/16);
837 BYTE
*src
= image
.GetData();
838 BYTE
*data
= new BYTE
[h
*len
];
839 memset(data
, 0, h
*len
);
840 BYTE r
= image
.GetMaskRed(),
841 g
= image
.GetMaskGreen(),
842 b
= image
.GetMaskBlue();
844 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
848 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
850 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
853 if ( (mask
>>= 1) == 0 )
861 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
864 wxLogLastError(_T("CreateBitmap(mask)"));
868 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
877 wxImage
wxBitmap::ConvertToImage() const
879 // convert DDB to DIB
887 // and then DIB to our wxImage
888 wxImage image
= dib
.ConvertToImage();
894 // now do the same for the mask, if we have any
895 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
898 wxDIB
dibMask(hbmpMask
);
899 if ( dibMask
.IsOk() )
901 // TODO: use wxRawBitmap to iterate over DIB
903 // we hard code the mask colour for now but we could also make an
904 // effort (and waste time) to choose a colour not present in the
905 // image already to avoid having to fudge the pixels below --
906 // whether it's worth to do it is unclear however
907 static const int MASK_RED
= 1;
908 static const int MASK_GREEN
= 2;
909 static const int MASK_BLUE
= 3;
910 static const int MASK_BLUE_REPLACEMENT
= 2;
912 const int h
= dibMask
.GetHeight();
913 const int w
= dibMask
.GetWidth();
914 const int bpp
= dibMask
.GetDepth();
915 const int maskBytesPerPixel
= bpp
>> 3;
916 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
917 unsigned char *data
= image
.GetData();
919 // remember that DIBs are stored in bottom to top order
921 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
923 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
925 // traverse one mask DIB line
926 unsigned char *mask
= maskLineStart
;
927 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
929 // should this pixel be transparent?
932 // no, check that it isn't transparent by accident
933 if ( (data
[0] == MASK_RED
) &&
934 (data
[1] == MASK_GREEN
) &&
935 (data
[2] == MASK_BLUE
) )
937 // we have to fudge the colour a bit to prevent
938 // this pixel from appearing transparent
939 data
[2] = MASK_BLUE_REPLACEMENT
;
944 else // yes, transparent pixel
947 *data
++ = MASK_GREEN
;
953 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
960 #else // !wxUSE_WXDIB
963 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
970 wxImage
wxBitmap::ConvertToImage() const
975 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
977 #endif // wxUSE_IMAGE
979 // ----------------------------------------------------------------------------
980 // loading and saving bitmaps
981 // ----------------------------------------------------------------------------
983 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
987 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
991 m_refData
= new wxBitmapRefData
;
993 return handler
->LoadFile(this, filename
, type
, -1, -1);
995 #if wxUSE_IMAGE && wxUSE_WXDIB
996 else // no bitmap handler found
999 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1001 *this = wxBitmap(image
);
1006 #endif // wxUSE_IMAGE
1011 bool wxBitmap::Create(const void* data
, long type
, int width
, int height
, int depth
)
1015 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1019 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1024 m_refData
= new wxBitmapRefData
;
1026 return handler
->Create(this, data
, type
, width
, height
, depth
);
1029 bool wxBitmap::SaveFile(const wxString
& filename
,
1031 const wxPalette
*palette
)
1033 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1037 return handler
->SaveFile(this, filename
, type
, palette
);
1039 #if wxUSE_IMAGE && wxUSE_WXDIB
1040 else // no bitmap handler found
1042 // FIXME what about palette? shouldn't we use it?
1043 wxImage image
= ConvertToImage();
1046 return image
.SaveFile(filename
, type
);
1049 #endif // wxUSE_IMAGE
1054 // ----------------------------------------------------------------------------
1055 // sub bitmap extraction
1056 // ----------------------------------------------------------------------------
1058 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1060 wxCHECK_MSG( Ok() &&
1061 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1062 (rect
.x
+rect
.width
<= GetWidth()) &&
1063 (rect
.y
+rect
.height
<= GetHeight()),
1064 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1066 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1067 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1069 #ifndef __WXMICROWIN__
1070 // handle alpha channel, if any
1079 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1080 selectDst(dcDst
, GetHbitmapOf(ret
));
1082 if ( !selectSrc
|| !selectDst
)
1084 wxLogLastError(_T("SelectObjct(hBitmap)"));
1087 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1088 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1090 wxLogLastError(_T("BitBlt"));
1094 // copy mask if there is one
1097 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1099 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1100 selectDst(dcDst
, hbmpMask
);
1102 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1103 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1105 wxLogLastError(_T("BitBlt"));
1108 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1111 #endif // !__WXMICROWIN__
1116 // ----------------------------------------------------------------------------
1117 // wxBitmap accessors
1118 // ----------------------------------------------------------------------------
1121 wxPalette
* wxBitmap::GetPalette() const
1123 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1124 : (wxPalette
*) NULL
;
1128 wxMask
*wxBitmap::GetMask() const
1130 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1133 wxBitmap
wxBitmap::GetMaskBitmap() const
1136 wxMask
*mask
= GetMask();
1138 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1144 wxDC
*wxBitmap::GetSelectedInto() const
1146 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1151 #if WXWIN_COMPATIBILITY_2_4
1153 int wxBitmap::GetQuality() const
1158 #endif // WXWIN_COMPATIBILITY_2_4
1160 void wxBitmap::UseAlpha()
1162 if ( GetBitmapData() )
1163 GetBitmapData()->m_hasAlpha
= true;
1166 bool wxBitmap::HasAlpha() const
1168 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1171 // ----------------------------------------------------------------------------
1173 // ----------------------------------------------------------------------------
1177 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1179 if ( GetBitmapData() )
1180 GetBitmapData()->m_selectedInto
= dc
;
1187 void wxBitmap::SetPalette(const wxPalette
& palette
)
1191 GetBitmapData()->m_bitmapPalette
= palette
;
1194 #endif // wxUSE_PALETTE
1196 void wxBitmap::SetMask(wxMask
*mask
)
1200 GetBitmapData()->SetMask(mask
);
1203 #if WXWIN_COMPATIBILITY_2_4
1205 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1209 #endif // WXWIN_COMPATIBILITY_2_4
1211 // ----------------------------------------------------------------------------
1212 // raw bitmap access support
1213 // ----------------------------------------------------------------------------
1215 #ifdef wxHAVE_RAW_BITMAP
1216 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1221 // no bitmap, no data (raw or otherwise)
1225 // if we're already a DIB we can access our data directly, but if not we
1226 // need to convert this DDB to a DIB section and use it for raw access and
1227 // then convert it back
1229 if ( !GetBitmapData()->m_isDIB
)
1231 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1232 _T("GetRawData() may be called only once") );
1234 wxDIB
*dib
= new wxDIB(*this);
1242 // we'll free it in UngetRawData()
1243 GetBitmapData()->m_dib
= dib
;
1245 hDIB
= dib
->GetHandle();
1249 hDIB
= GetHbitmap();
1253 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1255 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1260 // check that the bitmap is in correct format
1261 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1263 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1268 // ok, store the relevant info in wxPixelDataBase
1269 const LONG h
= ds
.dsBm
.bmHeight
;
1271 data
.m_width
= ds
.dsBm
.bmWidth
;
1274 // remember that DIBs are stored in top to bottom order!
1275 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1276 // multiple of 2, as required by the documentation. So we use the official
1277 // formula, which we already use elsewhere.)
1278 const LONG bytesPerRow
=
1279 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1280 data
.m_stride
= -bytesPerRow
;
1282 char *bits
= (char *)ds
.dsBm
.bmBits
;
1285 bits
+= (h
- 1)*bytesPerRow
;
1294 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1302 // invalid data, don't crash -- but don't assert neither as we're
1303 // called automatically from wxPixelDataBase dtor and so there is no
1304 // way to prevent this from happening
1308 // if we're a DDB we need to convert DIB back to DDB now to make the
1309 // changes made via raw bitmap access effective
1310 if ( !GetBitmapData()->m_isDIB
)
1312 wxDIB
*dib
= GetBitmapData()->m_dib
;
1313 GetBitmapData()->m_dib
= NULL
;
1319 #endif // wxUSE_WXDIB
1321 #endif // #ifdef wxHAVE_RAW_BITMAP
1323 // ----------------------------------------------------------------------------
1325 // ----------------------------------------------------------------------------
1332 // Construct a mask from a bitmap and a colour indicating
1333 // the transparent area
1334 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1337 Create(bitmap
, colour
);
1340 // Construct a mask from a bitmap and a palette index indicating
1341 // the transparent area
1342 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1345 Create(bitmap
, paletteIndex
);
1348 // Construct a mask from a mono bitmap (copies the bitmap).
1349 wxMask::wxMask(const wxBitmap
& bitmap
)
1358 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1361 // Create a mask from a mono bitmap (copies the bitmap).
1362 bool wxMask::Create(const wxBitmap
& bitmap
)
1364 #ifndef __WXMICROWIN__
1365 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1366 _T("can't create mask from invalid or not monochrome bitmap") );
1370 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1374 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1379 HDC srcDC
= CreateCompatibleDC(0);
1380 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1381 HDC destDC
= CreateCompatibleDC(0);
1382 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1383 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1384 SelectObject(srcDC
, 0);
1386 SelectObject(destDC
, 0);
1390 wxUnusedVar(bitmap
);
1395 // Create a mask from a bitmap and a palette index indicating
1396 // the transparent area
1397 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1401 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1406 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1408 unsigned char red
, green
, blue
;
1409 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1411 wxColour
transparentColour(red
, green
, blue
);
1412 return Create(bitmap
, transparentColour
);
1415 #endif // wxUSE_PALETTE
1420 // Create a mask from a bitmap and a colour indicating
1421 // the transparent area
1422 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1424 #ifndef __WXMICROWIN__
1425 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1429 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1433 int width
= bitmap
.GetWidth(),
1434 height
= bitmap
.GetHeight();
1436 // scan the bitmap for the transparent colour and set the corresponding
1437 // pixels in the mask to BLACK and the rest to WHITE
1438 COLORREF maskColour
= wxColourToPalRGB(colour
);
1439 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1441 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1442 HDC destDC
= ::CreateCompatibleDC(NULL
);
1443 if ( !srcDC
|| !destDC
)
1445 wxLogLastError(wxT("CreateCompatibleDC"));
1450 // SelectObject() will fail
1451 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1452 _T("bitmap can't be selected in another DC") );
1454 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1457 wxLogLastError(wxT("SelectObject"));
1462 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1465 wxLogLastError(wxT("SelectObject"));
1472 // this will create a monochrome bitmap with 0 points for the pixels
1473 // which have the same value as the background colour and 1 for the
1475 ::SetBkColor(srcDC
, maskColour
);
1476 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1479 ::SelectObject(srcDC
, hbmpSrcOld
);
1481 ::SelectObject(destDC
, hbmpDstOld
);
1485 #else // __WXMICROWIN__
1486 wxUnusedVar(bitmap
);
1487 wxUnusedVar(colour
);
1489 #endif // __WXMICROWIN__/!__WXMICROWIN__
1492 // ----------------------------------------------------------------------------
1494 // ----------------------------------------------------------------------------
1496 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1499 int width
, int height
, int depth
)
1501 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1503 return bitmap
&& Create(bitmap
, data
, flags
, width
, height
, depth
);
1506 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1507 const wxString
& name
,
1509 int width
, int height
)
1511 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1513 return bitmap
&& LoadFile(bitmap
, name
, flags
, width
, height
);
1516 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1517 const wxString
& name
,
1520 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1522 return bitmap
&& SaveFile(bitmap
, name
, type
);
1525 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1526 const void* WXUNUSED(data
),
1527 long WXUNUSED(type
),
1528 int WXUNUSED(width
),
1529 int WXUNUSED(height
),
1530 int WXUNUSED(depth
))
1535 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1536 const wxString
& WXUNUSED(name
),
1537 long WXUNUSED(type
),
1538 int WXUNUSED(desiredWidth
),
1539 int WXUNUSED(desiredHeight
))
1544 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1545 const wxString
& WXUNUSED(name
),
1547 const wxPalette
*WXUNUSED(palette
))
1552 // ----------------------------------------------------------------------------
1554 // ----------------------------------------------------------------------------
1556 #ifndef __WXMICROWIN__
1557 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1558 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1560 unsigned long i
, headerSize
;
1562 // Allocate space for a DIB header
1563 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1564 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1565 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1567 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1569 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1571 // Fill in the static parts of the DIB header
1572 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1573 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1574 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1575 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1577 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1578 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1579 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1580 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1581 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1584 // Initialize the DIB palette
1585 for (i
= 0; i
< 256; i
++) {
1586 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1587 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1588 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1589 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1592 *lpDIBHeader
= lpDIBheader
;
1597 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1603 // ----------------------------------------------------------------------------
1604 // global helper functions implemented here
1605 // ----------------------------------------------------------------------------
1607 // helper of wxBitmapToHICON/HCURSOR
1609 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1616 // we can't create an icon/cursor form nothing
1620 if ( bmp
.HasAlpha() )
1622 // Create an empty mask bitmap.
1623 // it doesn't seem to work if we mess with the mask at all.
1624 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1627 wxZeroMemory(iconInfo
);
1628 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1631 iconInfo
.xHotspot
= hotSpotX
;
1632 iconInfo
.yHotspot
= hotSpotY
;
1635 iconInfo
.hbmMask
= hMonoBitmap
;
1636 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1638 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1640 ::DeleteObject(hMonoBitmap
);
1645 wxMask
* mask
= bmp
.GetMask();
1649 // we must have a mask for an icon, so even if it's probably incorrect,
1650 // do create it (grey is the "standard" transparent colour)
1651 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1655 wxZeroMemory(iconInfo
);
1656 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1659 iconInfo
.xHotspot
= hotSpotX
;
1660 iconInfo
.yHotspot
= hotSpotY
;
1663 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1664 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1666 // black out the transparent area to preserve background colour, because
1667 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1668 // the mask to the dest rect.
1670 MemoryHDC dcSrc
, dcDst
;
1671 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1672 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1674 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1675 dcSrc
, 0, 0, SRCAND
) )
1677 wxLogLastError(_T("BitBlt"));
1681 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1683 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1685 // we created the mask, now delete it
1689 // delete the inverted mask bitmap we created as well
1690 ::DeleteObject(iconInfo
.hbmMask
);
1695 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1697 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1700 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1702 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1705 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1707 #ifndef __WXMICROWIN__
1708 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1710 // get width/height from the bitmap if not given
1714 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1719 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1720 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1721 if ( !hdcSrc
|| !hdcDst
)
1723 wxLogLastError(wxT("CreateCompatibleDC"));
1726 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1729 wxLogLastError(wxT("CreateBitmap"));
1732 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1733 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1734 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1738 wxLogLastError(wxT("BitBlt"));
1742 SelectObject(hdcSrc
,srcTmp
);
1743 SelectObject(hdcDst
,dstTmp
);