1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Shade" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
43 #include <synfig/segment.h>
51 using namespace synfig;
56 #define TYPE_FASTGUASSIAN 1
58 #define TYPE_GAUSSIAN 3
62 /* -- G L O B A L S --------------------------------------------------------- */
64 SYNFIG_LAYER_INIT(Layer_Shade);
65 SYNFIG_LAYER_SET_NAME(Layer_Shade,"shade");
66 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shade,N_("Shade"));
67 SYNFIG_LAYER_SET_CATEGORY(Layer_Shade,N_("Stylize"));
68 SYNFIG_LAYER_SET_VERSION(Layer_Shade,"0.2");
69 SYNFIG_LAYER_SET_CVS_ID(Layer_Shade,"$Id$");
71 /* -- F U N C T I O N S ----------------------------------------------------- */
73 inline void clamp(synfig::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));
94 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
95 set_blend_method(Color::BLEND_ALPHA_OVER);
96 color.set_a(1); } else transparent_color_ = true; } });
100 IMPORT_AS(origin,"offset");
102 return Layer_Composite::set_param(param,value);
106 Layer_Shade::get_param(const String ¶m)const
117 return Layer_Composite::get_param(param);
121 Layer_Shade::get_color(Context context, const Point &pos)const
123 Point blurpos = Blur(size,type)(pos);
125 if(get_amount()==0.0)
126 return context.get_color(pos);
131 shade.set_a(context.get_color(blurpos-origin).get_a());
133 shade.set_a(1.0f-context.get_color(blurpos-origin).get_a());
135 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
139 Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
143 const int w = renddesc.get_w(),
144 h = renddesc.get_h();
145 const Real pw = renddesc.get_pw(),
146 ph = renddesc.get_ph();
148 RendDesc workdesc(renddesc);
150 etl::surface<float> blurred;
152 //expand the working surface to accommodate the blur
154 //the expanded size = 1/2 the size in each direction rounded up
155 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
156 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
158 int origin_u(-round_to_int(origin[0]/pw)),origin_v(-round_to_int(origin[1]/ph));
160 int origin_w(w+abs(origin_u)),origin_h(h+abs(origin_v));
162 workdesc.set_subwindow(
163 origin_u<0?origin_u:0,
164 origin_v<0?origin_v:0,
165 (origin_u>0?origin_u:0)+w,
166 (origin_v>0?origin_v:0)+h
174 else if (quality == 9)
180 //expand by 1/2 size in each direction on either side
187 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),origin_w+2*max(1,halfsizex),origin_h+2*max(1,halfsizey));
190 case Blur::FASTGAUSSIAN:
197 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),origin_w+2*max(1,halfsizex),origin_h+2*max(1,halfsizey));
202 #define GAUSSIAN_ADJUSTMENT (0.05)
203 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
204 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
209 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
210 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
212 halfsizex = (halfsizex + 1)/2;
213 halfsizey = (halfsizey + 1)/2;
214 workdesc.set_subwindow( -halfsizex, -halfsizey, origin_w+2*halfsizex, origin_h+2*halfsizey );
219 #define SCALE_FACTOR (64.0)
220 if(/*quality>9 || */size[0]<=pw*SCALE_FACTOR)
222 SuperCallback stageone(cb,0,5000,10000);
223 SuperCallback stagetwo(cb,5000,10000,10000);
225 //callbacks depend on how long the blur takes
226 if(size[0] || size[1])
228 if(type == Blur::DISC)
230 stageone = SuperCallback(cb,0,5000,10000);
231 stagetwo = SuperCallback(cb,5000,10000,10000);
235 stageone = SuperCallback(cb,0,9000,10000);
236 stagetwo = SuperCallback(cb,9000,10000,10000);
241 stageone = SuperCallback(cb,0,9999,10000);
242 stagetwo = SuperCallback(cb,9999,10000,10000);
247 //render the background onto the expanded surface
248 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
251 // Copy over the alpha
252 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
253 for(int j=0;j<worksurface.get_h();j++)
254 for(int i=0;i<worksurface.get_w();i++)
256 blurred[j][i]=worksurface[j][i].get_a();
260 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
262 //be sure the surface is of the correct size
263 surface->set_wh(renddesc.get_w(),renddesc.get_h());
265 int u = halfsizex-(origin_u<0?origin_u:0), v = halfsizey-(origin_v<0?origin_v:0);
266 for(y=0;y<renddesc.get_h();y++,v++)
268 u = halfsizex-(origin_u<0?origin_u:0);
269 for(x=0;x<renddesc.get_w();x++,u++)
274 a.set_a(blurred.linear_sample(origin_u+(float)u,origin_v+(float)v));
276 a.set_a(1.0f-blurred.linear_sample(origin_u+(float)u,origin_v+(float)v));
278 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
280 (*surface)[y][x]=Color::blend(a,worksurface[v][u],get_amount(),get_blend_method());
282 else (*surface)[y][x] = worksurface[v][u];
289 SuperCallback stageone(cb,0,5000,10000);
290 SuperCallback stagetwo(cb,5000,10000,10000);
292 //callbacks depend on how long the blur takes
293 if(size[0] || size[1])
295 if(type == Blur::DISC)
297 stageone = SuperCallback(cb,0,5000,10000);
298 stagetwo = SuperCallback(cb,5000,10000,10000);
302 stageone = SuperCallback(cb,0,9000,10000);
303 stagetwo = SuperCallback(cb,9000,10000,10000);
308 stageone = SuperCallback(cb,0,9999,10000);
309 stagetwo = SuperCallback(cb,9999,10000,10000);
312 int fw(floor_to_int(abs(size[0]/(pw*SCALE_FACTOR)))+1);
313 int fh(floor_to_int(abs(size[1]/(ph*SCALE_FACTOR)))+1);
314 int tmpw(round_to_int((float)workdesc.get_w()/fw)),tmph(round_to_int((float)workdesc.get_h()/fh));
316 workdesc.clear_flags();
317 workdesc.set_wh(tmpw,tmph);
318 //synfig::info("fw: %d, fh: %d",fw,fh);
320 //render the blur fodder
321 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
324 //render the background
325 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
328 // Copy over the alpha
329 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
330 for(int j=0;j<worksurface.get_h();j++)
331 for(int i=0;i<worksurface.get_w();i++)
332 blurred[j][i]=worksurface[j][i].get_a();
335 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
338 int u = halfsizex-(origin_u<0?origin_u:0), v = halfsizey-(origin_v<0?origin_v:0);
339 for(y=0;y<renddesc.get_h();y++,v++)
341 u = halfsizex-(origin_u<0?origin_u:0);
342 for(x=0;x<renddesc.get_w();x++,u++)
347 a.set_a(blurred.linear_sample(((float)origin_u+(float)u)/(float)fw,((float)origin_v+(float)v)/(float)fh));
349 a.set_a(1.0f-blurred.linear_sample(((float)origin_u+(float)u)/fw,((float)origin_v+(float)v)/(float)fh));
351 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
353 (*surface)[y][x]=Color::blend(a,(*surface)[y][x],get_amount(),get_blend_method());
360 if(cb && !cb->amount_complete(10000,10000))
362 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
370 Layer_Shade::get_param_vocab(void)const
372 Layer::Vocab ret(Layer_Composite::get_param_vocab());
374 ret.push_back(ParamDesc("color")
375 .set_local_name(_("Color"))
377 ret.push_back(ParamDesc("origin")
378 .set_local_name(_("Origin"))
380 ret.push_back(ParamDesc("size")
381 .set_local_name(_("Size"))
382 .set_description(_("Size of Shade"))
384 .set_origin("origin")
386 ret.push_back(ParamDesc("type")
387 .set_local_name(_("Type"))
388 .set_description(_("Type of blur to use"))
390 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
391 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
392 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
393 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
394 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
397 ret.push_back(ParamDesc("invert")
398 .set_local_name(_("Invert"))
405 Layer_Shade::get_full_bounding_rect(Context context)const
408 return context.get_full_bounding_rect();
411 return Rect::full_plane();
413 Rect under(context.get_full_bounding_rect());
415 if(Color::is_onto(get_blend_method()))
418 Rect bounds((under+origin).expand_x(size[0]).expand_y(size[1]));