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);
122 Circle::get_param_vocab()const
124 Layer::Vocab ret(Layer_Composite::get_param_vocab());
126 ret.push_back(ParamDesc("color")
127 .set_local_name(_("Color"))
128 .set_description(_("Fill color of the layer"))
130 ret.push_back(ParamDesc("radius")
131 .set_local_name(_("Radius"))
132 .set_origin("origin")
133 .set_description(_("Radius of the circle"))
136 ret.push_back(ParamDesc("feather")
137 .set_local_name(_("Feather"))
139 .set_description(_("Amount of feather of the circle"))
141 ret.push_back(ParamDesc("origin")
142 .set_local_name(_("Origin"))
143 .set_description(_("Center of the circle"))
145 ret.push_back(ParamDesc("invert")
146 .set_local_name(_("Invert"))
147 .set_description(_("Invert the circle"))
150 ret.push_back(ParamDesc("falloff")
151 .set_local_name(_("Falloff"))
152 .set_description(_("Determines the falloff function for the feather"))
154 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
155 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
156 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
157 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
158 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
164 synfig::Layer::Handle
165 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
167 Point temp=origin-point;
170 return context.hit_check(point);
172 bool in_circle(temp.mag_squared() <= radius*radius);
176 in_circle=!in_circle;
177 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
182 if(get_amount()-(feather/radius)<=0.0)
188 synfig::Layer::Handle tmp;
189 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
191 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
193 return const_cast<Circle*>(this);
196 return context.hit_check(point);
200 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
202 //squared proportional falloff
203 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
206 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
208 //squared proportional falloff
209 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
213 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
215 //linear distance falloff
216 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
217 //then take the square root of it
222 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
224 //linear distance falloff
225 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
226 //then take the square root of it
227 ret = 1.0 - sqrt(ret);
231 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
233 //linear distance falloff
234 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
237 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
239 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
240 //linear distance falloff
243 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
245 //linear distance falloff
246 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
247 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
248 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
249 ret = 1.0 / (1 + exp(-(ret*10-5)) );
253 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
255 //linear distance falloff
256 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
257 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
258 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
259 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
265 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
267 //Cosine distance falloff
268 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
272 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
274 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
275 //Cosine distance falloff
278 void Circle::constructcache()
280 cache.inner_radius = radius - feather;
281 if(cache.inner_radius < 0)
282 cache.inner_radius = 0;
284 cache.outer_radius = radius + feather;
286 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
287 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
289 cache.diff_sqd = feather*feather*4.0;
290 cache.double_feather = feather*2.0;
292 falloff_func = GetFalloffFunc();
295 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
299 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
301 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
303 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
305 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
308 default: return invert?InvCosineFalloff:CosineFalloff;
313 Circle::get_color(Context context, const Point &point)const
315 if(is_disabled() || (radius==0 && invert==false && !feather))
316 return context.get_color(point);
319 Point temp=origin-point;
321 /*const Real inner_radius = radius-feather;
322 const Real outer_radius = radius+feather;
324 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
325 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
327 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
328 const Real double_feather = feather*2.0;*/
330 /*const Real &inner_radius = cache.inner_radius;
331 const Real &outer_radius = cache.outer_radius;*/
333 const Real &inner_radius_sqd = cache.inner_radius_sqd;
334 const Real &outer_radius_sqd = cache.outer_radius_sqd;
336 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
337 const Real &double_feather = cache.double_feather;*/
339 const Vector::value_type mag_squared = temp.mag_squared();
341 //Outside the circle, with feathering enabled
342 if( mag_squared > outer_radius_sqd )
344 // inverted -> outside == colored in
347 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
350 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
353 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
356 //inside the circle's solid area (with feathering)
357 else if(mag_squared <= inner_radius_sqd)
359 // !invert -> solid area
361 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
364 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
366 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
369 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
372 Color::value_type alpha;
377 case FALLOFF_SQUARED:
378 //squared proportional falloff
379 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
383 //linear distance falloff
384 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
385 //then take the square root of it
389 case FALLOFF_INTERPOLATION_LINEAR:
390 //linear distance falloff
391 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
394 case FALLOFF_SIGMOND:
396 //linear distance falloff
397 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
398 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
399 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
400 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
404 //If we're inverted, we need to invert the falloff value
408 alpha = falloff_func(cache,mag_squared);
410 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
414 Color NormalBlend(Color a, Color b, float amount)
416 return (b-a)*amount+a;
421 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
424 if(is_disabled() || (radius==0 && invert==false && !feather))
425 return context.accelerated_render(surface,quality, renddesc, cb);
427 // Another trivial case
428 if(invert && radius==0 && is_solid_color())
430 surface->set_wh(renddesc.get_w(),renddesc.get_h());
431 surface->fill(color);
432 if(cb && !cb->amount_complete(10000,10000))
438 const Point tl(renddesc.get_tl());
439 const Point br(renddesc.get_br());
440 const int w(renddesc.get_w());
441 const int h(renddesc.get_h());
443 const Real x_neg = tl[0] > br[0] ? -1 : 1;
444 const Real y_neg = tl[1] > br[1] ? -1 : 1;
446 // Width and Height of a pixel
447 const Real pw = (br[0] - tl[0]) / w;
448 const Real ph = (br[1] - tl[1]) / h;
450 // Increasing the feather amount by the size of
451 // a pixel will create an anti-aliased appearance
452 // don't render feathering at all when quality is 10
453 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
456 int left = (int) floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
457 int right = (int) ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
458 int top = (int) floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
459 int bottom = (int) ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
461 //clip the rectangle bounds
471 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
472 const Real outer_radius = radius+newfeather;
474 const Real inner_radius_sqd = inner_radius*inner_radius;
475 const Real outer_radius_sqd = outer_radius*outer_radius;
477 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
478 const Real double_feather = newfeather * 2.0;
480 //Compile the temporary cache for the falloff calculations
481 FALLOFF_FUNC *func = GetFalloffFunc();
483 const CircleDataCache cache =
485 inner_radius,outer_radius,
486 inner_radius_sqd,outer_radius_sqd,
487 diff_radii_sqd,double_feather
490 //info("Circle: Initialized everything");
492 //let the rendering begin
493 SuperCallback supercb(cb,0,9000,10000);
495 //if it's a degenerate circle, do what we need to do, and then leave
496 if(left >= right || top >= bottom)
500 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
502 surface->set_wh(w,h);
503 surface->fill(color);
507 // Render what is behind us
508 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
510 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
514 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
522 // Render what is behind us
523 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
525 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
532 if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
533 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
534 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
535 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
539 // Render what is behind us
540 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
542 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
547 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
549 surface->set_wh(w,h);
550 surface->fill(color);
556 //info("Circle: Non degenerate, rasterize %c", invert);
558 //we start in the middle of the left-top pixel
559 Real leftf = (left + 0.5)*pw + tl[0];
560 Real topf = (top + 0.5)*ph + tl[1];
562 //the looping variables
566 //Loop normally, since we are not inverted
569 // Render what is behind us
570 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
572 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
576 //make topf and leftf relative to the center of the circle
583 //Loop over the valid y-values in the bounding square
584 for(;j <= bottom; j++, y += ph)
589 //for each y-value, Loop over the bounding x-values in the bounding square
590 for(;i <= right; i++, x += pw)
592 //for each pixel, figure out the distance and blend
595 //if in the inner circle then the full color shows through
596 if(r <= inner_radius_sqd)
598 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
599 (*surface)[j][i]=color;
601 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
603 //if it's within the outer circle then it's in the feathering range
604 else if(r <= outer_radius_sqd)
610 case FALLOFF_SQUARED:
611 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
615 myamount = (outer_radius - sqrt(r)) / double_feather;
616 myamount = sqrt(myamount);
619 case FALLOFF_INTERPOLATION_LINEAR:
620 myamount = (outer_radius - sqrt(r)) / double_feather;
623 case FALLOFF_SIGMOND:
625 myamount = (outer_radius - sqrt(r)) / double_feather;
626 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
630 Real myamount = func(cache,r);
632 //if(myamount<0.0)myamount=0.0;
633 //if(myamount>1.0)myamount=1.0;
634 myamount *= get_amount();
635 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
643 RendDesc desc(renddesc);
646 int offset_x=0,offset_y=0;
648 //fill the surface with the background color initially
649 surface->set_wh(w,h);
650 surface->fill(color);
652 //then render the background to an alternate surface
653 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
658 //if there is no background showing through we are done
659 if(right < left || bottom < top)
662 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
664 // Render what is behind us
665 if(!context.accelerated_render(&background,quality,desc,&supercb))
667 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
678 leftf = /*0.5*pw +*/ tl[0];
679 topf = /*0.5*ph +*/ tl[1];
681 // Render what is behind us
682 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
684 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
695 for(;j <= bottom; j++, y+=ph)
700 for(;i <= right; i++, x+=pw)
702 Vector::value_type r = x*x + y*y;
704 if(r < inner_radius_sqd)
706 (*surface)[j][i] = background[j-offset_y][i-offset_x];
708 else if(r < outer_radius_sqd)
714 case FALLOFF_SQUARED:
715 amount = (r - inner_radius_sqd) / diff_radii_sqd;
717 case FALLOFF_INTERPOLATION_LINEAR:
718 amount = (sqrt(r) - inner_radius) / double_feather;
721 amount = (outer_radius - sqrt(r)) / double_feather;
722 amount = 1.0 - sqrt(amount);
724 case FALLOFF_SIGMOND:
726 amount = (outer_radius - sqrt(r)) / double_feather;
727 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
731 Real amount = func(cache,r);
733 if(amount<0.0)amount=0.0;
734 if(amount>1.0)amount=1.0;
736 amount*=get_amount();
738 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
739 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
741 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
747 // Mark our progress as finished
748 if(cb && !cb->amount_complete(10000,10000))
755 Circle::get_bounding_rect()const
758 return Rect::full_plane();
761 origin[0]+(radius+feather),
762 origin[1]+(radius+feather),
763 origin[0]-(radius+feather),
764 origin[1]-(radius+feather)
771 Circle::get_full_bounding_rect(Context context)const
775 if(is_solid_color() && color.get_a()==0)
778 origin[0]+(radius+feather),
779 origin[1]+(radius+feather),
780 origin[0]-(radius+feather),
781 origin[1]-(radius+feather)
783 return bounds & context.get_full_bounding_rect();
785 return Rect::full_plane();
788 return Layer_Composite::get_full_bounding_rect(context);