1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 #ifdef wxHAVE_RAW_BITMAP
54 #include "wx/rawbmp.h"
57 // missing from mingw32 header
59 #define CLR_INVALID ((COLORREF)-1)
60 #endif // no CLR_INVALID
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
70 virtual ~wxBitmapRefData() { Free(); }
74 // set the mask object to use as the mask, we take ownership of it
75 void SetMask(wxMask
*mask
)
81 // set the HBITMAP to use as the mask
82 void SetMask(HBITMAP hbmpMask
)
84 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
88 wxMask
*GetMask() const { return m_bitmapMask
; }
92 wxPalette m_bitmapPalette
;
93 #endif // wxUSE_PALETTE
99 // this field is solely for error checking: we detect selecting a bitmap
100 // into more than one DC at once or deleting a bitmap still selected into a
101 // DC (both are serious programming errors under Windows)
102 wxDC
*m_selectedInto
;
103 #endif // __WXDEBUG__
106 // when GetRawData() is called for a DDB we need to convert it to a DIB
107 // first to be able to provide direct access to it and we cache that DIB
108 // here and convert it back to DDB when UngetRawData() is called
112 // true if we have alpha transparency info and can be drawn using
116 // true if our HBITMAP is a DIB section, false if it is a DDB
120 // optional mask for transparent drawing
121 wxMask
*m_bitmapMask
;
123 DECLARE_NO_COPY_CLASS(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
144 static bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
147 // here is the logic:
149 // (a) if hdc is specified, the caller explicitly wants DDB
150 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
152 // (c) finally, create DIBs under Win9x even if the depth hasn't been
153 // explicitly specified but the current display depth is 24 or more
154 // and the image is "big", i.e. > 16Mb which is the theoretical limit
155 // for DDBs under Win9x
157 // consequences (all of which seem to make sense):
159 // (i) by default, DDBs are created (depth == -1 usually)
160 // (ii) DIBs can be created by explicitly specifying the depth
161 // (iii) using a DC always forces creating a DDB
165 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 wxBitmapRefData::wxBitmapRefData()
178 m_selectedInto
= NULL
;
182 m_hBitmap
= (WXHBITMAP
) NULL
;
191 void wxBitmapRefData::Free()
193 wxASSERT_MSG( !m_selectedInto
,
194 wxT("deleting bitmap still selected into wxMemoryDC") );
197 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
202 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
204 wxLogLastError(wxT("DeleteObject(hbitmap)"));
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 // this function should be called from all wxBitmap ctors
217 void wxBitmap::Init()
219 // m_refData = NULL; done in the base class ctor
222 wxGDIImageRefData
*wxBitmap::CreateData() const
224 return new wxBitmapRefData
;
229 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
231 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
232 // it may be either HICON or HCURSOR
233 HICON hicon
= (HICON
)icon
.GetHandle();
236 if ( !::GetIconInfo(hicon
, &iconInfo
) )
238 wxLogLastError(wxT("GetIconInfo"));
243 wxBitmapRefData
*refData
= new wxBitmapRefData
;
246 int w
= icon
.GetWidth(),
247 h
= icon
.GetHeight();
249 refData
->m_width
= w
;
250 refData
->m_height
= h
;
251 refData
->m_depth
= wxDisplayDepth();
253 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
255 // the mask returned by GetIconInfo() is inversed compared to the usual
257 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
260 // delete the old one now as we don't need it any more
261 ::DeleteObject(iconInfo
.hbmMask
);
271 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
278 return CopyFromIconOrCursor(cursor
);
281 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
288 return CopyFromIconOrCursor(icon
);
292 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
294 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
296 HBITMAP hbitmap
= dib
.CreateDDB();
302 wxBitmapRefData
*refData
= new wxBitmapRefData
;
305 refData
->m_width
= dib
.GetWidth();
306 refData
->m_height
= dib
.GetHeight();
307 refData
->m_depth
= dib
.GetDepth();
309 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
312 wxPalette
*palette
= dib
.CreatePalette();
315 refData
->m_bitmapPalette
= *palette
;
319 #endif // wxUSE_PALETTE
325 wxBitmap::~wxBitmap()
329 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
333 #ifndef __WXMICROWIN__
334 wxBitmapRefData
*refData
= new wxBitmapRefData
;
337 refData
->m_width
= width
;
338 refData
->m_height
= height
;
339 refData
->m_depth
= depth
;
344 // we assume that it is in XBM format which is not quite the same as
345 // the format CreateBitmap() wants because the order of bytes in the
347 const size_t bytesPerLine
= (width
+ 7) / 8;
348 const size_t padding
= bytesPerLine
% 2;
349 const size_t len
= height
* ( padding
+ bytesPerLine
);
350 data
= (char *)malloc(len
);
351 const char *src
= bits
;
354 for ( int rows
= 0; rows
< height
; rows
++ )
356 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
358 unsigned char val
= *src
++;
359 unsigned char reversed
= 0;
361 for ( int bits
= 0; bits
< 8; bits
++)
364 reversed
|= (val
& 0x01);
376 // bits should already be in Windows standard format
377 data
= (char *)bits
; // const_cast is harmless
380 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
383 wxLogLastError(wxT("CreateBitmap"));
391 SetHBITMAP((WXHBITMAP
)hbmp
);
395 // Create from XPM data
396 #if wxUSE_IMAGE && wxUSE_XPM
397 bool wxBitmap::CreateFromXpm(const char **data
)
399 bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data
))
402 #if wxUSE_IMAGE && wxUSE_XPM
405 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
407 wxXPMDecoder decoder
;
408 wxImage img
= decoder
.ReadData(data
);
409 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
411 *this = wxBitmap(img
);
418 wxBitmap::wxBitmap(int w
, int h
, int d
)
422 (void)Create(w
, h
, d
);
425 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
429 (void)Create(w
, h
, dc
);
432 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
436 (void)Create(data
, type
, width
, height
, depth
);
439 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
443 LoadFile(filename
, (int)type
);
446 bool wxBitmap::Create(int width
, int height
, int depth
)
448 return DoCreate(width
, height
, depth
, 0);
451 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
453 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
455 return DoCreate(width
, height
, -1, dc
.GetHDC());
458 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
462 m_refData
= new wxBitmapRefData
;
464 GetBitmapData()->m_width
= w
;
465 GetBitmapData()->m_height
= h
;
470 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
474 // create DIBs without alpha channel by default
482 // don't delete the DIB section in dib object dtor
485 GetBitmapData()->m_isDIB
= TRUE
;
486 GetBitmapData()->m_depth
= d
;
492 #ifndef __WXMICROWIN__
495 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
498 wxLogLastError(wxT("CreateBitmap"));
502 #endif // !__WXMICROWIN__
505 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
508 wxLogLastError(wxT("CreateCompatibleBitmap"));
511 GetBitmapData()->m_depth
= wxDisplayDepth();
515 SetHBITMAP((WXHBITMAP
)hbmp
);
522 // ----------------------------------------------------------------------------
523 // wxImage to/from conversions for Microwin
524 // ----------------------------------------------------------------------------
526 // Microwin versions are so different from normal ones that it really doesn't
527 // make sense to use #ifdefs inside the function bodies
528 #ifdef __WXMICROWIN__
530 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
532 // Set this to 1 to experiment with mask code,
533 // which currently doesn't work
536 m_refData
= new wxBitmapRefData();
538 // Initial attempt at a simple-minded implementation.
539 // The bitmap will always be created at the screen depth,
540 // so the 'depth' argument is ignored.
542 HDC hScreenDC
= ::GetDC(NULL
);
543 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
545 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
546 HBITMAP hMaskBitmap
= NULL
;
547 HBITMAP hOldMaskBitmap
= NULL
;
549 unsigned char maskR
= 0;
550 unsigned char maskG
= 0;
551 unsigned char maskB
= 0;
553 // printf("Created bitmap %d\n", (int) hBitmap);
556 ::ReleaseDC(NULL
, hScreenDC
);
559 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
561 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
562 ::ReleaseDC(NULL
, hScreenDC
);
564 // created an mono-bitmap for the possible mask
565 bool hasMask
= image
.HasMask();
570 // FIXME: we should be able to pass bpp = 1, but
571 // GdBlit can't handle a different depth
573 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
575 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
577 maskR
= image
.GetMaskRed();
578 maskG
= image
.GetMaskGreen();
579 maskB
= image
.GetMaskBlue();
587 hScreenDC
= ::GetDC(NULL
);
588 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
589 ::ReleaseDC(NULL
, hScreenDC
);
591 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
599 for (i
= 0; i
< image
.GetWidth(); i
++)
601 for (j
= 0; j
< image
.GetHeight(); j
++)
603 unsigned char red
= image
.GetRed(i
, j
);
604 unsigned char green
= image
.GetGreen(i
, j
);
605 unsigned char blue
= image
.GetBlue(i
, j
);
607 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
611 // scan the bitmap for the transparent colour and set the corresponding
612 // pixels in the mask to BLACK and the rest to WHITE
613 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
614 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
616 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
621 ::SelectObject(hMemDC
, hOldBitmap
);
625 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
628 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
631 SetWidth(image
.GetWidth());
632 SetHeight(image
.GetHeight());
633 SetDepth(screenDepth
);
634 SetHBITMAP( (WXHBITMAP
) hBitmap
);
637 // Copy the palette from the source image
638 SetPalette(image
.GetPalette());
639 #endif // wxUSE_PALETTE
644 wxImage
wxBitmap::ConvertToImage() const
646 // Initial attempt at a simple-minded implementation.
647 // The bitmap will always be created at the screen depth,
648 // so the 'depth' argument is ignored.
649 // TODO: transparency (create a mask image)
653 wxFAIL_MSG( wxT("bitmap is invalid") );
659 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
661 // create an wxImage object
662 int width
= GetWidth();
663 int height
= GetHeight();
664 image
.Create( width
, height
);
665 unsigned char *data
= image
.GetData();
668 wxFAIL_MSG( wxT("could not allocate data for image") );
672 HDC hScreenDC
= ::GetDC(NULL
);
674 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
675 ::ReleaseDC(NULL
, hScreenDC
);
677 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
679 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
682 for (i
= 0; i
< GetWidth(); i
++)
684 for (j
= 0; j
< GetHeight(); j
++)
686 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
687 unsigned char red
= GetRValue(color
);
688 unsigned char green
= GetGValue(color
);
689 unsigned char blue
= GetBValue(color
);
691 image
.SetRGB(i
, j
, red
, green
, blue
);
695 ::SelectObject(hMemDC
, hOldBitmap
);
699 // Copy the palette from the source image
701 image
.SetPalette(* GetPalette());
702 #endif // wxUSE_PALETTE
707 #endif // __WXMICROWIN__
709 // ----------------------------------------------------------------------------
710 // wxImage to/from conversions
711 // ----------------------------------------------------------------------------
713 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
715 return CreateFromImage(image
, depth
, 0);
718 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
720 wxCHECK_MSG( dc
.Ok(), FALSE
,
721 _T("invalid HDC in wxBitmap::CreateFromImage()") );
723 return CreateFromImage(image
, -1, dc
.GetHDC());
726 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
729 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
733 // first convert the image to DIB
734 const int h
= image
.GetHeight();
735 const int w
= image
.GetWidth();
742 // store the bitmap parameters
743 wxBitmapRefData
*refData
= new wxBitmapRefData
;
744 refData
->m_width
= w
;
745 refData
->m_height
= h
;
746 refData
->m_hasAlpha
= image
.HasAlpha();
751 // next either store DIB as is or create a DDB from it
754 // are we going to use DIB?
756 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
757 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
759 // don't delete the DIB section in dib object dtor
760 hbitmap
= dib
.Detach();
762 refData
->m_isDIB
= TRUE
;
763 refData
->m_depth
= dib
.GetDepth();
765 else // we need to convert DIB to DDB
767 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
769 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
772 // validate this object
773 SetHBITMAP((WXHBITMAP
)hbitmap
);
775 // finally also set the mask if we have one
776 if ( image
.HasMask() )
778 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
779 image
.GetMaskGreen(),
780 image
.GetMaskBlue())));
785 // FIXME: wxWinCE doesn't support wxDIB yet
790 wxImage
wxBitmap::ConvertToImage() const
792 // FIXME: this is untested code for WinCE, and
793 // the mask is not yet handled.
795 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
798 // the colour used as transparent one in wxImage and the one it is replaced
799 // with when it really occurs in the bitmap
800 static const int MASK_RED
= 1;
801 static const int MASK_GREEN
= 2;
802 static const int MASK_BLUE
= 3;
803 static const int MASK_BLUE_REPLACEMENT
= 2;
807 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
809 // create an wxImage object
810 int width
= GetWidth();
811 int height
= GetHeight();
812 image
.Create( width
, height
);
813 unsigned char *data
= image
.GetData();
816 wxFAIL_MSG( wxT("could not allocate data for image") );
820 // calc the number of bytes per scanline and padding in the DIB
821 int bytePerLine
= width
*3;
822 int sizeDWORD
= sizeof( DWORD
);
823 int lineBoundary
= bytePerLine
% sizeDWORD
;
825 if( lineBoundary
> 0 )
827 padding
= sizeDWORD
- lineBoundary
;
828 bytePerLine
+= padding
;
831 // create a DIB header
832 int headersize
= sizeof(BITMAPINFOHEADER
);
833 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
836 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
840 // Fill in the DIB header
841 lpDIBh
->bmiHeader
.biSize
= headersize
;
842 lpDIBh
->bmiHeader
.biWidth
= width
;
843 lpDIBh
->bmiHeader
.biHeight
= -height
;
844 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
845 lpDIBh
->bmiHeader
.biPlanes
= 1;
846 lpDIBh
->bmiHeader
.biBitCount
= 24;
847 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
848 lpDIBh
->bmiHeader
.biClrUsed
= 0;
849 // These seem not really needed for our purpose here.
850 lpDIBh
->bmiHeader
.biClrImportant
= 0;
851 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
852 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
854 // memory for DIB data is allocated by CreateDIBSection
857 // copy data from the device-dependent bitmap to the DIB
858 HDC hdc
= ::GetDC(NULL
);
859 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
861 HBITMAP hBitmapSection
= ::CreateDIBSection( hdc
, lpDIBh
, DIB_RGB_COLORS
, & lpBits
, NULL
, 0 );
864 wxFAIL_MSG( wxT("could not create a DIB section") );
868 // Copy the image from the DDB to the DIBSection
869 // Need to copy the supplied bitmap onto the newly created DIBsection
870 HDC hMemDC
= CreateCompatibleDC(hdc
);
871 HDC hCopyDC
= CreateCompatibleDC(hdc
);
873 if (! hMemDC
|| ! hCopyDC
)
875 wxFAIL_MSG( wxT("unable to create compatible DCs") );
882 SelectPalette(hMemDC
, m_hPal
, FALSE
); RealizePalette(hMemDC
);
883 SelectPalette(hCopyDC
, m_hPal
, FALSE
); RealizePalette(hCopyDC
);
887 HBITMAP hOldMemBitmap
= (HBITMAP
) SelectObject(hMemDC
, hBitmap
);
888 HBITMAP hOldCopyBitmap
= (HBITMAP
) SelectObject(hCopyDC
, hBitmapSection
);
890 BitBlt(hCopyDC
, 0, 0, GetWidth(), GetHeight(), hMemDC
, 0, 0, SRCCOPY
);
892 SelectObject(hMemDC
, hOldMemBitmap
);
893 SelectObject(hCopyDC
, hOldCopyBitmap
);
900 HGDIOBJ hObj
= ::GetStockObject(DEFAULT_PALETTE
);
901 SelectObject(hMemDC
, hObj
);
902 SelectObject(hCopyDC
, hObj
);
906 ReleaseDC(NULL
, hdc
);
908 // copy DIB data into the wxImage object
910 unsigned char *ptdata
= data
;
911 unsigned char *ptbits
= (unsigned char*) lpBits
;
912 for( i
=0; i
<height
; i
++ )
914 for( j
=0; j
<width
; j
++ )
916 *(ptdata
++) = *(ptbits
+2);
917 *(ptdata
++) = *(ptbits
+1);
918 *(ptdata
++) = *(ptbits
);
926 // similarly, set data according to the possible mask bitmap
927 if( GetMask() && GetMask()->GetMaskBitmap() )
929 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
930 // memory DC created, color set, data copied, and memory DC deleted
931 HDC memdc
= ::CreateCompatibleDC( hdc
);
932 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
933 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
934 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
938 for( i
=0; i
<height
; i
++ )
940 for( j
=0; j
<width
; j
++ )
942 // is this pixel transparent?
945 if ( (ptdata
[0] == MASK_RED
) &&
946 (ptdata
[1] == MASK_GREEN
) &&
947 (ptdata
[2] == MASK_BLUE
) )
949 // we have to fudge the colour a bit to prevent this
950 // pixel from appearing transparent
951 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
957 *(ptdata
++) = MASK_RED
;
958 *(ptdata
++) = MASK_GREEN
;
959 *(ptdata
++) = MASK_BLUE
;
966 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
967 image
.SetMask( TRUE
);
972 image
.SetMask( FALSE
);
975 // free allocated resources
976 ::ReleaseDC(NULL
, hdc
);
979 // Delete the DIB section
980 ::DeleteObject(hBitmapSection
);
984 // the colour used as transparent one in wxImage and the one it is replaced
985 // with when it really occurs in the bitmap
986 static const int MASK_RED
= 1;
987 static const int MASK_GREEN
= 2;
988 static const int MASK_BLUE
= 3;
989 static const int MASK_BLUE_REPLACEMENT
= 2;
993 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
995 // create an wxImage object
996 int width
= GetWidth();
997 int height
= GetHeight();
998 image
.Create( width
, height
);
999 unsigned char *data
= image
.GetData();
1002 wxFAIL_MSG( wxT("could not allocate data for image") );
1006 // calc the number of bytes per scanline and padding in the DIB
1007 int bytePerLine
= width
*3;
1008 int sizeDWORD
= sizeof( DWORD
);
1009 int lineBoundary
= bytePerLine
% sizeDWORD
;
1011 if( lineBoundary
> 0 )
1013 padding
= sizeDWORD
- lineBoundary
;
1014 bytePerLine
+= padding
;
1017 // create a DIB header
1018 int headersize
= sizeof(BITMAPINFOHEADER
);
1019 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1022 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1026 // Fill in the DIB header
1027 lpDIBh
->bmiHeader
.biSize
= headersize
;
1028 lpDIBh
->bmiHeader
.biWidth
= width
;
1029 lpDIBh
->bmiHeader
.biHeight
= -height
;
1030 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1031 lpDIBh
->bmiHeader
.biPlanes
= 1;
1032 lpDIBh
->bmiHeader
.biBitCount
= 24;
1033 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1034 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1035 // These seem not really needed for our purpose here.
1036 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1037 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1038 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1039 // memory for DIB data
1040 unsigned char *lpBits
;
1041 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1044 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1050 // copy data from the device-dependent bitmap to the DIB
1051 HDC hdc
= ::GetDC(NULL
);
1053 hbitmap
= (HBITMAP
) GetHBITMAP();
1054 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1056 // copy DIB data into the wxImage object
1058 unsigned char *ptdata
= data
;
1059 unsigned char *ptbits
= lpBits
;
1060 for( i
=0; i
<height
; i
++ )
1062 for( j
=0; j
<width
; j
++ )
1064 *(ptdata
++) = *(ptbits
+2);
1065 *(ptdata
++) = *(ptbits
+1);
1066 *(ptdata
++) = *(ptbits
);
1072 // similarly, set data according to the possible mask bitmap
1073 if( GetMask() && GetMask()->GetMaskBitmap() )
1075 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1076 // memory DC created, color set, data copied, and memory DC deleted
1077 HDC memdc
= ::CreateCompatibleDC( hdc
);
1078 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1079 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1080 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1081 ::DeleteDC( memdc
);
1084 for( i
=0; i
<height
; i
++ )
1086 for( j
=0; j
<width
; j
++ )
1088 // is this pixel transparent?
1091 if ( (ptdata
[0] == MASK_RED
) &&
1092 (ptdata
[1] == MASK_GREEN
) &&
1093 (ptdata
[2] == MASK_BLUE
) )
1095 // we have to fudge the colour a bit to prevent this
1096 // pixel from appearing transparent
1097 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1101 else // masked pixel
1103 *(ptdata
++) = MASK_RED
;
1104 *(ptdata
++) = MASK_GREEN
;
1105 *(ptdata
++) = MASK_BLUE
;
1112 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1113 image
.SetMask( TRUE
);
1117 image
.SetMask( FALSE
);
1119 // free allocated resources
1120 ::ReleaseDC(NULL
, hdc
);
1128 #endif // wxUSE_IMAGE
1130 // ----------------------------------------------------------------------------
1131 // loading and saving bitmaps
1132 // ----------------------------------------------------------------------------
1134 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1138 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1142 m_refData
= new wxBitmapRefData
;
1144 return handler
->LoadFile(this, filename
, type
, -1, -1);
1150 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1152 *this = wxBitmap(image
);
1157 #endif // wxUSE_IMAGE
1162 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1166 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1170 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1175 m_refData
= new wxBitmapRefData
;
1177 return handler
->Create(this, data
, type
, width
, height
, depth
);
1180 bool wxBitmap::SaveFile(const wxString
& filename
,
1182 const wxPalette
*palette
)
1184 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1188 return handler
->SaveFile(this, filename
, type
, palette
);
1193 // FIXME what about palette? shouldn't we use it?
1194 wxImage image
= ConvertToImage();
1197 return image
.SaveFile(filename
, type
);
1200 #endif // wxUSE_IMAGE
1205 // ----------------------------------------------------------------------------
1206 // sub bitmap extraction
1207 // ----------------------------------------------------------------------------
1209 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1211 wxCHECK_MSG( Ok() &&
1212 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1213 (rect
.x
+rect
.width
<= GetWidth()) &&
1214 (rect
.y
+rect
.height
<= GetHeight()),
1215 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1217 wxBitmap
ret( rect
.width
, rect
.height
);
1218 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1220 #ifndef __WXMICROWIN__
1221 // TODO: copy alpha channel data if any
1228 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1229 selectDst(dcDst
, GetHbitmapOf(ret
));
1231 if ( !selectSrc
|| !selectDst
)
1233 wxLogLastError(_T("SelectObjct(hBitmap)"));
1236 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1237 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1239 wxLogLastError(_T("BitBlt"));
1243 // copy mask if there is one
1246 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1248 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1249 selectDst(dcDst
, hbmpMask
);
1251 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1252 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1254 wxLogLastError(_T("BitBlt"));
1257 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1260 #endif // !__WXMICROWIN__
1265 // ----------------------------------------------------------------------------
1266 // wxBitmap accessors
1267 // ----------------------------------------------------------------------------
1270 wxPalette
* wxBitmap::GetPalette() const
1272 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1273 : (wxPalette
*) NULL
;
1277 wxMask
*wxBitmap::GetMask() const
1279 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1284 wxDC
*wxBitmap::GetSelectedInto() const
1286 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1291 #if WXWIN_COMPATIBILITY_2_4
1293 int wxBitmap::GetQuality() const
1298 #endif // WXWIN_COMPATIBILITY_2_4
1300 void wxBitmap::UseAlpha()
1302 if ( GetBitmapData() )
1303 GetBitmapData()->m_hasAlpha
= true;
1306 bool wxBitmap::HasAlpha() const
1308 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1317 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1319 if ( GetBitmapData() )
1320 GetBitmapData()->m_selectedInto
= dc
;
1327 void wxBitmap::SetPalette(const wxPalette
& palette
)
1331 GetBitmapData()->m_bitmapPalette
= palette
;
1334 #endif // wxUSE_PALETTE
1336 void wxBitmap::SetMask(wxMask
*mask
)
1340 GetBitmapData()->SetMask(mask
);
1343 #if WXWIN_COMPATIBILITY_2_4
1345 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1349 #endif // WXWIN_COMPATIBILITY_2_4
1351 // ----------------------------------------------------------------------------
1352 // raw bitmap access support
1353 // ----------------------------------------------------------------------------
1355 #ifdef wxHAVE_RAW_BITMAP
1356 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1361 // no bitmap, no data (raw or otherwise)
1365 // if we're already a DIB we can access our data directly, but if not we
1366 // need to convert this DDB to a DIB section and use it for raw access and
1367 // then convert it back
1369 if ( !GetBitmapData()->m_isDIB
)
1371 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1372 _T("GetRawData() may be called only once") );
1374 wxDIB
*dib
= new wxDIB(*this);
1382 // we'll free it in UngetRawData()
1383 GetBitmapData()->m_dib
= dib
;
1385 hDIB
= dib
->GetHandle();
1389 hDIB
= GetHbitmap();
1393 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1395 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1400 // check that the bitmap is in correct format
1401 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1403 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1408 // ok, store the relevant info in wxPixelDataBase
1409 const LONG h
= ds
.dsBm
.bmHeight
;
1411 data
.m_width
= ds
.dsBm
.bmWidth
;
1414 // remember that DIBs are stored in top to bottom order!
1415 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1416 data
.m_stride
= -bytesPerRow
;
1418 char *bits
= (char *)ds
.dsBm
.bmBits
;
1421 bits
+= (h
- 1)*bytesPerRow
;
1430 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1436 // the cast is ugly but we can't do without it and without making this
1437 // function template (and hence inline) unfortunately
1438 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1439 PixelData
& data
= (PixelData
&)dataBase
;
1443 // invalid data, don't crash -- but don't assert neither as we're
1444 // called automatically from wxPixelDataBase dtor and so there is no
1445 // way to prevent this from happening
1449 if ( GetBitmapData()->m_hasAlpha
)
1451 // AlphaBlend() wants to have premultiplied source alpha but
1452 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1454 PixelData::Iterator
p(data
);
1456 const int w
= data
.GetWidth();
1457 const int h
= data
.GetHeight();
1459 for ( int y
= 0; y
< h
; y
++ )
1461 PixelData::Iterator rowStart
= p
;
1463 for ( int x
= 0; x
< w
; x
++ )
1465 const unsigned alpha
= p
.Alpha();
1467 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1468 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1469 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1479 // if we're a DDB we need to convert DIB back to DDB now to make the
1480 // changes made via raw bitmap access effective
1481 if ( !GetBitmapData()->m_isDIB
)
1483 wxDIB
*dib
= GetBitmapData()->m_dib
;
1484 GetBitmapData()->m_dib
= NULL
;
1490 #endif // wxUSE_WXDIB
1492 #endif // #ifdef wxHAVE_RAW_BITMAP
1494 // ----------------------------------------------------------------------------
1496 // ----------------------------------------------------------------------------
1503 // Construct a mask from a bitmap and a colour indicating
1504 // the transparent area
1505 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1508 Create(bitmap
, colour
);
1511 // Construct a mask from a bitmap and a palette index indicating
1512 // the transparent area
1513 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1516 Create(bitmap
, paletteIndex
);
1519 // Construct a mask from a mono bitmap (copies the bitmap).
1520 wxMask::wxMask(const wxBitmap
& bitmap
)
1529 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1532 // Create a mask from a mono bitmap (copies the bitmap).
1533 bool wxMask::Create(const wxBitmap
& bitmap
)
1535 #ifndef __WXMICROWIN__
1536 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1537 _T("can't create mask from invalid or not monochrome bitmap") );
1541 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1545 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1550 HDC srcDC
= CreateCompatibleDC(0);
1551 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1552 HDC destDC
= CreateCompatibleDC(0);
1553 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1554 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1555 SelectObject(srcDC
, 0);
1557 SelectObject(destDC
, 0);
1565 // Create a mask from a bitmap and a palette index indicating
1566 // the transparent area
1567 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1571 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1576 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1578 unsigned char red
, green
, blue
;
1579 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1581 wxColour
transparentColour(red
, green
, blue
);
1582 return Create(bitmap
, transparentColour
);
1585 #endif // wxUSE_PALETTE
1590 // Create a mask from a bitmap and a colour indicating
1591 // the transparent area
1592 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1594 #ifndef __WXMICROWIN__
1595 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1599 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1603 int width
= bitmap
.GetWidth(),
1604 height
= bitmap
.GetHeight();
1606 // scan the bitmap for the transparent colour and set the corresponding
1607 // pixels in the mask to BLACK and the rest to WHITE
1608 COLORREF maskColour
= wxColourToPalRGB(colour
);
1609 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1611 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1612 HDC destDC
= ::CreateCompatibleDC(NULL
);
1613 if ( !srcDC
|| !destDC
)
1615 wxLogLastError(wxT("CreateCompatibleDC"));
1620 // SelectObject() will fail
1621 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1622 _T("bitmap can't be selected in another DC") );
1624 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1627 wxLogLastError(wxT("SelectObject"));
1632 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1635 wxLogLastError(wxT("SelectObject"));
1642 // this will create a monochrome bitmap with 0 points for the pixels
1643 // which have the same value as the background colour and 1 for the
1645 ::SetBkColor(srcDC
, maskColour
);
1646 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1649 ::SelectObject(srcDC
, hbmpSrcOld
);
1651 ::SelectObject(destDC
, hbmpDstOld
);
1655 #else // __WXMICROWIN__
1657 #endif // __WXMICROWIN__/!__WXMICROWIN__
1660 // ----------------------------------------------------------------------------
1662 // ----------------------------------------------------------------------------
1664 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1667 int width
, int height
, int depth
)
1669 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1671 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1674 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1675 const wxString
& name
,
1677 int width
, int height
)
1679 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1681 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1684 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1685 const wxString
& name
,
1688 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1690 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1693 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1694 void *WXUNUSED(data
),
1695 long WXUNUSED(type
),
1696 int WXUNUSED(width
),
1697 int WXUNUSED(height
),
1698 int WXUNUSED(depth
))
1703 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1704 const wxString
& WXUNUSED(name
),
1705 long WXUNUSED(type
),
1706 int WXUNUSED(desiredWidth
),
1707 int WXUNUSED(desiredHeight
))
1712 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1713 const wxString
& WXUNUSED(name
),
1715 const wxPalette
*WXUNUSED(palette
))
1720 // ----------------------------------------------------------------------------
1722 // ----------------------------------------------------------------------------
1724 #ifndef __WXMICROWIN__
1725 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1726 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1728 unsigned long i
, headerSize
;
1730 // Allocate space for a DIB header
1731 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1732 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1733 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1735 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1737 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1739 // Fill in the static parts of the DIB header
1740 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1741 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1742 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1743 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1745 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1746 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1747 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1748 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1749 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1752 // Initialize the DIB palette
1753 for (i
= 0; i
< 256; i
++) {
1754 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1755 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1756 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1757 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1760 *lpDIBHeader
= lpDIBheader
;
1765 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1771 // ----------------------------------------------------------------------------
1772 // global helper functions implemented here
1773 // ----------------------------------------------------------------------------
1775 // helper of wxBitmapToHICON/HCURSOR
1777 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1784 // we can't create an icon/cursor form nothing
1788 wxMask
*mask
= bmp
.GetMask();
1791 // we must have a mask for an icon, so even if it's probably incorrect,
1792 // do create it (grey is the "standard" transparent colour)
1793 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1797 wxZeroMemory(iconInfo
);
1798 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1801 iconInfo
.xHotspot
= hotSpotX
;
1802 iconInfo
.yHotspot
= hotSpotY
;
1805 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1806 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1808 // black out the transparent area to preserve background colour, because
1809 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1810 // the mask to the dest rect.
1812 MemoryHDC dcSrc
, dcDst
;
1813 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1814 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1816 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1817 dcSrc
, 0, 0, SRCAND
) )
1819 wxLogLastError(_T("BitBlt"));
1823 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1825 if ( !bmp
.GetMask() )
1827 // we created the mask, now delete it
1831 // delete the inverted mask bitmap we created as well
1832 ::DeleteObject(iconInfo
.hbmMask
);
1837 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1839 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1842 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1844 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1847 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1849 #ifndef __WXMICROWIN__
1850 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1852 // get width/height from the bitmap if not given
1856 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1861 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1862 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1863 if ( !hdcSrc
|| !hdcDst
)
1865 wxLogLastError(wxT("CreateCompatibleDC"));
1868 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1871 wxLogLastError(wxT("CreateBitmap"));
1874 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1875 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1876 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1880 wxLogLastError(wxT("BitBlt"));
1884 SelectObject(hdcSrc
,srcTmp
);
1885 SelectObject(hdcDst
,dstTmp
);