1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
5 ** $Id: bevel.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 <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
41 #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(Layer_Bevel);
65 SYNFIG_LAYER_SET_NAME(Layer_Bevel,"bevel");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Bevel,_("Bevel"));
67 SYNFIG_LAYER_SET_CATEGORY(Layer_Bevel,_("Stylize"));
68 SYNFIG_LAYER_SET_VERSION(Layer_Bevel,"0.2");
69 SYNFIG_LAYER_SET_CVS_ID(Layer_Bevel,"$Id: bevel.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(synfig::Vector &v)
79 Layer_Bevel::Layer_Bevel():
80 Layer_Composite (0.75,Color::BLEND_ONTO),
82 type(Blur::FASTGAUSSIAN),
83 color1(Color::white()),
84 color2(Color::black()),
87 angle=Angle::deg(135);
94 Layer_Bevel::calc_offset()
96 offset[0]=Angle::cos(angle).get()*depth;
97 offset[1]=Angle::sin(angle).get()*depth;
99 offset45[0]=Angle::cos(angle-Angle::deg(45)).get()*depth*0.707106781;
100 offset45[1]=Angle::sin(angle-Angle::deg(45)).get()*depth*0.707106781;
104 Layer_Bevel::set_param(const String ¶m, const ValueBase &value)
106 IMPORT_PLUS(softness,softness=softness>0?softness:0);
109 IMPORT_PLUS(depth,calc_offset());
110 IMPORT_PLUS(angle,calc_offset());
115 return Layer_Composite::set_param(param,value);
119 Layer_Bevel::get_param(const String ¶m)const
133 return Layer_Composite::get_param(param);
137 Layer_Bevel::get_color(Context context, const Point &pos)const
139 const Vector size(softness,softness);
140 Point blurpos = Blur(size,type)(pos);
142 if(get_amount()==0.0)
143 return context.get_color(pos);
147 Real hi_alpha(1.0f-context.get_color(blurpos+offset).get_a());
148 Real lo_alpha(1.0f-context.get_color(blurpos-offset).get_a());
150 Real shade_alpha(hi_alpha-lo_alpha);
152 shade=color1,shade.set_a(shade_alpha);
154 shade=color2,shade.set_a(-shade_alpha);
156 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
160 Layer_Bevel::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
163 SuperCallback stageone(cb,0,5000,10000);
164 SuperCallback stagetwo(cb,5000,10000,10000);
166 const int w = renddesc.get_w(),
167 h = renddesc.get_h();
168 const Real pw = renddesc.get_pw(),
169 ph = renddesc.get_ph();
170 const Vector size(softness,softness);
172 RendDesc workdesc(renddesc);
174 etl::surface<float> blurred;
176 //callbacks depend on how long the blur takes
177 if(size[0] || size[1])
179 if(type == Blur::DISC)
181 stageone = SuperCallback(cb,0,5000,10000);
182 stagetwo = SuperCallback(cb,5000,10000,10000);
186 stageone = SuperCallback(cb,0,9000,10000);
187 stagetwo = SuperCallback(cb,9000,10000,10000);
192 stageone = SuperCallback(cb,0,9999,10000);
193 stagetwo = SuperCallback(cb,9999,10000,10000);
196 //expand the working surface to accommodate the blur
198 //the expanded size = 1/2 the size in each direction rounded up
199 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
200 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
202 int offset_u(round_to_int(offset[0]/pw)),offset_v(round_to_int(offset[1]/ph));
203 int offset_w(w+abs(offset_u)*2),offset_h(h+abs(offset_v)*2);
205 workdesc.set_subwindow(
212 //expand by 1/2 size in each direction on either side
219 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
222 case Blur::FASTGAUSSIAN:
229 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
234 #define GAUSSIAN_ADJUSTMENT (0.05)
235 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
236 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
241 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
242 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
244 halfsizex = (halfsizex + 1)/2;
245 halfsizey = (halfsizey + 1)/2;
246 workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
252 //render the background onto the expanded surface
253 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
256 // Copy over the alpha
257 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
260 for(int j=0;j<worksurface.get_h();j++)
261 for(int i=0;i<worksurface.get_w();i++)
263 blurred[j][i]=worksurface[j][i].get_a();
268 for(int j=0;j<worksurface.get_h();j++)
269 for(int i=0;i<worksurface.get_w();i++)
271 blurred[j][i]=worksurface[j][i].get_a()*worksurface[j][i].get_y();
276 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
278 //be sure the surface is of the correct size
279 surface->set_wh(renddesc.get_w(),renddesc.get_h());
281 int u = halfsizex+abs(offset_u), v = halfsizey+abs(offset_v);
282 for(y=0;y<renddesc.get_h();y++,v++)
284 u = halfsizex+abs(offset_u);
285 for(x=0;x<renddesc.get_w();x++,u++)
291 const float u2(offset[0]/pw),v2(offset[1]/ph);
292 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v);
295 const float u2(-offset[0]/pw),v2(-offset[1]/ph);
296 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v);
299 const float u2(offset45[0]/pw),v2(offset45[1]/ph);
300 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
303 const float u2(offset45[1]/ph),v2(-offset45[0]/pw);
304 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
307 const float u2(-offset45[0]/pw),v2(-offset45[1]/ph);
308 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
311 const float u2(-offset45[1]/ph),v2(offset45[0]/pw);
312 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
319 shade=Color::blend(color1,color2,alpha,Color::BLEND_STRAIGHT);
325 shade=color1,shade.set_a(shade.get_a()*alpha);
327 shade=color2,shade.set_a(shade.get_a()*-alpha);
334 (*surface)[y][x]=Color::blend(shade,worksurface[v][u],get_amount(),get_blend_method());
336 else (*surface)[y][x] = worksurface[v][u];
340 if(cb && !cb->amount_complete(10000,10000))
342 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
350 Layer_Bevel::get_param_vocab(void)const
352 Layer::Vocab ret(Layer_Composite::get_param_vocab());
354 ret.push_back(ParamDesc("type")
355 .set_local_name(_("Type"))
356 .set_description(_("Type of blur to use"))
358 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
359 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
360 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
361 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
362 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
365 ret.push_back(ParamDesc("color1")
366 .set_local_name(_("Hi-Color"))
368 ret.push_back(ParamDesc("color2")
369 .set_local_name(_("Lo-Color"))
371 ret.push_back(ParamDesc("angle")
372 .set_local_name(_("Light Angle"))
374 ret.push_back(ParamDesc("depth")
376 .set_local_name(_("Depth of Bevel"))
378 ret.push_back(ParamDesc("softness")
380 .set_local_name(_("Softness"))
382 ret.push_back(ParamDesc("use_luma")
383 .set_local_name(_("Use Luma"))
385 ret.push_back(ParamDesc("solid")
386 .set_local_name(_("Solid"))
393 Layer_Bevel::get_full_bounding_rect(Context context)const
396 return context.get_full_bounding_rect();
398 Rect under(context.get_full_bounding_rect());
400 if(Color::is_onto(get_blend_method()))
403 Rect bounds(under.expand(softness));
404 bounds.expand_x(abs(depth));
405 bounds.expand_y(abs(depth));