1 /* === S Y N F I G ========================================================= */
2 /*! \file mod_filter/blur.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 /* ========================================================================= */
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,_("Blur"));
67 SYNFIG_LAYER_SET_CATEGORY(Blur_Layer,_("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
125 SuperCallback stageone(cb,0,5000,10000);
126 SuperCallback stagetwo(cb,5000,10000,10000);
128 const int w = renddesc.get_w(),
129 h = renddesc.get_h();
130 const Real pw = renddesc.get_pw(),
131 ph = renddesc.get_ph();
133 RendDesc workdesc(renddesc);
134 Surface worksurface,blurred;
136 //callbacks depend on how long the blur takes
137 if(size[0] || size[1])
139 if(type == Blur::DISC)
141 stageone = SuperCallback(cb,0,5000,10000);
142 stagetwo = SuperCallback(cb,5000,10000,10000);
146 stageone = SuperCallback(cb,0,9000,10000);
147 stagetwo = SuperCallback(cb,9000,10000,10000);
152 stageone = SuperCallback(cb,0,9999,10000);
153 stagetwo = SuperCallback(cb,9999,10000,10000);
156 //expand the working surface to accommodate the blur
158 //the expanded size = 1/2 the size in each direction rounded up
159 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
160 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
162 //expand by 1/2 size in each direction on either side
169 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
172 case Blur::FASTGAUSSIAN:
179 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
184 #define GAUSSIAN_ADJUSTMENT (0.05)
185 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
186 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
191 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
192 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
194 halfsizex = (halfsizex + 1)/2;
195 halfsizey = (halfsizey + 1)/2;
196 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
202 //render the background onto the expanded surface
203 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
207 Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
209 //be sure the surface is of the correct size
210 surface->set_wh(renddesc.get_w(),renddesc.get_h());
213 Surface::pen pen(surface->begin());
214 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
217 Surface::alpha_pen pen(surface->begin());
218 pen.set_alpha(get_amount());
219 pen.set_blend_method(get_blend_method());
220 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
222 if(cb && !cb->amount_complete(10000,10000))
224 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
232 Blur_Layer::get_param_vocab(void)const
234 Layer::Vocab ret(Layer_Composite::get_param_vocab());
236 ret.push_back(ParamDesc("size")
237 .set_local_name(_("Size"))
238 .set_description(_("Size of Blur"))
240 ret.push_back(ParamDesc("type")
241 .set_local_name(_("Type"))
242 .set_description(_("Type of blur to use"))
244 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
245 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
246 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
247 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
248 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
255 Blur_Layer::get_full_bounding_rect(Context context)const
257 if(is_disabled() || Color::is_onto(get_blend_method()))
258 return context.get_full_bounding_rect();
260 Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));