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()
370 wxBitmap::wxBitmap(const wxBitmap
& bitmap
)
376 wxBitmap::wxBitmap(const char **data
)
381 wxBitmap::wxBitmap(char **data
)
383 CreateFromXpm((const char **)data
);
388 wxBitmap::wxBitmap(const wxImage
& image
, int depth
)
390 (void)CreateFromImage(image
, depth
);
393 wxBitmap::wxBitmap(const wxImage
& image
, const wxDC
& dc
)
395 (void)CreateFromImage(image
, dc
);
398 #endif // wxUSE_IMAGE
400 wxBitmap::wxBitmap(const wxIcon
& icon
)
406 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
410 #ifndef __WXMICROWIN__
411 wxBitmapRefData
*refData
= new wxBitmapRefData
;
414 refData
->m_width
= width
;
415 refData
->m_height
= height
;
416 refData
->m_depth
= depth
;
421 // we assume that it is in XBM format which is not quite the same as
422 // the format CreateBitmap() wants because the order of bytes in the
424 const size_t bytesPerLine
= (width
+ 7) / 8;
425 const size_t padding
= bytesPerLine
% 2;
426 const size_t len
= height
* ( padding
+ bytesPerLine
);
427 data
= (char *)malloc(len
);
428 const char *src
= bits
;
431 for ( int rows
= 0; rows
< height
; rows
++ )
433 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
435 unsigned char val
= *src
++;
436 unsigned char reversed
= 0;
438 for ( int bits
= 0; bits
< 8; bits
++)
441 reversed
|= (val
& 0x01);
453 // bits should already be in Windows standard format
454 data
= (char *)bits
; // const_cast is harmless
457 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
460 wxLogLastError(wxT("CreateBitmap"));
468 SetHBITMAP((WXHBITMAP
)hbmp
);
472 // Create from XPM data
473 bool wxBitmap::CreateFromXpm(const char **data
)
475 #if wxUSE_IMAGE && wxUSE_XPM
478 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
480 wxXPMDecoder decoder
;
481 wxImage img
= decoder
.ReadData(data
);
482 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
484 *this = wxBitmap(img
);
491 wxBitmap::wxBitmap(int w
, int h
, int d
)
495 (void)Create(w
, h
, d
);
498 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
502 (void)Create(w
, h
, dc
);
505 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
509 (void)Create(data
, type
, width
, height
, depth
);
512 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
516 LoadFile(filename
, (int)type
);
519 bool wxBitmap::Create(int width
, int height
, int depth
)
521 return DoCreate(width
, height
, depth
, 0);
524 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
526 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
528 return DoCreate(width
, height
, -1, dc
.GetHDC());
531 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
535 m_refData
= new wxBitmapRefData
;
537 GetBitmapData()->m_width
= w
;
538 GetBitmapData()->m_height
= h
;
543 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
547 // create DIBs without alpha channel by default
555 // don't delete the DIB section in dib object dtor
558 GetBitmapData()->m_isDIB
= TRUE
;
559 GetBitmapData()->m_depth
= d
;
565 d
= wxDisplayDepth();
567 GetBitmapData()->m_depth
= d
;
569 #ifndef __WXMICROWIN__
572 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
575 wxLogLastError(wxT("CreateBitmap"));
579 #endif // !__WXMICROWIN__
582 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
585 wxLogLastError(wxT("CreateCompatibleBitmap"));
588 GetBitmapData()->m_depth
= wxDisplayDepth();
592 SetHBITMAP((WXHBITMAP
)hbmp
);
599 // ----------------------------------------------------------------------------
600 // wxImage to/from conversions for Microwin
601 // ----------------------------------------------------------------------------
603 // Microwin versions are so different from normal ones that it really doesn't
604 // make sense to use #ifdefs inside the function bodies
605 #ifdef __WXMICROWIN__
607 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
609 // Set this to 1 to experiment with mask code,
610 // which currently doesn't work
613 m_refData
= new wxBitmapRefData();
615 // Initial attempt at a simple-minded implementation.
616 // The bitmap will always be created at the screen depth,
617 // so the 'depth' argument is ignored.
619 HDC hScreenDC
= ::GetDC(NULL
);
620 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
622 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
623 HBITMAP hMaskBitmap
= NULL
;
624 HBITMAP hOldMaskBitmap
= NULL
;
626 unsigned char maskR
= 0;
627 unsigned char maskG
= 0;
628 unsigned char maskB
= 0;
630 // printf("Created bitmap %d\n", (int) hBitmap);
633 ::ReleaseDC(NULL
, hScreenDC
);
636 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
638 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
639 ::ReleaseDC(NULL
, hScreenDC
);
641 // created an mono-bitmap for the possible mask
642 bool hasMask
= image
.HasMask();
647 // FIXME: we should be able to pass bpp = 1, but
648 // GdBlit can't handle a different depth
650 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
652 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
654 maskR
= image
.GetMaskRed();
655 maskG
= image
.GetMaskGreen();
656 maskB
= image
.GetMaskBlue();
664 hScreenDC
= ::GetDC(NULL
);
665 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
666 ::ReleaseDC(NULL
, hScreenDC
);
668 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
676 for (i
= 0; i
< image
.GetWidth(); i
++)
678 for (j
= 0; j
< image
.GetHeight(); j
++)
680 unsigned char red
= image
.GetRed(i
, j
);
681 unsigned char green
= image
.GetGreen(i
, j
);
682 unsigned char blue
= image
.GetBlue(i
, j
);
684 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
688 // scan the bitmap for the transparent colour and set the corresponding
689 // pixels in the mask to BLACK and the rest to WHITE
690 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
691 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
693 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
698 ::SelectObject(hMemDC
, hOldBitmap
);
702 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
705 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
708 SetWidth(image
.GetWidth());
709 SetHeight(image
.GetHeight());
710 SetDepth(screenDepth
);
711 SetHBITMAP( (WXHBITMAP
) hBitmap
);
714 // Copy the palette from the source image
715 SetPalette(image
.GetPalette());
716 #endif // wxUSE_PALETTE
721 wxImage
wxBitmap::ConvertToImage() const
723 // Initial attempt at a simple-minded implementation.
724 // The bitmap will always be created at the screen depth,
725 // so the 'depth' argument is ignored.
726 // TODO: transparency (create a mask image)
730 wxFAIL_MSG( wxT("bitmap is invalid") );
736 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
738 // create an wxImage object
739 int width
= GetWidth();
740 int height
= GetHeight();
741 image
.Create( width
, height
);
742 unsigned char *data
= image
.GetData();
745 wxFAIL_MSG( wxT("could not allocate data for image") );
749 HDC hScreenDC
= ::GetDC(NULL
);
751 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
752 ::ReleaseDC(NULL
, hScreenDC
);
754 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
756 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
759 for (i
= 0; i
< GetWidth(); i
++)
761 for (j
= 0; j
< GetHeight(); j
++)
763 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
764 unsigned char red
= GetRValue(color
);
765 unsigned char green
= GetGValue(color
);
766 unsigned char blue
= GetBValue(color
);
768 image
.SetRGB(i
, j
, red
, green
, blue
);
772 ::SelectObject(hMemDC
, hOldBitmap
);
776 // Copy the palette from the source image
778 image
.SetPalette(* GetPalette());
779 #endif // wxUSE_PALETTE
784 #endif // __WXMICROWIN__
786 // ----------------------------------------------------------------------------
787 // wxImage to/from conversions
788 // ----------------------------------------------------------------------------
790 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
792 return CreateFromImage(image
, depth
, 0);
795 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
797 wxCHECK_MSG( dc
.Ok(), FALSE
,
798 _T("invalid HDC in wxBitmap::CreateFromImage()") );
800 return CreateFromImage(image
, -1, dc
.GetHDC());
803 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
806 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
810 // first convert the image to DIB
811 const int h
= image
.GetHeight();
812 const int w
= image
.GetWidth();
819 // store the bitmap parameters
820 wxBitmapRefData
*refData
= new wxBitmapRefData
;
821 refData
->m_width
= w
;
822 refData
->m_height
= h
;
823 refData
->m_hasAlpha
= image
.HasAlpha();
828 // next either store DIB as is or create a DDB from it
831 // are we going to use DIB?
833 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
834 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
836 // don't delete the DIB section in dib object dtor
837 hbitmap
= dib
.Detach();
839 refData
->m_isDIB
= TRUE
;
840 refData
->m_depth
= dib
.GetDepth();
842 else // we need to convert DIB to DDB
844 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
846 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
849 // validate this object
850 SetHBITMAP((WXHBITMAP
)hbitmap
);
852 // finally also set the mask if we have one
853 if ( image
.HasMask() )
855 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
856 image
.GetMaskGreen(),
857 image
.GetMaskBlue())));
862 // FIXME: wxWinCE doesn't support wxDIB yet
867 wxImage
wxBitmap::ConvertToImage() const
869 // FIXME: this is untested code for WinCE, and
870 // the mask is not yet handled.
872 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
875 // the colour used as transparent one in wxImage and the one it is replaced
876 // with when it really occurs in the bitmap
877 static const int MASK_RED
= 1;
878 static const int MASK_GREEN
= 2;
879 static const int MASK_BLUE
= 3;
880 static const int MASK_BLUE_REPLACEMENT
= 2;
884 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
886 // create an wxImage object
887 int width
= GetWidth();
888 int height
= GetHeight();
889 image
.Create( width
, height
);
890 unsigned char *data
= image
.GetData();
893 wxFAIL_MSG( wxT("could not allocate data for image") );
897 // calc the number of bytes per scanline and padding in the DIB
898 int bytePerLine
= width
*3;
899 int sizeDWORD
= sizeof( DWORD
);
900 int lineBoundary
= bytePerLine
% sizeDWORD
;
902 if( lineBoundary
> 0 )
904 padding
= sizeDWORD
- lineBoundary
;
905 bytePerLine
+= padding
;
908 // create a DIB header
909 int headersize
= sizeof(BITMAPINFOHEADER
);
910 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
913 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
917 // Fill in the DIB header
918 lpDIBh
->bmiHeader
.biSize
= headersize
;
919 lpDIBh
->bmiHeader
.biWidth
= width
;
920 lpDIBh
->bmiHeader
.biHeight
= -height
;
921 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
922 lpDIBh
->bmiHeader
.biPlanes
= 1;
923 lpDIBh
->bmiHeader
.biBitCount
= 24;
924 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
925 lpDIBh
->bmiHeader
.biClrUsed
= 0;
926 // These seem not really needed for our purpose here.
927 lpDIBh
->bmiHeader
.biClrImportant
= 0;
928 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
929 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
931 // memory for DIB data is allocated by CreateDIBSection
934 // copy data from the device-dependent bitmap to the DIB
935 HDC hdc
= ::GetDC(NULL
);
936 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
938 HBITMAP hBitmapSection
= ::CreateDIBSection( hdc
, lpDIBh
, DIB_RGB_COLORS
, & lpBits
, NULL
, 0 );
941 wxFAIL_MSG( wxT("could not create a DIB section") );
945 // Copy the image from the DDB to the DIBSection
946 // Need to copy the supplied bitmap onto the newly created DIBsection
947 HDC hMemDC
= CreateCompatibleDC(hdc
);
948 HDC hCopyDC
= CreateCompatibleDC(hdc
);
950 if (! hMemDC
|| ! hCopyDC
)
952 wxFAIL_MSG( wxT("unable to create compatible DCs") );
959 SelectPalette(hMemDC
, m_hPal
, FALSE
); RealizePalette(hMemDC
);
960 SelectPalette(hCopyDC
, m_hPal
, FALSE
); RealizePalette(hCopyDC
);
964 HBITMAP hOldMemBitmap
= (HBITMAP
) SelectObject(hMemDC
, hBitmap
);
965 HBITMAP hOldCopyBitmap
= (HBITMAP
) SelectObject(hCopyDC
, hBitmapSection
);
967 BitBlt(hCopyDC
, 0, 0, GetWidth(), GetHeight(), hMemDC
, 0, 0, SRCCOPY
);
969 SelectObject(hMemDC
, hOldMemBitmap
);
970 SelectObject(hCopyDC
, hOldCopyBitmap
);
977 HGDIOBJ hObj
= ::GetStockObject(DEFAULT_PALETTE
);
978 SelectObject(hMemDC
, hObj
);
979 SelectObject(hCopyDC
, hObj
);
983 ReleaseDC(NULL
, hdc
);
985 // copy DIB data into the wxImage object
987 unsigned char *ptdata
= data
;
988 unsigned char *ptbits
= (unsigned char*) lpBits
;
989 for( i
=0; i
<height
; i
++ )
991 for( j
=0; j
<width
; j
++ )
993 *(ptdata
++) = *(ptbits
+2);
994 *(ptdata
++) = *(ptbits
+1);
995 *(ptdata
++) = *(ptbits
);
1003 // similarly, set data according to the possible mask bitmap
1004 if( GetMask() && GetMask()->GetMaskBitmap() )
1006 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1007 // memory DC created, color set, data copied, and memory DC deleted
1008 HDC memdc
= ::CreateCompatibleDC( hdc
);
1009 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1010 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1011 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1012 ::DeleteDC( memdc
);
1015 for( i
=0; i
<height
; i
++ )
1017 for( j
=0; j
<width
; j
++ )
1019 // is this pixel transparent?
1022 if ( (ptdata
[0] == MASK_RED
) &&
1023 (ptdata
[1] == MASK_GREEN
) &&
1024 (ptdata
[2] == MASK_BLUE
) )
1026 // we have to fudge the colour a bit to prevent this
1027 // pixel from appearing transparent
1028 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1032 else // masked pixel
1034 *(ptdata
++) = MASK_RED
;
1035 *(ptdata
++) = MASK_GREEN
;
1036 *(ptdata
++) = MASK_BLUE
;
1043 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1044 image
.SetMask( TRUE
);
1049 image
.SetMask( FALSE
);
1052 // free allocated resources
1053 ::ReleaseDC(NULL
, hdc
);
1056 // Delete the DIB section
1057 ::DeleteObject(hBitmapSection
);
1061 // the colour used as transparent one in wxImage and the one it is replaced
1062 // with when it really occurs in the bitmap
1063 static const int MASK_RED
= 1;
1064 static const int MASK_GREEN
= 2;
1065 static const int MASK_BLUE
= 3;
1066 static const int MASK_BLUE_REPLACEMENT
= 2;
1070 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1072 // create an wxImage object
1073 int width
= GetWidth();
1074 int height
= GetHeight();
1075 image
.Create( width
, height
);
1076 unsigned char *data
= image
.GetData();
1079 wxFAIL_MSG( wxT("could not allocate data for image") );
1083 // calc the number of bytes per scanline and padding in the DIB
1084 int bytePerLine
= width
*3;
1085 int sizeDWORD
= sizeof( DWORD
);
1086 int lineBoundary
= bytePerLine
% sizeDWORD
;
1088 if( lineBoundary
> 0 )
1090 padding
= sizeDWORD
- lineBoundary
;
1091 bytePerLine
+= padding
;
1094 // create a DIB header
1095 int headersize
= sizeof(BITMAPINFOHEADER
);
1096 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1099 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1103 // Fill in the DIB header
1104 lpDIBh
->bmiHeader
.biSize
= headersize
;
1105 lpDIBh
->bmiHeader
.biWidth
= width
;
1106 lpDIBh
->bmiHeader
.biHeight
= -height
;
1107 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1108 lpDIBh
->bmiHeader
.biPlanes
= 1;
1109 lpDIBh
->bmiHeader
.biBitCount
= 24;
1110 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1111 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1112 // These seem not really needed for our purpose here.
1113 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1114 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1115 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1116 // memory for DIB data
1117 unsigned char *lpBits
;
1118 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1121 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1127 // copy data from the device-dependent bitmap to the DIB
1128 HDC hdc
= ::GetDC(NULL
);
1130 hbitmap
= (HBITMAP
) GetHBITMAP();
1131 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1133 // copy DIB data into the wxImage object
1135 unsigned char *ptdata
= data
;
1136 unsigned char *ptbits
= lpBits
;
1137 for( i
=0; i
<height
; i
++ )
1139 for( j
=0; j
<width
; j
++ )
1141 *(ptdata
++) = *(ptbits
+2);
1142 *(ptdata
++) = *(ptbits
+1);
1143 *(ptdata
++) = *(ptbits
);
1149 // similarly, set data according to the possible mask bitmap
1150 if( GetMask() && GetMask()->GetMaskBitmap() )
1152 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1153 // memory DC created, color set, data copied, and memory DC deleted
1154 HDC memdc
= ::CreateCompatibleDC( hdc
);
1155 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1156 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1157 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1158 ::DeleteDC( memdc
);
1161 for( i
=0; i
<height
; i
++ )
1163 for( j
=0; j
<width
; j
++ )
1165 // is this pixel transparent?
1168 if ( (ptdata
[0] == MASK_RED
) &&
1169 (ptdata
[1] == MASK_GREEN
) &&
1170 (ptdata
[2] == MASK_BLUE
) )
1172 // we have to fudge the colour a bit to prevent this
1173 // pixel from appearing transparent
1174 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1178 else // masked pixel
1180 *(ptdata
++) = MASK_RED
;
1181 *(ptdata
++) = MASK_GREEN
;
1182 *(ptdata
++) = MASK_BLUE
;
1189 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1190 image
.SetMask( TRUE
);
1194 image
.SetMask( FALSE
);
1196 // free allocated resources
1197 ::ReleaseDC(NULL
, hdc
);
1205 #endif // wxUSE_IMAGE
1207 // ----------------------------------------------------------------------------
1208 // loading and saving bitmaps
1209 // ----------------------------------------------------------------------------
1211 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1215 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1219 m_refData
= new wxBitmapRefData
;
1221 return handler
->LoadFile(this, filename
, type
, -1, -1);
1227 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1229 *this = wxBitmap(image
);
1234 #endif // wxUSE_IMAGE
1239 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1243 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1247 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1252 m_refData
= new wxBitmapRefData
;
1254 return handler
->Create(this, data
, type
, width
, height
, depth
);
1257 bool wxBitmap::SaveFile(const wxString
& filename
,
1259 const wxPalette
*palette
)
1261 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1265 return handler
->SaveFile(this, filename
, type
, palette
);
1270 // FIXME what about palette? shouldn't we use it?
1271 wxImage image
= ConvertToImage();
1274 return image
.SaveFile(filename
, type
);
1277 #endif // wxUSE_IMAGE
1282 // ----------------------------------------------------------------------------
1283 // sub bitmap extraction
1284 // ----------------------------------------------------------------------------
1286 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1288 wxCHECK_MSG( Ok() &&
1289 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1290 (rect
.x
+rect
.width
<= GetWidth()) &&
1291 (rect
.y
+rect
.height
<= GetHeight()),
1292 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1294 wxBitmap
ret( rect
.width
, rect
.height
);
1295 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1297 #ifndef __WXMICROWIN__
1298 // TODO: copy alpha channel data if any
1305 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1306 selectDst(dcDst
, GetHbitmapOf(ret
));
1308 if ( !selectSrc
|| !selectDst
)
1310 wxLogLastError(_T("SelectObjct(hBitmap)"));
1313 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1314 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1316 wxLogLastError(_T("BitBlt"));
1320 // copy mask if there is one
1323 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1325 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1326 selectDst(dcDst
, hbmpMask
);
1328 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1329 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1331 wxLogLastError(_T("BitBlt"));
1334 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1337 #endif // !__WXMICROWIN__
1342 // ----------------------------------------------------------------------------
1343 // wxBitmap accessors
1344 // ----------------------------------------------------------------------------
1347 wxPalette
* wxBitmap::GetPalette() const
1349 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1350 : (wxPalette
*) NULL
;
1354 wxMask
*wxBitmap::GetMask() const
1356 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1361 wxDC
*wxBitmap::GetSelectedInto() const
1363 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1368 #if WXWIN_COMPATIBILITY_2_4
1370 int wxBitmap::GetQuality() const
1375 #endif // WXWIN_COMPATIBILITY_2_4
1377 void wxBitmap::UseAlpha()
1379 if ( GetBitmapData() )
1380 GetBitmapData()->m_hasAlpha
= true;
1383 bool wxBitmap::HasAlpha() const
1385 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1388 // ----------------------------------------------------------------------------
1390 // ----------------------------------------------------------------------------
1394 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1396 if ( GetBitmapData() )
1397 GetBitmapData()->m_selectedInto
= dc
;
1404 void wxBitmap::SetPalette(const wxPalette
& palette
)
1408 GetBitmapData()->m_bitmapPalette
= palette
;
1411 #endif // wxUSE_PALETTE
1413 void wxBitmap::SetMask(wxMask
*mask
)
1417 GetBitmapData()->SetMask(mask
);
1420 #if WXWIN_COMPATIBILITY_2_4
1422 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1426 #endif // WXWIN_COMPATIBILITY_2_4
1428 // ----------------------------------------------------------------------------
1429 // raw bitmap access support
1430 // ----------------------------------------------------------------------------
1432 #ifdef wxHAVE_RAW_BITMAP
1433 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1438 // no bitmap, no data (raw or otherwise)
1442 // if we're already a DIB we can access our data directly, but if not we
1443 // need to convert this DDB to a DIB section and use it for raw access and
1444 // then convert it back
1446 if ( !GetBitmapData()->m_isDIB
)
1448 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1449 _T("GetRawData() may be called only once") );
1451 wxDIB
*dib
= new wxDIB(*this);
1459 // we'll free it in UngetRawData()
1460 GetBitmapData()->m_dib
= dib
;
1462 hDIB
= dib
->GetHandle();
1466 hDIB
= GetHbitmap();
1470 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1472 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1477 // check that the bitmap is in correct format
1478 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1480 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1485 // ok, store the relevant info in wxPixelDataBase
1486 const LONG h
= ds
.dsBm
.bmHeight
;
1488 data
.m_width
= ds
.dsBm
.bmWidth
;
1491 // remember that DIBs are stored in top to bottom order!
1492 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1493 data
.m_stride
= -bytesPerRow
;
1495 char *bits
= (char *)ds
.dsBm
.bmBits
;
1498 bits
+= (h
- 1)*bytesPerRow
;
1507 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1513 // the cast is ugly but we can't do without it and without making this
1514 // function template (and hence inline) unfortunately
1515 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1516 PixelData
& data
= (PixelData
&)dataBase
;
1520 // invalid data, don't crash -- but don't assert neither as we're
1521 // called automatically from wxPixelDataBase dtor and so there is no
1522 // way to prevent this from happening
1526 if ( GetBitmapData()->m_hasAlpha
)
1528 // AlphaBlend() wants to have premultiplied source alpha but
1529 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1531 PixelData::Iterator
p(data
);
1533 const int w
= data
.GetWidth();
1534 const int h
= data
.GetHeight();
1536 for ( int y
= 0; y
< h
; y
++ )
1538 PixelData::Iterator rowStart
= p
;
1540 for ( int x
= 0; x
< w
; x
++ )
1542 const unsigned alpha
= p
.Alpha();
1544 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1545 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1546 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1556 // if we're a DDB we need to convert DIB back to DDB now to make the
1557 // changes made via raw bitmap access effective
1558 if ( !GetBitmapData()->m_isDIB
)
1560 wxDIB
*dib
= GetBitmapData()->m_dib
;
1561 GetBitmapData()->m_dib
= NULL
;
1567 #endif // wxUSE_WXDIB
1569 #endif // #ifdef wxHAVE_RAW_BITMAP
1571 // ----------------------------------------------------------------------------
1573 // ----------------------------------------------------------------------------
1580 // Construct a mask from a bitmap and a colour indicating
1581 // the transparent area
1582 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1585 Create(bitmap
, colour
);
1588 // Construct a mask from a bitmap and a palette index indicating
1589 // the transparent area
1590 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1593 Create(bitmap
, paletteIndex
);
1596 // Construct a mask from a mono bitmap (copies the bitmap).
1597 wxMask::wxMask(const wxBitmap
& bitmap
)
1606 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1609 // Create a mask from a mono bitmap (copies the bitmap).
1610 bool wxMask::Create(const wxBitmap
& bitmap
)
1612 #ifndef __WXMICROWIN__
1613 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1614 _T("can't create mask from invalid or not monochrome bitmap") );
1618 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1622 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1627 HDC srcDC
= CreateCompatibleDC(0);
1628 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1629 HDC destDC
= CreateCompatibleDC(0);
1630 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1631 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1632 SelectObject(srcDC
, 0);
1634 SelectObject(destDC
, 0);
1642 // Create a mask from a bitmap and a palette index indicating
1643 // the transparent area
1644 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1648 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1653 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1655 unsigned char red
, green
, blue
;
1656 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1658 wxColour
transparentColour(red
, green
, blue
);
1659 return Create(bitmap
, transparentColour
);
1662 #endif // wxUSE_PALETTE
1667 // Create a mask from a bitmap and a colour indicating
1668 // the transparent area
1669 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1671 #ifndef __WXMICROWIN__
1672 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1676 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1680 int width
= bitmap
.GetWidth(),
1681 height
= bitmap
.GetHeight();
1683 // scan the bitmap for the transparent colour and set the corresponding
1684 // pixels in the mask to BLACK and the rest to WHITE
1685 COLORREF maskColour
= wxColourToPalRGB(colour
);
1686 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1688 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1689 HDC destDC
= ::CreateCompatibleDC(NULL
);
1690 if ( !srcDC
|| !destDC
)
1692 wxLogLastError(wxT("CreateCompatibleDC"));
1697 // SelectObject() will fail
1698 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1699 _T("bitmap can't be selected in another DC") );
1701 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1704 wxLogLastError(wxT("SelectObject"));
1709 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1712 wxLogLastError(wxT("SelectObject"));
1719 // this will create a monochrome bitmap with 0 points for the pixels
1720 // which have the same value as the background colour and 1 for the
1722 ::SetBkColor(srcDC
, maskColour
);
1723 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1726 ::SelectObject(srcDC
, hbmpSrcOld
);
1728 ::SelectObject(destDC
, hbmpDstOld
);
1732 #else // __WXMICROWIN__
1734 #endif // __WXMICROWIN__/!__WXMICROWIN__
1737 // ----------------------------------------------------------------------------
1739 // ----------------------------------------------------------------------------
1741 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1744 int width
, int height
, int depth
)
1746 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1748 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1751 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1752 const wxString
& name
,
1754 int width
, int height
)
1756 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1758 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1761 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1762 const wxString
& name
,
1765 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1767 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1770 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1771 void *WXUNUSED(data
),
1772 long WXUNUSED(type
),
1773 int WXUNUSED(width
),
1774 int WXUNUSED(height
),
1775 int WXUNUSED(depth
))
1780 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1781 const wxString
& WXUNUSED(name
),
1782 long WXUNUSED(type
),
1783 int WXUNUSED(desiredWidth
),
1784 int WXUNUSED(desiredHeight
))
1789 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1790 const wxString
& WXUNUSED(name
),
1792 const wxPalette
*WXUNUSED(palette
))
1797 // ----------------------------------------------------------------------------
1799 // ----------------------------------------------------------------------------
1801 #ifndef __WXMICROWIN__
1802 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1803 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1805 unsigned long i
, headerSize
;
1807 // Allocate space for a DIB header
1808 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1809 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1810 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1812 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1814 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1816 // Fill in the static parts of the DIB header
1817 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1818 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1819 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1820 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1822 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1823 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1824 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1825 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1826 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1829 // Initialize the DIB palette
1830 for (i
= 0; i
< 256; i
++) {
1831 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1832 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1833 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1834 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1837 *lpDIBHeader
= lpDIBheader
;
1842 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1848 // ----------------------------------------------------------------------------
1849 // global helper functions implemented here
1850 // ----------------------------------------------------------------------------
1852 // helper of wxBitmapToHICON/HCURSOR
1854 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1861 // we can't create an icon/cursor form nothing
1865 wxMask
*mask
= bmp
.GetMask();
1868 // we must have a mask for an icon, so even if it's probably incorrect,
1869 // do create it (grey is the "standard" transparent colour)
1870 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1874 wxZeroMemory(iconInfo
);
1875 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1878 iconInfo
.xHotspot
= hotSpotX
;
1879 iconInfo
.yHotspot
= hotSpotY
;
1882 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1883 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1885 // black out the transparent area to preserve background colour, because
1886 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1887 // the mask to the dest rect.
1889 MemoryHDC dcSrc
, dcDst
;
1890 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1891 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1893 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1894 dcSrc
, 0, 0, SRCAND
) )
1896 wxLogLastError(_T("BitBlt"));
1900 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1902 if ( !bmp
.GetMask() )
1904 // we created the mask, now delete it
1908 // delete the inverted mask bitmap we created as well
1909 ::DeleteObject(iconInfo
.hbmMask
);
1914 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1916 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1919 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1921 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1924 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1926 #ifndef __WXMICROWIN__
1927 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1929 // get width/height from the bitmap if not given
1933 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1938 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1939 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1940 if ( !hdcSrc
|| !hdcDst
)
1942 wxLogLastError(wxT("CreateCompatibleDC"));
1945 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1948 wxLogLastError(wxT("CreateBitmap"));
1951 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1952 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1953 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1957 wxLogLastError(wxT("BitBlt"));
1961 SelectObject(hdcSrc
,srcTmp
);
1962 SelectObject(hdcDst
,dstTmp
);