1 /* === S Y N F I G ========================================================= */
2 /*! \file radialblur.cpp
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
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>
49 /* === M A C R O S ========================================================= */
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(RadialBlur);
54 SYNFIG_LAYER_SET_NAME(RadialBlur,"radial_blur");
55 SYNFIG_LAYER_SET_LOCAL_NAME(RadialBlur,_("Radial Blur"));
56 SYNFIG_LAYER_SET_CATEGORY(RadialBlur,_("Blurs"));
57 SYNFIG_LAYER_SET_VERSION(RadialBlur,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(RadialBlur,"$Id$");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 /* === E N T R Y P O I N T ================================================= */
66 RadialBlur::RadialBlur():
73 RadialBlur::~RadialBlur()
78 RadialBlur::set_param(const String & param, const ValueBase &value)
84 return Layer_Composite::set_param(param,value);
88 RadialBlur::get_param(const String ¶m)const
97 return Layer_Composite::get_param(param);
101 RadialBlur::get_param_vocab()const
103 Layer::Vocab ret(Layer_Composite::get_param_vocab());
105 ret.push_back(ParamDesc("origin")
106 .set_local_name(_("Origin"))
107 .set_description(_("Point where you want the origin to be"))
110 ret.push_back(ParamDesc("size")
111 .set_local_name(_("Size"))
112 .set_description(_("Size of blur"))
113 .set_origin("origin")
116 ret.push_back(ParamDesc("fade_out")
117 .set_local_name(_("Fade Out"))
124 RadialBlur::get_color(Context context, const Point &p)const
127 return context.get_color(p);
131 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
133 if(cb && !cb->amount_complete(0,10000))
137 const Point tl(renddesc.get_tl()), br(renddesc.get_br());
138 const int w(renddesc.get_w()), h(renddesc.get_h());
139 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
144 // find how far towards the origin of the blur we are going to
145 // wander for each of the 4 corners of our tile, expanding the
146 // render description for each of them if necessary
148 for(y=0,pos[1]=tl[1];y<h;y+=(h-1),pos[1]+=ph*(h-1))
149 for(x=0,pos[0]=tl[0];x<w;x+=(w-1),pos[0]+=pw*(w-1))
150 rect.expand((pos-origin)*(1.0f-size) + origin);
152 // round out to the nearest pixel
153 Point tmp_surface_tl = Point(tl[0] - pw*(int((tl[0]-rect.get_min()[0])/pw+1-1e-6)),
154 tl[1] - ph*(int((tl[1]-rect.get_max()[1])/ph+1-1e-6)));
155 Point tmp_surface_br = Point(br[0] + pw*(int((rect.get_max()[0]-br[0])/pw+2-1e-6)),
156 br[1] + ph*(int((rect.get_min()[1]-br[1])/ph+2-1e-6)));
158 // round to nearest integer width and height (should be very
159 // nearly whole numbers already, but don't want to round 5.99999
161 int tmp_surface_width = int((tmp_surface_br[0]-tmp_surface_tl[0])/pw + 0.5);
162 int tmp_surface_height = int((tmp_surface_br[1]-tmp_surface_tl[1])/ph + 0.5);
164 RendDesc desc(renddesc);
166 desc.set_wh(tmp_surface_width,tmp_surface_height);
167 desc.set_tl(tmp_surface_tl);
168 desc.set_br(tmp_surface_br);
170 // render the layers beneath us
171 if(!context.accelerated_render(&tmp_surface,quality,desc,cb))
174 // copy the part of the layers beneath us that corresponds to this tile
175 surface->set_wh(w, h);
176 Surface::pen pen(surface->get_pen(0, 0));
177 tmp_surface.blit_to(pen,
178 int((tl[0] - tmp_surface_tl[0])/pw + 0.5),
179 int((tl[1] - tmp_surface_tl[1])/ph + 0.5),
182 Surface::alpha_pen apen(surface->begin());
184 apen.set_alpha(get_amount());
185 apen.set_blend_method(get_blend_method());
190 if(quality>=9)steps=20;
191 else if(quality>=5)steps=30;
192 else if(quality>=4)steps=60;
193 else if(quality>=3)steps=100;
197 Surface::value_prep_type cooker;
199 // loop through the pixels
200 for(y=0,pos[1]=tl[1];y<h;y++,apen.inc_y(),apen.dec_x(x),pos[1]+=ph)
201 for(x=0,pos[0]=tl[0];x<w;x++,apen.inc_x(),pos[0]+=pw)
204 begin(pos-tmp_surface_tl),
205 end((pos-origin)*(1.0f-size) + origin-tmp_surface_tl);
206 begin[0]/=pw;begin[1]/=ph;
207 end[0]/=pw;end[1]/=ph;
209 Color pool(Color::alpha());
212 int x0(round_to_int(begin[0])),
213 y0(round_to_int(begin[1])),
214 x1(round_to_int(end[0])),
215 y1(round_to_int(end[1]));
219 int sx, sy; /* step positive or negative (1 or -1) */
220 int dx, dy; /* delta (difference in X and Y between points) */
222 int w(tmp_surface_width), h(tmp_surface_height);
225 sx = ((x1 - x0) > 0) ? 1 : -1;
227 sy = ((y1 - y0) > 0) ? 1 : -1;
237 for (i = 0; i < dx; i++)
239 if(y0>=0 && x0>=0 && y0<h && x0<w)
244 pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
246 pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
252 pool+=cooker.cook(tmp_surface[y0][x0]);
254 pool+=cooker.cook(tmp_surface[x0][y0]);
258 printf("%s:%d unexpected %d >= %d or %d >= %d?\n", __FILE__, __LINE__, x0, w, y0, h);
271 apen.put_value(cooker.uncook(pool));
276 end=(pos-origin)*(1.0f-size)+origin;
278 Color pool(Color::alpha());
281 int steps(steps*size);
282 for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
284 Point loc((end-begin)*f+begin-tl);
285 loc[0]/=pw;loc[1]/=ph;
288 pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
290 pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
293 apen.put_value(pool);
298 if(cb && !cb->amount_complete(10000,10000)) return false;
300 // #define DRAW_TILE_OUTLINES
301 #ifdef DRAW_TILE_OUTLINES
302 // draw red lines to show tiles
305 if (w != 0 && h != 0) {
306 Surface::alpha_pen apen(surface->begin());
307 apen.set_alpha(get_amount());
308 apen.set_blend_method(get_blend_method());
309 apen.set_value(Color(1, 0, 0, .1));
310 for (x = 0; x < w; x++) { apen.put_value(); apen.inc_x(); } apen.dec_x(w);
311 for (y = 0; y < h; y++) { apen.put_value(); apen.inc_y(); } apen.dec_y(h);
314 #endif // DRAW_TILE_OUTLINES