1 /* === S I N F G =========================================================== */
3 ** \brief Template Header
5 ** $Id: blur.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #include <sinfg/string.h>
34 #include <sinfg/time.h>
35 #include <sinfg/context.h>
36 #include <sinfg/paramdesc.h>
37 #include <sinfg/renddesc.h>
38 #include <sinfg/surface.h>
39 #include <sinfg/value.h>
40 #include <sinfg/valuenode.h>
41 #include <sinfg/segment.h>
48 using namespace sinfg;
53 #define TYPE_FASTGUASSIAN 1
54 #define TYPE_FASTGAUSSIAN 1
56 #define TYPE_GUASSIAN 3
57 #define TYPE_GAUSSIAN 3
61 /* -- G L O B A L S --------------------------------------------------------- */
63 SINFG_LAYER_INIT(Blur_Layer);
64 SINFG_LAYER_SET_NAME(Blur_Layer,"blur");
65 SINFG_LAYER_SET_LOCAL_NAME(Blur_Layer,_("Blur"));
66 SINFG_LAYER_SET_CATEGORY(Blur_Layer,_("Blurs"));
67 SINFG_LAYER_SET_VERSION(Blur_Layer,"0.2");
68 SINFG_LAYER_SET_CVS_ID(Blur_Layer,"$Id: blur.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
70 /* -- F U N C T I O N S ----------------------------------------------------- */
72 inline void clamp(sinfg::Vector &v)
78 Blur_Layer::Blur_Layer():
79 Layer_Composite (1.0,Color::BLEND_STRAIGHT),
81 type(Blur::FASTGAUSSIAN)
86 Blur_Layer::set_param(const String ¶m, const ValueBase &value)
88 IMPORT_PLUS(size,clamp(size));
91 return Layer_Composite::set_param(param,value);
95 Blur_Layer::get_param(const String ¶m)const
103 return Layer_Composite::get_param(param);
107 Blur_Layer::get_color(Context context, const Point &pos)const
109 Point blurpos = Blur(size,type)(pos);
111 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
112 return context.get_color(blurpos);
114 if(get_amount()==0.0)
115 return context.get_color(pos);
117 return Color::blend(context.get_color(blurpos),context.get_color(pos),get_amount(),get_blend_method());
121 Blur_Layer::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
124 SuperCallback stageone(cb,0,5000,10000);
125 SuperCallback stagetwo(cb,5000,10000,10000);
127 const int w = renddesc.get_w(),
128 h = renddesc.get_h();
129 const Real pw = renddesc.get_pw(),
130 ph = renddesc.get_ph();
132 RendDesc workdesc(renddesc);
133 Surface worksurface,blurred;
135 //callbacks depend on how long the blur takes
136 if(size[0] || size[1])
138 if(type == Blur::DISC)
140 stageone = SuperCallback(cb,0,5000,10000);
141 stagetwo = SuperCallback(cb,5000,10000,10000);
145 stageone = SuperCallback(cb,0,9000,10000);
146 stagetwo = SuperCallback(cb,9000,10000,10000);
151 stageone = SuperCallback(cb,0,9999,10000);
152 stagetwo = SuperCallback(cb,9999,10000,10000);
155 //expand the working surface to accommodate the blur
157 //the expanded size = 1/2 the size in each direction rounded up
158 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
159 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
161 //expand by 1/2 size in each direction on either side
168 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
171 case Blur::FASTGAUSSIAN:
178 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
183 #define GAUSSIAN_ADJUSTMENT (0.05)
184 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
185 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
190 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
191 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
193 halfsizex = (halfsizex + 1)/2;
194 halfsizey = (halfsizey + 1)/2;
195 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
201 //render the background onto the expanded surface
202 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
206 Blur(size,type,&stagetwo)(worksurface,workdesc.get_br()-workdesc.get_tl(),blurred);
208 //be sure the surface is of the correct size
209 surface->set_wh(renddesc.get_w(),renddesc.get_h());
212 Surface::pen pen(surface->begin());
213 worksurface.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
216 Surface::alpha_pen pen(surface->begin());
217 pen.set_alpha(get_amount());
218 pen.set_blend_method(get_blend_method());
219 blurred.blit_to(pen,halfsizex,halfsizey,renddesc.get_w(),renddesc.get_h());
221 if(cb && !cb->amount_complete(10000,10000))
223 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
231 Blur_Layer::get_param_vocab(void)const
233 Layer::Vocab ret(Layer_Composite::get_param_vocab());
235 ret.push_back(ParamDesc("size")
236 .set_local_name(_("Size"))
237 .set_description(_("Size of Blur"))
239 ret.push_back(ParamDesc("type")
240 .set_local_name(_("Type"))
241 .set_description(_("Type of blur to use"))
243 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
244 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
245 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
246 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
247 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
254 Blur_Layer::get_full_bounding_rect(Context context)const
256 if(is_disabled() || Color::is_onto(get_blend_method()))
257 return context.get_full_bounding_rect();
259 Rect bounds(context.get_full_bounding_rect().expand_x(size[0]).expand_y(size[1]));