1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Circle" 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 ======================================================= */
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>
47 using namespace synfig;
51 /* -- G L O B A L S --------------------------------------------------------- */
53 SYNFIG_LAYER_INIT(Circle);
54 SYNFIG_LAYER_SET_NAME(Circle,"circle");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Circle,N_("Circle"));
56 SYNFIG_LAYER_SET_CATEGORY(Circle,N_("Geometry"));
57 SYNFIG_LAYER_SET_VERSION(Circle,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(Circle,"$Id$");
60 /* -- F U N C T I O N S ----------------------------------------------------- */
63 Layer_Composite (1.0,Color::BLEND_COMPOSITE),
64 color (Color::black()),
69 falloff (FALLOFF_INTERPOLATION_LINEAR)
72 Layer::Vocab voc(get_param_vocab());
73 Layer::fill_static(voc);
77 Circle::ImportParameters(const String ¶m, const ValueBase &value)
79 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
80 set_blend_method(Color::BLEND_ALPHA_OVER);
81 color.set_a(1); } else transparent_color_ = true; } });
83 IMPORT_PLUS(feather, if(feather<0)feather=0;);
88 IMPORT_AS(origin,"pos");
90 return Layer_Composite::set_param(param,value);
94 Circle::set_param(const String ¶m, const ValueBase &value)
96 if(ImportParameters(param,value))
106 Circle::get_param(const String ¶m)const
118 return Layer_Composite::get_param(param);
123 Circle::set_param_static(const String ¶m, const bool x)
125 return Layer_Composite::set_param_static(param, x);
130 Circle::get_param_static(const String ¶m) const
132 return Layer_Composite::get_param_static(param);
137 Circle::get_param_vocab()const
139 Layer::Vocab ret(Layer_Composite::get_param_vocab());
141 ret.push_back(ParamDesc("color")
142 .set_local_name(_("Color"))
144 ret.push_back(ParamDesc("radius")
145 .set_local_name(_("Radius"))
146 .set_origin("origin")
149 ret.push_back(ParamDesc("feather")
150 .set_local_name(_("Feather"))
153 ret.push_back(ParamDesc("origin")
154 .set_local_name(_("Origin"))
156 ret.push_back(ParamDesc("invert")
157 .set_local_name(_("Invert"))
158 .set_description(_("Invert the circle"))
161 ret.push_back(ParamDesc("falloff")
162 .set_local_name(_("Falloff"))
163 .set_description(_("Determines the falloff function for the feather"))
165 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
166 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
167 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
168 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
169 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
175 synfig::Layer::Handle
176 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
178 Point temp=origin-point;
181 return context.hit_check(point);
183 bool in_circle(temp.mag_squared() <= radius*radius);
187 in_circle=!in_circle;
188 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
193 if(get_amount()-(feather/radius)<=0.0)
199 synfig::Layer::Handle tmp;
200 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
202 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
204 return const_cast<Circle*>(this);
207 return context.hit_check(point);
211 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
213 //squared proportional falloff
214 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
217 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
219 //squared proportional falloff
220 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
224 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
226 //linear distance falloff
227 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
228 //then take the square root of it
233 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
235 //linear distance falloff
236 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
237 //then take the square root of it
238 ret = 1.0 - sqrt(ret);
242 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
244 //linear distance falloff
245 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
248 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
250 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
251 //linear distance falloff
254 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
256 //linear distance falloff
257 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
258 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
259 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
260 ret = 1.0 / (1 + exp(-(ret*10-5)) );
264 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
266 //linear distance falloff
267 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
268 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
269 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
270 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
276 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
278 //Cosine distance falloff
279 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
283 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
285 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
286 //Cosine distance falloff
289 void Circle::constructcache()
291 cache.inner_radius = radius - feather;
292 if(cache.inner_radius < 0)
293 cache.inner_radius = 0;
295 cache.outer_radius = radius + feather;
297 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
298 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
300 cache.diff_sqd = feather*feather*4.0;
301 cache.double_feather = feather*2.0;
303 falloff_func = GetFalloffFunc();
306 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
310 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
312 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
314 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
316 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
319 default: return invert?InvCosineFalloff:CosineFalloff;
324 Circle::get_color(Context context, const Point &point)const
326 if(is_disabled() || (radius==0 && invert==false && !feather))
327 return context.get_color(point);
330 Point temp=origin-point;
332 /*const Real inner_radius = radius-feather;
333 const Real outer_radius = radius+feather;
335 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
336 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
338 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
339 const Real double_feather = feather*2.0;*/
341 /*const Real &inner_radius = cache.inner_radius;
342 const Real &outer_radius = cache.outer_radius;*/
344 const Real &inner_radius_sqd = cache.inner_radius_sqd;
345 const Real &outer_radius_sqd = cache.outer_radius_sqd;
347 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
348 const Real &double_feather = cache.double_feather;*/
350 const Vector::value_type mag_squared = temp.mag_squared();
352 //Outside the circle, with feathering enabled
353 if( mag_squared > outer_radius_sqd )
355 // inverted -> outside == colored in
358 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
361 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
364 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
367 //inside the circle's solid area (with feathering)
368 else if(mag_squared <= inner_radius_sqd)
370 // !invert -> solid area
372 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
375 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
377 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
380 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
383 Color::value_type alpha;
388 case FALLOFF_SQUARED:
389 //squared proportional falloff
390 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
394 //linear distance falloff
395 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
396 //then take the square root of it
400 case FALLOFF_INTERPOLATION_LINEAR:
401 //linear distance falloff
402 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
405 case FALLOFF_SIGMOND:
407 //linear distance falloff
408 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
409 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
410 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
411 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
415 //If we're inverted, we need to invert the falloff value
419 alpha = falloff_func(cache,mag_squared);
421 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
425 Color NormalBlend(Color a, Color b, float amount)
427 return (b-a)*amount+a;
432 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
435 if(is_disabled() || (radius==0 && invert==false && !feather))
436 return context.accelerated_render(surface,quality, renddesc, cb);
438 // Another trivial case
439 if(invert && radius==0 && is_solid_color())
441 surface->set_wh(renddesc.get_w(),renddesc.get_h());
442 surface->fill(color);
443 if(cb && !cb->amount_complete(10000,10000))
449 const Point tl(renddesc.get_tl());
450 const Point br(renddesc.get_br());
451 const int w(renddesc.get_w());
452 const int h(renddesc.get_h());
454 const Real x_neg = tl[0] > br[0] ? -1 : 1;
455 const Real y_neg = tl[1] > br[1] ? -1 : 1;
457 // Width and Height of a pixel
458 const Real pw = (br[0] - tl[0]) / w;
459 const Real ph = (br[1] - tl[1]) / h;
461 // Increasing the feather amount by the size of
462 // a pixel will create an anti-aliased appearance
463 // don't render feathering at all when quality is 10
464 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
467 int left = (int) floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
468 int right = (int) ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
469 int top = (int) floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
470 int bottom = (int) ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
472 //clip the rectangle bounds
482 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
483 const Real outer_radius = radius+newfeather;
485 const Real inner_radius_sqd = inner_radius*inner_radius;
486 const Real outer_radius_sqd = outer_radius*outer_radius;
488 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
489 const Real double_feather = newfeather * 2.0;
491 //Compile the temporary cache for the falloff calculations
492 FALLOFF_FUNC *func = GetFalloffFunc();
494 const CircleDataCache cache =
496 inner_radius,outer_radius,
497 inner_radius_sqd,outer_radius_sqd,
498 diff_radii_sqd,double_feather
501 //info("Circle: Initialized everything");
503 //let the rendering begin
504 SuperCallback supercb(cb,0,9000,10000);
506 //if it's a degenerate circle, do what we need to do, and then leave
507 if(left >= right || top >= bottom)
511 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
513 surface->set_wh(w,h);
514 surface->fill(color);
518 // Render what is behind us
519 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
521 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
525 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
533 // Render what is behind us
534 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
536 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
543 if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
544 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
545 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
546 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
550 // Render what is behind us
551 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
553 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
558 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
560 surface->set_wh(w,h);
561 surface->fill(color);
567 //info("Circle: Non degenerate, rasterize %c", invert);
569 //we start in the middle of the left-top pixel
570 Real leftf = (left + 0.5)*pw + tl[0];
571 Real topf = (top + 0.5)*ph + tl[1];
573 //the looping variables
577 //Loop normally, since we are not inverted
580 // Render what is behind us
581 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
583 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
587 //make topf and leftf relative to the center of the circle
594 //Loop over the valid y-values in the bounding square
595 for(;j <= bottom; j++, y += ph)
600 //for each y-value, Loop over the bounding x-values in the bounding square
601 for(;i <= right; i++, x += pw)
603 //for each pixel, figure out the distance and blend
606 //if in the inner circle then the full color shows through
607 if(r <= inner_radius_sqd)
609 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
610 (*surface)[j][i]=color;
612 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
614 //if it's within the outer circle then it's in the feathering range
615 else if(r <= outer_radius_sqd)
621 case FALLOFF_SQUARED:
622 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
626 myamount = (outer_radius - sqrt(r)) / double_feather;
627 myamount = sqrt(myamount);
630 case FALLOFF_INTERPOLATION_LINEAR:
631 myamount = (outer_radius - sqrt(r)) / double_feather;
634 case FALLOFF_SIGMOND:
636 myamount = (outer_radius - sqrt(r)) / double_feather;
637 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
641 Real myamount = func(cache,r);
643 //if(myamount<0.0)myamount=0.0;
644 //if(myamount>1.0)myamount=1.0;
645 myamount *= get_amount();
646 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
654 RendDesc desc(renddesc);
657 int offset_x=0,offset_y=0;
659 //fill the surface with the background color initially
660 surface->set_wh(w,h);
661 surface->fill(color);
663 //then render the background to an alternate surface
664 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
669 //if there is no background showing through we are done
670 if(right < left || bottom < top)
673 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
675 // Render what is behind us
676 if(!context.accelerated_render(&background,quality,desc,&supercb))
678 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
689 leftf = /*0.5*pw +*/ tl[0];
690 topf = /*0.5*ph +*/ tl[1];
692 // Render what is behind us
693 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
695 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
706 for(;j <= bottom; j++, y+=ph)
711 for(;i <= right; i++, x+=pw)
713 Vector::value_type r = x*x + y*y;
715 if(r < inner_radius_sqd)
717 (*surface)[j][i] = background[j-offset_y][i-offset_x];
719 else if(r < outer_radius_sqd)
725 case FALLOFF_SQUARED:
726 amount = (r - inner_radius_sqd) / diff_radii_sqd;
728 case FALLOFF_INTERPOLATION_LINEAR:
729 amount = (sqrt(r) - inner_radius) / double_feather;
732 amount = (outer_radius - sqrt(r)) / double_feather;
733 amount = 1.0 - sqrt(amount);
735 case FALLOFF_SIGMOND:
737 amount = (outer_radius - sqrt(r)) / double_feather;
738 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
742 Real amount = func(cache,r);
744 if(amount<0.0)amount=0.0;
745 if(amount>1.0)amount=1.0;
747 amount*=get_amount();
749 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
750 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
752 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
758 // Mark our progress as finished
759 if(cb && !cb->amount_complete(10000,10000))
766 Circle::get_bounding_rect()const
769 return Rect::full_plane();
772 origin[0]+(radius+feather),
773 origin[1]+(radius+feather),
774 origin[0]-(radius+feather),
775 origin[1]-(radius+feather)
782 Circle::get_full_bounding_rect(Context context)const
786 if(is_solid_color() && color.get_a()==0)
789 origin[0]+(radius+feather),
790 origin[1]+(radius+feather),
791 origin[0]-(radius+feather),
792 origin[1]-(radius+feather)
794 return bounds & context.get_full_bounding_rect();
796 return Rect::full_plane();
799 return Layer_Composite::get_full_bounding_rect(context);