- wxBitmap bitmap;
- wxCHECK_MSG( Ok(), bitmap, "invalid image" );
- int width = GetWidth();
- int height = GetHeight();
- bitmap.SetWidth( width );
- bitmap.SetHeight( height );
- bitmap.SetDepth( wxDisplayDepth() );
-
- int headersize = sizeof(BITMAPINFOHEADER);
- LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
- wxCHECK_MSG( lpDIBh, bitmap, "could not allocate memory for DIB header" );
-
-// Fill in the DIB header
- lpDIBh->bmiHeader.biSize = headersize;
- lpDIBh->bmiHeader.biWidth = width;
- lpDIBh->bmiHeader.biHeight = -height;
- lpDIBh->bmiHeader.biSizeImage = width * height * 3;
-
- lpDIBh->bmiHeader.biPlanes = 1;
- lpDIBh->bmiHeader.biBitCount = 24;
- lpDIBh->bmiHeader.biCompression = BI_RGB;
- lpDIBh->bmiHeader.biClrUsed = 0;
-
-// These seem not needed for our purpose here.
-// lpDIBh->bmiHeader.biClrImportant = 0;
-// lpDIBh->bmiHeader.biXPelsPerMeter = 0;
-// lpDIBh->bmiHeader.biYPelsPerMeter = 0;
-
- unsigned char *lpBits = (unsigned char *) malloc( width*height*3 );
- if( !lpBits )
- {
- wxFAIL_MSG( "could not allocate memory for DIB" );
- free( lpDIBh );
- return bitmap;
- }
-
- unsigned char *data = GetData();
-
- unsigned char *ptdata = data, *ptbits = lpBits;
- for( int i=0; i<width*height; i++ )
- {
- *(ptbits++) = *(ptdata+2);
- *(ptbits++) = *(ptdata+1);
- *(ptbits++) = *(ptdata );
- ptdata += 3;
- }
-
- HDC hdc = ::GetDC(NULL);
-
- HBITMAP hbitmap;
- hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
-
-// The above line is equivalent to the following two lines.
-// hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
-// ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
-// or the following lines
-// hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
-// HDC memdc = ::CreateCompatibleDC( hdc );
-// ::SelectObject( memdc, hbitmap);
-// ::SetDIBitsToDevice( memdc, 0, 0, width, height,
-// 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
-// ::SelectObject( memdc, 0 );
-// ::DeleteDC( memdc );
-
- bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
-
- if( HasMask() )
- {
- unsigned char r = GetMaskRed();
- unsigned char g = GetMaskGreen();
- unsigned char b = GetMaskBlue();
- unsigned char zero = 0, one = 255;
- ptdata = data;
- ptbits = lpBits;
- for( int i=0; i<width*height; i++ )
- {
- if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
- {
- *(ptbits++) = one;
- *(ptbits++) = one;
- *(ptbits++) = one;
- }
- else
- {
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- }
- }
- hbitmap = ::CreateBitmap( (WORD)width, (WORD)height, 1, 1, NULL );
- ::SetDIBits( hdc, hbitmap, 0, (WORD)height, lpBits, lpDIBh, DIB_RGB_COLORS);
- wxMask *mask = new wxMask();
- mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
- bitmap.SetMask( mask );
-
-/* The following can also be used but is slow to run
- wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
- wxMask *mask = new wxMask( bitmap, colour );
- bitmap.SetMask( mask );
-*/
- }
-
- ::ReleaseDC(NULL, hdc);
- free(lpDIBh);
- free(lpBits);
-
- if( bitmap.GetHBITMAP() )
- bitmap.SetOk( TRUE );
- else
- bitmap.SetOk( FALSE );
-
- return bitmap;
-}