]>
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 #if !USE_SHARED_LIBRARIES
54 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
55 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
57 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
60 // ============================================================================
62 // ============================================================================
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 wxBitmapRefData::wxBitmapRefData()
71 m_selectedInto
= NULL
;
74 m_hBitmap
= (WXHBITMAP
) NULL
;
77 void wxBitmapRefData::Free()
79 wxASSERT_MSG( !m_selectedInto
,
80 wxT("deleting bitmap still selected into wxMemoryDC") );
84 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
86 wxLogLastError("DeleteObject(hbitmap)");
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // this function should be called from all wxBitmap ctors
101 // m_refData = NULL; done in the base class ctor
103 if ( wxTheBitmapList
)
104 wxTheBitmapList
->AddBitmap(this);
109 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
111 // it may be either HICON or HCURSOR
112 HICON hicon
= (HICON
)icon
.GetHandle();
115 if ( !::GetIconInfo(hicon
, &iconInfo
) )
117 wxLogLastError("GetIconInfo");
122 wxBitmapRefData
*refData
= new wxBitmapRefData
;
125 refData
->m_width
= icon
.GetWidth();
126 refData
->m_height
= icon
.GetHeight();
127 refData
->m_depth
= wxDisplayDepth();
129 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
130 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)iconInfo
.hbmMask
);
132 #if WXWIN_COMPATIBILITY_2
133 refData
->m_ok
= TRUE
;
134 #endif // WXWIN_COMPATIBILITY_2
141 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
149 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
154 return CopyFromIconOrCursor(cursor
);
157 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
164 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
165 // to create a bitmap from icon there - but using this way we won't have
168 int width
= icon
.GetWidth(),
169 height
= icon
.GetHeight();
171 // copy the icon to the bitmap
173 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
174 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
175 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
177 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
179 ::SelectObject(hdc
, hbmpOld
);
182 wxBitmapRefData
*refData
= new wxBitmapRefData
;
185 refData
->m_width
= width
;
186 refData
->m_height
= height
;
187 refData
->m_depth
= wxDisplayDepth();
189 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
191 #if WXWIN_COMPATIBILITY_2
192 refData
->m_ok
= TRUE
;
193 #endif // WXWIN_COMPATIBILITY_2
197 return CopyFromIconOrCursor(icon
);
198 #endif // Win16/Win32
201 wxBitmap::~wxBitmap()
204 wxTheBitmapList
->DeleteObject(this);
207 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
211 wxBitmapRefData
*refData
= new wxBitmapRefData
;
214 refData
->m_width
= the_width
;
215 refData
->m_height
= the_height
;
216 refData
->m_depth
= no_bits
;
217 refData
->m_numColors
= 0;
218 refData
->m_selectedInto
= NULL
;
220 HBITMAP hbmp
= ::CreateBitmap(the_width
, the_height
, 1, no_bits
, bits
);
223 wxLogLastError("CreateBitmap");
226 SetHBITMAP((WXHBITMAP
)hbmp
);
229 // Create from XPM data
230 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
234 (void)Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
237 wxBitmap::wxBitmap(int w
, int h
, int d
)
241 (void)Create(w
, h
, d
);
244 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
248 (void)Create(data
, type
, width
, height
, depth
);
251 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
255 LoadFile(filename
, (int)type
);
258 bool wxBitmap::Create(int w
, int h
, int d
)
262 m_refData
= new wxBitmapRefData
;
264 GetBitmapData()->m_width
= w
;
265 GetBitmapData()->m_height
= h
;
266 GetBitmapData()->m_depth
= d
;
272 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
275 wxLogLastError("CreateBitmap");
281 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
284 wxLogLastError("CreateCompatibleBitmap");
287 GetBitmapData()->m_depth
= wxDisplayDepth();
290 SetHBITMAP((WXHBITMAP
)hbmp
);
292 #if WXWIN_COMPATIBILITY_2
293 GetBitmapData()->m_ok
= hbmp
!= 0;
294 #endif // WXWIN_COMPATIBILITY_2
299 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
303 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
307 m_refData
= new wxBitmapRefData
;
309 return handler
->LoadFile(this, filename
, type
, -1, -1);
314 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
317 *this = image
.ConvertToBitmap();
323 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
327 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
331 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
332 "type %d defined."), type
);
337 m_refData
= new wxBitmapRefData
;
339 return handler
->Create(this, data
, type
, width
, height
, depth
);
342 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
344 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
348 return handler
->SaveFile(this, filename
, type
, palette
);
352 // FIXME what about palette? shouldn't we use it?
353 wxImage
image( *this );
357 return image
.SaveFile( filename
, type
);
361 // ----------------------------------------------------------------------------
362 // wxBitmap accessors
363 // ----------------------------------------------------------------------------
365 void wxBitmap::SetQuality(int q
)
369 GetBitmapData()->m_quality
= q
;
372 #if WXWIN_COMPATIBILITY_2
373 void wxBitmap::SetOk(bool isOk
)
377 GetBitmapData()->m_ok
= isOk
;
379 #endif // WXWIN_COMPATIBILITY_2
381 void wxBitmap::SetPalette(const wxPalette
& palette
)
385 GetBitmapData()->m_bitmapPalette
= palette
;
388 void wxBitmap::SetMask(wxMask
*mask
)
392 GetBitmapData()->m_bitmapMask
= mask
;
395 // Creates a bitmap that matches the device context, from
396 // an arbitray bitmap. At present, the original bitmap must have an
397 // associated palette. TODO: use a default palette if no palette exists.
398 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
399 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
402 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
403 HPALETTE hPal
= (HPALETTE
) NULL
;
405 void *lpBits
= (void*) NULL
;
407 if( GetPalette() && GetPalette()->Ok() )
409 tmpBitmap
.SetPalette(*GetPalette());
410 memDC
.SelectObject(tmpBitmap
);
411 memDC
.SetPalette(*GetPalette());
412 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
416 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
418 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
419 tmpBitmap
.SetPalette( palette
);
420 memDC
.SelectObject(tmpBitmap
);
421 memDC
.SetPalette( palette
);
424 // set the height negative because in a DIB the order of the lines is
426 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
431 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
433 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
435 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
436 GetWidth(), GetHeight(),
437 0, 0, 0, GetHeight(),
438 lpBits
, lpDib
, DIB_RGB_COLORS
);
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
456 // Construct a mask from a bitmap and a colour indicating
457 // the transparent area
458 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
461 Create(bitmap
, colour
);
464 // Construct a mask from a bitmap and a palette index indicating
465 // the transparent area
466 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
469 Create(bitmap
, paletteIndex
);
472 // Construct a mask from a mono bitmap (copies the bitmap).
473 wxMask::wxMask(const wxBitmap
& bitmap
)
482 ::DeleteObject((HBITMAP
) m_maskBitmap
);
485 // Create a mask from a mono bitmap (copies the bitmap).
486 bool wxMask::Create(const wxBitmap
& bitmap
)
490 ::DeleteObject((HBITMAP
) m_maskBitmap
);
493 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
497 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
502 HDC srcDC
= CreateCompatibleDC(0);
503 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
504 HDC destDC
= CreateCompatibleDC(0);
505 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
506 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
507 SelectObject(srcDC
, 0);
509 SelectObject(destDC
, 0);
514 // Create a mask from a bitmap and a palette index indicating
515 // the transparent area
516 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
520 ::DeleteObject((HBITMAP
) m_maskBitmap
);
523 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
525 unsigned char red
, green
, blue
;
526 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
528 wxColour
transparentColour(red
, green
, blue
);
529 return Create(bitmap
, transparentColour
);
535 // Create a mask from a bitmap and a colour indicating
536 // the transparent area
537 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
541 ::DeleteObject((HBITMAP
) m_maskBitmap
);
549 // scan the bitmap for the transparent colour and set
550 // the corresponding pixels in the mask to BLACK and
552 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
553 m_maskBitmap
= (WXHBITMAP
) ::CreateBitmap(
558 HDC srcDC
= ::CreateCompatibleDC(0);
559 ::SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
560 HDC destDC
= ::CreateCompatibleDC(0);
561 ::SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
563 // this is not very efficient, but I can't think
564 // of a better way of doing it
565 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
567 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
569 COLORREF col
= GetPixel(srcDC
, w
, h
);
570 if (col
== maskColour
)
572 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
576 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
580 ::SelectObject(srcDC
, 0);
582 ::SelectObject(destDC
, 0);
587 // ----------------------------------------------------------------------------
589 // ----------------------------------------------------------------------------
591 bool wxBitmapHandler::Create(wxGDIImage
*image
,
594 int width
, int height
, int depth
)
596 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
598 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
601 bool wxBitmapHandler::Load(wxGDIImage
*image
,
602 const wxString
& name
,
604 int width
, int height
)
606 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
608 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
611 bool wxBitmapHandler::Save(wxGDIImage
*image
,
612 const wxString
& name
,
615 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
617 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
620 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
621 void *WXUNUSED(data
),
624 int WXUNUSED(height
),
630 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
631 const wxString
& WXUNUSED(name
),
633 int WXUNUSED(desiredWidth
),
634 int WXUNUSED(desiredHeight
))
639 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
640 const wxString
& WXUNUSED(name
),
642 const wxPalette
*WXUNUSED(palette
))
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
652 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
654 unsigned long i
, headerSize
;
655 LPBITMAPINFO lpDIBheader
= NULL
;
656 LPPALETTEENTRY lpPe
= NULL
;
659 // Allocate space for a DIB header
660 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
661 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
662 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
664 GetPaletteEntries(hPal
, 0, 256, lpPe
);
666 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
668 // Fill in the static parts of the DIB header
669 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
670 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
671 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
672 lpDIBheader
->bmiHeader
.biPlanes
= 1;
674 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
675 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
676 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
677 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
678 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
681 // Initialize the DIB palette
682 for (i
= 0; i
< 256; i
++) {
683 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
684 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
685 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
686 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
689 *lpDIBHeader
= lpDIBheader
;
694 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)