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 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
311 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
313 HBITMAP hbitmap
= dib
.CreateDDB();
319 wxBitmapRefData
*refData
= new wxBitmapRefData
;
322 refData
->m_width
= dib
.GetWidth();
323 refData
->m_height
= dib
.GetHeight();
324 refData
->m_depth
= dib
.GetDepth();
326 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
329 wxPalette
*palette
= dib
.CreatePalette();
332 refData
->m_bitmapPalette
= *palette
;
336 #endif // wxUSE_PALETTE
341 wxBitmap::~wxBitmap()
345 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
349 #ifndef __WXMICROWIN__
350 wxBitmapRefData
*refData
= new wxBitmapRefData
;
353 refData
->m_width
= width
;
354 refData
->m_height
= height
;
355 refData
->m_depth
= depth
;
360 // we assume that it is in XBM format which is not quite the same as
361 // the format CreateBitmap() wants because the order of bytes in the
363 const size_t bytesPerLine
= (width
+ 7) / 8;
364 const size_t padding
= bytesPerLine
% 2;
365 const size_t len
= height
* ( padding
+ bytesPerLine
);
366 data
= (char *)malloc(len
);
367 const char *src
= bits
;
370 for ( int rows
= 0; rows
< height
; rows
++ )
372 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
374 unsigned char val
= *src
++;
375 unsigned char reversed
= 0;
377 for ( int bits
= 0; bits
< 8; bits
++)
380 reversed
|= (val
& 0x01);
392 // bits should already be in Windows standard format
393 data
= (char *)bits
; // const_cast is harmless
396 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
399 wxLogLastError(wxT("CreateBitmap"));
407 SetHBITMAP((WXHBITMAP
)hbmp
);
411 // Create from XPM data
412 bool wxBitmap::CreateFromXpm(const char **data
)
414 #if wxUSE_IMAGE && wxUSE_XPM
417 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
419 wxXPMDecoder decoder
;
420 wxImage img
= decoder
.ReadData(data
);
421 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
423 *this = wxBitmap(img
);
430 wxBitmap::wxBitmap(int w
, int h
, int d
)
434 (void)Create(w
, h
, d
);
437 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
441 (void)Create(data
, type
, width
, height
, depth
);
444 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
448 LoadFile(filename
, (int)type
);
451 bool wxBitmap::Create(int width
, int height
, int depth
)
453 return DoCreate(width
, height
, depth
, 0);
456 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
458 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
460 return DoCreate(width
, height
, -1, dc
.GetHDC());
463 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
467 m_refData
= new wxBitmapRefData
;
469 GetBitmapData()->m_width
= w
;
470 GetBitmapData()->m_height
= h
;
474 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
478 // create DIBs without alpha channel by default
486 // don't delete the DIB section in dib object dtor
489 GetBitmapData()->m_depth
= d
;
494 d
= wxDisplayDepth();
496 GetBitmapData()->m_depth
= d
;
498 #ifndef __WXMICROWIN__
501 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
504 wxLogLastError(wxT("CreateBitmap"));
508 #endif // !__WXMICROWIN__
511 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
514 wxLogLastError(wxT("CreateCompatibleBitmap"));
517 GetBitmapData()->m_depth
= wxDisplayDepth();
521 SetHBITMAP((WXHBITMAP
)hbmp
);
523 #if WXWIN_COMPATIBILITY_2
524 GetBitmapData()->m_ok
= hbmp
!= 0;
525 #endif // WXWIN_COMPATIBILITY_2
532 // ----------------------------------------------------------------------------
533 // wxImage to/from conversions for Microwin
534 // ----------------------------------------------------------------------------
536 // Microwin versions are so different from normal ones that it really doesn't
537 // make sense to use #ifdefs inside the function bodies
538 #ifdef __WXMICROWIN__
540 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
542 // Set this to 1 to experiment with mask code,
543 // which currently doesn't work
546 m_refData
= new wxBitmapRefData();
548 // Initial attempt at a simple-minded implementation.
549 // The bitmap will always be created at the screen depth,
550 // so the 'depth' argument is ignored.
552 HDC hScreenDC
= ::GetDC(NULL
);
553 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
555 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
556 HBITMAP hMaskBitmap
= NULL
;
557 HBITMAP hOldMaskBitmap
= NULL
;
559 unsigned char maskR
= 0;
560 unsigned char maskG
= 0;
561 unsigned char maskB
= 0;
563 // printf("Created bitmap %d\n", (int) hBitmap);
566 ::ReleaseDC(NULL
, hScreenDC
);
569 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
571 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
572 ::ReleaseDC(NULL
, hScreenDC
);
574 // created an mono-bitmap for the possible mask
575 bool hasMask
= image
.HasMask();
580 // FIXME: we should be able to pass bpp = 1, but
581 // GdBlit can't handle a different depth
583 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
585 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
587 maskR
= image
.GetMaskRed();
588 maskG
= image
.GetMaskGreen();
589 maskB
= image
.GetMaskBlue();
597 hScreenDC
= ::GetDC(NULL
);
598 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
599 ::ReleaseDC(NULL
, hScreenDC
);
601 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
609 for (i
= 0; i
< image
.GetWidth(); i
++)
611 for (j
= 0; j
< image
.GetHeight(); j
++)
613 unsigned char red
= image
.GetRed(i
, j
);
614 unsigned char green
= image
.GetGreen(i
, j
);
615 unsigned char blue
= image
.GetBlue(i
, j
);
617 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
621 // scan the bitmap for the transparent colour and set the corresponding
622 // pixels in the mask to BLACK and the rest to WHITE
623 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
624 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
626 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
631 ::SelectObject(hMemDC
, hOldBitmap
);
635 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
638 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
641 SetWidth(image
.GetWidth());
642 SetHeight(image
.GetHeight());
643 SetDepth(screenDepth
);
644 SetHBITMAP( (WXHBITMAP
) hBitmap
);
647 // Copy the palette from the source image
648 SetPalette(image
.GetPalette());
649 #endif // wxUSE_PALETTE
651 #if WXWIN_COMPATIBILITY_2
652 // check the wxBitmap object
653 GetBitmapData()->SetOk();
654 #endif // WXWIN_COMPATIBILITY_2
659 wxImage
wxBitmap::ConvertToImage() const
661 // Initial attempt at a simple-minded implementation.
662 // The bitmap will always be created at the screen depth,
663 // so the 'depth' argument is ignored.
664 // TODO: transparency (create a mask image)
668 wxFAIL_MSG( wxT("bitmap is invalid") );
674 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
676 // create an wxImage object
677 int width
= GetWidth();
678 int height
= GetHeight();
679 image
.Create( width
, height
);
680 unsigned char *data
= image
.GetData();
683 wxFAIL_MSG( wxT("could not allocate data for image") );
687 HDC hScreenDC
= ::GetDC(NULL
);
689 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
690 ::ReleaseDC(NULL
, hScreenDC
);
692 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
694 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
697 for (i
= 0; i
< GetWidth(); i
++)
699 for (j
= 0; j
< GetHeight(); j
++)
701 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
702 unsigned char red
= GetRValue(color
);
703 unsigned char green
= GetGValue(color
);
704 unsigned char blue
= GetBValue(color
);
706 image
.SetRGB(i
, j
, red
, green
, blue
);
710 ::SelectObject(hMemDC
, hOldBitmap
);
714 // Copy the palette from the source image
716 image
.SetPalette(* GetPalette());
717 #endif // wxUSE_PALETTE
722 #endif // __WXMICROWIN__
724 // ----------------------------------------------------------------------------
725 // wxImage to/from conversions
726 // ----------------------------------------------------------------------------
728 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
730 return CreateFromImage(image
, depth
, 0);
733 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
735 wxCHECK_MSG( dc
.Ok(), FALSE
,
736 _T("invalid HDC in wxBitmap::CreateFromImage()") );
738 return CreateFromImage(image
, -1, dc
.GetHDC());
741 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
743 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
747 // first convert the image to DIB
748 const int h
= image
.GetHeight();
749 const int w
= image
.GetWidth();
756 // store the bitmap parameters
757 wxBitmapRefData
*refData
= new wxBitmapRefData
;
758 refData
->m_width
= w
;
759 refData
->m_height
= h
;
760 refData
->m_hasAlpha
= image
.HasAlpha();
765 // next either store DIB as is or create a DDB from it
768 // are we going to use DIB?
769 if ( wxShouldCreateDIB(w
, h
, depth
, hdc
) )
771 // don't delete the DIB section in dib object dtor
772 hbitmap
= dib
.Detach();
774 refData
->m_depth
= dib
.GetDepth();
776 else // we need to convert DIB to DDB
778 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
780 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
783 // validate this object
784 SetHBITMAP((WXHBITMAP
)hbitmap
);
786 #if WXWIN_COMPATIBILITY_2
787 m_refData
->m_ok
= TRUE
;
788 #endif // WXWIN_COMPATIBILITY_2
790 // finally also set the mask if we have one
791 if ( image
.HasMask() )
793 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
794 image
.GetMaskGreen(),
795 image
.GetMaskBlue())));
801 wxImage
wxBitmap::ConvertToImage() const
805 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
807 // create an wxImage object
808 int width
= GetWidth();
809 int height
= GetHeight();
810 image
.Create( width
, height
);
811 unsigned char *data
= image
.GetData();
814 wxFAIL_MSG( wxT("could not allocate data for image") );
818 // calc the number of bytes per scanline and padding in the DIB
819 int bytePerLine
= width
*3;
820 int sizeDWORD
= sizeof( DWORD
);
821 int lineBoundary
= bytePerLine
% sizeDWORD
;
823 if( lineBoundary
> 0 )
825 padding
= sizeDWORD
- lineBoundary
;
826 bytePerLine
+= padding
;
829 // create a DIB header
830 int headersize
= sizeof(BITMAPINFOHEADER
);
831 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
834 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
838 // Fill in the DIB header
839 lpDIBh
->bmiHeader
.biSize
= headersize
;
840 lpDIBh
->bmiHeader
.biWidth
= width
;
841 lpDIBh
->bmiHeader
.biHeight
= -height
;
842 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
843 lpDIBh
->bmiHeader
.biPlanes
= 1;
844 lpDIBh
->bmiHeader
.biBitCount
= 24;
845 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
846 lpDIBh
->bmiHeader
.biClrUsed
= 0;
847 // These seem not really needed for our purpose here.
848 lpDIBh
->bmiHeader
.biClrImportant
= 0;
849 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
850 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
851 // memory for DIB data
852 unsigned char *lpBits
;
853 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
856 wxFAIL_MSG( wxT("could not allocate data for DIB") );
862 // copy data from the device-dependent bitmap to the DIB
863 HDC hdc
= ::GetDC(NULL
);
865 hbitmap
= (HBITMAP
) GetHBITMAP();
866 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
868 // copy DIB data into the wxImage object
870 unsigned char *ptdata
= data
;
871 unsigned char *ptbits
= lpBits
;
872 for( i
=0; i
<height
; i
++ )
874 for( j
=0; j
<width
; j
++ )
876 *(ptdata
++) = *(ptbits
+2);
877 *(ptdata
++) = *(ptbits
+1);
878 *(ptdata
++) = *(ptbits
);
884 // similarly, set data according to the possible mask bitmap
885 if( GetMask() && GetMask()->GetMaskBitmap() )
887 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
888 // memory DC created, color set, data copied, and memory DC deleted
889 HDC memdc
= ::CreateCompatibleDC( hdc
);
890 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
891 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
892 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
894 // background color set to RGB(16,16,16) in consistent with wxGTK
895 unsigned char r
=16, g
=16, b
=16;
898 for( i
=0; i
<height
; i
++ )
900 for( j
=0; j
<width
; j
++ )
914 image
.SetMaskColour( r
, g
, b
);
915 image
.SetMask( TRUE
);
919 image
.SetMask( FALSE
);
921 // free allocated resources
922 ::ReleaseDC(NULL
, hdc
);
929 #endif // wxUSE_IMAGE
931 // ----------------------------------------------------------------------------
932 // loading and saving bitmaps
933 // ----------------------------------------------------------------------------
935 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
939 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
943 m_refData
= new wxBitmapRefData
;
945 return handler
->LoadFile(this, filename
, type
, -1, -1);
951 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
953 *this = wxBitmap(image
);
958 #endif // wxUSE_IMAGE
963 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
967 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
971 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
976 m_refData
= new wxBitmapRefData
;
978 return handler
->Create(this, data
, type
, width
, height
, depth
);
981 bool wxBitmap::SaveFile(const wxString
& filename
,
983 const wxPalette
*palette
)
985 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
989 return handler
->SaveFile(this, filename
, type
, palette
);
994 // FIXME what about palette? shouldn't we use it?
995 wxImage image
= ConvertToImage();
998 return image
.SaveFile(filename
, type
);
1001 #endif // wxUSE_IMAGE
1006 // ----------------------------------------------------------------------------
1007 // sub bitmap extraction
1008 // ----------------------------------------------------------------------------
1010 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1012 wxCHECK_MSG( Ok() &&
1013 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1014 (rect
.x
+rect
.width
<= GetWidth()) &&
1015 (rect
.y
+rect
.height
<= GetHeight()),
1016 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1018 wxBitmap
ret( rect
.width
, rect
.height
);
1019 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1021 #ifndef __WXMICROWIN__
1022 // TODO: copy alpha channel data if any
1029 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1030 selectDst(dcDst
, GetHbitmapOf(ret
));
1032 if ( !selectSrc
|| !selectDst
)
1034 wxLogLastError(_T("SelectObjct(hBitmap)"));
1037 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1038 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1040 wxLogLastError(_T("BitBlt"));
1044 // copy mask if there is one
1047 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1049 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1050 selectDst(dcDst
, hbmpMask
);
1052 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1053 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1055 wxLogLastError(_T("BitBlt"));
1058 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1061 #endif // !__WXMICROWIN__
1066 // ----------------------------------------------------------------------------
1067 // wxBitmap accessors
1068 // ----------------------------------------------------------------------------
1070 wxPalette
* wxBitmap::GetPalette() const
1072 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1073 : (wxPalette
*) NULL
;
1076 wxMask
*wxBitmap::GetMask() const
1078 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1083 wxDC
*wxBitmap::GetSelectedInto() const
1085 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1090 #if WXWIN_COMPATIBILITY_2_4
1092 int wxBitmap::GetQuality() const
1097 #endif // WXWIN_COMPATIBILITY_2_4
1099 bool wxBitmap::HasAlpha() const
1101 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1104 // ----------------------------------------------------------------------------
1106 // ----------------------------------------------------------------------------
1110 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1112 if ( GetBitmapData() )
1113 GetBitmapData()->m_selectedInto
= dc
;
1120 void wxBitmap::SetPalette(const wxPalette
& palette
)
1124 GetBitmapData()->m_bitmapPalette
= palette
;
1127 #endif // wxUSE_PALETTE
1129 void wxBitmap::SetMask(wxMask
*mask
)
1133 GetBitmapData()->SetMask(mask
);
1136 #if WXWIN_COMPATIBILITY_2
1138 void wxBitmap::SetOk(bool isOk
)
1142 GetBitmapData()->m_ok
= isOk
;
1145 #endif // WXWIN_COMPATIBILITY_2
1147 #if WXWIN_COMPATIBILITY_2_4
1149 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1153 #endif // WXWIN_COMPATIBILITY_2_4
1155 // ----------------------------------------------------------------------------
1156 // raw bitmap access support
1157 // ----------------------------------------------------------------------------
1159 bool wxBitmap::GetRawData(wxRawBitmapData
*data
)
1161 wxCHECK_MSG( data
, FALSE
, _T("NULL pointer in wxBitmap::GetRawData") );
1165 // no bitmap, no data (raw or otherwise)
1169 // we only support raw access to the DIBs, so check if we have one
1171 if ( !::GetObject(GetHbitmap(), sizeof(ds
), &ds
) )
1176 // ok, store the relevant info in wxRawBitmapData
1177 const LONG h
= ds
.dsBm
.bmHeight
;
1179 data
->m_width
= ds
.dsBm
.bmWidth
;
1181 data
->m_bypp
= ds
.dsBm
.bmBitsPixel
/ 8;
1183 // remember that DIBs are stored in top to bottom order!
1184 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1185 data
->m_stride
= -bytesPerRow
;
1186 data
->m_pixels
= (unsigned char *)ds
.dsBm
.bmBits
;
1189 data
->m_pixels
+= (h
- 1)*bytesPerRow
;
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1204 // Construct a mask from a bitmap and a colour indicating
1205 // the transparent area
1206 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1209 Create(bitmap
, colour
);
1212 // Construct a mask from a bitmap and a palette index indicating
1213 // the transparent area
1214 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1217 Create(bitmap
, paletteIndex
);
1220 // Construct a mask from a mono bitmap (copies the bitmap).
1221 wxMask::wxMask(const wxBitmap
& bitmap
)
1230 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1233 // Create a mask from a mono bitmap (copies the bitmap).
1234 bool wxMask::Create(const wxBitmap
& bitmap
)
1236 #ifndef __WXMICROWIN__
1237 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1238 _T("can't create mask from invalid or not monochrome bitmap") );
1242 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1246 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1251 HDC srcDC
= CreateCompatibleDC(0);
1252 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1253 HDC destDC
= CreateCompatibleDC(0);
1254 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1255 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1256 SelectObject(srcDC
, 0);
1258 SelectObject(destDC
, 0);
1266 // Create a mask from a bitmap and a palette index indicating
1267 // the transparent area
1268 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1272 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1277 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1279 unsigned char red
, green
, blue
;
1280 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1282 wxColour
transparentColour(red
, green
, blue
);
1283 return Create(bitmap
, transparentColour
);
1286 #endif // wxUSE_PALETTE
1291 // Create a mask from a bitmap and a colour indicating
1292 // the transparent area
1293 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1295 #ifndef __WXMICROWIN__
1296 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1300 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1304 int width
= bitmap
.GetWidth(),
1305 height
= bitmap
.GetHeight();
1307 // scan the bitmap for the transparent colour and set the corresponding
1308 // pixels in the mask to BLACK and the rest to WHITE
1309 COLORREF maskColour
= wxColourToPalRGB(colour
);
1310 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1312 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1313 HDC destDC
= ::CreateCompatibleDC(NULL
);
1314 if ( !srcDC
|| !destDC
)
1316 wxLogLastError(wxT("CreateCompatibleDC"));
1321 // SelectObject() will fail
1322 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1323 _T("bitmap can't be selected in another DC") );
1325 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1328 wxLogLastError(wxT("SelectObject"));
1333 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1336 wxLogLastError(wxT("SelectObject"));
1343 // this will create a monochrome bitmap with 0 points for the pixels
1344 // which have the same value as the background colour and 1 for the
1346 ::SetBkColor(srcDC
, maskColour
);
1347 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1350 ::SelectObject(srcDC
, hbmpSrcOld
);
1352 ::SelectObject(destDC
, hbmpDstOld
);
1356 #else // __WXMICROWIN__
1358 #endif // __WXMICROWIN__/!__WXMICROWIN__
1361 // ----------------------------------------------------------------------------
1363 // ----------------------------------------------------------------------------
1365 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1368 int width
, int height
, int depth
)
1370 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1372 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1375 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1376 const wxString
& name
,
1378 int width
, int height
)
1380 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1382 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1385 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1386 const wxString
& name
,
1389 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1391 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1394 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1395 void *WXUNUSED(data
),
1396 long WXUNUSED(type
),
1397 int WXUNUSED(width
),
1398 int WXUNUSED(height
),
1399 int WXUNUSED(depth
))
1404 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1405 const wxString
& WXUNUSED(name
),
1406 long WXUNUSED(type
),
1407 int WXUNUSED(desiredWidth
),
1408 int WXUNUSED(desiredHeight
))
1413 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1414 const wxString
& WXUNUSED(name
),
1416 const wxPalette
*WXUNUSED(palette
))
1421 // ----------------------------------------------------------------------------
1423 // ----------------------------------------------------------------------------
1425 #ifndef __WXMICROWIN__
1426 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1427 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1429 unsigned long i
, headerSize
;
1430 LPBITMAPINFO lpDIBheader
= NULL
;
1431 LPPALETTEENTRY lpPe
= NULL
;
1434 // Allocate space for a DIB header
1435 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1436 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1437 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1439 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1441 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1443 // Fill in the static parts of the DIB header
1444 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1445 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1446 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1447 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1449 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1450 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1451 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1452 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1453 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1456 // Initialize the DIB palette
1457 for (i
= 0; i
< 256; i
++) {
1458 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1459 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1460 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1461 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1464 *lpDIBHeader
= lpDIBheader
;
1469 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1475 // ----------------------------------------------------------------------------
1476 // global helper functions implemented here
1477 // ----------------------------------------------------------------------------
1479 // helper of wxBitmapToHICON/HCURSOR
1481 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1488 // we can't create an icon/cursor form nothing
1492 wxMask
*mask
= bmp
.GetMask();
1495 // we must have a mask for an icon, so even if it's probably incorrect,
1496 // do create it (grey is the "standard" transparent colour)
1497 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1501 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1504 iconInfo
.xHotspot
= hotSpotX
;
1505 iconInfo
.yHotspot
= hotSpotY
;
1508 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1509 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1511 // black out the transparent area to preserve background colour, because
1512 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1513 // the mask to the dest rect.
1515 MemoryHDC dcSrc
, dcDst
;
1516 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1517 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1519 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1520 dcSrc
, 0, 0, SRCAND
) )
1522 wxLogLastError(_T("BitBlt"));
1526 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1528 if ( !bmp
.GetMask() )
1530 // we created the mask, now delete it
1534 // delete the inverted mask bitmap we created as well
1535 ::DeleteObject(iconInfo
.hbmMask
);
1540 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1542 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1545 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1547 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1550 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1552 #ifndef __WXMICROWIN__
1553 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1555 // get width/height from the bitmap if not given
1559 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1564 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1565 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1566 if ( !hdcSrc
|| !hdcDst
)
1568 wxLogLastError(wxT("CreateCompatibleDC"));
1571 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1574 wxLogLastError(wxT("CreateBitmap"));
1577 ::SelectObject(hdcSrc
, hbmpMask
);
1578 ::SelectObject(hdcDst
, hbmpInvMask
);
1579 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1583 wxLogLastError(wxT("BitBlt"));