1 /* === S I N F G =========================================================== */
3 ** \brief Template Header
5 ** $Id: shade.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>
49 using namespace sinfg;
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 SINFG_LAYER_INIT(Layer_Shade);
65 SINFG_LAYER_SET_NAME(Layer_Shade,"shade");
66 SINFG_LAYER_SET_LOCAL_NAME(Layer_Shade,_("Shade"));
67 SINFG_LAYER_SET_CATEGORY(Layer_Shade,_("Stylize"));
68 SINFG_LAYER_SET_VERSION(Layer_Shade,"0.2");
69 SINFG_LAYER_SET_CVS_ID(Layer_Shade,"$Id: shade.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
71 /* -- F U N C T I O N S ----------------------------------------------------- */
73 inline void clamp(sinfg::Vector &v)
79 Layer_Shade::Layer_Shade():
80 Layer_Composite (0.75,Color::BLEND_BEHIND),
82 type(Blur::FASTGAUSSIAN),
83 color(Color::black()),
90 Layer_Shade::set_param(const String ¶m, const ValueBase &value)
92 IMPORT_PLUS(size,clamp(size));
98 return Layer_Composite::set_param(param,value);
102 Layer_Shade::get_param(const String ¶m)const
113 return Layer_Composite::get_param(param);
117 Layer_Shade::get_color(Context context, const Point &pos)const
119 Point blurpos = Blur(size,type)(pos);
121 if(get_amount()==0.0)
122 return context.get_color(pos);
127 shade.set_a(context.get_color(blurpos-offset).get_a());
129 shade.set_a(1.0f-context.get_color(blurpos-offset).get_a());
131 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
135 Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
139 const int w = renddesc.get_w(),
140 h = renddesc.get_h();
141 const Real pw = renddesc.get_pw(),
142 ph = renddesc.get_ph();
144 RendDesc workdesc(renddesc);
146 etl::surface<float> blurred;
148 //expand the working surface to accommodate the blur
150 //the expanded size = 1/2 the size in each direction rounded up
151 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
152 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
154 int offset_u(-round_to_int(offset[0]/pw)),offset_v(-round_to_int(offset[1]/ph));
156 int offset_w(w+abs(offset_u)),offset_h(h+abs(offset_v));
158 workdesc.set_subwindow(
159 offset_u<0?offset_u:0,
160 offset_v<0?offset_v:0,
161 (offset_u>0?offset_u:0)+w,
162 (offset_v>0?offset_v:0)+h
178 //expand by 1/2 size in each direction on either side
185 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
188 case Blur::FASTGAUSSIAN:
195 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
200 #define GAUSSIAN_ADJUSTMENT (0.05)
201 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
202 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
207 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
208 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
210 halfsizex = (halfsizex + 1)/2;
211 halfsizey = (halfsizey + 1)/2;
212 workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
217 #define SCALE_FACTOR (64.0)
218 if(/*quality>9 || */size[0]<=pw*SCALE_FACTOR)
220 SuperCallback stageone(cb,0,5000,10000);
221 SuperCallback stagetwo(cb,5000,10000,10000);
223 //callbacks depend on how long the blur takes
224 if(size[0] || size[1])
226 if(type == Blur::DISC)
228 stageone = SuperCallback(cb,0,5000,10000);
229 stagetwo = SuperCallback(cb,5000,10000,10000);
233 stageone = SuperCallback(cb,0,9000,10000);
234 stagetwo = SuperCallback(cb,9000,10000,10000);
239 stageone = SuperCallback(cb,0,9999,10000);
240 stagetwo = SuperCallback(cb,9999,10000,10000);
245 //render the background onto the expanded surface
246 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
249 // Copy over the alpha
250 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
251 for(int j=0;j<worksurface.get_h();j++)
252 for(int i=0;i<worksurface.get_w();i++)
254 blurred[j][i]=worksurface[j][i].get_a();
258 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
260 //be sure the surface is of the correct size
261 surface->set_wh(renddesc.get_w(),renddesc.get_h());
263 int u = halfsizex-(offset_u<0?offset_u:0), v = halfsizey-(offset_v<0?offset_v:0);
264 for(y=0;y<renddesc.get_h();y++,v++)
266 u = halfsizex-(offset_u<0?offset_u:0);
267 for(x=0;x<renddesc.get_w();x++,u++)
272 a.set_a(blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
274 a.set_a(1.0f-blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
276 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
278 (*surface)[y][x]=Color::blend(a,worksurface[v][u],get_amount(),get_blend_method());
280 else (*surface)[y][x] = worksurface[v][u];
287 SuperCallback stageone(cb,0,5000,10000);
288 SuperCallback stagetwo(cb,5000,10000,10000);
290 //callbacks depend on how long the blur takes
291 if(size[0] || size[1])
293 if(type == Blur::DISC)
295 stageone = SuperCallback(cb,0,5000,10000);
296 stagetwo = SuperCallback(cb,5000,10000,10000);
300 stageone = SuperCallback(cb,0,9000,10000);
301 stagetwo = SuperCallback(cb,9000,10000,10000);
306 stageone = SuperCallback(cb,0,9999,10000);
307 stagetwo = SuperCallback(cb,9999,10000,10000);
310 int fw(floor_to_int(abs(size[0]/(pw*SCALE_FACTOR)))+1);
311 int fh(floor_to_int(abs(size[1]/(ph*SCALE_FACTOR)))+1);
312 int tmpw(round_to_int((float)workdesc.get_w()/fw)),tmph(round_to_int((float)workdesc.get_h()/fh));
314 workdesc.clear_flags();
315 workdesc.set_wh(tmpw,tmph);
316 //sinfg::info("fw: %d, fh: %d",fw,fh);
318 //render the blur fodder
319 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
322 //render the background
323 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
326 // Copy over the alpha
327 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
328 for(int j=0;j<worksurface.get_h();j++)
329 for(int i=0;i<worksurface.get_w();i++)
330 blurred[j][i]=worksurface[j][i].get_a();
333 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
336 int u = halfsizex-(offset_u<0?offset_u:0), v = halfsizey-(offset_v<0?offset_v:0);
337 for(y=0;y<renddesc.get_h();y++,v++)
339 u = halfsizex-(offset_u<0?offset_u:0);
340 for(x=0;x<renddesc.get_w();x++,u++)
345 a.set_a(blurred.linear_sample(((float)offset_u+(float)u)/(float)fw,((float)offset_v+(float)v)/(float)fh));
347 a.set_a(1.0f-blurred.linear_sample(((float)offset_u+(float)u)/fw,((float)offset_v+(float)v)/(float)fh));
349 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
351 (*surface)[y][x]=Color::blend(a,(*surface)[y][x],get_amount(),get_blend_method());
358 if(cb && !cb->amount_complete(10000,10000))
360 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
368 Layer_Shade::get_param_vocab(void)const
370 Layer::Vocab ret(Layer_Composite::get_param_vocab());
372 ret.push_back(ParamDesc("color")
373 .set_local_name(_("Color"))
375 ret.push_back(ParamDesc("offset")
376 .set_local_name(_("Offset"))
378 ret.push_back(ParamDesc("size")
379 .set_local_name(_("Size"))
380 .set_description(_("Size of Shade"))
382 .set_origin("offset")
384 ret.push_back(ParamDesc("type")
385 .set_local_name(_("Type"))
386 .set_description(_("Type of blur to use"))
388 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
389 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
390 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
391 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
392 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
395 ret.push_back(ParamDesc("invert")
396 .set_local_name(_("Invert"))
403 Layer_Shade::get_full_bounding_rect(Context context)const
406 return context.get_full_bounding_rect();
409 return Rect::full_plane();
411 Rect under(context.get_full_bounding_rect());
413 if(Color::is_onto(get_blend_method()))
416 Rect bounds((under+offset).expand_x(size[0]).expand_y(size[1]));