Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / modules / mod_filter / radialblur.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file radialblur.cpp
3 **      \brief Template Header
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 **
21 ** === N O T E S ===========================================================
22 **
23 ** ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include "radialblur.h"
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/transform.h>
44 #include <ETL/misc>
45
46 #endif
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_LAYER_INIT(RadialBlur);
53 SYNFIG_LAYER_SET_NAME(RadialBlur,"radial_blur");
54 SYNFIG_LAYER_SET_LOCAL_NAME(RadialBlur,_("Radial Blur"));
55 SYNFIG_LAYER_SET_CATEGORY(RadialBlur,_("Blurs"));
56 SYNFIG_LAYER_SET_VERSION(RadialBlur,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(RadialBlur,"$Id$");
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 RadialBlur::RadialBlur():
66         origin  (0,0),
67         size    (0.2),
68         fade_out(false)
69 {
70 }
71
72 RadialBlur::~RadialBlur()
73 {
74 }
75
76 bool
77 RadialBlur::set_param(const String & param, const ValueBase &value)
78 {
79         IMPORT(origin);
80         IMPORT(size);
81         IMPORT(fade_out);
82
83         return Layer_Composite::set_param(param,value);
84 }
85
86 ValueBase
87 RadialBlur::get_param(const String &param)const
88 {
89         EXPORT(origin);
90         EXPORT(size);
91         EXPORT(fade_out);
92
93         EXPORT_NAME();
94         EXPORT_VERSION();
95
96         return Layer_Composite::get_param(param);
97 }
98
99 Layer::Vocab
100 RadialBlur::get_param_vocab()const
101 {
102         Layer::Vocab ret(Layer_Composite::get_param_vocab());
103
104         ret.push_back(ParamDesc("origin")
105                 .set_local_name(_("Origin"))
106                 .set_description(_("Point where you want the origin to be"))
107         );
108
109         ret.push_back(ParamDesc("size")
110                 .set_local_name(_("Size"))
111                 .set_description(_("Size of blur"))
112                 .set_origin("origin")
113         );
114
115         ret.push_back(ParamDesc("fade_out")
116                 .set_local_name(_("Fade Out"))
117         );
118
119         return ret;
120 }
121
122 Color
123 RadialBlur::get_color(Context context, const Point &p)const
124 {
125         //! \writeme
126         return context.get_color(p);
127 }
128
129 bool
130 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
131 {
132         if(cb && !cb->amount_complete(0,10000))
133                 return false;
134
135         Surface tmp_surface;
136
137         if(!context.accelerated_render(surface,quality,renddesc,cb))
138                 return false;
139
140         tmp_surface=*surface;
141
142         int x,y;
143
144         const Point tl(renddesc.get_tl());
145         Point pos;
146         const int w(surface->get_w());
147         const int h(surface->get_h());
148         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
149
150         Surface::alpha_pen apen(surface->begin());
151
152         apen.set_alpha(get_amount());
153         apen.set_blend_method(get_blend_method());
154
155         int steps(5);
156
157         if(quality>=9)steps=20;
158         else if(quality>=5)steps=30;
159         else if(quality>=4)steps=60;
160         else if(quality>=3)steps=100;
161         else steps=120;
162
163         Surface::value_prep_type cooker;
164
165         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
166                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
167                 {
168                         Point
169                                 begin(pos-tl),
170                                 end((pos-origin)*(1.0f-size)+origin-tl);
171                         begin[0]/=pw;begin[1]/=ph;
172                         end[0]/=pw;end[1]/=ph;
173
174                         Color pool(Color::alpha());
175                         int poolsize(0);
176
177                         int x0(round_to_int(begin[0])),
178                                 y0(round_to_int(begin[1])),
179                                 x1(round_to_int(end[0])),
180                                 y1(round_to_int(end[1]));
181
182                         int i;
183                         int steep = 1;
184                         int sx, sy;  /* step positive or negative (1 or -1) */
185                         int dx, dy;  /* delta (difference in X and Y between points) */
186                         int e;
187                         int w(tmp_surface.get_w()),h(tmp_surface.get_h());
188
189                         dx = abs(x1 - x0);
190                         sx = ((x1 - x0) > 0) ? 1 : -1;
191                         dy = abs(y1 - y0);
192                         sy = ((y1 - y0) > 0) ? 1 : -1;
193                         if (dy > dx)
194                         {
195                                 steep = 0;
196                                 swap(x0, y0);
197                                 swap(dx, dy);
198                                 swap(sx, sy);
199                                 swap(w,h);
200                         }
201                         e = (dy << 1) - dx;
202                         for (i = 0; i < dx; i++)
203                         {
204                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
205                                 {
206                                         if(fade_out)
207                                         {
208                                                 if (steep)
209                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
210                                                 else
211                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
212                                                 poolsize+=(i-dx);
213                                         }
214                                         else
215                                         {
216                                                 if (steep)
217                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
218                                                 else
219                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
220                                                 poolsize+=1;
221                                         }
222                                 }
223
224                                 while (e >= 0)
225                                 {
226                                         y0 += sy;
227                                         e -= (dx << 1);
228                                 }
229                                 x0 += sx;
230                                 e += (dy << 1);
231                         }
232                         if(poolsize)
233                         {
234                                 pool/=poolsize;
235                                 apen.put_value(cooker.uncook(pool));
236                         }
237 /*
238                         Point begin,end;
239                         begin=pos;
240                         end=(pos-origin)*(1.0f-size)+origin;
241
242                         Color pool(Color::alpha());
243                         float f,poolsize(0);
244                         int i;
245                         int steps(steps*size);
246                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
247                         {
248                                 Point loc((end-begin)*f+begin-tl);
249                                 loc[0]/=pw;loc[1]/=ph;
250
251                                 if(fade_out)
252                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
253                                 else
254                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
255                         }
256                         pool/=poolsize;
257                         apen.put_value(pool);
258 */
259                 }
260
261
262         if(cb && !cb->amount_complete(10000,10000)) return false;
263
264         return true;
265 }