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