1 /* === S Y N F I G ========================================================= */
3 ** \brief Template Header
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 ======================================================= */
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
46 using namespace synfig;
50 /* -- G L O B A L S --------------------------------------------------------- */
52 SYNFIG_LAYER_INIT(Circle);
53 SYNFIG_LAYER_SET_NAME(Circle,"circle");
54 SYNFIG_LAYER_SET_LOCAL_NAME(Circle,_("Circle"));
55 SYNFIG_LAYER_SET_CATEGORY(Circle,_("Geometry"));
56 SYNFIG_LAYER_SET_VERSION(Circle,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(Circle,"$Id$");
59 /* -- F U N C T I O N S ----------------------------------------------------- */
62 Layer_Composite (1.0,Color::BLEND_STRAIGHT),
63 color (Color::black()),
68 falloff (FALLOFF_INTERPOLATION_LINEAR)
74 Circle::ImportParameters(const String ¶m, const ValueBase &value)
83 return Layer_Composite::set_param(param,value);
87 Circle::set_param(const String ¶m, const ValueBase &value)
89 if(ImportParameters(param,value))
99 Circle::get_param(const String ¶m)const
111 return Layer_Composite::get_param(param);
115 Circle::get_param_vocab()const
117 Layer::Vocab ret(Layer_Composite::get_param_vocab());
119 ret.push_back(ParamDesc("color")
120 .set_local_name(_("Color"))
122 ret.push_back(ParamDesc("radius")
123 .set_local_name(_("Radius"))
127 ret.push_back(ParamDesc("feather")
128 .set_local_name(_("Feather"))
131 ret.push_back(ParamDesc("pos")
132 .set_local_name(_("Center"))
134 ret.push_back(ParamDesc("invert")
135 .set_local_name(_("Invert"))
136 .set_description(_("Invert the circle"))
139 ret.push_back(ParamDesc("falloff")
140 .set_local_name(_("Falloff"))
141 .set_description(_("Determines the falloff function for the feather"))
143 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
144 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
145 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
146 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
147 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
153 synfig::Layer::Handle
154 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
156 Point temp=pos-point;
159 return context.hit_check(point);
161 bool in_circle(temp.mag_squared() <= radius*radius);
165 in_circle=!in_circle;
166 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
171 if(get_amount()-(feather/radius)<=0.0)
177 synfig::Layer::Handle tmp;
178 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
180 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
182 return const_cast<Circle*>(this);
185 return context.hit_check(point);
189 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
191 //squared proportional falloff
192 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
195 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
197 //squared proportional falloff
198 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
202 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
204 //linear distance falloff
205 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
206 //then take the square root of it
211 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
213 //linear distance falloff
214 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
215 //then take the square root of it
216 ret = 1.0 - sqrt(ret);
220 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
222 //linear distance falloff
223 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
226 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
228 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
229 //linear distance falloff
232 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
234 //linear distance falloff
235 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
236 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
237 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
238 ret = 1.0 / (1 + exp(-(ret*10-5)) );
242 Real Circle::InvSigmondFalloff(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 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
247 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
248 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
254 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
256 //Cosine distance falloff
257 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
261 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
263 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
264 //Cosine distance falloff
267 void Circle::constructcache()
269 cache.inner_radius = radius - feather;
270 if(cache.inner_radius < 0)
271 cache.inner_radius = 0;
273 cache.outer_radius = radius + feather;
275 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
276 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
278 cache.diff_sqd = feather*feather*4.0;
279 cache.double_feather = feather*2.0;
281 falloff_func = GetFalloffFunc();
284 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
288 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
290 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
292 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
294 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
297 default: return invert?InvCosineFalloff:CosineFalloff;
302 Circle::get_color(Context context, const Point &point)const
304 if(radius==0 || is_disabled())
305 return context.get_color(point);
308 Point temp=pos-point;
310 /*const Real inner_radius = radius-feather;
311 const Real outer_radius = radius+feather;
313 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
314 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
316 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
317 const Real double_feather = feather*2.0;*/
319 /*const Real &inner_radius = cache.inner_radius;
320 const Real &outer_radius = cache.outer_radius;*/
322 const Real &inner_radius_sqd = cache.inner_radius_sqd;
323 const Real &outer_radius_sqd = cache.outer_radius_sqd;
325 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
326 const Real &double_feather = cache.double_feather;*/
328 const Vector::value_type mag_squared = temp.mag_squared();
330 //Outside the circle, with feathering enabled
331 if( mag_squared > outer_radius_sqd )
333 // inverted -> outside == colored in
336 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
339 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
342 return context.get_color(point);
345 //inside the circle's solid area (with feathering)
346 else if(mag_squared <= inner_radius_sqd)
348 // !invert -> solid area
350 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
353 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
355 return context.get_color(point);
358 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
361 Color::value_type alpha;
366 case FALLOFF_SQUARED:
367 //squared proportional falloff
368 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
372 //linear distance falloff
373 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
374 //then take the square root of it
378 case FALLOFF_INTERPOLATION_LINEAR:
379 //linear distance falloff
380 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
383 case FALLOFF_SIGMOND:
385 //linear distance falloff
386 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
387 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
388 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
389 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
393 //If we're inverted, we need to invert the falloff value
397 alpha = falloff_func(cache,mag_squared);
399 //Compose falloff value with amount from the composite layer, and that is the blend value
400 alpha *= get_amount();
402 return Color::blend(color,context.get_color(point),alpha,get_blend_method());
406 Color NormalBlend(Color a, Color b, float amount)
408 return (b-a)*amount+a;
413 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
416 if(is_disabled() || (radius==0 && invert==false))
417 return context.accelerated_render(surface,quality, renddesc, cb);
419 // Another trivial case
420 if(invert && radius==0 && is_solid_color())
422 surface->set_wh(renddesc.get_w(),renddesc.get_h());
423 surface->fill(color);
424 if(cb && !cb->amount_complete(10000,10000))
430 const Point tl(renddesc.get_tl());
431 const Point br(renddesc.get_br());
432 const int w(renddesc.get_w());
433 const int h(renddesc.get_h());
435 const Real x_neg = tl[0] > br[0] ? -1 : 1;
436 const Real y_neg = tl[1] > br[1] ? -1 : 1;
438 // Width and Height of a pixel
439 const Real pw = (br[0] - tl[0]) / w;
440 const Real ph = (br[1] - tl[1]) / h;
442 // Increasing the feather amount by the size of
443 // a pixel will create an anti-aliased appearance
444 const Real newfeather=feather + (abs(ph)+abs(pw))/4.0;
447 int left = (int) floor( (pos[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
448 int right = (int) ceil( (pos[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
449 int top = (int) floor( (pos[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
450 int bottom = (int) ceil( (pos[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
452 //clip the rectangle bounds
462 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
463 const Real outer_radius = radius+newfeather;
465 const Real inner_radius_sqd = inner_radius*inner_radius;
466 const Real outer_radius_sqd = outer_radius*outer_radius;
468 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
469 const Real double_feather = newfeather * 2.0;
471 //Compile the temporary cache for the falloff calculations
472 FALLOFF_FUNC *func = GetFalloffFunc();
474 const CircleDataCache cache =
476 inner_radius,outer_radius,
477 inner_radius_sqd,outer_radius_sqd,
478 diff_radii_sqd,double_feather
481 //info("Circle: Initialized everything");
483 //let the rendering begin
484 SuperCallback supercb(cb,0,9000,10000);
486 //if it's a degenerate circle, do what we need to do, and then leave
487 if(left >= right || top >= bottom)
491 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
493 surface->set_wh(w,h);
494 surface->fill(color);
498 // Render what is behind us
499 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
501 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
505 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
513 // Render what is behind us
514 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
516 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
523 if( (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd
524 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
525 && (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
526 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd )
530 // Render what is behind us
531 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
533 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
538 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
540 surface->set_wh(w,h);
541 surface->fill(color);
547 //info("Circle: Non degenerate, rasterize %c", invert);
549 //we start in the middle of the left-top pixel
550 Real leftf = (left + 0.5)*pw + tl[0];
551 Real topf = (top + 0.5)*ph + tl[1];
553 //the looping variables
557 //Loop normally, since we are not inverted
560 // Render what is behind us
561 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
563 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
567 //make topf and leftf relative to the center of the circle
574 //Loop over the valid y-values in the bounding square
575 for(;j <= bottom; j++, y += ph)
580 //for each y-value, Loop over the bounding x-values in the bounding square
581 for(;i <= right; i++, x += pw)
583 //for each pixel, figure out the distance and blend
586 //if in the inner circle then the full color shows through
587 if(r <= inner_radius_sqd)
589 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
590 (*surface)[j][i]=color;
592 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
594 //if it's within the outer circle then it's in the feathering range
595 else if(r <= outer_radius_sqd)
601 case FALLOFF_SQUARED:
602 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
606 myamount = (outer_radius - sqrt(r)) / double_feather;
607 myamount = sqrt(myamount);
610 case FALLOFF_INTERPOLATION_LINEAR:
611 myamount = (outer_radius - sqrt(r)) / double_feather;
614 case FALLOFF_SIGMOND:
616 myamount = (outer_radius - sqrt(r)) / double_feather;
617 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
621 Real myamount = func(cache,r);
623 //if(myamount<0.0)myamount=0.0;
624 //if(myamount>1.0)myamount=1.0;
625 myamount *= get_amount();
626 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
634 RendDesc desc(renddesc);
637 int offset_x=0,offset_y=0;
639 //fill the surface with the background color initially
640 surface->set_wh(w,h);
641 surface->fill(color);
643 //then render the background to an alternate surface
644 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
649 //if there is no background showing through we are done
650 if(right < left || bottom < top)
653 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
655 // Render what is behind us
656 if(!context.accelerated_render(&background,quality,desc,&supercb))
658 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
669 leftf = /*0.5*pw +*/ tl[0];
670 topf = /*0.5*ph +*/ tl[1];
672 // Render what is behind us
673 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
675 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
686 for(;j <= bottom; j++, y+=ph)
691 for(;i <= right; i++, x+=pw)
693 Vector::value_type r = x*x + y*y;
695 if(r < inner_radius_sqd)
697 (*surface)[j][i] = background[j-offset_y][i-offset_x];
699 else if(r < outer_radius_sqd)
705 case FALLOFF_SQUARED:
706 amount = (r - inner_radius_sqd) / diff_radii_sqd;
708 case FALLOFF_INTERPOLATION_LINEAR:
709 amount = (sqrt(r) - inner_radius) / double_feather;
712 amount = (outer_radius - sqrt(r)) / double_feather;
713 amount = 1.0 - sqrt(amount);
715 case FALLOFF_SIGMOND:
717 amount = (outer_radius - sqrt(r)) / double_feather;
718 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
722 Real amount = func(cache,r);
724 if(amount<0.0)amount=0.0;
725 if(amount>1.0)amount=1.0;
727 amount*=get_amount();
729 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
730 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
732 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
738 // Mark our progress as finished
739 if(cb && !cb->amount_complete(10000,10000))
746 Circle::get_bounding_rect()const
749 return Rect::full_plane();
752 pos[0]+(radius+feather),
753 pos[1]+(radius+feather),
754 pos[0]-(radius+feather),
755 pos[1]-(radius+feather)
762 Circle::get_full_bounding_rect(Context context)const
766 if(is_solid_color() && color.get_a()==0)
769 pos[0]+(radius+feather),
770 pos[1]+(radius+feather),
771 pos[0]-(radius+feather),
772 pos[1]-(radius+feather)
774 return bounds & context.get_full_bounding_rect();
776 return Rect::full_plane();
779 return Layer_Composite::get_full_bounding_rect(context);