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_LEVEL
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 wxT("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
, wxT("forgot to call wxBitmap::UngetRawData()!") );
235 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
237 wxLogLastError(wxT("DeleteObject(hbitmap)"));
241 wxDELETE(m_bitmapMask
);
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 wxGDIImageRefData
*wxBitmap::CreateData() const
250 return new wxBitmapRefData
;
253 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*dataOrig
) const
255 const wxBitmapRefData
*
256 data
= 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
= const_cast<wxBitmap
*>(this);
266 wxBitmapRefData
*selfdata
;
268 // copy the other bitmap
269 if ( data
->m_hBitmap
)
271 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
272 self
->CopyFromDIB(dib
);
274 selfdata
= static_cast<wxBitmapRefData
*>(m_refData
);
275 selfdata
->m_hasAlpha
= data
->m_hasAlpha
;
278 #endif // wxUSE_WXDIB
280 // copy the bitmap data
281 selfdata
= new wxBitmapRefData(*data
);
282 self
->m_refData
= selfdata
;
285 // copy also the mask
286 wxMask
* const maskSrc
= data
->GetMask();
289 selfdata
->SetMask(new wxMask(*maskSrc
));
295 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
,
296 wxBitmapTransparency transp
)
298 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
299 // it may be either HICON or HCURSOR
300 HICON hicon
= (HICON
)icon
.GetHandle();
303 if ( !::GetIconInfo(hicon
, &iconInfo
) )
305 wxLogLastError(wxT("GetIconInfo"));
310 wxBitmapRefData
*refData
= new wxBitmapRefData
;
313 int w
= icon
.GetWidth(),
314 h
= icon
.GetHeight();
316 refData
->m_width
= w
;
317 refData
->m_height
= h
;
318 refData
->m_depth
= wxDisplayDepth();
320 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
325 wxFAIL_MSG( wxT("unknown wxBitmapTransparency value") );
327 case wxBitmapTransparency_None
:
328 // nothing to do, refData->m_hasAlpha is false by default
331 case wxBitmapTransparency_Auto
:
333 // If the icon is 32 bits per pixel then it may have alpha channel
334 // data, although there are some icons that are 32 bpp but have no
335 // alpha... So convert to a DIB and manually check the 4th byte for
339 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(bm
), &bm
) &&
340 (bm
.bmBitsPixel
== 32) )
342 wxDIB
dib(iconInfo
.hbmColor
);
345 unsigned char* const pixels
= dib
.GetData();
347 for ( 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;
358 if ( refData
->m_hasAlpha
)
360 // If we do have alpha, ensure we use premultiplied
361 // data for our pixels as this is what the bitmaps
362 // created in other ways do and this is necessary
363 // for e.g. AlphaBlend() to work with this bitmap.
364 for ( idx
= 0; idx
< w
*h
*4; idx
+= 4 )
366 const unsigned char a
= pixels
[idx
+3];
368 pixels
[idx
] = ((pixels
[idx
] *a
) + 127)/255;
369 pixels
[idx
+1] = ((pixels
[idx
+1]*a
) + 127)/255;
370 pixels
[idx
+2] = ((pixels
[idx
+2]*a
) + 127)/255;
373 ::DeleteObject(refData
->m_hBitmap
);
374 refData
->m_hBitmap
= dib
.Detach();
380 #endif // wxUSE_WXDIB
382 case wxBitmapTransparency_Always
:
383 refData
->m_hasAlpha
= true;
387 if ( !refData
->m_hasAlpha
)
389 // the mask returned by GetIconInfo() is inverted compared to the usual
391 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
394 // delete the old one now as we don't need it any more
395 ::DeleteObject(iconInfo
.hbmMask
);
398 #else // __WXMICROWIN__ || __WXWINCE__
403 #endif // !__WXWINCE__/__WXWINCE__
406 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
410 if ( !cursor
.IsOk() )
413 return CopyFromIconOrCursor(cursor
, transp
);
416 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
423 return CopyFromIconOrCursor(icon
, transp
);
426 #ifndef NEVER_USE_DIB
428 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
430 wxCHECK_MSG( dib
.IsOk(), false, wxT("invalid DIB in CopyFromDIB") );
432 #ifdef SOMETIMES_USE_DIB
433 HBITMAP hbitmap
= dib
.CreateDDB();
436 #else // ALWAYS_USE_DIB
437 HBITMAP hbitmap
= const_cast<wxDIB
&>(dib
).Detach();
438 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
442 wxBitmapRefData
*refData
= new wxBitmapRefData
;
445 refData
->m_width
= dib
.GetWidth();
446 refData
->m_height
= dib
.GetHeight();
447 refData
->m_depth
= dib
.GetDepth();
449 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
452 wxPalette
*palette
= dib
.CreatePalette();
455 refData
->m_bitmapPalette
= *palette
;
459 #endif // wxUSE_PALETTE
464 #endif // NEVER_USE_DIB
466 wxBitmap::~wxBitmap()
470 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
472 #ifndef __WXMICROWIN__
473 wxBitmapRefData
*refData
= new wxBitmapRefData
;
476 refData
->m_width
= width
;
477 refData
->m_height
= height
;
478 refData
->m_depth
= depth
;
483 // we assume that it is in XBM format which is not quite the same as
484 // the format CreateBitmap() wants because the order of bytes in the
486 const size_t bytesPerLine
= (width
+ 7) / 8;
487 const size_t padding
= bytesPerLine
% 2;
488 const size_t len
= height
* ( padding
+ bytesPerLine
);
489 data
= (char *)malloc(len
);
490 const char *src
= bits
;
493 for ( int rows
= 0; rows
< height
; rows
++ )
495 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
497 unsigned char val
= *src
++;
498 unsigned char reversed
= 0;
500 for ( int bits
= 0; bits
< 8; bits
++)
503 reversed
|= (unsigned char)(val
& 0x01);
515 // bits should already be in Windows standard format
516 data
= const_cast<char *>(bits
);
519 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
522 wxLogLastError(wxT("CreateBitmap"));
530 SetHBITMAP((WXHBITMAP
)hbmp
);
534 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
536 (void)Create(w
, h
, dc
);
539 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
541 (void)Create(data
, type
, width
, height
, depth
);
544 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
546 LoadFile(filename
, type
);
549 bool wxBitmap::Create(int width
, int height
, int depth
)
551 return DoCreate(width
, height
, depth
, 0);
554 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
556 wxCHECK_MSG( dc
.IsOk(), false, wxT("invalid HDC in wxBitmap::Create()") );
558 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
561 return DoCreate(width
, height
, -1, impl
->GetHDC());
566 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
570 m_refData
= new wxBitmapRefData
;
572 GetBitmapData()->m_width
= w
;
573 GetBitmapData()->m_height
= h
;
575 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
577 #ifndef NEVER_USE_DIB
578 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
582 // create DIBs without alpha channel by default
590 // don't delete the DIB section in dib object dtor
593 GetBitmapData()->m_isDIB
= true;
594 GetBitmapData()->m_depth
= d
;
597 #endif // NEVER_USE_DIB
599 #ifndef ALWAYS_USE_DIB
600 #ifndef __WXMICROWIN__
603 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
606 wxLogLastError(wxT("CreateBitmap"));
609 GetBitmapData()->m_depth
= d
;
611 else // d == 0, create bitmap compatible with the screen
612 #endif // !__WXMICROWIN__
615 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
618 wxLogLastError(wxT("CreateCompatibleBitmap"));
621 GetBitmapData()->m_depth
= wxDisplayDepth();
623 #endif // !ALWAYS_USE_DIB
626 SetHBITMAP((WXHBITMAP
)hbmp
);
633 // ----------------------------------------------------------------------------
634 // wxImage to/from conversions for Microwin
635 // ----------------------------------------------------------------------------
637 // Microwin versions are so different from normal ones that it really doesn't
638 // make sense to use #ifdefs inside the function bodies
639 #ifdef __WXMICROWIN__
641 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
643 // Set this to 1 to experiment with mask code,
644 // which currently doesn't work
647 m_refData
= new wxBitmapRefData();
649 // Initial attempt at a simple-minded implementation.
650 // The bitmap will always be created at the screen depth,
651 // so the 'depth' argument is ignored.
653 HDC hScreenDC
= ::GetDC(NULL
);
654 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
656 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
657 HBITMAP hMaskBitmap
= NULL
;
658 HBITMAP hOldMaskBitmap
= NULL
;
660 unsigned char maskR
= 0;
661 unsigned char maskG
= 0;
662 unsigned char maskB
= 0;
664 // printf("Created bitmap %d\n", (int) hBitmap);
667 ::ReleaseDC(NULL
, hScreenDC
);
670 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
672 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
673 ::ReleaseDC(NULL
, hScreenDC
);
675 // created an mono-bitmap for the possible mask
676 bool hasMask
= image
.HasMask();
681 // FIXME: we should be able to pass bpp = 1, but
682 // GdBlit can't handle a different depth
684 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
686 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
688 maskR
= image
.GetMaskRed();
689 maskG
= image
.GetMaskGreen();
690 maskB
= image
.GetMaskBlue();
698 hScreenDC
= ::GetDC(NULL
);
699 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
700 ::ReleaseDC(NULL
, hScreenDC
);
702 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
710 for (i
= 0; i
< image
.GetWidth(); i
++)
712 for (j
= 0; j
< image
.GetHeight(); j
++)
714 unsigned char red
= image
.GetRed(i
, j
);
715 unsigned char green
= image
.GetGreen(i
, j
);
716 unsigned char blue
= image
.GetBlue(i
, j
);
718 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
722 // scan the bitmap for the transparent colour and set the corresponding
723 // pixels in the mask to BLACK and the rest to WHITE
724 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
725 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
727 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
732 ::SelectObject(hMemDC
, hOldBitmap
);
736 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
739 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
742 SetWidth(image
.GetWidth());
743 SetHeight(image
.GetHeight());
744 SetDepth(screenDepth
);
745 SetHBITMAP( (WXHBITMAP
) hBitmap
);
748 // Copy the palette from the source image
749 SetPalette(image
.GetPalette());
750 #endif // wxUSE_PALETTE
755 wxImage
wxBitmap::ConvertToImage() const
757 // Initial attempt at a simple-minded implementation.
758 // The bitmap will always be created at the screen depth,
759 // so the 'depth' argument is ignored.
760 // TODO: transparency (create a mask image)
764 wxFAIL_MSG( wxT("bitmap is invalid") );
770 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
772 // create an wxImage object
773 int width
= GetWidth();
774 int height
= GetHeight();
775 image
.Create( width
, height
);
776 unsigned char *data
= image
.GetData();
779 wxFAIL_MSG( wxT("could not allocate data for image") );
783 HDC hScreenDC
= ::GetDC(NULL
);
785 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
786 ::ReleaseDC(NULL
, hScreenDC
);
788 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
790 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
793 for (i
= 0; i
< GetWidth(); i
++)
795 for (j
= 0; j
< GetHeight(); j
++)
797 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
798 unsigned char red
= GetRValue(color
);
799 unsigned char green
= GetGValue(color
);
800 unsigned char blue
= GetBValue(color
);
802 image
.SetRGB(i
, j
, red
, green
, blue
);
806 ::SelectObject(hMemDC
, hOldBitmap
);
810 // Copy the palette from the source image
812 image
.SetPalette(* GetPalette());
813 #endif // wxUSE_PALETTE
818 #endif // __WXMICROWIN__
820 // ----------------------------------------------------------------------------
821 // wxImage to/from conversions
822 // ----------------------------------------------------------------------------
824 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
826 return CreateFromImage(image
, depth
, 0);
829 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
831 wxCHECK_MSG( dc
.IsOk(), false,
832 wxT("invalid HDC in wxBitmap::CreateFromImage()") );
834 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
837 return CreateFromImage(image
, -1, impl
->GetHDC());
844 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
846 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
850 // first convert the image to DIB
851 const int h
= image
.GetHeight();
852 const int w
= image
.GetWidth();
858 const bool hasAlpha
= image
.HasAlpha();
861 depth
= dib
.GetDepth();
863 // store the bitmap parameters
864 wxBitmapRefData
* const refData
= new wxBitmapRefData
;
865 refData
->m_width
= w
;
866 refData
->m_height
= h
;
867 refData
->m_hasAlpha
= hasAlpha
;
868 refData
->m_depth
= depth
;
873 // next either store DIB as is or create a DDB from it
874 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
876 // are we going to use DIB?
878 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
879 if ( hasAlpha
|| wxShouldCreateDIB(w
, h
, depth
, hdc
) )
881 // don't delete the DIB section in dib object dtor
882 hbitmap
= dib
.Detach();
884 refData
->m_isDIB
= true;
886 #ifndef ALWAYS_USE_DIB
887 else // we need to convert DIB to DDB
889 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
891 #endif // !ALWAYS_USE_DIB
893 // validate this object
894 SetHBITMAP((WXHBITMAP
)hbitmap
);
896 // finally also set the mask if we have one
897 if ( image
.HasMask() )
899 const size_t len
= 2*((w
+15)/16);
900 BYTE
*src
= image
.GetData();
901 BYTE
*data
= new BYTE
[h
*len
];
902 memset(data
, 0, h
*len
);
903 BYTE r
= image
.GetMaskRed(),
904 g
= image
.GetMaskGreen(),
905 b
= image
.GetMaskBlue();
907 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
911 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
913 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
916 if ( (mask
>>= 1) == 0 )
924 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
927 wxLogLastError(wxT("CreateBitmap(mask)"));
931 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
940 wxImage
wxBitmap::ConvertToImage() const
942 // convert DDB to DIB
950 // and then DIB to our wxImage
951 wxImage image
= dib
.ConvertToImage();
957 // now do the same for the mask, if we have any
958 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
961 wxDIB
dibMask(hbmpMask
);
962 if ( dibMask
.IsOk() )
964 // TODO: use wxRawBitmap to iterate over DIB
966 // we hard code the mask colour for now but we could also make an
967 // effort (and waste time) to choose a colour not present in the
968 // image already to avoid having to fudge the pixels below --
969 // whether it's worth to do it is unclear however
970 static const int MASK_RED
= 1;
971 static const int MASK_GREEN
= 2;
972 static const int MASK_BLUE
= 3;
973 static const int MASK_BLUE_REPLACEMENT
= 2;
975 const int h
= dibMask
.GetHeight();
976 const int w
= dibMask
.GetWidth();
977 const int bpp
= dibMask
.GetDepth();
978 const int maskBytesPerPixel
= bpp
>> 3;
979 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
980 unsigned char *data
= image
.GetData();
982 // remember that DIBs are stored in bottom to top order
984 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
986 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
988 // traverse one mask DIB line
989 unsigned char *mask
= maskLineStart
;
990 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
992 // should this pixel be transparent?
995 // no, check that it isn't transparent by accident
996 if ( (data
[0] == MASK_RED
) &&
997 (data
[1] == MASK_GREEN
) &&
998 (data
[2] == MASK_BLUE
) )
1000 // we have to fudge the colour a bit to prevent
1001 // this pixel from appearing transparent
1002 data
[2] = MASK_BLUE_REPLACEMENT
;
1007 else // yes, transparent pixel
1010 *data
++ = MASK_GREEN
;
1011 *data
++ = MASK_BLUE
;
1016 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1023 #else // !wxUSE_WXDIB
1026 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1027 int WXUNUSED(depth
),
1028 WXHDC
WXUNUSED(hdc
))
1033 wxImage
wxBitmap::ConvertToImage() const
1038 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1040 #endif // wxUSE_IMAGE
1042 // ----------------------------------------------------------------------------
1043 // loading and saving bitmaps
1044 // ----------------------------------------------------------------------------
1046 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1050 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1054 m_refData
= new wxBitmapRefData
;
1056 return handler
->LoadFile(this, filename
, type
, -1, -1);
1058 #if wxUSE_IMAGE && wxUSE_WXDIB
1059 else // no bitmap handler found
1062 if ( image
.LoadFile( filename
, type
) && image
.IsOk() )
1064 *this = wxBitmap(image
);
1069 #endif // wxUSE_IMAGE
1074 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1078 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1082 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1087 m_refData
= new wxBitmapRefData
;
1089 return handler
->Create(this, data
, type
, width
, height
, depth
);
1092 bool wxBitmap::SaveFile(const wxString
& filename
,
1094 const wxPalette
*palette
) const
1096 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1100 return handler
->SaveFile(this, filename
, type
, palette
);
1102 #if wxUSE_IMAGE && wxUSE_WXDIB
1103 else // no bitmap handler found
1105 // FIXME what about palette? shouldn't we use it?
1106 wxImage image
= ConvertToImage();
1109 return image
.SaveFile(filename
, type
);
1112 #endif // wxUSE_IMAGE
1117 // ----------------------------------------------------------------------------
1118 // sub bitmap extraction
1119 // ----------------------------------------------------------------------------
1120 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1123 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1124 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1127 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1129 wxCHECK_MSG( IsOk() &&
1130 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1131 (rect
.x
+rect
.width
<= GetWidth()) &&
1132 (rect
.y
+rect
.height
<= GetHeight()),
1133 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1135 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1136 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1138 #ifndef __WXMICROWIN__
1139 // handle alpha channel, if any
1148 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1152 wxLogLastError(wxT("SelectObject(destBitmap)"));
1155 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1156 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1158 wxLogLastError(wxT("BitBlt"));
1162 // copy mask if there is one
1165 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1167 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1168 selectDst(dcDst
, hbmpMask
);
1170 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1171 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1173 wxLogLastError(wxT("BitBlt"));
1176 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1179 #endif // !__WXMICROWIN__
1184 // ----------------------------------------------------------------------------
1185 // wxBitmap accessors
1186 // ----------------------------------------------------------------------------
1189 wxPalette
* wxBitmap::GetPalette() const
1191 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1196 wxMask
*wxBitmap::GetMask() const
1198 return GetBitmapData() ? GetBitmapData()->GetMask() : NULL
;
1201 wxBitmap
wxBitmap::GetMaskBitmap() const
1204 wxMask
*mask
= GetMask();
1206 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1210 wxDC
*wxBitmap::GetSelectedInto() const
1213 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: NULL
;
1219 void wxBitmap::UseAlpha()
1221 if ( GetBitmapData() )
1222 GetBitmapData()->m_hasAlpha
= true;
1225 bool wxBitmap::HasAlpha() const
1227 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1230 // ----------------------------------------------------------------------------
1232 // ----------------------------------------------------------------------------
1234 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1237 if ( GetBitmapData() )
1238 GetBitmapData()->m_selectedInto
= dc
;
1246 void wxBitmap::SetPalette(const wxPalette
& palette
)
1250 GetBitmapData()->m_bitmapPalette
= palette
;
1253 #endif // wxUSE_PALETTE
1255 void wxBitmap::SetMask(wxMask
*mask
)
1259 GetBitmapData()->SetMask(mask
);
1262 // ----------------------------------------------------------------------------
1263 // raw bitmap access support
1264 // ----------------------------------------------------------------------------
1266 #ifdef wxHAS_RAW_BITMAP
1268 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1273 // no bitmap, no data (raw or otherwise)
1277 // if we're already a DIB we can access our data directly, but if not we
1278 // need to convert this DDB to a DIB section and use it for raw access and
1279 // then convert it back
1281 if ( !GetBitmapData()->m_isDIB
)
1283 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1284 wxT("GetRawData() may be called only once") );
1286 wxDIB
*dib
= new wxDIB(*this);
1294 // we'll free it in UngetRawData()
1295 GetBitmapData()->m_dib
= dib
;
1297 hDIB
= dib
->GetHandle();
1301 hDIB
= GetHbitmap();
1305 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1307 wxFAIL_MSG( wxT("failed to get DIBSECTION from a DIB?") );
1312 // check that the bitmap is in correct format
1313 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1315 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
1320 // ok, store the relevant info in wxPixelDataBase
1321 const LONG h
= ds
.dsBm
.bmHeight
;
1323 data
.m_width
= ds
.dsBm
.bmWidth
;
1326 // remember that DIBs are stored in top to bottom order!
1327 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1328 // multiple of 2, as required by the documentation. So we use the official
1329 // formula, which we already use elsewhere.)
1330 const LONG bytesPerRow
=
1331 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1332 data
.m_stride
= -bytesPerRow
;
1334 char *bits
= (char *)ds
.dsBm
.bmBits
;
1337 bits
+= (h
- 1)*bytesPerRow
;
1346 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1354 // invalid data, don't crash -- but don't assert neither as we're
1355 // called automatically from wxPixelDataBase dtor and so there is no
1356 // way to prevent this from happening
1360 // if we're a DDB we need to convert DIB back to DDB now to make the
1361 // changes made via raw bitmap access effective
1362 if ( !GetBitmapData()->m_isDIB
)
1364 wxDIB
*dib
= GetBitmapData()->m_dib
;
1365 GetBitmapData()->m_dib
= NULL
;
1371 #endif // wxUSE_WXDIB
1373 #endif // wxHAS_RAW_BITMAP
1375 // ----------------------------------------------------------------------------
1377 // ----------------------------------------------------------------------------
1385 wxMask::wxMask(const wxMask
&mask
)
1390 HDC srcDC
= CreateCompatibleDC(0);
1391 HDC destDC
= CreateCompatibleDC(0);
1393 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1394 // so we'll use GetObject() API here:
1395 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1397 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1401 // create our HBITMAP
1402 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1403 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1405 // copy the mask's HBITMAP into our HBITMAP
1406 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1407 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1409 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1411 SelectObject(srcDC
, 0);
1413 SelectObject(destDC
, 0);
1417 // Construct a mask from a bitmap and a colour indicating
1418 // the transparent area
1419 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1422 Create(bitmap
, colour
);
1425 // Construct a mask from a bitmap and a palette index indicating
1426 // the transparent area
1427 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1430 Create(bitmap
, paletteIndex
);
1433 // Construct a mask from a mono bitmap (copies the bitmap).
1434 wxMask::wxMask(const wxBitmap
& bitmap
)
1443 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1446 // Create a mask from a mono bitmap (copies the bitmap).
1447 bool wxMask::Create(const wxBitmap
& bitmap
)
1449 #ifndef __WXMICROWIN__
1450 wxCHECK_MSG( bitmap
.IsOk() && bitmap
.GetDepth() == 1, false,
1451 wxT("can't create mask from invalid or not monochrome bitmap") );
1455 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1459 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1464 HDC srcDC
= CreateCompatibleDC(0);
1465 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1466 HDC destDC
= CreateCompatibleDC(0);
1467 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1468 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1469 SelectObject(srcDC
, 0);
1471 SelectObject(destDC
, 0);
1475 wxUnusedVar(bitmap
);
1480 // Create a mask from a bitmap and a palette index indicating
1481 // the transparent area
1482 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1486 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1491 if (bitmap
.IsOk() && bitmap
.GetPalette()->IsOk())
1493 unsigned char red
, green
, blue
;
1494 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1496 wxColour
transparentColour(red
, green
, blue
);
1497 return Create(bitmap
, transparentColour
);
1500 #endif // wxUSE_PALETTE
1505 // Create a mask from a bitmap and a colour indicating
1506 // the transparent area
1507 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1509 #ifndef __WXMICROWIN__
1510 wxCHECK_MSG( bitmap
.IsOk(), false, wxT("invalid bitmap in wxMask::Create") );
1514 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1518 int width
= bitmap
.GetWidth(),
1519 height
= bitmap
.GetHeight();
1521 // scan the bitmap for the transparent colour and set the corresponding
1522 // pixels in the mask to BLACK and the rest to WHITE
1523 COLORREF maskColour
= wxColourToPalRGB(colour
);
1524 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1526 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1527 HDC destDC
= ::CreateCompatibleDC(NULL
);
1528 if ( !srcDC
|| !destDC
)
1530 wxLogLastError(wxT("CreateCompatibleDC"));
1535 // SelectObject() will fail
1536 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1537 wxT("bitmap can't be selected in another DC") );
1539 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1542 wxLogLastError(wxT("SelectObject"));
1547 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1550 wxLogLastError(wxT("SelectObject"));
1557 // this will create a monochrome bitmap with 0 points for the pixels
1558 // which have the same value as the background colour and 1 for the
1560 ::SetBkColor(srcDC
, maskColour
);
1561 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1564 ::SelectObject(srcDC
, hbmpSrcOld
);
1566 ::SelectObject(destDC
, hbmpDstOld
);
1570 #else // __WXMICROWIN__
1571 wxUnusedVar(bitmap
);
1572 wxUnusedVar(colour
);
1574 #endif // __WXMICROWIN__/!__WXMICROWIN__
1577 // ----------------------------------------------------------------------------
1579 // ----------------------------------------------------------------------------
1581 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1584 int width
, int height
, int depth
)
1586 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1588 return bitmap
&& Create(bitmap
, data
, type
, width
, height
, depth
);
1591 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1592 const wxString
& name
,
1594 int width
, int height
)
1596 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1598 return bitmap
&& LoadFile(bitmap
, name
, type
, width
, height
);
1601 bool wxBitmapHandler::Save(const wxGDIImage
*image
,
1602 const wxString
& name
,
1603 wxBitmapType type
) const
1605 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1607 return bitmap
&& SaveFile(bitmap
, name
, type
);
1610 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1611 const void* WXUNUSED(data
),
1612 wxBitmapType
WXUNUSED(type
),
1613 int WXUNUSED(width
),
1614 int WXUNUSED(height
),
1615 int WXUNUSED(depth
))
1620 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1621 const wxString
& WXUNUSED(name
),
1622 wxBitmapType
WXUNUSED(type
),
1623 int WXUNUSED(desiredWidth
),
1624 int WXUNUSED(desiredHeight
))
1629 bool wxBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1630 const wxString
& WXUNUSED(name
),
1631 wxBitmapType
WXUNUSED(type
),
1632 const wxPalette
*WXUNUSED(palette
)) const
1637 // ----------------------------------------------------------------------------
1638 // global helper functions implemented here
1639 // ----------------------------------------------------------------------------
1641 // helper of wxBitmapToHICON/HCURSOR
1643 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1650 // we can't create an icon/cursor form nothing
1654 if ( bmp
.HasAlpha() )
1658 #if wxUSE_WXDIB && wxUSE_IMAGE
1659 // CreateIconIndirect() requires non-pre-multiplied pixel data on input
1660 // as it does pre-multiplication internally itself so we need to create
1661 // a special DIB in such format to pass to it. This is inefficient but
1662 // better than creating an icon with wrong colours.
1663 AutoHBITMAP hbmpRelease
;
1664 hbmp
= wxDIB(bmp
.ConvertToImage(),
1665 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
1666 hbmpRelease
.Init(hbmp
);
1667 #else // !(wxUSE_WXDIB && wxUSE_IMAGE)
1668 hbmp
= GetHbitmapOf(bmp
);
1669 #endif // wxUSE_WXDIB && wxUSE_IMAGE
1671 // Create an empty mask bitmap.
1672 // it doesn't seem to work if we mess with the mask at all.
1674 hMonoBitmap(CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
));
1677 wxZeroMemory(iconInfo
);
1678 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1681 iconInfo
.xHotspot
= hotSpotX
;
1682 iconInfo
.yHotspot
= hotSpotY
;
1685 iconInfo
.hbmMask
= hMonoBitmap
;
1686 iconInfo
.hbmColor
= hbmp
;
1688 return ::CreateIconIndirect(&iconInfo
);
1691 wxMask
* mask
= bmp
.GetMask();
1695 // we must have a mask for an icon, so even if it's probably incorrect,
1696 // do create it (grey is the "standard" transparent colour)
1697 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1701 wxZeroMemory(iconInfo
);
1702 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1705 iconInfo
.xHotspot
= hotSpotX
;
1706 iconInfo
.yHotspot
= hotSpotY
;
1709 AutoHBITMAP
hbmpMask(wxInvertMask((HBITMAP
)mask
->GetMaskBitmap()));
1710 iconInfo
.hbmMask
= hbmpMask
;
1711 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1713 // black out the transparent area to preserve background colour, because
1714 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1715 // the mask to the dest rect.
1717 MemoryHDC dcSrc
, dcDst
;
1718 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1719 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1721 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1722 dcSrc
, 0, 0, SRCAND
) )
1724 wxLogLastError(wxT("BitBlt"));
1728 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1730 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1732 // we created the mask, now delete it
1739 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1741 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1744 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1746 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1749 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1751 #ifndef __WXMICROWIN__
1752 wxCHECK_MSG( hbmpMask
, 0, wxT("invalid bitmap in wxInvertMask") );
1754 // get width/height from the bitmap if not given
1758 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1763 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1764 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1765 if ( !hdcSrc
|| !hdcDst
)
1767 wxLogLastError(wxT("CreateCompatibleDC"));
1770 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1773 wxLogLastError(wxT("CreateBitmap"));
1776 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1777 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1778 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1782 wxLogLastError(wxT("BitBlt"));
1786 SelectObject(hdcSrc
,srcTmp
);
1787 SelectObject(hdcDst
,dstTmp
);