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
;
490 GetBitmapData()->m_hasAlpha
= d
== 32; // 32bpp DIBs have alpha channel
495 d
= wxDisplayDepth();
497 GetBitmapData()->m_depth
= d
;
499 #ifndef __WXMICROWIN__
502 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
505 wxLogLastError(wxT("CreateBitmap"));
509 #endif // !__WXMICROWIN__
512 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
515 wxLogLastError(wxT("CreateCompatibleBitmap"));
518 GetBitmapData()->m_depth
= wxDisplayDepth();
522 SetHBITMAP((WXHBITMAP
)hbmp
);
524 #if WXWIN_COMPATIBILITY_2
525 GetBitmapData()->m_ok
= hbmp
!= 0;
526 #endif // WXWIN_COMPATIBILITY_2
533 // ----------------------------------------------------------------------------
534 // wxImage to/from conversions for Microwin
535 // ----------------------------------------------------------------------------
537 // Microwin versions are so different from normal ones that it really doesn't
538 // make sense to use #ifdefs inside the function bodies
539 #ifdef __WXMICROWIN__
541 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
543 // Set this to 1 to experiment with mask code,
544 // which currently doesn't work
547 m_refData
= new wxBitmapRefData();
549 // Initial attempt at a simple-minded implementation.
550 // The bitmap will always be created at the screen depth,
551 // so the 'depth' argument is ignored.
553 HDC hScreenDC
= ::GetDC(NULL
);
554 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
556 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
557 HBITMAP hMaskBitmap
= NULL
;
558 HBITMAP hOldMaskBitmap
= NULL
;
560 unsigned char maskR
= 0;
561 unsigned char maskG
= 0;
562 unsigned char maskB
= 0;
564 // printf("Created bitmap %d\n", (int) hBitmap);
567 ::ReleaseDC(NULL
, hScreenDC
);
570 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
572 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
573 ::ReleaseDC(NULL
, hScreenDC
);
575 // created an mono-bitmap for the possible mask
576 bool hasMask
= image
.HasMask();
581 // FIXME: we should be able to pass bpp = 1, but
582 // GdBlit can't handle a different depth
584 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
586 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
588 maskR
= image
.GetMaskRed();
589 maskG
= image
.GetMaskGreen();
590 maskB
= image
.GetMaskBlue();
598 hScreenDC
= ::GetDC(NULL
);
599 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
600 ::ReleaseDC(NULL
, hScreenDC
);
602 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
610 for (i
= 0; i
< image
.GetWidth(); i
++)
612 for (j
= 0; j
< image
.GetHeight(); j
++)
614 unsigned char red
= image
.GetRed(i
, j
);
615 unsigned char green
= image
.GetGreen(i
, j
);
616 unsigned char blue
= image
.GetBlue(i
, j
);
618 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
622 // scan the bitmap for the transparent colour and set the corresponding
623 // pixels in the mask to BLACK and the rest to WHITE
624 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
625 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
627 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
632 ::SelectObject(hMemDC
, hOldBitmap
);
636 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
639 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
642 SetWidth(image
.GetWidth());
643 SetHeight(image
.GetHeight());
644 SetDepth(screenDepth
);
645 SetHBITMAP( (WXHBITMAP
) hBitmap
);
648 // Copy the palette from the source image
649 SetPalette(image
.GetPalette());
650 #endif // wxUSE_PALETTE
652 #if WXWIN_COMPATIBILITY_2
653 // check the wxBitmap object
654 GetBitmapData()->SetOk();
655 #endif // WXWIN_COMPATIBILITY_2
660 wxImage
wxBitmap::ConvertToImage() const
662 // Initial attempt at a simple-minded implementation.
663 // The bitmap will always be created at the screen depth,
664 // so the 'depth' argument is ignored.
665 // TODO: transparency (create a mask image)
669 wxFAIL_MSG( wxT("bitmap is invalid") );
675 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
677 // create an wxImage object
678 int width
= GetWidth();
679 int height
= GetHeight();
680 image
.Create( width
, height
);
681 unsigned char *data
= image
.GetData();
684 wxFAIL_MSG( wxT("could not allocate data for image") );
688 HDC hScreenDC
= ::GetDC(NULL
);
690 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
691 ::ReleaseDC(NULL
, hScreenDC
);
693 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
695 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
698 for (i
= 0; i
< GetWidth(); i
++)
700 for (j
= 0; j
< GetHeight(); j
++)
702 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
703 unsigned char red
= GetRValue(color
);
704 unsigned char green
= GetGValue(color
);
705 unsigned char blue
= GetBValue(color
);
707 image
.SetRGB(i
, j
, red
, green
, blue
);
711 ::SelectObject(hMemDC
, hOldBitmap
);
715 // Copy the palette from the source image
717 image
.SetPalette(* GetPalette());
718 #endif // wxUSE_PALETTE
723 #endif // __WXMICROWIN__
725 // ----------------------------------------------------------------------------
726 // wxImage to/from conversions
727 // ----------------------------------------------------------------------------
729 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
731 return CreateFromImage(image
, depth
, 0);
734 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
736 wxCHECK_MSG( dc
.Ok(), FALSE
,
737 _T("invalid HDC in wxBitmap::CreateFromImage()") );
739 return CreateFromImage(image
, -1, dc
.GetHDC());
742 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
744 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
748 // first convert the image to DIB
749 const int h
= image
.GetHeight();
750 const int w
= image
.GetWidth();
757 // store the bitmap parameters
758 wxBitmapRefData
*refData
= new wxBitmapRefData
;
759 refData
->m_width
= w
;
760 refData
->m_height
= h
;
761 refData
->m_hasAlpha
= image
.HasAlpha();
766 // next either store DIB as is or create a DDB from it
769 // are we going to use DIB?
770 if ( wxShouldCreateDIB(w
, h
, depth
, hdc
) )
772 // don't delete the DIB section in dib object dtor
773 hbitmap
= dib
.Detach();
775 refData
->m_depth
= dib
.GetDepth();
777 else // we need to convert DIB to DDB
779 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
781 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
784 // validate this object
785 SetHBITMAP((WXHBITMAP
)hbitmap
);
787 #if WXWIN_COMPATIBILITY_2
788 m_refData
->m_ok
= TRUE
;
789 #endif // WXWIN_COMPATIBILITY_2
791 // finally also set the mask if we have one
792 if ( image
.HasMask() )
794 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
795 image
.GetMaskGreen(),
796 image
.GetMaskBlue())));
802 wxImage
wxBitmap::ConvertToImage() const
806 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
808 // create an wxImage object
809 int width
= GetWidth();
810 int height
= GetHeight();
811 image
.Create( width
, height
);
812 unsigned char *data
= image
.GetData();
815 wxFAIL_MSG( wxT("could not allocate data for image") );
819 // calc the number of bytes per scanline and padding in the DIB
820 int bytePerLine
= width
*3;
821 int sizeDWORD
= sizeof( DWORD
);
822 int lineBoundary
= bytePerLine
% sizeDWORD
;
824 if( lineBoundary
> 0 )
826 padding
= sizeDWORD
- lineBoundary
;
827 bytePerLine
+= padding
;
830 // create a DIB header
831 int headersize
= sizeof(BITMAPINFOHEADER
);
832 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
835 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
839 // Fill in the DIB header
840 lpDIBh
->bmiHeader
.biSize
= headersize
;
841 lpDIBh
->bmiHeader
.biWidth
= width
;
842 lpDIBh
->bmiHeader
.biHeight
= -height
;
843 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
844 lpDIBh
->bmiHeader
.biPlanes
= 1;
845 lpDIBh
->bmiHeader
.biBitCount
= 24;
846 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
847 lpDIBh
->bmiHeader
.biClrUsed
= 0;
848 // These seem not really needed for our purpose here.
849 lpDIBh
->bmiHeader
.biClrImportant
= 0;
850 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
851 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
852 // memory for DIB data
853 unsigned char *lpBits
;
854 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
857 wxFAIL_MSG( wxT("could not allocate data for DIB") );
863 // copy data from the device-dependent bitmap to the DIB
864 HDC hdc
= ::GetDC(NULL
);
866 hbitmap
= (HBITMAP
) GetHBITMAP();
867 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
869 // copy DIB data into the wxImage object
871 unsigned char *ptdata
= data
;
872 unsigned char *ptbits
= lpBits
;
873 for( i
=0; i
<height
; i
++ )
875 for( j
=0; j
<width
; j
++ )
877 *(ptdata
++) = *(ptbits
+2);
878 *(ptdata
++) = *(ptbits
+1);
879 *(ptdata
++) = *(ptbits
);
885 // similarly, set data according to the possible mask bitmap
886 if( GetMask() && GetMask()->GetMaskBitmap() )
888 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
889 // memory DC created, color set, data copied, and memory DC deleted
890 HDC memdc
= ::CreateCompatibleDC( hdc
);
891 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
892 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
893 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
895 // background color set to RGB(16,16,16) in consistent with wxGTK
896 unsigned char r
=16, g
=16, b
=16;
899 for( i
=0; i
<height
; i
++ )
901 for( j
=0; j
<width
; j
++ )
915 image
.SetMaskColour( r
, g
, b
);
916 image
.SetMask( TRUE
);
920 image
.SetMask( FALSE
);
922 // free allocated resources
923 ::ReleaseDC(NULL
, hdc
);
930 #endif // wxUSE_IMAGE
932 // ----------------------------------------------------------------------------
933 // loading and saving bitmaps
934 // ----------------------------------------------------------------------------
936 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
940 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
944 m_refData
= new wxBitmapRefData
;
946 return handler
->LoadFile(this, filename
, type
, -1, -1);
952 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
954 *this = wxBitmap(image
);
959 #endif // wxUSE_IMAGE
964 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
968 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
972 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
977 m_refData
= new wxBitmapRefData
;
979 return handler
->Create(this, data
, type
, width
, height
, depth
);
982 bool wxBitmap::SaveFile(const wxString
& filename
,
984 const wxPalette
*palette
)
986 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
990 return handler
->SaveFile(this, filename
, type
, palette
);
995 // FIXME what about palette? shouldn't we use it?
996 wxImage image
= ConvertToImage();
999 return image
.SaveFile(filename
, type
);
1002 #endif // wxUSE_IMAGE
1007 // ----------------------------------------------------------------------------
1008 // sub bitmap extraction
1009 // ----------------------------------------------------------------------------
1011 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1013 wxCHECK_MSG( Ok() &&
1014 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1015 (rect
.x
+rect
.width
<= GetWidth()) &&
1016 (rect
.y
+rect
.height
<= GetHeight()),
1017 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1019 wxBitmap
ret( rect
.width
, rect
.height
);
1020 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1022 #ifndef __WXMICROWIN__
1023 // TODO: copy alpha channel data if any
1030 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1031 selectDst(dcDst
, GetHbitmapOf(ret
));
1033 if ( !selectSrc
|| !selectDst
)
1035 wxLogLastError(_T("SelectObjct(hBitmap)"));
1038 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1039 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1041 wxLogLastError(_T("BitBlt"));
1045 // copy mask if there is one
1048 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1050 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1051 selectDst(dcDst
, hbmpMask
);
1053 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1054 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1056 wxLogLastError(_T("BitBlt"));
1059 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1062 #endif // !__WXMICROWIN__
1067 // ----------------------------------------------------------------------------
1068 // wxBitmap accessors
1069 // ----------------------------------------------------------------------------
1071 wxPalette
* wxBitmap::GetPalette() const
1073 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1074 : (wxPalette
*) NULL
;
1077 wxMask
*wxBitmap::GetMask() const
1079 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1084 wxDC
*wxBitmap::GetSelectedInto() const
1086 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1091 #if WXWIN_COMPATIBILITY_2_4
1093 int wxBitmap::GetQuality() const
1098 #endif // WXWIN_COMPATIBILITY_2_4
1100 bool wxBitmap::HasAlpha() const
1102 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1105 // ----------------------------------------------------------------------------
1107 // ----------------------------------------------------------------------------
1111 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1113 if ( GetBitmapData() )
1114 GetBitmapData()->m_selectedInto
= dc
;
1121 void wxBitmap::SetPalette(const wxPalette
& palette
)
1125 GetBitmapData()->m_bitmapPalette
= palette
;
1128 #endif // wxUSE_PALETTE
1130 void wxBitmap::SetMask(wxMask
*mask
)
1134 GetBitmapData()->SetMask(mask
);
1137 #if WXWIN_COMPATIBILITY_2
1139 void wxBitmap::SetOk(bool isOk
)
1143 GetBitmapData()->m_ok
= isOk
;
1146 #endif // WXWIN_COMPATIBILITY_2
1148 #if WXWIN_COMPATIBILITY_2_4
1150 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1154 #endif // WXWIN_COMPATIBILITY_2_4
1156 // ----------------------------------------------------------------------------
1157 // raw bitmap access support
1158 // ----------------------------------------------------------------------------
1160 bool wxBitmap::GetRawData(wxRawBitmapData
*data
)
1162 wxCHECK_MSG( data
, FALSE
, _T("NULL pointer in wxBitmap::GetRawData") );
1166 // no bitmap, no data (raw or otherwise)
1170 // we only support raw access to the DIBs, so check if we have one
1172 if ( !::GetObject(GetHbitmap(), sizeof(ds
), &ds
) )
1177 // ok, store the relevant info in wxRawBitmapData
1178 const LONG h
= ds
.dsBm
.bmHeight
;
1180 data
->m_width
= ds
.dsBm
.bmWidth
;
1182 data
->m_bypp
= ds
.dsBm
.bmBitsPixel
/ 8;
1184 // remember that DIBs are stored in top to bottom order!
1185 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1186 data
->m_stride
= -bytesPerRow
;
1187 data
->m_pixels
= (unsigned char *)ds
.dsBm
.bmBits
;
1190 data
->m_pixels
+= (h
- 1)*bytesPerRow
;
1196 // ----------------------------------------------------------------------------
1197 // TODO: to be replaced by something better
1198 // ----------------------------------------------------------------------------
1200 // Creates a bitmap that matches the device context, from
1201 // an arbitray bitmap. At present, the original bitmap must have an
1202 // associated palette. TODO: use a default palette if no palette exists.
1203 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1204 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1206 #ifdef __WXMICROWIN__
1210 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1211 HPALETTE hPal
= (HPALETTE
) NULL
;
1213 void *lpBits
= (void*) NULL
;
1216 if( GetPalette() && GetPalette()->Ok() )
1218 tmpBitmap
.SetPalette(*GetPalette());
1219 memDC
.SelectObject(tmpBitmap
);
1220 memDC
.SetPalette(*GetPalette());
1221 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1225 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1227 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1228 tmpBitmap
.SetPalette( palette
);
1229 memDC
.SelectObject(tmpBitmap
);
1230 memDC
.SetPalette( palette
);
1232 #else // !wxUSE_PALETTE
1233 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1234 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1236 // set the height negative because in a DIB the order of the lines is
1238 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1240 return wxNullBitmap
;
1243 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1245 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1247 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1248 GetWidth(), GetHeight(),
1249 0, 0, 0, GetHeight(),
1250 lpBits
, lpDib
, DIB_RGB_COLORS
);
1260 // ----------------------------------------------------------------------------
1262 // ----------------------------------------------------------------------------
1269 // Construct a mask from a bitmap and a colour indicating
1270 // the transparent area
1271 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1274 Create(bitmap
, colour
);
1277 // Construct a mask from a bitmap and a palette index indicating
1278 // the transparent area
1279 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1282 Create(bitmap
, paletteIndex
);
1285 // Construct a mask from a mono bitmap (copies the bitmap).
1286 wxMask::wxMask(const wxBitmap
& bitmap
)
1295 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1298 // Create a mask from a mono bitmap (copies the bitmap).
1299 bool wxMask::Create(const wxBitmap
& bitmap
)
1301 #ifndef __WXMICROWIN__
1302 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1303 _T("can't create mask from invalid or not monochrome bitmap") );
1307 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1311 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1316 HDC srcDC
= CreateCompatibleDC(0);
1317 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1318 HDC destDC
= CreateCompatibleDC(0);
1319 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1320 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1321 SelectObject(srcDC
, 0);
1323 SelectObject(destDC
, 0);
1331 // Create a mask from a bitmap and a palette index indicating
1332 // the transparent area
1333 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1337 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1342 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1344 unsigned char red
, green
, blue
;
1345 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1347 wxColour
transparentColour(red
, green
, blue
);
1348 return Create(bitmap
, transparentColour
);
1351 #endif // wxUSE_PALETTE
1356 // Create a mask from a bitmap and a colour indicating
1357 // the transparent area
1358 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1360 #ifndef __WXMICROWIN__
1361 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1365 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1369 int width
= bitmap
.GetWidth(),
1370 height
= bitmap
.GetHeight();
1372 // scan the bitmap for the transparent colour and set the corresponding
1373 // pixels in the mask to BLACK and the rest to WHITE
1374 COLORREF maskColour
= wxColourToPalRGB(colour
);
1375 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1377 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1378 HDC destDC
= ::CreateCompatibleDC(NULL
);
1379 if ( !srcDC
|| !destDC
)
1381 wxLogLastError(wxT("CreateCompatibleDC"));
1386 // SelectObject() will fail
1387 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1388 _T("bitmap can't be selected in another DC") );
1390 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1393 wxLogLastError(wxT("SelectObject"));
1398 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1401 wxLogLastError(wxT("SelectObject"));
1408 // this will create a monochrome bitmap with 0 points for the pixels
1409 // which have the same value as the background colour and 1 for the
1411 ::SetBkColor(srcDC
, maskColour
);
1412 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1415 ::SelectObject(srcDC
, hbmpSrcOld
);
1417 ::SelectObject(destDC
, hbmpDstOld
);
1421 #else // __WXMICROWIN__
1423 #endif // __WXMICROWIN__/!__WXMICROWIN__
1426 // ----------------------------------------------------------------------------
1428 // ----------------------------------------------------------------------------
1430 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1433 int width
, int height
, int depth
)
1435 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1437 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1440 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1441 const wxString
& name
,
1443 int width
, int height
)
1445 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1447 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1450 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1451 const wxString
& name
,
1454 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1456 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1459 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1460 void *WXUNUSED(data
),
1461 long WXUNUSED(type
),
1462 int WXUNUSED(width
),
1463 int WXUNUSED(height
),
1464 int WXUNUSED(depth
))
1469 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1470 const wxString
& WXUNUSED(name
),
1471 long WXUNUSED(type
),
1472 int WXUNUSED(desiredWidth
),
1473 int WXUNUSED(desiredHeight
))
1478 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1479 const wxString
& WXUNUSED(name
),
1481 const wxPalette
*WXUNUSED(palette
))
1486 // ----------------------------------------------------------------------------
1488 // ----------------------------------------------------------------------------
1490 #ifndef __WXMICROWIN__
1491 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1492 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1494 unsigned long i
, headerSize
;
1495 LPBITMAPINFO lpDIBheader
= NULL
;
1496 LPPALETTEENTRY lpPe
= NULL
;
1499 // Allocate space for a DIB header
1500 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1501 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1502 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1504 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1506 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1508 // Fill in the static parts of the DIB header
1509 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1510 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1511 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1512 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1514 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1515 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1516 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1517 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1518 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1521 // Initialize the DIB palette
1522 for (i
= 0; i
< 256; i
++) {
1523 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1524 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1525 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1526 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1529 *lpDIBHeader
= lpDIBheader
;
1534 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1540 // ----------------------------------------------------------------------------
1541 // global helper functions implemented here
1542 // ----------------------------------------------------------------------------
1544 // helper of wxBitmapToHICON/HCURSOR
1546 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1553 // we can't create an icon/cursor form nothing
1557 wxMask
*mask
= bmp
.GetMask();
1560 // we must have a mask for an icon, so even if it's probably incorrect,
1561 // do create it (grey is the "standard" transparent colour)
1562 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1566 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1569 iconInfo
.xHotspot
= hotSpotX
;
1570 iconInfo
.yHotspot
= hotSpotY
;
1573 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1574 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1576 // black out the transparent area to preserve background colour, because
1577 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1578 // the mask to the dest rect.
1580 MemoryHDC dcSrc
, dcDst
;
1581 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1582 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1584 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1585 dcSrc
, 0, 0, SRCAND
) )
1587 wxLogLastError(_T("BitBlt"));
1591 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1593 if ( !bmp
.GetMask() )
1595 // we created the mask, now delete it
1599 // delete the inverted mask bitmap we created as well
1600 ::DeleteObject(iconInfo
.hbmMask
);
1605 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1607 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1610 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1612 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1615 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1617 #ifndef __WXMICROWIN__
1618 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1620 // get width/height from the bitmap if not given
1624 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1629 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1630 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1631 if ( !hdcSrc
|| !hdcDst
)
1633 wxLogLastError(wxT("CreateCompatibleDC"));
1636 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1639 wxLogLastError(wxT("CreateBitmap"));
1642 ::SelectObject(hdcSrc
, hbmpMask
);
1643 ::SelectObject(hdcDst
, hbmpInvMask
);
1644 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1648 wxLogLastError(wxT("BitBlt"));