moreupdates
[synfig.git] / synfig-core / trunk / src / modules / example / filledrect.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file filledrect.cpp
3 **      \brief Template Header
4 **
5 **      $Id: filledrect.cpp,v 1.1.1.1 2005/01/04 01:23:09 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <synfig/string.h>
32 #include <synfig/time.h>
33 #include <synfig/context.h>
34 #include <synfig/paramdesc.h>
35 #include <synfig/renddesc.h>
36 #include <synfig/surface.h>
37 #include <synfig/value.h>
38 #include <synfig/valuenode.h>
39 #include <ETL/pen>
40
41 #include "filledrect.h"
42
43 #endif
44
45 /* === U S I N G =========================================================== */
46
47 using namespace etl;
48 using namespace std;
49 using namespace synfig;
50
51 /* === G L O B A L S ======================================================= */
52
53 SYNFIG_LAYER_INIT(FilledRect);
54 SYNFIG_LAYER_SET_NAME(FilledRect,"rectangle");
55 SYNFIG_LAYER_SET_LOCAL_NAME(FilledRect,_("Rectangle"));
56 SYNFIG_LAYER_SET_CATEGORY(FilledRect,_("Geometry"));
57 SYNFIG_LAYER_SET_VERSION(FilledRect,"0.1");
58 SYNFIG_LAYER_SET_CVS_ID(FilledRect,"$Id: filledrect.cpp,v 1.1.1.1 2005/01/04 01:23:09 darco Exp $");
59
60 /* === P R O C E D U R E S ================================================= */
61
62 /* === M E T H O D S ======================================================= */
63
64 /* === E N T R Y P O I N T ================================================= */
65
66 FilledRect::FilledRect():
67         Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68         color(Color::black()),
69         point1(0,0),
70         point2(1,1),
71         feather_x(0),
72         feather_y(0),
73         bevel(0),
74         bevCircle(0)
75 {
76 }
77         
78 bool
79 FilledRect::set_param(const String & param, const ValueBase &value)
80 {
81         IMPORT(color);
82         IMPORT(point1);
83         IMPORT(point2);
84         IMPORT(feather_x);
85         IMPORT(feather_y);
86         IMPORT(bevel);
87         IMPORT(bevCircle);
88         
89         return Layer_Composite::set_param(param,value);
90 }
91
92 ValueBase
93 FilledRect::get_param(const String &param)const
94 {
95         EXPORT(color);
96         EXPORT(point1);
97         EXPORT(point2);
98         EXPORT(feather_x);
99         EXPORT(feather_y);
100         EXPORT(bevel);
101         EXPORT(bevCircle);
102
103         EXPORT_NAME();
104         EXPORT_VERSION();
105                 
106         return Layer_Composite::get_param(param);       
107 }
108
109 Layer::Vocab
110 FilledRect::get_param_vocab()const
111 {
112         Layer::Vocab ret(Layer_Composite::get_param_vocab());
113         
114         ret.push_back(ParamDesc("color")
115                 .set_local_name(_("Color"))
116         );
117
118         ret.push_back(ParamDesc("point1")
119                 .set_local_name(_("Point 1"))
120         );
121         
122         ret.push_back(ParamDesc("point2")
123                 .set_local_name(_("Point 2"))
124         );
125         
126         ret.push_back(ParamDesc("feather_x")
127                 .set_local_name(_("Feather X"))
128         );
129         
130         ret.push_back(ParamDesc("feather_y")
131                 .set_local_name(_("Feather Y"))
132         );
133         
134         ret.push_back(ParamDesc("bevel")
135                 .set_local_name(_("Bevel"))
136         );
137         
138         ret.push_back(ParamDesc("bevCircle")
139                 .set_local_name(_("Keep Bevel Circular"))
140         );
141         
142         return ret;
143 }
144
145 bool
146 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
147 {
148         Point p[2] = {point1,point2};
149         Real swap;
150         
151         if(p[0][0] > p[1][0])
152         {
153                 swap = p[0][0];
154                 p[0][0] = p[1][0];
155                 p[1][0] = swap;
156         }
157         
158         if(p[0][1] > p[1][1])
159         {
160                 swap = p[0][1];
161                 p[0][1] = p[1][1];
162                 p[1][1] = swap;
163         }
164         
165         /*
166         p[0][0] -= feather_x;
167         p[1][0] += feather_x;
168         p[0][1] -= feather_y;
169         p[1][1] += feather_y;*/
170         const Real      w = p[1][0] - p[0][0];
171         const Real      h = p[1][1] - p[0][1];
172
173         if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
174         {
175                 Real value = 1;
176                 
177                 if(feather_x > 0)
178                 {
179                         Real xdist = pos[0] - p[0][0];
180                         xdist = min(xdist,p[1][0]-pos[0]);
181                 
182                         if(xdist < feather_x)
183                         {
184                                 value = xdist/feather_x;
185                         }
186                 }
187                 
188                 if(feather_y > 0)
189                 {
190                         Real ydist = pos[1]-p[0][1];
191                         ydist = min(ydist,p[1][1]-pos[1]);
192                 
193                         if(ydist < feather_y)
194                         {
195                                 value = min(value,(ydist/feather_y));
196                         }
197                 }
198                 
199                 //if we're beveled then check with ellipse code...
200                 if(bevel > 0)
201                 {                       
202                         const Real bev = (bevel > 1) ? 1 : bevel;
203                         const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
204                         const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
205                         
206                         Vector v(0,0);
207                         bool    in = false;
208                         
209                         //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
210                         if(pos[0] < p[0][0] + bevx)
211                         {
212                                  if(pos[1] < p[0][1] + bevy)
213                                  {
214                                          v[0] = p[0][0] + bevx - pos[0];
215                                          v[1] = p[0][1] + bevy - pos[1];
216                                          in = true;
217                                  }else if(pos[1] > p[1][1] - bevy)
218                                  {
219                                          v[0] = p[0][0] + bevx - pos[0];
220                                          v[1] = pos[1] - (p[1][1] - bevy);
221                                          in = true;
222                                  }
223                         }
224                         else if(pos[0] > p[1][0] - bevx)
225                         {
226                                 if(pos[1] < p[0][1] + bevy)
227                                  {
228                                          v[0] = pos[0] - (p[1][0] - bevx);
229                                          v[1] = p[0][1] + bevy - pos[1];                                         
230                                          in = true;
231                                  }else if(pos[1] > p[1][1] - bevy)
232                                  {
233                                          v[0] = pos[0] - (p[1][0] - bevx);
234                                          v[1] = pos[1] - (p[1][1] - bevy);      
235                                          in = true;
236                                  }                              
237                         }
238                         
239                         //if it's inside a bevelled block
240                         if(in)
241                         {
242                                 const Vector scale(bevx,bevy);
243                                 
244                                 Vector vc(v[0]/scale[0],v[1]/scale[1]);
245                                 Real    d = vc.mag();
246                                 
247                                 //if it's inside the ellipse
248                                 if(d < 1)
249                                 {
250                                         Real val = atan2(vc[1],vc[0]);
251                                         val /= (PI/2); //< will always be (0,pi/2) because both components are positive
252                                                                                 
253                                         Real fthx=1,fthy=1;
254                                         
255                                         //change d into distance away from edge
256                                         d = 1 - d;
257                                         
258                                         if(feather_x > 0)
259                                         {       
260                                                 if(scale[0] < feather_x)
261                                                 {
262                                                         fthy = scale[0]/feather_x;
263                                                 }
264                                                 
265                                                 if(d*scale[0] < feather_x)
266                                                 {
267                                                         fthx = d*scale[0]/feather_x;
268                                                 }
269                                         }
270                                         
271                                         if(feather_y > 0)
272                                         {
273                                                 if(scale[1] < feather_y)
274                                                 {
275                                                         fthx = min(fthx,scale[1]/feather_y);
276                                                 }
277                                                 
278                                                 if(d*scale[1] < feather_y)
279                                                 {
280                                                         fthy = min(fthy,d*scale[1]/feather_y);
281                                                 }
282                                         }
283                                         
284                                         //interpolate
285                                         outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
286                                         out = color;
287                                         return true;
288                                                 
289                                 }else return false;
290                         }
291                 }
292                         
293                 outamount = value * get_amount();
294                 out = color;
295                 
296                 return true;
297         }else
298                 return false;
299 }
300
301 Color
302 FilledRect::get_color(Context context, const Point &pos)const
303 {
304         Color   clr;
305         Real    amt;
306         
307         if(get_color(pos,clr,amt))
308         {
309                 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
310                         return clr;
311                 else
312                         return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
313         }
314         else
315                 return context.get_color(pos);
316 }
317
318 bool
319 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
320 {       
321         // Width and Height of a pixel
322         const Point br(renddesc.get_br()), tl(renddesc.get_tl());
323         const int w = renddesc.get_w(), h = renddesc.get_h();
324         
325         Real    wpp = (br[0]-tl[0])/w;
326         Real    hpp = (br[1]-tl[1])/h;
327         //const Real    xneg = wpp<0?-1:1;
328         //const Real    yneg = hpp<0?-1:1;      
329         
330         //the bounds of the rectangle
331         Point p[2] = {point1,point2};
332         
333         if((p[0][0] > p[1][0]) ^ wpp < 0)
334         {
335                 swap(p[0][0],p[1][0]);
336         }
337         
338         if((p[0][1] > p[1][1]) ^ hpp < 0)
339         {
340                 swap(p[0][1],p[1][1]);
341         }
342         
343         /*p[0][0] -= xneg*feather_x;
344         p[1][0] += xneg*feather_x;
345         p[0][1] -= yneg*feather_y;
346         p[1][1] += yneg*feather_y;*/
347                 
348         //the integer coordinates
349         int y_start = (int)((p[0][1] - tl[1])/hpp +.5);         //round start up
350         int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
351         int y_end = (int)((p[1][1] - tl[1])/hpp +.5);   //and ends up
352         int x_end =     (int)((p[1][0] - tl[0])/wpp +.5);
353         
354         y_start = max(0,y_start);
355         x_start = max(0,x_start);
356         y_end = min(h,y_end);
357         x_end = min(w,x_end);
358
359         SuperCallback supercb(cb,0,9000,10000);
360         
361         if(y_start >= h || x_start > w  || x_end < 0 || y_end < 0)
362         {
363                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
364                 {
365                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
366                         return false;
367                 }
368                 
369                 return true;
370         }
371         
372         Real xf_start = tl[0] + x_start*wpp;
373         Point pos(xf_start,tl[1] + y_start*hpp);
374         
375         Color   clr = Color::black();
376         Real    amt;
377         
378         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
379         {
380                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
381                 return false;
382         }
383         
384         for(int y = y_start; y < y_end; y++, pos[1] += hpp)
385         {
386                 pos[0] = xf_start;
387                 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
388                 {
389                         if(get_color(pos,clr,amt))
390                         {                               
391                                 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
392                                         (*surface)[y][x] = clr;
393                                 else
394                                         (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
395                         }
396                 }               
397         }
398         
399         return true;
400
401 #if     0       
402         //faster version but much more complex
403         //the floating point 
404         Real y1,y2,y3;
405         Real x1,x2,x3;
406         Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
407         
408         //the transparent 
409         Real fx = 0, fy = 0;
410         Real dfx,dfy;   
411
412         //Get the slopes of the lines we need to worry about
413         if(feather_x)
414         {
415                 dfx = 1/(fxsize);
416                 
417                 if(fxsize*2 > xfw)
418                 {
419                         x1 = xfw/2;
420                         x2 = 0;
421                         x3 = xfw;
422                 }else
423                 {
424                         x1 = fxsize;
425                         x2 = xfw - fxsize;
426                         x3 = xfw;
427                 }               
428         }else
429         {
430                 fx = 1;
431                 dfx = 0;
432                 x1=0;
433                 x2=xfw;
434                 x3=0;
435         }
436         
437         if(feather_y)
438         {
439                 dfy = 1/(fysize);
440                 
441                 if(fysize*2 > yfh)
442                 {
443                         y1 = yfh/2;
444                         y2 = 0;
445                         y3 = yfh;
446                 }else
447                 {
448                         y1 = fysize;
449                         y2 = yfh - fysize;
450                         y3 = yfh;
451                 }
452                 
453                 dx1 = ph*feather_x/feather_y;
454                 dx2 = -2*dx1;
455                 
456         }else
457         {
458                 fy = 1;
459                 dfy = 0;
460                 y1=0;
461                 y2=yfh;
462                 y3=0;
463         }
464
465         fy = yf*dfy;
466         
467         int x,y;
468         Real value = 0;
469         SuperCallback supercb(cb,0,9000,10000);
470         Surface::pen    p;
471         
472         Real tx1 = 0,tx2 = 
473         for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
474         {       
475                 fx = xf*dfx;
476                 
477                 p = surface->get_pen(x_start,y);                
478                 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
479                 {
480                         //we are in the x portion... use fx
481                         value = fx*get_amount();
482                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
483                                 p.put_value(color);
484                         else
485                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
486                 }
487                 
488                 for(;xf < x2; xf++,p.inc_x())
489                 {
490                         //we are now in y... use fy
491                         value = fy*get_amount();
492                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
493                                 p.put_value(color);
494                         else
495                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));                                                
496                 }
497                 
498                 fx = xfw?(xfw - xf)/xfw:1;
499                 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
500                 {
501                         //we are in the x portion... use fx
502                         value = max(0,fx*get_amount());
503                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
504                                 p.put_value(color);
505                         else
506                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));                        
507                 }
508         }
509         
510         x1 = 
511         for(;fy < 1.0; y++,yf++,fy+=dfy)
512         {
513                 fx = xf*dfx;
514                 
515                 p = surface->get_pen(x_start,y);                
516                 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
517                 {
518                         //we are in the x portion... use fx
519                         value = fx*get_amount();
520                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
521                                 p.put_value(color);
522                         else
523                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
524                 }
525                 
526                 for(;xf < x2; xf++,p.inc_x())
527                 {
528                         //we are now in y... use fy
529                         value = fy*get_amount();
530                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
531                                 p.put_value(color);
532                         else
533                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));                                                
534                 }
535                 
536                 fx = xfw?(xfw - xf)/xfw:1;
537                 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
538                 {
539                         //we are in the x portion... use fx
540                         value = max(0,fx*get_amount());
541                         if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
542                                 p.put_value(color);
543                         else
544                                 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));                        
545                 }
546         }
547         
548         for()
549         {
550                 for(int x = x_start; x < x_end; x++)
551                 {
552                         
553                 }
554         }
555
556 #endif
557
558         // Mark our progress as finished
559         if(cb && !cb->amount_complete(10000,10000))
560                 return false;
561
562         return true;
563 }