1 /* === S Y N F I G ========================================================= */
2 /*! \file rectangle.cpp
3 ** \brief Implementation of the "Rectangle" layer
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 ** Copyright (c) 2007, 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>
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,N_("Rectangle"));
59 SYNFIG_LAYER_SET_CATEGORY(Rectangle,N_("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)
90 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
91 set_blend_method(Color::BLEND_ALPHA_OVER);
92 color.set_a(1); } else transparent_color_ = true; } });
98 return Layer_Composite::set_param(param,value);
102 Rectangle::get_param(const String ¶m)const
113 return Layer_Composite::get_param(param);
117 Rectangle::get_param_vocab()const
119 Layer::Vocab ret(Layer_Composite::get_param_vocab());
121 ret.push_back(ParamDesc("color")
122 .set_local_name(_("Color"))
125 ret.push_back(ParamDesc("point1")
126 .set_local_name(_("Point 1"))
130 ret.push_back(ParamDesc("point2")
131 .set_local_name(_("Point 2"))
134 ret.push_back(ParamDesc("expand")
136 .set_local_name(_("Expand amount"))
139 ret.push_back(ParamDesc("invert")
140 .set_local_name(_("Invert the rectangle"))
146 synfig::Layer::Handle
147 Rectangle::hit_check(synfig::Context context, const synfig::Point &pos)const
150 return context.hit_check(pos);
154 max[0]=std::max(point1[0],point2[0])+expand;
155 max[1]=std::max(point1[1],point2[1])+expand;
156 min[0]=std::min(point1[0],point2[0])-expand;
157 min[1]=std::min(point1[1],point2[1])-expand;
159 bool intersect(false);
161 if( pos[0]<max[0] && pos[0]>min[0] &&
162 pos[1]<max[1] && pos[1]>min[1] )
168 intersect=!intersect;
172 synfig::Layer::Handle tmp;
173 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(pos)))
175 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(pos)))
177 return const_cast<Rectangle*>(this);
180 return context.hit_check(pos);
184 Rectangle::is_solid_color()const
186 return Layer_Composite::is_solid_color() ||
187 (get_blend_method() == Color::BLEND_COMPOSITE &&
188 get_amount() == 1.0f &&
189 color.get_a() == 1.0f);
193 Rectangle::get_color(Context context, const Point &pos)const
196 return context.get_color(pos);
200 max[0]=std::max(point1[0],point2[0])+expand;
201 max[1]=std::max(point1[1],point2[1])+expand;
202 min[0]=std::min(point1[0],point2[0])-expand;
203 min[1]=std::min(point1[1],point2[1])-expand;
205 /**************************
206 // This is darco's old-old-old feathered box code
207 // it produces really nice feathered edges
210 if( pos[0]<=max[0]-feather/2.0 && pos[0]>=min[0]+feather/2.0 &&
211 pos[1]<=max[1]-feather/2.0 && pos[1]>=min[1]+feather/2.0 )
214 return (*context).GetColor(context,pos);
219 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
220 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
225 return (*context).GetColor(context,pos);
228 Color::unit alpha=1000000;
229 Color::unit alpha2=1000000;
231 if(max[0]-pos[0]+feather/2.0<alpha)
232 alpha=max[0]-pos[0]+feather/2.0;
233 if(pos[0]-min[0]+feather/2.0<alpha)
234 alpha=pos[0]-min[0]+feather/2.0;
236 if(max[1]-pos[1]+feather/2.0<alpha2)
237 alpha2=max[1]-pos[1]+feather/2.0;
238 if(pos[1]-min[1]+feather/2.0<alpha2)
239 alpha2=pos[1]-min[1]+feather/2.0;
242 if(alpha<=feather && alpha2<=feather)
245 alpha2=feather-alpha2;
247 alpha=sqrt(alpha*alpha+alpha2*alpha2);
254 return (*context).GetColor(context,pos);
261 alpha=(alpha<alpha2)?alpha:alpha2;
269 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
274 if( pos[0]<max[0] && pos[0]>min[0] &&
275 pos[1]<max[1] && pos[1]>min[1] )
277 // inside the expanded rectangle
279 return Color::blend(Color::alpha(),context.get_color(pos),get_amount(),get_blend_method());
284 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
288 // outside the expanded rectangle
290 return Color::blend(Color::alpha(),context.get_color(pos),get_amount(),get_blend_method());
295 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
300 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
303 return context.accelerated_render(surface,quality,renddesc,cb);
305 const Point tl(renddesc.get_tl());
306 const Point br(renddesc.get_br());
308 const int w(renddesc.get_w());
309 const int h(renddesc.get_h());
311 // Width and Height of a pixel
312 const Real pw = (br[0] - tl[0]) / w;
313 const Real ph = (br[1] - tl[1]) / h;
315 Point max(point1),min(point2);
324 max=context.get_bounding_rect().get_max();
325 min=context.get_bounding_rect().get_min();
329 max=context.get_full_bounding_rect().get_max();
330 min=context.get_full_bounding_rect().get_min();
338 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
339 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
365 int left(floor_to_int((min[0]-tl[0])/pw));
366 int right(ceil_to_int((max[0]-tl[0])/pw));
367 int top(floor_to_int((min[1]-tl[1])/ph));
368 int bottom(ceil_to_int((max[1]-tl[1])/ph));
370 float left_edge((min[0]-tl[0])/pw-float(left));
371 float right_edge(float(right)-(max[0]-tl[0])/pw);
372 float top_edge((min[1]-tl[1])/ph-float(top));
373 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
375 if(top<0)top=0,top_edge=0;
376 if(left<0)left=0,left_edge=0;
377 if(bottom>h)bottom=h,bottom_edge=0;
378 if(right>w)right=w,right_edge=0;
383 RendDesc desc(renddesc);
386 //fill the surface with the background color initially
387 surface->set_wh(w,h);
388 surface->fill(color);
390 // Check for the case where there is nothing to render
391 if (right <= left || bottom <= top)
394 desc.set_subwindow(left,top,right-left,bottom-top);
396 // Render what is behind us
397 if(!context.accelerated_render(&subimage,quality,desc,cb))
399 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
403 Surface::pen pen(surface->get_pen(left,top));
405 subimage.blit_to(pen);
409 if(!context.accelerated_render(surface,quality,renddesc,cb))
411 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
417 // Check for the case where there is something to render
418 if (right > left && bottom > top)
420 // save a copy of the overlapping region from surface into subimage
421 subimage.set_wh(right-left,bottom-top);
422 Surface::pen subimage_pen(subimage.begin());
423 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
426 // fill surface with the rectangle's color
427 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
428 surface->fill(color,surface_pen,w,h);
432 // copy the saved overlapping region back from subimage into surface
433 Surface::pen pen(surface->get_pen(left,top));
434 subimage.blit_to(pen);
437 // if there's no overlapping region, return now of the following code corrupts memory
441 Surface::alpha_pen pen;
443 if(bottom-1>=0 && bottom_edge)
445 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
446 surface->fill(color,pen,right-left,1);
449 if(right-1>=0 && right_edge)
451 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
452 surface->fill(color,pen,1,bottom-top);
455 if(left>=0 && left_edge)
457 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
458 surface->fill(color,pen,1,bottom-top);
461 if(top>=0 && top_edge)
463 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
464 surface->fill(color,pen,right-left,1);
472 int left(ceil_to_int((min[0]-tl[0])/pw));
473 int right(floor_to_int((max[0]-tl[0])/pw));
474 int top(ceil_to_int((min[1]-tl[1])/ph));
475 int bottom(floor_to_int((max[1]-tl[1])/ph));
477 float left_edge(float(left)-(min[0]-tl[0])/pw);
478 float right_edge((max[0]-tl[0])/pw-float(right));
479 float top_edge(float(top)-(min[1]-tl[1])/ph);
480 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
482 if(top<=0)top=0,top_edge=0;
483 if(left<=0)left=0,left_edge=0;
484 if(bottom>=h)bottom=h,bottom_edge=0;
485 if(right>=w)right=w,right_edge=0;
488 top = std::max(0,top);
489 left = std::max(0,left);
490 bottom = std::min(h,bottom);
491 right = std::min(w,right);
494 // optimization - if the whole tile is covered by this rectangle,
495 // and the rectangle is a solid color, we don't need to render
497 if (is_solid_color() && top == 0 && left == 0 && bottom == h && right == w)
499 surface->set_wh(w,h);
500 surface->fill(color);
504 // Render what is behind us
505 if(!context.accelerated_render(surface,quality,renddesc,cb))
507 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
511 // In the case where there is nothing to render...
512 if (right < left || bottom < top)
515 Surface::alpha_pen pen;
517 if(right-left>0&&bottom-top>0)
520 surface->fill(color,left,top,right-left,bottom-top);
523 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
524 surface->fill(color,pen,right-left,bottom-top);
528 if(bottom<surface->get_h() && bottom_edge>=0.0001)
530 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
531 surface->fill(color,pen,right-left,1);
534 if(right<surface->get_w() && right_edge>=0.0001)
536 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
537 surface->fill(color,pen,1,bottom-top);
540 if(left>0 && left_edge>=0.0001)
542 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
543 surface->fill(color,pen,1,bottom-top);
546 if(top>0 && top_edge>=0.0001)
548 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
549 surface->fill(color,pen,right-left,1);
557 Rectangle::get_bounding_rect()const
560 return Rect::full_plane();
562 Point max(point1),min(point2);
563 if((min[0] > max[0]))swap(min[0],max[0]);
564 if((min[1] > max[1]))swap(min[1],max[1]);
587 Rect bounds(min,max);
593 Rectangle::get_full_bounding_rect(Context context)const
597 if(is_solid_color() && color.get_a()==0)
599 Point max(point1),min(point2);
600 if((min[0] > max[0]))swap(min[0],max[0]);
601 if((min[1] > max[1]))swap(min[1],max[1]);
624 Rect bounds(min,max);
626 return bounds & context.get_full_bounding_rect();
628 return Rect::full_plane();
631 return Layer_Composite::get_full_bounding_rect(context);