Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / spiralgradient.cpp
1 /* === S I N F G =========================================================== */
2 /*!     \file spiralgradient.cpp
3 **      \brief Template Header
4 **
5 **      $Id: spiralgradient.cpp,v 1.1.1.1 2005/01/04 01:23:11 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 "spiralgradient.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(SpiralGradient);
53 SINFG_LAYER_SET_NAME(SpiralGradient,"spiral_gradient");
54 SINFG_LAYER_SET_LOCAL_NAME(SpiralGradient,_("Spiral Gradient"));
55 SINFG_LAYER_SET_CATEGORY(SpiralGradient,_("Gradients"));
56 SINFG_LAYER_SET_VERSION(SpiralGradient,"0.1");
57 SINFG_LAYER_SET_CVS_ID(SpiralGradient,"$Id: spiralgradient.cpp,v 1.1.1.1 2005/01/04 01:23:11 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 SpiralGradient::SpiralGradient():
66         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
67         gradient(Color::black(),Color::white()),
68         center(0,0),
69         radius(0.5),
70         angle(Angle::zero()),
71         clockwise(false)
72 {
73 }
74         
75 bool
76 SpiralGradient::set_param(const String & param, const ValueBase &value)
77 {
78         IMPORT(gradient);
79         IMPORT(center);
80         IMPORT(radius);
81         IMPORT(angle);
82         IMPORT(clockwise);
83         
84         return Layer_Composite::set_param(param,value);
85 }
86
87 ValueBase
88 SpiralGradient::get_param(const String &param)const
89 {
90         EXPORT(gradient);
91         EXPORT(center);
92         EXPORT(radius);
93         EXPORT(angle);
94         EXPORT(clockwise);
95         
96         EXPORT_NAME();
97         EXPORT_VERSION();
98                 
99         return Layer_Composite::get_param(param);       
100 }
101
102 Layer::Vocab
103 SpiralGradient::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_is_distance()
119                 .set_origin("center")
120         );
121
122         ret.push_back(ParamDesc("angle")
123                 .set_local_name(_("Angle"))
124                 .set_origin("center")
125         );
126
127         ret.push_back(ParamDesc("clockwise")
128                 .set_local_name(_("Clockwise"))
129         );
130         
131         return ret;
132 }
133
134 inline Color
135 SpiralGradient::color_func(const Point &pos, float supersample)const
136 {
137         const Point centered(pos-center);
138         Angle a;
139         a=Angle::tan(-centered[1],centered[0]).mod();
140         a=a+angle;
141         
142         if(supersample<0.00001)supersample=0.00001;
143         
144         Real dist((pos-center).mag()/radius);
145         if(clockwise)
146                 dist+=Angle::rot(a.mod()).get();
147         else
148                 dist-=Angle::rot(a.mod()).get();
149                 
150         dist-=floor(dist);
151         if(dist+supersample*0.5>1.0)
152         {
153                 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
154                 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
155                 return pool.demult_alpha();
156         }
157         if(dist-supersample*0.5<0.0)
158         {
159                 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
160                 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
161                 return pool.demult_alpha();
162         }
163         
164         return gradient(dist,supersample);
165 }
166
167 float
168 SpiralGradient::calc_supersample(const sinfg::Point &x, float pw,float ph)const
169 {
170         return (1.41421*pw/radius+(1.41421*pw/Point(x-center).mag())/(PI*2))*0.5;
171 }
172
173 sinfg::Layer::Handle
174 SpiralGradient::hit_check(sinfg::Context context, const sinfg::Point &point)const
175 {
176         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
177                 return const_cast<SpiralGradient*>(this);
178         if(get_amount()==0.0)
179                 return context.hit_check(point);
180         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
181                 return const_cast<SpiralGradient*>(this);
182         return context.hit_check(point);
183 }
184
185 Color
186 SpiralGradient::get_color(Context context, const Point &pos)const
187 {
188         const Color color(color_func(pos));
189         
190         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
191                 return color;
192         else
193                 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
194 }
195         
196 bool
197 SpiralGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
198 {
199         SuperCallback supercb(cb,0,9500,10000);
200
201         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
202         {
203                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
204         }
205         else
206         {
207                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
208                         return false;
209                 if(get_amount()==0)
210                         return true;
211         }
212
213                 
214         int x,y;
215
216         Surface::pen pen(surface->begin());
217         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
218         Point pos;
219         Point tl(renddesc.get_tl());
220         const int w(surface->get_w());
221         const int h(surface->get_h());
222         
223         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
224         {
225                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
226                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
227                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
228         }
229         else
230         {
231                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
232                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
233                                 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
234         }
235
236         // Mark our progress as finished
237         if(cb && !cb->amount_complete(10000,10000))
238                 return false;
239
240         return true;
241 }