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"))
125 ret.push_back(ParamDesc("point2")
126 .set_local_name(_("Point 2"))
129 ret.push_back(ParamDesc("feather_x")
130 .set_local_name(_("Feather X"))
133 ret.push_back(ParamDesc("feather_y")
134 .set_local_name(_("Feather Y"))
137 ret.push_back(ParamDesc("bevel")
138 .set_local_name(_("Bevel"))
141 ret.push_back(ParamDesc("bevCircle")
142 .set_local_name(_("Keep Bevel Circular"))
148 synfig::Layer::Handle
149 FilledRect::hit_check(synfig::Context context, const synfig::Point &point)const
154 if (!get_color(point,clr,amt))
155 return context.hit_check(point);
157 synfig::Layer::Handle tmp;
159 if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
162 if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
165 return const_cast<FilledRect*>(this);
169 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
171 Point p[2] = {point1,point2};
174 if(p[0][0] > p[1][0])
181 if(p[0][1] > p[1][1])
189 p[0][0] -= feather_x;
190 p[1][0] += feather_x;
191 p[0][1] -= feather_y;
192 p[1][1] += feather_y;*/
193 const Real w = p[1][0] - p[0][0];
194 const Real h = p[1][1] - p[0][1];
196 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
202 Real xdist = pos[0] - p[0][0];
203 xdist = min(xdist,p[1][0]-pos[0]);
205 if(xdist < feather_x)
207 value = xdist/feather_x;
213 Real ydist = pos[1]-p[0][1];
214 ydist = min(ydist,p[1][1]-pos[1]);
216 if(ydist < feather_y)
218 value = min(value,(ydist/feather_y));
222 //if we're beveled then check with ellipse code...
225 const Real bev = (bevel > 1) ? 1 : bevel;
226 const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
227 const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
232 //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
233 if(pos[0] < p[0][0] + bevx)
235 if(pos[1] < p[0][1] + bevy)
237 v[0] = p[0][0] + bevx - pos[0];
238 v[1] = p[0][1] + bevy - pos[1];
240 }else if(pos[1] > p[1][1] - bevy)
242 v[0] = p[0][0] + bevx - pos[0];
243 v[1] = pos[1] - (p[1][1] - bevy);
247 else if(pos[0] > p[1][0] - bevx)
249 if(pos[1] < p[0][1] + bevy)
251 v[0] = pos[0] - (p[1][0] - bevx);
252 v[1] = p[0][1] + bevy - pos[1];
254 }else if(pos[1] > p[1][1] - bevy)
256 v[0] = pos[0] - (p[1][0] - bevx);
257 v[1] = pos[1] - (p[1][1] - bevy);
262 //if it's inside a bevelled block
265 const Vector scale(bevx,bevy);
267 Vector vc(v[0]/scale[0],v[1]/scale[1]);
270 //if it's inside the ellipse
273 Real val = atan2(vc[1],vc[0]);
274 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
278 //change d into distance away from edge
283 if(scale[0] < feather_x)
285 fthy = scale[0]/feather_x;
288 if(d*scale[0] < feather_x)
290 fthx = d*scale[0]/feather_x;
296 if(scale[1] < feather_y)
298 fthx = min(fthx,scale[1]/feather_y);
301 if(d*scale[1] < feather_y)
303 fthy = min(fthy,d*scale[1]/feather_y);
308 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
316 outamount = value * get_amount();
325 FilledRect::get_color(Context context, const Point &pos)const
330 if(get_color(pos,clr,amt))
332 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
335 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
338 return context.get_color(pos);
342 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
344 // Width and Height of a pixel
345 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
346 const int w = renddesc.get_w(), h = renddesc.get_h();
348 Real wpp = (br[0]-tl[0])/w;
349 Real hpp = (br[1]-tl[1])/h;
350 //const Real xneg = wpp<0?-1:1;
351 //const Real yneg = hpp<0?-1:1;
353 //the bounds of the rectangle
354 Point p[2] = {point1,point2};
356 if((p[0][0] > p[1][0]) ^ (wpp < 0))
358 swap(p[0][0],p[1][0]);
361 if((p[0][1] > p[1][1]) ^ (hpp < 0))
363 swap(p[0][1],p[1][1]);
366 /*p[0][0] -= xneg*feather_x;
367 p[1][0] += xneg*feather_x;
368 p[0][1] -= yneg*feather_y;
369 p[1][1] += yneg*feather_y;*/
371 //the integer coordinates
372 int y_start = (int)((p[0][1] - tl[1])/hpp +.5); //round start up
373 int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
374 int y_end = (int)((p[1][1] - tl[1])/hpp +.5); //and ends up
375 int x_end = (int)((p[1][0] - tl[0])/wpp +.5);
377 y_start = max(0,y_start);
378 x_start = max(0,x_start);
379 y_end = min(h,y_end);
380 x_end = min(w,x_end);
382 SuperCallback supercb(cb,0,9000,10000);
384 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
386 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
388 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
395 Real xf_start = tl[0] + x_start*wpp;
396 Point pos(xf_start,tl[1] + y_start*hpp);
398 Color clr = Color::black();
401 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
403 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
407 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
410 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
412 if(get_color(pos,clr,amt))
414 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
415 (*surface)[y][x] = clr;
417 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
425 //faster version but much more complex
429 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
435 //Get the slopes of the lines we need to worry about
476 dx1 = ph*feather_x/feather_y;
492 SuperCallback supercb(cb,0,9000,10000);
496 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
500 p = surface->get_pen(x_start,y);
501 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
503 //we are in the x portion... use fx
504 value = 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()));
511 for(;xf < x2; xf++,p.inc_x())
513 //we are now in y... use fy
514 value = fy*get_amount();
515 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
518 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
521 fx = xfw?(xfw - xf)/xfw:1;
522 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
524 //we are in the x portion... use fx
525 value = max(0,fx*get_amount());
526 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
529 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
534 for(;fy < 1.0; y++,yf++,fy+=dfy)
538 p = surface->get_pen(x_start,y);
539 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
541 //we are in the x portion... use fx
542 value = 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()));
549 for(;xf < x2; xf++,p.inc_x())
551 //we are now in y... use fy
552 value = fy*get_amount();
553 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
556 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
559 fx = xfw?(xfw - xf)/xfw:1;
560 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
562 //we are in the x portion... use fx
563 value = max(0,fx*get_amount());
564 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
567 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
573 for(int x = x_start; x < x_end; x++)
581 // Mark our progress as finished
582 if(cb && !cb->amount_complete(10000,10000))