1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 #ifdef wxHAVE_RAW_BITMAP
54 #include "wx/rawbmp.h"
57 // missing from mingw32 header
59 #define CLR_INVALID ((COLORREF)-1)
60 #endif // no CLR_INVALID
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
70 wxBitmapRefData(const wxBitmapRefData
& data
);
71 virtual ~wxBitmapRefData() { Free(); }
75 // set the mask object to use as the mask, we take ownership of it
76 void SetMask(wxMask
*mask
)
82 // set the HBITMAP to use as the mask
83 void SetMask(HBITMAP hbmpMask
)
85 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
89 wxMask
*GetMask() const { return m_bitmapMask
; }
93 wxPalette m_bitmapPalette
;
94 #endif // wxUSE_PALETTE
100 // this field is solely for error checking: we detect selecting a bitmap
101 // into more than one DC at once or deleting a bitmap still selected into a
102 // DC (both are serious programming errors under Windows)
103 wxDC
*m_selectedInto
;
104 #endif // __WXDEBUG__
107 // when GetRawData() is called for a DDB we need to convert it to a DIB
108 // first to be able to provide direct access to it and we cache that DIB
109 // here and convert it back to DDB when UngetRawData() is called
113 // true if we have alpha transparency info and can be drawn using
117 // true if our HBITMAP is a DIB section, false if it is a DDB
121 // optional mask for transparent drawing
122 wxMask
*m_bitmapMask
;
126 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
134 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
136 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // decide whether we should create a DIB or a DDB for the given parameters
148 // NB: we always use DIBs under Windows CE as this is much simpler (even if
149 // also less efficient...) and we obviously can't use them if there is no
150 // DIB support compiled in at all
152 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
154 #define ALWAYS_USE_DIB
156 // no sense in defining wxShouldCreateDIB() as we can't compile code
157 // executed if it is true, so we have to use #if's anyhow
158 #define NEVER_USE_DIB
159 #else // wxUSE_WXDIB && !__WXWINCE__
160 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
162 // here is the logic:
164 // (a) if hdc is specified, the caller explicitly wants DDB
165 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
166 // or less DIBs anyhow)
167 // (c) finally, create DIBs under Win9x even if the depth hasn't been
168 // explicitly specified but the current display depth is 24 or
169 // more and the image is "big", i.e. > 16Mb which is the
170 // theoretical limit for DDBs under Win9x
172 // consequences (all of which seem to make sense):
174 // (i) by default, DDBs are created (depth == -1 usually)
175 // (ii) DIBs can be created by explicitly specifying the depth
176 // (iii) using a DC always forces creating a DDB
180 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
183 #define SOMETIMES_USE_DIB
184 #endif // different DIB usage scenarious
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxBitmapRefData::wxBitmapRefData()
193 m_selectedInto
= NULL
;
197 m_hBitmap
= (WXHBITMAP
) NULL
;
206 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
207 : wxGDIImageRefData(data
)
210 m_selectedInto
= NULL
;
213 // can't copy the mask as the other bitmap destroys it
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 // this function should be called from all wxBitmap ctors
249 void wxBitmap::Init()
251 // m_refData = NULL; done in the base class ctor
254 wxGDIImageRefData
*wxBitmap::CreateData() const
256 return new wxBitmapRefData
;
259 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
261 const wxBitmapRefData
*
262 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
266 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
269 // copy the other bitmap
270 if ( data
->m_hBitmap
)
272 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
273 self
->CopyFromDIB(dib
);
276 #endif // wxUSE_WXDIB
278 // don't copy the bitmap data, but do copy the size, depth, ...
279 self
->m_refData
= new wxBitmapRefData(*data
);
287 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
289 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
290 // it may be either HICON or HCURSOR
291 HICON hicon
= (HICON
)icon
.GetHandle();
294 if ( !::GetIconInfo(hicon
, &iconInfo
) )
296 wxLogLastError(wxT("GetIconInfo"));
301 wxBitmapRefData
*refData
= new wxBitmapRefData
;
304 int w
= icon
.GetWidth(),
305 h
= icon
.GetHeight();
307 refData
->m_width
= w
;
308 refData
->m_height
= h
;
309 refData
->m_depth
= wxDisplayDepth();
311 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
314 // If the icon is 32 bits per pixel then it may have alpha channel data,
315 // although there are some icons that are 32 bpp but have no alpha... So
316 // convert to a DIB and manually check the 4th byte for each pixel.
318 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
319 && bm
.bmBitsPixel
== 32)
321 wxDIB
dib(iconInfo
.hbmColor
);
324 unsigned char* pixels
= dib
.GetData();
325 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
327 if (pixels
[idx
+3] != 0)
329 // If there is an alpha byte that is non-zero then set the
330 // alpha flag and bail out of the loop.
331 refData
->m_hasAlpha
= true;
338 if ( !refData
->m_hasAlpha
)
340 // the mask returned by GetIconInfo() is inverted compared to the usual
342 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
345 // delete the old one now as we don't need it any more
346 ::DeleteObject(iconInfo
.hbmMask
);
357 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
364 return CopyFromIconOrCursor(cursor
);
367 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
374 return CopyFromIconOrCursor(icon
);
377 #ifndef NEVER_USE_DIB
379 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
381 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
383 #ifdef SOMETIMES_USE_DIB
384 HBITMAP hbitmap
= dib
.CreateDDB();
387 #else // ALWAYS_USE_DIB
388 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
389 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
393 wxBitmapRefData
*refData
= new wxBitmapRefData
;
396 refData
->m_width
= dib
.GetWidth();
397 refData
->m_height
= dib
.GetHeight();
398 refData
->m_depth
= dib
.GetDepth();
400 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
403 wxPalette
*palette
= dib
.CreatePalette();
406 refData
->m_bitmapPalette
= *palette
;
410 #endif // wxUSE_PALETTE
415 #endif // NEVER_USE_DIB
417 wxBitmap::~wxBitmap()
421 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
425 #ifndef __WXMICROWIN__
426 wxBitmapRefData
*refData
= new wxBitmapRefData
;
429 refData
->m_width
= width
;
430 refData
->m_height
= height
;
431 refData
->m_depth
= depth
;
436 // we assume that it is in XBM format which is not quite the same as
437 // the format CreateBitmap() wants because the order of bytes in the
439 const size_t bytesPerLine
= (width
+ 7) / 8;
440 const size_t padding
= bytesPerLine
% 2;
441 const size_t len
= height
* ( padding
+ bytesPerLine
);
442 data
= (char *)malloc(len
);
443 const char *src
= bits
;
446 for ( int rows
= 0; rows
< height
; rows
++ )
448 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
450 unsigned char val
= *src
++;
451 unsigned char reversed
= 0;
453 for ( int bits
= 0; bits
< 8; bits
++)
456 reversed
|= (unsigned char)(val
& 0x01);
468 // bits should already be in Windows standard format
469 data
= (char *)bits
; // const_cast is harmless
472 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
475 wxLogLastError(wxT("CreateBitmap"));
483 SetHBITMAP((WXHBITMAP
)hbmp
);
487 // Create from XPM data
488 bool wxBitmap::CreateFromXpm(const char **data
)
490 #if wxUSE_IMAGE && wxUSE_XPM
493 wxCHECK_MSG( data
!= NULL
, false, wxT("invalid bitmap data") )
495 wxXPMDecoder decoder
;
496 wxImage img
= decoder
.ReadData(data
);
497 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
499 *this = wxBitmap(img
);
507 wxBitmap::wxBitmap(int w
, int h
, int d
)
511 (void)Create(w
, h
, d
);
514 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
518 (void)Create(w
, h
, dc
);
521 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
525 (void)Create(data
, type
, width
, height
, depth
);
528 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
532 LoadFile(filename
, (int)type
);
535 bool wxBitmap::Create(int width
, int height
, int depth
)
537 return DoCreate(width
, height
, depth
, 0);
540 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
542 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
544 return DoCreate(width
, height
, -1, dc
.GetHDC());
547 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
551 m_refData
= new wxBitmapRefData
;
553 GetBitmapData()->m_width
= w
;
554 GetBitmapData()->m_height
= h
;
556 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
558 #ifndef NEVER_USE_DIB
559 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
563 // create DIBs without alpha channel by default
571 // don't delete the DIB section in dib object dtor
574 GetBitmapData()->m_isDIB
= true;
575 GetBitmapData()->m_depth
= d
;
578 #endif // NEVER_USE_DIB
580 #ifndef ALWAYS_USE_DIB
581 #ifndef __WXMICROWIN__
584 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
587 wxLogLastError(wxT("CreateBitmap"));
590 GetBitmapData()->m_depth
= d
;
592 else // d == 0, create bitmap compatible with the screen
593 #endif // !__WXMICROWIN__
596 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
599 wxLogLastError(wxT("CreateCompatibleBitmap"));
602 GetBitmapData()->m_depth
= wxDisplayDepth();
604 #endif // !ALWAYS_USE_DIB
607 SetHBITMAP((WXHBITMAP
)hbmp
);
614 // ----------------------------------------------------------------------------
615 // wxImage to/from conversions for Microwin
616 // ----------------------------------------------------------------------------
618 // Microwin versions are so different from normal ones that it really doesn't
619 // make sense to use #ifdefs inside the function bodies
620 #ifdef __WXMICROWIN__
622 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
624 // Set this to 1 to experiment with mask code,
625 // which currently doesn't work
628 m_refData
= new wxBitmapRefData();
630 // Initial attempt at a simple-minded implementation.
631 // The bitmap will always be created at the screen depth,
632 // so the 'depth' argument is ignored.
634 HDC hScreenDC
= ::GetDC(NULL
);
635 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
637 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
638 HBITMAP hMaskBitmap
= NULL
;
639 HBITMAP hOldMaskBitmap
= NULL
;
641 unsigned char maskR
= 0;
642 unsigned char maskG
= 0;
643 unsigned char maskB
= 0;
645 // printf("Created bitmap %d\n", (int) hBitmap);
648 ::ReleaseDC(NULL
, hScreenDC
);
651 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
653 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
654 ::ReleaseDC(NULL
, hScreenDC
);
656 // created an mono-bitmap for the possible mask
657 bool hasMask
= image
.HasMask();
662 // FIXME: we should be able to pass bpp = 1, but
663 // GdBlit can't handle a different depth
665 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
667 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
669 maskR
= image
.GetMaskRed();
670 maskG
= image
.GetMaskGreen();
671 maskB
= image
.GetMaskBlue();
679 hScreenDC
= ::GetDC(NULL
);
680 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
681 ::ReleaseDC(NULL
, hScreenDC
);
683 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
691 for (i
= 0; i
< image
.GetWidth(); i
++)
693 for (j
= 0; j
< image
.GetHeight(); j
++)
695 unsigned char red
= image
.GetRed(i
, j
);
696 unsigned char green
= image
.GetGreen(i
, j
);
697 unsigned char blue
= image
.GetBlue(i
, j
);
699 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
703 // scan the bitmap for the transparent colour and set the corresponding
704 // pixels in the mask to BLACK and the rest to WHITE
705 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
706 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
708 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
713 ::SelectObject(hMemDC
, hOldBitmap
);
717 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
720 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
723 SetWidth(image
.GetWidth());
724 SetHeight(image
.GetHeight());
725 SetDepth(screenDepth
);
726 SetHBITMAP( (WXHBITMAP
) hBitmap
);
729 // Copy the palette from the source image
730 SetPalette(image
.GetPalette());
731 #endif // wxUSE_PALETTE
736 wxImage
wxBitmap::ConvertToImage() const
738 // Initial attempt at a simple-minded implementation.
739 // The bitmap will always be created at the screen depth,
740 // so the 'depth' argument is ignored.
741 // TODO: transparency (create a mask image)
745 wxFAIL_MSG( wxT("bitmap is invalid") );
751 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
753 // create an wxImage object
754 int width
= GetWidth();
755 int height
= GetHeight();
756 image
.Create( width
, height
);
757 unsigned char *data
= image
.GetData();
760 wxFAIL_MSG( wxT("could not allocate data for image") );
764 HDC hScreenDC
= ::GetDC(NULL
);
766 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
767 ::ReleaseDC(NULL
, hScreenDC
);
769 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
771 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
774 for (i
= 0; i
< GetWidth(); i
++)
776 for (j
= 0; j
< GetHeight(); j
++)
778 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
779 unsigned char red
= GetRValue(color
);
780 unsigned char green
= GetGValue(color
);
781 unsigned char blue
= GetBValue(color
);
783 image
.SetRGB(i
, j
, red
, green
, blue
);
787 ::SelectObject(hMemDC
, hOldBitmap
);
791 // Copy the palette from the source image
793 image
.SetPalette(* GetPalette());
794 #endif // wxUSE_PALETTE
799 #endif // __WXMICROWIN__
801 // ----------------------------------------------------------------------------
802 // wxImage to/from conversions
803 // ----------------------------------------------------------------------------
807 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
809 return CreateFromImage(image
, depth
, 0);
812 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
814 wxCHECK_MSG( dc
.Ok(), false,
815 _T("invalid HDC in wxBitmap::CreateFromImage()") );
817 return CreateFromImage(image
, -1, dc
.GetHDC());
820 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
822 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
826 // first convert the image to DIB
827 const int h
= image
.GetHeight();
828 const int w
= image
.GetWidth();
835 // store the bitmap parameters
836 wxBitmapRefData
*refData
= new wxBitmapRefData
;
837 refData
->m_width
= w
;
838 refData
->m_height
= h
;
839 refData
->m_hasAlpha
= image
.HasAlpha();
844 // next either store DIB as is or create a DDB from it
845 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
847 // are we going to use DIB?
849 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
850 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
852 // don't delete the DIB section in dib object dtor
853 hbitmap
= dib
.Detach();
855 refData
->m_isDIB
= true;
856 refData
->m_depth
= dib
.GetDepth();
858 #ifndef ALWAYS_USE_DIB
859 else // we need to convert DIB to DDB
861 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
863 refData
->m_depth
= depth
== -1 ? dib
.GetDepth() : depth
;
865 #endif // !ALWAYS_USE_DIB
867 // validate this object
868 SetHBITMAP((WXHBITMAP
)hbitmap
);
870 // finally also set the mask if we have one
871 if ( image
.HasMask() )
873 const size_t len
= 2*((w
+15)/16);
874 BYTE
*src
= image
.GetData();
875 BYTE
*data
= new BYTE
[h
*len
];
876 memset(data
, 0, h
*len
);
877 BYTE r
= image
.GetMaskRed(),
878 g
= image
.GetMaskGreen(),
879 b
= image
.GetMaskBlue();
881 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
885 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
887 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
890 if ( (mask
>>= 1) == 0 )
898 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
901 wxLogLastError(_T("CreateBitmap(mask)"));
905 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
914 wxImage
wxBitmap::ConvertToImage() const
916 // convert DDB to DIB
924 // and then DIB to our wxImage
925 wxImage image
= dib
.ConvertToImage();
931 // now do the same for the mask, if we have any
932 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
935 wxDIB
dibMask(hbmpMask
);
936 if ( dibMask
.IsOk() )
938 // TODO: use wxRawBitmap to iterate over DIB
940 // we hard code the mask colour for now but we could also make an
941 // effort (and waste time) to choose a colour not present in the
942 // image already to avoid having to fudge the pixels below --
943 // whether it's worth to do it is unclear however
944 static const int MASK_RED
= 1;
945 static const int MASK_GREEN
= 2;
946 static const int MASK_BLUE
= 3;
947 static const int MASK_BLUE_REPLACEMENT
= 2;
949 const int h
= dibMask
.GetHeight();
950 const int w
= dibMask
.GetWidth();
951 const int bpp
= dibMask
.GetDepth();
952 const int maskBytesPerPixel
= bpp
>> 3;
953 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
954 unsigned char *data
= image
.GetData();
956 // remember that DIBs are stored in bottom to top order
958 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
960 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
962 // traverse one mask DIB line
963 unsigned char *mask
= maskLineStart
;
964 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
966 // should this pixel be transparent?
969 // no, check that it isn't transparent by accident
970 if ( (data
[0] == MASK_RED
) &&
971 (data
[1] == MASK_GREEN
) &&
972 (data
[2] == MASK_BLUE
) )
974 // we have to fudge the colour a bit to prevent
975 // this pixel from appearing transparent
976 data
[2] = MASK_BLUE_REPLACEMENT
;
981 else // yes, transparent pixel
984 *data
++ = MASK_GREEN
;
990 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
997 #endif // wxUSE_WXDIB
999 #endif // wxUSE_IMAGE
1001 // ----------------------------------------------------------------------------
1002 // loading and saving bitmaps
1003 // ----------------------------------------------------------------------------
1005 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1009 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1013 m_refData
= new wxBitmapRefData
;
1015 return handler
->LoadFile(this, filename
, type
, -1, -1);
1018 else // no bitmap handler found
1021 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1023 *this = wxBitmap(image
);
1028 #endif // wxUSE_IMAGE
1033 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1037 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1041 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1046 m_refData
= new wxBitmapRefData
;
1048 return handler
->Create(this, data
, type
, width
, height
, depth
);
1051 bool wxBitmap::SaveFile(const wxString
& filename
,
1053 const wxPalette
*palette
)
1055 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1059 return handler
->SaveFile(this, filename
, type
, palette
);
1062 else // no bitmap handler found
1064 // FIXME what about palette? shouldn't we use it?
1065 wxImage image
= ConvertToImage();
1068 return image
.SaveFile(filename
, type
);
1071 #endif // wxUSE_IMAGE
1076 // ----------------------------------------------------------------------------
1077 // sub bitmap extraction
1078 // ----------------------------------------------------------------------------
1080 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1082 wxCHECK_MSG( Ok() &&
1083 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1084 (rect
.x
+rect
.width
<= GetWidth()) &&
1085 (rect
.y
+rect
.height
<= GetHeight()),
1086 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1088 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1089 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1091 #ifndef __WXMICROWIN__
1092 // handle alpha channel, if any
1101 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1102 selectDst(dcDst
, GetHbitmapOf(ret
));
1104 if ( !selectSrc
|| !selectDst
)
1106 wxLogLastError(_T("SelectObjct(hBitmap)"));
1109 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1110 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1112 wxLogLastError(_T("BitBlt"));
1116 // copy mask if there is one
1119 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1121 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1122 selectDst(dcDst
, hbmpMask
);
1124 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1125 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1127 wxLogLastError(_T("BitBlt"));
1130 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1133 #endif // !__WXMICROWIN__
1138 // ----------------------------------------------------------------------------
1139 // wxBitmap accessors
1140 // ----------------------------------------------------------------------------
1143 wxPalette
* wxBitmap::GetPalette() const
1145 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1146 : (wxPalette
*) NULL
;
1150 wxMask
*wxBitmap::GetMask() const
1152 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1155 wxBitmap
wxBitmap::GetMaskBitmap() const
1158 wxMask
*mask
= GetMask();
1160 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1166 wxDC
*wxBitmap::GetSelectedInto() const
1168 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1173 #if WXWIN_COMPATIBILITY_2_4
1175 int wxBitmap::GetQuality() const
1180 #endif // WXWIN_COMPATIBILITY_2_4
1182 void wxBitmap::UseAlpha()
1184 if ( GetBitmapData() )
1185 GetBitmapData()->m_hasAlpha
= true;
1188 bool wxBitmap::HasAlpha() const
1190 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1193 // ----------------------------------------------------------------------------
1195 // ----------------------------------------------------------------------------
1199 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1201 if ( GetBitmapData() )
1202 GetBitmapData()->m_selectedInto
= dc
;
1209 void wxBitmap::SetPalette(const wxPalette
& palette
)
1213 GetBitmapData()->m_bitmapPalette
= palette
;
1216 #endif // wxUSE_PALETTE
1218 void wxBitmap::SetMask(wxMask
*mask
)
1222 GetBitmapData()->SetMask(mask
);
1225 #if WXWIN_COMPATIBILITY_2_4
1227 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1231 #endif // WXWIN_COMPATIBILITY_2_4
1233 // ----------------------------------------------------------------------------
1234 // raw bitmap access support
1235 // ----------------------------------------------------------------------------
1237 #ifdef wxHAVE_RAW_BITMAP
1238 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1243 // no bitmap, no data (raw or otherwise)
1247 // if we're already a DIB we can access our data directly, but if not we
1248 // need to convert this DDB to a DIB section and use it for raw access and
1249 // then convert it back
1251 if ( !GetBitmapData()->m_isDIB
)
1253 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1254 _T("GetRawData() may be called only once") );
1256 wxDIB
*dib
= new wxDIB(*this);
1264 // we'll free it in UngetRawData()
1265 GetBitmapData()->m_dib
= dib
;
1267 hDIB
= dib
->GetHandle();
1271 hDIB
= GetHbitmap();
1275 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1277 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1282 // check that the bitmap is in correct format
1283 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1285 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1290 // ok, store the relevant info in wxPixelDataBase
1291 const LONG h
= ds
.dsBm
.bmHeight
;
1293 data
.m_width
= ds
.dsBm
.bmWidth
;
1296 // remember that DIBs are stored in top to bottom order!
1297 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1298 // multiple of 2, as required by the documentation. So we use the official
1299 // formula, which we already use elsewhere.)
1300 const LONG bytesPerRow
=
1301 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1302 data
.m_stride
= -bytesPerRow
;
1304 char *bits
= (char *)ds
.dsBm
.bmBits
;
1307 bits
+= (h
- 1)*bytesPerRow
;
1316 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1324 // invalid data, don't crash -- but don't assert neither as we're
1325 // called automatically from wxPixelDataBase dtor and so there is no
1326 // way to prevent this from happening
1330 // if we're a DDB we need to convert DIB back to DDB now to make the
1331 // changes made via raw bitmap access effective
1332 if ( !GetBitmapData()->m_isDIB
)
1334 wxDIB
*dib
= GetBitmapData()->m_dib
;
1335 GetBitmapData()->m_dib
= NULL
;
1341 #endif // wxUSE_WXDIB
1343 #endif // #ifdef wxHAVE_RAW_BITMAP
1345 // ----------------------------------------------------------------------------
1347 // ----------------------------------------------------------------------------
1354 // Construct a mask from a bitmap and a colour indicating
1355 // the transparent area
1356 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1359 Create(bitmap
, colour
);
1362 // Construct a mask from a bitmap and a palette index indicating
1363 // the transparent area
1364 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1367 Create(bitmap
, paletteIndex
);
1370 // Construct a mask from a mono bitmap (copies the bitmap).
1371 wxMask::wxMask(const wxBitmap
& bitmap
)
1380 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1383 // Create a mask from a mono bitmap (copies the bitmap).
1384 bool wxMask::Create(const wxBitmap
& bitmap
)
1386 #ifndef __WXMICROWIN__
1387 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1388 _T("can't create mask from invalid or not monochrome bitmap") );
1392 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1396 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1401 HDC srcDC
= CreateCompatibleDC(0);
1402 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1403 HDC destDC
= CreateCompatibleDC(0);
1404 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1405 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1406 SelectObject(srcDC
, 0);
1408 SelectObject(destDC
, 0);
1412 wxUnusedVar(bitmap
);
1417 // Create a mask from a bitmap and a palette index indicating
1418 // the transparent area
1419 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1423 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1428 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1430 unsigned char red
, green
, blue
;
1431 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1433 wxColour
transparentColour(red
, green
, blue
);
1434 return Create(bitmap
, transparentColour
);
1437 #endif // wxUSE_PALETTE
1442 // Create a mask from a bitmap and a colour indicating
1443 // the transparent area
1444 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1446 #ifndef __WXMICROWIN__
1447 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1451 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1455 int width
= bitmap
.GetWidth(),
1456 height
= bitmap
.GetHeight();
1458 // scan the bitmap for the transparent colour and set the corresponding
1459 // pixels in the mask to BLACK and the rest to WHITE
1460 COLORREF maskColour
= wxColourToPalRGB(colour
);
1461 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1463 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1464 HDC destDC
= ::CreateCompatibleDC(NULL
);
1465 if ( !srcDC
|| !destDC
)
1467 wxLogLastError(wxT("CreateCompatibleDC"));
1472 // SelectObject() will fail
1473 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1474 _T("bitmap can't be selected in another DC") );
1476 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1479 wxLogLastError(wxT("SelectObject"));
1484 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1487 wxLogLastError(wxT("SelectObject"));
1494 // this will create a monochrome bitmap with 0 points for the pixels
1495 // which have the same value as the background colour and 1 for the
1497 ::SetBkColor(srcDC
, maskColour
);
1498 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1501 ::SelectObject(srcDC
, hbmpSrcOld
);
1503 ::SelectObject(destDC
, hbmpDstOld
);
1507 #else // __WXMICROWIN__
1508 wxUnusedVar(bitmap
);
1509 wxUnusedVar(colour
);
1511 #endif // __WXMICROWIN__/!__WXMICROWIN__
1514 // ----------------------------------------------------------------------------
1516 // ----------------------------------------------------------------------------
1518 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1521 int width
, int height
, int depth
)
1523 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1525 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : false;
1528 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1529 const wxString
& name
,
1531 int width
, int height
)
1533 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1535 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : false;
1538 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1539 const wxString
& name
,
1542 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1544 return bitmap
? SaveFile(bitmap
, name
, type
) : false;
1547 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1548 void *WXUNUSED(data
),
1549 long WXUNUSED(type
),
1550 int WXUNUSED(width
),
1551 int WXUNUSED(height
),
1552 int WXUNUSED(depth
))
1557 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1558 const wxString
& WXUNUSED(name
),
1559 long WXUNUSED(type
),
1560 int WXUNUSED(desiredWidth
),
1561 int WXUNUSED(desiredHeight
))
1566 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1567 const wxString
& WXUNUSED(name
),
1569 const wxPalette
*WXUNUSED(palette
))
1574 // ----------------------------------------------------------------------------
1576 // ----------------------------------------------------------------------------
1578 #ifndef __WXMICROWIN__
1579 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1580 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1582 unsigned long i
, headerSize
;
1584 // Allocate space for a DIB header
1585 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1586 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1587 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1589 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1591 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1593 // Fill in the static parts of the DIB header
1594 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1595 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1596 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1597 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1599 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1600 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1601 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1602 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1603 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1606 // Initialize the DIB palette
1607 for (i
= 0; i
< 256; i
++) {
1608 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1609 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1610 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1611 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1614 *lpDIBHeader
= lpDIBheader
;
1619 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1625 // ----------------------------------------------------------------------------
1626 // global helper functions implemented here
1627 // ----------------------------------------------------------------------------
1629 // helper of wxBitmapToHICON/HCURSOR
1631 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1638 // we can't create an icon/cursor form nothing
1644 if ( bmp
.HasAlpha() )
1646 // Convert alpha to a mask. NOTE: It would be better to actually put
1647 // the alpha into the icon instead of making a mask, but I don't have
1648 // time to figure that out today.
1649 wxImage img
= bmp
.ConvertToImage();
1650 img
.ConvertAlphaToMask();
1651 newbmp
= wxBitmap(img
);
1652 mask
= newbmp
.GetMask();
1656 mask
= bmp
.GetMask();
1661 // we must have a mask for an icon, so even if it's probably incorrect,
1662 // do create it (grey is the "standard" transparent colour)
1663 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1667 wxZeroMemory(iconInfo
);
1668 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1671 iconInfo
.xHotspot
= hotSpotX
;
1672 iconInfo
.yHotspot
= hotSpotY
;
1675 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1676 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1678 // black out the transparent area to preserve background colour, because
1679 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1680 // the mask to the dest rect.
1682 MemoryHDC dcSrc
, dcDst
;
1683 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1684 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1686 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1687 dcSrc
, 0, 0, SRCAND
) )
1689 wxLogLastError(_T("BitBlt"));
1693 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1695 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1697 // we created the mask, now delete it
1701 // delete the inverted mask bitmap we created as well
1702 ::DeleteObject(iconInfo
.hbmMask
);
1707 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1709 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1712 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1714 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1717 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1719 #ifndef __WXMICROWIN__
1720 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1722 // get width/height from the bitmap if not given
1726 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1731 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1732 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1733 if ( !hdcSrc
|| !hdcDst
)
1735 wxLogLastError(wxT("CreateCompatibleDC"));
1738 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1741 wxLogLastError(wxT("CreateBitmap"));
1744 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1745 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1746 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1750 wxLogLastError(wxT("BitBlt"));
1754 SelectObject(hdcSrc
,srcTmp
);
1755 SelectObject(hdcDst
,dstTmp
);