Initial Stable Commit
[synfig.git] / synfig-core / trunk / src / modules / mod_filter / radialblur.cpp
1 /*! ========================================================================
2 ** Sinfg
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 Robert B. Quattlebaum Jr.
7 **
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
11 **
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
16 **
17 ** === N O T E S ===========================================================
18 **
19 ** ========================================================================= */
20
21 /* === H E A D E R S ======================================================= */
22
23 #ifdef USING_PCH
24 #       include "pch.h"
25 #else
26 #ifdef HAVE_CONFIG_H
27 #       include <config.h>
28 #endif
29
30 #include "radialblur.h"
31 #include <sinfg/string.h>
32 #include <sinfg/time.h>
33 #include <sinfg/context.h>
34 #include <sinfg/paramdesc.h>
35 #include <sinfg/renddesc.h>
36 #include <sinfg/surface.h>
37 #include <sinfg/value.h>
38 #include <sinfg/valuenode.h>
39 #include <sinfg/transform.h>
40 #include <ETL/misc>
41
42 #endif
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 SINFG_LAYER_INIT(RadialBlur);
49 SINFG_LAYER_SET_NAME(RadialBlur,"radial_blur");
50 SINFG_LAYER_SET_LOCAL_NAME(RadialBlur,_("Radial Blur"));
51 SINFG_LAYER_SET_CATEGORY(RadialBlur,_("Blurs"));
52 SINFG_LAYER_SET_VERSION(RadialBlur,"0.1");
53 SINFG_LAYER_SET_CVS_ID(RadialBlur,"$Id: radialblur.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 /* === E N T R Y P O I N T ================================================= */
60
61 RadialBlur::RadialBlur():
62         origin  (0,0),
63         size    (0.2),
64         fade_out(false)
65 {
66 }
67
68 RadialBlur::~RadialBlur()
69 {
70 }
71
72 bool
73 RadialBlur::set_param(const String & param, const ValueBase &value)
74 {
75         IMPORT(origin);
76         IMPORT(size);
77         IMPORT(fade_out);
78         
79         return Layer_Composite::set_param(param,value); 
80 }
81
82 ValueBase
83 RadialBlur::get_param(const String &param)const
84 {
85         EXPORT(origin);
86         EXPORT(size);
87         EXPORT(fade_out);
88         
89         EXPORT_NAME();
90         EXPORT_VERSION();
91                 
92         return Layer_Composite::get_param(param);       
93 }
94
95 Layer::Vocab
96 RadialBlur::get_param_vocab()const
97 {
98         Layer::Vocab ret(Layer_Composite::get_param_vocab());
99         
100         ret.push_back(ParamDesc("origin")
101                 .set_local_name(_("Origin"))
102                 .set_description(_("Point where you want the origin to be"))
103         );
104
105         ret.push_back(ParamDesc("size")
106                 .set_local_name(_("Size"))
107                 .set_description(_("Size of blur"))
108                 .set_origin("origin")
109         );
110
111         ret.push_back(ParamDesc("fade_out")
112                 .set_local_name(_("Fade Out"))
113         );
114         
115         return ret;
116 }
117
118 Color
119 RadialBlur::get_color(Context context, const Point &p)const
120 {
121         //! \writeme
122         return context.get_color(p);
123 }
124
125 bool
126 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
127 {
128         if(cb && !cb->amount_complete(0,10000))
129                 return false;
130         
131         Surface tmp_surface;
132         
133         if(!context.accelerated_render(surface,quality,renddesc,cb))
134                 return false;
135
136         tmp_surface=*surface;
137         
138         int x,y;
139
140         const Point tl(renddesc.get_tl());
141         Point pos;
142         const int w(surface->get_w());
143         const int h(surface->get_h());
144         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
145
146         Surface::alpha_pen apen(surface->begin());
147
148         apen.set_alpha(get_amount());
149         apen.set_blend_method(get_blend_method());
150         
151         int steps(5);
152         
153         if(quality>=9)steps=20;
154         else if(quality>=5)steps=30;
155         else if(quality>=4)steps=60;
156         else if(quality>=3)steps=100;
157         else steps=120;
158         
159         Surface::value_prep_type cooker;
160         
161         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
162                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
163                 {
164                         Point
165                                 begin(pos-tl),
166                                 end((pos-origin)*(1.0f-size)+origin-tl);
167                         begin[0]/=pw;begin[1]/=ph;
168                         end[0]/=pw;end[1]/=ph;
169
170                         Color pool(Color::alpha());
171                         int poolsize(0);
172                         
173                         int x0(round_to_int(begin[0])),
174                                 y0(round_to_int(begin[1])),
175                                 x1(round_to_int(end[0])),
176                                 y1(round_to_int(end[1]));
177                         
178                         int i;
179                         int steep = 1;
180                         int sx, sy;  /* step positive or negative (1 or -1) */
181                         int dx, dy;  /* delta (difference in X and Y between points) */
182                         int e;
183                         int w(tmp_surface.get_w()),h(tmp_surface.get_h());
184                         
185                         dx = abs(x1 - x0);
186                         sx = ((x1 - x0) > 0) ? 1 : -1;
187                         dy = abs(y1 - y0);
188                         sy = ((y1 - y0) > 0) ? 1 : -1;
189                         if (dy > dx)
190                         {
191                                 steep = 0;
192                                 swap(x0, y0);
193                                 swap(dx, dy);
194                                 swap(sx, sy);
195                                 swap(w,h);
196                         }
197                         e = (dy << 1) - dx;
198                         for (i = 0; i < dx; i++)
199                         {
200                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
201                                 {
202                                         if(fade_out)
203                                         {
204                                                 if (steep)
205                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
206                                                 else
207                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
208                                                 poolsize+=(i-dx);
209                                         }
210                                         else
211                                         {
212                                                 if (steep)
213                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
214                                                 else
215                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
216                                                 poolsize+=1;
217                                         }
218                                 }
219                                 
220                                 while (e >= 0)
221                                 {
222                                         y0 += sy;
223                                         e -= (dx << 1);
224                                 }
225                                 x0 += sx;
226                                 e += (dy << 1);
227                         }
228                         if(poolsize)
229                         {
230                                 pool/=poolsize;
231                                 apen.put_value(cooker.uncook(pool));
232                         }
233 /*
234                         Point begin,end;
235                         begin=pos;
236                         end=(pos-origin)*(1.0f-size)+origin;
237                         
238                         Color pool(Color::alpha());
239                         float f,poolsize(0);
240                         int i;
241                         int steps(steps*size);
242                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
243                         {
244                                 Point loc((end-begin)*f+begin-tl);
245                                 loc[0]/=pw;loc[1]/=ph;
246                                 
247                                 if(fade_out)
248                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
249                                 else
250                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
251                         }
252                         pool/=poolsize;
253                         apen.put_value(pool);
254 */
255                 }
256
257
258         if(cb && !cb->amount_complete(10000,10000)) return false;
259
260         return true;
261 }