7f7aa14f75bd0a673e8aa3d46d5a09878d0d3039
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 int w
= icon
.GetWidth(),
126 h
= icon
.GetHeight();
128 refData
->m_width
= w
;
129 refData
->m_height
= h
;
130 refData
->m_depth
= wxDisplayDepth();
132 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
134 // the mask returned by GetIconInfo() is inversed compared to the usual
136 HBITMAP hbmpMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
138 // the icons mask is opposite to the usual wxWin convention
139 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
140 HDC dcDst
= ::CreateCompatibleDC(NULL
);
141 (void)SelectObject(dcSrc
, iconInfo
.hbmMask
);
142 (void)SelectObject(dcDst
, hbmpMask
);
144 HBRUSH brush
= ::CreateSolidBrush(RGB(255, 255, 255));
145 RECT rect
= { 0, 0, w
, h
};
146 FillRect(dcDst
, &rect
, brush
);
148 BitBlt(dcDst
, 0, 0, w
, h
, dcSrc
, 0, 0, SRCINVERT
);
150 SelectObject(dcDst
, NULL
);
151 SelectObject(dcSrc
, NULL
);
155 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)hbmpMask
);
157 #if WXWIN_COMPATIBILITY_2
158 refData
->m_ok
= TRUE
;
159 #endif // WXWIN_COMPATIBILITY_2
166 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
174 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
179 return CopyFromIconOrCursor(cursor
);
182 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
189 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
190 // to create a bitmap from icon there - but using this way we won't have
193 int width
= icon
.GetWidth(),
194 height
= icon
.GetHeight();
196 // copy the icon to the bitmap
198 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
199 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
200 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
202 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
204 ::SelectObject(hdc
, hbmpOld
);
207 wxBitmapRefData
*refData
= new wxBitmapRefData
;
210 refData
->m_width
= width
;
211 refData
->m_height
= height
;
212 refData
->m_depth
= wxDisplayDepth();
214 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
216 #if WXWIN_COMPATIBILITY_2
217 refData
->m_ok
= TRUE
;
218 #endif // WXWIN_COMPATIBILITY_2
222 return CopyFromIconOrCursor(icon
);
223 #endif // Win16/Win32
226 wxBitmap::~wxBitmap()
229 wxTheBitmapList
->DeleteObject(this);
232 wxBitmap::wxBitmap(const char bits
[], int the_width
, int the_height
, int no_bits
)
236 wxBitmapRefData
*refData
= new wxBitmapRefData
;
239 refData
->m_width
= the_width
;
240 refData
->m_height
= the_height
;
241 refData
->m_depth
= no_bits
;
242 refData
->m_numColors
= 0;
243 refData
->m_selectedInto
= NULL
;
245 HBITMAP hbmp
= ::CreateBitmap(the_width
, the_height
, 1, no_bits
, bits
);
248 wxLogLastError("CreateBitmap");
251 SetHBITMAP((WXHBITMAP
)hbmp
);
254 // Create from XPM data
255 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
259 (void)Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
262 wxBitmap::wxBitmap(int w
, int h
, int d
)
266 (void)Create(w
, h
, d
);
269 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
273 (void)Create(data
, type
, width
, height
, depth
);
276 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
280 LoadFile(filename
, (int)type
);
283 bool wxBitmap::Create(int w
, int h
, int d
)
287 m_refData
= new wxBitmapRefData
;
289 GetBitmapData()->m_width
= w
;
290 GetBitmapData()->m_height
= h
;
291 GetBitmapData()->m_depth
= d
;
297 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
300 wxLogLastError("CreateBitmap");
306 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
309 wxLogLastError("CreateCompatibleBitmap");
312 GetBitmapData()->m_depth
= wxDisplayDepth();
315 SetHBITMAP((WXHBITMAP
)hbmp
);
317 #if WXWIN_COMPATIBILITY_2
318 GetBitmapData()->m_ok
= hbmp
!= 0;
319 #endif // WXWIN_COMPATIBILITY_2
324 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
328 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
332 m_refData
= new wxBitmapRefData
;
334 return handler
->LoadFile(this, filename
, type
, -1, -1);
339 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
342 *this = image
.ConvertToBitmap();
348 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
352 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
356 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
357 "type %d defined."), type
);
362 m_refData
= new wxBitmapRefData
;
364 return handler
->Create(this, data
, type
, width
, height
, depth
);
367 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
369 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
373 return handler
->SaveFile(this, filename
, type
, palette
);
377 // FIXME what about palette? shouldn't we use it?
378 wxImage
image( *this );
382 return image
.SaveFile( filename
, type
);
386 // ----------------------------------------------------------------------------
387 // wxBitmap accessors
388 // ----------------------------------------------------------------------------
390 void wxBitmap::SetQuality(int q
)
394 GetBitmapData()->m_quality
= q
;
397 #if WXWIN_COMPATIBILITY_2
398 void wxBitmap::SetOk(bool isOk
)
402 GetBitmapData()->m_ok
= isOk
;
404 #endif // WXWIN_COMPATIBILITY_2
406 void wxBitmap::SetPalette(const wxPalette
& palette
)
410 GetBitmapData()->m_bitmapPalette
= palette
;
413 void wxBitmap::SetMask(wxMask
*mask
)
417 GetBitmapData()->m_bitmapMask
= mask
;
420 // Creates a bitmap that matches the device context, from
421 // an arbitray bitmap. At present, the original bitmap must have an
422 // associated palette. TODO: use a default palette if no palette exists.
423 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
424 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
427 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
428 HPALETTE hPal
= (HPALETTE
) NULL
;
430 void *lpBits
= (void*) NULL
;
432 if( GetPalette() && GetPalette()->Ok() )
434 tmpBitmap
.SetPalette(*GetPalette());
435 memDC
.SelectObject(tmpBitmap
);
436 memDC
.SetPalette(*GetPalette());
437 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
441 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
443 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
444 tmpBitmap
.SetPalette( palette
);
445 memDC
.SelectObject(tmpBitmap
);
446 memDC
.SetPalette( palette
);
449 // set the height negative because in a DIB the order of the lines is
451 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
456 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
458 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
460 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
461 GetWidth(), GetHeight(),
462 0, 0, 0, GetHeight(),
463 lpBits
, lpDib
, DIB_RGB_COLORS
);
472 // ----------------------------------------------------------------------------
474 // ----------------------------------------------------------------------------
481 // Construct a mask from a bitmap and a colour indicating
482 // the transparent area
483 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
486 Create(bitmap
, colour
);
489 // Construct a mask from a bitmap and a palette index indicating
490 // the transparent area
491 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
494 Create(bitmap
, paletteIndex
);
497 // Construct a mask from a mono bitmap (copies the bitmap).
498 wxMask::wxMask(const wxBitmap
& bitmap
)
507 ::DeleteObject((HBITMAP
) m_maskBitmap
);
510 // Create a mask from a mono bitmap (copies the bitmap).
511 bool wxMask::Create(const wxBitmap
& bitmap
)
515 ::DeleteObject((HBITMAP
) m_maskBitmap
);
518 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
522 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
527 HDC srcDC
= CreateCompatibleDC(0);
528 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
529 HDC destDC
= CreateCompatibleDC(0);
530 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
531 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
532 SelectObject(srcDC
, 0);
534 SelectObject(destDC
, 0);
539 // Create a mask from a bitmap and a palette index indicating
540 // the transparent area
541 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
545 ::DeleteObject((HBITMAP
) m_maskBitmap
);
548 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
550 unsigned char red
, green
, blue
;
551 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
553 wxColour
transparentColour(red
, green
, blue
);
554 return Create(bitmap
, transparentColour
);
560 // Create a mask from a bitmap and a colour indicating
561 // the transparent area
562 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
566 ::DeleteObject((HBITMAP
) m_maskBitmap
);
574 // scan the bitmap for the transparent colour and set
575 // the corresponding pixels in the mask to BLACK and
577 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
578 m_maskBitmap
= (WXHBITMAP
) ::CreateBitmap(
583 HDC srcDC
= ::CreateCompatibleDC(0);
584 ::SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
585 HDC destDC
= ::CreateCompatibleDC(0);
586 ::SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
588 // this is not very efficient, but I can't think
589 // of a better way of doing it
590 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
592 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
594 COLORREF col
= GetPixel(srcDC
, w
, h
);
595 if (col
== maskColour
)
597 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
601 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
605 ::SelectObject(srcDC
, 0);
607 ::SelectObject(destDC
, 0);
612 // ----------------------------------------------------------------------------
614 // ----------------------------------------------------------------------------
616 bool wxBitmapHandler::Create(wxGDIImage
*image
,
619 int width
, int height
, int depth
)
621 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
623 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
626 bool wxBitmapHandler::Load(wxGDIImage
*image
,
627 const wxString
& name
,
629 int width
, int height
)
631 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
633 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
636 bool wxBitmapHandler::Save(wxGDIImage
*image
,
637 const wxString
& name
,
640 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
642 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
645 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
646 void *WXUNUSED(data
),
649 int WXUNUSED(height
),
655 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
656 const wxString
& WXUNUSED(name
),
658 int WXUNUSED(desiredWidth
),
659 int WXUNUSED(desiredHeight
))
664 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
665 const wxString
& WXUNUSED(name
),
667 const wxPalette
*WXUNUSED(palette
))
672 // ----------------------------------------------------------------------------
674 // ----------------------------------------------------------------------------
676 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
677 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
679 unsigned long i
, headerSize
;
680 LPBITMAPINFO lpDIBheader
= NULL
;
681 LPPALETTEENTRY lpPe
= NULL
;
684 // Allocate space for a DIB header
685 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
686 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
687 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
689 GetPaletteEntries(hPal
, 0, 256, lpPe
);
691 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
693 // Fill in the static parts of the DIB header
694 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
695 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
696 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
697 lpDIBheader
->bmiHeader
.biPlanes
= 1;
699 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
700 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
701 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
702 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
703 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
706 // Initialize the DIB palette
707 for (i
= 0; i
< 256; i
++) {
708 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
709 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
710 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
711 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
714 *lpDIBHeader
= lpDIBheader
;
719 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)