1 /* === S Y N F I G ========================================================= */
2 /*! \file mod_gradient/radialgradient.cpp
3 ** \brief Implementation of the "Radial Gradient" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
42 #include "radialgradient.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_LAYER_INIT(RadialGradient);
55 SYNFIG_LAYER_SET_NAME(RadialGradient,"radial_gradient");
56 SYNFIG_LAYER_SET_LOCAL_NAME(RadialGradient,N_("Radial Gradient"));
57 SYNFIG_LAYER_SET_CATEGORY(RadialGradient,N_("Gradients"));
58 SYNFIG_LAYER_SET_VERSION(RadialGradient,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(RadialGradient,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 /* === E N T R Y P O I N T ================================================= */
67 RadialGradient::RadialGradient():
68 Layer_Composite(1.0,Color::BLEND_COMPOSITE),
69 gradient(Color::black(),Color::white()),
75 Layer::Vocab voc(get_param_vocab());
76 Layer::fill_static(voc);
80 RadialGradient::set_param(const String & param, const ValueBase &value)
88 return Layer_Composite::set_param(param,value);
92 RadialGradient::get_param(const String ¶m)const
103 return Layer_Composite::get_param(param);
107 RadialGradient::get_param_vocab()const
109 Layer::Vocab ret(Layer_Composite::get_param_vocab());
111 ret.push_back(ParamDesc("gradient")
112 .set_local_name(_("Gradient"))
115 ret.push_back(ParamDesc("center")
116 .set_local_name(_("Center"))
119 ret.push_back(ParamDesc("radius")
120 .set_local_name(_("Radius"))
121 .set_description(_("This is the radius of the circle"))
123 .set_origin("center")
126 ret.push_back(ParamDesc("loop")
127 .set_local_name(_("Loop"))
130 ret.push_back(ParamDesc("zigzag")
131 .set_local_name(_("ZigZag"))
138 RadialGradient::color_func(const Point &point, float supersample)const
140 Real dist((point-center).mag()/radius);
146 if(dist>1)dist=2.0-dist;
153 if(dist+supersample*0.5>1.0)
155 float left(supersample*0.5-(dist-1.0));
156 float right(supersample*0.5+(dist-1.0));
157 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
158 if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
159 else pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
160 return pool.demult_alpha();
162 if(dist-supersample*0.5<0.0)
164 float left(supersample*0.5-dist);
165 float right(supersample*0.5+dist);
166 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
167 if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
168 else pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
169 return pool.demult_alpha();
173 return gradient(dist,supersample);
178 RadialGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
180 // return sqrt(pw*pw+ph*ph)/radius;
181 return 1.2*pw/radius;
184 synfig::Layer::Handle
185 RadialGradient::hit_check(synfig::Context context, const synfig::Point &point)const
187 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
188 return const_cast<RadialGradient*>(this);
189 if(get_amount()==0.0)
190 return context.hit_check(point);
191 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
192 return const_cast<RadialGradient*>(this);
193 return context.hit_check(point);
197 RadialGradient::get_color(Context context, const Point &pos)const
199 const Color color(color_func(pos));
201 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
204 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
208 RadialGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
210 SuperCallback supercb(cb,0,9500,10000);
212 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
214 surface->set_wh(renddesc.get_w(),renddesc.get_h());
218 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
227 Surface::pen pen(surface->begin());
228 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
230 Point tl(renddesc.get_tl());
231 const int w(surface->get_w());
232 const int h(surface->get_h());
234 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
236 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
237 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
238 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
242 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
243 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
244 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
247 // Mark our progress as finished
248 if(cb && !cb->amount_complete(10000,10000))