1 /* === S Y N F I G ========================================================= */
2 /*! \file lineargradient.cpp
3 ** \brief Implementation of the "Linear Gradient" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "lineargradient.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 SYNFIG_LAYER_INIT(LinearGradient);
52 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
53 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,N_("Linear Gradient"));
54 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,N_("Gradients"));
55 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
56 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id$");
58 /* === P R O C E D U R E S ================================================= */
60 /* === M E T H O D S ======================================================= */
63 LinearGradient::sync()
66 const Real mag(diff.inv_mag());
71 LinearGradient::LinearGradient():
72 Layer_Composite(1.0,Color::BLEND_COMPOSITE),
75 gradient(Color::black(), Color::white()),
83 LinearGradient::color_func(const Point &point, float supersample)const
85 Real dist(point*diff-p1*diff);
94 if(dist>1)dist=2.0-dist;
99 if(dist+supersample*0.5>1.0)
101 float left(supersample*0.5-(dist-1.0));
102 float right(supersample*0.5+(dist-1.0));
103 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
104 if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
105 else pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
106 return pool.demult_alpha();
108 if(dist-supersample*0.5<0.0)
110 float left(supersample*0.5-dist);
111 float right(supersample*0.5+dist);
112 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
113 if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
114 else pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
115 return pool.demult_alpha();
118 return gradient(dist,supersample);
122 LinearGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
124 return pw/(p2-p1).mag();
127 synfig::Layer::Handle
128 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
130 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
131 return const_cast<LinearGradient*>(this);
132 if(get_amount()==0.0)
133 return context.hit_check(point);
134 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
135 return const_cast<LinearGradient*>(this);
136 return context.hit_check(point);
140 LinearGradient::set_param(const String & param, const ValueBase &value)
142 if(param=="p1" && value.same_type_as(p1))
148 if(param=="p2" && value.same_type_as(p2))
161 return Layer_Composite::set_param(param,value);
165 LinearGradient::get_param(const String & param)const
176 return Layer_Composite::get_param(param);
180 LinearGradient::get_param_vocab()const
182 Layer::Vocab ret(Layer_Composite::get_param_vocab());
184 ret.push_back(ParamDesc("p1")
185 .set_local_name(_("Point 1"))
188 ret.push_back(ParamDesc("p2")
189 .set_local_name(_("Point 2"))
191 ret.push_back(ParamDesc("gradient")
192 .set_local_name(_("Gradient"))
194 ret.push_back(ParamDesc("loop")
195 .set_local_name(_("Loop"))
197 ret.push_back(ParamDesc("zigzag")
198 .set_local_name(_("ZigZag"))
205 LinearGradient::get_color(Context context, const Point &point)const
207 const Color color(color_func(point));
209 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
212 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
216 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
218 SuperCallback supercb(cb,0,9500,10000);
220 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
222 surface->set_wh(renddesc.get_w(),renddesc.get_h());
226 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
235 Surface::pen pen(surface->begin());
236 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
238 Point tl(renddesc.get_tl());
239 const int w(surface->get_w());
240 const int h(surface->get_h());
242 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
244 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
245 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
246 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
250 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
251 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
252 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
255 // Mark our progress as finished
256 if(cb && !cb->amount_complete(10000,10000))