Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add ability to set a given rgb color to alpha (gdal.input) #1018

Closed
springmeyer opened this Issue · 3 comments

1 participant

@springmeyer
Owner

closing, best either done by raster colorizer (for single band) or for true rgb color #1161 (setting nodata on the fly) should handle most usecases.

@springmeyer springmeyer closed this
@springmeyer
Owner

in case a more full blow solution is ever needed an old patch (refs #1161) was:

Index: include/mapnik/raster_symbolizer.hpp
===================================================================
--- include/mapnik/raster_symbolizer.hpp    (revision 2276)
+++ include/mapnik/raster_symbolizer.hpp    (working copy)
@@ -25,6 +25,8 @@
 #define RASTER_SYMBOLIZER_HPP

 #include <mapnik/config.hpp>
+#include <mapnik/color.hpp>
+#include <boost/optional/optional.hpp>

 namespace mapnik
 {
@@ -32,7 +34,8 @@
         explicit raster_symbolizer()
             : mode_("normal"),
               scaling_("fast"),
-              opacity_(1.0) {}
+              opacity_(1.0),
+              color_to_alpha_tol_(0) {}

         std::string const& get_mode() const
         {
@@ -58,10 +61,29 @@
         {
             return opacity_;
         }
+        void set_color_to_alpha(mapnik::color color_to_alpha)
+        {
+            color_to_alpha_ = color_to_alpha;
+        }
+        boost::optional<mapnik::color> const& get_color_to_alpha() const
+        {
+            return color_to_alpha_;
+        }
+        void set_color_to_alpha_tol(unsigned color_to_alpha_tol)
+        {
+            color_to_alpha_tol_ = color_to_alpha_tol;
+        }
+        unsigned get_color_to_alpha_tol() const
+        {
+            return color_to_alpha_tol_;
+        }
+
     private:
         std::string mode_;
         std::string scaling_;
         float opacity_;
+        boost::optional<mapnik::color> color_to_alpha_;
+        unsigned color_to_alpha_tol_;
     };
 }

Index: include/mapnik/image_data.hpp
===================================================================
--- include/mapnik/image_data.hpp   (revision 2276)
+++ include/mapnik/image_data.hpp   (working copy)
@@ -26,8 +26,10 @@
 #define IMAGE_DATA_HPP

 #include <mapnik/global.hpp>
+#include <mapnik/color.hpp>
 #include <cassert>
 #include <cstring>
+#include <cmath>

 namespace mapnik 
 {
@@ -81,6 +83,41 @@
                }
             }
         }
+
+        inline void set_color_to_alpha(const mapnik::color& c, unsigned tol)
+        {
+            for (unsigned y = 0; y < height_; ++y)
+            {
+                unsigned int* row_to = getRow(y);
+                for (unsigned x = 0; x < width_; ++x)
+                {
+                    unsigned rgba = row_to[x];
+                    unsigned a = (rgba >> 24) & 0xff;
+                    unsigned r = rgba & 0xff;
+                    unsigned g = (rgba >> 8 ) & 0xff;
+                    unsigned b = (rgba >> 16) & 0xff;
+                    if (tol == 0)
+                    {
+                        if (r == c.red() && g == c.green() && b == c.blue())
+                        {
+                            a = 0;
+                            row_to[x] = (a << 24)| (b << 16) |  (g << 8) | (r) ;
+                        }
+                    }
+                    else
+                    {
+                        if ( (fabs(r - c.red()) <= tol) &&
+                             (fabs(g - c.green()) <= tol) &&
+                             (fabs(b - c.blue()) <= tol)
+                           )
+                        {
+                            a = 0;
+                            row_to[x] = (a << 24)| (b << 16) |  (g << 8) | (r) ;
+                        }
+                    }
+                }
+            }
+        }

         inline const T* getData() const
         {
Index: src/load_map.cpp
===================================================================
--- src/load_map.cpp    (revision 2276)
+++ src/load_map.cpp    (working copy)
@@ -1656,6 +1656,16 @@
            float opacity = get_css<float>(css, css_name);
            raster_sym.set_opacity(opacity);
        }
+       else if (css_name == "color-to-alpha")
+       {
+           color color_to_alpha = get_css<color>(css, css_name);
+           raster_sym.set_color_to_alpha(color_to_alpha);
+       }
+       else if (css_name == "color-to-alpha-tol")
+       {
+           unsigned color_to_alpha_tol = get_css<unsigned>(css, css_name);
+           raster_sym.set_color_to_alpha_tol(color_to_alpha_tol);
+       }
        else
        {
            throw config_error(std::string("Failed to parse unknown CSS ") +
@@ -1683,6 +1693,14 @@
         optional<double> opacity = get_opt_attr<double>(sym, "opacity");
         if (opacity) raster_sym.set_opacity(*opacity);

+        // color-to-alpha
+        optional<color> color_to_alpha = get_opt_attr<color>(sym, "color-to-alpha");
+        if (color_to_alpha) raster_sym.set_color_to_alpha(*color_to_alpha);
+
+        // color-to-alpha-tol
+        optional<unsigned> color_to_alpha_tol = get_opt_attr<unsigned>(sym, "color-to-alpha-tol");
+        if (color_to_alpha_tol) raster_sym.set_color_to_alpha_tol(*color_to_alpha_tol);
+
    rule.append(raster_sym);
     }
     catch (const config_error & ex)
Index: src/cairo_renderer.cpp
===================================================================
--- src/cairo_renderer.cpp  (revision 2276)
+++ src/cairo_renderer.cpp  (working copy)
@@ -997,6 +997,12 @@
                scale_image<ImageData32>(target,raster->data_);
             }

+            boost::optional<color> color_to_alpha = sym.get_color_to_alpha();
+            if (color_to_alpha)
+            {
+                target.set_color_to_alpha(*color_to_alpha,sym.get_color_to_alpha_tol());
+            }
+
             cairo_context context(context_);

             //TODO -- support for advanced image merging
Index: src/agg_renderer.cpp
===================================================================
--- src/agg_renderer.cpp    (revision 2276)
+++ src/agg_renderer.cpp    (working copy)
@@ -739,6 +739,12 @@
                scale_image<ImageData32>(target,raster->data_);
             }

+            boost::optional<color> color_to_alpha = sym.get_color_to_alpha();
+            if (color_to_alpha)
+            {
+                target.set_color_to_alpha(*color_to_alpha,sym.get_color_to_alpha_tol());
+            }
+            
             if (sym.get_mode() == "normal"){
                 if (sym.get_opacity() == 1.0) {
                    pixmap_.set_rectangle_alpha(start_x,start_y,target);
@springmeyer springmeyer referenced this issue from a commit
@springmeyer springmeyer support nodata for paletted images and allow user to set nodata on-th…
…e-fly - closes #1160 and #1161 - refs #688 and refs #730 and refs #50 and refs #1018
46b16c9
@springmeyer
Owner

tracking actually getting this done at #2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.