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