]>
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"
48 #include "wx/xpmdecod.h"
50 // missing from mingw32 header
52 #define CLR_INVALID ((COLORREF)-1)
53 #endif // no CLR_INVALID
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
60 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxBitmapRefData::wxBitmapRefData()
75 m_selectedInto
= NULL
;
78 m_hBitmap
= (WXHBITMAP
) NULL
;
81 void wxBitmapRefData::Free()
83 wxASSERT_MSG( !m_selectedInto
,
84 wxT("deleting bitmap still selected into wxMemoryDC") );
88 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
90 wxLogLastError(wxT("DeleteObject(hbitmap)"));
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // this function should be called from all wxBitmap ctors
103 void wxBitmap::Init()
105 // m_refData = NULL; done in the base class ctor
107 if ( wxTheBitmapList
)
108 wxTheBitmapList
->AddBitmap(this);
113 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
115 // it may be either HICON or HCURSOR
116 HICON hicon
= (HICON
)icon
.GetHandle();
119 if ( !::GetIconInfo(hicon
, &iconInfo
) )
121 wxLogLastError(wxT("GetIconInfo"));
126 wxBitmapRefData
*refData
= new wxBitmapRefData
;
129 int w
= icon
.GetWidth(),
130 h
= icon
.GetHeight();
132 refData
->m_width
= w
;
133 refData
->m_height
= h
;
134 refData
->m_depth
= wxDisplayDepth();
136 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
138 // the mask returned by GetIconInfo() is inversed compared to the usual
140 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
141 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
143 #if WXWIN_COMPATIBILITY_2
144 refData
->m_ok
= TRUE
;
145 #endif // WXWIN_COMPATIBILITY_2
152 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
160 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
164 return CopyFromIconOrCursor(cursor
);
168 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
175 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
176 // to create a bitmap from icon there - but using this way we won't have
179 int width
= icon
.GetWidth(),
180 height
= icon
.GetHeight();
182 // copy the icon to the bitmap
184 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
185 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
186 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
188 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
190 ::SelectObject(hdc
, hbmpOld
);
193 wxBitmapRefData
*refData
= new wxBitmapRefData
;
196 refData
->m_width
= width
;
197 refData
->m_height
= height
;
198 refData
->m_depth
= wxDisplayDepth();
200 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
202 #if WXWIN_COMPATIBILITY_2
203 refData
->m_ok
= TRUE
;
204 #endif // WXWIN_COMPATIBILITY_2
208 return CopyFromIconOrCursor(icon
);
209 #endif // Win16/Win32
212 wxBitmap::~wxBitmap()
215 wxTheBitmapList
->DeleteObject(this);
218 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
222 wxBitmapRefData
*refData
= new wxBitmapRefData
;
225 refData
->m_width
= width
;
226 refData
->m_height
= height
;
227 refData
->m_depth
= depth
;
228 refData
->m_numColors
= 0;
229 refData
->m_selectedInto
= NULL
;
234 // we assume that it is in XBM format which is not quite the same as
235 // the format CreateBitmap() wants because the order of bytes in the
237 static const size_t bytesPerLine
= (width
+ 7) / 8;
238 static const size_t padding
= bytesPerLine
% 2;
239 static const size_t len
= height
* ( padding
+ bytesPerLine
);
240 data
= (char *)malloc(len
);
241 const char *src
= bits
;
244 for ( int rows
= 0; rows
< height
; rows
++ )
246 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
248 unsigned char val
= *src
++;
249 unsigned char reversed
= 0;
251 for ( int bits
= 0; bits
< 8; bits
++)
254 reversed
|= (val
& 0x01);
266 // bits should already be in Windows standard format
267 data
= (char *)bits
; // const_cast is harmless
270 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
273 wxLogLastError(wxT("CreateBitmap"));
281 SetHBITMAP((WXHBITMAP
)hbmp
);
284 // Create from XPM data
285 bool wxBitmap::CreateFromXpm(const char **data
)
287 #if wxUSE_IMAGE && wxUSE_XPM
290 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
292 wxXPMDecoder decoder
;
293 wxImage img
= decoder
.ReadData(data
);
294 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
296 *this = wxBitmap(img
);
303 wxBitmap::wxBitmap(int w
, int h
, int d
)
307 (void)Create(w
, h
, d
);
310 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
314 (void)Create(data
, type
, width
, height
, depth
);
317 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
321 LoadFile(filename
, (int)type
);
324 bool wxBitmap::Create(int w
, int h
, int d
)
328 m_refData
= new wxBitmapRefData
;
330 GetBitmapData()->m_width
= w
;
331 GetBitmapData()->m_height
= h
;
332 GetBitmapData()->m_depth
= d
;
338 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
341 wxLogLastError(wxT("CreateBitmap"));
347 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
350 wxLogLastError(wxT("CreateCompatibleBitmap"));
353 GetBitmapData()->m_depth
= wxDisplayDepth();
356 SetHBITMAP((WXHBITMAP
)hbmp
);
358 #if WXWIN_COMPATIBILITY_2
359 GetBitmapData()->m_ok
= hbmp
!= 0;
360 #endif // WXWIN_COMPATIBILITY_2
365 // ----------------------------------------------------------------------------
366 // wxImage to/from conversions
367 // ----------------------------------------------------------------------------
371 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
373 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
375 m_refData
= new wxBitmapRefData();
377 // sizeLimit is the MS upper limit for the DIB size
379 int sizeLimit
= 1024*768*3;
381 int sizeLimit
= 0x7fff ;
384 // width and height of the device-dependent bitmap
385 int width
= image
.GetWidth();
386 int bmpHeight
= image
.GetHeight();
388 // calc the number of bytes per scanline and padding
389 int bytePerLine
= width
*3;
390 int sizeDWORD
= sizeof( DWORD
);
391 int lineBoundary
= bytePerLine
% sizeDWORD
;
393 if( lineBoundary
> 0 )
395 padding
= sizeDWORD
- lineBoundary
;
396 bytePerLine
+= padding
;
398 // calc the number of DIBs and heights of DIBs
401 int height
= sizeLimit
/bytePerLine
;
402 if( height
>= bmpHeight
)
406 numDIB
= bmpHeight
/ height
;
407 hRemain
= bmpHeight
% height
;
408 if( hRemain
>0 ) numDIB
++;
411 // set bitmap parameters
412 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
414 SetHeight( bmpHeight
);
415 if (depth
== -1) depth
= wxDisplayDepth();
418 // create a DIB header
419 int headersize
= sizeof(BITMAPINFOHEADER
);
420 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
421 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
422 // Fill in the DIB header
423 lpDIBh
->bmiHeader
.biSize
= headersize
;
424 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
425 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
426 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
427 // the general formula for biSizeImage:
428 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
429 lpDIBh
->bmiHeader
.biPlanes
= 1;
430 lpDIBh
->bmiHeader
.biBitCount
= 24;
431 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
432 lpDIBh
->bmiHeader
.biClrUsed
= 0;
433 // These seem not really needed for our purpose here.
434 lpDIBh
->bmiHeader
.biClrImportant
= 0;
435 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
436 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
437 // memory for DIB data
438 unsigned char *lpBits
;
439 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
442 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
447 // create and set the device-dependent bitmap
448 HDC hdc
= ::GetDC(NULL
);
449 HDC memdc
= ::CreateCompatibleDC( hdc
);
451 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
452 ::SelectObject( memdc
, hbitmap
);
454 HPALETTE hOldPalette
= 0;
455 if (image
.GetPalette().Ok())
457 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
458 ::RealizePalette(memdc
);
461 // copy image data into DIB data and then into DDB (in a loop)
462 unsigned char *data
= image
.GetData();
465 unsigned char *ptdata
= data
;
466 unsigned char *ptbits
;
468 for( n
=0; n
<numDIB
; n
++ )
470 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
472 // redefine height and size of the (possibly) last smaller DIB
473 // memory is not reallocated
475 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
476 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
480 for( j
=0; j
<height
; j
++ )
482 for( i
=0; i
<width
; i
++ )
484 *(ptbits
++) = *(ptdata
+2);
485 *(ptbits
++) = *(ptdata
+1);
486 *(ptbits
++) = *(ptdata
);
489 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
491 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
492 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
494 // if numDIB = 1, lines below can also be used
495 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
496 // The above line is equivalent to the following two lines.
497 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
498 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
499 // or the following lines
500 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
501 // HDC memdc = ::CreateCompatibleDC( hdc );
502 // ::SelectObject( memdc, hbitmap);
503 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
504 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
505 // ::SelectObject( memdc, 0 );
506 // ::DeleteDC( memdc );
508 SetHBITMAP( (WXHBITMAP
) hbitmap
);
511 SelectPalette(memdc
, hOldPalette
, FALSE
);
513 // similarly, created an mono-bitmap for the possible mask
514 if( image
.HasMask() )
516 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
517 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
518 if( numDIB
== 1 ) height
= bmpHeight
;
519 else height
= sizeLimit
/bytePerLine
;
520 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
521 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
523 unsigned char r
= image
.GetMaskRed();
524 unsigned char g
= image
.GetMaskGreen();
525 unsigned char b
= image
.GetMaskBlue();
526 unsigned char zero
= 0, one
= 255;
528 for( n
=0; n
<numDIB
; n
++ )
530 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
532 // redefine height and size of the (possibly) last smaller DIB
533 // memory is not reallocated
535 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
536 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
539 for( int j
=0; j
<height
; j
++ )
541 for(i
=0; i
<width
; i
++ )
543 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
544 unsigned char cr
= (*(ptdata
++)) ;
545 unsigned char cg
= (*(ptdata
++)) ;
546 unsigned char cb
= (*(ptdata
++)) ;
548 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
561 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
563 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
564 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
567 // create a wxMask object
568 wxMask
*mask
= new wxMask();
569 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
571 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
572 /* The following can also be used but is slow to run
573 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
574 wxMask *mask = new wxMask( *this, colour );
578 ::SelectObject( memdc
, hbmpOld
);
581 // free allocated resources
583 ::ReleaseDC(NULL
, hdc
);
587 #if WXWIN_COMPATIBILITY_2
588 // check the wxBitmap object
589 GetBitmapData()->SetOk();
590 #endif // WXWIN_COMPATIBILITY_2
592 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
597 wxImage
wxBitmap::ConvertToImage() const
601 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
603 // create an wxImage object
604 int width
= GetWidth();
605 int height
= GetHeight();
606 image
.Create( width
, height
);
607 unsigned char *data
= image
.GetData();
610 wxFAIL_MSG( wxT("could not allocate data for image") );
614 // calc the number of bytes per scanline and padding in the DIB
615 int bytePerLine
= width
*3;
616 int sizeDWORD
= sizeof( DWORD
);
617 int lineBoundary
= bytePerLine
% sizeDWORD
;
619 if( lineBoundary
> 0 )
621 padding
= sizeDWORD
- lineBoundary
;
622 bytePerLine
+= padding
;
625 // create a DIB header
626 int headersize
= sizeof(BITMAPINFOHEADER
);
627 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
630 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
634 // Fill in the DIB header
635 lpDIBh
->bmiHeader
.biSize
= headersize
;
636 lpDIBh
->bmiHeader
.biWidth
= width
;
637 lpDIBh
->bmiHeader
.biHeight
= -height
;
638 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
639 lpDIBh
->bmiHeader
.biPlanes
= 1;
640 lpDIBh
->bmiHeader
.biBitCount
= 24;
641 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
642 lpDIBh
->bmiHeader
.biClrUsed
= 0;
643 // These seem not really needed for our purpose here.
644 lpDIBh
->bmiHeader
.biClrImportant
= 0;
645 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
646 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
647 // memory for DIB data
648 unsigned char *lpBits
;
649 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
652 wxFAIL_MSG( wxT("could not allocate data for DIB") );
658 // copy data from the device-dependent bitmap to the DIB
659 HDC hdc
= ::GetDC(NULL
);
661 hbitmap
= (HBITMAP
) GetHBITMAP();
662 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
664 // copy DIB data into the wxImage object
666 unsigned char *ptdata
= data
;
667 unsigned char *ptbits
= lpBits
;
668 for( i
=0; i
<height
; i
++ )
670 for( j
=0; j
<width
; j
++ )
672 *(ptdata
++) = *(ptbits
+2);
673 *(ptdata
++) = *(ptbits
+1);
674 *(ptdata
++) = *(ptbits
);
680 // similarly, set data according to the possible mask bitmap
681 if( GetMask() && GetMask()->GetMaskBitmap() )
683 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
684 // memory DC created, color set, data copied, and memory DC deleted
685 HDC memdc
= ::CreateCompatibleDC( hdc
);
686 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
687 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
688 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
690 // background color set to RGB(16,16,16) in consistent with wxGTK
691 unsigned char r
=16, g
=16, b
=16;
694 for( i
=0; i
<height
; i
++ )
696 for( j
=0; j
<width
; j
++ )
710 image
.SetMaskColour( r
, g
, b
);
711 image
.SetMask( TRUE
);
715 image
.SetMask( FALSE
);
717 // free allocated resources
718 ::ReleaseDC(NULL
, hdc
);
725 #endif // wxUSE_IMAGE
727 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
731 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
735 m_refData
= new wxBitmapRefData
;
737 return handler
->LoadFile(this, filename
, type
, -1, -1);
743 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
745 *this = image
.ConvertToBitmap();
750 #endif // wxUSE_IMAGE
755 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
759 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
763 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
768 m_refData
= new wxBitmapRefData
;
770 return handler
->Create(this, data
, type
, width
, height
, depth
);
773 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
775 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
779 return handler
->SaveFile(this, filename
, type
, palette
);
784 // FIXME what about palette? shouldn't we use it?
785 wxImage
image( *this );
788 return image
.SaveFile(filename
, type
);
791 #endif // wxUSE_IMAGE
796 // ----------------------------------------------------------------------------
797 // sub bitmap extraction
798 // ----------------------------------------------------------------------------
800 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
803 (rect
.x
>= 0) && (rect
.y
>= 0) &&
804 (rect
.x
+rect
.width
<= GetWidth()) &&
805 (rect
.y
+rect
.height
<= GetHeight()),
806 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
808 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
809 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
812 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
813 HDC dcDst
= ::CreateCompatibleDC(NULL
);
814 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
815 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
816 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
818 // copy mask if there is one
821 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
823 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
824 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
825 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
827 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
831 SelectObject(dcDst
, NULL
);
832 SelectObject(dcSrc
, NULL
);
839 // ----------------------------------------------------------------------------
840 // wxBitmap accessors
841 // ----------------------------------------------------------------------------
843 void wxBitmap::SetQuality(int q
)
847 GetBitmapData()->m_quality
= q
;
850 #if WXWIN_COMPATIBILITY_2
851 void wxBitmap::SetOk(bool isOk
)
855 GetBitmapData()->m_ok
= isOk
;
857 #endif // WXWIN_COMPATIBILITY_2
859 void wxBitmap::SetPalette(const wxPalette
& palette
)
863 GetBitmapData()->m_bitmapPalette
= palette
;
866 void wxBitmap::SetMask(wxMask
*mask
)
870 GetBitmapData()->m_bitmapMask
= mask
;
873 // Creates a bitmap that matches the device context, from
874 // an arbitray bitmap. At present, the original bitmap must have an
875 // associated palette. TODO: use a default palette if no palette exists.
876 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
877 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
880 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
881 HPALETTE hPal
= (HPALETTE
) NULL
;
883 void *lpBits
= (void*) NULL
;
885 if( GetPalette() && GetPalette()->Ok() )
887 tmpBitmap
.SetPalette(*GetPalette());
888 memDC
.SelectObject(tmpBitmap
);
889 memDC
.SetPalette(*GetPalette());
890 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
894 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
896 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
897 tmpBitmap
.SetPalette( palette
);
898 memDC
.SelectObject(tmpBitmap
);
899 memDC
.SetPalette( palette
);
902 // set the height negative because in a DIB the order of the lines is
904 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
909 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
911 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
913 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
914 GetWidth(), GetHeight(),
915 0, 0, 0, GetHeight(),
916 lpBits
, lpDib
, DIB_RGB_COLORS
);
925 // ----------------------------------------------------------------------------
927 // ----------------------------------------------------------------------------
934 // Construct a mask from a bitmap and a colour indicating
935 // the transparent area
936 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
939 Create(bitmap
, colour
);
942 // Construct a mask from a bitmap and a palette index indicating
943 // the transparent area
944 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
947 Create(bitmap
, paletteIndex
);
950 // Construct a mask from a mono bitmap (copies the bitmap).
951 wxMask::wxMask(const wxBitmap
& bitmap
)
960 ::DeleteObject((HBITMAP
) m_maskBitmap
);
963 // Create a mask from a mono bitmap (copies the bitmap).
964 bool wxMask::Create(const wxBitmap
& bitmap
)
966 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
967 _T("can't create mask from invalid or not monochrome bitmap") );
971 ::DeleteObject((HBITMAP
) m_maskBitmap
);
975 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
980 HDC srcDC
= CreateCompatibleDC(0);
981 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
982 HDC destDC
= CreateCompatibleDC(0);
983 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
984 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
985 SelectObject(srcDC
, 0);
987 SelectObject(destDC
, 0);
992 // Create a mask from a bitmap and a palette index indicating
993 // the transparent area
994 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
998 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1001 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1003 unsigned char red
, green
, blue
;
1004 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1006 wxColour
transparentColour(red
, green
, blue
);
1007 return Create(bitmap
, transparentColour
);
1013 // Create a mask from a bitmap and a colour indicating
1014 // the transparent area
1015 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1017 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1021 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1025 int width
= bitmap
.GetWidth(),
1026 height
= bitmap
.GetHeight();
1028 // scan the bitmap for the transparent colour and set the corresponding
1029 // pixels in the mask to BLACK and the rest to WHITE
1030 COLORREF maskColour
= wxColourToRGB(colour
);
1031 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1033 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1034 HDC destDC
= ::CreateCompatibleDC(NULL
);
1035 if ( !srcDC
|| !destDC
)
1037 wxLogLastError(wxT("CreateCompatibleDC"));
1042 // SelectObject() will fail
1043 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1044 _T("bitmap can't be selected in another DC") );
1046 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1049 wxLogLastError(wxT("SelectObject"));
1054 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1057 wxLogLastError(wxT("SelectObject"));
1062 // this is not very efficient, but I can't think of a better way of doing
1064 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1066 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1068 COLORREF col
= GetPixel(srcDC
, w
, h
);
1069 if ( col
== CLR_INVALID
)
1071 wxLogLastError(wxT("GetPixel"));
1073 // doesn't make sense to continue
1079 if ( col
== maskColour
)
1081 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1085 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1090 ::SelectObject(srcDC
, hbmpSrcOld
);
1092 ::SelectObject(destDC
, hbmpDstOld
);
1098 // ----------------------------------------------------------------------------
1100 // ----------------------------------------------------------------------------
1102 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1105 int width
, int height
, int depth
)
1107 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1109 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1112 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1113 const wxString
& name
,
1115 int width
, int height
)
1117 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1119 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1122 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1123 const wxString
& name
,
1126 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1128 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1131 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1132 void *WXUNUSED(data
),
1133 long WXUNUSED(type
),
1134 int WXUNUSED(width
),
1135 int WXUNUSED(height
),
1136 int WXUNUSED(depth
))
1141 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1142 const wxString
& WXUNUSED(name
),
1143 long WXUNUSED(type
),
1144 int WXUNUSED(desiredWidth
),
1145 int WXUNUSED(desiredHeight
))
1150 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1151 const wxString
& WXUNUSED(name
),
1153 const wxPalette
*WXUNUSED(palette
))
1158 // ----------------------------------------------------------------------------
1160 // ----------------------------------------------------------------------------
1162 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1163 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1165 unsigned long i
, headerSize
;
1166 LPBITMAPINFO lpDIBheader
= NULL
;
1167 LPPALETTEENTRY lpPe
= NULL
;
1170 // Allocate space for a DIB header
1171 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1172 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1173 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1175 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1177 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1179 // Fill in the static parts of the DIB header
1180 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1181 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1182 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1183 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1185 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1186 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1187 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1188 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1189 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1192 // Initialize the DIB palette
1193 for (i
= 0; i
< 256; i
++) {
1194 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1195 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1196 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1197 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1200 *lpDIBHeader
= lpDIBheader
;
1205 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1210 // ----------------------------------------------------------------------------
1211 // other helper functions
1212 // ----------------------------------------------------------------------------
1214 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1216 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1218 // get width/height from the bitmap if not given
1222 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1227 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1228 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1229 if ( !hdcSrc
|| !hdcDst
)
1231 wxLogLastError(wxT("CreateCompatibleDC"));
1234 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1237 wxLogLastError(wxT("CreateBitmap"));
1240 ::SelectObject(hdcSrc
, hbmpMask
);
1241 ::SelectObject(hdcDst
, hbmpInvMask
);
1242 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1246 wxLogLastError(wxT("BitBlt"));