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_COMPOSITE),
79 color(Color::black()),
85 Layer::Vocab voc(get_param_vocab());
86 Layer::fill_static(voc);
90 Rectangle::set_param(const String & param, const ValueBase &value)
92 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
93 set_blend_method(Color::BLEND_ALPHA_OVER);
94 color.set_a(1); } else transparent_color_ = true; } });
100 return Layer_Composite::set_param(param,value);
104 Rectangle::get_param(const String ¶m)const
115 return Layer_Composite::get_param(param);
119 Rectangle::get_param_vocab()const
121 Layer::Vocab ret(Layer_Composite::get_param_vocab());
123 ret.push_back(ParamDesc("color")
124 .set_local_name(_("Color"))
127 ret.push_back(ParamDesc("point1")
128 .set_local_name(_("Point 1"))
132 ret.push_back(ParamDesc("point2")
133 .set_local_name(_("Point 2"))
136 ret.push_back(ParamDesc("expand")
138 .set_local_name(_("Expand amount"))
141 ret.push_back(ParamDesc("invert")
142 .set_local_name(_("Invert the rectangle"))
148 synfig::Layer::Handle
149 Rectangle::hit_check(synfig::Context context, const synfig::Point &pos)const
152 return context.hit_check(pos);
156 max[0]=std::max(point1[0],point2[0])+expand;
157 max[1]=std::max(point1[1],point2[1])+expand;
158 min[0]=std::min(point1[0],point2[0])-expand;
159 min[1]=std::min(point1[1],point2[1])-expand;
161 bool intersect(false);
163 if( pos[0]<max[0] && pos[0]>min[0] &&
164 pos[1]<max[1] && pos[1]>min[1] )
170 intersect=!intersect;
174 synfig::Layer::Handle tmp;
175 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(pos)))
177 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(pos)))
179 return const_cast<Rectangle*>(this);
182 return context.hit_check(pos);
186 Rectangle::is_solid_color()const
188 return Layer_Composite::is_solid_color() ||
189 (get_blend_method() == Color::BLEND_COMPOSITE &&
190 get_amount() == 1.0f &&
191 color.get_a() == 1.0f);
195 Rectangle::get_color(Context context, const Point &pos)const
198 return context.get_color(pos);
202 max[0]=std::max(point1[0],point2[0])+expand;
203 max[1]=std::max(point1[1],point2[1])+expand;
204 min[0]=std::min(point1[0],point2[0])-expand;
205 min[1]=std::min(point1[1],point2[1])-expand;
207 /**************************
208 // This is darco's old-old-old feathered box code
209 // it produces really nice feathered edges
212 if( pos[0]<=max[0]-feather/2.0 && pos[0]>=min[0]+feather/2.0 &&
213 pos[1]<=max[1]-feather/2.0 && pos[1]>=min[1]+feather/2.0 )
216 return (*context).GetColor(context,pos);
221 if( pos[0]>=max[0]+feather/2.0 || pos[0]<=min[0]-feather/2.0 ||
222 pos[1]>=max[1]+feather/2.0 || pos[1]<=min[1]-feather/2.0 )
227 return (*context).GetColor(context,pos);
230 Color::unit alpha=1000000;
231 Color::unit alpha2=1000000;
233 if(max[0]-pos[0]+feather/2.0<alpha)
234 alpha=max[0]-pos[0]+feather/2.0;
235 if(pos[0]-min[0]+feather/2.0<alpha)
236 alpha=pos[0]-min[0]+feather/2.0;
238 if(max[1]-pos[1]+feather/2.0<alpha2)
239 alpha2=max[1]-pos[1]+feather/2.0;
240 if(pos[1]-min[1]+feather/2.0<alpha2)
241 alpha2=pos[1]-min[1]+feather/2.0;
244 if(alpha<=feather && alpha2<=feather)
247 alpha2=feather-alpha2;
249 alpha=sqrt(alpha*alpha+alpha2*alpha2);
256 return (*context).GetColor(context,pos);
263 alpha=(alpha<alpha2)?alpha:alpha2;
271 return Color::blend(color,context.get_color(pos),alpha,get_blend_method());
276 if( pos[0]<max[0] && pos[0]>min[0] &&
277 pos[1]<max[1] && pos[1]>min[1] )
279 // inside the expanded rectangle
281 return Color::blend(Color::alpha(),context.get_color(pos),get_amount(),get_blend_method());
286 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
290 // outside the expanded rectangle
292 return Color::blend(Color::alpha(),context.get_color(pos),get_amount(),get_blend_method());
297 return Color::blend(color,context.get_color(pos),get_amount(),get_blend_method());
302 Rectangle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
305 return context.accelerated_render(surface,quality,renddesc,cb);
307 const Point tl(renddesc.get_tl());
308 const Point br(renddesc.get_br());
310 const int w(renddesc.get_w());
311 const int h(renddesc.get_h());
313 // Width and Height of a pixel
314 const Real pw = (br[0] - tl[0]) / w;
315 const Real ph = (br[1] - tl[1]) / h;
317 Point max(point1),min(point2);
326 max=context.get_bounding_rect().get_max();
327 min=context.get_bounding_rect().get_min();
331 max=context.get_full_bounding_rect().get_max();
332 min=context.get_full_bounding_rect().get_min();
340 if((min[0] > max[0]) ^ (pw < 0))swap(min[0],max[0]);
341 if((min[1] > max[1]) ^ (ph < 0))swap(min[1],max[1]);
367 int left(floor_to_int((min[0]-tl[0])/pw));
368 int right(ceil_to_int((max[0]-tl[0])/pw));
369 int top(floor_to_int((min[1]-tl[1])/ph));
370 int bottom(ceil_to_int((max[1]-tl[1])/ph));
372 float left_edge((min[0]-tl[0])/pw-float(left));
373 float right_edge(float(right)-(max[0]-tl[0])/pw);
374 float top_edge((min[1]-tl[1])/ph-float(top));
375 float bottom_edge(float(bottom)-(max[1]-tl[1])/ph);
377 if(top<0)top=0,top_edge=0;
378 if(left<0)left=0,left_edge=0;
379 if(bottom>h)bottom=h,bottom_edge=0;
380 if(right>w)right=w,right_edge=0;
385 RendDesc desc(renddesc);
388 //fill the surface with the background color initially
389 surface->set_wh(w,h);
390 surface->fill(color);
392 // Check for the case where there is nothing to render
393 if (right <= left || bottom <= top)
396 desc.set_subwindow(left,top,right-left,bottom-top);
398 // Render what is behind us
399 if(!context.accelerated_render(&subimage,quality,desc,cb))
401 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
405 Surface::pen pen(surface->get_pen(left,top));
407 subimage.blit_to(pen);
411 if(!context.accelerated_render(surface,quality,renddesc,cb))
413 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
419 // Check for the case where there is something to render
420 if (right > left && bottom > top)
422 // save a copy of the overlapping region from surface into subimage
423 subimage.set_wh(right-left,bottom-top);
424 Surface::pen subimage_pen(subimage.begin());
425 surface->blit_to(subimage_pen,left,top,right-left,bottom-top);
428 // fill surface with the rectangle's color
429 Surface::alpha_pen surface_pen(surface->begin(),get_amount(),get_blend_method());
430 surface->fill(color,surface_pen,w,h);
434 // copy the saved overlapping region back from subimage into surface
435 Surface::pen pen(surface->get_pen(left,top));
436 subimage.blit_to(pen);
439 // if there's no overlapping region, return now of the following code corrupts memory
443 Surface::alpha_pen pen;
445 if(bottom-1>=0 && bottom_edge)
447 pen=Surface::alpha_pen(surface->get_pen(left,bottom-1),get_amount()*bottom_edge,get_blend_method());
448 surface->fill(color,pen,right-left,1);
451 if(right-1>=0 && right_edge)
453 pen=Surface::alpha_pen(surface->get_pen(right-1,top),get_amount()*right_edge,get_blend_method());
454 surface->fill(color,pen,1,bottom-top);
457 if(left>=0 && left_edge)
459 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*left_edge,get_blend_method());
460 surface->fill(color,pen,1,bottom-top);
463 if(top>=0 && top_edge)
465 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount()*top_edge,get_blend_method());
466 surface->fill(color,pen,right-left,1);
474 int left(ceil_to_int((min[0]-tl[0])/pw));
475 int right(floor_to_int((max[0]-tl[0])/pw));
476 int top(ceil_to_int((min[1]-tl[1])/ph));
477 int bottom(floor_to_int((max[1]-tl[1])/ph));
479 float left_edge(float(left)-(min[0]-tl[0])/pw);
480 float right_edge((max[0]-tl[0])/pw-float(right));
481 float top_edge(float(top)-(min[1]-tl[1])/ph);
482 float bottom_edge((max[1]-tl[1])/ph-float(bottom));
484 if(top<=0)top=0,top_edge=0;
485 if(left<=0)left=0,left_edge=0;
486 if(bottom>=h)bottom=h,bottom_edge=0;
487 if(right>=w)right=w,right_edge=0;
490 top = std::max(0,top);
491 left = std::max(0,left);
492 bottom = std::min(h,bottom);
493 right = std::min(w,right);
496 // optimization - if the whole tile is covered by this rectangle,
497 // and the rectangle is a solid color, we don't need to render
499 if (is_solid_color() && top == 0 && left == 0 && bottom == h && right == w)
501 surface->set_wh(w,h);
502 surface->fill(color);
506 // Render what is behind us
507 if(!context.accelerated_render(surface,quality,renddesc,cb))
509 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
513 // In the case where there is nothing to render...
514 if (right < left || bottom < top)
517 Surface::alpha_pen pen;
519 if(right-left>0&&bottom-top>0)
522 surface->fill(color,left,top,right-left,bottom-top);
525 pen=Surface::alpha_pen(surface->get_pen(left,top),get_amount(),get_blend_method());
526 surface->fill(color,pen,right-left,bottom-top);
530 if(bottom<surface->get_h() && bottom_edge>=0.0001)
532 pen=Surface::alpha_pen(surface->get_pen(left,bottom),get_amount()*bottom_edge,get_blend_method());
533 surface->fill(color,pen,right-left,1);
536 if(right<surface->get_w() && right_edge>=0.0001)
538 pen=Surface::alpha_pen(surface->get_pen(right,top),get_amount()*right_edge,get_blend_method());
539 surface->fill(color,pen,1,bottom-top);
542 if(left>0 && left_edge>=0.0001)
544 pen=Surface::alpha_pen(surface->get_pen(left-1,top),get_amount()*left_edge,get_blend_method());
545 surface->fill(color,pen,1,bottom-top);
548 if(top>0 && top_edge>=0.0001)
550 pen=Surface::alpha_pen(surface->get_pen(left,top-1),get_amount()*top_edge,get_blend_method());
551 surface->fill(color,pen,right-left,1);
559 Rectangle::get_bounding_rect()const
562 return Rect::full_plane();
564 Point max(point1),min(point2);
565 if((min[0] > max[0]))swap(min[0],max[0]);
566 if((min[1] > max[1]))swap(min[1],max[1]);
589 Rect bounds(min,max);
595 Rectangle::get_full_bounding_rect(Context context)const
599 if(is_solid_color() && color.get_a()==0)
601 Point max(point1),min(point2);
602 if((min[0] > max[0]))swap(min[0],max[0]);
603 if((min[1] > max[1]))swap(min[1],max[1]);
626 Rect bounds(min,max);
628 return bounds & context.get_full_bounding_rect();
630 return Rect::full_plane();
633 return Layer_Composite::get_full_bounding_rect(context);