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_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"))
147 synfig::Layer::Handle
148 FilledRect::hit_check(synfig::Context context, const synfig::Point &point)const
153 if (!get_color(point,clr,amt))
154 return context.hit_check(point);
156 synfig::Layer::Handle tmp;
158 if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
161 if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
164 return const_cast<FilledRect*>(this);
168 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
170 Point p[2] = {point1,point2};
173 if(p[0][0] > p[1][0])
180 if(p[0][1] > p[1][1])
188 p[0][0] -= feather_x;
189 p[1][0] += feather_x;
190 p[0][1] -= feather_y;
191 p[1][1] += feather_y;*/
192 const Real w = p[1][0] - p[0][0];
193 const Real h = p[1][1] - p[0][1];
195 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
201 Real xdist = pos[0] - p[0][0];
202 xdist = min(xdist,p[1][0]-pos[0]);
204 if(xdist < feather_x)
206 value = xdist/feather_x;
212 Real ydist = pos[1]-p[0][1];
213 ydist = min(ydist,p[1][1]-pos[1]);
215 if(ydist < feather_y)
217 value = min(value,(ydist/feather_y));
221 //if we're beveled then check with ellipse code...
224 const Real bev = (bevel > 1) ? 1 : bevel;
225 const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
226 const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
231 //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
232 if(pos[0] < p[0][0] + bevx)
234 if(pos[1] < p[0][1] + bevy)
236 v[0] = p[0][0] + bevx - pos[0];
237 v[1] = p[0][1] + bevy - pos[1];
239 }else if(pos[1] > p[1][1] - bevy)
241 v[0] = p[0][0] + bevx - pos[0];
242 v[1] = pos[1] - (p[1][1] - bevy);
246 else if(pos[0] > p[1][0] - bevx)
248 if(pos[1] < p[0][1] + bevy)
250 v[0] = pos[0] - (p[1][0] - bevx);
251 v[1] = p[0][1] + bevy - pos[1];
253 }else if(pos[1] > p[1][1] - bevy)
255 v[0] = pos[0] - (p[1][0] - bevx);
256 v[1] = pos[1] - (p[1][1] - bevy);
261 //if it's inside a bevelled block
264 const Vector scale(bevx,bevy);
266 Vector vc(v[0]/scale[0],v[1]/scale[1]);
269 //if it's inside the ellipse
272 Real val = atan2(vc[1],vc[0]);
273 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
277 //change d into distance away from edge
282 if(scale[0] < feather_x)
284 fthy = scale[0]/feather_x;
287 if(d*scale[0] < feather_x)
289 fthx = d*scale[0]/feather_x;
295 if(scale[1] < feather_y)
297 fthx = min(fthx,scale[1]/feather_y);
300 if(d*scale[1] < feather_y)
302 fthy = min(fthy,d*scale[1]/feather_y);
307 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
315 outamount = value * get_amount();
324 FilledRect::get_color(Context context, const Point &pos)const
329 if(get_color(pos,clr,amt))
331 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
334 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
337 return context.get_color(pos);
341 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
343 // Width and Height of a pixel
344 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
345 const int w = renddesc.get_w(), h = renddesc.get_h();
347 Real wpp = (br[0]-tl[0])/w;
348 Real hpp = (br[1]-tl[1])/h;
349 //const Real xneg = wpp<0?-1:1;
350 //const Real yneg = hpp<0?-1:1;
352 //the bounds of the rectangle
353 Point p[2] = {point1,point2};
355 if((p[0][0] > p[1][0]) ^ (wpp < 0))
357 swap(p[0][0],p[1][0]);
360 if((p[0][1] > p[1][1]) ^ (hpp < 0))
362 swap(p[0][1],p[1][1]);
365 /*p[0][0] -= xneg*feather_x;
366 p[1][0] += xneg*feather_x;
367 p[0][1] -= yneg*feather_y;
368 p[1][1] += yneg*feather_y;*/
370 //the integer coordinates
371 int y_start = (int)((p[0][1] - tl[1])/hpp +.5); //round start up
372 int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
373 int y_end = (int)((p[1][1] - tl[1])/hpp +.5); //and ends up
374 int x_end = (int)((p[1][0] - tl[0])/wpp +.5);
376 y_start = max(0,y_start);
377 x_start = max(0,x_start);
378 y_end = min(h,y_end);
379 x_end = min(w,x_end);
381 SuperCallback supercb(cb,0,9000,10000);
383 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
385 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
387 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
394 Real xf_start = tl[0] + x_start*wpp;
395 Point pos(xf_start,tl[1] + y_start*hpp);
397 Color clr = Color::black();
400 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
402 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
406 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
409 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
411 if(get_color(pos,clr,amt))
413 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
414 (*surface)[y][x] = clr;
416 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
424 //faster version but much more complex
428 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
434 //Get the slopes of the lines we need to worry about
475 dx1 = ph*feather_x/feather_y;
491 SuperCallback supercb(cb,0,9000,10000);
495 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
499 p = surface->get_pen(x_start,y);
500 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
502 //we are in the x portion... use fx
503 value = 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()));
510 for(;xf < x2; xf++,p.inc_x())
512 //we are now in y... use fy
513 value = fy*get_amount();
514 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
517 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
520 fx = xfw?(xfw - xf)/xfw:1;
521 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
523 //we are in the x portion... use fx
524 value = max(0,fx*get_amount());
525 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
528 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
533 for(;fy < 1.0; y++,yf++,fy+=dfy)
537 p = surface->get_pen(x_start,y);
538 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
540 //we are in the x portion... use fx
541 value = 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()));
548 for(;xf < x2; xf++,p.inc_x())
550 //we are now in y... use fy
551 value = fy*get_amount();
552 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
555 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
558 fx = xfw?(xfw - xf)/xfw:1;
559 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
561 //we are in the x portion... use fx
562 value = max(0,fx*get_amount());
563 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
566 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
572 for(int x = x_start; x < x_end; x++)
580 // Mark our progress as finished
581 if(cb && !cb->amount_complete(10000,10000))