Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / src / modules / mod_gradient / radialgradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file mod_gradient/radialgradient.cpp
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
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>
41
42 #include "radialgradient.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 /* === G L O B A L S ======================================================= */
53
54 SYNFIG_LAYER_INIT(RadialGradient);
55 SYNFIG_LAYER_SET_NAME(RadialGradient,"radial_gradient");
56 SYNFIG_LAYER_SET_LOCAL_NAME(RadialGradient,_("Radial Gradient"));
57 SYNFIG_LAYER_SET_CATEGORY(RadialGradient,_("Gradients"));
58 SYNFIG_LAYER_SET_VERSION(RadialGradient,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(RadialGradient,"$Id$");
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 /* === E N T R Y P O I N T ================================================= */
66
67 RadialGradient::RadialGradient():
68         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
69         gradient(Color::black(),Color::white()),
70         center(0,0),
71         radius(0.5),
72         loop(false),
73         zigzag(false)
74 {
75 }
76
77 bool
78 RadialGradient::set_param(const String & param, const ValueBase &value)
79 {
80         IMPORT(gradient);
81         IMPORT(center);
82         IMPORT(radius);
83         IMPORT(loop);
84         IMPORT(zigzag);
85
86         return Layer_Composite::set_param(param,value);
87 }
88
89 ValueBase
90 RadialGradient::get_param(const String &param)const
91 {
92         EXPORT(gradient);
93         EXPORT(center);
94         EXPORT(radius);
95         EXPORT(loop);
96         EXPORT(zigzag);
97
98         EXPORT_NAME();
99         EXPORT_VERSION();
100
101         return Layer_Composite::get_param(param);
102 }
103
104 Layer::Vocab
105 RadialGradient::get_param_vocab()const
106 {
107         Layer::Vocab ret(Layer_Composite::get_param_vocab());
108
109         ret.push_back(ParamDesc("gradient")
110                 .set_local_name(_("Gradient"))
111         );
112
113         ret.push_back(ParamDesc("center")
114                 .set_local_name(_("Center"))
115         );
116
117         ret.push_back(ParamDesc("radius")
118                 .set_local_name(_("Radius"))
119                 .set_description(_("This is the radius of the circle"))
120                 .set_is_distance()
121                 .set_origin("center")
122         );
123
124         ret.push_back(ParamDesc("loop")
125                 .set_local_name(_("Loop"))
126         );
127
128         ret.push_back(ParamDesc("zigzag")
129                 .set_local_name(_("ZigZag"))
130         );
131
132         return ret;
133 }
134
135 inline Color
136 RadialGradient::color_func(const Point &point, float supersample)const
137 {
138         Real dist((point-center).mag()/radius);
139
140         if(zigzag)
141         {
142                 dist*=2.0;
143                 supersample*=2.0;
144                 if(dist>1)dist=2.0-dist;
145         }
146
147         if(loop)
148         {
149                 dist-=floor(dist);
150
151                 if(dist+supersample*0.5>1.0)
152                 {
153                         float  left(supersample*0.5-(dist-1.0));
154                         float right(supersample*0.5+(dist-1.0));
155                         Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
156                         if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
157                         else            pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
158                         return pool.demult_alpha();
159                 }
160                 if(dist-supersample*0.5<0.0)
161                 {
162                         float  left(supersample*0.5-dist);
163                         float right(supersample*0.5+dist);
164                         Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
165                         if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
166                         else            pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
167                         return pool.demult_alpha();
168                 }
169         }
170
171         return gradient(dist,supersample);
172 }
173
174
175 float
176 RadialGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
177 {
178 //      return sqrt(pw*pw+ph*ph)/radius;
179         return 1.2*pw/radius;
180 }
181
182 synfig::Layer::Handle
183 RadialGradient::hit_check(synfig::Context context, const synfig::Point &point)const
184 {
185         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
186                 return const_cast<RadialGradient*>(this);
187         if(get_amount()==0.0)
188                 return context.hit_check(point);
189         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
190                 return const_cast<RadialGradient*>(this);
191         return context.hit_check(point);
192 }
193
194 Color
195 RadialGradient::get_color(Context context, const Point &pos)const
196 {
197         const Color color(color_func(pos));
198
199         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
200                 return color;
201         else
202                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
203 }
204
205 bool
206 RadialGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
207 {
208         SuperCallback supercb(cb,0,9500,10000);
209
210         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
211         {
212                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
213         }
214         else
215         {
216                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
217                         return false;
218                 if(get_amount()==0)
219                         return true;
220         }
221
222
223         int x,y;
224
225         Surface::pen pen(surface->begin());
226         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
227         Point pos;
228         Point tl(renddesc.get_tl());
229         const int w(surface->get_w());
230         const int h(surface->get_h());
231
232         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
233         {
234                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
235                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
236                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
237         }
238         else
239         {
240                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
241                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
242                                 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
243         }
244
245         // Mark our progress as finished
246         if(cb && !cb->amount_complete(10000,10000))
247                 return false;
248
249         return true;
250 }