1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Bevel" layer
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>
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(Layer_Bevel);
66 SYNFIG_LAYER_SET_NAME(Layer_Bevel,"bevel");
67 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Bevel,N_("Bevel"));
68 SYNFIG_LAYER_SET_CATEGORY(Layer_Bevel,N_("Stylize"));
69 SYNFIG_LAYER_SET_VERSION(Layer_Bevel,"0.2");
70 SYNFIG_LAYER_SET_CVS_ID(Layer_Bevel,"$Id$");
72 /* -- F U N C T I O N S ----------------------------------------------------- */
74 inline void clamp(synfig::Vector &v)
80 Layer_Bevel::Layer_Bevel():
81 Layer_Composite (0.75,Color::BLEND_ONTO),
83 type(Blur::FASTGAUSSIAN),
84 color1(Color::white()),
85 color2(Color::black()),
88 angle=Angle::deg(135);
92 Layer::Vocab voc(get_param_vocab());
93 Layer::fill_static(voc);
97 Layer_Bevel::calc_offset()
99 offset[0]=Angle::cos(angle).get()*depth;
100 offset[1]=Angle::sin(angle).get()*depth;
102 offset45[0]=Angle::cos(angle-Angle::deg(45)).get()*depth*0.707106781;
103 offset45[1]=Angle::sin(angle-Angle::deg(45)).get()*depth*0.707106781;
107 Layer_Bevel::set_param(const String ¶m, const ValueBase &value)
109 IMPORT_PLUS(softness,softness=softness>0?softness:0);
112 IMPORT_PLUS(depth,calc_offset());
113 IMPORT_PLUS(angle,calc_offset());
118 return Layer_Composite::set_param(param,value);
122 Layer_Bevel::get_param(const String ¶m)const
136 return Layer_Composite::get_param(param);
140 Layer_Bevel::get_color(Context context, const Point &pos)const
142 const Vector size(softness,softness);
143 Point blurpos = Blur(size,type)(pos);
145 if(get_amount()==0.0)
146 return context.get_color(pos);
150 Real hi_alpha(1.0f-context.get_color(blurpos+offset).get_a());
151 Real lo_alpha(1.0f-context.get_color(blurpos-offset).get_a());
153 Real shade_alpha(hi_alpha-lo_alpha);
155 shade=color1,shade.set_a(shade_alpha);
157 shade=color2,shade.set_a(-shade_alpha);
159 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
163 Layer_Bevel::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
166 SuperCallback stageone(cb,0,5000,10000);
167 SuperCallback stagetwo(cb,5000,10000,10000);
169 const int w = renddesc.get_w(),
170 h = renddesc.get_h();
171 const Real pw = renddesc.get_pw(),
172 ph = renddesc.get_ph();
173 const Vector size(softness,softness);
175 RendDesc workdesc(renddesc);
177 etl::surface<float> blurred;
179 //callbacks depend on how long the blur takes
180 if(size[0] || size[1])
182 if(type == Blur::DISC)
184 stageone = SuperCallback(cb,0,5000,10000);
185 stagetwo = SuperCallback(cb,5000,10000,10000);
189 stageone = SuperCallback(cb,0,9000,10000);
190 stagetwo = SuperCallback(cb,9000,10000,10000);
195 stageone = SuperCallback(cb,0,9999,10000);
196 stagetwo = SuperCallback(cb,9999,10000,10000);
199 //expand the working surface to accommodate the blur
201 //the expanded size = 1/2 the size in each direction rounded up
202 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
203 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
205 int offset_u(round_to_int(offset[0]/pw)),offset_v(round_to_int(offset[1]/ph));
206 int offset_w(w+abs(offset_u)*2),offset_h(h+abs(offset_v)*2);
208 workdesc.set_subwindow(
215 //expand by 1/2 size in each direction on either side
222 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
225 case Blur::FASTGAUSSIAN:
232 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
237 #define GAUSSIAN_ADJUSTMENT (0.05)
238 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
239 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
244 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
245 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
247 halfsizex = (halfsizex + 1)/2;
248 halfsizey = (halfsizey + 1)/2;
249 workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
255 //render the background onto the expanded surface
256 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
259 // Copy over the alpha
260 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
263 for(int j=0;j<worksurface.get_h();j++)
264 for(int i=0;i<worksurface.get_w();i++)
266 blurred[j][i]=worksurface[j][i].get_a();
271 for(int j=0;j<worksurface.get_h();j++)
272 for(int i=0;i<worksurface.get_w();i++)
274 blurred[j][i]=worksurface[j][i].get_a()*worksurface[j][i].get_y();
279 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
281 //be sure the surface is of the correct size
282 surface->set_wh(renddesc.get_w(),renddesc.get_h());
284 int u = halfsizex+abs(offset_u), v = halfsizey+abs(offset_v);
285 for(y=0;y<renddesc.get_h();y++,v++)
287 u = halfsizex+abs(offset_u);
288 for(x=0;x<renddesc.get_w();x++,u++)
294 const float u2(offset[0]/pw),v2(offset[1]/ph);
295 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v);
298 const float u2(-offset[0]/pw),v2(-offset[1]/ph);
299 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v);
302 const float u2(offset45[0]/pw),v2(offset45[1]/ph);
303 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
306 const float u2(offset45[1]/ph),v2(-offset45[0]/pw);
307 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
310 const float u2(-offset45[0]/pw),v2(-offset45[1]/ph);
311 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
314 const float u2(-offset45[1]/ph),v2(offset45[0]/pw);
315 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
322 shade=Color::blend(color1,color2,alpha,Color::BLEND_STRAIGHT);
328 shade=color1,shade.set_a(shade.get_a()*alpha);
330 shade=color2,shade.set_a(shade.get_a()*-alpha);
337 (*surface)[y][x]=Color::blend(shade,worksurface[v][u],get_amount(),get_blend_method());
339 else (*surface)[y][x] = worksurface[v][u];
343 if(cb && !cb->amount_complete(10000,10000))
345 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
353 Layer_Bevel::get_param_vocab(void)const
355 Layer::Vocab ret(Layer_Composite::get_param_vocab());
357 ret.push_back(ParamDesc("type")
358 .set_local_name(_("Type"))
359 .set_description(_("Type of blur to use"))
361 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
362 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
363 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
364 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
365 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
368 ret.push_back(ParamDesc("color1")
369 .set_local_name(_("Hi-Color"))
371 ret.push_back(ParamDesc("color2")
372 .set_local_name(_("Lo-Color"))
374 ret.push_back(ParamDesc("angle")
375 .set_local_name(_("Light Angle"))
377 ret.push_back(ParamDesc("depth")
379 .set_local_name(_("Depth of Bevel"))
381 ret.push_back(ParamDesc("softness")
383 .set_local_name(_("Softness"))
385 ret.push_back(ParamDesc("use_luma")
386 .set_local_name(_("Use Luma"))
388 ret.push_back(ParamDesc("solid")
389 .set_local_name(_("Solid"))
396 Layer_Bevel::get_full_bounding_rect(Context context)const
399 return context.get_full_bounding_rect();
401 Rect under(context.get_full_bounding_rect());
403 if(Color::is_onto(get_blend_method()))
406 Rect bounds(under.expand(softness));
407 bounds.expand_x(abs(depth));
408 bounds.expand_y(abs(depth));