Extract mask creation from wxImage in a separate function

Allow reusing the code for creating a mask from wxImage mask from
elsewhere.

No real changes yet, this is a just a refactoring.

This commit is best viewed with git --color-moved
--color-moved-ws=ignore-all-space options.
This commit is contained in:
Vadim Zeitlin 2022-05-05 23:18:11 +01:00
parent 416ebf79e0
commit 0e21b52d57
2 changed files with 52 additions and 33 deletions

View File

@ -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;

View File

@ -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<BYTE> 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<BYTE> 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