1 /* === S Y N F I G ========================================================= */
2 /*! \file rectangle.cpp
3 ** \point2ief Template Header
5 ** $Id: rectangle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quatpoint1ebaum Jr.
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <synfig/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
43 #include "rectangle.h"
47 /* === U S I N G =========================================================== */
51 using namespace synfig;
53 /* === G L O B A L S ======================================================= */
55 SYNFIG_LAYER_INIT(Rectangle);
56 SYNFIG_LAYER_SET_NAME(Rectangle,"rectangle");
57 SYNFIG_LAYER_SET_LOCAL_NAME(Rectangle,_("Rectangle"));
58 SYNFIG_LAYER_SET_CATEGORY(Rectangle,_("Geometry"));
59 SYNFIG_LAYER_SET_VERSION(Rectangle,"0.2");
60 SYNFIG_LAYER_SET_CVS_ID(Rectangle,"$Id: rectangle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
62 /* === P R O C E D U R E S ================================================= */
65 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
66 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
68 inline int floor_to_int(const float x) { return static_cast<int>(floor(x)); }
69 inline int floor_to_int(const double x) { return static_cast<int>(floor(x)); }
72 /* === M E T H O D S ======================================================= */
74 /* === E N T R Y P O I N T ================================================= */
76 Rectangle::Rectangle():
77 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
78 color(Color::black()),
87 Rectangle::set_param(const String & param, const ValueBase &value)
95 return Layer_Composite::set_param(param,value);
99 Rectangle::get_param(const String ¶m)const
110 return Layer_Composite::get_param(param);
114 Rectangle::get_param_vocab()const
116 Layer::Vocab ret(Layer_Composite::get_param_vocab());
118 ret.push_back(ParamDesc("color")
119 .set_local_name(_("Color"))
122 ret.push_back(ParamDesc("point1")
123 .set_local_name(_("Point 1"))
127 ret.push_back(ParamDesc("point2")
128 .set_local_name(_("Point 2"))
131 ret.push_back(ParamDesc("expand")
133 .set_local_name(_("Expand amount"))
136 ret.push_back(ParamDesc("invert")
137 .set_local_name(_("Invert the rectangle"))
143 synfig::Layer::Handle
144 Rectangle::hit_check(synfig::Context context, const synfig::Point &pos)const
147 return context.hit_check(pos);
151 max[0]=std::max(point1[0],point2[0])+expand;
152 max[1]=std::max(point1[1],point2[1])+expand;
153 min[0]=std::min(point1[0],point2[0])-expand;
154 min[1]=std::min(point1[1],point2[1])-expand;
156 bool intersect(false);
158 if( pos[0]<max[0] && pos[0]>min[0] &&
159 pos[1]<max[1] && pos[1]>min[1] )
165 intersect=!intersect;
169 synfig::Layer::Handle tmp;
170 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(pos)))
172 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(pos)))
174 return const_cast<Rectangle*>(this);
177 return context.hit_check(pos);
181 Rectangle::get_color(Context context, const Point &pos)const
184 return context.get_color(pos);
188 max[0]=std::max(point1[0],point2[0])+expand;
189 max[1]=std::max(point1[1],point2[1])+expand;
190 min[0]=std::min(point1[0],point2[0])-expand;
191 min[1]=std::min(point1[1],point2[1])-expand;
193 /**************************
194 // This is darco's old-old-old feathered box code
195 // it produces really nice feathered edges
198 if( pos[0]<=max[0]-feather/2.0 && pos[0]>=min[0]+feather/2.0 &&
199 pos[1]<=max[1]-feather/2.0 && pos[1]>=min[1]+feather/2.0 )
202 return (*context).GetColor(context,pos);
207 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
208 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
213 return (*context).GetColor(context,pos);
216 Color::unit alpha=1000000;
217 Color::unit alpha2=1000000;
219 if(max[0]-pos[0]+feather/2.0<alpha)
220 alpha=max[0]-pos[0]+feather/2.0;
221 if(pos[0]-min[0]+feather/2.0<alpha)
222 alpha=pos[0]-min[0]+feather/2.0;
224 if(max[1]-pos[1]+feather/2.0<alpha2)
225 alpha2=max[1]-pos[1]+feather/2.0;
226 if(pos[1]-min[1]+feather/2.0<alpha2)
227 alpha2=pos[1]-min[1]+feather/2.0;
230 if(alpha<=feather && alpha2<=feather)
233 alpha2=feather-alpha2;
235 alpha=sqrt(alpha*alpha+alpha2*alpha2);
242 return (*context).GetColor(context,pos);
249 alpha=(alpha<alpha2)?alpha:alpha2;
257 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
262 if( pos[0]<max[0] && pos[0]>min[0] &&
263 pos[1]<max[1] && pos[1]>min[1] )
266 return context.get_color(pos);
272 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
282 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
285 return context.get_color(pos);
289 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
292 return context.accelerated_render(surface,quality,renddesc,cb);
294 const Point tl(renddesc.get_tl());
295 const Point br(renddesc.get_br());
297 const int w(renddesc.get_w());
298 const int h(renddesc.get_h());
300 // Width and Height of a pixel
301 const Real pw = (br[0] - tl[0]) / w;
302 const Real ph = (br[1] - tl[1]) / h;
304 Point max(point1),min(point2);
313 max=context.get_bounding_rect().get_max();
314 min=context.get_bounding_rect().get_min();
318 max=context.get_full_bounding_rect().get_max();
319 min=context.get_full_bounding_rect().get_min();
327 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
328 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
354 int left(floor_to_int((min[0]-tl[0])/pw));
355 int right(ceil_to_int((max[0]-tl[0])/pw));
356 int top(floor_to_int((min[1]-tl[1])/ph));
357 int bottom(ceil_to_int((max[1]-tl[1])/ph));
359 float left_edge((min[0]-tl[0])/pw-float(left));
360 float right_edge(float(right)-(max[0]-tl[0])/pw);
361 float top_edge((min[1]-tl[1])/ph-float(top));
362 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
364 if(top<0)top=0,top_edge=0;
365 if(left<0)left=0,left_edge=0;
366 if(bottom>h)bottom=h,bottom_edge=0;
367 if(right>w)right=w,right_edge=0;
372 RendDesc desc(renddesc);
375 //fill the surface with the background color initially
376 surface->set_wh(w,h);
377 surface->fill(color);
379 // Check for the case where there is nothing to render
380 if(right-left<=0||bottom-top<=0)
383 desc.set_subwindow(left,top,right-left,bottom-top);
385 // Render what is behind us
386 if(!context.accelerated_render(&subimage,quality,desc,cb))
388 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
392 Surface::pen pen(surface->get_pen(left,top));
394 subimage.blit_to(pen);
398 if(!context.accelerated_render(surface,quality,renddesc,cb))
400 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
405 // Check for the case where there is nothing to render
406 if(!(right-left<=0||bottom-top<=0))
408 subimage.set_wh(right-left,bottom-top);
409 Surface::pen subimage_pen(subimage.begin());
410 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
413 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
415 surface->fill(color,surface_pen,w,h);
419 Surface::pen pen(surface->get_pen(left,top));
420 subimage.blit_to(pen);
424 Surface::alpha_pen pen;
426 if(bottom-1>=0 && bottom_edge)
428 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
429 surface->fill(color,pen,right-left,1);
432 if(right-1>=0 && right_edge)
434 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
435 surface->fill(color,pen,1,bottom-top);
438 if(left>=0 && left_edge)
440 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
441 surface->fill(color,pen,1,bottom-top);
444 if(top>=0 && top_edge)
446 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
447 surface->fill(color,pen,right-left,1);
453 // Render what is behind us
454 if(!context.accelerated_render(surface,quality,renddesc,cb))
456 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
460 int left(ceil_to_int((min[0]-tl[0])/pw));
461 int right(floor_to_int((max[0]-tl[0])/pw));
462 int top(ceil_to_int((min[1]-tl[1])/ph));
463 int bottom(floor_to_int((max[1]-tl[1])/ph));
465 float left_edge(float(left)-(min[0]-tl[0])/pw);
466 float right_edge((max[0]-tl[0])/pw-float(right));
467 float top_edge(float(top)-(min[1]-tl[1])/ph);
468 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
470 if(top<=0)top=0,top_edge=0;
471 if(left<=0)left=0,left_edge=0;
472 if(bottom>=h)bottom=h,bottom_edge=0;
473 if(right>=w)right=w,right_edge=0;
476 top = std::max(0,top);
477 left = std::max(0,left);
478 bottom = std::min(h,bottom);
479 right = std::min(w,right);
482 Surface::alpha_pen pen;
484 // In the case where there is nothing to render...
485 if(right-left<0||bottom-top<0)
488 if(right-left>0&&bottom-top>0)
491 surface->fill(color,left,top,right-left,bottom-top);
494 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
495 surface->fill(color,pen,right-left,bottom-top);
499 if(bottom<surface->get_h() && bottom_edge>=0.0001)
501 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
502 surface->fill(color,pen,right-left,1);
505 if(right<surface->get_w() && right_edge>=0.0001)
507 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
508 surface->fill(color,pen,1,bottom-top);
511 if(left>0 && left_edge>=0.0001)
513 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
514 surface->fill(color,pen,1,bottom-top);
517 if(top>0 && top_edge>=0.0001)
519 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
520 surface->fill(color,pen,right-left,1);
528 Rectangle::get_bounding_rect()const
531 return Rect::full_plane();
533 Point max(point1),min(point2);
534 if((min[0] > max[0]))swap(min[0],max[0]);
535 if((min[1] > max[1]))swap(min[1],max[1]);
558 Rect bounds(min,max);
564 Rectangle::get_full_bounding_rect(Context context)const
568 if(is_solid_color() && color.get_a()==0)
570 Point max(point1),min(point2);
571 if((min[0] > max[0]))swap(min[0],max[0]);
572 if((min[1] > max[1]))swap(min[1],max[1]);
595 Rect bounds(min,max);
597 return bounds & context.get_full_bounding_rect();
599 return Rect::full_plane();
602 return Layer_Composite::get_full_bounding_rect(context);