1 /* === S I N F G =========================================================== */
2 /*! \file filledrect.cpp
3 ** \brief Template Header
5 ** $Id: filledrect.cpp,v 1.1.1.1 2005/01/04 01:23:09 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <sinfg/string.h>
32 #include <sinfg/time.h>
33 #include <sinfg/context.h>
34 #include <sinfg/paramdesc.h>
35 #include <sinfg/renddesc.h>
36 #include <sinfg/surface.h>
37 #include <sinfg/value.h>
38 #include <sinfg/valuenode.h>
41 #include "filledrect.h"
45 /* === U S I N G =========================================================== */
49 using namespace sinfg;
51 /* === G L O B A L S ======================================================= */
53 SINFG_LAYER_INIT(FilledRect);
54 SINFG_LAYER_SET_NAME(FilledRect,"rectangle");
55 SINFG_LAYER_SET_LOCAL_NAME(FilledRect,_("Rectangle"));
56 SINFG_LAYER_SET_CATEGORY(FilledRect,_("Geometry"));
57 SINFG_LAYER_SET_VERSION(FilledRect,"0.1");
58 SINFG_LAYER_SET_CVS_ID(FilledRect,"$Id: filledrect.cpp,v 1.1.1.1 2005/01/04 01:23:09 darco Exp $");
60 /* === P R O C E D U R E S ================================================= */
62 /* === M E T H O D S ======================================================= */
64 /* === E N T R Y P O I N T ================================================= */
66 FilledRect::FilledRect():
67 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
68 color(Color::black()),
79 FilledRect::set_param(const String & param, const ValueBase &value)
89 return Layer_Composite::set_param(param,value);
93 FilledRect::get_param(const String ¶m)const
106 return Layer_Composite::get_param(param);
110 FilledRect::get_param_vocab()const
112 Layer::Vocab ret(Layer_Composite::get_param_vocab());
114 ret.push_back(ParamDesc("color")
115 .set_local_name(_("Color"))
118 ret.push_back(ParamDesc("point1")
119 .set_local_name(_("Point 1"))
122 ret.push_back(ParamDesc("point2")
123 .set_local_name(_("Point 2"))
126 ret.push_back(ParamDesc("feather_x")
127 .set_local_name(_("Feather X"))
130 ret.push_back(ParamDesc("feather_y")
131 .set_local_name(_("Feather Y"))
134 ret.push_back(ParamDesc("bevel")
135 .set_local_name(_("Bevel"))
138 ret.push_back(ParamDesc("bevCircle")
139 .set_local_name(_("Keep Bevel Circular"))
146 FilledRect::get_color(const Point &pos, Color &out, Real &outamount) const
148 Point p[2] = {point1,point2};
151 if(p[0][0] > p[1][0])
158 if(p[0][1] > p[1][1])
166 p[0][0] -= feather_x;
167 p[1][0] += feather_x;
168 p[0][1] -= feather_y;
169 p[1][1] += feather_y;*/
170 const Real w = p[1][0] - p[0][0];
171 const Real h = p[1][1] - p[0][1];
173 if(pos[0] >= p[0][0] && pos[0] <= p[1][0] && pos[1] >= p[0][1] && pos[1] <= p[1][1])
179 Real xdist = pos[0] - p[0][0];
180 xdist = min(xdist,p[1][0]-pos[0]);
182 if(xdist < feather_x)
184 value = xdist/feather_x;
190 Real ydist = pos[1]-p[0][1];
191 ydist = min(ydist,p[1][1]-pos[1]);
193 if(ydist < feather_y)
195 value = min(value,(ydist/feather_y));
199 //if we're beveled then check with ellipse code...
202 const Real bev = (bevel > 1) ? 1 : bevel;
203 const Real bevx = bevCircle ? min(w*bev/2,h*bev/2) : w*bev/2;
204 const Real bevy = bevCircle ? min(w*bev/2,h*bev/2) : h*bev/2;;
209 //based on which quarter it is in (and because it's x/y symmetric) get a positive vector (x/y)
210 if(pos[0] < p[0][0] + bevx)
212 if(pos[1] < p[0][1] + bevy)
214 v[0] = p[0][0] + bevx - pos[0];
215 v[1] = p[0][1] + bevy - pos[1];
217 }else if(pos[1] > p[1][1] - bevy)
219 v[0] = p[0][0] + bevx - pos[0];
220 v[1] = pos[1] - (p[1][1] - bevy);
224 else if(pos[0] > p[1][0] - bevx)
226 if(pos[1] < p[0][1] + bevy)
228 v[0] = pos[0] - (p[1][0] - bevx);
229 v[1] = p[0][1] + bevy - pos[1];
231 }else if(pos[1] > p[1][1] - bevy)
233 v[0] = pos[0] - (p[1][0] - bevx);
234 v[1] = pos[1] - (p[1][1] - bevy);
239 //if it's inside a bevelled block
242 const Vector scale(bevx,bevy);
244 Vector vc(v[0]/scale[0],v[1]/scale[1]);
247 //if it's inside the ellipse
250 Real val = atan2(vc[1],vc[0]);
251 val /= (PI/2); //< will always be (0,pi/2) because both components are positive
255 //change d into distance away from edge
260 if(scale[0] < feather_x)
262 fthy = scale[0]/feather_x;
265 if(d*scale[0] < feather_x)
267 fthx = d*scale[0]/feather_x;
273 if(scale[1] < feather_y)
275 fthx = min(fthx,scale[1]/feather_y);
278 if(d*scale[1] < feather_y)
280 fthy = min(fthy,d*scale[1]/feather_y);
285 outamount = min(value,((1-val)*fthx + val*fthy)) * get_amount();
293 outamount = value * get_amount();
302 FilledRect::get_color(Context context, const Point &pos)const
307 if(get_color(pos,clr,amt))
309 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
312 return Color::blend(clr,context.get_color(pos),amt,get_blend_method());
315 return context.get_color(pos);
319 FilledRect::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
321 // Width and Height of a pixel
322 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
323 const int w = renddesc.get_w(), h = renddesc.get_h();
325 Real wpp = (br[0]-tl[0])/w;
326 Real hpp = (br[1]-tl[1])/h;
327 //const Real xneg = wpp<0?-1:1;
328 //const Real yneg = hpp<0?-1:1;
330 //the bounds of the rectangle
331 Point p[2] = {point1,point2};
333 if((p[0][0] > p[1][0]) ^ wpp < 0)
335 swap(p[0][0],p[1][0]);
338 if((p[0][1] > p[1][1]) ^ hpp < 0)
340 swap(p[0][1],p[1][1]);
343 /*p[0][0] -= xneg*feather_x;
344 p[1][0] += xneg*feather_x;
345 p[0][1] -= yneg*feather_y;
346 p[1][1] += yneg*feather_y;*/
348 //the integer coordinates
349 int y_start = (int)((p[0][1] - tl[1])/hpp +.5); //round start up
350 int x_start = (int)((p[0][0] - tl[0])/wpp +.5);
351 int y_end = (int)((p[1][1] - tl[1])/hpp +.5); //and ends up
352 int x_end = (int)((p[1][0] - tl[0])/wpp +.5);
354 y_start = max(0,y_start);
355 x_start = max(0,x_start);
356 y_end = min(h,y_end);
357 x_end = min(w,x_end);
359 SuperCallback supercb(cb,0,9000,10000);
361 if(y_start >= h || x_start > w || x_end < 0 || y_end < 0)
363 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
365 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
372 Real xf_start = tl[0] + x_start*wpp;
373 Point pos(xf_start,tl[1] + y_start*hpp);
375 Color clr = Color::black();
378 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
380 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
384 for(int y = y_start; y < y_end; y++, pos[1] += hpp)
387 for(int x = x_start; x < x_end; x++, pos[0] += wpp)
389 if(get_color(pos,clr,amt))
391 if(amt==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
392 (*surface)[y][x] = clr;
394 (*surface)[y][x] = Color::blend(clr,(*surface)[y][x],amt,get_blend_method());
402 //faster version but much more complex
406 Real dx1,dx2; //reversed in 3rd y section, not used in 2nd
412 //Get the slopes of the lines we need to worry about
453 dx1 = ph*feather_x/feather_y;
469 SuperCallback supercb(cb,0,9000,10000);
473 for(y = y_start;yf < y1; y++,yf++,fy+=dfy, tx1+=dx1)
477 p = surface->get_pen(x_start,y);
478 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
480 //we are in the x portion... use fx
481 value = fx*get_amount();
482 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
485 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
488 for(;xf < x2; xf++,p.inc_x())
490 //we are now in y... use fy
491 value = fy*get_amount();
492 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
495 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
498 fx = xfw?(xfw - xf)/xfw:1;
499 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
501 //we are in the x portion... use fx
502 value = max(0,fx*get_amount());
503 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
506 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
511 for(;fy < 1.0; y++,yf++,fy+=dfy)
515 p = surface->get_pen(x_start,y);
516 for(; xf < x1; xf++,p.inc_x(), fx+=dfx)
518 //we are in the x portion... use fx
519 value = fx*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 for(;xf < x2; xf++,p.inc_x())
528 //we are now in y... use fy
529 value = fy*get_amount();
530 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
533 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
536 fx = xfw?(xfw - xf)/xfw:1;
537 for(;xf < x3 && ; xf++,p.inc_x(), fx-=dfx)
539 //we are in the x portion... use fx
540 value = max(0,fx*get_amount());
541 if(value==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
544 p.put_value(Color::blend(color,p.get_value(),value,get_blend_method()));
550 for(int x = x_start; x < x_end; x++)
558 // Mark our progress as finished
559 if(cb && !cb->amount_complete(10000,10000))