1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/palette.h"
34 #include "wx/dcmemory.h"
35 #include "wx/bitmap.h"
39 #include "wx/msw/private.h"
43 #include "wx/msw/dib.h"
47 #include "wx/xpmdecod.h"
49 #ifdef wxHAVE_RAW_BITMAP
50 #include "wx/rawbmp.h"
53 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
66 wxBitmapRefData(const wxBitmapRefData
& data
);
67 virtual ~wxBitmapRefData() { Free(); }
71 // set the mask object to use as the mask, we take ownership of it
72 void SetMask(wxMask
*mask
)
78 // set the HBITMAP to use as the mask
79 void SetMask(HBITMAP hbmpMask
)
81 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
85 wxMask
*GetMask() const { return m_bitmapMask
; }
89 wxPalette m_bitmapPalette
;
90 #endif // wxUSE_PALETTE
96 // this field is solely for error checking: we detect selecting a bitmap
97 // into more than one DC at once or deleting a bitmap still selected into a
98 // DC (both are serious programming errors under Windows)
100 #endif // __WXDEBUG__
103 // when GetRawData() is called for a DDB we need to convert it to a DIB
104 // first to be able to provide direct access to it and we cache that DIB
105 // here and convert it back to DDB when UngetRawData() is called
109 // true if we have alpha transparency info and can be drawn using
113 // true if our HBITMAP is a DIB section, false if it is a DDB
117 // optional mask for transparent drawing
118 wxMask
*m_bitmapMask
;
122 wxBitmapRefData
& operator=(const wxBitmapRefData
&);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
130 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
132 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 // decide whether we should create a DIB or a DDB for the given parameters
144 // NB: we always use DIBs under Windows CE as this is much simpler (even if
145 // also less efficient...) and we obviously can't use them if there is no
146 // DIB support compiled in at all
148 static inline bool wxShouldCreateDIB(int, int, int, WXHDC
) { return true; }
150 #define ALWAYS_USE_DIB
152 // no sense in defining wxShouldCreateDIB() as we can't compile code
153 // executed if it is true, so we have to use #if's anyhow
154 #define NEVER_USE_DIB
155 #else // wxUSE_WXDIB && !__WXWINCE__
156 static inline bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
158 // here is the logic:
160 // (a) if hdc is specified, the caller explicitly wants DDB
161 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
162 // or less DIBs anyhow)
163 // (c) finally, create DIBs under Win9x even if the depth hasn't been
164 // explicitly specified but the current display depth is 24 or
165 // more and the image is "big", i.e. > 16Mb which is the
166 // theoretical limit for DDBs under Win9x
168 // consequences (all of which seem to make sense):
170 // (i) by default, DDBs are created (depth == -1 usually)
171 // (ii) DIBs can be created by explicitly specifying the depth
172 // (iii) using a DC always forces creating a DDB
176 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
179 #define SOMETIMES_USE_DIB
180 #endif // different DIB usage scenarious
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 wxBitmapRefData::wxBitmapRefData()
189 m_selectedInto
= NULL
;
193 m_hBitmap
= (WXHBITMAP
) NULL
;
202 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
203 : wxGDIImageRefData(data
)
206 m_selectedInto
= NULL
;
209 // can't copy the mask as the other bitmap destroys it
212 wxASSERT_MSG( !data
.m_isDIB
,
213 _T("can't copy bitmap locked for raw access!") );
216 m_hasAlpha
= data
.m_hasAlpha
;
219 void wxBitmapRefData::Free()
221 wxASSERT_MSG( !m_selectedInto
,
222 wxT("deleting bitmap still selected into wxMemoryDC") );
225 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
230 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
232 wxLogLastError(wxT("DeleteObject(hbitmap)"));
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 // this function should be called from all wxBitmap ctors
245 void wxBitmap::Init()
247 // m_refData = NULL; done in the base class ctor
250 wxGDIImageRefData
*wxBitmap::CreateData() const
252 return new wxBitmapRefData
;
255 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*dataOrig
) const
257 const wxBitmapRefData
*
258 data
= wx_static_cast(const wxBitmapRefData
*, dataOrig
);
262 wxBitmap
*self
= wx_const_cast(wxBitmap
*, this);
265 // copy the other bitmap
266 if ( data
->m_hBitmap
)
268 wxDIB
dib((HBITMAP
)(data
->m_hBitmap
));
269 self
->CopyFromDIB(dib
);
272 #endif // wxUSE_WXDIB
274 // don't copy the bitmap data, but do copy the size, depth, ...
275 self
->m_refData
= new wxBitmapRefData(*data
);
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
;
310 // If the icon is 32 bits per pixel then it may have alpha channel data,
311 // although there are some icons that are 32 bpp but have no alpha... So
312 // convert to a DIB and manually check the 4th byte for each pixel.
314 if ( ::GetObject(iconInfo
.hbmColor
, sizeof(BITMAP
), (LPVOID
)&bm
)
315 && bm
.bmBitsPixel
== 32)
317 wxDIB
dib(iconInfo
.hbmColor
);
320 unsigned char* pixels
= dib
.GetData();
321 for (int idx
=0; idx
<w
*h
*4; idx
+=4)
323 if (pixels
[idx
+3] != 0)
325 // If there is an alpha byte that is non-zero then set the
326 // alpha flag and bail out of the loop.
327 refData
->m_hasAlpha
= true;
334 if ( !refData
->m_hasAlpha
)
336 // the mask returned by GetIconInfo() is inverted compared to the usual
338 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
341 // delete the old one now as we don't need it any more
342 ::DeleteObject(iconInfo
.hbmMask
);
353 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
360 return CopyFromIconOrCursor(cursor
);
363 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
370 return CopyFromIconOrCursor(icon
);
373 #ifndef NEVER_USE_DIB
375 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
377 wxCHECK_MSG( dib
.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
379 #ifdef SOMETIMES_USE_DIB
380 HBITMAP hbitmap
= dib
.CreateDDB();
383 #else // ALWAYS_USE_DIB
384 HBITMAP hbitmap
= ((wxDIB
&)dib
).Detach(); // const_cast
385 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
389 wxBitmapRefData
*refData
= new wxBitmapRefData
;
392 refData
->m_width
= dib
.GetWidth();
393 refData
->m_height
= dib
.GetHeight();
394 refData
->m_depth
= dib
.GetDepth();
396 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
399 wxPalette
*palette
= dib
.CreatePalette();
402 refData
->m_bitmapPalette
= *palette
;
406 #endif // wxUSE_PALETTE
411 #endif // NEVER_USE_DIB
413 wxBitmap::~wxBitmap()
417 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
421 #ifndef __WXMICROWIN__
422 wxBitmapRefData
*refData
= new wxBitmapRefData
;
425 refData
->m_width
= width
;
426 refData
->m_height
= height
;
427 refData
->m_depth
= depth
;
432 // we assume that it is in XBM format which is not quite the same as
433 // the format CreateBitmap() wants because the order of bytes in the
435 const size_t bytesPerLine
= (width
+ 7) / 8;
436 const size_t padding
= bytesPerLine
% 2;
437 const size_t len
= height
* ( padding
+ bytesPerLine
);
438 data
= (char *)malloc(len
);
439 const char *src
= bits
;
442 for ( int rows
= 0; rows
< height
; rows
++ )
444 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
446 unsigned char val
= *src
++;
447 unsigned char reversed
= 0;
449 for ( int bits
= 0; bits
< 8; bits
++)
452 reversed
|= (unsigned char)(val
& 0x01);
464 // bits should already be in Windows standard format
465 data
= (char *)bits
; // const_cast is harmless
468 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
471 wxLogLastError(wxT("CreateBitmap"));
479 SetHBITMAP((WXHBITMAP
)hbmp
);
483 // Create from XPM data
484 bool wxBitmap::CreateFromXpm(const char **data
)
486 #if wxUSE_IMAGE && wxUSE_XPM
489 wxCHECK_MSG( data
!= NULL
, false, wxT("invalid bitmap data") )
491 wxXPMDecoder decoder
;
492 wxImage img
= decoder
.ReadData(data
);
493 wxCHECK_MSG( img
.Ok(), false, wxT("invalid bitmap data") )
495 *this = wxBitmap(img
);
503 wxBitmap::wxBitmap(int w
, int h
, int d
)
507 (void)Create(w
, h
, d
);
510 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
514 (void)Create(w
, h
, dc
);
517 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
521 (void)Create(data
, type
, width
, height
, depth
);
524 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
528 LoadFile(filename
, (int)type
);
531 bool wxBitmap::Create(int width
, int height
, int depth
)
533 return DoCreate(width
, height
, depth
, 0);
536 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
538 wxCHECK_MSG( dc
.Ok(), false, _T("invalid HDC in wxBitmap::Create()") );
540 return DoCreate(width
, height
, -1, dc
.GetHDC());
543 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
547 m_refData
= new wxBitmapRefData
;
549 GetBitmapData()->m_width
= w
;
550 GetBitmapData()->m_height
= h
;
552 HBITMAP hbmp
wxDUMMY_INITIALIZE(0);
554 #ifndef NEVER_USE_DIB
555 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
559 // create DIBs without alpha channel by default
567 // don't delete the DIB section in dib object dtor
570 GetBitmapData()->m_isDIB
= true;
571 GetBitmapData()->m_depth
= d
;
574 #endif // NEVER_USE_DIB
576 #ifndef ALWAYS_USE_DIB
577 #ifndef __WXMICROWIN__
580 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
583 wxLogLastError(wxT("CreateBitmap"));
586 GetBitmapData()->m_depth
= d
;
588 else // d == 0, create bitmap compatible with the screen
589 #endif // !__WXMICROWIN__
592 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
595 wxLogLastError(wxT("CreateCompatibleBitmap"));
598 GetBitmapData()->m_depth
= wxDisplayDepth();
600 #endif // !ALWAYS_USE_DIB
603 SetHBITMAP((WXHBITMAP
)hbmp
);
610 // ----------------------------------------------------------------------------
611 // wxImage to/from conversions for Microwin
612 // ----------------------------------------------------------------------------
614 // Microwin versions are so different from normal ones that it really doesn't
615 // make sense to use #ifdefs inside the function bodies
616 #ifdef __WXMICROWIN__
618 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
620 // Set this to 1 to experiment with mask code,
621 // which currently doesn't work
624 m_refData
= new wxBitmapRefData();
626 // Initial attempt at a simple-minded implementation.
627 // The bitmap will always be created at the screen depth,
628 // so the 'depth' argument is ignored.
630 HDC hScreenDC
= ::GetDC(NULL
);
631 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
633 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
634 HBITMAP hMaskBitmap
= NULL
;
635 HBITMAP hOldMaskBitmap
= NULL
;
637 unsigned char maskR
= 0;
638 unsigned char maskG
= 0;
639 unsigned char maskB
= 0;
641 // printf("Created bitmap %d\n", (int) hBitmap);
644 ::ReleaseDC(NULL
, hScreenDC
);
647 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
649 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
650 ::ReleaseDC(NULL
, hScreenDC
);
652 // created an mono-bitmap for the possible mask
653 bool hasMask
= image
.HasMask();
658 // FIXME: we should be able to pass bpp = 1, but
659 // GdBlit can't handle a different depth
661 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
663 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
665 maskR
= image
.GetMaskRed();
666 maskG
= image
.GetMaskGreen();
667 maskB
= image
.GetMaskBlue();
675 hScreenDC
= ::GetDC(NULL
);
676 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
677 ::ReleaseDC(NULL
, hScreenDC
);
679 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
687 for (i
= 0; i
< image
.GetWidth(); i
++)
689 for (j
= 0; j
< image
.GetHeight(); j
++)
691 unsigned char red
= image
.GetRed(i
, j
);
692 unsigned char green
= image
.GetGreen(i
, j
);
693 unsigned char blue
= image
.GetBlue(i
, j
);
695 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
699 // scan the bitmap for the transparent colour and set the corresponding
700 // pixels in the mask to BLACK and the rest to WHITE
701 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
702 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
704 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
709 ::SelectObject(hMemDC
, hOldBitmap
);
713 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
716 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
719 SetWidth(image
.GetWidth());
720 SetHeight(image
.GetHeight());
721 SetDepth(screenDepth
);
722 SetHBITMAP( (WXHBITMAP
) hBitmap
);
725 // Copy the palette from the source image
726 SetPalette(image
.GetPalette());
727 #endif // wxUSE_PALETTE
732 wxImage
wxBitmap::ConvertToImage() const
734 // Initial attempt at a simple-minded implementation.
735 // The bitmap will always be created at the screen depth,
736 // so the 'depth' argument is ignored.
737 // TODO: transparency (create a mask image)
741 wxFAIL_MSG( wxT("bitmap is invalid") );
747 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
749 // create an wxImage object
750 int width
= GetWidth();
751 int height
= GetHeight();
752 image
.Create( width
, height
);
753 unsigned char *data
= image
.GetData();
756 wxFAIL_MSG( wxT("could not allocate data for image") );
760 HDC hScreenDC
= ::GetDC(NULL
);
762 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
763 ::ReleaseDC(NULL
, hScreenDC
);
765 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
767 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
770 for (i
= 0; i
< GetWidth(); i
++)
772 for (j
= 0; j
< GetHeight(); j
++)
774 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
775 unsigned char red
= GetRValue(color
);
776 unsigned char green
= GetGValue(color
);
777 unsigned char blue
= GetBValue(color
);
779 image
.SetRGB(i
, j
, red
, green
, blue
);
783 ::SelectObject(hMemDC
, hOldBitmap
);
787 // Copy the palette from the source image
789 image
.SetPalette(* GetPalette());
790 #endif // wxUSE_PALETTE
795 #endif // __WXMICROWIN__
797 // ----------------------------------------------------------------------------
798 // wxImage to/from conversions
799 // ----------------------------------------------------------------------------
803 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
805 return CreateFromImage(image
, depth
, 0);
808 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
810 wxCHECK_MSG( dc
.Ok(), false,
811 _T("invalid HDC in wxBitmap::CreateFromImage()") );
813 return CreateFromImage(image
, -1, dc
.GetHDC());
816 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
818 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
822 // first convert the image to DIB
823 const int h
= image
.GetHeight();
824 const int w
= image
.GetWidth();
830 depth
= dib
.GetDepth(); // Get depth from image if none specified
832 // store the bitmap parameters
833 wxBitmapRefData
*refData
= new wxBitmapRefData
;
834 refData
->m_width
= w
;
835 refData
->m_height
= h
;
836 refData
->m_hasAlpha
= image
.HasAlpha();
841 // next either store DIB as is or create a DDB from it
842 HBITMAP hbitmap
wxDUMMY_INITIALIZE(0);
844 // are we going to use DIB?
846 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
847 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
849 // don't delete the DIB section in dib object dtor
850 hbitmap
= dib
.Detach();
852 refData
->m_isDIB
= true;
853 refData
->m_depth
= depth
;
855 #ifndef ALWAYS_USE_DIB
856 else // we need to convert DIB to DDB
858 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
860 refData
->m_depth
= depth
;
862 #endif // !ALWAYS_USE_DIB
864 // validate this object
865 SetHBITMAP((WXHBITMAP
)hbitmap
);
867 // finally also set the mask if we have one
868 if ( image
.HasMask() )
870 const size_t len
= 2*((w
+15)/16);
871 BYTE
*src
= image
.GetData();
872 BYTE
*data
= new BYTE
[h
*len
];
873 memset(data
, 0, h
*len
);
874 BYTE r
= image
.GetMaskRed(),
875 g
= image
.GetMaskGreen(),
876 b
= image
.GetMaskBlue();
878 for ( int y
= 0; y
< h
; y
++, dst
+= len
)
882 for ( int x
= 0; x
< w
; x
++, src
+= 3 )
884 if (src
[0] != r
|| src
[1] != g
|| src
[2] != b
)
887 if ( (mask
>>= 1) == 0 )
895 hbitmap
= ::CreateBitmap(w
, h
, 1, 1, data
);
898 wxLogLastError(_T("CreateBitmap(mask)"));
902 SetMask(new wxMask((WXHBITMAP
)hbitmap
));
911 wxImage
wxBitmap::ConvertToImage() const
913 // convert DDB to DIB
921 // and then DIB to our wxImage
922 wxImage image
= dib
.ConvertToImage();
928 // now do the same for the mask, if we have any
929 HBITMAP hbmpMask
= GetMask() ? (HBITMAP
) GetMask()->GetMaskBitmap() : NULL
;
932 wxDIB
dibMask(hbmpMask
);
933 if ( dibMask
.IsOk() )
935 // TODO: use wxRawBitmap to iterate over DIB
937 // we hard code the mask colour for now but we could also make an
938 // effort (and waste time) to choose a colour not present in the
939 // image already to avoid having to fudge the pixels below --
940 // whether it's worth to do it is unclear however
941 static const int MASK_RED
= 1;
942 static const int MASK_GREEN
= 2;
943 static const int MASK_BLUE
= 3;
944 static const int MASK_BLUE_REPLACEMENT
= 2;
946 const int h
= dibMask
.GetHeight();
947 const int w
= dibMask
.GetWidth();
948 const int bpp
= dibMask
.GetDepth();
949 const int maskBytesPerPixel
= bpp
>> 3;
950 const int maskBytesPerLine
= wxDIB::GetLineSize(w
, bpp
);
951 unsigned char *data
= image
.GetData();
953 // remember that DIBs are stored in bottom to top order
955 maskLineStart
= dibMask
.GetData() + ((h
- 1) * maskBytesPerLine
);
957 for ( int y
= 0; y
< h
; y
++, maskLineStart
-= maskBytesPerLine
)
959 // traverse one mask DIB line
960 unsigned char *mask
= maskLineStart
;
961 for ( int x
= 0; x
< w
; x
++, mask
+= maskBytesPerPixel
)
963 // should this pixel be transparent?
966 // no, check that it isn't transparent by accident
967 if ( (data
[0] == MASK_RED
) &&
968 (data
[1] == MASK_GREEN
) &&
969 (data
[2] == MASK_BLUE
) )
971 // we have to fudge the colour a bit to prevent
972 // this pixel from appearing transparent
973 data
[2] = MASK_BLUE_REPLACEMENT
;
978 else // yes, transparent pixel
981 *data
++ = MASK_GREEN
;
987 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
994 #endif // wxUSE_WXDIB
996 #endif // wxUSE_IMAGE
998 // ----------------------------------------------------------------------------
999 // loading and saving bitmaps
1000 // ----------------------------------------------------------------------------
1002 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1006 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1010 m_refData
= new wxBitmapRefData
;
1012 return handler
->LoadFile(this, filename
, type
, -1, -1);
1015 else // no bitmap handler found
1018 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1020 *this = wxBitmap(image
);
1025 #endif // wxUSE_IMAGE
1030 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1034 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1038 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1043 m_refData
= new wxBitmapRefData
;
1045 return handler
->Create(this, data
, type
, width
, height
, depth
);
1048 bool wxBitmap::SaveFile(const wxString
& filename
,
1050 const wxPalette
*palette
)
1052 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1056 return handler
->SaveFile(this, filename
, type
, palette
);
1059 else // no bitmap handler found
1061 // FIXME what about palette? shouldn't we use it?
1062 wxImage image
= ConvertToImage();
1065 return image
.SaveFile(filename
, type
);
1068 #endif // wxUSE_IMAGE
1073 // ----------------------------------------------------------------------------
1074 // sub bitmap extraction
1075 // ----------------------------------------------------------------------------
1077 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1079 wxCHECK_MSG( Ok() &&
1080 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1081 (rect
.x
+rect
.width
<= GetWidth()) &&
1082 (rect
.y
+rect
.height
<= GetHeight()),
1083 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1085 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1086 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1088 #ifndef __WXMICROWIN__
1089 // handle alpha channel, if any
1098 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1099 selectDst(dcDst
, GetHbitmapOf(ret
));
1101 if ( !selectSrc
|| !selectDst
)
1103 wxLogLastError(_T("SelectObjct(hBitmap)"));
1106 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1107 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1109 wxLogLastError(_T("BitBlt"));
1113 // copy mask if there is one
1116 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1118 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1119 selectDst(dcDst
, hbmpMask
);
1121 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1122 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1124 wxLogLastError(_T("BitBlt"));
1127 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1130 #endif // !__WXMICROWIN__
1135 // ----------------------------------------------------------------------------
1136 // wxBitmap accessors
1137 // ----------------------------------------------------------------------------
1140 wxPalette
* wxBitmap::GetPalette() const
1142 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1143 : (wxPalette
*) NULL
;
1147 wxMask
*wxBitmap::GetMask() const
1149 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1152 wxBitmap
wxBitmap::GetMaskBitmap() const
1155 wxMask
*mask
= GetMask();
1157 bmp
.SetHBITMAP(mask
->GetMaskBitmap());
1163 wxDC
*wxBitmap::GetSelectedInto() const
1165 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1170 #if WXWIN_COMPATIBILITY_2_4
1172 int wxBitmap::GetQuality() const
1177 #endif // WXWIN_COMPATIBILITY_2_4
1179 void wxBitmap::UseAlpha()
1181 if ( GetBitmapData() )
1182 GetBitmapData()->m_hasAlpha
= true;
1185 bool wxBitmap::HasAlpha() const
1187 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1190 // ----------------------------------------------------------------------------
1192 // ----------------------------------------------------------------------------
1196 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1198 if ( GetBitmapData() )
1199 GetBitmapData()->m_selectedInto
= dc
;
1206 void wxBitmap::SetPalette(const wxPalette
& palette
)
1210 GetBitmapData()->m_bitmapPalette
= palette
;
1213 #endif // wxUSE_PALETTE
1215 void wxBitmap::SetMask(wxMask
*mask
)
1219 GetBitmapData()->SetMask(mask
);
1222 #if WXWIN_COMPATIBILITY_2_4
1224 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1228 #endif // WXWIN_COMPATIBILITY_2_4
1230 // ----------------------------------------------------------------------------
1231 // raw bitmap access support
1232 // ----------------------------------------------------------------------------
1234 #ifdef wxHAVE_RAW_BITMAP
1235 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1240 // no bitmap, no data (raw or otherwise)
1244 // if we're already a DIB we can access our data directly, but if not we
1245 // need to convert this DDB to a DIB section and use it for raw access and
1246 // then convert it back
1248 if ( !GetBitmapData()->m_isDIB
)
1250 wxCHECK_MSG( !GetBitmapData()->m_dib
, NULL
,
1251 _T("GetRawData() may be called only once") );
1253 wxDIB
*dib
= new wxDIB(*this);
1261 // we'll free it in UngetRawData()
1262 GetBitmapData()->m_dib
= dib
;
1264 hDIB
= dib
->GetHandle();
1268 hDIB
= GetHbitmap();
1272 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1274 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1279 // check that the bitmap is in correct format
1280 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1282 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1287 // ok, store the relevant info in wxPixelDataBase
1288 const LONG h
= ds
.dsBm
.bmHeight
;
1290 data
.m_width
= ds
.dsBm
.bmWidth
;
1293 // remember that DIBs are stored in top to bottom order!
1294 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1295 // multiple of 2, as required by the documentation. So we use the official
1296 // formula, which we already use elsewhere.)
1297 const LONG bytesPerRow
=
1298 wxDIB::GetLineSize(ds
.dsBm
.bmWidth
, ds
.dsBm
.bmBitsPixel
);
1299 data
.m_stride
= -bytesPerRow
;
1301 char *bits
= (char *)ds
.dsBm
.bmBits
;
1304 bits
+= (h
- 1)*bytesPerRow
;
1313 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1321 // invalid data, don't crash -- but don't assert neither as we're
1322 // called automatically from wxPixelDataBase dtor and so there is no
1323 // way to prevent this from happening
1327 // if we're a DDB we need to convert DIB back to DDB now to make the
1328 // changes made via raw bitmap access effective
1329 if ( !GetBitmapData()->m_isDIB
)
1331 wxDIB
*dib
= GetBitmapData()->m_dib
;
1332 GetBitmapData()->m_dib
= NULL
;
1338 #endif // wxUSE_WXDIB
1340 #endif // #ifdef wxHAVE_RAW_BITMAP
1342 // ----------------------------------------------------------------------------
1344 // ----------------------------------------------------------------------------
1351 // Construct a mask from a bitmap and a colour indicating
1352 // the transparent area
1353 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1356 Create(bitmap
, colour
);
1359 // Construct a mask from a bitmap and a palette index indicating
1360 // the transparent area
1361 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1364 Create(bitmap
, paletteIndex
);
1367 // Construct a mask from a mono bitmap (copies the bitmap).
1368 wxMask::wxMask(const wxBitmap
& bitmap
)
1377 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1380 // Create a mask from a mono bitmap (copies the bitmap).
1381 bool wxMask::Create(const wxBitmap
& bitmap
)
1383 #ifndef __WXMICROWIN__
1384 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, false,
1385 _T("can't create mask from invalid or not monochrome bitmap") );
1389 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1393 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1398 HDC srcDC
= CreateCompatibleDC(0);
1399 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1400 HDC destDC
= CreateCompatibleDC(0);
1401 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1402 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1403 SelectObject(srcDC
, 0);
1405 SelectObject(destDC
, 0);
1409 wxUnusedVar(bitmap
);
1414 // Create a mask from a bitmap and a palette index indicating
1415 // the transparent area
1416 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1420 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1425 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1427 unsigned char red
, green
, blue
;
1428 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1430 wxColour
transparentColour(red
, green
, blue
);
1431 return Create(bitmap
, transparentColour
);
1434 #endif // wxUSE_PALETTE
1439 // Create a mask from a bitmap and a colour indicating
1440 // the transparent area
1441 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1443 #ifndef __WXMICROWIN__
1444 wxCHECK_MSG( bitmap
.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1448 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1452 int width
= bitmap
.GetWidth(),
1453 height
= bitmap
.GetHeight();
1455 // scan the bitmap for the transparent colour and set the corresponding
1456 // pixels in the mask to BLACK and the rest to WHITE
1457 COLORREF maskColour
= wxColourToPalRGB(colour
);
1458 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1460 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1461 HDC destDC
= ::CreateCompatibleDC(NULL
);
1462 if ( !srcDC
|| !destDC
)
1464 wxLogLastError(wxT("CreateCompatibleDC"));
1469 // SelectObject() will fail
1470 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1471 _T("bitmap can't be selected in another DC") );
1473 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1476 wxLogLastError(wxT("SelectObject"));
1481 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1484 wxLogLastError(wxT("SelectObject"));
1491 // this will create a monochrome bitmap with 0 points for the pixels
1492 // which have the same value as the background colour and 1 for the
1494 ::SetBkColor(srcDC
, maskColour
);
1495 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1498 ::SelectObject(srcDC
, hbmpSrcOld
);
1500 ::SelectObject(destDC
, hbmpDstOld
);
1504 #else // __WXMICROWIN__
1505 wxUnusedVar(bitmap
);
1506 wxUnusedVar(colour
);
1508 #endif // __WXMICROWIN__/!__WXMICROWIN__
1511 // ----------------------------------------------------------------------------
1513 // ----------------------------------------------------------------------------
1515 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1518 int width
, int height
, int depth
)
1520 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1522 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : false;
1525 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1526 const wxString
& name
,
1528 int width
, int height
)
1530 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1532 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : false;
1535 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1536 const wxString
& name
,
1539 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1541 return bitmap
? SaveFile(bitmap
, name
, type
) : false;
1544 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1545 void *WXUNUSED(data
),
1546 long WXUNUSED(type
),
1547 int WXUNUSED(width
),
1548 int WXUNUSED(height
),
1549 int WXUNUSED(depth
))
1554 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1555 const wxString
& WXUNUSED(name
),
1556 long WXUNUSED(type
),
1557 int WXUNUSED(desiredWidth
),
1558 int WXUNUSED(desiredHeight
))
1563 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1564 const wxString
& WXUNUSED(name
),
1566 const wxPalette
*WXUNUSED(palette
))
1571 // ----------------------------------------------------------------------------
1573 // ----------------------------------------------------------------------------
1575 #ifndef __WXMICROWIN__
1576 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1577 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1579 unsigned long i
, headerSize
;
1581 // Allocate space for a DIB header
1582 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1583 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1584 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1586 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1588 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1590 // Fill in the static parts of the DIB header
1591 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1592 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1593 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1594 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1596 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1597 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1598 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1599 lpDIBheader
->bmiHeader
.biSizeImage
= (xSize
* abs(ySize
) * bitsPerPixel
) >> 3;
1600 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1603 // Initialize the DIB palette
1604 for (i
= 0; i
< 256; i
++) {
1605 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1606 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1607 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1608 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1611 *lpDIBHeader
= lpDIBheader
;
1616 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1622 // ----------------------------------------------------------------------------
1623 // global helper functions implemented here
1624 // ----------------------------------------------------------------------------
1626 // helper of wxBitmapToHICON/HCURSOR
1628 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1635 // we can't create an icon/cursor form nothing
1639 if ( bmp
.HasAlpha() )
1641 // Create an empty mask bitmap.
1642 // it doesn't seem to work if we mess with the mask at all.
1643 HBITMAP hMonoBitmap
= CreateBitmap(bmp
.GetWidth(),bmp
.GetHeight(),1,1,NULL
);
1646 wxZeroMemory(iconInfo
);
1647 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1650 iconInfo
.xHotspot
= hotSpotX
;
1651 iconInfo
.yHotspot
= hotSpotY
;
1654 iconInfo
.hbmMask
= hMonoBitmap
;
1655 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1657 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1659 ::DeleteObject(hMonoBitmap
);
1664 wxMask
* mask
= bmp
.GetMask();
1668 // we must have a mask for an icon, so even if it's probably incorrect,
1669 // do create it (grey is the "standard" transparent colour)
1670 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1674 wxZeroMemory(iconInfo
);
1675 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1678 iconInfo
.xHotspot
= hotSpotX
;
1679 iconInfo
.yHotspot
= hotSpotY
;
1682 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1683 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1685 // black out the transparent area to preserve background colour, because
1686 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1687 // the mask to the dest rect.
1689 MemoryHDC dcSrc
, dcDst
;
1690 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1691 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1693 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1694 dcSrc
, 0, 0, SRCAND
) )
1696 wxLogLastError(_T("BitBlt"));
1700 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1702 if ( !bmp
.GetMask() && !bmp
.HasAlpha() )
1704 // we created the mask, now delete it
1708 // delete the inverted mask bitmap we created as well
1709 ::DeleteObject(iconInfo
.hbmMask
);
1714 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1716 return wxBitmapToIconOrCursor(bmp
, true, 0, 0);
1719 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1721 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, false, hotSpotX
, hotSpotY
);
1724 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1726 #ifndef __WXMICROWIN__
1727 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1729 // get width/height from the bitmap if not given
1733 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1738 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1739 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1740 if ( !hdcSrc
|| !hdcDst
)
1742 wxLogLastError(wxT("CreateCompatibleDC"));
1745 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1748 wxLogLastError(wxT("CreateBitmap"));
1751 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1752 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1753 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1757 wxLogLastError(wxT("BitBlt"));
1761 SelectObject(hdcSrc
,srcTmp
);
1762 SelectObject(hdcDst
,dstTmp
);