+ support multiple color stops in colorize-alpha

( TODO: add support for:  <color-stop>     = <color> [ <percentage> | <length> ]? )
This commit is contained in:
artemp 2013-03-20 17:21:09 +00:00
parent 4b713f6ed0
commit 542805e4d6
4 changed files with 29 additions and 29 deletions

View file

@ -32,6 +32,7 @@
#include <boost/variant/static_visitor.hpp> #include <boost/variant/static_visitor.hpp>
#include <boost/gil/gil_all.hpp> #include <boost/gil/gil_all.hpp>
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>
#include <boost/foreach.hpp>
// agg // agg
#include "agg_basics.h" #include "agg_basics.h"
@ -39,7 +40,7 @@
#include "agg_pixfmt_rgba.h" #include "agg_pixfmt_rgba.h"
#include "agg_scanline_u.h" #include "agg_scanline_u.h"
#include "agg_blur.h" #include "agg_blur.h"
#include "agg_gradient_lut.h"
// stl // stl
#include <cmath> #include <cmath>
@ -409,18 +410,22 @@ void apply_filter(Src & src, colorize_alpha const& op)
{ {
using namespace boost::gil; using namespace boost::gil;
mapnik::color const& c0 = op.c0_; agg::gradient_lut<agg::color_interpolator<agg::rgba8> > grad_lut;
mapnik::color const& c1 = op.c1_; grad_lut.remove_all();
uint8_t reds[256]; std::size_t size = op.size();
uint8_t greens[256]; if (size < 2) return;
uint8_t blues[256];
for (unsigned a=0; a < 256;++a) double step = 1.0/(size-1);
double offset = 0.0;
BOOST_FOREACH( mapnik::color const& c, op)
{ {
reds[a] = (c0.red() + (c1.red() - c0.red()) * a) >> 8; grad_lut.add_color(offset, agg::rgba(c.red()/256.0,
greens[a] = (c0.green() + (c1.green() - c0.green()) * a) >> 8; c.green()/256.0,
blues[a] = (c0.blue() + (c1.blue() - c0.blue()) * a) >> 8; c.blue()/256.0,
c.alpha()/256.0));
offset += step;
} }
grad_lut.build_lut();
rgba8_view_t src_view = rgba8_view(src); rgba8_view_t src_view = rgba8_view(src);
for (int y=0; y<src_view.height(); ++y) for (int y=0; y<src_view.height(); ++y)
@ -434,14 +439,10 @@ void apply_filter(Src & src, colorize_alpha const& op)
uint8_t & a = get_color(src_it[x], alpha_t()); uint8_t & a = get_color(src_it[x], alpha_t());
if ( a > 0) if ( a > 0)
{ {
agg::rgba8 c = grad_lut[a];
//r = (c0.red() + (c1.red() - c0.red()) * a) >> 8; r = (c.r * a + 255) >> 8;
//g = (c0.green() + (c1.green() - c0.green()) * a) >> 8; g = (c.g * a + 255) >> 8;
//b = (c0.blue() + (c1.blue() - c0.blue()) * a) >> 8; b = (c.b * a + 255) >> 8;
r = (reds[a] * a + 255) >> 8;
g = (greens[a] * a + 255) >> 8;
b = (blues[a] * a + 255) >> 8;
#if 0 #if 0
// rainbow // rainbow
r = 0; r = 0;

View file

@ -27,6 +27,7 @@
#include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/qi.hpp>
// mapnik // mapnik
#include <mapnik/css_color_grammar.hpp> #include <mapnik/css_color_grammar.hpp>
#include <mapnik/image_filter.hpp>
// stl // stl
#include <vector> #include <vector>
@ -45,7 +46,7 @@ struct image_filter_grammar :
qi::rule<Iterator, qi::locals<int,int>, void(ContType&), qi::ascii::space_type> agg_blur_filter; qi::rule<Iterator, qi::locals<int,int>, void(ContType&), qi::ascii::space_type> agg_blur_filter;
qi::rule<Iterator, qi::locals<double,double,double,double,double,double,double,double>, qi::rule<Iterator, qi::locals<double,double,double,double,double,double,double,double>,
void(ContType&), qi::ascii::space_type> hsla_filter; void(ContType&), qi::ascii::space_type> hsla_filter;
qi::rule<Iterator, qi::locals<mapnik::color,mapnik::color>, void(ContType&), qi::ascii::space_type> colorize_alpha_filter; qi::rule<Iterator, qi::locals<mapnik::filter::colorize_alpha>, void(ContType&), qi::ascii::space_type> colorize_alpha_filter;
qi::rule<Iterator, qi::ascii::space_type> no_args; qi::rule<Iterator, qi::ascii::space_type> no_args;
qi::uint_parser< unsigned, 10, 1, 3 > radius_; qi::uint_parser< unsigned, 10, 1, 3 > radius_;
css_color_grammar<Iterator> css_color_; css_color_grammar<Iterator> css_color_;

View file

@ -98,17 +98,16 @@ struct hsla
double a1; double a1;
}; };
struct colorize_alpha struct colorize_alpha : std::vector<mapnik::color>
{ {
colorize_alpha() {}
colorize_alpha(mapnik::color const& c0, mapnik::color const& c1) colorize_alpha(mapnik::color const& c0, mapnik::color const& c1)
: c0_(c0),
c1_(c1)
{ {
this->push_back(c0);
this->push_back(c1);
// TODO: support multiple color-stops // TODO: support multiple color-stops
// https://developer.mozilla.org/en-US/docs/CSS/linear-gradient // https://developer.mozilla.org/en-US/docs/CSS/linear-gradient
} }
mapnik::color c0_;
mapnik::color c1_;
}; };
typedef boost::variant<filter::blur, typedef boost::variant<filter::blur,

View file

@ -22,7 +22,6 @@
// mapnik // mapnik
#include <mapnik/image_filter_grammar.hpp> #include <mapnik/image_filter_grammar.hpp>
#include <mapnik/image_filter.hpp>
// boost // boost
#include <boost/version.hpp> #include <boost/version.hpp>
@ -110,11 +109,11 @@ image_filter_grammar<Iterator,ContType>::image_filter_grammar()
[push_back(_r1, construct<mapnik::filter::hsla>(_a,_b,_c,_d,_e,_f,_g,_h))] [push_back(_r1, construct<mapnik::filter::hsla>(_a,_b,_c,_d,_e,_f,_g,_h))]
; ;
colorize_alpha_filter = lit("colorize-alpha") colorize_alpha_filter = lit("colorize-alpha")[_a = construct<mapnik::filter::colorize_alpha>()]
>> lit('(') >> lit('(')
>> css_color_[_a = _1] >> lit(',') >> css_color_[push_back(_a, _1)]
>> css_color_[_b = _1] >> lit(')') >> +(lit(',') >> css_color_[push_back(_a, _1)])
[push_back(_r1,construct<mapnik::filter::colorize_alpha>(_a,_b))] >> lit(')') [push_back(_r1,_a)]
; ;
no_args = -(lit('(') >> lit(')')); no_args = -(lit('(') >> lit(')'));