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 const unsigned char* pixels
= dib
.GetData();
346 for (int idx
= 0; idx
< w
*h
*4; idx
+=4)
348 if (pixels
[idx
+3] != 0)
350 // If there is an alpha byte that is non-zero
351 // then set the alpha flag and stop checking
352 refData
->m_hasAlpha
= true;
360 #endif // wxUSE_WXDIB
362 case wxBitmapTransparency_Always
:
363 refData
->m_hasAlpha
= true;
367 if ( !refData
->m_hasAlpha
)
369 // the mask returned by GetIconInfo() is inverted compared to the usual
371 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
374 // delete the old one now as we don't need it any more
375 ::DeleteObject(iconInfo
.hbmMask
);
378 #else // __WXMICROWIN__ || __WXWINCE__
383 #endif // !__WXWINCE__/__WXWINCE__
386 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
390 if ( !cursor
.IsOk() )
393 return CopyFromIconOrCursor(cursor
, transp
);
396 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
403 return CopyFromIconOrCursor(icon
, transp
);
406 #ifndef NEVER_USE_DIB
408 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
410 wxCHECK_MSG( dib
.IsOk(), false, wxT("invalid DIB in CopyFromDIB") );
412 #ifdef SOMETIMES_USE_DIB
413 HBITMAP hbitmap
= dib
.CreateDDB();
416 #else // ALWAYS_USE_DIB
417 HBITMAP hbitmap
= const_cast<wxDIB
&>(dib
).Detach();
418 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
422 wxBitmapRefData
*refData
= new wxBitmapRefData
;
425 refData
->m_width
= dib
.GetWidth();
426 refData
->m_height
= dib
.GetHeight();
427 refData
->m_depth
= dib
.GetDepth();
429 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
432 wxPalette
*palette
= dib
.CreatePalette();
435 refData
->m_bitmapPalette
= *palette
;
439 #endif // wxUSE_PALETTE
444 #endif // NEVER_USE_DIB
446 wxBitmap::~wxBitmap()
450 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
452 #ifndef __WXMICROWIN__
453 wxBitmapRefData
*refData
= new wxBitmapRefData
;
456 refData
->m_width
= width
;
457 refData
->m_height
= height
;
458 refData
->m_depth
= depth
;
463 // we assume that it is in XBM format which is not quite the same as
464 // the format CreateBitmap() wants because the order of bytes in the
466 const size_t bytesPerLine
= (width
+ 7) / 8;
467 const size_t padding
= bytesPerLine
% 2;
468 const size_t len
= height
* ( padding
+ bytesPerLine
);
469 data
= (char *)malloc(len
);
470 const char *src
= bits
;
473 for ( int rows
= 0; rows
< height
; rows
++ )
475 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
477 unsigned char val
= *src
++;
478 unsigned char reversed
= 0;
480 for ( int bits
= 0; bits
< 8; bits
++)
483 reversed
|= (unsigned char)(val
& 0x01);
495 // bits should already be in Windows standard format
496 data
= const_cast<char *>(bits
);
499 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
502 wxLogLastError(wxT("CreateBitmap"));
510 SetHBITMAP((WXHBITMAP
)hbmp
);
514 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
516 (void)Create(w
, h
, dc
);
519 wxBitmap::wxBitmap(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
521 (void)Create(data
, type
, width
, height
, depth
);
524 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
526 LoadFile(filename
, type
);
529 bool wxBitmap::Create(int width
, int height
, int depth
)
531 return DoCreate(width
, height
, depth
, 0);
534 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
536 wxCHECK_MSG( dc
.IsOk(), false, wxT("invalid HDC in wxBitmap::Create()") );
538 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
541 return DoCreate(width
, height
, -1, impl
->GetHDC());
546 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
550 m_refData
= new wxBitmapRefData
;
552 GetBitmapData()->m_width
= w
;
553 GetBitmapData()->m_height
= h
;
555 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
557 #ifndef NEVER_USE_DIB
558 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
562 // create DIBs without alpha channel by default
570 // don't delete the DIB section in dib object dtor
573 GetBitmapData()->m_isDIB
= true;
574 GetBitmapData()->m_depth
= d
;
577 #endif // NEVER_USE_DIB
579 #ifndef ALWAYS_USE_DIB
580 #ifndef __WXMICROWIN__
583 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
586 wxLogLastError(wxT("CreateBitmap"));
589 GetBitmapData()->m_depth
= d
;
591 else // d == 0, create bitmap compatible with the screen
592 #endif // !__WXMICROWIN__
595 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
598 wxLogLastError(wxT("CreateCompatibleBitmap"));
601 GetBitmapData()->m_depth
= wxDisplayDepth();
603 #endif // !ALWAYS_USE_DIB
606 SetHBITMAP((WXHBITMAP
)hbmp
);
613 // ----------------------------------------------------------------------------
614 // wxImage to/from conversions for Microwin
615 // ----------------------------------------------------------------------------
617 // Microwin versions are so different from normal ones that it really doesn't
618 // make sense to use #ifdefs inside the function bodies
619 #ifdef __WXMICROWIN__
621 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
623 // Set this to 1 to experiment with mask code,
624 // which currently doesn't work
627 m_refData
= new wxBitmapRefData();
629 // Initial attempt at a simple-minded implementation.
630 // The bitmap will always be created at the screen depth,
631 // so the 'depth' argument is ignored.
633 HDC hScreenDC
= ::GetDC(NULL
);
634 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
636 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
637 HBITMAP hMaskBitmap
= NULL
;
638 HBITMAP hOldMaskBitmap
= NULL
;
640 unsigned char maskR
= 0;
641 unsigned char maskG
= 0;
642 unsigned char maskB
= 0;
644 // printf("Created bitmap %d\n", (int) hBitmap);
647 ::ReleaseDC(NULL
, hScreenDC
);
650 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
652 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
653 ::ReleaseDC(NULL
, hScreenDC
);
655 // created an mono-bitmap for the possible mask
656 bool hasMask
= image
.HasMask();
661 // FIXME: we should be able to pass bpp = 1, but
662 // GdBlit can't handle a different depth
664 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
666 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
668 maskR
= image
.GetMaskRed();
669 maskG
= image
.GetMaskGreen();
670 maskB
= image
.GetMaskBlue();
678 hScreenDC
= ::GetDC(NULL
);
679 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
680 ::ReleaseDC(NULL
, hScreenDC
);
682 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
690 for (i
= 0; i
< image
.GetWidth(); i
++)
692 for (j
= 0; j
< image
.GetHeight(); j
++)
694 unsigned char red
= image
.GetRed(i
, j
);
695 unsigned char green
= image
.GetGreen(i
, j
);
696 unsigned char blue
= image
.GetBlue(i
, j
);
698 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
702 // scan the bitmap for the transparent colour and set the corresponding
703 // pixels in the mask to BLACK and the rest to WHITE
704 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
705 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
707 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
712 ::SelectObject(hMemDC
, hOldBitmap
);
716 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
719 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
722 SetWidth(image
.GetWidth());
723 SetHeight(image
.GetHeight());
724 SetDepth(screenDepth
);
725 SetHBITMAP( (WXHBITMAP
) hBitmap
);
728 // Copy the palette from the source image
729 SetPalette(image
.GetPalette());
730 #endif // wxUSE_PALETTE
735 wxImage
wxBitmap::ConvertToImage() const
737 // Initial attempt at a simple-minded implementation.
738 // The bitmap will always be created at the screen depth,
739 // so the 'depth' argument is ignored.
740 // TODO: transparency (create a mask image)
744 wxFAIL_MSG( wxT("bitmap is invalid") );
750 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
752 // create an wxImage object
753 int width
= GetWidth();
754 int height
= GetHeight();
755 image
.Create( width
, height
);
756 unsigned char *data
= image
.GetData();
759 wxFAIL_MSG( wxT("could not allocate data for image") );
763 HDC hScreenDC
= ::GetDC(NULL
);
765 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
766 ::ReleaseDC(NULL
, hScreenDC
);
768 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
770 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
773 for (i
= 0; i
< GetWidth(); i
++)
775 for (j
= 0; j
< GetHeight(); j
++)
777 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
778 unsigned char red
= GetRValue(color
);
779 unsigned char green
= GetGValue(color
);
780 unsigned char blue
= GetBValue(color
);
782 image
.SetRGB(i
, j
, red
, green
, blue
);
786 ::SelectObject(hMemDC
, hOldBitmap
);
790 // Copy the palette from the source image
792 image
.SetPalette(* GetPalette());
793 #endif // wxUSE_PALETTE
798 #endif // __WXMICROWIN__
800 // ----------------------------------------------------------------------------
801 // wxImage to/from conversions
802 // ----------------------------------------------------------------------------
804 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
806 return CreateFromImage(image
, depth
, 0);
809 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
811 wxCHECK_MSG( dc
.IsOk(), false,
812 wxT("invalid HDC in wxBitmap::CreateFromImage()") );
814 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
817 return CreateFromImage(image
, -1, impl
->GetHDC());
824 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
826 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
830 // first convert the image to DIB
831 const int h
= image
.GetHeight();
832 const int w
= image
.GetWidth();
838 const bool hasAlpha
= image
.HasAlpha();
841 depth
= dib
.GetDepth();
843 // store the bitmap parameters
844 wxBitmapRefData
* const refData
= new wxBitmapRefData
;
845 refData
->m_width
= w
;
846 refData
->m_height
= h
;
847 refData
->m_hasAlpha
= hasAlpha
;
848 refData
->m_depth
= depth
;
853 // next either store DIB as is or create a DDB from it
854 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
856 // are we going to use DIB?
858 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
859 if ( hasAlpha
|| wxShouldCreateDIB(w
, h
, depth
, hdc
) )
861 // don't delete the DIB section in dib object dtor
862 hbitmap
= dib
.Detach();
864 refData
->m_isDIB
= true;
866 #ifndef ALWAYS_USE_DIB
867 else // we need to convert DIB to DDB
869 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
871 #endif // !ALWAYS_USE_DIB
873 // validate this object
874 SetHBITMAP((WXHBITMAP
)hbitmap
);
876 // finally also set the mask if we have one
877 if ( image
.HasMask() )
879 const size_t len
= 2*((w
+15)/16);
880 BYTE
*src
= image
.GetData();
881 BYTE
*data
= new BYTE
[h
*len
];
882 memset(data
, 0, h
*len
);
883 BYTE r
= image
.GetMaskRed(),
884 g
= image
.GetMaskGreen(),
885 b
= image
.GetMaskBlue();
887 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
891 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
893 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
896 if ( (mask
>>= 1) == 0 )
904 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
907 wxLogLastError(wxT("CreateBitmap(mask)"));
911 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
920 wxImage
wxBitmap::ConvertToImage() const
922 // convert DDB to DIB
930 // and then DIB to our wxImage
931 wxImage image
= dib
.ConvertToImage();
937 // now do the same for the mask, if we have any
938 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
941 wxDIB
dibMask(hbmpMask
);
942 if ( dibMask
.IsOk() )
944 // TODO: use wxRawBitmap to iterate over DIB
946 // we hard code the mask colour for now but we could also make an
947 // effort (and waste time) to choose a colour not present in the
948 // image already to avoid having to fudge the pixels below --
949 // whether it's worth to do it is unclear however
950 static const int MASK_RED
= 1;
951 static const int MASK_GREEN
= 2;
952 static const int MASK_BLUE
= 3;
953 static const int MASK_BLUE_REPLACEMENT
= 2;
955 const int h
= dibMask
.GetHeight();
956 const int w
= dibMask
.GetWidth();
957 const int bpp
= dibMask
.GetDepth();
958 const int maskBytesPerPixel
= bpp
>> 3;
959 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
960 unsigned char *data
= image
.GetData();
962 // remember that DIBs are stored in bottom to top order
964 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
966 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
968 // traverse one mask DIB line
969 unsigned char *mask
= maskLineStart
;
970 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
972 // should this pixel be transparent?
975 // no, check that it isn't transparent by accident
976 if ( (data
[0] == MASK_RED
) &&
977 (data
[1] == MASK_GREEN
) &&
978 (data
[2] == MASK_BLUE
) )
980 // we have to fudge the colour a bit to prevent
981 // this pixel from appearing transparent
982 data
[2] = MASK_BLUE_REPLACEMENT
;
987 else // yes, transparent pixel
990 *data
++ = MASK_GREEN
;
996 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1003 #else // !wxUSE_WXDIB
1006 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1007 int WXUNUSED(depth
),
1008 WXHDC
WXUNUSED(hdc
))
1013 wxImage
wxBitmap::ConvertToImage() const
1018 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1020 #endif // wxUSE_IMAGE
1022 // ----------------------------------------------------------------------------
1023 // loading and saving bitmaps
1024 // ----------------------------------------------------------------------------
1026 bool wxBitmap::LoadFile(const wxString
& filename
, wxBitmapType type
)
1030 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1034 m_refData
= new wxBitmapRefData
;
1036 return handler
->LoadFile(this, filename
, type
, -1, -1);
1038 #if wxUSE_IMAGE && wxUSE_WXDIB
1039 else // no bitmap handler found
1042 if ( image
.LoadFile( filename
, type
) && image
.IsOk() )
1044 *this = wxBitmap(image
);
1049 #endif // wxUSE_IMAGE
1054 bool wxBitmap::Create(const void* data
, wxBitmapType type
, int width
, int height
, int depth
)
1058 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1062 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1067 m_refData
= new wxBitmapRefData
;
1069 return handler
->Create(this, data
, type
, width
, height
, depth
);
1072 bool wxBitmap::SaveFile(const wxString
& filename
,
1074 const wxPalette
*palette
) const
1076 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1080 return handler
->SaveFile(this, filename
, type
, palette
);
1082 #if wxUSE_IMAGE && wxUSE_WXDIB
1083 else // no bitmap handler found
1085 // FIXME what about palette? shouldn't we use it?
1086 wxImage image
= ConvertToImage();
1089 return image
.SaveFile(filename
, type
);
1092 #endif // wxUSE_IMAGE
1097 // ----------------------------------------------------------------------------
1098 // sub bitmap extraction
1099 // ----------------------------------------------------------------------------
1100 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1103 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1104 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1107 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1109 wxCHECK_MSG( IsOk() &&
1110 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1111 (rect
.x
+rect
.width
<= GetWidth()) &&
1112 (rect
.y
+rect
.height
<= GetHeight()),
1113 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1115 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1116 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1118 #ifndef __WXMICROWIN__
1119 // handle alpha channel, if any
1128 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1132 wxLogLastError(wxT("SelectObject(destBitmap)"));
1135 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1136 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1138 wxLogLastError(wxT("BitBlt"));
1142 // copy mask if there is one
1145 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1147 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1148 selectDst(dcDst
, hbmpMask
);
1150 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1151 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1153 wxLogLastError(wxT("BitBlt"));
1156 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1159 #endif // !__WXMICROWIN__
1164 // ----------------------------------------------------------------------------
1165 // wxBitmap accessors
1166 // ----------------------------------------------------------------------------
1169 wxPalette
* wxBitmap::GetPalette() const
1171 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1176 wxMask
*wxBitmap::GetMask() const
1178 return GetBitmapData() ? GetBitmapData()->GetMask() : NULL
;
1181 wxBitmap
wxBitmap::GetMaskBitmap() const
1184 wxMask
*mask
= GetMask();
1186 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1190 wxDC
*wxBitmap::GetSelectedInto() const
1193 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: NULL
;
1199 void wxBitmap::UseAlpha()
1201 if ( GetBitmapData() )
1202 GetBitmapData()->m_hasAlpha
= true;
1205 bool wxBitmap::HasAlpha() const
1207 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1214 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1217 if ( GetBitmapData() )
1218 GetBitmapData()->m_selectedInto
= dc
;
1226 void wxBitmap::SetPalette(const wxPalette
& palette
)
1230 GetBitmapData()->m_bitmapPalette
= palette
;
1233 #endif // wxUSE_PALETTE
1235 void wxBitmap::SetMask(wxMask
*mask
)
1239 GetBitmapData()->SetMask(mask
);
1242 // ----------------------------------------------------------------------------
1243 // raw bitmap access support
1244 // ----------------------------------------------------------------------------
1246 #ifdef wxHAS_RAW_BITMAP
1248 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1253 // no bitmap, no data (raw or otherwise)
1257 // if we're already a DIB we can access our data directly, but if not we
1258 // need to convert this DDB to a DIB section and use it for raw access and
1259 // then convert it back
1261 if ( !GetBitmapData()->m_isDIB
)
1263 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1264 wxT("GetRawData() may be called only once") );
1266 wxDIB
*dib
= new wxDIB(*this);
1274 // we'll free it in UngetRawData()
1275 GetBitmapData()->m_dib
= dib
;
1277 hDIB
= dib
->GetHandle();
1281 hDIB
= GetHbitmap();
1285 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1287 wxFAIL_MSG( wxT("failed to get DIBSECTION from a DIB?") );
1292 // check that the bitmap is in correct format
1293 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1295 wxFAIL_MSG( wxT("incorrect bitmap type in wxBitmap::GetRawData()") );
1300 // ok, store the relevant info in wxPixelDataBase
1301 const LONG h
= ds
.dsBm
.bmHeight
;
1303 data
.m_width
= ds
.dsBm
.bmWidth
;
1306 // remember that DIBs are stored in top to bottom order!
1307 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1308 // multiple of 2, as required by the documentation. So we use the official
1309 // formula, which we already use elsewhere.)
1310 const LONG bytesPerRow
=
1311 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1312 data
.m_stride
= -bytesPerRow
;
1314 char *bits
= (char *)ds
.dsBm
.bmBits
;
1317 bits
+= (h
- 1)*bytesPerRow
;
1326 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1334 // invalid data, don't crash -- but don't assert neither as we're
1335 // called automatically from wxPixelDataBase dtor and so there is no
1336 // way to prevent this from happening
1340 // if we're a DDB we need to convert DIB back to DDB now to make the
1341 // changes made via raw bitmap access effective
1342 if ( !GetBitmapData()->m_isDIB
)
1344 wxDIB
*dib
= GetBitmapData()->m_dib
;
1345 GetBitmapData()->m_dib
= NULL
;
1351 #endif // wxUSE_WXDIB
1353 #endif // wxHAS_RAW_BITMAP
1355 // ----------------------------------------------------------------------------
1357 // ----------------------------------------------------------------------------
1365 wxMask::wxMask(const wxMask
&mask
)
1370 HDC srcDC
= CreateCompatibleDC(0);
1371 HDC destDC
= CreateCompatibleDC(0);
1373 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1374 // so we'll use GetObject() API here:
1375 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1377 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1381 // create our HBITMAP
1382 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1383 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1385 // copy the mask's HBITMAP into our HBITMAP
1386 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1387 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1389 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1391 SelectObject(srcDC
, 0);
1393 SelectObject(destDC
, 0);
1397 // Construct a mask from a bitmap and a colour indicating
1398 // the transparent area
1399 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1402 Create(bitmap
, colour
);
1405 // Construct a mask from a bitmap and a palette index indicating
1406 // the transparent area
1407 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1410 Create(bitmap
, paletteIndex
);
1413 // Construct a mask from a mono bitmap (copies the bitmap).
1414 wxMask::wxMask(const wxBitmap
& bitmap
)
1423 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1426 // Create a mask from a mono bitmap (copies the bitmap).
1427 bool wxMask::Create(const wxBitmap
& bitmap
)
1429 #ifndef __WXMICROWIN__
1430 wxCHECK_MSG( bitmap
.IsOk() && bitmap
.GetDepth() == 1, false,
1431 wxT("can't create mask from invalid or not monochrome bitmap") );
1435 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1439 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1444 HDC srcDC
= CreateCompatibleDC(0);
1445 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1446 HDC destDC
= CreateCompatibleDC(0);
1447 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1448 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1449 SelectObject(srcDC
, 0);
1451 SelectObject(destDC
, 0);
1455 wxUnusedVar(bitmap
);
1460 // Create a mask from a bitmap and a palette index indicating
1461 // the transparent area
1462 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1466 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1471 if (bitmap
.IsOk() && bitmap
.GetPalette()->IsOk())
1473 unsigned char red
, green
, blue
;
1474 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1476 wxColour
transparentColour(red
, green
, blue
);
1477 return Create(bitmap
, transparentColour
);
1480 #endif // wxUSE_PALETTE
1485 // Create a mask from a bitmap and a colour indicating
1486 // the transparent area
1487 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1489 #ifndef __WXMICROWIN__
1490 wxCHECK_MSG( bitmap
.IsOk(), false, wxT("invalid bitmap in wxMask::Create") );
1494 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1498 int width
= bitmap
.GetWidth(),
1499 height
= bitmap
.GetHeight();
1501 // scan the bitmap for the transparent colour and set the corresponding
1502 // pixels in the mask to BLACK and the rest to WHITE
1503 COLORREF maskColour
= wxColourToPalRGB(colour
);
1504 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1506 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1507 HDC destDC
= ::CreateCompatibleDC(NULL
);
1508 if ( !srcDC
|| !destDC
)
1510 wxLogLastError(wxT("CreateCompatibleDC"));
1515 // SelectObject() will fail
1516 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1517 wxT("bitmap can't be selected in another DC") );
1519 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1522 wxLogLastError(wxT("SelectObject"));
1527 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1530 wxLogLastError(wxT("SelectObject"));
1537 // this will create a monochrome bitmap with 0 points for the pixels
1538 // which have the same value as the background colour and 1 for the
1540 ::SetBkColor(srcDC
, maskColour
);
1541 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1544 ::SelectObject(srcDC
, hbmpSrcOld
);
1546 ::SelectObject(destDC
, hbmpDstOld
);
1550 #else // __WXMICROWIN__
1551 wxUnusedVar(bitmap
);
1552 wxUnusedVar(colour
);
1554 #endif // __WXMICROWIN__/!__WXMICROWIN__
1557 // ----------------------------------------------------------------------------
1559 // ----------------------------------------------------------------------------
1561 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1564 int width
, int height
, int depth
)
1566 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1568 return bitmap
&& Create(bitmap
, data
, type
, width
, height
, depth
);
1571 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1572 const wxString
& name
,
1574 int width
, int height
)
1576 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1578 return bitmap
&& LoadFile(bitmap
, name
, type
, width
, height
);
1581 bool wxBitmapHandler::Save(const wxGDIImage
*image
,
1582 const wxString
& name
,
1583 wxBitmapType type
) const
1585 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1587 return bitmap
&& SaveFile(bitmap
, name
, type
);
1590 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1591 const void* WXUNUSED(data
),
1592 wxBitmapType
WXUNUSED(type
),
1593 int WXUNUSED(width
),
1594 int WXUNUSED(height
),
1595 int WXUNUSED(depth
))
1600 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1601 const wxString
& WXUNUSED(name
),
1602 wxBitmapType
WXUNUSED(type
),
1603 int WXUNUSED(desiredWidth
),
1604 int WXUNUSED(desiredHeight
))
1609 bool wxBitmapHandler::SaveFile(const wxBitmap
*WXUNUSED(bitmap
),
1610 const wxString
& WXUNUSED(name
),
1611 wxBitmapType
WXUNUSED(type
),
1612 const wxPalette
*WXUNUSED(palette
)) const
1617 // ----------------------------------------------------------------------------
1618 // global helper functions implemented here
1619 // ----------------------------------------------------------------------------
1621 // helper of wxBitmapToHICON/HCURSOR
1623 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1630 // we can't create an icon/cursor form nothing
1634 if ( bmp
.HasAlpha() )
1638 #if wxUSE_WXDIB && wxUSE_IMAGE
1639 // CreateIconIndirect() requires non-pre-multiplied pixel data on input
1640 // as it does pre-multiplication internally itself so we need to create
1641 // a special DIB in such format to pass to it. This is inefficient but
1642 // better than creating an icon with wrong colours.
1643 AutoHBITMAP hbmpRelease
;
1644 hbmp
= wxDIB(bmp
.ConvertToImage(),
1645 wxDIB::PixelFormat_NotPreMultiplied
).Detach();
1646 hbmpRelease
.Init(hbmp
);
1647 #else // !(wxUSE_WXDIB && wxUSE_IMAGE)
1648 hbmp
= GetHbitmapOf(bmp
);
1649 #endif // wxUSE_WXDIB && wxUSE_IMAGE
1651 // Create an empty mask bitmap.
1652 // it doesn't seem to work if we mess with the mask at all.
1654 hMonoBitmap(CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
));
1657 wxZeroMemory(iconInfo
);
1658 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1661 iconInfo
.xHotspot
= hotSpotX
;
1662 iconInfo
.yHotspot
= hotSpotY
;
1665 iconInfo
.hbmMask
= hMonoBitmap
;
1666 iconInfo
.hbmColor
= hbmp
;
1668 return ::CreateIconIndirect(&iconInfo
);
1671 wxMask
* mask
= bmp
.GetMask();
1675 // we must have a mask for an icon, so even if it's probably incorrect,
1676 // do create it (grey is the "standard" transparent colour)
1677 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1681 wxZeroMemory(iconInfo
);
1682 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1685 iconInfo
.xHotspot
= hotSpotX
;
1686 iconInfo
.yHotspot
= hotSpotY
;
1689 AutoHBITMAP
hbmpMask(wxInvertMask((HBITMAP
)mask
->GetMaskBitmap()));
1690 iconInfo
.hbmMask
= hbmpMask
;
1691 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1693 // black out the transparent area to preserve background colour, because
1694 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1695 // the mask to the dest rect.
1697 MemoryHDC dcSrc
, dcDst
;
1698 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1699 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1701 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1702 dcSrc
, 0, 0, SRCAND
) )
1704 wxLogLastError(wxT("BitBlt"));
1708 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1710 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1712 // we created the mask, now delete it
1719 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1721 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1724 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1726 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1729 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1731 #ifndef __WXMICROWIN__
1732 wxCHECK_MSG( hbmpMask
, 0, wxT("invalid bitmap in wxInvertMask") );
1734 // get width/height from the bitmap if not given
1738 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1743 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1744 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1745 if ( !hdcSrc
|| !hdcDst
)
1747 wxLogLastError(wxT("CreateCompatibleDC"));
1750 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1753 wxLogLastError(wxT("CreateBitmap"));
1756 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1757 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1758 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1762 wxLogLastError(wxT("BitBlt"));
1766 SelectObject(hdcSrc
,srcTmp
);
1767 SelectObject(hdcDst
,dstTmp
);