1 /* === S Y N F I G ========================================================= */
2 /*! \file rectangle.cpp
3 ** \brief Template Header
8 ** Copyright (c) 2002 Robert B. Quattlebaum 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$");
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::is_solid_color()const
183 return Layer_Composite::is_solid_color() ||
184 (get_blend_method() == Color::BLEND_COMPOSITE &&
185 get_amount() == 1.0f &&
186 color.get_a() == 1.0f);
190 Rectangle::get_color(Context context, const Point &pos)const
193 return context.get_color(pos);
197 max[0]=std::max(point1[0],point2[0])+expand;
198 max[1]=std::max(point1[1],point2[1])+expand;
199 min[0]=std::min(point1[0],point2[0])-expand;
200 min[1]=std::min(point1[1],point2[1])-expand;
202 /**************************
203 // This is darco's old-old-old feathered box code
204 // it produces really nice feathered edges
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 )
211 return (*context).GetColor(context,pos);
216 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
217 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
222 return (*context).GetColor(context,pos);
225 Color::unit alpha=1000000;
226 Color::unit alpha2=1000000;
228 if(max[0]-pos[0]+feather/2.0<alpha)
229 alpha=max[0]-pos[0]+feather/2.0;
230 if(pos[0]-min[0]+feather/2.0<alpha)
231 alpha=pos[0]-min[0]+feather/2.0;
233 if(max[1]-pos[1]+feather/2.0<alpha2)
234 alpha2=max[1]-pos[1]+feather/2.0;
235 if(pos[1]-min[1]+feather/2.0<alpha2)
236 alpha2=pos[1]-min[1]+feather/2.0;
239 if(alpha<=feather && alpha2<=feather)
242 alpha2=feather-alpha2;
244 alpha=sqrt(alpha*alpha+alpha2*alpha2);
251 return (*context).GetColor(context,pos);
258 alpha=(alpha<alpha2)?alpha:alpha2;
266 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
271 if( pos[0]<max[0] && pos[0]>min[0] &&
272 pos[1]<max[1] && pos[1]>min[1] )
275 return context.get_color(pos);
281 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
291 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
294 return context.get_color(pos);
298 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
301 return context.accelerated_render(surface,quality,renddesc,cb);
303 const Point tl(renddesc.get_tl());
304 const Point br(renddesc.get_br());
306 const int w(renddesc.get_w());
307 const int h(renddesc.get_h());
309 // Width and Height of a pixel
310 const Real pw = (br[0] - tl[0]) / w;
311 const Real ph = (br[1] - tl[1]) / h;
313 Point max(point1),min(point2);
322 max=context.get_bounding_rect().get_max();
323 min=context.get_bounding_rect().get_min();
327 max=context.get_full_bounding_rect().get_max();
328 min=context.get_full_bounding_rect().get_min();
336 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
337 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
363 int left(floor_to_int((min[0]-tl[0])/pw));
364 int right(ceil_to_int((max[0]-tl[0])/pw));
365 int top(floor_to_int((min[1]-tl[1])/ph));
366 int bottom(ceil_to_int((max[1]-tl[1])/ph));
368 float left_edge((min[0]-tl[0])/pw-float(left));
369 float right_edge(float(right)-(max[0]-tl[0])/pw);
370 float top_edge((min[1]-tl[1])/ph-float(top));
371 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
373 if(top<0)top=0,top_edge=0;
374 if(left<0)left=0,left_edge=0;
375 if(bottom>h)bottom=h,bottom_edge=0;
376 if(right>w)right=w,right_edge=0;
381 RendDesc desc(renddesc);
384 //fill the surface with the background color initially
385 surface->set_wh(w,h);
386 surface->fill(color);
388 // Check for the case where there is nothing to render
389 if (right <= left || bottom <= top)
392 desc.set_subwindow(left,top,right-left,bottom-top);
394 // Render what is behind us
395 if(!context.accelerated_render(&subimage,quality,desc,cb))
397 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
401 Surface::pen pen(surface->get_pen(left,top));
403 subimage.blit_to(pen);
407 if(!context.accelerated_render(surface,quality,renddesc,cb))
409 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
415 // Check for the case where there is something to render
416 if (right > left && bottom > top)
418 // save a copy of the overlapping region from surface into subimage
419 subimage.set_wh(right-left,bottom-top);
420 Surface::pen subimage_pen(subimage.begin());
421 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
424 // fill surface with the rectangle's colour
425 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
426 surface->fill(color,surface_pen,w,h);
430 // copy the saved overlapping region back from subimage into surface
431 Surface::pen pen(surface->get_pen(left,top));
432 subimage.blit_to(pen);
435 // if there's no overlapping region, return now of the following code corrupts memory
439 Surface::alpha_pen pen;
441 if(bottom-1>=0 && bottom_edge)
443 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
444 surface->fill(color,pen,right-left,1);
447 if(right-1>=0 && right_edge)
449 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
450 surface->fill(color,pen,1,bottom-top);
453 if(left>=0 && left_edge)
455 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
456 surface->fill(color,pen,1,bottom-top);
459 if(top>=0 && top_edge)
461 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
462 surface->fill(color,pen,right-left,1);
470 int left(ceil_to_int((min[0]-tl[0])/pw));
471 int right(floor_to_int((max[0]-tl[0])/pw));
472 int top(ceil_to_int((min[1]-tl[1])/ph));
473 int bottom(floor_to_int((max[1]-tl[1])/ph));
475 float left_edge(float(left)-(min[0]-tl[0])/pw);
476 float right_edge((max[0]-tl[0])/pw-float(right));
477 float top_edge(float(top)-(min[1]-tl[1])/ph);
478 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
480 if(top<=0)top=0,top_edge=0;
481 if(left<=0)left=0,left_edge=0;
482 if(bottom>=h)bottom=h,bottom_edge=0;
483 if(right>=w)right=w,right_edge=0;
486 top = std::max(0,top);
487 left = std::max(0,left);
488 bottom = std::min(h,bottom);
489 right = std::min(w,right);
492 Surface::alpha_pen pen;
494 // In the case where there is nothing to render...
495 if (right < left || bottom < top)
498 // optimisation - if the whole tile is covered by this rectangle,
499 // and the rectangle is a solid colour, we don't need to render
501 if (is_solid_color() && top == 0 && left == 0 && bottom == h && right == w)
503 surface->set_wh(w,h);
504 surface->fill(color);
508 // Render what is behind us
509 if(!context.accelerated_render(surface,quality,renddesc,cb))
511 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
515 if(right-left>0&&bottom-top>0)
518 surface->fill(color,left,top,right-left,bottom-top);
521 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
522 surface->fill(color,pen,right-left,bottom-top);
526 if(bottom<surface->get_h() && bottom_edge>=0.0001)
528 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
529 surface->fill(color,pen,right-left,1);
532 if(right<surface->get_w() && right_edge>=0.0001)
534 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
535 surface->fill(color,pen,1,bottom-top);
538 if(left>0 && left_edge>=0.0001)
540 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
541 surface->fill(color,pen,1,bottom-top);
544 if(top>0 && top_edge>=0.0001)
546 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
547 surface->fill(color,pen,right-left,1);
555 Rectangle::get_bounding_rect()const
558 return Rect::full_plane();
560 Point max(point1),min(point2);
561 if((min[0] > max[0]))swap(min[0],max[0]);
562 if((min[1] > max[1]))swap(min[1],max[1]);
585 Rect bounds(min,max);
591 Rectangle::get_full_bounding_rect(Context context)const
595 if(is_solid_color() && color.get_a()==0)
597 Point max(point1),min(point2);
598 if((min[0] > max[0]))swap(min[0],max[0]);
599 if((min[1] > max[1]))swap(min[1],max[1]);
622 Rect bounds(min,max);
624 return bounds & context.get_full_bounding_rect();
626 return Rect::full_plane();
629 return Layer_Composite::get_full_bounding_rect(context);