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
)
279 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
283 return CopyFromIconOrCursor(cursor
);
287 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
294 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
295 // to create a bitmap from icon there - but using this way we won't have
298 int width
= icon
.GetWidth(),
299 height
= icon
.GetHeight();
301 // copy the icon to the bitmap
303 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
304 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
305 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
307 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
309 ::SelectObject(hdc
, hbmpOld
);
312 wxBitmapRefData
*refData
= new wxBitmapRefData
;
315 refData
->m_width
= width
;
316 refData
->m_height
= height
;
317 refData
->m_depth
= wxDisplayDepth();
319 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
323 return CopyFromIconOrCursor(icon
);
324 #endif // Win16/Win32
328 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
330 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
332 HBITMAP hbitmap
= dib
.CreateDDB();
338 wxBitmapRefData
*refData
= new wxBitmapRefData
;
341 refData
->m_width
= dib
.GetWidth();
342 refData
->m_height
= dib
.GetHeight();
343 refData
->m_depth
= dib
.GetDepth();
345 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
348 wxPalette
*palette
= dib
.CreatePalette();
351 refData
->m_bitmapPalette
= *palette
;
355 #endif // wxUSE_PALETTE
361 wxBitmap::~wxBitmap()
365 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
369 #ifndef __WXMICROWIN__
370 wxBitmapRefData
*refData
= new wxBitmapRefData
;
373 refData
->m_width
= width
;
374 refData
->m_height
= height
;
375 refData
->m_depth
= depth
;
380 // we assume that it is in XBM format which is not quite the same as
381 // the format CreateBitmap() wants because the order of bytes in the
383 const size_t bytesPerLine
= (width
+ 7) / 8;
384 const size_t padding
= bytesPerLine
% 2;
385 const size_t len
= height
* ( padding
+ bytesPerLine
);
386 data
= (char *)malloc(len
);
387 const char *src
= bits
;
390 for ( int rows
= 0; rows
< height
; rows
++ )
392 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
394 unsigned char val
= *src
++;
395 unsigned char reversed
= 0;
397 for ( int bits
= 0; bits
< 8; bits
++)
400 reversed
|= (val
& 0x01);
412 // bits should already be in Windows standard format
413 data
= (char *)bits
; // const_cast is harmless
416 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
419 wxLogLastError(wxT("CreateBitmap"));
427 SetHBITMAP((WXHBITMAP
)hbmp
);
431 // Create from XPM data
432 #if wxUSE_IMAGE && wxUSE_XPM
433 bool wxBitmap::CreateFromXpm(const char **data
)
435 bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data
))
438 #if wxUSE_IMAGE && wxUSE_XPM
441 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
443 wxXPMDecoder decoder
;
444 wxImage img
= decoder
.ReadData(data
);
445 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
447 *this = wxBitmap(img
);
454 wxBitmap::wxBitmap(int w
, int h
, int d
)
458 (void)Create(w
, h
, d
);
461 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
465 (void)Create(w
, h
, dc
);
468 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
472 (void)Create(data
, type
, width
, height
, depth
);
475 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
479 LoadFile(filename
, (int)type
);
482 bool wxBitmap::Create(int width
, int height
, int depth
)
484 return DoCreate(width
, height
, depth
, 0);
487 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
489 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
491 return DoCreate(width
, height
, -1, dc
.GetHDC());
494 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
498 m_refData
= new wxBitmapRefData
;
500 GetBitmapData()->m_width
= w
;
501 GetBitmapData()->m_height
= h
;
506 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
510 // create DIBs without alpha channel by default
518 // don't delete the DIB section in dib object dtor
521 GetBitmapData()->m_isDIB
= TRUE
;
522 GetBitmapData()->m_depth
= d
;
528 #ifndef __WXMICROWIN__
531 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
534 wxLogLastError(wxT("CreateBitmap"));
538 #endif // !__WXMICROWIN__
541 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
544 wxLogLastError(wxT("CreateCompatibleBitmap"));
547 GetBitmapData()->m_depth
= wxDisplayDepth();
551 SetHBITMAP((WXHBITMAP
)hbmp
);
558 // ----------------------------------------------------------------------------
559 // wxImage to/from conversions for Microwin
560 // ----------------------------------------------------------------------------
562 // Microwin versions are so different from normal ones that it really doesn't
563 // make sense to use #ifdefs inside the function bodies
564 #ifdef __WXMICROWIN__
566 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
568 // Set this to 1 to experiment with mask code,
569 // which currently doesn't work
572 m_refData
= new wxBitmapRefData();
574 // Initial attempt at a simple-minded implementation.
575 // The bitmap will always be created at the screen depth,
576 // so the 'depth' argument is ignored.
578 HDC hScreenDC
= ::GetDC(NULL
);
579 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
581 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
582 HBITMAP hMaskBitmap
= NULL
;
583 HBITMAP hOldMaskBitmap
= NULL
;
585 unsigned char maskR
= 0;
586 unsigned char maskG
= 0;
587 unsigned char maskB
= 0;
589 // printf("Created bitmap %d\n", (int) hBitmap);
592 ::ReleaseDC(NULL
, hScreenDC
);
595 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
597 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
598 ::ReleaseDC(NULL
, hScreenDC
);
600 // created an mono-bitmap for the possible mask
601 bool hasMask
= image
.HasMask();
606 // FIXME: we should be able to pass bpp = 1, but
607 // GdBlit can't handle a different depth
609 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
611 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
613 maskR
= image
.GetMaskRed();
614 maskG
= image
.GetMaskGreen();
615 maskB
= image
.GetMaskBlue();
623 hScreenDC
= ::GetDC(NULL
);
624 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
625 ::ReleaseDC(NULL
, hScreenDC
);
627 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
635 for (i
= 0; i
< image
.GetWidth(); i
++)
637 for (j
= 0; j
< image
.GetHeight(); j
++)
639 unsigned char red
= image
.GetRed(i
, j
);
640 unsigned char green
= image
.GetGreen(i
, j
);
641 unsigned char blue
= image
.GetBlue(i
, j
);
643 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
647 // scan the bitmap for the transparent colour and set the corresponding
648 // pixels in the mask to BLACK and the rest to WHITE
649 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
650 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
652 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
657 ::SelectObject(hMemDC
, hOldBitmap
);
661 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
664 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
667 SetWidth(image
.GetWidth());
668 SetHeight(image
.GetHeight());
669 SetDepth(screenDepth
);
670 SetHBITMAP( (WXHBITMAP
) hBitmap
);
673 // Copy the palette from the source image
674 SetPalette(image
.GetPalette());
675 #endif // wxUSE_PALETTE
680 wxImage
wxBitmap::ConvertToImage() const
682 // Initial attempt at a simple-minded implementation.
683 // The bitmap will always be created at the screen depth,
684 // so the 'depth' argument is ignored.
685 // TODO: transparency (create a mask image)
689 wxFAIL_MSG( wxT("bitmap is invalid") );
695 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
697 // create an wxImage object
698 int width
= GetWidth();
699 int height
= GetHeight();
700 image
.Create( width
, height
);
701 unsigned char *data
= image
.GetData();
704 wxFAIL_MSG( wxT("could not allocate data for image") );
708 HDC hScreenDC
= ::GetDC(NULL
);
710 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
711 ::ReleaseDC(NULL
, hScreenDC
);
713 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
715 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
718 for (i
= 0; i
< GetWidth(); i
++)
720 for (j
= 0; j
< GetHeight(); j
++)
722 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
723 unsigned char red
= GetRValue(color
);
724 unsigned char green
= GetGValue(color
);
725 unsigned char blue
= GetBValue(color
);
727 image
.SetRGB(i
, j
, red
, green
, blue
);
731 ::SelectObject(hMemDC
, hOldBitmap
);
735 // Copy the palette from the source image
737 image
.SetPalette(* GetPalette());
738 #endif // wxUSE_PALETTE
743 #endif // __WXMICROWIN__
745 // ----------------------------------------------------------------------------
746 // wxImage to/from conversions
747 // ----------------------------------------------------------------------------
749 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
751 return CreateFromImage(image
, depth
, 0);
754 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
756 wxCHECK_MSG( dc
.Ok(), FALSE
,
757 _T("invalid HDC in wxBitmap::CreateFromImage()") );
759 return CreateFromImage(image
, -1, dc
.GetHDC());
762 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
765 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
769 // first convert the image to DIB
770 const int h
= image
.GetHeight();
771 const int w
= image
.GetWidth();
778 // store the bitmap parameters
779 wxBitmapRefData
*refData
= new wxBitmapRefData
;
780 refData
->m_width
= w
;
781 refData
->m_height
= h
;
782 refData
->m_hasAlpha
= image
.HasAlpha();
787 // next either store DIB as is or create a DDB from it
790 // are we going to use DIB?
792 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
793 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
795 // don't delete the DIB section in dib object dtor
796 hbitmap
= dib
.Detach();
798 refData
->m_isDIB
= TRUE
;
799 refData
->m_depth
= dib
.GetDepth();
801 else // we need to convert DIB to DDB
803 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
805 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
808 // validate this object
809 SetHBITMAP((WXHBITMAP
)hbitmap
);
811 // finally also set the mask if we have one
812 if ( image
.HasMask() )
814 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
815 image
.GetMaskGreen(),
816 image
.GetMaskBlue())));
821 // FIXME: wxWinCE doesn't support wxDIB yet
826 wxImage
wxBitmap::ConvertToImage() const
828 // FIXME: this is untested code for WinCE, and
829 // the mask is not yet handled.
831 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
834 // the colour used as transparent one in wxImage and the one it is replaced
835 // with when it really occurs in the bitmap
836 static const int MASK_RED
= 1;
837 static const int MASK_GREEN
= 2;
838 static const int MASK_BLUE
= 3;
839 static const int MASK_BLUE_REPLACEMENT
= 2;
843 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
845 // create an wxImage object
846 int width
= GetWidth();
847 int height
= GetHeight();
848 image
.Create( width
, height
);
849 unsigned char *data
= image
.GetData();
852 wxFAIL_MSG( wxT("could not allocate data for image") );
856 // calc the number of bytes per scanline and padding in the DIB
857 int bytePerLine
= width
*3;
858 int sizeDWORD
= sizeof( DWORD
);
859 int lineBoundary
= bytePerLine
% sizeDWORD
;
861 if( lineBoundary
> 0 )
863 padding
= sizeDWORD
- lineBoundary
;
864 bytePerLine
+= padding
;
867 // create a DIB header
868 int headersize
= sizeof(BITMAPINFOHEADER
);
869 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
872 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
876 // Fill in the DIB header
877 lpDIBh
->bmiHeader
.biSize
= headersize
;
878 lpDIBh
->bmiHeader
.biWidth
= width
;
879 lpDIBh
->bmiHeader
.biHeight
= -height
;
880 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
881 lpDIBh
->bmiHeader
.biPlanes
= 1;
882 lpDIBh
->bmiHeader
.biBitCount
= 24;
883 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
884 lpDIBh
->bmiHeader
.biClrUsed
= 0;
885 // These seem not really needed for our purpose here.
886 lpDIBh
->bmiHeader
.biClrImportant
= 0;
887 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
888 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
890 // memory for DIB data is allocated by CreateDIBSection
893 // copy data from the device-dependent bitmap to the DIB
894 HDC hdc
= ::GetDC(NULL
);
895 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
897 HBITMAP hBitmapSection
= ::CreateDIBSection( hdc
, lpDIBh
, DIB_RGB_COLORS
, & lpBits
, NULL
, 0 );
900 wxFAIL_MSG( wxT("could not create a DIB section") );
904 // Copy the image from the DDB to the DIBSection
905 // Need to copy the supplied bitmap onto the newly created DIBsection
906 HDC hMemDC
= CreateCompatibleDC(hdc
);
907 HDC hCopyDC
= CreateCompatibleDC(hdc
);
909 if (! hMemDC
|| ! hCopyDC
)
911 wxFAIL_MSG( wxT("unable to create compatible DCs") );
918 SelectPalette(hMemDC
, m_hPal
, FALSE
); RealizePalette(hMemDC
);
919 SelectPalette(hCopyDC
, m_hPal
, FALSE
); RealizePalette(hCopyDC
);
923 HBITMAP hOldMemBitmap
= (HBITMAP
) SelectObject(hMemDC
, hBitmap
);
924 HBITMAP hOldCopyBitmap
= (HBITMAP
) SelectObject(hCopyDC
, hBitmapSection
);
926 BitBlt(hCopyDC
, 0, 0, GetWidth(), GetHeight(), hMemDC
, 0, 0, SRCCOPY
);
928 SelectObject(hMemDC
, hOldMemBitmap
);
929 SelectObject(hCopyDC
, hOldCopyBitmap
);
936 HGDIOBJ hObj
= ::GetStockObject(DEFAULT_PALETTE
);
937 SelectObject(hMemDC
, hObj
);
938 SelectObject(hCopyDC
, hObj
);
942 ReleaseDC(NULL
, hdc
);
944 // copy DIB data into the wxImage object
946 unsigned char *ptdata
= data
;
947 unsigned char *ptbits
= (unsigned char*) lpBits
;
948 for( i
=0; i
<height
; i
++ )
950 for( j
=0; j
<width
; j
++ )
952 *(ptdata
++) = *(ptbits
+2);
953 *(ptdata
++) = *(ptbits
+1);
954 *(ptdata
++) = *(ptbits
);
962 // similarly, set data according to the possible mask bitmap
963 if( GetMask() && GetMask()->GetMaskBitmap() )
965 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
966 // memory DC created, color set, data copied, and memory DC deleted
967 HDC memdc
= ::CreateCompatibleDC( hdc
);
968 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
969 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
970 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
974 for( i
=0; i
<height
; i
++ )
976 for( j
=0; j
<width
; j
++ )
978 // is this pixel transparent?
981 if ( (ptdata
[0] == MASK_RED
) &&
982 (ptdata
[1] == MASK_GREEN
) &&
983 (ptdata
[2] == MASK_BLUE
) )
985 // we have to fudge the colour a bit to prevent this
986 // pixel from appearing transparent
987 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
993 *(ptdata
++) = MASK_RED
;
994 *(ptdata
++) = MASK_GREEN
;
995 *(ptdata
++) = MASK_BLUE
;
1002 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1003 image
.SetMask( TRUE
);
1008 image
.SetMask( FALSE
);
1011 // free allocated resources
1012 ::ReleaseDC(NULL
, hdc
);
1015 // Delete the DIB section
1016 ::DeleteObject(hBitmapSection
);
1020 // the colour used as transparent one in wxImage and the one it is replaced
1021 // with when it really occurs in the bitmap
1022 static const int MASK_RED
= 1;
1023 static const int MASK_GREEN
= 2;
1024 static const int MASK_BLUE
= 3;
1025 static const int MASK_BLUE_REPLACEMENT
= 2;
1029 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1031 // create an wxImage object
1032 int width
= GetWidth();
1033 int height
= GetHeight();
1034 image
.Create( width
, height
);
1035 unsigned char *data
= image
.GetData();
1038 wxFAIL_MSG( wxT("could not allocate data for image") );
1042 // calc the number of bytes per scanline and padding in the DIB
1043 int bytePerLine
= width
*3;
1044 int sizeDWORD
= sizeof( DWORD
);
1045 int lineBoundary
= bytePerLine
% sizeDWORD
;
1047 if( lineBoundary
> 0 )
1049 padding
= sizeDWORD
- lineBoundary
;
1050 bytePerLine
+= padding
;
1053 // create a DIB header
1054 int headersize
= sizeof(BITMAPINFOHEADER
);
1055 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1058 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1062 // Fill in the DIB header
1063 lpDIBh
->bmiHeader
.biSize
= headersize
;
1064 lpDIBh
->bmiHeader
.biWidth
= width
;
1065 lpDIBh
->bmiHeader
.biHeight
= -height
;
1066 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1067 lpDIBh
->bmiHeader
.biPlanes
= 1;
1068 lpDIBh
->bmiHeader
.biBitCount
= 24;
1069 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1070 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1071 // These seem not really needed for our purpose here.
1072 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1073 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1074 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1075 // memory for DIB data
1076 unsigned char *lpBits
;
1077 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1080 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1086 // copy data from the device-dependent bitmap to the DIB
1087 HDC hdc
= ::GetDC(NULL
);
1089 hbitmap
= (HBITMAP
) GetHBITMAP();
1090 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1092 // copy DIB data into the wxImage object
1094 unsigned char *ptdata
= data
;
1095 unsigned char *ptbits
= lpBits
;
1096 for( i
=0; i
<height
; i
++ )
1098 for( j
=0; j
<width
; j
++ )
1100 *(ptdata
++) = *(ptbits
+2);
1101 *(ptdata
++) = *(ptbits
+1);
1102 *(ptdata
++) = *(ptbits
);
1108 // similarly, set data according to the possible mask bitmap
1109 if( GetMask() && GetMask()->GetMaskBitmap() )
1111 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1112 // memory DC created, color set, data copied, and memory DC deleted
1113 HDC memdc
= ::CreateCompatibleDC( hdc
);
1114 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1115 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1116 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1117 ::DeleteDC( memdc
);
1120 for( i
=0; i
<height
; i
++ )
1122 for( j
=0; j
<width
; j
++ )
1124 // is this pixel transparent?
1127 if ( (ptdata
[0] == MASK_RED
) &&
1128 (ptdata
[1] == MASK_GREEN
) &&
1129 (ptdata
[2] == MASK_BLUE
) )
1131 // we have to fudge the colour a bit to prevent this
1132 // pixel from appearing transparent
1133 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1137 else // masked pixel
1139 *(ptdata
++) = MASK_RED
;
1140 *(ptdata
++) = MASK_GREEN
;
1141 *(ptdata
++) = MASK_BLUE
;
1148 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1149 image
.SetMask( TRUE
);
1153 image
.SetMask( FALSE
);
1155 // free allocated resources
1156 ::ReleaseDC(NULL
, hdc
);
1164 #endif // wxUSE_IMAGE
1166 // ----------------------------------------------------------------------------
1167 // loading and saving bitmaps
1168 // ----------------------------------------------------------------------------
1170 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1174 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1178 m_refData
= new wxBitmapRefData
;
1180 return handler
->LoadFile(this, filename
, type
, -1, -1);
1186 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1188 *this = wxBitmap(image
);
1193 #endif // wxUSE_IMAGE
1198 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1202 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1206 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1211 m_refData
= new wxBitmapRefData
;
1213 return handler
->Create(this, data
, type
, width
, height
, depth
);
1216 bool wxBitmap::SaveFile(const wxString
& filename
,
1218 const wxPalette
*palette
)
1220 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1224 return handler
->SaveFile(this, filename
, type
, palette
);
1229 // FIXME what about palette? shouldn't we use it?
1230 wxImage image
= ConvertToImage();
1233 return image
.SaveFile(filename
, type
);
1236 #endif // wxUSE_IMAGE
1241 // ----------------------------------------------------------------------------
1242 // sub bitmap extraction
1243 // ----------------------------------------------------------------------------
1245 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1247 wxCHECK_MSG( Ok() &&
1248 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1249 (rect
.x
+rect
.width
<= GetWidth()) &&
1250 (rect
.y
+rect
.height
<= GetHeight()),
1251 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1253 wxBitmap
ret( rect
.width
, rect
.height
);
1254 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1256 #ifndef __WXMICROWIN__
1257 // TODO: copy alpha channel data if any
1264 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1265 selectDst(dcDst
, GetHbitmapOf(ret
));
1267 if ( !selectSrc
|| !selectDst
)
1269 wxLogLastError(_T("SelectObjct(hBitmap)"));
1272 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1273 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1275 wxLogLastError(_T("BitBlt"));
1279 // copy mask if there is one
1282 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1284 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1285 selectDst(dcDst
, hbmpMask
);
1287 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1288 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1290 wxLogLastError(_T("BitBlt"));
1293 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1296 #endif // !__WXMICROWIN__
1301 // ----------------------------------------------------------------------------
1302 // wxBitmap accessors
1303 // ----------------------------------------------------------------------------
1306 wxPalette
* wxBitmap::GetPalette() const
1308 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1309 : (wxPalette
*) NULL
;
1313 wxMask
*wxBitmap::GetMask() const
1315 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1320 wxDC
*wxBitmap::GetSelectedInto() const
1322 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1327 #if WXWIN_COMPATIBILITY_2_4
1329 int wxBitmap::GetQuality() const
1334 #endif // WXWIN_COMPATIBILITY_2_4
1336 void wxBitmap::UseAlpha()
1338 if ( GetBitmapData() )
1339 GetBitmapData()->m_hasAlpha
= true;
1342 bool wxBitmap::HasAlpha() const
1344 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1347 // ----------------------------------------------------------------------------
1349 // ----------------------------------------------------------------------------
1353 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1355 if ( GetBitmapData() )
1356 GetBitmapData()->m_selectedInto
= dc
;
1363 void wxBitmap::SetPalette(const wxPalette
& palette
)
1367 GetBitmapData()->m_bitmapPalette
= palette
;
1370 #endif // wxUSE_PALETTE
1372 void wxBitmap::SetMask(wxMask
*mask
)
1376 GetBitmapData()->SetMask(mask
);
1379 #if WXWIN_COMPATIBILITY_2_4
1381 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1385 #endif // WXWIN_COMPATIBILITY_2_4
1387 // ----------------------------------------------------------------------------
1388 // raw bitmap access support
1389 // ----------------------------------------------------------------------------
1391 #ifdef wxHAVE_RAW_BITMAP
1392 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1397 // no bitmap, no data (raw or otherwise)
1401 // if we're already a DIB we can access our data directly, but if not we
1402 // need to convert this DDB to a DIB section and use it for raw access and
1403 // then convert it back
1405 if ( !GetBitmapData()->m_isDIB
)
1407 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1408 _T("GetRawData() may be called only once") );
1410 wxDIB
*dib
= new wxDIB(*this);
1418 // we'll free it in UngetRawData()
1419 GetBitmapData()->m_dib
= dib
;
1421 hDIB
= dib
->GetHandle();
1425 hDIB
= GetHbitmap();
1429 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1431 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1436 // check that the bitmap is in correct format
1437 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1439 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1444 // ok, store the relevant info in wxPixelDataBase
1445 const LONG h
= ds
.dsBm
.bmHeight
;
1447 data
.m_width
= ds
.dsBm
.bmWidth
;
1450 // remember that DIBs are stored in top to bottom order!
1451 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1452 data
.m_stride
= -bytesPerRow
;
1454 char *bits
= (char *)ds
.dsBm
.bmBits
;
1457 bits
+= (h
- 1)*bytesPerRow
;
1466 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1472 // the cast is ugly but we can't do without it and without making this
1473 // function template (and hence inline) unfortunately
1474 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1475 PixelData
& data
= (PixelData
&)dataBase
;
1479 // invalid data, don't crash -- but don't assert neither as we're
1480 // called automatically from wxPixelDataBase dtor and so there is no
1481 // way to prevent this from happening
1485 if ( GetBitmapData()->m_hasAlpha
)
1487 // AlphaBlend() wants to have premultiplied source alpha but
1488 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1490 PixelData::Iterator
p(data
);
1492 const int w
= data
.GetWidth();
1493 const int h
= data
.GetHeight();
1495 for ( int y
= 0; y
< h
; y
++ )
1497 PixelData::Iterator rowStart
= p
;
1499 for ( int x
= 0; x
< w
; x
++ )
1501 const unsigned alpha
= p
.Alpha();
1503 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1504 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1505 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1515 // if we're a DDB we need to convert DIB back to DDB now to make the
1516 // changes made via raw bitmap access effective
1517 if ( !GetBitmapData()->m_isDIB
)
1519 wxDIB
*dib
= GetBitmapData()->m_dib
;
1520 GetBitmapData()->m_dib
= NULL
;
1526 #endif // wxUSE_WXDIB
1528 #endif // #ifdef wxHAVE_RAW_BITMAP
1530 // ----------------------------------------------------------------------------
1532 // ----------------------------------------------------------------------------
1539 // Construct a mask from a bitmap and a colour indicating
1540 // the transparent area
1541 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1544 Create(bitmap
, colour
);
1547 // Construct a mask from a bitmap and a palette index indicating
1548 // the transparent area
1549 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1552 Create(bitmap
, paletteIndex
);
1555 // Construct a mask from a mono bitmap (copies the bitmap).
1556 wxMask::wxMask(const wxBitmap
& bitmap
)
1565 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1568 // Create a mask from a mono bitmap (copies the bitmap).
1569 bool wxMask::Create(const wxBitmap
& bitmap
)
1571 #ifndef __WXMICROWIN__
1572 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1573 _T("can't create mask from invalid or not monochrome bitmap") );
1577 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1581 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1586 HDC srcDC
= CreateCompatibleDC(0);
1587 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1588 HDC destDC
= CreateCompatibleDC(0);
1589 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1590 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1591 SelectObject(srcDC
, 0);
1593 SelectObject(destDC
, 0);
1601 // Create a mask from a bitmap and a palette index indicating
1602 // the transparent area
1603 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1607 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1612 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1614 unsigned char red
, green
, blue
;
1615 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1617 wxColour
transparentColour(red
, green
, blue
);
1618 return Create(bitmap
, transparentColour
);
1621 #endif // wxUSE_PALETTE
1626 // Create a mask from a bitmap and a colour indicating
1627 // the transparent area
1628 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1630 #ifndef __WXMICROWIN__
1631 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1635 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1639 int width
= bitmap
.GetWidth(),
1640 height
= bitmap
.GetHeight();
1642 // scan the bitmap for the transparent colour and set the corresponding
1643 // pixels in the mask to BLACK and the rest to WHITE
1644 COLORREF maskColour
= wxColourToPalRGB(colour
);
1645 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1647 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1648 HDC destDC
= ::CreateCompatibleDC(NULL
);
1649 if ( !srcDC
|| !destDC
)
1651 wxLogLastError(wxT("CreateCompatibleDC"));
1656 // SelectObject() will fail
1657 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1658 _T("bitmap can't be selected in another DC") );
1660 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1663 wxLogLastError(wxT("SelectObject"));
1668 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1671 wxLogLastError(wxT("SelectObject"));
1678 // this will create a monochrome bitmap with 0 points for the pixels
1679 // which have the same value as the background colour and 1 for the
1681 ::SetBkColor(srcDC
, maskColour
);
1682 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1685 ::SelectObject(srcDC
, hbmpSrcOld
);
1687 ::SelectObject(destDC
, hbmpDstOld
);
1691 #else // __WXMICROWIN__
1693 #endif // __WXMICROWIN__/!__WXMICROWIN__
1696 // ----------------------------------------------------------------------------
1698 // ----------------------------------------------------------------------------
1700 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1703 int width
, int height
, int depth
)
1705 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1707 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1710 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1711 const wxString
& name
,
1713 int width
, int height
)
1715 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1717 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1720 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1721 const wxString
& name
,
1724 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1726 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1729 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1730 void *WXUNUSED(data
),
1731 long WXUNUSED(type
),
1732 int WXUNUSED(width
),
1733 int WXUNUSED(height
),
1734 int WXUNUSED(depth
))
1739 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1740 const wxString
& WXUNUSED(name
),
1741 long WXUNUSED(type
),
1742 int WXUNUSED(desiredWidth
),
1743 int WXUNUSED(desiredHeight
))
1748 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1749 const wxString
& WXUNUSED(name
),
1751 const wxPalette
*WXUNUSED(palette
))
1756 // ----------------------------------------------------------------------------
1758 // ----------------------------------------------------------------------------
1760 #ifndef __WXMICROWIN__
1761 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1762 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1764 unsigned long i
, headerSize
;
1766 // Allocate space for a DIB header
1767 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1768 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1769 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1771 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1773 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1775 // Fill in the static parts of the DIB header
1776 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1777 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1778 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1779 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1781 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1782 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1783 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1784 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1785 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1788 // Initialize the DIB palette
1789 for (i
= 0; i
< 256; i
++) {
1790 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1791 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1792 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1793 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1796 *lpDIBHeader
= lpDIBheader
;
1801 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1807 // ----------------------------------------------------------------------------
1808 // global helper functions implemented here
1809 // ----------------------------------------------------------------------------
1811 // helper of wxBitmapToHICON/HCURSOR
1813 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1820 // we can't create an icon/cursor form nothing
1824 wxMask
*mask
= bmp
.GetMask();
1827 // we must have a mask for an icon, so even if it's probably incorrect,
1828 // do create it (grey is the "standard" transparent colour)
1829 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1833 wxZeroMemory(iconInfo
);
1834 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1837 iconInfo
.xHotspot
= hotSpotX
;
1838 iconInfo
.yHotspot
= hotSpotY
;
1841 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1842 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1844 // black out the transparent area to preserve background colour, because
1845 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1846 // the mask to the dest rect.
1848 MemoryHDC dcSrc
, dcDst
;
1849 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1850 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1852 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1853 dcSrc
, 0, 0, SRCAND
) )
1855 wxLogLastError(_T("BitBlt"));
1859 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1861 if ( !bmp
.GetMask() )
1863 // we created the mask, now delete it
1867 // delete the inverted mask bitmap we created as well
1868 ::DeleteObject(iconInfo
.hbmMask
);
1873 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1875 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1878 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1880 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1883 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1885 #ifndef __WXMICROWIN__
1886 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1888 // get width/height from the bitmap if not given
1892 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1897 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1898 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1899 if ( !hdcSrc
|| !hdcDst
)
1901 wxLogLastError(wxT("CreateCompatibleDC"));
1904 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1907 wxLogLastError(wxT("CreateBitmap"));
1910 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1911 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1912 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1916 wxLogLastError(wxT("BitBlt"));
1920 SelectObject(hdcSrc
,srcTmp
);
1921 SelectObject(hdcDst
,dstTmp
);