+wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
+{
+ Init();
+
+#ifndef __WXMICROWIN__
+ wxBitmapRefData *refData = new wxBitmapRefData;
+ m_refData = refData;
+
+ refData->m_width = width;
+ refData->m_height = height;
+ refData->m_depth = depth;
+
+ char *data;
+ if ( depth == 1 )
+ {
+ // we assume that it is in XBM format which is not quite the same as
+ // the format CreateBitmap() wants because the order of bytes in the
+ // line is inversed!
+ const size_t bytesPerLine = (width + 7) / 8;
+ const size_t padding = bytesPerLine % 2;
+ const size_t len = height * ( padding + bytesPerLine );
+ data = (char *)malloc(len);
+ const char *src = bits;
+ char *dst = data;
+
+ for ( int rows = 0; rows < height; rows++ )
+ {
+ for ( size_t cols = 0; cols < bytesPerLine; cols++ )
+ {
+ unsigned char val = *src++;
+ unsigned char reversed = 0;
+
+ for ( int bits = 0; bits < 8; bits++)
+ {
+ reversed <<= 1;
+ reversed |= (val & 0x01);
+ val >>= 1;
+ }
+ *dst++ = reversed;
+ }
+
+ if ( padding )
+ *dst++ = 0;
+ }
+ }
+ else
+ {
+ // bits should already be in Windows standard format
+ data = (char *)bits; // const_cast is harmless
+ }
+
+ HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateBitmap"));
+ }
+
+ if ( data != bits )
+ {
+ free(data);
+ }
+
+ SetHBITMAP((WXHBITMAP)hbmp);
+#endif
+}
+
+// Create from XPM data
+#if wxUSE_IMAGE && wxUSE_XPM
+bool wxBitmap::CreateFromXpm(const char **data)
+#else
+bool wxBitmap::CreateFromXpm(const char **WXUNUSED(data))
+#endif
+{
+#if wxUSE_IMAGE && wxUSE_XPM
+ Init();
+
+ wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
+
+ wxXPMDecoder decoder;
+ wxImage img = decoder.ReadData(data);
+ wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
+
+ *this = wxBitmap(img);
+ return TRUE;
+#else
+ return FALSE;
+#endif
+}
+
+wxBitmap::wxBitmap(int w, int h, int d)
+{
+ Init();
+
+ (void)Create(w, h, d);
+}
+
+wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
+{
+ Init();
+
+ (void)Create(w, h, dc);
+}
+
+wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
+{
+ Init();
+
+ (void)Create(data, type, width, height, depth);
+}
+
+wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
+{
+ Init();
+
+ LoadFile(filename, (int)type);
+}
+
+bool wxBitmap::Create(int width, int height, int depth)
+{
+ return DoCreate(width, height, depth, 0);
+}
+
+bool wxBitmap::Create(int width, int height, const wxDC& dc)
+{
+ wxCHECK_MSG( dc.Ok(), FALSE, _T("invalid HDC in wxBitmap::Create()") );
+
+ return DoCreate(width, height, -1, dc.GetHDC());
+}
+
+bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
+{
+ UnRef();
+
+ m_refData = new wxBitmapRefData;
+
+ GetBitmapData()->m_width = w;
+ GetBitmapData()->m_height = h;
+
+ HBITMAP hbmp wxDUMMY_INITIALIZE(0);
+
+#ifndef NEVER_USE_DIB
+ if ( wxShouldCreateDIB(w, h, d, hdc) )
+ {
+ if ( d == -1 )
+ {
+ // create DIBs without alpha channel by default
+ d = 24;
+ }
+
+ wxDIB dib(w, h, d);
+ if ( !dib.IsOk() )
+ return FALSE;
+
+ // don't delete the DIB section in dib object dtor
+ hbmp = dib.Detach();
+
+ GetBitmapData()->m_isDIB = TRUE;
+ GetBitmapData()->m_depth = d;
+ }
+ else // create a DDB
+#endif // NEVER_USE_DIB
+ {
+#ifndef ALWAYS_USE_DIB
+#ifndef __WXMICROWIN__
+ if ( d > 0 )
+ {
+ hbmp = ::CreateBitmap(w, h, 1, d, NULL);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateBitmap"));
+ }
+
+ GetBitmapData()->m_depth = d;
+ }
+ else // d == 0, create bitmap compatible with the screen
+#endif // !__WXMICROWIN__
+ {
+ ScreenHDC dc;
+ hbmp = ::CreateCompatibleBitmap(dc, w, h);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateCompatibleBitmap"));
+ }
+
+ GetBitmapData()->m_depth = wxDisplayDepth();
+ }
+#endif // !ALWAYS_USE_DIB
+ }
+
+ SetHBITMAP((WXHBITMAP)hbmp);
+
+ return Ok();
+}
+
+#if wxUSE_IMAGE
+
+// ----------------------------------------------------------------------------
+// wxImage to/from conversions for Microwin
+// ----------------------------------------------------------------------------
+
+// Microwin versions are so different from normal ones that it really doesn't
+// make sense to use #ifdefs inside the function bodies
+#ifdef __WXMICROWIN__
+
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
+{
+ // Set this to 1 to experiment with mask code,
+ // which currently doesn't work
+ #define USE_MASKS 0
+
+ m_refData = new wxBitmapRefData();
+
+ // Initial attempt at a simple-minded implementation.
+ // The bitmap will always be created at the screen depth,
+ // so the 'depth' argument is ignored.
+
+ HDC hScreenDC = ::GetDC(NULL);
+ int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
+
+ HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
+ HBITMAP hMaskBitmap = NULL;
+ HBITMAP hOldMaskBitmap = NULL;
+ HDC hMaskDC = NULL;
+ unsigned char maskR = 0;
+ unsigned char maskG = 0;
+ unsigned char maskB = 0;
+
+ // printf("Created bitmap %d\n", (int) hBitmap);
+ if (hBitmap == NULL)
+ {
+ ::ReleaseDC(NULL, hScreenDC);
+ return FALSE;
+ }
+ HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
+
+ HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ // created an mono-bitmap for the possible mask
+ bool hasMask = image.HasMask();
+
+ if ( hasMask )
+ {
+#if USE_MASKS
+ // FIXME: we should be able to pass bpp = 1, but
+ // GdBlit can't handle a different depth
+#if 0
+ hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
+#else
+ hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
+#endif
+ maskR = image.GetMaskRed();
+ maskG = image.GetMaskGreen();
+ maskB = image.GetMaskBlue();
+
+ if (!hMaskBitmap)
+ {
+ hasMask = FALSE;
+ }
+ else
+ {
+ hScreenDC = ::GetDC(NULL);
+ hMaskDC = ::CreateCompatibleDC(hScreenDC);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
+ }
+#else
+ hasMask = FALSE;
+#endif
+ }
+
+ int i, j;
+ for (i = 0; i < image.GetWidth(); i++)
+ {
+ for (j = 0; j < image.GetHeight(); j++)
+ {
+ unsigned char red = image.GetRed(i, j);
+ unsigned char green = image.GetGreen(i, j);
+ unsigned char blue = image.GetBlue(i, j);
+
+ ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
+
+ if (hasMask)
+ {
+ // scan the bitmap for the transparent colour and set the corresponding
+ // pixels in the mask to BLACK and the rest to WHITE
+ if (maskR == red && maskG == green && maskB == blue)
+ ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
+ else
+ ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
+ }
+ }
+ }
+
+ ::SelectObject(hMemDC, hOldBitmap);
+ ::DeleteDC(hMemDC);
+ if (hasMask)
+ {
+ ::SelectObject(hMaskDC, hOldMaskBitmap);
+ ::DeleteDC(hMaskDC);
+
+ ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
+ }
+
+ SetWidth(image.GetWidth());
+ SetHeight(image.GetHeight());
+ SetDepth(screenDepth);
+ SetHBITMAP( (WXHBITMAP) hBitmap );
+
+#if wxUSE_PALETTE
+ // Copy the palette from the source image
+ SetPalette(image.GetPalette());
+#endif // wxUSE_PALETTE
+
+ return TRUE;
+}
+
+wxImage wxBitmap::ConvertToImage() const
+{
+ // Initial attempt at a simple-minded implementation.
+ // The bitmap will always be created at the screen depth,
+ // so the 'depth' argument is ignored.
+ // TODO: transparency (create a mask image)
+
+ if (!Ok())
+ {
+ wxFAIL_MSG( wxT("bitmap is invalid") );
+ return wxNullImage;
+ }
+
+ wxImage image;
+
+ wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
+
+ // create an wxImage object
+ int width = GetWidth();
+ int height = GetHeight();
+ image.Create( width, height );
+ unsigned char *data = image.GetData();
+ if( !data )
+ {
+ wxFAIL_MSG( wxT("could not allocate data for image") );
+ return wxNullImage;
+ }
+
+ HDC hScreenDC = ::GetDC(NULL);
+
+ HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
+ ::ReleaseDC(NULL, hScreenDC);
+
+ HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
+
+ HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
+
+ int i, j;
+ for (i = 0; i < GetWidth(); i++)
+ {
+ for (j = 0; j < GetHeight(); j++)
+ {
+ COLORREF color = ::GetPixel(hMemDC, i, j);
+ unsigned char red = GetRValue(color);
+ unsigned char green = GetGValue(color);
+ unsigned char blue = GetBValue(color);
+
+ image.SetRGB(i, j, red, green, blue);
+ }
+ }
+
+ ::SelectObject(hMemDC, hOldBitmap);
+ ::DeleteDC(hMemDC);
+
+#if wxUSE_PALETTE
+ // Copy the palette from the source image
+ if (GetPalette())
+ image.SetPalette(* GetPalette());
+#endif // wxUSE_PALETTE
+
+ return image;
+}
+
+#endif // __WXMICROWIN__
+
+// ----------------------------------------------------------------------------
+// wxImage to/from conversions
+// ----------------------------------------------------------------------------
+
+#if wxUSE_WXDIB
+
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
+{
+ return CreateFromImage(image, depth, 0);
+}
+
+bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
+{
+ wxCHECK_MSG( dc.Ok(), FALSE,
+ _T("invalid HDC in wxBitmap::CreateFromImage()") );
+
+ return CreateFromImage(image, -1, dc.GetHDC());
+}
+
+bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
+{
+ wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
+
+ UnRef();
+
+ // first convert the image to DIB
+ const int h = image.GetHeight();
+ const int w = image.GetWidth();
+
+ wxDIB dib(image);
+ if ( !dib.IsOk() )
+ return FALSE;
+
+
+ // store the bitmap parameters
+ wxBitmapRefData *refData = new wxBitmapRefData;
+ refData->m_width = w;
+ refData->m_height = h;
+ refData->m_hasAlpha = image.HasAlpha();
+
+ m_refData = refData;
+
+
+ // next either store DIB as is or create a DDB from it
+ HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
+
+ // are we going to use DIB?
+ //
+ // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
+ if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
+ {
+ // don't delete the DIB section in dib object dtor
+ hbitmap = dib.Detach();
+
+ refData->m_isDIB = TRUE;
+ refData->m_depth = dib.GetDepth();
+ }
+#ifndef ALWAYS_USE_DIB
+ else // we need to convert DIB to DDB
+ {
+ hbitmap = dib.CreateDDB((HDC)hdc);
+
+ refData->m_depth = depth == -1 ? dib.GetDepth() : depth;
+ }
+#endif // !ALWAYS_USE_DIB
+
+ // validate this object
+ SetHBITMAP((WXHBITMAP)hbitmap);
+
+ // finally also set the mask if we have one
+ if ( image.HasMask() )
+ {
+ SetMask(new wxMask(*this, wxColour(image.GetMaskRed(),
+ image.GetMaskGreen(),
+ image.GetMaskBlue())));
+ }
+
+ return TRUE;
+}
+
+wxImage wxBitmap::ConvertToImage() const
+{
+ wxDIB dib(*this);
+
+ if ( !dib.IsOk() )
+ {
+ 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 )
+ {
+ 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<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.SetMask( true );
+
+ // free allocated resources
+ ::ReleaseDC(NULL, hdc);
+ free(lpDIBh);
+ free(lpBits);
+ }
+#endif // __WXWINCE__
+
+ return image;
+}
+
+#endif // wxUSE_WXDIB
+
+#endif // wxUSE_IMAGE
+
+// ----------------------------------------------------------------------------
+// loading and saving bitmaps
+// ----------------------------------------------------------------------------
+
+bool wxBitmap::LoadFile(const wxString& filename, long type)
+{
+ UnRef();
+
+ wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
+
+ if ( handler )
+ {
+ m_refData = new wxBitmapRefData;
+
+ return handler->LoadFile(this, filename, type, -1, -1);
+ }
+#if wxUSE_IMAGE
+ else // no bitmap handler found
+ {
+ wxImage image;
+ if ( image.LoadFile( filename, type ) && image.Ok() )
+ {
+ *this = wxBitmap(image);
+
+ return TRUE;
+ }
+ }
+#endif // wxUSE_IMAGE
+
+ return FALSE;
+}
+
+bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
+{
+ UnRef();
+
+ wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
+
+ if ( !handler )
+ {
+ wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
+
+ return FALSE;
+ }