Allow static option for the Radial Blur Layer
[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 }
73
74 RadialBlur::~RadialBlur()
75 {
76 }
77
78 bool
79 RadialBlur::set_param(const String & param, const ValueBase &value)
80 {
81         IMPORT(origin);
82         IMPORT(size);
83         IMPORT(fade_out);
84
85         return Layer_Composite::set_param(param,value);
86 }
87
88 ValueBase
89 RadialBlur::get_param(const String &param)const
90 {
91         EXPORT(origin);
92         EXPORT(size);
93         EXPORT(fade_out);
94
95         EXPORT_NAME();
96         EXPORT_VERSION();
97
98         return Layer_Composite::get_param(param);
99 }
100
101 bool
102 RadialBlur::set_param_static(const String &param, const bool x)
103 {
104
105         SET_STATIC(origin, x)
106         SET_STATIC(size, x)
107         SET_STATIC(fade_out, x)
108
109         return Layer_Composite::set_param_static(param, x);
110 }
111
112 bool
113 RadialBlur::get_param_static(const String &param)const
114 {
115
116         GET_STATIC(origin)
117         GET_STATIC(size)
118         GET_STATIC(fade_out)
119
120         return Layer_Composite::get_param_static(param);
121 }
122
123 Layer::Vocab
124 RadialBlur::get_param_vocab()const
125 {
126         Layer::Vocab ret(Layer_Composite::get_param_vocab());
127
128         ret.push_back(ParamDesc("origin")
129                 .set_local_name(_("Origin"))
130                 .set_description(_("Point where you want the origin to be"))
131         );
132
133         ret.push_back(ParamDesc("size")
134                 .set_local_name(_("Size"))
135                 .set_description(_("Size of blur"))
136                 .set_origin("origin")
137         );
138
139         ret.push_back(ParamDesc("fade_out")
140                 .set_local_name(_("Fade Out"))
141         );
142
143         return ret;
144 }
145
146 Color
147 RadialBlur::get_color(Context context, const Point &p)const
148 {
149         //! \writeme
150         return context.get_color(p);
151 }
152
153 bool
154 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
155 {
156         // don't do anything at quality 10
157         if (quality == 10)
158                 return context.accelerated_render(surface,quality,renddesc,cb);
159
160         if(cb && !cb->amount_complete(0,10000))
161                 return false;
162
163         Surface tmp_surface;
164         const Point tl(renddesc.get_tl()), br(renddesc.get_br());
165         const int w(renddesc.get_w()), h(renddesc.get_h());
166         const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
167
168         Rect rect(tl, br);
169         Point pos;
170
171         // find how far towards the origin of the blur we are going to
172         // wander for each of the 4 corners of our tile, expanding the
173         // render description for each of them if necessary
174         int x, y;
175         for(y=0,pos[1]=tl[1];y<h;y+=(h-1),pos[1]+=ph*(h-1))
176                 for(x=0,pos[0]=tl[0];x<w;x+=(w-1),pos[0]+=pw*(w-1))
177                         rect.expand((pos-origin)*(1.0f-size) + origin);
178
179         // round out to the nearest pixel
180         Point tmp_surface_tl = Point(tl[0] - pw*(int((tl[0]-rect.get_min()[0])/pw+1-1e-6)),
181                                                                  tl[1] - ph*(int((tl[1]-rect.get_max()[1])/ph+1-1e-6)));
182         Point tmp_surface_br = Point(br[0] + pw*(int((rect.get_max()[0]-br[0])/pw+2-1e-6)),
183                                                                  br[1] + ph*(int((rect.get_min()[1]-br[1])/ph+2-1e-6)));
184
185         // round to nearest integer width and height (should be very
186         // nearly whole numbers already, but don't want to round 5.99999
187         // down to 5)
188         int tmp_surface_width = int((tmp_surface_br[0]-tmp_surface_tl[0])/pw + 0.5);
189         int tmp_surface_height = int((tmp_surface_br[1]-tmp_surface_tl[1])/ph + 0.5);
190
191         RendDesc desc(renddesc);
192         desc.clear_flags();
193         desc.set_wh(tmp_surface_width,tmp_surface_height);
194         desc.set_tl(tmp_surface_tl);
195         desc.set_br(tmp_surface_br);
196
197         // render the layers beneath us
198         if(!context.accelerated_render(&tmp_surface,quality,desc,cb))
199                 return false;
200
201         // copy the part of the layers beneath us that corresponds to this tile
202         surface->set_wh(w, h);
203         Surface::pen pen(surface->get_pen(0, 0));
204         tmp_surface.blit_to(pen,
205                                                 int((tl[0] - tmp_surface_tl[0])/pw + 0.5),
206                                                 int((tl[1] - tmp_surface_tl[1])/ph + 0.5),
207                                                 w, h);
208
209         Surface::alpha_pen apen(surface->begin());
210
211         apen.set_alpha(get_amount());
212         apen.set_blend_method(get_blend_method());
213
214 /*
215         int steps(5);
216
217         if(quality>=9)steps=20;
218         else if(quality>=5)steps=30;
219         else if(quality>=4)steps=60;
220         else if(quality>=3)steps=100;
221         else steps=120;
222 */
223
224         Surface::value_prep_type cooker;
225
226         // loop through the pixels
227         for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
228                 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
229                 {
230                         Point
231                                 begin(pos-tmp_surface_tl),
232                                 end((pos-origin)*(1.0f-size) + origin-tmp_surface_tl);
233                         begin[0]/=pw;begin[1]/=ph;
234                         end[0]/=pw;end[1]/=ph;
235
236                         Color pool(Color::alpha());
237                         int poolsize(0);
238
239                         int x0(round_to_int(begin[0])),
240                                 y0(round_to_int(begin[1])),
241                                 x1(round_to_int(end[0])),
242                                 y1(round_to_int(end[1]));
243
244                         int i;
245                         int steep = 1;
246                         int sx, sy;  /* step positive or negative (1 or -1) */
247                         int dx, dy;  /* delta (difference in X and Y between points) */
248                         int e;
249                         int w(tmp_surface_width), h(tmp_surface_height);
250
251                         dx = abs(x1 - x0);
252                         sx = ((x1 - x0) > 0) ? 1 : -1;
253                         dy = abs(y1 - y0);
254                         sy = ((y1 - y0) > 0) ? 1 : -1;
255                         if (dy > dx)
256                         {
257                                 steep = 0;
258                                 swap(x0, y0);
259                                 swap(dx, dy);
260                                 swap(sx, sy);
261                                 swap(w,h);
262                         }
263                         e = (dy << 1) - dx;
264                         for (i = 0; i < dx; i++)
265                         {
266                                 if(y0>=0 && x0>=0 && y0<h && x0<w)
267                                 {
268                                         if(fade_out)
269                                         {
270                                                 if (steep)
271                                                         pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
272                                                 else
273                                                         pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
274                                                 poolsize+=(i-dx);
275                                         }
276                                         else
277                                         {
278                                                 if (steep)
279                                                         pool+=cooker.cook(tmp_surface[y0][x0]);
280                                                 else
281                                                         pool+=cooker.cook(tmp_surface[x0][y0]);
282                                                 poolsize+=1;
283                                         }
284                                 } else
285                                         printf("%s:%d unexpected %d >= %d or %d >= %d?\n", __FILE__, __LINE__, x0, w, y0, h);
286
287                                 while (e >= 0)
288                                 {
289                                         y0 += sy;
290                                         e -= (dx << 1);
291                                 }
292                                 x0 += sx;
293                                 e += (dy << 1);
294                         }
295                         if(poolsize)
296                         {
297                                 pool/=poolsize;
298                                 apen.put_value(cooker.uncook(pool));
299                         }
300 /*
301                         Point begin,end;
302                         begin=pos;
303                         end=(pos-origin)*(1.0f-size)+origin;
304
305                         Color pool(Color::alpha());
306                         float f,poolsize(0);
307                         int i;
308                         int steps(steps*size);
309                         for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
310                         {
311                                 Point loc((end-begin)*f+begin-tl);
312                                 loc[0]/=pw;loc[1]/=ph;
313
314                                 if(fade_out)
315                                         pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
316                                 else
317                                         pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
318                         }
319                         pool/=poolsize;
320                         apen.put_value(pool);
321 */
322                 }
323
324
325         if(cb && !cb->amount_complete(10000,10000)) return false;
326
327 // #define DRAW_TILE_OUTLINES
328 #ifdef DRAW_TILE_OUTLINES
329         // draw red lines to show tiles
330         {
331                 int x, y;
332                 if (w != 0 && h != 0) {
333                         Surface::alpha_pen apen(surface->begin());
334                         apen.set_alpha(get_amount());
335                         apen.set_blend_method(get_blend_method());
336                         apen.set_value(Color(1, 0, 0, .1));
337                         for (x = 0; x < w; x++) { apen.put_value(); apen.inc_x(); } apen.dec_x(w);
338                         for (y = 0; y < h; y++) { apen.put_value(); apen.inc_y(); } apen.dec_y(h);
339                 }
340         }
341 #endif // DRAW_TILE_OUTLINES
342
343         return true;
344 }