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
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
57 #define TYPE_GAUSSIAN 3
61 /* -- G L O B A L S --------------------------------------------------------- */
63 SYNFIG_LAYER_INIT(Layer_Shade);
64 SYNFIG_LAYER_SET_NAME(Layer_Shade,"shade");
65 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shade,N_("Shade"));
66 SYNFIG_LAYER_SET_CATEGORY(Layer_Shade,N_("Stylize"));
67 SYNFIG_LAYER_SET_VERSION(Layer_Shade,"0.2");
68 SYNFIG_LAYER_SET_CVS_ID(Layer_Shade,"$Id$");
70 /* -- F U N C T I O N S ----------------------------------------------------- */
72 inline void clamp(synfig::Vector &v)
78 Layer_Shade::Layer_Shade():
79 Layer_Composite (0.75,Color::BLEND_BEHIND),
81 type(Blur::FASTGAUSSIAN),
82 color(Color::black()),
89 Layer_Shade::set_param(const String ¶m, const ValueBase &value)
91 IMPORT_PLUS(size,clamp(size));
97 return Layer_Composite::set_param(param,value);
101 Layer_Shade::get_param(const String ¶m)const
112 return Layer_Composite::get_param(param);
116 Layer_Shade::get_color(Context context, const Point &pos)const
118 Point blurpos = Blur(size,type)(pos);
120 if(get_amount()==0.0)
121 return context.get_color(pos);
126 shade.set_a(context.get_color(blurpos-offset).get_a());
128 shade.set_a(1.0f-context.get_color(blurpos-offset).get_a());
130 return Color::blend(shade,context.get_color(pos),get_amount(),get_blend_method());
134 Layer_Shade::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
138 const int w = renddesc.get_w(),
139 h = renddesc.get_h();
140 const Real pw = renddesc.get_pw(),
141 ph = renddesc.get_ph();
143 RendDesc workdesc(renddesc);
145 etl::surface<float> blurred;
147 //expand the working surface to accommodate the blur
149 //the expanded size = 1/2 the size in each direction rounded up
150 int halfsizex = (int) (abs(size[0]*.5/pw) + 3),
151 halfsizey = (int) (abs(size[1]*.5/ph) + 3);
153 int offset_u(-round_to_int(offset[0]/pw)),offset_v(-round_to_int(offset[1]/ph));
155 int offset_w(w+abs(offset_u)),offset_h(h+abs(offset_v));
157 workdesc.set_subwindow(
158 offset_u<0?offset_u:0,
159 offset_v<0?offset_v:0,
160 (offset_u>0?offset_u:0)+w,
161 (offset_v>0?offset_v:0)+h
177 //expand by 1/2 size in each direction on either side
184 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
187 case Blur::FASTGAUSSIAN:
194 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
199 #define GAUSSIAN_ADJUSTMENT (0.05)
200 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
201 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
206 halfsizex = (int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
207 halfsizey = (int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
209 halfsizex = (halfsizex + 1)/2;
210 halfsizey = (halfsizey + 1)/2;
211 workdesc.set_subwindow( -halfsizex, -halfsizey, offset_w+2*halfsizex, offset_h+2*halfsizey );
216 #define SCALE_FACTOR (64.0)
217 if(/*quality>9 || */size[0]<=pw*SCALE_FACTOR)
219 SuperCallback stageone(cb,0,5000,10000);
220 SuperCallback stagetwo(cb,5000,10000,10000);
222 //callbacks depend on how long the blur takes
223 if(size[0] || size[1])
225 if(type == Blur::DISC)
227 stageone = SuperCallback(cb,0,5000,10000);
228 stagetwo = SuperCallback(cb,5000,10000,10000);
232 stageone = SuperCallback(cb,0,9000,10000);
233 stagetwo = SuperCallback(cb,9000,10000,10000);
238 stageone = SuperCallback(cb,0,9999,10000);
239 stagetwo = SuperCallback(cb,9999,10000,10000);
244 //render the background onto the expanded surface
245 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
248 // Copy over the alpha
249 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
250 for(int j=0;j<worksurface.get_h();j++)
251 for(int i=0;i<worksurface.get_w();i++)
253 blurred[j][i]=worksurface[j][i].get_a();
257 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
259 //be sure the surface is of the correct size
260 surface->set_wh(renddesc.get_w(),renddesc.get_h());
262 int u = halfsizex-(offset_u<0?offset_u:0), v = halfsizey-(offset_v<0?offset_v:0);
263 for(y=0;y<renddesc.get_h();y++,v++)
265 u = halfsizex-(offset_u<0?offset_u:0);
266 for(x=0;x<renddesc.get_w();x++,u++)
271 a.set_a(blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
273 a.set_a(1.0f-blurred.linear_sample(offset_u+(float)u,offset_v+(float)v));
275 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
277 (*surface)[y][x]=Color::blend(a,worksurface[v][u],get_amount(),get_blend_method());
279 else (*surface)[y][x] = worksurface[v][u];
286 SuperCallback stageone(cb,0,5000,10000);
287 SuperCallback stagetwo(cb,5000,10000,10000);
289 //callbacks depend on how long the blur takes
290 if(size[0] || size[1])
292 if(type == Blur::DISC)
294 stageone = SuperCallback(cb,0,5000,10000);
295 stagetwo = SuperCallback(cb,5000,10000,10000);
299 stageone = SuperCallback(cb,0,9000,10000);
300 stagetwo = SuperCallback(cb,9000,10000,10000);
305 stageone = SuperCallback(cb,0,9999,10000);
306 stagetwo = SuperCallback(cb,9999,10000,10000);
309 int fw(floor_to_int(abs(size[0]/(pw*SCALE_FACTOR)))+1);
310 int fh(floor_to_int(abs(size[1]/(ph*SCALE_FACTOR)))+1);
311 int tmpw(round_to_int((float)workdesc.get_w()/fw)),tmph(round_to_int((float)workdesc.get_h()/fh));
313 workdesc.clear_flags();
314 workdesc.set_wh(tmpw,tmph);
315 //synfig::info("fw: %d, fh: %d",fw,fh);
317 //render the blur fodder
318 if(!context.accelerated_render(&worksurface,quality,workdesc,&stageone))
321 //render the background
322 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
325 // Copy over the alpha
326 blurred.set_wh(worksurface.get_w(),worksurface.get_h());
327 for(int j=0;j<worksurface.get_h();j++)
328 for(int i=0;i<worksurface.get_w();i++)
329 blurred[j][i]=worksurface[j][i].get_a();
332 Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
335 int u = halfsizex-(offset_u<0?offset_u:0), v = halfsizey-(offset_v<0?offset_v:0);
336 for(y=0;y<renddesc.get_h();y++,v++)
338 u = halfsizex-(offset_u<0?offset_u:0);
339 for(x=0;x<renddesc.get_w();x++,u++)
344 a.set_a(blurred.linear_sample(((float)offset_u+(float)u)/(float)fw,((float)offset_v+(float)v)/(float)fh));
346 a.set_a(1.0f-blurred.linear_sample(((float)offset_u+(float)u)/fw,((float)offset_v+(float)v)/(float)fh));
348 if(a.get_a() || get_blend_method()==Color::BLEND_STRAIGHT)
350 (*surface)[y][x]=Color::blend(a,(*surface)[y][x],get_amount(),get_blend_method());
357 if(cb && !cb->amount_complete(10000,10000))
359 //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
367 Layer_Shade::get_param_vocab(void)const
369 Layer::Vocab ret(Layer_Composite::get_param_vocab());
371 ret.push_back(ParamDesc("color")
372 .set_local_name(_("Color"))
374 ret.push_back(ParamDesc("offset")
375 .set_local_name(_("Offset"))
377 ret.push_back(ParamDesc("size")
378 .set_local_name(_("Size"))
379 .set_description(_("Size of Shade"))
381 .set_origin("offset")
383 ret.push_back(ParamDesc("type")
384 .set_local_name(_("Type"))
385 .set_description(_("Type of blur to use"))
387 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
388 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
389 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
390 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
391 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
394 ret.push_back(ParamDesc("invert")
395 .set_local_name(_("Invert"))
402 Layer_Shade::get_full_bounding_rect(Context context)const
405 return context.get_full_bounding_rect();
408 return Rect::full_plane();
410 Rect under(context.get_full_bounding_rect());
412 if(Color::is_onto(get_blend_method()))
415 Rect bounds((under+offset).expand_x(size[0]).expand_y(size[1]));