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