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 // can't copy the mask as the other bitmap destroys it
211 wxASSERT_MSG( !data
.m_isDIB
,
212 _T("can't copy bitmap locked for raw access!") );
215 m_hasAlpha
= data
.m_hasAlpha
;
218 void wxBitmapRefData::Free()
220 wxASSERT_MSG( !m_selectedInto
,
221 wxT("deleting bitmap still selected into wxMemoryDC") );
224 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
229 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
231 wxLogLastError(wxT("DeleteObject(hbitmap)"));
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 wxGDIImageRefData
*wxBitmap::CreateData() const
245 return new wxBitmapRefData
;
248 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
250 const wxBitmapRefData
*
251 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
255 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
258 // copy the other bitmap
259 if ( data
->m_hBitmap
)
261 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
262 self
->CopyFromDIB(dib
);
265 #endif // wxUSE_WXDIB
267 // don't copy the bitmap data, but do copy the size, depth, ...
268 self
->m_refData
= new wxBitmapRefData(*data
);
276 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
278 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
279 // it may be either HICON or HCURSOR
280 HICON hicon
= (HICON
)icon
.GetHandle();
283 if ( !::GetIconInfo(hicon
, &iconInfo
) )
285 wxLogLastError(wxT("GetIconInfo"));
290 wxBitmapRefData
*refData
= new wxBitmapRefData
;
293 int w
= icon
.GetWidth(),
294 h
= icon
.GetHeight();
296 refData
->m_width
= w
;
297 refData
->m_height
= h
;
298 refData
->m_depth
= wxDisplayDepth();
300 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
303 // If the icon is 32 bits per pixel then it may have alpha channel data,
304 // although there are some icons that are 32 bpp but have no alpha... So
305 // convert to a DIB and manually check the 4th byte for each pixel.
307 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
308 && bm
.bmBitsPixel
== 32)
310 wxDIB
dib(iconInfo
.hbmColor
);
313 unsigned char* pixels
= dib
.GetData();
314 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
316 if (pixels
[idx
+3] != 0)
318 // If there is an alpha byte that is non-zero then set the
319 // alpha flag and bail out of the loop.
320 refData
->m_hasAlpha
= true;
327 if ( !refData
->m_hasAlpha
)
329 // the mask returned by GetIconInfo() is inverted compared to the usual
331 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
334 // delete the old one now as we don't need it any more
335 ::DeleteObject(iconInfo
.hbmMask
);
346 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
353 return CopyFromIconOrCursor(cursor
);
356 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
363 return CopyFromIconOrCursor(icon
);
366 #ifndef NEVER_USE_DIB
368 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
370 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
372 #ifdef SOMETIMES_USE_DIB
373 HBITMAP hbitmap
= dib
.CreateDDB();
376 #else // ALWAYS_USE_DIB
377 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
378 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
382 wxBitmapRefData
*refData
= new wxBitmapRefData
;
385 refData
->m_width
= dib
.GetWidth();
386 refData
->m_height
= dib
.GetHeight();
387 refData
->m_depth
= dib
.GetDepth();
389 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
392 wxPalette
*palette
= dib
.CreatePalette();
395 refData
->m_bitmapPalette
= *palette
;
399 #endif // wxUSE_PALETTE
404 #endif // NEVER_USE_DIB
406 wxBitmap::~wxBitmap()
410 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
412 #ifndef __WXMICROWIN__
413 wxBitmapRefData
*refData
= new wxBitmapRefData
;
416 refData
->m_width
= width
;
417 refData
->m_height
= height
;
418 refData
->m_depth
= depth
;
423 // we assume that it is in XBM format which is not quite the same as
424 // the format CreateBitmap() wants because the order of bytes in the
426 const size_t bytesPerLine
= (width
+ 7) / 8;
427 const size_t padding
= bytesPerLine
% 2;
428 const size_t len
= height
* ( padding
+ bytesPerLine
);
429 data
= (char *)malloc(len
);
430 const char *src
= bits
;
433 for ( int rows
= 0; rows
< height
; rows
++ )
435 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
437 unsigned char val
= *src
++;
438 unsigned char reversed
= 0;
440 for ( int bits
= 0; bits
< 8; bits
++)
443 reversed
|= (unsigned char)(val
& 0x01);
455 // bits should already be in Windows standard format
456 data
= (char *)bits
; // const_cast is harmless
459 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
462 wxLogLastError(wxT("CreateBitmap"));
470 SetHBITMAP((WXHBITMAP
)hbmp
);
474 wxBitmap::wxBitmap(int w
, int h
, int d
)
476 (void)Create(w
, h
, d
);
479 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
481 (void)Create(w
, h
, dc
);
484 wxBitmap::wxBitmap(const void* data
, long type
, int width
, int height
, int depth
)
486 (void)Create(data
, type
, width
, height
, depth
);
489 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
491 LoadFile(filename
, (int)type
);
494 bool wxBitmap::Create(int width
, int height
, int depth
)
496 return DoCreate(width
, height
, depth
, 0);
499 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
501 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
503 return DoCreate(width
, height
, -1, dc
.GetHDC());
506 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
510 m_refData
= new wxBitmapRefData
;
512 GetBitmapData()->m_width
= w
;
513 GetBitmapData()->m_height
= h
;
515 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
517 #ifndef NEVER_USE_DIB
518 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
522 // create DIBs without alpha channel by default
530 // don't delete the DIB section in dib object dtor
533 GetBitmapData()->m_isDIB
= true;
534 GetBitmapData()->m_depth
= d
;
537 #endif // NEVER_USE_DIB
539 #ifndef ALWAYS_USE_DIB
540 #ifndef __WXMICROWIN__
543 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
546 wxLogLastError(wxT("CreateBitmap"));
549 GetBitmapData()->m_depth
= d
;
551 else // d == 0, create bitmap compatible with the screen
552 #endif // !__WXMICROWIN__
555 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
558 wxLogLastError(wxT("CreateCompatibleBitmap"));
561 GetBitmapData()->m_depth
= wxDisplayDepth();
563 #endif // !ALWAYS_USE_DIB
566 SetHBITMAP((WXHBITMAP
)hbmp
);
573 // ----------------------------------------------------------------------------
574 // wxImage to/from conversions for Microwin
575 // ----------------------------------------------------------------------------
577 // Microwin versions are so different from normal ones that it really doesn't
578 // make sense to use #ifdefs inside the function bodies
579 #ifdef __WXMICROWIN__
581 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
583 // Set this to 1 to experiment with mask code,
584 // which currently doesn't work
587 m_refData
= new wxBitmapRefData();
589 // Initial attempt at a simple-minded implementation.
590 // The bitmap will always be created at the screen depth,
591 // so the 'depth' argument is ignored.
593 HDC hScreenDC
= ::GetDC(NULL
);
594 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
596 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
597 HBITMAP hMaskBitmap
= NULL
;
598 HBITMAP hOldMaskBitmap
= NULL
;
600 unsigned char maskR
= 0;
601 unsigned char maskG
= 0;
602 unsigned char maskB
= 0;
604 // printf("Created bitmap %d\n", (int) hBitmap);
607 ::ReleaseDC(NULL
, hScreenDC
);
610 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
612 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
613 ::ReleaseDC(NULL
, hScreenDC
);
615 // created an mono-bitmap for the possible mask
616 bool hasMask
= image
.HasMask();
621 // FIXME: we should be able to pass bpp = 1, but
622 // GdBlit can't handle a different depth
624 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
626 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
628 maskR
= image
.GetMaskRed();
629 maskG
= image
.GetMaskGreen();
630 maskB
= image
.GetMaskBlue();
638 hScreenDC
= ::GetDC(NULL
);
639 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
640 ::ReleaseDC(NULL
, hScreenDC
);
642 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
650 for (i
= 0; i
< image
.GetWidth(); i
++)
652 for (j
= 0; j
< image
.GetHeight(); j
++)
654 unsigned char red
= image
.GetRed(i
, j
);
655 unsigned char green
= image
.GetGreen(i
, j
);
656 unsigned char blue
= image
.GetBlue(i
, j
);
658 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
662 // scan the bitmap for the transparent colour and set the corresponding
663 // pixels in the mask to BLACK and the rest to WHITE
664 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
665 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
667 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
672 ::SelectObject(hMemDC
, hOldBitmap
);
676 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
679 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
682 SetWidth(image
.GetWidth());
683 SetHeight(image
.GetHeight());
684 SetDepth(screenDepth
);
685 SetHBITMAP( (WXHBITMAP
) hBitmap
);
688 // Copy the palette from the source image
689 SetPalette(image
.GetPalette());
690 #endif // wxUSE_PALETTE
695 wxImage
wxBitmap::ConvertToImage() const
697 // Initial attempt at a simple-minded implementation.
698 // The bitmap will always be created at the screen depth,
699 // so the 'depth' argument is ignored.
700 // TODO: transparency (create a mask image)
704 wxFAIL_MSG( wxT("bitmap is invalid") );
710 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
712 // create an wxImage object
713 int width
= GetWidth();
714 int height
= GetHeight();
715 image
.Create( width
, height
);
716 unsigned char *data
= image
.GetData();
719 wxFAIL_MSG( wxT("could not allocate data for image") );
723 HDC hScreenDC
= ::GetDC(NULL
);
725 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
726 ::ReleaseDC(NULL
, hScreenDC
);
728 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
730 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
733 for (i
= 0; i
< GetWidth(); i
++)
735 for (j
= 0; j
< GetHeight(); j
++)
737 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
738 unsigned char red
= GetRValue(color
);
739 unsigned char green
= GetGValue(color
);
740 unsigned char blue
= GetBValue(color
);
742 image
.SetRGB(i
, j
, red
, green
, blue
);
746 ::SelectObject(hMemDC
, hOldBitmap
);
750 // Copy the palette from the source image
752 image
.SetPalette(* GetPalette());
753 #endif // wxUSE_PALETTE
758 #endif // __WXMICROWIN__
760 // ----------------------------------------------------------------------------
761 // wxImage to/from conversions
762 // ----------------------------------------------------------------------------
764 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
766 return CreateFromImage(image
, depth
, 0);
769 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
771 wxCHECK_MSG( dc
.Ok(), false,
772 _T("invalid HDC in wxBitmap::CreateFromImage()") );
774 return CreateFromImage(image
, -1, dc
.GetHDC());
779 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
781 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
785 // first convert the image to DIB
786 const int h
= image
.GetHeight();
787 const int w
= image
.GetWidth();
794 depth
= dib
.GetDepth(); // Get depth from image if none specified
796 // store the bitmap parameters
797 wxBitmapRefData
*refData
= new wxBitmapRefData
;
798 refData
->m_width
= w
;
799 refData
->m_height
= h
;
800 refData
->m_hasAlpha
= image
.HasAlpha();
805 // next either store DIB as is or create a DDB from it
806 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
808 // are we going to use DIB?
810 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
811 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
813 // don't delete the DIB section in dib object dtor
814 hbitmap
= dib
.Detach();
816 refData
->m_isDIB
= true;
817 refData
->m_depth
= depth
;
819 #ifndef ALWAYS_USE_DIB
820 else // we need to convert DIB to DDB
822 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
824 refData
->m_depth
= depth
;
826 #endif // !ALWAYS_USE_DIB
828 // validate this object
829 SetHBITMAP((WXHBITMAP
)hbitmap
);
831 // finally also set the mask if we have one
832 if ( image
.HasMask() )
834 const size_t len
= 2*((w
+15)/16);
835 BYTE
*src
= image
.GetData();
836 BYTE
*data
= new BYTE
[h
*len
];
837 memset(data
, 0, h
*len
);
838 BYTE r
= image
.GetMaskRed(),
839 g
= image
.GetMaskGreen(),
840 b
= image
.GetMaskBlue();
842 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
846 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
848 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
851 if ( (mask
>>= 1) == 0 )
859 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
862 wxLogLastError(_T("CreateBitmap(mask)"));
866 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
875 wxImage
wxBitmap::ConvertToImage() const
877 // convert DDB to DIB
885 // and then DIB to our wxImage
886 wxImage image
= dib
.ConvertToImage();
892 // now do the same for the mask, if we have any
893 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
896 wxDIB
dibMask(hbmpMask
);
897 if ( dibMask
.IsOk() )
899 // TODO: use wxRawBitmap to iterate over DIB
901 // we hard code the mask colour for now but we could also make an
902 // effort (and waste time) to choose a colour not present in the
903 // image already to avoid having to fudge the pixels below --
904 // whether it's worth to do it is unclear however
905 static const int MASK_RED
= 1;
906 static const int MASK_GREEN
= 2;
907 static const int MASK_BLUE
= 3;
908 static const int MASK_BLUE_REPLACEMENT
= 2;
910 const int h
= dibMask
.GetHeight();
911 const int w
= dibMask
.GetWidth();
912 const int bpp
= dibMask
.GetDepth();
913 const int maskBytesPerPixel
= bpp
>> 3;
914 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
915 unsigned char *data
= image
.GetData();
917 // remember that DIBs are stored in bottom to top order
919 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
921 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
923 // traverse one mask DIB line
924 unsigned char *mask
= maskLineStart
;
925 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
927 // should this pixel be transparent?
930 // no, check that it isn't transparent by accident
931 if ( (data
[0] == MASK_RED
) &&
932 (data
[1] == MASK_GREEN
) &&
933 (data
[2] == MASK_BLUE
) )
935 // we have to fudge the colour a bit to prevent
936 // this pixel from appearing transparent
937 data
[2] = MASK_BLUE_REPLACEMENT
;
942 else // yes, transparent pixel
945 *data
++ = MASK_GREEN
;
951 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
958 #else // !wxUSE_WXDIB
961 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
968 wxImage
wxBitmap::ConvertToImage() const
973 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
975 #endif // wxUSE_IMAGE
977 // ----------------------------------------------------------------------------
978 // loading and saving bitmaps
979 // ----------------------------------------------------------------------------
981 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
985 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
989 m_refData
= new wxBitmapRefData
;
991 return handler
->LoadFile(this, filename
, type
, -1, -1);
993 #if wxUSE_IMAGE && wxUSE_WXDIB
994 else // no bitmap handler found
997 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
999 *this = wxBitmap(image
);
1004 #endif // wxUSE_IMAGE
1009 bool wxBitmap::Create(const void* data
, long type
, int width
, int height
, int depth
)
1013 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1017 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1022 m_refData
= new wxBitmapRefData
;
1024 return handler
->Create(this, data
, type
, width
, height
, depth
);
1027 bool wxBitmap::SaveFile(const wxString
& filename
,
1029 const wxPalette
*palette
)
1031 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1035 return handler
->SaveFile(this, filename
, type
, palette
);
1037 #if wxUSE_IMAGE && wxUSE_WXDIB
1038 else // no bitmap handler found
1040 // FIXME what about palette? shouldn't we use it?
1041 wxImage image
= ConvertToImage();
1044 return image
.SaveFile(filename
, type
);
1047 #endif // wxUSE_IMAGE
1052 // ----------------------------------------------------------------------------
1053 // sub bitmap extraction
1054 // ----------------------------------------------------------------------------
1056 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1058 wxCHECK_MSG( Ok() &&
1059 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1060 (rect
.x
+rect
.width
<= GetWidth()) &&
1061 (rect
.y
+rect
.height
<= GetHeight()),
1062 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1064 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1065 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1067 #ifndef __WXMICROWIN__
1068 // handle alpha channel, if any
1077 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1078 selectDst(dcDst
, GetHbitmapOf(ret
));
1080 if ( !selectSrc
|| !selectDst
)
1082 wxLogLastError(_T("SelectObjct(hBitmap)"));
1085 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1086 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1088 wxLogLastError(_T("BitBlt"));
1092 // copy mask if there is one
1095 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1097 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1098 selectDst(dcDst
, hbmpMask
);
1100 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1101 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1103 wxLogLastError(_T("BitBlt"));
1106 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1109 #endif // !__WXMICROWIN__
1114 // ----------------------------------------------------------------------------
1115 // wxBitmap accessors
1116 // ----------------------------------------------------------------------------
1119 wxPalette
* wxBitmap::GetPalette() const
1121 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1122 : (wxPalette
*) NULL
;
1126 wxMask
*wxBitmap::GetMask() const
1128 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1131 wxBitmap
wxBitmap::GetMaskBitmap() const
1134 wxMask
*mask
= GetMask();
1136 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1142 wxDC
*wxBitmap::GetSelectedInto() const
1144 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1149 #if WXWIN_COMPATIBILITY_2_4
1151 int wxBitmap::GetQuality() const
1156 #endif // WXWIN_COMPATIBILITY_2_4
1158 void wxBitmap::UseAlpha()
1160 if ( GetBitmapData() )
1161 GetBitmapData()->m_hasAlpha
= true;
1164 bool wxBitmap::HasAlpha() const
1166 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1169 // ----------------------------------------------------------------------------
1171 // ----------------------------------------------------------------------------
1175 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1177 if ( GetBitmapData() )
1178 GetBitmapData()->m_selectedInto
= dc
;
1185 void wxBitmap::SetPalette(const wxPalette
& palette
)
1189 GetBitmapData()->m_bitmapPalette
= palette
;
1192 #endif // wxUSE_PALETTE
1194 void wxBitmap::SetMask(wxMask
*mask
)
1198 GetBitmapData()->SetMask(mask
);
1201 #if WXWIN_COMPATIBILITY_2_4
1203 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1207 #endif // WXWIN_COMPATIBILITY_2_4
1209 // ----------------------------------------------------------------------------
1210 // raw bitmap access support
1211 // ----------------------------------------------------------------------------
1213 #ifdef wxHAVE_RAW_BITMAP
1214 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1219 // no bitmap, no data (raw or otherwise)
1223 // if we're already a DIB we can access our data directly, but if not we
1224 // need to convert this DDB to a DIB section and use it for raw access and
1225 // then convert it back
1227 if ( !GetBitmapData()->m_isDIB
)
1229 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1230 _T("GetRawData() may be called only once") );
1232 wxDIB
*dib
= new wxDIB(*this);
1240 // we'll free it in UngetRawData()
1241 GetBitmapData()->m_dib
= dib
;
1243 hDIB
= dib
->GetHandle();
1247 hDIB
= GetHbitmap();
1251 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1253 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1258 // check that the bitmap is in correct format
1259 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1261 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1266 // ok, store the relevant info in wxPixelDataBase
1267 const LONG h
= ds
.dsBm
.bmHeight
;
1269 data
.m_width
= ds
.dsBm
.bmWidth
;
1272 // remember that DIBs are stored in top to bottom order!
1273 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1274 // multiple of 2, as required by the documentation. So we use the official
1275 // formula, which we already use elsewhere.)
1276 const LONG bytesPerRow
=
1277 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1278 data
.m_stride
= -bytesPerRow
;
1280 char *bits
= (char *)ds
.dsBm
.bmBits
;
1283 bits
+= (h
- 1)*bytesPerRow
;
1292 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1300 // invalid data, don't crash -- but don't assert neither as we're
1301 // called automatically from wxPixelDataBase dtor and so there is no
1302 // way to prevent this from happening
1306 // if we're a DDB we need to convert DIB back to DDB now to make the
1307 // changes made via raw bitmap access effective
1308 if ( !GetBitmapData()->m_isDIB
)
1310 wxDIB
*dib
= GetBitmapData()->m_dib
;
1311 GetBitmapData()->m_dib
= NULL
;
1317 #endif // wxUSE_WXDIB
1319 #endif // #ifdef wxHAVE_RAW_BITMAP
1321 // ----------------------------------------------------------------------------
1323 // ----------------------------------------------------------------------------
1330 // Construct a mask from a bitmap and a colour indicating
1331 // the transparent area
1332 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1335 Create(bitmap
, colour
);
1338 // Construct a mask from a bitmap and a palette index indicating
1339 // the transparent area
1340 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1343 Create(bitmap
, paletteIndex
);
1346 // Construct a mask from a mono bitmap (copies the bitmap).
1347 wxMask::wxMask(const wxBitmap
& bitmap
)
1356 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1359 // Create a mask from a mono bitmap (copies the bitmap).
1360 bool wxMask::Create(const wxBitmap
& bitmap
)
1362 #ifndef __WXMICROWIN__
1363 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1364 _T("can't create mask from invalid or not monochrome bitmap") );
1368 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1372 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1377 HDC srcDC
= CreateCompatibleDC(0);
1378 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1379 HDC destDC
= CreateCompatibleDC(0);
1380 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1381 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1382 SelectObject(srcDC
, 0);
1384 SelectObject(destDC
, 0);
1388 wxUnusedVar(bitmap
);
1393 // Create a mask from a bitmap and a palette index indicating
1394 // the transparent area
1395 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1399 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1404 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1406 unsigned char red
, green
, blue
;
1407 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1409 wxColour
transparentColour(red
, green
, blue
);
1410 return Create(bitmap
, transparentColour
);
1413 #endif // wxUSE_PALETTE
1418 // Create a mask from a bitmap and a colour indicating
1419 // the transparent area
1420 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1422 #ifndef __WXMICROWIN__
1423 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1427 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1431 int width
= bitmap
.GetWidth(),
1432 height
= bitmap
.GetHeight();
1434 // scan the bitmap for the transparent colour and set the corresponding
1435 // pixels in the mask to BLACK and the rest to WHITE
1436 COLORREF maskColour
= wxColourToPalRGB(colour
);
1437 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1439 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1440 HDC destDC
= ::CreateCompatibleDC(NULL
);
1441 if ( !srcDC
|| !destDC
)
1443 wxLogLastError(wxT("CreateCompatibleDC"));
1448 // SelectObject() will fail
1449 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1450 _T("bitmap can't be selected in another DC") );
1452 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1455 wxLogLastError(wxT("SelectObject"));
1460 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1463 wxLogLastError(wxT("SelectObject"));
1470 // this will create a monochrome bitmap with 0 points for the pixels
1471 // which have the same value as the background colour and 1 for the
1473 ::SetBkColor(srcDC
, maskColour
);
1474 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1477 ::SelectObject(srcDC
, hbmpSrcOld
);
1479 ::SelectObject(destDC
, hbmpDstOld
);
1483 #else // __WXMICROWIN__
1484 wxUnusedVar(bitmap
);
1485 wxUnusedVar(colour
);
1487 #endif // __WXMICROWIN__/!__WXMICROWIN__
1490 // ----------------------------------------------------------------------------
1492 // ----------------------------------------------------------------------------
1494 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1497 int width
, int height
, int depth
)
1499 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1501 return bitmap
&& Create(bitmap
, data
, flags
, width
, height
, depth
);
1504 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1505 const wxString
& name
,
1507 int width
, int height
)
1509 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1511 return bitmap
&& LoadFile(bitmap
, name
, flags
, width
, height
);
1514 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1515 const wxString
& name
,
1518 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1520 return bitmap
&& SaveFile(bitmap
, name
, type
);
1523 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1524 const void* WXUNUSED(data
),
1525 long WXUNUSED(type
),
1526 int WXUNUSED(width
),
1527 int WXUNUSED(height
),
1528 int WXUNUSED(depth
))
1533 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1534 const wxString
& WXUNUSED(name
),
1535 long WXUNUSED(type
),
1536 int WXUNUSED(desiredWidth
),
1537 int WXUNUSED(desiredHeight
))
1542 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1543 const wxString
& WXUNUSED(name
),
1545 const wxPalette
*WXUNUSED(palette
))
1550 // ----------------------------------------------------------------------------
1552 // ----------------------------------------------------------------------------
1554 #ifndef __WXMICROWIN__
1555 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1556 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1558 unsigned long i
, headerSize
;
1560 // Allocate space for a DIB header
1561 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1562 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1563 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1565 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1567 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1569 // Fill in the static parts of the DIB header
1570 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1571 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1572 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1573 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1575 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1576 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1577 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1578 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1579 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1582 // Initialize the DIB palette
1583 for (i
= 0; i
< 256; i
++) {
1584 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1585 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1586 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1587 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1590 *lpDIBHeader
= lpDIBheader
;
1595 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1601 // ----------------------------------------------------------------------------
1602 // global helper functions implemented here
1603 // ----------------------------------------------------------------------------
1605 // helper of wxBitmapToHICON/HCURSOR
1607 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1614 // we can't create an icon/cursor form nothing
1618 if ( bmp
.HasAlpha() )
1620 // Create an empty mask bitmap.
1621 // it doesn't seem to work if we mess with the mask at all.
1622 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1625 wxZeroMemory(iconInfo
);
1626 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1629 iconInfo
.xHotspot
= hotSpotX
;
1630 iconInfo
.yHotspot
= hotSpotY
;
1633 iconInfo
.hbmMask
= hMonoBitmap
;
1634 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1636 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1638 ::DeleteObject(hMonoBitmap
);
1643 wxMask
* mask
= bmp
.GetMask();
1647 // we must have a mask for an icon, so even if it's probably incorrect,
1648 // do create it (grey is the "standard" transparent colour)
1649 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1653 wxZeroMemory(iconInfo
);
1654 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1657 iconInfo
.xHotspot
= hotSpotX
;
1658 iconInfo
.yHotspot
= hotSpotY
;
1661 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1662 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1664 // black out the transparent area to preserve background colour, because
1665 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1666 // the mask to the dest rect.
1668 MemoryHDC dcSrc
, dcDst
;
1669 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1670 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1672 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1673 dcSrc
, 0, 0, SRCAND
) )
1675 wxLogLastError(_T("BitBlt"));
1679 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1681 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1683 // we created the mask, now delete it
1687 // delete the inverted mask bitmap we created as well
1688 ::DeleteObject(iconInfo
.hbmMask
);
1693 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1695 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1698 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1700 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1703 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1705 #ifndef __WXMICROWIN__
1706 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1708 // get width/height from the bitmap if not given
1712 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1717 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1718 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1719 if ( !hdcSrc
|| !hdcDst
)
1721 wxLogLastError(wxT("CreateCompatibleDC"));
1724 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1727 wxLogLastError(wxT("CreateBitmap"));
1730 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1731 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1732 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1736 wxLogLastError(wxT("BitBlt"));
1740 SelectObject(hdcSrc
,srcTmp
);
1741 SelectObject(hdcDst
,dstTmp
);