]>
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 #if !defined(__WXMICROWIN__)
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
63 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 wxBitmapRefData::wxBitmapRefData()
78 m_selectedInto
= NULL
;
81 m_hBitmap
= (WXHBITMAP
) NULL
;
84 void wxBitmapRefData::Free()
86 wxASSERT_MSG( !m_selectedInto
,
87 wxT("deleting bitmap still selected into wxMemoryDC") );
91 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
93 wxLogLastError(wxT("DeleteObject(hbitmap)"));
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // this function should be called from all wxBitmap ctors
106 void wxBitmap::Init()
108 // m_refData = NULL; done in the base class ctor
110 if ( wxTheBitmapList
)
111 wxTheBitmapList
->AddBitmap(this);
116 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
118 #ifndef __WXMICROWIN__
119 // it may be either HICON or HCURSOR
120 HICON hicon
= (HICON
)icon
.GetHandle();
123 if ( !::GetIconInfo(hicon
, &iconInfo
) )
125 wxLogLastError(wxT("GetIconInfo"));
130 wxBitmapRefData
*refData
= new wxBitmapRefData
;
133 int w
= icon
.GetWidth(),
134 h
= icon
.GetHeight();
136 refData
->m_width
= w
;
137 refData
->m_height
= h
;
138 refData
->m_depth
= wxDisplayDepth();
140 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
142 // the mask returned by GetIconInfo() is inversed compared to the usual
144 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
145 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
147 #if WXWIN_COMPATIBILITY_2
148 refData
->m_ok
= TRUE
;
149 #endif // WXWIN_COMPATIBILITY_2
159 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
167 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
171 return CopyFromIconOrCursor(cursor
);
175 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
182 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
183 // to create a bitmap from icon there - but using this way we won't have
186 int width
= icon
.GetWidth(),
187 height
= icon
.GetHeight();
189 // copy the icon to the bitmap
191 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
192 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
193 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
195 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
197 ::SelectObject(hdc
, hbmpOld
);
200 wxBitmapRefData
*refData
= new wxBitmapRefData
;
203 refData
->m_width
= width
;
204 refData
->m_height
= height
;
205 refData
->m_depth
= wxDisplayDepth();
207 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
209 #if WXWIN_COMPATIBILITY_2
210 refData
->m_ok
= TRUE
;
211 #endif // WXWIN_COMPATIBILITY_2
215 return CopyFromIconOrCursor(icon
);
216 #endif // Win16/Win32
219 wxBitmap::~wxBitmap()
222 wxTheBitmapList
->DeleteObject(this);
225 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
229 #ifndef __WXMICROWIN__
230 wxBitmapRefData
*refData
= new wxBitmapRefData
;
233 refData
->m_width
= width
;
234 refData
->m_height
= height
;
235 refData
->m_depth
= depth
;
236 refData
->m_numColors
= 0;
237 refData
->m_selectedInto
= NULL
;
242 // we assume that it is in XBM format which is not quite the same as
243 // the format CreateBitmap() wants because the order of bytes in the
245 const size_t bytesPerLine
= (width
+ 7) / 8;
246 const size_t padding
= bytesPerLine
% 2;
247 const size_t len
= height
* ( padding
+ bytesPerLine
);
248 data
= (char *)malloc(len
);
249 const char *src
= bits
;
252 for ( int rows
= 0; rows
< height
; rows
++ )
254 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
256 unsigned char val
= *src
++;
257 unsigned char reversed
= 0;
259 for ( int bits
= 0; bits
< 8; bits
++)
262 reversed
|= (val
& 0x01);
274 // bits should already be in Windows standard format
275 data
= (char *)bits
; // const_cast is harmless
278 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
281 wxLogLastError(wxT("CreateBitmap"));
289 SetHBITMAP((WXHBITMAP
)hbmp
);
293 // Create from XPM data
294 bool wxBitmap::CreateFromXpm(const char **data
)
296 #if wxUSE_IMAGE && wxUSE_XPM
299 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
301 wxXPMDecoder decoder
;
302 wxImage img
= decoder
.ReadData(data
);
303 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
305 *this = wxBitmap(img
);
312 wxBitmap::wxBitmap(int w
, int h
, int d
)
316 (void)Create(w
, h
, d
);
319 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
323 (void)Create(data
, type
, width
, height
, depth
);
326 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
330 LoadFile(filename
, (int)type
);
333 bool wxBitmap::Create(int w
, int h
, int d
)
335 #ifndef __WXMICROWIN__
338 m_refData
= new wxBitmapRefData
;
340 GetBitmapData()->m_width
= w
;
341 GetBitmapData()->m_height
= h
;
342 GetBitmapData()->m_depth
= d
;
348 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
351 wxLogLastError(wxT("CreateBitmap"));
357 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
360 wxLogLastError(wxT("CreateCompatibleBitmap"));
363 GetBitmapData()->m_depth
= wxDisplayDepth();
366 SetHBITMAP((WXHBITMAP
)hbmp
);
368 #if WXWIN_COMPATIBILITY_2
369 GetBitmapData()->m_ok
= hbmp
!= 0;
370 #endif // WXWIN_COMPATIBILITY_2
377 // ----------------------------------------------------------------------------
378 // wxImage to/from conversions
379 // ----------------------------------------------------------------------------
383 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
385 #ifdef __WXMICROWIN__
389 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
391 m_refData
= new wxBitmapRefData();
393 // sizeLimit is the MS upper limit for the DIB size
395 int sizeLimit
= 1024*768*3;
397 int sizeLimit
= 0x7fff ;
400 // width and height of the device-dependent bitmap
401 int width
= image
.GetWidth();
402 int bmpHeight
= image
.GetHeight();
404 // calc the number of bytes per scanline and padding
405 int bytePerLine
= width
*3;
406 int sizeDWORD
= sizeof( DWORD
);
407 int lineBoundary
= bytePerLine
% sizeDWORD
;
409 if( lineBoundary
> 0 )
411 padding
= sizeDWORD
- lineBoundary
;
412 bytePerLine
+= padding
;
414 // calc the number of DIBs and heights of DIBs
417 int height
= sizeLimit
/bytePerLine
;
418 if( height
>= bmpHeight
)
422 numDIB
= bmpHeight
/ height
;
423 hRemain
= bmpHeight
% height
;
424 if( hRemain
>0 ) numDIB
++;
427 // set bitmap parameters
428 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
430 SetHeight( bmpHeight
);
431 if (depth
== -1) depth
= wxDisplayDepth();
434 // create a DIB header
435 int headersize
= sizeof(BITMAPINFOHEADER
);
436 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
437 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
438 // Fill in the DIB header
439 lpDIBh
->bmiHeader
.biSize
= headersize
;
440 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
441 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
442 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
443 // the general formula for biSizeImage:
444 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
445 lpDIBh
->bmiHeader
.biPlanes
= 1;
446 lpDIBh
->bmiHeader
.biBitCount
= 24;
447 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
448 lpDIBh
->bmiHeader
.biClrUsed
= 0;
449 // These seem not really needed for our purpose here.
450 lpDIBh
->bmiHeader
.biClrImportant
= 0;
451 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
452 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
453 // memory for DIB data
454 unsigned char *lpBits
;
455 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
458 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
463 // create and set the device-dependent bitmap
464 HDC hdc
= ::GetDC(NULL
);
465 HDC memdc
= ::CreateCompatibleDC( hdc
);
467 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
468 ::SelectObject( memdc
, hbitmap
);
470 HPALETTE hOldPalette
= 0;
471 if (image
.GetPalette().Ok())
473 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
474 ::RealizePalette(memdc
);
477 // copy image data into DIB data and then into DDB (in a loop)
478 unsigned char *data
= image
.GetData();
481 unsigned char *ptdata
= data
;
482 unsigned char *ptbits
;
484 for( n
=0; n
<numDIB
; n
++ )
486 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
488 // redefine height and size of the (possibly) last smaller DIB
489 // memory is not reallocated
491 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
492 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
496 for( j
=0; j
<height
; j
++ )
498 for( i
=0; i
<width
; i
++ )
500 *(ptbits
++) = *(ptdata
+2);
501 *(ptbits
++) = *(ptdata
+1);
502 *(ptbits
++) = *(ptdata
);
505 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
507 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
508 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
510 // if numDIB = 1, lines below can also be used
511 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
512 // The above line is equivalent to the following two lines.
513 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
514 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
515 // or the following lines
516 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
517 // HDC memdc = ::CreateCompatibleDC( hdc );
518 // ::SelectObject( memdc, hbitmap);
519 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
520 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
521 // ::SelectObject( memdc, 0 );
522 // ::DeleteDC( memdc );
524 SetHBITMAP( (WXHBITMAP
) hbitmap
);
527 SelectPalette(memdc
, hOldPalette
, FALSE
);
529 // similarly, created an mono-bitmap for the possible mask
530 if( image
.HasMask() )
532 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
533 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
534 if( numDIB
== 1 ) height
= bmpHeight
;
535 else height
= sizeLimit
/bytePerLine
;
536 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
537 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
539 unsigned char r
= image
.GetMaskRed();
540 unsigned char g
= image
.GetMaskGreen();
541 unsigned char b
= image
.GetMaskBlue();
542 unsigned char zero
= 0, one
= 255;
544 for( n
=0; n
<numDIB
; n
++ )
546 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
548 // redefine height and size of the (possibly) last smaller DIB
549 // memory is not reallocated
551 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
552 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
555 for( int j
=0; j
<height
; j
++ )
557 for(i
=0; i
<width
; i
++ )
559 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
560 unsigned char cr
= (*(ptdata
++)) ;
561 unsigned char cg
= (*(ptdata
++)) ;
562 unsigned char cb
= (*(ptdata
++)) ;
564 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
577 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
579 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
580 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
583 // create a wxMask object
584 wxMask
*mask
= new wxMask();
585 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
587 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
588 /* The following can also be used but is slow to run
589 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
590 wxMask *mask = new wxMask( *this, colour );
594 ::SelectObject( memdc
, hbmpOld
);
597 // free allocated resources
599 ::ReleaseDC(NULL
, hdc
);
603 #if WXWIN_COMPATIBILITY_2
604 // check the wxBitmap object
605 GetBitmapData()->SetOk();
606 #endif // WXWIN_COMPATIBILITY_2
608 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
614 wxImage
wxBitmap::ConvertToImage() const
616 #ifdef __WXMICROWIN__
622 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
624 // create an wxImage object
625 int width
= GetWidth();
626 int height
= GetHeight();
627 image
.Create( width
, height
);
628 unsigned char *data
= image
.GetData();
631 wxFAIL_MSG( wxT("could not allocate data for image") );
635 // calc the number of bytes per scanline and padding in the DIB
636 int bytePerLine
= width
*3;
637 int sizeDWORD
= sizeof( DWORD
);
638 int lineBoundary
= bytePerLine
% sizeDWORD
;
640 if( lineBoundary
> 0 )
642 padding
= sizeDWORD
- lineBoundary
;
643 bytePerLine
+= padding
;
646 // create a DIB header
647 int headersize
= sizeof(BITMAPINFOHEADER
);
648 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
651 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
655 // Fill in the DIB header
656 lpDIBh
->bmiHeader
.biSize
= headersize
;
657 lpDIBh
->bmiHeader
.biWidth
= width
;
658 lpDIBh
->bmiHeader
.biHeight
= -height
;
659 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
660 lpDIBh
->bmiHeader
.biPlanes
= 1;
661 lpDIBh
->bmiHeader
.biBitCount
= 24;
662 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
663 lpDIBh
->bmiHeader
.biClrUsed
= 0;
664 // These seem not really needed for our purpose here.
665 lpDIBh
->bmiHeader
.biClrImportant
= 0;
666 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
667 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
668 // memory for DIB data
669 unsigned char *lpBits
;
670 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
673 wxFAIL_MSG( wxT("could not allocate data for DIB") );
679 // copy data from the device-dependent bitmap to the DIB
680 HDC hdc
= ::GetDC(NULL
);
682 hbitmap
= (HBITMAP
) GetHBITMAP();
683 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
685 // copy DIB data into the wxImage object
687 unsigned char *ptdata
= data
;
688 unsigned char *ptbits
= lpBits
;
689 for( i
=0; i
<height
; i
++ )
691 for( j
=0; j
<width
; j
++ )
693 *(ptdata
++) = *(ptbits
+2);
694 *(ptdata
++) = *(ptbits
+1);
695 *(ptdata
++) = *(ptbits
);
701 // similarly, set data according to the possible mask bitmap
702 if( GetMask() && GetMask()->GetMaskBitmap() )
704 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
705 // memory DC created, color set, data copied, and memory DC deleted
706 HDC memdc
= ::CreateCompatibleDC( hdc
);
707 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
708 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
709 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
711 // background color set to RGB(16,16,16) in consistent with wxGTK
712 unsigned char r
=16, g
=16, b
=16;
715 for( i
=0; i
<height
; i
++ )
717 for( j
=0; j
<width
; j
++ )
731 image
.SetMaskColour( r
, g
, b
);
732 image
.SetMask( TRUE
);
736 image
.SetMask( FALSE
);
738 // free allocated resources
739 ::ReleaseDC(NULL
, hdc
);
747 #endif // wxUSE_IMAGE
749 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
753 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
757 m_refData
= new wxBitmapRefData
;
759 return handler
->LoadFile(this, filename
, type
, -1, -1);
765 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
767 *this = image
.ConvertToBitmap();
772 #endif // wxUSE_IMAGE
777 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
781 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
785 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
790 m_refData
= new wxBitmapRefData
;
792 return handler
->Create(this, data
, type
, width
, height
, depth
);
795 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
797 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
801 return handler
->SaveFile(this, filename
, type
, palette
);
806 // FIXME what about palette? shouldn't we use it?
807 wxImage
image( *this );
810 return image
.SaveFile(filename
, type
);
813 #endif // wxUSE_IMAGE
818 // ----------------------------------------------------------------------------
819 // sub bitmap extraction
820 // ----------------------------------------------------------------------------
822 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
824 #ifndef __WXMICROWIN__
826 (rect
.x
>= 0) && (rect
.y
>= 0) &&
827 (rect
.x
+rect
.width
<= GetWidth()) &&
828 (rect
.y
+rect
.height
<= GetHeight()),
829 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
831 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
832 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
835 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
836 HDC dcDst
= ::CreateCompatibleDC(NULL
);
837 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
838 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
839 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
841 // copy mask if there is one
844 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
846 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
847 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
848 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
850 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
854 SelectObject(dcDst
, NULL
);
855 SelectObject(dcSrc
, NULL
);
865 // ----------------------------------------------------------------------------
866 // wxBitmap accessors
867 // ----------------------------------------------------------------------------
869 void wxBitmap::SetQuality(int q
)
873 GetBitmapData()->m_quality
= q
;
876 #if WXWIN_COMPATIBILITY_2
877 void wxBitmap::SetOk(bool isOk
)
881 GetBitmapData()->m_ok
= isOk
;
883 #endif // WXWIN_COMPATIBILITY_2
885 void wxBitmap::SetPalette(const wxPalette
& palette
)
889 GetBitmapData()->m_bitmapPalette
= palette
;
892 void wxBitmap::SetMask(wxMask
*mask
)
896 GetBitmapData()->m_bitmapMask
= mask
;
899 // Creates a bitmap that matches the device context, from
900 // an arbitray bitmap. At present, the original bitmap must have an
901 // associated palette. TODO: use a default palette if no palette exists.
902 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
903 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
905 #ifdef __WXMICROWIN__
909 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
910 HPALETTE hPal
= (HPALETTE
) NULL
;
912 void *lpBits
= (void*) NULL
;
914 if( GetPalette() && GetPalette()->Ok() )
916 tmpBitmap
.SetPalette(*GetPalette());
917 memDC
.SelectObject(tmpBitmap
);
918 memDC
.SetPalette(*GetPalette());
919 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
923 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
925 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
926 tmpBitmap
.SetPalette( palette
);
927 memDC
.SelectObject(tmpBitmap
);
928 memDC
.SetPalette( palette
);
931 // set the height negative because in a DIB the order of the lines is
933 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
938 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
940 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
942 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
943 GetWidth(), GetHeight(),
944 0, 0, 0, GetHeight(),
945 lpBits
, lpDib
, DIB_RGB_COLORS
);
955 // ----------------------------------------------------------------------------
957 // ----------------------------------------------------------------------------
964 // Construct a mask from a bitmap and a colour indicating
965 // the transparent area
966 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
969 Create(bitmap
, colour
);
972 // Construct a mask from a bitmap and a palette index indicating
973 // the transparent area
974 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
977 Create(bitmap
, paletteIndex
);
980 // Construct a mask from a mono bitmap (copies the bitmap).
981 wxMask::wxMask(const wxBitmap
& bitmap
)
990 ::DeleteObject((HBITMAP
) m_maskBitmap
);
993 // Create a mask from a mono bitmap (copies the bitmap).
994 bool wxMask::Create(const wxBitmap
& bitmap
)
996 #ifndef __WXMICROWIN__
997 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
998 _T("can't create mask from invalid or not monochrome bitmap") );
1002 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1006 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1011 HDC srcDC
= CreateCompatibleDC(0);
1012 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1013 HDC destDC
= CreateCompatibleDC(0);
1014 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1015 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1016 SelectObject(srcDC
, 0);
1018 SelectObject(destDC
, 0);
1026 // Create a mask from a bitmap and a palette index indicating
1027 // the transparent area
1028 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1032 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1035 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1037 unsigned char red
, green
, blue
;
1038 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1040 wxColour
transparentColour(red
, green
, blue
);
1041 return Create(bitmap
, transparentColour
);
1047 // Create a mask from a bitmap and a colour indicating
1048 // the transparent area
1049 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1051 #ifndef __WXMICROWIN__
1052 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1056 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1060 int width
= bitmap
.GetWidth(),
1061 height
= bitmap
.GetHeight();
1063 // scan the bitmap for the transparent colour and set the corresponding
1064 // pixels in the mask to BLACK and the rest to WHITE
1065 COLORREF maskColour
= wxColourToRGB(colour
);
1066 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1068 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1069 HDC destDC
= ::CreateCompatibleDC(NULL
);
1070 if ( !srcDC
|| !destDC
)
1072 wxLogLastError(wxT("CreateCompatibleDC"));
1077 // SelectObject() will fail
1078 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1079 _T("bitmap can't be selected in another DC") );
1081 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1084 wxLogLastError(wxT("SelectObject"));
1089 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1092 wxLogLastError(wxT("SelectObject"));
1097 // this is not very efficient, but I can't think of a better way of doing
1099 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1101 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1103 COLORREF col
= GetPixel(srcDC
, w
, h
);
1104 if ( col
== CLR_INVALID
)
1106 wxLogLastError(wxT("GetPixel"));
1108 // doesn't make sense to continue
1114 if ( col
== maskColour
)
1116 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1120 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1125 ::SelectObject(srcDC
, hbmpSrcOld
);
1127 ::SelectObject(destDC
, hbmpDstOld
);
1136 // ----------------------------------------------------------------------------
1138 // ----------------------------------------------------------------------------
1140 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1143 int width
, int height
, int depth
)
1145 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1147 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1150 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1151 const wxString
& name
,
1153 int width
, int height
)
1155 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1157 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1160 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1161 const wxString
& name
,
1164 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1166 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1169 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1170 void *WXUNUSED(data
),
1171 long WXUNUSED(type
),
1172 int WXUNUSED(width
),
1173 int WXUNUSED(height
),
1174 int WXUNUSED(depth
))
1179 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1180 const wxString
& WXUNUSED(name
),
1181 long WXUNUSED(type
),
1182 int WXUNUSED(desiredWidth
),
1183 int WXUNUSED(desiredHeight
))
1188 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1189 const wxString
& WXUNUSED(name
),
1191 const wxPalette
*WXUNUSED(palette
))
1196 // ----------------------------------------------------------------------------
1198 // ----------------------------------------------------------------------------
1200 #ifndef __WXMICROWIN__
1201 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1202 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1204 unsigned long i
, headerSize
;
1205 LPBITMAPINFO lpDIBheader
= NULL
;
1206 LPPALETTEENTRY lpPe
= NULL
;
1209 // Allocate space for a DIB header
1210 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1211 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1212 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1214 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1216 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1218 // Fill in the static parts of the DIB header
1219 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1220 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1221 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1222 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1224 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1225 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1226 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1227 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1228 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1231 // Initialize the DIB palette
1232 for (i
= 0; i
< 256; i
++) {
1233 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1234 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1235 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1236 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1239 *lpDIBHeader
= lpDIBheader
;
1244 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1250 // ----------------------------------------------------------------------------
1251 // other helper functions
1252 // ----------------------------------------------------------------------------
1254 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1256 #ifndef __WXMICROWIN__
1257 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1259 // get width/height from the bitmap if not given
1263 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1268 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1269 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1270 if ( !hdcSrc
|| !hdcDst
)
1272 wxLogLastError(wxT("CreateCompatibleDC"));
1275 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1278 wxLogLastError(wxT("CreateBitmap"));
1281 ::SelectObject(hdcSrc
, hbmpMask
);
1282 ::SelectObject(hdcDst
, hbmpInvMask
);
1283 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1287 wxLogLastError(wxT("BitBlt"));