From: Vadim Zeitlin Date: Sat, 19 Feb 2005 21:51:37 +0000 (+0000) Subject: added InitAlpha() (replaces patch 991168) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/828f093601fa1985a8740ebf7102ab1e4cfe68f7 added InitAlpha() (replaces patch 991168) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@32213 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 8710399209..134bbf566c 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -53,6 +53,7 @@ All: - "Alt" key (VK_MENU) now results in WXK_ALT keyboard event, not WXK_MENU - wxFFile::ReadAll() now takes an optional wxMBConv parameter - wxCommandProcessor::MarkAsSaved() and IsDirty() added (Angela Wrobel) +- added wxStackWalker and related classes (Win32 and some Unix versions only) All (GUI): @@ -74,6 +75,7 @@ All (GUI): of the progress (with new "Skip" button in dialog) - wxGenericListCtrl::SetItemState(-1) now changes the state of all items as in wxMSW version (Gunnar Roth) +- added wxImage::InitAlpha() Unix: diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex index 18c099fb71..498e1f510d 100644 --- a/docs/latex/wx/image.tex +++ b/docs/latex/wx/image.tex @@ -639,6 +639,16 @@ Returns true if the given option is present. The function is case-insensitive to \helpref{wxImage::GetOptionInt}{wximagegetoptionint} +\membersection{wxImage::InitAlpha}\label{wximageinitalpha} + +\func{void}{InitAlpha}{\void} + +Initializes the image alpha channel data. It is an error to call it +if the image already has alpha data. If it doesn't, alpha data will be +by default initialized to all pixels being fully opaque. But if the image has a +a mask colour, all mask pixels will be completely transparent. + + \membersection{wxImage::InitStandardHandlers}\label{wximageinitstandardhandlers} \func{static void}{InitStandardHandlers}{\void} diff --git a/include/wx/image.h b/include/wx/image.h index 7a9e084fb2..eea421031f 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -272,6 +272,7 @@ public: unsigned char *GetAlpha() const; // may return NULL! bool HasAlpha() const { return GetAlpha() != NULL; } void SetAlpha(unsigned char *alpha = NULL); + void InitAlpha(); // Mask functions void SetMaskColour( unsigned char r, unsigned char g, unsigned char b ); diff --git a/src/common/image.cpp b/src/common/image.cpp index 6270bb1890..204c4b6657 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -912,6 +912,44 @@ unsigned char *wxImage::GetAlpha() const return M_IMGDATA->m_alpha; } +void wxImage::InitAlpha() +{ + wxCHECK_RET( !HasAlpha(), wxT("image already has an alpha channel") ); + + // initialize memory for alpha channel + SetAlpha(); + + unsigned char *alpha = M_IMGDATA->m_alpha; + const size_t lenAlpha = M_IMGDATA->m_width * M_IMGDATA->m_height; + + static const unsigned char ALPHA_TRANSPARENT = 0; + static const unsigned char ALPHA_OPAQUE = 0xff; + if ( HasMask() ) + { + // use the mask to initialize the alpha channel. + const unsigned char * const alphaEnd = alpha + lenAlpha; + + const unsigned char mr = M_IMGDATA->m_maskRed; + const unsigned char mg = M_IMGDATA->m_maskGreen; + const unsigned char mb = M_IMGDATA->m_maskBlue; + for ( unsigned char *src = M_IMGDATA->m_data; + alpha < alphaEnd; + src += 3, alpha++ ) + { + *alpha = (src[0] == mr && src[1] == mg && src[2] == mb) + ? ALPHA_TRANSPARENT + : ALPHA_OPAQUE; + } + + M_IMGDATA->m_hasMask = false; + } + else // no mask + { + // make the image fully opaque + memset(alpha, ALPHA_OPAQUE, lenAlpha); + } +} + // ---------------------------------------------------------------------------- // mask support // ----------------------------------------------------------------------------