diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index c055716fcd..3ec8473d36 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -276,6 +276,10 @@ public: WXHBITMAP GetMaskBitmap() const { return m_maskBitmap; } void SetMaskBitmap(WXHBITMAP bmp) { m_maskBitmap = bmp; } +#if wxUSE_IMAGE + bool MSWCreateFromImageMask(const wxImage& image); +#endif // wxUSE_IMAGE + protected: WXHBITMAP m_maskBitmap; diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 2922b49698..ab6de0841f 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -934,40 +934,11 @@ bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc) // finally also set the mask if we have one if ( image.HasMask() ) { - const size_t len = 2*((w+15)/16); - BYTE *src = image.GetData(); - wxScopedArray data(h*len); - memset(data.get(), 0, h*len); - BYTE r = image.GetMaskRed(), - g = image.GetMaskGreen(), - b = image.GetMaskBlue(); - BYTE *dst = data.get(); - for ( int y = 0; y < h; y++, dst += len ) - { - BYTE *dstLine = dst; - BYTE mask = 0x80; - for ( int x = 0; x < w; x++, src += 3 ) - { - if (src[0] != r || src[1] != g || src[2] != b) - *dstLine |= mask; - - if ( (mask >>= 1) == 0 ) - { - dstLine++; - mask = 0x80; - } - } - } - - hbitmap = ::CreateBitmap(w, h, 1, 1, data.get()); - if ( !hbitmap ) - { - wxLogLastError(wxT("CreateBitmap(mask)")); - } + wxMask* mask = new wxMask; + if ( mask->MSWCreateFromImageMask(image) ) + SetMask(mask); else - { - SetMask(new wxMask((WXHBITMAP)hbitmap)); - } + delete mask; } return true; @@ -1699,6 +1670,50 @@ bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) return ok; } +#if wxUSE_IMAGE +bool wxMask::MSWCreateFromImageMask(const wxImage& image) +{ + const int h = image.GetHeight(); + const int w = image.GetWidth(); + + const size_t len = 2*((w+15)/16); + BYTE *src = image.GetData(); + wxScopedArray data(h*len); + memset(data.get(), 0, h*len); + BYTE r = image.GetMaskRed(), + g = image.GetMaskGreen(), + b = image.GetMaskBlue(); + BYTE *dst = data.get(); + for ( int y = 0; y < h; y++, dst += len ) + { + BYTE *dstLine = dst; + BYTE mask = 0x80; + for ( int x = 0; x < w; x++, src += 3 ) + { + if (src[0] != r || src[1] != g || src[2] != b) + *dstLine |= mask; + + if ( (mask >>= 1) == 0 ) + { + dstLine++; + mask = 0x80; + } + } + } + + HBITMAP hbitmap = ::CreateBitmap(w, h, 1, 1, data.get()); + if ( !hbitmap ) + { + wxLogLastError(wxT("CreateBitmap(mask)")); + return false; + } + + m_maskBitmap = hbitmap; + + return true; +} +#endif // wxUSE_IMAGE + wxBitmap wxMask::GetBitmap() const { // We have to do a deep copy of the mask bitmap