]>
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 #ifndef __WXMICROWIN__
1011 wxCHECK_MSG( Ok() &&
1012 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1013 (rect
.x
+rect
.width
<= GetWidth()) &&
1014 (rect
.y
+rect
.height
<= GetHeight()),
1015 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1017 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
1018 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1021 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
1022 HDC dcDst
= ::CreateCompatibleDC(NULL
);
1023 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
1024 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
1025 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
1027 // copy mask if there is one
1030 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1032 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
1033 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
1034 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
1036 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1040 SelectObject(dcDst
, NULL
);
1041 SelectObject(dcSrc
, NULL
);
1051 // ----------------------------------------------------------------------------
1052 // wxBitmap accessors
1053 // ----------------------------------------------------------------------------
1055 void wxBitmap::SetQuality(int q
)
1059 GetBitmapData()->m_quality
= q
;
1062 #if WXWIN_COMPATIBILITY_2
1063 void wxBitmap::SetOk(bool isOk
)
1067 GetBitmapData()->m_ok
= isOk
;
1069 #endif // WXWIN_COMPATIBILITY_2
1073 void wxBitmap::SetPalette(const wxPalette
& palette
)
1077 GetBitmapData()->m_bitmapPalette
= palette
;
1080 #endif // wxUSE_PALETTE
1082 void wxBitmap::SetMask(wxMask
*mask
)
1086 GetBitmapData()->m_bitmapMask
= mask
;
1089 // Creates a bitmap that matches the device context, from
1090 // an arbitray bitmap. At present, the original bitmap must have an
1091 // associated palette. TODO: use a default palette if no palette exists.
1092 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1093 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1095 #ifdef __WXMICROWIN__
1099 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1100 HPALETTE hPal
= (HPALETTE
) NULL
;
1102 void *lpBits
= (void*) NULL
;
1105 if( GetPalette() && GetPalette()->Ok() )
1107 tmpBitmap
.SetPalette(*GetPalette());
1108 memDC
.SelectObject(tmpBitmap
);
1109 memDC
.SetPalette(*GetPalette());
1110 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1114 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1116 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1117 tmpBitmap
.SetPalette( palette
);
1118 memDC
.SelectObject(tmpBitmap
);
1119 memDC
.SetPalette( palette
);
1121 #else // !wxUSE_PALETTE
1122 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1123 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1125 // set the height negative because in a DIB the order of the lines is
1127 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1129 return wxNullBitmap
;
1132 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1134 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1136 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1137 GetWidth(), GetHeight(),
1138 0, 0, 0, GetHeight(),
1139 lpBits
, lpDib
, DIB_RGB_COLORS
);
1149 // ----------------------------------------------------------------------------
1151 // ----------------------------------------------------------------------------
1158 // Construct a mask from a bitmap and a colour indicating
1159 // the transparent area
1160 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1163 Create(bitmap
, colour
);
1166 // Construct a mask from a bitmap and a palette index indicating
1167 // the transparent area
1168 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1171 Create(bitmap
, paletteIndex
);
1174 // Construct a mask from a mono bitmap (copies the bitmap).
1175 wxMask::wxMask(const wxBitmap
& bitmap
)
1184 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1187 // Create a mask from a mono bitmap (copies the bitmap).
1188 bool wxMask::Create(const wxBitmap
& bitmap
)
1190 #ifndef __WXMICROWIN__
1191 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1192 _T("can't create mask from invalid or not monochrome bitmap") );
1196 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1200 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1205 HDC srcDC
= CreateCompatibleDC(0);
1206 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1207 HDC destDC
= CreateCompatibleDC(0);
1208 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1209 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1210 SelectObject(srcDC
, 0);
1212 SelectObject(destDC
, 0);
1220 // Create a mask from a bitmap and a palette index indicating
1221 // the transparent area
1222 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1226 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1231 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1233 unsigned char red
, green
, blue
;
1234 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1236 wxColour
transparentColour(red
, green
, blue
);
1237 return Create(bitmap
, transparentColour
);
1240 #endif // wxUSE_PALETTE
1245 // Create a mask from a bitmap and a colour indicating
1246 // the transparent area
1247 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1249 #ifndef __WXMICROWIN__
1250 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1254 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1258 int width
= bitmap
.GetWidth(),
1259 height
= bitmap
.GetHeight();
1261 // scan the bitmap for the transparent colour and set the corresponding
1262 // pixels in the mask to BLACK and the rest to WHITE
1263 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1264 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1266 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1267 HDC destDC
= ::CreateCompatibleDC(NULL
);
1268 if ( !srcDC
|| !destDC
)
1270 wxLogLastError(wxT("CreateCompatibleDC"));
1275 // SelectObject() will fail
1276 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1277 _T("bitmap can't be selected in another DC") );
1279 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1282 wxLogLastError(wxT("SelectObject"));
1287 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1290 wxLogLastError(wxT("SelectObject"));
1297 // this will create a monochrome bitmap with 0 points for the pixels
1298 // which have the same value as the background colour and 1 for the
1300 ::SetBkColor(srcDC
, maskColour
);
1301 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1304 ::SelectObject(srcDC
, hbmpSrcOld
);
1306 ::SelectObject(destDC
, hbmpDstOld
);
1310 #else // __WXMICROWIN__
1312 #endif // __WXMICROWIN__/!__WXMICROWIN__
1315 // ----------------------------------------------------------------------------
1317 // ----------------------------------------------------------------------------
1319 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1322 int width
, int height
, int depth
)
1324 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1326 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1329 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1330 const wxString
& name
,
1332 int width
, int height
)
1334 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1336 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1339 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1340 const wxString
& name
,
1343 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1345 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1348 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1349 void *WXUNUSED(data
),
1350 long WXUNUSED(type
),
1351 int WXUNUSED(width
),
1352 int WXUNUSED(height
),
1353 int WXUNUSED(depth
))
1358 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1359 const wxString
& WXUNUSED(name
),
1360 long WXUNUSED(type
),
1361 int WXUNUSED(desiredWidth
),
1362 int WXUNUSED(desiredHeight
))
1367 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1368 const wxString
& WXUNUSED(name
),
1370 const wxPalette
*WXUNUSED(palette
))
1375 // ----------------------------------------------------------------------------
1377 // ----------------------------------------------------------------------------
1379 #ifndef __WXMICROWIN__
1380 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1381 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1383 unsigned long i
, headerSize
;
1384 LPBITMAPINFO lpDIBheader
= NULL
;
1385 LPPALETTEENTRY lpPe
= NULL
;
1388 // Allocate space for a DIB header
1389 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1390 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1391 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1393 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1395 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1397 // Fill in the static parts of the DIB header
1398 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1399 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1400 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1401 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1403 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1404 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1405 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1406 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1407 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1410 // Initialize the DIB palette
1411 for (i
= 0; i
< 256; i
++) {
1412 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1413 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1414 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1415 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1418 *lpDIBHeader
= lpDIBheader
;
1423 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1429 // ----------------------------------------------------------------------------
1430 // other helper functions
1431 // ----------------------------------------------------------------------------
1433 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1435 #ifndef __WXMICROWIN__
1436 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1438 // get width/height from the bitmap if not given
1442 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1447 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1448 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1449 if ( !hdcSrc
|| !hdcDst
)
1451 wxLogLastError(wxT("CreateCompatibleDC"));
1454 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1457 wxLogLastError(wxT("CreateBitmap"));
1460 ::SelectObject(hdcSrc
, hbmpMask
);
1461 ::SelectObject(hdcDst
, hbmpInvMask
);
1462 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1466 wxLogLastError(wxT("BitBlt"));