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