From bde82aa9fb8afd78f06bd05aabe100f668085789 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sat, 16 Jan 2021 22:19:14 +0100 Subject: [PATCH] Fix adding image using bitmap and mask colour to nonmasked wxImageList (wxMSW) If image using bitmap and mask colour is added to nonmasked wxImageList we need to convert effective mask to alpha channel values prior to adding the image to the native list to preserve transparency. --- src/msw/imaglist.cpp | 23 ++++------------------- tests/graphics/imagelist.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index 72530fc61e..e7a3285373 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -231,26 +231,11 @@ int wxImageList::Add(const wxBitmap& bitmap, const wxBitmap& mask) // 'bitmap'. int wxImageList::Add(const wxBitmap& bitmap, const wxColour& maskColour) { - HBITMAP hbmp; - -#if wxUSE_WXDIB && wxUSE_IMAGE - // See the comment in overloaded Add() above. + HBITMAP hbmp = NULL; AutoHBITMAP hbmpRelease; - if ( bitmap.HasAlpha() ) - { - wxImage img = bitmap.ConvertToImage(); - - if ( wxApp::GetComCtl32Version() < 600 ) - { - img.ClearAlpha(); - } - - hbmp = wxDIB(img, wxDIB::PixelFormat_NotPreMultiplied).Detach(); - hbmpRelease.Init(hbmp); - } - else -#endif // wxUSE_WXDIB && wxUSE_IMAGE - hbmp = GetHbitmapOf(bitmap); + AutoHBITMAP hbmpMask; + wxMask mask(bitmap, maskColour); + GetImageListBitmaps(bitmap, mask.GetBitmap(), m_useMask, hbmpRelease, hbmpMask, hbmp); int index = ImageList_AddMasked(GetHImageList(), hbmp, diff --git a/tests/graphics/imagelist.cpp b/tests/graphics/imagelist.cpp index 03828d94de..a4ca7d6efb 100644 --- a/tests/graphics/imagelist.cpp +++ b/tests/graphics/imagelist.cpp @@ -100,6 +100,14 @@ TEST_CASE("ImageList:WithMask", "[imagelist][withmask]") CHECK(bmp2.GetMask() != NULL); CHECK(bmp2.GetWidth() == 32); CHECK(bmp2.GetHeight() == 32); + + idx = il.Add(bmpRGB, *wxRED); + CHECK(il.GetImageCount() == 3); + wxBitmap bmp3 = il.GetBitmap(idx); + CHECK(bmp3.HasAlpha() == false); + CHECK(bmp3.GetMask() != NULL); + CHECK(bmp3.GetWidth() == 32); + CHECK(bmp3.GetHeight() == 32); } SECTION("Add RGBA image to list") @@ -120,6 +128,14 @@ TEST_CASE("ImageList:WithMask", "[imagelist][withmask]") CHECK(bmp2.GetMask() != NULL); CHECK(bmp2.GetWidth() == 32); CHECK(bmp2.GetHeight() == 32); + + idx = il.Add(bmpRGBA, *wxRED); + CHECK(il.GetImageCount() == 3); + wxBitmap bmp3 = il.GetBitmap(idx); + CHECK(bmp3.HasAlpha() == false); + CHECK(bmp3.GetMask() != NULL); + CHECK(bmp3.GetWidth() == 32); + CHECK(bmp3.GetHeight() == 32); } SECTION("Add icon to list") @@ -257,6 +273,14 @@ TEST_CASE("ImageList:NoMask", "[imagelist][nomask]") CHECK(bmp2.GetMask() == NULL); CHECK(bmp2.GetWidth() == 32); CHECK(bmp2.GetHeight() == 32); + + idx = il.Add(bmpRGB, *wxRED); + CHECK(il.GetImageCount() == 3); + wxBitmap bmp3 = il.GetBitmap(idx); + CHECK(bmp3.HasAlpha() == true); + CHECK(bmp3.GetMask() == NULL); + CHECK(bmp3.GetWidth() == 32); + CHECK(bmp3.GetHeight() == 32); } SECTION("Add RGBA image to list") @@ -277,6 +301,14 @@ TEST_CASE("ImageList:NoMask", "[imagelist][nomask]") CHECK(bmp2.GetMask() == NULL); CHECK(bmp2.GetWidth() == 32); CHECK(bmp2.GetHeight() == 32); + + idx = il.Add(bmpRGBA, *wxRED); + CHECK(il.GetImageCount() == 3); + wxBitmap bmp3 = il.GetBitmap(idx); + CHECK(bmp3.HasAlpha() == true); + CHECK(bmp3.GetMask() == NULL); + CHECK(bmp3.GetWidth() == 32); + CHECK(bmp3.GetHeight() == 32); } SECTION("Add icon to list")