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"))
128 ret.push_back(ParamDesc("point2")
129 .set_local_name(_("Point 2"))
132 ret.push_back(ParamDesc("feather_x")
133 .set_local_name(_("Feather X"))
136 ret.push_back(ParamDesc("feather_y")
137 .set_local_name(_("Feather Y"))
140 ret.push_back(ParamDesc("bevel")
141 .set_local_name(_("Bevel"))
142 .set_description(_("Use Bevel for the corners"))
145 ret.push_back(ParamDesc("bevCircle")
146 .set_local_name(_("Keep Bevel Circular"))
147 .set_description(_("When checked the bevel is circular"))
153 synfig::Layer::Handle
154 FilledRect::hit_check(synfig::Context context, const synfig::Point &point)const
159 if (!get_color(point,clr,amt))
160 return context.hit_check(point);
162 synfig::Layer::Handle tmp;
164 if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
167 if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
170 return const_cast<FilledRect*>(this);
174 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
176 Point p[2] = {point1,point2};
179 if(p[0][0] > p[1][0])
186 if(p[0][1] > p[1][1])
194 p[0][0] -= feather_x;
195 p[1][0] += feather_x;
196 p[0][1] -= feather_y;
197 p[1][1] += feather_y;*/
198 const Real w = p[1][0] - p[0][0];
199 const Real h = p[1][1] - p[0][1];
201 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
207 Real xdist = pos[0] - p[0][0];
208 xdist = min(xdist,p[1][0]-pos[0]);
210 if(xdist < feather_x)
212 value = xdist/feather_x;
218 Real ydist = pos[1]-p[0][1];
219 ydist = min(ydist,p[1][1]-pos[1]);
221 if(ydist < feather_y)
223 value = min(value,(ydist/feather_y));
227 //if we're beveled then check with ellipse code...
230 const Real bev = (bevel > 1) ? 1 : bevel;
231 const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
232 const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
237 //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
238 if(pos[0] < p[0][0] + bevx)
240 if(pos[1] < p[0][1] + bevy)
242 v[0] = p[0][0] + bevx - pos[0];
243 v[1] = p[0][1] + bevy - pos[1];
245 }else if(pos[1] > p[1][1] - bevy)
247 v[0] = p[0][0] + bevx - pos[0];
248 v[1] = pos[1] - (p[1][1] - bevy);
252 else if(pos[0] > p[1][0] - bevx)
254 if(pos[1] < p[0][1] + bevy)
256 v[0] = pos[0] - (p[1][0] - bevx);
257 v[1] = p[0][1] + bevy - pos[1];
259 }else if(pos[1] > p[1][1] - bevy)
261 v[0] = pos[0] - (p[1][0] - bevx);
262 v[1] = pos[1] - (p[1][1] - bevy);
267 //if it's inside a bevelled block
270 const Vector scale(bevx,bevy);
272 Vector vc(v[0]/scale[0],v[1]/scale[1]);
275 //if it's inside the ellipse
278 Real val = atan2(vc[1],vc[0]);
279 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
283 //change d into distance away from edge
288 if(scale[0] < feather_x)
290 fthy = scale[0]/feather_x;
293 if(d*scale[0] < feather_x)
295 fthx = d*scale[0]/feather_x;
301 if(scale[1] < feather_y)
303 fthx = min(fthx,scale[1]/feather_y);
306 if(d*scale[1] < feather_y)
308 fthy = min(fthy,d*scale[1]/feather_y);
313 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
321 outamount = value * get_amount();
330 FilledRect::get_color(Context context, const Point &pos)const
335 if(get_color(pos,clr,amt))
337 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
340 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
343 return context.get_color(pos);
347 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
349 // Width and Height of a pixel
350 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
351 const int w = renddesc.get_w(), h = renddesc.get_h();
353 Real wpp = (br[0]-tl[0])/w;
354 Real hpp = (br[1]-tl[1])/h;
355 //const Real xneg = wpp<0?-1:1;
356 //const Real yneg = hpp<0?-1:1;
358 //the bounds of the rectangle
359 Point p[2] = {point1,point2};
361 if((p[0][0] > p[1][0]) ^ (wpp < 0))
363 swap(p[0][0],p[1][0]);
366 if((p[0][1] > p[1][1]) ^ (hpp < 0))
368 swap(p[0][1],p[1][1]);
371 /*p[0][0] -= xneg*feather_x;
372 p[1][0] += xneg*feather_x;
373 p[0][1] -= yneg*feather_y;
374 p[1][1] += yneg*feather_y;*/
376 //the integer coordinates
377 int y_start = (int)((p[0][1] - tl[1])/hpp +.5); //round start up
378 int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
379 int y_end = (int)((p[1][1] - tl[1])/hpp +.5); //and ends up
380 int x_end = (int)((p[1][0] - tl[0])/wpp +.5);
382 y_start = max(0,y_start);
383 x_start = max(0,x_start);
384 y_end = min(h,y_end);
385 x_end = min(w,x_end);
387 SuperCallback supercb(cb,0,9000,10000);
389 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
391 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
393 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
400 Real xf_start = tl[0] + x_start*wpp;
401 Point pos(xf_start,tl[1] + y_start*hpp);
403 Color clr = Color::black();
406 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
408 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
412 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
415 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
417 if(get_color(pos,clr,amt))
419 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
420 (*surface)[y][x] = clr;
422 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
430 //faster version but much more complex
434 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
440 //Get the slopes of the lines we need to worry about
481 dx1 = ph*feather_x/feather_y;
497 SuperCallback supercb(cb,0,9000,10000);
501 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
505 p = surface->get_pen(x_start,y);
506 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
508 //we are in the x portion... use fx
509 value = fx*get_amount();
510 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
513 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
516 for(;xf < x2; xf++,p.inc_x())
518 //we are now in y... use fy
519 value = fy*get_amount();
520 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
523 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
526 fx = xfw?(xfw - xf)/xfw:1;
527 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
529 //we are in the x portion... use fx
530 value = max(0,fx*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()));
539 for(;fy < 1.0; y++,yf++,fy+=dfy)
543 p = surface->get_pen(x_start,y);
544 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
546 //we are in the x portion... use fx
547 value = fx*get_amount();
548 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
551 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
554 for(;xf < x2; xf++,p.inc_x())
556 //we are now in y... use fy
557 value = fy*get_amount();
558 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
561 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
564 fx = xfw?(xfw - xf)/xfw:1;
565 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
567 //we are in the x portion... use fx
568 value = max(0,fx*get_amount());
569 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
572 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
578 for(int x = x_start; x < x_end; x++)
586 // Mark our progress as finished
587 if(cb && !cb->amount_complete(10000,10000))