Use map of static values indexed by parameter name instead of the macros and the...
[synfig.git] / synfig-core / src / modules / mod_filter / radialblur.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file radialblur.cpp
3 **      \brief Implementation of the "Radial Blur" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 **
22 ** === N O T E S ===========================================================
23 **
24 ** ========================================================================= */
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "radialblur.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44 #include <synfig/transform.h>
45 #include <ETL/misc>
46
47 #endif
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(RadialBlur);
54 SYNFIG_LAYER_SET_NAME(RadialBlur,"radial_blur");
55 SYNFIG_LAYER_SET_LOCAL_NAME(RadialBlur,N_("Radial Blur"));
56 SYNFIG_LAYER_SET_CATEGORY(RadialBlur,N_("Blurs"));
57 SYNFIG_LAYER_SET_VERSION(RadialBlur,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(RadialBlur,"$Id$");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65
66 RadialBlur::RadialBlur():
67         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68         origin  (0,0),
69         size    (0.2),
70         fade_out(false)
71 {
72         Layer::Vocab voc(get_param_vocab());
73         Layer::fill_static(voc);
74         set_param_static("blend_method",true);
75 }
76
77 RadialBlur::~RadialBlur()
78 {
79 }
80
81 bool
82 RadialBlur::set_param(const String & param, const ValueBase &value)
83 {
84         IMPORT(origin);
85         IMPORT(size);
86         IMPORT(fade_out);
87
88         return Layer_Composite::set_param(param,value);
89 }
90
91 ValueBase
92 RadialBlur::get_param(const String &param)const
93 {
94         EXPORT(origin);
95         EXPORT(size);
96         EXPORT(fade_out);
97
98         EXPORT_NAME();
99         EXPORT_VERSION();
100
101         return Layer_Composite::get_param(param);
102 }
103
104 bool
105 RadialBlur::set_param_static(const String &param, const bool x)
106 {
107         return Layer_Composite::set_param_static(param, x);
108 }
109
110 bool
111 RadialBlur::get_param_static(const String &param)const
112 {
113         return Layer_Composite::get_param_static(param);
114 }
115
116 Layer::Vocab
117 RadialBlur::get_param_vocab()const
118 {
119         Layer::Vocab ret(Layer_Composite::get_param_vocab());
120
121         ret.push_back(ParamDesc("origin")
122                 .set_local_name(_("Origin"))
123                 .set_description(_("Point where you want the origin to be"))
124         );
125
126         ret.push_back(ParamDesc("size")
127                 .set_local_name(_("Size"))
128                 .set_description(_("Size of blur"))
129                 .set_origin("origin")
130         );
131
132         ret.push_back(ParamDesc("fade_out")
133                 .set_local_name(_("Fade Out"))
134         );
135
136         return ret;
137 }
138
139 Color
140 RadialBlur::get_color(Context context, const Point &p)const
141 {
142         //! \writeme
143         return context.get_color(p);
144 }
145
146 bool
147 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
148 {
149         // don't do anything at quality 10
150         if (quality == 10)
151                 return context.accelerated_render(surface,quality,renddesc,cb);
152
153         if(cb && !cb->amount_complete(0,10000))
154                 return false;
155
156         Surface tmp_surface;
157         const Point tl(renddesc.get_tl()), br(renddesc.get_br());
158         const int w(renddesc.get_w()), h(renddesc.get_h());
159         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
160
161         Rect rect(tl, br);
162         Point pos;
163
164         // find how far towards the origin of the blur we are going to
165         // wander for each of the 4 corners of our tile, expanding the
166         // render description for each of them if necessary
167         int x, y;
168         for(y=0,pos[1]=tl[1];y<h;y+=(h-1),pos[1]+=ph*(h-1))
169                 for(x=0,pos[0]=tl[0];x<w;x+=(w-1),pos[0]+=pw*(w-1))
170                         rect.expand((pos-origin)*(1.0f-size) + origin);
171
172         // round out to the nearest pixel
173         Point tmp_surface_tl = Point(tl[0] - pw*(int((tl[0]-rect.get_min()[0])/pw+1-1e-6)),
174                                                                  tl[1] - ph*(int((tl[1]-rect.get_max()[1])/ph+1-1e-6)));
175         Point tmp_surface_br = Point(br[0] + pw*(int((rect.get_max()[0]-br[0])/pw+2-1e-6)),
176                                                                  br[1] + ph*(int((rect.get_min()[1]-br[1])/ph+2-1e-6)));
177
178         // round to nearest integer width and height (should be very
179         // nearly whole numbers already, but don't want to round 5.99999
180         // down to 5)
181         int tmp_surface_width = int((tmp_surface_br[0]-tmp_surface_tl[0])/pw + 0.5);
182         int tmp_surface_height = int((tmp_surface_br[1]-tmp_surface_tl[1])/ph + 0.5);
183
184         RendDesc desc(renddesc);
185         desc.clear_flags();
186         desc.set_wh(tmp_surface_width,tmp_surface_height);
187         desc.set_tl(tmp_surface_tl);
188         desc.set_br(tmp_surface_br);
189
190         // render the layers beneath us
191         if(!context.accelerated_render(&tmp_surface,quality,desc,cb))
192                 return false;
193
194         // copy the part of the layers beneath us that corresponds to this tile
195         surface->set_wh(w, h);
196         Surface::pen pen(surface->get_pen(0, 0));
197         tmp_surface.blit_to(pen,
198                                                 int((tl[0] - tmp_surface_tl[0])/pw + 0.5),
199                                                 int((tl[1] - tmp_surface_tl[1])/ph + 0.5),
200                                                 w, h);
201
202         Surface::alpha_pen apen(surface->begin());
203
204         apen.set_alpha(get_amount());
205         apen.set_blend_method(get_blend_method());
206
207 /*
208         int steps(5);
209
210         if(quality>=9)steps=20;
211         else if(quality>=5)steps=30;
212         else if(quality>=4)steps=60;
213         else if(quality>=3)steps=100;
214         else steps=120;
215 */
216
217         Surface::value_prep_type cooker;
218
219         // loop through the pixels
220         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
221                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
222                 {
223                         Point
224                                 begin(pos-tmp_surface_tl),
225                                 end((pos-origin)*(1.0f-size) + origin-tmp_surface_tl);
226                         begin[0]/=pw;begin[1]/=ph;
227                         end[0]/=pw;end[1]/=ph;
228
229                         Color pool(Color::alpha());
230                         int poolsize(0);
231
232                         int x0(round_to_int(begin[0])),
233                                 y0(round_to_int(begin[1])),
234                                 x1(round_to_int(end[0])),
235                                 y1(round_to_int(end[1]));
236
237                         int i;
238                         int steep = 1;
239                         int sx, sy;  /* step positive or negative (1 or -1) */
240                         int dx, dy;  /* delta (difference in X and Y between points) */
241                         int e;
242                         int w(tmp_surface_width), h(tmp_surface_height);
243
244                         dx = abs(x1 - x0);
245                         sx = ((x1 - x0) > 0) ? 1 : -1;
246                         dy = abs(y1 - y0);
247                         sy = ((y1 - y0) > 0) ? 1 : -1;
248                         if (dy > dx)
249                         {
250                                 steep = 0;
251                                 swap(x0, y0);
252                                 swap(dx, dy);
253                                 swap(sx, sy);
254                                 swap(w,h);
255                         }
256                         e = (dy << 1) - dx;
257                         for (i = 0; i < dx; i++)
258                         {
259                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
260                                 {
261                                         if(fade_out)
262                                         {
263                                                 if (steep)
264                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
265                                                 else
266                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
267                                                 poolsize+=(i-dx);
268                                         }
269                                         else
270                                         {
271                                                 if (steep)
272                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
273                                                 else
274                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
275                                                 poolsize+=1;
276                                         }
277                                 } else
278                                         printf("%s:%d unexpected %d >= %d or %d >= %d?\n", __FILE__, __LINE__, x0, w, y0, h);
279
280                                 while (e >= 0)
281                                 {
282                                         y0 += sy;
283                                         e -= (dx << 1);
284                                 }
285                                 x0 += sx;
286                                 e += (dy << 1);
287                         }
288                         if(poolsize)
289                         {
290                                 pool/=poolsize;
291                                 apen.put_value(cooker.uncook(pool));
292                         }
293 /*
294                         Point begin,end;
295                         begin=pos;
296                         end=(pos-origin)*(1.0f-size)+origin;
297
298                         Color pool(Color::alpha());
299                         float f,poolsize(0);
300                         int i;
301                         int steps(steps*size);
302                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
303                         {
304                                 Point loc((end-begin)*f+begin-tl);
305                                 loc[0]/=pw;loc[1]/=ph;
306
307                                 if(fade_out)
308                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
309                                 else
310                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
311                         }
312                         pool/=poolsize;
313                         apen.put_value(pool);
314 */
315                 }
316
317
318         if(cb && !cb->amount_complete(10000,10000)) return false;
319
320 // #define DRAW_TILE_OUTLINES
321 #ifdef DRAW_TILE_OUTLINES
322         // draw red lines to show tiles
323         {
324                 int x, y;
325                 if (w != 0 && h != 0) {
326                         Surface::alpha_pen apen(surface->begin());
327                         apen.set_alpha(get_amount());
328                         apen.set_blend_method(get_blend_method());
329                         apen.set_value(Color(1, 0, 0, .1));
330                         for (x = 0; x < w; x++) { apen.put_value(); apen.inc_x(); } apen.dec_x(w);
331                         for (y = 0; y < h; y++) { apen.put_value(); apen.inc_y(); } apen.dec_y(h);
332                 }
333         }
334 #endif // DRAW_TILE_OUTLINES
335
336         return true;
337 }