]>
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
);
252 // Create from XPM data
253 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
257 (void)Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
260 wxBitmap::wxBitmap(int w
, int h
, int d
)
264 (void)Create(w
, h
, d
);
267 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
271 (void)Create(data
, type
, width
, height
, depth
);
274 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
278 LoadFile(filename
, (int)type
);
281 bool wxBitmap::Create(int w
, int h
, int d
)
285 m_refData
= new wxBitmapRefData
;
287 GetBitmapData()->m_width
= w
;
288 GetBitmapData()->m_height
= h
;
289 GetBitmapData()->m_depth
= d
;
295 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
298 wxLogLastError("CreateBitmap");
304 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
307 wxLogLastError("CreateCompatibleBitmap");
310 GetBitmapData()->m_depth
= wxDisplayDepth();
313 SetHBITMAP((WXHBITMAP
)hbmp
);
315 #if WXWIN_COMPATIBILITY_2
316 GetBitmapData()->m_ok
= hbmp
!= 0;
317 #endif // WXWIN_COMPATIBILITY_2
322 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
326 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
330 m_refData
= new wxBitmapRefData
;
332 return handler
->LoadFile(this, filename
, type
, -1, -1);
337 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
340 *this = image
.ConvertToBitmap();
346 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
350 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
354 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
355 "type %d defined."), type
);
360 m_refData
= new wxBitmapRefData
;
362 return handler
->Create(this, data
, type
, width
, height
, depth
);
365 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
367 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
371 return handler
->SaveFile(this, filename
, type
, palette
);
375 // FIXME what about palette? shouldn't we use it?
376 wxImage
image( *this );
380 return image
.SaveFile( filename
, type
);
384 // ----------------------------------------------------------------------------
385 // wxBitmap accessors
386 // ----------------------------------------------------------------------------
388 void wxBitmap::SetQuality(int q
)
392 GetBitmapData()->m_quality
= q
;
395 #if WXWIN_COMPATIBILITY_2
396 void wxBitmap::SetOk(bool isOk
)
400 GetBitmapData()->m_ok
= isOk
;
402 #endif // WXWIN_COMPATIBILITY_2
404 void wxBitmap::SetPalette(const wxPalette
& palette
)
408 GetBitmapData()->m_bitmapPalette
= palette
;
411 void wxBitmap::SetMask(wxMask
*mask
)
415 GetBitmapData()->m_bitmapMask
= mask
;
418 // Creates a bitmap that matches the device context, from
419 // an arbitray bitmap. At present, the original bitmap must have an
420 // associated palette. TODO: use a default palette if no palette exists.
421 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
422 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
425 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
426 HPALETTE hPal
= (HPALETTE
) NULL
;
428 void *lpBits
= (void*) NULL
;
430 if( GetPalette() && GetPalette()->Ok() )
432 tmpBitmap
.SetPalette(*GetPalette());
433 memDC
.SelectObject(tmpBitmap
);
434 memDC
.SetPalette(*GetPalette());
435 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
439 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
441 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
442 tmpBitmap
.SetPalette( palette
);
443 memDC
.SelectObject(tmpBitmap
);
444 memDC
.SetPalette( palette
);
447 // set the height negative because in a DIB the order of the lines is
449 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
454 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
456 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
458 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
459 GetWidth(), GetHeight(),
460 0, 0, 0, GetHeight(),
461 lpBits
, lpDib
, DIB_RGB_COLORS
);
470 // ----------------------------------------------------------------------------
472 // ----------------------------------------------------------------------------
479 // Construct a mask from a bitmap and a colour indicating
480 // the transparent area
481 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
484 Create(bitmap
, colour
);
487 // Construct a mask from a bitmap and a palette index indicating
488 // the transparent area
489 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
492 Create(bitmap
, paletteIndex
);
495 // Construct a mask from a mono bitmap (copies the bitmap).
496 wxMask::wxMask(const wxBitmap
& bitmap
)
505 ::DeleteObject((HBITMAP
) m_maskBitmap
);
508 // Create a mask from a mono bitmap (copies the bitmap).
509 bool wxMask::Create(const wxBitmap
& bitmap
)
513 ::DeleteObject((HBITMAP
) m_maskBitmap
);
516 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
520 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
525 HDC srcDC
= CreateCompatibleDC(0);
526 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
527 HDC destDC
= CreateCompatibleDC(0);
528 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
529 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
530 SelectObject(srcDC
, 0);
532 SelectObject(destDC
, 0);
537 // Create a mask from a bitmap and a palette index indicating
538 // the transparent area
539 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
543 ::DeleteObject((HBITMAP
) m_maskBitmap
);
546 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
548 unsigned char red
, green
, blue
;
549 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
551 wxColour
transparentColour(red
, green
, blue
);
552 return Create(bitmap
, transparentColour
);
558 // Create a mask from a bitmap and a colour indicating
559 // the transparent area
560 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
564 ::DeleteObject((HBITMAP
) m_maskBitmap
);
572 // scan the bitmap for the transparent colour and set
573 // the corresponding pixels in the mask to BLACK and
575 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
576 m_maskBitmap
= (WXHBITMAP
) ::CreateBitmap(
581 HDC srcDC
= ::CreateCompatibleDC(0);
582 ::SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
583 HDC destDC
= ::CreateCompatibleDC(0);
584 ::SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
586 // this is not very efficient, but I can't think
587 // of a better way of doing it
588 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
590 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
592 COLORREF col
= GetPixel(srcDC
, w
, h
);
593 if (col
== maskColour
)
595 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
599 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
603 ::SelectObject(srcDC
, 0);
605 ::SelectObject(destDC
, 0);
610 // ----------------------------------------------------------------------------
612 // ----------------------------------------------------------------------------
614 bool wxBitmapHandler::Create(wxGDIImage
*image
,
617 int width
, int height
, int depth
)
619 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
621 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
624 bool wxBitmapHandler::Load(wxGDIImage
*image
,
625 const wxString
& name
,
627 int width
, int height
)
629 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
631 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
634 bool wxBitmapHandler::Save(wxGDIImage
*image
,
635 const wxString
& name
,
638 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
640 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
643 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
644 void *WXUNUSED(data
),
647 int WXUNUSED(height
),
653 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
654 const wxString
& WXUNUSED(name
),
656 int WXUNUSED(desiredWidth
),
657 int WXUNUSED(desiredHeight
))
662 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
663 const wxString
& WXUNUSED(name
),
665 const wxPalette
*WXUNUSED(palette
))
670 // ----------------------------------------------------------------------------
672 // ----------------------------------------------------------------------------
674 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
675 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
677 unsigned long i
, headerSize
;
678 LPBITMAPINFO lpDIBheader
= NULL
;
679 LPPALETTEENTRY lpPe
= NULL
;
682 // Allocate space for a DIB header
683 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
684 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
685 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
687 GetPaletteEntries(hPal
, 0, 256, lpPe
);
689 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
691 // Fill in the static parts of the DIB header
692 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
693 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
694 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
695 lpDIBheader
->bmiHeader
.biPlanes
= 1;
697 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
698 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
699 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
700 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
701 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
704 // Initialize the DIB palette
705 for (i
= 0; i
< 256; i
++) {
706 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
707 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
708 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
709 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
712 *lpDIBHeader
= lpDIBheader
;
717 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)