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] )
278 return context.get_color(pos);
284 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
294 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
297 return context.get_color(pos);
301 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
304 return context.accelerated_render(surface,quality,renddesc,cb);
306 const Point tl(renddesc.get_tl());
307 const Point br(renddesc.get_br());
309 const int w(renddesc.get_w());
310 const int h(renddesc.get_h());
312 // Width and Height of a pixel
313 const Real pw = (br[0] - tl[0]) / w;
314 const Real ph = (br[1] - tl[1]) / h;
316 Point max(point1),min(point2);
325 max=context.get_bounding_rect().get_max();
326 min=context.get_bounding_rect().get_min();
330 max=context.get_full_bounding_rect().get_max();
331 min=context.get_full_bounding_rect().get_min();
339 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
340 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
366 int left(floor_to_int((min[0]-tl[0])/pw));
367 int right(ceil_to_int((max[0]-tl[0])/pw));
368 int top(floor_to_int((min[1]-tl[1])/ph));
369 int bottom(ceil_to_int((max[1]-tl[1])/ph));
371 float left_edge((min[0]-tl[0])/pw-float(left));
372 float right_edge(float(right)-(max[0]-tl[0])/pw);
373 float top_edge((min[1]-tl[1])/ph-float(top));
374 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
376 if(top<0)top=0,top_edge=0;
377 if(left<0)left=0,left_edge=0;
378 if(bottom>h)bottom=h,bottom_edge=0;
379 if(right>w)right=w,right_edge=0;
384 RendDesc desc(renddesc);
387 //fill the surface with the background color initially
388 surface->set_wh(w,h);
389 surface->fill(color);
391 // Check for the case where there is nothing to render
392 if (right <= left || bottom <= top)
395 desc.set_subwindow(left,top,right-left,bottom-top);
397 // Render what is behind us
398 if(!context.accelerated_render(&subimage,quality,desc,cb))
400 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
404 Surface::pen pen(surface->get_pen(left,top));
406 subimage.blit_to(pen);
410 if(!context.accelerated_render(surface,quality,renddesc,cb))
412 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
418 // Check for the case where there is something to render
419 if (right > left && bottom > top)
421 // save a copy of the overlapping region from surface into subimage
422 subimage.set_wh(right-left,bottom-top);
423 Surface::pen subimage_pen(subimage.begin());
424 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
427 // fill surface with the rectangle's color
428 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
429 surface->fill(color,surface_pen,w,h);
433 // copy the saved overlapping region back from subimage into surface
434 Surface::pen pen(surface->get_pen(left,top));
435 subimage.blit_to(pen);
438 // if there's no overlapping region, return now of the following code corrupts memory
442 Surface::alpha_pen pen;
444 if(bottom-1>=0 && bottom_edge)
446 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
447 surface->fill(color,pen,right-left,1);
450 if(right-1>=0 && right_edge)
452 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
453 surface->fill(color,pen,1,bottom-top);
456 if(left>=0 && left_edge)
458 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
459 surface->fill(color,pen,1,bottom-top);
462 if(top>=0 && top_edge)
464 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
465 surface->fill(color,pen,right-left,1);
473 int left(ceil_to_int((min[0]-tl[0])/pw));
474 int right(floor_to_int((max[0]-tl[0])/pw));
475 int top(ceil_to_int((min[1]-tl[1])/ph));
476 int bottom(floor_to_int((max[1]-tl[1])/ph));
478 float left_edge(float(left)-(min[0]-tl[0])/pw);
479 float right_edge((max[0]-tl[0])/pw-float(right));
480 float top_edge(float(top)-(min[1]-tl[1])/ph);
481 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
483 if(top<=0)top=0,top_edge=0;
484 if(left<=0)left=0,left_edge=0;
485 if(bottom>=h)bottom=h,bottom_edge=0;
486 if(right>=w)right=w,right_edge=0;
489 top = std::max(0,top);
490 left = std::max(0,left);
491 bottom = std::min(h,bottom);
492 right = std::min(w,right);
495 // optimization - if the whole tile is covered by this rectangle,
496 // and the rectangle is a solid color, we don't need to render
498 if (is_solid_color() && top == 0 && left == 0 && bottom == h && right == w)
500 surface->set_wh(w,h);
501 surface->fill(color);
505 // Render what is behind us
506 if(!context.accelerated_render(surface,quality,renddesc,cb))
508 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
512 // In the case where there is nothing to render...
513 if (right < left || bottom < top)
516 Surface::alpha_pen pen;
518 if(right-left>0&&bottom-top>0)
521 surface->fill(color,left,top,right-left,bottom-top);
524 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
525 surface->fill(color,pen,right-left,bottom-top);
529 if(bottom<surface->get_h() && bottom_edge>=0.0001)
531 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
532 surface->fill(color,pen,right-left,1);
535 if(right<surface->get_w() && right_edge>=0.0001)
537 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
538 surface->fill(color,pen,1,bottom-top);
541 if(left>0 && left_edge>=0.0001)
543 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
544 surface->fill(color,pen,1,bottom-top);
547 if(top>0 && top_edge>=0.0001)
549 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
550 surface->fill(color,pen,right-left,1);
558 Rectangle::get_bounding_rect()const
561 return Rect::full_plane();
563 Point max(point1),min(point2);
564 if((min[0] > max[0]))swap(min[0],max[0]);
565 if((min[1] > max[1]))swap(min[1],max[1]);
588 Rect bounds(min,max);
594 Rectangle::get_full_bounding_rect(Context context)const
598 if(is_solid_color() && color.get_a()==0)
600 Point max(point1),min(point2);
601 if((min[0] > max[0]))swap(min[0],max[0]);
602 if((min[1] > max[1]))swap(min[1],max[1]);
625 Rect bounds(min,max);
627 return bounds & context.get_full_bounding_rect();
629 return Rect::full_plane();
632 return Layer_Composite::get_full_bounding_rect(context);