]>
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 // ----------------------------------------------------------------------------
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 HBITMAP hbmpMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
136 // the icons mask is opposite to the usual wxWin convention
137 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
138 HDC dcDst
= ::CreateCompatibleDC(NULL
);
139 (void)SelectObject(dcSrc
, iconInfo
.hbmMask
);
140 (void)SelectObject(dcDst
, hbmpMask
);
142 HBRUSH brush
= ::CreateSolidBrush(RGB(255, 255, 255));
143 RECT rect
= { 0, 0, w
, h
};
144 FillRect(dcDst
, &rect
, brush
);
146 BitBlt(dcDst
, 0, 0, w
, h
, dcSrc
, 0, 0, SRCINVERT
);
148 SelectObject(dcDst
, NULL
);
149 SelectObject(dcSrc
, NULL
);
153 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)hbmpMask
);
155 #if WXWIN_COMPATIBILITY_2
156 refData
->m_ok
= TRUE
;
157 #endif // WXWIN_COMPATIBILITY_2
164 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
172 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
176 return CopyFromIconOrCursor(cursor
);
180 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
187 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
188 // to create a bitmap from icon there - but using this way we won't have
191 int width
= icon
.GetWidth(),
192 height
= icon
.GetHeight();
194 // copy the icon to the bitmap
196 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
197 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
198 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
200 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
202 ::SelectObject(hdc
, hbmpOld
);
205 wxBitmapRefData
*refData
= new wxBitmapRefData
;
208 refData
->m_width
= width
;
209 refData
->m_height
= height
;
210 refData
->m_depth
= wxDisplayDepth();
212 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
214 #if WXWIN_COMPATIBILITY_2
215 refData
->m_ok
= TRUE
;
216 #endif // WXWIN_COMPATIBILITY_2
220 return CopyFromIconOrCursor(icon
);
221 #endif // Win16/Win32
224 wxBitmap::~wxBitmap()
227 wxTheBitmapList
->DeleteObject(this);
230 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
234 wxBitmapRefData
*refData
= new wxBitmapRefData
;
237 refData
->m_width
= the_width
;
238 refData
->m_height
= the_height
;
239 refData
->m_depth
= no_bits
;
240 refData
->m_numColors
= 0;
241 refData
->m_selectedInto
= NULL
;
243 HBITMAP hbmp
= ::CreateBitmap(the_width
, the_height
, 1, no_bits
, bits
);
246 wxLogLastError("CreateBitmap");
249 SetHBITMAP((WXHBITMAP
)hbmp
);
253 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
256 (rect
.x
>= 0) && (rect
.y
>= 0) &&
257 (rect
.x
+rect
.width
<= GetWidth()) &&
258 (rect
.y
+rect
.height
<= GetHeight()),
259 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
261 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
262 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
265 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
266 HDC dcDst
= ::CreateCompatibleDC(NULL
);
267 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
268 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
269 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
271 // copy mask if there is one
274 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
276 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
277 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
278 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
280 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
284 SelectObject(dcDst
, NULL
);
285 SelectObject(dcSrc
, NULL
);
292 // Create from XPM data
293 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
297 (void)Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
300 wxBitmap::wxBitmap(int w
, int h
, int d
)
304 (void)Create(w
, h
, d
);
307 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
311 (void)Create(data
, type
, width
, height
, depth
);
314 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
318 LoadFile(filename
, (int)type
);
321 bool wxBitmap::Create(int w
, int h
, int d
)
325 m_refData
= new wxBitmapRefData
;
327 GetBitmapData()->m_width
= w
;
328 GetBitmapData()->m_height
= h
;
329 GetBitmapData()->m_depth
= d
;
335 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
338 wxLogLastError("CreateBitmap");
344 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
347 wxLogLastError("CreateCompatibleBitmap");
350 GetBitmapData()->m_depth
= wxDisplayDepth();
353 SetHBITMAP((WXHBITMAP
)hbmp
);
355 #if WXWIN_COMPATIBILITY_2
356 GetBitmapData()->m_ok
= hbmp
!= 0;
357 #endif // WXWIN_COMPATIBILITY_2
362 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
366 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
370 m_refData
= new wxBitmapRefData
;
372 return handler
->LoadFile(this, filename
, type
, -1, -1);
377 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
380 *this = image
.ConvertToBitmap();
386 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
390 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
394 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
395 "type %d defined."), type
);
400 m_refData
= new wxBitmapRefData
;
402 return handler
->Create(this, data
, type
, width
, height
, depth
);
405 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
407 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
411 return handler
->SaveFile(this, filename
, type
, palette
);
415 // FIXME what about palette? shouldn't we use it?
416 wxImage
image( *this );
420 return image
.SaveFile( filename
, type
);
424 // ----------------------------------------------------------------------------
425 // wxBitmap accessors
426 // ----------------------------------------------------------------------------
428 void wxBitmap::SetQuality(int q
)
432 GetBitmapData()->m_quality
= q
;
435 #if WXWIN_COMPATIBILITY_2
436 void wxBitmap::SetOk(bool isOk
)
440 GetBitmapData()->m_ok
= isOk
;
442 #endif // WXWIN_COMPATIBILITY_2
444 void wxBitmap::SetPalette(const wxPalette
& palette
)
448 GetBitmapData()->m_bitmapPalette
= palette
;
451 void wxBitmap::SetMask(wxMask
*mask
)
455 GetBitmapData()->m_bitmapMask
= mask
;
458 // Creates a bitmap that matches the device context, from
459 // an arbitray bitmap. At present, the original bitmap must have an
460 // associated palette. TODO: use a default palette if no palette exists.
461 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
462 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
465 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
466 HPALETTE hPal
= (HPALETTE
) NULL
;
468 void *lpBits
= (void*) NULL
;
470 if( GetPalette() && GetPalette()->Ok() )
472 tmpBitmap
.SetPalette(*GetPalette());
473 memDC
.SelectObject(tmpBitmap
);
474 memDC
.SetPalette(*GetPalette());
475 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
479 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
481 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
482 tmpBitmap
.SetPalette( palette
);
483 memDC
.SelectObject(tmpBitmap
);
484 memDC
.SetPalette( palette
);
487 // set the height negative because in a DIB the order of the lines is
489 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
494 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
496 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
498 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
499 GetWidth(), GetHeight(),
500 0, 0, 0, GetHeight(),
501 lpBits
, lpDib
, DIB_RGB_COLORS
);
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
519 // Construct a mask from a bitmap and a colour indicating
520 // the transparent area
521 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
524 Create(bitmap
, colour
);
527 // Construct a mask from a bitmap and a palette index indicating
528 // the transparent area
529 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
532 Create(bitmap
, paletteIndex
);
535 // Construct a mask from a mono bitmap (copies the bitmap).
536 wxMask::wxMask(const wxBitmap
& bitmap
)
545 ::DeleteObject((HBITMAP
) m_maskBitmap
);
548 // Create a mask from a mono bitmap (copies the bitmap).
549 bool wxMask::Create(const wxBitmap
& bitmap
)
553 ::DeleteObject((HBITMAP
) m_maskBitmap
);
556 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
560 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
565 HDC srcDC
= CreateCompatibleDC(0);
566 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
567 HDC destDC
= CreateCompatibleDC(0);
568 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
569 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
570 SelectObject(srcDC
, 0);
572 SelectObject(destDC
, 0);
577 // Create a mask from a bitmap and a palette index indicating
578 // the transparent area
579 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
583 ::DeleteObject((HBITMAP
) m_maskBitmap
);
586 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
588 unsigned char red
, green
, blue
;
589 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
591 wxColour
transparentColour(red
, green
, blue
);
592 return Create(bitmap
, transparentColour
);
598 // Create a mask from a bitmap and a colour indicating
599 // the transparent area
600 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
604 ::DeleteObject((HBITMAP
) m_maskBitmap
);
612 // scan the bitmap for the transparent colour and set
613 // the corresponding pixels in the mask to BLACK and
615 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
616 m_maskBitmap
= (WXHBITMAP
) ::CreateBitmap(
621 HDC srcDC
= ::CreateCompatibleDC(0);
622 ::SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
623 HDC destDC
= ::CreateCompatibleDC(0);
624 ::SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
626 // this is not very efficient, but I can't think
627 // of a better way of doing it
628 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
630 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
632 COLORREF col
= GetPixel(srcDC
, w
, h
);
633 if (col
== maskColour
)
635 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
639 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
643 ::SelectObject(srcDC
, 0);
645 ::SelectObject(destDC
, 0);
650 // ----------------------------------------------------------------------------
652 // ----------------------------------------------------------------------------
654 bool wxBitmapHandler::Create(wxGDIImage
*image
,
657 int width
, int height
, int depth
)
659 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
661 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
664 bool wxBitmapHandler::Load(wxGDIImage
*image
,
665 const wxString
& name
,
667 int width
, int height
)
669 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
671 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
674 bool wxBitmapHandler::Save(wxGDIImage
*image
,
675 const wxString
& name
,
678 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
680 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
683 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
684 void *WXUNUSED(data
),
687 int WXUNUSED(height
),
693 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
694 const wxString
& WXUNUSED(name
),
696 int WXUNUSED(desiredWidth
),
697 int WXUNUSED(desiredHeight
))
702 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
703 const wxString
& WXUNUSED(name
),
705 const wxPalette
*WXUNUSED(palette
))
710 // ----------------------------------------------------------------------------
712 // ----------------------------------------------------------------------------
714 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
715 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
717 unsigned long i
, headerSize
;
718 LPBITMAPINFO lpDIBheader
= NULL
;
719 LPPALETTEENTRY lpPe
= NULL
;
722 // Allocate space for a DIB header
723 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
724 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
725 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
727 GetPaletteEntries(hPal
, 0, 256, lpPe
);
729 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
731 // Fill in the static parts of the DIB header
732 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
733 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
734 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
735 lpDIBheader
->bmiHeader
.biPlanes
= 1;
737 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
738 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
739 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
740 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
741 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
744 // Initialize the DIB palette
745 for (i
= 0; i
< 256; i
++) {
746 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
747 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
748 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
749 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
752 *lpDIBHeader
= lpDIBheader
;
757 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)