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 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*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 // copy also the mask
281 wxMask
* const maskSrc
= data
->GetMask();
284 wxBitmapRefData
*selfdata
= wx_static_cast(wxBitmapRefData
*, m_refData
);
286 selfdata
->SetMask(new wxMask(*maskSrc
));
294 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
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
;
321 // If the icon is 32 bits per pixel then it may have alpha channel data,
322 // although there are some icons that are 32 bpp but have no alpha... So
323 // convert to a DIB and manually check the 4th byte for each pixel.
325 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
326 && bm
.bmBitsPixel
== 32)
328 wxDIB
dib(iconInfo
.hbmColor
);
331 unsigned char* pixels
= dib
.GetData();
332 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
334 if (pixels
[idx
+3] != 0)
336 // If there is an alpha byte that is non-zero then set the
337 // alpha flag and bail out of the loop.
338 refData
->m_hasAlpha
= true;
345 if ( !refData
->m_hasAlpha
)
347 // the mask returned by GetIconInfo() is inverted compared to the usual
349 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
352 // delete the old one now as we don't need it any more
353 ::DeleteObject(iconInfo
.hbmMask
);
364 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
371 return CopyFromIconOrCursor(cursor
);
374 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
381 return CopyFromIconOrCursor(icon
);
384 #ifndef NEVER_USE_DIB
386 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
388 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
390 #ifdef SOMETIMES_USE_DIB
391 HBITMAP hbitmap
= dib
.CreateDDB();
394 #else // ALWAYS_USE_DIB
395 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
396 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
400 wxBitmapRefData
*refData
= new wxBitmapRefData
;
403 refData
->m_width
= dib
.GetWidth();
404 refData
->m_height
= dib
.GetHeight();
405 refData
->m_depth
= dib
.GetDepth();
407 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
410 wxPalette
*palette
= dib
.CreatePalette();
413 refData
->m_bitmapPalette
= *palette
;
417 #endif // wxUSE_PALETTE
422 #endif // NEVER_USE_DIB
424 wxBitmap::~wxBitmap()
428 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
430 #ifndef __WXMICROWIN__
431 wxBitmapRefData
*refData
= new wxBitmapRefData
;
434 refData
->m_width
= width
;
435 refData
->m_height
= height
;
436 refData
->m_depth
= depth
;
441 // we assume that it is in XBM format which is not quite the same as
442 // the format CreateBitmap() wants because the order of bytes in the
444 const size_t bytesPerLine
= (width
+ 7) / 8;
445 const size_t padding
= bytesPerLine
% 2;
446 const size_t len
= height
* ( padding
+ bytesPerLine
);
447 data
= (char *)malloc(len
);
448 const char *src
= bits
;
451 for ( int rows
= 0; rows
< height
; rows
++ )
453 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
455 unsigned char val
= *src
++;
456 unsigned char reversed
= 0;
458 for ( int bits
= 0; bits
< 8; bits
++)
461 reversed
|= (unsigned char)(val
& 0x01);
473 // bits should already be in Windows standard format
474 data
= (char *)bits
; // const_cast is harmless
477 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
480 wxLogLastError(wxT("CreateBitmap"));
488 SetHBITMAP((WXHBITMAP
)hbmp
);
492 wxBitmap::wxBitmap(int w
, int h
, int d
)
494 (void)Create(w
, h
, d
);
497 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
499 (void)Create(w
, h
, dc
);
502 wxBitmap::wxBitmap(const void* data
, long type
, int width
, int height
, int depth
)
504 (void)Create(data
, type
, width
, height
, depth
);
507 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
509 LoadFile(filename
, (int)type
);
512 bool wxBitmap::Create(int width
, int height
, int depth
)
514 return DoCreate(width
, height
, depth
, 0);
517 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
519 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
521 return DoCreate(width
, height
, -1, dc
.GetHDC());
524 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
528 m_refData
= new wxBitmapRefData
;
530 GetBitmapData()->m_width
= w
;
531 GetBitmapData()->m_height
= h
;
533 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
535 #ifndef NEVER_USE_DIB
536 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
540 // create DIBs without alpha channel by default
548 // don't delete the DIB section in dib object dtor
551 GetBitmapData()->m_isDIB
= true;
552 GetBitmapData()->m_depth
= d
;
555 #endif // NEVER_USE_DIB
557 #ifndef ALWAYS_USE_DIB
558 #ifndef __WXMICROWIN__
561 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
564 wxLogLastError(wxT("CreateBitmap"));
567 GetBitmapData()->m_depth
= d
;
569 else // d == 0, create bitmap compatible with the screen
570 #endif // !__WXMICROWIN__
573 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
576 wxLogLastError(wxT("CreateCompatibleBitmap"));
579 GetBitmapData()->m_depth
= wxDisplayDepth();
581 #endif // !ALWAYS_USE_DIB
584 SetHBITMAP((WXHBITMAP
)hbmp
);
591 // ----------------------------------------------------------------------------
592 // wxImage to/from conversions for Microwin
593 // ----------------------------------------------------------------------------
595 // Microwin versions are so different from normal ones that it really doesn't
596 // make sense to use #ifdefs inside the function bodies
597 #ifdef __WXMICROWIN__
599 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
601 // Set this to 1 to experiment with mask code,
602 // which currently doesn't work
605 m_refData
= new wxBitmapRefData();
607 // Initial attempt at a simple-minded implementation.
608 // The bitmap will always be created at the screen depth,
609 // so the 'depth' argument is ignored.
611 HDC hScreenDC
= ::GetDC(NULL
);
612 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
614 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
615 HBITMAP hMaskBitmap
= NULL
;
616 HBITMAP hOldMaskBitmap
= NULL
;
618 unsigned char maskR
= 0;
619 unsigned char maskG
= 0;
620 unsigned char maskB
= 0;
622 // printf("Created bitmap %d\n", (int) hBitmap);
625 ::ReleaseDC(NULL
, hScreenDC
);
628 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
630 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
631 ::ReleaseDC(NULL
, hScreenDC
);
633 // created an mono-bitmap for the possible mask
634 bool hasMask
= image
.HasMask();
639 // FIXME: we should be able to pass bpp = 1, but
640 // GdBlit can't handle a different depth
642 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
644 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
646 maskR
= image
.GetMaskRed();
647 maskG
= image
.GetMaskGreen();
648 maskB
= image
.GetMaskBlue();
656 hScreenDC
= ::GetDC(NULL
);
657 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
658 ::ReleaseDC(NULL
, hScreenDC
);
660 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
668 for (i
= 0; i
< image
.GetWidth(); i
++)
670 for (j
= 0; j
< image
.GetHeight(); j
++)
672 unsigned char red
= image
.GetRed(i
, j
);
673 unsigned char green
= image
.GetGreen(i
, j
);
674 unsigned char blue
= image
.GetBlue(i
, j
);
676 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
680 // scan the bitmap for the transparent colour and set the corresponding
681 // pixels in the mask to BLACK and the rest to WHITE
682 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
683 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
685 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
690 ::SelectObject(hMemDC
, hOldBitmap
);
694 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
697 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
700 SetWidth(image
.GetWidth());
701 SetHeight(image
.GetHeight());
702 SetDepth(screenDepth
);
703 SetHBITMAP( (WXHBITMAP
) hBitmap
);
706 // Copy the palette from the source image
707 SetPalette(image
.GetPalette());
708 #endif // wxUSE_PALETTE
713 wxImage
wxBitmap::ConvertToImage() const
715 // Initial attempt at a simple-minded implementation.
716 // The bitmap will always be created at the screen depth,
717 // so the 'depth' argument is ignored.
718 // TODO: transparency (create a mask image)
722 wxFAIL_MSG( wxT("bitmap is invalid") );
728 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
730 // create an wxImage object
731 int width
= GetWidth();
732 int height
= GetHeight();
733 image
.Create( width
, height
);
734 unsigned char *data
= image
.GetData();
737 wxFAIL_MSG( wxT("could not allocate data for image") );
741 HDC hScreenDC
= ::GetDC(NULL
);
743 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
744 ::ReleaseDC(NULL
, hScreenDC
);
746 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
748 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
751 for (i
= 0; i
< GetWidth(); i
++)
753 for (j
= 0; j
< GetHeight(); j
++)
755 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
756 unsigned char red
= GetRValue(color
);
757 unsigned char green
= GetGValue(color
);
758 unsigned char blue
= GetBValue(color
);
760 image
.SetRGB(i
, j
, red
, green
, blue
);
764 ::SelectObject(hMemDC
, hOldBitmap
);
768 // Copy the palette from the source image
770 image
.SetPalette(* GetPalette());
771 #endif // wxUSE_PALETTE
776 #endif // __WXMICROWIN__
778 // ----------------------------------------------------------------------------
779 // wxImage to/from conversions
780 // ----------------------------------------------------------------------------
782 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
784 return CreateFromImage(image
, depth
, 0);
787 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
789 wxCHECK_MSG( dc
.Ok(), false,
790 _T("invalid HDC in wxBitmap::CreateFromImage()") );
792 return CreateFromImage(image
, -1, dc
.GetHDC());
797 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
799 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
803 // first convert the image to DIB
804 const int h
= image
.GetHeight();
805 const int w
= image
.GetWidth();
812 depth
= dib
.GetDepth(); // Get depth from image if none specified
814 // store the bitmap parameters
815 wxBitmapRefData
*refData
= new wxBitmapRefData
;
816 refData
->m_width
= w
;
817 refData
->m_height
= h
;
818 refData
->m_hasAlpha
= image
.HasAlpha();
823 // next either store DIB as is or create a DDB from it
824 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
826 // are we going to use DIB?
828 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
829 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
831 // don't delete the DIB section in dib object dtor
832 hbitmap
= dib
.Detach();
834 refData
->m_isDIB
= true;
835 refData
->m_depth
= depth
;
837 #ifndef ALWAYS_USE_DIB
838 else // we need to convert DIB to DDB
840 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
842 refData
->m_depth
= depth
;
844 #endif // !ALWAYS_USE_DIB
846 // validate this object
847 SetHBITMAP((WXHBITMAP
)hbitmap
);
849 // finally also set the mask if we have one
850 if ( image
.HasMask() )
852 const size_t len
= 2*((w
+15)/16);
853 BYTE
*src
= image
.GetData();
854 BYTE
*data
= new BYTE
[h
*len
];
855 memset(data
, 0, h
*len
);
856 BYTE r
= image
.GetMaskRed(),
857 g
= image
.GetMaskGreen(),
858 b
= image
.GetMaskBlue();
860 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
864 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
866 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
869 if ( (mask
>>= 1) == 0 )
877 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
880 wxLogLastError(_T("CreateBitmap(mask)"));
884 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
893 wxImage
wxBitmap::ConvertToImage() const
895 // convert DDB to DIB
903 // and then DIB to our wxImage
904 wxImage image
= dib
.ConvertToImage();
910 // now do the same for the mask, if we have any
911 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
914 wxDIB
dibMask(hbmpMask
);
915 if ( dibMask
.IsOk() )
917 // TODO: use wxRawBitmap to iterate over DIB
919 // we hard code the mask colour for now but we could also make an
920 // effort (and waste time) to choose a colour not present in the
921 // image already to avoid having to fudge the pixels below --
922 // whether it's worth to do it is unclear however
923 static const int MASK_RED
= 1;
924 static const int MASK_GREEN
= 2;
925 static const int MASK_BLUE
= 3;
926 static const int MASK_BLUE_REPLACEMENT
= 2;
928 const int h
= dibMask
.GetHeight();
929 const int w
= dibMask
.GetWidth();
930 const int bpp
= dibMask
.GetDepth();
931 const int maskBytesPerPixel
= bpp
>> 3;
932 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
933 unsigned char *data
= image
.GetData();
935 // remember that DIBs are stored in bottom to top order
937 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
939 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
941 // traverse one mask DIB line
942 unsigned char *mask
= maskLineStart
;
943 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
945 // should this pixel be transparent?
948 // no, check that it isn't transparent by accident
949 if ( (data
[0] == MASK_RED
) &&
950 (data
[1] == MASK_GREEN
) &&
951 (data
[2] == MASK_BLUE
) )
953 // we have to fudge the colour a bit to prevent
954 // this pixel from appearing transparent
955 data
[2] = MASK_BLUE_REPLACEMENT
;
960 else // yes, transparent pixel
963 *data
++ = MASK_GREEN
;
969 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
976 #else // !wxUSE_WXDIB
979 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
986 wxImage
wxBitmap::ConvertToImage() const
991 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
993 #endif // wxUSE_IMAGE
995 // ----------------------------------------------------------------------------
996 // loading and saving bitmaps
997 // ----------------------------------------------------------------------------
999 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1003 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1007 m_refData
= new wxBitmapRefData
;
1009 return handler
->LoadFile(this, filename
, type
, -1, -1);
1011 #if wxUSE_IMAGE && wxUSE_WXDIB
1012 else // no bitmap handler found
1015 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1017 *this = wxBitmap(image
);
1022 #endif // wxUSE_IMAGE
1027 bool wxBitmap::Create(const void* data
, long type
, int width
, int height
, int depth
)
1031 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1035 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1040 m_refData
= new wxBitmapRefData
;
1042 return handler
->Create(this, data
, type
, width
, height
, depth
);
1045 bool wxBitmap::SaveFile(const wxString
& filename
,
1047 const wxPalette
*palette
)
1049 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1053 return handler
->SaveFile(this, filename
, type
, palette
);
1055 #if wxUSE_IMAGE && wxUSE_WXDIB
1056 else // no bitmap handler found
1058 // FIXME what about palette? shouldn't we use it?
1059 wxImage image
= ConvertToImage();
1062 return image
.SaveFile(filename
, type
);
1065 #endif // wxUSE_IMAGE
1070 // ----------------------------------------------------------------------------
1071 // sub bitmap extraction
1072 // ----------------------------------------------------------------------------
1074 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1076 wxCHECK_MSG( Ok() &&
1077 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1078 (rect
.x
+rect
.width
<= GetWidth()) &&
1079 (rect
.y
+rect
.height
<= GetHeight()),
1080 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1082 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1083 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1085 #ifndef __WXMICROWIN__
1086 // handle alpha channel, if any
1095 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1096 selectDst(dcDst
, GetHbitmapOf(ret
));
1098 if ( !selectSrc
|| !selectDst
)
1100 wxLogLastError(_T("SelectObjct(hBitmap)"));
1103 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1104 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1106 wxLogLastError(_T("BitBlt"));
1110 // copy mask if there is one
1113 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1115 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1116 selectDst(dcDst
, hbmpMask
);
1118 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1119 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1121 wxLogLastError(_T("BitBlt"));
1124 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1127 #endif // !__WXMICROWIN__
1132 // ----------------------------------------------------------------------------
1133 // wxBitmap accessors
1134 // ----------------------------------------------------------------------------
1137 wxPalette
* wxBitmap::GetPalette() const
1139 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1140 : (wxPalette
*) NULL
;
1144 wxMask
*wxBitmap::GetMask() const
1146 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1149 wxBitmap
wxBitmap::GetMaskBitmap() const
1152 wxMask
*mask
= GetMask();
1154 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1160 wxDC
*wxBitmap::GetSelectedInto() const
1162 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1167 #if WXWIN_COMPATIBILITY_2_4
1169 int wxBitmap::GetQuality() const
1174 #endif // WXWIN_COMPATIBILITY_2_4
1176 void wxBitmap::UseAlpha()
1178 if ( GetBitmapData() )
1179 GetBitmapData()->m_hasAlpha
= true;
1182 bool wxBitmap::HasAlpha() const
1184 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1187 // ----------------------------------------------------------------------------
1189 // ----------------------------------------------------------------------------
1193 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1195 if ( GetBitmapData() )
1196 GetBitmapData()->m_selectedInto
= dc
;
1203 void wxBitmap::SetPalette(const wxPalette
& palette
)
1207 GetBitmapData()->m_bitmapPalette
= palette
;
1210 #endif // wxUSE_PALETTE
1212 void wxBitmap::SetMask(wxMask
*mask
)
1216 GetBitmapData()->SetMask(mask
);
1219 #if WXWIN_COMPATIBILITY_2_4
1221 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1225 #endif // WXWIN_COMPATIBILITY_2_4
1227 // ----------------------------------------------------------------------------
1228 // raw bitmap access support
1229 // ----------------------------------------------------------------------------
1231 #ifdef wxHAVE_RAW_BITMAP
1232 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1237 // no bitmap, no data (raw or otherwise)
1241 // if we're already a DIB we can access our data directly, but if not we
1242 // need to convert this DDB to a DIB section and use it for raw access and
1243 // then convert it back
1245 if ( !GetBitmapData()->m_isDIB
)
1247 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1248 _T("GetRawData() may be called only once") );
1250 wxDIB
*dib
= new wxDIB(*this);
1258 // we'll free it in UngetRawData()
1259 GetBitmapData()->m_dib
= dib
;
1261 hDIB
= dib
->GetHandle();
1265 hDIB
= GetHbitmap();
1269 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1271 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1276 // check that the bitmap is in correct format
1277 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1279 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1284 // ok, store the relevant info in wxPixelDataBase
1285 const LONG h
= ds
.dsBm
.bmHeight
;
1287 data
.m_width
= ds
.dsBm
.bmWidth
;
1290 // remember that DIBs are stored in top to bottom order!
1291 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1292 // multiple of 2, as required by the documentation. So we use the official
1293 // formula, which we already use elsewhere.)
1294 const LONG bytesPerRow
=
1295 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1296 data
.m_stride
= -bytesPerRow
;
1298 char *bits
= (char *)ds
.dsBm
.bmBits
;
1301 bits
+= (h
- 1)*bytesPerRow
;
1310 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1318 // invalid data, don't crash -- but don't assert neither as we're
1319 // called automatically from wxPixelDataBase dtor and so there is no
1320 // way to prevent this from happening
1324 // if we're a DDB we need to convert DIB back to DDB now to make the
1325 // changes made via raw bitmap access effective
1326 if ( !GetBitmapData()->m_isDIB
)
1328 wxDIB
*dib
= GetBitmapData()->m_dib
;
1329 GetBitmapData()->m_dib
= NULL
;
1335 #endif // wxUSE_WXDIB
1337 #endif // #ifdef wxHAVE_RAW_BITMAP
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1349 wxMask::wxMask(const wxMask
&mask
)
1353 HDC srcDC
= CreateCompatibleDC(0);
1354 HDC destDC
= CreateCompatibleDC(0);
1356 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1357 // so we'll use GetObject() API here:
1358 if (::GetObject((HGDIOBJ
)mask
.m_maskBitmap
, sizeof(bmp
), &bmp
) == 0)
1360 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1364 // create our HBITMAP
1365 int w
= bmp
.bmWidth
, h
= bmp
.bmHeight
;
1366 m_maskBitmap
= (WXHBITMAP
)CreateCompatibleBitmap(srcDC
, w
, h
);
1368 // copy the mask's HBITMAP into our HBITMAP
1369 SelectObject(srcDC
, (HBITMAP
) mask
.m_maskBitmap
);
1370 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1372 BitBlt(destDC
, 0, 0, w
, h
, srcDC
, 0, 0, SRCCOPY
);
1374 SelectObject(srcDC
, 0);
1376 SelectObject(destDC
, 0);
1380 // Construct a mask from a bitmap and a colour indicating
1381 // the transparent area
1382 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1385 Create(bitmap
, colour
);
1388 // Construct a mask from a bitmap and a palette index indicating
1389 // the transparent area
1390 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1393 Create(bitmap
, paletteIndex
);
1396 // Construct a mask from a mono bitmap (copies the bitmap).
1397 wxMask::wxMask(const wxBitmap
& bitmap
)
1406 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1409 // Create a mask from a mono bitmap (copies the bitmap).
1410 bool wxMask::Create(const wxBitmap
& bitmap
)
1412 #ifndef __WXMICROWIN__
1413 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1414 _T("can't create mask from invalid or not monochrome bitmap") );
1418 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1422 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1427 HDC srcDC
= CreateCompatibleDC(0);
1428 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1429 HDC destDC
= CreateCompatibleDC(0);
1430 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1431 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1432 SelectObject(srcDC
, 0);
1434 SelectObject(destDC
, 0);
1438 wxUnusedVar(bitmap
);
1443 // Create a mask from a bitmap and a palette index indicating
1444 // the transparent area
1445 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1449 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1454 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1456 unsigned char red
, green
, blue
;
1457 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1459 wxColour
transparentColour(red
, green
, blue
);
1460 return Create(bitmap
, transparentColour
);
1463 #endif // wxUSE_PALETTE
1468 // Create a mask from a bitmap and a colour indicating
1469 // the transparent area
1470 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1472 #ifndef __WXMICROWIN__
1473 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1477 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1481 int width
= bitmap
.GetWidth(),
1482 height
= bitmap
.GetHeight();
1484 // scan the bitmap for the transparent colour and set the corresponding
1485 // pixels in the mask to BLACK and the rest to WHITE
1486 COLORREF maskColour
= wxColourToPalRGB(colour
);
1487 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1489 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1490 HDC destDC
= ::CreateCompatibleDC(NULL
);
1491 if ( !srcDC
|| !destDC
)
1493 wxLogLastError(wxT("CreateCompatibleDC"));
1498 // SelectObject() will fail
1499 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1500 _T("bitmap can't be selected in another DC") );
1502 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1505 wxLogLastError(wxT("SelectObject"));
1510 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1513 wxLogLastError(wxT("SelectObject"));
1520 // this will create a monochrome bitmap with 0 points for the pixels
1521 // which have the same value as the background colour and 1 for the
1523 ::SetBkColor(srcDC
, maskColour
);
1524 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1527 ::SelectObject(srcDC
, hbmpSrcOld
);
1529 ::SelectObject(destDC
, hbmpDstOld
);
1533 #else // __WXMICROWIN__
1534 wxUnusedVar(bitmap
);
1535 wxUnusedVar(colour
);
1537 #endif // __WXMICROWIN__/!__WXMICROWIN__
1540 // ----------------------------------------------------------------------------
1542 // ----------------------------------------------------------------------------
1544 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1547 int width
, int height
, int depth
)
1549 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1551 return bitmap
&& Create(bitmap
, data
, flags
, width
, height
, depth
);
1554 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1555 const wxString
& name
,
1557 int width
, int height
)
1559 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1561 return bitmap
&& LoadFile(bitmap
, name
, flags
, width
, height
);
1564 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1565 const wxString
& name
,
1568 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1570 return bitmap
&& SaveFile(bitmap
, name
, type
);
1573 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1574 const void* WXUNUSED(data
),
1575 long WXUNUSED(type
),
1576 int WXUNUSED(width
),
1577 int WXUNUSED(height
),
1578 int WXUNUSED(depth
))
1583 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1584 const wxString
& WXUNUSED(name
),
1585 long WXUNUSED(type
),
1586 int WXUNUSED(desiredWidth
),
1587 int WXUNUSED(desiredHeight
))
1592 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1593 const wxString
& WXUNUSED(name
),
1595 const wxPalette
*WXUNUSED(palette
))
1600 // ----------------------------------------------------------------------------
1602 // ----------------------------------------------------------------------------
1604 #ifndef __WXMICROWIN__
1605 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1606 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1608 unsigned long i
, headerSize
;
1610 // Allocate space for a DIB header
1611 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1612 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1613 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1615 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1617 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1619 // Fill in the static parts of the DIB header
1620 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1621 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1622 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1623 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1625 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1626 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1627 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1628 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1629 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1632 // Initialize the DIB palette
1633 for (i
= 0; i
< 256; i
++) {
1634 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1635 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1636 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1637 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1640 *lpDIBHeader
= lpDIBheader
;
1645 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1651 // ----------------------------------------------------------------------------
1652 // global helper functions implemented here
1653 // ----------------------------------------------------------------------------
1655 // helper of wxBitmapToHICON/HCURSOR
1657 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1664 // we can't create an icon/cursor form nothing
1668 if ( bmp
.HasAlpha() )
1670 // Create an empty mask bitmap.
1671 // it doesn't seem to work if we mess with the mask at all.
1672 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1675 wxZeroMemory(iconInfo
);
1676 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1679 iconInfo
.xHotspot
= hotSpotX
;
1680 iconInfo
.yHotspot
= hotSpotY
;
1683 iconInfo
.hbmMask
= hMonoBitmap
;
1684 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1686 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1688 ::DeleteObject(hMonoBitmap
);
1693 wxMask
* mask
= bmp
.GetMask();
1697 // we must have a mask for an icon, so even if it's probably incorrect,
1698 // do create it (grey is the "standard" transparent colour)
1699 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1703 wxZeroMemory(iconInfo
);
1704 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1707 iconInfo
.xHotspot
= hotSpotX
;
1708 iconInfo
.yHotspot
= hotSpotY
;
1711 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1712 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1714 // black out the transparent area to preserve background colour, because
1715 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1716 // the mask to the dest rect.
1718 MemoryHDC dcSrc
, dcDst
;
1719 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1720 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1722 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1723 dcSrc
, 0, 0, SRCAND
) )
1725 wxLogLastError(_T("BitBlt"));
1729 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1731 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1733 // we created the mask, now delete it
1737 // delete the inverted mask bitmap we created as well
1738 ::DeleteObject(iconInfo
.hbmMask
);
1743 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1745 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1748 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1750 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1753 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1755 #ifndef __WXMICROWIN__
1756 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1758 // get width/height from the bitmap if not given
1762 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1767 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1768 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1769 if ( !hdcSrc
|| !hdcDst
)
1771 wxLogLastError(wxT("CreateCompatibleDC"));
1774 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1777 wxLogLastError(wxT("CreateBitmap"));
1780 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1781 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1782 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1786 wxLogLastError(wxT("BitBlt"));
1790 SelectObject(hdcSrc
,srcTmp
);
1791 SelectObject(hdcDst
,dstTmp
);