Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_05 / synfig-core / src / modules / mod_gradient / lineargradient.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** Template File
4 ** $Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
5 **
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 **
18 ** === N O T E S ===========================================================
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 "lineargradient.h"
32
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 SYNFIG_LAYER_INIT(LinearGradient);
49 SYNFIG_LAYER_SET_NAME(LinearGradient,"linear_gradient");
50 SYNFIG_LAYER_SET_LOCAL_NAME(LinearGradient,_("Linear Gradient"));
51 SYNFIG_LAYER_SET_CATEGORY(LinearGradient,_("Gradients"));
52 SYNFIG_LAYER_SET_VERSION(LinearGradient,"0.0");
53 SYNFIG_LAYER_SET_CVS_ID(LinearGradient,"$Id: lineargradient.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
54
55 /* === P R O C E D U R E S ================================================= */
56
57 /* === M E T H O D S ======================================================= */
58
59 inline void
60 LinearGradient::sync()
61 {
62         diff=(p2-p1);
63         const Real mag(diff.inv_mag());
64         diff*=mag*mag;
65 }
66
67
68 LinearGradient::LinearGradient():
69         p1(1,1),
70         p2(-1,-1),
71         gradient(Color::black(), Color::white()),
72         loop(false),
73         zigzag(false)
74 {
75         sync();
76 }
77
78 inline Color
79 LinearGradient::color_func(const Point &point, float supersample)const
80 {
81         Real dist(point*diff-p1*diff);
82         
83         if(loop)
84                 dist-=floor(dist);
85         
86         if(zigzag)
87         {
88                 dist*=2.0;
89                 supersample*=2.0;
90                 if(dist>1)dist=2.0-dist;
91         }
92
93         if(loop)
94         {
95                 if(dist+supersample*0.5>1.0)
96                 {
97                         Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
98                         pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
99                         return pool.demult_alpha();
100                 }
101                 if(dist-supersample*0.5<0.0)
102                 {
103                         Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
104                         pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
105                         return pool.demult_alpha();
106                 }
107         }
108         return gradient(dist,supersample);
109 }
110
111 float
112 LinearGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
113 {
114         return pw/(p2-p1).mag();
115 }
116
117 synfig::Layer::Handle
118 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
119 {
120         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
121                 return const_cast<LinearGradient*>(this);
122         if(get_amount()==0.0)
123                 return context.hit_check(point);
124         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
125                 return const_cast<LinearGradient*>(this);
126         return context.hit_check(point);
127 }
128
129 bool
130 LinearGradient::set_param(const String & param, const ValueBase &value)
131 {
132         if(param=="p1" && value.same_as(p1))
133         {
134                 p1=value.get(p1);
135                 sync();
136                 return true;
137         }
138         if(param=="p2" && value.same_as(p2))
139         {
140                 p2=value.get(p2);
141                 sync();
142                 return true;
143         }
144         //IMPORT(p1);
145         //IMPORT(p2);
146         
147         
148         IMPORT(gradient);
149         IMPORT(loop);
150         IMPORT(zigzag);
151         return Layer_Composite::set_param(param,value); 
152 }
153
154 ValueBase
155 LinearGradient::get_param(const String & param)const
156 {
157         EXPORT(p1);
158         EXPORT(p2);
159         EXPORT(gradient);
160         EXPORT(loop);
161         EXPORT(zigzag);
162         
163         EXPORT_NAME();
164         EXPORT_VERSION();
165                 
166         return Layer_Composite::get_param(param);       
167 }
168
169 Layer::Vocab
170 LinearGradient::get_param_vocab()const
171 {
172         Layer::Vocab ret(Layer_Composite::get_param_vocab());
173         
174         ret.push_back(ParamDesc("p1")
175                 .set_local_name(_("Point 1"))
176                 .set_connect("p2")
177         );
178         ret.push_back(ParamDesc("p2")
179                 .set_local_name(_("Point 2"))
180         );
181         ret.push_back(ParamDesc("gradient")
182                 .set_local_name(_("Gradient"))
183         );
184         ret.push_back(ParamDesc("loop")
185                 .set_local_name(_("Loop"))
186         );
187         ret.push_back(ParamDesc("zigzag")
188                 .set_local_name(_("ZigZag"))
189         );
190         
191         return ret;
192 }
193
194 Color
195 LinearGradient::get_color(Context context, const Point &point)const
196 {
197         const Color color(color_func(point));
198
199         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
200                 return color;
201         else
202                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
203 }
204
205 bool
206 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
207 {
208         SuperCallback supercb(cb,0,9500,10000);
209
210         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
211         {
212                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
213         }
214         else
215         {
216                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
217                         return false;
218                 if(get_amount()==0)
219                         return true;
220         }
221
222                 
223         int x,y;
224
225         Surface::pen pen(surface->begin());
226         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
227         Point pos;
228         Point tl(renddesc.get_tl());
229         const int w(surface->get_w());
230         const int h(surface->get_h());
231         
232         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
233         {
234                 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
235                         for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
236                                 pen.put_value(color_func(pos,calc_supersample(pos,pw,ph)));
237         }
238         else
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::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
243         }
244
245         // Mark our progress as finished
246         if(cb && !cb->amount_complete(10000,10000))
247                 return false;
248
249         return true;
250 }