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()
132 m_selectedInto
= NULL
;
135 m_hBitmap
= (WXHBITMAP
) NULL
;
139 void wxBitmapRefData::Free()
141 wxASSERT_MSG( !m_selectedInto
,
142 wxT("deleting bitmap still selected into wxMemoryDC") );
146 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
148 wxLogLastError(wxT("DeleteObject(hbitmap)"));
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
160 // this function should be called from all wxBitmap ctors
161 void wxBitmap::Init()
163 // m_refData = NULL; done in the base class ctor
167 wxGDIImageRefData
*wxBitmap::CreateData() const
169 return new wxBitmapRefData
;
174 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
176 #ifndef __WXMICROWIN__
177 // it may be either HICON or HCURSOR
178 HICON hicon
= (HICON
)icon
.GetHandle();
181 if ( !::GetIconInfo(hicon
, &iconInfo
) )
183 wxLogLastError(wxT("GetIconInfo"));
188 wxBitmapRefData
*refData
= new wxBitmapRefData
;
191 int w
= icon
.GetWidth(),
192 h
= icon
.GetHeight();
194 refData
->m_width
= w
;
195 refData
->m_height
= h
;
196 refData
->m_depth
= wxDisplayDepth();
198 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
200 // the mask returned by GetIconInfo() is inversed compared to the usual
202 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
205 // delete the old one now as we don't need it any more
206 ::DeleteObject(iconInfo
.hbmMask
);
208 #if WXWIN_COMPATIBILITY_2
209 refData
->m_ok
= TRUE
;
210 #endif // WXWIN_COMPATIBILITY_2
220 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
228 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
232 return CopyFromIconOrCursor(cursor
);
236 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
243 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
244 // to create a bitmap from icon there - but using this way we won't have
247 int width
= icon
.GetWidth(),
248 height
= icon
.GetHeight();
250 // copy the icon to the bitmap
252 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
253 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
254 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
256 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
258 ::SelectObject(hdc
, hbmpOld
);
261 wxBitmapRefData
*refData
= new wxBitmapRefData
;
264 refData
->m_width
= width
;
265 refData
->m_height
= height
;
266 refData
->m_depth
= wxDisplayDepth();
268 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
270 #if WXWIN_COMPATIBILITY_2
271 refData
->m_ok
= TRUE
;
272 #endif // WXWIN_COMPATIBILITY_2
276 return CopyFromIconOrCursor(icon
);
277 #endif // Win16/Win32
280 wxBitmap::~wxBitmap()
284 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
288 #ifndef __WXMICROWIN__
289 wxBitmapRefData
*refData
= new wxBitmapRefData
;
292 refData
->m_width
= width
;
293 refData
->m_height
= height
;
294 refData
->m_depth
= depth
;
299 // we assume that it is in XBM format which is not quite the same as
300 // the format CreateBitmap() wants because the order of bytes in the
302 const size_t bytesPerLine
= (width
+ 7) / 8;
303 const size_t padding
= bytesPerLine
% 2;
304 const size_t len
= height
* ( padding
+ bytesPerLine
);
305 data
= (char *)malloc(len
);
306 const char *src
= bits
;
309 for ( int rows
= 0; rows
< height
; rows
++ )
311 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
313 unsigned char val
= *src
++;
314 unsigned char reversed
= 0;
316 for ( int bits
= 0; bits
< 8; bits
++)
319 reversed
|= (val
& 0x01);
331 // bits should already be in Windows standard format
332 data
= (char *)bits
; // const_cast is harmless
335 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
338 wxLogLastError(wxT("CreateBitmap"));
346 SetHBITMAP((WXHBITMAP
)hbmp
);
350 // Create from XPM data
351 bool wxBitmap::CreateFromXpm(const char **data
)
353 #if wxUSE_IMAGE && wxUSE_XPM
356 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
358 wxXPMDecoder decoder
;
359 wxImage img
= decoder
.ReadData(data
);
360 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
362 *this = wxBitmap(img
);
369 wxBitmap::wxBitmap(int w
, int h
, int d
)
373 (void)Create(w
, h
, d
);
376 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
380 (void)Create(data
, type
, width
, height
, depth
);
383 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
387 LoadFile(filename
, (int)type
);
390 bool wxBitmap::Create(int w
, int h
, int d
)
394 m_refData
= new wxBitmapRefData
;
396 #if wxUSE_DIB_FOR_BITMAP
397 if ( w
&& h
&& d
>= 16 )
399 if ( !CreateDIB(w
, h
, d
) )
403 #endif // wxUSE_DIB_FOR_BITMAP
405 GetBitmapData()->m_width
= w
;
406 GetBitmapData()->m_height
= h
;
407 GetBitmapData()->m_depth
= d
;
410 #ifndef __WXMICROWIN__
413 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
416 wxLogLastError(wxT("CreateBitmap"));
420 #endif // !__WXMICROWIN__
423 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
426 wxLogLastError(wxT("CreateCompatibleBitmap"));
429 GetBitmapData()->m_depth
= wxDisplayDepth();
432 SetHBITMAP((WXHBITMAP
)hbmp
);
434 #if WXWIN_COMPATIBILITY_2
435 GetBitmapData()->m_ok
= hbmp
!= 0;
436 #endif // WXWIN_COMPATIBILITY_2
444 // ----------------------------------------------------------------------------
445 // wxImage to/from conversions for Microwin
446 // ----------------------------------------------------------------------------
448 // Microwin versions are so different from normal ones that it really doesn't
449 // make sense to use #ifdefs inside the function bodies
450 #ifdef __WXMICROWIN__
452 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
454 // Set this to 1 to experiment with mask code,
455 // which currently doesn't work
458 m_refData
= new wxBitmapRefData();
460 // Initial attempt at a simple-minded implementation.
461 // The bitmap will always be created at the screen depth,
462 // so the 'depth' argument is ignored.
464 HDC hScreenDC
= ::GetDC(NULL
);
465 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
467 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
468 HBITMAP hMaskBitmap
= NULL
;
469 HBITMAP hOldMaskBitmap
= NULL
;
471 unsigned char maskR
= 0;
472 unsigned char maskG
= 0;
473 unsigned char maskB
= 0;
475 // printf("Created bitmap %d\n", (int) hBitmap);
478 ::ReleaseDC(NULL
, hScreenDC
);
481 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
483 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
484 ::ReleaseDC(NULL
, hScreenDC
);
486 // created an mono-bitmap for the possible mask
487 bool hasMask
= image
.HasMask();
492 // FIXME: we should be able to pass bpp = 1, but
493 // GdBlit can't handle a different depth
495 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
497 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
499 maskR
= image
.GetMaskRed();
500 maskG
= image
.GetMaskGreen();
501 maskB
= image
.GetMaskBlue();
509 hScreenDC
= ::GetDC(NULL
);
510 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
511 ::ReleaseDC(NULL
, hScreenDC
);
513 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
521 for (i
= 0; i
< image
.GetWidth(); i
++)
523 for (j
= 0; j
< image
.GetHeight(); j
++)
525 unsigned char red
= image
.GetRed(i
, j
);
526 unsigned char green
= image
.GetGreen(i
, j
);
527 unsigned char blue
= image
.GetBlue(i
, j
);
529 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
533 // scan the bitmap for the transparent colour and set the corresponding
534 // pixels in the mask to BLACK and the rest to WHITE
535 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
536 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
538 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
543 ::SelectObject(hMemDC
, hOldBitmap
);
547 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
550 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
553 SetWidth(image
.GetWidth());
554 SetHeight(image
.GetHeight());
555 SetDepth(screenDepth
);
556 SetHBITMAP( (WXHBITMAP
) hBitmap
);
559 // Copy the palette from the source image
560 SetPalette(image
.GetPalette());
561 #endif // wxUSE_PALETTE
563 #if WXWIN_COMPATIBILITY_2
564 // check the wxBitmap object
565 GetBitmapData()->SetOk();
566 #endif // WXWIN_COMPATIBILITY_2
571 wxImage
wxBitmap::ConvertToImage() const
573 // Initial attempt at a simple-minded implementation.
574 // The bitmap will always be created at the screen depth,
575 // so the 'depth' argument is ignored.
576 // TODO: transparency (create a mask image)
580 wxFAIL_MSG( wxT("bitmap is invalid") );
586 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
588 // create an wxImage object
589 int width
= GetWidth();
590 int height
= GetHeight();
591 image
.Create( width
, height
);
592 unsigned char *data
= image
.GetData();
595 wxFAIL_MSG( wxT("could not allocate data for image") );
599 HDC hScreenDC
= ::GetDC(NULL
);
601 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
602 ::ReleaseDC(NULL
, hScreenDC
);
604 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
606 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
609 for (i
= 0; i
< GetWidth(); i
++)
611 for (j
= 0; j
< GetHeight(); j
++)
613 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
614 unsigned char red
= GetRValue(color
);
615 unsigned char green
= GetGValue(color
);
616 unsigned char blue
= GetBValue(color
);
618 image
.SetRGB(i
, j
, red
, green
, blue
);
622 ::SelectObject(hMemDC
, hOldBitmap
);
626 // Copy the palette from the source image
628 image
.SetPalette(* GetPalette());
629 #endif // wxUSE_PALETTE
634 #endif // __WXMICROWIN__
636 // ----------------------------------------------------------------------------
637 // wxImage to/from conversions
638 // ----------------------------------------------------------------------------
640 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
642 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
646 // first convert the image to DIB
647 const int h
= image
.GetHeight();
648 const int w
= image
.GetWidth();
655 // store the bitmap parameters
656 wxBitmapRefData
*refData
= new wxBitmapRefData
;
657 refData
->m_width
= w
;
658 refData
->m_height
= h
;
659 refData
->m_depth
= dib
.GetDepth();
660 refData
->m_hasAlpha
= image
.HasAlpha();
665 // next either store DIB as is or create a DDB from it
668 // TODO: if we're ready to use DIB as is, we can just do this:
670 // hbitmap = dib.Detach();
673 // create and set the device-dependent bitmap
675 // VZ: why don't we just use SetDIBits() instead? because of the
676 // palette or is there some other reason?
677 hbitmap
= ::CreateCompatibleBitmap(ScreenHDC(), w
, h
);
680 wxLogLastError(_T("CreateCompatibleBitmap()"));
686 SelectInHDC
select(hdcMem
, hbitmap
);
689 wxLogLastError(_T("SelectObjct(hBitmap)"));
693 const wxPalette
& palette
= image
.GetPalette();
695 HPALETTE hOldPalette
;
700 hOldPalette
= ::SelectPalette
703 GetHpaletteOf(palette
),
704 FALSE
// ignored for hdcMem
709 wxLogLastError(_T("SelectPalette()"));
712 if ( ::RealizePalette(hdcMem
) == GDI_ERROR
)
714 wxLogLastError(_T("RealizePalette()"));
717 else // no valid palette
721 #endif // wxUSE_PALETTE
724 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
726 wxLogLastError(_T("GetObject(hDIB)"));
729 if ( ::StretchDIBits(hdcMem
,
733 (BITMAPINFO
*)&ds
.dsBmih
,
735 SRCCOPY
) == GDI_ERROR
)
737 wxLogLastError(_T("StretchDIBits()"));
745 ::SelectPalette(hdcMem
, hOldPalette
, FALSE
);
747 #endif // wxUSE_PALETTE
750 // validate this object
751 SetHBITMAP((WXHBITMAP
)hbitmap
);
753 #if WXWIN_COMPATIBILITY_2
754 m_refData
->m_ok
= TRUE
;
755 #endif // WXWIN_COMPATIBILITY_2
757 // finally also set the mask if we have one
758 if ( image
.HasMask() )
760 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
761 image
.GetMaskGreen(),
762 image
.GetMaskBlue())));
768 wxImage
wxBitmap::ConvertToImage() const
772 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
774 // create an wxImage object
775 int width
= GetWidth();
776 int height
= GetHeight();
777 image
.Create( width
, height
);
778 unsigned char *data
= image
.GetData();
781 wxFAIL_MSG( wxT("could not allocate data for image") );
785 // calc the number of bytes per scanline and padding in the DIB
786 int bytePerLine
= width
*3;
787 int sizeDWORD
= sizeof( DWORD
);
788 int lineBoundary
= bytePerLine
% sizeDWORD
;
790 if( lineBoundary
> 0 )
792 padding
= sizeDWORD
- lineBoundary
;
793 bytePerLine
+= padding
;
796 // create a DIB header
797 int headersize
= sizeof(BITMAPINFOHEADER
);
798 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
801 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
805 // Fill in the DIB header
806 lpDIBh
->bmiHeader
.biSize
= headersize
;
807 lpDIBh
->bmiHeader
.biWidth
= width
;
808 lpDIBh
->bmiHeader
.biHeight
= -height
;
809 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
810 lpDIBh
->bmiHeader
.biPlanes
= 1;
811 lpDIBh
->bmiHeader
.biBitCount
= 24;
812 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
813 lpDIBh
->bmiHeader
.biClrUsed
= 0;
814 // These seem not really needed for our purpose here.
815 lpDIBh
->bmiHeader
.biClrImportant
= 0;
816 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
817 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
818 // memory for DIB data
819 unsigned char *lpBits
;
820 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
823 wxFAIL_MSG( wxT("could not allocate data for DIB") );
829 // copy data from the device-dependent bitmap to the DIB
830 HDC hdc
= ::GetDC(NULL
);
832 hbitmap
= (HBITMAP
) GetHBITMAP();
833 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
835 // copy DIB data into the wxImage object
837 unsigned char *ptdata
= data
;
838 unsigned char *ptbits
= lpBits
;
839 for( i
=0; i
<height
; i
++ )
841 for( j
=0; j
<width
; j
++ )
843 *(ptdata
++) = *(ptbits
+2);
844 *(ptdata
++) = *(ptbits
+1);
845 *(ptdata
++) = *(ptbits
);
851 // similarly, set data according to the possible mask bitmap
852 if( GetMask() && GetMask()->GetMaskBitmap() )
854 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
855 // memory DC created, color set, data copied, and memory DC deleted
856 HDC memdc
= ::CreateCompatibleDC( hdc
);
857 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
858 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
859 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
861 // background color set to RGB(16,16,16) in consistent with wxGTK
862 unsigned char r
=16, g
=16, b
=16;
865 for( i
=0; i
<height
; i
++ )
867 for( j
=0; j
<width
; j
++ )
881 image
.SetMaskColour( r
, g
, b
);
882 image
.SetMask( TRUE
);
886 image
.SetMask( FALSE
);
888 // free allocated resources
889 ::ReleaseDC(NULL
, hdc
);
896 #endif // wxUSE_IMAGE
898 // ----------------------------------------------------------------------------
899 // loading and saving bitmaps
900 // ----------------------------------------------------------------------------
902 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
906 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
910 m_refData
= new wxBitmapRefData
;
912 return handler
->LoadFile(this, filename
, type
, -1, -1);
918 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
920 *this = wxBitmap(image
);
925 #endif // wxUSE_IMAGE
930 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
934 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
938 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
943 m_refData
= new wxBitmapRefData
;
945 return handler
->Create(this, data
, type
, width
, height
, depth
);
948 bool wxBitmap::SaveFile(const wxString
& filename
,
950 const wxPalette
*palette
)
952 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
956 return handler
->SaveFile(this, filename
, type
, palette
);
961 // FIXME what about palette? shouldn't we use it?
962 wxImage image
= ConvertToImage();
965 return image
.SaveFile(filename
, type
);
968 #endif // wxUSE_IMAGE
973 // ----------------------------------------------------------------------------
974 // sub bitmap extraction
975 // ----------------------------------------------------------------------------
977 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
980 (rect
.x
>= 0) && (rect
.y
>= 0) &&
981 (rect
.x
+rect
.width
<= GetWidth()) &&
982 (rect
.y
+rect
.height
<= GetHeight()),
983 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
985 wxBitmap
ret( rect
.width
, rect
.height
);
986 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
988 #ifndef __WXMICROWIN__
989 // TODO: copy alpha channel data if any
996 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
997 selectDst(dcDst
, GetHbitmapOf(ret
));
999 if ( !selectSrc
|| !selectDst
)
1001 wxLogLastError(_T("SelectObjct(hBitmap)"));
1004 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1005 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1007 wxLogLastError(_T("BitBlt"));
1011 // copy mask if there is one
1014 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1016 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1017 selectDst(dcDst
, hbmpMask
);
1019 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1020 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1022 wxLogLastError(_T("BitBlt"));
1025 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1028 #endif // !__WXMICROWIN__
1033 // ----------------------------------------------------------------------------
1034 // wxBitmap accessors
1035 // ----------------------------------------------------------------------------
1037 wxPalette
* wxBitmap::GetPalette() const
1039 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1040 : (wxPalette
*) NULL
;
1043 wxMask
*wxBitmap::GetMask() const
1045 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1050 wxDC
*wxBitmap::GetSelectedInto() const
1052 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1057 #if WXWIN_COMPATIBILITY_2_4
1059 int wxBitmap::GetQuality() const
1064 #endif // WXWIN_COMPATIBILITY_2_4
1066 bool wxBitmap::HasAlpha() const
1068 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1077 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1079 if ( GetBitmapData() )
1080 GetBitmapData()->m_selectedInto
= dc
;
1087 void wxBitmap::SetPalette(const wxPalette
& palette
)
1091 GetBitmapData()->m_bitmapPalette
= palette
;
1094 #endif // wxUSE_PALETTE
1096 void wxBitmap::SetMask(wxMask
*mask
)
1100 GetBitmapData()->SetMask(mask
);
1103 #if WXWIN_COMPATIBILITY_2
1105 void wxBitmap::SetOk(bool isOk
)
1109 GetBitmapData()->m_ok
= isOk
;
1112 #endif // WXWIN_COMPATIBILITY_2
1114 #if WXWIN_COMPATIBILITY_2_4
1116 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1120 #endif // WXWIN_COMPATIBILITY_2_4
1122 // ----------------------------------------------------------------------------
1123 // TODO: to be replaced by something better
1124 // ----------------------------------------------------------------------------
1126 // Creates a bitmap that matches the device context, from
1127 // an arbitray bitmap. At present, the original bitmap must have an
1128 // associated palette. TODO: use a default palette if no palette exists.
1129 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1130 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1132 #ifdef __WXMICROWIN__
1136 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1137 HPALETTE hPal
= (HPALETTE
) NULL
;
1139 void *lpBits
= (void*) NULL
;
1142 if( GetPalette() && GetPalette()->Ok() )
1144 tmpBitmap
.SetPalette(*GetPalette());
1145 memDC
.SelectObject(tmpBitmap
);
1146 memDC
.SetPalette(*GetPalette());
1147 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1151 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1153 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1154 tmpBitmap
.SetPalette( palette
);
1155 memDC
.SelectObject(tmpBitmap
);
1156 memDC
.SetPalette( palette
);
1158 #else // !wxUSE_PALETTE
1159 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1160 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1162 // set the height negative because in a DIB the order of the lines is
1164 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1166 return wxNullBitmap
;
1169 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1171 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1173 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1174 GetWidth(), GetHeight(),
1175 0, 0, 0, GetHeight(),
1176 lpBits
, lpDib
, DIB_RGB_COLORS
);
1186 // ----------------------------------------------------------------------------
1188 // ----------------------------------------------------------------------------
1195 // Construct a mask from a bitmap and a colour indicating
1196 // the transparent area
1197 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1200 Create(bitmap
, colour
);
1203 // Construct a mask from a bitmap and a palette index indicating
1204 // the transparent area
1205 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1208 Create(bitmap
, paletteIndex
);
1211 // Construct a mask from a mono bitmap (copies the bitmap).
1212 wxMask::wxMask(const wxBitmap
& bitmap
)
1221 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1224 // Create a mask from a mono bitmap (copies the bitmap).
1225 bool wxMask::Create(const wxBitmap
& bitmap
)
1227 #ifndef __WXMICROWIN__
1228 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1229 _T("can't create mask from invalid or not monochrome bitmap") );
1233 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1237 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1242 HDC srcDC
= CreateCompatibleDC(0);
1243 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1244 HDC destDC
= CreateCompatibleDC(0);
1245 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1246 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1247 SelectObject(srcDC
, 0);
1249 SelectObject(destDC
, 0);
1257 // Create a mask from a bitmap and a palette index indicating
1258 // the transparent area
1259 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1263 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1268 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1270 unsigned char red
, green
, blue
;
1271 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1273 wxColour
transparentColour(red
, green
, blue
);
1274 return Create(bitmap
, transparentColour
);
1277 #endif // wxUSE_PALETTE
1282 // Create a mask from a bitmap and a colour indicating
1283 // the transparent area
1284 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1286 #ifndef __WXMICROWIN__
1287 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1291 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1295 int width
= bitmap
.GetWidth(),
1296 height
= bitmap
.GetHeight();
1298 // scan the bitmap for the transparent colour and set the corresponding
1299 // pixels in the mask to BLACK and the rest to WHITE
1300 COLORREF maskColour
= wxColourToPalRGB(colour
);
1301 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1303 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1304 HDC destDC
= ::CreateCompatibleDC(NULL
);
1305 if ( !srcDC
|| !destDC
)
1307 wxLogLastError(wxT("CreateCompatibleDC"));
1312 // SelectObject() will fail
1313 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1314 _T("bitmap can't be selected in another DC") );
1316 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1319 wxLogLastError(wxT("SelectObject"));
1324 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1327 wxLogLastError(wxT("SelectObject"));
1334 // this will create a monochrome bitmap with 0 points for the pixels
1335 // which have the same value as the background colour and 1 for the
1337 ::SetBkColor(srcDC
, maskColour
);
1338 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1341 ::SelectObject(srcDC
, hbmpSrcOld
);
1343 ::SelectObject(destDC
, hbmpDstOld
);
1347 #else // __WXMICROWIN__
1349 #endif // __WXMICROWIN__/!__WXMICROWIN__
1352 // ----------------------------------------------------------------------------
1354 // ----------------------------------------------------------------------------
1356 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1359 int width
, int height
, int depth
)
1361 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1363 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1366 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1367 const wxString
& name
,
1369 int width
, int height
)
1371 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1373 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1376 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1377 const wxString
& name
,
1380 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1382 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1385 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1386 void *WXUNUSED(data
),
1387 long WXUNUSED(type
),
1388 int WXUNUSED(width
),
1389 int WXUNUSED(height
),
1390 int WXUNUSED(depth
))
1395 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1396 const wxString
& WXUNUSED(name
),
1397 long WXUNUSED(type
),
1398 int WXUNUSED(desiredWidth
),
1399 int WXUNUSED(desiredHeight
))
1404 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1405 const wxString
& WXUNUSED(name
),
1407 const wxPalette
*WXUNUSED(palette
))
1412 // ----------------------------------------------------------------------------
1414 // ----------------------------------------------------------------------------
1416 #ifndef __WXMICROWIN__
1417 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1418 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1420 unsigned long i
, headerSize
;
1421 LPBITMAPINFO lpDIBheader
= NULL
;
1422 LPPALETTEENTRY lpPe
= NULL
;
1425 // Allocate space for a DIB header
1426 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1427 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1428 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1430 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1432 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1434 // Fill in the static parts of the DIB header
1435 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1436 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1437 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1438 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1440 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1441 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1442 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1443 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1444 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1447 // Initialize the DIB palette
1448 for (i
= 0; i
< 256; i
++) {
1449 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1450 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1451 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1452 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1455 *lpDIBHeader
= lpDIBheader
;
1460 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1466 // ----------------------------------------------------------------------------
1467 // other helper functions
1468 // ----------------------------------------------------------------------------
1470 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1472 #ifndef __WXMICROWIN__
1473 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1475 // get width/height from the bitmap if not given
1479 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1484 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1485 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1486 if ( !hdcSrc
|| !hdcDst
)
1488 wxLogLastError(wxT("CreateCompatibleDC"));
1491 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1494 wxLogLastError(wxT("CreateBitmap"));
1497 ::SelectObject(hdcSrc
, hbmpMask
);
1498 ::SelectObject(hdcDst
, hbmpInvMask
);
1499 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1503 wxLogLastError(wxT("BitBlt"));