moreupdates
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / conicalgradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file conicalgradient.cpp
3 **      \brief Template Header
4 **
5 **      $Id: conicalgradient.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 <synfig/string.h>
32 #include <synfig/time.h>
33 #include <synfig/context.h>
34 #include <synfig/paramdesc.h>
35 #include <synfig/renddesc.h>
36 #include <synfig/surface.h>
37 #include <synfig/value.h>
38 #include <synfig/valuenode.h>
39 #include <synfig/angle.h>
40
41 #include "conicalgradient.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(ConicalGradient);
54 SYNFIG_LAYER_SET_NAME(ConicalGradient,"conical_gradient");
55 SYNFIG_LAYER_SET_LOCAL_NAME(ConicalGradient,_("Conical Gradient"));
56 SYNFIG_LAYER_SET_CATEGORY(ConicalGradient,_("Gradients"));
57 SYNFIG_LAYER_SET_VERSION(ConicalGradient,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(ConicalGradient,"$Id: conicalgradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
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 ConicalGradient::ConicalGradient():
67         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68         gradient(Color::black(),Color::white()),
69         center(0,0),
70         angle(Angle::zero()),
71         symmetric(false)
72 {
73 }
74         
75 bool
76 ConicalGradient::set_param(const String & param, const ValueBase &value)
77 {
78         IMPORT(gradient);
79         IMPORT(center);
80         IMPORT(angle);
81         IMPORT(symmetric);
82         return Layer_Composite::set_param(param,value);
83 }
84
85 ValueBase
86 ConicalGradient::get_param(const String &param)const
87 {
88         EXPORT(gradient);
89         EXPORT(center);
90         EXPORT(angle);
91         EXPORT(symmetric);
92
93         EXPORT_NAME();
94         EXPORT_VERSION();
95                 
96         return Layer_Composite::get_param(param);       
97 }
98
99 Layer::Vocab
100 ConicalGradient::get_param_vocab()const
101 {
102         Layer::Vocab ret(Layer_Composite::get_param_vocab());
103         
104         ret.push_back(ParamDesc("gradient")
105                 .set_local_name(_("Gradient"))
106         );
107
108         ret.push_back(ParamDesc("center")
109                 .set_local_name(_("Center"))
110         );
111         
112         ret.push_back(ParamDesc("angle")
113                 .set_local_name(_("Angle"))
114                 .set_origin("center")
115         );
116
117         ret.push_back(ParamDesc("symmetric")
118                 .set_local_name(_("Symmetric"))
119         );
120         
121         return ret;
122 }
123
124 inline Color
125 ConicalGradient::color_func(const Point &pos, float supersample)const
126 {
127         const Point centered(pos-center);
128         Angle::rot a=Angle::tan(-centered[1],centered[0]).mod();
129         a+=angle;
130         Real dist(a.mod().get());
131         
132         dist-=floor(dist);
133
134         if(symmetric)
135         {
136                 dist*=2.0;
137                 supersample*=2.0;
138                 if(dist>1)dist=2.0-dist;
139         }
140 /*      if(dist+supersample*0.5>1.0)
141         {
142                 Color pool(gradient(dist,supersample*0.5)*(1.0-(dist-supersample*0.5)));
143                 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5)*((dist+supersample*0.5)-1.0);
144                 if(pool.get_a() && pool.is_valid())
145                 {
146                         pool.set_r(pool.get_r()/pool.get_a());
147                         pool.set_g(pool.get_g()/pool.get_a());
148                         pool.set_b(pool.get_b()/pool.get_a());
149                         pool.set_a(pool.get_a()/supersample);
150                 }
151                 return pool;
152         }
153
154         if(dist-supersample*0.5<0.0)
155         {
156                 Color pool(gradient(dist,supersample*0.5)*(dist+supersample*0.5));
157                 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5)*(-(dist-supersample*0.5));
158                 if(pool.get_a() && pool.is_valid())
159                 {
160                         pool.set_r(pool.get_r()/pool.get_a());
161                         pool.set_g(pool.get_g()/pool.get_a());
162                         pool.set_b(pool.get_b()/pool.get_a());
163                         pool.set_a(pool.get_a()/supersample);
164                         return pool;
165                 }
166         }
167 */
168         if(dist+supersample*0.5>1.0)
169         {
170                 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
171                 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
172                 return pool.demult_alpha();
173         }
174         if(dist-supersample*0.5<0.0)
175         {
176                 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
177                 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
178                 return pool.demult_alpha();
179         }
180
181         return gradient(dist,supersample);
182 }
183
184 float
185 ConicalGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
186 {
187         Point adj(x-center);
188         if(abs(adj[0])<abs(pw*0.5) && abs(adj[1])<abs(ph*0.5))
189                 return 0.5;
190         return (pw/Point(x-center).mag())/(PI*2);
191 }
192
193 synfig::Layer::Handle
194 ConicalGradient::hit_check(synfig::Context context, const synfig::Point &point)const
195 {
196         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
197                 return const_cast<ConicalGradient*>(this);
198         if(get_amount()==0.0)
199                 return context.hit_check(point);
200         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
201                 return const_cast<ConicalGradient*>(this);
202         return context.hit_check(point);
203 }
204
205 Color
206 ConicalGradient::get_color(Context context, const Point &pos)const
207 {
208         const Color color(color_func(pos));
209         
210         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
211                 return color;
212         else
213                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
214 }
215         
216 bool
217 ConicalGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
218 {
219         SuperCallback supercb(cb,0,9500,10000);
220
221         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
222         {
223                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
224         }
225         else
226         {
227                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
228                         return false;
229                 if(get_amount()==0)
230                         return true;
231         }
232
233                 
234         int x,y;
235
236         Surface::pen pen(surface->begin());
237         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
238         Point pos;
239         Point tl(renddesc.get_tl());
240         const int w(surface->get_w());
241         const int h(surface->get_h());
242         
243         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
244         {
245                 if(quality<9)
246                 {
247                         for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
248                                 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
249                                         pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
250                 }
251                 else
252                 {
253                         for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
254                                 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
255                                         pen.put_value(color_func(pos,0));
256                 }
257         }
258         else
259         {
260                 if(quality<9)
261                 {
262                         for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
263                                 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
264                                         pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
265                 }
266                 else
267                 {
268                         for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
269                                 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
270                                         pen.put_value(Color::blend(color_func(pos,0),pen.get_value(),get_amount(),get_blend_method()));
271                 }
272         }
273
274         // Mark our progress as finished
275         if(cb && !cb->amount_complete(10000,10000))
276                 return false;
277
278         return true;
279 }