1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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
);
263 #if WXWIN_COMPATIBILITY_2
264 refData
->m_ok
= TRUE
;
265 #endif // WXWIN_COMPATIBILITY_2
275 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
283 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
287 return CopyFromIconOrCursor(cursor
);
291 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
298 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
299 // to create a bitmap from icon there - but using this way we won't have
302 int width
= icon
.GetWidth(),
303 height
= icon
.GetHeight();
305 // copy the icon to the bitmap
307 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
308 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
309 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
311 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
313 ::SelectObject(hdc
, hbmpOld
);
316 wxBitmapRefData
*refData
= new wxBitmapRefData
;
319 refData
->m_width
= width
;
320 refData
->m_height
= height
;
321 refData
->m_depth
= wxDisplayDepth();
323 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
325 #if WXWIN_COMPATIBILITY_2
326 refData
->m_ok
= TRUE
;
327 #endif // WXWIN_COMPATIBILITY_2
331 return CopyFromIconOrCursor(icon
);
332 #endif // Win16/Win32
336 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
338 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
340 HBITMAP hbitmap
= dib
.CreateDDB();
346 wxBitmapRefData
*refData
= new wxBitmapRefData
;
349 refData
->m_width
= dib
.GetWidth();
350 refData
->m_height
= dib
.GetHeight();
351 refData
->m_depth
= dib
.GetDepth();
353 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
356 wxPalette
*palette
= dib
.CreatePalette();
359 refData
->m_bitmapPalette
= *palette
;
363 #endif // wxUSE_PALETTE
369 wxBitmap::~wxBitmap()
373 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
377 #ifndef __WXMICROWIN__
378 wxBitmapRefData
*refData
= new wxBitmapRefData
;
381 refData
->m_width
= width
;
382 refData
->m_height
= height
;
383 refData
->m_depth
= depth
;
388 // we assume that it is in XBM format which is not quite the same as
389 // the format CreateBitmap() wants because the order of bytes in the
391 const size_t bytesPerLine
= (width
+ 7) / 8;
392 const size_t padding
= bytesPerLine
% 2;
393 const size_t len
= height
* ( padding
+ bytesPerLine
);
394 data
= (char *)malloc(len
);
395 const char *src
= bits
;
398 for ( int rows
= 0; rows
< height
; rows
++ )
400 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
402 unsigned char val
= *src
++;
403 unsigned char reversed
= 0;
405 for ( int bits
= 0; bits
< 8; bits
++)
408 reversed
|= (val
& 0x01);
420 // bits should already be in Windows standard format
421 data
= (char *)bits
; // const_cast is harmless
424 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
427 wxLogLastError(wxT("CreateBitmap"));
435 SetHBITMAP((WXHBITMAP
)hbmp
);
439 // Create from XPM data
440 bool wxBitmap::CreateFromXpm(const char **data
)
442 #if wxUSE_IMAGE && wxUSE_XPM
445 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
447 wxXPMDecoder decoder
;
448 wxImage img
= decoder
.ReadData(data
);
449 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
451 *this = wxBitmap(img
);
458 wxBitmap::wxBitmap(int w
, int h
, int d
)
462 (void)Create(w
, h
, d
);
465 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
469 (void)Create(w
, h
, dc
);
472 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
476 (void)Create(data
, type
, width
, height
, depth
);
479 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
483 LoadFile(filename
, (int)type
);
486 bool wxBitmap::Create(int width
, int height
, int depth
)
488 return DoCreate(width
, height
, depth
, 0);
491 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
493 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
495 return DoCreate(width
, height
, -1, dc
.GetHDC());
498 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
502 m_refData
= new wxBitmapRefData
;
504 GetBitmapData()->m_width
= w
;
505 GetBitmapData()->m_height
= h
;
510 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
514 // create DIBs without alpha channel by default
522 // don't delete the DIB section in dib object dtor
525 GetBitmapData()->m_isDIB
= TRUE
;
526 GetBitmapData()->m_depth
= d
;
532 d
= wxDisplayDepth();
534 GetBitmapData()->m_depth
= d
;
536 #ifndef __WXMICROWIN__
539 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
542 wxLogLastError(wxT("CreateBitmap"));
546 #endif // !__WXMICROWIN__
549 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
552 wxLogLastError(wxT("CreateCompatibleBitmap"));
555 GetBitmapData()->m_depth
= wxDisplayDepth();
559 SetHBITMAP((WXHBITMAP
)hbmp
);
561 #if WXWIN_COMPATIBILITY_2
562 GetBitmapData()->m_ok
= hbmp
!= 0;
563 #endif // WXWIN_COMPATIBILITY_2
570 // ----------------------------------------------------------------------------
571 // wxImage to/from conversions for Microwin
572 // ----------------------------------------------------------------------------
574 // Microwin versions are so different from normal ones that it really doesn't
575 // make sense to use #ifdefs inside the function bodies
576 #ifdef __WXMICROWIN__
578 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
580 // Set this to 1 to experiment with mask code,
581 // which currently doesn't work
584 m_refData
= new wxBitmapRefData();
586 // Initial attempt at a simple-minded implementation.
587 // The bitmap will always be created at the screen depth,
588 // so the 'depth' argument is ignored.
590 HDC hScreenDC
= ::GetDC(NULL
);
591 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
593 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
594 HBITMAP hMaskBitmap
= NULL
;
595 HBITMAP hOldMaskBitmap
= NULL
;
597 unsigned char maskR
= 0;
598 unsigned char maskG
= 0;
599 unsigned char maskB
= 0;
601 // printf("Created bitmap %d\n", (int) hBitmap);
604 ::ReleaseDC(NULL
, hScreenDC
);
607 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
609 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
610 ::ReleaseDC(NULL
, hScreenDC
);
612 // created an mono-bitmap for the possible mask
613 bool hasMask
= image
.HasMask();
618 // FIXME: we should be able to pass bpp = 1, but
619 // GdBlit can't handle a different depth
621 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
623 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
625 maskR
= image
.GetMaskRed();
626 maskG
= image
.GetMaskGreen();
627 maskB
= image
.GetMaskBlue();
635 hScreenDC
= ::GetDC(NULL
);
636 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
637 ::ReleaseDC(NULL
, hScreenDC
);
639 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
647 for (i
= 0; i
< image
.GetWidth(); i
++)
649 for (j
= 0; j
< image
.GetHeight(); j
++)
651 unsigned char red
= image
.GetRed(i
, j
);
652 unsigned char green
= image
.GetGreen(i
, j
);
653 unsigned char blue
= image
.GetBlue(i
, j
);
655 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
659 // scan the bitmap for the transparent colour and set the corresponding
660 // pixels in the mask to BLACK and the rest to WHITE
661 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
662 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
664 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
669 ::SelectObject(hMemDC
, hOldBitmap
);
673 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
676 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
679 SetWidth(image
.GetWidth());
680 SetHeight(image
.GetHeight());
681 SetDepth(screenDepth
);
682 SetHBITMAP( (WXHBITMAP
) hBitmap
);
685 // Copy the palette from the source image
686 SetPalette(image
.GetPalette());
687 #endif // wxUSE_PALETTE
689 #if WXWIN_COMPATIBILITY_2
690 // check the wxBitmap object
691 GetBitmapData()->SetOk();
692 #endif // WXWIN_COMPATIBILITY_2
697 wxImage
wxBitmap::ConvertToImage() const
699 // Initial attempt at a simple-minded implementation.
700 // The bitmap will always be created at the screen depth,
701 // so the 'depth' argument is ignored.
702 // TODO: transparency (create a mask image)
706 wxFAIL_MSG( wxT("bitmap is invalid") );
712 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
714 // create an wxImage object
715 int width
= GetWidth();
716 int height
= GetHeight();
717 image
.Create( width
, height
);
718 unsigned char *data
= image
.GetData();
721 wxFAIL_MSG( wxT("could not allocate data for image") );
725 HDC hScreenDC
= ::GetDC(NULL
);
727 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
728 ::ReleaseDC(NULL
, hScreenDC
);
730 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
732 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
735 for (i
= 0; i
< GetWidth(); i
++)
737 for (j
= 0; j
< GetHeight(); j
++)
739 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
740 unsigned char red
= GetRValue(color
);
741 unsigned char green
= GetGValue(color
);
742 unsigned char blue
= GetBValue(color
);
744 image
.SetRGB(i
, j
, red
, green
, blue
);
748 ::SelectObject(hMemDC
, hOldBitmap
);
752 // Copy the palette from the source image
754 image
.SetPalette(* GetPalette());
755 #endif // wxUSE_PALETTE
760 #endif // __WXMICROWIN__
762 // ----------------------------------------------------------------------------
763 // wxImage to/from conversions
764 // ----------------------------------------------------------------------------
766 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
768 return CreateFromImage(image
, depth
, 0);
771 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
773 wxCHECK_MSG( dc
.Ok(), FALSE
,
774 _T("invalid HDC in wxBitmap::CreateFromImage()") );
776 return CreateFromImage(image
, -1, dc
.GetHDC());
779 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
782 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
786 // first convert the image to DIB
787 const int h
= image
.GetHeight();
788 const int w
= image
.GetWidth();
795 // store the bitmap parameters
796 wxBitmapRefData
*refData
= new wxBitmapRefData
;
797 refData
->m_width
= w
;
798 refData
->m_height
= h
;
799 refData
->m_hasAlpha
= image
.HasAlpha();
804 // next either store DIB as is or create a DDB from it
807 // are we going to use DIB?
809 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
810 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
812 // don't delete the DIB section in dib object dtor
813 hbitmap
= dib
.Detach();
815 refData
->m_isDIB
= TRUE
;
816 refData
->m_depth
= dib
.GetDepth();
818 else // we need to convert DIB to DDB
820 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
822 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
825 // validate this object
826 SetHBITMAP((WXHBITMAP
)hbitmap
);
828 #if WXWIN_COMPATIBILITY_2
829 m_refData
->m_ok
= TRUE
;
830 #endif // WXWIN_COMPATIBILITY_2
832 // finally also set the mask if we have one
833 if ( image
.HasMask() )
835 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
836 image
.GetMaskGreen(),
837 image
.GetMaskBlue())));
842 // FIXME: wxWinCE doesn't support wxDIB yet
847 wxImage
wxBitmap::ConvertToImage() const
849 // FIXME: this is untested code for WinCE, and
850 // the mask is not yet handled.
852 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
855 // the colour used as transparent one in wxImage and the one it is replaced
856 // with when it really occurs in the bitmap
857 static const int MASK_RED
= 1;
858 static const int MASK_GREEN
= 2;
859 static const int MASK_BLUE
= 3;
860 static const int MASK_BLUE_REPLACEMENT
= 2;
864 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
866 // create an wxImage object
867 int width
= GetWidth();
868 int height
= GetHeight();
869 image
.Create( width
, height
);
870 unsigned char *data
= image
.GetData();
873 wxFAIL_MSG( wxT("could not allocate data for image") );
877 // calc the number of bytes per scanline and padding in the DIB
878 int bytePerLine
= width
*3;
879 int sizeDWORD
= sizeof( DWORD
);
880 int lineBoundary
= bytePerLine
% sizeDWORD
;
882 if( lineBoundary
> 0 )
884 padding
= sizeDWORD
- lineBoundary
;
885 bytePerLine
+= padding
;
888 // create a DIB header
889 int headersize
= sizeof(BITMAPINFOHEADER
);
890 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
893 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
897 // Fill in the DIB header
898 lpDIBh
->bmiHeader
.biSize
= headersize
;
899 lpDIBh
->bmiHeader
.biWidth
= width
;
900 lpDIBh
->bmiHeader
.biHeight
= -height
;
901 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
902 lpDIBh
->bmiHeader
.biPlanes
= 1;
903 lpDIBh
->bmiHeader
.biBitCount
= 24;
904 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
905 lpDIBh
->bmiHeader
.biClrUsed
= 0;
906 // These seem not really needed for our purpose here.
907 lpDIBh
->bmiHeader
.biClrImportant
= 0;
908 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
909 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
911 // memory for DIB data is allocated by CreateDIBSection
914 // copy data from the device-dependent bitmap to the DIB
915 HDC hdc
= ::GetDC(NULL
);
916 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
918 HBITMAP hBitmapSection
= ::CreateDIBSection( hdc
, lpDIBh
, DIB_RGB_COLORS
, & lpBits
, NULL
, 0 );
921 wxFAIL_MSG( wxT("could not create a DIB section") );
925 // Copy the image from the DDB to the DIBSection
926 // Need to copy the supplied bitmap onto the newly created DIBsection
927 HDC hMemDC
= CreateCompatibleDC(hdc
);
928 HDC hCopyDC
= CreateCompatibleDC(hdc
);
930 if (! hMemDC
|| ! hCopyDC
)
932 wxFAIL_MSG( wxT("unable to create compatible DCs") );
939 SelectPalette(hMemDC
, m_hPal
, FALSE
); RealizePalette(hMemDC
);
940 SelectPalette(hCopyDC
, m_hPal
, FALSE
); RealizePalette(hCopyDC
);
944 HBITMAP hOldMemBitmap
= (HBITMAP
) SelectObject(hMemDC
, hBitmap
);
945 HBITMAP hOldCopyBitmap
= (HBITMAP
) SelectObject(hCopyDC
, hBitmapSection
);
947 BitBlt(hCopyDC
, 0, 0, GetWidth(), GetHeight(), hMemDC
, 0, 0, SRCCOPY
);
949 SelectObject(hMemDC
, hOldMemBitmap
);
950 SelectObject(hCopyDC
, hOldCopyBitmap
);
957 HGDIOBJ hObj
= ::GetStockObject(DEFAULT_PALETTE
);
958 SelectObject(hMemDC
, hObj
);
959 SelectObject(hCopyDC
, hObj
);
963 ReleaseDC(NULL
, hdc
);
965 // copy DIB data into the wxImage object
967 unsigned char *ptdata
= data
;
968 unsigned char *ptbits
= (unsigned char*) lpBits
;
969 for( i
=0; i
<height
; i
++ )
971 for( j
=0; j
<width
; j
++ )
973 *(ptdata
++) = *(ptbits
+2);
974 *(ptdata
++) = *(ptbits
+1);
975 *(ptdata
++) = *(ptbits
);
983 // similarly, set data according to the possible mask bitmap
984 if( GetMask() && GetMask()->GetMaskBitmap() )
986 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
987 // memory DC created, color set, data copied, and memory DC deleted
988 HDC memdc
= ::CreateCompatibleDC( hdc
);
989 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
990 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
991 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
995 for( i
=0; i
<height
; i
++ )
997 for( j
=0; j
<width
; j
++ )
999 // is this pixel transparent?
1002 if ( (ptdata
[0] == MASK_RED
) &&
1003 (ptdata
[1] == MASK_GREEN
) &&
1004 (ptdata
[2] == MASK_BLUE
) )
1006 // we have to fudge the colour a bit to prevent this
1007 // pixel from appearing transparent
1008 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1012 else // masked pixel
1014 *(ptdata
++) = MASK_RED
;
1015 *(ptdata
++) = MASK_GREEN
;
1016 *(ptdata
++) = MASK_BLUE
;
1023 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1024 image
.SetMask( TRUE
);
1029 image
.SetMask( FALSE
);
1032 // free allocated resources
1033 ::ReleaseDC(NULL
, hdc
);
1036 // Delete the DIB section
1037 ::DeleteObject(hBitmapSection
);
1041 // the colour used as transparent one in wxImage and the one it is replaced
1042 // with when it really occurs in the bitmap
1043 static const int MASK_RED
= 1;
1044 static const int MASK_GREEN
= 2;
1045 static const int MASK_BLUE
= 3;
1046 static const int MASK_BLUE_REPLACEMENT
= 2;
1050 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1052 // create an wxImage object
1053 int width
= GetWidth();
1054 int height
= GetHeight();
1055 image
.Create( width
, height
);
1056 unsigned char *data
= image
.GetData();
1059 wxFAIL_MSG( wxT("could not allocate data for image") );
1063 // calc the number of bytes per scanline and padding in the DIB
1064 int bytePerLine
= width
*3;
1065 int sizeDWORD
= sizeof( DWORD
);
1066 int lineBoundary
= bytePerLine
% sizeDWORD
;
1068 if( lineBoundary
> 0 )
1070 padding
= sizeDWORD
- lineBoundary
;
1071 bytePerLine
+= padding
;
1074 // create a DIB header
1075 int headersize
= sizeof(BITMAPINFOHEADER
);
1076 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1079 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1083 // Fill in the DIB header
1084 lpDIBh
->bmiHeader
.biSize
= headersize
;
1085 lpDIBh
->bmiHeader
.biWidth
= width
;
1086 lpDIBh
->bmiHeader
.biHeight
= -height
;
1087 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1088 lpDIBh
->bmiHeader
.biPlanes
= 1;
1089 lpDIBh
->bmiHeader
.biBitCount
= 24;
1090 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1091 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1092 // These seem not really needed for our purpose here.
1093 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1094 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1095 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1096 // memory for DIB data
1097 unsigned char *lpBits
;
1098 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1101 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1107 // copy data from the device-dependent bitmap to the DIB
1108 HDC hdc
= ::GetDC(NULL
);
1110 hbitmap
= (HBITMAP
) GetHBITMAP();
1111 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1113 // copy DIB data into the wxImage object
1115 unsigned char *ptdata
= data
;
1116 unsigned char *ptbits
= lpBits
;
1117 for( i
=0; i
<height
; i
++ )
1119 for( j
=0; j
<width
; j
++ )
1121 *(ptdata
++) = *(ptbits
+2);
1122 *(ptdata
++) = *(ptbits
+1);
1123 *(ptdata
++) = *(ptbits
);
1129 // similarly, set data according to the possible mask bitmap
1130 if( GetMask() && GetMask()->GetMaskBitmap() )
1132 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1133 // memory DC created, color set, data copied, and memory DC deleted
1134 HDC memdc
= ::CreateCompatibleDC( hdc
);
1135 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1136 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1137 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1138 ::DeleteDC( memdc
);
1141 for( i
=0; i
<height
; i
++ )
1143 for( j
=0; j
<width
; j
++ )
1145 // is this pixel transparent?
1148 if ( (ptdata
[0] == MASK_RED
) &&
1149 (ptdata
[1] == MASK_GREEN
) &&
1150 (ptdata
[2] == MASK_BLUE
) )
1152 // we have to fudge the colour a bit to prevent this
1153 // pixel from appearing transparent
1154 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1158 else // masked pixel
1160 *(ptdata
++) = MASK_RED
;
1161 *(ptdata
++) = MASK_GREEN
;
1162 *(ptdata
++) = MASK_BLUE
;
1169 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1170 image
.SetMask( TRUE
);
1174 image
.SetMask( FALSE
);
1176 // free allocated resources
1177 ::ReleaseDC(NULL
, hdc
);
1185 #endif // wxUSE_IMAGE
1187 // ----------------------------------------------------------------------------
1188 // loading and saving bitmaps
1189 // ----------------------------------------------------------------------------
1191 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1195 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1199 m_refData
= new wxBitmapRefData
;
1201 return handler
->LoadFile(this, filename
, type
, -1, -1);
1207 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1209 *this = wxBitmap(image
);
1214 #endif // wxUSE_IMAGE
1219 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1223 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1227 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1232 m_refData
= new wxBitmapRefData
;
1234 return handler
->Create(this, data
, type
, width
, height
, depth
);
1237 bool wxBitmap::SaveFile(const wxString
& filename
,
1239 const wxPalette
*palette
)
1241 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1245 return handler
->SaveFile(this, filename
, type
, palette
);
1250 // FIXME what about palette? shouldn't we use it?
1251 wxImage image
= ConvertToImage();
1254 return image
.SaveFile(filename
, type
);
1257 #endif // wxUSE_IMAGE
1262 // ----------------------------------------------------------------------------
1263 // sub bitmap extraction
1264 // ----------------------------------------------------------------------------
1266 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1268 wxCHECK_MSG( Ok() &&
1269 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1270 (rect
.x
+rect
.width
<= GetWidth()) &&
1271 (rect
.y
+rect
.height
<= GetHeight()),
1272 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1274 wxBitmap
ret( rect
.width
, rect
.height
);
1275 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1277 #ifndef __WXMICROWIN__
1278 // TODO: copy alpha channel data if any
1285 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1286 selectDst(dcDst
, GetHbitmapOf(ret
));
1288 if ( !selectSrc
|| !selectDst
)
1290 wxLogLastError(_T("SelectObjct(hBitmap)"));
1293 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1294 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1296 wxLogLastError(_T("BitBlt"));
1300 // copy mask if there is one
1303 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1305 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1306 selectDst(dcDst
, hbmpMask
);
1308 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1309 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1311 wxLogLastError(_T("BitBlt"));
1314 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1317 #endif // !__WXMICROWIN__
1322 // ----------------------------------------------------------------------------
1323 // wxBitmap accessors
1324 // ----------------------------------------------------------------------------
1327 wxPalette
* wxBitmap::GetPalette() const
1329 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1330 : (wxPalette
*) NULL
;
1334 wxMask
*wxBitmap::GetMask() const
1336 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1341 wxDC
*wxBitmap::GetSelectedInto() const
1343 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1348 #if WXWIN_COMPATIBILITY_2_4
1350 int wxBitmap::GetQuality() const
1355 #endif // WXWIN_COMPATIBILITY_2_4
1357 void wxBitmap::UseAlpha()
1359 if ( GetBitmapData() )
1360 GetBitmapData()->m_hasAlpha
= true;
1363 bool wxBitmap::HasAlpha() const
1365 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1368 // ----------------------------------------------------------------------------
1370 // ----------------------------------------------------------------------------
1374 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1376 if ( GetBitmapData() )
1377 GetBitmapData()->m_selectedInto
= dc
;
1384 void wxBitmap::SetPalette(const wxPalette
& palette
)
1388 GetBitmapData()->m_bitmapPalette
= palette
;
1391 #endif // wxUSE_PALETTE
1393 void wxBitmap::SetMask(wxMask
*mask
)
1397 GetBitmapData()->SetMask(mask
);
1400 #if WXWIN_COMPATIBILITY_2
1402 void wxBitmap::SetOk(bool isOk
)
1406 GetBitmapData()->m_ok
= isOk
;
1409 #endif // WXWIN_COMPATIBILITY_2
1411 #if WXWIN_COMPATIBILITY_2_4
1413 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1417 #endif // WXWIN_COMPATIBILITY_2_4
1419 // ----------------------------------------------------------------------------
1420 // raw bitmap access support
1421 // ----------------------------------------------------------------------------
1423 #ifdef wxHAVE_RAW_BITMAP
1424 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1429 // no bitmap, no data (raw or otherwise)
1433 // if we're already a DIB we can access our data directly, but if not we
1434 // need to convert this DDB to a DIB section and use it for raw access and
1435 // then convert it back
1437 if ( !GetBitmapData()->m_isDIB
)
1439 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1440 _T("GetRawData() may be called only once") );
1442 wxDIB
*dib
= new wxDIB(*this);
1450 // we'll free it in UngetRawData()
1451 GetBitmapData()->m_dib
= dib
;
1453 hDIB
= dib
->GetHandle();
1457 hDIB
= GetHbitmap();
1461 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1463 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1468 // check that the bitmap is in correct format
1469 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1471 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1476 // ok, store the relevant info in wxPixelDataBase
1477 const LONG h
= ds
.dsBm
.bmHeight
;
1479 data
.m_width
= ds
.dsBm
.bmWidth
;
1482 // remember that DIBs are stored in top to bottom order!
1483 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1484 data
.m_stride
= -bytesPerRow
;
1486 char *bits
= (char *)ds
.dsBm
.bmBits
;
1489 bits
+= (h
- 1)*bytesPerRow
;
1498 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1504 // the cast is ugly but we can't do without it and without making this
1505 // function template (and hence inline) unfortunately
1506 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1507 PixelData
& data
= (PixelData
&)dataBase
;
1511 // invalid data, don't crash -- but don't assert neither as we're
1512 // called automatically from wxPixelDataBase dtor and so there is no
1513 // way to prevent this from happening
1517 if ( GetBitmapData()->m_hasAlpha
)
1519 // AlphaBlend() wants to have premultiplied source alpha but
1520 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1522 PixelData::Iterator
p(data
);
1524 const int w
= data
.GetWidth();
1525 const int h
= data
.GetHeight();
1527 for ( int y
= 0; y
< h
; y
++ )
1529 PixelData::Iterator rowStart
= p
;
1531 for ( int x
= 0; x
< w
; x
++ )
1533 const unsigned alpha
= p
.Alpha();
1535 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1536 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1537 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1546 // if we're a DDB we need to convert DIB back to DDB now to make the
1547 // changes made via raw bitmap access effective
1548 if ( !GetBitmapData()->m_isDIB
)
1550 wxDIB
*dib
= GetBitmapData()->m_dib
;
1551 GetBitmapData()->m_dib
= NULL
;
1560 #endif // #ifdef wxHAVE_RAW_BITMAP
1562 // ----------------------------------------------------------------------------
1564 // ----------------------------------------------------------------------------
1571 // Construct a mask from a bitmap and a colour indicating
1572 // the transparent area
1573 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1576 Create(bitmap
, colour
);
1579 // Construct a mask from a bitmap and a palette index indicating
1580 // the transparent area
1581 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1584 Create(bitmap
, paletteIndex
);
1587 // Construct a mask from a mono bitmap (copies the bitmap).
1588 wxMask::wxMask(const wxBitmap
& bitmap
)
1597 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1600 // Create a mask from a mono bitmap (copies the bitmap).
1601 bool wxMask::Create(const wxBitmap
& bitmap
)
1603 #ifndef __WXMICROWIN__
1604 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1605 _T("can't create mask from invalid or not monochrome bitmap") );
1609 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1613 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1618 HDC srcDC
= CreateCompatibleDC(0);
1619 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1620 HDC destDC
= CreateCompatibleDC(0);
1621 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1622 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1623 SelectObject(srcDC
, 0);
1625 SelectObject(destDC
, 0);
1633 // Create a mask from a bitmap and a palette index indicating
1634 // the transparent area
1635 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1639 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1644 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1646 unsigned char red
, green
, blue
;
1647 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1649 wxColour
transparentColour(red
, green
, blue
);
1650 return Create(bitmap
, transparentColour
);
1653 #endif // wxUSE_PALETTE
1658 // Create a mask from a bitmap and a colour indicating
1659 // the transparent area
1660 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1662 #ifndef __WXMICROWIN__
1663 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1667 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1671 int width
= bitmap
.GetWidth(),
1672 height
= bitmap
.GetHeight();
1674 // scan the bitmap for the transparent colour and set the corresponding
1675 // pixels in the mask to BLACK and the rest to WHITE
1676 COLORREF maskColour
= wxColourToPalRGB(colour
);
1677 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1679 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1680 HDC destDC
= ::CreateCompatibleDC(NULL
);
1681 if ( !srcDC
|| !destDC
)
1683 wxLogLastError(wxT("CreateCompatibleDC"));
1688 // SelectObject() will fail
1689 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1690 _T("bitmap can't be selected in another DC") );
1692 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1695 wxLogLastError(wxT("SelectObject"));
1700 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1703 wxLogLastError(wxT("SelectObject"));
1710 // this will create a monochrome bitmap with 0 points for the pixels
1711 // which have the same value as the background colour and 1 for the
1713 ::SetBkColor(srcDC
, maskColour
);
1714 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1717 ::SelectObject(srcDC
, hbmpSrcOld
);
1719 ::SelectObject(destDC
, hbmpDstOld
);
1723 #else // __WXMICROWIN__
1725 #endif // __WXMICROWIN__/!__WXMICROWIN__
1728 // ----------------------------------------------------------------------------
1730 // ----------------------------------------------------------------------------
1732 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1735 int width
, int height
, int depth
)
1737 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1739 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1742 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1743 const wxString
& name
,
1745 int width
, int height
)
1747 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1749 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1752 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1753 const wxString
& name
,
1756 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1758 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1761 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1762 void *WXUNUSED(data
),
1763 long WXUNUSED(type
),
1764 int WXUNUSED(width
),
1765 int WXUNUSED(height
),
1766 int WXUNUSED(depth
))
1771 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1772 const wxString
& WXUNUSED(name
),
1773 long WXUNUSED(type
),
1774 int WXUNUSED(desiredWidth
),
1775 int WXUNUSED(desiredHeight
))
1780 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1781 const wxString
& WXUNUSED(name
),
1783 const wxPalette
*WXUNUSED(palette
))
1788 // ----------------------------------------------------------------------------
1790 // ----------------------------------------------------------------------------
1792 #ifndef __WXMICROWIN__
1793 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1794 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1796 unsigned long i
, headerSize
;
1797 LPBITMAPINFO lpDIBheader
= NULL
;
1798 LPPALETTEENTRY lpPe
= NULL
;
1801 // Allocate space for a DIB header
1802 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1803 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1804 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1806 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1808 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1810 // Fill in the static parts of the DIB header
1811 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1812 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1813 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1814 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1816 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1817 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1818 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1819 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1820 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1823 // Initialize the DIB palette
1824 for (i
= 0; i
< 256; i
++) {
1825 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1826 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1827 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1828 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1831 *lpDIBHeader
= lpDIBheader
;
1836 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1842 // ----------------------------------------------------------------------------
1843 // global helper functions implemented here
1844 // ----------------------------------------------------------------------------
1846 // helper of wxBitmapToHICON/HCURSOR
1848 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1855 // we can't create an icon/cursor form nothing
1859 wxMask
*mask
= bmp
.GetMask();
1862 // we must have a mask for an icon, so even if it's probably incorrect,
1863 // do create it (grey is the "standard" transparent colour)
1864 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1868 wxZeroMemory(iconInfo
);
1869 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1872 iconInfo
.xHotspot
= hotSpotX
;
1873 iconInfo
.yHotspot
= hotSpotY
;
1876 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1877 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1879 // black out the transparent area to preserve background colour, because
1880 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1881 // the mask to the dest rect.
1883 MemoryHDC dcSrc
, dcDst
;
1884 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1885 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1887 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1888 dcSrc
, 0, 0, SRCAND
) )
1890 wxLogLastError(_T("BitBlt"));
1894 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1896 if ( !bmp
.GetMask() )
1898 // we created the mask, now delete it
1902 // delete the inverted mask bitmap we created as well
1903 ::DeleteObject(iconInfo
.hbmMask
);
1908 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1910 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1913 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1915 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1918 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1920 #ifndef __WXMICROWIN__
1921 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1923 // get width/height from the bitmap if not given
1927 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1932 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1933 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1934 if ( !hdcSrc
|| !hdcDst
)
1936 wxLogLastError(wxT("CreateCompatibleDC"));
1939 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1942 wxLogLastError(wxT("CreateBitmap"));
1945 ::SelectObject(hdcSrc
, hbmpMask
);
1946 ::SelectObject(hdcDst
, hbmpInvMask
);
1947 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1951 wxLogLastError(wxT("BitBlt"));