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
9 ** Copyright (c) 2008 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 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
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/segment.h>
50 using namespace synfig;
55 #define TYPE_FASTGUASSIAN 1
56 #define TYPE_FASTGAUSSIAN 1
58 #define TYPE_GUASSIAN 3
59 #define TYPE_GAUSSIAN 3
63 /* -- G L O B A L S --------------------------------------------------------- */
65 SYNFIG_LAYER_INIT(Blur_Layer);
66 SYNFIG_LAYER_SET_NAME(Blur_Layer,"blur");
67 SYNFIG_LAYER_SET_LOCAL_NAME(Blur_Layer,N_("Blur"));
68 SYNFIG_LAYER_SET_CATEGORY(Blur_Layer,N_("Blurs"));
69 SYNFIG_LAYER_SET_VERSION(Blur_Layer,"0.2");
70 SYNFIG_LAYER_SET_CVS_ID(Blur_Layer,"$Id$");
72 /* -- F U N C T I O N S ----------------------------------------------------- */
74 inline void clamp(synfig::Vector &v)
80 Blur_Layer::Blur_Layer():
81 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
83 type(Blur::FASTGAUSSIAN)
85 Layer::Vocab voc(get_param_vocab());
86 Layer::fill_static(voc);
87 set_param_static("blend_method", true);
91 Blur_Layer::set_param(const String ¶m, const ValueBase &value)
93 IMPORT_PLUS(size,clamp(size));
96 return Layer_Composite::set_param(param,value);
100 Blur_Layer::get_param(const String ¶m)const
108 return Layer_Composite::get_param(param);
112 Blur_Layer::set_param_static(const String ¶m, const bool x)
114 return Layer_Composite::set_param_static(param, x);
118 Blur_Layer::get_param_static(const String ¶m)const
120 return Layer_Composite::get_param_static(param);
125 Blur_Layer::get_color(Context context, const Point &pos)const
127 Point blurpos = Blur(size,type)(pos);
129 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
130 return context.get_color(blurpos);
132 if(get_amount()==0.0)
133 return context.get_color(pos);
135 return Color::blend(context.get_color(blurpos),context.get_color(pos),get_amount(),get_blend_method());
139 Blur_Layer::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
141 // don't do anything at quality 10
143 return context.accelerated_render(surface,quality,renddesc,cb);
146 SuperCallback stageone(cb,0,5000,10000);
147 SuperCallback stagetwo(cb,5000,10000,10000);
149 const int w = renddesc.get_w(),
150 h = renddesc.get_h();
151 const Real pw = renddesc.get_pw(),
152 ph = renddesc.get_ph();
154 RendDesc workdesc(renddesc);
155 Surface worksurface,blurred;
157 //callbacks depend on how long the blur takes
158 if(size[0] || size[1])
160 if(type == Blur::DISC)
162 stageone = SuperCallback(cb,0,5000,10000);
163 stagetwo = SuperCallback(cb,5000,10000,10000);
167 stageone = SuperCallback(cb,0,9000,10000);
168 stagetwo = SuperCallback(cb,9000,10000,10000);
173 stageone = SuperCallback(cb,0,9999,10000);
174 stagetwo = SuperCallback(cb,9999,10000,10000);
177 //expand the working surface to accommodate the blur
179 //the expanded size = 1/2 the size in each direction rounded up
180 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
181 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
183 //expand by 1/2 size in each direction on either side
190 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
193 case Blur::FASTGAUSSIAN:
200 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
205 #define GAUSSIAN_ADJUSTMENT (0.05)
206 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
207 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
212 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
213 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
215 halfsizex = (halfsizex + 1)/2;
216 halfsizey = (halfsizey + 1)/2;
217 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
223 //render the background onto the expanded surface
224 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
228 Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
230 //be sure the surface is of the correct size
231 surface->set_wh(renddesc.get_w(),renddesc.get_h());
234 Surface::pen pen(surface->begin());
235 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
238 Surface::alpha_pen pen(surface->begin());
239 pen.set_alpha(get_amount());
240 pen.set_blend_method(get_blend_method());
241 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
243 if(cb && !cb->amount_complete(10000,10000))
245 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
253 Blur_Layer::get_param_vocab(void)const
255 Layer::Vocab ret(Layer_Composite::get_param_vocab());
257 ret.push_back(ParamDesc("size")
258 .set_local_name(_("Size"))
259 .set_description(_("Size of Blur"))
261 ret.push_back(ParamDesc("type")
262 .set_local_name(_("Type"))
263 .set_description(_("Type of blur to use"))
265 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
266 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
267 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
268 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
269 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
276 Blur_Layer::get_full_bounding_rect(Context context)const
278 if(is_disabled() || Color::is_onto(get_blend_method()))
279 return context.get_full_bounding_rect();
281 Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));