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
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.
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.
21 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
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>
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
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$");
59 /* === P R O C E D U R E S ================================================= */
61 /* === M E T H O D S ======================================================= */
63 /* === E N T R Y P O I N T ================================================= */
65 RadialBlur::RadialBlur():
72 RadialBlur::~RadialBlur()
77 RadialBlur::set_param(const String & param, const ValueBase &value)
83 return Layer_Composite::set_param(param,value);
87 RadialBlur::get_param(const String ¶m)const
96 return Layer_Composite::get_param(param);
100 RadialBlur::get_param_vocab()const
102 Layer::Vocab ret(Layer_Composite::get_param_vocab());
104 ret.push_back(ParamDesc("origin")
105 .set_local_name(_("Origin"))
106 .set_description(_("Point where you want the origin to be"))
109 ret.push_back(ParamDesc("size")
110 .set_local_name(_("Size"))
111 .set_description(_("Size of blur"))
112 .set_origin("origin")
115 ret.push_back(ParamDesc("fade_out")
116 .set_local_name(_("Fade Out"))
123 RadialBlur::get_color(Context context, const Point &p)const
126 return context.get_color(p);
130 RadialBlur::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
132 if(cb && !cb->amount_complete(0,10000))
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());
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
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);
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)));
157 // round to nearest integer width and height (should be very
158 // nearly whole numbers already, but dont want to round 5.99999
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);
163 RendDesc desc(renddesc);
165 desc.set_wh(tmp_surface_width,tmp_surface_height);
166 desc.set_tl(tmp_surface_tl);
167 desc.set_br(tmp_surface_br);
169 // render the layers beneath us
170 if(!context.accelerated_render(&tmp_surface,quality,desc,cb))
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),
181 Surface::alpha_pen apen(surface->begin());
183 apen.set_alpha(get_amount());
184 apen.set_blend_method(get_blend_method());
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;
196 Surface::value_prep_type cooker;
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)
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;
208 Color pool(Color::alpha());
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]));
218 int sx, sy; /* step positive or negative (1 or -1) */
219 int dx, dy; /* delta (difference in X and Y between points) */
221 int w(tmp_surface_width), h(tmp_surface_height);
224 sx = ((x1 - x0) > 0) ? 1 : -1;
226 sy = ((y1 - y0) > 0) ? 1 : -1;
236 for (i = 0; i < dx; i++)
238 if(y0>=0 && x0>=0 && y0<h && x0<w)
243 pool+=cooker.cook(tmp_surface[y0][x0])*(i-dx);
245 pool+=cooker.cook(tmp_surface[x0][y0])*(i-dx);
251 pool+=cooker.cook(tmp_surface[y0][x0]);
253 pool+=cooker.cook(tmp_surface[x0][y0]);
257 printf("%s:%d unexpected %d >= %d or %d >= %d?\n", __FILE__, __LINE__, x0, w, y0, h);
270 apen.put_value(cooker.uncook(pool));
275 end=(pos-origin)*(1.0f-size)+origin;
277 Color pool(Color::alpha());
280 int steps(steps*size);
281 for(f=0,i=0;i<steps;i++,f+=1.0f/(steps-1))
283 Point loc((end-begin)*f+begin-tl);
284 loc[0]/=pw;loc[1]/=ph;
287 pool+=tmp_surface.linear_sample(loc[0],loc[1])*(i-steps),poolsize+=(i-steps);
289 pool+=tmp_surface.linear_sample(loc[0],loc[1]),poolsize+=1;
292 apen.put_value(pool);
297 if(cb && !cb->amount_complete(10000,10000)) return false;
299 // #define DRAW_TILE_OUTLINES
300 #ifdef DRAW_TILE_OUTLINES
301 // draw red lines to show tiles
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);
313 #endif // DRAW_TILE_OUTLINES