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