]>
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
);
471 HPALETTE hOldPalette
= 0;
472 if (image
.GetPalette().Ok())
474 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
475 ::RealizePalette(memdc
);
477 #endif // wxUSE_PALETTE
479 // copy image data into DIB data and then into DDB (in a loop)
480 unsigned char *data
= image
.GetData();
483 unsigned char *ptdata
= data
;
484 unsigned char *ptbits
;
486 for( n
=0; n
<numDIB
; n
++ )
488 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
490 // redefine height and size of the (possibly) last smaller DIB
491 // memory is not reallocated
493 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
494 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
498 for( j
=0; j
<height
; j
++ )
500 for( i
=0; i
<width
; i
++ )
502 *(ptbits
++) = *(ptdata
+2);
503 *(ptbits
++) = *(ptdata
+1);
504 *(ptbits
++) = *(ptdata
);
507 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
509 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
510 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
512 // if numDIB = 1, lines below can also be used
513 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
514 // The above line is equivalent to the following two lines.
515 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
516 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
517 // or the following lines
518 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
519 // HDC memdc = ::CreateCompatibleDC( hdc );
520 // ::SelectObject( memdc, hbitmap);
521 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
522 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
523 // ::SelectObject( memdc, 0 );
524 // ::DeleteDC( memdc );
526 SetHBITMAP( (WXHBITMAP
) hbitmap
);
530 SelectPalette(memdc
, hOldPalette
, FALSE
);
531 #endif // wxUSE_PALETTE
533 // similarly, created an mono-bitmap for the possible mask
534 if( image
.HasMask() )
536 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
537 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
538 if( numDIB
== 1 ) height
= bmpHeight
;
539 else height
= sizeLimit
/bytePerLine
;
540 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
541 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
543 unsigned char r
= image
.GetMaskRed();
544 unsigned char g
= image
.GetMaskGreen();
545 unsigned char b
= image
.GetMaskBlue();
546 unsigned char zero
= 0, one
= 255;
548 for( n
=0; n
<numDIB
; n
++ )
550 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
552 // redefine height and size of the (possibly) last smaller DIB
553 // memory is not reallocated
555 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
556 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
559 for( int j
=0; j
<height
; j
++ )
561 for(i
=0; i
<width
; i
++ )
563 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
564 unsigned char cr
= (*(ptdata
++)) ;
565 unsigned char cg
= (*(ptdata
++)) ;
566 unsigned char cb
= (*(ptdata
++)) ;
568 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
581 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
583 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
584 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
587 // create a wxMask object
588 wxMask
*mask
= new wxMask();
589 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
591 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
592 /* The following can also be used but is slow to run
593 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
594 wxMask *mask = new wxMask( *this, colour );
598 ::SelectObject( memdc
, hbmpOld
);
601 // free allocated resources
603 ::ReleaseDC(NULL
, hdc
);
607 #if WXWIN_COMPATIBILITY_2
608 // check the wxBitmap object
609 GetBitmapData()->SetOk();
610 #endif // WXWIN_COMPATIBILITY_2
612 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
618 wxImage
wxBitmap::ConvertToImage() const
620 #ifdef __WXMICROWIN__
626 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
628 // create an wxImage object
629 int width
= GetWidth();
630 int height
= GetHeight();
631 image
.Create( width
, height
);
632 unsigned char *data
= image
.GetData();
635 wxFAIL_MSG( wxT("could not allocate data for image") );
639 // calc the number of bytes per scanline and padding in the DIB
640 int bytePerLine
= width
*3;
641 int sizeDWORD
= sizeof( DWORD
);
642 int lineBoundary
= bytePerLine
% sizeDWORD
;
644 if( lineBoundary
> 0 )
646 padding
= sizeDWORD
- lineBoundary
;
647 bytePerLine
+= padding
;
650 // create a DIB header
651 int headersize
= sizeof(BITMAPINFOHEADER
);
652 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
655 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
659 // Fill in the DIB header
660 lpDIBh
->bmiHeader
.biSize
= headersize
;
661 lpDIBh
->bmiHeader
.biWidth
= width
;
662 lpDIBh
->bmiHeader
.biHeight
= -height
;
663 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
664 lpDIBh
->bmiHeader
.biPlanes
= 1;
665 lpDIBh
->bmiHeader
.biBitCount
= 24;
666 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
667 lpDIBh
->bmiHeader
.biClrUsed
= 0;
668 // These seem not really needed for our purpose here.
669 lpDIBh
->bmiHeader
.biClrImportant
= 0;
670 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
671 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
672 // memory for DIB data
673 unsigned char *lpBits
;
674 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
677 wxFAIL_MSG( wxT("could not allocate data for DIB") );
683 // copy data from the device-dependent bitmap to the DIB
684 HDC hdc
= ::GetDC(NULL
);
686 hbitmap
= (HBITMAP
) GetHBITMAP();
687 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
689 // copy DIB data into the wxImage object
691 unsigned char *ptdata
= data
;
692 unsigned char *ptbits
= lpBits
;
693 for( i
=0; i
<height
; i
++ )
695 for( j
=0; j
<width
; j
++ )
697 *(ptdata
++) = *(ptbits
+2);
698 *(ptdata
++) = *(ptbits
+1);
699 *(ptdata
++) = *(ptbits
);
705 // similarly, set data according to the possible mask bitmap
706 if( GetMask() && GetMask()->GetMaskBitmap() )
708 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
709 // memory DC created, color set, data copied, and memory DC deleted
710 HDC memdc
= ::CreateCompatibleDC( hdc
);
711 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
712 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
713 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
715 // background color set to RGB(16,16,16) in consistent with wxGTK
716 unsigned char r
=16, g
=16, b
=16;
719 for( i
=0; i
<height
; i
++ )
721 for( j
=0; j
<width
; j
++ )
735 image
.SetMaskColour( r
, g
, b
);
736 image
.SetMask( TRUE
);
740 image
.SetMask( FALSE
);
742 // free allocated resources
743 ::ReleaseDC(NULL
, hdc
);
751 #endif // wxUSE_IMAGE
753 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
757 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
761 m_refData
= new wxBitmapRefData
;
763 return handler
->LoadFile(this, filename
, type
, -1, -1);
769 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
771 *this = image
.ConvertToBitmap();
776 #endif // wxUSE_IMAGE
781 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
785 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
789 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
794 m_refData
= new wxBitmapRefData
;
796 return handler
->Create(this, data
, type
, width
, height
, depth
);
799 bool wxBitmap::SaveFile(const wxString
& filename
,
801 const wxPalette
*palette
)
803 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
807 return handler
->SaveFile(this, filename
, type
, palette
);
812 // FIXME what about palette? shouldn't we use it?
813 wxImage
image( *this );
816 return image
.SaveFile(filename
, type
);
819 #endif // wxUSE_IMAGE
824 // ----------------------------------------------------------------------------
825 // sub bitmap extraction
826 // ----------------------------------------------------------------------------
828 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
830 #ifndef __WXMICROWIN__
832 (rect
.x
>= 0) && (rect
.y
>= 0) &&
833 (rect
.x
+rect
.width
<= GetWidth()) &&
834 (rect
.y
+rect
.height
<= GetHeight()),
835 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
837 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
838 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
841 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
842 HDC dcDst
= ::CreateCompatibleDC(NULL
);
843 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
844 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
845 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
847 // copy mask if there is one
850 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
852 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
853 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
854 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
856 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
860 SelectObject(dcDst
, NULL
);
861 SelectObject(dcSrc
, NULL
);
871 // ----------------------------------------------------------------------------
872 // wxBitmap accessors
873 // ----------------------------------------------------------------------------
875 void wxBitmap::SetQuality(int q
)
879 GetBitmapData()->m_quality
= q
;
882 #if WXWIN_COMPATIBILITY_2
883 void wxBitmap::SetOk(bool isOk
)
887 GetBitmapData()->m_ok
= isOk
;
889 #endif // WXWIN_COMPATIBILITY_2
893 void wxBitmap::SetPalette(const wxPalette
& palette
)
897 GetBitmapData()->m_bitmapPalette
= palette
;
900 #endif // wxUSE_PALETTE
902 void wxBitmap::SetMask(wxMask
*mask
)
906 GetBitmapData()->m_bitmapMask
= mask
;
909 // Creates a bitmap that matches the device context, from
910 // an arbitray bitmap. At present, the original bitmap must have an
911 // associated palette. TODO: use a default palette if no palette exists.
912 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
913 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
915 #ifdef __WXMICROWIN__
919 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
920 HPALETTE hPal
= (HPALETTE
) NULL
;
922 void *lpBits
= (void*) NULL
;
925 if( GetPalette() && GetPalette()->Ok() )
927 tmpBitmap
.SetPalette(*GetPalette());
928 memDC
.SelectObject(tmpBitmap
);
929 memDC
.SetPalette(*GetPalette());
930 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
934 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
936 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
937 tmpBitmap
.SetPalette( palette
);
938 memDC
.SelectObject(tmpBitmap
);
939 memDC
.SetPalette( palette
);
941 #else // !wxUSE_PALETTE
942 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
943 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
945 // set the height negative because in a DIB the order of the lines is
947 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
952 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
954 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
956 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
957 GetWidth(), GetHeight(),
958 0, 0, 0, GetHeight(),
959 lpBits
, lpDib
, DIB_RGB_COLORS
);
969 // ----------------------------------------------------------------------------
971 // ----------------------------------------------------------------------------
978 // Construct a mask from a bitmap and a colour indicating
979 // the transparent area
980 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
983 Create(bitmap
, colour
);
986 // Construct a mask from a bitmap and a palette index indicating
987 // the transparent area
988 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
991 Create(bitmap
, paletteIndex
);
994 // Construct a mask from a mono bitmap (copies the bitmap).
995 wxMask::wxMask(const wxBitmap
& bitmap
)
1004 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1007 // Create a mask from a mono bitmap (copies the bitmap).
1008 bool wxMask::Create(const wxBitmap
& bitmap
)
1010 #ifndef __WXMICROWIN__
1011 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1012 _T("can't create mask from invalid or not monochrome bitmap") );
1016 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1020 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1025 HDC srcDC
= CreateCompatibleDC(0);
1026 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1027 HDC destDC
= CreateCompatibleDC(0);
1028 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1029 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1030 SelectObject(srcDC
, 0);
1032 SelectObject(destDC
, 0);
1040 // Create a mask from a bitmap and a palette index indicating
1041 // the transparent area
1042 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1046 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1051 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1053 unsigned char red
, green
, blue
;
1054 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1056 wxColour
transparentColour(red
, green
, blue
);
1057 return Create(bitmap
, transparentColour
);
1060 #endif // wxUSE_PALETTE
1065 // Create a mask from a bitmap and a colour indicating
1066 // the transparent area
1067 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1069 #ifndef __WXMICROWIN__
1070 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1074 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1078 int width
= bitmap
.GetWidth(),
1079 height
= bitmap
.GetHeight();
1081 // scan the bitmap for the transparent colour and set the corresponding
1082 // pixels in the mask to BLACK and the rest to WHITE
1083 COLORREF maskColour
= wxColourToRGB(colour
);
1084 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1086 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1087 HDC destDC
= ::CreateCompatibleDC(NULL
);
1088 if ( !srcDC
|| !destDC
)
1090 wxLogLastError(wxT("CreateCompatibleDC"));
1095 // SelectObject() will fail
1096 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1097 _T("bitmap can't be selected in another DC") );
1099 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1102 wxLogLastError(wxT("SelectObject"));
1107 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1110 wxLogLastError(wxT("SelectObject"));
1115 // this is not very efficient, but I can't think of a better way of doing
1117 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1119 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1121 COLORREF col
= GetPixel(srcDC
, w
, h
);
1122 if ( col
== CLR_INVALID
)
1124 wxLogLastError(wxT("GetPixel"));
1126 // doesn't make sense to continue
1132 if ( col
== maskColour
)
1134 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1138 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1143 ::SelectObject(srcDC
, hbmpSrcOld
);
1145 ::SelectObject(destDC
, hbmpDstOld
);
1154 // ----------------------------------------------------------------------------
1156 // ----------------------------------------------------------------------------
1158 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1161 int width
, int height
, int depth
)
1163 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1165 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1168 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1169 const wxString
& name
,
1171 int width
, int height
)
1173 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1175 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1178 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1179 const wxString
& name
,
1182 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1184 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1187 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1188 void *WXUNUSED(data
),
1189 long WXUNUSED(type
),
1190 int WXUNUSED(width
),
1191 int WXUNUSED(height
),
1192 int WXUNUSED(depth
))
1197 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1198 const wxString
& WXUNUSED(name
),
1199 long WXUNUSED(type
),
1200 int WXUNUSED(desiredWidth
),
1201 int WXUNUSED(desiredHeight
))
1206 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1207 const wxString
& WXUNUSED(name
),
1209 const wxPalette
*WXUNUSED(palette
))
1214 // ----------------------------------------------------------------------------
1216 // ----------------------------------------------------------------------------
1218 #ifndef __WXMICROWIN__
1219 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1220 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1222 unsigned long i
, headerSize
;
1223 LPBITMAPINFO lpDIBheader
= NULL
;
1224 LPPALETTEENTRY lpPe
= NULL
;
1227 // Allocate space for a DIB header
1228 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1229 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1230 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1232 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1234 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1236 // Fill in the static parts of the DIB header
1237 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1238 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1239 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1240 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1242 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1243 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1244 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1245 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1246 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1249 // Initialize the DIB palette
1250 for (i
= 0; i
< 256; i
++) {
1251 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1252 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1253 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1254 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1257 *lpDIBHeader
= lpDIBheader
;
1262 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1268 // ----------------------------------------------------------------------------
1269 // other helper functions
1270 // ----------------------------------------------------------------------------
1272 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1274 #ifndef __WXMICROWIN__
1275 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1277 // get width/height from the bitmap if not given
1281 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1286 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1287 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1288 if ( !hdcSrc
|| !hdcDst
)
1290 wxLogLastError(wxT("CreateCompatibleDC"));
1293 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1296 wxLogLastError(wxT("CreateBitmap"));
1299 ::SelectObject(hdcSrc
, hbmpMask
);
1300 ::SelectObject(hdcDst
, hbmpInvMask
);
1301 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1305 wxLogLastError(wxT("BitBlt"));