1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
46 #if !defined(__WXMICROWIN__)
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 #include "wx/rawbmp.h"
55 // missing from mingw32 header
57 #define CLR_INVALID ((COLORREF)-1)
58 #endif // no CLR_INVALID
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
68 virtual ~wxBitmapRefData() { Free(); }
72 // set the mask object to use as the mask, we take ownership of it
73 void SetMask(wxMask
*mask
)
79 // set the HBITMAP to use as the mask
80 void SetMask(HBITMAP hbmpMask
)
82 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
86 wxMask
*GetMask() const { return m_bitmapMask
; }
90 wxPalette m_bitmapPalette
;
91 #endif // wxUSE_PALETTE
97 // this field is solely for error checking: we detect selecting a bitmap
98 // into more than one DC at once or deleting a bitmap still selected into a
99 // DC (both are serious programming errors under Windows)
100 wxDC
*m_selectedInto
;
101 #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
108 // true if we have alpha transparency info and can be drawn using
112 // true if our HBITMAP is a DIB section, false if it is a DDB
116 // optional mask for transparent drawing
117 wxMask
*m_bitmapMask
;
119 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
127 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
129 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 // decide whether we should create a DIB or a DDB for the given parameters
140 static bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
142 // here is the logic:
144 // (a) if hdc is specified, the caller explicitly wants DDB
145 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
147 // (c) finally, create DIBs under Win9x even if the depth hasn't been
148 // explicitly specified but the current display depth is 24 or more
149 // and the image is "big", i.e. > 16Mb which is the theoretical limit
150 // for DDBs under Win9x
152 // consequences (all of which seem to make sense):
154 // (i) by default, DDBs are created (depth == -1 usually)
155 // (ii) DIBs can be created by explicitly specifying the depth
156 // (iii) using a DC always forces creating a DDB
160 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 wxBitmapRefData::wxBitmapRefData()
170 m_selectedInto
= NULL
;
174 m_hBitmap
= (WXHBITMAP
) NULL
;
181 void wxBitmapRefData::Free()
183 wxASSERT_MSG( !m_selectedInto
,
184 wxT("deleting bitmap still selected into wxMemoryDC") );
186 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
190 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
192 wxLogLastError(wxT("DeleteObject(hbitmap)"));
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // this function should be called from all wxBitmap ctors
205 void wxBitmap::Init()
207 // m_refData = NULL; done in the base class ctor
210 wxGDIImageRefData
*wxBitmap::CreateData() const
212 return new wxBitmapRefData
;
217 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
219 #ifndef __WXMICROWIN__
220 // it may be either HICON or HCURSOR
221 HICON hicon
= (HICON
)icon
.GetHandle();
224 if ( !::GetIconInfo(hicon
, &iconInfo
) )
226 wxLogLastError(wxT("GetIconInfo"));
231 wxBitmapRefData
*refData
= new wxBitmapRefData
;
234 int w
= icon
.GetWidth(),
235 h
= icon
.GetHeight();
237 refData
->m_width
= w
;
238 refData
->m_height
= h
;
239 refData
->m_depth
= wxDisplayDepth();
241 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
243 // the mask returned by GetIconInfo() is inversed compared to the usual
245 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
248 // delete the old one now as we don't need it any more
249 ::DeleteObject(iconInfo
.hbmMask
);
251 #if WXWIN_COMPATIBILITY_2
252 refData
->m_ok
= TRUE
;
253 #endif // WXWIN_COMPATIBILITY_2
263 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
271 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
275 return CopyFromIconOrCursor(cursor
);
279 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
286 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
287 // to create a bitmap from icon there - but using this way we won't have
290 int width
= icon
.GetWidth(),
291 height
= icon
.GetHeight();
293 // copy the icon to the bitmap
295 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
296 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
297 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
299 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
301 ::SelectObject(hdc
, hbmpOld
);
304 wxBitmapRefData
*refData
= new wxBitmapRefData
;
307 refData
->m_width
= width
;
308 refData
->m_height
= height
;
309 refData
->m_depth
= wxDisplayDepth();
311 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
313 #if WXWIN_COMPATIBILITY_2
314 refData
->m_ok
= TRUE
;
315 #endif // WXWIN_COMPATIBILITY_2
319 return CopyFromIconOrCursor(icon
);
320 #endif // Win16/Win32
323 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
325 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
327 HBITMAP hbitmap
= dib
.CreateDDB();
333 wxBitmapRefData
*refData
= new wxBitmapRefData
;
336 refData
->m_width
= dib
.GetWidth();
337 refData
->m_height
= dib
.GetHeight();
338 refData
->m_depth
= dib
.GetDepth();
340 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
343 wxPalette
*palette
= dib
.CreatePalette();
346 refData
->m_bitmapPalette
= *palette
;
350 #endif // wxUSE_PALETTE
355 wxBitmap::~wxBitmap()
359 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
363 #ifndef __WXMICROWIN__
364 wxBitmapRefData
*refData
= new wxBitmapRefData
;
367 refData
->m_width
= width
;
368 refData
->m_height
= height
;
369 refData
->m_depth
= depth
;
374 // we assume that it is in XBM format which is not quite the same as
375 // the format CreateBitmap() wants because the order of bytes in the
377 const size_t bytesPerLine
= (width
+ 7) / 8;
378 const size_t padding
= bytesPerLine
% 2;
379 const size_t len
= height
* ( padding
+ bytesPerLine
);
380 data
= (char *)malloc(len
);
381 const char *src
= bits
;
384 for ( int rows
= 0; rows
< height
; rows
++ )
386 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
388 unsigned char val
= *src
++;
389 unsigned char reversed
= 0;
391 for ( int bits
= 0; bits
< 8; bits
++)
394 reversed
|= (val
& 0x01);
406 // bits should already be in Windows standard format
407 data
= (char *)bits
; // const_cast is harmless
410 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
413 wxLogLastError(wxT("CreateBitmap"));
421 SetHBITMAP((WXHBITMAP
)hbmp
);
425 // Create from XPM data
426 bool wxBitmap::CreateFromXpm(const char **data
)
428 #if wxUSE_IMAGE && wxUSE_XPM
431 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
433 wxXPMDecoder decoder
;
434 wxImage img
= decoder
.ReadData(data
);
435 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
437 *this = wxBitmap(img
);
444 wxBitmap::wxBitmap(int w
, int h
, int d
)
448 (void)Create(w
, h
, d
);
451 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
455 (void)Create(w
, h
, dc
);
458 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
462 (void)Create(data
, type
, width
, height
, depth
);
465 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
469 LoadFile(filename
, (int)type
);
472 bool wxBitmap::Create(int width
, int height
, int depth
)
474 return DoCreate(width
, height
, depth
, 0);
477 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
479 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
481 return DoCreate(width
, height
, -1, dc
.GetHDC());
484 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
488 m_refData
= new wxBitmapRefData
;
490 GetBitmapData()->m_width
= w
;
491 GetBitmapData()->m_height
= h
;
495 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
499 // create DIBs without alpha channel by default
507 // don't delete the DIB section in dib object dtor
510 GetBitmapData()->m_isDIB
= TRUE
;
511 GetBitmapData()->m_depth
= d
;
516 d
= wxDisplayDepth();
518 GetBitmapData()->m_depth
= d
;
520 #ifndef __WXMICROWIN__
523 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
526 wxLogLastError(wxT("CreateBitmap"));
530 #endif // !__WXMICROWIN__
533 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
536 wxLogLastError(wxT("CreateCompatibleBitmap"));
539 GetBitmapData()->m_depth
= wxDisplayDepth();
543 SetHBITMAP((WXHBITMAP
)hbmp
);
545 #if WXWIN_COMPATIBILITY_2
546 GetBitmapData()->m_ok
= hbmp
!= 0;
547 #endif // WXWIN_COMPATIBILITY_2
554 // ----------------------------------------------------------------------------
555 // wxImage to/from conversions for Microwin
556 // ----------------------------------------------------------------------------
558 // Microwin versions are so different from normal ones that it really doesn't
559 // make sense to use #ifdefs inside the function bodies
560 #ifdef __WXMICROWIN__
562 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
564 // Set this to 1 to experiment with mask code,
565 // which currently doesn't work
568 m_refData
= new wxBitmapRefData();
570 // Initial attempt at a simple-minded implementation.
571 // The bitmap will always be created at the screen depth,
572 // so the 'depth' argument is ignored.
574 HDC hScreenDC
= ::GetDC(NULL
);
575 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
577 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
578 HBITMAP hMaskBitmap
= NULL
;
579 HBITMAP hOldMaskBitmap
= NULL
;
581 unsigned char maskR
= 0;
582 unsigned char maskG
= 0;
583 unsigned char maskB
= 0;
585 // printf("Created bitmap %d\n", (int) hBitmap);
588 ::ReleaseDC(NULL
, hScreenDC
);
591 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
593 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
594 ::ReleaseDC(NULL
, hScreenDC
);
596 // created an mono-bitmap for the possible mask
597 bool hasMask
= image
.HasMask();
602 // FIXME: we should be able to pass bpp = 1, but
603 // GdBlit can't handle a different depth
605 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
607 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
609 maskR
= image
.GetMaskRed();
610 maskG
= image
.GetMaskGreen();
611 maskB
= image
.GetMaskBlue();
619 hScreenDC
= ::GetDC(NULL
);
620 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
621 ::ReleaseDC(NULL
, hScreenDC
);
623 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
631 for (i
= 0; i
< image
.GetWidth(); i
++)
633 for (j
= 0; j
< image
.GetHeight(); j
++)
635 unsigned char red
= image
.GetRed(i
, j
);
636 unsigned char green
= image
.GetGreen(i
, j
);
637 unsigned char blue
= image
.GetBlue(i
, j
);
639 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
643 // scan the bitmap for the transparent colour and set the corresponding
644 // pixels in the mask to BLACK and the rest to WHITE
645 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
646 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
648 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
653 ::SelectObject(hMemDC
, hOldBitmap
);
657 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
660 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
663 SetWidth(image
.GetWidth());
664 SetHeight(image
.GetHeight());
665 SetDepth(screenDepth
);
666 SetHBITMAP( (WXHBITMAP
) hBitmap
);
669 // Copy the palette from the source image
670 SetPalette(image
.GetPalette());
671 #endif // wxUSE_PALETTE
673 #if WXWIN_COMPATIBILITY_2
674 // check the wxBitmap object
675 GetBitmapData()->SetOk();
676 #endif // WXWIN_COMPATIBILITY_2
681 wxImage
wxBitmap::ConvertToImage() const
683 // Initial attempt at a simple-minded implementation.
684 // The bitmap will always be created at the screen depth,
685 // so the 'depth' argument is ignored.
686 // TODO: transparency (create a mask image)
690 wxFAIL_MSG( wxT("bitmap is invalid") );
696 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
698 // create an wxImage object
699 int width
= GetWidth();
700 int height
= GetHeight();
701 image
.Create( width
, height
);
702 unsigned char *data
= image
.GetData();
705 wxFAIL_MSG( wxT("could not allocate data for image") );
709 HDC hScreenDC
= ::GetDC(NULL
);
711 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
712 ::ReleaseDC(NULL
, hScreenDC
);
714 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
716 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
719 for (i
= 0; i
< GetWidth(); i
++)
721 for (j
= 0; j
< GetHeight(); j
++)
723 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
724 unsigned char red
= GetRValue(color
);
725 unsigned char green
= GetGValue(color
);
726 unsigned char blue
= GetBValue(color
);
728 image
.SetRGB(i
, j
, red
, green
, blue
);
732 ::SelectObject(hMemDC
, hOldBitmap
);
736 // Copy the palette from the source image
738 image
.SetPalette(* GetPalette());
739 #endif // wxUSE_PALETTE
744 #endif // __WXMICROWIN__
746 // ----------------------------------------------------------------------------
747 // wxImage to/from conversions
748 // ----------------------------------------------------------------------------
750 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
752 return CreateFromImage(image
, depth
, 0);
755 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
757 wxCHECK_MSG( dc
.Ok(), FALSE
,
758 _T("invalid HDC in wxBitmap::CreateFromImage()") );
760 return CreateFromImage(image
, -1, dc
.GetHDC());
763 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
765 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
769 // first convert the image to DIB
770 const int h
= image
.GetHeight();
771 const int w
= image
.GetWidth();
778 // store the bitmap parameters
779 wxBitmapRefData
*refData
= new wxBitmapRefData
;
780 refData
->m_width
= w
;
781 refData
->m_height
= h
;
782 refData
->m_hasAlpha
= image
.HasAlpha();
787 // next either store DIB as is or create a DDB from it
790 // are we going to use DIB?
791 if ( wxShouldCreateDIB(w
, h
, depth
, hdc
) )
793 // don't delete the DIB section in dib object dtor
794 hbitmap
= dib
.Detach();
796 refData
->m_isDIB
= TRUE
;
797 refData
->m_depth
= dib
.GetDepth();
799 else // we need to convert DIB to DDB
801 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
803 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
806 // validate this object
807 SetHBITMAP((WXHBITMAP
)hbitmap
);
809 #if WXWIN_COMPATIBILITY_2
810 m_refData
->m_ok
= TRUE
;
811 #endif // WXWIN_COMPATIBILITY_2
813 // finally also set the mask if we have one
814 if ( image
.HasMask() )
816 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
817 image
.GetMaskGreen(),
818 image
.GetMaskBlue())));
824 wxImage
wxBitmap::ConvertToImage() const
828 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
830 // create an wxImage object
831 int width
= GetWidth();
832 int height
= GetHeight();
833 image
.Create( width
, height
);
834 unsigned char *data
= image
.GetData();
837 wxFAIL_MSG( wxT("could not allocate data for image") );
841 // calc the number of bytes per scanline and padding in the DIB
842 int bytePerLine
= width
*3;
843 int sizeDWORD
= sizeof( DWORD
);
844 int lineBoundary
= bytePerLine
% sizeDWORD
;
846 if( lineBoundary
> 0 )
848 padding
= sizeDWORD
- lineBoundary
;
849 bytePerLine
+= padding
;
852 // create a DIB header
853 int headersize
= sizeof(BITMAPINFOHEADER
);
854 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
857 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
861 // Fill in the DIB header
862 lpDIBh
->bmiHeader
.biSize
= headersize
;
863 lpDIBh
->bmiHeader
.biWidth
= width
;
864 lpDIBh
->bmiHeader
.biHeight
= -height
;
865 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
866 lpDIBh
->bmiHeader
.biPlanes
= 1;
867 lpDIBh
->bmiHeader
.biBitCount
= 24;
868 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
869 lpDIBh
->bmiHeader
.biClrUsed
= 0;
870 // These seem not really needed for our purpose here.
871 lpDIBh
->bmiHeader
.biClrImportant
= 0;
872 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
873 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
874 // memory for DIB data
875 unsigned char *lpBits
;
876 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
879 wxFAIL_MSG( wxT("could not allocate data for DIB") );
885 // copy data from the device-dependent bitmap to the DIB
886 HDC hdc
= ::GetDC(NULL
);
888 hbitmap
= (HBITMAP
) GetHBITMAP();
889 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
891 // copy DIB data into the wxImage object
893 unsigned char *ptdata
= data
;
894 unsigned char *ptbits
= lpBits
;
895 for( i
=0; i
<height
; i
++ )
897 for( j
=0; j
<width
; j
++ )
899 *(ptdata
++) = *(ptbits
+2);
900 *(ptdata
++) = *(ptbits
+1);
901 *(ptdata
++) = *(ptbits
);
907 // similarly, set data according to the possible mask bitmap
908 if( GetMask() && GetMask()->GetMaskBitmap() )
910 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
911 // memory DC created, color set, data copied, and memory DC deleted
912 HDC memdc
= ::CreateCompatibleDC( hdc
);
913 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
914 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
915 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
917 // background color set to RGB(16,16,16) in consistent with wxGTK
918 unsigned char r
=16, g
=16, b
=16;
921 for( i
=0; i
<height
; i
++ )
923 for( j
=0; j
<width
; j
++ )
937 image
.SetMaskColour( r
, g
, b
);
938 image
.SetMask( TRUE
);
942 image
.SetMask( FALSE
);
944 // free allocated resources
945 ::ReleaseDC(NULL
, hdc
);
952 #endif // wxUSE_IMAGE
954 // ----------------------------------------------------------------------------
955 // loading and saving bitmaps
956 // ----------------------------------------------------------------------------
958 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
962 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
966 m_refData
= new wxBitmapRefData
;
968 return handler
->LoadFile(this, filename
, type
, -1, -1);
974 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
976 *this = wxBitmap(image
);
981 #endif // wxUSE_IMAGE
986 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
990 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
994 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
999 m_refData
= new wxBitmapRefData
;
1001 return handler
->Create(this, data
, type
, width
, height
, depth
);
1004 bool wxBitmap::SaveFile(const wxString
& filename
,
1006 const wxPalette
*palette
)
1008 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1012 return handler
->SaveFile(this, filename
, type
, palette
);
1017 // FIXME what about palette? shouldn't we use it?
1018 wxImage image
= ConvertToImage();
1021 return image
.SaveFile(filename
, type
);
1024 #endif // wxUSE_IMAGE
1029 // ----------------------------------------------------------------------------
1030 // sub bitmap extraction
1031 // ----------------------------------------------------------------------------
1033 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1035 wxCHECK_MSG( Ok() &&
1036 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1037 (rect
.x
+rect
.width
<= GetWidth()) &&
1038 (rect
.y
+rect
.height
<= GetHeight()),
1039 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1041 wxBitmap
ret( rect
.width
, rect
.height
);
1042 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1044 #ifndef __WXMICROWIN__
1045 // TODO: copy alpha channel data if any
1052 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1053 selectDst(dcDst
, GetHbitmapOf(ret
));
1055 if ( !selectSrc
|| !selectDst
)
1057 wxLogLastError(_T("SelectObjct(hBitmap)"));
1060 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1061 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1063 wxLogLastError(_T("BitBlt"));
1067 // copy mask if there is one
1070 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1072 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1073 selectDst(dcDst
, hbmpMask
);
1075 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1076 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1078 wxLogLastError(_T("BitBlt"));
1081 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1084 #endif // !__WXMICROWIN__
1089 // ----------------------------------------------------------------------------
1090 // wxBitmap accessors
1091 // ----------------------------------------------------------------------------
1093 wxPalette
* wxBitmap::GetPalette() const
1095 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1096 : (wxPalette
*) NULL
;
1099 wxMask
*wxBitmap::GetMask() const
1101 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1106 wxDC
*wxBitmap::GetSelectedInto() const
1108 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1113 #if WXWIN_COMPATIBILITY_2_4
1115 int wxBitmap::GetQuality() const
1120 #endif // WXWIN_COMPATIBILITY_2_4
1122 void wxBitmap::UseAlpha()
1124 if ( GetBitmapData() )
1125 GetBitmapData()->m_hasAlpha
= true;
1128 bool wxBitmap::HasAlpha() const
1130 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1133 // ----------------------------------------------------------------------------
1135 // ----------------------------------------------------------------------------
1139 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1141 if ( GetBitmapData() )
1142 GetBitmapData()->m_selectedInto
= dc
;
1149 void wxBitmap::SetPalette(const wxPalette
& palette
)
1153 GetBitmapData()->m_bitmapPalette
= palette
;
1156 #endif // wxUSE_PALETTE
1158 void wxBitmap::SetMask(wxMask
*mask
)
1162 GetBitmapData()->SetMask(mask
);
1165 #if WXWIN_COMPATIBILITY_2
1167 void wxBitmap::SetOk(bool isOk
)
1171 GetBitmapData()->m_ok
= isOk
;
1174 #endif // WXWIN_COMPATIBILITY_2
1176 #if WXWIN_COMPATIBILITY_2_4
1178 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1182 #endif // WXWIN_COMPATIBILITY_2_4
1184 // ----------------------------------------------------------------------------
1185 // raw bitmap access support
1186 // ----------------------------------------------------------------------------
1188 bool wxBitmap::GetRawData(wxRawBitmapData
*data
)
1190 wxCHECK_MSG( data
, FALSE
, _T("NULL pointer in wxBitmap::GetRawData") );
1194 // no bitmap, no data (raw or otherwise)
1198 // if we're already a DIB we can access our data directly, but if not we
1199 // need to convert this DDB to a DIB section and use it for raw access and
1200 // then convert it back
1202 if ( !GetBitmapData()->m_isDIB
)
1204 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1205 _T("GetRawData() may be called only once") );
1207 wxDIB
*dib
= new wxDIB(*this);
1215 // we'll free it in UngetRawData()
1216 GetBitmapData()->m_dib
= dib
;
1218 hDIB
= dib
->GetHandle();
1222 hDIB
= GetHbitmap();
1226 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1228 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1233 // ok, store the relevant info in wxRawBitmapData
1234 const LONG h
= ds
.dsBm
.bmHeight
;
1236 data
->m_width
= ds
.dsBm
.bmWidth
;
1238 data
->m_bypp
= ds
.dsBm
.bmBitsPixel
/ 8;
1240 // remember that DIBs are stored in top to bottom order!
1241 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1242 data
->m_stride
= -bytesPerRow
;
1243 data
->m_pixels
= (unsigned char *)ds
.dsBm
.bmBits
;
1246 data
->m_pixels
+= (h
- 1)*bytesPerRow
;
1252 void wxBitmap::UngetRawData(wxRawBitmapData
*data
)
1254 wxCHECK_RET( data
, _T("NULL pointer in wxBitmap::UngetRawData()") );
1261 // invalid data, don't crash -- but don't assert neither as we're
1262 // called automatically from wxRawBitmapData dtor and so there is no
1263 // way to prevent this from happening
1267 // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap
1268 // API uses normal, not premultiplied, colours, so adjust them here now
1269 wxRawBitmapIterator
p(*data
);
1271 const int w
= data
->GetWidth();
1272 const int h
= data
->GetHeight();
1274 for ( int y
= 0; y
< h
; y
++ )
1276 wxRawBitmapIterator rowStart
= p
;
1278 for ( int x
= 0; x
< w
; x
++ )
1280 const unsigned alpha
= p
.Alpha();
1282 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1283 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1284 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1293 // if we're a DDB we need to convert DIB back to DDB now to make the
1294 // changes made via wxRawBitmapData effective
1295 if ( !GetBitmapData()->m_isDIB
)
1297 wxDIB
*dib
= GetBitmapData()->m_dib
;
1298 GetBitmapData()->m_dib
= NULL
;
1306 // ----------------------------------------------------------------------------
1308 // ----------------------------------------------------------------------------
1315 // Construct a mask from a bitmap and a colour indicating
1316 // the transparent area
1317 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1320 Create(bitmap
, colour
);
1323 // Construct a mask from a bitmap and a palette index indicating
1324 // the transparent area
1325 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1328 Create(bitmap
, paletteIndex
);
1331 // Construct a mask from a mono bitmap (copies the bitmap).
1332 wxMask::wxMask(const wxBitmap
& bitmap
)
1341 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1344 // Create a mask from a mono bitmap (copies the bitmap).
1345 bool wxMask::Create(const wxBitmap
& bitmap
)
1347 #ifndef __WXMICROWIN__
1348 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1349 _T("can't create mask from invalid or not monochrome bitmap") );
1353 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1357 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1362 HDC srcDC
= CreateCompatibleDC(0);
1363 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1364 HDC destDC
= CreateCompatibleDC(0);
1365 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1366 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1367 SelectObject(srcDC
, 0);
1369 SelectObject(destDC
, 0);
1377 // Create a mask from a bitmap and a palette index indicating
1378 // the transparent area
1379 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1383 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1388 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1390 unsigned char red
, green
, blue
;
1391 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1393 wxColour
transparentColour(red
, green
, blue
);
1394 return Create(bitmap
, transparentColour
);
1397 #endif // wxUSE_PALETTE
1402 // Create a mask from a bitmap and a colour indicating
1403 // the transparent area
1404 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1406 #ifndef __WXMICROWIN__
1407 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1411 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1415 int width
= bitmap
.GetWidth(),
1416 height
= bitmap
.GetHeight();
1418 // scan the bitmap for the transparent colour and set the corresponding
1419 // pixels in the mask to BLACK and the rest to WHITE
1420 COLORREF maskColour
= wxColourToPalRGB(colour
);
1421 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1423 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1424 HDC destDC
= ::CreateCompatibleDC(NULL
);
1425 if ( !srcDC
|| !destDC
)
1427 wxLogLastError(wxT("CreateCompatibleDC"));
1432 // SelectObject() will fail
1433 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1434 _T("bitmap can't be selected in another DC") );
1436 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1439 wxLogLastError(wxT("SelectObject"));
1444 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1447 wxLogLastError(wxT("SelectObject"));
1454 // this will create a monochrome bitmap with 0 points for the pixels
1455 // which have the same value as the background colour and 1 for the
1457 ::SetBkColor(srcDC
, maskColour
);
1458 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1461 ::SelectObject(srcDC
, hbmpSrcOld
);
1463 ::SelectObject(destDC
, hbmpDstOld
);
1467 #else // __WXMICROWIN__
1469 #endif // __WXMICROWIN__/!__WXMICROWIN__
1472 // ----------------------------------------------------------------------------
1474 // ----------------------------------------------------------------------------
1476 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1479 int width
, int height
, int depth
)
1481 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1483 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1486 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1487 const wxString
& name
,
1489 int width
, int height
)
1491 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1493 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1496 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1497 const wxString
& name
,
1500 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1502 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1505 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1506 void *WXUNUSED(data
),
1507 long WXUNUSED(type
),
1508 int WXUNUSED(width
),
1509 int WXUNUSED(height
),
1510 int WXUNUSED(depth
))
1515 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1516 const wxString
& WXUNUSED(name
),
1517 long WXUNUSED(type
),
1518 int WXUNUSED(desiredWidth
),
1519 int WXUNUSED(desiredHeight
))
1524 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1525 const wxString
& WXUNUSED(name
),
1527 const wxPalette
*WXUNUSED(palette
))
1532 // ----------------------------------------------------------------------------
1534 // ----------------------------------------------------------------------------
1536 #ifndef __WXMICROWIN__
1537 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1538 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1540 unsigned long i
, headerSize
;
1541 LPBITMAPINFO lpDIBheader
= NULL
;
1542 LPPALETTEENTRY lpPe
= NULL
;
1545 // Allocate space for a DIB header
1546 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1547 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1548 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1550 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1552 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1554 // Fill in the static parts of the DIB header
1555 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1556 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1557 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1558 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1560 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1561 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1562 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1563 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1564 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1567 // Initialize the DIB palette
1568 for (i
= 0; i
< 256; i
++) {
1569 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1570 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1571 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1572 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1575 *lpDIBHeader
= lpDIBheader
;
1580 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1586 // ----------------------------------------------------------------------------
1587 // global helper functions implemented here
1588 // ----------------------------------------------------------------------------
1590 // helper of wxBitmapToHICON/HCURSOR
1592 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1599 // we can't create an icon/cursor form nothing
1603 wxMask
*mask
= bmp
.GetMask();
1606 // we must have a mask for an icon, so even if it's probably incorrect,
1607 // do create it (grey is the "standard" transparent colour)
1608 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1612 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1615 iconInfo
.xHotspot
= hotSpotX
;
1616 iconInfo
.yHotspot
= hotSpotY
;
1619 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1620 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1622 // black out the transparent area to preserve background colour, because
1623 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1624 // the mask to the dest rect.
1626 MemoryHDC dcSrc
, dcDst
;
1627 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1628 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1630 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1631 dcSrc
, 0, 0, SRCAND
) )
1633 wxLogLastError(_T("BitBlt"));
1637 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1639 if ( !bmp
.GetMask() )
1641 // we created the mask, now delete it
1645 // delete the inverted mask bitmap we created as well
1646 ::DeleteObject(iconInfo
.hbmMask
);
1651 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1653 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1656 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1658 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1661 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1663 #ifndef __WXMICROWIN__
1664 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1666 // get width/height from the bitmap if not given
1670 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1675 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1676 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1677 if ( !hdcSrc
|| !hdcDst
)
1679 wxLogLastError(wxT("CreateCompatibleDC"));
1682 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1685 wxLogLastError(wxT("CreateBitmap"));
1688 ::SelectObject(hdcSrc
, hbmpMask
);
1689 ::SelectObject(hdcDst
, hbmpInvMask
);
1690 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1694 wxLogLastError(wxT("BitBlt"));