1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
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 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
66 virtual ~wxBitmapRefData() { Free(); }
70 // set the mask object to use as the mask, we take ownership of it
71 void SetMask(wxMask
*mask
)
77 // set the HBITMAP to use as the mask
78 void SetMask(HBITMAP hbmpMask
)
80 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
84 wxMask
*GetMask() const { return m_bitmapMask
; }
88 wxPalette m_bitmapPalette
;
89 #endif // wxUSE_PALETTE
95 // this field is solely for error checking: we detect selecting a bitmap
96 // into more than one DC at once or deleting a bitmap still selected into a
97 // DC (both are serious programming errors under Windows)
101 // true if we have alpha transparency info and can be drawn using
106 // optional mask for transparent drawing
107 wxMask
*m_bitmapMask
;
109 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
117 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
119 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 wxBitmapRefData::wxBitmapRefData()
131 m_selectedInto
= NULL
;
133 m_hBitmap
= (WXHBITMAP
) NULL
;
137 void wxBitmapRefData::Free()
139 wxASSERT_MSG( !m_selectedInto
,
140 wxT("deleting bitmap still selected into wxMemoryDC") );
144 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
146 wxLogLastError(wxT("DeleteObject(hbitmap)"));
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 // this function should be called from all wxBitmap ctors
159 void wxBitmap::Init()
161 // m_refData = NULL; done in the base class ctor
165 wxGDIImageRefData
*wxBitmap::CreateData() const
167 return new wxBitmapRefData
;
172 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
174 #ifndef __WXMICROWIN__
175 // it may be either HICON or HCURSOR
176 HICON hicon
= (HICON
)icon
.GetHandle();
179 if ( !::GetIconInfo(hicon
, &iconInfo
) )
181 wxLogLastError(wxT("GetIconInfo"));
186 wxBitmapRefData
*refData
= new wxBitmapRefData
;
189 int w
= icon
.GetWidth(),
190 h
= icon
.GetHeight();
192 refData
->m_width
= w
;
193 refData
->m_height
= h
;
194 refData
->m_depth
= wxDisplayDepth();
196 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
198 // the mask returned by GetIconInfo() is inversed compared to the usual
200 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
203 // delete the old one now as we don't need it any more
204 ::DeleteObject(iconInfo
.hbmMask
);
206 #if WXWIN_COMPATIBILITY_2
207 refData
->m_ok
= TRUE
;
208 #endif // WXWIN_COMPATIBILITY_2
218 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
226 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
230 return CopyFromIconOrCursor(cursor
);
234 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
241 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
242 // to create a bitmap from icon there - but using this way we won't have
245 int width
= icon
.GetWidth(),
246 height
= icon
.GetHeight();
248 // copy the icon to the bitmap
250 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
251 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
252 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
254 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
256 ::SelectObject(hdc
, hbmpOld
);
259 wxBitmapRefData
*refData
= new wxBitmapRefData
;
262 refData
->m_width
= width
;
263 refData
->m_height
= height
;
264 refData
->m_depth
= wxDisplayDepth();
266 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
268 #if WXWIN_COMPATIBILITY_2
269 refData
->m_ok
= TRUE
;
270 #endif // WXWIN_COMPATIBILITY_2
274 return CopyFromIconOrCursor(icon
);
275 #endif // Win16/Win32
278 wxBitmap::~wxBitmap()
282 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
286 #ifndef __WXMICROWIN__
287 wxBitmapRefData
*refData
= new wxBitmapRefData
;
290 refData
->m_width
= width
;
291 refData
->m_height
= height
;
292 refData
->m_depth
= depth
;
297 // we assume that it is in XBM format which is not quite the same as
298 // the format CreateBitmap() wants because the order of bytes in the
300 const size_t bytesPerLine
= (width
+ 7) / 8;
301 const size_t padding
= bytesPerLine
% 2;
302 const size_t len
= height
* ( padding
+ bytesPerLine
);
303 data
= (char *)malloc(len
);
304 const char *src
= bits
;
307 for ( int rows
= 0; rows
< height
; rows
++ )
309 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
311 unsigned char val
= *src
++;
312 unsigned char reversed
= 0;
314 for ( int bits
= 0; bits
< 8; bits
++)
317 reversed
|= (val
& 0x01);
329 // bits should already be in Windows standard format
330 data
= (char *)bits
; // const_cast is harmless
333 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
336 wxLogLastError(wxT("CreateBitmap"));
344 SetHBITMAP((WXHBITMAP
)hbmp
);
348 // Create from XPM data
349 bool wxBitmap::CreateFromXpm(const char **data
)
351 #if wxUSE_IMAGE && wxUSE_XPM
354 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
356 wxXPMDecoder decoder
;
357 wxImage img
= decoder
.ReadData(data
);
358 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
360 *this = wxBitmap(img
);
367 wxBitmap::wxBitmap(int w
, int h
, int d
)
371 (void)Create(w
, h
, d
);
374 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
378 (void)Create(data
, type
, width
, height
, depth
);
381 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
385 LoadFile(filename
, (int)type
);
388 bool wxBitmap::Create(int w
, int h
, int d
)
392 m_refData
= new wxBitmapRefData
;
394 #if wxUSE_DIB_FOR_BITMAP
395 if ( w
&& h
&& d
>= 16 )
397 if ( !CreateDIB(w
, h
, d
) )
401 #endif // wxUSE_DIB_FOR_BITMAP
403 GetBitmapData()->m_width
= w
;
404 GetBitmapData()->m_height
= h
;
405 GetBitmapData()->m_depth
= d
;
408 #ifndef __WXMICROWIN__
411 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
414 wxLogLastError(wxT("CreateBitmap"));
418 #endif // !__WXMICROWIN__
421 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
424 wxLogLastError(wxT("CreateCompatibleBitmap"));
427 GetBitmapData()->m_depth
= wxDisplayDepth();
430 SetHBITMAP((WXHBITMAP
)hbmp
);
432 #if WXWIN_COMPATIBILITY_2
433 GetBitmapData()->m_ok
= hbmp
!= 0;
434 #endif // WXWIN_COMPATIBILITY_2
442 // ----------------------------------------------------------------------------
443 // wxImage to/from conversions for Microwin
444 // ----------------------------------------------------------------------------
446 // Microwin versions are so different from normal ones that it really doesn't
447 // make sense to use #ifdefs inside the function bodies
448 #ifdef __WXMICROWIN__
450 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
452 // Set this to 1 to experiment with mask code,
453 // which currently doesn't work
456 m_refData
= new wxBitmapRefData();
458 // Initial attempt at a simple-minded implementation.
459 // The bitmap will always be created at the screen depth,
460 // so the 'depth' argument is ignored.
462 HDC hScreenDC
= ::GetDC(NULL
);
463 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
465 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
466 HBITMAP hMaskBitmap
= NULL
;
467 HBITMAP hOldMaskBitmap
= NULL
;
469 unsigned char maskR
= 0;
470 unsigned char maskG
= 0;
471 unsigned char maskB
= 0;
473 // printf("Created bitmap %d\n", (int) hBitmap);
476 ::ReleaseDC(NULL
, hScreenDC
);
479 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
481 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
482 ::ReleaseDC(NULL
, hScreenDC
);
484 // created an mono-bitmap for the possible mask
485 bool hasMask
= image
.HasMask();
490 // FIXME: we should be able to pass bpp = 1, but
491 // GdBlit can't handle a different depth
493 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
495 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
497 maskR
= image
.GetMaskRed();
498 maskG
= image
.GetMaskGreen();
499 maskB
= image
.GetMaskBlue();
507 hScreenDC
= ::GetDC(NULL
);
508 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
509 ::ReleaseDC(NULL
, hScreenDC
);
511 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
519 for (i
= 0; i
< image
.GetWidth(); i
++)
521 for (j
= 0; j
< image
.GetHeight(); j
++)
523 unsigned char red
= image
.GetRed(i
, j
);
524 unsigned char green
= image
.GetGreen(i
, j
);
525 unsigned char blue
= image
.GetBlue(i
, j
);
527 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
531 // scan the bitmap for the transparent colour and set the corresponding
532 // pixels in the mask to BLACK and the rest to WHITE
533 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
534 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
536 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
541 ::SelectObject(hMemDC
, hOldBitmap
);
545 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
548 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
551 SetWidth(image
.GetWidth());
552 SetHeight(image
.GetHeight());
553 SetDepth(screenDepth
);
554 SetHBITMAP( (WXHBITMAP
) hBitmap
);
557 // Copy the palette from the source image
558 SetPalette(image
.GetPalette());
559 #endif // wxUSE_PALETTE
561 #if WXWIN_COMPATIBILITY_2
562 // check the wxBitmap object
563 GetBitmapData()->SetOk();
564 #endif // WXWIN_COMPATIBILITY_2
569 wxImage
wxBitmap::ConvertToImage() const
571 // Initial attempt at a simple-minded implementation.
572 // The bitmap will always be created at the screen depth,
573 // so the 'depth' argument is ignored.
574 // TODO: transparency (create a mask image)
578 wxFAIL_MSG( wxT("bitmap is invalid") );
584 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
586 // create an wxImage object
587 int width
= GetWidth();
588 int height
= GetHeight();
589 image
.Create( width
, height
);
590 unsigned char *data
= image
.GetData();
593 wxFAIL_MSG( wxT("could not allocate data for image") );
597 HDC hScreenDC
= ::GetDC(NULL
);
599 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
600 ::ReleaseDC(NULL
, hScreenDC
);
602 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
604 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
607 for (i
= 0; i
< GetWidth(); i
++)
609 for (j
= 0; j
< GetHeight(); j
++)
611 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
612 unsigned char red
= GetRValue(color
);
613 unsigned char green
= GetGValue(color
);
614 unsigned char blue
= GetBValue(color
);
616 image
.SetRGB(i
, j
, red
, green
, blue
);
620 ::SelectObject(hMemDC
, hOldBitmap
);
624 // Copy the palette from the source image
626 image
.SetPalette(* GetPalette());
627 #endif // wxUSE_PALETTE
632 #endif // __WXMICROWIN__
634 // ----------------------------------------------------------------------------
635 // wxImage to/from conversions
636 // ----------------------------------------------------------------------------
638 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
640 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
644 // first convert the image to DIB
645 const int h
= image
.GetHeight();
646 const int w
= image
.GetWidth();
653 // store the bitmap parameters
654 wxBitmapRefData
*refData
= new wxBitmapRefData
;
655 refData
->m_width
= w
;
656 refData
->m_height
= h
;
657 refData
->m_depth
= dib
.GetDepth();
658 refData
->m_hasAlpha
= image
.HasAlpha();
663 // next either store DIB as is or create a DDB from it
666 // TODO: if we're ready to use DIB as is, we can just do this:
668 // hbitmap = dib.Detach();
671 // create and set the device-dependent bitmap
673 // VZ: why don't we just use SetDIBits() instead? because of the
674 // palette or is there some other reason?
675 hbitmap
= ::CreateCompatibleBitmap(ScreenHDC(), w
, h
);
678 wxLogLastError(_T("CreateCompatibleBitmap()"));
684 SelectInHDC
select(hdcMem
, hbitmap
);
687 wxLogLastError(_T("SelectObjct(hBitmap)"));
691 const wxPalette
& palette
= image
.GetPalette();
693 HPALETTE hOldPalette
;
698 hOldPalette
= ::SelectPalette
701 GetHpaletteOf(palette
),
702 FALSE
// ignored for hdcMem
707 wxLogLastError(_T("SelectPalette()"));
710 if ( ::RealizePalette(hdcMem
) == GDI_ERROR
)
712 wxLogLastError(_T("RealizePalette()"));
715 else // no valid palette
719 #endif // wxUSE_PALETTE
722 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
724 wxLogLastError(_T("GetObject(hDIB)"));
727 if ( ::StretchDIBits(hdcMem
,
731 (BITMAPINFO
*)&ds
.dsBmih
,
733 SRCCOPY
) == GDI_ERROR
)
735 wxLogLastError(_T("StretchDIBits()"));
743 ::SelectPalette(hdcMem
, hOldPalette
, FALSE
);
745 #endif // wxUSE_PALETTE
748 // validate this object
749 SetHBITMAP((WXHBITMAP
)hbitmap
);
751 #if WXWIN_COMPATIBILITY_2
752 m_refData
->m_ok
= TRUE
;
753 #endif // WXWIN_COMPATIBILITY_2
755 // finally also set the mask if we have one
756 if ( image
.HasMask() )
758 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
759 image
.GetMaskGreen(),
760 image
.GetMaskBlue())));
766 wxImage
wxBitmap::ConvertToImage() const
770 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
772 // create an wxImage object
773 int width
= GetWidth();
774 int height
= GetHeight();
775 image
.Create( width
, height
);
776 unsigned char *data
= image
.GetData();
779 wxFAIL_MSG( wxT("could not allocate data for image") );
783 // calc the number of bytes per scanline and padding in the DIB
784 int bytePerLine
= width
*3;
785 int sizeDWORD
= sizeof( DWORD
);
786 int lineBoundary
= bytePerLine
% sizeDWORD
;
788 if( lineBoundary
> 0 )
790 padding
= sizeDWORD
- lineBoundary
;
791 bytePerLine
+= padding
;
794 // create a DIB header
795 int headersize
= sizeof(BITMAPINFOHEADER
);
796 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
799 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
803 // Fill in the DIB header
804 lpDIBh
->bmiHeader
.biSize
= headersize
;
805 lpDIBh
->bmiHeader
.biWidth
= width
;
806 lpDIBh
->bmiHeader
.biHeight
= -height
;
807 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
808 lpDIBh
->bmiHeader
.biPlanes
= 1;
809 lpDIBh
->bmiHeader
.biBitCount
= 24;
810 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
811 lpDIBh
->bmiHeader
.biClrUsed
= 0;
812 // These seem not really needed for our purpose here.
813 lpDIBh
->bmiHeader
.biClrImportant
= 0;
814 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
815 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
816 // memory for DIB data
817 unsigned char *lpBits
;
818 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
821 wxFAIL_MSG( wxT("could not allocate data for DIB") );
827 // copy data from the device-dependent bitmap to the DIB
828 HDC hdc
= ::GetDC(NULL
);
830 hbitmap
= (HBITMAP
) GetHBITMAP();
831 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
833 // copy DIB data into the wxImage object
835 unsigned char *ptdata
= data
;
836 unsigned char *ptbits
= lpBits
;
837 for( i
=0; i
<height
; i
++ )
839 for( j
=0; j
<width
; j
++ )
841 *(ptdata
++) = *(ptbits
+2);
842 *(ptdata
++) = *(ptbits
+1);
843 *(ptdata
++) = *(ptbits
);
849 // similarly, set data according to the possible mask bitmap
850 if( GetMask() && GetMask()->GetMaskBitmap() )
852 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
853 // memory DC created, color set, data copied, and memory DC deleted
854 HDC memdc
= ::CreateCompatibleDC( hdc
);
855 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
856 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
857 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
859 // background color set to RGB(16,16,16) in consistent with wxGTK
860 unsigned char r
=16, g
=16, b
=16;
863 for( i
=0; i
<height
; i
++ )
865 for( j
=0; j
<width
; j
++ )
879 image
.SetMaskColour( r
, g
, b
);
880 image
.SetMask( TRUE
);
884 image
.SetMask( FALSE
);
886 // free allocated resources
887 ::ReleaseDC(NULL
, hdc
);
894 #endif // wxUSE_IMAGE
896 // ----------------------------------------------------------------------------
897 // loading and saving bitmaps
898 // ----------------------------------------------------------------------------
900 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
904 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
908 m_refData
= new wxBitmapRefData
;
910 return handler
->LoadFile(this, filename
, type
, -1, -1);
916 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
918 *this = wxBitmap(image
);
923 #endif // wxUSE_IMAGE
928 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
932 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
936 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
941 m_refData
= new wxBitmapRefData
;
943 return handler
->Create(this, data
, type
, width
, height
, depth
);
946 bool wxBitmap::SaveFile(const wxString
& filename
,
948 const wxPalette
*palette
)
950 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
954 return handler
->SaveFile(this, filename
, type
, palette
);
959 // FIXME what about palette? shouldn't we use it?
960 wxImage image
= ConvertToImage();
963 return image
.SaveFile(filename
, type
);
966 #endif // wxUSE_IMAGE
971 // ----------------------------------------------------------------------------
972 // sub bitmap extraction
973 // ----------------------------------------------------------------------------
975 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
978 (rect
.x
>= 0) && (rect
.y
>= 0) &&
979 (rect
.x
+rect
.width
<= GetWidth()) &&
980 (rect
.y
+rect
.height
<= GetHeight()),
981 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
983 wxBitmap
ret( rect
.width
, rect
.height
);
984 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
986 #ifndef __WXMICROWIN__
987 // TODO: copy alpha channel data if any
994 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
995 selectDst(dcDst
, GetHbitmapOf(ret
));
997 if ( !selectSrc
|| !selectDst
)
999 wxLogLastError(_T("SelectObjct(hBitmap)"));
1002 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1003 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1005 wxLogLastError(_T("BitBlt"));
1009 // copy mask if there is one
1012 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1014 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1015 selectDst(dcDst
, hbmpMask
);
1017 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1018 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1020 wxLogLastError(_T("BitBlt"));
1023 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1026 #endif // !__WXMICROWIN__
1031 // ----------------------------------------------------------------------------
1032 // wxBitmap accessors
1033 // ----------------------------------------------------------------------------
1035 wxPalette
* wxBitmap::GetPalette() const
1037 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1038 : (wxPalette
*) NULL
;
1041 wxMask
*wxBitmap::GetMask() const
1043 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1046 wxDC
*wxBitmap::GetSelectedInto() const
1048 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1051 #if WXWIN_COMPATIBILITY_2_4
1053 int wxBitmap::GetQuality() const
1058 #endif // WXWIN_COMPATIBILITY_2_4
1060 bool wxBitmap::HasAlpha() const
1062 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1069 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1071 if ( GetBitmapData() )
1072 GetBitmapData()->m_selectedInto
= dc
;
1077 void wxBitmap::SetPalette(const wxPalette
& palette
)
1081 GetBitmapData()->m_bitmapPalette
= palette
;
1084 #endif // wxUSE_PALETTE
1086 void wxBitmap::SetMask(wxMask
*mask
)
1090 GetBitmapData()->SetMask(mask
);
1093 #if WXWIN_COMPATIBILITY_2
1095 void wxBitmap::SetOk(bool isOk
)
1099 GetBitmapData()->m_ok
= isOk
;
1102 #endif // WXWIN_COMPATIBILITY_2
1104 #if WXWIN_COMPATIBILITY_2_4
1106 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1110 #endif // WXWIN_COMPATIBILITY_2_4
1112 // ----------------------------------------------------------------------------
1113 // TODO: to be replaced by something better
1114 // ----------------------------------------------------------------------------
1116 // Creates a bitmap that matches the device context, from
1117 // an arbitray bitmap. At present, the original bitmap must have an
1118 // associated palette. TODO: use a default palette if no palette exists.
1119 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1120 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1122 #ifdef __WXMICROWIN__
1126 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1127 HPALETTE hPal
= (HPALETTE
) NULL
;
1129 void *lpBits
= (void*) NULL
;
1132 if( GetPalette() && GetPalette()->Ok() )
1134 tmpBitmap
.SetPalette(*GetPalette());
1135 memDC
.SelectObject(tmpBitmap
);
1136 memDC
.SetPalette(*GetPalette());
1137 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1141 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1143 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1144 tmpBitmap
.SetPalette( palette
);
1145 memDC
.SelectObject(tmpBitmap
);
1146 memDC
.SetPalette( palette
);
1148 #else // !wxUSE_PALETTE
1149 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1150 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1152 // set the height negative because in a DIB the order of the lines is
1154 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1156 return wxNullBitmap
;
1159 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1161 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1163 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1164 GetWidth(), GetHeight(),
1165 0, 0, 0, GetHeight(),
1166 lpBits
, lpDib
, DIB_RGB_COLORS
);
1176 // ----------------------------------------------------------------------------
1178 // ----------------------------------------------------------------------------
1185 // Construct a mask from a bitmap and a colour indicating
1186 // the transparent area
1187 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1190 Create(bitmap
, colour
);
1193 // Construct a mask from a bitmap and a palette index indicating
1194 // the transparent area
1195 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1198 Create(bitmap
, paletteIndex
);
1201 // Construct a mask from a mono bitmap (copies the bitmap).
1202 wxMask::wxMask(const wxBitmap
& bitmap
)
1211 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1214 // Create a mask from a mono bitmap (copies the bitmap).
1215 bool wxMask::Create(const wxBitmap
& bitmap
)
1217 #ifndef __WXMICROWIN__
1218 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1219 _T("can't create mask from invalid or not monochrome bitmap") );
1223 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1227 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1232 HDC srcDC
= CreateCompatibleDC(0);
1233 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1234 HDC destDC
= CreateCompatibleDC(0);
1235 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1236 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1237 SelectObject(srcDC
, 0);
1239 SelectObject(destDC
, 0);
1247 // Create a mask from a bitmap and a palette index indicating
1248 // the transparent area
1249 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1253 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1258 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1260 unsigned char red
, green
, blue
;
1261 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1263 wxColour
transparentColour(red
, green
, blue
);
1264 return Create(bitmap
, transparentColour
);
1267 #endif // wxUSE_PALETTE
1272 // Create a mask from a bitmap and a colour indicating
1273 // the transparent area
1274 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1276 #ifndef __WXMICROWIN__
1277 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1281 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1285 int width
= bitmap
.GetWidth(),
1286 height
= bitmap
.GetHeight();
1288 // scan the bitmap for the transparent colour and set the corresponding
1289 // pixels in the mask to BLACK and the rest to WHITE
1290 COLORREF maskColour
= wxColourToPalRGB(colour
);
1291 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1293 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1294 HDC destDC
= ::CreateCompatibleDC(NULL
);
1295 if ( !srcDC
|| !destDC
)
1297 wxLogLastError(wxT("CreateCompatibleDC"));
1302 // SelectObject() will fail
1303 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1304 _T("bitmap can't be selected in another DC") );
1306 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1309 wxLogLastError(wxT("SelectObject"));
1314 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1317 wxLogLastError(wxT("SelectObject"));
1324 // this will create a monochrome bitmap with 0 points for the pixels
1325 // which have the same value as the background colour and 1 for the
1327 ::SetBkColor(srcDC
, maskColour
);
1328 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1331 ::SelectObject(srcDC
, hbmpSrcOld
);
1333 ::SelectObject(destDC
, hbmpDstOld
);
1337 #else // __WXMICROWIN__
1339 #endif // __WXMICROWIN__/!__WXMICROWIN__
1342 // ----------------------------------------------------------------------------
1344 // ----------------------------------------------------------------------------
1346 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1349 int width
, int height
, int depth
)
1351 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1353 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1356 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1357 const wxString
& name
,
1359 int width
, int height
)
1361 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1363 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1366 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1367 const wxString
& name
,
1370 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1372 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1375 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1376 void *WXUNUSED(data
),
1377 long WXUNUSED(type
),
1378 int WXUNUSED(width
),
1379 int WXUNUSED(height
),
1380 int WXUNUSED(depth
))
1385 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1386 const wxString
& WXUNUSED(name
),
1387 long WXUNUSED(type
),
1388 int WXUNUSED(desiredWidth
),
1389 int WXUNUSED(desiredHeight
))
1394 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1395 const wxString
& WXUNUSED(name
),
1397 const wxPalette
*WXUNUSED(palette
))
1402 // ----------------------------------------------------------------------------
1404 // ----------------------------------------------------------------------------
1406 #ifndef __WXMICROWIN__
1407 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1408 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1410 unsigned long i
, headerSize
;
1411 LPBITMAPINFO lpDIBheader
= NULL
;
1412 LPPALETTEENTRY lpPe
= NULL
;
1415 // Allocate space for a DIB header
1416 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1417 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1418 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1420 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1422 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1424 // Fill in the static parts of the DIB header
1425 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1426 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1427 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1428 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1430 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1431 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1432 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1433 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1434 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1437 // Initialize the DIB palette
1438 for (i
= 0; i
< 256; i
++) {
1439 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1440 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1441 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1442 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1445 *lpDIBHeader
= lpDIBheader
;
1450 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1456 // ----------------------------------------------------------------------------
1457 // other helper functions
1458 // ----------------------------------------------------------------------------
1460 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1462 #ifndef __WXMICROWIN__
1463 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1465 // get width/height from the bitmap if not given
1469 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1474 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1475 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1476 if ( !hdcSrc
|| !hdcDst
)
1478 wxLogLastError(wxT("CreateCompatibleDC"));
1481 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1484 wxLogLastError(wxT("CreateBitmap"));
1487 ::SelectObject(hdcSrc
, hbmpMask
);
1488 ::SelectObject(hdcDst
, hbmpInvMask
);
1489 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1493 wxLogLastError(wxT("BitBlt"));