]>
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"
45 #include "wx/msw/private.h"
48 #if !defined(__WXMICROWIN__)
49 #include "wx/msw/dib.h"
53 #include "wx/xpmdecod.h"
55 // missing from mingw32 header
57 #define CLR_INVALID ((COLORREF)-1)
58 #endif // no CLR_INVALID
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
67 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 wxBitmapRefData::wxBitmapRefData()
80 m_selectedInto
= NULL
;
83 m_hBitmap
= (WXHBITMAP
) NULL
;
86 void wxBitmapRefData::Free()
88 wxASSERT_MSG( !m_selectedInto
,
89 wxT("deleting bitmap still selected into wxMemoryDC") );
93 // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap);
95 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
97 wxLogLastError(wxT("DeleteObject(hbitmap)"));
106 // ----------------------------------------------------------------------------
108 // ----------------------------------------------------------------------------
110 // this function should be called from all wxBitmap ctors
111 void wxBitmap::Init()
113 // m_refData = NULL; done in the base class ctor
119 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
121 #ifndef __WXMICROWIN__
122 // it may be either HICON or HCURSOR
123 HICON hicon
= (HICON
)icon
.GetHandle();
126 if ( !::GetIconInfo(hicon
, &iconInfo
) )
128 wxLogLastError(wxT("GetIconInfo"));
133 wxBitmapRefData
*refData
= new wxBitmapRefData
;
136 int w
= icon
.GetWidth(),
137 h
= icon
.GetHeight();
139 refData
->m_width
= w
;
140 refData
->m_height
= h
;
141 refData
->m_depth
= wxDisplayDepth();
143 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
145 // the mask returned by GetIconInfo() is inversed compared to the usual
147 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
148 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
151 // delete the old one now as we don't need it any more
152 ::DeleteObject(iconInfo
.hbmMask
);
154 #if WXWIN_COMPATIBILITY_2
155 refData
->m_ok
= TRUE
;
156 #endif // WXWIN_COMPATIBILITY_2
166 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
174 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
178 return CopyFromIconOrCursor(cursor
);
182 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
189 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
190 // to create a bitmap from icon there - but using this way we won't have
193 int width
= icon
.GetWidth(),
194 height
= icon
.GetHeight();
196 // copy the icon to the bitmap
198 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
199 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
200 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
202 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
204 ::SelectObject(hdc
, hbmpOld
);
207 wxBitmapRefData
*refData
= new wxBitmapRefData
;
210 refData
->m_width
= width
;
211 refData
->m_height
= height
;
212 refData
->m_depth
= wxDisplayDepth();
214 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
216 #if WXWIN_COMPATIBILITY_2
217 refData
->m_ok
= TRUE
;
218 #endif // WXWIN_COMPATIBILITY_2
222 return CopyFromIconOrCursor(icon
);
223 #endif // Win16/Win32
226 wxBitmap::~wxBitmap()
230 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
234 #ifndef __WXMICROWIN__
235 wxBitmapRefData
*refData
= new wxBitmapRefData
;
238 refData
->m_width
= width
;
239 refData
->m_height
= height
;
240 refData
->m_depth
= depth
;
241 refData
->m_numColors
= 0;
242 refData
->m_selectedInto
= NULL
;
247 // we assume that it is in XBM format which is not quite the same as
248 // the format CreateBitmap() wants because the order of bytes in the
250 const size_t bytesPerLine
= (width
+ 7) / 8;
251 const size_t padding
= bytesPerLine
% 2;
252 const size_t len
= height
* ( padding
+ bytesPerLine
);
253 data
= (char *)malloc(len
);
254 const char *src
= bits
;
257 for ( int rows
= 0; rows
< height
; rows
++ )
259 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
261 unsigned char val
= *src
++;
262 unsigned char reversed
= 0;
264 for ( int bits
= 0; bits
< 8; bits
++)
267 reversed
|= (val
& 0x01);
279 // bits should already be in Windows standard format
280 data
= (char *)bits
; // const_cast is harmless
283 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
286 wxLogLastError(wxT("CreateBitmap"));
294 SetHBITMAP((WXHBITMAP
)hbmp
);
298 // Create from XPM data
299 bool wxBitmap::CreateFromXpm(const char **data
)
301 #if wxUSE_IMAGE && wxUSE_XPM
304 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
306 wxXPMDecoder decoder
;
307 wxImage img
= decoder
.ReadData(data
);
308 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
310 *this = wxBitmap(img
);
317 wxBitmap::wxBitmap(int w
, int h
, int d
)
321 (void)Create(w
, h
, d
);
324 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
328 (void)Create(data
, type
, width
, height
, depth
);
331 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
335 LoadFile(filename
, (int)type
);
338 bool wxBitmap::Create(int w
, int h
, int d
)
342 m_refData
= new wxBitmapRefData
;
344 GetBitmapData()->m_width
= w
;
345 GetBitmapData()->m_height
= h
;
346 GetBitmapData()->m_depth
= d
;
349 #ifndef __WXMICROWIN__
352 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
355 wxLogLastError(wxT("CreateBitmap"));
362 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
365 wxLogLastError(wxT("CreateCompatibleBitmap"));
368 GetBitmapData()->m_depth
= wxDisplayDepth();
371 SetHBITMAP((WXHBITMAP
)hbmp
);
373 #if WXWIN_COMPATIBILITY_2
374 GetBitmapData()->m_ok
= hbmp
!= 0;
375 #endif // WXWIN_COMPATIBILITY_2
379 // ----------------------------------------------------------------------------
380 // wxImage to/from conversions
381 // ----------------------------------------------------------------------------
385 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
387 #ifdef __WXMICROWIN__
389 // Set this to 1 to experiment with mask code,
390 // which currently doesn't work
393 m_refData
= new wxBitmapRefData();
395 // Initial attempt at a simple-minded implementation.
396 // The bitmap will always be created at the screen depth,
397 // so the 'depth' argument is ignored.
399 HDC hScreenDC
= ::GetDC(NULL
);
400 // printf("Screen planes = %d, bpp = %d\n", hScreenDC->psd->planes, hScreenDC->psd->bpp);
401 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
403 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
404 HBITMAP hMaskBitmap
= NULL
;
405 HBITMAP hOldMaskBitmap
= NULL
;
407 unsigned char maskR
= 0;
408 unsigned char maskG
= 0;
409 unsigned char maskB
= 0;
411 // printf("Created bitmap %d\n", (int) hBitmap);
414 ::ReleaseDC(NULL
, hScreenDC
);
417 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
419 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
420 ::ReleaseDC(NULL
, hScreenDC
);
422 // created an mono-bitmap for the possible mask
423 bool hasMask
= image
.HasMask();
428 // FIXME: we should be able to pass bpp = 1, but
429 // GdBlit can't handle a different depth
431 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
433 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
435 maskR
= image
.GetMaskRed();
436 maskG
= image
.GetMaskGreen();
437 maskB
= image
.GetMaskBlue();
445 hScreenDC
= ::GetDC(NULL
);
446 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
447 ::ReleaseDC(NULL
, hScreenDC
);
449 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
457 for (i
= 0; i
< image
.GetWidth(); i
++)
459 for (j
= 0; j
< image
.GetHeight(); j
++)
461 unsigned char red
= image
.GetRed(i
, j
);
462 unsigned char green
= image
.GetGreen(i
, j
);
463 unsigned char blue
= image
.GetBlue(i
, j
);
465 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
469 // scan the bitmap for the transparent colour and set the corresponding
470 // pixels in the mask to BLACK and the rest to WHITE
471 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
472 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
474 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
479 ::SelectObject(hMemDC
, hOldBitmap
);
483 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
486 ((wxBitmapRefData
*)m_refData
)->m_bitmapMask
= new wxMask((WXHBITMAP
) hMaskBitmap
);
489 SetWidth(image
.GetWidth());
490 SetHeight(image
.GetHeight());
491 SetDepth(screenDepth
);
492 SetHBITMAP( (WXHBITMAP
) hBitmap
);
495 // Copy the palette from the source image
496 SetPalette(image
.GetPalette());
497 #endif // wxUSE_PALETTE
499 #if WXWIN_COMPATIBILITY_2
500 // check the wxBitmap object
501 GetBitmapData()->SetOk();
502 #endif // WXWIN_COMPATIBILITY_2
507 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
509 m_refData
= new wxBitmapRefData();
511 // sizeLimit is the MS upper limit for the DIB size
513 int sizeLimit
= 1024*768*3;
515 int sizeLimit
= 0x7fff ;
518 // width and height of the device-dependent bitmap
519 int width
= image
.GetWidth();
520 int bmpHeight
= image
.GetHeight();
522 // calc the number of bytes per scanline and padding
523 int bytePerLine
= width
*3;
524 int sizeDWORD
= sizeof( DWORD
);
525 int lineBoundary
= bytePerLine
% sizeDWORD
;
527 if( lineBoundary
> 0 )
529 padding
= sizeDWORD
- lineBoundary
;
530 bytePerLine
+= padding
;
532 // calc the number of DIBs and heights of DIBs
535 int height
= sizeLimit
/bytePerLine
;
536 if( height
>= bmpHeight
)
540 numDIB
= bmpHeight
/ height
;
541 hRemain
= bmpHeight
% height
;
542 if( hRemain
>0 ) numDIB
++;
545 // set bitmap parameters
546 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
548 SetHeight( bmpHeight
);
549 if (depth
== -1) depth
= wxDisplayDepth();
553 // Copy the palette from the source image
554 SetPalette(image
.GetPalette());
555 #endif // wxUSE_PALETTE
557 // create a DIB header
558 int headersize
= sizeof(BITMAPINFOHEADER
);
559 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
560 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
561 // Fill in the DIB header
562 lpDIBh
->bmiHeader
.biSize
= headersize
;
563 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
564 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
565 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
566 // the general formula for biSizeImage:
567 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
568 lpDIBh
->bmiHeader
.biPlanes
= 1;
569 lpDIBh
->bmiHeader
.biBitCount
= 24;
570 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
571 lpDIBh
->bmiHeader
.biClrUsed
= 0;
572 // These seem not really needed for our purpose here.
573 lpDIBh
->bmiHeader
.biClrImportant
= 0;
574 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
575 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
576 // memory for DIB data
577 unsigned char *lpBits
;
578 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
581 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
586 // create and set the device-dependent bitmap
587 HDC hdc
= ::GetDC(NULL
);
588 HDC memdc
= ::CreateCompatibleDC( hdc
);
590 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
591 ::SelectObject( memdc
, hbitmap
);
594 HPALETTE hOldPalette
= 0;
595 if (image
.GetPalette().Ok())
597 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
598 ::RealizePalette(memdc
);
600 #endif // wxUSE_PALETTE
602 // copy image data into DIB data and then into DDB (in a loop)
603 unsigned char *data
= image
.GetData();
606 unsigned char *ptdata
= data
;
607 unsigned char *ptbits
;
609 for( n
=0; n
<numDIB
; n
++ )
611 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
613 // redefine height and size of the (possibly) last smaller DIB
614 // memory is not reallocated
616 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
617 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
621 for( j
=0; j
<height
; j
++ )
623 for( i
=0; i
<width
; i
++ )
625 *(ptbits
++) = *(ptdata
+2);
626 *(ptbits
++) = *(ptdata
+1);
627 *(ptbits
++) = *(ptdata
);
630 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
632 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
633 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
635 // if numDIB = 1, lines below can also be used
636 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
637 // The above line is equivalent to the following two lines.
638 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
639 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
640 // or the following lines
641 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
642 // HDC memdc = ::CreateCompatibleDC( hdc );
643 // ::SelectObject( memdc, hbitmap);
644 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
645 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
646 // ::SelectObject( memdc, 0 );
647 // ::DeleteDC( memdc );
649 SetHBITMAP( (WXHBITMAP
) hbitmap
);
653 SelectPalette(memdc
, hOldPalette
, FALSE
);
654 #endif // wxUSE_PALETTE
656 // similarly, created an mono-bitmap for the possible mask
657 if( image
.HasMask() )
659 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
660 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
661 if( numDIB
== 1 ) height
= bmpHeight
;
662 else height
= sizeLimit
/bytePerLine
;
663 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
664 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
666 unsigned char r
= image
.GetMaskRed();
667 unsigned char g
= image
.GetMaskGreen();
668 unsigned char b
= image
.GetMaskBlue();
669 unsigned char zero
= 0, one
= 255;
671 for( n
=0; n
<numDIB
; n
++ )
673 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
675 // redefine height and size of the (possibly) last smaller DIB
676 // memory is not reallocated
678 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
679 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
682 for( int j
=0; j
<height
; j
++ )
684 for(i
=0; i
<width
; i
++ )
686 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
687 unsigned char cr
= (*(ptdata
++)) ;
688 unsigned char cg
= (*(ptdata
++)) ;
689 unsigned char cb
= (*(ptdata
++)) ;
691 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
704 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
706 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
707 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
710 // create a wxMask object
711 wxMask
*mask
= new wxMask();
712 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
714 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
715 /* The following can also be used but is slow to run
716 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
717 wxMask *mask = new wxMask( *this, colour );
721 ::SelectObject( memdc
, hbmpOld
);
724 // free allocated resources
726 ::ReleaseDC(NULL
, hdc
);
730 #if WXWIN_COMPATIBILITY_2
731 // check the wxBitmap object
732 GetBitmapData()->SetOk();
733 #endif // WXWIN_COMPATIBILITY_2
739 wxImage
wxBitmap::ConvertToImage() const
741 #ifdef __WXMICROWIN__
742 // Initial attempt at a simple-minded implementation.
743 // The bitmap will always be created at the screen depth,
744 // so the 'depth' argument is ignored.
745 // TODO: transparency (create a mask image)
749 wxFAIL_MSG( wxT("bitmap is invalid") );
755 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
757 // create an wxImage object
758 int width
= GetWidth();
759 int height
= GetHeight();
760 image
.Create( width
, height
);
761 unsigned char *data
= image
.GetData();
764 wxFAIL_MSG( wxT("could not allocate data for image") );
768 HDC hScreenDC
= ::GetDC(NULL
);
770 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
771 ::ReleaseDC(NULL
, hScreenDC
);
773 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
775 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
778 for (i
= 0; i
< GetWidth(); i
++)
780 for (j
= 0; j
< GetHeight(); j
++)
782 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
783 unsigned char red
= GetRValue(color
);
784 unsigned char green
= GetGValue(color
);
785 unsigned char blue
= GetBValue(color
);
787 image
.SetRGB(i
, j
, red
, green
, blue
);
791 ::SelectObject(hMemDC
, hOldBitmap
);
795 // Copy the palette from the source image
797 image
.SetPalette(* GetPalette());
798 #endif // wxUSE_PALETTE
802 #else // __MICROWIN__
806 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
808 // create an wxImage object
809 int width
= GetWidth();
810 int height
= GetHeight();
811 image
.Create( width
, height
);
812 unsigned char *data
= image
.GetData();
815 wxFAIL_MSG( wxT("could not allocate data for image") );
819 // calc the number of bytes per scanline and padding in the DIB
820 int bytePerLine
= width
*3;
821 int sizeDWORD
= sizeof( DWORD
);
822 int lineBoundary
= bytePerLine
% sizeDWORD
;
824 if( lineBoundary
> 0 )
826 padding
= sizeDWORD
- lineBoundary
;
827 bytePerLine
+= padding
;
830 // create a DIB header
831 int headersize
= sizeof(BITMAPINFOHEADER
);
832 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
835 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
839 // Fill in the DIB header
840 lpDIBh
->bmiHeader
.biSize
= headersize
;
841 lpDIBh
->bmiHeader
.biWidth
= width
;
842 lpDIBh
->bmiHeader
.biHeight
= -height
;
843 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
844 lpDIBh
->bmiHeader
.biPlanes
= 1;
845 lpDIBh
->bmiHeader
.biBitCount
= 24;
846 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
847 lpDIBh
->bmiHeader
.biClrUsed
= 0;
848 // These seem not really needed for our purpose here.
849 lpDIBh
->bmiHeader
.biClrImportant
= 0;
850 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
851 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
852 // memory for DIB data
853 unsigned char *lpBits
;
854 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
857 wxFAIL_MSG( wxT("could not allocate data for DIB") );
863 // copy data from the device-dependent bitmap to the DIB
864 HDC hdc
= ::GetDC(NULL
);
866 hbitmap
= (HBITMAP
) GetHBITMAP();
867 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
869 // copy DIB data into the wxImage object
871 unsigned char *ptdata
= data
;
872 unsigned char *ptbits
= lpBits
;
873 for( i
=0; i
<height
; i
++ )
875 for( j
=0; j
<width
; j
++ )
877 *(ptdata
++) = *(ptbits
+2);
878 *(ptdata
++) = *(ptbits
+1);
879 *(ptdata
++) = *(ptbits
);
885 // similarly, set data according to the possible mask bitmap
886 if( GetMask() && GetMask()->GetMaskBitmap() )
888 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
889 // memory DC created, color set, data copied, and memory DC deleted
890 HDC memdc
= ::CreateCompatibleDC( hdc
);
891 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
892 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
893 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
895 // background color set to RGB(16,16,16) in consistent with wxGTK
896 unsigned char r
=16, g
=16, b
=16;
899 for( i
=0; i
<height
; i
++ )
901 for( j
=0; j
<width
; j
++ )
915 image
.SetMaskColour( r
, g
, b
);
916 image
.SetMask( TRUE
);
920 image
.SetMask( FALSE
);
922 // free allocated resources
923 ::ReleaseDC(NULL
, hdc
);
931 #endif // wxUSE_IMAGE
933 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
937 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
941 m_refData
= new wxBitmapRefData
;
943 return handler
->LoadFile(this, filename
, type
, -1, -1);
949 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
951 *this = image
.ConvertToBitmap();
956 #endif // wxUSE_IMAGE
961 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
965 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
969 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
974 m_refData
= new wxBitmapRefData
;
976 return handler
->Create(this, data
, type
, width
, height
, depth
);
979 bool wxBitmap::SaveFile(const wxString
& filename
,
981 const wxPalette
*palette
)
983 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
987 return handler
->SaveFile(this, filename
, type
, palette
);
992 // FIXME what about palette? shouldn't we use it?
993 wxImage
image( *this );
996 return image
.SaveFile(filename
, type
);
999 #endif // wxUSE_IMAGE
1004 // ----------------------------------------------------------------------------
1005 // sub bitmap extraction
1006 // ----------------------------------------------------------------------------
1008 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1010 wxCHECK_MSG( Ok() &&
1011 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1012 (rect
.x
+rect
.width
<= GetWidth()) &&
1013 (rect
.y
+rect
.height
<= GetHeight()),
1014 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1016 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1017 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1019 #ifndef __WXMICROWIN__
1021 MemoryHDC dcSrc
, dcDst
;
1024 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1025 selectDst(dcDst
, GetHbitmapOf(ret
));
1027 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1028 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1030 wxLogLastError(_T("BitBlt"));
1034 // copy mask if there is one
1037 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1039 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1040 selectDst(dcDst
, hbmpMask
);
1042 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1043 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1045 wxLogLastError(_T("BitBlt"));
1048 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1051 #endif // !__WXMICROWIN__
1056 // ----------------------------------------------------------------------------
1057 // wxBitmap accessors
1058 // ----------------------------------------------------------------------------
1060 void wxBitmap::SetQuality(int q
)
1064 GetBitmapData()->m_quality
= q
;
1067 #if WXWIN_COMPATIBILITY_2
1068 void wxBitmap::SetOk(bool isOk
)
1072 GetBitmapData()->m_ok
= isOk
;
1074 #endif // WXWIN_COMPATIBILITY_2
1078 void wxBitmap::SetPalette(const wxPalette
& palette
)
1082 GetBitmapData()->m_bitmapPalette
= palette
;
1085 #endif // wxUSE_PALETTE
1087 void wxBitmap::SetMask(wxMask
*mask
)
1091 GetBitmapData()->m_bitmapMask
= mask
;
1094 // Creates a bitmap that matches the device context, from
1095 // an arbitray bitmap. At present, the original bitmap must have an
1096 // associated palette. TODO: use a default palette if no palette exists.
1097 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1098 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1100 #ifdef __WXMICROWIN__
1104 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1105 HPALETTE hPal
= (HPALETTE
) NULL
;
1107 void *lpBits
= (void*) NULL
;
1110 if( GetPalette() && GetPalette()->Ok() )
1112 tmpBitmap
.SetPalette(*GetPalette());
1113 memDC
.SelectObject(tmpBitmap
);
1114 memDC
.SetPalette(*GetPalette());
1115 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1119 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1121 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1122 tmpBitmap
.SetPalette( palette
);
1123 memDC
.SelectObject(tmpBitmap
);
1124 memDC
.SetPalette( palette
);
1126 #else // !wxUSE_PALETTE
1127 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1128 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1130 // set the height negative because in a DIB the order of the lines is
1132 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1134 return wxNullBitmap
;
1137 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1139 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1141 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1142 GetWidth(), GetHeight(),
1143 0, 0, 0, GetHeight(),
1144 lpBits
, lpDib
, DIB_RGB_COLORS
);
1154 // ----------------------------------------------------------------------------
1156 // ----------------------------------------------------------------------------
1163 // Construct a mask from a bitmap and a colour indicating
1164 // the transparent area
1165 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1168 Create(bitmap
, colour
);
1171 // Construct a mask from a bitmap and a palette index indicating
1172 // the transparent area
1173 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1176 Create(bitmap
, paletteIndex
);
1179 // Construct a mask from a mono bitmap (copies the bitmap).
1180 wxMask::wxMask(const wxBitmap
& bitmap
)
1189 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1192 // Create a mask from a mono bitmap (copies the bitmap).
1193 bool wxMask::Create(const wxBitmap
& bitmap
)
1195 #ifndef __WXMICROWIN__
1196 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1197 _T("can't create mask from invalid or not monochrome bitmap") );
1201 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1205 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1210 HDC srcDC
= CreateCompatibleDC(0);
1211 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1212 HDC destDC
= CreateCompatibleDC(0);
1213 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1214 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1215 SelectObject(srcDC
, 0);
1217 SelectObject(destDC
, 0);
1225 // Create a mask from a bitmap and a palette index indicating
1226 // the transparent area
1227 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1231 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1236 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1238 unsigned char red
, green
, blue
;
1239 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1241 wxColour
transparentColour(red
, green
, blue
);
1242 return Create(bitmap
, transparentColour
);
1245 #endif // wxUSE_PALETTE
1250 // Create a mask from a bitmap and a colour indicating
1251 // the transparent area
1252 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1254 #ifndef __WXMICROWIN__
1255 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1259 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1263 int width
= bitmap
.GetWidth(),
1264 height
= bitmap
.GetHeight();
1266 // scan the bitmap for the transparent colour and set the corresponding
1267 // pixels in the mask to BLACK and the rest to WHITE
1268 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1269 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1271 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1272 HDC destDC
= ::CreateCompatibleDC(NULL
);
1273 if ( !srcDC
|| !destDC
)
1275 wxLogLastError(wxT("CreateCompatibleDC"));
1280 // SelectObject() will fail
1281 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1282 _T("bitmap can't be selected in another DC") );
1284 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1287 wxLogLastError(wxT("SelectObject"));
1292 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1295 wxLogLastError(wxT("SelectObject"));
1302 // this will create a monochrome bitmap with 0 points for the pixels
1303 // which have the same value as the background colour and 1 for the
1305 ::SetBkColor(srcDC
, maskColour
);
1306 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1309 ::SelectObject(srcDC
, hbmpSrcOld
);
1311 ::SelectObject(destDC
, hbmpDstOld
);
1315 #else // __WXMICROWIN__
1317 #endif // __WXMICROWIN__/!__WXMICROWIN__
1320 // ----------------------------------------------------------------------------
1322 // ----------------------------------------------------------------------------
1324 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1327 int width
, int height
, int depth
)
1329 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1331 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1334 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1335 const wxString
& name
,
1337 int width
, int height
)
1339 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1341 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1344 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1345 const wxString
& name
,
1348 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1350 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1353 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1354 void *WXUNUSED(data
),
1355 long WXUNUSED(type
),
1356 int WXUNUSED(width
),
1357 int WXUNUSED(height
),
1358 int WXUNUSED(depth
))
1363 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1364 const wxString
& WXUNUSED(name
),
1365 long WXUNUSED(type
),
1366 int WXUNUSED(desiredWidth
),
1367 int WXUNUSED(desiredHeight
))
1372 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1373 const wxString
& WXUNUSED(name
),
1375 const wxPalette
*WXUNUSED(palette
))
1380 // ----------------------------------------------------------------------------
1382 // ----------------------------------------------------------------------------
1384 #ifndef __WXMICROWIN__
1385 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1386 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1388 unsigned long i
, headerSize
;
1389 LPBITMAPINFO lpDIBheader
= NULL
;
1390 LPPALETTEENTRY lpPe
= NULL
;
1393 // Allocate space for a DIB header
1394 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1395 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1396 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1398 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1400 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1402 // Fill in the static parts of the DIB header
1403 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1404 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1405 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1406 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1408 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1409 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1410 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1411 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1412 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1415 // Initialize the DIB palette
1416 for (i
= 0; i
< 256; i
++) {
1417 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1418 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1419 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1420 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1423 *lpDIBHeader
= lpDIBheader
;
1428 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1434 // ----------------------------------------------------------------------------
1435 // other helper functions
1436 // ----------------------------------------------------------------------------
1438 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1440 #ifndef __WXMICROWIN__
1441 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1443 // get width/height from the bitmap if not given
1447 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1452 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1453 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1454 if ( !hdcSrc
|| !hdcDst
)
1456 wxLogLastError(wxT("CreateCompatibleDC"));
1459 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1462 wxLogLastError(wxT("CreateBitmap"));
1465 ::SelectObject(hdcSrc
, hbmpMask
);
1466 ::SelectObject(hdcDst
, hbmpInvMask
);
1467 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1471 wxLogLastError(wxT("BitBlt"));