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