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