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(feather, x)
133 SET_STATIC(invert, x)
134 SET_STATIC(origin, x)
135 SET_STATIC(falloff, x)
137 return Layer_Composite::set_param_static(param, x);
142 Circle::get_param_static(const String ¶m) const
152 return Layer_Composite::get_param_static(param);
157 Circle::get_param_vocab()const
159 Layer::Vocab ret(Layer_Composite::get_param_vocab());
161 ret.push_back(ParamDesc("color")
162 .set_local_name(_("Color"))
164 ret.push_back(ParamDesc("radius")
165 .set_local_name(_("Radius"))
166 .set_origin("origin")
169 ret.push_back(ParamDesc("feather")
170 .set_local_name(_("Feather"))
173 ret.push_back(ParamDesc("origin")
174 .set_local_name(_("Origin"))
176 ret.push_back(ParamDesc("invert")
177 .set_local_name(_("Invert"))
178 .set_description(_("Invert the circle"))
181 ret.push_back(ParamDesc("falloff")
182 .set_local_name(_("Falloff"))
183 .set_description(_("Determines the falloff function for the feather"))
185 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
186 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
187 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
188 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
189 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
195 synfig::Layer::Handle
196 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
198 Point temp=origin-point;
201 return context.hit_check(point);
203 bool in_circle(temp.mag_squared() <= radius*radius);
207 in_circle=!in_circle;
208 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
213 if(get_amount()-(feather/radius)<=0.0)
219 synfig::Layer::Handle tmp;
220 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
222 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
224 return const_cast<Circle*>(this);
227 return context.hit_check(point);
231 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
233 //squared proportional falloff
234 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
237 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
239 //squared proportional falloff
240 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
244 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
246 //linear distance falloff
247 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
248 //then take the square root of it
253 Real Circle::InvSqrtFalloff(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 //then take the square root of it
258 ret = 1.0 - sqrt(ret);
262 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
264 //linear distance falloff
265 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
268 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
270 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
271 //linear distance falloff
274 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
276 //linear distance falloff
277 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
278 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
279 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
280 ret = 1.0 / (1 + exp(-(ret*10-5)) );
284 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
286 //linear distance falloff
287 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
288 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
289 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
290 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
296 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
298 //Cosine distance falloff
299 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
303 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
305 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
306 //Cosine distance falloff
309 void Circle::constructcache()
311 cache.inner_radius = radius - feather;
312 if(cache.inner_radius < 0)
313 cache.inner_radius = 0;
315 cache.outer_radius = radius + feather;
317 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
318 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
320 cache.diff_sqd = feather*feather*4.0;
321 cache.double_feather = feather*2.0;
323 falloff_func = GetFalloffFunc();
326 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
330 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
332 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
334 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
336 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
339 default: return invert?InvCosineFalloff:CosineFalloff;
344 Circle::get_color(Context context, const Point &point)const
346 if(is_disabled() || (radius==0 && invert==false && !feather))
347 return context.get_color(point);
350 Point temp=origin-point;
352 /*const Real inner_radius = radius-feather;
353 const Real outer_radius = radius+feather;
355 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
356 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
358 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
359 const Real double_feather = feather*2.0;*/
361 /*const Real &inner_radius = cache.inner_radius;
362 const Real &outer_radius = cache.outer_radius;*/
364 const Real &inner_radius_sqd = cache.inner_radius_sqd;
365 const Real &outer_radius_sqd = cache.outer_radius_sqd;
367 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
368 const Real &double_feather = cache.double_feather;*/
370 const Vector::value_type mag_squared = temp.mag_squared();
372 //Outside the circle, with feathering enabled
373 if( mag_squared > outer_radius_sqd )
375 // inverted -> outside == colored in
378 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
381 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
384 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
387 //inside the circle's solid area (with feathering)
388 else if(mag_squared <= inner_radius_sqd)
390 // !invert -> solid area
392 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
395 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
397 return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
400 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
403 Color::value_type alpha;
408 case FALLOFF_SQUARED:
409 //squared proportional falloff
410 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
414 //linear distance falloff
415 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
416 //then take the square root of it
420 case FALLOFF_INTERPOLATION_LINEAR:
421 //linear distance falloff
422 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
425 case FALLOFF_SIGMOND:
427 //linear distance falloff
428 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
429 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
430 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
431 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
435 //If we're inverted, we need to invert the falloff value
439 alpha = falloff_func(cache,mag_squared);
441 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
445 Color NormalBlend(Color a, Color b, float amount)
447 return (b-a)*amount+a;
452 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
455 if(is_disabled() || (radius==0 && invert==false && !feather))
456 return context.accelerated_render(surface,quality, renddesc, cb);
458 // Another trivial case
459 if(invert && radius==0 && is_solid_color())
461 surface->set_wh(renddesc.get_w(),renddesc.get_h());
462 surface->fill(color);
463 if(cb && !cb->amount_complete(10000,10000))
469 const Point tl(renddesc.get_tl());
470 const Point br(renddesc.get_br());
471 const int w(renddesc.get_w());
472 const int h(renddesc.get_h());
474 const Real x_neg = tl[0] > br[0] ? -1 : 1;
475 const Real y_neg = tl[1] > br[1] ? -1 : 1;
477 // Width and Height of a pixel
478 const Real pw = (br[0] - tl[0]) / w;
479 const Real ph = (br[1] - tl[1]) / h;
481 // Increasing the feather amount by the size of
482 // a pixel will create an anti-aliased appearance
483 // don't render feathering at all when quality is 10
484 const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
487 int left = (int) floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
488 int right = (int) ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
489 int top = (int) floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
490 int bottom = (int) ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
492 //clip the rectangle bounds
502 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
503 const Real outer_radius = radius+newfeather;
505 const Real inner_radius_sqd = inner_radius*inner_radius;
506 const Real outer_radius_sqd = outer_radius*outer_radius;
508 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
509 const Real double_feather = newfeather * 2.0;
511 //Compile the temporary cache for the falloff calculations
512 FALLOFF_FUNC *func = GetFalloffFunc();
514 const CircleDataCache cache =
516 inner_radius,outer_radius,
517 inner_radius_sqd,outer_radius_sqd,
518 diff_radii_sqd,double_feather
521 //info("Circle: Initialized everything");
523 //let the rendering begin
524 SuperCallback supercb(cb,0,9000,10000);
526 //if it's a degenerate circle, do what we need to do, and then leave
527 if(left >= right || top >= bottom)
531 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
533 surface->set_wh(w,h);
534 surface->fill(color);
538 // Render what is behind us
539 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
541 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
545 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
553 // Render what is behind us
554 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
556 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
563 if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
564 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
565 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
566 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
570 // Render what is behind us
571 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
573 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
578 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
580 surface->set_wh(w,h);
581 surface->fill(color);
587 //info("Circle: Non degenerate, rasterize %c", invert);
589 //we start in the middle of the left-top pixel
590 Real leftf = (left + 0.5)*pw + tl[0];
591 Real topf = (top + 0.5)*ph + tl[1];
593 //the looping variables
597 //Loop normally, since we are not inverted
600 // Render what is behind us
601 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
603 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
607 //make topf and leftf relative to the center of the circle
614 //Loop over the valid y-values in the bounding square
615 for(;j <= bottom; j++, y += ph)
620 //for each y-value, Loop over the bounding x-values in the bounding square
621 for(;i <= right; i++, x += pw)
623 //for each pixel, figure out the distance and blend
626 //if in the inner circle then the full color shows through
627 if(r <= inner_radius_sqd)
629 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
630 (*surface)[j][i]=color;
632 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
634 //if it's within the outer circle then it's in the feathering range
635 else if(r <= outer_radius_sqd)
641 case FALLOFF_SQUARED:
642 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
646 myamount = (outer_radius - sqrt(r)) / double_feather;
647 myamount = sqrt(myamount);
650 case FALLOFF_INTERPOLATION_LINEAR:
651 myamount = (outer_radius - sqrt(r)) / double_feather;
654 case FALLOFF_SIGMOND:
656 myamount = (outer_radius - sqrt(r)) / double_feather;
657 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
661 Real myamount = func(cache,r);
663 //if(myamount<0.0)myamount=0.0;
664 //if(myamount>1.0)myamount=1.0;
665 myamount *= get_amount();
666 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
674 RendDesc desc(renddesc);
677 int offset_x=0,offset_y=0;
679 //fill the surface with the background color initially
680 surface->set_wh(w,h);
681 surface->fill(color);
683 //then render the background to an alternate surface
684 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
689 //if there is no background showing through we are done
690 if(right < left || bottom < top)
693 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
695 // Render what is behind us
696 if(!context.accelerated_render(&background,quality,desc,&supercb))
698 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
709 leftf = /*0.5*pw +*/ tl[0];
710 topf = /*0.5*ph +*/ tl[1];
712 // Render what is behind us
713 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
715 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
726 for(;j <= bottom; j++, y+=ph)
731 for(;i <= right; i++, x+=pw)
733 Vector::value_type r = x*x + y*y;
735 if(r < inner_radius_sqd)
737 (*surface)[j][i] = background[j-offset_y][i-offset_x];
739 else if(r < outer_radius_sqd)
745 case FALLOFF_SQUARED:
746 amount = (r - inner_radius_sqd) / diff_radii_sqd;
748 case FALLOFF_INTERPOLATION_LINEAR:
749 amount = (sqrt(r) - inner_radius) / double_feather;
752 amount = (outer_radius - sqrt(r)) / double_feather;
753 amount = 1.0 - sqrt(amount);
755 case FALLOFF_SIGMOND:
757 amount = (outer_radius - sqrt(r)) / double_feather;
758 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
762 Real amount = func(cache,r);
764 if(amount<0.0)amount=0.0;
765 if(amount>1.0)amount=1.0;
767 amount*=get_amount();
769 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
770 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
772 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
778 // Mark our progress as finished
779 if(cb && !cb->amount_complete(10000,10000))
786 Circle::get_bounding_rect()const
789 return Rect::full_plane();
792 origin[0]+(radius+feather),
793 origin[1]+(radius+feather),
794 origin[0]-(radius+feather),
795 origin[1]-(radius+feather)
802 Circle::get_full_bounding_rect(Context context)const
806 if(is_solid_color() && color.get_a()==0)
809 origin[0]+(radius+feather),
810 origin[1]+(radius+feather),
811 origin[0]-(radius+feather),
812 origin[1]-(radius+feather)
814 return bounds & context.get_full_bounding_rect();
816 return Rect::full_plane();
819 return Layer_Composite::get_full_bounding_rect(context);