]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
015b00e73249610b483b5482b14dac586d3e0587
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 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
54 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
56 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 wxBitmapRefData::wxBitmapRefData()
69 m_selectedInto
= NULL
;
72 m_hBitmap
= (WXHBITMAP
) NULL
;
75 void wxBitmapRefData::Free()
77 wxASSERT_MSG( !m_selectedInto
,
78 wxT("deleting bitmap still selected into wxMemoryDC") );
82 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
84 wxLogLastError("DeleteObject(hbitmap)");
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // this function should be called from all wxBitmap ctors
99 // m_refData = NULL; done in the base class ctor
101 if ( wxTheBitmapList
)
102 wxTheBitmapList
->AddBitmap(this);
107 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
109 // it may be either HICON or HCURSOR
110 HICON hicon
= (HICON
)icon
.GetHandle();
113 if ( !::GetIconInfo(hicon
, &iconInfo
) )
115 wxLogLastError("GetIconInfo");
120 wxBitmapRefData
*refData
= new wxBitmapRefData
;
123 int w
= icon
.GetWidth(),
124 h
= icon
.GetHeight();
126 refData
->m_width
= w
;
127 refData
->m_height
= h
;
128 refData
->m_depth
= wxDisplayDepth();
130 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
132 // the mask returned by GetIconInfo() is inversed compared to the usual
134 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
135 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
137 #if WXWIN_COMPATIBILITY_2
138 refData
->m_ok
= TRUE
;
139 #endif // WXWIN_COMPATIBILITY_2
146 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
154 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
158 return CopyFromIconOrCursor(cursor
);
162 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
169 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
170 // to create a bitmap from icon there - but using this way we won't have
173 int width
= icon
.GetWidth(),
174 height
= icon
.GetHeight();
176 // copy the icon to the bitmap
178 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
179 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
180 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
182 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
184 ::SelectObject(hdc
, hbmpOld
);
187 wxBitmapRefData
*refData
= new wxBitmapRefData
;
190 refData
->m_width
= width
;
191 refData
->m_height
= height
;
192 refData
->m_depth
= wxDisplayDepth();
194 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
196 #if WXWIN_COMPATIBILITY_2
197 refData
->m_ok
= TRUE
;
198 #endif // WXWIN_COMPATIBILITY_2
202 return CopyFromIconOrCursor(icon
);
203 #endif // Win16/Win32
206 wxBitmap::~wxBitmap()
209 wxTheBitmapList
->DeleteObject(this);
212 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
216 wxBitmapRefData
*refData
= new wxBitmapRefData
;
219 refData
->m_width
= width
;
220 refData
->m_height
= height
;
221 refData
->m_depth
= depth
;
222 refData
->m_numColors
= 0;
223 refData
->m_selectedInto
= NULL
;
228 // we assume that it is in XBM format which is not quite the same as
229 // the format CreateBitmap() wants because the order of bytes in the
231 static const size_t bytesPerLine
= (width
+ 7) / 8;
232 static const size_t padding
= bytesPerLine
% 2;
233 static const size_t len
= height
* ( padding
+ bytesPerLine
);
234 data
= (char *)malloc(len
);
235 const char *src
= bits
;
238 for ( int rows
= 0; rows
< height
; rows
++ )
240 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
242 unsigned char val
= *src
++;
243 unsigned char reversed
= 0;
245 for ( int bits
= 0; bits
< 8; bits
++)
248 reversed
|= (val
& 0x01);
260 // bits should already be in Windows standard format
261 data
= (char *)bits
; // const_cast is harmless
264 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
267 wxLogLastError("CreateBitmap");
275 SetHBITMAP((WXHBITMAP
)hbmp
);
278 // Create from XPM data
279 bool wxBitmap::CreateFromXpm(const char **data
)
283 return Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
286 wxBitmap::wxBitmap(int w
, int h
, int d
)
290 (void)Create(w
, h
, d
);
293 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
297 (void)Create(data
, type
, width
, height
, depth
);
300 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
304 LoadFile(filename
, (int)type
);
307 bool wxBitmap::Create(int w
, int h
, int d
)
311 m_refData
= new wxBitmapRefData
;
313 GetBitmapData()->m_width
= w
;
314 GetBitmapData()->m_height
= h
;
315 GetBitmapData()->m_depth
= d
;
321 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
324 wxLogLastError("CreateBitmap");
330 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
333 wxLogLastError("CreateCompatibleBitmap");
336 GetBitmapData()->m_depth
= wxDisplayDepth();
339 SetHBITMAP((WXHBITMAP
)hbmp
);
341 #if WXWIN_COMPATIBILITY_2
342 GetBitmapData()->m_ok
= hbmp
!= 0;
343 #endif // WXWIN_COMPATIBILITY_2
348 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
352 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
356 m_refData
= new wxBitmapRefData
;
358 return handler
->LoadFile(this, filename
, type
, -1, -1);
363 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
366 *this = image
.ConvertToBitmap();
372 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
376 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
380 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
381 "type %d defined."), type
);
386 m_refData
= new wxBitmapRefData
;
388 return handler
->Create(this, data
, type
, width
, height
, depth
);
391 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
393 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
397 return handler
->SaveFile(this, filename
, type
, palette
);
401 // FIXME what about palette? shouldn't we use it?
402 wxImage
image( *this );
406 return image
.SaveFile( filename
, type
);
410 // ----------------------------------------------------------------------------
411 // sub bitmap extraction
412 // ----------------------------------------------------------------------------
414 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
417 (rect
.x
>= 0) && (rect
.y
>= 0) &&
418 (rect
.x
+rect
.width
<= GetWidth()) &&
419 (rect
.y
+rect
.height
<= GetHeight()),
420 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
422 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
423 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
426 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
427 HDC dcDst
= ::CreateCompatibleDC(NULL
);
428 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
429 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
430 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
432 // copy mask if there is one
435 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
437 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
438 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
439 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
441 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
445 SelectObject(dcDst
, NULL
);
446 SelectObject(dcSrc
, NULL
);
453 // ----------------------------------------------------------------------------
454 // wxBitmap accessors
455 // ----------------------------------------------------------------------------
457 void wxBitmap::SetQuality(int q
)
461 GetBitmapData()->m_quality
= q
;
464 #if WXWIN_COMPATIBILITY_2
465 void wxBitmap::SetOk(bool isOk
)
469 GetBitmapData()->m_ok
= isOk
;
471 #endif // WXWIN_COMPATIBILITY_2
473 void wxBitmap::SetPalette(const wxPalette
& palette
)
477 GetBitmapData()->m_bitmapPalette
= palette
;
480 void wxBitmap::SetMask(wxMask
*mask
)
484 GetBitmapData()->m_bitmapMask
= mask
;
487 // Creates a bitmap that matches the device context, from
488 // an arbitray bitmap. At present, the original bitmap must have an
489 // associated palette. TODO: use a default palette if no palette exists.
490 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
491 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
494 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
495 HPALETTE hPal
= (HPALETTE
) NULL
;
497 void *lpBits
= (void*) NULL
;
499 if( GetPalette() && GetPalette()->Ok() )
501 tmpBitmap
.SetPalette(*GetPalette());
502 memDC
.SelectObject(tmpBitmap
);
503 memDC
.SetPalette(*GetPalette());
504 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
508 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
510 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
511 tmpBitmap
.SetPalette( palette
);
512 memDC
.SelectObject(tmpBitmap
);
513 memDC
.SetPalette( palette
);
516 // set the height negative because in a DIB the order of the lines is
518 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
523 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
525 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
527 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
528 GetWidth(), GetHeight(),
529 0, 0, 0, GetHeight(),
530 lpBits
, lpDib
, DIB_RGB_COLORS
);
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
548 // Construct a mask from a bitmap and a colour indicating
549 // the transparent area
550 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
553 Create(bitmap
, colour
);
556 // Construct a mask from a bitmap and a palette index indicating
557 // the transparent area
558 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
561 Create(bitmap
, paletteIndex
);
564 // Construct a mask from a mono bitmap (copies the bitmap).
565 wxMask::wxMask(const wxBitmap
& bitmap
)
574 ::DeleteObject((HBITMAP
) m_maskBitmap
);
577 // Create a mask from a mono bitmap (copies the bitmap).
578 bool wxMask::Create(const wxBitmap
& bitmap
)
582 ::DeleteObject((HBITMAP
) m_maskBitmap
);
585 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
589 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
594 HDC srcDC
= CreateCompatibleDC(0);
595 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
596 HDC destDC
= CreateCompatibleDC(0);
597 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
598 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
599 SelectObject(srcDC
, 0);
601 SelectObject(destDC
, 0);
606 // Create a mask from a bitmap and a palette index indicating
607 // the transparent area
608 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
612 ::DeleteObject((HBITMAP
) m_maskBitmap
);
615 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
617 unsigned char red
, green
, blue
;
618 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
620 wxColour
transparentColour(red
, green
, blue
);
621 return Create(bitmap
, transparentColour
);
627 // Create a mask from a bitmap and a colour indicating
628 // the transparent area
629 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
631 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
635 ::DeleteObject((HBITMAP
) m_maskBitmap
);
639 int width
= bitmap
.GetWidth(),
640 height
= bitmap
.GetHeight();
642 // scan the bitmap for the transparent colour and set the corresponding
643 // pixels in the mask to BLACK and the rest to WHITE
644 COLORREF maskColour
= wxColourToRGB(colour
);
645 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
647 HDC srcDC
= ::CreateCompatibleDC(NULL
);
648 HDC destDC
= ::CreateCompatibleDC(NULL
);
649 if ( !srcDC
|| !destDC
)
651 wxLogLastError("CreateCompatibleDC");
654 if ( !::SelectObject(srcDC
, GetHbitmapOf(bitmap
)) )
656 wxLogLastError("SelectObject");
658 if ( !::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
) )
660 wxLogLastError("SelectObject");
663 // this is not very efficient, but I can't think of a better way of doing
665 for ( int w
= 0; w
< width
; w
++ )
667 for ( int h
= 0; h
< height
; h
++ )
669 COLORREF col
= GetPixel(srcDC
, w
, h
);
670 if ( col
== CLR_INVALID
)
672 wxLogLastError("GetPixel");
674 // doesn't make sense to continue
675 ::SelectObject(srcDC
, 0);
677 ::SelectObject(destDC
, 0);
683 if ( col
== maskColour
)
685 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
689 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
694 ::SelectObject(srcDC
, 0);
696 ::SelectObject(destDC
, 0);
702 // ----------------------------------------------------------------------------
704 // ----------------------------------------------------------------------------
706 bool wxBitmapHandler::Create(wxGDIImage
*image
,
709 int width
, int height
, int depth
)
711 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
713 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
716 bool wxBitmapHandler::Load(wxGDIImage
*image
,
717 const wxString
& name
,
719 int width
, int height
)
721 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
723 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
726 bool wxBitmapHandler::Save(wxGDIImage
*image
,
727 const wxString
& name
,
730 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
732 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
735 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
736 void *WXUNUSED(data
),
739 int WXUNUSED(height
),
745 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
746 const wxString
& WXUNUSED(name
),
748 int WXUNUSED(desiredWidth
),
749 int WXUNUSED(desiredHeight
))
754 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
755 const wxString
& WXUNUSED(name
),
757 const wxPalette
*WXUNUSED(palette
))
762 // ----------------------------------------------------------------------------
764 // ----------------------------------------------------------------------------
766 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
767 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
769 unsigned long i
, headerSize
;
770 LPBITMAPINFO lpDIBheader
= NULL
;
771 LPPALETTEENTRY lpPe
= NULL
;
774 // Allocate space for a DIB header
775 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
776 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
777 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
779 GetPaletteEntries(hPal
, 0, 256, lpPe
);
781 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
783 // Fill in the static parts of the DIB header
784 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
785 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
786 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
787 lpDIBheader
->bmiHeader
.biPlanes
= 1;
789 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
790 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
791 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
792 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
793 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
796 // Initialize the DIB palette
797 for (i
= 0; i
< 256; i
++) {
798 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
799 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
800 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
801 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
804 *lpDIBHeader
= lpDIBheader
;
809 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
814 // ----------------------------------------------------------------------------
815 // other helper functions
816 // ----------------------------------------------------------------------------
818 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
820 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
822 // get width/height from the bitmap if not given
826 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
831 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
832 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
833 if ( !hdcSrc
|| !hdcDst
)
835 wxLogLastError("CreateCompatibleDC");
838 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
841 wxLogLastError("CreateBitmap");
844 ::SelectObject(hdcSrc
, hbmpMask
);
845 ::SelectObject(hdcDst
, hbmpInvMask
);
846 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
850 wxLogLastError("BitBlt");