0d0126d1568d9a92a827991bfd45693a67608043
[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 **      $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         const Point tl(renddesc.get_tl()), br(renddesc.get_br());
137         const int w(renddesc.get_w()), h(renddesc.get_h());
138         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
139
140         Rect rect(tl, br);
141         Point pos;
142
143         // find how far towards the origin of the blur we are going to
144         // wander for each of the 4 corners of our tile, expanding the
145         // render description for each of them if necessary
146         int x, y;
147         for(y=0,pos[1]=tl[1];y<h;y+=(h-1),pos[1]+=ph*(h-1))
148                 for(x=0,pos[0]=tl[0];x<w;x+=(w-1),pos[0]+=pw*(w-1))
149                         rect.expand((pos-origin)*(1.0f-size) + origin);
150
151         // round out to the nearest pixel
152         Point tmp_surface_tl = Point(tl[0] - pw*(int((tl[0]-rect.get_min()[0])/pw+1-1e-6)),
153                                                                  tl[1] - ph*(int((tl[1]-rect.get_max()[1])/ph+1-1e-6)));
154         Point tmp_surface_br = Point(br[0] + pw*(int((rect.get_max()[0]-br[0])/pw+2-1e-6)),
155                                                                  br[1] + ph*(int((rect.get_min()[1]-br[1])/ph+2-1e-6)));
156
157         // round to nearest integer width and height (should be very
158         // nearly whole numbers already, but dont want to round 5.99999
159         // down to 5)
160         int tmp_surface_width = int((tmp_surface_br[0]-tmp_surface_tl[0])/pw + 0.5);
161         int tmp_surface_height = int((tmp_surface_br[1]-tmp_surface_tl[1])/ph + 0.5);
162
163         RendDesc desc(renddesc);
164         desc.clear_flags();
165         desc.set_wh(tmp_surface_width,tmp_surface_height);
166         desc.set_tl(tmp_surface_tl);
167         desc.set_br(tmp_surface_br);
168
169         // render the layers beneath us
170         if(!context.accelerated_render(&tmp_surface,quality,desc,cb))
171                 return false;
172
173         // copy the part of the layers beneath us that corresponds to this tile
174         surface->set_wh(w, h);
175         Surface::pen pen(surface->get_pen(0, 0));
176         tmp_surface.blit_to(pen,
177                                                 int((tl[0] - tmp_surface_tl[0])/pw + 0.5),
178                                                 int((tl[1] - tmp_surface_tl[1])/ph + 0.5),
179                                                 w, h);
180
181         Surface::alpha_pen apen(surface->begin());
182
183         apen.set_alpha(get_amount());
184         apen.set_blend_method(get_blend_method());
185
186 /*
187         int steps(5);
188
189         if(quality>=9)steps=20;
190         else if(quality>=5)steps=30;
191         else if(quality>=4)steps=60;
192         else if(quality>=3)steps=100;
193         else steps=120;
194 */
195
196         Surface::value_prep_type cooker;
197
198         // loop through the pixels
199         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
200                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
201                 {
202                         Point
203                                 begin(pos-tmp_surface_tl),
204                                 end((pos-origin)*(1.0f-size) + origin-tmp_surface_tl);
205                         begin[0]/=pw;begin[1]/=ph;
206                         end[0]/=pw;end[1]/=ph;
207
208                         Color pool(Color::alpha());
209                         int poolsize(0);
210
211                         int x0(round_to_int(begin[0])),
212                                 y0(round_to_int(begin[1])),
213                                 x1(round_to_int(end[0])),
214                                 y1(round_to_int(end[1]));
215
216                         int i;
217                         int steep = 1;
218                         int sx, sy;  /* step positive or negative (1 or -1) */
219                         int dx, dy;  /* delta (difference in X and Y between points) */
220                         int e;
221                         int w(tmp_surface_width), h(tmp_surface_height);
222
223                         dx = abs(x1 - x0);
224                         sx = ((x1 - x0) > 0) ? 1 : -1;
225                         dy = abs(y1 - y0);
226                         sy = ((y1 - y0) > 0) ? 1 : -1;
227                         if (dy > dx)
228                         {
229                                 steep = 0;
230                                 swap(x0, y0);
231                                 swap(dx, dy);
232                                 swap(sx, sy);
233                                 swap(w,h);
234                         }
235                         e = (dy << 1) - dx;
236                         for (i = 0; i < dx; i++)
237                         {
238                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
239                                 {
240                                         if(fade_out)
241                                         {
242                                                 if (steep)
243                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
244                                                 else
245                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
246                                                 poolsize+=(i-dx);
247                                         }
248                                         else
249                                         {
250                                                 if (steep)
251                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
252                                                 else
253                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
254                                                 poolsize+=1;
255                                         }
256                                 } else
257                                         printf("%s:%d unexpected %d >= %d or %d >= %d?\n", __FILE__, __LINE__, x0, w, y0, h);
258
259                                 while (e >= 0)
260                                 {
261                                         y0 += sy;
262                                         e -= (dx << 1);
263                                 }
264                                 x0 += sx;
265                                 e += (dy << 1);
266                         }
267                         if(poolsize)
268                         {
269                                 pool/=poolsize;
270                                 apen.put_value(cooker.uncook(pool));
271                         }
272 /*
273                         Point begin,end;
274                         begin=pos;
275                         end=(pos-origin)*(1.0f-size)+origin;
276
277                         Color pool(Color::alpha());
278                         float f,poolsize(0);
279                         int i;
280                         int steps(steps*size);
281                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
282                         {
283                                 Point loc((end-begin)*f+begin-tl);
284                                 loc[0]/=pw;loc[1]/=ph;
285
286                                 if(fade_out)
287                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
288                                 else
289                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
290                         }
291                         pool/=poolsize;
292                         apen.put_value(pool);
293 */
294                 }
295
296
297         if(cb && !cb->amount_complete(10000,10000)) return false;
298
299 // #define DRAW_TILE_OUTLINES
300 #ifdef DRAW_TILE_OUTLINES
301         // draw red lines to show tiles
302         {
303                 int x, y;
304                 if (w != 0 && h != 0) {
305                         Surface::alpha_pen apen(surface->begin());
306                         apen.set_alpha(get_amount());
307                         apen.set_blend_method(get_blend_method());
308                         apen.set_value(Color(1, 0, 0, .1));
309                         for (x = 0; x < w; x++) { apen.put_value(); apen.inc_x(); } apen.dec_x(w);
310                         for (y = 0; y < h; y++) { apen.put_value(); apen.inc_y(); } apen.dec_y(h);
311                 }
312         }
313 #endif // DRAW_TILE_OUTLINES
314
315         return true;
316 }