]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
46 #if !defined(__WXMICROWIN__)
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 // missing from mingw32 header
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
63 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 wxBitmapRefData::wxBitmapRefData()
78 m_selectedInto
= NULL
;
81 m_hBitmap
= (WXHBITMAP
) NULL
;
84 void wxBitmapRefData::Free()
86 wxASSERT_MSG( !m_selectedInto
,
87 wxT("deleting bitmap still selected into wxMemoryDC") );
91 // printf("About to delete bitmap %d\n", (int) (HBITMAP) m_hBitmap);
92 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
94 wxLogLastError(wxT("DeleteObject(hbitmap)"));
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // this function should be called from all wxBitmap ctors
107 void wxBitmap::Init()
109 // m_refData = NULL; done in the base class ctor
115 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
117 #ifndef __WXMICROWIN__
118 // it may be either HICON or HCURSOR
119 HICON hicon
= (HICON
)icon
.GetHandle();
122 if ( !::GetIconInfo(hicon
, &iconInfo
) )
124 wxLogLastError(wxT("GetIconInfo"));
129 wxBitmapRefData
*refData
= new wxBitmapRefData
;
132 int w
= icon
.GetWidth(),
133 h
= icon
.GetHeight();
135 refData
->m_width
= w
;
136 refData
->m_height
= h
;
137 refData
->m_depth
= wxDisplayDepth();
139 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
141 // the mask returned by GetIconInfo() is inversed compared to the usual
143 refData
->m_bitmapMask
= new wxMask((WXHBITMAP
)
144 wxInvertMask(iconInfo
.hbmMask
, w
, h
));
147 // delete the old one now as we don't need it any more
148 ::DeleteObject(iconInfo
.hbmMask
);
150 #if WXWIN_COMPATIBILITY_2
151 refData
->m_ok
= TRUE
;
152 #endif // WXWIN_COMPATIBILITY_2
162 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
170 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
174 return CopyFromIconOrCursor(cursor
);
178 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
185 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
186 // to create a bitmap from icon there - but using this way we won't have
189 int width
= icon
.GetWidth(),
190 height
= icon
.GetHeight();
192 // copy the icon to the bitmap
194 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
195 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
196 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
198 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
200 ::SelectObject(hdc
, hbmpOld
);
203 wxBitmapRefData
*refData
= new wxBitmapRefData
;
206 refData
->m_width
= width
;
207 refData
->m_height
= height
;
208 refData
->m_depth
= wxDisplayDepth();
210 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
212 #if WXWIN_COMPATIBILITY_2
213 refData
->m_ok
= TRUE
;
214 #endif // WXWIN_COMPATIBILITY_2
218 return CopyFromIconOrCursor(icon
);
219 #endif // Win16/Win32
222 wxBitmap::~wxBitmap()
226 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
230 #ifndef __WXMICROWIN__
231 wxBitmapRefData
*refData
= new wxBitmapRefData
;
234 refData
->m_width
= width
;
235 refData
->m_height
= height
;
236 refData
->m_depth
= depth
;
237 refData
->m_numColors
= 0;
238 refData
->m_selectedInto
= NULL
;
243 // we assume that it is in XBM format which is not quite the same as
244 // the format CreateBitmap() wants because the order of bytes in the
246 const size_t bytesPerLine
= (width
+ 7) / 8;
247 const size_t padding
= bytesPerLine
% 2;
248 const size_t len
= height
* ( padding
+ bytesPerLine
);
249 data
= (char *)malloc(len
);
250 const char *src
= bits
;
253 for ( int rows
= 0; rows
< height
; rows
++ )
255 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
257 unsigned char val
= *src
++;
258 unsigned char reversed
= 0;
260 for ( int bits
= 0; bits
< 8; bits
++)
263 reversed
|= (val
& 0x01);
275 // bits should already be in Windows standard format
276 data
= (char *)bits
; // const_cast is harmless
279 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
282 wxLogLastError(wxT("CreateBitmap"));
290 SetHBITMAP((WXHBITMAP
)hbmp
);
294 // Create from XPM data
295 bool wxBitmap::CreateFromXpm(const char **data
)
297 #if wxUSE_IMAGE && wxUSE_XPM
300 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
302 wxXPMDecoder decoder
;
303 wxImage img
= decoder
.ReadData(data
);
304 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
306 *this = wxBitmap(img
);
313 wxBitmap::wxBitmap(int w
, int h
, int d
)
317 (void)Create(w
, h
, d
);
320 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
324 (void)Create(data
, type
, width
, height
, depth
);
327 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
331 LoadFile(filename
, (int)type
);
334 bool wxBitmap::Create(int w
, int h
, int d
)
338 m_refData
= new wxBitmapRefData
;
340 GetBitmapData()->m_width
= w
;
341 GetBitmapData()->m_height
= h
;
342 GetBitmapData()->m_depth
= d
;
345 #ifndef __WXMICROWIN__
348 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
351 wxLogLastError(wxT("CreateBitmap"));
358 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
361 wxLogLastError(wxT("CreateCompatibleBitmap"));
364 GetBitmapData()->m_depth
= wxDisplayDepth();
367 SetHBITMAP((WXHBITMAP
)hbmp
);
369 #if WXWIN_COMPATIBILITY_2
370 GetBitmapData()->m_ok
= hbmp
!= 0;
371 #endif // WXWIN_COMPATIBILITY_2
375 // ----------------------------------------------------------------------------
376 // wxImage to/from conversions
377 // ----------------------------------------------------------------------------
381 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
383 #ifdef __WXMICROWIN__
384 m_refData
= new wxBitmapRefData();
386 // Initial attempt at a simple-minded implementation.
387 // The bitmap will always be created at the screen depth,
388 // so the 'depth' argument is ignored.
389 // TODO: transparency (create a mask image)
391 HDC hScreenDC
= ::GetDC(NULL
);
392 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
394 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
395 // printf("Created bitmap %d\n", (int) hBitmap);
398 ::ReleaseDC(NULL
, hScreenDC
);
401 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
402 ::ReleaseDC(NULL
, hScreenDC
);
404 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
407 for (i
= 0; i
< image
.GetWidth(); i
++)
409 for (j
= 0; j
< image
.GetHeight(); j
++)
411 unsigned char red
= image
.GetRed(i
, j
);
412 unsigned char green
= image
.GetGreen(i
, j
);
413 unsigned char blue
= image
.GetBlue(i
, j
);
415 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
419 ::SelectObject(hMemDC
, hOldBitmap
);
422 SetWidth(image
.GetWidth());
423 SetHeight(image
.GetHeight());
424 SetDepth(screenDepth
);
425 SetHBITMAP( (WXHBITMAP
) hBitmap
);
428 // Copy the palette from the source image
429 SetPalette(image
.GetPalette());
430 #endif // wxUSE_PALETTE
432 #if WXWIN_COMPATIBILITY_2
433 // check the wxBitmap object
434 GetBitmapData()->SetOk();
435 #endif // WXWIN_COMPATIBILITY_2
440 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
442 m_refData
= new wxBitmapRefData();
444 // sizeLimit is the MS upper limit for the DIB size
446 int sizeLimit
= 1024*768*3;
448 int sizeLimit
= 0x7fff ;
451 // width and height of the device-dependent bitmap
452 int width
= image
.GetWidth();
453 int bmpHeight
= image
.GetHeight();
455 // calc the number of bytes per scanline and padding
456 int bytePerLine
= width
*3;
457 int sizeDWORD
= sizeof( DWORD
);
458 int lineBoundary
= bytePerLine
% sizeDWORD
;
460 if( lineBoundary
> 0 )
462 padding
= sizeDWORD
- lineBoundary
;
463 bytePerLine
+= padding
;
465 // calc the number of DIBs and heights of DIBs
468 int height
= sizeLimit
/bytePerLine
;
469 if( height
>= bmpHeight
)
473 numDIB
= bmpHeight
/ height
;
474 hRemain
= bmpHeight
% height
;
475 if( hRemain
>0 ) numDIB
++;
478 // set bitmap parameters
479 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
481 SetHeight( bmpHeight
);
482 if (depth
== -1) depth
= wxDisplayDepth();
486 // Copy the palette from the source image
487 SetPalette(image
.GetPalette());
488 #endif // wxUSE_PALETTE
490 // create a DIB header
491 int headersize
= sizeof(BITMAPINFOHEADER
);
492 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
493 wxCHECK_MSG( lpDIBh
, FALSE
, wxT("could not allocate memory for DIB header") );
494 // Fill in the DIB header
495 lpDIBh
->bmiHeader
.biSize
= headersize
;
496 lpDIBh
->bmiHeader
.biWidth
= (DWORD
)width
;
497 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
498 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
499 // the general formula for biSizeImage:
500 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
501 lpDIBh
->bmiHeader
.biPlanes
= 1;
502 lpDIBh
->bmiHeader
.biBitCount
= 24;
503 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
504 lpDIBh
->bmiHeader
.biClrUsed
= 0;
505 // These seem not really needed for our purpose here.
506 lpDIBh
->bmiHeader
.biClrImportant
= 0;
507 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
508 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
509 // memory for DIB data
510 unsigned char *lpBits
;
511 lpBits
= (unsigned char *)malloc( lpDIBh
->bmiHeader
.biSizeImage
);
514 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
519 // create and set the device-dependent bitmap
520 HDC hdc
= ::GetDC(NULL
);
521 HDC memdc
= ::CreateCompatibleDC( hdc
);
523 hbitmap
= ::CreateCompatibleBitmap( hdc
, width
, bmpHeight
);
524 ::SelectObject( memdc
, hbitmap
);
527 HPALETTE hOldPalette
= 0;
528 if (image
.GetPalette().Ok())
530 hOldPalette
= ::SelectPalette(memdc
, (HPALETTE
) image
.GetPalette().GetHPALETTE(), FALSE
);
531 ::RealizePalette(memdc
);
533 #endif // wxUSE_PALETTE
535 // copy image data into DIB data and then into DDB (in a loop)
536 unsigned char *data
= image
.GetData();
539 unsigned char *ptdata
= data
;
540 unsigned char *ptbits
;
542 for( n
=0; n
<numDIB
; n
++ )
544 if( numDIB
> 1 && n
== numDIB
-1 && hRemain
> 0 )
546 // redefine height and size of the (possibly) last smaller DIB
547 // memory is not reallocated
549 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
550 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
554 for( j
=0; j
<height
; j
++ )
556 for( i
=0; i
<width
; i
++ )
558 *(ptbits
++) = *(ptdata
+2);
559 *(ptbits
++) = *(ptdata
+1);
560 *(ptbits
++) = *(ptdata
);
563 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = 0;
565 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
566 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
568 // if numDIB = 1, lines below can also be used
569 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
570 // The above line is equivalent to the following two lines.
571 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
572 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
573 // or the following lines
574 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
575 // HDC memdc = ::CreateCompatibleDC( hdc );
576 // ::SelectObject( memdc, hbitmap);
577 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
578 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
579 // ::SelectObject( memdc, 0 );
580 // ::DeleteDC( memdc );
582 SetHBITMAP( (WXHBITMAP
) hbitmap
);
586 SelectPalette(memdc
, hOldPalette
, FALSE
);
587 #endif // wxUSE_PALETTE
589 // similarly, created an mono-bitmap for the possible mask
590 if( image
.HasMask() )
592 hbitmap
= ::CreateBitmap( (WORD
)width
, (WORD
)bmpHeight
, 1, 1, NULL
);
593 HGDIOBJ hbmpOld
= ::SelectObject( memdc
, hbitmap
);
594 if( numDIB
== 1 ) height
= bmpHeight
;
595 else height
= sizeLimit
/bytePerLine
;
596 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
597 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
599 unsigned char r
= image
.GetMaskRed();
600 unsigned char g
= image
.GetMaskGreen();
601 unsigned char b
= image
.GetMaskBlue();
602 unsigned char zero
= 0, one
= 255;
604 for( n
=0; n
<numDIB
; n
++ )
606 if( numDIB
> 1 && n
== numDIB
- 1 && hRemain
> 0 )
608 // redefine height and size of the (possibly) last smaller DIB
609 // memory is not reallocated
611 lpDIBh
->bmiHeader
.biHeight
= (DWORD
)(-height
);
612 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
*height
;
615 for( int j
=0; j
<height
; j
++ )
617 for(i
=0; i
<width
; i
++ )
619 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
620 unsigned char cr
= (*(ptdata
++)) ;
621 unsigned char cg
= (*(ptdata
++)) ;
622 unsigned char cb
= (*(ptdata
++)) ;
624 if( ( cr
!=r
) || (cg
!=g
) || (cb
!=b
) )
637 for( i
=0; i
< padding
; i
++ ) *(ptbits
++) = zero
;
639 ::StretchDIBits( memdc
, 0, origin
, width
, height
,\
640 0, 0, width
, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
, SRCCOPY
);
643 // create a wxMask object
644 wxMask
*mask
= new wxMask();
645 mask
->SetMaskBitmap( (WXHBITMAP
) hbitmap
);
647 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
648 /* The following can also be used but is slow to run
649 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
650 wxMask *mask = new wxMask( *this, colour );
654 ::SelectObject( memdc
, hbmpOld
);
657 // free allocated resources
659 ::ReleaseDC(NULL
, hdc
);
663 #if WXWIN_COMPATIBILITY_2
664 // check the wxBitmap object
665 GetBitmapData()->SetOk();
666 #endif // WXWIN_COMPATIBILITY_2
672 wxImage
wxBitmap::ConvertToImage() const
674 #ifdef __WXMICROWIN__
675 // Initial attempt at a simple-minded implementation.
676 // The bitmap will always be created at the screen depth,
677 // so the 'depth' argument is ignored.
678 // TODO: transparency (create a mask image)
682 wxFAIL_MSG( wxT("bitmap is invalid") );
688 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
690 // create an wxImage object
691 int width
= GetWidth();
692 int height
= GetHeight();
693 image
.Create( width
, height
);
694 unsigned char *data
= image
.GetData();
697 wxFAIL_MSG( wxT("could not allocate data for image") );
701 HDC hScreenDC
= ::GetDC(NULL
);
703 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
704 ::ReleaseDC(NULL
, hScreenDC
);
706 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
708 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
711 for (i
= 0; i
< GetWidth(); i
++)
713 for (j
= 0; j
< GetHeight(); j
++)
715 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
716 unsigned char red
= GetRValue(color
);
717 unsigned char green
= GetGValue(color
);
718 unsigned char blue
= GetBValue(color
);
720 image
.SetRGB(i
, j
, red
, green
, blue
);
724 ::SelectObject(hMemDC
, hOldBitmap
);
728 // Copy the palette from the source image
730 image
.SetPalette(* GetPalette());
731 #endif // wxUSE_PALETTE
735 #else // __MICROWIN__
739 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
741 // create an wxImage object
742 int width
= GetWidth();
743 int height
= GetHeight();
744 image
.Create( width
, height
);
745 unsigned char *data
= image
.GetData();
748 wxFAIL_MSG( wxT("could not allocate data for image") );
752 // calc the number of bytes per scanline and padding in the DIB
753 int bytePerLine
= width
*3;
754 int sizeDWORD
= sizeof( DWORD
);
755 int lineBoundary
= bytePerLine
% sizeDWORD
;
757 if( lineBoundary
> 0 )
759 padding
= sizeDWORD
- lineBoundary
;
760 bytePerLine
+= padding
;
763 // create a DIB header
764 int headersize
= sizeof(BITMAPINFOHEADER
);
765 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
768 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
772 // Fill in the DIB header
773 lpDIBh
->bmiHeader
.biSize
= headersize
;
774 lpDIBh
->bmiHeader
.biWidth
= width
;
775 lpDIBh
->bmiHeader
.biHeight
= -height
;
776 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
777 lpDIBh
->bmiHeader
.biPlanes
= 1;
778 lpDIBh
->bmiHeader
.biBitCount
= 24;
779 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
780 lpDIBh
->bmiHeader
.biClrUsed
= 0;
781 // These seem not really needed for our purpose here.
782 lpDIBh
->bmiHeader
.biClrImportant
= 0;
783 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
784 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
785 // memory for DIB data
786 unsigned char *lpBits
;
787 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
790 wxFAIL_MSG( wxT("could not allocate data for DIB") );
796 // copy data from the device-dependent bitmap to the DIB
797 HDC hdc
= ::GetDC(NULL
);
799 hbitmap
= (HBITMAP
) GetHBITMAP();
800 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
802 // copy DIB data into the wxImage object
804 unsigned char *ptdata
= data
;
805 unsigned char *ptbits
= lpBits
;
806 for( i
=0; i
<height
; i
++ )
808 for( j
=0; j
<width
; j
++ )
810 *(ptdata
++) = *(ptbits
+2);
811 *(ptdata
++) = *(ptbits
+1);
812 *(ptdata
++) = *(ptbits
);
818 // similarly, set data according to the possible mask bitmap
819 if( GetMask() && GetMask()->GetMaskBitmap() )
821 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
822 // memory DC created, color set, data copied, and memory DC deleted
823 HDC memdc
= ::CreateCompatibleDC( hdc
);
824 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
825 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
826 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
828 // background color set to RGB(16,16,16) in consistent with wxGTK
829 unsigned char r
=16, g
=16, b
=16;
832 for( i
=0; i
<height
; i
++ )
834 for( j
=0; j
<width
; j
++ )
848 image
.SetMaskColour( r
, g
, b
);
849 image
.SetMask( TRUE
);
853 image
.SetMask( FALSE
);
855 // free allocated resources
856 ::ReleaseDC(NULL
, hdc
);
864 #endif // wxUSE_IMAGE
866 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
870 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
874 m_refData
= new wxBitmapRefData
;
876 return handler
->LoadFile(this, filename
, type
, -1, -1);
882 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
884 *this = image
.ConvertToBitmap();
889 #endif // wxUSE_IMAGE
894 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
898 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
902 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type
);
907 m_refData
= new wxBitmapRefData
;
909 return handler
->Create(this, data
, type
, width
, height
, depth
);
912 bool wxBitmap::SaveFile(const wxString
& filename
,
914 const wxPalette
*palette
)
916 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
920 return handler
->SaveFile(this, filename
, type
, palette
);
925 // FIXME what about palette? shouldn't we use it?
926 wxImage
image( *this );
929 return image
.SaveFile(filename
, type
);
932 #endif // wxUSE_IMAGE
937 // ----------------------------------------------------------------------------
938 // sub bitmap extraction
939 // ----------------------------------------------------------------------------
941 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
943 #ifndef __WXMICROWIN__
945 (rect
.x
>= 0) && (rect
.y
>= 0) &&
946 (rect
.x
+rect
.width
<= GetWidth()) &&
947 (rect
.y
+rect
.height
<= GetHeight()),
948 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
950 wxBitmap
ret( rect
.width
, rect
.height
, GetDepth() );
951 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
954 HDC dcSrc
= ::CreateCompatibleDC(NULL
);
955 HDC dcDst
= ::CreateCompatibleDC(NULL
);
956 SelectObject(dcSrc
, (HBITMAP
) GetHBITMAP());
957 SelectObject(dcDst
, (HBITMAP
) ret
.GetHBITMAP());
958 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
960 // copy mask if there is one
963 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
965 SelectObject(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap());
966 SelectObject(dcDst
, (HBITMAP
) hbmpMask
);
967 BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
, dcSrc
, rect
.x
, rect
.y
, SRCCOPY
);
969 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
973 SelectObject(dcDst
, NULL
);
974 SelectObject(dcSrc
, NULL
);
984 // ----------------------------------------------------------------------------
985 // wxBitmap accessors
986 // ----------------------------------------------------------------------------
988 void wxBitmap::SetQuality(int q
)
992 GetBitmapData()->m_quality
= q
;
995 #if WXWIN_COMPATIBILITY_2
996 void wxBitmap::SetOk(bool isOk
)
1000 GetBitmapData()->m_ok
= isOk
;
1002 #endif // WXWIN_COMPATIBILITY_2
1006 void wxBitmap::SetPalette(const wxPalette
& palette
)
1010 GetBitmapData()->m_bitmapPalette
= palette
;
1013 #endif // wxUSE_PALETTE
1015 void wxBitmap::SetMask(wxMask
*mask
)
1019 GetBitmapData()->m_bitmapMask
= mask
;
1022 // Creates a bitmap that matches the device context, from
1023 // an arbitray bitmap. At present, the original bitmap must have an
1024 // associated palette. TODO: use a default palette if no palette exists.
1025 // Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
1026 wxBitmap
wxBitmap::GetBitmapForDC(wxDC
& dc
) const
1028 #ifdef __WXMICROWIN__
1032 wxBitmap
tmpBitmap(GetWidth(), GetHeight(), dc
.GetDepth());
1033 HPALETTE hPal
= (HPALETTE
) NULL
;
1035 void *lpBits
= (void*) NULL
;
1038 if( GetPalette() && GetPalette()->Ok() )
1040 tmpBitmap
.SetPalette(*GetPalette());
1041 memDC
.SelectObject(tmpBitmap
);
1042 memDC
.SetPalette(*GetPalette());
1043 hPal
= (HPALETTE
)GetPalette()->GetHPALETTE();
1047 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1049 palette
.SetHPALETTE( (WXHPALETTE
)hPal
);
1050 tmpBitmap
.SetPalette( palette
);
1051 memDC
.SelectObject(tmpBitmap
);
1052 memDC
.SetPalette( palette
);
1054 #else // !wxUSE_PALETTE
1055 hPal
= (HPALETTE
) ::GetStockObject(DEFAULT_PALETTE
);
1056 #endif // wxUSE_PALETTE/!wxUSE_PALETTE
1058 // set the height negative because in a DIB the order of the lines is
1060 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal
, &lpDib
) )
1062 return wxNullBitmap
;
1065 lpBits
= malloc(lpDib
->bmiHeader
.biSizeImage
);
1067 ::GetBitmapBits(GetHbitmap(), lpDib
->bmiHeader
.biSizeImage
, lpBits
);
1069 ::SetDIBitsToDevice(GetHdcOf(memDC
), 0, 0,
1070 GetWidth(), GetHeight(),
1071 0, 0, 0, GetHeight(),
1072 lpBits
, lpDib
, DIB_RGB_COLORS
);
1082 // ----------------------------------------------------------------------------
1084 // ----------------------------------------------------------------------------
1091 // Construct a mask from a bitmap and a colour indicating
1092 // the transparent area
1093 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1096 Create(bitmap
, colour
);
1099 // Construct a mask from a bitmap and a palette index indicating
1100 // the transparent area
1101 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1104 Create(bitmap
, paletteIndex
);
1107 // Construct a mask from a mono bitmap (copies the bitmap).
1108 wxMask::wxMask(const wxBitmap
& bitmap
)
1117 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1120 // Create a mask from a mono bitmap (copies the bitmap).
1121 bool wxMask::Create(const wxBitmap
& bitmap
)
1123 #ifndef __WXMICROWIN__
1124 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1125 _T("can't create mask from invalid or not monochrome bitmap") );
1129 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1133 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1138 HDC srcDC
= CreateCompatibleDC(0);
1139 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1140 HDC destDC
= CreateCompatibleDC(0);
1141 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1142 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1143 SelectObject(srcDC
, 0);
1145 SelectObject(destDC
, 0);
1153 // Create a mask from a bitmap and a palette index indicating
1154 // the transparent area
1155 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1159 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1164 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1166 unsigned char red
, green
, blue
;
1167 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1169 wxColour
transparentColour(red
, green
, blue
);
1170 return Create(bitmap
, transparentColour
);
1173 #endif // wxUSE_PALETTE
1178 // Create a mask from a bitmap and a colour indicating
1179 // the transparent area
1180 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1182 #ifndef __WXMICROWIN__
1183 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1187 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1191 int width
= bitmap
.GetWidth(),
1192 height
= bitmap
.GetHeight();
1194 // scan the bitmap for the transparent colour and set the corresponding
1195 // pixels in the mask to BLACK and the rest to WHITE
1196 COLORREF maskColour
= RGB(colour
.Red(), colour
.Green(), colour
.Blue());
1197 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1199 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1200 HDC destDC
= ::CreateCompatibleDC(NULL
);
1201 if ( !srcDC
|| !destDC
)
1203 wxLogLastError(wxT("CreateCompatibleDC"));
1208 // SelectObject() will fail
1209 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1210 _T("bitmap can't be selected in another DC") );
1212 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1215 wxLogLastError(wxT("SelectObject"));
1220 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1223 wxLogLastError(wxT("SelectObject"));
1228 // this is not very efficient, but I can't think of a better way of doing
1230 for ( int w
= 0; ok
&& (w
< width
); w
++ )
1232 for ( int h
= 0; ok
&& (h
< height
); h
++ )
1234 COLORREF col
= GetPixel(srcDC
, w
, h
);
1235 if ( col
== CLR_INVALID
)
1237 wxLogLastError(wxT("GetPixel"));
1239 // doesn't make sense to continue
1245 if ( col
== maskColour
)
1247 ::SetPixel(destDC
, w
, h
, RGB(0, 0, 0));
1251 ::SetPixel(destDC
, w
, h
, RGB(255, 255, 255));
1256 ::SelectObject(srcDC
, hbmpSrcOld
);
1258 ::SelectObject(destDC
, hbmpDstOld
);
1267 // ----------------------------------------------------------------------------
1269 // ----------------------------------------------------------------------------
1271 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1274 int width
, int height
, int depth
)
1276 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1278 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1281 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1282 const wxString
& name
,
1284 int width
, int height
)
1286 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1288 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1291 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1292 const wxString
& name
,
1295 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1297 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1300 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1301 void *WXUNUSED(data
),
1302 long WXUNUSED(type
),
1303 int WXUNUSED(width
),
1304 int WXUNUSED(height
),
1305 int WXUNUSED(depth
))
1310 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1311 const wxString
& WXUNUSED(name
),
1312 long WXUNUSED(type
),
1313 int WXUNUSED(desiredWidth
),
1314 int WXUNUSED(desiredHeight
))
1319 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1320 const wxString
& WXUNUSED(name
),
1322 const wxPalette
*WXUNUSED(palette
))
1327 // ----------------------------------------------------------------------------
1329 // ----------------------------------------------------------------------------
1331 #ifndef __WXMICROWIN__
1332 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1333 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1335 unsigned long i
, headerSize
;
1336 LPBITMAPINFO lpDIBheader
= NULL
;
1337 LPPALETTEENTRY lpPe
= NULL
;
1340 // Allocate space for a DIB header
1341 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1342 lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1343 lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1345 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1347 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1349 // Fill in the static parts of the DIB header
1350 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1351 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1352 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1353 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1355 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1356 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1357 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1358 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1359 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1362 // Initialize the DIB palette
1363 for (i
= 0; i
< 256; i
++) {
1364 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1365 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1366 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1367 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1370 *lpDIBHeader
= lpDIBheader
;
1375 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1381 // ----------------------------------------------------------------------------
1382 // other helper functions
1383 // ----------------------------------------------------------------------------
1385 extern HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1387 #ifndef __WXMICROWIN__
1388 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1390 // get width/height from the bitmap if not given
1394 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1399 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1400 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1401 if ( !hdcSrc
|| !hdcDst
)
1403 wxLogLastError(wxT("CreateCompatibleDC"));
1406 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1409 wxLogLastError(wxT("CreateBitmap"));
1412 ::SelectObject(hdcSrc
, hbmpMask
);
1413 ::SelectObject(hdcDst
, hbmpInvMask
);
1414 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1418 wxLogLastError(wxT("BitBlt"));