Use map of static values indexed by parameter name instead of the macros and the...
[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 {
71         constructcache();
72         Layer::Vocab voc(get_param_vocab());
73         Layer::fill_static(voc);
74 }
75
76 bool
77 Circle::ImportParameters(const String &param, const ValueBase &value)
78 {
79         IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
80                                         set_blend_method(Color::BLEND_ALPHA_OVER);
81                                         color.set_a(1); } else transparent_color_ = true; } });
82         IMPORT(radius);
83         IMPORT_PLUS(feather, if(feather<0)feather=0;);
84         IMPORT(invert);
85         IMPORT(origin);
86         IMPORT(falloff);
87
88         IMPORT_AS(origin,"pos");
89
90         return Layer_Composite::set_param(param,value);
91 }
92
93 bool
94 Circle::set_param(const String &param, const ValueBase &value)
95 {
96         if(ImportParameters(param,value))
97         {
98                 constructcache();
99                 return true;
100         }
101
102         return false;
103 }
104
105 ValueBase
106 Circle::get_param(const String &param)const
107 {
108         EXPORT(color);
109         EXPORT(radius);
110         EXPORT(feather);
111         EXPORT(invert);
112         EXPORT(origin);
113         EXPORT(falloff);
114
115         EXPORT_NAME();
116         EXPORT_VERSION();
117
118         return Layer_Composite::get_param(param);
119 }
120
121
122 bool
123 Circle::set_param_static(const String &param, const bool x)
124 {
125         return Layer_Composite::set_param_static(param, x);
126 }
127
128
129 bool
130 Circle::get_param_static(const String &param) const
131 {
132         return Layer_Composite::get_param_static(param);
133 }
134
135
136 Layer::Vocab
137 Circle::get_param_vocab()const
138 {
139         Layer::Vocab ret(Layer_Composite::get_param_vocab());
140
141         ret.push_back(ParamDesc("color")
142                 .set_local_name(_("Color"))
143         );
144         ret.push_back(ParamDesc("radius")
145                 .set_local_name(_("Radius"))
146                 .set_origin("origin")
147                 .set_is_distance()
148         );
149         ret.push_back(ParamDesc("feather")
150                 .set_local_name(_("Feather"))
151                 .set_is_distance()
152         );
153         ret.push_back(ParamDesc("origin")
154                 .set_local_name(_("Origin"))
155         );
156         ret.push_back(ParamDesc("invert")
157                 .set_local_name(_("Invert"))
158                 .set_description(_("Invert the circle"))
159         );
160
161         ret.push_back(ParamDesc("falloff")
162                 .set_local_name(_("Falloff"))
163                 .set_description(_("Determines the falloff function for the feather"))
164                 .set_hint("enum")
165                 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
166                 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
167                 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
168                 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
169                 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
170         );
171
172         return ret;
173 }
174
175 synfig::Layer::Handle
176 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
177 {
178         Point temp=origin-point;
179
180         if(get_amount()==0)
181                 return context.hit_check(point);
182
183         bool in_circle(temp.mag_squared() <= radius*radius);
184
185         if(invert)
186         {
187                 in_circle=!in_circle;
188                 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
189                         in_circle=false;
190         }
191         else
192         {
193                 if(get_amount()-(feather/radius)<=0.0)
194                         in_circle=false;
195         }
196
197         if(in_circle)
198         {
199                 synfig::Layer::Handle tmp;
200                 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
201                         return tmp;
202                 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
203                         return 0;
204                 return const_cast<Circle*>(this);
205         }
206
207         return context.hit_check(point);
208 }
209
210 //falloff functions
211 Real    Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
212 {
213         //squared proportional falloff
214         return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
215 }
216
217 Real    Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
218 {
219         //squared proportional falloff
220         return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
221 }
222
223
224 Real    Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
225 {
226         //linear distance falloff
227         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
228         //then take the square root of it
229         ret = sqrt(ret);
230         return ret;
231 }
232
233 Real    Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
234 {
235         //linear distance falloff
236         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
237         //then take the square root of it
238         ret = 1.0 - sqrt(ret);
239         return ret;
240 }
241
242 Real    Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
243 {
244         //linear distance falloff
245         return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
246 }
247
248 Real    Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
249 {
250         return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
251         //linear distance falloff
252 }
253
254 Real    Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
255 {
256         //linear distance falloff
257         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
258         // inverse exponential of the linear falloff (asymptotes at 0 and 1)
259         // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
260         ret = 1.0 / (1 + exp(-(ret*10-5)) );
261         return ret;
262 }
263
264 Real    Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
265 {
266         //linear distance falloff
267         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
268         // inverse exponential of the linear falloff (asymptotes at 0 and 1)
269         // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
270         ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
271         return ret;
272 }
273
274
275 Real
276 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
277 {
278         //Cosine distance falloff
279         return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
280 }
281
282 Real
283 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
284 {
285         return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
286         //Cosine distance falloff
287 }
288
289 void Circle::constructcache()
290 {
291         cache.inner_radius = radius - feather;
292         if(cache.inner_radius < 0)
293                 cache.inner_radius = 0;
294
295         cache.outer_radius = radius + feather;
296
297         cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
298         cache.outer_radius_sqd = (radius+feather)*(radius+feather);
299
300         cache.diff_sqd = feather*feather*4.0;
301         cache.double_feather = feather*2.0;
302
303         falloff_func = GetFalloffFunc();
304 }
305
306 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
307 {
308         switch(falloff)
309         {
310         case FALLOFF_SQUARED:   return invert?InvSqdFalloff:SqdFalloff;
311
312         case FALLOFF_SQRT:              return invert?InvSqrtFalloff:SqrtFalloff;
313
314         case FALLOFF_INTERPOLATION_LINEAR:      return invert?InvLinearFalloff:LinearFalloff;
315
316         case FALLOFF_SIGMOND:   return invert?InvSigmondFalloff:SigmondFalloff;
317
318         case FALLOFF_COSINE:
319         default:                                return invert?InvCosineFalloff:CosineFalloff;
320         }
321 }
322
323 Color
324 Circle::get_color(Context context, const Point &point)const
325 {
326         if(is_disabled() || (radius==0 && invert==false && !feather))
327                 return context.get_color(point);
328
329
330         Point temp=origin-point;
331
332         /*const Real inner_radius = radius-feather;
333         const Real outer_radius = radius+feather;
334
335         const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
336         const Real outer_radius_sqd = (radius+feather)*(radius+feather);
337
338         const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
339         const Real double_feather = feather*2.0;*/
340
341         /*const Real &inner_radius = cache.inner_radius;
342         const Real &outer_radius = cache.outer_radius;*/
343
344         const Real &inner_radius_sqd = cache.inner_radius_sqd;
345         const Real &outer_radius_sqd = cache.outer_radius_sqd;
346
347         /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
348         const Real &double_feather = cache.double_feather;*/
349
350         const Vector::value_type mag_squared = temp.mag_squared();
351
352         //Outside the circle, with feathering enabled
353         if( mag_squared > outer_radius_sqd )
354         {
355                 // inverted -> outside == colored in
356                 if(invert)
357                 {
358                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
359                                 return color;
360                         else
361                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
362                 }
363                 else
364                         return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
365         }
366
367         //inside the circle's solid area (with feathering)
368         else if(mag_squared <= inner_radius_sqd)
369         {
370                 // !invert -> solid area
371                 if(!invert)
372                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
373                                 return color;
374                         else
375                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
376                 else
377                         return Color::blend(Color::alpha(),context.get_color(point),get_amount(),get_blend_method());
378         }
379
380         //If we get here, the pixel is within the feathering area, and is thus subject to falloff
381         else
382         {
383                 Color::value_type alpha;
384
385                 /*switch(falloff)
386                 {
387
388                 case FALLOFF_SQUARED:
389                         //squared proportional falloff
390                         alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
391                         break;
392
393                 case FALLOFF_SQRT:
394                         //linear distance falloff
395                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
396                         //then take the square root of it
397                         alpha = sqrt(alpha);
398                         break;
399
400                 case FALLOFF_INTERPOLATION_LINEAR:
401                         //linear distance falloff
402                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
403                         break;
404
405                 case FALLOFF_SIGMOND:
406                 default:
407                         //linear distance falloff
408                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
409                         // inverse exponential of the linear falloff (asymptotes at 0 and 1)
410                         // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
411                         alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
412                         break;
413                 }
414
415                 //If we're inverted, we need to invert the falloff value
416                 if(invert)
417                         alpha=1.0-alpha;*/
418
419                 alpha = falloff_func(cache,mag_squared);
420
421                 return Color::blend(color*alpha,context.get_color(point),get_amount(),get_blend_method());
422         }
423 }
424
425 Color NormalBlend(Color a, Color b, float amount)
426 {
427         return (b-a)*amount+a;
428 }
429
430
431 bool
432 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
433 {
434         // trivial case
435         if(is_disabled() || (radius==0 && invert==false && !feather))
436                 return context.accelerated_render(surface,quality, renddesc, cb);
437
438         // Another trivial case
439         if(invert && radius==0 && is_solid_color())
440         {
441                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
442                 surface->fill(color);
443                 if(cb && !cb->amount_complete(10000,10000))
444                         return false;
445                 return true;
446         }
447
448         // Window Boundaries
449         const Point     tl(renddesc.get_tl());
450         const Point br(renddesc.get_br());
451         const int       w(renddesc.get_w());
452         const int       h(renddesc.get_h());
453
454         const Real x_neg = tl[0] > br[0] ? -1 : 1;
455         const Real y_neg = tl[1] > br[1] ? -1 : 1;
456
457         // Width and Height of a pixel
458         const Real pw = (br[0] - tl[0]) / w;
459         const Real ph = (br[1] - tl[1]) / h;
460
461         // Increasing the feather amount by the size of
462         // a pixel will create an anti-aliased appearance
463         // don't render feathering at all when quality is 10
464         const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
465
466         //int u,v;
467         int left =      (int)   floor( (origin[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
468         int right = (int)       ceil( (origin[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
469         int top =       (int)   floor( (origin[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
470         int bottom = (int)      ceil( (origin[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
471
472         //clip the rectangle bounds
473         if(left < 0)
474                 left = 0;
475         if(top < 0)
476                 top = 0;
477         if(right >= w)
478                 right = w-1;
479         if(bottom >= h)
480                 bottom = h-1;
481
482         const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
483         const Real outer_radius = radius+newfeather;
484
485         const Real inner_radius_sqd = inner_radius*inner_radius;
486         const Real outer_radius_sqd = outer_radius*outer_radius;
487
488         const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
489         const Real double_feather = newfeather * 2.0;
490
491         //Compile the temporary cache for the falloff calculations
492         FALLOFF_FUNC *func = GetFalloffFunc();
493
494         const CircleDataCache cache =
495         {
496                 inner_radius,outer_radius,
497                 inner_radius_sqd,outer_radius_sqd,
498                 diff_radii_sqd,double_feather
499         };
500
501         //info("Circle: Initialized everything");
502
503         //let the rendering begin
504         SuperCallback supercb(cb,0,9000,10000);
505
506         //if it's a degenerate circle, do what we need to do, and then leave
507         if(left >= right || top >= bottom)
508         {
509                 if(invert)
510                 {
511                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
512                         {
513                                 surface->set_wh(w,h);
514                                 surface->fill(color);
515                                 return true;
516                         }else
517                         {
518                                 // Render what is behind us
519                                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
520                                 {
521                                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
522                                         return false;
523                                 }
524
525                                 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
526
527                                 p.set_value(color);
528                                 p.put_block(h,w);
529                                 return true;
530                         }
531                 }else
532                 {
533                         // Render what is behind us
534                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
535                         {
536                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
537                                 return false;
538                         }
539                         return true;
540                 }
541         }
542
543         if( (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd
544                 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
545                 && (origin[0] - tl[0])*(origin[0] - tl[0]) + (origin[1] - br[1])*(origin[1] - br[1]) < inner_radius_sqd
546                 && (origin[0] - br[0])*(origin[0] - br[0]) + (origin[1] - tl[1])*(origin[1] - tl[1]) < inner_radius_sqd )
547         {
548                 if(invert)
549                 {
550                         // Render what is behind us
551                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
552                         {
553                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
554                                 return false;
555                         }
556                 }else
557                 {
558                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
559                         {
560                                 surface->set_wh(w,h);
561                                 surface->fill(color);
562                                 return true;
563                         }
564                 }
565         }
566
567         //info("Circle: Non degenerate, rasterize %c", invert);
568
569         //we start in the middle of the left-top pixel
570         Real leftf      = (left + 0.5)*pw + tl[0];
571         Real topf       = (top + 0.5)*ph + tl[1];
572
573         //the looping variables
574         Real            x,y;
575         int                     i,j;
576
577         //Loop normally, since we are not inverted
578         if(!invert)
579         {
580                 // Render what is behind us
581                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
582                 {
583                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
584                         return false;
585                 }
586
587                 //make topf and leftf relative to the center of the circle
588                 leftf   -=      origin[0];
589                 topf    -=      origin[1];
590
591                 j = top;
592                 y = topf;
593
594                 //Loop over the valid y-values in the bounding square
595                 for(;j <= bottom; j++, y += ph)
596                 {
597                         i = left;
598                         x = leftf;
599
600                         //for each y-value, Loop over the bounding x-values in the bounding square
601                         for(;i <= right; i++, x += pw)
602                         {
603                                 //for each pixel, figure out the distance and blend
604                                 Real    r = x*x + y*y;
605
606                                 //if in the inner circle then the full color shows through
607                                 if(r <= inner_radius_sqd)
608                                 {
609                                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
610                                                 (*surface)[j][i]=color;
611                                         else
612                                                 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
613                                 }
614                                 //if it's within the outer circle then it's in the feathering range
615                                 else if(r <= outer_radius_sqd)
616                                 {
617                                         /*float myamount;
618
619                                         switch(falloff)
620                                         {
621                                         case FALLOFF_SQUARED:
622                                                 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
623                                                 break;
624
625                                         case FALLOFF_SQRT:
626                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
627                                                 myamount = sqrt(myamount);
628                                                 break;
629
630                                         case FALLOFF_INTERPOLATION_LINEAR:
631                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
632                                                 break;
633
634                                         case FALLOFF_SIGMOND:
635                                         default:
636                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
637                                                 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
638                                                 break;
639                                         }*/
640
641                                         Real    myamount = func(cache,r);
642
643                                         //if(myamount<0.0)myamount=0.0;
644                                         //if(myamount>1.0)myamount=1.0;
645                                         myamount *= get_amount();
646                                         (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
647                                 }
648                         }
649                 }
650         }
651         else
652         {
653                 Surface background;
654                 RendDesc desc(renddesc);
655                 desc.set_flags(0);
656
657                 int offset_x=0,offset_y=0;
658
659                 //fill the surface with the background color initially
660                 surface->set_wh(w,h);
661                 surface->fill(color);
662
663                 //then render the background to an alternate surface
664                 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
665                 {
666                         offset_x = left;
667                         offset_y = top;
668
669                         //if there is no background showing through we are done
670                         if(right < left || bottom < top)
671                                 return true;
672
673                         desc.set_subwindow(left,top,right-left+1,bottom-top+1);
674
675                         // Render what is behind us
676                         if(!context.accelerated_render(&background,quality,desc,&supercb))
677                         {
678                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
679                                 return false;
680                         }
681                 }
682                 else
683                 {
684                         left = 0;
685                         right = w-1;
686                         top = 0;
687                         bottom = h-1;
688
689                         leftf = /*0.5*pw +*/ tl[0];
690                         topf = /*0.5*ph +*/ tl[1];
691
692                         // Render what is behind us
693                         if(!context.accelerated_render(&background,quality,renddesc,&supercb))
694                         {
695                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
696                                 return false;
697                         }
698                 }
699
700                 topf -= origin[1];
701                 leftf-= origin[0];
702
703                 j = top;
704                 y = topf;
705
706                 for(;j <= bottom; j++, y+=ph)
707                 {
708                         i = left;
709                         x = leftf;
710
711                         for(;i <= right; i++, x+=pw)
712                         {
713                                 Vector::value_type r = x*x + y*y;
714
715                                 if(r < inner_radius_sqd)
716                                 {
717                                         (*surface)[j][i] = background[j-offset_y][i-offset_x];
718                                 }
719                                 else if(r < outer_radius_sqd)
720                                 {
721                                         /*float amount;
722
723                                         switch(falloff)
724                                         {
725                                         case FALLOFF_SQUARED:
726                                                 amount = (r - inner_radius_sqd) / diff_radii_sqd;
727                                                 break;
728                                         case FALLOFF_INTERPOLATION_LINEAR:
729                                                 amount = (sqrt(r) - inner_radius) / double_feather;
730                                                 break;
731                                         case FALLOFF_SQRT:
732                                                 amount = (outer_radius - sqrt(r)) / double_feather;
733                                                 amount = 1.0 - sqrt(amount);
734                                                 break;
735                                         case FALLOFF_SIGMOND:
736                                         default:
737                                                 amount = (outer_radius - sqrt(r)) / double_feather;
738                                                 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
739                                                 break;
740                                         }*/
741
742                                         Real amount = func(cache,r);
743
744                                         if(amount<0.0)amount=0.0;
745                                         if(amount>1.0)amount=1.0;
746
747                                         amount*=get_amount();
748
749                                         (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
750                                 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
751                                 {
752                                         (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
753                                 }
754                         }
755                 }
756     }
757
758         // Mark our progress as finished
759         if(cb && !cb->amount_complete(10000,10000))
760                 return false;
761
762         return true;
763 }
764
765 Rect
766 Circle::get_bounding_rect()const
767 {
768         if(invert)
769                 return Rect::full_plane();
770
771         Rect bounds(
772                 origin[0]+(radius+feather),
773                 origin[1]+(radius+feather),
774                 origin[0]-(radius+feather),
775                 origin[1]-(radius+feather)
776         );
777
778         return bounds;
779 }
780
781 Rect
782 Circle::get_full_bounding_rect(Context context)const
783 {
784         if(invert)
785         {
786                 if(is_solid_color() && color.get_a()==0)
787                 {
788                         Rect bounds(
789                                 origin[0]+(radius+feather),
790                                 origin[1]+(radius+feather),
791                                 origin[0]-(radius+feather),
792                                 origin[1]-(radius+feather)
793                         );
794                         return bounds & context.get_full_bounding_rect();
795                 }
796                 return Rect::full_plane();
797         }
798
799         return Layer_Composite::get_full_bounding_rect(context);
800 }