Partially fix 1684240: gets rid of most of the artifacts
[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                         float  left(supersample*0.5-(dist-1.0));
99                         float right(supersample*0.5+(dist-1.0));
100                         Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
101                         if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
102                         else            pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
103                         return pool.demult_alpha();
104                 }
105                 if(dist-supersample*0.5<0.0)
106                 {
107                         float  left(supersample*0.5-dist);
108                         float right(supersample*0.5+dist);
109                         Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
110                         if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
111                         else            pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
112                         return pool.demult_alpha();
113                 }
114         }
115         return gradient(dist,supersample);
116 }
117
118 float
119 LinearGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
120 {
121         return pw/(p2-p1).mag();
122 }
123
124 synfig::Layer::Handle
125 LinearGradient::hit_check(synfig::Context context, const synfig::Point &point)const
126 {
127         if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
128                 return const_cast<LinearGradient*>(this);
129         if(get_amount()==0.0)
130                 return context.hit_check(point);
131         if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE) && color_func(point).get_a()>0.5)
132                 return const_cast<LinearGradient*>(this);
133         return context.hit_check(point);
134 }
135
136 bool
137 LinearGradient::set_param(const String & param, const ValueBase &value)
138 {
139         if(param=="p1" && value.same_as(p1))
140         {
141                 p1=value.get(p1);
142                 sync();
143                 return true;
144         }
145         if(param=="p2" && value.same_as(p2))
146         {
147                 p2=value.get(p2);
148                 sync();
149                 return true;
150         }
151         //IMPORT(p1);
152         //IMPORT(p2);
153
154
155         IMPORT(gradient);
156         IMPORT(loop);
157         IMPORT(zigzag);
158         return Layer_Composite::set_param(param,value);
159 }
160
161 ValueBase
162 LinearGradient::get_param(const String & param)const
163 {
164         EXPORT(p1);
165         EXPORT(p2);
166         EXPORT(gradient);
167         EXPORT(loop);
168         EXPORT(zigzag);
169
170         EXPORT_NAME();
171         EXPORT_VERSION();
172
173         return Layer_Composite::get_param(param);
174 }
175
176 Layer::Vocab
177 LinearGradient::get_param_vocab()const
178 {
179         Layer::Vocab ret(Layer_Composite::get_param_vocab());
180
181         ret.push_back(ParamDesc("p1")
182                 .set_local_name(_("Point 1"))
183                 .set_connect("p2")
184         );
185         ret.push_back(ParamDesc("p2")
186                 .set_local_name(_("Point 2"))
187         );
188         ret.push_back(ParamDesc("gradient")
189                 .set_local_name(_("Gradient"))
190         );
191         ret.push_back(ParamDesc("loop")
192                 .set_local_name(_("Loop"))
193         );
194         ret.push_back(ParamDesc("zigzag")
195                 .set_local_name(_("ZigZag"))
196         );
197
198         return ret;
199 }
200
201 Color
202 LinearGradient::get_color(Context context, const Point &point)const
203 {
204         const Color color(color_func(point));
205
206         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
207                 return color;
208         else
209                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
210 }
211
212 bool
213 LinearGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
214 {
215         SuperCallback supercb(cb,0,9500,10000);
216
217         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
218         {
219                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
220         }
221         else
222         {
223                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
224                         return false;
225                 if(get_amount()==0)
226                         return true;
227         }
228
229
230         int x,y;
231
232         Surface::pen pen(surface->begin());
233         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
234         Point pos;
235         Point tl(renddesc.get_tl());
236         const int w(surface->get_w());
237         const int h(surface->get_h());
238
239         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
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_func(pos,calc_supersample(pos,pw,ph)));
244         }
245         else
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::blend(color_func(pos,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
250         }
251
252         // Mark our progress as finished
253         if(cb && !cb->amount_complete(10000,10000))
254                 return false;
255
256         return true;
257 }