1 /* === S Y N F I G ========================================================= */
2 /*! \file filledrect.cpp
3 ** \brief Template Header
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
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>
42 #include "filledrect.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
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$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 /* === E N T R Y P O I N T ================================================= */
67 FilledRect::FilledRect():
68 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
69 color(Color::black()),
80 FilledRect::set_param(const String & param, const ValueBase &value)
90 return Layer_Composite::set_param(param,value);
94 FilledRect::get_param(const String ¶m)const
107 return Layer_Composite::get_param(param);
111 FilledRect::get_param_vocab()const
113 Layer::Vocab ret(Layer_Composite::get_param_vocab());
115 ret.push_back(ParamDesc("color")
116 .set_local_name(_("Color"))
119 ret.push_back(ParamDesc("point1")
120 .set_local_name(_("Point 1"))
123 ret.push_back(ParamDesc("point2")
124 .set_local_name(_("Point 2"))
127 ret.push_back(ParamDesc("feather_x")
128 .set_local_name(_("Feather X"))
131 ret.push_back(ParamDesc("feather_y")
132 .set_local_name(_("Feather Y"))
135 ret.push_back(ParamDesc("bevel")
136 .set_local_name(_("Bevel"))
139 ret.push_back(ParamDesc("bevCircle")
140 .set_local_name(_("Keep Bevel Circular"))
147 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
149 Point p[2] = {point1,point2};
152 if(p[0][0] > p[1][0])
159 if(p[0][1] > p[1][1])
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];
174 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
180 Real xdist = pos[0] - p[0][0];
181 xdist = min(xdist,p[1][0]-pos[0]);
183 if(xdist < feather_x)
185 value = xdist/feather_x;
191 Real ydist = pos[1]-p[0][1];
192 ydist = min(ydist,p[1][1]-pos[1]);
194 if(ydist < feather_y)
196 value = min(value,(ydist/feather_y));
200 //if we're beveled then check with ellipse code...
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;;
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)
213 if(pos[1] < p[0][1] + bevy)
215 v[0] = p[0][0] + bevx - pos[0];
216 v[1] = p[0][1] + bevy - pos[1];
218 }else if(pos[1] > p[1][1] - bevy)
220 v[0] = p[0][0] + bevx - pos[0];
221 v[1] = pos[1] - (p[1][1] - bevy);
225 else if(pos[0] > p[1][0] - bevx)
227 if(pos[1] < p[0][1] + bevy)
229 v[0] = pos[0] - (p[1][0] - bevx);
230 v[1] = p[0][1] + bevy - pos[1];
232 }else if(pos[1] > p[1][1] - bevy)
234 v[0] = pos[0] - (p[1][0] - bevx);
235 v[1] = pos[1] - (p[1][1] - bevy);
240 //if it's inside a bevelled block
243 const Vector scale(bevx,bevy);
245 Vector vc(v[0]/scale[0],v[1]/scale[1]);
248 //if it's inside the ellipse
251 Real val = atan2(vc[1],vc[0]);
252 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
256 //change d into distance away from edge
261 if(scale[0] < feather_x)
263 fthy = scale[0]/feather_x;
266 if(d*scale[0] < feather_x)
268 fthx = d*scale[0]/feather_x;
274 if(scale[1] < feather_y)
276 fthx = min(fthx,scale[1]/feather_y);
279 if(d*scale[1] < feather_y)
281 fthy = min(fthy,d*scale[1]/feather_y);
286 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
294 outamount = value * get_amount();
303 FilledRect::get_color(Context context, const Point &pos)const
308 if(get_color(pos,clr,amt))
310 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
313 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
316 return context.get_color(pos);
320 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
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();
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;
331 //the bounds of the rectangle
332 Point p[2] = {point1,point2};
334 if((p[0][0] > p[1][0]) ^ wpp < 0)
336 swap(p[0][0],p[1][0]);
339 if((p[0][1] > p[1][1]) ^ hpp < 0)
341 swap(p[0][1],p[1][1]);
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;*/
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);
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);
360 SuperCallback supercb(cb,0,9000,10000);
362 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
364 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
366 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
373 Real xf_start = tl[0] + x_start*wpp;
374 Point pos(xf_start,tl[1] + y_start*hpp);
376 Color clr = Color::black();
379 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
381 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
385 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
388 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
390 if(get_color(pos,clr,amt))
392 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
393 (*surface)[y][x] = clr;
395 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
403 //faster version but much more complex
407 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
413 //Get the slopes of the lines we need to worry about
454 dx1 = ph*feather_x/feather_y;
470 SuperCallback supercb(cb,0,9000,10000);
474 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
478 p = surface->get_pen(x_start,y);
479 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
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)
486 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
489 for(;xf < x2; xf++,p.inc_x())
491 //we are now in y... use fy
492 value = fy*get_amount();
493 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
496 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
499 fx = xfw?(xfw - xf)/xfw:1;
500 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
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)
507 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
512 for(;fy < 1.0; y++,yf++,fy+=dfy)
516 p = surface->get_pen(x_start,y);
517 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
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)
524 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
527 for(;xf < x2; xf++,p.inc_x())
529 //we are now in y... use fy
530 value = fy*get_amount();
531 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
534 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
537 fx = xfw?(xfw - xf)/xfw:1;
538 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
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)
545 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
551 for(int x = x_start; x < x_end; x++)
559 // Mark our progress as finished
560 if(cb && !cb->amount_complete(10000,10000))