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-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,_("Bevel"));
68 SYNFIG_LAYER_SET_CATEGORY(Layer_Bevel,_("Stylize"));
69 SYNFIG_LAYER_SET_VERSION(Layer_Bevel,"0.2");
70 SYNFIG_LAYER_SET_CVS_ID(Layer_Bevel,"$Id: bevel.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
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);
95 Layer_Bevel::calc_offset()
97 offset[0]=Angle::cos(angle).get()*depth;
98 offset[1]=Angle::sin(angle).get()*depth;
100 offset45[0]=Angle::cos(angle-Angle::deg(45)).get()*depth*0.707106781;
101 offset45[1]=Angle::sin(angle-Angle::deg(45)).get()*depth*0.707106781;
105 Layer_Bevel::set_param(const String ¶m, const ValueBase &value)
107 IMPORT_PLUS(softness,softness=softness>0?softness:0);
110 IMPORT_PLUS(depth,calc_offset());
111 IMPORT_PLUS(angle,calc_offset());
116 return Layer_Composite::set_param(param,value);
120 Layer_Bevel::get_param(const String ¶m)const
134 return Layer_Composite::get_param(param);
138 Layer_Bevel::get_color(Context context, const Point &pos)const
140 const Vector size(softness,softness);
141 Point blurpos = Blur(size,type)(pos);
143 if(get_amount()==0.0)
144 return context.get_color(pos);
148 Real hi_alpha(1.0f-context.get_color(blurpos+offset).get_a());
149 Real lo_alpha(1.0f-context.get_color(blurpos-offset).get_a());
151 Real shade_alpha(hi_alpha-lo_alpha);
153 shade=color1,shade.set_a(shade_alpha);
155 shade=color2,shade.set_a(-shade_alpha);
157 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
161 Layer_Bevel::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
164 SuperCallback stageone(cb,0,5000,10000);
165 SuperCallback stagetwo(cb,5000,10000,10000);
167 const int w = renddesc.get_w(),
168 h = renddesc.get_h();
169 const Real pw = renddesc.get_pw(),
170 ph = renddesc.get_ph();
171 const Vector size(softness,softness);
173 RendDesc workdesc(renddesc);
175 etl::surface<float> blurred;
177 //callbacks depend on how long the blur takes
178 if(size[0] || size[1])
180 if(type == Blur::DISC)
182 stageone = SuperCallback(cb,0,5000,10000);
183 stagetwo = SuperCallback(cb,5000,10000,10000);
187 stageone = SuperCallback(cb,0,9000,10000);
188 stagetwo = SuperCallback(cb,9000,10000,10000);
193 stageone = SuperCallback(cb,0,9999,10000);
194 stagetwo = SuperCallback(cb,9999,10000,10000);
197 //expand the working surface to accommodate the blur
199 //the expanded size = 1/2 the size in each direction rounded up
200 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
201 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
203 int offset_u(round_to_int(offset[0]/pw)),offset_v(round_to_int(offset[1]/ph));
204 int offset_w(w+abs(offset_u)*2),offset_h(h+abs(offset_v)*2);
206 workdesc.set_subwindow(
213 //expand by 1/2 size in each direction on either side
220 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
223 case Blur::FASTGAUSSIAN:
230 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
235 #define GAUSSIAN_ADJUSTMENT (0.05)
236 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
237 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
242 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
243 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
245 halfsizex = (halfsizex + 1)/2;
246 halfsizey = (halfsizey + 1)/2;
247 workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
253 //render the background onto the expanded surface
254 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
257 // Copy over the alpha
258 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
261 for(int j=0;j<worksurface.get_h();j++)
262 for(int i=0;i<worksurface.get_w();i++)
264 blurred[j][i]=worksurface[j][i].get_a();
269 for(int j=0;j<worksurface.get_h();j++)
270 for(int i=0;i<worksurface.get_w();i++)
272 blurred[j][i]=worksurface[j][i].get_a()*worksurface[j][i].get_y();
277 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
279 //be sure the surface is of the correct size
280 surface->set_wh(renddesc.get_w(),renddesc.get_h());
282 int u = halfsizex+abs(offset_u), v = halfsizey+abs(offset_v);
283 for(y=0;y<renddesc.get_h();y++,v++)
285 u = halfsizex+abs(offset_u);
286 for(x=0;x<renddesc.get_w();x++,u++)
292 const float u2(offset[0]/pw),v2(offset[1]/ph);
293 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v);
296 const float u2(-offset[0]/pw),v2(-offset[1]/ph);
297 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v);
300 const float u2(offset45[0]/pw),v2(offset45[1]/ph);
301 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
304 const float u2(offset45[1]/ph),v2(-offset45[0]/pw);
305 alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
308 const float u2(-offset45[0]/pw),v2(-offset45[1]/ph);
309 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
312 const float u2(-offset45[1]/ph),v2(offset45[0]/pw);
313 alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
320 shade=Color::blend(color1,color2,alpha,Color::BLEND_STRAIGHT);
326 shade=color1,shade.set_a(shade.get_a()*alpha);
328 shade=color2,shade.set_a(shade.get_a()*-alpha);
335 (*surface)[y][x]=Color::blend(shade,worksurface[v][u],get_amount(),get_blend_method());
337 else (*surface)[y][x] = worksurface[v][u];
341 if(cb && !cb->amount_complete(10000,10000))
343 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
351 Layer_Bevel::get_param_vocab(void)const
353 Layer::Vocab ret(Layer_Composite::get_param_vocab());
355 ret.push_back(ParamDesc("type")
356 .set_local_name(_("Type"))
357 .set_description(_("Type of blur to use"))
359 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
360 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
361 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
362 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
363 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
366 ret.push_back(ParamDesc("color1")
367 .set_local_name(_("Hi-Color"))
369 ret.push_back(ParamDesc("color2")
370 .set_local_name(_("Lo-Color"))
372 ret.push_back(ParamDesc("angle")
373 .set_local_name(_("Light Angle"))
375 ret.push_back(ParamDesc("depth")
377 .set_local_name(_("Depth of Bevel"))
379 ret.push_back(ParamDesc("softness")
381 .set_local_name(_("Softness"))
383 ret.push_back(ParamDesc("use_luma")
384 .set_local_name(_("Use Luma"))
386 ret.push_back(ParamDesc("solid")
387 .set_local_name(_("Solid"))
394 Layer_Bevel::get_full_bounding_rect(Context context)const
397 return context.get_full_bounding_rect();
399 Rect under(context.get_full_bounding_rect());
401 if(Color::is_onto(get_blend_method()))
404 Rect bounds(under.expand(softness));
405 bounds.expand_x(abs(depth));
406 bounds.expand_y(abs(depth));