1 /* === S Y N F I G ========================================================= */
2 /*! \file mod_filter/blur.cpp
3 ** \brief Implementation of the "Blur" layer
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 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42 #include <synfig/segment.h>
49 using namespace synfig;
54 #define TYPE_FASTGUASSIAN 1
55 #define TYPE_FASTGAUSSIAN 1
57 #define TYPE_GUASSIAN 3
58 #define TYPE_GAUSSIAN 3
62 /* -- G L O B A L S --------------------------------------------------------- */
64 SYNFIG_LAYER_INIT(Blur_Layer);
65 SYNFIG_LAYER_SET_NAME(Blur_Layer,"blur");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Blur_Layer,N_("Blur"));
67 SYNFIG_LAYER_SET_CATEGORY(Blur_Layer,N_("Blurs"));
68 SYNFIG_LAYER_SET_VERSION(Blur_Layer,"0.2");
69 SYNFIG_LAYER_SET_CVS_ID(Blur_Layer,"$Id$");
71 /* -- F U N C T I O N S ----------------------------------------------------- */
73 inline void clamp(synfig::Vector &v)
79 Blur_Layer::Blur_Layer():
80 Layer_Composite (1.0,Color::BLEND_STRAIGHT),
82 type(Blur::FASTGAUSSIAN)
87 Blur_Layer::set_param(const String ¶m, const ValueBase &value)
89 IMPORT_PLUS(size,clamp(size));
92 return Layer_Composite::set_param(param,value);
96 Blur_Layer::get_param(const String ¶m)const
104 return Layer_Composite::get_param(param);
108 Blur_Layer::get_color(Context context, const Point &pos)const
110 Point blurpos = Blur(size,type)(pos);
112 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
113 return context.get_color(blurpos);
115 if(get_amount()==0.0)
116 return context.get_color(pos);
118 return Color::blend(context.get_color(blurpos),context.get_color(pos),get_amount(),get_blend_method());
122 Blur_Layer::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
124 // don't do anything at quality 10
126 return context.accelerated_render(surface,quality,renddesc,cb);
129 SuperCallback stageone(cb,0,5000,10000);
130 SuperCallback stagetwo(cb,5000,10000,10000);
132 const int w = renddesc.get_w(),
133 h = renddesc.get_h();
134 const Real pw = renddesc.get_pw(),
135 ph = renddesc.get_ph();
137 RendDesc workdesc(renddesc);
138 Surface worksurface,blurred;
140 //callbacks depend on how long the blur takes
141 if(size[0] || size[1])
143 if(type == Blur::DISC)
145 stageone = SuperCallback(cb,0,5000,10000);
146 stagetwo = SuperCallback(cb,5000,10000,10000);
150 stageone = SuperCallback(cb,0,9000,10000);
151 stagetwo = SuperCallback(cb,9000,10000,10000);
156 stageone = SuperCallback(cb,0,9999,10000);
157 stagetwo = SuperCallback(cb,9999,10000,10000);
160 //expand the working surface to accommodate the blur
162 //the expanded size = 1/2 the size in each direction rounded up
163 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
164 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
166 //expand by 1/2 size in each direction on either side
173 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
176 case Blur::FASTGAUSSIAN:
183 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
188 #define GAUSSIAN_ADJUSTMENT (0.05)
189 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
190 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
195 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
196 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
198 halfsizex = (halfsizex + 1)/2;
199 halfsizey = (halfsizey + 1)/2;
200 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
206 //render the background onto the expanded surface
207 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
211 Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
213 //be sure the surface is of the correct size
214 surface->set_wh(renddesc.get_w(),renddesc.get_h());
217 Surface::pen pen(surface->begin());
218 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
221 Surface::alpha_pen pen(surface->begin());
222 pen.set_alpha(get_amount());
223 pen.set_blend_method(get_blend_method());
224 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
226 if(cb && !cb->amount_complete(10000,10000))
228 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
236 Blur_Layer::get_param_vocab(void)const
238 Layer::Vocab ret(Layer_Composite::get_param_vocab());
240 ret.push_back(ParamDesc("size")
241 .set_local_name(_("Size"))
242 .set_description(_("Size of Blur"))
244 ret.push_back(ParamDesc("type")
245 .set_local_name(_("Type"))
246 .set_description(_("Type of blur to use"))
248 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
249 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
250 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
251 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
252 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
259 Blur_Layer::get_full_bounding_rect(Context context)const
261 if(is_disabled() || Color::is_onto(get_blend_method()))
262 return context.get_full_bounding_rect();
264 Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));