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),
71 origin_static (false),
72 radius_static (false),
73 feather_static (false),
81 Circle::ImportParameters(const String ¶m, const ValueBase &value)
83 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
84 set_blend_method(Color::BLEND_ALPHA_OVER);
85 color.set_a(1); } else transparent_color_ = true; } });
87 IMPORT_PLUS(feather, if(feather<0)feather=0;);
92 IMPORT_AS(origin,"pos");
94 return Layer_Composite::set_param(param,value);
98 Circle::set_param(const String ¶m, const ValueBase &value)
100 if(ImportParameters(param,value))
110 Circle::get_param(const String ¶m)const
122 return Layer_Composite::get_param(param);
127 Circle::set_param_static(const String ¶m, const bool x)
131 SET_STATIC(radius, x)
132 SET_STATIC(origin, x)
133 SET_STATIC(feather, x)
134 SET_STATIC(falloff, x)
136 return Layer_Composite::set_param_static(param, x);
141 Circle::get_param_static(const String ¶m) const
150 return Layer_Composite::get_param_static(param);
155 Circle::get_param_vocab()const
157 Layer::Vocab ret(Layer_Composite::get_param_vocab());
159 ret.push_back(ParamDesc("color")
160 .set_local_name(_("Color"))
162 ret.push_back(ParamDesc("radius")
163 .set_local_name(_("Radius"))
164 .set_origin("origin")
167 ret.push_back(ParamDesc("feather")
168 .set_local_name(_("Feather"))
171 ret.push_back(ParamDesc("origin")
172 .set_local_name(_("Origin"))
174 ret.push_back(ParamDesc("invert")
175 .set_local_name(_("Invert"))
176 .set_description(_("Invert the circle"))
179 ret.push_back(ParamDesc("falloff")
180 .set_local_name(_("Falloff"))
181 .set_description(_("Determines the falloff function for the feather"))
183 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
184 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
185 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
186 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
187 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
193 synfig::Layer::Handle
194 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
196 Point temp=origin-point;
199 return context.hit_check(point);
201 bool in_circle(temp.mag_squared() <= radius*radius);
205 in_circle=!in_circle;
206 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
211 if(get_amount()-(feather/radius)<=0.0)
217 synfig::Layer::Handle tmp;
218 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
220 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
222 return const_cast<Circle*>(this);
225 return context.hit_check(point);
229 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
231 //squared proportional falloff
232 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
235 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
237 //squared proportional falloff
238 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
242 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
244 //linear distance falloff
245 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
246 //then take the square root of it
251 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
253 //linear distance falloff
254 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
255 //then take the square root of it
256 ret = 1.0 - sqrt(ret);
260 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
262 //linear distance falloff
263 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
266 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
268 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
269 //linear distance falloff
272 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
274 //linear distance falloff
275 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
276 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
277 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
278 ret = 1.0 / (1 + exp(-(ret*10-5)) );
282 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
284 //linear distance falloff
285 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
286 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
287 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
288 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
294 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
296 //Cosine distance falloff
297 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
301 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
303 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
304 //Cosine distance falloff
307 void Circle::constructcache()
309 cache.inner_radius = radius - feather;
310 if(cache.inner_radius < 0)
311 cache.inner_radius = 0;
313 cache.outer_radius = radius + feather;
315 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
316 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
318 cache.diff_sqd = feather*feather*4.0;
319 cache.double_feather = feather*2.0;
321 falloff_func = GetFalloffFunc();
324 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
328 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
330 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
332 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
334 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
337 default: return invert?InvCosineFalloff:CosineFalloff;
342 Circle::get_color(Context context, const Point &point)const
344 if(is_disabled() || (radius==0 && invert==false && !feather))
345 return context.get_color(point);
348 Point temp=origin-point;
350 /*const Real inner_radius = radius-feather;
351 const Real outer_radius = radius+feather;
353 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
354 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
356 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
357 const Real double_feather = feather*2.0;*/
359 /*const Real &inner_radius = cache.inner_radius;
360 const Real &outer_radius = cache.outer_radius;*/
362 const Real &inner_radius_sqd = cache.inner_radius_sqd;
363 const Real &outer_radius_sqd = cache.outer_radius_sqd;
365 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
366 const Real &double_feather = cache.double_feather;*/
368 const Vector::value_type mag_squared = temp.mag_squared();
370 //Outside the circle, with feathering enabled
371 if( mag_squared > outer_radius_sqd )
373 // inverted -> outside == colored in
376 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
379 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
382 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
385 //inside the circle's solid area (with feathering)
386 else if(mag_squared <= inner_radius_sqd)
388 // !invert -> solid area
390 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
393 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
395 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
398 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
401 Color::value_type alpha;
406 case FALLOFF_SQUARED:
407 //squared proportional falloff
408 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
412 //linear distance falloff
413 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
414 //then take the square root of it
418 case FALLOFF_INTERPOLATION_LINEAR:
419 //linear distance falloff
420 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
423 case FALLOFF_SIGMOND:
425 //linear distance falloff
426 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
427 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
428 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
429 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
433 //If we're inverted, we need to invert the falloff value
437 alpha = falloff_func(cache,mag_squared);
439 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
443 Color NormalBlend(Color a, Color b, float amount)
445 return (b-a)*amount+a;
450 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
453 if(is_disabled() || (radius==0 && invert==false && !feather))
454 return context.accelerated_render(surface,quality, renddesc, cb);
456 // Another trivial case
457 if(invert && radius==0 && is_solid_color())
459 surface->set_wh(renddesc.get_w(),renddesc.get_h());
460 surface->fill(color);
461 if(cb && !cb->amount_complete(10000,10000))
467 const Point tl(renddesc.get_tl());
468 const Point br(renddesc.get_br());
469 const int w(renddesc.get_w());
470 const int h(renddesc.get_h());
472 const Real x_neg = tl[0] > br[0] ? -1 : 1;
473 const Real y_neg = tl[1] > br[1] ? -1 : 1;
475 // Width and Height of a pixel
476 const Real pw = (br[0] - tl[0]) / w;
477 const Real ph = (br[1] - tl[1]) / h;
479 // Increasing the feather amount by the size of
480 // a pixel will create an anti-aliased appearance
481 // don't render feathering at all when quality is 10
482 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
485 int left = (int) floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
486 int right = (int) ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
487 int top = (int) floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
488 int bottom = (int) ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
490 //clip the rectangle bounds
500 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
501 const Real outer_radius = radius+newfeather;
503 const Real inner_radius_sqd = inner_radius*inner_radius;
504 const Real outer_radius_sqd = outer_radius*outer_radius;
506 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
507 const Real double_feather = newfeather * 2.0;
509 //Compile the temporary cache for the falloff calculations
510 FALLOFF_FUNC *func = GetFalloffFunc();
512 const CircleDataCache cache =
514 inner_radius,outer_radius,
515 inner_radius_sqd,outer_radius_sqd,
516 diff_radii_sqd,double_feather
519 //info("Circle: Initialized everything");
521 //let the rendering begin
522 SuperCallback supercb(cb,0,9000,10000);
524 //if it's a degenerate circle, do what we need to do, and then leave
525 if(left >= right || top >= bottom)
529 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
531 surface->set_wh(w,h);
532 surface->fill(color);
536 // Render what is behind us
537 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
539 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
543 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
551 // Render what is behind us
552 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
554 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
561 if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
562 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
563 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
564 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
568 // Render what is behind us
569 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
571 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
576 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
578 surface->set_wh(w,h);
579 surface->fill(color);
585 //info("Circle: Non degenerate, rasterize %c", invert);
587 //we start in the middle of the left-top pixel
588 Real leftf = (left + 0.5)*pw + tl[0];
589 Real topf = (top + 0.5)*ph + tl[1];
591 //the looping variables
595 //Loop normally, since we are not inverted
598 // Render what is behind us
599 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
601 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
605 //make topf and leftf relative to the center of the circle
612 //Loop over the valid y-values in the bounding square
613 for(;j <= bottom; j++, y += ph)
618 //for each y-value, Loop over the bounding x-values in the bounding square
619 for(;i <= right; i++, x += pw)
621 //for each pixel, figure out the distance and blend
624 //if in the inner circle then the full color shows through
625 if(r <= inner_radius_sqd)
627 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
628 (*surface)[j][i]=color;
630 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
632 //if it's within the outer circle then it's in the feathering range
633 else if(r <= outer_radius_sqd)
639 case FALLOFF_SQUARED:
640 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
644 myamount = (outer_radius - sqrt(r)) / double_feather;
645 myamount = sqrt(myamount);
648 case FALLOFF_INTERPOLATION_LINEAR:
649 myamount = (outer_radius - sqrt(r)) / double_feather;
652 case FALLOFF_SIGMOND:
654 myamount = (outer_radius - sqrt(r)) / double_feather;
655 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
659 Real myamount = func(cache,r);
661 //if(myamount<0.0)myamount=0.0;
662 //if(myamount>1.0)myamount=1.0;
663 myamount *= get_amount();
664 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
672 RendDesc desc(renddesc);
675 int offset_x=0,offset_y=0;
677 //fill the surface with the background color initially
678 surface->set_wh(w,h);
679 surface->fill(color);
681 //then render the background to an alternate surface
682 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
687 //if there is no background showing through we are done
688 if(right < left || bottom < top)
691 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
693 // Render what is behind us
694 if(!context.accelerated_render(&background,quality,desc,&supercb))
696 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
707 leftf = /*0.5*pw +*/ tl[0];
708 topf = /*0.5*ph +*/ tl[1];
710 // Render what is behind us
711 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
713 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
724 for(;j <= bottom; j++, y+=ph)
729 for(;i <= right; i++, x+=pw)
731 Vector::value_type r = x*x + y*y;
733 if(r < inner_radius_sqd)
735 (*surface)[j][i] = background[j-offset_y][i-offset_x];
737 else if(r < outer_radius_sqd)
743 case FALLOFF_SQUARED:
744 amount = (r - inner_radius_sqd) / diff_radii_sqd;
746 case FALLOFF_INTERPOLATION_LINEAR:
747 amount = (sqrt(r) - inner_radius) / double_feather;
750 amount = (outer_radius - sqrt(r)) / double_feather;
751 amount = 1.0 - sqrt(amount);
753 case FALLOFF_SIGMOND:
755 amount = (outer_radius - sqrt(r)) / double_feather;
756 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
760 Real amount = func(cache,r);
762 if(amount<0.0)amount=0.0;
763 if(amount>1.0)amount=1.0;
765 amount*=get_amount();
767 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
768 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
770 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
776 // Mark our progress as finished
777 if(cb && !cb->amount_complete(10000,10000))
784 Circle::get_bounding_rect()const
787 return Rect::full_plane();
790 origin[0]+(radius+feather),
791 origin[1]+(radius+feather),
792 origin[0]-(radius+feather),
793 origin[1]-(radius+feather)
800 Circle::get_full_bounding_rect(Context context)const
804 if(is_solid_color() && color.get_a()==0)
807 origin[0]+(radius+feather),
808 origin[1]+(radius+feather),
809 origin[0]-(radius+feather),
810 origin[1]-(radius+feather)
812 return bounds & context.get_full_bounding_rect();
814 return Rect::full_plane();
817 return Layer_Composite::get_full_bounding_rect(context);