1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bitmap.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/bitmap.h"
34 #include "wx/palette.h"
35 #include "wx/dcmemory.h"
41 #include "wx/scopedptr.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
65 wxBitmapRefData() { Init(); }
66 wxBitmapRefData(const wxBitmapRefData
& data
);
67 virtual ~wxBitmapRefData() { Free(); }
71 void CopyFromDIB(const wxDIB
& dib
);
73 // set the mask object to use as the mask, we take ownership of it
74 void SetMask(wxMask
*mask
)
80 // set the HBITMAP to use as the mask
81 void SetMask(HBITMAP hbmpMask
)
83 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
87 wxMask
*GetMask() const { return m_bitmapMask
; }
91 wxPalette m_bitmapPalette
;
92 #endif // wxUSE_PALETTE
98 // this field is solely for error checking: we detect selecting a bitmap
99 // into more than one DC at once or deleting a bitmap still selected into a
100 // DC (both are serious programming errors under Windows)
101 wxDC
*m_selectedInto
;
102 #endif // wxDEBUG_LEVEL
105 // when GetRawData() is called for a DDB we need to convert it to a DIB
106 // first to be able to provide direct access to it and we cache that DIB
107 // here and convert it back to DDB when UngetRawData() is called
111 // true if we have alpha transparency info and can be drawn using
115 // true if our HBITMAP is a DIB section, false if it is a DDB
121 // optional mask for transparent drawing
122 wxMask
*m_bitmapMask
;
126 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
134 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
136 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // decide whether we should create a DIB or a DDB for the given parameters
148 // NB: we always use DIBs under Windows CE as this is much simpler (even if
149 // also less efficient...) and we obviously can't use them if there is no
150 // DIB support compiled in at all
152 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
154 #define ALWAYS_USE_DIB
156 // no sense in defining wxShouldCreateDIB() as we can't compile code
157 // executed if it is true, so we have to use #if's anyhow
158 #define NEVER_USE_DIB
159 #else // wxUSE_WXDIB && !__WXWINCE__
160 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
162 // here is the logic:
164 // (a) if hdc is specified, the caller explicitly wants DDB
165 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
166 // or less DIBs anyhow)
167 // (c) finally, create DIBs under Win9x even if the depth hasn't been
168 // explicitly specified but the current display depth is 24 or
169 // more and the image is "big", i.e. > 16Mb which is the
170 // theoretical limit for DDBs under Win9x
172 // consequences (all of which seem to make sense):
174 // (i) by default, DDBs are created (depth == -1 usually)
175 // (ii) DIBs can be created by explicitly specifying the depth
176 // (iii) using a DC always forces creating a DDB
180 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
183 #define SOMETIMES_USE_DIB
184 #endif // different DIB usage scenarious
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 void wxBitmapRefData::Init()
193 m_selectedInto
= NULL
;
197 m_hBitmap
= (WXHBITMAP
) NULL
;
206 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
207 : wxGDIImageRefData(data
)
211 // (deep) copy the mask if present
212 if (data
.m_bitmapMask
)
213 m_bitmapMask
= new wxMask(*data
.m_bitmapMask
);
215 wxASSERT_MSG( !data
.m_dib
,
216 wxT("can't copy bitmap locked for raw access!") );
218 m_hasAlpha
= data
.m_hasAlpha
;
221 // copy the other bitmap
222 if ( data
.m_hBitmap
)
224 wxDIB
dib((HBITMAP
)(data
.m_hBitmap
));
227 #endif // wxUSE_WXDIB
230 void wxBitmapRefData::Free()
232 wxASSERT_MSG( !m_selectedInto
,
233 wxT("deleting bitmap still selected into wxMemoryDC") );
236 wxASSERT_MSG( !m_dib
, wxT("forgot to call wxBitmap::UngetRawData()!") );
241 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
243 wxLogLastError(wxT("DeleteObject(hbitmap)"));
247 wxDELETE(m_bitmapMask
);
250 void wxBitmapRefData::CopyFromDIB(const wxDIB
& dib
)
252 wxCHECK_RET( !IsOk(), "bitmap already initialized" );
253 wxCHECK_RET( dib
.IsOk(), wxT("invalid DIB in CopyFromDIB") );
255 #ifdef SOMETIMES_USE_DIB
256 HBITMAP hbitmap
= dib
.CreateDDB();
260 #else // ALWAYS_USE_DIB
261 HBITMAP hbitmap
= const_cast<wxDIB
&>(dib
).Detach();
263 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
265 m_width
= dib
.GetWidth();
266 m_height
= dib
.GetHeight();
267 m_depth
= dib
.GetDepth();
269 m_hBitmap
= (WXHBITMAP
)hbitmap
;
272 wxPalette
*palette
= dib
.CreatePalette();
274 m_bitmapPalette
= *palette
;
276 #endif // wxUSE_PALETTE
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 wxGDIImageRefData
*wxBitmap::CreateData() const
285 return new wxBitmapRefData
;
288 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
290 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
293 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
,
294 wxBitmapTransparency transp
)
296 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
297 // it may be either HICON or HCURSOR
298 HICON hicon
= (HICON
)icon
.GetHandle();
301 if ( !::GetIconInfo(hicon
, &iconInfo
) )
303 wxLogLastError(wxT("GetIconInfo"));
308 wxBitmapRefData
*refData
= new wxBitmapRefData
;
311 int w
= icon
.GetWidth(),
312 h
= icon
.GetHeight();
314 refData
->m_width
= w
;
315 refData
->m_height
= h
;
316 refData
->m_depth
= wxDisplayDepth();
318 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
323 wxFAIL_MSG( wxT("unknown wxBitmapTransparency value") );
325 case wxBitmapTransparency_None
:
326 // nothing to do, refData->m_hasAlpha is false by default
329 case wxBitmapTransparency_Auto
:
331 // If the icon is 32 bits per pixel then it may have alpha channel
332 // data, although there are some icons that are 32 bpp but have no
333 // alpha... So convert to a DIB and manually check the 4th byte for
337 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(bm
), &bm
) &&
338 (bm
.bmBitsPixel
== 32) )
340 wxDIB
dib(iconInfo
.hbmColor
);
343 unsigned char* const pixels
= dib
.GetData();
345 for ( idx
= 0; idx
< w
*h
*4; idx
+= 4 )
347 if (pixels
[idx
+3] != 0)
349 // If there is an alpha byte that is non-zero
350 // then set the alpha flag and stop checking
351 refData
->m_hasAlpha
= true;
356 if ( refData
->m_hasAlpha
)
358 // If we do have alpha, ensure we use premultiplied
359 // data for our pixels as this is what the bitmaps
360 // created in other ways do and this is necessary
361 // for e.g. AlphaBlend() to work with this bitmap.
362 for ( idx
= 0; idx
< w
*h
*4; idx
+= 4 )
364 const unsigned char a
= pixels
[idx
+3];
366 pixels
[idx
] = ((pixels
[idx
] *a
) + 127)/255;
367 pixels
[idx
+1] = ((pixels
[idx
+1]*a
) + 127)/255;
368 pixels
[idx
+2] = ((pixels
[idx
+2]*a
) + 127)/255;
371 ::DeleteObject(refData
->m_hBitmap
);
372 refData
->m_hBitmap
= dib
.Detach();
378 #endif // wxUSE_WXDIB
380 case wxBitmapTransparency_Always
:
381 refData
->m_hasAlpha
= true;
385 if ( !refData
->m_hasAlpha
)
387 // the mask returned by GetIconInfo() is inverted compared to the usual
389 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
392 // delete the old one now as we don't need it any more
393 ::DeleteObject(iconInfo
.hbmMask
);
396 #else // __WXMICROWIN__ || __WXWINCE__
401 #endif // !__WXWINCE__/__WXWINCE__
404 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
408 if ( !cursor
.IsOk() )
411 return CopyFromIconOrCursor(cursor
, transp
);
414 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
421 return CopyFromIconOrCursor(icon
, transp
);
424 #ifndef NEVER_USE_DIB
426 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
428 wxScopedPtr
<wxBitmapRefData
> newData(new wxBitmapRefData
);
429 newData
->CopyFromDIB(dib
);
430 if ( !newData
->IsOk() )
434 m_refData
= newData
.release();
438 #endif // NEVER_USE_DIB
440 wxBitmap::~wxBitmap()
444 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
446 #ifndef __WXMICROWIN__
447 wxBitmapRefData
*refData
= new wxBitmapRefData
;
450 refData
->m_width
= width
;
451 refData
->m_height
= height
;
452 refData
->m_depth
= depth
;
457 // we assume that it is in XBM format which is not quite the same as
458 // the format CreateBitmap() wants because the order of bytes in the
460 const size_t bytesPerLine
= (width
+ 7) / 8;
461 const size_t padding
= bytesPerLine
% 2;
462 const size_t len
= height
* ( padding
+ bytesPerLine
);
463 data
= (char *)malloc(len
);
464 const char *src
= bits
;
467 for ( int rows
= 0; rows
< height
; rows
++ )
469 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
471 unsigned char val
= *src
++;
472 unsigned char reversed
= 0;
474 for ( int bits
= 0; bits
< 8; bits
++)
477 reversed
|= (unsigned char)(val
& 0x01);
489 // bits should already be in Windows standard format
490 data
= const_cast<char *>(bits
);
493 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
496 wxLogLastError(wxT("CreateBitmap"));
504 SetHBITMAP((WXHBITMAP
)hbmp
);
508 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
510 (void)Create(w
, h
, dc
);
513 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
515 (void)Create(data
, type
, width
, height
, depth
);
518 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
520 LoadFile(filename
, type
);
523 bool wxBitmap::Create(int width
, int height
, int depth
)
525 return DoCreate(width
, height
, depth
, 0);
528 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
530 wxCHECK_MSG( dc
.IsOk(), false, wxT("invalid HDC in wxBitmap::Create()") );
532 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
535 return DoCreate(width
, height
, -1, impl
->GetHDC());
540 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
544 m_refData
= new wxBitmapRefData
;
546 GetBitmapData()->m_width
= w
;
547 GetBitmapData()->m_height
= h
;
549 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
551 #ifndef NEVER_USE_DIB
552 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
556 // create DIBs without alpha channel by default
564 // don't delete the DIB section in dib object dtor
567 GetBitmapData()->m_isDIB
= true;
568 GetBitmapData()->m_depth
= d
;
571 #endif // NEVER_USE_DIB
573 #ifndef ALWAYS_USE_DIB
574 #ifndef __WXMICROWIN__
577 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
580 wxLogLastError(wxT("CreateBitmap"));
583 GetBitmapData()->m_depth
= d
;
585 else // d == 0, create bitmap compatible with the screen
586 #endif // !__WXMICROWIN__
589 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
592 wxLogLastError(wxT("CreateCompatibleBitmap"));
595 GetBitmapData()->m_depth
= wxDisplayDepth();
597 #endif // !ALWAYS_USE_DIB
600 SetHBITMAP((WXHBITMAP
)hbmp
);
607 // ----------------------------------------------------------------------------
608 // wxImage to/from conversions for Microwin
609 // ----------------------------------------------------------------------------
611 // Microwin versions are so different from normal ones that it really doesn't
612 // make sense to use #ifdefs inside the function bodies
613 #ifdef __WXMICROWIN__
615 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
617 // Set this to 1 to experiment with mask code,
618 // which currently doesn't work
621 m_refData
= new wxBitmapRefData();
623 // Initial attempt at a simple-minded implementation.
624 // The bitmap will always be created at the screen depth,
625 // so the 'depth' argument is ignored.
627 HDC hScreenDC
= ::GetDC(NULL
);
628 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
630 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
631 HBITMAP hMaskBitmap
= NULL
;
632 HBITMAP hOldMaskBitmap
= NULL
;
634 unsigned char maskR
= 0;
635 unsigned char maskG
= 0;
636 unsigned char maskB
= 0;
638 // printf("Created bitmap %d\n", (int) hBitmap);
641 ::ReleaseDC(NULL
, hScreenDC
);
644 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
646 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
647 ::ReleaseDC(NULL
, hScreenDC
);
649 // created an mono-bitmap for the possible mask
650 bool hasMask
= image
.HasMask();
655 // FIXME: we should be able to pass bpp = 1, but
656 // GdBlit can't handle a different depth
658 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
660 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
662 maskR
= image
.GetMaskRed();
663 maskG
= image
.GetMaskGreen();
664 maskB
= image
.GetMaskBlue();
672 hScreenDC
= ::GetDC(NULL
);
673 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
674 ::ReleaseDC(NULL
, hScreenDC
);
676 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
684 for (i
= 0; i
< image
.GetWidth(); i
++)
686 for (j
= 0; j
< image
.GetHeight(); j
++)
688 unsigned char red
= image
.GetRed(i
, j
);
689 unsigned char green
= image
.GetGreen(i
, j
);
690 unsigned char blue
= image
.GetBlue(i
, j
);
692 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
696 // scan the bitmap for the transparent colour and set the corresponding
697 // pixels in the mask to BLACK and the rest to WHITE
698 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
699 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
701 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
706 ::SelectObject(hMemDC
, hOldBitmap
);
710 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
713 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
716 SetWidth(image
.GetWidth());
717 SetHeight(image
.GetHeight());
718 SetDepth(screenDepth
);
719 SetHBITMAP( (WXHBITMAP
) hBitmap
);
722 // Copy the palette from the source image
723 SetPalette(image
.GetPalette());
724 #endif // wxUSE_PALETTE
729 wxImage
wxBitmap::ConvertToImage() const
731 // Initial attempt at a simple-minded implementation.
732 // The bitmap will always be created at the screen depth,
733 // so the 'depth' argument is ignored.
734 // TODO: transparency (create a mask image)
738 wxFAIL_MSG( wxT("bitmap is invalid") );
744 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
746 // create an wxImage object
747 int width
= GetWidth();
748 int height
= GetHeight();
749 image
.Create( width
, height
);
750 unsigned char *data
= image
.GetData();
753 wxFAIL_MSG( wxT("could not allocate data for image") );
757 HDC hScreenDC
= ::GetDC(NULL
);
759 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
760 ::ReleaseDC(NULL
, hScreenDC
);
762 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
764 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
767 for (i
= 0; i
< GetWidth(); i
++)
769 for (j
= 0; j
< GetHeight(); j
++)
771 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
772 unsigned char red
= GetRValue(color
);
773 unsigned char green
= GetGValue(color
);
774 unsigned char blue
= GetBValue(color
);
776 image
.SetRGB(i
, j
, red
, green
, blue
);
780 ::SelectObject(hMemDC
, hOldBitmap
);
784 // Copy the palette from the source image
786 image
.SetPalette(* GetPalette());
787 #endif // wxUSE_PALETTE
792 #endif // __WXMICROWIN__
794 // ----------------------------------------------------------------------------
795 // wxImage to/from conversions
796 // ----------------------------------------------------------------------------
798 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
800 return CreateFromImage(image
, depth
, 0);
803 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
805 wxCHECK_MSG( dc
.IsOk(), false,
806 wxT("invalid HDC in wxBitmap::CreateFromImage()") );
808 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
811 return CreateFromImage(image
, -1, impl
->GetHDC());
818 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
820 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
824 // first convert the image to DIB
825 const int h
= image
.GetHeight();
826 const int w
= image
.GetWidth();
832 const bool hasAlpha
= image
.HasAlpha();
835 depth
= dib
.GetDepth();
837 // store the bitmap parameters
838 wxBitmapRefData
* const refData
= new wxBitmapRefData
;
839 refData
->m_width
= w
;
840 refData
->m_height
= h
;
841 refData
->m_hasAlpha
= hasAlpha
;
842 refData
->m_depth
= depth
;
847 // next either store DIB as is or create a DDB from it
848 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
850 // are we going to use DIB?
852 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
853 if ( hasAlpha
|| wxShouldCreateDIB(w
, h
, depth
, hdc
) )
855 // don't delete the DIB section in dib object dtor
856 hbitmap
= dib
.Detach();
858 refData
->m_isDIB
= true;
860 #ifndef ALWAYS_USE_DIB
861 else // we need to convert DIB to DDB
863 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
865 #endif // !ALWAYS_USE_DIB
867 // validate this object
868 SetHBITMAP((WXHBITMAP
)hbitmap
);
870 // finally also set the mask if we have one
871 if ( image
.HasMask() )
873 const size_t len
= 2*((w
+15)/16);
874 BYTE
*src
= image
.GetData();
875 BYTE
*data
= new BYTE
[h
*len
];
876 memset(data
, 0, h
*len
);
877 BYTE r
= image
.GetMaskRed(),
878 g
= image
.GetMaskGreen(),
879 b
= image
.GetMaskBlue();
881 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
885 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
887 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
890 if ( (mask
>>= 1) == 0 )
898 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
901 wxLogLastError(wxT("CreateBitmap(mask)"));
905 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
914 wxImage
wxBitmap::ConvertToImage() const
916 // convert DDB to DIB
924 // and then DIB to our wxImage
925 wxImage image
= dib
.ConvertToImage();
931 // now do the same for the mask, if we have any
932 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
935 wxDIB
dibMask(hbmpMask
);
936 if ( dibMask
.IsOk() )
938 // TODO: use wxRawBitmap to iterate over DIB
940 // we hard code the mask colour for now but we could also make an
941 // effort (and waste time) to choose a colour not present in the
942 // image already to avoid having to fudge the pixels below --
943 // whether it's worth to do it is unclear however
944 static const int MASK_RED
= 1;
945 static const int MASK_GREEN
= 2;
946 static const int MASK_BLUE
= 3;
947 static const int MASK_BLUE_REPLACEMENT
= 2;
949 const int h
= dibMask
.GetHeight();
950 const int w
= dibMask
.GetWidth();
951 const int bpp
= dibMask
.GetDepth();
952 const int maskBytesPerPixel
= bpp
>> 3;
953 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
954 unsigned char *data
= image
.GetData();
956 // remember that DIBs are stored in bottom to top order
958 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
960 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
962 // traverse one mask DIB line
963 unsigned char *mask
= maskLineStart
;
964 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
966 // should this pixel be transparent?
969 // no, check that it isn't transparent by accident
970 if ( (data
[0] == MASK_RED
) &&
971 (data
[1] == MASK_GREEN
) &&
972 (data
[2] == MASK_BLUE
) )
974 // we have to fudge the colour a bit to prevent
975 // this pixel from appearing transparent
976 data
[2] = MASK_BLUE_REPLACEMENT
;
981 else // yes, transparent pixel
984 *data
++ = MASK_GREEN
;
990 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
997 #else // !wxUSE_WXDIB
1000 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1001 int WXUNUSED(depth
),
1002 WXHDC
WXUNUSED(hdc
))
1007 wxImage
wxBitmap::ConvertToImage() const
1012 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1014 #endif // wxUSE_IMAGE
1016 // ----------------------------------------------------------------------------
1017 // loading and saving bitmaps
1018 // ----------------------------------------------------------------------------
1020 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1024 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1028 m_refData
= new wxBitmapRefData
;
1030 return handler
->LoadFile(this, filename
, type
, -1, -1);
1032 #if wxUSE_IMAGE && wxUSE_WXDIB
1033 else // no bitmap handler found
1036 if ( image
.LoadFile( filename
, type
) && image
.IsOk() )
1038 *this = wxBitmap(image
);
1043 #endif // wxUSE_IMAGE
1048 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1052 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1056 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1061 m_refData
= new wxBitmapRefData
;
1063 return handler
->Create(this, data
, type
, width
, height
, depth
);
1066 bool wxBitmap::SaveFile(const wxString
& filename
,
1068 const wxPalette
*palette
) const
1070 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1074 return handler
->SaveFile(this, filename
, type
, palette
);
1076 #if wxUSE_IMAGE && wxUSE_WXDIB
1077 else // no bitmap handler found
1079 // FIXME what about palette? shouldn't we use it?
1080 wxImage image
= ConvertToImage();
1083 return image
.SaveFile(filename
, type
);
1086 #endif // wxUSE_IMAGE
1091 // ----------------------------------------------------------------------------
1092 // sub bitmap extraction
1093 // ----------------------------------------------------------------------------
1094 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1097 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1098 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1101 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1103 wxCHECK_MSG( IsOk() &&
1104 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1105 (rect
.x
+rect
.width
<= GetWidth()) &&
1106 (rect
.y
+rect
.height
<= GetHeight()),
1107 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1109 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1110 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1112 #ifndef __WXMICROWIN__
1113 // handle alpha channel, if any
1122 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1126 wxLogLastError(wxT("SelectObject(destBitmap)"));
1129 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1130 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1132 wxLogLastError(wxT("BitBlt"));
1136 // copy mask if there is one
1139 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1141 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1142 selectDst(dcDst
, hbmpMask
);
1144 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1145 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1147 wxLogLastError(wxT("BitBlt"));
1150 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1153 #endif // !__WXMICROWIN__
1158 // ----------------------------------------------------------------------------
1159 // wxBitmap accessors
1160 // ----------------------------------------------------------------------------
1163 wxPalette
* wxBitmap::GetPalette() const
1165 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1170 wxMask
*wxBitmap::GetMask() const
1172 return GetBitmapData() ? GetBitmapData()->GetMask() : NULL
;
1175 wxDC
*wxBitmap::GetSelectedInto() const
1178 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: 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 // ----------------------------------------------------------------------------
1199 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1202 if ( GetBitmapData() )
1203 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 wxHAS_RAW_BITMAP
1233 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1238 // no bitmap, no data (raw or otherwise)
1242 // if we're already a DIB we can access our data directly, but if not we
1243 // need to convert this DDB to a DIB section and use it for raw access and
1244 // then convert it back
1246 if ( !GetBitmapData()->m_isDIB
)
1248 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1249 wxT("GetRawData() may be called only once") );
1251 wxDIB
*dib
= new wxDIB(*this);
1259 // we'll free it in UngetRawData()
1260 GetBitmapData()->m_dib
= dib
;
1262 hDIB
= dib
->GetHandle();
1266 hDIB
= GetHbitmap();
1270 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1272 wxFAIL_MSG( wxT("failed to get DIBSECTION from a DIB?") );
1277 // check that the bitmap is in correct format
1278 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1280 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
1285 // ok, store the relevant info in wxPixelDataBase
1286 const LONG h
= ds
.dsBm
.bmHeight
;
1288 data
.m_width
= ds
.dsBm
.bmWidth
;
1291 // remember that DIBs are stored in top to bottom order!
1292 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1293 // multiple of 2, as required by the documentation. So we use the official
1294 // formula, which we already use elsewhere.)
1295 const LONG bytesPerRow
=
1296 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1297 data
.m_stride
= -bytesPerRow
;
1299 char *bits
= (char *)ds
.dsBm
.bmBits
;
1302 bits
+= (h
- 1)*bytesPerRow
;
1311 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1319 // invalid data, don't crash -- but don't assert neither as we're
1320 // called automatically from wxPixelDataBase dtor and so there is no
1321 // way to prevent this from happening
1325 // if we're a DDB we need to convert DIB back to DDB now to make the
1326 // changes made via raw bitmap access effective
1327 if ( !GetBitmapData()->m_isDIB
)
1329 wxDIB
*dib
= GetBitmapData()->m_dib
;
1330 GetBitmapData()->m_dib
= NULL
;
1336 #endif // wxUSE_WXDIB
1338 #endif // wxHAS_RAW_BITMAP
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1350 wxMask::wxMask(const wxMask
&mask
)
1355 HDC srcDC
= CreateCompatibleDC(0);
1356 HDC destDC
= CreateCompatibleDC(0);
1358 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1359 // so we'll use GetObject() API here:
1360 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1362 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1366 // create our HBITMAP
1367 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1368 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1370 // copy the mask's HBITMAP into our HBITMAP
1371 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1372 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1374 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1376 SelectObject(srcDC
, 0);
1378 SelectObject(destDC
, 0);
1382 // Construct a mask from a bitmap and a colour indicating
1383 // the transparent area
1384 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1387 Create(bitmap
, colour
);
1390 // Construct a mask from a bitmap and a palette index indicating
1391 // the transparent area
1392 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1395 Create(bitmap
, paletteIndex
);
1398 // Construct a mask from a mono bitmap (copies the bitmap).
1399 wxMask::wxMask(const wxBitmap
& bitmap
)
1408 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1411 // Create a mask from a mono bitmap (copies the bitmap).
1412 bool wxMask::Create(const wxBitmap
& bitmap
)
1414 #ifndef __WXMICROWIN__
1415 wxCHECK_MSG( bitmap
.IsOk() && bitmap
.GetDepth() == 1, false,
1416 wxT("can't create mask from invalid or not monochrome bitmap") );
1420 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1424 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1429 HDC srcDC
= CreateCompatibleDC(0);
1430 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1431 HDC destDC
= CreateCompatibleDC(0);
1432 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1433 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1434 SelectObject(srcDC
, 0);
1436 SelectObject(destDC
, 0);
1440 wxUnusedVar(bitmap
);
1445 // Create a mask from a bitmap and a palette index indicating
1446 // the transparent area
1447 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1451 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1456 if (bitmap
.IsOk() && bitmap
.GetPalette()->IsOk())
1458 unsigned char red
, green
, blue
;
1459 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1461 wxColour
transparentColour(red
, green
, blue
);
1462 return Create(bitmap
, transparentColour
);
1465 #endif // wxUSE_PALETTE
1470 // Create a mask from a bitmap and a colour indicating
1471 // the transparent area
1472 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1474 #ifndef __WXMICROWIN__
1475 wxCHECK_MSG( bitmap
.IsOk(), false, wxT("invalid bitmap in wxMask::Create") );
1479 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1483 int width
= bitmap
.GetWidth(),
1484 height
= bitmap
.GetHeight();
1486 // scan the bitmap for the transparent colour and set the corresponding
1487 // pixels in the mask to BLACK and the rest to WHITE
1488 COLORREF maskColour
= wxColourToPalRGB(colour
);
1489 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1491 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1492 HDC destDC
= ::CreateCompatibleDC(NULL
);
1493 if ( !srcDC
|| !destDC
)
1495 wxLogLastError(wxT("CreateCompatibleDC"));
1500 // SelectObject() will fail
1501 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1502 wxT("bitmap can't be selected in another DC") );
1504 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1507 wxLogLastError(wxT("SelectObject"));
1512 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1515 wxLogLastError(wxT("SelectObject"));
1522 // this will create a monochrome bitmap with 0 points for the pixels
1523 // which have the same value as the background colour and 1 for the
1525 ::SetBkColor(srcDC
, maskColour
);
1526 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1529 ::SelectObject(srcDC
, hbmpSrcOld
);
1531 ::SelectObject(destDC
, hbmpDstOld
);
1535 #else // __WXMICROWIN__
1536 wxUnusedVar(bitmap
);
1537 wxUnusedVar(colour
);
1539 #endif // __WXMICROWIN__/!__WXMICROWIN__
1542 wxBitmap
wxMask::GetBitmap() const
1545 bmp
.SetHBITMAP(m_maskBitmap
);
1549 // ----------------------------------------------------------------------------
1551 // ----------------------------------------------------------------------------
1553 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1556 int width
, int height
, int depth
)
1558 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1560 return bitmap
&& Create(bitmap
, data
, type
, width
, height
, depth
);
1563 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1564 const wxString
& name
,
1566 int width
, int height
)
1568 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1570 return bitmap
&& LoadFile(bitmap
, name
, type
, width
, height
);
1573 bool wxBitmapHandler::Save(const wxGDIImage
*image
,
1574 const wxString
& name
,
1575 wxBitmapType type
) const
1577 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1579 return bitmap
&& SaveFile(bitmap
, name
, type
);
1582 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1583 const void* WXUNUSED(data
),
1584 wxBitmapType
WXUNUSED(type
),
1585 int WXUNUSED(width
),
1586 int WXUNUSED(height
),
1587 int WXUNUSED(depth
))
1592 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1593 const wxString
& WXUNUSED(name
),
1594 wxBitmapType
WXUNUSED(type
),
1595 int WXUNUSED(desiredWidth
),
1596 int WXUNUSED(desiredHeight
))
1601 bool wxBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1602 const wxString
& WXUNUSED(name
),
1603 wxBitmapType
WXUNUSED(type
),
1604 const wxPalette
*WXUNUSED(palette
)) const
1609 // ----------------------------------------------------------------------------
1610 // global helper functions implemented here
1611 // ----------------------------------------------------------------------------
1613 // helper of wxBitmapToHICON/HCURSOR
1615 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1622 // we can't create an icon/cursor form nothing
1626 if ( bmp
.HasAlpha() )
1630 #if wxUSE_WXDIB && wxUSE_IMAGE
1631 // CreateIconIndirect() requires non-pre-multiplied pixel data on input
1632 // as it does pre-multiplication internally itself so we need to create
1633 // a special DIB in such format to pass to it. This is inefficient but
1634 // better than creating an icon with wrong colours.
1635 AutoHBITMAP hbmpRelease
;
1636 hbmp
= wxDIB(bmp
.ConvertToImage(),
1637 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
1638 hbmpRelease
.Init(hbmp
);
1639 #else // !(wxUSE_WXDIB && wxUSE_IMAGE)
1640 hbmp
= GetHbitmapOf(bmp
);
1641 #endif // wxUSE_WXDIB && wxUSE_IMAGE
1643 // Create an empty mask bitmap.
1644 // it doesn't seem to work if we mess with the mask at all.
1646 hMonoBitmap(CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
));
1649 wxZeroMemory(iconInfo
);
1650 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1653 iconInfo
.xHotspot
= hotSpotX
;
1654 iconInfo
.yHotspot
= hotSpotY
;
1657 iconInfo
.hbmMask
= hMonoBitmap
;
1658 iconInfo
.hbmColor
= hbmp
;
1660 return ::CreateIconIndirect(&iconInfo
);
1663 wxMask
* mask
= bmp
.GetMask();
1667 // we must have a mask for an icon, so even if it's probably incorrect,
1668 // do create it (grey is the "standard" transparent colour)
1669 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1673 wxZeroMemory(iconInfo
);
1674 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1677 iconInfo
.xHotspot
= hotSpotX
;
1678 iconInfo
.yHotspot
= hotSpotY
;
1681 AutoHBITMAP
hbmpMask(wxInvertMask((HBITMAP
)mask
->GetMaskBitmap()));
1682 iconInfo
.hbmMask
= hbmpMask
;
1683 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1685 // black out the transparent area to preserve background colour, because
1686 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1687 // the mask to the dest rect.
1689 MemoryHDC dcSrc
, dcDst
;
1690 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1691 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1693 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1694 dcSrc
, 0, 0, SRCAND
) )
1696 wxLogLastError(wxT("BitBlt"));
1700 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1702 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1704 // we created the mask, now delete it
1711 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1713 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1716 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1718 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1721 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1723 #ifndef __WXMICROWIN__
1724 wxCHECK_MSG( hbmpMask
, 0, wxT("invalid bitmap in wxInvertMask") );
1726 // get width/height from the bitmap if not given
1730 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1735 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1736 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1737 if ( !hdcSrc
|| !hdcDst
)
1739 wxLogLastError(wxT("CreateCompatibleDC"));
1742 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1745 wxLogLastError(wxT("CreateBitmap"));
1748 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1749 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1750 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1754 wxLogLastError(wxT("BitBlt"));
1758 SelectObject(hdcSrc
,srcTmp
);
1759 SelectObject(hdcDst
,dstTmp
);