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 #ifdef wxHAVE_RAW_BITMAP
54 #include "wx/rawbmp.h"
57 // missing from mingw32 header
59 #define CLR_INVALID ((COLORREF)-1)
60 #endif // no CLR_INVALID
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
70 virtual ~wxBitmapRefData() { Free(); }
74 // set the mask object to use as the mask, we take ownership of it
75 void SetMask(wxMask
*mask
)
81 // set the HBITMAP to use as the mask
82 void SetMask(HBITMAP hbmpMask
)
84 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
88 wxMask
*GetMask() const { return m_bitmapMask
; }
92 wxPalette m_bitmapPalette
;
93 #endif // wxUSE_PALETTE
99 // this field is solely for error checking: we detect selecting a bitmap
100 // into more than one DC at once or deleting a bitmap still selected into a
101 // DC (both are serious programming errors under Windows)
102 wxDC
*m_selectedInto
;
103 #endif // __WXDEBUG__
105 // when GetRawData() is called for a DDB we need to convert it to a DIB
106 // first to be able to provide direct access to it and we cache that DIB
107 // here and convert it back to DDB when UngetRawData() is called
110 // true if we have alpha transparency info and can be drawn using
114 // true if our HBITMAP is a DIB section, false if it is a DDB
118 // optional mask for transparent drawing
119 wxMask
*m_bitmapMask
;
121 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
129 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 // decide whether we should create a DIB or a DDB for the given parameters
142 static bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
144 // here is the logic:
146 // (a) if hdc is specified, the caller explicitly wants DDB
147 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
149 // (c) finally, create DIBs under Win9x even if the depth hasn't been
150 // explicitly specified but the current display depth is 24 or more
151 // and the image is "big", i.e. > 16Mb which is the theoretical limit
152 // for DDBs under Win9x
154 // consequences (all of which seem to make sense):
156 // (i) by default, DDBs are created (depth == -1 usually)
157 // (ii) DIBs can be created by explicitly specifying the depth
158 // (iii) using a DC always forces creating a DDB
162 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 wxBitmapRefData::wxBitmapRefData()
172 m_selectedInto
= NULL
;
176 m_hBitmap
= (WXHBITMAP
) NULL
;
183 void wxBitmapRefData::Free()
185 wxASSERT_MSG( !m_selectedInto
,
186 wxT("deleting bitmap still selected into wxMemoryDC") );
188 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
192 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
194 wxLogLastError(wxT("DeleteObject(hbitmap)"));
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 // this function should be called from all wxBitmap ctors
207 void wxBitmap::Init()
209 // m_refData = NULL; done in the base class ctor
212 wxGDIImageRefData
*wxBitmap::CreateData() const
214 return new wxBitmapRefData
;
219 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
221 #ifndef __WXMICROWIN__
222 // it may be either HICON or HCURSOR
223 HICON hicon
= (HICON
)icon
.GetHandle();
226 if ( !::GetIconInfo(hicon
, &iconInfo
) )
228 wxLogLastError(wxT("GetIconInfo"));
233 wxBitmapRefData
*refData
= new wxBitmapRefData
;
236 int w
= icon
.GetWidth(),
237 h
= icon
.GetHeight();
239 refData
->m_width
= w
;
240 refData
->m_height
= h
;
241 refData
->m_depth
= wxDisplayDepth();
243 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
245 // the mask returned by GetIconInfo() is inversed compared to the usual
247 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
250 // delete the old one now as we don't need it any more
251 ::DeleteObject(iconInfo
.hbmMask
);
253 #if WXWIN_COMPATIBILITY_2
254 refData
->m_ok
= TRUE
;
255 #endif // WXWIN_COMPATIBILITY_2
265 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
273 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
277 return CopyFromIconOrCursor(cursor
);
281 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
288 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
289 // to create a bitmap from icon there - but using this way we won't have
292 int width
= icon
.GetWidth(),
293 height
= icon
.GetHeight();
295 // copy the icon to the bitmap
297 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
298 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
299 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
301 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
303 ::SelectObject(hdc
, hbmpOld
);
306 wxBitmapRefData
*refData
= new wxBitmapRefData
;
309 refData
->m_width
= width
;
310 refData
->m_height
= height
;
311 refData
->m_depth
= wxDisplayDepth();
313 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
315 #if WXWIN_COMPATIBILITY_2
316 refData
->m_ok
= TRUE
;
317 #endif // WXWIN_COMPATIBILITY_2
321 return CopyFromIconOrCursor(icon
);
322 #endif // Win16/Win32
325 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
327 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
329 HBITMAP hbitmap
= dib
.CreateDDB();
335 wxBitmapRefData
*refData
= new wxBitmapRefData
;
338 refData
->m_width
= dib
.GetWidth();
339 refData
->m_height
= dib
.GetHeight();
340 refData
->m_depth
= dib
.GetDepth();
342 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
345 wxPalette
*palette
= dib
.CreatePalette();
348 refData
->m_bitmapPalette
= *palette
;
352 #endif // wxUSE_PALETTE
357 wxBitmap::~wxBitmap()
361 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
365 #ifndef __WXMICROWIN__
366 wxBitmapRefData
*refData
= new wxBitmapRefData
;
369 refData
->m_width
= width
;
370 refData
->m_height
= height
;
371 refData
->m_depth
= depth
;
376 // we assume that it is in XBM format which is not quite the same as
377 // the format CreateBitmap() wants because the order of bytes in the
379 const size_t bytesPerLine
= (width
+ 7) / 8;
380 const size_t padding
= bytesPerLine
% 2;
381 const size_t len
= height
* ( padding
+ bytesPerLine
);
382 data
= (char *)malloc(len
);
383 const char *src
= bits
;
386 for ( int rows
= 0; rows
< height
; rows
++ )
388 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
390 unsigned char val
= *src
++;
391 unsigned char reversed
= 0;
393 for ( int bits
= 0; bits
< 8; bits
++)
396 reversed
|= (val
& 0x01);
408 // bits should already be in Windows standard format
409 data
= (char *)bits
; // const_cast is harmless
412 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
415 wxLogLastError(wxT("CreateBitmap"));
423 SetHBITMAP((WXHBITMAP
)hbmp
);
427 // Create from XPM data
428 bool wxBitmap::CreateFromXpm(const char **data
)
430 #if wxUSE_IMAGE && wxUSE_XPM
433 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
435 wxXPMDecoder decoder
;
436 wxImage img
= decoder
.ReadData(data
);
437 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
439 *this = wxBitmap(img
);
446 wxBitmap::wxBitmap(int w
, int h
, int d
)
450 (void)Create(w
, h
, d
);
453 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
457 (void)Create(w
, h
, dc
);
460 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
464 (void)Create(data
, type
, width
, height
, depth
);
467 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
471 LoadFile(filename
, (int)type
);
474 bool wxBitmap::Create(int width
, int height
, int depth
)
476 return DoCreate(width
, height
, depth
, 0);
479 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
481 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
483 return DoCreate(width
, height
, -1, dc
.GetHDC());
486 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
490 m_refData
= new wxBitmapRefData
;
492 GetBitmapData()->m_width
= w
;
493 GetBitmapData()->m_height
= h
;
497 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
501 // create DIBs without alpha channel by default
509 // don't delete the DIB section in dib object dtor
512 GetBitmapData()->m_isDIB
= TRUE
;
513 GetBitmapData()->m_depth
= d
;
518 d
= wxDisplayDepth();
520 GetBitmapData()->m_depth
= d
;
522 #ifndef __WXMICROWIN__
525 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
528 wxLogLastError(wxT("CreateBitmap"));
532 #endif // !__WXMICROWIN__
535 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
538 wxLogLastError(wxT("CreateCompatibleBitmap"));
541 GetBitmapData()->m_depth
= wxDisplayDepth();
545 SetHBITMAP((WXHBITMAP
)hbmp
);
547 #if WXWIN_COMPATIBILITY_2
548 GetBitmapData()->m_ok
= hbmp
!= 0;
549 #endif // WXWIN_COMPATIBILITY_2
556 // ----------------------------------------------------------------------------
557 // wxImage to/from conversions for Microwin
558 // ----------------------------------------------------------------------------
560 // Microwin versions are so different from normal ones that it really doesn't
561 // make sense to use #ifdefs inside the function bodies
562 #ifdef __WXMICROWIN__
564 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
566 // Set this to 1 to experiment with mask code,
567 // which currently doesn't work
570 m_refData
= new wxBitmapRefData();
572 // Initial attempt at a simple-minded implementation.
573 // The bitmap will always be created at the screen depth,
574 // so the 'depth' argument is ignored.
576 HDC hScreenDC
= ::GetDC(NULL
);
577 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
579 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
580 HBITMAP hMaskBitmap
= NULL
;
581 HBITMAP hOldMaskBitmap
= NULL
;
583 unsigned char maskR
= 0;
584 unsigned char maskG
= 0;
585 unsigned char maskB
= 0;
587 // printf("Created bitmap %d\n", (int) hBitmap);
590 ::ReleaseDC(NULL
, hScreenDC
);
593 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
595 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
596 ::ReleaseDC(NULL
, hScreenDC
);
598 // created an mono-bitmap for the possible mask
599 bool hasMask
= image
.HasMask();
604 // FIXME: we should be able to pass bpp = 1, but
605 // GdBlit can't handle a different depth
607 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
609 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
611 maskR
= image
.GetMaskRed();
612 maskG
= image
.GetMaskGreen();
613 maskB
= image
.GetMaskBlue();
621 hScreenDC
= ::GetDC(NULL
);
622 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
623 ::ReleaseDC(NULL
, hScreenDC
);
625 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
633 for (i
= 0; i
< image
.GetWidth(); i
++)
635 for (j
= 0; j
< image
.GetHeight(); j
++)
637 unsigned char red
= image
.GetRed(i
, j
);
638 unsigned char green
= image
.GetGreen(i
, j
);
639 unsigned char blue
= image
.GetBlue(i
, j
);
641 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
645 // scan the bitmap for the transparent colour and set the corresponding
646 // pixels in the mask to BLACK and the rest to WHITE
647 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
648 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
650 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
655 ::SelectObject(hMemDC
, hOldBitmap
);
659 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
662 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
665 SetWidth(image
.GetWidth());
666 SetHeight(image
.GetHeight());
667 SetDepth(screenDepth
);
668 SetHBITMAP( (WXHBITMAP
) hBitmap
);
671 // Copy the palette from the source image
672 SetPalette(image
.GetPalette());
673 #endif // wxUSE_PALETTE
675 #if WXWIN_COMPATIBILITY_2
676 // check the wxBitmap object
677 GetBitmapData()->SetOk();
678 #endif // WXWIN_COMPATIBILITY_2
683 wxImage
wxBitmap::ConvertToImage() const
685 // Initial attempt at a simple-minded implementation.
686 // The bitmap will always be created at the screen depth,
687 // so the 'depth' argument is ignored.
688 // TODO: transparency (create a mask image)
692 wxFAIL_MSG( wxT("bitmap is invalid") );
698 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
700 // create an wxImage object
701 int width
= GetWidth();
702 int height
= GetHeight();
703 image
.Create( width
, height
);
704 unsigned char *data
= image
.GetData();
707 wxFAIL_MSG( wxT("could not allocate data for image") );
711 HDC hScreenDC
= ::GetDC(NULL
);
713 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
714 ::ReleaseDC(NULL
, hScreenDC
);
716 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
718 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
721 for (i
= 0; i
< GetWidth(); i
++)
723 for (j
= 0; j
< GetHeight(); j
++)
725 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
726 unsigned char red
= GetRValue(color
);
727 unsigned char green
= GetGValue(color
);
728 unsigned char blue
= GetBValue(color
);
730 image
.SetRGB(i
, j
, red
, green
, blue
);
734 ::SelectObject(hMemDC
, hOldBitmap
);
738 // Copy the palette from the source image
740 image
.SetPalette(* GetPalette());
741 #endif // wxUSE_PALETTE
746 #endif // __WXMICROWIN__
748 // ----------------------------------------------------------------------------
749 // wxImage to/from conversions
750 // ----------------------------------------------------------------------------
752 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
754 return CreateFromImage(image
, depth
, 0);
757 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
759 wxCHECK_MSG( dc
.Ok(), FALSE
,
760 _T("invalid HDC in wxBitmap::CreateFromImage()") );
762 return CreateFromImage(image
, -1, dc
.GetHDC());
765 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
767 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
771 // first convert the image to DIB
772 const int h
= image
.GetHeight();
773 const int w
= image
.GetWidth();
780 // store the bitmap parameters
781 wxBitmapRefData
*refData
= new wxBitmapRefData
;
782 refData
->m_width
= w
;
783 refData
->m_height
= h
;
784 refData
->m_hasAlpha
= image
.HasAlpha();
789 // next either store DIB as is or create a DDB from it
792 // are we going to use DIB?
794 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
795 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
797 // don't delete the DIB section in dib object dtor
798 hbitmap
= dib
.Detach();
800 refData
->m_isDIB
= TRUE
;
801 refData
->m_depth
= dib
.GetDepth();
803 else // we need to convert DIB to DDB
805 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
807 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
810 // validate this object
811 SetHBITMAP((WXHBITMAP
)hbitmap
);
813 #if WXWIN_COMPATIBILITY_2
814 m_refData
->m_ok
= TRUE
;
815 #endif // WXWIN_COMPATIBILITY_2
817 // finally also set the mask if we have one
818 if ( image
.HasMask() )
820 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
821 image
.GetMaskGreen(),
822 image
.GetMaskBlue())));
828 wxImage
wxBitmap::ConvertToImage() const
830 // the colour used as transparent one in wxImage and the one it is replaced
831 // with when it really occurs in the bitmap
832 static const int MASK_RED
= 1;
833 static const int MASK_GREEN
= 2;
834 static const int MASK_BLUE
= 3;
835 static const int MASK_BLUE_REPLACEMENT
= 2;
839 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
841 // create an wxImage object
842 int width
= GetWidth();
843 int height
= GetHeight();
844 image
.Create( width
, height
);
845 unsigned char *data
= image
.GetData();
848 wxFAIL_MSG( wxT("could not allocate data for image") );
852 // calc the number of bytes per scanline and padding in the DIB
853 int bytePerLine
= width
*3;
854 int sizeDWORD
= sizeof( DWORD
);
855 int lineBoundary
= bytePerLine
% sizeDWORD
;
857 if( lineBoundary
> 0 )
859 padding
= sizeDWORD
- lineBoundary
;
860 bytePerLine
+= padding
;
863 // create a DIB header
864 int headersize
= sizeof(BITMAPINFOHEADER
);
865 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
868 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
872 // Fill in the DIB header
873 lpDIBh
->bmiHeader
.biSize
= headersize
;
874 lpDIBh
->bmiHeader
.biWidth
= width
;
875 lpDIBh
->bmiHeader
.biHeight
= -height
;
876 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
877 lpDIBh
->bmiHeader
.biPlanes
= 1;
878 lpDIBh
->bmiHeader
.biBitCount
= 24;
879 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
880 lpDIBh
->bmiHeader
.biClrUsed
= 0;
881 // These seem not really needed for our purpose here.
882 lpDIBh
->bmiHeader
.biClrImportant
= 0;
883 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
884 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
885 // memory for DIB data
886 unsigned char *lpBits
;
887 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
890 wxFAIL_MSG( wxT("could not allocate data for DIB") );
896 // copy data from the device-dependent bitmap to the DIB
897 HDC hdc
= ::GetDC(NULL
);
899 hbitmap
= (HBITMAP
) GetHBITMAP();
900 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
902 // copy DIB data into the wxImage object
904 unsigned char *ptdata
= data
;
905 unsigned char *ptbits
= lpBits
;
906 for( i
=0; i
<height
; i
++ )
908 for( j
=0; j
<width
; j
++ )
910 *(ptdata
++) = *(ptbits
+2);
911 *(ptdata
++) = *(ptbits
+1);
912 *(ptdata
++) = *(ptbits
);
918 // similarly, set data according to the possible mask bitmap
919 if( GetMask() && GetMask()->GetMaskBitmap() )
921 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
922 // memory DC created, color set, data copied, and memory DC deleted
923 HDC memdc
= ::CreateCompatibleDC( hdc
);
924 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
925 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
926 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
930 for( i
=0; i
<height
; i
++ )
932 for( j
=0; j
<width
; j
++ )
934 // is this pixel transparent?
937 if ( (ptdata
[0] == MASK_RED
) &&
938 (ptdata
[1] == MASK_GREEN
) &&
939 (ptdata
[2] == MASK_BLUE
) )
941 // we have to fudge the colour a bit to prevent this
942 // pixel from appearing transparent
943 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
949 *(ptdata
++) = MASK_RED
;
950 *(ptdata
++) = MASK_GREEN
;
951 *(ptdata
++) = MASK_BLUE
;
958 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
959 image
.SetMask( TRUE
);
963 image
.SetMask( FALSE
);
965 // free allocated resources
966 ::ReleaseDC(NULL
, hdc
);
973 #endif // wxUSE_IMAGE
975 // ----------------------------------------------------------------------------
976 // loading and saving bitmaps
977 // ----------------------------------------------------------------------------
979 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
983 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
987 m_refData
= new wxBitmapRefData
;
989 return handler
->LoadFile(this, filename
, type
, -1, -1);
995 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
997 *this = wxBitmap(image
);
1002 #endif // wxUSE_IMAGE
1007 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1011 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1015 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1020 m_refData
= new wxBitmapRefData
;
1022 return handler
->Create(this, data
, type
, width
, height
, depth
);
1025 bool wxBitmap::SaveFile(const wxString
& filename
,
1027 const wxPalette
*palette
)
1029 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1033 return handler
->SaveFile(this, filename
, type
, palette
);
1038 // FIXME what about palette? shouldn't we use it?
1039 wxImage image
= ConvertToImage();
1042 return image
.SaveFile(filename
, type
);
1045 #endif // wxUSE_IMAGE
1050 // ----------------------------------------------------------------------------
1051 // sub bitmap extraction
1052 // ----------------------------------------------------------------------------
1054 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1056 wxCHECK_MSG( Ok() &&
1057 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1058 (rect
.x
+rect
.width
<= GetWidth()) &&
1059 (rect
.y
+rect
.height
<= GetHeight()),
1060 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1062 wxBitmap
ret( rect
.width
, rect
.height
);
1063 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1065 #ifndef __WXMICROWIN__
1066 // TODO: copy alpha channel data if any
1073 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1074 selectDst(dcDst
, GetHbitmapOf(ret
));
1076 if ( !selectSrc
|| !selectDst
)
1078 wxLogLastError(_T("SelectObjct(hBitmap)"));
1081 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1082 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1084 wxLogLastError(_T("BitBlt"));
1088 // copy mask if there is one
1091 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1093 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1094 selectDst(dcDst
, hbmpMask
);
1096 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1097 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1099 wxLogLastError(_T("BitBlt"));
1102 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1105 #endif // !__WXMICROWIN__
1110 // ----------------------------------------------------------------------------
1111 // wxBitmap accessors
1112 // ----------------------------------------------------------------------------
1115 wxPalette
* wxBitmap::GetPalette() const
1117 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1118 : (wxPalette
*) NULL
;
1122 wxMask
*wxBitmap::GetMask() const
1124 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1129 wxDC
*wxBitmap::GetSelectedInto() const
1131 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1136 #if WXWIN_COMPATIBILITY_2_4
1138 int wxBitmap::GetQuality() const
1143 #endif // WXWIN_COMPATIBILITY_2_4
1145 void wxBitmap::UseAlpha()
1147 if ( GetBitmapData() )
1148 GetBitmapData()->m_hasAlpha
= true;
1151 bool wxBitmap::HasAlpha() const
1153 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1156 // ----------------------------------------------------------------------------
1158 // ----------------------------------------------------------------------------
1162 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1164 if ( GetBitmapData() )
1165 GetBitmapData()->m_selectedInto
= dc
;
1172 void wxBitmap::SetPalette(const wxPalette
& palette
)
1176 GetBitmapData()->m_bitmapPalette
= palette
;
1179 #endif // wxUSE_PALETTE
1181 void wxBitmap::SetMask(wxMask
*mask
)
1185 GetBitmapData()->SetMask(mask
);
1188 #if WXWIN_COMPATIBILITY_2
1190 void wxBitmap::SetOk(bool isOk
)
1194 GetBitmapData()->m_ok
= isOk
;
1197 #endif // WXWIN_COMPATIBILITY_2
1199 #if WXWIN_COMPATIBILITY_2_4
1201 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1205 #endif // WXWIN_COMPATIBILITY_2_4
1207 // ----------------------------------------------------------------------------
1208 // raw bitmap access support
1209 // ----------------------------------------------------------------------------
1211 #ifdef wxHAVE_RAW_BITMAP
1212 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1216 // no bitmap, no data (raw or otherwise)
1220 // if we're already a DIB we can access our data directly, but if not we
1221 // need to convert this DDB to a DIB section and use it for raw access and
1222 // then convert it back
1224 if ( !GetBitmapData()->m_isDIB
)
1226 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1227 _T("GetRawData() may be called only once") );
1229 wxDIB
*dib
= new wxDIB(*this);
1237 // we'll free it in UngetRawData()
1238 GetBitmapData()->m_dib
= dib
;
1240 hDIB
= dib
->GetHandle();
1244 hDIB
= GetHbitmap();
1248 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1250 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1255 // check that the bitmap is in correct format
1256 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1258 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1263 // ok, store the relevant info in wxPixelDataBase
1264 const LONG h
= ds
.dsBm
.bmHeight
;
1266 data
.m_width
= ds
.dsBm
.bmWidth
;
1269 // remember that DIBs are stored in top to bottom order!
1270 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1271 data
.m_stride
= -bytesPerRow
;
1273 char *bits
= (char *)ds
.dsBm
.bmBits
;
1276 bits
+= (h
- 1)*bytesPerRow
;
1282 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1287 // the cast is ugly but we can't do without it and without making this
1288 // function template (and hence inline) unfortunately
1289 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1290 PixelData
& data
= (PixelData
&)dataBase
;
1294 // invalid data, don't crash -- but don't assert neither as we're
1295 // called automatically from wxPixelDataBase dtor and so there is no
1296 // way to prevent this from happening
1300 if ( GetBitmapData()->m_hasAlpha
)
1302 // AlphaBlend() wants to have premultiplied source alpha but
1303 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1305 PixelData::Iterator
p(data
);
1307 const int w
= data
.GetWidth();
1308 const int h
= data
.GetHeight();
1310 for ( int y
= 0; y
< h
; y
++ )
1312 PixelData::Iterator rowStart
= p
;
1314 for ( int x
= 0; x
< w
; x
++ )
1316 const unsigned alpha
= p
.Alpha();
1318 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1319 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1320 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1329 // if we're a DDB we need to convert DIB back to DDB now to make the
1330 // changes made via raw bitmap access effective
1331 if ( !GetBitmapData()->m_isDIB
)
1333 wxDIB
*dib
= GetBitmapData()->m_dib
;
1334 GetBitmapData()->m_dib
= NULL
;
1342 #endif // #ifdef wxHAVE_RAW_BITMAP
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1353 // Construct a mask from a bitmap and a colour indicating
1354 // the transparent area
1355 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1358 Create(bitmap
, colour
);
1361 // Construct a mask from a bitmap and a palette index indicating
1362 // the transparent area
1363 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1366 Create(bitmap
, paletteIndex
);
1369 // Construct a mask from a mono bitmap (copies the bitmap).
1370 wxMask::wxMask(const wxBitmap
& bitmap
)
1379 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1382 // Create a mask from a mono bitmap (copies the bitmap).
1383 bool wxMask::Create(const wxBitmap
& bitmap
)
1385 #ifndef __WXMICROWIN__
1386 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1387 _T("can't create mask from invalid or not monochrome bitmap") );
1391 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1395 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1400 HDC srcDC
= CreateCompatibleDC(0);
1401 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1402 HDC destDC
= CreateCompatibleDC(0);
1403 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1404 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1405 SelectObject(srcDC
, 0);
1407 SelectObject(destDC
, 0);
1415 // Create a mask from a bitmap and a palette index indicating
1416 // the transparent area
1417 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1421 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1426 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1428 unsigned char red
, green
, blue
;
1429 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1431 wxColour
transparentColour(red
, green
, blue
);
1432 return Create(bitmap
, transparentColour
);
1435 #endif // wxUSE_PALETTE
1440 // Create a mask from a bitmap and a colour indicating
1441 // the transparent area
1442 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1444 #ifndef __WXMICROWIN__
1445 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1449 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1453 int width
= bitmap
.GetWidth(),
1454 height
= bitmap
.GetHeight();
1456 // scan the bitmap for the transparent colour and set the corresponding
1457 // pixels in the mask to BLACK and the rest to WHITE
1458 COLORREF maskColour
= wxColourToPalRGB(colour
);
1459 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1461 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1462 HDC destDC
= ::CreateCompatibleDC(NULL
);
1463 if ( !srcDC
|| !destDC
)
1465 wxLogLastError(wxT("CreateCompatibleDC"));
1470 // SelectObject() will fail
1471 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1472 _T("bitmap can't be selected in another DC") );
1474 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1477 wxLogLastError(wxT("SelectObject"));
1482 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1485 wxLogLastError(wxT("SelectObject"));
1492 // this will create a monochrome bitmap with 0 points for the pixels
1493 // which have the same value as the background colour and 1 for the
1495 ::SetBkColor(srcDC
, maskColour
);
1496 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1499 ::SelectObject(srcDC
, hbmpSrcOld
);
1501 ::SelectObject(destDC
, hbmpDstOld
);
1505 #else // __WXMICROWIN__
1507 #endif // __WXMICROWIN__/!__WXMICROWIN__
1510 // ----------------------------------------------------------------------------
1512 // ----------------------------------------------------------------------------
1514 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1517 int width
, int height
, int depth
)
1519 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1521 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1524 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1525 const wxString
& name
,
1527 int width
, int height
)
1529 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1531 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1534 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1535 const wxString
& name
,
1538 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1540 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1543 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1544 void *WXUNUSED(data
),
1545 long WXUNUSED(type
),
1546 int WXUNUSED(width
),
1547 int WXUNUSED(height
),
1548 int WXUNUSED(depth
))
1553 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1554 const wxString
& WXUNUSED(name
),
1555 long WXUNUSED(type
),
1556 int WXUNUSED(desiredWidth
),
1557 int WXUNUSED(desiredHeight
))
1562 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1563 const wxString
& WXUNUSED(name
),
1565 const wxPalette
*WXUNUSED(palette
))
1570 // ----------------------------------------------------------------------------
1572 // ----------------------------------------------------------------------------
1574 #ifndef __WXMICROWIN__
1575 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1576 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1578 unsigned long i
, headerSize
;
1579 LPBITMAPINFO lpDIBheader
= NULL
;
1580 LPPALETTEENTRY lpPe
= NULL
;
1583 // Allocate space for a DIB header
1584 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1585 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1586 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1588 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1590 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1592 // Fill in the static parts of the DIB header
1593 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1594 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1595 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1596 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1598 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1599 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1600 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1601 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1602 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1605 // Initialize the DIB palette
1606 for (i
= 0; i
< 256; i
++) {
1607 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1608 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1609 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1610 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1613 *lpDIBHeader
= lpDIBheader
;
1618 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1624 // ----------------------------------------------------------------------------
1625 // global helper functions implemented here
1626 // ----------------------------------------------------------------------------
1628 // helper of wxBitmapToHICON/HCURSOR
1630 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1637 // we can't create an icon/cursor form nothing
1641 wxMask
*mask
= bmp
.GetMask();
1644 // we must have a mask for an icon, so even if it's probably incorrect,
1645 // do create it (grey is the "standard" transparent colour)
1646 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1650 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1653 iconInfo
.xHotspot
= hotSpotX
;
1654 iconInfo
.yHotspot
= hotSpotY
;
1657 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1658 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1660 // black out the transparent area to preserve background colour, because
1661 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1662 // the mask to the dest rect.
1664 MemoryHDC dcSrc
, dcDst
;
1665 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1666 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1668 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1669 dcSrc
, 0, 0, SRCAND
) )
1671 wxLogLastError(_T("BitBlt"));
1675 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1677 if ( !bmp
.GetMask() )
1679 // we created the mask, now delete it
1683 // delete the inverted mask bitmap we created as well
1684 ::DeleteObject(iconInfo
.hbmMask
);
1689 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1691 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1694 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1696 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1699 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1701 #ifndef __WXMICROWIN__
1702 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1704 // get width/height from the bitmap if not given
1708 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1713 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1714 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1715 if ( !hdcSrc
|| !hdcDst
)
1717 wxLogLastError(wxT("CreateCompatibleDC"));
1720 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1723 wxLogLastError(wxT("CreateBitmap"));
1726 ::SelectObject(hdcSrc
, hbmpMask
);
1727 ::SelectObject(hdcDst
, hbmpInvMask
);
1728 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1732 wxLogLastError(wxT("BitBlt"));