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
= wx_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
= wx_const_cast(wxBitmap
*, this);
268 // copy the other bitmap
269 if ( data
->m_hBitmap
)
271 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
272 self
->CopyFromDIB(dib
);
275 #endif // wxUSE_WXDIB
277 // copy the bitmap data
278 self
->m_refData
= new wxBitmapRefData(*data
);
281 wxBitmapRefData
* const
282 selfdata
= wx_static_cast(wxBitmapRefData
*, m_refData
);
284 // copy also the mask
285 wxMask
* const maskSrc
= data
->GetMask();
288 selfdata
->SetMask(new wxMask(*maskSrc
));
294 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
,
295 wxBitmapTransparency transp
)
297 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
298 // it may be either HICON or HCURSOR
299 HICON hicon
= (HICON
)icon
.GetHandle();
302 if ( !::GetIconInfo(hicon
, &iconInfo
) )
304 wxLogLastError(wxT("GetIconInfo"));
309 wxBitmapRefData
*refData
= new wxBitmapRefData
;
312 int w
= icon
.GetWidth(),
313 h
= icon
.GetHeight();
315 refData
->m_width
= w
;
316 refData
->m_height
= h
;
317 refData
->m_depth
= wxDisplayDepth();
319 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
324 wxFAIL_MSG( _T("unknown wxBitmapTransparency value") );
326 case wxBitmapTransparency_None
:
327 // nothing to do, refData->m_hasAlpha is false by default
330 case wxBitmapTransparency_Auto
:
332 // If the icon is 32 bits per pixel then it may have alpha channel
333 // data, although there are some icons that are 32 bpp but have no
334 // alpha... So convert to a DIB and manually check the 4th byte for
338 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(bm
), &bm
) &&
339 (bm
.bmBitsPixel
== 32) )
341 wxDIB
dib(iconInfo
.hbmColor
);
344 const unsigned char* pixels
= dib
.GetData();
345 for (int 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;
359 #endif // wxUSE_WXDIB
361 case wxBitmapTransparency_Always
:
362 refData
->m_hasAlpha
= true;
366 if ( !refData
->m_hasAlpha
)
368 // the mask returned by GetIconInfo() is inverted compared to the usual
370 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
373 // delete the old one now as we don't need it any more
374 ::DeleteObject(iconInfo
.hbmMask
);
377 #else // __WXMICROWIN__ || __WXWINCE__
382 #endif // !__WXWINCE__/__WXWINCE__
385 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
392 return CopyFromIconOrCursor(cursor
, transp
);
395 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
402 return CopyFromIconOrCursor(icon
, transp
);
405 #ifndef NEVER_USE_DIB
407 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
409 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
411 #ifdef SOMETIMES_USE_DIB
412 HBITMAP hbitmap
= dib
.CreateDDB();
415 #else // ALWAYS_USE_DIB
416 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
417 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
421 wxBitmapRefData
*refData
= new wxBitmapRefData
;
424 refData
->m_width
= dib
.GetWidth();
425 refData
->m_height
= dib
.GetHeight();
426 refData
->m_depth
= dib
.GetDepth();
428 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
431 wxPalette
*palette
= dib
.CreatePalette();
434 refData
->m_bitmapPalette
= *palette
;
438 #endif // wxUSE_PALETTE
443 #endif // NEVER_USE_DIB
445 wxBitmap::~wxBitmap()
449 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
451 #ifndef __WXMICROWIN__
452 wxBitmapRefData
*refData
= new wxBitmapRefData
;
455 refData
->m_width
= width
;
456 refData
->m_height
= height
;
457 refData
->m_depth
= depth
;
462 // we assume that it is in XBM format which is not quite the same as
463 // the format CreateBitmap() wants because the order of bytes in the
465 const size_t bytesPerLine
= (width
+ 7) / 8;
466 const size_t padding
= bytesPerLine
% 2;
467 const size_t len
= height
* ( padding
+ bytesPerLine
);
468 data
= (char *)malloc(len
);
469 const char *src
= bits
;
472 for ( int rows
= 0; rows
< height
; rows
++ )
474 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
476 unsigned char val
= *src
++;
477 unsigned char reversed
= 0;
479 for ( int bits
= 0; bits
< 8; bits
++)
482 reversed
|= (unsigned char)(val
& 0x01);
494 // bits should already be in Windows standard format
495 data
= (char *)bits
; // const_cast is harmless
498 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
501 wxLogLastError(wxT("CreateBitmap"));
509 SetHBITMAP((WXHBITMAP
)hbmp
);
513 wxBitmap::wxBitmap(int w
, int h
, int d
)
515 (void)Create(w
, h
, d
);
518 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
520 (void)Create(w
, h
, dc
);
523 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
525 (void)Create(data
, type
, width
, height
, depth
);
528 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
530 LoadFile(filename
, type
);
533 bool wxBitmap::Create(int width
, int height
, int depth
)
535 return DoCreate(width
, height
, depth
, 0);
538 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
540 wxCHECK_MSG( dc
.IsOk(), false, _T("invalid HDC in wxBitmap::Create()") );
542 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
545 return DoCreate(width
, height
, -1, impl
->GetHDC());
550 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
554 m_refData
= new wxBitmapRefData
;
556 GetBitmapData()->m_width
= w
;
557 GetBitmapData()->m_height
= h
;
559 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
561 #ifndef NEVER_USE_DIB
562 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
566 // create DIBs without alpha channel by default
574 // don't delete the DIB section in dib object dtor
577 GetBitmapData()->m_isDIB
= true;
578 GetBitmapData()->m_depth
= d
;
581 #endif // NEVER_USE_DIB
583 #ifndef ALWAYS_USE_DIB
584 #ifndef __WXMICROWIN__
587 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
590 wxLogLastError(wxT("CreateBitmap"));
593 GetBitmapData()->m_depth
= d
;
595 else // d == 0, create bitmap compatible with the screen
596 #endif // !__WXMICROWIN__
599 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
602 wxLogLastError(wxT("CreateCompatibleBitmap"));
605 GetBitmapData()->m_depth
= wxDisplayDepth();
607 #endif // !ALWAYS_USE_DIB
610 SetHBITMAP((WXHBITMAP
)hbmp
);
617 // ----------------------------------------------------------------------------
618 // wxImage to/from conversions for Microwin
619 // ----------------------------------------------------------------------------
621 // Microwin versions are so different from normal ones that it really doesn't
622 // make sense to use #ifdefs inside the function bodies
623 #ifdef __WXMICROWIN__
625 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
627 // Set this to 1 to experiment with mask code,
628 // which currently doesn't work
631 m_refData
= new wxBitmapRefData();
633 // Initial attempt at a simple-minded implementation.
634 // The bitmap will always be created at the screen depth,
635 // so the 'depth' argument is ignored.
637 HDC hScreenDC
= ::GetDC(NULL
);
638 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
640 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
641 HBITMAP hMaskBitmap
= NULL
;
642 HBITMAP hOldMaskBitmap
= NULL
;
644 unsigned char maskR
= 0;
645 unsigned char maskG
= 0;
646 unsigned char maskB
= 0;
648 // printf("Created bitmap %d\n", (int) hBitmap);
651 ::ReleaseDC(NULL
, hScreenDC
);
654 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
656 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
657 ::ReleaseDC(NULL
, hScreenDC
);
659 // created an mono-bitmap for the possible mask
660 bool hasMask
= image
.HasMask();
665 // FIXME: we should be able to pass bpp = 1, but
666 // GdBlit can't handle a different depth
668 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
670 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
672 maskR
= image
.GetMaskRed();
673 maskG
= image
.GetMaskGreen();
674 maskB
= image
.GetMaskBlue();
682 hScreenDC
= ::GetDC(NULL
);
683 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
684 ::ReleaseDC(NULL
, hScreenDC
);
686 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
694 for (i
= 0; i
< image
.GetWidth(); i
++)
696 for (j
= 0; j
< image
.GetHeight(); j
++)
698 unsigned char red
= image
.GetRed(i
, j
);
699 unsigned char green
= image
.GetGreen(i
, j
);
700 unsigned char blue
= image
.GetBlue(i
, j
);
702 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
706 // scan the bitmap for the transparent colour and set the corresponding
707 // pixels in the mask to BLACK and the rest to WHITE
708 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
709 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
711 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
716 ::SelectObject(hMemDC
, hOldBitmap
);
720 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
723 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
726 SetWidth(image
.GetWidth());
727 SetHeight(image
.GetHeight());
728 SetDepth(screenDepth
);
729 SetHBITMAP( (WXHBITMAP
) hBitmap
);
732 // Copy the palette from the source image
733 SetPalette(image
.GetPalette());
734 #endif // wxUSE_PALETTE
739 wxImage
wxBitmap::ConvertToImage() const
741 // Initial attempt at a simple-minded implementation.
742 // The bitmap will always be created at the screen depth,
743 // so the 'depth' argument is ignored.
744 // TODO: transparency (create a mask image)
748 wxFAIL_MSG( wxT("bitmap is invalid") );
754 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
756 // create an wxImage object
757 int width
= GetWidth();
758 int height
= GetHeight();
759 image
.Create( width
, height
);
760 unsigned char *data
= image
.GetData();
763 wxFAIL_MSG( wxT("could not allocate data for image") );
767 HDC hScreenDC
= ::GetDC(NULL
);
769 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
770 ::ReleaseDC(NULL
, hScreenDC
);
772 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
774 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
777 for (i
= 0; i
< GetWidth(); i
++)
779 for (j
= 0; j
< GetHeight(); j
++)
781 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
782 unsigned char red
= GetRValue(color
);
783 unsigned char green
= GetGValue(color
);
784 unsigned char blue
= GetBValue(color
);
786 image
.SetRGB(i
, j
, red
, green
, blue
);
790 ::SelectObject(hMemDC
, hOldBitmap
);
794 // Copy the palette from the source image
796 image
.SetPalette(* GetPalette());
797 #endif // wxUSE_PALETTE
802 #endif // __WXMICROWIN__
804 // ----------------------------------------------------------------------------
805 // wxImage to/from conversions
806 // ----------------------------------------------------------------------------
808 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
810 return CreateFromImage(image
, depth
, 0);
813 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
815 wxCHECK_MSG( dc
.IsOk(), false,
816 _T("invalid HDC in wxBitmap::CreateFromImage()") );
818 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
821 return CreateFromImage(image
, -1, impl
->GetHDC());
828 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
830 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
834 // first convert the image to DIB
835 const int h
= image
.GetHeight();
836 const int w
= image
.GetWidth();
843 depth
= dib
.GetDepth(); // Get depth from image if none specified
845 // store the bitmap parameters
846 wxBitmapRefData
*refData
= new wxBitmapRefData
;
847 refData
->m_width
= w
;
848 refData
->m_height
= h
;
849 refData
->m_hasAlpha
= image
.HasAlpha();
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 ( image
.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;
866 refData
->m_depth
= depth
;
868 #ifndef ALWAYS_USE_DIB
869 else // we need to convert DIB to DDB
871 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
873 refData
->m_depth
= depth
;
875 #endif // !ALWAYS_USE_DIB
877 // validate this object
878 SetHBITMAP((WXHBITMAP
)hbitmap
);
880 // finally also set the mask if we have one
881 if ( image
.HasMask() )
883 const size_t len
= 2*((w
+15)/16);
884 BYTE
*src
= image
.GetData();
885 BYTE
*data
= new BYTE
[h
*len
];
886 memset(data
, 0, h
*len
);
887 BYTE r
= image
.GetMaskRed(),
888 g
= image
.GetMaskGreen(),
889 b
= image
.GetMaskBlue();
891 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
895 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
897 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
900 if ( (mask
>>= 1) == 0 )
908 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
911 wxLogLastError(_T("CreateBitmap(mask)"));
915 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
924 wxImage
wxBitmap::ConvertToImage() const
926 // convert DDB to DIB
934 // and then DIB to our wxImage
935 wxImage image
= dib
.ConvertToImage();
941 // now do the same for the mask, if we have any
942 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
945 wxDIB
dibMask(hbmpMask
);
946 if ( dibMask
.IsOk() )
948 // TODO: use wxRawBitmap to iterate over DIB
950 // we hard code the mask colour for now but we could also make an
951 // effort (and waste time) to choose a colour not present in the
952 // image already to avoid having to fudge the pixels below --
953 // whether it's worth to do it is unclear however
954 static const int MASK_RED
= 1;
955 static const int MASK_GREEN
= 2;
956 static const int MASK_BLUE
= 3;
957 static const int MASK_BLUE_REPLACEMENT
= 2;
959 const int h
= dibMask
.GetHeight();
960 const int w
= dibMask
.GetWidth();
961 const int bpp
= dibMask
.GetDepth();
962 const int maskBytesPerPixel
= bpp
>> 3;
963 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
964 unsigned char *data
= image
.GetData();
966 // remember that DIBs are stored in bottom to top order
968 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
970 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
972 // traverse one mask DIB line
973 unsigned char *mask
= maskLineStart
;
974 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
976 // should this pixel be transparent?
979 // no, check that it isn't transparent by accident
980 if ( (data
[0] == MASK_RED
) &&
981 (data
[1] == MASK_GREEN
) &&
982 (data
[2] == MASK_BLUE
) )
984 // we have to fudge the colour a bit to prevent
985 // this pixel from appearing transparent
986 data
[2] = MASK_BLUE_REPLACEMENT
;
991 else // yes, transparent pixel
994 *data
++ = MASK_GREEN
;
1000 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1007 #else // !wxUSE_WXDIB
1010 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1011 int WXUNUSED(depth
),
1012 WXHDC
WXUNUSED(hdc
))
1017 wxImage
wxBitmap::ConvertToImage() const
1022 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1024 #endif // wxUSE_IMAGE
1026 // ----------------------------------------------------------------------------
1027 // loading and saving bitmaps
1028 // ----------------------------------------------------------------------------
1030 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1034 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1038 m_refData
= new wxBitmapRefData
;
1040 return handler
->LoadFile(this, filename
, type
, -1, -1);
1042 #if wxUSE_IMAGE && wxUSE_WXDIB
1043 else // no bitmap handler found
1046 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1048 *this = wxBitmap(image
);
1053 #endif // wxUSE_IMAGE
1058 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1062 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1066 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1071 m_refData
= new wxBitmapRefData
;
1073 return handler
->Create(this, data
, type
, width
, height
, depth
);
1076 bool wxBitmap::SaveFile(const wxString
& filename
,
1078 const wxPalette
*palette
) const
1080 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1084 return handler
->SaveFile(this, filename
, type
, palette
);
1086 #if wxUSE_IMAGE && wxUSE_WXDIB
1087 else // no bitmap handler found
1089 // FIXME what about palette? shouldn't we use it?
1090 wxImage image
= ConvertToImage();
1093 return image
.SaveFile(filename
, type
);
1096 #endif // wxUSE_IMAGE
1101 // ----------------------------------------------------------------------------
1102 // sub bitmap extraction
1103 // ----------------------------------------------------------------------------
1104 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1107 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1108 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1111 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1113 wxCHECK_MSG( Ok() &&
1114 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1115 (rect
.x
+rect
.width
<= GetWidth()) &&
1116 (rect
.y
+rect
.height
<= GetHeight()),
1117 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1119 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1120 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1122 #ifndef __WXMICROWIN__
1123 // handle alpha channel, if any
1132 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1136 wxLogLastError(_T("SelectObject(destBitmap)"));
1139 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1140 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1142 wxLogLastError(_T("BitBlt"));
1146 // copy mask if there is one
1149 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1151 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1152 selectDst(dcDst
, hbmpMask
);
1154 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1155 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1157 wxLogLastError(_T("BitBlt"));
1160 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1163 #endif // !__WXMICROWIN__
1168 // ----------------------------------------------------------------------------
1169 // wxBitmap accessors
1170 // ----------------------------------------------------------------------------
1173 wxPalette
* wxBitmap::GetPalette() const
1175 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1176 : (wxPalette
*) NULL
;
1180 wxMask
*wxBitmap::GetMask() const
1182 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1185 wxBitmap
wxBitmap::GetMaskBitmap() const
1188 wxMask
*mask
= GetMask();
1190 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1196 wxDC
*wxBitmap::GetSelectedInto() const
1198 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1203 void wxBitmap::UseAlpha()
1205 if ( GetBitmapData() )
1206 GetBitmapData()->m_hasAlpha
= true;
1209 bool wxBitmap::HasAlpha() const
1211 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1214 // ----------------------------------------------------------------------------
1216 // ----------------------------------------------------------------------------
1220 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1222 if ( GetBitmapData() )
1223 GetBitmapData()->m_selectedInto
= dc
;
1230 void wxBitmap::SetPalette(const wxPalette
& palette
)
1234 GetBitmapData()->m_bitmapPalette
= palette
;
1237 #endif // wxUSE_PALETTE
1239 void wxBitmap::SetMask(wxMask
*mask
)
1243 GetBitmapData()->SetMask(mask
);
1246 // ----------------------------------------------------------------------------
1247 // raw bitmap access support
1248 // ----------------------------------------------------------------------------
1250 #ifdef wxHAS_RAW_BITMAP
1252 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1257 // no bitmap, no data (raw or otherwise)
1261 // if we're already a DIB we can access our data directly, but if not we
1262 // need to convert this DDB to a DIB section and use it for raw access and
1263 // then convert it back
1265 if ( !GetBitmapData()->m_isDIB
)
1267 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1268 _T("GetRawData() may be called only once") );
1270 wxDIB
*dib
= new wxDIB(*this);
1278 // we'll free it in UngetRawData()
1279 GetBitmapData()->m_dib
= dib
;
1281 hDIB
= dib
->GetHandle();
1285 hDIB
= GetHbitmap();
1289 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1291 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1296 // check that the bitmap is in correct format
1297 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1299 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1304 // ok, store the relevant info in wxPixelDataBase
1305 const LONG h
= ds
.dsBm
.bmHeight
;
1307 data
.m_width
= ds
.dsBm
.bmWidth
;
1310 // remember that DIBs are stored in top to bottom order!
1311 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1312 // multiple of 2, as required by the documentation. So we use the official
1313 // formula, which we already use elsewhere.)
1314 const LONG bytesPerRow
=
1315 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1316 data
.m_stride
= -bytesPerRow
;
1318 char *bits
= (char *)ds
.dsBm
.bmBits
;
1321 bits
+= (h
- 1)*bytesPerRow
;
1330 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1338 // invalid data, don't crash -- but don't assert neither as we're
1339 // called automatically from wxPixelDataBase dtor and so there is no
1340 // way to prevent this from happening
1344 // if we're a DDB we need to convert DIB back to DDB now to make the
1345 // changes made via raw bitmap access effective
1346 if ( !GetBitmapData()->m_isDIB
)
1348 wxDIB
*dib
= GetBitmapData()->m_dib
;
1349 GetBitmapData()->m_dib
= NULL
;
1355 #endif // wxUSE_WXDIB
1357 #endif // wxHAS_RAW_BITMAP
1359 // ----------------------------------------------------------------------------
1361 // ----------------------------------------------------------------------------
1369 wxMask::wxMask(const wxMask
&mask
)
1374 HDC srcDC
= CreateCompatibleDC(0);
1375 HDC destDC
= CreateCompatibleDC(0);
1377 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1378 // so we'll use GetObject() API here:
1379 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1381 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1385 // create our HBITMAP
1386 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1387 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1389 // copy the mask's HBITMAP into our HBITMAP
1390 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1391 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1393 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1395 SelectObject(srcDC
, 0);
1397 SelectObject(destDC
, 0);
1401 // Construct a mask from a bitmap and a colour indicating
1402 // the transparent area
1403 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1406 Create(bitmap
, colour
);
1409 // Construct a mask from a bitmap and a palette index indicating
1410 // the transparent area
1411 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1414 Create(bitmap
, paletteIndex
);
1417 // Construct a mask from a mono bitmap (copies the bitmap).
1418 wxMask::wxMask(const wxBitmap
& bitmap
)
1427 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1430 // Create a mask from a mono bitmap (copies the bitmap).
1431 bool wxMask::Create(const wxBitmap
& bitmap
)
1433 #ifndef __WXMICROWIN__
1434 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1435 _T("can't create mask from invalid or not monochrome bitmap") );
1439 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1443 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1448 HDC srcDC
= CreateCompatibleDC(0);
1449 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1450 HDC destDC
= CreateCompatibleDC(0);
1451 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1452 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1453 SelectObject(srcDC
, 0);
1455 SelectObject(destDC
, 0);
1459 wxUnusedVar(bitmap
);
1464 // Create a mask from a bitmap and a palette index indicating
1465 // the transparent area
1466 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1470 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1475 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1477 unsigned char red
, green
, blue
;
1478 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1480 wxColour
transparentColour(red
, green
, blue
);
1481 return Create(bitmap
, transparentColour
);
1484 #endif // wxUSE_PALETTE
1489 // Create a mask from a bitmap and a colour indicating
1490 // the transparent area
1491 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1493 #ifndef __WXMICROWIN__
1494 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1498 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1502 int width
= bitmap
.GetWidth(),
1503 height
= bitmap
.GetHeight();
1505 // scan the bitmap for the transparent colour and set the corresponding
1506 // pixels in the mask to BLACK and the rest to WHITE
1507 COLORREF maskColour
= wxColourToPalRGB(colour
);
1508 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1510 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1511 HDC destDC
= ::CreateCompatibleDC(NULL
);
1512 if ( !srcDC
|| !destDC
)
1514 wxLogLastError(wxT("CreateCompatibleDC"));
1519 // SelectObject() will fail
1520 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1521 _T("bitmap can't be selected in another DC") );
1523 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1526 wxLogLastError(wxT("SelectObject"));
1531 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1534 wxLogLastError(wxT("SelectObject"));
1541 // this will create a monochrome bitmap with 0 points for the pixels
1542 // which have the same value as the background colour and 1 for the
1544 ::SetBkColor(srcDC
, maskColour
);
1545 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1548 ::SelectObject(srcDC
, hbmpSrcOld
);
1550 ::SelectObject(destDC
, hbmpDstOld
);
1554 #else // __WXMICROWIN__
1555 wxUnusedVar(bitmap
);
1556 wxUnusedVar(colour
);
1558 #endif // __WXMICROWIN__/!__WXMICROWIN__
1561 // ----------------------------------------------------------------------------
1563 // ----------------------------------------------------------------------------
1565 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1568 int width
, int height
, int depth
)
1570 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1572 return bitmap
&& Create(bitmap
, data
, type
, width
, height
, depth
);
1575 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1576 const wxString
& name
,
1578 int width
, int height
)
1580 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1582 return bitmap
&& LoadFile(bitmap
, name
, type
, width
, height
);
1585 bool wxBitmapHandler::Save(const wxGDIImage
*image
,
1586 const wxString
& name
,
1587 wxBitmapType type
) const
1589 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1591 return bitmap
&& SaveFile(bitmap
, name
, type
);
1594 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1595 const void* WXUNUSED(data
),
1596 wxBitmapType
WXUNUSED(type
),
1597 int WXUNUSED(width
),
1598 int WXUNUSED(height
),
1599 int WXUNUSED(depth
))
1604 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1605 const wxString
& WXUNUSED(name
),
1606 wxBitmapType
WXUNUSED(type
),
1607 int WXUNUSED(desiredWidth
),
1608 int WXUNUSED(desiredHeight
))
1613 bool wxBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1614 const wxString
& WXUNUSED(name
),
1615 wxBitmapType
WXUNUSED(type
),
1616 const wxPalette
*WXUNUSED(palette
)) const
1621 // ----------------------------------------------------------------------------
1622 // global helper functions implemented here
1623 // ----------------------------------------------------------------------------
1625 // helper of wxBitmapToHICON/HCURSOR
1627 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1634 // we can't create an icon/cursor form nothing
1638 if ( bmp
.HasAlpha() )
1640 // Create an empty mask bitmap.
1641 // it doesn't seem to work if we mess with the mask at all.
1642 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1645 wxZeroMemory(iconInfo
);
1646 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1649 iconInfo
.xHotspot
= hotSpotX
;
1650 iconInfo
.yHotspot
= hotSpotY
;
1653 iconInfo
.hbmMask
= hMonoBitmap
;
1654 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1656 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1658 ::DeleteObject(hMonoBitmap
);
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 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1682 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1684 // black out the transparent area to preserve background colour, because
1685 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1686 // the mask to the dest rect.
1688 MemoryHDC dcSrc
, dcDst
;
1689 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1690 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1692 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1693 dcSrc
, 0, 0, SRCAND
) )
1695 wxLogLastError(_T("BitBlt"));
1699 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1701 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1703 // we created the mask, now delete it
1707 // delete the inverted mask bitmap we created as well
1708 ::DeleteObject(iconInfo
.hbmMask
);
1713 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1715 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1718 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1720 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1723 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1725 #ifndef __WXMICROWIN__
1726 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1728 // get width/height from the bitmap if not given
1732 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1737 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1738 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1739 if ( !hdcSrc
|| !hdcDst
)
1741 wxLogLastError(wxT("CreateCompatibleDC"));
1744 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1747 wxLogLastError(wxT("CreateBitmap"));
1750 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1751 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1752 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1756 wxLogLastError(wxT("BitBlt"));
1760 SelectObject(hdcSrc
,srcTmp
);
1761 SelectObject(hdcDst
,dstTmp
);