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 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__
106 // when GetRawData() is called for a DDB we need to convert it to a DIB
107 // first to be able to provide direct access to it and we cache that DIB
108 // here and convert it back to DDB when UngetRawData() is called
112 // true if we have alpha transparency info and can be drawn using
116 // true if our HBITMAP is a DIB section, false if it is a DDB
120 // optional mask for transparent drawing
121 wxMask
*m_bitmapMask
;
123 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 // decide whether we should create a DIB or a DDB for the given parameters
145 // NB: we always use DIBs under Windows CE as this is much simpler (even if
146 // also less efficient...) and we obviously can't use them if there is no
147 // DIB support compiled in at all
149 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
151 #define ALWAYS_USE_DIB
153 // no sense in defining wxShouldCreateDIB() as we can't compile code
154 // executed if it is true, so we have to use #if's anyhow
155 #define NEVER_USE_DIB
156 #else // wxUSE_WXDIB && !__WXWINCE__
157 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
159 // here is the logic:
161 // (a) if hdc is specified, the caller explicitly wants DDB
162 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
163 // or less DIBs anyhow)
164 // (c) finally, create DIBs under Win9x even if the depth hasn't been
165 // explicitly specified but the current display depth is 24 or
166 // more and the image is "big", i.e. > 16Mb which is the
167 // theoretical limit for DDBs under Win9x
169 // consequences (all of which seem to make sense):
171 // (i) by default, DDBs are created (depth == -1 usually)
172 // (ii) DIBs can be created by explicitly specifying the depth
173 // (iii) using a DC always forces creating a DDB
177 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
180 #define SOMETIMES_USE_DIB
181 #endif // different DIB usage scenarious
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 wxBitmapRefData::wxBitmapRefData()
190 m_selectedInto
= NULL
;
194 m_hBitmap
= (WXHBITMAP
) NULL
;
203 void wxBitmapRefData::Free()
205 wxASSERT_MSG( !m_selectedInto
,
206 wxT("deleting bitmap still selected into wxMemoryDC") );
209 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
214 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
216 wxLogLastError(wxT("DeleteObject(hbitmap)"));
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 // this function should be called from all wxBitmap ctors
229 void wxBitmap::Init()
231 // m_refData = NULL; done in the base class ctor
234 wxGDIImageRefData
*wxBitmap::CreateData() const
236 return new wxBitmapRefData
;
241 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
243 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
244 // it may be either HICON or HCURSOR
245 HICON hicon
= (HICON
)icon
.GetHandle();
248 if ( !::GetIconInfo(hicon
, &iconInfo
) )
250 wxLogLastError(wxT("GetIconInfo"));
255 wxBitmapRefData
*refData
= new wxBitmapRefData
;
258 int w
= icon
.GetWidth(),
259 h
= icon
.GetHeight();
261 refData
->m_width
= w
;
262 refData
->m_height
= h
;
263 refData
->m_depth
= wxDisplayDepth();
265 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
267 // the mask returned by GetIconInfo() is inversed compared to the usual
269 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
272 // delete the old one now as we don't need it any more
273 ::DeleteObject(iconInfo
.hbmMask
);
284 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
291 return CopyFromIconOrCursor(cursor
);
294 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
301 return CopyFromIconOrCursor(icon
);
304 #ifndef NEVER_USE_DIB
306 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
308 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
310 #ifdef SOMETIMES_USE_DIB
311 HBITMAP hbitmap
= dib
.CreateDDB();
314 #else // ALWAYS_USE_DIB
315 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
316 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
320 wxBitmapRefData
*refData
= new wxBitmapRefData
;
323 refData
->m_width
= dib
.GetWidth();
324 refData
->m_height
= dib
.GetHeight();
325 refData
->m_depth
= dib
.GetDepth();
327 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
330 wxPalette
*palette
= dib
.CreatePalette();
333 refData
->m_bitmapPalette
= *palette
;
337 #endif // wxUSE_PALETTE
342 #endif // NEVER_USE_DIB
344 wxBitmap::~wxBitmap()
348 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
352 #ifndef __WXMICROWIN__
353 wxBitmapRefData
*refData
= new wxBitmapRefData
;
356 refData
->m_width
= width
;
357 refData
->m_height
= height
;
358 refData
->m_depth
= depth
;
363 // we assume that it is in XBM format which is not quite the same as
364 // the format CreateBitmap() wants because the order of bytes in the
366 const size_t bytesPerLine
= (width
+ 7) / 8;
367 const size_t padding
= bytesPerLine
% 2;
368 const size_t len
= height
* ( padding
+ bytesPerLine
);
369 data
= (char *)malloc(len
);
370 const char *src
= bits
;
373 for ( int rows
= 0; rows
< height
; rows
++ )
375 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
377 unsigned char val
= *src
++;
378 unsigned char reversed
= 0;
380 for ( int bits
= 0; bits
< 8; bits
++)
383 reversed
|= (val
& 0x01);
395 // bits should already be in Windows standard format
396 data
= (char *)bits
; // const_cast is harmless
399 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
402 wxLogLastError(wxT("CreateBitmap"));
410 SetHBITMAP((WXHBITMAP
)hbmp
);
414 // Create from XPM data
415 #if wxUSE_IMAGE && wxUSE_XPM
416 bool wxBitmap::CreateFromXpm(const char **data
)
418 bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data
))
421 #if wxUSE_IMAGE && wxUSE_XPM
424 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
426 wxXPMDecoder decoder
;
427 wxImage img
= decoder
.ReadData(data
);
428 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
430 *this = wxBitmap(img
);
437 wxBitmap::wxBitmap(int w
, int h
, int d
)
441 (void)Create(w
, h
, d
);
444 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
448 (void)Create(w
, h
, dc
);
451 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
455 (void)Create(data
, type
, width
, height
, depth
);
458 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
462 LoadFile(filename
, (int)type
);
465 bool wxBitmap::Create(int width
, int height
, int depth
)
467 return DoCreate(width
, height
, depth
, 0);
470 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
472 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
474 return DoCreate(width
, height
, -1, dc
.GetHDC());
477 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
481 m_refData
= new wxBitmapRefData
;
483 GetBitmapData()->m_width
= w
;
484 GetBitmapData()->m_height
= h
;
486 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
488 #ifndef NEVER_USE_DIB
489 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
493 // create DIBs without alpha channel by default
501 // don't delete the DIB section in dib object dtor
504 GetBitmapData()->m_isDIB
= TRUE
;
505 GetBitmapData()->m_depth
= d
;
508 #endif // NEVER_USE_DIB
510 #ifndef ALWAYS_USE_DIB
511 #ifndef __WXMICROWIN__
514 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
517 wxLogLastError(wxT("CreateBitmap"));
520 GetBitmapData()->m_depth
= d
;
522 else // d == 0, create bitmap compatible with the screen
523 #endif // !__WXMICROWIN__
526 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
529 wxLogLastError(wxT("CreateCompatibleBitmap"));
532 GetBitmapData()->m_depth
= wxDisplayDepth();
534 #endif // !ALWAYS_USE_DIB
537 SetHBITMAP((WXHBITMAP
)hbmp
);
544 // ----------------------------------------------------------------------------
545 // wxImage to/from conversions for Microwin
546 // ----------------------------------------------------------------------------
548 // Microwin versions are so different from normal ones that it really doesn't
549 // make sense to use #ifdefs inside the function bodies
550 #ifdef __WXMICROWIN__
552 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
554 // Set this to 1 to experiment with mask code,
555 // which currently doesn't work
558 m_refData
= new wxBitmapRefData();
560 // Initial attempt at a simple-minded implementation.
561 // The bitmap will always be created at the screen depth,
562 // so the 'depth' argument is ignored.
564 HDC hScreenDC
= ::GetDC(NULL
);
565 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
567 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
568 HBITMAP hMaskBitmap
= NULL
;
569 HBITMAP hOldMaskBitmap
= NULL
;
571 unsigned char maskR
= 0;
572 unsigned char maskG
= 0;
573 unsigned char maskB
= 0;
575 // printf("Created bitmap %d\n", (int) hBitmap);
578 ::ReleaseDC(NULL
, hScreenDC
);
581 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
583 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
584 ::ReleaseDC(NULL
, hScreenDC
);
586 // created an mono-bitmap for the possible mask
587 bool hasMask
= image
.HasMask();
592 // FIXME: we should be able to pass bpp = 1, but
593 // GdBlit can't handle a different depth
595 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
597 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
599 maskR
= image
.GetMaskRed();
600 maskG
= image
.GetMaskGreen();
601 maskB
= image
.GetMaskBlue();
609 hScreenDC
= ::GetDC(NULL
);
610 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
611 ::ReleaseDC(NULL
, hScreenDC
);
613 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
621 for (i
= 0; i
< image
.GetWidth(); i
++)
623 for (j
= 0; j
< image
.GetHeight(); j
++)
625 unsigned char red
= image
.GetRed(i
, j
);
626 unsigned char green
= image
.GetGreen(i
, j
);
627 unsigned char blue
= image
.GetBlue(i
, j
);
629 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
633 // scan the bitmap for the transparent colour and set the corresponding
634 // pixels in the mask to BLACK and the rest to WHITE
635 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
636 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
638 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
643 ::SelectObject(hMemDC
, hOldBitmap
);
647 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
650 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
653 SetWidth(image
.GetWidth());
654 SetHeight(image
.GetHeight());
655 SetDepth(screenDepth
);
656 SetHBITMAP( (WXHBITMAP
) hBitmap
);
659 // Copy the palette from the source image
660 SetPalette(image
.GetPalette());
661 #endif // wxUSE_PALETTE
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 // ----------------------------------------------------------------------------
737 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
739 return CreateFromImage(image
, depth
, 0);
742 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
744 wxCHECK_MSG( dc
.Ok(), FALSE
,
745 _T("invalid HDC in wxBitmap::CreateFromImage()") );
747 return CreateFromImage(image
, -1, dc
.GetHDC());
750 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
752 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
756 // first convert the image to DIB
757 const int h
= image
.GetHeight();
758 const int w
= image
.GetWidth();
765 // store the bitmap parameters
766 wxBitmapRefData
*refData
= new wxBitmapRefData
;
767 refData
->m_width
= w
;
768 refData
->m_height
= h
;
769 refData
->m_hasAlpha
= image
.HasAlpha();
774 // next either store DIB as is or create a DDB from it
775 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
777 // are we going to use DIB?
779 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
780 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
782 // don't delete the DIB section in dib object dtor
783 hbitmap
= dib
.Detach();
785 refData
->m_isDIB
= TRUE
;
786 refData
->m_depth
= dib
.GetDepth();
788 #ifndef ALWAYS_USE_DIB
789 else // we need to convert DIB to DDB
791 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
793 refData
->m_depth
= depth
== -1 ? dib
.GetDepth() : depth
;
795 #endif // !ALWAYS_USE_DIB
797 // validate this object
798 SetHBITMAP((WXHBITMAP
)hbitmap
);
800 // finally also set the mask if we have one
801 if ( image
.HasMask() )
803 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
804 image
.GetMaskGreen(),
805 image
.GetMaskBlue())));
811 wxImage
wxBitmap::ConvertToImage() const
813 // convert DDB to DIB
821 // and then DIB to our wxImage
822 wxImage image
= dib
.ConvertToImage();
828 // now do the same for the mask, if we have any
829 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
832 wxDIB
dibMask(hbmpMask
);
833 if ( dibMask
.IsOk() )
835 // TODO: use wxRawBitmap to iterate over DIB
837 // we hard code the mask colour for now but we could also make an
838 // effort (and waste time) to choose a colour not present in the
839 // image already to avoid having to fudge the pixels below --
840 // whether it's worth to do it is unclear however
841 static const int MASK_RED
= 1;
842 static const int MASK_GREEN
= 2;
843 static const int MASK_BLUE
= 3;
844 static const int MASK_BLUE_REPLACEMENT
= 2;
846 const int h
= dibMask
.GetHeight();
847 const int w
= dibMask
.GetWidth();
848 const int bpp
= dibMask
.GetDepth();
849 const int maskBytesPerPixel
= bpp
>> 3;
850 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
851 unsigned char *data
= image
.GetData();
853 // remember that DIBs are stored in bottom to top order
855 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
857 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
859 // traverse one mask DIB line
860 unsigned char *mask
= maskLineStart
;
861 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
863 // should this pixel be transparent?
866 // no, check that it isn't transparent by accident
867 if ( (data
[0] == MASK_RED
) &&
868 (data
[1] == MASK_GREEN
) &&
869 (data
[2] == MASK_BLUE
) )
871 // we have to fudge the colour a bit to prevent
872 // this pixel from appearing transparent
873 data
[2] = MASK_BLUE_REPLACEMENT
;
878 else // yes, transparent pixel
881 *data
++ = MASK_GREEN
;
887 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
894 #endif // wxUSE_WXDIB
896 #endif // wxUSE_IMAGE
898 // ----------------------------------------------------------------------------
899 // loading and saving bitmaps
900 // ----------------------------------------------------------------------------
902 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
906 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
910 m_refData
= new wxBitmapRefData
;
912 return handler
->LoadFile(this, filename
, type
, -1, -1);
915 else // no bitmap handler found
918 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
920 *this = wxBitmap(image
);
925 #endif // wxUSE_IMAGE
930 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
934 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
938 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
943 m_refData
= new wxBitmapRefData
;
945 return handler
->Create(this, data
, type
, width
, height
, depth
);
948 bool wxBitmap::SaveFile(const wxString
& filename
,
950 const wxPalette
*palette
)
952 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
956 return handler
->SaveFile(this, filename
, type
, palette
);
959 else // no bitmap handler found
961 // FIXME what about palette? shouldn't we use it?
962 wxImage image
= ConvertToImage();
965 return image
.SaveFile(filename
, type
);
968 #endif // wxUSE_IMAGE
973 // ----------------------------------------------------------------------------
974 // sub bitmap extraction
975 // ----------------------------------------------------------------------------
977 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
980 (rect
.x
>= 0) && (rect
.y
>= 0) &&
981 (rect
.x
+rect
.width
<= GetWidth()) &&
982 (rect
.y
+rect
.height
<= GetHeight()),
983 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
985 wxBitmap
ret( rect
.width
, rect
.height
);
986 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
988 #ifndef __WXMICROWIN__
989 // TODO: copy alpha channel data if any
996 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
997 selectDst(dcDst
, GetHbitmapOf(ret
));
999 if ( !selectSrc
|| !selectDst
)
1001 wxLogLastError(_T("SelectObjct(hBitmap)"));
1004 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1005 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1007 wxLogLastError(_T("BitBlt"));
1011 // copy mask if there is one
1014 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1016 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1017 selectDst(dcDst
, hbmpMask
);
1019 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1020 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1022 wxLogLastError(_T("BitBlt"));
1025 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1028 #endif // !__WXMICROWIN__
1033 // ----------------------------------------------------------------------------
1034 // wxBitmap accessors
1035 // ----------------------------------------------------------------------------
1038 wxPalette
* wxBitmap::GetPalette() const
1040 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1041 : (wxPalette
*) NULL
;
1045 wxMask
*wxBitmap::GetMask() const
1047 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1052 wxDC
*wxBitmap::GetSelectedInto() const
1054 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1059 #if WXWIN_COMPATIBILITY_2_4
1061 int wxBitmap::GetQuality() const
1066 #endif // WXWIN_COMPATIBILITY_2_4
1068 void wxBitmap::UseAlpha()
1070 if ( GetBitmapData() )
1071 GetBitmapData()->m_hasAlpha
= true;
1074 bool wxBitmap::HasAlpha() const
1076 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1079 // ----------------------------------------------------------------------------
1081 // ----------------------------------------------------------------------------
1085 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1087 if ( GetBitmapData() )
1088 GetBitmapData()->m_selectedInto
= dc
;
1095 void wxBitmap::SetPalette(const wxPalette
& palette
)
1099 GetBitmapData()->m_bitmapPalette
= palette
;
1102 #endif // wxUSE_PALETTE
1104 void wxBitmap::SetMask(wxMask
*mask
)
1108 GetBitmapData()->SetMask(mask
);
1111 #if WXWIN_COMPATIBILITY_2_4
1113 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1117 #endif // WXWIN_COMPATIBILITY_2_4
1119 // ----------------------------------------------------------------------------
1120 // raw bitmap access support
1121 // ----------------------------------------------------------------------------
1123 #ifdef wxHAVE_RAW_BITMAP
1124 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1129 // no bitmap, no data (raw or otherwise)
1133 // if we're already a DIB we can access our data directly, but if not we
1134 // need to convert this DDB to a DIB section and use it for raw access and
1135 // then convert it back
1137 if ( !GetBitmapData()->m_isDIB
)
1139 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1140 _T("GetRawData() may be called only once") );
1142 wxDIB
*dib
= new wxDIB(*this);
1150 // we'll free it in UngetRawData()
1151 GetBitmapData()->m_dib
= dib
;
1153 hDIB
= dib
->GetHandle();
1157 hDIB
= GetHbitmap();
1161 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1163 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1168 // check that the bitmap is in correct format
1169 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1171 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1176 // ok, store the relevant info in wxPixelDataBase
1177 const LONG h
= ds
.dsBm
.bmHeight
;
1179 data
.m_width
= ds
.dsBm
.bmWidth
;
1182 // remember that DIBs are stored in top to bottom order!
1183 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1184 data
.m_stride
= -bytesPerRow
;
1186 char *bits
= (char *)ds
.dsBm
.bmBits
;
1189 bits
+= (h
- 1)*bytesPerRow
;
1198 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1204 // the cast is ugly but we can't do without it and without making this
1205 // function template (and hence inline) unfortunately
1206 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1207 PixelData
& data
= (PixelData
&)dataBase
;
1211 // invalid data, don't crash -- but don't assert neither as we're
1212 // called automatically from wxPixelDataBase dtor and so there is no
1213 // way to prevent this from happening
1217 if ( GetBitmapData()->m_hasAlpha
)
1219 // AlphaBlend() wants to have premultiplied source alpha but
1220 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1222 PixelData::Iterator
p(data
);
1224 const int w
= data
.GetWidth();
1225 const int h
= data
.GetHeight();
1227 for ( int y
= 0; y
< h
; y
++ )
1229 PixelData::Iterator 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 // if we're a DDB we need to convert DIB back to DDB now to make the
1248 // changes made via raw bitmap access effective
1249 if ( !GetBitmapData()->m_isDIB
)
1251 wxDIB
*dib
= GetBitmapData()->m_dib
;
1252 GetBitmapData()->m_dib
= NULL
;
1258 #endif // wxUSE_WXDIB
1260 #endif // #ifdef wxHAVE_RAW_BITMAP
1262 // ----------------------------------------------------------------------------
1264 // ----------------------------------------------------------------------------
1271 // Construct a mask from a bitmap and a colour indicating
1272 // the transparent area
1273 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1276 Create(bitmap
, colour
);
1279 // Construct a mask from a bitmap and a palette index indicating
1280 // the transparent area
1281 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1284 Create(bitmap
, paletteIndex
);
1287 // Construct a mask from a mono bitmap (copies the bitmap).
1288 wxMask::wxMask(const wxBitmap
& bitmap
)
1297 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1300 // Create a mask from a mono bitmap (copies the bitmap).
1301 bool wxMask::Create(const wxBitmap
& bitmap
)
1303 #ifndef __WXMICROWIN__
1304 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1305 _T("can't create mask from invalid or not monochrome bitmap") );
1309 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1313 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1318 HDC srcDC
= CreateCompatibleDC(0);
1319 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1320 HDC destDC
= CreateCompatibleDC(0);
1321 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1322 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1323 SelectObject(srcDC
, 0);
1325 SelectObject(destDC
, 0);
1333 // Create a mask from a bitmap and a palette index indicating
1334 // the transparent area
1335 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1339 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1344 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1346 unsigned char red
, green
, blue
;
1347 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1349 wxColour
transparentColour(red
, green
, blue
);
1350 return Create(bitmap
, transparentColour
);
1353 #endif // wxUSE_PALETTE
1358 // Create a mask from a bitmap and a colour indicating
1359 // the transparent area
1360 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1362 #ifndef __WXMICROWIN__
1363 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1367 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1371 int width
= bitmap
.GetWidth(),
1372 height
= bitmap
.GetHeight();
1374 // scan the bitmap for the transparent colour and set the corresponding
1375 // pixels in the mask to BLACK and the rest to WHITE
1376 COLORREF maskColour
= wxColourToPalRGB(colour
);
1377 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1379 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1380 HDC destDC
= ::CreateCompatibleDC(NULL
);
1381 if ( !srcDC
|| !destDC
)
1383 wxLogLastError(wxT("CreateCompatibleDC"));
1388 // SelectObject() will fail
1389 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1390 _T("bitmap can't be selected in another DC") );
1392 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1395 wxLogLastError(wxT("SelectObject"));
1400 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1403 wxLogLastError(wxT("SelectObject"));
1410 // this will create a monochrome bitmap with 0 points for the pixels
1411 // which have the same value as the background colour and 1 for the
1413 ::SetBkColor(srcDC
, maskColour
);
1414 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1417 ::SelectObject(srcDC
, hbmpSrcOld
);
1419 ::SelectObject(destDC
, hbmpDstOld
);
1423 #else // __WXMICROWIN__
1425 #endif // __WXMICROWIN__/!__WXMICROWIN__
1428 // ----------------------------------------------------------------------------
1430 // ----------------------------------------------------------------------------
1432 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1435 int width
, int height
, int depth
)
1437 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1439 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1442 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1443 const wxString
& name
,
1445 int width
, int height
)
1447 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1449 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1452 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1453 const wxString
& name
,
1456 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1458 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1461 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1462 void *WXUNUSED(data
),
1463 long WXUNUSED(type
),
1464 int WXUNUSED(width
),
1465 int WXUNUSED(height
),
1466 int WXUNUSED(depth
))
1471 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1472 const wxString
& WXUNUSED(name
),
1473 long WXUNUSED(type
),
1474 int WXUNUSED(desiredWidth
),
1475 int WXUNUSED(desiredHeight
))
1480 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1481 const wxString
& WXUNUSED(name
),
1483 const wxPalette
*WXUNUSED(palette
))
1488 // ----------------------------------------------------------------------------
1490 // ----------------------------------------------------------------------------
1492 #ifndef __WXMICROWIN__
1493 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1494 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1496 unsigned long i
, headerSize
;
1498 // Allocate space for a DIB header
1499 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1500 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1501 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1503 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1505 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1507 // Fill in the static parts of the DIB header
1508 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1509 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1510 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1511 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1513 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1514 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1515 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1516 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1517 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1520 // Initialize the DIB palette
1521 for (i
= 0; i
< 256; i
++) {
1522 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1523 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1524 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1525 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1528 *lpDIBHeader
= lpDIBheader
;
1533 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1539 // ----------------------------------------------------------------------------
1540 // global helper functions implemented here
1541 // ----------------------------------------------------------------------------
1543 // helper of wxBitmapToHICON/HCURSOR
1545 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1552 // we can't create an icon/cursor form nothing
1556 wxMask
*mask
= bmp
.GetMask();
1559 // we must have a mask for an icon, so even if it's probably incorrect,
1560 // do create it (grey is the "standard" transparent colour)
1561 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1565 wxZeroMemory(iconInfo
);
1566 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1569 iconInfo
.xHotspot
= hotSpotX
;
1570 iconInfo
.yHotspot
= hotSpotY
;
1573 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1574 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1576 // black out the transparent area to preserve background colour, because
1577 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1578 // the mask to the dest rect.
1580 MemoryHDC dcSrc
, dcDst
;
1581 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1582 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1584 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1585 dcSrc
, 0, 0, SRCAND
) )
1587 wxLogLastError(_T("BitBlt"));
1591 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1593 if ( !bmp
.GetMask() )
1595 // we created the mask, now delete it
1599 // delete the inverted mask bitmap we created as well
1600 ::DeleteObject(iconInfo
.hbmMask
);
1605 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1607 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1610 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1612 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1615 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1617 #ifndef __WXMICROWIN__
1618 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1620 // get width/height from the bitmap if not given
1624 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1629 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1630 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1631 if ( !hdcSrc
|| !hdcDst
)
1633 wxLogLastError(wxT("CreateCompatibleDC"));
1636 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1639 wxLogLastError(wxT("CreateBitmap"));
1642 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1643 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1644 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1648 wxLogLastError(wxT("BitBlt"));
1652 SelectObject(hdcSrc
,srcTmp
);
1653 SelectObject(hdcDst
,dstTmp
);