1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bitmap.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/bitmap.h"
35 #include "wx/palette.h"
36 #include "wx/dcmemory.h"
42 #include "wx/msw/private.h"
45 #include "wx/msw/dib.h"
48 #ifdef wxHAVE_RAW_BITMAP
49 #include "wx/rawbmp.h"
52 // missing from mingw32 header
54 #define CLR_INVALID ((COLORREF)-1)
55 #endif // no CLR_INVALID
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
65 wxBitmapRefData(const wxBitmapRefData
& data
);
66 virtual ~wxBitmapRefData() { Free(); }
70 // set the mask object to use as the mask, we take ownership of it
71 void SetMask(wxMask
*mask
)
77 // set the HBITMAP to use as the mask
78 void SetMask(HBITMAP hbmpMask
)
80 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
84 wxMask
*GetMask() const { return m_bitmapMask
; }
88 wxPalette m_bitmapPalette
;
89 #endif // wxUSE_PALETTE
95 // this field is solely for error checking: we detect selecting a bitmap
96 // into more than one DC at once or deleting a bitmap still selected into a
97 // DC (both are serious programming errors under Windows)
102 // when GetRawData() is called for a DDB we need to convert it to a DIB
103 // first to be able to provide direct access to it and we cache that DIB
104 // here and convert it back to DDB when UngetRawData() is called
108 // true if we have alpha transparency info and can be drawn using
112 // true if our HBITMAP is a DIB section, false if it is a DDB
116 // optional mask for transparent drawing
117 wxMask
*m_bitmapMask
;
121 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
129 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 // decide whether we should create a DIB or a DDB for the given parameters
143 // NB: we always use DIBs under Windows CE as this is much simpler (even if
144 // also less efficient...) and we obviously can't use them if there is no
145 // DIB support compiled in at all
147 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
149 #define ALWAYS_USE_DIB
151 // no sense in defining wxShouldCreateDIB() as we can't compile code
152 // executed if it is true, so we have to use #if's anyhow
153 #define NEVER_USE_DIB
154 #else // wxUSE_WXDIB && !__WXWINCE__
155 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
157 // here is the logic:
159 // (a) if hdc is specified, the caller explicitly wants DDB
160 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
161 // or less DIBs anyhow)
162 // (c) finally, create DIBs under Win9x even if the depth hasn't been
163 // explicitly specified but the current display depth is 24 or
164 // more and the image is "big", i.e. > 16Mb which is the
165 // theoretical limit for DDBs under Win9x
167 // consequences (all of which seem to make sense):
169 // (i) by default, DDBs are created (depth == -1 usually)
170 // (ii) DIBs can be created by explicitly specifying the depth
171 // (iii) using a DC always forces creating a DDB
175 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
178 #define SOMETIMES_USE_DIB
179 #endif // different DIB usage scenarious
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 wxBitmapRefData::wxBitmapRefData()
188 m_selectedInto
= NULL
;
192 m_hBitmap
= (WXHBITMAP
) NULL
;
201 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
202 : wxGDIImageRefData(data
)
205 m_selectedInto
= NULL
;
208 // (deep) copy the mask if present
210 if (data
.m_bitmapMask
)
211 m_bitmapMask
= new wxMask(*data
.m_bitmapMask
);
213 // FIXME: we don't copy m_hBitmap currently but we should, see wxBitmap::
216 wxASSERT_MSG( !data
.m_isDIB
,
217 _T("can't copy bitmap locked for raw access!") );
220 m_hasAlpha
= data
.m_hasAlpha
;
223 void wxBitmapRefData::Free()
225 wxASSERT_MSG( !m_selectedInto
,
226 wxT("deleting bitmap still selected into wxMemoryDC") );
229 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
234 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
236 wxLogLastError(wxT("DeleteObject(hbitmap)"));
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 wxGDIImageRefData
*wxBitmap::CreateData() const
250 return new wxBitmapRefData
;
253 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
255 const wxBitmapRefData
*
256 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
260 // FIXME: this method is backwards, it should just create a new
261 // wxBitmapRefData using its copy ctor but instead it modifies this
262 // bitmap itself and then returns its m_refData -- which works, of
263 // course (except in !wxUSE_WXDIB), but is completely illogical
264 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
267 // copy the other bitmap
268 if ( data
->m_hBitmap
)
270 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
271 self
->CopyFromDIB(dib
);
274 #endif // wxUSE_WXDIB
276 // copy the bitmap data
277 self
->m_refData
= new wxBitmapRefData(*data
);
280 // copy also the mask
281 wxMask
* const maskSrc
= data
->GetMask();
284 wxBitmapRefData
*selfdata
= wx_static_cast(wxBitmapRefData
*, m_refData
);
286 selfdata
->SetMask(new wxMask(*maskSrc
));
292 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
,
293 wxBitmapTransparency transp
)
295 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
296 // it may be either HICON or HCURSOR
297 HICON hicon
= (HICON
)icon
.GetHandle();
300 if ( !::GetIconInfo(hicon
, &iconInfo
) )
302 wxLogLastError(wxT("GetIconInfo"));
307 wxBitmapRefData
*refData
= new wxBitmapRefData
;
310 int w
= icon
.GetWidth(),
311 h
= icon
.GetHeight();
313 refData
->m_width
= w
;
314 refData
->m_height
= h
;
315 refData
->m_depth
= wxDisplayDepth();
317 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
322 wxFAIL_MSG( _T("unknown wxBitmapTransparency value") );
324 case wxBitmapTransparency_None
:
325 // nothing to do, refData->m_hasAlpha is false by default
328 case wxBitmapTransparency_Auto
:
330 // If the icon is 32 bits per pixel then it may have alpha channel
331 // data, although there are some icons that are 32 bpp but have no
332 // alpha... So convert to a DIB and manually check the 4th byte for
336 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(bm
), &bm
) &&
337 (bm
.bmBitsPixel
== 32) )
339 wxDIB
dib(iconInfo
.hbmColor
);
342 const unsigned char* pixels
= dib
.GetData();
343 for (int idx
= 0; idx
< w
*h
*4; idx
+=4)
345 if (pixels
[idx
+3] != 0)
347 // If there is an alpha byte that is non-zero
348 // then set the alpha flag and stop checking
349 refData
->m_hasAlpha
= true;
357 #endif // wxUSE_WXDIB
359 case wxBitmapTransparency_Always
:
360 refData
->m_hasAlpha
= true;
364 if ( !refData
->m_hasAlpha
)
366 // the mask returned by GetIconInfo() is inverted compared to the usual
368 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
371 // delete the old one now as we don't need it any more
372 ::DeleteObject(iconInfo
.hbmMask
);
375 #else // __WXMICROWIN__ || __WXWINCE__
378 #endif // !__WXWINCE__/__WXWINCE__
381 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
388 return CopyFromIconOrCursor(cursor
, transp
);
391 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
398 return CopyFromIconOrCursor(icon
, transp
);
401 #ifndef NEVER_USE_DIB
403 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
405 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
407 #ifdef SOMETIMES_USE_DIB
408 HBITMAP hbitmap
= dib
.CreateDDB();
411 #else // ALWAYS_USE_DIB
412 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
413 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
417 wxBitmapRefData
*refData
= new wxBitmapRefData
;
420 refData
->m_width
= dib
.GetWidth();
421 refData
->m_height
= dib
.GetHeight();
422 refData
->m_depth
= dib
.GetDepth();
424 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
427 wxPalette
*palette
= dib
.CreatePalette();
430 refData
->m_bitmapPalette
= *palette
;
434 #endif // wxUSE_PALETTE
439 #endif // NEVER_USE_DIB
441 wxBitmap::~wxBitmap()
445 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
447 #ifndef __WXMICROWIN__
448 wxBitmapRefData
*refData
= new wxBitmapRefData
;
451 refData
->m_width
= width
;
452 refData
->m_height
= height
;
453 refData
->m_depth
= depth
;
458 // we assume that it is in XBM format which is not quite the same as
459 // the format CreateBitmap() wants because the order of bytes in the
461 const size_t bytesPerLine
= (width
+ 7) / 8;
462 const size_t padding
= bytesPerLine
% 2;
463 const size_t len
= height
* ( padding
+ bytesPerLine
);
464 data
= (char *)malloc(len
);
465 const char *src
= bits
;
468 for ( int rows
= 0; rows
< height
; rows
++ )
470 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
472 unsigned char val
= *src
++;
473 unsigned char reversed
= 0;
475 for ( int bits
= 0; bits
< 8; bits
++)
478 reversed
|= (unsigned char)(val
& 0x01);
490 // bits should already be in Windows standard format
491 data
= (char *)bits
; // const_cast is harmless
494 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
497 wxLogLastError(wxT("CreateBitmap"));
505 SetHBITMAP((WXHBITMAP
)hbmp
);
509 wxBitmap::wxBitmap(int w
, int h
, int d
)
511 (void)Create(w
, h
, d
);
514 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
516 (void)Create(w
, h
, dc
);
519 wxBitmap::wxBitmap(const void* data
, long type
, int width
, int height
, int depth
)
521 (void)Create(data
, type
, width
, height
, depth
);
524 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
526 LoadFile(filename
, (int)type
);
529 bool wxBitmap::Create(int width
, int height
, int depth
)
531 return DoCreate(width
, height
, depth
, 0);
534 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
536 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
538 return DoCreate(width
, height
, -1, dc
.GetHDC());
541 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
545 m_refData
= new wxBitmapRefData
;
547 GetBitmapData()->m_width
= w
;
548 GetBitmapData()->m_height
= h
;
550 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
552 #ifndef NEVER_USE_DIB
553 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
557 // create DIBs without alpha channel by default
565 // don't delete the DIB section in dib object dtor
568 GetBitmapData()->m_isDIB
= true;
569 GetBitmapData()->m_depth
= d
;
572 #endif // NEVER_USE_DIB
574 #ifndef ALWAYS_USE_DIB
575 #ifndef __WXMICROWIN__
578 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
581 wxLogLastError(wxT("CreateBitmap"));
584 GetBitmapData()->m_depth
= d
;
586 else // d == 0, create bitmap compatible with the screen
587 #endif // !__WXMICROWIN__
590 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
593 wxLogLastError(wxT("CreateCompatibleBitmap"));
596 GetBitmapData()->m_depth
= wxDisplayDepth();
598 #endif // !ALWAYS_USE_DIB
601 SetHBITMAP((WXHBITMAP
)hbmp
);
608 // ----------------------------------------------------------------------------
609 // wxImage to/from conversions for Microwin
610 // ----------------------------------------------------------------------------
612 // Microwin versions are so different from normal ones that it really doesn't
613 // make sense to use #ifdefs inside the function bodies
614 #ifdef __WXMICROWIN__
616 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
618 // Set this to 1 to experiment with mask code,
619 // which currently doesn't work
622 m_refData
= new wxBitmapRefData();
624 // Initial attempt at a simple-minded implementation.
625 // The bitmap will always be created at the screen depth,
626 // so the 'depth' argument is ignored.
628 HDC hScreenDC
= ::GetDC(NULL
);
629 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
631 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
632 HBITMAP hMaskBitmap
= NULL
;
633 HBITMAP hOldMaskBitmap
= NULL
;
635 unsigned char maskR
= 0;
636 unsigned char maskG
= 0;
637 unsigned char maskB
= 0;
639 // printf("Created bitmap %d\n", (int) hBitmap);
642 ::ReleaseDC(NULL
, hScreenDC
);
645 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
647 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
648 ::ReleaseDC(NULL
, hScreenDC
);
650 // created an mono-bitmap for the possible mask
651 bool hasMask
= image
.HasMask();
656 // FIXME: we should be able to pass bpp = 1, but
657 // GdBlit can't handle a different depth
659 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
661 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
663 maskR
= image
.GetMaskRed();
664 maskG
= image
.GetMaskGreen();
665 maskB
= image
.GetMaskBlue();
673 hScreenDC
= ::GetDC(NULL
);
674 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
675 ::ReleaseDC(NULL
, hScreenDC
);
677 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
685 for (i
= 0; i
< image
.GetWidth(); i
++)
687 for (j
= 0; j
< image
.GetHeight(); j
++)
689 unsigned char red
= image
.GetRed(i
, j
);
690 unsigned char green
= image
.GetGreen(i
, j
);
691 unsigned char blue
= image
.GetBlue(i
, j
);
693 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
697 // scan the bitmap for the transparent colour and set the corresponding
698 // pixels in the mask to BLACK and the rest to WHITE
699 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
700 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
702 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
707 ::SelectObject(hMemDC
, hOldBitmap
);
711 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
714 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
717 SetWidth(image
.GetWidth());
718 SetHeight(image
.GetHeight());
719 SetDepth(screenDepth
);
720 SetHBITMAP( (WXHBITMAP
) hBitmap
);
723 // Copy the palette from the source image
724 SetPalette(image
.GetPalette());
725 #endif // wxUSE_PALETTE
730 wxImage
wxBitmap::ConvertToImage() const
732 // Initial attempt at a simple-minded implementation.
733 // The bitmap will always be created at the screen depth,
734 // so the 'depth' argument is ignored.
735 // TODO: transparency (create a mask image)
739 wxFAIL_MSG( wxT("bitmap is invalid") );
745 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
747 // create an wxImage object
748 int width
= GetWidth();
749 int height
= GetHeight();
750 image
.Create( width
, height
);
751 unsigned char *data
= image
.GetData();
754 wxFAIL_MSG( wxT("could not allocate data for image") );
758 HDC hScreenDC
= ::GetDC(NULL
);
760 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
761 ::ReleaseDC(NULL
, hScreenDC
);
763 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
765 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
768 for (i
= 0; i
< GetWidth(); i
++)
770 for (j
= 0; j
< GetHeight(); j
++)
772 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
773 unsigned char red
= GetRValue(color
);
774 unsigned char green
= GetGValue(color
);
775 unsigned char blue
= GetBValue(color
);
777 image
.SetRGB(i
, j
, red
, green
, blue
);
781 ::SelectObject(hMemDC
, hOldBitmap
);
785 // Copy the palette from the source image
787 image
.SetPalette(* GetPalette());
788 #endif // wxUSE_PALETTE
793 #endif // __WXMICROWIN__
795 // ----------------------------------------------------------------------------
796 // wxImage to/from conversions
797 // ----------------------------------------------------------------------------
799 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
801 return CreateFromImage(image
, depth
, 0);
804 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
806 wxCHECK_MSG( dc
.Ok(), false,
807 _T("invalid HDC in wxBitmap::CreateFromImage()") );
809 return CreateFromImage(image
, -1, dc
.GetHDC());
814 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
816 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
820 // first convert the image to DIB
821 const int h
= image
.GetHeight();
822 const int w
= image
.GetWidth();
829 depth
= dib
.GetDepth(); // Get depth from image if none specified
831 // store the bitmap parameters
832 wxBitmapRefData
*refData
= new wxBitmapRefData
;
833 refData
->m_width
= w
;
834 refData
->m_height
= h
;
835 refData
->m_hasAlpha
= image
.HasAlpha();
840 // next either store DIB as is or create a DDB from it
841 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
843 // are we going to use DIB?
845 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
846 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
848 // don't delete the DIB section in dib object dtor
849 hbitmap
= dib
.Detach();
851 refData
->m_isDIB
= true;
852 refData
->m_depth
= depth
;
854 #ifndef ALWAYS_USE_DIB
855 else // we need to convert DIB to DDB
857 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
859 refData
->m_depth
= depth
;
861 #endif // !ALWAYS_USE_DIB
863 // validate this object
864 SetHBITMAP((WXHBITMAP
)hbitmap
);
866 // finally also set the mask if we have one
867 if ( image
.HasMask() )
869 const size_t len
= 2*((w
+15)/16);
870 BYTE
*src
= image
.GetData();
871 BYTE
*data
= new BYTE
[h
*len
];
872 memset(data
, 0, h
*len
);
873 BYTE r
= image
.GetMaskRed(),
874 g
= image
.GetMaskGreen(),
875 b
= image
.GetMaskBlue();
877 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
881 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
883 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
886 if ( (mask
>>= 1) == 0 )
894 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
897 wxLogLastError(_T("CreateBitmap(mask)"));
901 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
910 wxImage
wxBitmap::ConvertToImage() const
912 // convert DDB to DIB
920 // and then DIB to our wxImage
921 wxImage image
= dib
.ConvertToImage();
927 // now do the same for the mask, if we have any
928 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
931 wxDIB
dibMask(hbmpMask
);
932 if ( dibMask
.IsOk() )
934 // TODO: use wxRawBitmap to iterate over DIB
936 // we hard code the mask colour for now but we could also make an
937 // effort (and waste time) to choose a colour not present in the
938 // image already to avoid having to fudge the pixels below --
939 // whether it's worth to do it is unclear however
940 static const int MASK_RED
= 1;
941 static const int MASK_GREEN
= 2;
942 static const int MASK_BLUE
= 3;
943 static const int MASK_BLUE_REPLACEMENT
= 2;
945 const int h
= dibMask
.GetHeight();
946 const int w
= dibMask
.GetWidth();
947 const int bpp
= dibMask
.GetDepth();
948 const int maskBytesPerPixel
= bpp
>> 3;
949 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
950 unsigned char *data
= image
.GetData();
952 // remember that DIBs are stored in bottom to top order
954 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
956 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
958 // traverse one mask DIB line
959 unsigned char *mask
= maskLineStart
;
960 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
962 // should this pixel be transparent?
965 // no, check that it isn't transparent by accident
966 if ( (data
[0] == MASK_RED
) &&
967 (data
[1] == MASK_GREEN
) &&
968 (data
[2] == MASK_BLUE
) )
970 // we have to fudge the colour a bit to prevent
971 // this pixel from appearing transparent
972 data
[2] = MASK_BLUE_REPLACEMENT
;
977 else // yes, transparent pixel
980 *data
++ = MASK_GREEN
;
986 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
993 #else // !wxUSE_WXDIB
996 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1003 wxImage
wxBitmap::ConvertToImage() const
1008 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1010 #endif // wxUSE_IMAGE
1012 // ----------------------------------------------------------------------------
1013 // loading and saving bitmaps
1014 // ----------------------------------------------------------------------------
1016 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1020 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1024 m_refData
= new wxBitmapRefData
;
1026 return handler
->LoadFile(this, filename
, type
, -1, -1);
1028 #if wxUSE_IMAGE && wxUSE_WXDIB
1029 else // no bitmap handler found
1032 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1034 *this = wxBitmap(image
);
1039 #endif // wxUSE_IMAGE
1044 bool wxBitmap::Create(const void* data
, long type
, int width
, int height
, int depth
)
1048 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1052 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1057 m_refData
= new wxBitmapRefData
;
1059 return handler
->Create(this, data
, type
, width
, height
, depth
);
1062 bool wxBitmap::SaveFile(const wxString
& filename
,
1064 const wxPalette
*palette
)
1066 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1070 return handler
->SaveFile(this, filename
, type
, palette
);
1072 #if wxUSE_IMAGE && wxUSE_WXDIB
1073 else // no bitmap handler found
1075 // FIXME what about palette? shouldn't we use it?
1076 wxImage image
= ConvertToImage();
1079 return image
.SaveFile(filename
, type
);
1082 #endif // wxUSE_IMAGE
1087 // ----------------------------------------------------------------------------
1088 // sub bitmap extraction
1089 // ----------------------------------------------------------------------------
1091 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1093 wxCHECK_MSG( Ok() &&
1094 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1095 (rect
.x
+rect
.width
<= GetWidth()) &&
1096 (rect
.y
+rect
.height
<= GetHeight()),
1097 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1099 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1100 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1102 #ifndef __WXMICROWIN__
1103 // handle alpha channel, if any
1112 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1113 selectDst(dcDst
, GetHbitmapOf(ret
));
1115 if ( !selectSrc
|| !selectDst
)
1117 wxLogLastError(_T("SelectObjct(hBitmap)"));
1120 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1121 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1123 wxLogLastError(_T("BitBlt"));
1127 // copy mask if there is one
1130 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1132 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1133 selectDst(dcDst
, hbmpMask
);
1135 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1136 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1138 wxLogLastError(_T("BitBlt"));
1141 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1144 #endif // !__WXMICROWIN__
1149 // ----------------------------------------------------------------------------
1150 // wxBitmap accessors
1151 // ----------------------------------------------------------------------------
1154 wxPalette
* wxBitmap::GetPalette() const
1156 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1157 : (wxPalette
*) NULL
;
1161 wxMask
*wxBitmap::GetMask() const
1163 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1166 wxBitmap
wxBitmap::GetMaskBitmap() const
1169 wxMask
*mask
= GetMask();
1171 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1177 wxDC
*wxBitmap::GetSelectedInto() const
1179 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1184 void wxBitmap::UseAlpha()
1186 if ( GetBitmapData() )
1187 GetBitmapData()->m_hasAlpha
= true;
1190 bool wxBitmap::HasAlpha() const
1192 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1201 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1203 if ( GetBitmapData() )
1204 GetBitmapData()->m_selectedInto
= dc
;
1211 void wxBitmap::SetPalette(const wxPalette
& palette
)
1215 GetBitmapData()->m_bitmapPalette
= palette
;
1218 #endif // wxUSE_PALETTE
1220 void wxBitmap::SetMask(wxMask
*mask
)
1224 GetBitmapData()->SetMask(mask
);
1227 // ----------------------------------------------------------------------------
1228 // raw bitmap access support
1229 // ----------------------------------------------------------------------------
1231 #ifdef wxHAVE_RAW_BITMAP
1232 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1237 // no bitmap, no data (raw or otherwise)
1241 // if we're already a DIB we can access our data directly, but if not we
1242 // need to convert this DDB to a DIB section and use it for raw access and
1243 // then convert it back
1245 if ( !GetBitmapData()->m_isDIB
)
1247 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1248 _T("GetRawData() may be called only once") );
1250 wxDIB
*dib
= new wxDIB(*this);
1258 // we'll free it in UngetRawData()
1259 GetBitmapData()->m_dib
= dib
;
1261 hDIB
= dib
->GetHandle();
1265 hDIB
= GetHbitmap();
1269 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1271 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1276 // check that the bitmap is in correct format
1277 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1279 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1284 // ok, store the relevant info in wxPixelDataBase
1285 const LONG h
= ds
.dsBm
.bmHeight
;
1287 data
.m_width
= ds
.dsBm
.bmWidth
;
1290 // remember that DIBs are stored in top to bottom order!
1291 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1292 // multiple of 2, as required by the documentation. So we use the official
1293 // formula, which we already use elsewhere.)
1294 const LONG bytesPerRow
=
1295 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1296 data
.m_stride
= -bytesPerRow
;
1298 char *bits
= (char *)ds
.dsBm
.bmBits
;
1301 bits
+= (h
- 1)*bytesPerRow
;
1310 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1318 // invalid data, don't crash -- but don't assert neither as we're
1319 // called automatically from wxPixelDataBase dtor and so there is no
1320 // way to prevent this from happening
1324 // if we're a DDB we need to convert DIB back to DDB now to make the
1325 // changes made via raw bitmap access effective
1326 if ( !GetBitmapData()->m_isDIB
)
1328 wxDIB
*dib
= GetBitmapData()->m_dib
;
1329 GetBitmapData()->m_dib
= NULL
;
1335 #endif // wxUSE_WXDIB
1337 #endif // #ifdef wxHAVE_RAW_BITMAP
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1349 wxMask::wxMask(const wxMask
&mask
)
1354 HDC srcDC
= CreateCompatibleDC(0);
1355 HDC destDC
= CreateCompatibleDC(0);
1357 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1358 // so we'll use GetObject() API here:
1359 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1361 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1365 // create our HBITMAP
1366 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1367 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1369 // copy the mask's HBITMAP into our HBITMAP
1370 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1371 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1373 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1375 SelectObject(srcDC
, 0);
1377 SelectObject(destDC
, 0);
1381 // Construct a mask from a bitmap and a colour indicating
1382 // the transparent area
1383 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1386 Create(bitmap
, colour
);
1389 // Construct a mask from a bitmap and a palette index indicating
1390 // the transparent area
1391 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1394 Create(bitmap
, paletteIndex
);
1397 // Construct a mask from a mono bitmap (copies the bitmap).
1398 wxMask::wxMask(const wxBitmap
& bitmap
)
1407 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1410 // Create a mask from a mono bitmap (copies the bitmap).
1411 bool wxMask::Create(const wxBitmap
& bitmap
)
1413 #ifndef __WXMICROWIN__
1414 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1415 _T("can't create mask from invalid or not monochrome bitmap") );
1419 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1423 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1428 HDC srcDC
= CreateCompatibleDC(0);
1429 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1430 HDC destDC
= CreateCompatibleDC(0);
1431 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1432 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1433 SelectObject(srcDC
, 0);
1435 SelectObject(destDC
, 0);
1439 wxUnusedVar(bitmap
);
1444 // Create a mask from a bitmap and a palette index indicating
1445 // the transparent area
1446 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1450 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1455 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1457 unsigned char red
, green
, blue
;
1458 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1460 wxColour
transparentColour(red
, green
, blue
);
1461 return Create(bitmap
, transparentColour
);
1464 #endif // wxUSE_PALETTE
1469 // Create a mask from a bitmap and a colour indicating
1470 // the transparent area
1471 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1473 #ifndef __WXMICROWIN__
1474 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1478 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1482 int width
= bitmap
.GetWidth(),
1483 height
= bitmap
.GetHeight();
1485 // scan the bitmap for the transparent colour and set the corresponding
1486 // pixels in the mask to BLACK and the rest to WHITE
1487 COLORREF maskColour
= wxColourToPalRGB(colour
);
1488 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1490 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1491 HDC destDC
= ::CreateCompatibleDC(NULL
);
1492 if ( !srcDC
|| !destDC
)
1494 wxLogLastError(wxT("CreateCompatibleDC"));
1499 // SelectObject() will fail
1500 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1501 _T("bitmap can't be selected in another DC") );
1503 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1506 wxLogLastError(wxT("SelectObject"));
1511 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1514 wxLogLastError(wxT("SelectObject"));
1521 // this will create a monochrome bitmap with 0 points for the pixels
1522 // which have the same value as the background colour and 1 for the
1524 ::SetBkColor(srcDC
, maskColour
);
1525 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1528 ::SelectObject(srcDC
, hbmpSrcOld
);
1530 ::SelectObject(destDC
, hbmpDstOld
);
1534 #else // __WXMICROWIN__
1535 wxUnusedVar(bitmap
);
1536 wxUnusedVar(colour
);
1538 #endif // __WXMICROWIN__/!__WXMICROWIN__
1541 // ----------------------------------------------------------------------------
1543 // ----------------------------------------------------------------------------
1545 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1548 int width
, int height
, int depth
)
1550 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1552 return bitmap
&& Create(bitmap
, data
, flags
, width
, height
, depth
);
1555 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1556 const wxString
& name
,
1558 int width
, int height
)
1560 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1562 return bitmap
&& LoadFile(bitmap
, name
, flags
, width
, height
);
1565 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1566 const wxString
& name
,
1569 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1571 return bitmap
&& SaveFile(bitmap
, name
, type
);
1574 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1575 const void* WXUNUSED(data
),
1576 long WXUNUSED(type
),
1577 int WXUNUSED(width
),
1578 int WXUNUSED(height
),
1579 int WXUNUSED(depth
))
1584 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1585 const wxString
& WXUNUSED(name
),
1586 long WXUNUSED(type
),
1587 int WXUNUSED(desiredWidth
),
1588 int WXUNUSED(desiredHeight
))
1593 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1594 const wxString
& WXUNUSED(name
),
1596 const wxPalette
*WXUNUSED(palette
))
1601 // ----------------------------------------------------------------------------
1602 // global helper functions implemented here
1603 // ----------------------------------------------------------------------------
1605 // helper of wxBitmapToHICON/HCURSOR
1607 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1614 // we can't create an icon/cursor form nothing
1618 if ( bmp
.HasAlpha() )
1620 // Create an empty mask bitmap.
1621 // it doesn't seem to work if we mess with the mask at all.
1622 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1625 wxZeroMemory(iconInfo
);
1626 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1629 iconInfo
.xHotspot
= hotSpotX
;
1630 iconInfo
.yHotspot
= hotSpotY
;
1633 iconInfo
.hbmMask
= hMonoBitmap
;
1634 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1636 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1638 ::DeleteObject(hMonoBitmap
);
1643 wxMask
* mask
= bmp
.GetMask();
1647 // we must have a mask for an icon, so even if it's probably incorrect,
1648 // do create it (grey is the "standard" transparent colour)
1649 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1653 wxZeroMemory(iconInfo
);
1654 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1657 iconInfo
.xHotspot
= hotSpotX
;
1658 iconInfo
.yHotspot
= hotSpotY
;
1661 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1662 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1664 // black out the transparent area to preserve background colour, because
1665 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1666 // the mask to the dest rect.
1668 MemoryHDC dcSrc
, dcDst
;
1669 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1670 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1672 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1673 dcSrc
, 0, 0, SRCAND
) )
1675 wxLogLastError(_T("BitBlt"));
1679 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1681 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1683 // we created the mask, now delete it
1687 // delete the inverted mask bitmap we created as well
1688 ::DeleteObject(iconInfo
.hbmMask
);
1693 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1695 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1698 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1700 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1703 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1705 #ifndef __WXMICROWIN__
1706 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1708 // get width/height from the bitmap if not given
1712 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1717 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1718 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1719 if ( !hdcSrc
|| !hdcDst
)
1721 wxLogLastError(wxT("CreateCompatibleDC"));
1724 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1727 wxLogLastError(wxT("CreateBitmap"));
1730 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1731 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1732 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1736 wxLogLastError(wxT("BitBlt"));
1740 SelectObject(hdcSrc
,srcTmp
);
1741 SelectObject(hdcDst
,dstTmp
);