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