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,"filled_rectangle");
57 SYNFIG_LAYER_SET_LOCAL_NAME(FilledRect,N_("Filled Rectangle"));
58 SYNFIG_LAYER_SET_CATEGORY(FilledRect,N_("Example"));
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_COMPOSITE),
70 color(Color::black()),
78 Layer::Vocab voc(get_param_vocab());
79 Layer::fill_static(voc);
83 FilledRect::set_param(const String & param, const ValueBase &value)
88 IMPORT_PLUS(feather_x, if(feather_x<0)feather_x=0;);
89 IMPORT_PLUS(feather_y, if(feather_y<0)feather_y=0;);
93 return Layer_Composite::set_param(param,value);
97 FilledRect::get_param(const String ¶m)const
110 return Layer_Composite::get_param(param);
114 FilledRect::get_param_vocab()const
116 Layer::Vocab ret(Layer_Composite::get_param_vocab());
118 ret.push_back(ParamDesc("color")
119 .set_local_name(_("Color"))
120 .set_description(_("Fill color of the layer"))
123 ret.push_back(ParamDesc("point1")
124 .set_local_name(_("Point 1"))
125 .set_description(_("First corner of the rectangle"))
129 ret.push_back(ParamDesc("point2")
130 .set_local_name(_("Point 2"))
131 .set_description(_("Second corner of the rectangle"))
134 ret.push_back(ParamDesc("feather_x")
135 .set_local_name(_("Feather X"))
138 ret.push_back(ParamDesc("feather_y")
139 .set_local_name(_("Feather Y"))
142 ret.push_back(ParamDesc("bevel")
143 .set_local_name(_("Bevel"))
144 .set_description(_("Use Bevel for the corners"))
147 ret.push_back(ParamDesc("bevCircle")
148 .set_local_name(_("Keep Bevel Circular"))
149 .set_description(_("When checked the bevel is circular"))
155 synfig::Layer::Handle
156 FilledRect::hit_check(synfig::Context context, const synfig::Point &point)const
161 if (!get_color(point,clr,amt))
162 return context.hit_check(point);
164 synfig::Layer::Handle tmp;
166 if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
169 if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
172 return const_cast<FilledRect*>(this);
176 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
178 Point p[2] = {point1,point2};
181 if(p[0][0] > p[1][0])
188 if(p[0][1] > p[1][1])
196 p[0][0] -= feather_x;
197 p[1][0] += feather_x;
198 p[0][1] -= feather_y;
199 p[1][1] += feather_y;*/
200 const Real w = p[1][0] - p[0][0];
201 const Real h = p[1][1] - p[0][1];
203 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
209 Real xdist = pos[0] - p[0][0];
210 xdist = min(xdist,p[1][0]-pos[0]);
212 if(xdist < feather_x)
214 value = xdist/feather_x;
220 Real ydist = pos[1]-p[0][1];
221 ydist = min(ydist,p[1][1]-pos[1]);
223 if(ydist < feather_y)
225 value = min(value,(ydist/feather_y));
229 //if we're beveled then check with ellipse code...
232 const Real bev = (bevel > 1) ? 1 : bevel;
233 const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
234 const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
239 //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
240 if(pos[0] < p[0][0] + bevx)
242 if(pos[1] < p[0][1] + bevy)
244 v[0] = p[0][0] + bevx - pos[0];
245 v[1] = p[0][1] + bevy - pos[1];
247 }else if(pos[1] > p[1][1] - bevy)
249 v[0] = p[0][0] + bevx - pos[0];
250 v[1] = pos[1] - (p[1][1] - bevy);
254 else if(pos[0] > p[1][0] - bevx)
256 if(pos[1] < p[0][1] + bevy)
258 v[0] = pos[0] - (p[1][0] - bevx);
259 v[1] = p[0][1] + bevy - pos[1];
261 }else if(pos[1] > p[1][1] - bevy)
263 v[0] = pos[0] - (p[1][0] - bevx);
264 v[1] = pos[1] - (p[1][1] - bevy);
269 //if it's inside a bevelled block
272 const Vector scale(bevx,bevy);
274 Vector vc(v[0]/scale[0],v[1]/scale[1]);
277 //if it's inside the ellipse
280 Real val = atan2(vc[1],vc[0]);
281 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
285 //change d into distance away from edge
290 if(scale[0] < feather_x)
292 fthy = scale[0]/feather_x;
295 if(d*scale[0] < feather_x)
297 fthx = d*scale[0]/feather_x;
303 if(scale[1] < feather_y)
305 fthx = min(fthx,scale[1]/feather_y);
308 if(d*scale[1] < feather_y)
310 fthy = min(fthy,d*scale[1]/feather_y);
315 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
323 outamount = value * get_amount();
332 FilledRect::get_color(Context context, const Point &pos)const
337 if(get_color(pos,clr,amt))
339 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
342 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
345 return context.get_color(pos);
349 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
351 // Width and Height of a pixel
352 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
353 const int w = renddesc.get_w(), h = renddesc.get_h();
355 Real wpp = (br[0]-tl[0])/w;
356 Real hpp = (br[1]-tl[1])/h;
357 //const Real xneg = wpp<0?-1:1;
358 //const Real yneg = hpp<0?-1:1;
360 //the bounds of the rectangle
361 Point p[2] = {point1,point2};
363 if((p[0][0] > p[1][0]) ^ (wpp < 0))
365 swap(p[0][0],p[1][0]);
368 if((p[0][1] > p[1][1]) ^ (hpp < 0))
370 swap(p[0][1],p[1][1]);
373 /*p[0][0] -= xneg*feather_x;
374 p[1][0] += xneg*feather_x;
375 p[0][1] -= yneg*feather_y;
376 p[1][1] += yneg*feather_y;*/
378 //the integer coordinates
379 int y_start = (int)((p[0][1] - tl[1])/hpp +.5); //round start up
380 int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
381 int y_end = (int)((p[1][1] - tl[1])/hpp +.5); //and ends up
382 int x_end = (int)((p[1][0] - tl[0])/wpp +.5);
384 y_start = max(0,y_start);
385 x_start = max(0,x_start);
386 y_end = min(h,y_end);
387 x_end = min(w,x_end);
389 SuperCallback supercb(cb,0,9000,10000);
391 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
393 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
395 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
402 Real xf_start = tl[0] + x_start*wpp;
403 Point pos(xf_start,tl[1] + y_start*hpp);
405 Color clr = Color::black();
408 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
410 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
414 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
417 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
419 if(get_color(pos,clr,amt))
421 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
422 (*surface)[y][x] = clr;
424 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
432 //faster version but much more complex
436 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
442 //Get the slopes of the lines we need to worry about
483 dx1 = ph*feather_x/feather_y;
499 SuperCallback supercb(cb,0,9000,10000);
503 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
507 p = surface->get_pen(x_start,y);
508 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
510 //we are in the x portion... use fx
511 value = fx*get_amount();
512 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
515 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
518 for(;xf < x2; xf++,p.inc_x())
520 //we are now in y... use fy
521 value = fy*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 fx = xfw?(xfw - xf)/xfw:1;
529 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
531 //we are in the x portion... use fx
532 value = max(0,fx*get_amount());
533 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
536 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
541 for(;fy < 1.0; y++,yf++,fy+=dfy)
545 p = surface->get_pen(x_start,y);
546 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
548 //we are in the x portion... use fx
549 value = fx*get_amount();
550 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
553 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
556 for(;xf < x2; xf++,p.inc_x())
558 //we are now in y... use fy
559 value = fy*get_amount();
560 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
563 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
566 fx = xfw?(xfw - xf)/xfw:1;
567 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
569 //we are in the x portion... use fx
570 value = max(0,fx*get_amount());
571 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
574 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
580 for(int x = x_start; x < x_end; x++)
588 // Mark our progress as finished
589 if(cb && !cb->amount_complete(10000,10000))