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