From c1099d92544fb97cb230248e50cf0c81a2633cdd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Jan 2009 17:23:39 +0000 Subject: [PATCH] allow specifying the mask colour in wxImage::ConvertAlphaToMask() (closes #10143) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58404 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/image.h | 8 ++++++-- interface/wx/image.h | 40 +++++++++++++++++++++++++++++++++++----- src/common/image.cpp | 20 +++++++++++++++----- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index 76b9c81c6a..e0a6760eb3 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -322,9 +322,13 @@ public: bool SetMaskFromImage(const wxImage & mask, unsigned char mr, unsigned char mg, unsigned char mb); - // converts image's alpha channel to mask, if it has any, does nothing - // otherwise: + // converts image's alpha channel to mask (choosing mask colour + // automatically or using the specified colour for the mask), if it has + // any, does nothing otherwise: bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD); + void ConvertAlphaToMask(unsigned char mr, unsigned char mg, unsigned char mb, + unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD); + // This method converts an image where the original alpha // information is only available as a shades of a colour diff --git a/interface/wx/image.h b/interface/wx/image.h index 56739044b0..a45715b2c4 100644 --- a/interface/wx/image.h +++ b/interface/wx/image.h @@ -519,19 +519,49 @@ public: */ unsigned long ComputeHistogram(wxImageHistogram& histogram) const; + //@{ /** If the image has alpha channel, this method converts it to mask. - All pixels with alpha value less than @a threshold are replaced with mask - colour and the alpha channel is removed. Mask colour is chosen automatically - using FindFirstUnusedColour(). - - If the image image doesn't have alpha channel, ConvertAlphaToMask() does nothing. + If the image has an alpha channel, all pixels with alpha value less + than @a threshold are replaced with the mask colour and the alpha + channel is removed. Otherwise nothing is done. + + The mask colour is chosen automatically using + FindFirstUnusedColour() by this function, see the overload below if you + this is not appropriate. @return @false if FindFirstUnusedColour returns @false, @true otherwise. */ bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD); + /** + If the image has alpha channel, this method converts it to mask using + the specified colour as the mask colour. + + If the image has an alpha channel, all pixels with alpha value less + than @a threshold are replaced with the mask colour and the alpha + channel is removed. Otherwise nothing is done. + + @since 2.9.0 + + @param mr + The red component of the mask colour. + @param mg + The green component of the mask colour. + @param mb + The blue component of the mask colour. + @param threshold + Pixels with alpha channel values below the given threshold are + considered to be transparent, i.e. the corresponding mask pixels + are set. Pixels with the alpha values above the threshold are + considered to be opaque. + + */ + void ConvertAlphaToMask(unsigned char mr, unsigned char mg, unsigned char mb, + unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD); + //@} + /** Returns a greyscale version of the image. diff --git a/src/common/image.cpp b/src/common/image.cpp index b3883e6a49..d3bf74c5c4 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1905,16 +1905,28 @@ bool wxImage::SetMaskFromImage(const wxImage& mask, bool wxImage::ConvertAlphaToMask(unsigned char threshold) { - if (!HasAlpha()) + if ( !HasAlpha() ) return true; unsigned char mr, mg, mb; - if (!FindFirstUnusedColour(&mr, &mg, &mb)) + if ( !FindFirstUnusedColour(&mr, &mg, &mb) ) { wxLogError( _("No unused colour in image being masked.") ); return false; } + ConvertAlphaToMask(mr, mg, mb, threshold); + return true; +} + +void wxImage::ConvertAlphaToMask(unsigned char mr, + unsigned char mg, + unsigned char mb, + unsigned char threshold) +{ + if ( !HasAlpha() ) + return; + AllocExclusive(); SetMask(true); @@ -1939,13 +1951,11 @@ bool wxImage::ConvertAlphaToMask(unsigned char threshold) } } - if( !M_IMGDATA->m_staticAlpha ) + if ( !M_IMGDATA->m_staticAlpha ) free(M_IMGDATA->m_alpha); M_IMGDATA->m_alpha = NULL; M_IMGDATA->m_staticAlpha = false; - - return true; } // ---------------------------------------------------------------------------- -- 2.45.2