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_filter / radialblur.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** Template File
4 ** $Id: radialblur.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 "radialblur.h"
32 #include <synfig/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
40 #include <synfig/transform.h>
41 #include <ETL/misc>
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(RadialBlur);
50 SYNFIG_LAYER_SET_NAME(RadialBlur,"radial_blur");
51 SYNFIG_LAYER_SET_LOCAL_NAME(RadialBlur,_("Radial Blur"));
52 SYNFIG_LAYER_SET_CATEGORY(RadialBlur,_("Blurs"));
53 SYNFIG_LAYER_SET_VERSION(RadialBlur,"0.1");
54 SYNFIG_LAYER_SET_CVS_ID(RadialBlur,"$Id: radialblur.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
55
56 /* === P R O C E D U R E S ================================================= */
57
58 /* === M E T H O D S ======================================================= */
59
60 /* === E N T R Y P O I N T ================================================= */
61
62 RadialBlur::RadialBlur():
63         origin  (0,0),
64         size    (0.2),
65         fade_out(false)
66 {
67 }
68
69 RadialBlur::~RadialBlur()
70 {
71 }
72
73 bool
74 RadialBlur::set_param(const String & param, const ValueBase &value)
75 {
76         IMPORT(origin);
77         IMPORT(size);
78         IMPORT(fade_out);
79         
80         return Layer_Composite::set_param(param,value); 
81 }
82
83 ValueBase
84 RadialBlur::get_param(const String &param)const
85 {
86         EXPORT(origin);
87         EXPORT(size);
88         EXPORT(fade_out);
89         
90         EXPORT_NAME();
91         EXPORT_VERSION();
92                 
93         return Layer_Composite::get_param(param);       
94 }
95
96 Layer::Vocab
97 RadialBlur::get_param_vocab()const
98 {
99         Layer::Vocab ret(Layer_Composite::get_param_vocab());
100         
101         ret.push_back(ParamDesc("origin")
102                 .set_local_name(_("Origin"))
103                 .set_description(_("Point where you want the origin to be"))
104         );
105
106         ret.push_back(ParamDesc("size")
107                 .set_local_name(_("Size"))
108                 .set_description(_("Size of blur"))
109                 .set_origin("origin")
110         );
111
112         ret.push_back(ParamDesc("fade_out")
113                 .set_local_name(_("Fade Out"))
114         );
115         
116         return ret;
117 }
118
119 Color
120 RadialBlur::get_color(Context context, const Point &p)const
121 {
122         //! \writeme
123         return context.get_color(p);
124 }
125
126 bool
127 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
128 {
129         if(cb && !cb->amount_complete(0,10000))
130                 return false;
131         
132         Surface tmp_surface;
133         
134         if(!context.accelerated_render(surface,quality,renddesc,cb))
135                 return false;
136
137         tmp_surface=*surface;
138         
139         int x,y;
140
141         const Point tl(renddesc.get_tl());
142         Point pos;
143         const int w(surface->get_w());
144         const int h(surface->get_h());
145         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
146
147         Surface::alpha_pen apen(surface->begin());
148
149         apen.set_alpha(get_amount());
150         apen.set_blend_method(get_blend_method());
151         
152         int steps(5);
153         
154         if(quality>=9)steps=20;
155         else if(quality>=5)steps=30;
156         else if(quality>=4)steps=60;
157         else if(quality>=3)steps=100;
158         else steps=120;
159         
160         Surface::value_prep_type cooker;
161         
162         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
163                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
164                 {
165                         Point
166                                 begin(pos-tl),
167                                 end((pos-origin)*(1.0f-size)+origin-tl);
168                         begin[0]/=pw;begin[1]/=ph;
169                         end[0]/=pw;end[1]/=ph;
170
171                         Color pool(Color::alpha());
172                         int poolsize(0);
173                         
174                         int x0(round_to_int(begin[0])),
175                                 y0(round_to_int(begin[1])),
176                                 x1(round_to_int(end[0])),
177                                 y1(round_to_int(end[1]));
178                         
179                         int i;
180                         int steep = 1;
181                         int sx, sy;  /* step positive or negative (1 or -1) */
182                         int dx, dy;  /* delta (difference in X and Y between points) */
183                         int e;
184                         int w(tmp_surface.get_w()),h(tmp_surface.get_h());
185                         
186                         dx = abs(x1 - x0);
187                         sx = ((x1 - x0) > 0) ? 1 : -1;
188                         dy = abs(y1 - y0);
189                         sy = ((y1 - y0) > 0) ? 1 : -1;
190                         if (dy > dx)
191                         {
192                                 steep = 0;
193                                 swap(x0, y0);
194                                 swap(dx, dy);
195                                 swap(sx, sy);
196                                 swap(w,h);
197                         }
198                         e = (dy << 1) - dx;
199                         for (i = 0; i < dx; i++)
200                         {
201                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
202                                 {
203                                         if(fade_out)
204                                         {
205                                                 if (steep)
206                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
207                                                 else
208                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
209                                                 poolsize+=(i-dx);
210                                         }
211                                         else
212                                         {
213                                                 if (steep)
214                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
215                                                 else
216                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
217                                                 poolsize+=1;
218                                         }
219                                 }
220                                 
221                                 while (e >= 0)
222                                 {
223                                         y0 += sy;
224                                         e -= (dx << 1);
225                                 }
226                                 x0 += sx;
227                                 e += (dy << 1);
228                         }
229                         if(poolsize)
230                         {
231                                 pool/=poolsize;
232                                 apen.put_value(cooker.uncook(pool));
233                         }
234 /*
235                         Point begin,end;
236                         begin=pos;
237                         end=(pos-origin)*(1.0f-size)+origin;
238                         
239                         Color pool(Color::alpha());
240                         float f,poolsize(0);
241                         int i;
242                         int steps(steps*size);
243                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
244                         {
245                                 Point loc((end-begin)*f+begin-tl);
246                                 loc[0]/=pw;loc[1]/=ph;
247                                 
248                                 if(fade_out)
249                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
250                                 else
251                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
252                         }
253                         pool/=poolsize;
254                         apen.put_value(pool);
255 */
256                 }
257
258
259         if(cb && !cb->amount_complete(10000,10000)) return false;
260
261         return true;
262 }