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