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"
45 #include "wx/msw/dib.h"
48 #ifdef wxHAVE_RAW_BITMAP
49 #include "wx/rawbmp.h"
52 // missing from mingw32 header
54 #define CLR_INVALID ((COLORREF)-1)
55 #endif // no CLR_INVALID
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
65 wxBitmapRefData(const wxBitmapRefData
& data
);
66 virtual ~wxBitmapRefData() { Free(); }
70 // set the mask object to use as the mask, we take ownership of it
71 void SetMask(wxMask
*mask
)
77 // set the HBITMAP to use as the mask
78 void SetMask(HBITMAP hbmpMask
)
80 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
84 wxMask
*GetMask() const { return m_bitmapMask
; }
88 wxPalette m_bitmapPalette
;
89 #endif // wxUSE_PALETTE
95 // this field is solely for error checking: we detect selecting a bitmap
96 // into more than one DC at once or deleting a bitmap still selected into a
97 // DC (both are serious programming errors under Windows)
102 // when GetRawData() is called for a DDB we need to convert it to a DIB
103 // first to be able to provide direct access to it and we cache that DIB
104 // here and convert it back to DDB when UngetRawData() is called
108 // true if we have alpha transparency info and can be drawn using
112 // true if our HBITMAP is a DIB section, false if it is a DDB
116 // optional mask for transparent drawing
117 wxMask
*m_bitmapMask
;
121 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
129 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 // decide whether we should create a DIB or a DDB for the given parameters
143 // NB: we always use DIBs under Windows CE as this is much simpler (even if
144 // also less efficient...) and we obviously can't use them if there is no
145 // DIB support compiled in at all
147 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
149 #define ALWAYS_USE_DIB
151 // no sense in defining wxShouldCreateDIB() as we can't compile code
152 // executed if it is true, so we have to use #if's anyhow
153 #define NEVER_USE_DIB
154 #else // wxUSE_WXDIB && !__WXWINCE__
155 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
157 // here is the logic:
159 // (a) if hdc is specified, the caller explicitly wants DDB
160 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
161 // or less DIBs anyhow)
162 // (c) finally, create DIBs under Win9x even if the depth hasn't been
163 // explicitly specified but the current display depth is 24 or
164 // more and the image is "big", i.e. > 16Mb which is the
165 // theoretical limit for DDBs under Win9x
167 // consequences (all of which seem to make sense):
169 // (i) by default, DDBs are created (depth == -1 usually)
170 // (ii) DIBs can be created by explicitly specifying the depth
171 // (iii) using a DC always forces creating a DDB
175 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
178 #define SOMETIMES_USE_DIB
179 #endif // different DIB usage scenarious
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 wxBitmapRefData::wxBitmapRefData()
188 m_selectedInto
= NULL
;
192 m_hBitmap
= (WXHBITMAP
) NULL
;
201 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
202 : wxGDIImageRefData(data
)
205 m_selectedInto
= NULL
;
208 // (deep) copy the mask if present
210 if (data
.m_bitmapMask
)
211 m_bitmapMask
= new wxMask(*data
.m_bitmapMask
);
213 // FIXME: we don't copy m_hBitmap currently but we should, see wxBitmap::
216 wxASSERT_MSG( !data
.m_isDIB
,
217 _T("can't copy bitmap locked for raw access!") );
220 m_hasAlpha
= data
.m_hasAlpha
;
223 void wxBitmapRefData::Free()
225 wxASSERT_MSG( !m_selectedInto
,
226 wxT("deleting bitmap still selected into wxMemoryDC") );
229 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
234 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
236 wxLogLastError(wxT("DeleteObject(hbitmap)"));
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 wxGDIImageRefData
*wxBitmap::CreateData() const
250 return new wxBitmapRefData
;
253 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*dataOrig
) const
255 const wxBitmapRefData
*
256 data
= wx_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
= wx_const_cast(wxBitmap
*, this);
267 // copy the other bitmap
268 if ( data
->m_hBitmap
)
270 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
271 self
->CopyFromDIB(dib
);
274 #endif // wxUSE_WXDIB
276 // copy the bitmap data
277 self
->m_refData
= new wxBitmapRefData(*data
);
280 wxBitmapRefData
* const
281 selfdata
= wx_static_cast(wxBitmapRefData
*, m_refData
);
283 // copy also the mask
284 wxMask
* const maskSrc
= data
->GetMask();
287 selfdata
->SetMask(new wxMask(*maskSrc
));
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( _T("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 const unsigned char* pixels
= dib
.GetData();
344 for (int idx
= 0; idx
< w
*h
*4; idx
+=4)
346 if (pixels
[idx
+3] != 0)
348 // If there is an alpha byte that is non-zero
349 // then set the alpha flag and stop checking
350 refData
->m_hasAlpha
= true;
358 #endif // wxUSE_WXDIB
360 case wxBitmapTransparency_Always
:
361 refData
->m_hasAlpha
= true;
365 if ( !refData
->m_hasAlpha
)
367 // the mask returned by GetIconInfo() is inverted compared to the usual
369 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
372 // delete the old one now as we don't need it any more
373 ::DeleteObject(iconInfo
.hbmMask
);
376 #else // __WXMICROWIN__ || __WXWINCE__
381 #endif // !__WXWINCE__/__WXWINCE__
384 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
, wxBitmapTransparency transp
)
391 return CopyFromIconOrCursor(cursor
, transp
);
394 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
, wxBitmapTransparency transp
)
401 return CopyFromIconOrCursor(icon
, transp
);
404 #ifndef NEVER_USE_DIB
406 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
408 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
410 #ifdef SOMETIMES_USE_DIB
411 HBITMAP hbitmap
= dib
.CreateDDB();
414 #else // ALWAYS_USE_DIB
415 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
416 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
420 wxBitmapRefData
*refData
= new wxBitmapRefData
;
423 refData
->m_width
= dib
.GetWidth();
424 refData
->m_height
= dib
.GetHeight();
425 refData
->m_depth
= dib
.GetDepth();
427 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
430 wxPalette
*palette
= dib
.CreatePalette();
433 refData
->m_bitmapPalette
= *palette
;
437 #endif // wxUSE_PALETTE
442 #endif // NEVER_USE_DIB
444 wxBitmap::~wxBitmap()
448 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
450 #ifndef __WXMICROWIN__
451 wxBitmapRefData
*refData
= new wxBitmapRefData
;
454 refData
->m_width
= width
;
455 refData
->m_height
= height
;
456 refData
->m_depth
= depth
;
461 // we assume that it is in XBM format which is not quite the same as
462 // the format CreateBitmap() wants because the order of bytes in the
464 const size_t bytesPerLine
= (width
+ 7) / 8;
465 const size_t padding
= bytesPerLine
% 2;
466 const size_t len
= height
* ( padding
+ bytesPerLine
);
467 data
= (char *)malloc(len
);
468 const char *src
= bits
;
471 for ( int rows
= 0; rows
< height
; rows
++ )
473 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
475 unsigned char val
= *src
++;
476 unsigned char reversed
= 0;
478 for ( int bits
= 0; bits
< 8; bits
++)
481 reversed
|= (unsigned char)(val
& 0x01);
493 // bits should already be in Windows standard format
494 data
= (char *)bits
; // const_cast is harmless
497 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
500 wxLogLastError(wxT("CreateBitmap"));
508 SetHBITMAP((WXHBITMAP
)hbmp
);
512 wxBitmap::wxBitmap(int w
, int h
, int d
)
514 (void)Create(w
, h
, d
);
517 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
519 (void)Create(w
, h
, dc
);
522 wxBitmap::wxBitmap(const void* data
, long type
, int width
, int height
, int depth
)
524 (void)Create(data
, type
, width
, height
, depth
);
527 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
529 LoadFile(filename
, (int)type
);
532 bool wxBitmap::Create(int width
, int height
, int depth
)
534 return DoCreate(width
, height
, depth
, 0);
537 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
539 wxCHECK_MSG( dc
.IsOk(), false, _T("invalid HDC in wxBitmap::Create()") );
541 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
544 return DoCreate(width
, height
, -1, impl
->GetHDC());
549 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
553 m_refData
= new wxBitmapRefData
;
555 GetBitmapData()->m_width
= w
;
556 GetBitmapData()->m_height
= h
;
558 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
560 #ifndef NEVER_USE_DIB
561 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
565 // create DIBs without alpha channel by default
573 // don't delete the DIB section in dib object dtor
576 GetBitmapData()->m_isDIB
= true;
577 GetBitmapData()->m_depth
= d
;
580 #endif // NEVER_USE_DIB
582 #ifndef ALWAYS_USE_DIB
583 #ifndef __WXMICROWIN__
586 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
589 wxLogLastError(wxT("CreateBitmap"));
592 GetBitmapData()->m_depth
= d
;
594 else // d == 0, create bitmap compatible with the screen
595 #endif // !__WXMICROWIN__
598 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
601 wxLogLastError(wxT("CreateCompatibleBitmap"));
604 GetBitmapData()->m_depth
= wxDisplayDepth();
606 #endif // !ALWAYS_USE_DIB
609 SetHBITMAP((WXHBITMAP
)hbmp
);
616 // ----------------------------------------------------------------------------
617 // wxImage to/from conversions for Microwin
618 // ----------------------------------------------------------------------------
620 // Microwin versions are so different from normal ones that it really doesn't
621 // make sense to use #ifdefs inside the function bodies
622 #ifdef __WXMICROWIN__
624 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
626 // Set this to 1 to experiment with mask code,
627 // which currently doesn't work
630 m_refData
= new wxBitmapRefData();
632 // Initial attempt at a simple-minded implementation.
633 // The bitmap will always be created at the screen depth,
634 // so the 'depth' argument is ignored.
636 HDC hScreenDC
= ::GetDC(NULL
);
637 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
639 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
640 HBITMAP hMaskBitmap
= NULL
;
641 HBITMAP hOldMaskBitmap
= NULL
;
643 unsigned char maskR
= 0;
644 unsigned char maskG
= 0;
645 unsigned char maskB
= 0;
647 // printf("Created bitmap %d\n", (int) hBitmap);
650 ::ReleaseDC(NULL
, hScreenDC
);
653 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
655 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
656 ::ReleaseDC(NULL
, hScreenDC
);
658 // created an mono-bitmap for the possible mask
659 bool hasMask
= image
.HasMask();
664 // FIXME: we should be able to pass bpp = 1, but
665 // GdBlit can't handle a different depth
667 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
669 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
671 maskR
= image
.GetMaskRed();
672 maskG
= image
.GetMaskGreen();
673 maskB
= image
.GetMaskBlue();
681 hScreenDC
= ::GetDC(NULL
);
682 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
683 ::ReleaseDC(NULL
, hScreenDC
);
685 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
693 for (i
= 0; i
< image
.GetWidth(); i
++)
695 for (j
= 0; j
< image
.GetHeight(); j
++)
697 unsigned char red
= image
.GetRed(i
, j
);
698 unsigned char green
= image
.GetGreen(i
, j
);
699 unsigned char blue
= image
.GetBlue(i
, j
);
701 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
705 // scan the bitmap for the transparent colour and set the corresponding
706 // pixels in the mask to BLACK and the rest to WHITE
707 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
708 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
710 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
715 ::SelectObject(hMemDC
, hOldBitmap
);
719 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
722 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
725 SetWidth(image
.GetWidth());
726 SetHeight(image
.GetHeight());
727 SetDepth(screenDepth
);
728 SetHBITMAP( (WXHBITMAP
) hBitmap
);
731 // Copy the palette from the source image
732 SetPalette(image
.GetPalette());
733 #endif // wxUSE_PALETTE
738 wxImage
wxBitmap::ConvertToImage() const
740 // Initial attempt at a simple-minded implementation.
741 // The bitmap will always be created at the screen depth,
742 // so the 'depth' argument is ignored.
743 // TODO: transparency (create a mask image)
747 wxFAIL_MSG( wxT("bitmap is invalid") );
753 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
755 // create an wxImage object
756 int width
= GetWidth();
757 int height
= GetHeight();
758 image
.Create( width
, height
);
759 unsigned char *data
= image
.GetData();
762 wxFAIL_MSG( wxT("could not allocate data for image") );
766 HDC hScreenDC
= ::GetDC(NULL
);
768 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
769 ::ReleaseDC(NULL
, hScreenDC
);
771 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
773 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
776 for (i
= 0; i
< GetWidth(); i
++)
778 for (j
= 0; j
< GetHeight(); j
++)
780 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
781 unsigned char red
= GetRValue(color
);
782 unsigned char green
= GetGValue(color
);
783 unsigned char blue
= GetBValue(color
);
785 image
.SetRGB(i
, j
, red
, green
, blue
);
789 ::SelectObject(hMemDC
, hOldBitmap
);
793 // Copy the palette from the source image
795 image
.SetPalette(* GetPalette());
796 #endif // wxUSE_PALETTE
801 #endif // __WXMICROWIN__
803 // ----------------------------------------------------------------------------
804 // wxImage to/from conversions
805 // ----------------------------------------------------------------------------
807 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
809 return CreateFromImage(image
, depth
, 0);
812 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
814 wxCHECK_MSG( dc
.IsOk(), false,
815 _T("invalid HDC in wxBitmap::CreateFromImage()") );
817 const wxMSWDCImpl
*impl
= wxDynamicCast( dc
.GetImpl(), wxMSWDCImpl
);
820 return CreateFromImage(image
, -1, impl
->GetHDC());
827 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
829 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
833 // first convert the image to DIB
834 const int h
= image
.GetHeight();
835 const int w
= image
.GetWidth();
842 depth
= dib
.GetDepth(); // Get depth from image if none specified
844 // store the bitmap parameters
845 wxBitmapRefData
*refData
= new wxBitmapRefData
;
846 refData
->m_width
= w
;
847 refData
->m_height
= h
;
848 refData
->m_hasAlpha
= image
.HasAlpha();
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 ( image
.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;
865 refData
->m_depth
= depth
;
867 #ifndef ALWAYS_USE_DIB
868 else // we need to convert DIB to DDB
870 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
872 refData
->m_depth
= depth
;
874 #endif // !ALWAYS_USE_DIB
876 // validate this object
877 SetHBITMAP((WXHBITMAP
)hbitmap
);
879 // finally also set the mask if we have one
880 if ( image
.HasMask() )
882 const size_t len
= 2*((w
+15)/16);
883 BYTE
*src
= image
.GetData();
884 BYTE
*data
= new BYTE
[h
*len
];
885 memset(data
, 0, h
*len
);
886 BYTE r
= image
.GetMaskRed(),
887 g
= image
.GetMaskGreen(),
888 b
= image
.GetMaskBlue();
890 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
894 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
896 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
899 if ( (mask
>>= 1) == 0 )
907 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
910 wxLogLastError(_T("CreateBitmap(mask)"));
914 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
923 wxImage
wxBitmap::ConvertToImage() const
925 // convert DDB to DIB
933 // and then DIB to our wxImage
934 wxImage image
= dib
.ConvertToImage();
940 // now do the same for the mask, if we have any
941 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
944 wxDIB
dibMask(hbmpMask
);
945 if ( dibMask
.IsOk() )
947 // TODO: use wxRawBitmap to iterate over DIB
949 // we hard code the mask colour for now but we could also make an
950 // effort (and waste time) to choose a colour not present in the
951 // image already to avoid having to fudge the pixels below --
952 // whether it's worth to do it is unclear however
953 static const int MASK_RED
= 1;
954 static const int MASK_GREEN
= 2;
955 static const int MASK_BLUE
= 3;
956 static const int MASK_BLUE_REPLACEMENT
= 2;
958 const int h
= dibMask
.GetHeight();
959 const int w
= dibMask
.GetWidth();
960 const int bpp
= dibMask
.GetDepth();
961 const int maskBytesPerPixel
= bpp
>> 3;
962 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
963 unsigned char *data
= image
.GetData();
965 // remember that DIBs are stored in bottom to top order
967 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
969 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
971 // traverse one mask DIB line
972 unsigned char *mask
= maskLineStart
;
973 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
975 // should this pixel be transparent?
978 // no, check that it isn't transparent by accident
979 if ( (data
[0] == MASK_RED
) &&
980 (data
[1] == MASK_GREEN
) &&
981 (data
[2] == MASK_BLUE
) )
983 // we have to fudge the colour a bit to prevent
984 // this pixel from appearing transparent
985 data
[2] = MASK_BLUE_REPLACEMENT
;
990 else // yes, transparent pixel
993 *data
++ = MASK_GREEN
;
999 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1006 #else // !wxUSE_WXDIB
1009 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
1010 int WXUNUSED(depth
),
1011 WXHDC
WXUNUSED(hdc
))
1016 wxImage
wxBitmap::ConvertToImage() const
1021 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1023 #endif // wxUSE_IMAGE
1025 // ----------------------------------------------------------------------------
1026 // loading and saving bitmaps
1027 // ----------------------------------------------------------------------------
1029 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1033 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1037 m_refData
= new wxBitmapRefData
;
1039 return handler
->LoadFile(this, filename
, type
, -1, -1);
1041 #if wxUSE_IMAGE && wxUSE_WXDIB
1042 else // no bitmap handler found
1045 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1047 *this = wxBitmap(image
);
1052 #endif // wxUSE_IMAGE
1057 bool wxBitmap::Create(const void* data
, long type
, int width
, int height
, int depth
)
1061 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1065 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1070 m_refData
= new wxBitmapRefData
;
1072 return handler
->Create(this, data
, type
, width
, height
, depth
);
1075 bool wxBitmap::SaveFile(const wxString
& filename
,
1077 const wxPalette
*palette
)
1079 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1083 return handler
->SaveFile(this, filename
, type
, palette
);
1085 #if wxUSE_IMAGE && wxUSE_WXDIB
1086 else // no bitmap handler found
1088 // FIXME what about palette? shouldn't we use it?
1089 wxImage image
= ConvertToImage();
1092 return image
.SaveFile(filename
, type
);
1095 #endif // wxUSE_IMAGE
1100 // ----------------------------------------------------------------------------
1101 // sub bitmap extraction
1102 // ----------------------------------------------------------------------------
1103 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1106 SelectInHDC
selectSrc(dcSrc
, GetHbitmap());
1107 return GetSubBitmapOfHDC( rect
, (WXHDC
)dcSrc
);
1110 wxBitmap
wxBitmap::GetSubBitmapOfHDC( const wxRect
& rect
, WXHDC hdc
) const
1112 wxCHECK_MSG( Ok() &&
1113 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1114 (rect
.x
+rect
.width
<= GetWidth()) &&
1115 (rect
.y
+rect
.height
<= GetHeight()),
1116 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1118 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1119 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1121 #ifndef __WXMICROWIN__
1122 // handle alpha channel, if any
1131 SelectInHDC
selectDst(dcDst
, GetHbitmapOf(ret
));
1135 wxLogLastError(_T("SelectObject(destBitmap)"));
1138 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1139 (HDC
)hdc
, rect
.x
, rect
.y
, SRCCOPY
) )
1141 wxLogLastError(_T("BitBlt"));
1145 // copy mask if there is one
1148 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1150 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1151 selectDst(dcDst
, hbmpMask
);
1153 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1154 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1156 wxLogLastError(_T("BitBlt"));
1159 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1162 #endif // !__WXMICROWIN__
1167 // ----------------------------------------------------------------------------
1168 // wxBitmap accessors
1169 // ----------------------------------------------------------------------------
1172 wxPalette
* wxBitmap::GetPalette() const
1174 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1175 : (wxPalette
*) NULL
;
1179 wxMask
*wxBitmap::GetMask() const
1181 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1184 wxBitmap
wxBitmap::GetMaskBitmap() const
1187 wxMask
*mask
= GetMask();
1189 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1195 wxDC
*wxBitmap::GetSelectedInto() const
1197 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1202 void wxBitmap::UseAlpha()
1204 if ( GetBitmapData() )
1205 GetBitmapData()->m_hasAlpha
= true;
1208 bool wxBitmap::HasAlpha() const
1210 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1213 // ----------------------------------------------------------------------------
1215 // ----------------------------------------------------------------------------
1219 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1221 if ( GetBitmapData() )
1222 GetBitmapData()->m_selectedInto
= dc
;
1229 void wxBitmap::SetPalette(const wxPalette
& palette
)
1233 GetBitmapData()->m_bitmapPalette
= palette
;
1236 #endif // wxUSE_PALETTE
1238 void wxBitmap::SetMask(wxMask
*mask
)
1242 GetBitmapData()->SetMask(mask
);
1245 // ----------------------------------------------------------------------------
1246 // raw bitmap access support
1247 // ----------------------------------------------------------------------------
1249 #ifdef wxHAVE_RAW_BITMAP
1250 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1255 // no bitmap, no data (raw or otherwise)
1259 // if we're already a DIB we can access our data directly, but if not we
1260 // need to convert this DDB to a DIB section and use it for raw access and
1261 // then convert it back
1263 if ( !GetBitmapData()->m_isDIB
)
1265 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1266 _T("GetRawData() may be called only once") );
1268 wxDIB
*dib
= new wxDIB(*this);
1276 // we'll free it in UngetRawData()
1277 GetBitmapData()->m_dib
= dib
;
1279 hDIB
= dib
->GetHandle();
1283 hDIB
= GetHbitmap();
1287 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1289 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1294 // check that the bitmap is in correct format
1295 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1297 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1302 // ok, store the relevant info in wxPixelDataBase
1303 const LONG h
= ds
.dsBm
.bmHeight
;
1305 data
.m_width
= ds
.dsBm
.bmWidth
;
1308 // remember that DIBs are stored in top to bottom order!
1309 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1310 // multiple of 2, as required by the documentation. So we use the official
1311 // formula, which we already use elsewhere.)
1312 const LONG bytesPerRow
=
1313 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1314 data
.m_stride
= -bytesPerRow
;
1316 char *bits
= (char *)ds
.dsBm
.bmBits
;
1319 bits
+= (h
- 1)*bytesPerRow
;
1328 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1336 // invalid data, don't crash -- but don't assert neither as we're
1337 // called automatically from wxPixelDataBase dtor and so there is no
1338 // way to prevent this from happening
1342 // if we're a DDB we need to convert DIB back to DDB now to make the
1343 // changes made via raw bitmap access effective
1344 if ( !GetBitmapData()->m_isDIB
)
1346 wxDIB
*dib
= GetBitmapData()->m_dib
;
1347 GetBitmapData()->m_dib
= NULL
;
1353 #endif // wxUSE_WXDIB
1355 #endif // #ifdef wxHAVE_RAW_BITMAP
1357 // ----------------------------------------------------------------------------
1359 // ----------------------------------------------------------------------------
1367 wxMask::wxMask(const wxMask
&mask
)
1372 HDC srcDC
= CreateCompatibleDC(0);
1373 HDC destDC
= CreateCompatibleDC(0);
1375 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1376 // so we'll use GetObject() API here:
1377 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1379 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1383 // create our HBITMAP
1384 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1385 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1387 // copy the mask's HBITMAP into our HBITMAP
1388 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1389 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1391 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1393 SelectObject(srcDC
, 0);
1395 SelectObject(destDC
, 0);
1399 // Construct a mask from a bitmap and a colour indicating
1400 // the transparent area
1401 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1404 Create(bitmap
, colour
);
1407 // Construct a mask from a bitmap and a palette index indicating
1408 // the transparent area
1409 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1412 Create(bitmap
, paletteIndex
);
1415 // Construct a mask from a mono bitmap (copies the bitmap).
1416 wxMask::wxMask(const wxBitmap
& bitmap
)
1425 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1428 // Create a mask from a mono bitmap (copies the bitmap).
1429 bool wxMask::Create(const wxBitmap
& bitmap
)
1431 #ifndef __WXMICROWIN__
1432 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1433 _T("can't create mask from invalid or not monochrome bitmap") );
1437 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1441 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1446 HDC srcDC
= CreateCompatibleDC(0);
1447 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1448 HDC destDC
= CreateCompatibleDC(0);
1449 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1450 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1451 SelectObject(srcDC
, 0);
1453 SelectObject(destDC
, 0);
1457 wxUnusedVar(bitmap
);
1462 // Create a mask from a bitmap and a palette index indicating
1463 // the transparent area
1464 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1468 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1473 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1475 unsigned char red
, green
, blue
;
1476 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1478 wxColour
transparentColour(red
, green
, blue
);
1479 return Create(bitmap
, transparentColour
);
1482 #endif // wxUSE_PALETTE
1487 // Create a mask from a bitmap and a colour indicating
1488 // the transparent area
1489 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1491 #ifndef __WXMICROWIN__
1492 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1496 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1500 int width
= bitmap
.GetWidth(),
1501 height
= bitmap
.GetHeight();
1503 // scan the bitmap for the transparent colour and set the corresponding
1504 // pixels in the mask to BLACK and the rest to WHITE
1505 COLORREF maskColour
= wxColourToPalRGB(colour
);
1506 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1508 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1509 HDC destDC
= ::CreateCompatibleDC(NULL
);
1510 if ( !srcDC
|| !destDC
)
1512 wxLogLastError(wxT("CreateCompatibleDC"));
1517 // SelectObject() will fail
1518 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1519 _T("bitmap can't be selected in another DC") );
1521 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1524 wxLogLastError(wxT("SelectObject"));
1529 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1532 wxLogLastError(wxT("SelectObject"));
1539 // this will create a monochrome bitmap with 0 points for the pixels
1540 // which have the same value as the background colour and 1 for the
1542 ::SetBkColor(srcDC
, maskColour
);
1543 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1546 ::SelectObject(srcDC
, hbmpSrcOld
);
1548 ::SelectObject(destDC
, hbmpDstOld
);
1552 #else // __WXMICROWIN__
1553 wxUnusedVar(bitmap
);
1554 wxUnusedVar(colour
);
1556 #endif // __WXMICROWIN__/!__WXMICROWIN__
1559 // ----------------------------------------------------------------------------
1561 // ----------------------------------------------------------------------------
1563 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1566 int width
, int height
, int depth
)
1568 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1570 return bitmap
&& Create(bitmap
, data
, flags
, width
, height
, depth
);
1573 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1574 const wxString
& name
,
1576 int width
, int height
)
1578 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1580 return bitmap
&& LoadFile(bitmap
, name
, flags
, width
, height
);
1583 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1584 const wxString
& name
,
1587 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1589 return bitmap
&& SaveFile(bitmap
, name
, type
);
1592 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1593 const void* WXUNUSED(data
),
1594 long WXUNUSED(type
),
1595 int WXUNUSED(width
),
1596 int WXUNUSED(height
),
1597 int WXUNUSED(depth
))
1602 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1603 const wxString
& WXUNUSED(name
),
1604 long WXUNUSED(type
),
1605 int WXUNUSED(desiredWidth
),
1606 int WXUNUSED(desiredHeight
))
1611 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1612 const wxString
& WXUNUSED(name
),
1614 const wxPalette
*WXUNUSED(palette
))
1619 // ----------------------------------------------------------------------------
1620 // global helper functions implemented here
1621 // ----------------------------------------------------------------------------
1623 // helper of wxBitmapToHICON/HCURSOR
1625 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1632 // we can't create an icon/cursor form nothing
1636 if ( bmp
.HasAlpha() )
1638 // Create an empty mask bitmap.
1639 // it doesn't seem to work if we mess with the mask at all.
1640 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1643 wxZeroMemory(iconInfo
);
1644 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1647 iconInfo
.xHotspot
= hotSpotX
;
1648 iconInfo
.yHotspot
= hotSpotY
;
1651 iconInfo
.hbmMask
= hMonoBitmap
;
1652 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1654 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1656 ::DeleteObject(hMonoBitmap
);
1661 wxMask
* mask
= bmp
.GetMask();
1665 // we must have a mask for an icon, so even if it's probably incorrect,
1666 // do create it (grey is the "standard" transparent colour)
1667 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1671 wxZeroMemory(iconInfo
);
1672 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1675 iconInfo
.xHotspot
= hotSpotX
;
1676 iconInfo
.yHotspot
= hotSpotY
;
1679 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1680 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1682 // black out the transparent area to preserve background colour, because
1683 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1684 // the mask to the dest rect.
1686 MemoryHDC dcSrc
, dcDst
;
1687 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1688 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1690 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1691 dcSrc
, 0, 0, SRCAND
) )
1693 wxLogLastError(_T("BitBlt"));
1697 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1699 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1701 // we created the mask, now delete it
1705 // delete the inverted mask bitmap we created as well
1706 ::DeleteObject(iconInfo
.hbmMask
);
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, _T("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
);