Add LayerParamSetStatic and LayerParamUnSetStatic actions.
[synfig.git] / synfig-core / src / modules / mod_geometry / circle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file circle.cpp
3 **      \brief Implementation of the "Circle" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "circle.h"
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>
42
43 #include <cmath>
44
45 #endif
46
47 using namespace synfig;
48 using namespace std;
49 using namespace etl;
50
51 /* -- G L O B A L S --------------------------------------------------------- */
52
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$");
59
60 /* -- F U N C T I O N S ----------------------------------------------------- */
61
62 Circle::Circle():
63         Layer_Composite (1.0,Color::BLEND_COMPOSITE),
64         color                   (Color::black()),
65         origin                  (0,0),
66         radius                  (1),
67         feather                 (0),
68         invert                  (false),
69         falloff                 (FALLOFF_INTERPOLATION_LINEAR),
70         color_static    (false),
71         origin_static   (false),
72         radius_static   (false),
73         feather_static  (false),
74         invert_static   (true),
75         falloff_static  (true)
76 {
77         constructcache();
78 }
79
80 bool
81 Circle::ImportParameters(const String &param, const ValueBase &value)
82 {
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; } });
86         IMPORT(radius);
87         IMPORT_PLUS(feather, if(feather<0)feather=0;);
88         IMPORT(invert);
89         IMPORT(origin);
90         IMPORT(falloff);
91
92         IMPORT_AS(origin,"pos");
93
94         return Layer_Composite::set_param(param,value);
95 }
96
97 bool
98 Circle::set_param(const String &param, const ValueBase &value)
99 {
100         if(ImportParameters(param,value))
101         {
102                 constructcache();
103                 return true;
104         }
105
106         return false;
107 }
108
109 ValueBase
110 Circle::get_param(const String &param)const
111 {
112         EXPORT(color);
113         EXPORT(radius);
114         EXPORT(feather);
115         EXPORT(invert);
116         EXPORT(origin);
117         EXPORT(falloff);
118
119         EXPORT_NAME();
120         EXPORT_VERSION();
121
122         return Layer_Composite::get_param(param);
123 }
124
125
126 bool
127 Circle::set_param_static(const String &param, const bool x)
128 {
129
130         if(param=="color" && color_static!=x)
131         {
132                 color_static=x;
133                 return true;
134         }
135         if(param=="radius" && radius_static!=x)
136         {
137                 radius_static=x;
138                 return true;
139         }
140
141         if(param=="origin" && origin_static!=x)
142         {
143                 origin_static=x;
144                 return true;
145         }
146         if(param=="feather" && feather_static!=x)
147         {
148                 feather_static=x;
149                 return true;
150         }
151         if(param=="falloff" && falloff_static!=x)
152         {
153                 falloff_static=x;
154                 return true;
155         }
156
157         return Layer_Composite::set_param_static(param, x);
158 }
159
160
161 bool
162 Circle::get_param_static(const String &param) const
163 {
164
165         if(param=="color")
166                 return color_static;
167         if(param=="radius")
168                 return radius_static;
169         if(param=="origin")
170                 return origin_static;
171         if(param=="feather")
172                 return feather_static;
173         if(param=="falloff")
174                 return falloff_static;
175
176         return Layer_Composite::get_param_static(param);
177 }
178
179
180 Layer::Vocab
181 Circle::get_param_vocab()const
182 {
183         Layer::Vocab ret(Layer_Composite::get_param_vocab());
184
185         ret.push_back(ParamDesc("color")
186                 .set_local_name(_("Color"))
187         );
188         ret.push_back(ParamDesc("radius")
189                 .set_local_name(_("Radius"))
190                 .set_origin("origin")
191                 .set_is_distance()
192         );
193         ret.push_back(ParamDesc("feather")
194                 .set_local_name(_("Feather"))
195                 .set_is_distance()
196         );
197         ret.push_back(ParamDesc("origin")
198                 .set_local_name(_("Origin"))
199         );
200         ret.push_back(ParamDesc("invert")
201                 .set_local_name(_("Invert"))
202                 .set_description(_("Invert the circle"))
203         );
204
205         ret.push_back(ParamDesc("falloff")
206                 .set_local_name(_("Falloff"))
207                 .set_description(_("Determines the falloff function for the feather"))
208                 .set_hint("enum")
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"))
214         );
215
216         return ret;
217 }
218
219 synfig::Layer::Handle
220 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
221 {
222         Point temp=origin-point;
223
224         if(get_amount()==0)
225                 return context.hit_check(point);
226
227         bool in_circle(temp.mag_squared() <= radius*radius);
228
229         if(invert)
230         {
231                 in_circle=!in_circle;
232                 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
233                         in_circle=false;
234         }
235         else
236         {
237                 if(get_amount()-(feather/radius)<=0.0)
238                         in_circle=false;
239         }
240
241         if(in_circle)
242         {
243                 synfig::Layer::Handle tmp;
244                 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
245                         return tmp;
246                 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
247                         return 0;
248                 return const_cast<Circle*>(this);
249         }
250
251         return context.hit_check(point);
252 }
253
254 //falloff functions
255 Real    Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
256 {
257         //squared proportional falloff
258         return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
259 }
260
261 Real    Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
262 {
263         //squared proportional falloff
264         return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
265 }
266
267
268 Real    Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
269 {
270         //linear distance falloff
271         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
272         //then take the square root of it
273         ret = sqrt(ret);
274         return ret;
275 }
276
277 Real    Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
278 {
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);
283         return ret;
284 }
285
286 Real    Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
287 {
288         //linear distance falloff
289         return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
290 }
291
292 Real    Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
293 {
294         return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
295         //linear distance falloff
296 }
297
298 Real    Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
299 {
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)) );
305         return ret;
306 }
307
308 Real    Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
309 {
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)) );
315         return ret;
316 }
317
318
319 Real
320 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
321 {
322         //Cosine distance falloff
323         return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
324 }
325
326 Real
327 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
328 {
329         return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
330         //Cosine distance falloff
331 }
332
333 void Circle::constructcache()
334 {
335         cache.inner_radius = radius - feather;
336         if(cache.inner_radius < 0)
337                 cache.inner_radius = 0;
338
339         cache.outer_radius = radius + feather;
340
341         cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
342         cache.outer_radius_sqd = (radius+feather)*(radius+feather);
343
344         cache.diff_sqd = feather*feather*4.0;
345         cache.double_feather = feather*2.0;
346
347         falloff_func = GetFalloffFunc();
348 }
349
350 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
351 {
352         switch(falloff)
353         {
354         case FALLOFF_SQUARED:   return invert?InvSqdFalloff:SqdFalloff;
355
356         case FALLOFF_SQRT:              return invert?InvSqrtFalloff:SqrtFalloff;
357
358         case FALLOFF_INTERPOLATION_LINEAR:      return invert?InvLinearFalloff:LinearFalloff;
359
360         case FALLOFF_SIGMOND:   return invert?InvSigmondFalloff:SigmondFalloff;
361
362         case FALLOFF_COSINE:
363         default:                                return invert?InvCosineFalloff:CosineFalloff;
364         }
365 }
366
367 Color
368 Circle::get_color(Context context, const Point &point)const
369 {
370         if(is_disabled() || (radius==0 && invert==false && !feather))
371                 return context.get_color(point);
372
373
374         Point temp=origin-point;
375
376         /*const Real inner_radius = radius-feather;
377         const Real outer_radius = radius+feather;
378
379         const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
380         const Real outer_radius_sqd = (radius+feather)*(radius+feather);
381
382         const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
383         const Real double_feather = feather*2.0;*/
384
385         /*const Real &inner_radius = cache.inner_radius;
386         const Real &outer_radius = cache.outer_radius;*/
387
388         const Real &inner_radius_sqd = cache.inner_radius_sqd;
389         const Real &outer_radius_sqd = cache.outer_radius_sqd;
390
391         /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
392         const Real &double_feather = cache.double_feather;*/
393
394         const Vector::value_type mag_squared = temp.mag_squared();
395
396         //Outside the circle, with feathering enabled
397         if( mag_squared > outer_radius_sqd )
398         {
399                 // inverted -> outside == colored in
400                 if(invert)
401                 {
402                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
403                                 return color;
404                         else
405                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
406                 }
407                 else
408                         return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
409         }
410
411         //inside the circle's solid area (with feathering)
412         else if(mag_squared <= inner_radius_sqd)
413         {
414                 // !invert -> solid area
415                 if(!invert)
416                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
417                                 return color;
418                         else
419                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
420                 else
421                         return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
422         }
423
424         //If we get here, the pixel is within the feathering area, and is thus subject to falloff
425         else
426         {
427                 Color::value_type alpha;
428
429                 /*switch(falloff)
430                 {
431
432                 case FALLOFF_SQUARED:
433                         //squared proportional falloff
434                         alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
435                         break;
436
437                 case FALLOFF_SQRT:
438                         //linear distance falloff
439                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
440                         //then take the square root of it
441                         alpha = sqrt(alpha);
442                         break;
443
444                 case FALLOFF_INTERPOLATION_LINEAR:
445                         //linear distance falloff
446                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
447                         break;
448
449                 case FALLOFF_SIGMOND:
450                 default:
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)) );
456                         break;
457                 }
458
459                 //If we're inverted, we need to invert the falloff value
460                 if(invert)
461                         alpha=1.0-alpha;*/
462
463                 alpha = falloff_func(cache,mag_squared);
464
465                 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
466         }
467 }
468
469 Color NormalBlend(Color a, Color b, float amount)
470 {
471         return (b-a)*amount+a;
472 }
473
474
475 bool
476 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
477 {
478         // trivial case
479         if(is_disabled() || (radius==0 && invert==false && !feather))
480                 return context.accelerated_render(surface,quality, renddesc, cb);
481
482         // Another trivial case
483         if(invert && radius==0 && is_solid_color())
484         {
485                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
486                 surface->fill(color);
487                 if(cb && !cb->amount_complete(10000,10000))
488                         return false;
489                 return true;
490         }
491
492         // Window Boundaries
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());
497
498         const Real x_neg = tl[0] > br[0] ? -1 : 1;
499         const Real y_neg = tl[1] > br[1] ? -1 : 1;
500
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;
504
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;
509
510         //int u,v;
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 );
515
516         //clip the rectangle bounds
517         if(left < 0)
518                 left = 0;
519         if(top < 0)
520                 top = 0;
521         if(right >= w)
522                 right = w-1;
523         if(bottom >= h)
524                 bottom = h-1;
525
526         const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
527         const Real outer_radius = radius+newfeather;
528
529         const Real inner_radius_sqd = inner_radius*inner_radius;
530         const Real outer_radius_sqd = outer_radius*outer_radius;
531
532         const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
533         const Real double_feather = newfeather * 2.0;
534
535         //Compile the temporary cache for the falloff calculations
536         FALLOFF_FUNC *func = GetFalloffFunc();
537
538         const CircleDataCache cache =
539         {
540                 inner_radius,outer_radius,
541                 inner_radius_sqd,outer_radius_sqd,
542                 diff_radii_sqd,double_feather
543         };
544
545         //info("Circle: Initialized everything");
546
547         //let the rendering begin
548         SuperCallback supercb(cb,0,9000,10000);
549
550         //if it's a degenerate circle, do what we need to do, and then leave
551         if(left >= right || top >= bottom)
552         {
553                 if(invert)
554                 {
555                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
556                         {
557                                 surface->set_wh(w,h);
558                                 surface->fill(color);
559                                 return true;
560                         }else
561                         {
562                                 // Render what is behind us
563                                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
564                                 {
565                                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
566                                         return false;
567                                 }
568
569                                 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
570
571                                 p.set_value(color);
572                                 p.put_block(h,w);
573                                 return true;
574                         }
575                 }else
576                 {
577                         // Render what is behind us
578                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
579                         {
580                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
581                                 return false;
582                         }
583                         return true;
584                 }
585         }
586
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 )
591         {
592                 if(invert)
593                 {
594                         // Render what is behind us
595                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
596                         {
597                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
598                                 return false;
599                         }
600                 }else
601                 {
602                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
603                         {
604                                 surface->set_wh(w,h);
605                                 surface->fill(color);
606                                 return true;
607                         }
608                 }
609         }
610
611         //info("Circle: Non degenerate, rasterize %c", invert);
612
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];
616
617         //the looping variables
618         Real            x,y;
619         int                     i,j;
620
621         //Loop normally, since we are not inverted
622         if(!invert)
623         {
624                 // Render what is behind us
625                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
626                 {
627                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
628                         return false;
629                 }
630
631                 //make topf and leftf relative to the center of the circle
632                 leftf   -=      origin[0];
633                 topf    -=      origin[1];
634
635                 j = top;
636                 y = topf;
637
638                 //Loop over the valid y-values in the bounding square
639                 for(;j <= bottom; j++, y += ph)
640                 {
641                         i = left;
642                         x = leftf;
643
644                         //for each y-value, Loop over the bounding x-values in the bounding square
645                         for(;i <= right; i++, x += pw)
646                         {
647                                 //for each pixel, figure out the distance and blend
648                                 Real    r = x*x + y*y;
649
650                                 //if in the inner circle then the full color shows through
651                                 if(r <= inner_radius_sqd)
652                                 {
653                                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
654                                                 (*surface)[j][i]=color;
655                                         else
656                                                 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
657                                 }
658                                 //if it's within the outer circle then it's in the feathering range
659                                 else if(r <= outer_radius_sqd)
660                                 {
661                                         /*float myamount;
662
663                                         switch(falloff)
664                                         {
665                                         case FALLOFF_SQUARED:
666                                                 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
667                                                 break;
668
669                                         case FALLOFF_SQRT:
670                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
671                                                 myamount = sqrt(myamount);
672                                                 break;
673
674                                         case FALLOFF_INTERPOLATION_LINEAR:
675                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
676                                                 break;
677
678                                         case FALLOFF_SIGMOND:
679                                         default:
680                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
681                                                 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
682                                                 break;
683                                         }*/
684
685                                         Real    myamount = func(cache,r);
686
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());
691                                 }
692                         }
693                 }
694         }
695         else
696         {
697                 Surface background;
698                 RendDesc desc(renddesc);
699                 desc.set_flags(0);
700
701                 int offset_x=0,offset_y=0;
702
703                 //fill the surface with the background color initially
704                 surface->set_wh(w,h);
705                 surface->fill(color);
706
707                 //then render the background to an alternate surface
708                 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
709                 {
710                         offset_x = left;
711                         offset_y = top;
712
713                         //if there is no background showing through we are done
714                         if(right < left || bottom < top)
715                                 return true;
716
717                         desc.set_subwindow(left,top,right-left+1,bottom-top+1);
718
719                         // Render what is behind us
720                         if(!context.accelerated_render(&background,quality,desc,&supercb))
721                         {
722                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
723                                 return false;
724                         }
725                 }
726                 else
727                 {
728                         left = 0;
729                         right = w-1;
730                         top = 0;
731                         bottom = h-1;
732
733                         leftf = /*0.5*pw +*/ tl[0];
734                         topf = /*0.5*ph +*/ tl[1];
735
736                         // Render what is behind us
737                         if(!context.accelerated_render(&background,quality,renddesc,&supercb))
738                         {
739                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
740                                 return false;
741                         }
742                 }
743
744                 topf -= origin[1];
745                 leftf-= origin[0];
746
747                 j = top;
748                 y = topf;
749
750                 for(;j <= bottom; j++, y+=ph)
751                 {
752                         i = left;
753                         x = leftf;
754
755                         for(;i <= right; i++, x+=pw)
756                         {
757                                 Vector::value_type r = x*x + y*y;
758
759                                 if(r < inner_radius_sqd)
760                                 {
761                                         (*surface)[j][i] = background[j-offset_y][i-offset_x];
762                                 }
763                                 else if(r < outer_radius_sqd)
764                                 {
765                                         /*float amount;
766
767                                         switch(falloff)
768                                         {
769                                         case FALLOFF_SQUARED:
770                                                 amount = (r - inner_radius_sqd) / diff_radii_sqd;
771                                                 break;
772                                         case FALLOFF_INTERPOLATION_LINEAR:
773                                                 amount = (sqrt(r) - inner_radius) / double_feather;
774                                                 break;
775                                         case FALLOFF_SQRT:
776                                                 amount = (outer_radius - sqrt(r)) / double_feather;
777                                                 amount = 1.0 - sqrt(amount);
778                                                 break;
779                                         case FALLOFF_SIGMOND:
780                                         default:
781                                                 amount = (outer_radius - sqrt(r)) / double_feather;
782                                                 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
783                                                 break;
784                                         }*/
785
786                                         Real amount = func(cache,r);
787
788                                         if(amount<0.0)amount=0.0;
789                                         if(amount>1.0)amount=1.0;
790
791                                         amount*=get_amount();
792
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)
795                                 {
796                                         (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
797                                 }
798                         }
799                 }
800     }
801
802         // Mark our progress as finished
803         if(cb && !cb->amount_complete(10000,10000))
804                 return false;
805
806         return true;
807 }
808
809 Rect
810 Circle::get_bounding_rect()const
811 {
812         if(invert)
813                 return Rect::full_plane();
814
815         Rect bounds(
816                 origin[0]+(radius+feather),
817                 origin[1]+(radius+feather),
818                 origin[0]-(radius+feather),
819                 origin[1]-(radius+feather)
820         );
821
822         return bounds;
823 }
824
825 Rect
826 Circle::get_full_bounding_rect(Context context)const
827 {
828         if(invert)
829         {
830                 if(is_solid_color() && color.get_a()==0)
831                 {
832                         Rect bounds(
833                                 origin[0]+(radius+feather),
834                                 origin[1]+(radius+feather),
835                                 origin[0]-(radius+feather),
836                                 origin[1]-(radius+feather)
837                         );
838                         return bounds & context.get_full_bounding_rect();
839                 }
840                 return Rect::full_plane();
841         }
842
843         return Layer_Composite::get_full_bounding_rect(context);
844 }