1 /* === S Y N F I G ========================================================= */
2 /*! \file rectangle.cpp
3 ** \brief Template Header
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 ** Copyright (c) 2007 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>
44 #include "rectangle.h"
48 /* === U S I N G =========================================================== */
52 using namespace synfig;
54 /* === G L O B A L S ======================================================= */
56 SYNFIG_LAYER_INIT(Rectangle);
57 SYNFIG_LAYER_SET_NAME(Rectangle,"rectangle");
58 SYNFIG_LAYER_SET_LOCAL_NAME(Rectangle,_("Rectangle"));
59 SYNFIG_LAYER_SET_CATEGORY(Rectangle,_("Geometry"));
60 SYNFIG_LAYER_SET_VERSION(Rectangle,"0.2");
61 SYNFIG_LAYER_SET_CVS_ID(Rectangle,"$Id$");
63 /* === P R O C E D U R E S ================================================= */
66 inline int ceil_to_int(const float x) { return static_cast<int>(ceil(x)); }
67 inline int ceil_to_int(const double x) { return static_cast<int>(ceil(x)); }
69 inline int floor_to_int(const float x) { return static_cast<int>(floor(x)); }
70 inline int floor_to_int(const double x) { return static_cast<int>(floor(x)); }
73 /* === M E T H O D S ======================================================= */
75 /* === E N T R Y P O I N T ================================================= */
77 Rectangle::Rectangle():
78 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
79 color(Color::black()),
88 Rectangle::set_param(const String & param, const ValueBase &value)
96 return Layer_Composite::set_param(param,value);
100 Rectangle::get_param(const String ¶m)const
111 return Layer_Composite::get_param(param);
115 Rectangle::get_param_vocab()const
117 Layer::Vocab ret(Layer_Composite::get_param_vocab());
119 ret.push_back(ParamDesc("color")
120 .set_local_name(_("Color"))
123 ret.push_back(ParamDesc("point1")
124 .set_local_name(_("Point 1"))
128 ret.push_back(ParamDesc("point2")
129 .set_local_name(_("Point 2"))
132 ret.push_back(ParamDesc("expand")
134 .set_local_name(_("Expand amount"))
137 ret.push_back(ParamDesc("invert")
138 .set_local_name(_("Invert the rectangle"))
144 synfig::Layer::Handle
145 Rectangle::hit_check(synfig::Context context, const synfig::Point &pos)const
148 return context.hit_check(pos);
152 max[0]=std::max(point1[0],point2[0])+expand;
153 max[1]=std::max(point1[1],point2[1])+expand;
154 min[0]=std::min(point1[0],point2[0])-expand;
155 min[1]=std::min(point1[1],point2[1])-expand;
157 bool intersect(false);
159 if( pos[0]<max[0] && pos[0]>min[0] &&
160 pos[1]<max[1] && pos[1]>min[1] )
166 intersect=!intersect;
170 synfig::Layer::Handle tmp;
171 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(pos)))
173 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(pos)))
175 return const_cast<Rectangle*>(this);
178 return context.hit_check(pos);
182 Rectangle::is_solid_color()const
184 return Layer_Composite::is_solid_color() ||
185 (get_blend_method() == Color::BLEND_COMPOSITE &&
186 get_amount() == 1.0f &&
187 color.get_a() == 1.0f);
191 Rectangle::get_color(Context context, const Point &pos)const
194 return context.get_color(pos);
198 max[0]=std::max(point1[0],point2[0])+expand;
199 max[1]=std::max(point1[1],point2[1])+expand;
200 min[0]=std::min(point1[0],point2[0])-expand;
201 min[1]=std::min(point1[1],point2[1])-expand;
203 /**************************
204 // This is darco's old-old-old feathered box code
205 // it produces really nice feathered edges
208 if( pos[0]<=max[0]-feather/2.0 && pos[0]>=min[0]+feather/2.0 &&
209 pos[1]<=max[1]-feather/2.0 && pos[1]>=min[1]+feather/2.0 )
212 return (*context).GetColor(context,pos);
217 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
218 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
223 return (*context).GetColor(context,pos);
226 Color::unit alpha=1000000;
227 Color::unit alpha2=1000000;
229 if(max[0]-pos[0]+feather/2.0<alpha)
230 alpha=max[0]-pos[0]+feather/2.0;
231 if(pos[0]-min[0]+feather/2.0<alpha)
232 alpha=pos[0]-min[0]+feather/2.0;
234 if(max[1]-pos[1]+feather/2.0<alpha2)
235 alpha2=max[1]-pos[1]+feather/2.0;
236 if(pos[1]-min[1]+feather/2.0<alpha2)
237 alpha2=pos[1]-min[1]+feather/2.0;
240 if(alpha<=feather && alpha2<=feather)
243 alpha2=feather-alpha2;
245 alpha=sqrt(alpha*alpha+alpha2*alpha2);
252 return (*context).GetColor(context,pos);
259 alpha=(alpha<alpha2)?alpha:alpha2;
267 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
272 if( pos[0]<max[0] && pos[0]>min[0] &&
273 pos[1]<max[1] && pos[1]>min[1] )
276 return context.get_color(pos);
282 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
292 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
295 return context.get_color(pos);
299 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
302 return context.accelerated_render(surface,quality,renddesc,cb);
304 const Point tl(renddesc.get_tl());
305 const Point br(renddesc.get_br());
307 const int w(renddesc.get_w());
308 const int h(renddesc.get_h());
310 // Width and Height of a pixel
311 const Real pw = (br[0] - tl[0]) / w;
312 const Real ph = (br[1] - tl[1]) / h;
314 Point max(point1),min(point2);
323 max=context.get_bounding_rect().get_max();
324 min=context.get_bounding_rect().get_min();
328 max=context.get_full_bounding_rect().get_max();
329 min=context.get_full_bounding_rect().get_min();
337 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
338 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
364 int left(floor_to_int((min[0]-tl[0])/pw));
365 int right(ceil_to_int((max[0]-tl[0])/pw));
366 int top(floor_to_int((min[1]-tl[1])/ph));
367 int bottom(ceil_to_int((max[1]-tl[1])/ph));
369 float left_edge((min[0]-tl[0])/pw-float(left));
370 float right_edge(float(right)-(max[0]-tl[0])/pw);
371 float top_edge((min[1]-tl[1])/ph-float(top));
372 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
374 if(top<0)top=0,top_edge=0;
375 if(left<0)left=0,left_edge=0;
376 if(bottom>h)bottom=h,bottom_edge=0;
377 if(right>w)right=w,right_edge=0;
382 RendDesc desc(renddesc);
385 //fill the surface with the background color initially
386 surface->set_wh(w,h);
387 surface->fill(color);
389 // Check for the case where there is nothing to render
390 if (right <= left || bottom <= top)
393 desc.set_subwindow(left,top,right-left,bottom-top);
395 // Render what is behind us
396 if(!context.accelerated_render(&subimage,quality,desc,cb))
398 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
402 Surface::pen pen(surface->get_pen(left,top));
404 subimage.blit_to(pen);
408 if(!context.accelerated_render(surface,quality,renddesc,cb))
410 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
416 // Check for the case where there is something to render
417 if (right > left && bottom > top)
419 // save a copy of the overlapping region from surface into subimage
420 subimage.set_wh(right-left,bottom-top);
421 Surface::pen subimage_pen(subimage.begin());
422 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
425 // fill surface with the rectangle's colour
426 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
427 surface->fill(color,surface_pen,w,h);
431 // copy the saved overlapping region back from subimage into surface
432 Surface::pen pen(surface->get_pen(left,top));
433 subimage.blit_to(pen);
436 // if there's no overlapping region, return now of the following code corrupts memory
440 Surface::alpha_pen pen;
442 if(bottom-1>=0 && bottom_edge)
444 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
445 surface->fill(color,pen,right-left,1);
448 if(right-1>=0 && right_edge)
450 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
451 surface->fill(color,pen,1,bottom-top);
454 if(left>=0 && left_edge)
456 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
457 surface->fill(color,pen,1,bottom-top);
460 if(top>=0 && top_edge)
462 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
463 surface->fill(color,pen,right-left,1);
471 int left(ceil_to_int((min[0]-tl[0])/pw));
472 int right(floor_to_int((max[0]-tl[0])/pw));
473 int top(ceil_to_int((min[1]-tl[1])/ph));
474 int bottom(floor_to_int((max[1]-tl[1])/ph));
476 float left_edge(float(left)-(min[0]-tl[0])/pw);
477 float right_edge((max[0]-tl[0])/pw-float(right));
478 float top_edge(float(top)-(min[1]-tl[1])/ph);
479 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
481 if(top<=0)top=0,top_edge=0;
482 if(left<=0)left=0,left_edge=0;
483 if(bottom>=h)bottom=h,bottom_edge=0;
484 if(right>=w)right=w,right_edge=0;
487 top = std::max(0,top);
488 left = std::max(0,left);
489 bottom = std::min(h,bottom);
490 right = std::min(w,right);
493 // optimisation - if the whole tile is covered by this rectangle,
494 // and the rectangle is a solid colour, we don't need to render
496 if (is_solid_color() && top == 0 && left == 0 && bottom == h && right == w)
498 surface->set_wh(w,h);
499 surface->fill(color);
503 // Render what is behind us
504 if(!context.accelerated_render(surface,quality,renddesc,cb))
506 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
510 // In the case where there is nothing to render...
511 if (right < left || bottom < top)
514 Surface::alpha_pen pen;
516 if(right-left>0&&bottom-top>0)
519 surface->fill(color,left,top,right-left,bottom-top);
522 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
523 surface->fill(color,pen,right-left,bottom-top);
527 if(bottom<surface->get_h() && bottom_edge>=0.0001)
529 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
530 surface->fill(color,pen,right-left,1);
533 if(right<surface->get_w() && right_edge>=0.0001)
535 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
536 surface->fill(color,pen,1,bottom-top);
539 if(left>0 && left_edge>=0.0001)
541 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
542 surface->fill(color,pen,1,bottom-top);
545 if(top>0 && top_edge>=0.0001)
547 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
548 surface->fill(color,pen,right-left,1);
556 Rectangle::get_bounding_rect()const
559 return Rect::full_plane();
561 Point max(point1),min(point2);
562 if((min[0] > max[0]))swap(min[0],max[0]);
563 if((min[1] > max[1]))swap(min[1],max[1]);
586 Rect bounds(min,max);
592 Rectangle::get_full_bounding_rect(Context context)const
596 if(is_solid_color() && color.get_a()==0)
598 Point max(point1),min(point2);
599 if((min[0] > max[0]))swap(min[0],max[0]);
600 if((min[1] > max[1]))swap(min[1],max[1]);
623 Rect bounds(min,max);
625 return bounds & context.get_full_bounding_rect();
627 return Rect::full_plane();
630 return Layer_Composite::get_full_bounding_rect(context);