]>
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
114 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
116 #ifndef __WXMICROWIN__
117 // it may be either HICON or HCURSOR
118 HICON hicon
= (HICON
)icon
.GetHandle();
121 if ( !::GetIconInfo(hicon
, &iconInfo
) )
123 wxLogLastError(wxT("GetIconInfo"));
128 wxBitmapRefData
*refData
= new wxBitmapRefData
;
131 int w
= icon
.GetWidth(),
132 h
= icon
.GetHeight();
134 refData
->m_width
= w
;
135 refData
->m_height
= h
;
136 refData
->m_depth
= wxDisplayDepth();
138 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
140 // the mask returned by GetIconInfo() is inversed compared to the usual
142 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
143 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
146 // delete the old one now as we don't need it any more
147 ::DeleteObject(iconInfo
.hbmMask
);
149 #if WXWIN_COMPATIBILITY_2
150 refData
->m_ok
= TRUE
;
151 #endif // WXWIN_COMPATIBILITY_2
161 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
169 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
173 return CopyFromIconOrCursor(cursor
);
177 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
184 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
185 // to create a bitmap from icon there - but using this way we won't have
188 int width
= icon
.GetWidth(),
189 height
= icon
.GetHeight();
191 // copy the icon to the bitmap
193 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
194 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
195 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
197 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
199 ::SelectObject(hdc
, hbmpOld
);
202 wxBitmapRefData
*refData
= new wxBitmapRefData
;
205 refData
->m_width
= width
;
206 refData
->m_height
= height
;
207 refData
->m_depth
= wxDisplayDepth();
209 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
211 #if WXWIN_COMPATIBILITY_2
212 refData
->m_ok
= TRUE
;
213 #endif // WXWIN_COMPATIBILITY_2
217 return CopyFromIconOrCursor(icon
);
218 #endif // Win16/Win32
221 wxBitmap::~wxBitmap()
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();
435 // Copy the palette from the source image
436 SetPalette(image
.GetPalette());
437 #endif // wxUSE_PALETTE
439 // create a DIB header
440 int headersize
= sizeof(BITMAPINFOHEADER
);
441 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
442 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
443 // Fill in the DIB header
444 lpDIBh
->bmiHeader
.biSize
= headersize
;
445 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
446 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
447 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
448 // the general formula for biSizeImage:
449 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
450 lpDIBh
->bmiHeader
.biPlanes
= 1;
451 lpDIBh
->bmiHeader
.biBitCount
= 24;
452 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
453 lpDIBh
->bmiHeader
.biClrUsed
= 0;
454 // These seem not really needed for our purpose here.
455 lpDIBh
->bmiHeader
.biClrImportant
= 0;
456 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
457 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
458 // memory for DIB data
459 unsigned char *lpBits
;
460 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
463 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
468 // create and set the device-dependent bitmap
469 HDC hdc
= ::GetDC(NULL
);
470 HDC memdc
= ::CreateCompatibleDC( hdc
);
472 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
473 ::SelectObject( memdc
, hbitmap
);
476 HPALETTE hOldPalette
= 0;
477 if (image
.GetPalette().Ok())
479 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
480 ::RealizePalette(memdc
);
482 #endif // wxUSE_PALETTE
484 // copy image data into DIB data and then into DDB (in a loop)
485 unsigned char *data
= image
.GetData();
488 unsigned char *ptdata
= data
;
489 unsigned char *ptbits
;
491 for( n
=0; n
<numDIB
; n
++ )
493 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
495 // redefine height and size of the (possibly) last smaller DIB
496 // memory is not reallocated
498 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
499 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
503 for( j
=0; j
<height
; j
++ )
505 for( i
=0; i
<width
; i
++ )
507 *(ptbits
++) = *(ptdata
+2);
508 *(ptbits
++) = *(ptdata
+1);
509 *(ptbits
++) = *(ptdata
);
512 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
514 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
515 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
517 // if numDIB = 1, lines below can also be used
518 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
519 // The above line is equivalent to the following two lines.
520 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
521 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
522 // or the following lines
523 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
524 // HDC memdc = ::CreateCompatibleDC( hdc );
525 // ::SelectObject( memdc, hbitmap);
526 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
527 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
528 // ::SelectObject( memdc, 0 );
529 // ::DeleteDC( memdc );
531 SetHBITMAP( (WXHBITMAP
) hbitmap
);
535 SelectPalette(memdc
, hOldPalette
, FALSE
);
536 #endif // wxUSE_PALETTE
538 // similarly, created an mono-bitmap for the possible mask
539 if( image
.HasMask() )
541 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
542 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
543 if( numDIB
== 1 ) height
= bmpHeight
;
544 else height
= sizeLimit
/bytePerLine
;
545 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
546 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
548 unsigned char r
= image
.GetMaskRed();
549 unsigned char g
= image
.GetMaskGreen();
550 unsigned char b
= image
.GetMaskBlue();
551 unsigned char zero
= 0, one
= 255;
553 for( n
=0; n
<numDIB
; n
++ )
555 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
557 // redefine height and size of the (possibly) last smaller DIB
558 // memory is not reallocated
560 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
561 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
564 for( int j
=0; j
<height
; j
++ )
566 for(i
=0; i
<width
; i
++ )
568 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
569 unsigned char cr
= (*(ptdata
++)) ;
570 unsigned char cg
= (*(ptdata
++)) ;
571 unsigned char cb
= (*(ptdata
++)) ;
573 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
586 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
588 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
589 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
592 // create a wxMask object
593 wxMask
*mask
= new wxMask();
594 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
596 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
597 /* The following can also be used but is slow to run
598 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
599 wxMask *mask = new wxMask( *this, colour );
603 ::SelectObject( memdc
, hbmpOld
);
606 // free allocated resources
608 ::ReleaseDC(NULL
, hdc
);
612 #if WXWIN_COMPATIBILITY_2
613 // check the wxBitmap object
614 GetBitmapData()->SetOk();
615 #endif // WXWIN_COMPATIBILITY_2
621 wxImage
wxBitmap::ConvertToImage() const
623 #ifdef __WXMICROWIN__
629 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
631 // create an wxImage object
632 int width
= GetWidth();
633 int height
= GetHeight();
634 image
.Create( width
, height
);
635 unsigned char *data
= image
.GetData();
638 wxFAIL_MSG( wxT("could not allocate data for image") );
642 // calc the number of bytes per scanline and padding in the DIB
643 int bytePerLine
= width
*3;
644 int sizeDWORD
= sizeof( DWORD
);
645 int lineBoundary
= bytePerLine
% sizeDWORD
;
647 if( lineBoundary
> 0 )
649 padding
= sizeDWORD
- lineBoundary
;
650 bytePerLine
+= padding
;
653 // create a DIB header
654 int headersize
= sizeof(BITMAPINFOHEADER
);
655 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
658 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
662 // Fill in the DIB header
663 lpDIBh
->bmiHeader
.biSize
= headersize
;
664 lpDIBh
->bmiHeader
.biWidth
= width
;
665 lpDIBh
->bmiHeader
.biHeight
= -height
;
666 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
667 lpDIBh
->bmiHeader
.biPlanes
= 1;
668 lpDIBh
->bmiHeader
.biBitCount
= 24;
669 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
670 lpDIBh
->bmiHeader
.biClrUsed
= 0;
671 // These seem not really needed for our purpose here.
672 lpDIBh
->bmiHeader
.biClrImportant
= 0;
673 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
674 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
675 // memory for DIB data
676 unsigned char *lpBits
;
677 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
680 wxFAIL_MSG( wxT("could not allocate data for DIB") );
686 // copy data from the device-dependent bitmap to the DIB
687 HDC hdc
= ::GetDC(NULL
);
689 hbitmap
= (HBITMAP
) GetHBITMAP();
690 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
692 // copy DIB data into the wxImage object
694 unsigned char *ptdata
= data
;
695 unsigned char *ptbits
= lpBits
;
696 for( i
=0; i
<height
; i
++ )
698 for( j
=0; j
<width
; j
++ )
700 *(ptdata
++) = *(ptbits
+2);
701 *(ptdata
++) = *(ptbits
+1);
702 *(ptdata
++) = *(ptbits
);
708 // similarly, set data according to the possible mask bitmap
709 if( GetMask() && GetMask()->GetMaskBitmap() )
711 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
712 // memory DC created, color set, data copied, and memory DC deleted
713 HDC memdc
= ::CreateCompatibleDC( hdc
);
714 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
715 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
716 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
718 // background color set to RGB(16,16,16) in consistent with wxGTK
719 unsigned char r
=16, g
=16, b
=16;
722 for( i
=0; i
<height
; i
++ )
724 for( j
=0; j
<width
; j
++ )
738 image
.SetMaskColour( r
, g
, b
);
739 image
.SetMask( TRUE
);
743 image
.SetMask( FALSE
);
745 // free allocated resources
746 ::ReleaseDC(NULL
, hdc
);
754 #endif // wxUSE_IMAGE
756 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
760 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
764 m_refData
= new wxBitmapRefData
;
766 return handler
->LoadFile(this, filename
, type
, -1, -1);
772 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
774 *this = image
.ConvertToBitmap();
779 #endif // wxUSE_IMAGE
784 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
788 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
792 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
797 m_refData
= new wxBitmapRefData
;
799 return handler
->Create(this, data
, type
, width
, height
, depth
);
802 bool wxBitmap::SaveFile(const wxString
& filename
,
804 const wxPalette
*palette
)
806 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
810 return handler
->SaveFile(this, filename
, type
, palette
);
815 // FIXME what about palette? shouldn't we use it?
816 wxImage
image( *this );
819 return image
.SaveFile(filename
, type
);
822 #endif // wxUSE_IMAGE
827 // ----------------------------------------------------------------------------
828 // sub bitmap extraction
829 // ----------------------------------------------------------------------------
831 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
833 #ifndef __WXMICROWIN__
835 (rect
.x
>= 0) && (rect
.y
>= 0) &&
836 (rect
.x
+rect
.width
<= GetWidth()) &&
837 (rect
.y
+rect
.height
<= GetHeight()),
838 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
840 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
841 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
844 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
845 HDC dcDst
= ::CreateCompatibleDC(NULL
);
846 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
847 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
848 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
850 // copy mask if there is one
853 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
855 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
856 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
857 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
859 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
863 SelectObject(dcDst
, NULL
);
864 SelectObject(dcSrc
, NULL
);
874 // ----------------------------------------------------------------------------
875 // wxBitmap accessors
876 // ----------------------------------------------------------------------------
878 void wxBitmap::SetQuality(int q
)
882 GetBitmapData()->m_quality
= q
;
885 #if WXWIN_COMPATIBILITY_2
886 void wxBitmap::SetOk(bool isOk
)
890 GetBitmapData()->m_ok
= isOk
;
892 #endif // WXWIN_COMPATIBILITY_2
896 void wxBitmap::SetPalette(const wxPalette
& palette
)
900 GetBitmapData()->m_bitmapPalette
= palette
;
903 #endif // wxUSE_PALETTE
905 void wxBitmap::SetMask(wxMask
*mask
)
909 GetBitmapData()->m_bitmapMask
= mask
;
912 // Creates a bitmap that matches the device context, from
913 // an arbitray bitmap. At present, the original bitmap must have an
914 // associated palette. TODO: use a default palette if no palette exists.
915 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
916 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
918 #ifdef __WXMICROWIN__
922 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
923 HPALETTE hPal
= (HPALETTE
) NULL
;
925 void *lpBits
= (void*) NULL
;
928 if( GetPalette() && GetPalette()->Ok() )
930 tmpBitmap
.SetPalette(*GetPalette());
931 memDC
.SelectObject(tmpBitmap
);
932 memDC
.SetPalette(*GetPalette());
933 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
937 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
939 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
940 tmpBitmap
.SetPalette( palette
);
941 memDC
.SelectObject(tmpBitmap
);
942 memDC
.SetPalette( palette
);
944 #else // !wxUSE_PALETTE
945 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
946 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
948 // set the height negative because in a DIB the order of the lines is
950 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
955 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
957 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
959 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
960 GetWidth(), GetHeight(),
961 0, 0, 0, GetHeight(),
962 lpBits
, lpDib
, DIB_RGB_COLORS
);
972 // ----------------------------------------------------------------------------
974 // ----------------------------------------------------------------------------
981 // Construct a mask from a bitmap and a colour indicating
982 // the transparent area
983 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
986 Create(bitmap
, colour
);
989 // Construct a mask from a bitmap and a palette index indicating
990 // the transparent area
991 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
994 Create(bitmap
, paletteIndex
);
997 // Construct a mask from a mono bitmap (copies the bitmap).
998 wxMask::wxMask(const wxBitmap
& bitmap
)
1007 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1010 // Create a mask from a mono bitmap (copies the bitmap).
1011 bool wxMask::Create(const wxBitmap
& bitmap
)
1013 #ifndef __WXMICROWIN__
1014 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1015 _T("can't create mask from invalid or not monochrome bitmap") );
1019 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1023 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1028 HDC srcDC
= CreateCompatibleDC(0);
1029 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1030 HDC destDC
= CreateCompatibleDC(0);
1031 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1032 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1033 SelectObject(srcDC
, 0);
1035 SelectObject(destDC
, 0);
1043 // Create a mask from a bitmap and a palette index indicating
1044 // the transparent area
1045 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1049 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1054 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1056 unsigned char red
, green
, blue
;
1057 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1059 wxColour
transparentColour(red
, green
, blue
);
1060 return Create(bitmap
, transparentColour
);
1063 #endif // wxUSE_PALETTE
1068 // Create a mask from a bitmap and a colour indicating
1069 // the transparent area
1070 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1072 #ifndef __WXMICROWIN__
1073 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1077 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1081 int width
= bitmap
.GetWidth(),
1082 height
= bitmap
.GetHeight();
1084 // scan the bitmap for the transparent colour and set the corresponding
1085 // pixels in the mask to BLACK and the rest to WHITE
1086 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1087 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1089 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1090 HDC destDC
= ::CreateCompatibleDC(NULL
);
1091 if ( !srcDC
|| !destDC
)
1093 wxLogLastError(wxT("CreateCompatibleDC"));
1098 // SelectObject() will fail
1099 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1100 _T("bitmap can't be selected in another DC") );
1102 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1105 wxLogLastError(wxT("SelectObject"));
1110 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1113 wxLogLastError(wxT("SelectObject"));
1118 // this is not very efficient, but I can't think of a better way of doing
1120 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1122 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1124 COLORREF col
= GetPixel(srcDC
, w
, h
);
1125 if ( col
== CLR_INVALID
)
1127 wxLogLastError(wxT("GetPixel"));
1129 // doesn't make sense to continue
1135 if ( col
== maskColour
)
1137 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1141 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1146 ::SelectObject(srcDC
, hbmpSrcOld
);
1148 ::SelectObject(destDC
, hbmpDstOld
);
1157 // ----------------------------------------------------------------------------
1159 // ----------------------------------------------------------------------------
1161 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1164 int width
, int height
, int depth
)
1166 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1168 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1171 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1172 const wxString
& name
,
1174 int width
, int height
)
1176 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1178 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1181 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1182 const wxString
& name
,
1185 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1187 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1190 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1191 void *WXUNUSED(data
),
1192 long WXUNUSED(type
),
1193 int WXUNUSED(width
),
1194 int WXUNUSED(height
),
1195 int WXUNUSED(depth
))
1200 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1201 const wxString
& WXUNUSED(name
),
1202 long WXUNUSED(type
),
1203 int WXUNUSED(desiredWidth
),
1204 int WXUNUSED(desiredHeight
))
1209 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1210 const wxString
& WXUNUSED(name
),
1212 const wxPalette
*WXUNUSED(palette
))
1217 // ----------------------------------------------------------------------------
1219 // ----------------------------------------------------------------------------
1221 #ifndef __WXMICROWIN__
1222 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1223 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1225 unsigned long i
, headerSize
;
1226 LPBITMAPINFO lpDIBheader
= NULL
;
1227 LPPALETTEENTRY lpPe
= NULL
;
1230 // Allocate space for a DIB header
1231 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1232 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1233 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1235 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1237 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1239 // Fill in the static parts of the DIB header
1240 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1241 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1242 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1243 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1245 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1246 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1247 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1248 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1249 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1252 // Initialize the DIB palette
1253 for (i
= 0; i
< 256; i
++) {
1254 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1255 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1256 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1257 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1260 *lpDIBHeader
= lpDIBheader
;
1265 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1271 // ----------------------------------------------------------------------------
1272 // other helper functions
1273 // ----------------------------------------------------------------------------
1275 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1277 #ifndef __WXMICROWIN__
1278 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1280 // get width/height from the bitmap if not given
1284 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1289 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1290 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1291 if ( !hdcSrc
|| !hdcDst
)
1293 wxLogLastError(wxT("CreateCompatibleDC"));
1296 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1299 wxLogLastError(wxT("CreateBitmap"));
1302 ::SelectObject(hdcSrc
, hbmpMask
);
1303 ::SelectObject(hdcDst
, hbmpInvMask
);
1304 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1308 wxLogLastError(wxT("BitBlt"));