]>
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 width
, int height
, int depth
)
234 wxBitmapRefData
*refData
= new wxBitmapRefData
;
237 refData
->m_width
= width
;
238 refData
->m_height
= height
;
239 refData
->m_depth
= depth
;
240 refData
->m_numColors
= 0;
241 refData
->m_selectedInto
= NULL
;
246 // we assume that it is in XBM format which is not quite the same as
247 // the format CreateBitmap() wants because the order of bytes in the
249 static const size_t bytesPerLine
= (width
+ 7) / 8;
250 static const size_t padding
= bytesPerLine
% 2;
251 static const size_t len
= height
* ( padding
+ bytesPerLine
);
252 data
= (char *)malloc(len
);
253 const char *src
= bits
;
256 for ( int rows
= 0; rows
< height
; rows
++ )
258 // note that offset cannot be size_t due to >= 0 test!
259 for ( int offset
= bytesPerLine
- 1; offset
>= 0; offset
-- )
261 *dst
++ = *(src
+ offset
);
272 // bits should already be in Windows standard format
273 data
= (char *)bits
; // const_cast is harmless
276 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
279 wxLogLastError("CreateBitmap");
287 SetHBITMAP((WXHBITMAP
)hbmp
);
291 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
294 (rect
.x
>= 0) && (rect
.y
>= 0) &&
295 (rect
.x
+rect
.width
<= GetWidth()) &&
296 (rect
.y
+rect
.height
<= GetHeight()),
297 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
299 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
300 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
303 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
304 HDC dcDst
= ::CreateCompatibleDC(NULL
);
305 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
306 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
307 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
309 // copy mask if there is one
312 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
314 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
315 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
316 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
318 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
322 SelectObject(dcDst
, NULL
);
323 SelectObject(dcSrc
, NULL
);
330 // Create from XPM data
331 wxBitmap::wxBitmap(char **data
, wxControl
*WXUNUSED(anItem
))
335 (void)Create((void *)data
, wxBITMAP_TYPE_XPM_DATA
, 0, 0, 0);
338 wxBitmap::wxBitmap(int w
, int h
, int d
)
342 (void)Create(w
, h
, d
);
345 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
349 (void)Create(data
, type
, width
, height
, depth
);
352 wxBitmap::wxBitmap(const wxString
& filename
, long type
)
356 LoadFile(filename
, (int)type
);
359 bool wxBitmap::Create(int w
, int h
, int d
)
363 m_refData
= new wxBitmapRefData
;
365 GetBitmapData()->m_width
= w
;
366 GetBitmapData()->m_height
= h
;
367 GetBitmapData()->m_depth
= d
;
373 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
376 wxLogLastError("CreateBitmap");
382 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
385 wxLogLastError("CreateCompatibleBitmap");
388 GetBitmapData()->m_depth
= wxDisplayDepth();
391 SetHBITMAP((WXHBITMAP
)hbmp
);
393 #if WXWIN_COMPATIBILITY_2
394 GetBitmapData()->m_ok
= hbmp
!= 0;
395 #endif // WXWIN_COMPATIBILITY_2
400 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
404 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
408 m_refData
= new wxBitmapRefData
;
410 return handler
->LoadFile(this, filename
, type
, -1, -1);
415 if ( !image
.LoadFile( filename
, type
) || !image
.Ok() )
418 *this = image
.ConvertToBitmap();
424 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
428 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
432 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
433 "type %d defined."), type
);
438 m_refData
= new wxBitmapRefData
;
440 return handler
->Create(this, data
, type
, width
, height
, depth
);
443 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
445 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
449 return handler
->SaveFile(this, filename
, type
, palette
);
453 // FIXME what about palette? shouldn't we use it?
454 wxImage
image( *this );
458 return image
.SaveFile( filename
, type
);
462 // ----------------------------------------------------------------------------
463 // wxBitmap accessors
464 // ----------------------------------------------------------------------------
466 void wxBitmap::SetQuality(int q
)
470 GetBitmapData()->m_quality
= q
;
473 #if WXWIN_COMPATIBILITY_2
474 void wxBitmap::SetOk(bool isOk
)
478 GetBitmapData()->m_ok
= isOk
;
480 #endif // WXWIN_COMPATIBILITY_2
482 void wxBitmap::SetPalette(const wxPalette
& palette
)
486 GetBitmapData()->m_bitmapPalette
= palette
;
489 void wxBitmap::SetMask(wxMask
*mask
)
493 GetBitmapData()->m_bitmapMask
= mask
;
496 // Creates a bitmap that matches the device context, from
497 // an arbitray bitmap. At present, the original bitmap must have an
498 // associated palette. TODO: use a default palette if no palette exists.
499 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
500 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
503 wxBitmap
tmpBitmap(this->GetWidth(), this->GetHeight(), dc
.GetDepth());
504 HPALETTE hPal
= (HPALETTE
) NULL
;
506 void *lpBits
= (void*) NULL
;
508 if( GetPalette() && GetPalette()->Ok() )
510 tmpBitmap
.SetPalette(*GetPalette());
511 memDC
.SelectObject(tmpBitmap
);
512 memDC
.SetPalette(*GetPalette());
513 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
517 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
519 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
520 tmpBitmap
.SetPalette( palette
);
521 memDC
.SelectObject(tmpBitmap
);
522 memDC
.SetPalette( palette
);
525 // set the height negative because in a DIB the order of the lines is
527 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
532 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
534 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
536 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
537 GetWidth(), GetHeight(),
538 0, 0, 0, GetHeight(),
539 lpBits
, lpDib
, DIB_RGB_COLORS
);
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
557 // Construct a mask from a bitmap and a colour indicating
558 // the transparent area
559 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
562 Create(bitmap
, colour
);
565 // Construct a mask from a bitmap and a palette index indicating
566 // the transparent area
567 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
570 Create(bitmap
, paletteIndex
);
573 // Construct a mask from a mono bitmap (copies the bitmap).
574 wxMask::wxMask(const wxBitmap
& bitmap
)
583 ::DeleteObject((HBITMAP
) m_maskBitmap
);
586 // Create a mask from a mono bitmap (copies the bitmap).
587 bool wxMask::Create(const wxBitmap
& bitmap
)
591 ::DeleteObject((HBITMAP
) m_maskBitmap
);
594 if (!bitmap
.Ok() || bitmap
.GetDepth() != 1)
598 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
603 HDC srcDC
= CreateCompatibleDC(0);
604 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
605 HDC destDC
= CreateCompatibleDC(0);
606 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
607 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
608 SelectObject(srcDC
, 0);
610 SelectObject(destDC
, 0);
615 // Create a mask from a bitmap and a palette index indicating
616 // the transparent area
617 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
621 ::DeleteObject((HBITMAP
) m_maskBitmap
);
624 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
626 unsigned char red
, green
, blue
;
627 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
629 wxColour
transparentColour(red
, green
, blue
);
630 return Create(bitmap
, transparentColour
);
636 // Create a mask from a bitmap and a colour indicating
637 // the transparent area
638 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
642 ::DeleteObject((HBITMAP
) m_maskBitmap
);
650 // scan the bitmap for the transparent colour and set
651 // the corresponding pixels in the mask to BLACK and
653 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
654 m_maskBitmap
= (WXHBITMAP
) ::CreateBitmap(
659 HDC srcDC
= ::CreateCompatibleDC(0);
660 ::SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
661 HDC destDC
= ::CreateCompatibleDC(0);
662 ::SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
664 // this is not very efficient, but I can't think
665 // of a better way of doing it
666 for (int w
= 0; w
< bitmap
.GetWidth(); w
++)
668 for (int h
= 0; h
< bitmap
.GetHeight(); h
++)
670 COLORREF col
= GetPixel(srcDC
, w
, h
);
671 if (col
== maskColour
)
673 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
677 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
681 ::SelectObject(srcDC
, 0);
683 ::SelectObject(destDC
, 0);
688 // ----------------------------------------------------------------------------
690 // ----------------------------------------------------------------------------
692 bool wxBitmapHandler::Create(wxGDIImage
*image
,
695 int width
, int height
, int depth
)
697 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
699 return bitmap
? Create(bitmap
, data
, width
, height
, depth
) : FALSE
;
702 bool wxBitmapHandler::Load(wxGDIImage
*image
,
703 const wxString
& name
,
705 int width
, int height
)
707 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
709 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
712 bool wxBitmapHandler::Save(wxGDIImage
*image
,
713 const wxString
& name
,
716 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
718 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
721 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
722 void *WXUNUSED(data
),
725 int WXUNUSED(height
),
731 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
732 const wxString
& WXUNUSED(name
),
734 int WXUNUSED(desiredWidth
),
735 int WXUNUSED(desiredHeight
))
740 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
741 const wxString
& WXUNUSED(name
),
743 const wxPalette
*WXUNUSED(palette
))
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
753 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
755 unsigned long i
, headerSize
;
756 LPBITMAPINFO lpDIBheader
= NULL
;
757 LPPALETTEENTRY lpPe
= NULL
;
760 // Allocate space for a DIB header
761 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
762 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
763 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
765 GetPaletteEntries(hPal
, 0, 256, lpPe
);
767 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
769 // Fill in the static parts of the DIB header
770 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
771 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
772 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
773 lpDIBheader
->bmiHeader
.biPlanes
= 1;
775 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
776 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
777 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
778 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
779 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
782 // Initialize the DIB palette
783 for (i
= 0; i
< 256; i
++) {
784 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
785 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
786 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
787 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
790 *lpDIBHeader
= lpDIBheader
;
795 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)