Enable $Id$ expansion.
[synfig.git] / synfig-core / trunk / src / modules / mod_gradient / lineargradient.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file lineargradient.cpp
3 **      \brief Template Header
4 **
5 **      \legal
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **      \endlegal
18 **
19 ** === N O T E S ===========================================================
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 "lineargradient.h"
33
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42
43 #endif
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 SYNFIG_LAYER_INIT(LinearGradient);
50 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
51 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,_("Linear Gradient"));
52 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,_("Gradients"));
53 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
54 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id$");
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 inline void
61 LinearGradient::sync()
62 {
63         diff=(p2-p1);
64         const Real mag(diff.inv_mag());
65         diff*=mag*mag;
66 }
67
68
69 LinearGradient::LinearGradient():
70         p1(1,1),
71         p2(-1,-1),
72         gradient(Color::black(), Color::white()),
73         loop(false),
74         zigzag(false)
75 {
76         sync();
77 }
78
79 inline Color
80 LinearGradient::color_func(const Point &point, float supersample)const
81 {
82         Real dist(point*diff-p1*diff);
83
84         if(loop)
85                 dist-=floor(dist);
86
87         if(zigzag)
88         {
89                 dist*=2.0;
90                 supersample*=2.0;
91                 if(dist>1)dist=2.0-dist;
92         }
93
94         if(loop)
95         {
96                 if(dist+supersample*0.5>1.0)
97                 {
98                         Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
99                         pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
100                         return pool.demult_alpha();
101                 }
102                 if(dist-supersample*0.5<0.0)
103                 {
104                         Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
105                         pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
106                         return pool.demult_alpha();
107                 }
108         }
109         return gradient(dist,supersample);
110 }
111
112 float
113 LinearGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
114 {
115         return pw/(p2-p1).mag();
116 }
117
118 synfig::Layer::Handle
119 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
120 {
121         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
122                 return const_cast<LinearGradient*>(this);
123         if(get_amount()==0.0)
124                 return context.hit_check(point);
125         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
126                 return const_cast<LinearGradient*>(this);
127         return context.hit_check(point);
128 }
129
130 bool
131 LinearGradient::set_param(const String & param, const ValueBase &value)
132 {
133         if(param=="p1" && value.same_as(p1))
134         {
135                 p1=value.get(p1);
136                 sync();
137                 return true;
138         }
139         if(param=="p2" && value.same_as(p2))
140         {
141                 p2=value.get(p2);
142                 sync();
143                 return true;
144         }
145         //IMPORT(p1);
146         //IMPORT(p2);
147
148
149         IMPORT(gradient);
150         IMPORT(loop);
151         IMPORT(zigzag);
152         return Layer_Composite::set_param(param,value);
153 }
154
155 ValueBase
156 LinearGradient::get_param(const String & param)const
157 {
158         EXPORT(p1);
159         EXPORT(p2);
160         EXPORT(gradient);
161         EXPORT(loop);
162         EXPORT(zigzag);
163
164         EXPORT_NAME();
165         EXPORT_VERSION();
166
167         return Layer_Composite::get_param(param);
168 }
169
170 Layer::Vocab
171 LinearGradient::get_param_vocab()const
172 {
173         Layer::Vocab ret(Layer_Composite::get_param_vocab());
174
175         ret.push_back(ParamDesc("p1")
176                 .set_local_name(_("Point 1"))
177                 .set_connect("p2")
178         );
179         ret.push_back(ParamDesc("p2")
180                 .set_local_name(_("Point 2"))
181         );
182         ret.push_back(ParamDesc("gradient")
183                 .set_local_name(_("Gradient"))
184         );
185         ret.push_back(ParamDesc("loop")
186                 .set_local_name(_("Loop"))
187         );
188         ret.push_back(ParamDesc("zigzag")
189                 .set_local_name(_("ZigZag"))
190         );
191
192         return ret;
193 }
194
195 Color
196 LinearGradient::get_color(Context context, const Point &point)const
197 {
198         const Color color(color_func(point));
199
200         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
201                 return color;
202         else
203                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
204 }
205
206 bool
207 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
208 {
209         SuperCallback supercb(cb,0,9500,10000);
210
211         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
212         {
213                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
214         }
215         else
216         {
217                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
218                         return false;
219                 if(get_amount()==0)
220                         return true;
221         }
222
223
224         int x,y;
225
226         Surface::pen pen(surface->begin());
227         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
228         Point pos;
229         Point tl(renddesc.get_tl());
230         const int w(surface->get_w());
231         const int h(surface->get_h());
232
233         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
234         {
235                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
236                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
237                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
238         }
239         else
240         {
241                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
242                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
243                                 pen.put_value(Color::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
244         }
245
246         // Mark our progress as finished
247         if(cb && !cb->amount_complete(10000,10000))
248                 return false;
249
250         return true;
251 }