1 /* === S Y N F I G ========================================================= */
2 /*! \file filledrect.cpp
3 ** \brief Implementation of the "Rectangle" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
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>
43 #include "filledrect.h"
47 /* === U S I N G =========================================================== */
51 using namespace synfig;
53 /* === G L O B A L S ======================================================= */
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$");
62 /* === P R O C E D U R E S ================================================= */
64 /* === M E T H O D S ======================================================= */
66 /* === E N T R Y P O I N T ================================================= */
68 FilledRect::FilledRect():
69 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
70 color(Color::black()),
81 FilledRect::set_param(const String & param, const ValueBase &value)
86 IMPORT_PLUS(feather_x, if(feather_x<0)feather_x=0;);
87 IMPORT_PLUS(feather_y, if(feather_y<0)feather_y=0;);
91 return Layer_Composite::set_param(param,value);
95 FilledRect::get_param(const String ¶m)const
108 return Layer_Composite::get_param(param);
112 FilledRect::get_param_vocab()const
114 Layer::Vocab ret(Layer_Composite::get_param_vocab());
116 ret.push_back(ParamDesc("color")
117 .set_local_name(_("Color"))
120 ret.push_back(ParamDesc("point1")
121 .set_local_name(_("Point 1"))
124 ret.push_back(ParamDesc("point2")
125 .set_local_name(_("Point 2"))
128 ret.push_back(ParamDesc("feather_x")
129 .set_local_name(_("Feather X"))
132 ret.push_back(ParamDesc("feather_y")
133 .set_local_name(_("Feather Y"))
136 ret.push_back(ParamDesc("bevel")
137 .set_local_name(_("Bevel"))
140 ret.push_back(ParamDesc("bevCircle")
141 .set_local_name(_("Keep Bevel Circular"))
148 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
150 Point p[2] = {point1,point2};
153 if(p[0][0] > p[1][0])
160 if(p[0][1] > p[1][1])
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];
175 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
181 Real xdist = pos[0] - p[0][0];
182 xdist = min(xdist,p[1][0]-pos[0]);
184 if(xdist < feather_x)
186 value = xdist/feather_x;
192 Real ydist = pos[1]-p[0][1];
193 ydist = min(ydist,p[1][1]-pos[1]);
195 if(ydist < feather_y)
197 value = min(value,(ydist/feather_y));
201 //if we're beveled then check with ellipse code...
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;;
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)
214 if(pos[1] < p[0][1] + bevy)
216 v[0] = p[0][0] + bevx - pos[0];
217 v[1] = p[0][1] + bevy - pos[1];
219 }else if(pos[1] > p[1][1] - bevy)
221 v[0] = p[0][0] + bevx - pos[0];
222 v[1] = pos[1] - (p[1][1] - bevy);
226 else if(pos[0] > p[1][0] - bevx)
228 if(pos[1] < p[0][1] + bevy)
230 v[0] = pos[0] - (p[1][0] - bevx);
231 v[1] = p[0][1] + bevy - pos[1];
233 }else if(pos[1] > p[1][1] - bevy)
235 v[0] = pos[0] - (p[1][0] - bevx);
236 v[1] = pos[1] - (p[1][1] - bevy);
241 //if it's inside a bevelled block
244 const Vector scale(bevx,bevy);
246 Vector vc(v[0]/scale[0],v[1]/scale[1]);
249 //if it's inside the ellipse
252 Real val = atan2(vc[1],vc[0]);
253 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
257 //change d into distance away from edge
262 if(scale[0] < feather_x)
264 fthy = scale[0]/feather_x;
267 if(d*scale[0] < feather_x)
269 fthx = d*scale[0]/feather_x;
275 if(scale[1] < feather_y)
277 fthx = min(fthx,scale[1]/feather_y);
280 if(d*scale[1] < feather_y)
282 fthy = min(fthy,d*scale[1]/feather_y);
287 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
295 outamount = value * get_amount();
304 FilledRect::get_color(Context context, const Point &pos)const
309 if(get_color(pos,clr,amt))
311 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
314 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
317 return context.get_color(pos);
321 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
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();
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;
332 //the bounds of the rectangle
333 Point p[2] = {point1,point2};
335 if((p[0][0] > p[1][0]) ^ wpp < 0)
337 swap(p[0][0],p[1][0]);
340 if((p[0][1] > p[1][1]) ^ hpp < 0)
342 swap(p[0][1],p[1][1]);
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;*/
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);
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);
361 SuperCallback supercb(cb,0,9000,10000);
363 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
365 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
367 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
374 Real xf_start = tl[0] + x_start*wpp;
375 Point pos(xf_start,tl[1] + y_start*hpp);
377 Color clr = Color::black();
380 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
382 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
386 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
389 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
391 if(get_color(pos,clr,amt))
393 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
394 (*surface)[y][x] = clr;
396 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
404 //faster version but much more complex
408 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
414 //Get the slopes of the lines we need to worry about
455 dx1 = ph*feather_x/feather_y;
471 SuperCallback supercb(cb,0,9000,10000);
475 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
479 p = surface->get_pen(x_start,y);
480 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
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)
487 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
490 for(;xf < x2; xf++,p.inc_x())
492 //we are now in y... use fy
493 value = fy*get_amount();
494 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
497 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
500 fx = xfw?(xfw - xf)/xfw:1;
501 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
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)
508 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
513 for(;fy < 1.0; y++,yf++,fy+=dfy)
517 p = surface->get_pen(x_start,y);
518 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
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)
525 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
528 for(;xf < x2; xf++,p.inc_x())
530 //we are now in y... use fy
531 value = fy*get_amount();
532 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
535 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
538 fx = xfw?(xfw - xf)/xfw:1;
539 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
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)
546 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
552 for(int x = x_start; x < x_end; x++)
560 // Mark our progress as finished
561 if(cb && !cb->amount_complete(10000,10000))