reimplemented wxBitmap::ConvertToImage() using wxDIB

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28393 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-07-22 11:49:45 +00:00
parent e11d2abe46
commit 6faac21b0b

View File

@@ -810,6 +810,7 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
wxImage wxBitmap::ConvertToImage() const wxImage wxBitmap::ConvertToImage() const
{ {
// convert DDB to DIB
wxDIB dib(*this); wxDIB dib(*this);
if ( !dib.IsOk() ) if ( !dib.IsOk() )
@@ -817,122 +818,75 @@ wxImage wxBitmap::ConvertToImage() const
return wxNullImage; return wxNullImage;
} }
// and then DIB to our wxImage
wxImage image = dib.ConvertToImage(); wxImage image = dib.ConvertToImage();
if ( !image.Ok() ) if ( !image.Ok() )
{ {
return wxNullImage; return wxNullImage;
} }
// set mask // now do the same for the mask, if we have any
HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
// TODO: WinCE mask-copying support and use wxDIB if ( hbmpMask )
#ifndef __WXWINCE__
if( GetMask() && GetMask()->GetMaskBitmap() )
{ {
wxDIB dibMask(hbmpMask);
if ( dibMask.IsOk() )
{
// TODO: use wxRawBitmap to iterate over DIB
// we hard code the mask colour for now but we could also make an
// effort (and waste time) to choose a colour not present in the
// image already to avoid having to fudge the pixels below --
// whether it's worth to do it is unclear however
static const int MASK_RED = 1; static const int MASK_RED = 1;
static const int MASK_GREEN = 2; static const int MASK_GREEN = 2;
static const int MASK_BLUE = 3; static const int MASK_BLUE = 3;
static const int MASK_BLUE_REPLACEMENT = 2; static const int MASK_BLUE_REPLACEMENT = 2;
HBITMAP hbitmap = (HBITMAP) GetMask()->GetMaskBitmap(); const int h = dibMask.GetHeight();
int width = GetWidth(); const int w = dibMask.GetWidth();
int height = GetHeight(); const int bpp = dibMask.GetDepth();
const int maskBytesPerPixel = bpp >> 3;
const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
unsigned char *data = image.GetData();
int bytesPerLine = width*3; // remember that DIBs are stored in bottom to top order
int sizeDWORD = sizeof( DWORD ); unsigned char *
int lineBoundary = bytesPerLine % sizeDWORD; maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
int padding = 0;
if( lineBoundary > 0 ) for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
{ {
padding = sizeDWORD - lineBoundary; // traverse one mask DIB line
bytesPerLine += padding; unsigned char *mask = maskLineStart;
for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
{
// should this pixel be transparent?
if ( *mask )
{
// no, check that it isn't transparent by accident
if ( (data[0] == MASK_RED) &&
(data[1] == MASK_GREEN) &&
(data[2] == MASK_BLUE) )
{
// we have to fudge the colour a bit to prevent
// this pixel from appearing transparent
data[2] = MASK_BLUE_REPLACEMENT;
} }
// create a DIB header data += 3;
int headersize = sizeof(BITMAPINFOHEADER);
BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
if( !lpDIBh )
{
wxFAIL_MSG( wxT("could not allocate data for DIB header") );
//free( data );
return wxNullImage;
} }
else // yes, transparent pixel
// 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") ); *data++ = MASK_RED;
free( lpDIBh ); *data++ = MASK_GREEN;
return wxNullImage; *data++ = MASK_BLUE;
} }
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<height; i++ )
{
for( j=0; j<width; j++ )
{
// is this pixel transparent?
if ( *ptbits != 0 )
{
if ( (ptdata[0] == MASK_RED) &&
(ptdata[1] == MASK_GREEN) &&
(ptdata[2] == MASK_BLUE) )
{
// we have to fudge the colour a bit to prevent this
// pixel from appearing transparent
ptdata[2] = MASK_BLUE_REPLACEMENT;
} }
ptdata += 3;
}
else // masked pixel
{
*(ptdata++) = MASK_RED;
*(ptdata++) = MASK_GREEN;
*(ptdata++) = MASK_BLUE;
}
ptbits += 3;
}
ptbits += padding;
} }
image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE); image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
image.SetMask( true );
// free allocated resources
::ReleaseDC(NULL, hdc);
free(lpDIBh);
free(lpBits);
} }
#endif // __WXWINCE__ }
return image; return image;
} }