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)
130 if(param=="color" && color_static!=x)
135 if(param=="radius" && radius_static!=x)
141 if(param=="origin" && origin_static!=x)
146 if(param=="feather" && feather_static!=x)
151 if(param=="falloff" && falloff_static!=x)
157 return Layer_Composite::set_param_static(param, x);
162 Circle::get_param_static(const String ¶m) const
168 return radius_static;
170 return origin_static;
172 return feather_static;
174 return falloff_static;
176 return Layer_Composite::get_param_static(param);
181 Circle::get_param_vocab()const
183 Layer::Vocab ret(Layer_Composite::get_param_vocab());
185 ret.push_back(ParamDesc("color")
186 .set_local_name(_("Color"))
188 ret.push_back(ParamDesc("radius")
189 .set_local_name(_("Radius"))
190 .set_origin("origin")
193 ret.push_back(ParamDesc("feather")
194 .set_local_name(_("Feather"))
197 ret.push_back(ParamDesc("origin")
198 .set_local_name(_("Origin"))
200 ret.push_back(ParamDesc("invert")
201 .set_local_name(_("Invert"))
202 .set_description(_("Invert the circle"))
205 ret.push_back(ParamDesc("falloff")
206 .set_local_name(_("Falloff"))
207 .set_description(_("Determines the falloff function for the feather"))
209 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
210 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
211 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
212 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
213 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
219 synfig::Layer::Handle
220 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
222 Point temp=origin-point;
225 return context.hit_check(point);
227 bool in_circle(temp.mag_squared() <= radius*radius);
231 in_circle=!in_circle;
232 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
237 if(get_amount()-(feather/radius)<=0.0)
243 synfig::Layer::Handle tmp;
244 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
246 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
248 return const_cast<Circle*>(this);
251 return context.hit_check(point);
255 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
257 //squared proportional falloff
258 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
261 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
263 //squared proportional falloff
264 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
268 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
270 //linear distance falloff
271 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
272 //then take the square root of it
277 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
279 //linear distance falloff
280 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
281 //then take the square root of it
282 ret = 1.0 - sqrt(ret);
286 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
288 //linear distance falloff
289 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
292 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
294 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
295 //linear distance falloff
298 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
300 //linear distance falloff
301 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
302 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
303 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
304 ret = 1.0 / (1 + exp(-(ret*10-5)) );
308 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
310 //linear distance falloff
311 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
312 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
313 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
314 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
320 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
322 //Cosine distance falloff
323 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
327 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
329 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
330 //Cosine distance falloff
333 void Circle::constructcache()
335 cache.inner_radius = radius - feather;
336 if(cache.inner_radius < 0)
337 cache.inner_radius = 0;
339 cache.outer_radius = radius + feather;
341 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
342 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
344 cache.diff_sqd = feather*feather*4.0;
345 cache.double_feather = feather*2.0;
347 falloff_func = GetFalloffFunc();
350 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
354 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
356 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
358 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
360 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
363 default: return invert?InvCosineFalloff:CosineFalloff;
368 Circle::get_color(Context context, const Point &point)const
370 if(is_disabled() || (radius==0 && invert==false && !feather))
371 return context.get_color(point);
374 Point temp=origin-point;
376 /*const Real inner_radius = radius-feather;
377 const Real outer_radius = radius+feather;
379 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
380 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
382 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
383 const Real double_feather = feather*2.0;*/
385 /*const Real &inner_radius = cache.inner_radius;
386 const Real &outer_radius = cache.outer_radius;*/
388 const Real &inner_radius_sqd = cache.inner_radius_sqd;
389 const Real &outer_radius_sqd = cache.outer_radius_sqd;
391 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
392 const Real &double_feather = cache.double_feather;*/
394 const Vector::value_type mag_squared = temp.mag_squared();
396 //Outside the circle, with feathering enabled
397 if( mag_squared > outer_radius_sqd )
399 // inverted -> outside == colored in
402 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
405 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
408 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
411 //inside the circle's solid area (with feathering)
412 else if(mag_squared <= inner_radius_sqd)
414 // !invert -> solid area
416 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
419 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
421 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
424 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
427 Color::value_type alpha;
432 case FALLOFF_SQUARED:
433 //squared proportional falloff
434 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
438 //linear distance falloff
439 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
440 //then take the square root of it
444 case FALLOFF_INTERPOLATION_LINEAR:
445 //linear distance falloff
446 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
449 case FALLOFF_SIGMOND:
451 //linear distance falloff
452 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
453 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
454 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
455 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
459 //If we're inverted, we need to invert the falloff value
463 alpha = falloff_func(cache,mag_squared);
465 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
469 Color NormalBlend(Color a, Color b, float amount)
471 return (b-a)*amount+a;
476 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
479 if(is_disabled() || (radius==0 && invert==false && !feather))
480 return context.accelerated_render(surface,quality, renddesc, cb);
482 // Another trivial case
483 if(invert && radius==0 && is_solid_color())
485 surface->set_wh(renddesc.get_w(),renddesc.get_h());
486 surface->fill(color);
487 if(cb && !cb->amount_complete(10000,10000))
493 const Point tl(renddesc.get_tl());
494 const Point br(renddesc.get_br());
495 const int w(renddesc.get_w());
496 const int h(renddesc.get_h());
498 const Real x_neg = tl[0] > br[0] ? -1 : 1;
499 const Real y_neg = tl[1] > br[1] ? -1 : 1;
501 // Width and Height of a pixel
502 const Real pw = (br[0] - tl[0]) / w;
503 const Real ph = (br[1] - tl[1]) / h;
505 // Increasing the feather amount by the size of
506 // a pixel will create an anti-aliased appearance
507 // don't render feathering at all when quality is 10
508 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
511 int left = (int) floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
512 int right = (int) ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
513 int top = (int) floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
514 int bottom = (int) ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
516 //clip the rectangle bounds
526 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
527 const Real outer_radius = radius+newfeather;
529 const Real inner_radius_sqd = inner_radius*inner_radius;
530 const Real outer_radius_sqd = outer_radius*outer_radius;
532 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
533 const Real double_feather = newfeather * 2.0;
535 //Compile the temporary cache for the falloff calculations
536 FALLOFF_FUNC *func = GetFalloffFunc();
538 const CircleDataCache cache =
540 inner_radius,outer_radius,
541 inner_radius_sqd,outer_radius_sqd,
542 diff_radii_sqd,double_feather
545 //info("Circle: Initialized everything");
547 //let the rendering begin
548 SuperCallback supercb(cb,0,9000,10000);
550 //if it's a degenerate circle, do what we need to do, and then leave
551 if(left >= right || top >= bottom)
555 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
557 surface->set_wh(w,h);
558 surface->fill(color);
562 // Render what is behind us
563 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
565 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
569 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
577 // Render what is behind us
578 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
580 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
587 if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
588 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
589 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
590 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
594 // Render what is behind us
595 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
597 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
602 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
604 surface->set_wh(w,h);
605 surface->fill(color);
611 //info("Circle: Non degenerate, rasterize %c", invert);
613 //we start in the middle of the left-top pixel
614 Real leftf = (left + 0.5)*pw + tl[0];
615 Real topf = (top + 0.5)*ph + tl[1];
617 //the looping variables
621 //Loop normally, since we are not inverted
624 // Render what is behind us
625 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
627 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
631 //make topf and leftf relative to the center of the circle
638 //Loop over the valid y-values in the bounding square
639 for(;j <= bottom; j++, y += ph)
644 //for each y-value, Loop over the bounding x-values in the bounding square
645 for(;i <= right; i++, x += pw)
647 //for each pixel, figure out the distance and blend
650 //if in the inner circle then the full color shows through
651 if(r <= inner_radius_sqd)
653 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
654 (*surface)[j][i]=color;
656 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
658 //if it's within the outer circle then it's in the feathering range
659 else if(r <= outer_radius_sqd)
665 case FALLOFF_SQUARED:
666 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
670 myamount = (outer_radius - sqrt(r)) / double_feather;
671 myamount = sqrt(myamount);
674 case FALLOFF_INTERPOLATION_LINEAR:
675 myamount = (outer_radius - sqrt(r)) / double_feather;
678 case FALLOFF_SIGMOND:
680 myamount = (outer_radius - sqrt(r)) / double_feather;
681 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
685 Real myamount = func(cache,r);
687 //if(myamount<0.0)myamount=0.0;
688 //if(myamount>1.0)myamount=1.0;
689 myamount *= get_amount();
690 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
698 RendDesc desc(renddesc);
701 int offset_x=0,offset_y=0;
703 //fill the surface with the background color initially
704 surface->set_wh(w,h);
705 surface->fill(color);
707 //then render the background to an alternate surface
708 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
713 //if there is no background showing through we are done
714 if(right < left || bottom < top)
717 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
719 // Render what is behind us
720 if(!context.accelerated_render(&background,quality,desc,&supercb))
722 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
733 leftf = /*0.5*pw +*/ tl[0];
734 topf = /*0.5*ph +*/ tl[1];
736 // Render what is behind us
737 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
739 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
750 for(;j <= bottom; j++, y+=ph)
755 for(;i <= right; i++, x+=pw)
757 Vector::value_type r = x*x + y*y;
759 if(r < inner_radius_sqd)
761 (*surface)[j][i] = background[j-offset_y][i-offset_x];
763 else if(r < outer_radius_sqd)
769 case FALLOFF_SQUARED:
770 amount = (r - inner_radius_sqd) / diff_radii_sqd;
772 case FALLOFF_INTERPOLATION_LINEAR:
773 amount = (sqrt(r) - inner_radius) / double_feather;
776 amount = (outer_radius - sqrt(r)) / double_feather;
777 amount = 1.0 - sqrt(amount);
779 case FALLOFF_SIGMOND:
781 amount = (outer_radius - sqrt(r)) / double_feather;
782 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
786 Real amount = func(cache,r);
788 if(amount<0.0)amount=0.0;
789 if(amount>1.0)amount=1.0;
791 amount*=get_amount();
793 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
794 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
796 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
802 // Mark our progress as finished
803 if(cb && !cb->amount_complete(10000,10000))
810 Circle::get_bounding_rect()const
813 return Rect::full_plane();
816 origin[0]+(radius+feather),
817 origin[1]+(radius+feather),
818 origin[0]-(radius+feather),
819 origin[1]-(radius+feather)
826 Circle::get_full_bounding_rect(Context context)const
830 if(is_solid_color() && color.get_a()==0)
833 origin[0]+(radius+feather),
834 origin[1]+(radius+feather),
835 origin[0]-(radius+feather),
836 origin[1]-(radius+feather)
838 return bounds & context.get_full_bounding_rect();
840 return Rect::full_plane();
843 return Layer_Composite::get_full_bounding_rect(context);