]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
79b21b8e874799a90b0fdb19607ddbcbe5634bdf
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
46 #include "wx/msw/dib.h"
48 #include "wx/xpmdecod.h"
50 // missing from mingw32 header
52 #define CLR_INVALID ((COLORREF)-1)
53 #endif // no CLR_INVALID
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
60 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 wxBitmapRefData::wxBitmapRefData()
75 m_selectedInto
= NULL
;
78 m_hBitmap
= (WXHBITMAP
) NULL
;
81 void wxBitmapRefData::Free()
83 wxASSERT_MSG( !m_selectedInto
,
84 wxT("deleting bitmap still selected into wxMemoryDC") );
88 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
90 wxLogLastError(wxT("DeleteObject(hbitmap)"));
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // this function should be called from all wxBitmap ctors
103 void wxBitmap::Init()
105 // m_refData = NULL; done in the base class ctor
107 if ( wxTheBitmapList
)
108 wxTheBitmapList
->AddBitmap(this);
113 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
115 // it may be either HICON or HCURSOR
116 HICON hicon
= (HICON
)icon
.GetHandle();
119 if ( !::GetIconInfo(hicon
, &iconInfo
) )
121 wxLogLastError(wxT("GetIconInfo"));
126 wxBitmapRefData
*refData
= new wxBitmapRefData
;
129 int w
= icon
.GetWidth(),
130 h
= icon
.GetHeight();
132 refData
->m_width
= w
;
133 refData
->m_height
= h
;
134 refData
->m_depth
= wxDisplayDepth();
136 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
138 // the mask returned by GetIconInfo() is inversed compared to the usual
140 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
141 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
143 #if WXWIN_COMPATIBILITY_2
144 refData
->m_ok
= TRUE
;
145 #endif // WXWIN_COMPATIBILITY_2
152 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
160 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
164 return CopyFromIconOrCursor(cursor
);
168 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
175 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
176 // to create a bitmap from icon there - but using this way we won't have
179 int width
= icon
.GetWidth(),
180 height
= icon
.GetHeight();
182 // copy the icon to the bitmap
184 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
185 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
186 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
188 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
190 ::SelectObject(hdc
, hbmpOld
);
193 wxBitmapRefData
*refData
= new wxBitmapRefData
;
196 refData
->m_width
= width
;
197 refData
->m_height
= height
;
198 refData
->m_depth
= wxDisplayDepth();
200 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
202 #if WXWIN_COMPATIBILITY_2
203 refData
->m_ok
= TRUE
;
204 #endif // WXWIN_COMPATIBILITY_2
208 return CopyFromIconOrCursor(icon
);
209 #endif // Win16/Win32
212 wxBitmap::~wxBitmap()
215 wxTheBitmapList
->DeleteObject(this);
218 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
222 wxBitmapRefData
*refData
= new wxBitmapRefData
;
225 refData
->m_width
= width
;
226 refData
->m_height
= height
;
227 refData
->m_depth
= depth
;
228 refData
->m_numColors
= 0;
229 refData
->m_selectedInto
= NULL
;
234 // we assume that it is in XBM format which is not quite the same as
235 // the format CreateBitmap() wants because the order of bytes in the
237 static const size_t bytesPerLine
= (width
+ 7) / 8;
238 static const size_t padding
= bytesPerLine
% 2;
239 static const size_t len
= height
* ( padding
+ bytesPerLine
);
240 data
= (char *)malloc(len
);
241 const char *src
= bits
;
244 for ( int rows
= 0; rows
< height
; rows
++ )
246 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
248 unsigned char val
= *src
++;
249 unsigned char reversed
= 0;
251 for ( int bits
= 0; bits
< 8; bits
++)
254 reversed
|= (val
& 0x01);
266 // bits should already be in Windows standard format
267 data
= (char *)bits
; // const_cast is harmless
270 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
273 wxLogLastError(wxT("CreateBitmap"));
281 SetHBITMAP((WXHBITMAP
)hbmp
);
284 // Create from XPM data
285 bool wxBitmap::CreateFromXpm(const char **data
)
287 #if wxUSE_IMAGE && wxUSE_XPM
290 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
292 wxXPMDecoder decoder
;
293 wxImage img
= decoder
.ReadData(data
);
294 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
296 *this = wxBitmap(img
);
298 if ( wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
306 wxBitmap::wxBitmap(int w
, int h
, int d
)
310 (void)Create(w
, h
, d
);
313 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
317 (void)Create(data
, type
, width
, height
, depth
);
320 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
324 LoadFile(filename
, (int)type
);
327 bool wxBitmap::Create(int w
, int h
, int d
)
331 m_refData
= new wxBitmapRefData
;
333 GetBitmapData()->m_width
= w
;
334 GetBitmapData()->m_height
= h
;
335 GetBitmapData()->m_depth
= d
;
341 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
344 wxLogLastError(wxT("CreateBitmap"));
350 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
353 wxLogLastError(wxT("CreateCompatibleBitmap"));
356 GetBitmapData()->m_depth
= wxDisplayDepth();
359 SetHBITMAP((WXHBITMAP
)hbmp
);
361 #if WXWIN_COMPATIBILITY_2
362 GetBitmapData()->m_ok
= hbmp
!= 0;
363 #endif // WXWIN_COMPATIBILITY_2
368 // ----------------------------------------------------------------------------
369 // wxImage to/from conversions
370 // ----------------------------------------------------------------------------
374 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
376 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
378 m_refData
= new wxBitmapRefData();
380 // sizeLimit is the MS upper limit for the DIB size
382 int sizeLimit
= 1024*768*3;
384 int sizeLimit
= 0x7fff ;
387 // width and height of the device-dependent bitmap
388 int width
= image
.GetWidth();
389 int bmpHeight
= image
.GetHeight();
391 // calc the number of bytes per scanline and padding
392 int bytePerLine
= width
*3;
393 int sizeDWORD
= sizeof( DWORD
);
394 int lineBoundary
= bytePerLine
% sizeDWORD
;
396 if( lineBoundary
> 0 )
398 padding
= sizeDWORD
- lineBoundary
;
399 bytePerLine
+= padding
;
401 // calc the number of DIBs and heights of DIBs
404 int height
= sizeLimit
/bytePerLine
;
405 if( height
>= bmpHeight
)
409 numDIB
= bmpHeight
/ height
;
410 hRemain
= bmpHeight
% height
;
411 if( hRemain
>0 ) numDIB
++;
414 // set bitmap parameters
415 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
417 SetHeight( bmpHeight
);
418 if (depth
== -1) depth
= wxDisplayDepth();
421 // create a DIB header
422 int headersize
= sizeof(BITMAPINFOHEADER
);
423 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
424 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
425 // Fill in the DIB header
426 lpDIBh
->bmiHeader
.biSize
= headersize
;
427 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
428 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
429 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
430 // the general formula for biSizeImage:
431 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
432 lpDIBh
->bmiHeader
.biPlanes
= 1;
433 lpDIBh
->bmiHeader
.biBitCount
= 24;
434 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
435 lpDIBh
->bmiHeader
.biClrUsed
= 0;
436 // These seem not really needed for our purpose here.
437 lpDIBh
->bmiHeader
.biClrImportant
= 0;
438 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
439 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
440 // memory for DIB data
441 unsigned char *lpBits
;
442 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
445 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
450 // create and set the device-dependent bitmap
451 HDC hdc
= ::GetDC(NULL
);
452 HDC memdc
= ::CreateCompatibleDC( hdc
);
454 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
455 ::SelectObject( memdc
, hbitmap
);
457 HPALETTE hOldPalette
= 0;
458 if (image
.GetPalette().Ok())
460 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
461 ::RealizePalette(memdc
);
464 // copy image data into DIB data and then into DDB (in a loop)
465 unsigned char *data
= image
.GetData();
468 unsigned char *ptdata
= data
;
469 unsigned char *ptbits
;
471 for( n
=0; n
<numDIB
; n
++ )
473 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
475 // redefine height and size of the (possibly) last smaller DIB
476 // memory is not reallocated
478 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
479 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
483 for( j
=0; j
<height
; j
++ )
485 for( i
=0; i
<width
; i
++ )
487 *(ptbits
++) = *(ptdata
+2);
488 *(ptbits
++) = *(ptdata
+1);
489 *(ptbits
++) = *(ptdata
);
492 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
494 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
495 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
497 // if numDIB = 1, lines below can also be used
498 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
499 // The above line is equivalent to the following two lines.
500 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
501 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
502 // or the following lines
503 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
504 // HDC memdc = ::CreateCompatibleDC( hdc );
505 // ::SelectObject( memdc, hbitmap);
506 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
507 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
508 // ::SelectObject( memdc, 0 );
509 // ::DeleteDC( memdc );
511 SetHBITMAP( (WXHBITMAP
) hbitmap
);
514 SelectPalette(memdc
, hOldPalette
, FALSE
);
516 // similarly, created an mono-bitmap for the possible mask
517 if( image
.HasMask() )
519 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
520 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
521 if( numDIB
== 1 ) height
= bmpHeight
;
522 else height
= sizeLimit
/bytePerLine
;
523 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
524 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
526 unsigned char r
= image
.GetMaskRed();
527 unsigned char g
= image
.GetMaskGreen();
528 unsigned char b
= image
.GetMaskBlue();
529 unsigned char zero
= 0, one
= 255;
531 for( n
=0; n
<numDIB
; n
++ )
533 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
535 // redefine height and size of the (possibly) last smaller DIB
536 // memory is not reallocated
538 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
539 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
542 for( int j
=0; j
<height
; j
++ )
544 for(i
=0; i
<width
; i
++ )
546 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
547 unsigned char cr
= (*(ptdata
++)) ;
548 unsigned char cg
= (*(ptdata
++)) ;
549 unsigned char cb
= (*(ptdata
++)) ;
551 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
564 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
566 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
567 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
570 // create a wxMask object
571 wxMask
*mask
= new wxMask();
572 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
574 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
575 /* The following can also be used but is slow to run
576 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
577 wxMask *mask = new wxMask( *this, colour );
581 ::SelectObject( memdc
, hbmpOld
);
584 // free allocated resources
586 ::ReleaseDC(NULL
, hdc
);
590 #if WXWIN_COMPATIBILITY_2
591 // check the wxBitmap object
592 GetBitmapData()->SetOk();
593 #endif // WXWIN_COMPATIBILITY_2
595 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
600 wxImage
wxBitmap::ConvertToImage() const
604 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
606 // create an wxImage object
607 int width
= GetWidth();
608 int height
= GetHeight();
609 image
.Create( width
, height
);
610 unsigned char *data
= image
.GetData();
613 wxFAIL_MSG( wxT("could not allocate data for image") );
617 // calc the number of bytes per scanline and padding in the DIB
618 int bytePerLine
= width
*3;
619 int sizeDWORD
= sizeof( DWORD
);
620 int lineBoundary
= bytePerLine
% sizeDWORD
;
622 if( lineBoundary
> 0 )
624 padding
= sizeDWORD
- lineBoundary
;
625 bytePerLine
+= padding
;
628 // create a DIB header
629 int headersize
= sizeof(BITMAPINFOHEADER
);
630 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
633 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
637 // Fill in the DIB header
638 lpDIBh
->bmiHeader
.biSize
= headersize
;
639 lpDIBh
->bmiHeader
.biWidth
= width
;
640 lpDIBh
->bmiHeader
.biHeight
= -height
;
641 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
642 lpDIBh
->bmiHeader
.biPlanes
= 1;
643 lpDIBh
->bmiHeader
.biBitCount
= 24;
644 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
645 lpDIBh
->bmiHeader
.biClrUsed
= 0;
646 // These seem not really needed for our purpose here.
647 lpDIBh
->bmiHeader
.biClrImportant
= 0;
648 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
649 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
650 // memory for DIB data
651 unsigned char *lpBits
;
652 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
655 wxFAIL_MSG( wxT("could not allocate data for DIB") );
661 // copy data from the device-dependent bitmap to the DIB
662 HDC hdc
= ::GetDC(NULL
);
664 hbitmap
= (HBITMAP
) GetHBITMAP();
665 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
667 // copy DIB data into the wxImage object
669 unsigned char *ptdata
= data
;
670 unsigned char *ptbits
= lpBits
;
671 for( i
=0; i
<height
; i
++ )
673 for( j
=0; j
<width
; j
++ )
675 *(ptdata
++) = *(ptbits
+2);
676 *(ptdata
++) = *(ptbits
+1);
677 *(ptdata
++) = *(ptbits
);
683 // similarly, set data according to the possible mask bitmap
684 if( GetMask() && GetMask()->GetMaskBitmap() )
686 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
687 // memory DC created, color set, data copied, and memory DC deleted
688 HDC memdc
= ::CreateCompatibleDC( hdc
);
689 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
690 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
691 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
693 // background color set to RGB(16,16,16) in consistent with wxGTK
694 unsigned char r
=16, g
=16, b
=16;
697 for( i
=0; i
<height
; i
++ )
699 for( j
=0; j
<width
; j
++ )
713 image
.SetMaskColour( r
, g
, b
);
714 image
.SetMask( TRUE
);
718 image
.SetMask( FALSE
);
720 // free allocated resources
721 ::ReleaseDC(NULL
, hdc
);
728 #endif // wxUSE_IMAGE
730 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
734 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
738 m_refData
= new wxBitmapRefData
;
740 return handler
->LoadFile(this, filename
, type
, -1, -1);
746 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
748 *this = image
.ConvertToBitmap();
753 #endif // wxUSE_IMAGE
758 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
762 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
766 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
771 m_refData
= new wxBitmapRefData
;
773 return handler
->Create(this, data
, type
, width
, height
, depth
);
776 bool wxBitmap::SaveFile(const wxString
& filename
, int type
, const wxPalette
*palette
)
778 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
782 return handler
->SaveFile(this, filename
, type
, palette
);
787 // FIXME what about palette? shouldn't we use it?
788 wxImage
image( *this );
791 return image
.SaveFile(filename
, type
);
794 #endif // wxUSE_IMAGE
799 // ----------------------------------------------------------------------------
800 // sub bitmap extraction
801 // ----------------------------------------------------------------------------
803 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
806 (rect
.x
>= 0) && (rect
.y
>= 0) &&
807 (rect
.x
+rect
.width
<= GetWidth()) &&
808 (rect
.y
+rect
.height
<= GetHeight()),
809 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
811 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
812 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
815 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
816 HDC dcDst
= ::CreateCompatibleDC(NULL
);
817 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
818 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
819 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
821 // copy mask if there is one
824 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
826 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
827 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
828 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
830 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
834 SelectObject(dcDst
, NULL
);
835 SelectObject(dcSrc
, NULL
);
842 // ----------------------------------------------------------------------------
843 // wxBitmap accessors
844 // ----------------------------------------------------------------------------
846 void wxBitmap::SetQuality(int q
)
850 GetBitmapData()->m_quality
= q
;
853 #if WXWIN_COMPATIBILITY_2
854 void wxBitmap::SetOk(bool isOk
)
858 GetBitmapData()->m_ok
= isOk
;
860 #endif // WXWIN_COMPATIBILITY_2
862 void wxBitmap::SetPalette(const wxPalette
& palette
)
866 GetBitmapData()->m_bitmapPalette
= palette
;
869 void wxBitmap::SetMask(wxMask
*mask
)
873 GetBitmapData()->m_bitmapMask
= mask
;
876 // Creates a bitmap that matches the device context, from
877 // an arbitray bitmap. At present, the original bitmap must have an
878 // associated palette. TODO: use a default palette if no palette exists.
879 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
880 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
883 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
884 HPALETTE hPal
= (HPALETTE
) NULL
;
886 void *lpBits
= (void*) NULL
;
888 if( GetPalette() && GetPalette()->Ok() )
890 tmpBitmap
.SetPalette(*GetPalette());
891 memDC
.SelectObject(tmpBitmap
);
892 memDC
.SetPalette(*GetPalette());
893 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
897 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
899 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
900 tmpBitmap
.SetPalette( palette
);
901 memDC
.SelectObject(tmpBitmap
);
902 memDC
.SetPalette( palette
);
905 // set the height negative because in a DIB the order of the lines is
907 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
912 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
914 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
916 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
917 GetWidth(), GetHeight(),
918 0, 0, 0, GetHeight(),
919 lpBits
, lpDib
, DIB_RGB_COLORS
);
928 // ----------------------------------------------------------------------------
930 // ----------------------------------------------------------------------------
937 // Construct a mask from a bitmap and a colour indicating
938 // the transparent area
939 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
942 Create(bitmap
, colour
);
945 // Construct a mask from a bitmap and a palette index indicating
946 // the transparent area
947 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
950 Create(bitmap
, paletteIndex
);
953 // Construct a mask from a mono bitmap (copies the bitmap).
954 wxMask::wxMask(const wxBitmap
& bitmap
)
963 ::DeleteObject((HBITMAP
) m_maskBitmap
);
966 // Create a mask from a mono bitmap (copies the bitmap).
967 bool wxMask::Create(const wxBitmap
& bitmap
)
969 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
970 _T("can't create mask from invalid or not monochrome bitmap") );
974 ::DeleteObject((HBITMAP
) m_maskBitmap
);
978 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
983 HDC srcDC
= CreateCompatibleDC(0);
984 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
985 HDC destDC
= CreateCompatibleDC(0);
986 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
987 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
988 SelectObject(srcDC
, 0);
990 SelectObject(destDC
, 0);
995 // Create a mask from a bitmap and a palette index indicating
996 // the transparent area
997 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1001 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1004 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1006 unsigned char red
, green
, blue
;
1007 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1009 wxColour
transparentColour(red
, green
, blue
);
1010 return Create(bitmap
, transparentColour
);
1016 // Create a mask from a bitmap and a colour indicating
1017 // the transparent area
1018 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1020 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1024 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1028 int width
= bitmap
.GetWidth(),
1029 height
= bitmap
.GetHeight();
1031 // scan the bitmap for the transparent colour and set the corresponding
1032 // pixels in the mask to BLACK and the rest to WHITE
1033 COLORREF maskColour
= wxColourToRGB(colour
);
1034 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1036 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1037 HDC destDC
= ::CreateCompatibleDC(NULL
);
1038 if ( !srcDC
|| !destDC
)
1040 wxLogLastError(wxT("CreateCompatibleDC"));
1045 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1048 wxLogLastError(wxT("SelectObject"));
1053 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1056 wxLogLastError(wxT("SelectObject"));
1061 // this is not very efficient, but I can't think of a better way of doing
1063 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1065 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1067 COLORREF col
= GetPixel(srcDC
, w
, h
);
1068 if ( col
== CLR_INVALID
)
1070 wxLogLastError(wxT("GetPixel"));
1072 // doesn't make sense to continue
1078 if ( col
== maskColour
)
1080 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1084 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1089 ::SelectObject(srcDC
, hbmpSrcOld
);
1091 ::SelectObject(destDC
, hbmpDstOld
);
1097 // ----------------------------------------------------------------------------
1099 // ----------------------------------------------------------------------------
1101 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1104 int width
, int height
, int depth
)
1106 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1108 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1111 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1112 const wxString
& name
,
1114 int width
, int height
)
1116 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1118 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1121 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1122 const wxString
& name
,
1125 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1127 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1130 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1131 void *WXUNUSED(data
),
1132 long WXUNUSED(type
),
1133 int WXUNUSED(width
),
1134 int WXUNUSED(height
),
1135 int WXUNUSED(depth
))
1140 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1141 const wxString
& WXUNUSED(name
),
1142 long WXUNUSED(type
),
1143 int WXUNUSED(desiredWidth
),
1144 int WXUNUSED(desiredHeight
))
1149 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1150 const wxString
& WXUNUSED(name
),
1152 const wxPalette
*WXUNUSED(palette
))
1157 // ----------------------------------------------------------------------------
1159 // ----------------------------------------------------------------------------
1161 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1162 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1164 unsigned long i
, headerSize
;
1165 LPBITMAPINFO lpDIBheader
= NULL
;
1166 LPPALETTEENTRY lpPe
= NULL
;
1169 // Allocate space for a DIB header
1170 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1171 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1172 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1174 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1176 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1178 // Fill in the static parts of the DIB header
1179 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1180 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1181 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1182 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1184 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1185 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1186 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1187 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1188 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1191 // Initialize the DIB palette
1192 for (i
= 0; i
< 256; i
++) {
1193 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1194 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1195 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1196 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1199 *lpDIBHeader
= lpDIBheader
;
1204 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1209 // ----------------------------------------------------------------------------
1210 // other helper functions
1211 // ----------------------------------------------------------------------------
1213 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1215 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1217 // get width/height from the bitmap if not given
1221 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1226 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1227 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1228 if ( !hdcSrc
|| !hdcDst
)
1230 wxLogLastError(wxT("CreateCompatibleDC"));
1233 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1236 wxLogLastError(wxT("CreateBitmap"));
1239 ::SelectObject(hdcSrc
, hbmpMask
);
1240 ::SelectObject(hdcDst
, hbmpInvMask
);
1241 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1245 wxLogLastError(wxT("BitBlt"));