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 // true if we have alpha transparency info and can be drawn using
108 // optional mask for transparent drawing
109 wxMask
*m_bitmapMask
;
111 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
119 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
121 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 // decide whether we should create a DIB or a DDB for the given parameters
132 static bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
134 // here is the logic:
136 // (a) if hdc is specified, the caller explicitly wants DDB
137 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
139 // (c) finally, create DIBs under Win9x even if the depth hasn't been
140 // explicitly specified but the current display depth is 24 or more
141 // and the image is "big", i.e. > 16Mb which is the theoretical limit
142 // for DDBs under Win9x
144 // consequences (all of which seem to make sense):
146 // (i) by default, DDBs are created (depth == -1 usually)
147 // (ii) DIBs can be created by explicitly specifying the depth
148 // (iii) using a DC always forces creating a DDB
152 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 wxBitmapRefData::wxBitmapRefData()
162 m_selectedInto
= NULL
;
165 m_hBitmap
= (WXHBITMAP
) NULL
;
169 void wxBitmapRefData::Free()
171 wxASSERT_MSG( !m_selectedInto
,
172 wxT("deleting bitmap still selected into wxMemoryDC") );
176 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
178 wxLogLastError(wxT("DeleteObject(hbitmap)"));
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 // this function should be called from all wxBitmap ctors
191 void wxBitmap::Init()
193 // m_refData = NULL; done in the base class ctor
196 wxGDIImageRefData
*wxBitmap::CreateData() const
198 return new wxBitmapRefData
;
203 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
205 #ifndef __WXMICROWIN__
206 // it may be either HICON or HCURSOR
207 HICON hicon
= (HICON
)icon
.GetHandle();
210 if ( !::GetIconInfo(hicon
, &iconInfo
) )
212 wxLogLastError(wxT("GetIconInfo"));
217 wxBitmapRefData
*refData
= new wxBitmapRefData
;
220 int w
= icon
.GetWidth(),
221 h
= icon
.GetHeight();
223 refData
->m_width
= w
;
224 refData
->m_height
= h
;
225 refData
->m_depth
= wxDisplayDepth();
227 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
229 // the mask returned by GetIconInfo() is inversed compared to the usual
231 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
234 // delete the old one now as we don't need it any more
235 ::DeleteObject(iconInfo
.hbmMask
);
237 #if WXWIN_COMPATIBILITY_2
238 refData
->m_ok
= TRUE
;
239 #endif // WXWIN_COMPATIBILITY_2
249 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
257 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
261 return CopyFromIconOrCursor(cursor
);
265 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
272 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
273 // to create a bitmap from icon there - but using this way we won't have
276 int width
= icon
.GetWidth(),
277 height
= icon
.GetHeight();
279 // copy the icon to the bitmap
281 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
282 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
283 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
285 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
287 ::SelectObject(hdc
, hbmpOld
);
290 wxBitmapRefData
*refData
= new wxBitmapRefData
;
293 refData
->m_width
= width
;
294 refData
->m_height
= height
;
295 refData
->m_depth
= wxDisplayDepth();
297 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
299 #if WXWIN_COMPATIBILITY_2
300 refData
->m_ok
= TRUE
;
301 #endif // WXWIN_COMPATIBILITY_2
305 return CopyFromIconOrCursor(icon
);
306 #endif // Win16/Win32
309 wxBitmap::~wxBitmap()
313 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
317 #ifndef __WXMICROWIN__
318 wxBitmapRefData
*refData
= new wxBitmapRefData
;
321 refData
->m_width
= width
;
322 refData
->m_height
= height
;
323 refData
->m_depth
= depth
;
328 // we assume that it is in XBM format which is not quite the same as
329 // the format CreateBitmap() wants because the order of bytes in the
331 const size_t bytesPerLine
= (width
+ 7) / 8;
332 const size_t padding
= bytesPerLine
% 2;
333 const size_t len
= height
* ( padding
+ bytesPerLine
);
334 data
= (char *)malloc(len
);
335 const char *src
= bits
;
338 for ( int rows
= 0; rows
< height
; rows
++ )
340 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
342 unsigned char val
= *src
++;
343 unsigned char reversed
= 0;
345 for ( int bits
= 0; bits
< 8; bits
++)
348 reversed
|= (val
& 0x01);
360 // bits should already be in Windows standard format
361 data
= (char *)bits
; // const_cast is harmless
364 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
367 wxLogLastError(wxT("CreateBitmap"));
375 SetHBITMAP((WXHBITMAP
)hbmp
);
379 // Create from XPM data
380 bool wxBitmap::CreateFromXpm(const char **data
)
382 #if wxUSE_IMAGE && wxUSE_XPM
385 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
387 wxXPMDecoder decoder
;
388 wxImage img
= decoder
.ReadData(data
);
389 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
391 *this = wxBitmap(img
);
398 wxBitmap::wxBitmap(int w
, int h
, int d
)
402 (void)Create(w
, h
, d
);
405 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
409 (void)Create(data
, type
, width
, height
, depth
);
412 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
416 LoadFile(filename
, (int)type
);
419 bool wxBitmap::Create(int width
, int height
, int depth
)
421 return DoCreate(width
, height
, depth
, 0);
424 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
426 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
428 return DoCreate(width
, height
, -1, dc
.GetHDC());
431 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
435 m_refData
= new wxBitmapRefData
;
437 GetBitmapData()->m_width
= w
;
438 GetBitmapData()->m_height
= h
;
442 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
446 // create DIBs without alpha channel by default
454 // don't delete the DIB section in dib object dtor
457 GetBitmapData()->m_depth
= d
;
458 GetBitmapData()->m_hasAlpha
= d
== 32; // 32bpp DIBs have alpha channel
463 d
= wxDisplayDepth();
465 GetBitmapData()->m_depth
= d
;
467 #ifndef __WXMICROWIN__
470 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
473 wxLogLastError(wxT("CreateBitmap"));
477 #endif // !__WXMICROWIN__
480 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
483 wxLogLastError(wxT("CreateCompatibleBitmap"));
486 GetBitmapData()->m_depth
= wxDisplayDepth();
490 SetHBITMAP((WXHBITMAP
)hbmp
);
492 #if WXWIN_COMPATIBILITY_2
493 GetBitmapData()->m_ok
= hbmp
!= 0;
494 #endif // WXWIN_COMPATIBILITY_2
501 // ----------------------------------------------------------------------------
502 // wxImage to/from conversions for Microwin
503 // ----------------------------------------------------------------------------
505 // Microwin versions are so different from normal ones that it really doesn't
506 // make sense to use #ifdefs inside the function bodies
507 #ifdef __WXMICROWIN__
509 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
511 // Set this to 1 to experiment with mask code,
512 // which currently doesn't work
515 m_refData
= new wxBitmapRefData();
517 // Initial attempt at a simple-minded implementation.
518 // The bitmap will always be created at the screen depth,
519 // so the 'depth' argument is ignored.
521 HDC hScreenDC
= ::GetDC(NULL
);
522 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
524 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
525 HBITMAP hMaskBitmap
= NULL
;
526 HBITMAP hOldMaskBitmap
= NULL
;
528 unsigned char maskR
= 0;
529 unsigned char maskG
= 0;
530 unsigned char maskB
= 0;
532 // printf("Created bitmap %d\n", (int) hBitmap);
535 ::ReleaseDC(NULL
, hScreenDC
);
538 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
540 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
541 ::ReleaseDC(NULL
, hScreenDC
);
543 // created an mono-bitmap for the possible mask
544 bool hasMask
= image
.HasMask();
549 // FIXME: we should be able to pass bpp = 1, but
550 // GdBlit can't handle a different depth
552 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
554 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
556 maskR
= image
.GetMaskRed();
557 maskG
= image
.GetMaskGreen();
558 maskB
= image
.GetMaskBlue();
566 hScreenDC
= ::GetDC(NULL
);
567 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
568 ::ReleaseDC(NULL
, hScreenDC
);
570 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
578 for (i
= 0; i
< image
.GetWidth(); i
++)
580 for (j
= 0; j
< image
.GetHeight(); j
++)
582 unsigned char red
= image
.GetRed(i
, j
);
583 unsigned char green
= image
.GetGreen(i
, j
);
584 unsigned char blue
= image
.GetBlue(i
, j
);
586 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
590 // scan the bitmap for the transparent colour and set the corresponding
591 // pixels in the mask to BLACK and the rest to WHITE
592 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
593 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
595 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
600 ::SelectObject(hMemDC
, hOldBitmap
);
604 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
607 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
610 SetWidth(image
.GetWidth());
611 SetHeight(image
.GetHeight());
612 SetDepth(screenDepth
);
613 SetHBITMAP( (WXHBITMAP
) hBitmap
);
616 // Copy the palette from the source image
617 SetPalette(image
.GetPalette());
618 #endif // wxUSE_PALETTE
620 #if WXWIN_COMPATIBILITY_2
621 // check the wxBitmap object
622 GetBitmapData()->SetOk();
623 #endif // WXWIN_COMPATIBILITY_2
628 wxImage
wxBitmap::ConvertToImage() const
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.
633 // TODO: transparency (create a mask image)
637 wxFAIL_MSG( wxT("bitmap is invalid") );
643 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
645 // create an wxImage object
646 int width
= GetWidth();
647 int height
= GetHeight();
648 image
.Create( width
, height
);
649 unsigned char *data
= image
.GetData();
652 wxFAIL_MSG( wxT("could not allocate data for image") );
656 HDC hScreenDC
= ::GetDC(NULL
);
658 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
659 ::ReleaseDC(NULL
, hScreenDC
);
661 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
663 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
666 for (i
= 0; i
< GetWidth(); i
++)
668 for (j
= 0; j
< GetHeight(); j
++)
670 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
671 unsigned char red
= GetRValue(color
);
672 unsigned char green
= GetGValue(color
);
673 unsigned char blue
= GetBValue(color
);
675 image
.SetRGB(i
, j
, red
, green
, blue
);
679 ::SelectObject(hMemDC
, hOldBitmap
);
683 // Copy the palette from the source image
685 image
.SetPalette(* GetPalette());
686 #endif // wxUSE_PALETTE
691 #endif // __WXMICROWIN__
693 // ----------------------------------------------------------------------------
694 // wxImage to/from conversions
695 // ----------------------------------------------------------------------------
697 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
699 return CreateFromImage(image
, depth
, 0);
702 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
704 wxCHECK_MSG( dc
.Ok(), FALSE
,
705 _T("invalid HDC in wxBitmap::CreateFromImage()") );
707 return CreateFromImage(image
, -1, dc
.GetHDC());
710 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
712 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
716 // first convert the image to DIB
717 const int h
= image
.GetHeight();
718 const int w
= image
.GetWidth();
725 // store the bitmap parameters
726 wxBitmapRefData
*refData
= new wxBitmapRefData
;
727 refData
->m_width
= w
;
728 refData
->m_height
= h
;
729 refData
->m_hasAlpha
= image
.HasAlpha();
734 // next either store DIB as is or create a DDB from it
737 // are we going to use DIB?
738 if ( wxShouldCreateDIB(w
, h
, depth
, hdc
) )
740 // don't delete the DIB section in dib object dtor
741 hbitmap
= dib
.Detach();
743 refData
->m_depth
= dib
.GetDepth();
745 else // we need to convert DIB to DDB
747 // create and set the device-dependent bitmap
749 // VZ: why don't we just use SetDIBits() instead? because of the
750 // palette or is there some other reason?
751 hbitmap
= ::CreateCompatibleBitmap(hdc
? (HDC
)hdc
: ScreenHDC(), w
, h
);
754 wxLogLastError(_T("CreateCompatibleBitmap()"));
760 SelectInHDC
select(hdcMem
, hbitmap
);
763 wxLogLastError(_T("SelectObjct(hBitmap)"));
767 const wxPalette
& palette
= image
.GetPalette();
769 HPALETTE hOldPalette
;
774 hOldPalette
= ::SelectPalette
777 GetHpaletteOf(palette
),
778 FALSE
// ignored for hdcMem
783 wxLogLastError(_T("SelectPalette()"));
786 if ( ::RealizePalette(hdcMem
) == GDI_ERROR
)
788 wxLogLastError(_T("RealizePalette()"));
791 else // no valid palette
795 #endif // wxUSE_PALETTE
798 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
800 wxLogLastError(_T("GetObject(hDIB)"));
803 if ( ::StretchDIBits(hdcMem
,
807 (BITMAPINFO
*)&ds
.dsBmih
,
809 SRCCOPY
) == GDI_ERROR
)
811 wxLogLastError(_T("StretchDIBits()"));
819 ::SelectPalette(hdcMem
, hOldPalette
, FALSE
);
821 #endif // wxUSE_PALETTE
823 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
826 // validate this object
827 SetHBITMAP((WXHBITMAP
)hbitmap
);
829 #if WXWIN_COMPATIBILITY_2
830 m_refData
->m_ok
= TRUE
;
831 #endif // WXWIN_COMPATIBILITY_2
833 // finally also set the mask if we have one
834 if ( image
.HasMask() )
836 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
837 image
.GetMaskGreen(),
838 image
.GetMaskBlue())));
844 wxImage
wxBitmap::ConvertToImage() const
848 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
850 // create an wxImage object
851 int width
= GetWidth();
852 int height
= GetHeight();
853 image
.Create( width
, height
);
854 unsigned char *data
= image
.GetData();
857 wxFAIL_MSG( wxT("could not allocate data for image") );
861 // calc the number of bytes per scanline and padding in the DIB
862 int bytePerLine
= width
*3;
863 int sizeDWORD
= sizeof( DWORD
);
864 int lineBoundary
= bytePerLine
% sizeDWORD
;
866 if( lineBoundary
> 0 )
868 padding
= sizeDWORD
- lineBoundary
;
869 bytePerLine
+= padding
;
872 // create a DIB header
873 int headersize
= sizeof(BITMAPINFOHEADER
);
874 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
877 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
881 // Fill in the DIB header
882 lpDIBh
->bmiHeader
.biSize
= headersize
;
883 lpDIBh
->bmiHeader
.biWidth
= width
;
884 lpDIBh
->bmiHeader
.biHeight
= -height
;
885 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
886 lpDIBh
->bmiHeader
.biPlanes
= 1;
887 lpDIBh
->bmiHeader
.biBitCount
= 24;
888 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
889 lpDIBh
->bmiHeader
.biClrUsed
= 0;
890 // These seem not really needed for our purpose here.
891 lpDIBh
->bmiHeader
.biClrImportant
= 0;
892 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
893 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
894 // memory for DIB data
895 unsigned char *lpBits
;
896 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
899 wxFAIL_MSG( wxT("could not allocate data for DIB") );
905 // copy data from the device-dependent bitmap to the DIB
906 HDC hdc
= ::GetDC(NULL
);
908 hbitmap
= (HBITMAP
) GetHBITMAP();
909 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
911 // copy DIB data into the wxImage object
913 unsigned char *ptdata
= data
;
914 unsigned char *ptbits
= lpBits
;
915 for( i
=0; i
<height
; i
++ )
917 for( j
=0; j
<width
; j
++ )
919 *(ptdata
++) = *(ptbits
+2);
920 *(ptdata
++) = *(ptbits
+1);
921 *(ptdata
++) = *(ptbits
);
927 // similarly, set data according to the possible mask bitmap
928 if( GetMask() && GetMask()->GetMaskBitmap() )
930 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
931 // memory DC created, color set, data copied, and memory DC deleted
932 HDC memdc
= ::CreateCompatibleDC( hdc
);
933 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
934 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
935 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
937 // background color set to RGB(16,16,16) in consistent with wxGTK
938 unsigned char r
=16, g
=16, b
=16;
941 for( i
=0; i
<height
; i
++ )
943 for( j
=0; j
<width
; j
++ )
957 image
.SetMaskColour( r
, g
, b
);
958 image
.SetMask( TRUE
);
962 image
.SetMask( FALSE
);
964 // free allocated resources
965 ::ReleaseDC(NULL
, hdc
);
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);
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
);
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
);
1062 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1064 #ifndef __WXMICROWIN__
1065 // TODO: copy alpha channel data if any
1072 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1073 selectDst(dcDst
, GetHbitmapOf(ret
));
1075 if ( !selectSrc
|| !selectDst
)
1077 wxLogLastError(_T("SelectObjct(hBitmap)"));
1080 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1081 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1083 wxLogLastError(_T("BitBlt"));
1087 // copy mask if there is one
1090 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1092 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1093 selectDst(dcDst
, hbmpMask
);
1095 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1096 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1098 wxLogLastError(_T("BitBlt"));
1101 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1104 #endif // !__WXMICROWIN__
1109 // ----------------------------------------------------------------------------
1110 // wxBitmap accessors
1111 // ----------------------------------------------------------------------------
1113 wxPalette
* wxBitmap::GetPalette() const
1115 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1116 : (wxPalette
*) NULL
;
1119 wxMask
*wxBitmap::GetMask() const
1121 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1126 wxDC
*wxBitmap::GetSelectedInto() const
1128 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1133 #if WXWIN_COMPATIBILITY_2_4
1135 int wxBitmap::GetQuality() const
1140 #endif // WXWIN_COMPATIBILITY_2_4
1142 bool wxBitmap::HasAlpha() const
1144 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1147 // ----------------------------------------------------------------------------
1149 // ----------------------------------------------------------------------------
1153 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1155 if ( GetBitmapData() )
1156 GetBitmapData()->m_selectedInto
= dc
;
1163 void wxBitmap::SetPalette(const wxPalette
& palette
)
1167 GetBitmapData()->m_bitmapPalette
= palette
;
1170 #endif // wxUSE_PALETTE
1172 void wxBitmap::SetMask(wxMask
*mask
)
1176 GetBitmapData()->SetMask(mask
);
1179 #if WXWIN_COMPATIBILITY_2
1181 void wxBitmap::SetOk(bool isOk
)
1185 GetBitmapData()->m_ok
= isOk
;
1188 #endif // WXWIN_COMPATIBILITY_2
1190 #if WXWIN_COMPATIBILITY_2_4
1192 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1196 #endif // WXWIN_COMPATIBILITY_2_4
1198 // ----------------------------------------------------------------------------
1199 // raw bitmap access support
1200 // ----------------------------------------------------------------------------
1202 bool wxBitmap::GetRawData(wxRawBitmapData
*data
)
1204 wxCHECK_MSG( data
, FALSE
, _T("NULL pointer in wxBitmap::GetRawData") );
1208 // no bitmap, no data (raw or otherwise)
1212 // we only support raw access to the DIBs, so check if we have one
1214 if ( !::GetObject(GetHbitmap(), sizeof(ds
), &ds
) )
1219 // ok, store the relevant info in wxRawBitmapData
1220 const LONG h
= ds
.dsBm
.bmHeight
;
1222 data
->m_width
= ds
.dsBm
.bmWidth
;
1224 data
->m_bypp
= ds
.dsBm
.bmBitsPixel
/ 8;
1226 // remember that DIBs are stored in top to bottom order!
1227 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1228 data
->m_stride
= -bytesPerRow
;
1229 data
->m_pixels
= (unsigned char *)ds
.dsBm
.bmBits
;
1232 data
->m_pixels
+= (h
- 1)*bytesPerRow
;
1238 // ----------------------------------------------------------------------------
1239 // TODO: to be replaced by something better
1240 // ----------------------------------------------------------------------------
1242 // Creates a bitmap that matches the device context, from
1243 // an arbitray bitmap. At present, the original bitmap must have an
1244 // associated palette. TODO: use a default palette if no palette exists.
1245 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1246 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1248 #ifdef __WXMICROWIN__
1252 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1253 HPALETTE hPal
= (HPALETTE
) NULL
;
1255 void *lpBits
= (void*) NULL
;
1258 if( GetPalette() && GetPalette()->Ok() )
1260 tmpBitmap
.SetPalette(*GetPalette());
1261 memDC
.SelectObject(tmpBitmap
);
1262 memDC
.SetPalette(*GetPalette());
1263 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1267 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1269 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1270 tmpBitmap
.SetPalette( palette
);
1271 memDC
.SelectObject(tmpBitmap
);
1272 memDC
.SetPalette( palette
);
1274 #else // !wxUSE_PALETTE
1275 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1276 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1278 // set the height negative because in a DIB the order of the lines is
1280 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1282 return wxNullBitmap
;
1285 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1287 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1289 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1290 GetWidth(), GetHeight(),
1291 0, 0, 0, GetHeight(),
1292 lpBits
, lpDib
, DIB_RGB_COLORS
);
1302 // ----------------------------------------------------------------------------
1304 // ----------------------------------------------------------------------------
1311 // Construct a mask from a bitmap and a colour indicating
1312 // the transparent area
1313 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1316 Create(bitmap
, colour
);
1319 // Construct a mask from a bitmap and a palette index indicating
1320 // the transparent area
1321 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1324 Create(bitmap
, paletteIndex
);
1327 // Construct a mask from a mono bitmap (copies the bitmap).
1328 wxMask::wxMask(const wxBitmap
& bitmap
)
1337 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1340 // Create a mask from a mono bitmap (copies the bitmap).
1341 bool wxMask::Create(const wxBitmap
& bitmap
)
1343 #ifndef __WXMICROWIN__
1344 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1345 _T("can't create mask from invalid or not monochrome bitmap") );
1349 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1353 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1358 HDC srcDC
= CreateCompatibleDC(0);
1359 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1360 HDC destDC
= CreateCompatibleDC(0);
1361 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1362 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1363 SelectObject(srcDC
, 0);
1365 SelectObject(destDC
, 0);
1373 // Create a mask from a bitmap and a palette index indicating
1374 // the transparent area
1375 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1379 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1384 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1386 unsigned char red
, green
, blue
;
1387 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1389 wxColour
transparentColour(red
, green
, blue
);
1390 return Create(bitmap
, transparentColour
);
1393 #endif // wxUSE_PALETTE
1398 // Create a mask from a bitmap and a colour indicating
1399 // the transparent area
1400 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1402 #ifndef __WXMICROWIN__
1403 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1407 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1411 int width
= bitmap
.GetWidth(),
1412 height
= bitmap
.GetHeight();
1414 // scan the bitmap for the transparent colour and set the corresponding
1415 // pixels in the mask to BLACK and the rest to WHITE
1416 COLORREF maskColour
= wxColourToPalRGB(colour
);
1417 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1419 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1420 HDC destDC
= ::CreateCompatibleDC(NULL
);
1421 if ( !srcDC
|| !destDC
)
1423 wxLogLastError(wxT("CreateCompatibleDC"));
1428 // SelectObject() will fail
1429 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1430 _T("bitmap can't be selected in another DC") );
1432 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1435 wxLogLastError(wxT("SelectObject"));
1440 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1443 wxLogLastError(wxT("SelectObject"));
1450 // this will create a monochrome bitmap with 0 points for the pixels
1451 // which have the same value as the background colour and 1 for the
1453 ::SetBkColor(srcDC
, maskColour
);
1454 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1457 ::SelectObject(srcDC
, hbmpSrcOld
);
1459 ::SelectObject(destDC
, hbmpDstOld
);
1463 #else // __WXMICROWIN__
1465 #endif // __WXMICROWIN__/!__WXMICROWIN__
1468 // ----------------------------------------------------------------------------
1470 // ----------------------------------------------------------------------------
1472 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1475 int width
, int height
, int depth
)
1477 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1479 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1482 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1483 const wxString
& name
,
1485 int width
, int height
)
1487 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1489 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1492 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1493 const wxString
& name
,
1496 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1498 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1501 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1502 void *WXUNUSED(data
),
1503 long WXUNUSED(type
),
1504 int WXUNUSED(width
),
1505 int WXUNUSED(height
),
1506 int WXUNUSED(depth
))
1511 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1512 const wxString
& WXUNUSED(name
),
1513 long WXUNUSED(type
),
1514 int WXUNUSED(desiredWidth
),
1515 int WXUNUSED(desiredHeight
))
1520 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1521 const wxString
& WXUNUSED(name
),
1523 const wxPalette
*WXUNUSED(palette
))
1528 // ----------------------------------------------------------------------------
1530 // ----------------------------------------------------------------------------
1532 #ifndef __WXMICROWIN__
1533 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1534 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1536 unsigned long i
, headerSize
;
1537 LPBITMAPINFO lpDIBheader
= NULL
;
1538 LPPALETTEENTRY lpPe
= NULL
;
1541 // Allocate space for a DIB header
1542 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1543 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1544 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1546 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1548 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1550 // Fill in the static parts of the DIB header
1551 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1552 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1553 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1554 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1556 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1557 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1558 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1559 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1560 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1563 // Initialize the DIB palette
1564 for (i
= 0; i
< 256; i
++) {
1565 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1566 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1567 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1568 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1571 *lpDIBHeader
= lpDIBheader
;
1576 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1582 // ----------------------------------------------------------------------------
1583 // global helper functions implemented here
1584 // ----------------------------------------------------------------------------
1586 // helper of wxBitmapToHICON/HCURSOR
1588 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1595 // we can't create an icon/cursor form nothing
1599 wxMask
*mask
= bmp
.GetMask();
1602 // we must have a mask for an icon, so even if it's probably incorrect,
1603 // do create it (grey is the "standard" transparent colour)
1604 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1608 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1611 iconInfo
.xHotspot
= hotSpotX
;
1612 iconInfo
.yHotspot
= hotSpotY
;
1615 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1616 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1618 // black out the transparent area to preserve background colour, because
1619 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1620 // the mask to the dest rect.
1622 MemoryHDC dcSrc
, dcDst
;
1623 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1624 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1626 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1627 dcSrc
, 0, 0, SRCAND
) )
1629 wxLogLastError(_T("BitBlt"));
1633 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1635 if ( !bmp
.GetMask() )
1637 // we created the mask, now delete it
1641 // delete the inverted mask bitmap we created as well
1642 ::DeleteObject(iconInfo
.hbmMask
);
1647 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1649 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1652 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1654 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1657 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1659 #ifndef __WXMICROWIN__
1660 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1662 // get width/height from the bitmap if not given
1666 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1671 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1672 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1673 if ( !hdcSrc
|| !hdcDst
)
1675 wxLogLastError(wxT("CreateCompatibleDC"));
1678 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1681 wxLogLastError(wxT("CreateBitmap"));
1684 ::SelectObject(hdcSrc
, hbmpMask
);
1685 ::SelectObject(hdcDst
, hbmpInvMask
);
1686 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1690 wxLogLastError(wxT("BitBlt"));