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"
33 #include "wx/palette.h"
34 #include "wx/dcmemory.h"
35 #include "wx/bitmap.h"
40 #include "wx/msw/private.h"
43 #include "wx/msw/dib.h"
47 #include "wx/xpmdecod.h"
49 #ifdef wxHAVE_RAW_BITMAP
50 #include "wx/rawbmp.h"
53 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
66 wxBitmapRefData(const wxBitmapRefData
& data
);
67 virtual ~wxBitmapRefData() { Free(); }
71 // set the mask object to use as the mask, we take ownership of it
72 void SetMask(wxMask
*mask
)
78 // set the HBITMAP to use as the mask
79 void SetMask(HBITMAP hbmpMask
)
81 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
85 wxMask
*GetMask() const { return m_bitmapMask
; }
89 wxPalette m_bitmapPalette
;
90 #endif // wxUSE_PALETTE
96 // this field is solely for error checking: we detect selecting a bitmap
97 // into more than one DC at once or deleting a bitmap still selected into a
98 // DC (both are serious programming errors under Windows)
100 #endif // __WXDEBUG__
103 // when GetRawData() is called for a DDB we need to convert it to a DIB
104 // first to be able to provide direct access to it and we cache that DIB
105 // here and convert it back to DDB when UngetRawData() is called
109 // true if we have alpha transparency info and can be drawn using
113 // true if our HBITMAP is a DIB section, false if it is a DDB
117 // optional mask for transparent drawing
118 wxMask
*m_bitmapMask
;
122 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
130 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
132 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 // decide whether we should create a DIB or a DDB for the given parameters
144 // NB: we always use DIBs under Windows CE as this is much simpler (even if
145 // also less efficient...) and we obviously can't use them if there is no
146 // DIB support compiled in at all
148 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
150 #define ALWAYS_USE_DIB
152 // no sense in defining wxShouldCreateDIB() as we can't compile code
153 // executed if it is true, so we have to use #if's anyhow
154 #define NEVER_USE_DIB
155 #else // wxUSE_WXDIB && !__WXWINCE__
156 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
158 // here is the logic:
160 // (a) if hdc is specified, the caller explicitly wants DDB
161 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
162 // or less DIBs anyhow)
163 // (c) finally, create DIBs under Win9x even if the depth hasn't been
164 // explicitly specified but the current display depth is 24 or
165 // more and the image is "big", i.e. > 16Mb which is the
166 // theoretical limit for DDBs under Win9x
168 // consequences (all of which seem to make sense):
170 // (i) by default, DDBs are created (depth == -1 usually)
171 // (ii) DIBs can be created by explicitly specifying the depth
172 // (iii) using a DC always forces creating a DDB
176 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
179 #define SOMETIMES_USE_DIB
180 #endif // different DIB usage scenarious
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxBitmapRefData::wxBitmapRefData()
189 m_selectedInto
= NULL
;
193 m_hBitmap
= (WXHBITMAP
) NULL
;
202 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
203 : wxGDIImageRefData(data
)
206 m_selectedInto
= NULL
;
209 // can't copy the mask as the other bitmap destroys it
212 wxASSERT_MSG( !data
.m_isDIB
,
213 _T("can't copy bitmap locked for raw access!") );
216 m_hasAlpha
= data
.m_hasAlpha
;
219 void wxBitmapRefData::Free()
221 wxASSERT_MSG( !m_selectedInto
,
222 wxT("deleting bitmap still selected into wxMemoryDC") );
225 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
230 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
232 wxLogLastError(wxT("DeleteObject(hbitmap)"));
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 wxGDIImageRefData
*wxBitmap::CreateData() const
246 return new wxBitmapRefData
;
249 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
251 const wxBitmapRefData
*
252 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
256 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
259 // copy the other bitmap
260 if ( data
->m_hBitmap
)
262 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
263 self
->CopyFromDIB(dib
);
266 #endif // wxUSE_WXDIB
268 // don't copy the bitmap data, but do copy the size, depth, ...
269 self
->m_refData
= new wxBitmapRefData(*data
);
277 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
279 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
280 // it may be either HICON or HCURSOR
281 HICON hicon
= (HICON
)icon
.GetHandle();
284 if ( !::GetIconInfo(hicon
, &iconInfo
) )
286 wxLogLastError(wxT("GetIconInfo"));
291 wxBitmapRefData
*refData
= new wxBitmapRefData
;
294 int w
= icon
.GetWidth(),
295 h
= icon
.GetHeight();
297 refData
->m_width
= w
;
298 refData
->m_height
= h
;
299 refData
->m_depth
= wxDisplayDepth();
301 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
304 // If the icon is 32 bits per pixel then it may have alpha channel data,
305 // although there are some icons that are 32 bpp but have no alpha... So
306 // convert to a DIB and manually check the 4th byte for each pixel.
308 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
309 && bm
.bmBitsPixel
== 32)
311 wxDIB
dib(iconInfo
.hbmColor
);
314 unsigned char* pixels
= dib
.GetData();
315 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
317 if (pixels
[idx
+3] != 0)
319 // If there is an alpha byte that is non-zero then set the
320 // alpha flag and bail out of the loop.
321 refData
->m_hasAlpha
= true;
328 if ( !refData
->m_hasAlpha
)
330 // the mask returned by GetIconInfo() is inverted compared to the usual
332 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
335 // delete the old one now as we don't need it any more
336 ::DeleteObject(iconInfo
.hbmMask
);
347 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
354 return CopyFromIconOrCursor(cursor
);
357 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
364 return CopyFromIconOrCursor(icon
);
367 #ifndef NEVER_USE_DIB
369 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
371 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
373 #ifdef SOMETIMES_USE_DIB
374 HBITMAP hbitmap
= dib
.CreateDDB();
377 #else // ALWAYS_USE_DIB
378 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
379 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
383 wxBitmapRefData
*refData
= new wxBitmapRefData
;
386 refData
->m_width
= dib
.GetWidth();
387 refData
->m_height
= dib
.GetHeight();
388 refData
->m_depth
= dib
.GetDepth();
390 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
393 wxPalette
*palette
= dib
.CreatePalette();
396 refData
->m_bitmapPalette
= *palette
;
400 #endif // wxUSE_PALETTE
405 #endif // NEVER_USE_DIB
407 wxBitmap::~wxBitmap()
411 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
413 #ifndef __WXMICROWIN__
414 wxBitmapRefData
*refData
= new wxBitmapRefData
;
417 refData
->m_width
= width
;
418 refData
->m_height
= height
;
419 refData
->m_depth
= depth
;
424 // we assume that it is in XBM format which is not quite the same as
425 // the format CreateBitmap() wants because the order of bytes in the
427 const size_t bytesPerLine
= (width
+ 7) / 8;
428 const size_t padding
= bytesPerLine
% 2;
429 const size_t len
= height
* ( padding
+ bytesPerLine
);
430 data
= (char *)malloc(len
);
431 const char *src
= bits
;
434 for ( int rows
= 0; rows
< height
; rows
++ )
436 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
438 unsigned char val
= *src
++;
439 unsigned char reversed
= 0;
441 for ( int bits
= 0; bits
< 8; bits
++)
444 reversed
|= (unsigned char)(val
& 0x01);
456 // bits should already be in Windows standard format
457 data
= (char *)bits
; // const_cast is harmless
460 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
463 wxLogLastError(wxT("CreateBitmap"));
471 SetHBITMAP((WXHBITMAP
)hbmp
);
475 // Create from XPM data
476 bool wxBitmap::CreateFromXpm(const char **data
)
478 #if wxUSE_IMAGE && wxUSE_XPM && wxUSE_WXDIB
479 wxCHECK_MSG( data
!= NULL
, false, wxT("invalid bitmap data") );
481 wxXPMDecoder decoder
;
482 wxImage img
= decoder
.ReadData(data
);
483 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") );
485 *this = wxBitmap(img
);
493 wxBitmap::wxBitmap(int w
, int h
, int d
)
495 (void)Create(w
, h
, d
);
498 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
500 (void)Create(w
, h
, dc
);
503 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
505 (void)Create(data
, type
, width
, height
, depth
);
508 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
510 LoadFile(filename
, (int)type
);
513 bool wxBitmap::Create(int width
, int height
, int depth
)
515 return DoCreate(width
, height
, depth
, 0);
518 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
520 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
522 return DoCreate(width
, height
, -1, dc
.GetHDC());
525 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
529 m_refData
= new wxBitmapRefData
;
531 GetBitmapData()->m_width
= w
;
532 GetBitmapData()->m_height
= h
;
534 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
536 #ifndef NEVER_USE_DIB
537 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
541 // create DIBs without alpha channel by default
549 // don't delete the DIB section in dib object dtor
552 GetBitmapData()->m_isDIB
= true;
553 GetBitmapData()->m_depth
= d
;
556 #endif // NEVER_USE_DIB
558 #ifndef ALWAYS_USE_DIB
559 #ifndef __WXMICROWIN__
562 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
565 wxLogLastError(wxT("CreateBitmap"));
568 GetBitmapData()->m_depth
= d
;
570 else // d == 0, create bitmap compatible with the screen
571 #endif // !__WXMICROWIN__
574 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
577 wxLogLastError(wxT("CreateCompatibleBitmap"));
580 GetBitmapData()->m_depth
= wxDisplayDepth();
582 #endif // !ALWAYS_USE_DIB
585 SetHBITMAP((WXHBITMAP
)hbmp
);
592 // ----------------------------------------------------------------------------
593 // wxImage to/from conversions for Microwin
594 // ----------------------------------------------------------------------------
596 // Microwin versions are so different from normal ones that it really doesn't
597 // make sense to use #ifdefs inside the function bodies
598 #ifdef __WXMICROWIN__
600 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
602 // Set this to 1 to experiment with mask code,
603 // which currently doesn't work
606 m_refData
= new wxBitmapRefData();
608 // Initial attempt at a simple-minded implementation.
609 // The bitmap will always be created at the screen depth,
610 // so the 'depth' argument is ignored.
612 HDC hScreenDC
= ::GetDC(NULL
);
613 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
615 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
616 HBITMAP hMaskBitmap
= NULL
;
617 HBITMAP hOldMaskBitmap
= NULL
;
619 unsigned char maskR
= 0;
620 unsigned char maskG
= 0;
621 unsigned char maskB
= 0;
623 // printf("Created bitmap %d\n", (int) hBitmap);
626 ::ReleaseDC(NULL
, hScreenDC
);
629 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
631 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
632 ::ReleaseDC(NULL
, hScreenDC
);
634 // created an mono-bitmap for the possible mask
635 bool hasMask
= image
.HasMask();
640 // FIXME: we should be able to pass bpp = 1, but
641 // GdBlit can't handle a different depth
643 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
645 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
647 maskR
= image
.GetMaskRed();
648 maskG
= image
.GetMaskGreen();
649 maskB
= image
.GetMaskBlue();
657 hScreenDC
= ::GetDC(NULL
);
658 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
659 ::ReleaseDC(NULL
, hScreenDC
);
661 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
669 for (i
= 0; i
< image
.GetWidth(); i
++)
671 for (j
= 0; j
< image
.GetHeight(); j
++)
673 unsigned char red
= image
.GetRed(i
, j
);
674 unsigned char green
= image
.GetGreen(i
, j
);
675 unsigned char blue
= image
.GetBlue(i
, j
);
677 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
681 // scan the bitmap for the transparent colour and set the corresponding
682 // pixels in the mask to BLACK and the rest to WHITE
683 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
684 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
686 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
691 ::SelectObject(hMemDC
, hOldBitmap
);
695 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
698 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
701 SetWidth(image
.GetWidth());
702 SetHeight(image
.GetHeight());
703 SetDepth(screenDepth
);
704 SetHBITMAP( (WXHBITMAP
) hBitmap
);
707 // Copy the palette from the source image
708 SetPalette(image
.GetPalette());
709 #endif // wxUSE_PALETTE
714 wxImage
wxBitmap::ConvertToImage() const
716 // Initial attempt at a simple-minded implementation.
717 // The bitmap will always be created at the screen depth,
718 // so the 'depth' argument is ignored.
719 // TODO: transparency (create a mask image)
723 wxFAIL_MSG( wxT("bitmap is invalid") );
729 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
731 // create an wxImage object
732 int width
= GetWidth();
733 int height
= GetHeight();
734 image
.Create( width
, height
);
735 unsigned char *data
= image
.GetData();
738 wxFAIL_MSG( wxT("could not allocate data for image") );
742 HDC hScreenDC
= ::GetDC(NULL
);
744 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
745 ::ReleaseDC(NULL
, hScreenDC
);
747 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
749 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
752 for (i
= 0; i
< GetWidth(); i
++)
754 for (j
= 0; j
< GetHeight(); j
++)
756 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
757 unsigned char red
= GetRValue(color
);
758 unsigned char green
= GetGValue(color
);
759 unsigned char blue
= GetBValue(color
);
761 image
.SetRGB(i
, j
, red
, green
, blue
);
765 ::SelectObject(hMemDC
, hOldBitmap
);
769 // Copy the palette from the source image
771 image
.SetPalette(* GetPalette());
772 #endif // wxUSE_PALETTE
777 #endif // __WXMICROWIN__
779 // ----------------------------------------------------------------------------
780 // wxImage to/from conversions
781 // ----------------------------------------------------------------------------
783 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
785 return CreateFromImage(image
, depth
, 0);
788 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
790 wxCHECK_MSG( dc
.Ok(), false,
791 _T("invalid HDC in wxBitmap::CreateFromImage()") );
793 return CreateFromImage(image
, -1, dc
.GetHDC());
798 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
800 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
804 // first convert the image to DIB
805 const int h
= image
.GetHeight();
806 const int w
= image
.GetWidth();
813 depth
= dib
.GetDepth(); // Get depth from image if none specified
815 // store the bitmap parameters
816 wxBitmapRefData
*refData
= new wxBitmapRefData
;
817 refData
->m_width
= w
;
818 refData
->m_height
= h
;
819 refData
->m_hasAlpha
= image
.HasAlpha();
824 // next either store DIB as is or create a DDB from it
825 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
827 // are we going to use DIB?
829 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
830 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
832 // don't delete the DIB section in dib object dtor
833 hbitmap
= dib
.Detach();
835 refData
->m_isDIB
= true;
836 refData
->m_depth
= depth
;
838 #ifndef ALWAYS_USE_DIB
839 else // we need to convert DIB to DDB
841 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
843 refData
->m_depth
= depth
;
845 #endif // !ALWAYS_USE_DIB
847 // validate this object
848 SetHBITMAP((WXHBITMAP
)hbitmap
);
850 // finally also set the mask if we have one
851 if ( image
.HasMask() )
853 const size_t len
= 2*((w
+15)/16);
854 BYTE
*src
= image
.GetData();
855 BYTE
*data
= new BYTE
[h
*len
];
856 memset(data
, 0, h
*len
);
857 BYTE r
= image
.GetMaskRed(),
858 g
= image
.GetMaskGreen(),
859 b
= image
.GetMaskBlue();
861 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
865 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
867 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
870 if ( (mask
>>= 1) == 0 )
878 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
881 wxLogLastError(_T("CreateBitmap(mask)"));
885 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
894 wxImage
wxBitmap::ConvertToImage() const
896 // convert DDB to DIB
904 // and then DIB to our wxImage
905 wxImage image
= dib
.ConvertToImage();
911 // now do the same for the mask, if we have any
912 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
915 wxDIB
dibMask(hbmpMask
);
916 if ( dibMask
.IsOk() )
918 // TODO: use wxRawBitmap to iterate over DIB
920 // we hard code the mask colour for now but we could also make an
921 // effort (and waste time) to choose a colour not present in the
922 // image already to avoid having to fudge the pixels below --
923 // whether it's worth to do it is unclear however
924 static const int MASK_RED
= 1;
925 static const int MASK_GREEN
= 2;
926 static const int MASK_BLUE
= 3;
927 static const int MASK_BLUE_REPLACEMENT
= 2;
929 const int h
= dibMask
.GetHeight();
930 const int w
= dibMask
.GetWidth();
931 const int bpp
= dibMask
.GetDepth();
932 const int maskBytesPerPixel
= bpp
>> 3;
933 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
934 unsigned char *data
= image
.GetData();
936 // remember that DIBs are stored in bottom to top order
938 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
940 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
942 // traverse one mask DIB line
943 unsigned char *mask
= maskLineStart
;
944 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
946 // should this pixel be transparent?
949 // no, check that it isn't transparent by accident
950 if ( (data
[0] == MASK_RED
) &&
951 (data
[1] == MASK_GREEN
) &&
952 (data
[2] == MASK_BLUE
) )
954 // we have to fudge the colour a bit to prevent
955 // this pixel from appearing transparent
956 data
[2] = MASK_BLUE_REPLACEMENT
;
961 else // yes, transparent pixel
964 *data
++ = MASK_GREEN
;
970 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
977 #else // !wxUSE_WXDIB
980 wxBitmap::CreateFromImage(const wxImage
& WXUNUSED(image
),
987 wxImage
wxBitmap::ConvertToImage() const
992 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
994 #endif // wxUSE_IMAGE
996 // ----------------------------------------------------------------------------
997 // loading and saving bitmaps
998 // ----------------------------------------------------------------------------
1000 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1004 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1008 m_refData
= new wxBitmapRefData
;
1010 return handler
->LoadFile(this, filename
, type
, -1, -1);
1012 #if wxUSE_IMAGE && wxUSE_WXDIB
1013 else // no bitmap handler found
1016 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1018 *this = wxBitmap(image
);
1023 #endif // wxUSE_IMAGE
1028 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1032 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1036 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1041 m_refData
= new wxBitmapRefData
;
1043 return handler
->Create(this, data
, type
, width
, height
, depth
);
1046 bool wxBitmap::SaveFile(const wxString
& filename
,
1048 const wxPalette
*palette
)
1050 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1054 return handler
->SaveFile(this, filename
, type
, palette
);
1056 #if wxUSE_IMAGE && wxUSE_WXDIB
1057 else // no bitmap handler found
1059 // FIXME what about palette? shouldn't we use it?
1060 wxImage image
= ConvertToImage();
1063 return image
.SaveFile(filename
, type
);
1066 #endif // wxUSE_IMAGE
1071 // ----------------------------------------------------------------------------
1072 // sub bitmap extraction
1073 // ----------------------------------------------------------------------------
1075 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1077 wxCHECK_MSG( Ok() &&
1078 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1079 (rect
.x
+rect
.width
<= GetWidth()) &&
1080 (rect
.y
+rect
.height
<= GetHeight()),
1081 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1083 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1084 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1086 #ifndef __WXMICROWIN__
1087 // handle alpha channel, if any
1096 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1097 selectDst(dcDst
, GetHbitmapOf(ret
));
1099 if ( !selectSrc
|| !selectDst
)
1101 wxLogLastError(_T("SelectObjct(hBitmap)"));
1104 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1105 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1107 wxLogLastError(_T("BitBlt"));
1111 // copy mask if there is one
1114 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1116 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1117 selectDst(dcDst
, hbmpMask
);
1119 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1120 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1122 wxLogLastError(_T("BitBlt"));
1125 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1128 #endif // !__WXMICROWIN__
1133 // ----------------------------------------------------------------------------
1134 // wxBitmap accessors
1135 // ----------------------------------------------------------------------------
1138 wxPalette
* wxBitmap::GetPalette() const
1140 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1141 : (wxPalette
*) NULL
;
1145 wxMask
*wxBitmap::GetMask() const
1147 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1150 wxBitmap
wxBitmap::GetMaskBitmap() const
1153 wxMask
*mask
= GetMask();
1155 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1161 wxDC
*wxBitmap::GetSelectedInto() const
1163 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1168 #if WXWIN_COMPATIBILITY_2_4
1170 int wxBitmap::GetQuality() const
1175 #endif // WXWIN_COMPATIBILITY_2_4
1177 void wxBitmap::UseAlpha()
1179 if ( GetBitmapData() )
1180 GetBitmapData()->m_hasAlpha
= true;
1183 bool wxBitmap::HasAlpha() const
1185 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1188 // ----------------------------------------------------------------------------
1190 // ----------------------------------------------------------------------------
1194 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1196 if ( GetBitmapData() )
1197 GetBitmapData()->m_selectedInto
= dc
;
1204 void wxBitmap::SetPalette(const wxPalette
& palette
)
1208 GetBitmapData()->m_bitmapPalette
= palette
;
1211 #endif // wxUSE_PALETTE
1213 void wxBitmap::SetMask(wxMask
*mask
)
1217 GetBitmapData()->SetMask(mask
);
1220 #if WXWIN_COMPATIBILITY_2_4
1222 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1226 #endif // WXWIN_COMPATIBILITY_2_4
1228 // ----------------------------------------------------------------------------
1229 // raw bitmap access support
1230 // ----------------------------------------------------------------------------
1232 #ifdef wxHAVE_RAW_BITMAP
1233 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1238 // no bitmap, no data (raw or otherwise)
1242 // if we're already a DIB we can access our data directly, but if not we
1243 // need to convert this DDB to a DIB section and use it for raw access and
1244 // then convert it back
1246 if ( !GetBitmapData()->m_isDIB
)
1248 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1249 _T("GetRawData() may be called only once") );
1251 wxDIB
*dib
= new wxDIB(*this);
1259 // we'll free it in UngetRawData()
1260 GetBitmapData()->m_dib
= dib
;
1262 hDIB
= dib
->GetHandle();
1266 hDIB
= GetHbitmap();
1270 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1272 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1277 // check that the bitmap is in correct format
1278 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1280 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1285 // ok, store the relevant info in wxPixelDataBase
1286 const LONG h
= ds
.dsBm
.bmHeight
;
1288 data
.m_width
= ds
.dsBm
.bmWidth
;
1291 // remember that DIBs are stored in top to bottom order!
1292 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1293 // multiple of 2, as required by the documentation. So we use the official
1294 // formula, which we already use elsewhere.)
1295 const LONG bytesPerRow
=
1296 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1297 data
.m_stride
= -bytesPerRow
;
1299 char *bits
= (char *)ds
.dsBm
.bmBits
;
1302 bits
+= (h
- 1)*bytesPerRow
;
1311 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1319 // invalid data, don't crash -- but don't assert neither as we're
1320 // called automatically from wxPixelDataBase dtor and so there is no
1321 // way to prevent this from happening
1325 // if we're a DDB we need to convert DIB back to DDB now to make the
1326 // changes made via raw bitmap access effective
1327 if ( !GetBitmapData()->m_isDIB
)
1329 wxDIB
*dib
= GetBitmapData()->m_dib
;
1330 GetBitmapData()->m_dib
= NULL
;
1336 #endif // wxUSE_WXDIB
1338 #endif // #ifdef wxHAVE_RAW_BITMAP
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1349 // Construct a mask from a bitmap and a colour indicating
1350 // the transparent area
1351 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1354 Create(bitmap
, colour
);
1357 // Construct a mask from a bitmap and a palette index indicating
1358 // the transparent area
1359 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1362 Create(bitmap
, paletteIndex
);
1365 // Construct a mask from a mono bitmap (copies the bitmap).
1366 wxMask::wxMask(const wxBitmap
& bitmap
)
1375 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1378 // Create a mask from a mono bitmap (copies the bitmap).
1379 bool wxMask::Create(const wxBitmap
& bitmap
)
1381 #ifndef __WXMICROWIN__
1382 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1383 _T("can't create mask from invalid or not monochrome bitmap") );
1387 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1391 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1396 HDC srcDC
= CreateCompatibleDC(0);
1397 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1398 HDC destDC
= CreateCompatibleDC(0);
1399 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1400 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1401 SelectObject(srcDC
, 0);
1403 SelectObject(destDC
, 0);
1407 wxUnusedVar(bitmap
);
1412 // Create a mask from a bitmap and a palette index indicating
1413 // the transparent area
1414 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1418 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1423 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1425 unsigned char red
, green
, blue
;
1426 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1428 wxColour
transparentColour(red
, green
, blue
);
1429 return Create(bitmap
, transparentColour
);
1432 #endif // wxUSE_PALETTE
1437 // Create a mask from a bitmap and a colour indicating
1438 // the transparent area
1439 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1441 #ifndef __WXMICROWIN__
1442 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1446 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1450 int width
= bitmap
.GetWidth(),
1451 height
= bitmap
.GetHeight();
1453 // scan the bitmap for the transparent colour and set the corresponding
1454 // pixels in the mask to BLACK and the rest to WHITE
1455 COLORREF maskColour
= wxColourToPalRGB(colour
);
1456 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1458 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1459 HDC destDC
= ::CreateCompatibleDC(NULL
);
1460 if ( !srcDC
|| !destDC
)
1462 wxLogLastError(wxT("CreateCompatibleDC"));
1467 // SelectObject() will fail
1468 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1469 _T("bitmap can't be selected in another DC") );
1471 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1474 wxLogLastError(wxT("SelectObject"));
1479 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1482 wxLogLastError(wxT("SelectObject"));
1489 // this will create a monochrome bitmap with 0 points for the pixels
1490 // which have the same value as the background colour and 1 for the
1492 ::SetBkColor(srcDC
, maskColour
);
1493 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1496 ::SelectObject(srcDC
, hbmpSrcOld
);
1498 ::SelectObject(destDC
, hbmpDstOld
);
1502 #else // __WXMICROWIN__
1503 wxUnusedVar(bitmap
);
1504 wxUnusedVar(colour
);
1506 #endif // __WXMICROWIN__/!__WXMICROWIN__
1509 // ----------------------------------------------------------------------------
1511 // ----------------------------------------------------------------------------
1513 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1516 int width
, int height
, int depth
)
1518 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1520 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : false;
1523 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1524 const wxString
& name
,
1526 int width
, int height
)
1528 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1530 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : false;
1533 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1534 const wxString
& name
,
1537 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1539 return bitmap
? SaveFile(bitmap
, name
, type
) : false;
1542 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1543 void *WXUNUSED(data
),
1544 long WXUNUSED(type
),
1545 int WXUNUSED(width
),
1546 int WXUNUSED(height
),
1547 int WXUNUSED(depth
))
1552 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1553 const wxString
& WXUNUSED(name
),
1554 long WXUNUSED(type
),
1555 int WXUNUSED(desiredWidth
),
1556 int WXUNUSED(desiredHeight
))
1561 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1562 const wxString
& WXUNUSED(name
),
1564 const wxPalette
*WXUNUSED(palette
))
1569 // ----------------------------------------------------------------------------
1571 // ----------------------------------------------------------------------------
1573 #ifndef __WXMICROWIN__
1574 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1575 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1577 unsigned long i
, headerSize
;
1579 // Allocate space for a DIB header
1580 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1581 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1582 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1584 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1586 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1588 // Fill in the static parts of the DIB header
1589 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1590 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1591 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1592 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1594 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1595 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1596 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1597 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1598 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1601 // Initialize the DIB palette
1602 for (i
= 0; i
< 256; i
++) {
1603 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1604 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1605 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1606 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1609 *lpDIBHeader
= lpDIBheader
;
1614 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1620 // ----------------------------------------------------------------------------
1621 // global helper functions implemented here
1622 // ----------------------------------------------------------------------------
1624 // helper of wxBitmapToHICON/HCURSOR
1626 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1633 // we can't create an icon/cursor form nothing
1637 if ( bmp
.HasAlpha() )
1639 // Create an empty mask bitmap.
1640 // it doesn't seem to work if we mess with the mask at all.
1641 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1644 wxZeroMemory(iconInfo
);
1645 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1648 iconInfo
.xHotspot
= hotSpotX
;
1649 iconInfo
.yHotspot
= hotSpotY
;
1652 iconInfo
.hbmMask
= hMonoBitmap
;
1653 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1655 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1657 ::DeleteObject(hMonoBitmap
);
1662 wxMask
* mask
= bmp
.GetMask();
1666 // we must have a mask for an icon, so even if it's probably incorrect,
1667 // do create it (grey is the "standard" transparent colour)
1668 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1672 wxZeroMemory(iconInfo
);
1673 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1676 iconInfo
.xHotspot
= hotSpotX
;
1677 iconInfo
.yHotspot
= hotSpotY
;
1680 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1681 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1683 // black out the transparent area to preserve background colour, because
1684 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1685 // the mask to the dest rect.
1687 MemoryHDC dcSrc
, dcDst
;
1688 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1689 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1691 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1692 dcSrc
, 0, 0, SRCAND
) )
1694 wxLogLastError(_T("BitBlt"));
1698 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1700 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1702 // we created the mask, now delete it
1706 // delete the inverted mask bitmap we created as well
1707 ::DeleteObject(iconInfo
.hbmMask
);
1712 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1714 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1717 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1719 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1722 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1724 #ifndef __WXMICROWIN__
1725 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1727 // get width/height from the bitmap if not given
1731 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1736 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1737 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1738 if ( !hdcSrc
|| !hdcDst
)
1740 wxLogLastError(wxT("CreateCompatibleDC"));
1743 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1746 wxLogLastError(wxT("CreateBitmap"));
1749 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1750 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1751 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1755 wxLogLastError(wxT("BitBlt"));
1759 SelectObject(hdcSrc
,srcTmp
);
1760 SelectObject(hdcDst
,dstTmp
);