From 003e6c07cc25fc9a7c2ecaaec14a004935417610 Mon Sep 17 00:00:00 2001 From: Dimitri Schoolwerth Date: Wed, 19 May 2004 22:22:48 +0000 Subject: [PATCH] restored mask copying code when converting a wxBitmap to a wxImage (wxMSW only) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@27357 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/msw/bitmap.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 117 insertions(+), 5 deletions(-) diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 25fa4bc5c3..f0c834bb24 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -811,15 +811,127 @@ wxImage wxBitmap::ConvertToImage() const { wxDIB dib(*this); - wxImage image; - if ( dib.IsOk() ) + if ( !dib.IsOk() ) { - image = dib.ConvertToImage(); - if ( image.Ok() ) + return wxNullImage; + } + + wxImage image = dib.ConvertToImage(); + if ( !image.Ok() ) + { + return wxNullImage; + } + + // set mask + + // TODO: WinCE mask-copying support and use wxDIB +#ifndef __WXWINCE__ + + if( GetMask() && GetMask()->GetMaskBitmap() ) + { + static const int MASK_RED = 1; + static const int MASK_GREEN = 2; + static const int MASK_BLUE = 3; + static const int MASK_BLUE_REPLACEMENT = 2; + + HBITMAP hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); + int width = GetWidth(); + int height = GetHeight(); + + int bytesPerLine = width*3; + int sizeDWORD = sizeof( DWORD ); + int lineBoundary = bytesPerLine % sizeDWORD; + int padding = 0; + if( lineBoundary > 0 ) + { + padding = sizeDWORD - lineBoundary; + bytesPerLine += padding; + } + + // create a DIB header + int headersize = sizeof(BITMAPINFOHEADER); + BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize ); + if( !lpDIBh ) { - // TODO: set mask + wxFAIL_MSG( wxT("could not allocate data for DIB header") ); + //free( data ); + return wxNullImage; } + + // Fill in the DIB header + lpDIBh->bmiHeader.biSize = headersize; + lpDIBh->bmiHeader.biWidth = width; + lpDIBh->bmiHeader.biHeight = -height; + lpDIBh->bmiHeader.biSizeImage = bytesPerLine * height; + lpDIBh->bmiHeader.biPlanes = 1; + lpDIBh->bmiHeader.biBitCount = 24; + lpDIBh->bmiHeader.biCompression = BI_RGB; + lpDIBh->bmiHeader.biClrUsed = 0; + // These seem not really needed for our purpose here. + lpDIBh->bmiHeader.biClrImportant = 0; + lpDIBh->bmiHeader.biXPelsPerMeter = 0; + lpDIBh->bmiHeader.biYPelsPerMeter = 0; + + // memory DC created, color set, data copied, and memory DC deleted + + // memory for DIB data + unsigned char *lpBits + = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage ); + if( !lpBits ) + { + wxFAIL_MSG( wxT("could not allocate data for DIB") ); + free( lpDIBh ); + return wxNullImage; + } + + + HDC hdc = ::GetDC(NULL); + + HDC memdc = ::CreateCompatibleDC( hdc ); + ::SetTextColor( memdc, RGB( 0, 0, 0 ) ); + ::SetBkColor( memdc, RGB( 255, 255, 255 ) ); + ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS ); + ::DeleteDC( memdc ); + unsigned char *ptdata = image.GetData();//data; + unsigned char *ptbits = lpBits; + int i, j; + for( i=0; i