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"
43 #include "wx/msw/dc.h"
46 #include "wx/msw/dib.h"
49 #ifdef wxHAS_RAW_BITMAP
50 #include "wx/rawbmp.h"
53 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
66 wxBitmapRefData(const wxBitmapRefData
& data
);
67 virtual ~wxBitmapRefData() { Free(); }
71 // set the mask object to use as the mask, we take ownership of it
72 void SetMask(wxMask
*mask
)
78 // set the HBITMAP to use as the mask
79 void SetMask(HBITMAP hbmpMask
)
81 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
85 wxMask
*GetMask() const { return m_bitmapMask
; }
89 wxPalette m_bitmapPalette
;
90 #endif // wxUSE_PALETTE
96 // this field is solely for error checking: we detect selecting a bitmap
97 // into more than one DC at once or deleting a bitmap still selected into a
98 // DC (both are serious programming errors under Windows)
100 #endif // __WXDEBUG__
103 // when GetRawData() is called for a DDB we need to convert it to a DIB
104 // first to be able to provide direct access to it and we cache that DIB
105 // here and convert it back to DDB when UngetRawData() is called
109 // true if we have alpha transparency info and can be drawn using
113 // true if our HBITMAP is a DIB section, false if it is a DDB
117 // optional mask for transparent drawing
118 wxMask
*m_bitmapMask
;
122 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
130 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
132 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 // decide whether we should create a DIB or a DDB for the given parameters
144 // NB: we always use DIBs under Windows CE as this is much simpler (even if
145 // also less efficient...) and we obviously can't use them if there is no
146 // DIB support compiled in at all
148 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
150 #define ALWAYS_USE_DIB
152 // no sense in defining wxShouldCreateDIB() as we can't compile code
153 // executed if it is true, so we have to use #if's anyhow
154 #define NEVER_USE_DIB
155 #else // wxUSE_WXDIB && !__WXWINCE__
156 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
158 // here is the logic:
160 // (a) if hdc is specified, the caller explicitly wants DDB
161 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
162 // or less DIBs anyhow)
163 // (c) finally, create DIBs under Win9x even if the depth hasn't been
164 // explicitly specified but the current display depth is 24 or
165 // more and the image is "big", i.e. > 16Mb which is the
166 // theoretical limit for DDBs under Win9x
168 // consequences (all of which seem to make sense):
170 // (i) by default, DDBs are created (depth == -1 usually)
171 // (ii) DIBs can be created by explicitly specifying the depth
172 // (iii) using a DC always forces creating a DDB
176 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
179 #define SOMETIMES_USE_DIB
180 #endif // different DIB usage scenarious
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxBitmapRefData::wxBitmapRefData()
189 m_selectedInto
= NULL
;
193 m_hBitmap
= (WXHBITMAP
) NULL
;
202 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
203 : wxGDIImageRefData(data
)
206 m_selectedInto
= NULL
;
209 // (deep) copy the mask if present
211 if (data
.m_bitmapMask
)
212 m_bitmapMask
= new wxMask(*data
.m_bitmapMask
);
214 // FIXME: we don't copy m_hBitmap currently but we should, see wxBitmap::
217 wxASSERT_MSG( !data
.m_isDIB
,
218 _T("can't copy bitmap locked for raw access!") );
221 m_hasAlpha
= data
.m_hasAlpha
;
224 void wxBitmapRefData::Free()
226 wxASSERT_MSG( !m_selectedInto
,
227 wxT("deleting bitmap still selected into wxMemoryDC") );
230 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
235 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
237 wxLogLastError(wxT("DeleteObject(hbitmap)"));
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 wxGDIImageRefData
*wxBitmap::CreateData() const
251 return new wxBitmapRefData
;
254 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*dataOrig
) const
256 const wxBitmapRefData
*
257 data
= static_cast<const wxBitmapRefData
*>(dataOrig
);
261 // FIXME: this method is backwards, it should just create a new
262 // wxBitmapRefData using its copy ctor but instead it modifies this
263 // bitmap itself and then returns its m_refData -- which works, of
264 // course (except in !wxUSE_WXDIB), but is completely illogical
265 wxBitmap
*self
= const_cast<wxBitmap
*>(this);
267 wxBitmapRefData
*selfdata
;
269 // copy the other bitmap
270 if ( data
->m_hBitmap
)
272 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
273 self
->CopyFromDIB(dib
);
275 selfdata
= static_cast<wxBitmapRefData
*>(m_refData
);
276 selfdata
->m_hasAlpha
= data
->m_hasAlpha
;
279 #endif // wxUSE_WXDIB
281 // copy the bitmap data
282 selfdata
= new wxBitmapRefData(*data
);
283 self
->m_refData
= selfdata
;
286 // copy also the mask
287 wxMask
* const maskSrc
= data
->GetMask();
290 selfdata
->SetMask(new wxMask(*maskSrc
));
296 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
,
297 wxBitmapTransparency transp
)
299 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
300 // it may be either HICON or HCURSOR
301 HICON hicon
= (HICON
)icon
.GetHandle();
304 if ( !::GetIconInfo(hicon
, &iconInfo
) )
306 wxLogLastError(wxT("GetIconInfo"));
311 wxBitmapRefData
*refData
= new wxBitmapRefData
;
314 int w
= icon
.GetWidth(),
315 h
= icon
.GetHeight();
317 refData
->m_width
= w
;
318 refData
->m_height
= h
;
319 refData
->m_depth
= wxDisplayDepth();
321 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
326 wxFAIL_MSG( _T("unknown wxBitmapTransparency value") );
328 case wxBitmapTransparency_None
:
329 // nothing to do, refData->m_hasAlpha is false by default
332 case wxBitmapTransparency_Auto
:
334 // If the icon is 32 bits per pixel then it may have alpha channel
335 // data, although there are some icons that are 32 bpp but have no
336 // alpha... So convert to a DIB and manually check the 4th byte for
340 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(bm
), &bm
) &&
341 (bm
.bmBitsPixel
== 32) )
343 wxDIB
dib(iconInfo
.hbmColor
);
346 const unsigned char* pixels
= dib
.GetData();
347 for (int idx
= 0; idx
< w
*h
*4; idx
+=4)
349 if (pixels
[idx
+3] != 0)
351 // If there is an alpha byte that is non-zero
352 // then set the alpha flag and stop checking
353 refData
->m_hasAlpha
= true;
361 #endif // wxUSE_WXDIB
363 case wxBitmapTransparency_Always
:
364 refData
->m_hasAlpha
= true;
368 if ( !refData
->m_hasAlpha
)
370 // the mask returned by GetIconInfo() is inverted compared to the usual
372 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
375 // delete the old one now as we don't need it any more
376 ::DeleteObject(iconInfo
.hbmMask
);
379 #else // __WXMICROWIN__ || __WXWINCE__
384 #endif // !__WXWINCE__/__WXWINCE__
387 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
394 return CopyFromIconOrCursor(cursor
, transp
);
397 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
404 return CopyFromIconOrCursor(icon
, transp
);
407 #ifndef NEVER_USE_DIB
409 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
411 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
413 #ifdef SOMETIMES_USE_DIB
414 HBITMAP hbitmap
= dib
.CreateDDB();
417 #else // ALWAYS_USE_DIB
418 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
419 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
423 wxBitmapRefData
*refData
= new wxBitmapRefData
;
426 refData
->m_width
= dib
.GetWidth();
427 refData
->m_height
= dib
.GetHeight();
428 refData
->m_depth
= dib
.GetDepth();
430 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
433 wxPalette
*palette
= dib
.CreatePalette();
436 refData
->m_bitmapPalette
= *palette
;
440 #endif // wxUSE_PALETTE
445 #endif // NEVER_USE_DIB
447 wxBitmap::~wxBitmap()
451 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
453 #ifndef __WXMICROWIN__
454 wxBitmapRefData
*refData
= new wxBitmapRefData
;
457 refData
->m_width
= width
;
458 refData
->m_height
= height
;
459 refData
->m_depth
= depth
;
464 // we assume that it is in XBM format which is not quite the same as
465 // the format CreateBitmap() wants because the order of bytes in the
467 const size_t bytesPerLine
= (width
+ 7) / 8;
468 const size_t padding
= bytesPerLine
% 2;
469 const size_t len
= height
* ( padding
+ bytesPerLine
);
470 data
= (char *)malloc(len
);
471 const char *src
= bits
;
474 for ( int rows
= 0; rows
< height
; rows
++ )
476 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
478 unsigned char val
= *src
++;
479 unsigned char reversed
= 0;
481 for ( int bits
= 0; bits
< 8; bits
++)
484 reversed
|= (unsigned char)(val
& 0x01);
496 // bits should already be in Windows standard format
497 data
= (char *)bits
; // const_cast is harmless
500 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
503 wxLogLastError(wxT("CreateBitmap"));
511 SetHBITMAP((WXHBITMAP
)hbmp
);
515 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
517 (void)Create(w
, h
, dc
);
520 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
522 (void)Create(data
, type
, width
, height
, depth
);
525 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
527 LoadFile(filename
, type
);
530 bool wxBitmap::Create(int width
, int height
, int depth
)
532 return DoCreate(width
, height
, depth
, 0);
535 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
537 wxCHECK_MSG( dc
.IsOk(), false, _T("invalid HDC in wxBitmap::Create()") );
539 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
542 return DoCreate(width
, height
, -1, impl
->GetHDC());
547 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
551 m_refData
= new wxBitmapRefData
;
553 GetBitmapData()->m_width
= w
;
554 GetBitmapData()->m_height
= h
;
556 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
558 #ifndef NEVER_USE_DIB
559 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
563 // create DIBs without alpha channel by default
571 // don't delete the DIB section in dib object dtor
574 GetBitmapData()->m_isDIB
= true;
575 GetBitmapData()->m_depth
= d
;
578 #endif // NEVER_USE_DIB
580 #ifndef ALWAYS_USE_DIB
581 #ifndef __WXMICROWIN__
584 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
587 wxLogLastError(wxT("CreateBitmap"));
590 GetBitmapData()->m_depth
= d
;
592 else // d == 0, create bitmap compatible with the screen
593 #endif // !__WXMICROWIN__
596 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
599 wxLogLastError(wxT("CreateCompatibleBitmap"));
602 GetBitmapData()->m_depth
= wxDisplayDepth();
604 #endif // !ALWAYS_USE_DIB
607 SetHBITMAP((WXHBITMAP
)hbmp
);
614 // ----------------------------------------------------------------------------
615 // wxImage to/from conversions for Microwin
616 // ----------------------------------------------------------------------------
618 // Microwin versions are so different from normal ones that it really doesn't
619 // make sense to use #ifdefs inside the function bodies
620 #ifdef __WXMICROWIN__
622 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
624 // Set this to 1 to experiment with mask code,
625 // which currently doesn't work
628 m_refData
= new wxBitmapRefData();
630 // Initial attempt at a simple-minded implementation.
631 // The bitmap will always be created at the screen depth,
632 // so the 'depth' argument is ignored.
634 HDC hScreenDC
= ::GetDC(NULL
);
635 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
637 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
638 HBITMAP hMaskBitmap
= NULL
;
639 HBITMAP hOldMaskBitmap
= NULL
;
641 unsigned char maskR
= 0;
642 unsigned char maskG
= 0;
643 unsigned char maskB
= 0;
645 // printf("Created bitmap %d\n", (int) hBitmap);
648 ::ReleaseDC(NULL
, hScreenDC
);
651 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
653 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
654 ::ReleaseDC(NULL
, hScreenDC
);
656 // created an mono-bitmap for the possible mask
657 bool hasMask
= image
.HasMask();
662 // FIXME: we should be able to pass bpp = 1, but
663 // GdBlit can't handle a different depth
665 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
667 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
669 maskR
= image
.GetMaskRed();
670 maskG
= image
.GetMaskGreen();
671 maskB
= image
.GetMaskBlue();
679 hScreenDC
= ::GetDC(NULL
);
680 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
681 ::ReleaseDC(NULL
, hScreenDC
);
683 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
691 for (i
= 0; i
< image
.GetWidth(); i
++)
693 for (j
= 0; j
< image
.GetHeight(); j
++)
695 unsigned char red
= image
.GetRed(i
, j
);
696 unsigned char green
= image
.GetGreen(i
, j
);
697 unsigned char blue
= image
.GetBlue(i
, j
);
699 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
703 // scan the bitmap for the transparent colour and set the corresponding
704 // pixels in the mask to BLACK and the rest to WHITE
705 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
706 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
708 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
713 ::SelectObject(hMemDC
, hOldBitmap
);
717 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
720 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
723 SetWidth(image
.GetWidth());
724 SetHeight(image
.GetHeight());
725 SetDepth(screenDepth
);
726 SetHBITMAP( (WXHBITMAP
) hBitmap
);
729 // Copy the palette from the source image
730 SetPalette(image
.GetPalette());
731 #endif // wxUSE_PALETTE
736 wxImage
wxBitmap::ConvertToImage() const
738 // Initial attempt at a simple-minded implementation.
739 // The bitmap will always be created at the screen depth,
740 // so the 'depth' argument is ignored.
741 // TODO: transparency (create a mask image)
745 wxFAIL_MSG( wxT("bitmap is invalid") );
751 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
753 // create an wxImage object
754 int width
= GetWidth();
755 int height
= GetHeight();
756 image
.Create( width
, height
);
757 unsigned char *data
= image
.GetData();
760 wxFAIL_MSG( wxT("could not allocate data for image") );
764 HDC hScreenDC
= ::GetDC(NULL
);
766 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
767 ::ReleaseDC(NULL
, hScreenDC
);
769 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
771 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
774 for (i
= 0; i
< GetWidth(); i
++)
776 for (j
= 0; j
< GetHeight(); j
++)
778 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
779 unsigned char red
= GetRValue(color
);
780 unsigned char green
= GetGValue(color
);
781 unsigned char blue
= GetBValue(color
);
783 image
.SetRGB(i
, j
, red
, green
, blue
);
787 ::SelectObject(hMemDC
, hOldBitmap
);
791 // Copy the palette from the source image
793 image
.SetPalette(* GetPalette());
794 #endif // wxUSE_PALETTE
799 #endif // __WXMICROWIN__
801 // ----------------------------------------------------------------------------
802 // wxImage to/from conversions
803 // ----------------------------------------------------------------------------
805 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
807 return CreateFromImage(image
, depth
, 0);
810 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
812 wxCHECK_MSG( dc
.IsOk(), false,
813 _T("invalid HDC in wxBitmap::CreateFromImage()") );
815 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
818 return CreateFromImage(image
, -1, impl
->GetHDC());
825 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
827 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
831 // first convert the image to DIB
832 const int h
= image
.GetHeight();
833 const int w
= image
.GetWidth();
839 const bool hasAlpha
= image
.HasAlpha();
842 depth
= dib
.GetDepth();
844 // store the bitmap parameters
845 wxBitmapRefData
* const refData
= new wxBitmapRefData
;
846 refData
->m_width
= w
;
847 refData
->m_height
= h
;
848 refData
->m_hasAlpha
= hasAlpha
;
849 refData
->m_depth
= depth
;
854 // next either store DIB as is or create a DDB from it
855 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
857 // are we going to use DIB?
859 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
860 if ( hasAlpha
|| wxShouldCreateDIB(w
, h
, depth
, hdc
) )
862 // don't delete the DIB section in dib object dtor
863 hbitmap
= dib
.Detach();
865 refData
->m_isDIB
= true;
867 #ifndef ALWAYS_USE_DIB
868 else // we need to convert DIB to DDB
870 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
872 #endif // !ALWAYS_USE_DIB
874 // validate this object
875 SetHBITMAP((WXHBITMAP
)hbitmap
);
877 // finally also set the mask if we have one
878 if ( image
.HasMask() )
880 const size_t len
= 2*((w
+15)/16);
881 BYTE
*src
= image
.GetData();
882 BYTE
*data
= new BYTE
[h
*len
];
883 memset(data
, 0, h
*len
);
884 BYTE r
= image
.GetMaskRed(),
885 g
= image
.GetMaskGreen(),
886 b
= image
.GetMaskBlue();
888 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
892 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
894 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
897 if ( (mask
>>= 1) == 0 )
905 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
908 wxLogLastError(_T("CreateBitmap(mask)"));
912 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
921 wxImage
wxBitmap::ConvertToImage() const
923 // convert DDB to DIB
931 // and then DIB to our wxImage
932 wxImage image
= dib
.ConvertToImage();
938 // now do the same for the mask, if we have any
939 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
942 wxDIB
dibMask(hbmpMask
);
943 if ( dibMask
.IsOk() )
945 // TODO: use wxRawBitmap to iterate over DIB
947 // we hard code the mask colour for now but we could also make an
948 // effort (and waste time) to choose a colour not present in the
949 // image already to avoid having to fudge the pixels below --
950 // whether it's worth to do it is unclear however
951 static const int MASK_RED
= 1;
952 static const int MASK_GREEN
= 2;
953 static const int MASK_BLUE
= 3;
954 static const int MASK_BLUE_REPLACEMENT
= 2;
956 const int h
= dibMask
.GetHeight();
957 const int w
= dibMask
.GetWidth();
958 const int bpp
= dibMask
.GetDepth();
959 const int maskBytesPerPixel
= bpp
>> 3;
960 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
961 unsigned char *data
= image
.GetData();
963 // remember that DIBs are stored in bottom to top order
965 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
967 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
969 // traverse one mask DIB line
970 unsigned char *mask
= maskLineStart
;
971 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
973 // should this pixel be transparent?
976 // no, check that it isn't transparent by accident
977 if ( (data
[0] == MASK_RED
) &&
978 (data
[1] == MASK_GREEN
) &&
979 (data
[2] == MASK_BLUE
) )
981 // we have to fudge the colour a bit to prevent
982 // this pixel from appearing transparent
983 data
[2] = MASK_BLUE_REPLACEMENT
;
988 else // yes, transparent pixel
991 *data
++ = MASK_GREEN
;
997 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1004 #else // !wxUSE_WXDIB
1007 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1008 int WXUNUSED(depth
),
1009 WXHDC
WXUNUSED(hdc
))
1014 wxImage
wxBitmap::ConvertToImage() const
1019 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1021 #endif // wxUSE_IMAGE
1023 // ----------------------------------------------------------------------------
1024 // loading and saving bitmaps
1025 // ----------------------------------------------------------------------------
1027 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1031 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1035 m_refData
= new wxBitmapRefData
;
1037 return handler
->LoadFile(this, filename
, type
, -1, -1);
1039 #if wxUSE_IMAGE && wxUSE_WXDIB
1040 else // no bitmap handler found
1043 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1045 *this = wxBitmap(image
);
1050 #endif // wxUSE_IMAGE
1055 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1059 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1063 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1068 m_refData
= new wxBitmapRefData
;
1070 return handler
->Create(this, data
, type
, width
, height
, depth
);
1073 bool wxBitmap::SaveFile(const wxString
& filename
,
1075 const wxPalette
*palette
) const
1077 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1081 return handler
->SaveFile(this, filename
, type
, palette
);
1083 #if wxUSE_IMAGE && wxUSE_WXDIB
1084 else // no bitmap handler found
1086 // FIXME what about palette? shouldn't we use it?
1087 wxImage image
= ConvertToImage();
1090 return image
.SaveFile(filename
, type
);
1093 #endif // wxUSE_IMAGE
1098 // ----------------------------------------------------------------------------
1099 // sub bitmap extraction
1100 // ----------------------------------------------------------------------------
1101 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1104 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1105 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1108 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1110 wxCHECK_MSG( Ok() &&
1111 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1112 (rect
.x
+rect
.width
<= GetWidth()) &&
1113 (rect
.y
+rect
.height
<= GetHeight()),
1114 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1116 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1117 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1119 #ifndef __WXMICROWIN__
1120 // handle alpha channel, if any
1129 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1133 wxLogLastError(_T("SelectObject(destBitmap)"));
1136 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1137 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1139 wxLogLastError(_T("BitBlt"));
1143 // copy mask if there is one
1146 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1148 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1149 selectDst(dcDst
, hbmpMask
);
1151 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1152 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1154 wxLogLastError(_T("BitBlt"));
1157 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1160 #endif // !__WXMICROWIN__
1165 // ----------------------------------------------------------------------------
1166 // wxBitmap accessors
1167 // ----------------------------------------------------------------------------
1170 wxPalette
* wxBitmap::GetPalette() const
1172 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1177 wxMask
*wxBitmap::GetMask() const
1179 return GetBitmapData() ? GetBitmapData()->GetMask() : NULL
;
1182 wxBitmap
wxBitmap::GetMaskBitmap() const
1185 wxMask
*mask
= GetMask();
1187 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1193 wxDC
*wxBitmap::GetSelectedInto() const
1195 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: NULL
;
1200 void wxBitmap::UseAlpha()
1202 if ( GetBitmapData() )
1203 GetBitmapData()->m_hasAlpha
= true;
1206 bool wxBitmap::HasAlpha() const
1208 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1211 // ----------------------------------------------------------------------------
1213 // ----------------------------------------------------------------------------
1217 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1219 if ( GetBitmapData() )
1220 GetBitmapData()->m_selectedInto
= dc
;
1227 void wxBitmap::SetPalette(const wxPalette
& palette
)
1231 GetBitmapData()->m_bitmapPalette
= palette
;
1234 #endif // wxUSE_PALETTE
1236 void wxBitmap::SetMask(wxMask
*mask
)
1240 GetBitmapData()->SetMask(mask
);
1243 // ----------------------------------------------------------------------------
1244 // raw bitmap access support
1245 // ----------------------------------------------------------------------------
1247 #ifdef wxHAS_RAW_BITMAP
1249 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1254 // no bitmap, no data (raw or otherwise)
1258 // if we're already a DIB we can access our data directly, but if not we
1259 // need to convert this DDB to a DIB section and use it for raw access and
1260 // then convert it back
1262 if ( !GetBitmapData()->m_isDIB
)
1264 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1265 _T("GetRawData() may be called only once") );
1267 wxDIB
*dib
= new wxDIB(*this);
1275 // we'll free it in UngetRawData()
1276 GetBitmapData()->m_dib
= dib
;
1278 hDIB
= dib
->GetHandle();
1282 hDIB
= GetHbitmap();
1286 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1288 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1293 // check that the bitmap is in correct format
1294 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1296 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1301 // ok, store the relevant info in wxPixelDataBase
1302 const LONG h
= ds
.dsBm
.bmHeight
;
1304 data
.m_width
= ds
.dsBm
.bmWidth
;
1307 // remember that DIBs are stored in top to bottom order!
1308 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1309 // multiple of 2, as required by the documentation. So we use the official
1310 // formula, which we already use elsewhere.)
1311 const LONG bytesPerRow
=
1312 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1313 data
.m_stride
= -bytesPerRow
;
1315 char *bits
= (char *)ds
.dsBm
.bmBits
;
1318 bits
+= (h
- 1)*bytesPerRow
;
1327 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1335 // invalid data, don't crash -- but don't assert neither as we're
1336 // called automatically from wxPixelDataBase dtor and so there is no
1337 // way to prevent this from happening
1341 // if we're a DDB we need to convert DIB back to DDB now to make the
1342 // changes made via raw bitmap access effective
1343 if ( !GetBitmapData()->m_isDIB
)
1345 wxDIB
*dib
= GetBitmapData()->m_dib
;
1346 GetBitmapData()->m_dib
= NULL
;
1352 #endif // wxUSE_WXDIB
1354 #endif // wxHAS_RAW_BITMAP
1356 // ----------------------------------------------------------------------------
1358 // ----------------------------------------------------------------------------
1366 wxMask::wxMask(const wxMask
&mask
)
1371 HDC srcDC
= CreateCompatibleDC(0);
1372 HDC destDC
= CreateCompatibleDC(0);
1374 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1375 // so we'll use GetObject() API here:
1376 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1378 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1382 // create our HBITMAP
1383 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1384 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1386 // copy the mask's HBITMAP into our HBITMAP
1387 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1388 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1390 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1392 SelectObject(srcDC
, 0);
1394 SelectObject(destDC
, 0);
1398 // Construct a mask from a bitmap and a colour indicating
1399 // the transparent area
1400 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1403 Create(bitmap
, colour
);
1406 // Construct a mask from a bitmap and a palette index indicating
1407 // the transparent area
1408 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1411 Create(bitmap
, paletteIndex
);
1414 // Construct a mask from a mono bitmap (copies the bitmap).
1415 wxMask::wxMask(const wxBitmap
& bitmap
)
1424 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1427 // Create a mask from a mono bitmap (copies the bitmap).
1428 bool wxMask::Create(const wxBitmap
& bitmap
)
1430 #ifndef __WXMICROWIN__
1431 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1432 _T("can't create mask from invalid or not monochrome bitmap") );
1436 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1440 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1445 HDC srcDC
= CreateCompatibleDC(0);
1446 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1447 HDC destDC
= CreateCompatibleDC(0);
1448 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1449 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1450 SelectObject(srcDC
, 0);
1452 SelectObject(destDC
, 0);
1456 wxUnusedVar(bitmap
);
1461 // Create a mask from a bitmap and a palette index indicating
1462 // the transparent area
1463 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1467 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1472 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1474 unsigned char red
, green
, blue
;
1475 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1477 wxColour
transparentColour(red
, green
, blue
);
1478 return Create(bitmap
, transparentColour
);
1481 #endif // wxUSE_PALETTE
1486 // Create a mask from a bitmap and a colour indicating
1487 // the transparent area
1488 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1490 #ifndef __WXMICROWIN__
1491 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1495 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1499 int width
= bitmap
.GetWidth(),
1500 height
= bitmap
.GetHeight();
1502 // scan the bitmap for the transparent colour and set the corresponding
1503 // pixels in the mask to BLACK and the rest to WHITE
1504 COLORREF maskColour
= wxColourToPalRGB(colour
);
1505 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1507 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1508 HDC destDC
= ::CreateCompatibleDC(NULL
);
1509 if ( !srcDC
|| !destDC
)
1511 wxLogLastError(wxT("CreateCompatibleDC"));
1516 // SelectObject() will fail
1517 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1518 _T("bitmap can't be selected in another DC") );
1520 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1523 wxLogLastError(wxT("SelectObject"));
1528 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1531 wxLogLastError(wxT("SelectObject"));
1538 // this will create a monochrome bitmap with 0 points for the pixels
1539 // which have the same value as the background colour and 1 for the
1541 ::SetBkColor(srcDC
, maskColour
);
1542 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1545 ::SelectObject(srcDC
, hbmpSrcOld
);
1547 ::SelectObject(destDC
, hbmpDstOld
);
1551 #else // __WXMICROWIN__
1552 wxUnusedVar(bitmap
);
1553 wxUnusedVar(colour
);
1555 #endif // __WXMICROWIN__/!__WXMICROWIN__
1558 // ----------------------------------------------------------------------------
1560 // ----------------------------------------------------------------------------
1562 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1565 int width
, int height
, int depth
)
1567 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1569 return bitmap
&& Create(bitmap
, data
, type
, width
, height
, depth
);
1572 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1573 const wxString
& name
,
1575 int width
, int height
)
1577 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1579 return bitmap
&& LoadFile(bitmap
, name
, type
, width
, height
);
1582 bool wxBitmapHandler::Save(const wxGDIImage
*image
,
1583 const wxString
& name
,
1584 wxBitmapType type
) const
1586 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1588 return bitmap
&& SaveFile(bitmap
, name
, type
);
1591 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1592 const void* WXUNUSED(data
),
1593 wxBitmapType
WXUNUSED(type
),
1594 int WXUNUSED(width
),
1595 int WXUNUSED(height
),
1596 int WXUNUSED(depth
))
1601 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1602 const wxString
& WXUNUSED(name
),
1603 wxBitmapType
WXUNUSED(type
),
1604 int WXUNUSED(desiredWidth
),
1605 int WXUNUSED(desiredHeight
))
1610 bool wxBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1611 const wxString
& WXUNUSED(name
),
1612 wxBitmapType
WXUNUSED(type
),
1613 const wxPalette
*WXUNUSED(palette
)) const
1618 // ----------------------------------------------------------------------------
1619 // global helper functions implemented here
1620 // ----------------------------------------------------------------------------
1622 // helper of wxBitmapToHICON/HCURSOR
1624 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1631 // we can't create an icon/cursor form nothing
1635 if ( bmp
.HasAlpha() )
1637 // Create an empty mask bitmap.
1638 // it doesn't seem to work if we mess with the mask at all.
1639 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1642 wxZeroMemory(iconInfo
);
1643 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1646 iconInfo
.xHotspot
= hotSpotX
;
1647 iconInfo
.yHotspot
= hotSpotY
;
1650 iconInfo
.hbmMask
= hMonoBitmap
;
1651 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1653 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1655 ::DeleteObject(hMonoBitmap
);
1660 wxMask
* mask
= bmp
.GetMask();
1664 // we must have a mask for an icon, so even if it's probably incorrect,
1665 // do create it (grey is the "standard" transparent colour)
1666 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1670 wxZeroMemory(iconInfo
);
1671 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1674 iconInfo
.xHotspot
= hotSpotX
;
1675 iconInfo
.yHotspot
= hotSpotY
;
1678 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1679 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1681 // black out the transparent area to preserve background colour, because
1682 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1683 // the mask to the dest rect.
1685 MemoryHDC dcSrc
, dcDst
;
1686 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1687 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1689 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1690 dcSrc
, 0, 0, SRCAND
) )
1692 wxLogLastError(_T("BitBlt"));
1696 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1698 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1700 // we created the mask, now delete it
1704 // delete the inverted mask bitmap we created as well
1705 ::DeleteObject(iconInfo
.hbmMask
);
1710 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1712 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1715 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1717 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1720 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1722 #ifndef __WXMICROWIN__
1723 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1725 // get width/height from the bitmap if not given
1729 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1734 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1735 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1736 if ( !hdcSrc
|| !hdcDst
)
1738 wxLogLastError(wxT("CreateCompatibleDC"));
1741 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1744 wxLogLastError(wxT("CreateBitmap"));
1747 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1748 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1749 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1753 wxLogLastError(wxT("BitBlt"));
1757 SelectObject(hdcSrc
,srcTmp
);
1758 SelectObject(hdcDst
,dstTmp
);