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(int w
, int h
, const wxDC
& dc
)
441 (void)Create(w
, h
, dc
);
444 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
448 (void)Create(data
, type
, width
, height
, depth
);
451 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
455 LoadFile(filename
, (int)type
);
458 bool wxBitmap::Create(int width
, int height
, int depth
)
460 return DoCreate(width
, height
, depth
, 0);
463 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
465 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
467 return DoCreate(width
, height
, -1, dc
.GetHDC());
470 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
474 m_refData
= new wxBitmapRefData
;
476 GetBitmapData()->m_width
= w
;
477 GetBitmapData()->m_height
= h
;
481 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
485 // create DIBs without alpha channel by default
493 // don't delete the DIB section in dib object dtor
496 GetBitmapData()->m_depth
= d
;
501 d
= wxDisplayDepth();
503 GetBitmapData()->m_depth
= d
;
505 #ifndef __WXMICROWIN__
508 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
511 wxLogLastError(wxT("CreateBitmap"));
515 #endif // !__WXMICROWIN__
518 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
521 wxLogLastError(wxT("CreateCompatibleBitmap"));
524 GetBitmapData()->m_depth
= wxDisplayDepth();
528 SetHBITMAP((WXHBITMAP
)hbmp
);
530 #if WXWIN_COMPATIBILITY_2
531 GetBitmapData()->m_ok
= hbmp
!= 0;
532 #endif // WXWIN_COMPATIBILITY_2
539 // ----------------------------------------------------------------------------
540 // wxImage to/from conversions for Microwin
541 // ----------------------------------------------------------------------------
543 // Microwin versions are so different from normal ones that it really doesn't
544 // make sense to use #ifdefs inside the function bodies
545 #ifdef __WXMICROWIN__
547 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
549 // Set this to 1 to experiment with mask code,
550 // which currently doesn't work
553 m_refData
= new wxBitmapRefData();
555 // Initial attempt at a simple-minded implementation.
556 // The bitmap will always be created at the screen depth,
557 // so the 'depth' argument is ignored.
559 HDC hScreenDC
= ::GetDC(NULL
);
560 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
562 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
563 HBITMAP hMaskBitmap
= NULL
;
564 HBITMAP hOldMaskBitmap
= NULL
;
566 unsigned char maskR
= 0;
567 unsigned char maskG
= 0;
568 unsigned char maskB
= 0;
570 // printf("Created bitmap %d\n", (int) hBitmap);
573 ::ReleaseDC(NULL
, hScreenDC
);
576 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
578 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
579 ::ReleaseDC(NULL
, hScreenDC
);
581 // created an mono-bitmap for the possible mask
582 bool hasMask
= image
.HasMask();
587 // FIXME: we should be able to pass bpp = 1, but
588 // GdBlit can't handle a different depth
590 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
592 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
594 maskR
= image
.GetMaskRed();
595 maskG
= image
.GetMaskGreen();
596 maskB
= image
.GetMaskBlue();
604 hScreenDC
= ::GetDC(NULL
);
605 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
606 ::ReleaseDC(NULL
, hScreenDC
);
608 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
616 for (i
= 0; i
< image
.GetWidth(); i
++)
618 for (j
= 0; j
< image
.GetHeight(); j
++)
620 unsigned char red
= image
.GetRed(i
, j
);
621 unsigned char green
= image
.GetGreen(i
, j
);
622 unsigned char blue
= image
.GetBlue(i
, j
);
624 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
628 // scan the bitmap for the transparent colour and set the corresponding
629 // pixels in the mask to BLACK and the rest to WHITE
630 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
631 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
633 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
638 ::SelectObject(hMemDC
, hOldBitmap
);
642 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
645 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
648 SetWidth(image
.GetWidth());
649 SetHeight(image
.GetHeight());
650 SetDepth(screenDepth
);
651 SetHBITMAP( (WXHBITMAP
) hBitmap
);
654 // Copy the palette from the source image
655 SetPalette(image
.GetPalette());
656 #endif // wxUSE_PALETTE
658 #if WXWIN_COMPATIBILITY_2
659 // check the wxBitmap object
660 GetBitmapData()->SetOk();
661 #endif // WXWIN_COMPATIBILITY_2
666 wxImage
wxBitmap::ConvertToImage() const
668 // Initial attempt at a simple-minded implementation.
669 // The bitmap will always be created at the screen depth,
670 // so the 'depth' argument is ignored.
671 // TODO: transparency (create a mask image)
675 wxFAIL_MSG( wxT("bitmap is invalid") );
681 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
683 // create an wxImage object
684 int width
= GetWidth();
685 int height
= GetHeight();
686 image
.Create( width
, height
);
687 unsigned char *data
= image
.GetData();
690 wxFAIL_MSG( wxT("could not allocate data for image") );
694 HDC hScreenDC
= ::GetDC(NULL
);
696 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
697 ::ReleaseDC(NULL
, hScreenDC
);
699 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
701 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
704 for (i
= 0; i
< GetWidth(); i
++)
706 for (j
= 0; j
< GetHeight(); j
++)
708 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
709 unsigned char red
= GetRValue(color
);
710 unsigned char green
= GetGValue(color
);
711 unsigned char blue
= GetBValue(color
);
713 image
.SetRGB(i
, j
, red
, green
, blue
);
717 ::SelectObject(hMemDC
, hOldBitmap
);
721 // Copy the palette from the source image
723 image
.SetPalette(* GetPalette());
724 #endif // wxUSE_PALETTE
729 #endif // __WXMICROWIN__
731 // ----------------------------------------------------------------------------
732 // wxImage to/from conversions
733 // ----------------------------------------------------------------------------
735 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
737 return CreateFromImage(image
, depth
, 0);
740 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
742 wxCHECK_MSG( dc
.Ok(), FALSE
,
743 _T("invalid HDC in wxBitmap::CreateFromImage()") );
745 return CreateFromImage(image
, -1, dc
.GetHDC());
748 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
750 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
754 // first convert the image to DIB
755 const int h
= image
.GetHeight();
756 const int w
= image
.GetWidth();
763 // store the bitmap parameters
764 wxBitmapRefData
*refData
= new wxBitmapRefData
;
765 refData
->m_width
= w
;
766 refData
->m_height
= h
;
767 refData
->m_hasAlpha
= image
.HasAlpha();
772 // next either store DIB as is or create a DDB from it
775 // are we going to use DIB?
776 if ( wxShouldCreateDIB(w
, h
, depth
, hdc
) )
778 // don't delete the DIB section in dib object dtor
779 hbitmap
= dib
.Detach();
781 refData
->m_depth
= dib
.GetDepth();
783 else // we need to convert DIB to DDB
785 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
787 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
790 // validate this object
791 SetHBITMAP((WXHBITMAP
)hbitmap
);
793 #if WXWIN_COMPATIBILITY_2
794 m_refData
->m_ok
= TRUE
;
795 #endif // WXWIN_COMPATIBILITY_2
797 // finally also set the mask if we have one
798 if ( image
.HasMask() )
800 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
801 image
.GetMaskGreen(),
802 image
.GetMaskBlue())));
808 wxImage
wxBitmap::ConvertToImage() const
812 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
814 // create an wxImage object
815 int width
= GetWidth();
816 int height
= GetHeight();
817 image
.Create( width
, height
);
818 unsigned char *data
= image
.GetData();
821 wxFAIL_MSG( wxT("could not allocate data for image") );
825 // calc the number of bytes per scanline and padding in the DIB
826 int bytePerLine
= width
*3;
827 int sizeDWORD
= sizeof( DWORD
);
828 int lineBoundary
= bytePerLine
% sizeDWORD
;
830 if( lineBoundary
> 0 )
832 padding
= sizeDWORD
- lineBoundary
;
833 bytePerLine
+= padding
;
836 // create a DIB header
837 int headersize
= sizeof(BITMAPINFOHEADER
);
838 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
841 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
845 // Fill in the DIB header
846 lpDIBh
->bmiHeader
.biSize
= headersize
;
847 lpDIBh
->bmiHeader
.biWidth
= width
;
848 lpDIBh
->bmiHeader
.biHeight
= -height
;
849 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
850 lpDIBh
->bmiHeader
.biPlanes
= 1;
851 lpDIBh
->bmiHeader
.biBitCount
= 24;
852 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
853 lpDIBh
->bmiHeader
.biClrUsed
= 0;
854 // These seem not really needed for our purpose here.
855 lpDIBh
->bmiHeader
.biClrImportant
= 0;
856 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
857 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
858 // memory for DIB data
859 unsigned char *lpBits
;
860 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
863 wxFAIL_MSG( wxT("could not allocate data for DIB") );
869 // copy data from the device-dependent bitmap to the DIB
870 HDC hdc
= ::GetDC(NULL
);
872 hbitmap
= (HBITMAP
) GetHBITMAP();
873 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
875 // copy DIB data into the wxImage object
877 unsigned char *ptdata
= data
;
878 unsigned char *ptbits
= lpBits
;
879 for( i
=0; i
<height
; i
++ )
881 for( j
=0; j
<width
; j
++ )
883 *(ptdata
++) = *(ptbits
+2);
884 *(ptdata
++) = *(ptbits
+1);
885 *(ptdata
++) = *(ptbits
);
891 // similarly, set data according to the possible mask bitmap
892 if( GetMask() && GetMask()->GetMaskBitmap() )
894 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
895 // memory DC created, color set, data copied, and memory DC deleted
896 HDC memdc
= ::CreateCompatibleDC( hdc
);
897 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
898 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
899 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
901 // background color set to RGB(16,16,16) in consistent with wxGTK
902 unsigned char r
=16, g
=16, b
=16;
905 for( i
=0; i
<height
; i
++ )
907 for( j
=0; j
<width
; j
++ )
921 image
.SetMaskColour( r
, g
, b
);
922 image
.SetMask( TRUE
);
926 image
.SetMask( FALSE
);
928 // free allocated resources
929 ::ReleaseDC(NULL
, hdc
);
936 #endif // wxUSE_IMAGE
938 // ----------------------------------------------------------------------------
939 // loading and saving bitmaps
940 // ----------------------------------------------------------------------------
942 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
946 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
950 m_refData
= new wxBitmapRefData
;
952 return handler
->LoadFile(this, filename
, type
, -1, -1);
958 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
960 *this = wxBitmap(image
);
965 #endif // wxUSE_IMAGE
970 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
974 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
978 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
983 m_refData
= new wxBitmapRefData
;
985 return handler
->Create(this, data
, type
, width
, height
, depth
);
988 bool wxBitmap::SaveFile(const wxString
& filename
,
990 const wxPalette
*palette
)
992 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
996 return handler
->SaveFile(this, filename
, type
, palette
);
1001 // FIXME what about palette? shouldn't we use it?
1002 wxImage image
= ConvertToImage();
1005 return image
.SaveFile(filename
, type
);
1008 #endif // wxUSE_IMAGE
1013 // ----------------------------------------------------------------------------
1014 // sub bitmap extraction
1015 // ----------------------------------------------------------------------------
1017 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1019 wxCHECK_MSG( Ok() &&
1020 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1021 (rect
.x
+rect
.width
<= GetWidth()) &&
1022 (rect
.y
+rect
.height
<= GetHeight()),
1023 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1025 wxBitmap
ret( rect
.width
, rect
.height
);
1026 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1028 #ifndef __WXMICROWIN__
1029 // TODO: copy alpha channel data if any
1036 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1037 selectDst(dcDst
, GetHbitmapOf(ret
));
1039 if ( !selectSrc
|| !selectDst
)
1041 wxLogLastError(_T("SelectObjct(hBitmap)"));
1044 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1045 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1047 wxLogLastError(_T("BitBlt"));
1051 // copy mask if there is one
1054 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1056 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1057 selectDst(dcDst
, hbmpMask
);
1059 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1060 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1062 wxLogLastError(_T("BitBlt"));
1065 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1068 #endif // !__WXMICROWIN__
1073 // ----------------------------------------------------------------------------
1074 // wxBitmap accessors
1075 // ----------------------------------------------------------------------------
1077 wxPalette
* wxBitmap::GetPalette() const
1079 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1080 : (wxPalette
*) NULL
;
1083 wxMask
*wxBitmap::GetMask() const
1085 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1090 wxDC
*wxBitmap::GetSelectedInto() const
1092 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1097 #if WXWIN_COMPATIBILITY_2_4
1099 int wxBitmap::GetQuality() const
1104 #endif // WXWIN_COMPATIBILITY_2_4
1106 void wxBitmap::UseAlpha()
1108 if ( GetBitmapData() )
1109 GetBitmapData()->m_hasAlpha
= true;
1112 bool wxBitmap::HasAlpha() const
1114 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1117 // ----------------------------------------------------------------------------
1119 // ----------------------------------------------------------------------------
1123 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1125 if ( GetBitmapData() )
1126 GetBitmapData()->m_selectedInto
= dc
;
1133 void wxBitmap::SetPalette(const wxPalette
& palette
)
1137 GetBitmapData()->m_bitmapPalette
= palette
;
1140 #endif // wxUSE_PALETTE
1142 void wxBitmap::SetMask(wxMask
*mask
)
1146 GetBitmapData()->SetMask(mask
);
1149 #if WXWIN_COMPATIBILITY_2
1151 void wxBitmap::SetOk(bool isOk
)
1155 GetBitmapData()->m_ok
= isOk
;
1158 #endif // WXWIN_COMPATIBILITY_2
1160 #if WXWIN_COMPATIBILITY_2_4
1162 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1166 #endif // WXWIN_COMPATIBILITY_2_4
1168 // ----------------------------------------------------------------------------
1169 // raw bitmap access support
1170 // ----------------------------------------------------------------------------
1172 bool wxBitmap::GetRawData(wxRawBitmapData
*data
)
1174 wxCHECK_MSG( data
, FALSE
, _T("NULL pointer in wxBitmap::GetRawData") );
1178 // no bitmap, no data (raw or otherwise)
1182 // we only support raw access to the DIBs, so check if we have one
1184 if ( ::GetObject(GetHbitmap(), sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1189 // ok, store the relevant info in wxRawBitmapData
1190 const LONG h
= ds
.dsBm
.bmHeight
;
1192 data
->m_width
= ds
.dsBm
.bmWidth
;
1194 data
->m_bypp
= ds
.dsBm
.bmBitsPixel
/ 8;
1196 // remember that DIBs are stored in top to bottom order!
1197 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1198 data
->m_stride
= -bytesPerRow
;
1199 data
->m_pixels
= (unsigned char *)ds
.dsBm
.bmBits
;
1202 data
->m_pixels
+= (h
- 1)*bytesPerRow
;
1208 void wxBitmap::UngetRawData(wxRawBitmapData
*data
)
1210 wxCHECK_RET( data
, _T("NULL pointer in wxBitmap::UngetRawData()") );
1214 // invalid data, don't crash -- but don't assert neither as we're
1215 // called automatically from wxRawBitmapData dtor and so there is no
1216 // way to prevent this from happening
1220 // AlphaBlend() wants to have premultiplied source alpha but wxRawBitmap
1221 // API uses normal, not premultiplied, colours, so adjust them here now
1222 wxRawBitmapIterator
p(*data
);
1224 const int w
= data
->GetWidth();
1225 const int h
= data
->GetHeight();
1227 for ( int y
= 0; y
< h
; y
++ )
1229 wxRawBitmapIterator rowStart
= p
;
1231 for ( int x
= 0; x
< w
; x
++ )
1233 const unsigned alpha
= p
.Alpha();
1235 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1236 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1237 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1247 // ----------------------------------------------------------------------------
1249 // ----------------------------------------------------------------------------
1256 // Construct a mask from a bitmap and a colour indicating
1257 // the transparent area
1258 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1261 Create(bitmap
, colour
);
1264 // Construct a mask from a bitmap and a palette index indicating
1265 // the transparent area
1266 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1269 Create(bitmap
, paletteIndex
);
1272 // Construct a mask from a mono bitmap (copies the bitmap).
1273 wxMask::wxMask(const wxBitmap
& bitmap
)
1282 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1285 // Create a mask from a mono bitmap (copies the bitmap).
1286 bool wxMask::Create(const wxBitmap
& bitmap
)
1288 #ifndef __WXMICROWIN__
1289 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1290 _T("can't create mask from invalid or not monochrome bitmap") );
1294 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1298 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1303 HDC srcDC
= CreateCompatibleDC(0);
1304 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1305 HDC destDC
= CreateCompatibleDC(0);
1306 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1307 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1308 SelectObject(srcDC
, 0);
1310 SelectObject(destDC
, 0);
1318 // Create a mask from a bitmap and a palette index indicating
1319 // the transparent area
1320 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1324 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1329 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1331 unsigned char red
, green
, blue
;
1332 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1334 wxColour
transparentColour(red
, green
, blue
);
1335 return Create(bitmap
, transparentColour
);
1338 #endif // wxUSE_PALETTE
1343 // Create a mask from a bitmap and a colour indicating
1344 // the transparent area
1345 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1347 #ifndef __WXMICROWIN__
1348 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1352 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1356 int width
= bitmap
.GetWidth(),
1357 height
= bitmap
.GetHeight();
1359 // scan the bitmap for the transparent colour and set the corresponding
1360 // pixels in the mask to BLACK and the rest to WHITE
1361 COLORREF maskColour
= wxColourToPalRGB(colour
);
1362 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1364 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1365 HDC destDC
= ::CreateCompatibleDC(NULL
);
1366 if ( !srcDC
|| !destDC
)
1368 wxLogLastError(wxT("CreateCompatibleDC"));
1373 // SelectObject() will fail
1374 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1375 _T("bitmap can't be selected in another DC") );
1377 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1380 wxLogLastError(wxT("SelectObject"));
1385 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1388 wxLogLastError(wxT("SelectObject"));
1395 // this will create a monochrome bitmap with 0 points for the pixels
1396 // which have the same value as the background colour and 1 for the
1398 ::SetBkColor(srcDC
, maskColour
);
1399 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1402 ::SelectObject(srcDC
, hbmpSrcOld
);
1404 ::SelectObject(destDC
, hbmpDstOld
);
1408 #else // __WXMICROWIN__
1410 #endif // __WXMICROWIN__/!__WXMICROWIN__
1413 // ----------------------------------------------------------------------------
1415 // ----------------------------------------------------------------------------
1417 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1420 int width
, int height
, int depth
)
1422 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1424 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1427 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1428 const wxString
& name
,
1430 int width
, int height
)
1432 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1434 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1437 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1438 const wxString
& name
,
1441 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1443 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1446 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1447 void *WXUNUSED(data
),
1448 long WXUNUSED(type
),
1449 int WXUNUSED(width
),
1450 int WXUNUSED(height
),
1451 int WXUNUSED(depth
))
1456 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1457 const wxString
& WXUNUSED(name
),
1458 long WXUNUSED(type
),
1459 int WXUNUSED(desiredWidth
),
1460 int WXUNUSED(desiredHeight
))
1465 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1466 const wxString
& WXUNUSED(name
),
1468 const wxPalette
*WXUNUSED(palette
))
1473 // ----------------------------------------------------------------------------
1475 // ----------------------------------------------------------------------------
1477 #ifndef __WXMICROWIN__
1478 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1479 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1481 unsigned long i
, headerSize
;
1482 LPBITMAPINFO lpDIBheader
= NULL
;
1483 LPPALETTEENTRY lpPe
= NULL
;
1486 // Allocate space for a DIB header
1487 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1488 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1489 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1491 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1493 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1495 // Fill in the static parts of the DIB header
1496 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1497 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1498 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1499 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1501 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1502 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1503 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1504 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1505 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1508 // Initialize the DIB palette
1509 for (i
= 0; i
< 256; i
++) {
1510 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1511 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1512 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1513 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1516 *lpDIBHeader
= lpDIBheader
;
1521 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1527 // ----------------------------------------------------------------------------
1528 // global helper functions implemented here
1529 // ----------------------------------------------------------------------------
1531 // helper of wxBitmapToHICON/HCURSOR
1533 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1540 // we can't create an icon/cursor form nothing
1544 wxMask
*mask
= bmp
.GetMask();
1547 // we must have a mask for an icon, so even if it's probably incorrect,
1548 // do create it (grey is the "standard" transparent colour)
1549 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1553 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1556 iconInfo
.xHotspot
= hotSpotX
;
1557 iconInfo
.yHotspot
= hotSpotY
;
1560 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1561 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1563 // black out the transparent area to preserve background colour, because
1564 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1565 // the mask to the dest rect.
1567 MemoryHDC dcSrc
, dcDst
;
1568 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1569 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1571 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1572 dcSrc
, 0, 0, SRCAND
) )
1574 wxLogLastError(_T("BitBlt"));
1578 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1580 if ( !bmp
.GetMask() )
1582 // we created the mask, now delete it
1586 // delete the inverted mask bitmap we created as well
1587 ::DeleteObject(iconInfo
.hbmMask
);
1592 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1594 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1597 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1599 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1602 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1604 #ifndef __WXMICROWIN__
1605 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1607 // get width/height from the bitmap if not given
1611 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1616 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1617 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1618 if ( !hdcSrc
|| !hdcDst
)
1620 wxLogLastError(wxT("CreateCompatibleDC"));
1623 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1626 wxLogLastError(wxT("CreateBitmap"));
1629 ::SelectObject(hdcSrc
, hbmpMask
);
1630 ::SelectObject(hdcDst
, hbmpInvMask
);
1631 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1635 wxLogLastError(wxT("BitBlt"));