From ff5ad79411a4d5a88a3f76441ca5713d31cd5614 Mon Sep 17 00:00:00 2001
From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= <vslavik@fastmail.fm>
Date: Mon, 16 Aug 2004 12:31:08 +0000
Subject: [PATCH] added wxImage::ConvertAlphaToMask

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28810 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/image.tex | 16 ++++++++++++++++
 include/wx/image.h      |  7 ++++++-
 src/common/image.cpp    | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex
index 0b0719ea46..741ba4a385 100644
--- a/docs/latex/wx/image.tex
+++ b/docs/latex/wx/image.tex
@@ -257,6 +257,22 @@ WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
 
 Returns number of colours in the histogram.
 
+\membersection{wxImage::ConvertAlphaToMask}\label{wximageconvertalphatomask}
+
+\func{bool}{ConvertAlphaToMask}{\param{unsigned char}{ threshold = 128}}
+
+If the image has alpha channel, this method converts it to mask. All pixels
+with alpha value less than \arg{threshold} are replaced with mask colour
+and the alpha channel is removed. Mask colour is chosen automatically using
+\helpref{FindFirstUnusedColour}{wximagefindfirstunusedcolour}.
+
+If the image image doesn't have alpha channel,
+ConvertAlphaToMask does nothing.
+
+\wxheading{Return value}
+
+\false if FindFirstUnusedColour returns \false, \true otherwise. 
+
 \membersection{wxImage::ConvertToBitmap}\label{wximageconverttobitmap}
 
 \constfunc{wxBitmap}{ConvertToBitmap}{\void}
diff --git a/include/wx/image.h b/include/wx/image.h
index 1789d526c8..f39648b4d7 100644
--- a/include/wx/image.h
+++ b/include/wx/image.h
@@ -185,7 +185,8 @@ public:
     void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
                   unsigned char r2, unsigned char g2, unsigned char b2 );
 
-    // convert to monochrome image (<r,g,b> will be replaced by white, everything else by black)
+    // convert to monochrome image (<r,g,b> will be replaced by white,
+    // everything else by black)
     wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
 
     // these routines are slow but safe
@@ -206,6 +207,10 @@ 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:
+    bool ConvertAlphaToMask(unsigned threshold = 128);
+
     static bool CanRead( const wxString& name );
     static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
     virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
diff --git a/src/common/image.cpp b/src/common/image.cpp
index 369bdf5fcd..238b5195f8 100644
--- a/src/common/image.cpp
+++ b/src/common/image.cpp
@@ -935,6 +935,44 @@ bool wxImage::SetMaskFromImage(const wxImage& mask,
 
     return true;
 }
+    
+bool wxImage::ConvertAlphaToMask(unsigned threshold)
+{
+    if (!HasAlpha())
+        return true;
+
+    unsigned char mr, mg, mb;
+    if (!FindFirstUnusedColour(&mr, &mg, &mb))
+    {
+        wxLogError( _("No unused colour in image being masked.") );
+        return false;
+    }
+    
+    SetMask(true);
+    SetMaskColour(mr, mg, mb);
+    
+    unsigned char *imgdata = GetData();
+    unsigned char *alphadata = GetAlpha();
+
+    size_t w = GetWidth();
+    size_t h = GetHeight();
+
+    for (size_t y = 0; y < h; y++)
+    {
+        for (size_t x = 0; x < w; x++, imgdata += 3, alphadata++)
+        {
+            if (*alphadata < threshold)
+            {
+                imgdata[0] = mr;
+                imgdata[1] = mg;
+                imgdata[2] = mb;
+            }
+        }
+    }
+
+    free(M_IMGDATA->m_alpha);
+    M_IMGDATA->m_alpha = NULL;
+}
 
 #if wxUSE_PALETTE
 
-- 
2.50.0

