]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
0118ff56bdeac342f0f13becc8329a51aed279cd
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
)
337 m_refData
= new wxBitmapRefData
;
339 GetBitmapData()->m_width
= w
;
340 GetBitmapData()->m_height
= h
;
341 GetBitmapData()->m_depth
= d
;
344 #ifndef __WXMICROWIN__
347 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
350 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
374 // ----------------------------------------------------------------------------
375 // wxImage to/from conversions
376 // ----------------------------------------------------------------------------
380 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
382 #ifdef __WXMICROWIN__
384 // Initial attempt at a simple-minded implementation.
385 // The bitmap will always be created at the screen depth,
386 // so the 'depth' argument is ignored.
387 // TODO: transparency (create a mask image)
389 HDC hScreenDC
= ::GetDC(NULL
);
390 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
392 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
395 ::ReleaseDC(NULL
, hScreenDC
);
398 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
399 ::ReleaseDC(NULL
, hScreenDC
);
401 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
404 for (i
= 0; i
< image
.GetWidth(); i
++)
406 for (j
= 0; j
< image
.GetHeight(); j
++)
408 unsigned char red
= image
.GetRed(i
, j
);
409 unsigned char green
= image
.GetGreen(i
, j
);
410 unsigned char blue
= image
.GetBlue(i
, j
);
412 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
416 ::SelectObject(hMemDC
, hOldBitmap
);
419 m_refData
= new wxBitmapRefData();
421 SetWidth(image
.GetWidth());
422 SetHeight(image
.GetHeight());
423 SetDepth(screenDepth
);
424 SetHBITMAP( (WXHBITMAP
) hBitmap
);
427 // Copy the palette from the source image
428 SetPalette(image
.GetPalette());
429 #endif // wxUSE_PALETTE
434 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
436 m_refData
= new wxBitmapRefData();
438 // sizeLimit is the MS upper limit for the DIB size
440 int sizeLimit
= 1024*768*3;
442 int sizeLimit
= 0x7fff ;
445 // width and height of the device-dependent bitmap
446 int width
= image
.GetWidth();
447 int bmpHeight
= image
.GetHeight();
449 // calc the number of bytes per scanline and padding
450 int bytePerLine
= width
*3;
451 int sizeDWORD
= sizeof( DWORD
);
452 int lineBoundary
= bytePerLine
% sizeDWORD
;
454 if( lineBoundary
> 0 )
456 padding
= sizeDWORD
- lineBoundary
;
457 bytePerLine
+= padding
;
459 // calc the number of DIBs and heights of DIBs
462 int height
= sizeLimit
/bytePerLine
;
463 if( height
>= bmpHeight
)
467 numDIB
= bmpHeight
/ height
;
468 hRemain
= bmpHeight
% height
;
469 if( hRemain
>0 ) numDIB
++;
472 // set bitmap parameters
473 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
475 SetHeight( bmpHeight
);
476 if (depth
== -1) depth
= wxDisplayDepth();
480 // Copy the palette from the source image
481 SetPalette(image
.GetPalette());
482 #endif // wxUSE_PALETTE
484 // create a DIB header
485 int headersize
= sizeof(BITMAPINFOHEADER
);
486 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
487 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
488 // Fill in the DIB header
489 lpDIBh
->bmiHeader
.biSize
= headersize
;
490 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
491 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
492 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
493 // the general formula for biSizeImage:
494 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
495 lpDIBh
->bmiHeader
.biPlanes
= 1;
496 lpDIBh
->bmiHeader
.biBitCount
= 24;
497 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
498 lpDIBh
->bmiHeader
.biClrUsed
= 0;
499 // These seem not really needed for our purpose here.
500 lpDIBh
->bmiHeader
.biClrImportant
= 0;
501 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
502 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
503 // memory for DIB data
504 unsigned char *lpBits
;
505 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
508 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
513 // create and set the device-dependent bitmap
514 HDC hdc
= ::GetDC(NULL
);
515 HDC memdc
= ::CreateCompatibleDC( hdc
);
517 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
518 ::SelectObject( memdc
, hbitmap
);
521 HPALETTE hOldPalette
= 0;
522 if (image
.GetPalette().Ok())
524 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
525 ::RealizePalette(memdc
);
527 #endif // wxUSE_PALETTE
529 // copy image data into DIB data and then into DDB (in a loop)
530 unsigned char *data
= image
.GetData();
533 unsigned char *ptdata
= data
;
534 unsigned char *ptbits
;
536 for( n
=0; n
<numDIB
; n
++ )
538 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
540 // redefine height and size of the (possibly) last smaller DIB
541 // memory is not reallocated
543 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
544 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
548 for( j
=0; j
<height
; j
++ )
550 for( i
=0; i
<width
; i
++ )
552 *(ptbits
++) = *(ptdata
+2);
553 *(ptbits
++) = *(ptdata
+1);
554 *(ptbits
++) = *(ptdata
);
557 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
559 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
560 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
562 // if numDIB = 1, lines below can also be used
563 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
564 // The above line is equivalent to the following two lines.
565 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
566 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
567 // or the following lines
568 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
569 // HDC memdc = ::CreateCompatibleDC( hdc );
570 // ::SelectObject( memdc, hbitmap);
571 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
572 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
573 // ::SelectObject( memdc, 0 );
574 // ::DeleteDC( memdc );
576 SetHBITMAP( (WXHBITMAP
) hbitmap
);
580 SelectPalette(memdc
, hOldPalette
, FALSE
);
581 #endif // wxUSE_PALETTE
583 // similarly, created an mono-bitmap for the possible mask
584 if( image
.HasMask() )
586 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
587 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
588 if( numDIB
== 1 ) height
= bmpHeight
;
589 else height
= sizeLimit
/bytePerLine
;
590 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
591 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
593 unsigned char r
= image
.GetMaskRed();
594 unsigned char g
= image
.GetMaskGreen();
595 unsigned char b
= image
.GetMaskBlue();
596 unsigned char zero
= 0, one
= 255;
598 for( n
=0; n
<numDIB
; n
++ )
600 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
602 // redefine height and size of the (possibly) last smaller DIB
603 // memory is not reallocated
605 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
606 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
609 for( int j
=0; j
<height
; j
++ )
611 for(i
=0; i
<width
; i
++ )
613 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
614 unsigned char cr
= (*(ptdata
++)) ;
615 unsigned char cg
= (*(ptdata
++)) ;
616 unsigned char cb
= (*(ptdata
++)) ;
618 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
631 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
633 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
634 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
637 // create a wxMask object
638 wxMask
*mask
= new wxMask();
639 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
641 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
642 /* The following can also be used but is slow to run
643 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
644 wxMask *mask = new wxMask( *this, colour );
648 ::SelectObject( memdc
, hbmpOld
);
651 // free allocated resources
653 ::ReleaseDC(NULL
, hdc
);
657 #if WXWIN_COMPATIBILITY_2
658 // check the wxBitmap object
659 GetBitmapData()->SetOk();
660 #endif // WXWIN_COMPATIBILITY_2
666 wxImage
wxBitmap::ConvertToImage() const
668 #ifdef __WXMICROWIN__
669 // Initial attempt at a simple-minded implementation.
670 // The bitmap will always be created at the screen depth,
671 // so the 'depth' argument is ignored.
672 // TODO: transparency (create a mask image)
676 wxFAIL_MSG( wxT("bitmap is invalid") );
682 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
684 // create an wxImage object
685 int width
= GetWidth();
686 int height
= GetHeight();
687 image
.Create( width
, height
);
688 unsigned char *data
= image
.GetData();
691 wxFAIL_MSG( wxT("could not allocate data for image") );
695 HDC hScreenDC
= ::GetDC(NULL
);
697 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
698 ::ReleaseDC(NULL
, hScreenDC
);
700 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
702 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
705 for (i
= 0; i
< GetWidth(); i
++)
707 for (j
= 0; j
< GetHeight(); j
++)
709 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
710 unsigned char red
= GetRValue(color
);
711 unsigned char green
= GetGValue(color
);
712 unsigned char blue
= GetBValue(color
);
714 image
.SetRGB(i
, j
, red
, green
, blue
);
718 ::SelectObject(hMemDC
, hOldBitmap
);
722 // Copy the palette from the source image
724 image
.SetPalette(* GetPalette());
725 #endif // wxUSE_PALETTE
729 #else // __MICROWIN__
733 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
735 // create an wxImage object
736 int width
= GetWidth();
737 int height
= GetHeight();
738 image
.Create( width
, height
);
739 unsigned char *data
= image
.GetData();
742 wxFAIL_MSG( wxT("could not allocate data for image") );
746 // calc the number of bytes per scanline and padding in the DIB
747 int bytePerLine
= width
*3;
748 int sizeDWORD
= sizeof( DWORD
);
749 int lineBoundary
= bytePerLine
% sizeDWORD
;
751 if( lineBoundary
> 0 )
753 padding
= sizeDWORD
- lineBoundary
;
754 bytePerLine
+= padding
;
757 // create a DIB header
758 int headersize
= sizeof(BITMAPINFOHEADER
);
759 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
762 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
766 // Fill in the DIB header
767 lpDIBh
->bmiHeader
.biSize
= headersize
;
768 lpDIBh
->bmiHeader
.biWidth
= width
;
769 lpDIBh
->bmiHeader
.biHeight
= -height
;
770 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
771 lpDIBh
->bmiHeader
.biPlanes
= 1;
772 lpDIBh
->bmiHeader
.biBitCount
= 24;
773 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
774 lpDIBh
->bmiHeader
.biClrUsed
= 0;
775 // These seem not really needed for our purpose here.
776 lpDIBh
->bmiHeader
.biClrImportant
= 0;
777 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
778 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
779 // memory for DIB data
780 unsigned char *lpBits
;
781 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
784 wxFAIL_MSG( wxT("could not allocate data for DIB") );
790 // copy data from the device-dependent bitmap to the DIB
791 HDC hdc
= ::GetDC(NULL
);
793 hbitmap
= (HBITMAP
) GetHBITMAP();
794 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
796 // copy DIB data into the wxImage object
798 unsigned char *ptdata
= data
;
799 unsigned char *ptbits
= lpBits
;
800 for( i
=0; i
<height
; i
++ )
802 for( j
=0; j
<width
; j
++ )
804 *(ptdata
++) = *(ptbits
+2);
805 *(ptdata
++) = *(ptbits
+1);
806 *(ptdata
++) = *(ptbits
);
812 // similarly, set data according to the possible mask bitmap
813 if( GetMask() && GetMask()->GetMaskBitmap() )
815 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
816 // memory DC created, color set, data copied, and memory DC deleted
817 HDC memdc
= ::CreateCompatibleDC( hdc
);
818 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
819 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
820 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
822 // background color set to RGB(16,16,16) in consistent with wxGTK
823 unsigned char r
=16, g
=16, b
=16;
826 for( i
=0; i
<height
; i
++ )
828 for( j
=0; j
<width
; j
++ )
842 image
.SetMaskColour( r
, g
, b
);
843 image
.SetMask( TRUE
);
847 image
.SetMask( FALSE
);
849 // free allocated resources
850 ::ReleaseDC(NULL
, hdc
);
858 #endif // wxUSE_IMAGE
860 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
864 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
868 m_refData
= new wxBitmapRefData
;
870 return handler
->LoadFile(this, filename
, type
, -1, -1);
876 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
878 *this = image
.ConvertToBitmap();
883 #endif // wxUSE_IMAGE
888 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
892 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
896 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
901 m_refData
= new wxBitmapRefData
;
903 return handler
->Create(this, data
, type
, width
, height
, depth
);
906 bool wxBitmap::SaveFile(const wxString
& filename
,
908 const wxPalette
*palette
)
910 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
914 return handler
->SaveFile(this, filename
, type
, palette
);
919 // FIXME what about palette? shouldn't we use it?
920 wxImage
image( *this );
923 return image
.SaveFile(filename
, type
);
926 #endif // wxUSE_IMAGE
931 // ----------------------------------------------------------------------------
932 // sub bitmap extraction
933 // ----------------------------------------------------------------------------
935 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
937 #ifndef __WXMICROWIN__
939 (rect
.x
>= 0) && (rect
.y
>= 0) &&
940 (rect
.x
+rect
.width
<= GetWidth()) &&
941 (rect
.y
+rect
.height
<= GetHeight()),
942 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
944 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
945 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
948 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
949 HDC dcDst
= ::CreateCompatibleDC(NULL
);
950 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
951 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
952 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
954 // copy mask if there is one
957 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
959 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
960 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
961 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
963 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
967 SelectObject(dcDst
, NULL
);
968 SelectObject(dcSrc
, NULL
);
978 // ----------------------------------------------------------------------------
979 // wxBitmap accessors
980 // ----------------------------------------------------------------------------
982 void wxBitmap::SetQuality(int q
)
986 GetBitmapData()->m_quality
= q
;
989 #if WXWIN_COMPATIBILITY_2
990 void wxBitmap::SetOk(bool isOk
)
994 GetBitmapData()->m_ok
= isOk
;
996 #endif // WXWIN_COMPATIBILITY_2
1000 void wxBitmap::SetPalette(const wxPalette
& palette
)
1004 GetBitmapData()->m_bitmapPalette
= palette
;
1007 #endif // wxUSE_PALETTE
1009 void wxBitmap::SetMask(wxMask
*mask
)
1013 GetBitmapData()->m_bitmapMask
= mask
;
1016 // Creates a bitmap that matches the device context, from
1017 // an arbitray bitmap. At present, the original bitmap must have an
1018 // associated palette. TODO: use a default palette if no palette exists.
1019 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1020 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1022 #ifdef __WXMICROWIN__
1026 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1027 HPALETTE hPal
= (HPALETTE
) NULL
;
1029 void *lpBits
= (void*) NULL
;
1032 if( GetPalette() && GetPalette()->Ok() )
1034 tmpBitmap
.SetPalette(*GetPalette());
1035 memDC
.SelectObject(tmpBitmap
);
1036 memDC
.SetPalette(*GetPalette());
1037 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1041 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1043 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1044 tmpBitmap
.SetPalette( palette
);
1045 memDC
.SelectObject(tmpBitmap
);
1046 memDC
.SetPalette( palette
);
1048 #else // !wxUSE_PALETTE
1049 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1050 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1052 // set the height negative because in a DIB the order of the lines is
1054 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1056 return wxNullBitmap
;
1059 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1061 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1063 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1064 GetWidth(), GetHeight(),
1065 0, 0, 0, GetHeight(),
1066 lpBits
, lpDib
, DIB_RGB_COLORS
);
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1085 // Construct a mask from a bitmap and a colour indicating
1086 // the transparent area
1087 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1090 Create(bitmap
, colour
);
1093 // Construct a mask from a bitmap and a palette index indicating
1094 // the transparent area
1095 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1098 Create(bitmap
, paletteIndex
);
1101 // Construct a mask from a mono bitmap (copies the bitmap).
1102 wxMask::wxMask(const wxBitmap
& bitmap
)
1111 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1114 // Create a mask from a mono bitmap (copies the bitmap).
1115 bool wxMask::Create(const wxBitmap
& bitmap
)
1117 #ifndef __WXMICROWIN__
1118 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1119 _T("can't create mask from invalid or not monochrome bitmap") );
1123 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1127 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1132 HDC srcDC
= CreateCompatibleDC(0);
1133 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1134 HDC destDC
= CreateCompatibleDC(0);
1135 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1136 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1137 SelectObject(srcDC
, 0);
1139 SelectObject(destDC
, 0);
1147 // Create a mask from a bitmap and a palette index indicating
1148 // the transparent area
1149 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1153 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1158 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1160 unsigned char red
, green
, blue
;
1161 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1163 wxColour
transparentColour(red
, green
, blue
);
1164 return Create(bitmap
, transparentColour
);
1167 #endif // wxUSE_PALETTE
1172 // Create a mask from a bitmap and a colour indicating
1173 // the transparent area
1174 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1176 #ifndef __WXMICROWIN__
1177 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1181 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1185 int width
= bitmap
.GetWidth(),
1186 height
= bitmap
.GetHeight();
1188 // scan the bitmap for the transparent colour and set the corresponding
1189 // pixels in the mask to BLACK and the rest to WHITE
1190 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1191 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1193 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1194 HDC destDC
= ::CreateCompatibleDC(NULL
);
1195 if ( !srcDC
|| !destDC
)
1197 wxLogLastError(wxT("CreateCompatibleDC"));
1202 // SelectObject() will fail
1203 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1204 _T("bitmap can't be selected in another DC") );
1206 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1209 wxLogLastError(wxT("SelectObject"));
1214 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1217 wxLogLastError(wxT("SelectObject"));
1222 // this is not very efficient, but I can't think of a better way of doing
1224 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1226 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1228 COLORREF col
= GetPixel(srcDC
, w
, h
);
1229 if ( col
== CLR_INVALID
)
1231 wxLogLastError(wxT("GetPixel"));
1233 // doesn't make sense to continue
1239 if ( col
== maskColour
)
1241 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1245 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1250 ::SelectObject(srcDC
, hbmpSrcOld
);
1252 ::SelectObject(destDC
, hbmpDstOld
);
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1265 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1268 int width
, int height
, int depth
)
1270 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1272 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1275 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1276 const wxString
& name
,
1278 int width
, int height
)
1280 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1282 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1285 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1286 const wxString
& name
,
1289 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1291 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1294 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1295 void *WXUNUSED(data
),
1296 long WXUNUSED(type
),
1297 int WXUNUSED(width
),
1298 int WXUNUSED(height
),
1299 int WXUNUSED(depth
))
1304 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1305 const wxString
& WXUNUSED(name
),
1306 long WXUNUSED(type
),
1307 int WXUNUSED(desiredWidth
),
1308 int WXUNUSED(desiredHeight
))
1313 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1314 const wxString
& WXUNUSED(name
),
1316 const wxPalette
*WXUNUSED(palette
))
1321 // ----------------------------------------------------------------------------
1323 // ----------------------------------------------------------------------------
1325 #ifndef __WXMICROWIN__
1326 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1327 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1329 unsigned long i
, headerSize
;
1330 LPBITMAPINFO lpDIBheader
= NULL
;
1331 LPPALETTEENTRY lpPe
= NULL
;
1334 // Allocate space for a DIB header
1335 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1336 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1337 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1339 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1341 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1343 // Fill in the static parts of the DIB header
1344 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1345 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1346 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1347 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1349 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1350 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1351 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1352 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1353 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1356 // Initialize the DIB palette
1357 for (i
= 0; i
< 256; i
++) {
1358 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1359 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1360 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1361 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1364 *lpDIBHeader
= lpDIBheader
;
1369 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1375 // ----------------------------------------------------------------------------
1376 // other helper functions
1377 // ----------------------------------------------------------------------------
1379 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1381 #ifndef __WXMICROWIN__
1382 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1384 // get width/height from the bitmap if not given
1388 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1393 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1394 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1395 if ( !hdcSrc
|| !hdcDst
)
1397 wxLogLastError(wxT("CreateCompatibleDC"));
1400 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1403 wxLogLastError(wxT("CreateBitmap"));
1406 ::SelectObject(hdcSrc
, hbmpMask
);
1407 ::SelectObject(hdcDst
, hbmpInvMask
);
1408 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1412 wxLogLastError(wxT("BitBlt"));