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
;
313 // the mask returned by GetIconInfo() is inversed compared to the usual
315 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
318 // delete the old one now as we don't need it any more
319 ::DeleteObject(iconInfo
.hbmMask
);
330 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
337 return CopyFromIconOrCursor(cursor
);
340 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
347 return CopyFromIconOrCursor(icon
);
350 #ifndef NEVER_USE_DIB
352 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
354 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
356 #ifdef SOMETIMES_USE_DIB
357 HBITMAP hbitmap
= dib
.CreateDDB();
360 #else // ALWAYS_USE_DIB
361 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
362 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
366 wxBitmapRefData
*refData
= new wxBitmapRefData
;
369 refData
->m_width
= dib
.GetWidth();
370 refData
->m_height
= dib
.GetHeight();
371 refData
->m_depth
= dib
.GetDepth();
373 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
376 wxPalette
*palette
= dib
.CreatePalette();
379 refData
->m_bitmapPalette
= *palette
;
383 #endif // wxUSE_PALETTE
388 #endif // NEVER_USE_DIB
390 wxBitmap::~wxBitmap()
394 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
398 #ifndef __WXMICROWIN__
399 wxBitmapRefData
*refData
= new wxBitmapRefData
;
402 refData
->m_width
= width
;
403 refData
->m_height
= height
;
404 refData
->m_depth
= depth
;
409 // we assume that it is in XBM format which is not quite the same as
410 // the format CreateBitmap() wants because the order of bytes in the
412 const size_t bytesPerLine
= (width
+ 7) / 8;
413 const size_t padding
= bytesPerLine
% 2;
414 const size_t len
= height
* ( padding
+ bytesPerLine
);
415 data
= (char *)malloc(len
);
416 const char *src
= bits
;
419 for ( int rows
= 0; rows
< height
; rows
++ )
421 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
423 unsigned char val
= *src
++;
424 unsigned char reversed
= 0;
426 for ( int bits
= 0; bits
< 8; bits
++)
429 reversed
|= (unsigned char)(val
& 0x01);
441 // bits should already be in Windows standard format
442 data
= (char *)bits
; // const_cast is harmless
445 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
448 wxLogLastError(wxT("CreateBitmap"));
456 SetHBITMAP((WXHBITMAP
)hbmp
);
460 // Create from XPM data
461 bool wxBitmap::CreateFromXpm(const char **data
)
463 #if wxUSE_IMAGE && wxUSE_XPM
466 wxCHECK_MSG( data
!= NULL
, false, wxT("invalid bitmap data") )
468 wxXPMDecoder decoder
;
469 wxImage img
= decoder
.ReadData(data
);
470 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
472 *this = wxBitmap(img
);
480 wxBitmap::wxBitmap(int w
, int h
, int d
)
484 (void)Create(w
, h
, d
);
487 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
491 (void)Create(w
, h
, dc
);
494 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
498 (void)Create(data
, type
, width
, height
, depth
);
501 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
505 LoadFile(filename
, (int)type
);
508 bool wxBitmap::Create(int width
, int height
, int depth
)
510 return DoCreate(width
, height
, depth
, 0);
513 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
515 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
517 return DoCreate(width
, height
, -1, dc
.GetHDC());
520 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
524 m_refData
= new wxBitmapRefData
;
526 GetBitmapData()->m_width
= w
;
527 GetBitmapData()->m_height
= h
;
529 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
531 #ifndef NEVER_USE_DIB
532 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
536 // create DIBs without alpha channel by default
544 // don't delete the DIB section in dib object dtor
547 GetBitmapData()->m_isDIB
= true;
548 GetBitmapData()->m_depth
= d
;
551 #endif // NEVER_USE_DIB
553 #ifndef ALWAYS_USE_DIB
554 #ifndef __WXMICROWIN__
557 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
560 wxLogLastError(wxT("CreateBitmap"));
563 GetBitmapData()->m_depth
= d
;
565 else // d == 0, create bitmap compatible with the screen
566 #endif // !__WXMICROWIN__
569 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
572 wxLogLastError(wxT("CreateCompatibleBitmap"));
575 GetBitmapData()->m_depth
= wxDisplayDepth();
577 #endif // !ALWAYS_USE_DIB
580 SetHBITMAP((WXHBITMAP
)hbmp
);
587 // ----------------------------------------------------------------------------
588 // wxImage to/from conversions for Microwin
589 // ----------------------------------------------------------------------------
591 // Microwin versions are so different from normal ones that it really doesn't
592 // make sense to use #ifdefs inside the function bodies
593 #ifdef __WXMICROWIN__
595 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
597 // Set this to 1 to experiment with mask code,
598 // which currently doesn't work
601 m_refData
= new wxBitmapRefData();
603 // Initial attempt at a simple-minded implementation.
604 // The bitmap will always be created at the screen depth,
605 // so the 'depth' argument is ignored.
607 HDC hScreenDC
= ::GetDC(NULL
);
608 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
610 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
611 HBITMAP hMaskBitmap
= NULL
;
612 HBITMAP hOldMaskBitmap
= NULL
;
614 unsigned char maskR
= 0;
615 unsigned char maskG
= 0;
616 unsigned char maskB
= 0;
618 // printf("Created bitmap %d\n", (int) hBitmap);
621 ::ReleaseDC(NULL
, hScreenDC
);
624 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
626 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
627 ::ReleaseDC(NULL
, hScreenDC
);
629 // created an mono-bitmap for the possible mask
630 bool hasMask
= image
.HasMask();
635 // FIXME: we should be able to pass bpp = 1, but
636 // GdBlit can't handle a different depth
638 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
640 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
642 maskR
= image
.GetMaskRed();
643 maskG
= image
.GetMaskGreen();
644 maskB
= image
.GetMaskBlue();
652 hScreenDC
= ::GetDC(NULL
);
653 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
654 ::ReleaseDC(NULL
, hScreenDC
);
656 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
664 for (i
= 0; i
< image
.GetWidth(); i
++)
666 for (j
= 0; j
< image
.GetHeight(); j
++)
668 unsigned char red
= image
.GetRed(i
, j
);
669 unsigned char green
= image
.GetGreen(i
, j
);
670 unsigned char blue
= image
.GetBlue(i
, j
);
672 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
676 // scan the bitmap for the transparent colour and set the corresponding
677 // pixels in the mask to BLACK and the rest to WHITE
678 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
679 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
681 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
686 ::SelectObject(hMemDC
, hOldBitmap
);
690 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
693 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
696 SetWidth(image
.GetWidth());
697 SetHeight(image
.GetHeight());
698 SetDepth(screenDepth
);
699 SetHBITMAP( (WXHBITMAP
) hBitmap
);
702 // Copy the palette from the source image
703 SetPalette(image
.GetPalette());
704 #endif // wxUSE_PALETTE
709 wxImage
wxBitmap::ConvertToImage() const
711 // Initial attempt at a simple-minded implementation.
712 // The bitmap will always be created at the screen depth,
713 // so the 'depth' argument is ignored.
714 // TODO: transparency (create a mask image)
718 wxFAIL_MSG( wxT("bitmap is invalid") );
724 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
726 // create an wxImage object
727 int width
= GetWidth();
728 int height
= GetHeight();
729 image
.Create( width
, height
);
730 unsigned char *data
= image
.GetData();
733 wxFAIL_MSG( wxT("could not allocate data for image") );
737 HDC hScreenDC
= ::GetDC(NULL
);
739 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
740 ::ReleaseDC(NULL
, hScreenDC
);
742 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
744 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
747 for (i
= 0; i
< GetWidth(); i
++)
749 for (j
= 0; j
< GetHeight(); j
++)
751 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
752 unsigned char red
= GetRValue(color
);
753 unsigned char green
= GetGValue(color
);
754 unsigned char blue
= GetBValue(color
);
756 image
.SetRGB(i
, j
, red
, green
, blue
);
760 ::SelectObject(hMemDC
, hOldBitmap
);
764 // Copy the palette from the source image
766 image
.SetPalette(* GetPalette());
767 #endif // wxUSE_PALETTE
772 #endif // __WXMICROWIN__
774 // ----------------------------------------------------------------------------
775 // wxImage to/from conversions
776 // ----------------------------------------------------------------------------
780 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
782 return CreateFromImage(image
, depth
, 0);
785 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
787 wxCHECK_MSG( dc
.Ok(), false,
788 _T("invalid HDC in wxBitmap::CreateFromImage()") );
790 return CreateFromImage(image
, -1, dc
.GetHDC());
793 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
795 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
799 // first convert the image to DIB
800 const int h
= image
.GetHeight();
801 const int w
= image
.GetWidth();
808 // store the bitmap parameters
809 wxBitmapRefData
*refData
= new wxBitmapRefData
;
810 refData
->m_width
= w
;
811 refData
->m_height
= h
;
812 refData
->m_hasAlpha
= image
.HasAlpha();
817 // next either store DIB as is or create a DDB from it
818 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
820 // are we going to use DIB?
822 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
823 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
825 // don't delete the DIB section in dib object dtor
826 hbitmap
= dib
.Detach();
828 refData
->m_isDIB
= true;
829 refData
->m_depth
= dib
.GetDepth();
831 #ifndef ALWAYS_USE_DIB
832 else // we need to convert DIB to DDB
834 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
836 refData
->m_depth
= depth
== -1 ? dib
.GetDepth() : depth
;
838 #endif // !ALWAYS_USE_DIB
840 // validate this object
841 SetHBITMAP((WXHBITMAP
)hbitmap
);
843 // finally also set the mask if we have one
844 if ( image
.HasMask() )
846 const size_t len
= 2*((w
+15)/16);
847 BYTE
*src
= image
.GetData();
848 BYTE
*data
= new BYTE
[h
*len
];
849 memset(data
, 0, h
*len
);
850 BYTE r
= image
.GetMaskRed(),
851 g
= image
.GetMaskGreen(),
852 b
= image
.GetMaskBlue();
854 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
858 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
860 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
863 if ( (mask
>>= 1) == 0 )
871 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
874 wxLogLastError(_T("CreateBitmap(mask)"));
878 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
887 wxImage
wxBitmap::ConvertToImage() const
889 // convert DDB to DIB
897 // and then DIB to our wxImage
898 wxImage image
= dib
.ConvertToImage();
904 // now do the same for the mask, if we have any
905 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
908 wxDIB
dibMask(hbmpMask
);
909 if ( dibMask
.IsOk() )
911 // TODO: use wxRawBitmap to iterate over DIB
913 // we hard code the mask colour for now but we could also make an
914 // effort (and waste time) to choose a colour not present in the
915 // image already to avoid having to fudge the pixels below --
916 // whether it's worth to do it is unclear however
917 static const int MASK_RED
= 1;
918 static const int MASK_GREEN
= 2;
919 static const int MASK_BLUE
= 3;
920 static const int MASK_BLUE_REPLACEMENT
= 2;
922 const int h
= dibMask
.GetHeight();
923 const int w
= dibMask
.GetWidth();
924 const int bpp
= dibMask
.GetDepth();
925 const int maskBytesPerPixel
= bpp
>> 3;
926 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
927 unsigned char *data
= image
.GetData();
929 // remember that DIBs are stored in bottom to top order
931 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
933 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
935 // traverse one mask DIB line
936 unsigned char *mask
= maskLineStart
;
937 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
939 // should this pixel be transparent?
942 // no, check that it isn't transparent by accident
943 if ( (data
[0] == MASK_RED
) &&
944 (data
[1] == MASK_GREEN
) &&
945 (data
[2] == MASK_BLUE
) )
947 // we have to fudge the colour a bit to prevent
948 // this pixel from appearing transparent
949 data
[2] = MASK_BLUE_REPLACEMENT
;
954 else // yes, transparent pixel
957 *data
++ = MASK_GREEN
;
963 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
970 #endif // wxUSE_WXDIB
972 #endif // wxUSE_IMAGE
974 // ----------------------------------------------------------------------------
975 // loading and saving bitmaps
976 // ----------------------------------------------------------------------------
978 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
982 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
986 m_refData
= new wxBitmapRefData
;
988 return handler
->LoadFile(this, filename
, type
, -1, -1);
991 else // no bitmap handler found
994 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
996 *this = wxBitmap(image
);
1001 #endif // wxUSE_IMAGE
1006 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1010 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1014 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1019 m_refData
= new wxBitmapRefData
;
1021 return handler
->Create(this, data
, type
, width
, height
, depth
);
1024 bool wxBitmap::SaveFile(const wxString
& filename
,
1026 const wxPalette
*palette
)
1028 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1032 return handler
->SaveFile(this, filename
, type
, palette
);
1035 else // no bitmap handler found
1037 // FIXME what about palette? shouldn't we use it?
1038 wxImage image
= ConvertToImage();
1041 return image
.SaveFile(filename
, type
);
1044 #endif // wxUSE_IMAGE
1049 // ----------------------------------------------------------------------------
1050 // sub bitmap extraction
1051 // ----------------------------------------------------------------------------
1053 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1055 wxCHECK_MSG( Ok() &&
1056 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1057 (rect
.x
+rect
.width
<= GetWidth()) &&
1058 (rect
.y
+rect
.height
<= GetHeight()),
1059 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1061 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1062 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1064 #ifndef __WXMICROWIN__
1065 // handle alpha channel, if any
1074 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1075 selectDst(dcDst
, GetHbitmapOf(ret
));
1077 if ( !selectSrc
|| !selectDst
)
1079 wxLogLastError(_T("SelectObjct(hBitmap)"));
1082 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1083 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1085 wxLogLastError(_T("BitBlt"));
1089 // copy mask if there is one
1092 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1094 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1095 selectDst(dcDst
, hbmpMask
);
1097 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1098 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1100 wxLogLastError(_T("BitBlt"));
1103 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1106 #endif // !__WXMICROWIN__
1111 // ----------------------------------------------------------------------------
1112 // wxBitmap accessors
1113 // ----------------------------------------------------------------------------
1116 wxPalette
* wxBitmap::GetPalette() const
1118 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1119 : (wxPalette
*) NULL
;
1123 wxMask
*wxBitmap::GetMask() const
1125 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1128 wxBitmap
wxBitmap::GetMaskBitmap() const
1131 wxMask
*mask
= GetMask();
1133 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1139 wxDC
*wxBitmap::GetSelectedInto() const
1141 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1146 #if WXWIN_COMPATIBILITY_2_4
1148 int wxBitmap::GetQuality() const
1153 #endif // WXWIN_COMPATIBILITY_2_4
1155 void wxBitmap::UseAlpha()
1157 if ( GetBitmapData() )
1158 GetBitmapData()->m_hasAlpha
= true;
1161 bool wxBitmap::HasAlpha() const
1163 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1166 // ----------------------------------------------------------------------------
1168 // ----------------------------------------------------------------------------
1172 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1174 if ( GetBitmapData() )
1175 GetBitmapData()->m_selectedInto
= dc
;
1182 void wxBitmap::SetPalette(const wxPalette
& palette
)
1186 GetBitmapData()->m_bitmapPalette
= palette
;
1189 #endif // wxUSE_PALETTE
1191 void wxBitmap::SetMask(wxMask
*mask
)
1195 GetBitmapData()->SetMask(mask
);
1198 #if WXWIN_COMPATIBILITY_2_4
1200 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1204 #endif // WXWIN_COMPATIBILITY_2_4
1206 // ----------------------------------------------------------------------------
1207 // raw bitmap access support
1208 // ----------------------------------------------------------------------------
1210 #ifdef wxHAVE_RAW_BITMAP
1211 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1216 // no bitmap, no data (raw or otherwise)
1220 // if we're already a DIB we can access our data directly, but if not we
1221 // need to convert this DDB to a DIB section and use it for raw access and
1222 // then convert it back
1224 if ( !GetBitmapData()->m_isDIB
)
1226 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1227 _T("GetRawData() may be called only once") );
1229 wxDIB
*dib
= new wxDIB(*this);
1237 // we'll free it in UngetRawData()
1238 GetBitmapData()->m_dib
= dib
;
1240 hDIB
= dib
->GetHandle();
1244 hDIB
= GetHbitmap();
1248 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1250 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1255 // check that the bitmap is in correct format
1256 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1258 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1263 // ok, store the relevant info in wxPixelDataBase
1264 const LONG h
= ds
.dsBm
.bmHeight
;
1266 data
.m_width
= ds
.dsBm
.bmWidth
;
1269 // remember that DIBs are stored in top to bottom order!
1270 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1271 // multiple of 2, as required by the documentation. So we use the official
1272 // formula, which we already use elsewhere.)
1273 const LONG bytesPerRow
=
1274 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1275 data
.m_stride
= -bytesPerRow
;
1277 char *bits
= (char *)ds
.dsBm
.bmBits
;
1280 bits
+= (h
- 1)*bytesPerRow
;
1289 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1297 // invalid data, don't crash -- but don't assert neither as we're
1298 // called automatically from wxPixelDataBase dtor and so there is no
1299 // way to prevent this from happening
1303 // if we're a DDB we need to convert DIB back to DDB now to make the
1304 // changes made via raw bitmap access effective
1305 if ( !GetBitmapData()->m_isDIB
)
1307 wxDIB
*dib
= GetBitmapData()->m_dib
;
1308 GetBitmapData()->m_dib
= NULL
;
1314 #endif // wxUSE_WXDIB
1316 #endif // #ifdef wxHAVE_RAW_BITMAP
1318 // ----------------------------------------------------------------------------
1320 // ----------------------------------------------------------------------------
1327 // Construct a mask from a bitmap and a colour indicating
1328 // the transparent area
1329 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1332 Create(bitmap
, colour
);
1335 // Construct a mask from a bitmap and a palette index indicating
1336 // the transparent area
1337 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1340 Create(bitmap
, paletteIndex
);
1343 // Construct a mask from a mono bitmap (copies the bitmap).
1344 wxMask::wxMask(const wxBitmap
& bitmap
)
1353 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1356 // Create a mask from a mono bitmap (copies the bitmap).
1357 bool wxMask::Create(const wxBitmap
& bitmap
)
1359 #ifndef __WXMICROWIN__
1360 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1361 _T("can't create mask from invalid or not monochrome bitmap") );
1365 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1369 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1374 HDC srcDC
= CreateCompatibleDC(0);
1375 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1376 HDC destDC
= CreateCompatibleDC(0);
1377 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1378 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1379 SelectObject(srcDC
, 0);
1381 SelectObject(destDC
, 0);
1385 wxUnusedVar(bitmap
);
1390 // Create a mask from a bitmap and a palette index indicating
1391 // the transparent area
1392 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1396 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1401 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1403 unsigned char red
, green
, blue
;
1404 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1406 wxColour
transparentColour(red
, green
, blue
);
1407 return Create(bitmap
, transparentColour
);
1410 #endif // wxUSE_PALETTE
1415 // Create a mask from a bitmap and a colour indicating
1416 // the transparent area
1417 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1419 #ifndef __WXMICROWIN__
1420 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1424 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1428 int width
= bitmap
.GetWidth(),
1429 height
= bitmap
.GetHeight();
1431 // scan the bitmap for the transparent colour and set the corresponding
1432 // pixels in the mask to BLACK and the rest to WHITE
1433 COLORREF maskColour
= wxColourToPalRGB(colour
);
1434 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1436 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1437 HDC destDC
= ::CreateCompatibleDC(NULL
);
1438 if ( !srcDC
|| !destDC
)
1440 wxLogLastError(wxT("CreateCompatibleDC"));
1445 // SelectObject() will fail
1446 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1447 _T("bitmap can't be selected in another DC") );
1449 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1452 wxLogLastError(wxT("SelectObject"));
1457 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1460 wxLogLastError(wxT("SelectObject"));
1467 // this will create a monochrome bitmap with 0 points for the pixels
1468 // which have the same value as the background colour and 1 for the
1470 ::SetBkColor(srcDC
, maskColour
);
1471 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1474 ::SelectObject(srcDC
, hbmpSrcOld
);
1476 ::SelectObject(destDC
, hbmpDstOld
);
1480 #else // __WXMICROWIN__
1481 wxUnusedVar(bitmap
);
1482 wxUnusedVar(colour
);
1484 #endif // __WXMICROWIN__/!__WXMICROWIN__
1487 // ----------------------------------------------------------------------------
1489 // ----------------------------------------------------------------------------
1491 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1494 int width
, int height
, int depth
)
1496 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1498 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : false;
1501 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1502 const wxString
& name
,
1504 int width
, int height
)
1506 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1508 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : false;
1511 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1512 const wxString
& name
,
1515 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1517 return bitmap
? SaveFile(bitmap
, name
, type
) : false;
1520 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1521 void *WXUNUSED(data
),
1522 long WXUNUSED(type
),
1523 int WXUNUSED(width
),
1524 int WXUNUSED(height
),
1525 int WXUNUSED(depth
))
1530 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1531 const wxString
& WXUNUSED(name
),
1532 long WXUNUSED(type
),
1533 int WXUNUSED(desiredWidth
),
1534 int WXUNUSED(desiredHeight
))
1539 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1540 const wxString
& WXUNUSED(name
),
1542 const wxPalette
*WXUNUSED(palette
))
1547 // ----------------------------------------------------------------------------
1549 // ----------------------------------------------------------------------------
1551 #ifndef __WXMICROWIN__
1552 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1553 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1555 unsigned long i
, headerSize
;
1557 // Allocate space for a DIB header
1558 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1559 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1560 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1562 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1564 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1566 // Fill in the static parts of the DIB header
1567 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1568 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1569 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1570 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1572 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1573 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1574 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1575 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1576 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1579 // Initialize the DIB palette
1580 for (i
= 0; i
< 256; i
++) {
1581 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1582 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1583 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1584 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1587 *lpDIBHeader
= lpDIBheader
;
1592 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1598 // ----------------------------------------------------------------------------
1599 // global helper functions implemented here
1600 // ----------------------------------------------------------------------------
1602 // helper of wxBitmapToHICON/HCURSOR
1604 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1611 // we can't create an icon/cursor form nothing
1615 wxMask
*mask
= bmp
.GetMask();
1618 // we must have a mask for an icon, so even if it's probably incorrect,
1619 // do create it (grey is the "standard" transparent colour)
1620 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1624 wxZeroMemory(iconInfo
);
1625 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1628 iconInfo
.xHotspot
= hotSpotX
;
1629 iconInfo
.yHotspot
= hotSpotY
;
1632 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1633 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1635 // black out the transparent area to preserve background colour, because
1636 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1637 // the mask to the dest rect.
1639 MemoryHDC dcSrc
, dcDst
;
1640 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1641 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1643 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1644 dcSrc
, 0, 0, SRCAND
) )
1646 wxLogLastError(_T("BitBlt"));
1650 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1652 if ( !bmp
.GetMask() )
1654 // we created the mask, now delete it
1658 // delete the inverted mask bitmap we created as well
1659 ::DeleteObject(iconInfo
.hbmMask
);
1664 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1666 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1669 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1671 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1674 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1676 #ifndef __WXMICROWIN__
1677 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1679 // get width/height from the bitmap if not given
1683 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1688 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1689 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1690 if ( !hdcSrc
|| !hdcDst
)
1692 wxLogLastError(wxT("CreateCompatibleDC"));
1695 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1698 wxLogLastError(wxT("CreateBitmap"));
1701 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1702 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1703 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1707 wxLogLastError(wxT("BitBlt"));
1711 SelectObject(hdcSrc
,srcTmp
);
1712 SelectObject(hdcDst
,dstTmp
);