From a99e58e0740f9a8ab5918e671dfe242c8baf48cf Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 21 Jan 2016 17:31:48 +0100 Subject: [PATCH 1/2] Add method to convert wxDIB to wxImage enforcing alpha channel values (wxMSW). wxDIB::ConvertToImage called with Convert_AlphaAuto converts wxDIB to wxImage with automatic checking if 32 bpp DIB contains real alpha values (legacy way). When it is called with Convert_AlphaAlwaysIf32bpp then automatic checking is disabled and 32 bpp DIB is unconditionally converted to ARGB wxImage. --- include/wx/msw/dib.h | 13 ++++++++++++- src/msw/dib.cpp | 4 ++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/wx/msw/dib.h b/include/wx/msw/dib.h index a78230639b..b74880312b 100644 --- a/include/wx/msw/dib.h +++ b/include/wx/msw/dib.h @@ -164,7 +164,18 @@ public: bool Create(const wxImage& image, PixelFormat pf = PixelFormat_PreMultiplied); // create wxImage having the same data as this DIB - wxImage ConvertToImage() const; + + // Possible options of conversion to wxImage + enum ConversionFlags + { + // Determine whether 32bpp DIB contains real alpha channel + // and return wxImage with or without alpha channel values. + Convert_AlphaAuto, + // Assume that 32bpp DIB contains valid alpha channel and always + // return wxImage with alpha channel values in this case. + Convert_AlphaAlwaysIf32bpp + }; + wxImage ConvertToImage(ConversionFlags flags = Convert_AlphaAuto) const; #endif // wxUSE_IMAGE diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index 66a28cbf3e..616c59c7a4 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -676,7 +676,7 @@ bool wxDIB::Create(const wxImage& image, PixelFormat pf) return true; } -wxImage wxDIB::ConvertToImage() const +wxImage wxDIB::ConvertToImage(ConversionFlags flags) const { wxCHECK_MSG( IsOk(), wxNullImage, wxT("can't convert invalid DIB to wxImage") ); @@ -776,7 +776,7 @@ wxImage wxDIB::ConvertToImage() const if ( hasOpaque && hasTransparent ) hasAlpha = true; - if ( !hasAlpha && image.HasAlpha() ) + if ( !hasAlpha && image.HasAlpha() && flags == Convert_AlphaAuto ) image.ClearAlpha(); return image; From f6787b72081cd367b7b50d0fb7d683de46ab8fdb Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Thu, 21 Jan 2016 17:33:02 +0100 Subject: [PATCH 2/2] Convert 32bpp wxBitmap with alpha flag set to wxImage with alpha channel values. Create wxImage with alpha values if bitmap is explicitly marked as an ARGB one. Otherwise it will be checked automatically whether 32bpp bitmap contains real alpha channel values or not and output wxImage will be created with or without alpha channel values. --- src/msw/bitmap.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/msw/bitmap.cpp b/src/msw/bitmap.cpp index 780d12cb74..9b2c4ad9dc 100644 --- a/src/msw/bitmap.cpp +++ b/src/msw/bitmap.cpp @@ -932,7 +932,14 @@ wxImage wxBitmap::ConvertToImage() const } // and then DIB to our wxImage - wxImage image = dib.ConvertToImage(); + // By default, we autodetect the presence of alpha and consider unnecessary + // to create the alpha channel in wxImage if we don't have any effective + // alpha in the bitmap because its depth, on its own, is not an indicator + // that it uses alpha as it could be just the default screen depth. However + // if the user had explicitly called UseAlpha(), then we consider + // that the resulting image should really have the alpha channel too. + wxImage image = dib.ConvertToImage(HasAlpha() ? + wxDIB::Convert_AlphaAlwaysIf32bpp : wxDIB::Convert_AlphaAuto); if ( !image.IsOk() ) { return wxNullImage;