]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
46 #include "wx/msw/dib.h"
49 // missing from mingw32 header
51 #define CLR_INVALID ((COLORREF)-1)
52 #endif // no CLR_INVALID
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
59 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
61 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 wxBitmapRefData::wxBitmapRefData()
74 m_selectedInto
= NULL
;
77 m_hBitmap
= (WXHBITMAP
) NULL
;
80 void wxBitmapRefData::Free()
82 wxASSERT_MSG( !m_selectedInto
,
83 wxT("deleting bitmap still selected into wxMemoryDC") );
87 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
89 wxLogLastError("DeleteObject(hbitmap)");
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // this function should be called from all wxBitmap ctors
102 void wxBitmap::Init()
104 // m_refData = NULL; done in the base class ctor
106 if ( wxTheBitmapList
)
107 wxTheBitmapList
->AddBitmap(this);
112 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
114 // it may be either HICON or HCURSOR
115 HICON hicon
= (HICON
)icon
.GetHandle();
118 if ( !::GetIconInfo(hicon
, &iconInfo
) )
120 wxLogLastError("GetIconInfo");
125 wxBitmapRefData
*refData
= new wxBitmapRefData
;
128 int w
= icon
.GetWidth(),
129 h
= icon
.GetHeight();
131 refData
->m_width
= w
;
132 refData
->m_height
= h
;
133 refData
->m_depth
= wxDisplayDepth();
135 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
137 // the mask returned by GetIconInfo() is inversed compared to the usual
139 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
140 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
142 #if WXWIN_COMPATIBILITY_2
143 refData
->m_ok
= TRUE
;
144 #endif // WXWIN_COMPATIBILITY_2
151 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
159 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
163 return CopyFromIconOrCursor(cursor
);
167 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
174 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
175 // to create a bitmap from icon there - but using this way we won't have
178 int width
= icon
.GetWidth(),
179 height
= icon
.GetHeight();
181 // copy the icon to the bitmap
183 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
184 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
185 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
187 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
189 ::SelectObject(hdc
, hbmpOld
);
192 wxBitmapRefData
*refData
= new wxBitmapRefData
;
195 refData
->m_width
= width
;
196 refData
->m_height
= height
;
197 refData
->m_depth
= wxDisplayDepth();
199 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
201 #if WXWIN_COMPATIBILITY_2
202 refData
->m_ok
= TRUE
;
203 #endif // WXWIN_COMPATIBILITY_2
207 return CopyFromIconOrCursor(icon
);
208 #endif // Win16/Win32
211 wxBitmap::~wxBitmap()
214 wxTheBitmapList
->DeleteObject(this);
217 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
221 wxBitmapRefData
*refData
= new wxBitmapRefData
;
224 refData
->m_width
= width
;
225 refData
->m_height
= height
;
226 refData
->m_depth
= depth
;
227 refData
->m_numColors
= 0;
228 refData
->m_selectedInto
= NULL
;
233 // we assume that it is in XBM format which is not quite the same as
234 // the format CreateBitmap() wants because the order of bytes in the
236 static const size_t bytesPerLine
= (width
+ 7) / 8;
237 static const size_t padding
= bytesPerLine
% 2;
238 static const size_t len
= height
* ( padding
+ bytesPerLine
);
239 data
= (char *)malloc(len
);
240 const char *src
= bits
;
243 for ( int rows
= 0; rows
< height
; rows
++ )
245 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
247 unsigned char val
= *src
++;
248 unsigned char reversed
= 0;
250 for ( int bits
= 0; bits
< 8; bits
++)
253 reversed
|= (val
& 0x01);
265 // bits should already be in Windows standard format
266 data
= (char *)bits
; // const_cast is harmless
269 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
272 wxLogLastError("CreateBitmap");
280 SetHBITMAP((WXHBITMAP
)hbmp
);
283 // Create from XPM data
284 bool wxBitmap::CreateFromXpm(const char **data
)
288 return Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
291 wxBitmap::wxBitmap(int w
, int h
, int d
)
295 (void)Create(w
, h
, d
);
298 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
302 (void)Create(data
, type
, width
, height
, depth
);
305 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
309 LoadFile(filename
, (int)type
);
312 bool wxBitmap::Create(int w
, int h
, int d
)
316 m_refData
= new wxBitmapRefData
;
318 GetBitmapData()->m_width
= w
;
319 GetBitmapData()->m_height
= h
;
320 GetBitmapData()->m_depth
= d
;
326 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
329 wxLogLastError("CreateBitmap");
335 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
338 wxLogLastError("CreateCompatibleBitmap");
341 GetBitmapData()->m_depth
= wxDisplayDepth();
344 SetHBITMAP((WXHBITMAP
)hbmp
);
346 #if WXWIN_COMPATIBILITY_2
347 GetBitmapData()->m_ok
= hbmp
!= 0;
348 #endif // WXWIN_COMPATIBILITY_2
353 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
357 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
361 m_refData
= new wxBitmapRefData
;
363 return handler
->LoadFile(this, filename
, type
, -1, -1);
368 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
371 *this = image
.ConvertToBitmap();
377 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
381 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
385 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
386 "type %d defined."), type
);
391 m_refData
= new wxBitmapRefData
;
393 return handler
->Create(this, data
, type
, width
, height
, depth
);
396 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
398 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
402 return handler
->SaveFile(this, filename
, type
, palette
);
406 // FIXME what about palette? shouldn't we use it?
407 wxImage
image( *this );
411 return image
.SaveFile( filename
, type
);
415 // ----------------------------------------------------------------------------
416 // sub bitmap extraction
417 // ----------------------------------------------------------------------------
419 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
422 (rect
.x
>= 0) && (rect
.y
>= 0) &&
423 (rect
.x
+rect
.width
<= GetWidth()) &&
424 (rect
.y
+rect
.height
<= GetHeight()),
425 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
427 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
428 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
431 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
432 HDC dcDst
= ::CreateCompatibleDC(NULL
);
433 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
434 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
435 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
437 // copy mask if there is one
440 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
442 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
443 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
444 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
446 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
450 SelectObject(dcDst
, NULL
);
451 SelectObject(dcSrc
, NULL
);
458 // ----------------------------------------------------------------------------
459 // wxBitmap accessors
460 // ----------------------------------------------------------------------------
462 void wxBitmap::SetQuality(int q
)
466 GetBitmapData()->m_quality
= q
;
469 #if WXWIN_COMPATIBILITY_2
470 void wxBitmap::SetOk(bool isOk
)
474 GetBitmapData()->m_ok
= isOk
;
476 #endif // WXWIN_COMPATIBILITY_2
478 void wxBitmap::SetPalette(const wxPalette
& palette
)
482 GetBitmapData()->m_bitmapPalette
= palette
;
485 void wxBitmap::SetMask(wxMask
*mask
)
489 GetBitmapData()->m_bitmapMask
= mask
;
492 // Creates a bitmap that matches the device context, from
493 // an arbitray bitmap. At present, the original bitmap must have an
494 // associated palette. TODO: use a default palette if no palette exists.
495 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
496 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
499 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
500 HPALETTE hPal
= (HPALETTE
) NULL
;
502 void *lpBits
= (void*) NULL
;
504 if( GetPalette() && GetPalette()->Ok() )
506 tmpBitmap
.SetPalette(*GetPalette());
507 memDC
.SelectObject(tmpBitmap
);
508 memDC
.SetPalette(*GetPalette());
509 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
513 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
515 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
516 tmpBitmap
.SetPalette( palette
);
517 memDC
.SelectObject(tmpBitmap
);
518 memDC
.SetPalette( palette
);
521 // set the height negative because in a DIB the order of the lines is
523 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
528 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
530 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
532 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
533 GetWidth(), GetHeight(),
534 0, 0, 0, GetHeight(),
535 lpBits
, lpDib
, DIB_RGB_COLORS
);
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
553 // Construct a mask from a bitmap and a colour indicating
554 // the transparent area
555 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
558 Create(bitmap
, colour
);
561 // Construct a mask from a bitmap and a palette index indicating
562 // the transparent area
563 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
566 Create(bitmap
, paletteIndex
);
569 // Construct a mask from a mono bitmap (copies the bitmap).
570 wxMask::wxMask(const wxBitmap
& bitmap
)
579 ::DeleteObject((HBITMAP
) m_maskBitmap
);
582 // Create a mask from a mono bitmap (copies the bitmap).
583 bool wxMask::Create(const wxBitmap
& bitmap
)
585 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
586 _T("can't create mask from invalid or not monochrome bitmap") );
590 ::DeleteObject((HBITMAP
) m_maskBitmap
);
594 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
599 HDC srcDC
= CreateCompatibleDC(0);
600 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
601 HDC destDC
= CreateCompatibleDC(0);
602 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
603 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
604 SelectObject(srcDC
, 0);
606 SelectObject(destDC
, 0);
611 // Create a mask from a bitmap and a palette index indicating
612 // the transparent area
613 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
617 ::DeleteObject((HBITMAP
) m_maskBitmap
);
620 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
622 unsigned char red
, green
, blue
;
623 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
625 wxColour
transparentColour(red
, green
, blue
);
626 return Create(bitmap
, transparentColour
);
632 // Create a mask from a bitmap and a colour indicating
633 // the transparent area
634 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
636 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
640 ::DeleteObject((HBITMAP
) m_maskBitmap
);
644 int width
= bitmap
.GetWidth(),
645 height
= bitmap
.GetHeight();
647 // scan the bitmap for the transparent colour and set the corresponding
648 // pixels in the mask to BLACK and the rest to WHITE
649 COLORREF maskColour
= wxColourToRGB(colour
);
650 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
652 HDC srcDC
= ::CreateCompatibleDC(NULL
);
653 HDC destDC
= ::CreateCompatibleDC(NULL
);
654 if ( !srcDC
|| !destDC
)
656 wxLogLastError("CreateCompatibleDC");
661 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
664 wxLogLastError("SelectObject");
669 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
672 wxLogLastError("SelectObject");
677 // this is not very efficient, but I can't think of a better way of doing
679 for ( int w
= 0; ok
&& (w
< width
); w
++ )
681 for ( int h
= 0; ok
&& (h
< height
); h
++ )
683 COLORREF col
= GetPixel(srcDC
, w
, h
);
684 if ( col
== CLR_INVALID
)
686 wxLogLastError("GetPixel");
688 // doesn't make sense to continue
694 if ( col
== maskColour
)
696 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
700 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
705 ::SelectObject(srcDC
, hbmpSrcOld
);
707 ::SelectObject(destDC
, hbmpDstOld
);
713 // ----------------------------------------------------------------------------
715 // ----------------------------------------------------------------------------
717 bool wxBitmapHandler::Create(wxGDIImage
*image
,
720 int width
, int height
, int depth
)
722 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
724 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
727 bool wxBitmapHandler::Load(wxGDIImage
*image
,
728 const wxString
& name
,
730 int width
, int height
)
732 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
734 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
737 bool wxBitmapHandler::Save(wxGDIImage
*image
,
738 const wxString
& name
,
741 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
743 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
746 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
747 void *WXUNUSED(data
),
750 int WXUNUSED(height
),
756 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
757 const wxString
& WXUNUSED(name
),
759 int WXUNUSED(desiredWidth
),
760 int WXUNUSED(desiredHeight
))
765 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
766 const wxString
& WXUNUSED(name
),
768 const wxPalette
*WXUNUSED(palette
))
773 // ----------------------------------------------------------------------------
775 // ----------------------------------------------------------------------------
777 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
778 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
780 unsigned long i
, headerSize
;
781 LPBITMAPINFO lpDIBheader
= NULL
;
782 LPPALETTEENTRY lpPe
= NULL
;
785 // Allocate space for a DIB header
786 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
787 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
788 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
790 GetPaletteEntries(hPal
, 0, 256, lpPe
);
792 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
794 // Fill in the static parts of the DIB header
795 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
796 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
797 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
798 lpDIBheader
->bmiHeader
.biPlanes
= 1;
800 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
801 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
802 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
803 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
804 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
807 // Initialize the DIB palette
808 for (i
= 0; i
< 256; i
++) {
809 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
810 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
811 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
812 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
815 *lpDIBHeader
= lpDIBheader
;
820 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
825 // ----------------------------------------------------------------------------
826 // other helper functions
827 // ----------------------------------------------------------------------------
829 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
831 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
833 // get width/height from the bitmap if not given
837 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
842 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
843 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
844 if ( !hdcSrc
|| !hdcDst
)
846 wxLogLastError("CreateCompatibleDC");
849 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
852 wxLogLastError("CreateBitmap");
855 ::SelectObject(hdcSrc
, hbmpMask
);
856 ::SelectObject(hdcDst
, hbmpInvMask
);
857 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
861 wxLogLastError("BitBlt");