From 171f9ab8f42d018da984bf17d003d13c54d6d5a2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 17 Jun 2022 00:02:43 +0100 Subject: [PATCH] Make wxBitmap::HasAlpha() and UseAlpha() available in all ports HasAlpha() was already available in most of them, now ensure that it's present in all of them, especially as it has a reasonable default implementation. UseAlpha() was only present in wxMSW and wxOSX and still remains only implemented there, but provide at least a stub for it elsewhere as well to avoid problems such as that of #17393. See #22545. Closes #17397. --- include/wx/bitmap.h | 6 ++++++ include/wx/gtk/bitmap.h | 2 +- include/wx/msw/bitmap.h | 2 -- include/wx/osx/bitmap.h | 6 ++---- include/wx/qt/bitmap.h | 2 +- interface/wx/bitmap.h | 35 +++++++++++++++++++++++++++++++++++ src/common/bmpbase.cpp | 20 ++++++++++++++++++++ src/osx/core/bitmap.cpp | 4 +++- 8 files changed, 68 insertions(+), 9 deletions(-) diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index 1442c75a36..b5960c5397 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -250,6 +250,12 @@ public: virtual void SetPalette(const wxPalette& palette) = 0; #endif // wxUSE_PALETTE + // Alpha support for 32bpp bitmaps: check if it's used, request that it be + // used or not. + virtual bool HasAlpha() const; + virtual bool UseAlpha(bool use = true); + void ResetAlpha() { UseAlpha(false); } + // copies the contents and mask of the given (colour) icon to the bitmap bool CopyFromIcon(const wxIcon& icon); diff --git a/include/wx/gtk/bitmap.h b/include/wx/gtk/bitmap.h index e53186dc16..ba6902db6d 100644 --- a/include/wx/gtk/bitmap.h +++ b/include/wx/gtk/bitmap.h @@ -145,7 +145,7 @@ public: void *GetRawData(wxPixelDataBase& data, int bpp); void UngetRawData(wxPixelDataBase& data); - bool HasAlpha() const; + bool HasAlpha() const wxOVERRIDE; protected: #if wxUSE_IMAGE diff --git a/include/wx/msw/bitmap.h b/include/wx/msw/bitmap.h index 3ec8473d36..a49b2e6d96 100644 --- a/include/wx/msw/bitmap.h +++ b/include/wx/msw/bitmap.h @@ -176,8 +176,6 @@ public: wxMask *GetMask() const; void SetMask(wxMask *mask); - // these functions are internal and shouldn't be used, they risk to - // disappear in the future bool HasAlpha() const; void UseAlpha(bool use = true); void ResetAlpha() { UseAlpha(false); } diff --git a/include/wx/osx/bitmap.h b/include/wx/osx/bitmap.h index 59008b119f..ff5fcef436 100644 --- a/include/wx/osx/bitmap.h +++ b/include/wx/osx/bitmap.h @@ -180,10 +180,8 @@ public: void *GetRawData(wxPixelDataBase& data, int bpp); void UngetRawData(wxPixelDataBase& data); - // these functions are internal and shouldn't be used, they risk to - // disappear in the future - bool HasAlpha() const; - void UseAlpha(bool use = true); + bool HasAlpha() const wxOVERRIDE; + bool UseAlpha(bool use = true) wxOVERRIDE; // returns the 'native' implementation, a GWorldPtr for the content and one for the mask WXHBITMAP GetHBITMAP( WXHBITMAP * mask = NULL ) const; diff --git a/include/wx/qt/bitmap.h b/include/wx/qt/bitmap.h index 11c4ec81e9..e1ea1f6c03 100644 --- a/include/wx/qt/bitmap.h +++ b/include/wx/qt/bitmap.h @@ -70,7 +70,7 @@ public: // these functions are internal and shouldn't be used, they risk to // disappear in the future - bool HasAlpha() const; + bool HasAlpha() const wxOVERRIDE; QPixmap *GetHandle() const; diff --git a/interface/wx/bitmap.h b/interface/wx/bitmap.h index 0faae1d891..070bd5cf95 100644 --- a/interface/wx/bitmap.h +++ b/interface/wx/bitmap.h @@ -773,6 +773,16 @@ public: */ virtual int GetWidth() const; + /** + Returns true if the bitmap has an alpha channel. + + Note that the fact that a bitmap has an alpha channel doesn't + necessarily mean that it has any transparency, as all of its pixels + could be using wxALPHA_OPAQUE value. To actually examine the alpha + values, the bitmap can be converted to wxImage. + */ + bool HasAlpha() const; + /** Adds the standard bitmap format handlers, which, depending on wxWidgets configuration, can be handlers for Windows bitmap, Windows bitmap resource, @@ -877,6 +887,13 @@ public: */ static void Rescale(wxBitmap& bmp, const wxSize& sizeNeeded); + /** + Remove alpha channel from the bitmap. + + This is the same as calling UseAlpha() with @false argument. + */ + void ResetAlpha(); + /** Saves a bitmap in the named file. @@ -971,6 +988,24 @@ public: Bitmap width in pixels. */ virtual void SetWidth(int width); + + /** + Enable or disable use of alpha channel in this bitmap. + + This function is only useful for 32bpp bitmaps and changes their format + to use, or not use, the fourth byte of the pixel data for the alpha + channel. + + It currently is only implemented in wxMSW and wxOSX and simply always + returns @false under the other platforms. + + @return @true if the operation succeeded, @false otherwise, e.g. when + trying to enable alpha channel support for a non-32bpp bitmap or if + this operation is simply not supported by the current platform. + + @see HasAlpha(), ResetAlpha() + */ + bool UseAlpha(bool use = true); }; /** diff --git a/src/common/bmpbase.cpp b/src/common/bmpbase.cpp index 320a80ebb3..558417c5aa 100644 --- a/src/common/bmpbase.cpp +++ b/src/common/bmpbase.cpp @@ -264,6 +264,26 @@ wxSize wxBitmapBase::GetLogicalSize() const #endif // wxHAS_DPI_INDEPENDENT_PIXELS/!wxHAS_DPI_INDEPENDENT_PIXELS +// ---------------------------------------------------------------------------- +// Alpha support +// ---------------------------------------------------------------------------- + +bool wxBitmapBase::HasAlpha() const +{ + // We assume that only 32bpp bitmaps use alpha (which is always true) and + // that all 32bpp bitmaps do use it (which is not necessarily always the + // case, but the ports where it isn't need to override this function to + // deal with it as we can't do it here). + return GetDepth() == 32; +} + +bool wxBitmapBase::UseAlpha(bool WXUNUSED(use)) +{ + // This function is not implemented in the case class, we don't have any + // generic way to make a bitmap use, or prevent it from using, alpha. + return false; +} + #endif // wxUSE_BITMAP_BASE // ---------------------------------------------------------------------------- diff --git a/src/osx/core/bitmap.cpp b/src/osx/core/bitmap.cpp index e1a93c67cc..a8443648a2 100644 --- a/src/osx/core/bitmap.cpp +++ b/src/osx/core/bitmap.cpp @@ -2069,11 +2069,13 @@ void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(dataBase)) GetBitmapData()->EndRawAccess() ; } -void wxBitmap::UseAlpha(bool use ) +bool wxBitmap::UseAlpha(bool use) { // remember that we are using alpha channel: // we'll need to create a proper mask in UngetRawData() GetBitmapData()->UseAlpha( use ); + + return true; } void wxBitmap::SetSelectedInto(wxDC *dc)