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_STRAIGHT),
64 color (Color::black()),
69 falloff (FALLOFF_INTERPOLATION_LINEAR)
75 Circle::ImportParameters(const String ¶m, const ValueBase &value)
77 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
78 set_blend_method(Color::BLEND_ALPHA_OVER);
79 color.set_a(1); } else transparent_color_ = true; } });
81 IMPORT_PLUS(feather, if(feather<0)feather=0;);
86 return Layer_Composite::set_param(param,value);
90 Circle::set_param(const String ¶m, const ValueBase &value)
92 if(ImportParameters(param,value))
102 Circle::get_param(const String ¶m)const
114 return Layer_Composite::get_param(param);
118 Circle::get_param_vocab()const
120 Layer::Vocab ret(Layer_Composite::get_param_vocab());
122 ret.push_back(ParamDesc("color")
123 .set_local_name(_("Color"))
125 ret.push_back(ParamDesc("radius")
126 .set_local_name(_("Radius"))
130 ret.push_back(ParamDesc("feather")
131 .set_local_name(_("Feather"))
134 ret.push_back(ParamDesc("pos")
135 .set_local_name(_("Center"))
137 ret.push_back(ParamDesc("invert")
138 .set_local_name(_("Invert"))
139 .set_description(_("Invert the circle"))
142 ret.push_back(ParamDesc("falloff")
143 .set_local_name(_("Falloff"))
144 .set_description(_("Determines the falloff function for the feather"))
146 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
147 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
148 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
149 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
150 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
156 synfig::Layer::Handle
157 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
159 Point temp=pos-point;
162 return context.hit_check(point);
164 bool in_circle(temp.mag_squared() <= radius*radius);
168 in_circle=!in_circle;
169 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
174 if(get_amount()-(feather/radius)<=0.0)
180 synfig::Layer::Handle tmp;
181 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
183 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
185 return const_cast<Circle*>(this);
188 return context.hit_check(point);
192 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
194 //squared proportional falloff
195 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
198 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
200 //squared proportional falloff
201 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
205 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
207 //linear distance falloff
208 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
209 //then take the square root of it
214 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
216 //linear distance falloff
217 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
218 //then take the square root of it
219 ret = 1.0 - sqrt(ret);
223 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
225 //linear distance falloff
226 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
229 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
231 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
232 //linear distance falloff
235 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
237 //linear distance falloff
238 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
239 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
240 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
241 ret = 1.0 / (1 + exp(-(ret*10-5)) );
245 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
247 //linear distance falloff
248 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
249 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
250 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
251 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
257 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
259 //Cosine distance falloff
260 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
264 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
266 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
267 //Cosine distance falloff
270 void Circle::constructcache()
272 cache.inner_radius = radius - feather;
273 if(cache.inner_radius < 0)
274 cache.inner_radius = 0;
276 cache.outer_radius = radius + feather;
278 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
279 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
281 cache.diff_sqd = feather*feather*4.0;
282 cache.double_feather = feather*2.0;
284 falloff_func = GetFalloffFunc();
287 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
291 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
293 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
295 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
297 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
300 default: return invert?InvCosineFalloff:CosineFalloff;
305 Circle::get_color(Context context, const Point &point)const
307 if(radius==0 || is_disabled())
308 return context.get_color(point);
311 Point temp=pos-point;
313 /*const Real inner_radius = radius-feather;
314 const Real outer_radius = radius+feather;
316 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
317 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
319 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
320 const Real double_feather = feather*2.0;*/
322 /*const Real &inner_radius = cache.inner_radius;
323 const Real &outer_radius = cache.outer_radius;*/
325 const Real &inner_radius_sqd = cache.inner_radius_sqd;
326 const Real &outer_radius_sqd = cache.outer_radius_sqd;
328 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
329 const Real &double_feather = cache.double_feather;*/
331 const Vector::value_type mag_squared = temp.mag_squared();
333 //Outside the circle, with feathering enabled
334 if( mag_squared > outer_radius_sqd )
336 // inverted -> outside == colored in
339 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
342 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
345 return context.get_color(point);
348 //inside the circle's solid area (with feathering)
349 else if(mag_squared <= inner_radius_sqd)
351 // !invert -> solid area
353 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
356 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
358 return context.get_color(point);
361 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
364 Color::value_type alpha;
369 case FALLOFF_SQUARED:
370 //squared proportional falloff
371 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
375 //linear distance falloff
376 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
377 //then take the square root of it
381 case FALLOFF_INTERPOLATION_LINEAR:
382 //linear distance falloff
383 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
386 case FALLOFF_SIGMOND:
388 //linear distance falloff
389 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
390 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
391 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
392 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
396 //If we're inverted, we need to invert the falloff value
400 alpha = falloff_func(cache,mag_squared);
402 //Compose falloff value with amount from the composite layer, and that is the blend value
403 alpha *= get_amount();
405 return Color::blend(color,context.get_color(point),alpha,get_blend_method());
409 Color NormalBlend(Color a, Color b, float amount)
411 return (b-a)*amount+a;
416 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
419 if(is_disabled() || (radius==0 && invert==false))
420 return context.accelerated_render(surface,quality, renddesc, cb);
422 // Another trivial case
423 if(invert && radius==0 && is_solid_color())
425 surface->set_wh(renddesc.get_w(),renddesc.get_h());
426 surface->fill(color);
427 if(cb && !cb->amount_complete(10000,10000))
433 const Point tl(renddesc.get_tl());
434 const Point br(renddesc.get_br());
435 const int w(renddesc.get_w());
436 const int h(renddesc.get_h());
438 const Real x_neg = tl[0] > br[0] ? -1 : 1;
439 const Real y_neg = tl[1] > br[1] ? -1 : 1;
441 // Width and Height of a pixel
442 const Real pw = (br[0] - tl[0]) / w;
443 const Real ph = (br[1] - tl[1]) / h;
445 // Increasing the feather amount by the size of
446 // a pixel will create an anti-aliased appearance
447 // don't render feathering at all when quality is 10
448 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
451 int left = (int) floor( (pos[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
452 int right = (int) ceil( (pos[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
453 int top = (int) floor( (pos[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
454 int bottom = (int) ceil( (pos[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
456 //clip the rectangle bounds
466 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
467 const Real outer_radius = radius+newfeather;
469 const Real inner_radius_sqd = inner_radius*inner_radius;
470 const Real outer_radius_sqd = outer_radius*outer_radius;
472 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
473 const Real double_feather = newfeather * 2.0;
475 //Compile the temporary cache for the falloff calculations
476 FALLOFF_FUNC *func = GetFalloffFunc();
478 const CircleDataCache cache =
480 inner_radius,outer_radius,
481 inner_radius_sqd,outer_radius_sqd,
482 diff_radii_sqd,double_feather
485 //info("Circle: Initialized everything");
487 //let the rendering begin
488 SuperCallback supercb(cb,0,9000,10000);
490 //if it's a degenerate circle, do what we need to do, and then leave
491 if(left >= right || top >= bottom)
495 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
497 surface->set_wh(w,h);
498 surface->fill(color);
502 // Render what is behind us
503 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
505 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
509 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
517 // Render what is behind us
518 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
520 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
527 if( (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd
528 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
529 && (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
530 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd )
534 // Render what is behind us
535 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
537 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
542 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
544 surface->set_wh(w,h);
545 surface->fill(color);
551 //info("Circle: Non degenerate, rasterize %c", invert);
553 //we start in the middle of the left-top pixel
554 Real leftf = (left + 0.5)*pw + tl[0];
555 Real topf = (top + 0.5)*ph + tl[1];
557 //the looping variables
561 //Loop normally, since we are not inverted
564 // Render what is behind us
565 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
567 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
571 //make topf and leftf relative to the center of the circle
578 //Loop over the valid y-values in the bounding square
579 for(;j <= bottom; j++, y += ph)
584 //for each y-value, Loop over the bounding x-values in the bounding square
585 for(;i <= right; i++, x += pw)
587 //for each pixel, figure out the distance and blend
590 //if in the inner circle then the full color shows through
591 if(r <= inner_radius_sqd)
593 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
594 (*surface)[j][i]=color;
596 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
598 //if it's within the outer circle then it's in the feathering range
599 else if(r <= outer_radius_sqd)
605 case FALLOFF_SQUARED:
606 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
610 myamount = (outer_radius - sqrt(r)) / double_feather;
611 myamount = sqrt(myamount);
614 case FALLOFF_INTERPOLATION_LINEAR:
615 myamount = (outer_radius - sqrt(r)) / double_feather;
618 case FALLOFF_SIGMOND:
620 myamount = (outer_radius - sqrt(r)) / double_feather;
621 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
625 Real myamount = func(cache,r);
627 //if(myamount<0.0)myamount=0.0;
628 //if(myamount>1.0)myamount=1.0;
629 myamount *= get_amount();
630 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
638 RendDesc desc(renddesc);
641 int offset_x=0,offset_y=0;
643 //fill the surface with the background color initially
644 surface->set_wh(w,h);
645 surface->fill(color);
647 //then render the background to an alternate surface
648 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
653 //if there is no background showing through we are done
654 if(right < left || bottom < top)
657 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
659 // Render what is behind us
660 if(!context.accelerated_render(&background,quality,desc,&supercb))
662 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
673 leftf = /*0.5*pw +*/ tl[0];
674 topf = /*0.5*ph +*/ tl[1];
676 // Render what is behind us
677 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
679 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
690 for(;j <= bottom; j++, y+=ph)
695 for(;i <= right; i++, x+=pw)
697 Vector::value_type r = x*x + y*y;
699 if(r < inner_radius_sqd)
701 (*surface)[j][i] = background[j-offset_y][i-offset_x];
703 else if(r < outer_radius_sqd)
709 case FALLOFF_SQUARED:
710 amount = (r - inner_radius_sqd) / diff_radii_sqd;
712 case FALLOFF_INTERPOLATION_LINEAR:
713 amount = (sqrt(r) - inner_radius) / double_feather;
716 amount = (outer_radius - sqrt(r)) / double_feather;
717 amount = 1.0 - sqrt(amount);
719 case FALLOFF_SIGMOND:
721 amount = (outer_radius - sqrt(r)) / double_feather;
722 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
726 Real amount = func(cache,r);
728 if(amount<0.0)amount=0.0;
729 if(amount>1.0)amount=1.0;
731 amount*=get_amount();
733 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
734 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
736 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
742 // Mark our progress as finished
743 if(cb && !cb->amount_complete(10000,10000))
750 Circle::get_bounding_rect()const
753 return Rect::full_plane();
756 pos[0]+(radius+feather),
757 pos[1]+(radius+feather),
758 pos[0]-(radius+feather),
759 pos[1]-(radius+feather)
766 Circle::get_full_bounding_rect(Context context)const
770 if(is_solid_color() && color.get_a()==0)
773 pos[0]+(radius+feather),
774 pos[1]+(radius+feather),
775 pos[0]-(radius+feather),
776 pos[1]-(radius+feather)
778 return bounds & context.get_full_bounding_rect();
780 return Rect::full_plane();
783 return Layer_Composite::get_full_bounding_rect(context);