]>
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 static const size_t bytesPerLine
= (width
+ 7) / 8;
246 static const size_t padding
= bytesPerLine
% 2;
247 static 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 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
387 m_refData
= new wxBitmapRefData();
389 // sizeLimit is the MS upper limit for the DIB size
391 int sizeLimit
= 1024*768*3;
393 int sizeLimit
= 0x7fff ;
396 // width and height of the device-dependent bitmap
397 int width
= image
.GetWidth();
398 int bmpHeight
= image
.GetHeight();
400 // calc the number of bytes per scanline and padding
401 int bytePerLine
= width
*3;
402 int sizeDWORD
= sizeof( DWORD
);
403 int lineBoundary
= bytePerLine
% sizeDWORD
;
405 if( lineBoundary
> 0 )
407 padding
= sizeDWORD
- lineBoundary
;
408 bytePerLine
+= padding
;
410 // calc the number of DIBs and heights of DIBs
413 int height
= sizeLimit
/bytePerLine
;
414 if( height
>= bmpHeight
)
418 numDIB
= bmpHeight
/ height
;
419 hRemain
= bmpHeight
% height
;
420 if( hRemain
>0 ) numDIB
++;
423 // set bitmap parameters
424 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
426 SetHeight( bmpHeight
);
427 if (depth
== -1) depth
= wxDisplayDepth();
430 // create a DIB header
431 int headersize
= sizeof(BITMAPINFOHEADER
);
432 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
433 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
434 // Fill in the DIB header
435 lpDIBh
->bmiHeader
.biSize
= headersize
;
436 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
437 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
438 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
439 // the general formula for biSizeImage:
440 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
441 lpDIBh
->bmiHeader
.biPlanes
= 1;
442 lpDIBh
->bmiHeader
.biBitCount
= 24;
443 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
444 lpDIBh
->bmiHeader
.biClrUsed
= 0;
445 // These seem not really needed for our purpose here.
446 lpDIBh
->bmiHeader
.biClrImportant
= 0;
447 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
448 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
449 // memory for DIB data
450 unsigned char *lpBits
;
451 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
454 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
459 // create and set the device-dependent bitmap
460 HDC hdc
= ::GetDC(NULL
);
461 HDC memdc
= ::CreateCompatibleDC( hdc
);
463 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
464 ::SelectObject( memdc
, hbitmap
);
466 HPALETTE hOldPalette
= 0;
467 if (image
.GetPalette().Ok())
469 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
470 ::RealizePalette(memdc
);
473 // copy image data into DIB data and then into DDB (in a loop)
474 unsigned char *data
= image
.GetData();
477 unsigned char *ptdata
= data
;
478 unsigned char *ptbits
;
480 for( n
=0; n
<numDIB
; n
++ )
482 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
484 // redefine height and size of the (possibly) last smaller DIB
485 // memory is not reallocated
487 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
488 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
492 for( j
=0; j
<height
; j
++ )
494 for( i
=0; i
<width
; i
++ )
496 *(ptbits
++) = *(ptdata
+2);
497 *(ptbits
++) = *(ptdata
+1);
498 *(ptbits
++) = *(ptdata
);
501 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
503 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
504 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
506 // if numDIB = 1, lines below can also be used
507 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
508 // The above line is equivalent to the following two lines.
509 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
510 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
511 // or the following lines
512 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
513 // HDC memdc = ::CreateCompatibleDC( hdc );
514 // ::SelectObject( memdc, hbitmap);
515 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
516 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
517 // ::SelectObject( memdc, 0 );
518 // ::DeleteDC( memdc );
520 SetHBITMAP( (WXHBITMAP
) hbitmap
);
523 SelectPalette(memdc
, hOldPalette
, FALSE
);
525 // similarly, created an mono-bitmap for the possible mask
526 if( image
.HasMask() )
528 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
529 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
530 if( numDIB
== 1 ) height
= bmpHeight
;
531 else height
= sizeLimit
/bytePerLine
;
532 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
533 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
535 unsigned char r
= image
.GetMaskRed();
536 unsigned char g
= image
.GetMaskGreen();
537 unsigned char b
= image
.GetMaskBlue();
538 unsigned char zero
= 0, one
= 255;
540 for( n
=0; n
<numDIB
; n
++ )
542 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
544 // redefine height and size of the (possibly) last smaller DIB
545 // memory is not reallocated
547 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
548 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
551 for( int j
=0; j
<height
; j
++ )
553 for(i
=0; i
<width
; i
++ )
555 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
556 unsigned char cr
= (*(ptdata
++)) ;
557 unsigned char cg
= (*(ptdata
++)) ;
558 unsigned char cb
= (*(ptdata
++)) ;
560 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
573 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
575 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
576 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
579 // create a wxMask object
580 wxMask
*mask
= new wxMask();
581 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
583 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
584 /* The following can also be used but is slow to run
585 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
586 wxMask *mask = new wxMask( *this, colour );
590 ::SelectObject( memdc
, hbmpOld
);
593 // free allocated resources
595 ::ReleaseDC(NULL
, hdc
);
599 #if WXWIN_COMPATIBILITY_2
600 // check the wxBitmap object
601 GetBitmapData()->SetOk();
602 #endif // WXWIN_COMPATIBILITY_2
604 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
609 wxImage
wxBitmap::ConvertToImage() const
613 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
615 // create an wxImage object
616 int width
= GetWidth();
617 int height
= GetHeight();
618 image
.Create( width
, height
);
619 unsigned char *data
= image
.GetData();
622 wxFAIL_MSG( wxT("could not allocate data for image") );
626 // calc the number of bytes per scanline and padding in the DIB
627 int bytePerLine
= width
*3;
628 int sizeDWORD
= sizeof( DWORD
);
629 int lineBoundary
= bytePerLine
% sizeDWORD
;
631 if( lineBoundary
> 0 )
633 padding
= sizeDWORD
- lineBoundary
;
634 bytePerLine
+= padding
;
637 // create a DIB header
638 int headersize
= sizeof(BITMAPINFOHEADER
);
639 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
642 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
646 // Fill in the DIB header
647 lpDIBh
->bmiHeader
.biSize
= headersize
;
648 lpDIBh
->bmiHeader
.biWidth
= width
;
649 lpDIBh
->bmiHeader
.biHeight
= -height
;
650 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
651 lpDIBh
->bmiHeader
.biPlanes
= 1;
652 lpDIBh
->bmiHeader
.biBitCount
= 24;
653 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
654 lpDIBh
->bmiHeader
.biClrUsed
= 0;
655 // These seem not really needed for our purpose here.
656 lpDIBh
->bmiHeader
.biClrImportant
= 0;
657 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
658 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
659 // memory for DIB data
660 unsigned char *lpBits
;
661 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
664 wxFAIL_MSG( wxT("could not allocate data for DIB") );
670 // copy data from the device-dependent bitmap to the DIB
671 HDC hdc
= ::GetDC(NULL
);
673 hbitmap
= (HBITMAP
) GetHBITMAP();
674 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
676 // copy DIB data into the wxImage object
678 unsigned char *ptdata
= data
;
679 unsigned char *ptbits
= lpBits
;
680 for( i
=0; i
<height
; i
++ )
682 for( j
=0; j
<width
; j
++ )
684 *(ptdata
++) = *(ptbits
+2);
685 *(ptdata
++) = *(ptbits
+1);
686 *(ptdata
++) = *(ptbits
);
692 // similarly, set data according to the possible mask bitmap
693 if( GetMask() && GetMask()->GetMaskBitmap() )
695 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
696 // memory DC created, color set, data copied, and memory DC deleted
697 HDC memdc
= ::CreateCompatibleDC( hdc
);
698 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
699 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
700 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
702 // background color set to RGB(16,16,16) in consistent with wxGTK
703 unsigned char r
=16, g
=16, b
=16;
706 for( i
=0; i
<height
; i
++ )
708 for( j
=0; j
<width
; j
++ )
722 image
.SetMaskColour( r
, g
, b
);
723 image
.SetMask( TRUE
);
727 image
.SetMask( FALSE
);
729 // free allocated resources
730 ::ReleaseDC(NULL
, hdc
);
737 #endif // wxUSE_IMAGE
739 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
743 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
747 m_refData
= new wxBitmapRefData
;
749 return handler
->LoadFile(this, filename
, type
, -1, -1);
755 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
757 *this = image
.ConvertToBitmap();
762 #endif // wxUSE_IMAGE
767 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
771 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
775 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
780 m_refData
= new wxBitmapRefData
;
782 return handler
->Create(this, data
, type
, width
, height
, depth
);
785 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
787 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
791 return handler
->SaveFile(this, filename
, type
, palette
);
796 // FIXME what about palette? shouldn't we use it?
797 wxImage
image( *this );
800 return image
.SaveFile(filename
, type
);
803 #endif // wxUSE_IMAGE
808 // ----------------------------------------------------------------------------
809 // sub bitmap extraction
810 // ----------------------------------------------------------------------------
812 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
814 #ifndef __WXMICROWIN__
816 (rect
.x
>= 0) && (rect
.y
>= 0) &&
817 (rect
.x
+rect
.width
<= GetWidth()) &&
818 (rect
.y
+rect
.height
<= GetHeight()),
819 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
821 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
822 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
825 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
826 HDC dcDst
= ::CreateCompatibleDC(NULL
);
827 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
828 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
829 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
831 // copy mask if there is one
834 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
836 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
837 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
838 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
840 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
844 SelectObject(dcDst
, NULL
);
845 SelectObject(dcSrc
, NULL
);
855 // ----------------------------------------------------------------------------
856 // wxBitmap accessors
857 // ----------------------------------------------------------------------------
859 void wxBitmap::SetQuality(int q
)
863 GetBitmapData()->m_quality
= q
;
866 #if WXWIN_COMPATIBILITY_2
867 void wxBitmap::SetOk(bool isOk
)
871 GetBitmapData()->m_ok
= isOk
;
873 #endif // WXWIN_COMPATIBILITY_2
875 void wxBitmap::SetPalette(const wxPalette
& palette
)
879 GetBitmapData()->m_bitmapPalette
= palette
;
882 void wxBitmap::SetMask(wxMask
*mask
)
886 GetBitmapData()->m_bitmapMask
= mask
;
889 // Creates a bitmap that matches the device context, from
890 // an arbitray bitmap. At present, the original bitmap must have an
891 // associated palette. TODO: use a default palette if no palette exists.
892 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
893 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
895 #ifdef __WXMICROWIN__
899 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
900 HPALETTE hPal
= (HPALETTE
) NULL
;
902 void *lpBits
= (void*) NULL
;
904 if( GetPalette() && GetPalette()->Ok() )
906 tmpBitmap
.SetPalette(*GetPalette());
907 memDC
.SelectObject(tmpBitmap
);
908 memDC
.SetPalette(*GetPalette());
909 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
913 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
915 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
916 tmpBitmap
.SetPalette( palette
);
917 memDC
.SelectObject(tmpBitmap
);
918 memDC
.SetPalette( palette
);
921 // set the height negative because in a DIB the order of the lines is
923 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
928 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
930 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
932 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
933 GetWidth(), GetHeight(),
934 0, 0, 0, GetHeight(),
935 lpBits
, lpDib
, DIB_RGB_COLORS
);
945 // ----------------------------------------------------------------------------
947 // ----------------------------------------------------------------------------
954 // Construct a mask from a bitmap and a colour indicating
955 // the transparent area
956 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
959 Create(bitmap
, colour
);
962 // Construct a mask from a bitmap and a palette index indicating
963 // the transparent area
964 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
967 Create(bitmap
, paletteIndex
);
970 // Construct a mask from a mono bitmap (copies the bitmap).
971 wxMask::wxMask(const wxBitmap
& bitmap
)
980 ::DeleteObject((HBITMAP
) m_maskBitmap
);
983 // Create a mask from a mono bitmap (copies the bitmap).
984 bool wxMask::Create(const wxBitmap
& bitmap
)
986 #ifndef __WXMICROWIN__
987 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
988 _T("can't create mask from invalid or not monochrome bitmap") );
992 ::DeleteObject((HBITMAP
) m_maskBitmap
);
996 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1001 HDC srcDC
= CreateCompatibleDC(0);
1002 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1003 HDC destDC
= CreateCompatibleDC(0);
1004 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1005 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1006 SelectObject(srcDC
, 0);
1008 SelectObject(destDC
, 0);
1016 // Create a mask from a bitmap and a palette index indicating
1017 // the transparent area
1018 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1022 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1025 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1027 unsigned char red
, green
, blue
;
1028 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1030 wxColour
transparentColour(red
, green
, blue
);
1031 return Create(bitmap
, transparentColour
);
1037 // Create a mask from a bitmap and a colour indicating
1038 // the transparent area
1039 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1041 #ifndef __WXMICROWIN__
1042 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1046 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1050 int width
= bitmap
.GetWidth(),
1051 height
= bitmap
.GetHeight();
1053 // scan the bitmap for the transparent colour and set the corresponding
1054 // pixels in the mask to BLACK and the rest to WHITE
1055 COLORREF maskColour
= wxColourToRGB(colour
);
1056 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1058 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1059 HDC destDC
= ::CreateCompatibleDC(NULL
);
1060 if ( !srcDC
|| !destDC
)
1062 wxLogLastError(wxT("CreateCompatibleDC"));
1067 // SelectObject() will fail
1068 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1069 _T("bitmap can't be selected in another DC") );
1071 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1074 wxLogLastError(wxT("SelectObject"));
1079 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1082 wxLogLastError(wxT("SelectObject"));
1087 // this is not very efficient, but I can't think of a better way of doing
1089 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1091 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1093 COLORREF col
= GetPixel(srcDC
, w
, h
);
1094 if ( col
== CLR_INVALID
)
1096 wxLogLastError(wxT("GetPixel"));
1098 // doesn't make sense to continue
1104 if ( col
== maskColour
)
1106 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1110 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1115 ::SelectObject(srcDC
, hbmpSrcOld
);
1117 ::SelectObject(destDC
, hbmpDstOld
);
1126 // ----------------------------------------------------------------------------
1128 // ----------------------------------------------------------------------------
1130 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1133 int width
, int height
, int depth
)
1135 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1137 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1140 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1141 const wxString
& name
,
1143 int width
, int height
)
1145 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1147 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1150 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1151 const wxString
& name
,
1154 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1156 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1159 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1160 void *WXUNUSED(data
),
1161 long WXUNUSED(type
),
1162 int WXUNUSED(width
),
1163 int WXUNUSED(height
),
1164 int WXUNUSED(depth
))
1169 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1170 const wxString
& WXUNUSED(name
),
1171 long WXUNUSED(type
),
1172 int WXUNUSED(desiredWidth
),
1173 int WXUNUSED(desiredHeight
))
1178 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1179 const wxString
& WXUNUSED(name
),
1181 const wxPalette
*WXUNUSED(palette
))
1186 // ----------------------------------------------------------------------------
1188 // ----------------------------------------------------------------------------
1190 #ifndef __WXMICROWIN__
1191 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1192 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1194 unsigned long i
, headerSize
;
1195 LPBITMAPINFO lpDIBheader
= NULL
;
1196 LPPALETTEENTRY lpPe
= NULL
;
1199 // Allocate space for a DIB header
1200 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1201 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1202 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1204 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1206 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1208 // Fill in the static parts of the DIB header
1209 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1210 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1211 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1212 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1214 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1215 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1216 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1217 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1218 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1221 // Initialize the DIB palette
1222 for (i
= 0; i
< 256; i
++) {
1223 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1224 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1225 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1226 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1229 *lpDIBHeader
= lpDIBheader
;
1234 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1240 // ----------------------------------------------------------------------------
1241 // other helper functions
1242 // ----------------------------------------------------------------------------
1244 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1246 #ifndef __WXMICROWIN__
1247 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1249 // get width/height from the bitmap if not given
1253 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1258 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1259 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1260 if ( !hdcSrc
|| !hdcDst
)
1262 wxLogLastError(wxT("CreateCompatibleDC"));
1265 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1268 wxLogLastError(wxT("CreateBitmap"));
1271 ::SelectObject(hdcSrc
, hbmpMask
);
1272 ::SelectObject(hdcDst
, hbmpInvMask
);
1273 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1277 wxLogLastError(wxT("BitBlt"));