1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
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 wxBitmapRefData(const wxBitmapRefData
& data
);
71 virtual ~wxBitmapRefData() { Free(); }
75 // set the mask object to use as the mask, we take ownership of it
76 void SetMask(wxMask
*mask
)
82 // set the HBITMAP to use as the mask
83 void SetMask(HBITMAP hbmpMask
)
85 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
89 wxMask
*GetMask() const { return m_bitmapMask
; }
93 wxPalette m_bitmapPalette
;
94 #endif // wxUSE_PALETTE
100 // this field is solely for error checking: we detect selecting a bitmap
101 // into more than one DC at once or deleting a bitmap still selected into a
102 // DC (both are serious programming errors under Windows)
103 wxDC
*m_selectedInto
;
104 #endif // __WXDEBUG__
107 // when GetRawData() is called for a DDB we need to convert it to a DIB
108 // first to be able to provide direct access to it and we cache that DIB
109 // here and convert it back to DDB when UngetRawData() is called
113 // true if we have alpha transparency info and can be drawn using
117 // true if our HBITMAP is a DIB section, false if it is a DDB
121 // optional mask for transparent drawing
122 wxMask
*m_bitmapMask
;
126 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
134 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
136 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // decide whether we should create a DIB or a DDB for the given parameters
148 // NB: we always use DIBs under Windows CE as this is much simpler (even if
149 // also less efficient...) and we obviously can't use them if there is no
150 // DIB support compiled in at all
152 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
154 #define ALWAYS_USE_DIB
156 // no sense in defining wxShouldCreateDIB() as we can't compile code
157 // executed if it is true, so we have to use #if's anyhow
158 #define NEVER_USE_DIB
159 #else // wxUSE_WXDIB && !__WXWINCE__
160 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
162 // here is the logic:
164 // (a) if hdc is specified, the caller explicitly wants DDB
165 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
166 // or less DIBs anyhow)
167 // (c) finally, create DIBs under Win9x even if the depth hasn't been
168 // explicitly specified but the current display depth is 24 or
169 // more and the image is "big", i.e. > 16Mb which is the
170 // theoretical limit for DDBs under Win9x
172 // consequences (all of which seem to make sense):
174 // (i) by default, DDBs are created (depth == -1 usually)
175 // (ii) DIBs can be created by explicitly specifying the depth
176 // (iii) using a DC always forces creating a DDB
180 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
183 #define SOMETIMES_USE_DIB
184 #endif // different DIB usage scenarious
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 wxBitmapRefData::wxBitmapRefData()
193 m_selectedInto
= NULL
;
197 m_hBitmap
= (WXHBITMAP
) NULL
;
206 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
207 : wxGDIImageRefData(data
)
210 m_selectedInto
= NULL
;
213 // can't copy the mask as the other bitmap destroys it
216 wxASSERT_MSG( !data
.m_isDIB
,
217 _T("can't copy bitmap locked for raw access!") );
220 m_hasAlpha
= data
.m_hasAlpha
;
223 void wxBitmapRefData::Free()
225 wxASSERT_MSG( !m_selectedInto
,
226 wxT("deleting bitmap still selected into wxMemoryDC") );
229 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
234 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
236 wxLogLastError(wxT("DeleteObject(hbitmap)"));
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 // this function should be called from all wxBitmap ctors
249 void wxBitmap::Init()
251 // m_refData = NULL; done in the base class ctor
254 wxGDIImageRefData
*wxBitmap::CreateData() const
256 return new wxBitmapRefData
;
259 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
261 const wxBitmapRefData
*
262 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
266 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
267 self
->m_refData
= new wxBitmapRefData(*data
);
270 // copy the other bitmap
271 if ( data
->m_hBitmap
)
273 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
274 self
->CopyFromDIB(dib
);
276 #endif // wxUSE_WXDIB
283 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
285 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
286 // it may be either HICON or HCURSOR
287 HICON hicon
= (HICON
)icon
.GetHandle();
290 if ( !::GetIconInfo(hicon
, &iconInfo
) )
292 wxLogLastError(wxT("GetIconInfo"));
297 wxBitmapRefData
*refData
= new wxBitmapRefData
;
300 int w
= icon
.GetWidth(),
301 h
= icon
.GetHeight();
303 refData
->m_width
= w
;
304 refData
->m_height
= h
;
305 refData
->m_depth
= wxDisplayDepth();
307 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
309 // the mask returned by GetIconInfo() is inversed compared to the usual
311 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
314 // delete the old one now as we don't need it any more
315 ::DeleteObject(iconInfo
.hbmMask
);
326 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
333 return CopyFromIconOrCursor(cursor
);
336 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
343 return CopyFromIconOrCursor(icon
);
346 #ifndef NEVER_USE_DIB
348 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
350 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
352 #ifdef SOMETIMES_USE_DIB
353 HBITMAP hbitmap
= dib
.CreateDDB();
356 #else // ALWAYS_USE_DIB
357 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
358 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
362 wxBitmapRefData
*refData
= new wxBitmapRefData
;
365 refData
->m_width
= dib
.GetWidth();
366 refData
->m_height
= dib
.GetHeight();
367 refData
->m_depth
= dib
.GetDepth();
369 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
372 wxPalette
*palette
= dib
.CreatePalette();
375 refData
->m_bitmapPalette
= *palette
;
379 #endif // wxUSE_PALETTE
384 #endif // NEVER_USE_DIB
386 wxBitmap::~wxBitmap()
390 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
394 #ifndef __WXMICROWIN__
395 wxBitmapRefData
*refData
= new wxBitmapRefData
;
398 refData
->m_width
= width
;
399 refData
->m_height
= height
;
400 refData
->m_depth
= depth
;
405 // we assume that it is in XBM format which is not quite the same as
406 // the format CreateBitmap() wants because the order of bytes in the
408 const size_t bytesPerLine
= (width
+ 7) / 8;
409 const size_t padding
= bytesPerLine
% 2;
410 const size_t len
= height
* ( padding
+ bytesPerLine
);
411 data
= (char *)malloc(len
);
412 const char *src
= bits
;
415 for ( int rows
= 0; rows
< height
; rows
++ )
417 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
419 unsigned char val
= *src
++;
420 unsigned char reversed
= 0;
422 for ( int bits
= 0; bits
< 8; bits
++)
425 reversed
|= (val
& 0x01);
437 // bits should already be in Windows standard format
438 data
= (char *)bits
; // const_cast is harmless
441 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
444 wxLogLastError(wxT("CreateBitmap"));
452 SetHBITMAP((WXHBITMAP
)hbmp
);
456 // Create from XPM data
457 #if wxUSE_IMAGE && wxUSE_XPM
458 bool wxBitmap::CreateFromXpm(const char **data
)
460 bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data
))
463 #if wxUSE_IMAGE && wxUSE_XPM
466 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
468 wxXPMDecoder decoder
;
469 wxImage img
= decoder
.ReadData(data
);
470 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
472 *this = wxBitmap(img
);
479 wxBitmap::wxBitmap(int w
, int h
, int d
)
483 (void)Create(w
, h
, d
);
486 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
490 (void)Create(w
, h
, dc
);
493 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
497 (void)Create(data
, type
, width
, height
, depth
);
500 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
504 LoadFile(filename
, (int)type
);
507 bool wxBitmap::Create(int width
, int height
, int depth
)
509 return DoCreate(width
, height
, depth
, 0);
512 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
514 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
516 return DoCreate(width
, height
, -1, dc
.GetHDC());
519 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
523 m_refData
= new wxBitmapRefData
;
525 GetBitmapData()->m_width
= w
;
526 GetBitmapData()->m_height
= h
;
528 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
530 #ifndef NEVER_USE_DIB
531 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
535 // create DIBs without alpha channel by default
543 // don't delete the DIB section in dib object dtor
546 GetBitmapData()->m_isDIB
= TRUE
;
547 GetBitmapData()->m_depth
= d
;
550 #endif // NEVER_USE_DIB
552 #ifndef ALWAYS_USE_DIB
553 #ifndef __WXMICROWIN__
556 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
559 wxLogLastError(wxT("CreateBitmap"));
562 GetBitmapData()->m_depth
= d
;
564 else // d == 0, create bitmap compatible with the screen
565 #endif // !__WXMICROWIN__
568 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
571 wxLogLastError(wxT("CreateCompatibleBitmap"));
574 GetBitmapData()->m_depth
= wxDisplayDepth();
576 #endif // !ALWAYS_USE_DIB
579 SetHBITMAP((WXHBITMAP
)hbmp
);
586 // ----------------------------------------------------------------------------
587 // wxImage to/from conversions for Microwin
588 // ----------------------------------------------------------------------------
590 // Microwin versions are so different from normal ones that it really doesn't
591 // make sense to use #ifdefs inside the function bodies
592 #ifdef __WXMICROWIN__
594 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
596 // Set this to 1 to experiment with mask code,
597 // which currently doesn't work
600 m_refData
= new wxBitmapRefData();
602 // Initial attempt at a simple-minded implementation.
603 // The bitmap will always be created at the screen depth,
604 // so the 'depth' argument is ignored.
606 HDC hScreenDC
= ::GetDC(NULL
);
607 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
609 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
610 HBITMAP hMaskBitmap
= NULL
;
611 HBITMAP hOldMaskBitmap
= NULL
;
613 unsigned char maskR
= 0;
614 unsigned char maskG
= 0;
615 unsigned char maskB
= 0;
617 // printf("Created bitmap %d\n", (int) hBitmap);
620 ::ReleaseDC(NULL
, hScreenDC
);
623 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
625 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
626 ::ReleaseDC(NULL
, hScreenDC
);
628 // created an mono-bitmap for the possible mask
629 bool hasMask
= image
.HasMask();
634 // FIXME: we should be able to pass bpp = 1, but
635 // GdBlit can't handle a different depth
637 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
639 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
641 maskR
= image
.GetMaskRed();
642 maskG
= image
.GetMaskGreen();
643 maskB
= image
.GetMaskBlue();
651 hScreenDC
= ::GetDC(NULL
);
652 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
653 ::ReleaseDC(NULL
, hScreenDC
);
655 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
663 for (i
= 0; i
< image
.GetWidth(); i
++)
665 for (j
= 0; j
< image
.GetHeight(); j
++)
667 unsigned char red
= image
.GetRed(i
, j
);
668 unsigned char green
= image
.GetGreen(i
, j
);
669 unsigned char blue
= image
.GetBlue(i
, j
);
671 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
675 // scan the bitmap for the transparent colour and set the corresponding
676 // pixels in the mask to BLACK and the rest to WHITE
677 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
678 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
680 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
685 ::SelectObject(hMemDC
, hOldBitmap
);
689 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
692 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
695 SetWidth(image
.GetWidth());
696 SetHeight(image
.GetHeight());
697 SetDepth(screenDepth
);
698 SetHBITMAP( (WXHBITMAP
) hBitmap
);
701 // Copy the palette from the source image
702 SetPalette(image
.GetPalette());
703 #endif // wxUSE_PALETTE
708 wxImage
wxBitmap::ConvertToImage() const
710 // Initial attempt at a simple-minded implementation.
711 // The bitmap will always be created at the screen depth,
712 // so the 'depth' argument is ignored.
713 // TODO: transparency (create a mask image)
717 wxFAIL_MSG( wxT("bitmap is invalid") );
723 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
725 // create an wxImage object
726 int width
= GetWidth();
727 int height
= GetHeight();
728 image
.Create( width
, height
);
729 unsigned char *data
= image
.GetData();
732 wxFAIL_MSG( wxT("could not allocate data for image") );
736 HDC hScreenDC
= ::GetDC(NULL
);
738 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
739 ::ReleaseDC(NULL
, hScreenDC
);
741 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
743 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
746 for (i
= 0; i
< GetWidth(); i
++)
748 for (j
= 0; j
< GetHeight(); j
++)
750 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
751 unsigned char red
= GetRValue(color
);
752 unsigned char green
= GetGValue(color
);
753 unsigned char blue
= GetBValue(color
);
755 image
.SetRGB(i
, j
, red
, green
, blue
);
759 ::SelectObject(hMemDC
, hOldBitmap
);
763 // Copy the palette from the source image
765 image
.SetPalette(* GetPalette());
766 #endif // wxUSE_PALETTE
771 #endif // __WXMICROWIN__
773 // ----------------------------------------------------------------------------
774 // wxImage to/from conversions
775 // ----------------------------------------------------------------------------
779 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
781 return CreateFromImage(image
, depth
, 0);
784 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
786 wxCHECK_MSG( dc
.Ok(), FALSE
,
787 _T("invalid HDC in wxBitmap::CreateFromImage()") );
789 return CreateFromImage(image
, -1, dc
.GetHDC());
792 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
794 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
798 // first convert the image to DIB
799 const int h
= image
.GetHeight();
800 const int w
= image
.GetWidth();
807 // store the bitmap parameters
808 wxBitmapRefData
*refData
= new wxBitmapRefData
;
809 refData
->m_width
= w
;
810 refData
->m_height
= h
;
811 refData
->m_hasAlpha
= image
.HasAlpha();
816 // next either store DIB as is or create a DDB from it
817 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
819 // are we going to use DIB?
821 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
822 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
824 // don't delete the DIB section in dib object dtor
825 hbitmap
= dib
.Detach();
827 refData
->m_isDIB
= TRUE
;
828 refData
->m_depth
= dib
.GetDepth();
830 #ifndef ALWAYS_USE_DIB
831 else // we need to convert DIB to DDB
833 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
835 refData
->m_depth
= depth
== -1 ? dib
.GetDepth() : depth
;
837 #endif // !ALWAYS_USE_DIB
839 // validate this object
840 SetHBITMAP((WXHBITMAP
)hbitmap
);
842 // finally also set the mask if we have one
843 if ( image
.HasMask() )
845 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
846 image
.GetMaskGreen(),
847 image
.GetMaskBlue())));
853 wxImage
wxBitmap::ConvertToImage() const
855 // convert DDB to DIB
863 // and then DIB to our wxImage
864 wxImage image
= dib
.ConvertToImage();
870 // now do the same for the mask, if we have any
871 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
874 wxDIB
dibMask(hbmpMask
);
875 if ( dibMask
.IsOk() )
877 // TODO: use wxRawBitmap to iterate over DIB
879 // we hard code the mask colour for now but we could also make an
880 // effort (and waste time) to choose a colour not present in the
881 // image already to avoid having to fudge the pixels below --
882 // whether it's worth to do it is unclear however
883 static const int MASK_RED
= 1;
884 static const int MASK_GREEN
= 2;
885 static const int MASK_BLUE
= 3;
886 static const int MASK_BLUE_REPLACEMENT
= 2;
888 const int h
= dibMask
.GetHeight();
889 const int w
= dibMask
.GetWidth();
890 const int bpp
= dibMask
.GetDepth();
891 const int maskBytesPerPixel
= bpp
>> 3;
892 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
893 unsigned char *data
= image
.GetData();
895 // remember that DIBs are stored in bottom to top order
897 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
899 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
901 // traverse one mask DIB line
902 unsigned char *mask
= maskLineStart
;
903 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
905 // should this pixel be transparent?
908 // no, check that it isn't transparent by accident
909 if ( (data
[0] == MASK_RED
) &&
910 (data
[1] == MASK_GREEN
) &&
911 (data
[2] == MASK_BLUE
) )
913 // we have to fudge the colour a bit to prevent
914 // this pixel from appearing transparent
915 data
[2] = MASK_BLUE_REPLACEMENT
;
920 else // yes, transparent pixel
923 *data
++ = MASK_GREEN
;
929 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
936 #endif // wxUSE_WXDIB
938 #endif // wxUSE_IMAGE
940 // ----------------------------------------------------------------------------
941 // loading and saving bitmaps
942 // ----------------------------------------------------------------------------
944 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
948 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
952 m_refData
= new wxBitmapRefData
;
954 return handler
->LoadFile(this, filename
, type
, -1, -1);
957 else // no bitmap handler found
960 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
962 *this = wxBitmap(image
);
967 #endif // wxUSE_IMAGE
972 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
976 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
980 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
985 m_refData
= new wxBitmapRefData
;
987 return handler
->Create(this, data
, type
, width
, height
, depth
);
990 bool wxBitmap::SaveFile(const wxString
& filename
,
992 const wxPalette
*palette
)
994 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
998 return handler
->SaveFile(this, filename
, type
, palette
);
1001 else // no bitmap handler found
1003 // FIXME what about palette? shouldn't we use it?
1004 wxImage image
= ConvertToImage();
1007 return image
.SaveFile(filename
, type
);
1010 #endif // wxUSE_IMAGE
1015 // ----------------------------------------------------------------------------
1016 // sub bitmap extraction
1017 // ----------------------------------------------------------------------------
1019 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1021 wxCHECK_MSG( Ok() &&
1022 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1023 (rect
.x
+rect
.width
<= GetWidth()) &&
1024 (rect
.y
+rect
.height
<= GetHeight()),
1025 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1027 wxBitmap
ret( rect
.width
, rect
.height
);
1028 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1030 #ifndef __WXMICROWIN__
1031 // TODO: copy alpha channel data if any
1038 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1039 selectDst(dcDst
, GetHbitmapOf(ret
));
1041 if ( !selectSrc
|| !selectDst
)
1043 wxLogLastError(_T("SelectObjct(hBitmap)"));
1046 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1047 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1049 wxLogLastError(_T("BitBlt"));
1053 // copy mask if there is one
1056 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1058 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1059 selectDst(dcDst
, hbmpMask
);
1061 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1062 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1064 wxLogLastError(_T("BitBlt"));
1067 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1070 #endif // !__WXMICROWIN__
1075 // ----------------------------------------------------------------------------
1076 // wxBitmap accessors
1077 // ----------------------------------------------------------------------------
1080 wxPalette
* wxBitmap::GetPalette() const
1082 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1083 : (wxPalette
*) NULL
;
1087 wxMask
*wxBitmap::GetMask() const
1089 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1094 wxDC
*wxBitmap::GetSelectedInto() const
1096 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1101 #if WXWIN_COMPATIBILITY_2_4
1103 int wxBitmap::GetQuality() const
1108 #endif // WXWIN_COMPATIBILITY_2_4
1110 void wxBitmap::UseAlpha()
1112 if ( GetBitmapData() )
1113 GetBitmapData()->m_hasAlpha
= true;
1116 bool wxBitmap::HasAlpha() const
1118 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1121 // ----------------------------------------------------------------------------
1123 // ----------------------------------------------------------------------------
1127 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1129 if ( GetBitmapData() )
1130 GetBitmapData()->m_selectedInto
= dc
;
1137 void wxBitmap::SetPalette(const wxPalette
& palette
)
1141 GetBitmapData()->m_bitmapPalette
= palette
;
1144 #endif // wxUSE_PALETTE
1146 void wxBitmap::SetMask(wxMask
*mask
)
1150 GetBitmapData()->SetMask(mask
);
1153 #if WXWIN_COMPATIBILITY_2_4
1155 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1159 #endif // WXWIN_COMPATIBILITY_2_4
1161 // ----------------------------------------------------------------------------
1162 // raw bitmap access support
1163 // ----------------------------------------------------------------------------
1165 #ifdef wxHAVE_RAW_BITMAP
1166 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1171 // no bitmap, no data (raw or otherwise)
1175 // if we're already a DIB we can access our data directly, but if not we
1176 // need to convert this DDB to a DIB section and use it for raw access and
1177 // then convert it back
1179 if ( !GetBitmapData()->m_isDIB
)
1181 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1182 _T("GetRawData() may be called only once") );
1184 wxDIB
*dib
= new wxDIB(*this);
1192 // we'll free it in UngetRawData()
1193 GetBitmapData()->m_dib
= dib
;
1195 hDIB
= dib
->GetHandle();
1199 hDIB
= GetHbitmap();
1203 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1205 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1210 // check that the bitmap is in correct format
1211 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1213 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1218 // ok, store the relevant info in wxPixelDataBase
1219 const LONG h
= ds
.dsBm
.bmHeight
;
1221 data
.m_width
= ds
.dsBm
.bmWidth
;
1224 // remember that DIBs are stored in top to bottom order!
1225 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1226 data
.m_stride
= -bytesPerRow
;
1228 char *bits
= (char *)ds
.dsBm
.bmBits
;
1231 bits
+= (h
- 1)*bytesPerRow
;
1240 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1246 // the cast is ugly but we can't do without it and without making this
1247 // function template (and hence inline) unfortunately
1248 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1249 PixelData
& data
= (PixelData
&)dataBase
;
1253 // invalid data, don't crash -- but don't assert neither as we're
1254 // called automatically from wxPixelDataBase dtor and so there is no
1255 // way to prevent this from happening
1259 if ( GetBitmapData()->m_hasAlpha
)
1261 // AlphaBlend() wants to have premultiplied source alpha but
1262 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1264 PixelData::Iterator
p(data
);
1266 const int w
= data
.GetWidth();
1267 const int h
= data
.GetHeight();
1269 for ( int y
= 0; y
< h
; y
++ )
1271 PixelData::Iterator rowStart
= p
;
1273 for ( int x
= 0; x
< w
; x
++ )
1275 const unsigned alpha
= p
.Alpha();
1277 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1278 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1279 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1289 // if we're a DDB we need to convert DIB back to DDB now to make the
1290 // changes made via raw bitmap access effective
1291 if ( !GetBitmapData()->m_isDIB
)
1293 wxDIB
*dib
= GetBitmapData()->m_dib
;
1294 GetBitmapData()->m_dib
= NULL
;
1300 #endif // wxUSE_WXDIB
1302 #endif // #ifdef wxHAVE_RAW_BITMAP
1304 // ----------------------------------------------------------------------------
1306 // ----------------------------------------------------------------------------
1313 // Construct a mask from a bitmap and a colour indicating
1314 // the transparent area
1315 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1318 Create(bitmap
, colour
);
1321 // Construct a mask from a bitmap and a palette index indicating
1322 // the transparent area
1323 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1326 Create(bitmap
, paletteIndex
);
1329 // Construct a mask from a mono bitmap (copies the bitmap).
1330 wxMask::wxMask(const wxBitmap
& bitmap
)
1339 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1342 // Create a mask from a mono bitmap (copies the bitmap).
1343 bool wxMask::Create(const wxBitmap
& bitmap
)
1345 #ifndef __WXMICROWIN__
1346 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1347 _T("can't create mask from invalid or not monochrome bitmap") );
1351 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1355 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1360 HDC srcDC
= CreateCompatibleDC(0);
1361 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1362 HDC destDC
= CreateCompatibleDC(0);
1363 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1364 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1365 SelectObject(srcDC
, 0);
1367 SelectObject(destDC
, 0);
1375 // Create a mask from a bitmap and a palette index indicating
1376 // the transparent area
1377 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1381 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1386 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1388 unsigned char red
, green
, blue
;
1389 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1391 wxColour
transparentColour(red
, green
, blue
);
1392 return Create(bitmap
, transparentColour
);
1395 #endif // wxUSE_PALETTE
1400 // Create a mask from a bitmap and a colour indicating
1401 // the transparent area
1402 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1404 #ifndef __WXMICROWIN__
1405 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1409 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1413 int width
= bitmap
.GetWidth(),
1414 height
= bitmap
.GetHeight();
1416 // scan the bitmap for the transparent colour and set the corresponding
1417 // pixels in the mask to BLACK and the rest to WHITE
1418 COLORREF maskColour
= wxColourToPalRGB(colour
);
1419 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1421 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1422 HDC destDC
= ::CreateCompatibleDC(NULL
);
1423 if ( !srcDC
|| !destDC
)
1425 wxLogLastError(wxT("CreateCompatibleDC"));
1430 // SelectObject() will fail
1431 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1432 _T("bitmap can't be selected in another DC") );
1434 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1437 wxLogLastError(wxT("SelectObject"));
1442 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1445 wxLogLastError(wxT("SelectObject"));
1452 // this will create a monochrome bitmap with 0 points for the pixels
1453 // which have the same value as the background colour and 1 for the
1455 ::SetBkColor(srcDC
, maskColour
);
1456 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1459 ::SelectObject(srcDC
, hbmpSrcOld
);
1461 ::SelectObject(destDC
, hbmpDstOld
);
1465 #else // __WXMICROWIN__
1467 #endif // __WXMICROWIN__/!__WXMICROWIN__
1470 // ----------------------------------------------------------------------------
1472 // ----------------------------------------------------------------------------
1474 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1477 int width
, int height
, int depth
)
1479 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1481 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1484 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1485 const wxString
& name
,
1487 int width
, int height
)
1489 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1491 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1494 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1495 const wxString
& name
,
1498 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1500 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1503 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1504 void *WXUNUSED(data
),
1505 long WXUNUSED(type
),
1506 int WXUNUSED(width
),
1507 int WXUNUSED(height
),
1508 int WXUNUSED(depth
))
1513 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1514 const wxString
& WXUNUSED(name
),
1515 long WXUNUSED(type
),
1516 int WXUNUSED(desiredWidth
),
1517 int WXUNUSED(desiredHeight
))
1522 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1523 const wxString
& WXUNUSED(name
),
1525 const wxPalette
*WXUNUSED(palette
))
1530 // ----------------------------------------------------------------------------
1532 // ----------------------------------------------------------------------------
1534 #ifndef __WXMICROWIN__
1535 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1536 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1538 unsigned long i
, headerSize
;
1540 // Allocate space for a DIB header
1541 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1542 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1543 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1545 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1547 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1549 // Fill in the static parts of the DIB header
1550 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1551 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1552 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1553 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1555 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1556 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1557 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1558 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1559 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1562 // Initialize the DIB palette
1563 for (i
= 0; i
< 256; i
++) {
1564 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1565 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1566 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1567 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1570 *lpDIBHeader
= lpDIBheader
;
1575 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1581 // ----------------------------------------------------------------------------
1582 // global helper functions implemented here
1583 // ----------------------------------------------------------------------------
1585 // helper of wxBitmapToHICON/HCURSOR
1587 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1594 // we can't create an icon/cursor form nothing
1598 wxMask
*mask
= bmp
.GetMask();
1601 // we must have a mask for an icon, so even if it's probably incorrect,
1602 // do create it (grey is the "standard" transparent colour)
1603 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1607 wxZeroMemory(iconInfo
);
1608 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1611 iconInfo
.xHotspot
= hotSpotX
;
1612 iconInfo
.yHotspot
= hotSpotY
;
1615 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1616 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1618 // black out the transparent area to preserve background colour, because
1619 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1620 // the mask to the dest rect.
1622 MemoryHDC dcSrc
, dcDst
;
1623 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1624 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1626 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1627 dcSrc
, 0, 0, SRCAND
) )
1629 wxLogLastError(_T("BitBlt"));
1633 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1635 if ( !bmp
.GetMask() )
1637 // we created the mask, now delete it
1641 // delete the inverted mask bitmap we created as well
1642 ::DeleteObject(iconInfo
.hbmMask
);
1647 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1649 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1652 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1654 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1657 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1659 #ifndef __WXMICROWIN__
1660 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1662 // get width/height from the bitmap if not given
1666 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1671 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1672 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1673 if ( !hdcSrc
|| !hdcDst
)
1675 wxLogLastError(wxT("CreateCompatibleDC"));
1678 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1681 wxLogLastError(wxT("CreateBitmap"));
1684 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1685 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1686 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1690 wxLogLastError(wxT("BitBlt"));
1694 SelectObject(hdcSrc
,srcTmp
);
1695 SelectObject(hdcDst
,dstTmp
);