1 /* === S Y N F I G ========================================================= */
2 /*! \file layer_shape.cpp
3 ** \brief Implementation of the "Shape" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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 "layer_shape.h"
37 #include "paramdesc.h"
41 #include "valuenode.h"
45 #include "curve_helper.h"
53 /* === U S I N G =========================================================== */
55 using namespace synfig;
59 /* === G L O B A L S ======================================================= */
61 SYNFIG_LAYER_INIT(Layer_Shape);
62 SYNFIG_LAYER_SET_NAME(Layer_Shape,"shape");
63 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shape,N_("Shape"));
64 SYNFIG_LAYER_SET_CATEGORY(Layer_Shape,N_("Internal"));
65 SYNFIG_LAYER_SET_VERSION(Layer_Shape,"0.1");
66 SYNFIG_LAYER_SET_CVS_ID(Layer_Shape,"$Id$");
71 inline bool IsZero(const T &n)
73 return (n < EPSILON) && (n > -EPSILON);
76 /* === C L A S S E S ======================================================= */
78 //Assumes 64 byte aligned structures if at all
89 MOVE_TO = 0, //(x,y)+ after first point treated as line_to
90 CLOSE, // NOT RUNLENGTH enabled
91 LINE_TO, //(x,y)+ continuous func
92 CONIC_TO, //(x1,y1,x,y)+ " "
93 CONIC_TO_SMOOTH, //(x,y)+ " "
94 CUBIC_TO, //(x1,y1,x2,y2,x,y)+ " "
95 CUBIC_TO_SMOOTH, //(x2,y2,x,y)+ " "
100 //******** CURVE FUNCTIONS *****************
101 const int MAX_SUBDIVISION_SIZE = 64;
102 const int MIN_SUBDIVISION_DRAW_LEVELS = 4;
104 static void Subd_Conic_Stack(Point *arc)
122 arc[4][0] = arc[2][0];
125 a = arc[1][0] = (arc[0][0] + b)/2;
126 b = arc[3][0] = (arc[4][0] + b)/2;
127 arc[2][0] = (a + b)/2;
130 arc[4][1] = arc[2][1];
133 a = arc[1][1] = (arc[0][1] + b)/2;
134 b = arc[3][1] = (arc[4][1] + b)/2;
135 arc[2][1] = (a + b)/2;
141 arc[3] = (arc[2] + arc[1])/2;
142 arc[1] = (arc[0] + arc[1])/2;
144 arc[2] = (arc[1] + arc[3])/2;
150 static void Subd_Cubic_Stack(Point *arc)
159 * 1+2 b * 0+3*1+3*2+3
165 0.1 2.3 -> 0.1 2 3 4 5.6
169 arc[6][0] = arc[3][0];
174 a = arc[1][0] = (arc[0][0] + b)/2;
176 c = arc[5][0] = (arc[6][0] + c)/2;
178 a = arc[2][0] = (a + b)/2;
179 b = arc[4][0] = (b + c)/2;
181 arc[3][0] = (a + b)/2;
184 arc[6][1] = arc[3][1];
189 a = arc[1][1] = (arc[0][1] + b)/2;
191 c = arc[5][1] = (arc[6][1] + c)/2;
193 a = arc[2][1] = (a + b)/2;
194 b = arc[4][1] = (b + c)/2;
196 arc[3][1] = (a + b)/2;
203 //backwards to avoid overwriting
204 arc[5] = (arc[2] + arc[3])/2;
205 temp = (arc[1] + arc[2])/2;
206 arc[1] = (arc[0] + arc[1])/2;
208 arc[4] = (temp + arc[5])/2;
209 arc[2] = (arc[1] + temp)/2;
211 arc[3] = (arc[2] + arc[4])/2;
216 //************** PARAMETRIC RENDERER SUPPORT STRUCTURES ****************
223 vector<Point> pointlist;
225 MonoSegment(int dir = 0, Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
235 int intersect(Real x,Real y) const
237 if((y < aabb.miny+EPSILON) || (y > aabb.maxy) || (x < aabb.minx)) return 0;
238 if(x > aabb.maxx) return ydir;
241 //int size = pointlist.size();
242 //vector<Point>::const_iterator end = pointlist.end();
243 vector<Point>::const_iterator p = pointlist.begin();
245 //assumes that the rect culled away anything that would be beyond the edges
248 while(y > (*++p)[1]);
252 while(y < (*++p)[1]);
255 //for the loop to break there must have been a slope (straight line would do nothing)
256 //vector<Point>::const_iterator p1 = p-1;
257 Real dy = p[-1][1] - p[0][1];
258 Real dx = p[-1][0] - p[0][0];
262 Real xi = p[0][0] + (y - p[0][1]) * dx / dy;
263 return (x > xi)*ydir;
269 Rect aabb; //not necessarily as effective - can only reject values
270 vector<Point> pointlist; //run length - p0, p1, p2, p3 = p10, p11, p12, p13 = p20 ...
271 vector<char> degrees;
273 CurveArray(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
275 aabb.set(x0,y0,x1,y1);
278 void reset(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
280 aabb.set(x0,y0,x1,y1);
287 return degrees.size();
292 reset(m[0],m[0],m[1],m[1]);
293 pointlist.push_back(m);
296 void AddCubic(Point p1, Point p2, Point dest)
298 aabb.expand(p1[0],p1[1]);
299 aabb.expand(p2[0],p2[1]);
300 aabb.expand(dest[0],dest[1]);
302 pointlist.push_back(p1);
303 pointlist.push_back(p2);
304 pointlist.push_back(dest);
306 degrees.push_back(3);
309 void AddConic(Point p1, Point dest)
311 aabb.expand(p1[0],p1[1]);
312 aabb.expand(dest[0],dest[1]);
314 pointlist.push_back(p1);
315 pointlist.push_back(dest);
317 degrees.push_back(2);
320 static int intersect_conic(Real x, Real y, Point *p, int /*level*/ = 0)
322 Real ymin,ymax,xmin,xmax;
325 //sort the overall curve ys - degenerate detection
326 ymin = min(p[0][1],p[2][1]);
327 ymax = max(p[0][1],p[2][1]);
329 xmin = min(min(p[0][0],p[1][0]),p[2][0]);
330 xmax = max(max(p[0][0],p[1][0]),p[2][0]);
332 //to the left, to the right and out of range y, or completely out of range y
333 if( x < xmin ) return 0;
334 if( x > xmax && (y > ymax || y < ymin) ) return 0;
335 if( (y > ymax && y > p[1][1]) || (y < ymin && y < p[1][1]) ) return 0;
337 //degenerate line max
338 if(ymin == ymax == p[1][1])
341 //degenerate accept - to the right and crossing the base line
344 return (y <= ymax && y >= ymin);
347 //solve for curve = y
350 //0 roots - 0 intersection
351 //1 root - get x, and figure out x
352 //2 roots (non-double root) - get 2 xs, and count xs to the left
354 //for conic we can assume 1 intersection for monotonic curve
355 Real a = p[2][1] - 2*p[1][1] + p[0][1],
356 b = 2*p[1][1] - 2*p[0][1],
359 Real t1 = -1, t2 = -1;
364 if(b == 0) return 0; //may not need this check
366 t1 = - c / b; //bt + c = 0 solved
369 //2 degree polynomial
370 Real b2_4ac = b*b - 4*a*c;
372 //if there are double/no roots - no intersections (in real #s that is)
378 b2_4ac = sqrt(b2_4ac);
380 t1 = (-b - b2_4ac) / 2*a,
381 t2 = (-b + b2_4ac) / 2*a;
384 //calculate number of intersections
385 if(t1 >= 0 && t1 <= 1)
388 const Real invt = 1 - t;
390 //find x val and it counts if it's to the left of the point
391 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
392 const Real dy_t = 2*a*t + b;
396 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
400 if(t2 >= 0 && t2 <= 1)
403 const Real invt = 1 - t;
405 //find x val and it counts if it's to the left of the point
406 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
407 const Real dy_t = 2*a*t + b;
411 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
418 static int quadratic_eqn(Real a, Real b, Real c, Real *t0, Real *t1)
420 const Real b2_4ac = b*b - 4*a*c;
422 //degenerate reject (can't take sqrt)
428 const Real sqrtb2_4ac = sqrt(b2_4ac);
429 const Real signb = b < 0 ? -1 : 1;
430 const Real q = - 0.5 * (b + signb * sqrtb2_4ac);
435 return sqrtb2_4ac == 0 ? 1 : 2;
438 //Newton-Raphson root polishing (we don't care about bounds, assumes very near the desired root)
439 static Real polish_cubicroot(Real a, Real b, Real c, Real d, Real t, Real *dpdt)
441 const Real cn[4] = {a,b,c,d};
442 Real p,dp,newt,oldpmag=FLT_MAX;
444 //eval cubic eqn and its derivative
450 for(int i = 2; i < 4; i++)
458 synfig::warning("polish_cubicroot: Derivative should not vanish!!!");
464 if(newt == t || fabs(p) >= oldpmag)
475 static int intersect_cubic(Real x, Real y, Point *p, int /*level*/ = 0)
477 const Real INVALIDROOT = -FLT_MAX;
478 Real ymin,ymax,xmin,xmax;
479 Real ymin2,ymax2,ymintot,ymaxtot;
482 //sort the overall curve ys and xs - degenerate detection
484 //open span for the two end points
485 ymin = min(p[0][1],p[3][1]);
486 ymax = max(p[0][1],p[3][1]);
489 ymin2 = min(p[1][1],p[2][1]);
490 ymax2 = max(p[1][1],p[2][1]);
492 ymintot = min(ymin,ymin2);
493 ymaxtot = max(ymax,ymax2);
495 //the entire curve control polygon is in this x range
496 xmin = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
497 xmax = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][0]));
499 //outside all y boundaries (no intersect)
500 if( (y > ymaxtot) || (y < ymintot) ) return 0;
502 //left of curve (no intersect)
503 if(x < xmin) return 0;
505 //right of curve (and outside base range)
508 if( (y > ymax) || (y < ymin) ) return 0;
510 //degenerate accept - to the right and inside the [ymin,ymax] range (already rejected if out of range)
511 const Real n = p[3][1] - p[0][1];
513 //extract the sign from the value (we need valid data)
514 return n < 0 ? -1 : 1;
517 //degenerate horizontal line max -- doesn't happen enough to check for
518 if( ymintot == ymaxtot ) return 0;
521 // can have 0,1,2, or 3 real roots
522 // if any of them are double then reject the two...
524 // y-coefficients for f_y(t) - y = 0
525 Real a = p[3][1] - 3*p[2][1] + 3*p[1][1] - p[0][1],
526 b = 3*p[2][1] - 6*p[1][1] + 3*p[0][1],
527 c = 3*p[1][1] - 3*p[0][1],
530 Real ax = p[3][0] - 3*p[2][0] + 3*p[1][0] - p[0][0],
531 bx = 3*p[2][0] - 6*p[1][0] + 3*p[0][0],
532 cx = 3*p[1][0] - 3*p[0][0],
535 Real t1 = INVALIDROOT, t2 = INVALIDROOT, t3 = INVALIDROOT, t, dydt;
545 t1 = - d / c; //equation devolved into: ct + d = 0 - solve...
548 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
549 if(quadratic_eqn(a,b,c,&t1,&t2) != 2) return 0;
555 //algorithm courtesy of Numerical Recipes in C (algorithm copied from pg. 184/185)
560 //if cn is 0 (or really really close), then we can simplify this...
565 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
566 if(quadratic_eqn(a,b,c,&t1,&t2) != 2)
568 t1 = t2 = INVALIDROOT;
573 //otherwise run the normal cubic root equation
574 Real Q = (an*an - 3.0*bn) / 9.0;
575 Real R = ((2.0*an*an - 9.0*bn)*an + 27.0*cn)/54.0;
579 Real theta = acos(R / sqrt(Q*Q*Q));
581 t1 = -2.0*sqrt(Q)*cos(theta/3) - an/3.0;
582 t2 = -2.0*sqrt(Q)*cos((theta+2*PI)/3.0) - an/3.0;
583 t3 = -2.0*sqrt(Q)*cos((theta-2*PI)/3.0) - an/3.0;
585 //don't need to reorder,l just need to eliminate double/triple roots
586 //if(t3 == t2 && t1 == t2) t2 = t3 = INVALIDROOT;
587 if(t3 == t2) t2 = t3 = INVALIDROOT;
588 if(t1 == t2) t1 = t2 = INVALIDROOT;
589 if(t1 == t3) t1 = t3 = INVALIDROOT;
592 Real signR = R < 0 ? -1 : 1;
593 Real A = - signR * pow(signR*R + sqrt(R*R - Q*Q*Q),1/3.0);
599 //single real root in this case
600 t1 = (A + B) - an/3.0;
605 //if(t1 != INVALIDROOT)
607 t = t1;//polish_cubicroot(a,b,c,d,t1,&dydt);
610 //const Real invt = 1 - t;
612 //find x val and it counts if it's to the left of the point
613 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
614 dydt = (3*a*t + 2*b)*t + c;
618 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
623 //if(t2 != INVALIDROOT)
625 t = t2;//polish_cubicroot(a,b,c,d,t2,&dydt);
628 //const Real invt = 1 - t;
630 //find x val and it counts if it's to the left of the point
631 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
632 dydt = (3*a*t + 2*b)*t + c;
636 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
641 //if(t3 != INVALIDROOT)
643 t = t3;//polish_cubicroot(a,b,c,d,t3,&dydt);
646 //const Real invt = 1 - t;
648 //find x val and it counts if it's to the left of the point
649 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
650 dydt = (3*a*t + 2*b)*t + c;
654 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
662 int intersect(Real x,Real y, Point *table) const
664 if((y < aabb.miny) || (y > aabb.maxy) || (x < aabb.minx)) return 0;
666 int i, curdeg, intersects = 0;
667 const int numcurves = degrees.size();
669 vector<Point>::const_iterator p = pointlist.begin();
671 for(i=0; i < numcurves; i++)
681 table[2] = *p; //we want to include the last point for the next curve
683 intersects += intersect_conic(x,y,table);
693 table[3] = *p; //we want to include the last point for the next curve
695 intersects += intersect_cubic(x,y,table);
702 warning("Invalid degree (%d) inserted into the list (index: %d)\n", curdeg, i);
712 struct Layer_Shape::Intersector
716 //! true iff aabb hasn't been initialized yet
721 enum IntersectorFlags
734 Real close_x,close_y;
736 vector<MonoSegment> segs; //monotonically increasing
737 vector<CurveArray> curves; //big array of consecutive curves
749 return (flags & NotClosed) || (cur_x != close_x) || (cur_y != close_y);
752 void move_to(Real x, Real y)
759 tangent[0] = tangent[1] = 0;
765 }else aabb.expand(x,y);
770 void line_to(Real x, Real y)
772 int dir = (y > cur_y)*1 + (-1)*(y < cur_y);
774 //check for context (if not line start a new segment)
775 //if we're not in line mode (covers 0 set case), or if directions are different (not valid for 0 direction)
776 if(prim != TYPE_LINE || (dir && segs.back().ydir != dir))
778 MonoSegment seg(dir,x,x,y,y);
780 seg.aabb.expand(cur_x,cur_y);
781 seg.pointlist.push_back(Point(cur_x,cur_y));
782 seg.pointlist.push_back(Point(x,y));
785 //add to the last segment, because it works
788 segs.back().pointlist.push_back(Point(x,y));
789 segs.back().aabb.expand(x,y);
796 aabb.expand(x,y); //expand the entire thing's bounding box
798 tangent[0] = x - cur_x;
799 tangent[1] = x - cur_y;
805 void conic_to_smooth(Real x, Real y)
807 const Real x1 = tangent[0]/2.0 + cur_x;
808 const Real y1 = tangent[1]/2.0 + cur_y;
813 void conic_to(Real x1, Real y1, Real x, Real y)
815 //if we're not already a curve start one
816 if(prim != TYPE_CURVE)
820 c.Start(Point(cur_x,cur_y));
821 c.AddConic(Point(x1,y1),Point(x,y));
826 curves.back().AddConic(Point(x1,y1),Point(x,y));
835 tangent[0] = 2*(x - x1);
836 tangent[1] = 2*(y - y1);
842 void curve_to_smooth(Real x2, Real y2, Real x, Real y)
844 Real x1 = tangent[0]/3.0 + cur_x;
845 Real y1 = tangent[1]/3.0 + cur_y;
847 curve_to(x1,y1,x2,y2,x,y);
850 void curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
852 //if we're not already a curve start one
853 if(prim != TYPE_CURVE)
857 c.Start(Point(cur_x,cur_y));
858 c.AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
863 curves.back().AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
869 //expand bounding box around ALL of it
874 tangent[0] = 3*(x - x2);
875 tangent[1] = 3*(y - y2);
883 if(flags & NotClosed)
885 if(cur_x != close_x || cur_y != close_y)
887 line_to(close_x,close_y);
894 //assumes the line to count the intersections with is (-1,0)
895 int intersect (Real x, Real y) const
899 vector<MonoSegment>::const_iterator s = segs.begin();
900 vector<CurveArray>::const_iterator c = curves.begin();
902 Point memory[3*MAX_SUBDIVISION_SIZE + 1];
904 for(i = 0; i < segs.size(); i++,s++)
906 inter += s->intersect(x,y);
909 for(i=0; i < curves.size(); i++,c++)
910 inter += c->intersect(x,y,memory);
915 //intersect an arbitrary line
916 //int intersect (Real x, Real y, Real vx, Real vy) {return 0;}
924 cur_x = cur_y = close_x = close_y = 0;
926 tangent[0] = tangent[1] = 0;
931 //*********** SCANLINE RENDERER SUPPORT STRUCTURES ***************
938 PenMark(int xin, int yin, Real c, Real a)
939 :y(yin),x(xin),cover(c),area(a) {}
941 void set(int xin, int yin, Real c, Real a) { y = yin; x = xin; cover = c; area = a; }
943 void setcoord(int xin, int yin) { y = yin; x = xin; }
945 void setcover(Real c, Real a) { cover = c; area = a; }
946 void addcover(Real c, Real a) { cover += c; area += a; }
948 bool operator < (const PenMark &rhs) const
950 return y == rhs.y ? x < rhs.x : y < rhs.y;
954 typedef rect<int> ContextRect;
956 class Layer_Shape::PolySpan
959 typedef deque<PenMark> cover_array;
961 Point arc[3*MAX_SUBDIVISION_SIZE + 1];
968 //ending position of last primitive
972 //starting position of current primitive list
976 //flags for the current segment
979 //the window that will be drawn (used for clipping)
982 //for assignment to flags value
989 //default constructor - 0 everything
990 PolySpan() :current(0,0,0,0),flags(NotSorted)
992 cur_x = cur_y = close_x = close_y = 0;
996 bool notclosed() const
998 return (flags & NotClosed) || (cur_x != close_x) || (cur_y != close_y);
1001 //0 out all the variables involved in processing
1005 cur_x = cur_y = close_x = close_y = 0;
1007 current.set(0,0,0,0);
1011 //add the current cell, but only if there is information to add
1014 if(current.cover || current.area)
1016 covers.push_back(current);
1020 //move to the next cell (cover values 0 initially), keeping the current if necessary
1021 void move_pen(int x, int y)
1023 if(y != current.y || x != current.x)
1026 current.set(x,y,0,0);
1030 //close the primitives with a line (or rendering will not work as expected)
1033 if(flags & NotClosed)
1035 if(cur_x != close_x || cur_y != close_y)
1037 line_to(close_x,close_y);
1039 current.setcover(0,0);
1041 flags &= ~NotClosed;
1045 // Not recommended - destroys any separation of spans currently held
1048 sort(covers.begin(),covers.end());
1052 //will sort the marks if they are not sorted
1055 if(flags & NotSorted)
1057 //only sort the open index
1059 current.setcover(0,0);
1061 sort(covers.begin() + open_index,covers.end());
1062 flags &= ~NotSorted;
1066 //encapsulate the current sublist of marks (used for drawing)
1067 void encapsulate_current()
1069 //sort the current list then reposition the open list section
1071 open_index = covers.size();
1074 //move to start a new primitive list (enclose the last primitive if need be)
1075 void move_to(Real x, Real y)
1080 move_pen((int)floor(x),(int)floor(y));
1081 close_y = cur_y = y;
1082 close_x = cur_x = x;
1085 //primitive_to functions
1086 void line_to(Real x, Real y);
1087 void conic_to(Real x1, Real y1, Real x, Real y);
1088 void cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y);
1090 void draw_scanline(int y, Real x1, Real y1, Real x2, Real y2);
1091 void draw_line(Real x1, Real y1, Real x2, Real y2);
1093 Real ExtractAlpha(Real area, WindingStyle winding_style)
1098 if (winding_style == WINDING_NON_ZERO)
1100 // non-zero winding style
1104 else // if (winding_style == WINDING_EVEN_ODD)
1106 // even-odd winding style
1110 // want pyramid like thing
1119 /* === M E T H O D S ======================================================= */
1121 Layer_Shape::Layer_Shape(const Real &a, const Color::BlendMethod m):
1122 Layer_Composite (a,m),
1123 edge_table (new Intersector),
1124 color (Color::black()),
1128 blurtype (Blur::FASTGAUSSIAN),
1130 winding_style (WINDING_NON_ZERO),
1132 lastbyteop (Primitive::NONE),
1137 Layer_Shape::~Layer_Shape()
1143 Layer_Shape::clear()
1145 edge_table->clear();
1150 Layer_Shape::set_param(const String & param, const ValueBase &value)
1158 IMPORT(winding_style);
1160 return Layer_Composite::set_param(param,value);
1164 Layer_Shape::get_param(const String ¶m)const
1172 EXPORT(winding_style);
1177 return Layer_Composite::get_param(param);
1181 Layer_Shape::get_param_vocab()const
1183 Layer::Vocab ret(Layer_Composite::get_param_vocab());
1185 ret.push_back(ParamDesc("color")
1186 .set_local_name(_("Color"))
1187 .set_description(_("Layer_Shape Color"))
1189 ret.push_back(ParamDesc("offset")
1190 .set_local_name(_("Position"))
1192 ret.push_back(ParamDesc("invert")
1193 .set_local_name(_("Invert"))
1195 ret.push_back(ParamDesc("antialias")
1196 .set_local_name(_("Antialiasing"))
1198 ret.push_back(ParamDesc("feather")
1199 .set_local_name(_("Feather"))
1202 ret.push_back(ParamDesc("blurtype")
1203 .set_local_name(_("Type of Feather"))
1204 .set_description(_("Type of feathering to use"))
1206 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
1207 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
1208 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
1209 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
1210 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
1212 ret.push_back(ParamDesc("winding_style")
1213 .set_local_name(_("Winding Style"))
1214 .set_description(_("Winding style to use"))
1216 .add_enum_value(WINDING_NON_ZERO,"nonzero",_("Non Zero"))
1217 .add_enum_value(WINDING_EVEN_ODD,"evenodd",_("Even/Odd"))
1223 synfig::Layer::Handle
1224 Layer_Shape::hit_check(synfig::Context context, const synfig::Point &p)const
1226 Point pos(p-offset);
1228 int intercepts = edge_table->intersect(pos[0],pos[1]);
1230 // If we have an odd number of intercepts, we are inside.
1231 // If we have an even number of intercepts, we are outside.
1232 bool intersect = ((!!intercepts) ^ invert);
1234 if(get_amount() == 0 || get_blend_method() == Color::BLEND_ALPHA_OVER)
1241 synfig::Layer::Handle tmp;
1242 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(p)))
1244 if(Color::is_onto(get_blend_method()))
1246 //if there's something in the lower layer then we're set...
1247 if(!context.hit_check(p).empty())
1248 return const_cast<Layer_Shape*>(this);
1249 }else if(get_blend_method() == Color::BLEND_ALPHA_OVER)
1251 synfig::info("layer_shape::hit_check - we've got alphaover");
1252 //if there's something in the lower layer then we're set...
1253 if(color.get_a() < 0.1 && get_amount() > .9)
1255 synfig::info("layer_shape::hit_check - can see through us... so nothing");
1257 }else return context.hit_check(p);
1259 return const_cast<Layer_Shape*>(this);
1262 return context.hit_check(p);
1266 Layer_Shape::get_color(Context context, const Point &p)const
1271 pp = Blur(feather,feather,blurtype)(p);
1273 Point pos(pp-offset);
1275 int intercepts = edge_table->intersect(pos[0],pos[1]);
1277 // If we have an odd number of intercepts, we are inside.
1278 // If we have an even number of intercepts, we are outside.
1279 bool intersect = ((!!intercepts) ^ invert);
1282 return context.get_color(pp);
1284 //Ok, we're inside... bummmm ba bum buM...
1285 if(get_blend_method() == Color::BLEND_STRAIGHT && get_amount() == 1)
1288 return Color::blend(color,context.get_color(p),get_amount(),get_blend_method());
1291 //************** SCANLINE RENDERING *********************
1292 void Layer_Shape::PolySpan::line_to(Real x, Real y)
1295 bool afterx = false;
1297 const Real xin(x), yin(y);
1299 Real dx = x - cur_x;
1300 Real dy = y - cur_y;
1304 //outside y - ignore entirely
1305 if( (cur_y >= window.maxy && y >= window.maxy)
1306 ||(cur_y < window.miny && y < window.miny) )
1311 else //not degenerate - more complicated
1313 if(dy > 0) //be sure it's not tooooo small
1315 // cur_y ... window.miny ... window.maxy ... y
1317 //initial degenerate - initial clip
1318 if(cur_y < window.miny)
1320 //new clipped start point (must also move pen)
1321 n[2] = cur_x + (window.miny - cur_y) * dx / dy;
1324 cur_y = window.miny;
1325 move_pen((int)floor(cur_x),window.miny);
1328 //generate data for the ending clipped info
1331 //initial line to intersection (and degenerate)
1332 n[2] = x + (window.maxy - y) * dx / dy;
1341 //initial degenerate - initial clip
1342 if(cur_y > window.maxy)
1344 //new clipped start point (must also move pen)
1345 n[2] = cur_x + (window.maxy - cur_y) * dx / dy;
1348 cur_y = window.maxy;
1349 move_pen((int)floor(cur_x),window.maxy);
1352 //generate data for the ending clipped info
1355 //initial line to intersection (and degenerate)
1356 n[2] = x + (window.miny - y) * dx / dy;
1364 //all degenerate - but require bounded clipped values
1365 if( (cur_x >= window.maxx && x >= window.maxx)
1366 ||(cur_x < window.minx && x < window.minx) )
1368 //clip both vertices - but only needed in the x direction
1369 cur_x = max(cur_x, (Real)window.minx);
1370 cur_x = min(cur_x, (Real)window.maxx);
1372 //clip the dest values - y is already clipped
1373 x = max(x,(Real)window.minx);
1374 x = min(x,(Real)window.maxx);
1376 //must start at new point...
1377 move_pen((int)floor(cur_x),(int)floor(cur_y));
1379 draw_line(cur_x,cur_y,x,y);
1389 //initial degenerate - initial clip
1390 if(cur_x < window.minx)
1392 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1393 n[2] = cur_y + (window.minx - cur_x) * dy / dx;
1395 move_pen(window.minx,(int)floor(cur_y));
1396 draw_line(window.minx,cur_y,window.minx,n[2]);
1398 cur_x = window.minx;
1402 //generate data for the ending clipped info
1405 //initial line to intersection (and degenerate)
1406 n[2] = y + (window.maxx - x) * dy / dx;
1418 //initial degenerate - initial clip
1419 if(cur_x > window.maxx)
1421 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1422 n[2] = cur_y + (window.maxx - cur_x) * dy / dx;
1424 move_pen(window.maxx,(int)floor(cur_y));
1425 draw_line(window.maxx,cur_y,window.maxx,n[2]);
1427 cur_x = window.maxx;
1431 //generate data for the ending clipped info
1434 //initial line to intersection (and degenerate)
1435 n[2] = y + (window.minx - x) * dy / dx;
1447 move_pen((int)floor(cur_x),(int)floor(cur_y));
1448 //draw the relevant line (clipped)
1449 draw_line(cur_x,cur_y,x,y);
1453 draw_line(x,y,n[0],n[1]);
1460 } catch(...) { synfig::error("line_to: cur_x=%f, cur_y=%f, x=%f, y=%f", cur_x, cur_y, x, y); throw; }
1462 flags |= NotClosed|NotSorted;
1465 static inline bool clip_conic(const Point *const p, const ContextRect &r)
1467 const Real minx = min(min(p[0][0],p[1][0]),p[2][0]);
1468 const Real miny = min(min(p[0][1],p[1][1]),p[2][1]);
1469 const Real maxx = max(max(p[0][0],p[1][0]),p[2][0]);
1470 const Real maxy = max(max(p[0][1],p[1][1]),p[2][1]);
1472 return (minx > r.maxx) ||
1478 static inline bool clip_cubic(const Point *const p, const ContextRect &r)
1480 /*const Real minx = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
1481 const Real miny = min(min(p[0][1],p[1][1]),min(p[2][1],p[3][1]));
1482 const Real maxx = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][1]));
1483 const Real maxy = max(max(p[0][1],p[1][1]),max(p[2][1],p[3][1]));
1485 return (minx > r.maxx) ||
1490 return ((p[0][0] > r.maxx) && (p[1][0] > r.maxx) && (p[2][0] > r.maxx) && (p[3][0] > r.maxx)) ||
1491 ((p[0][0] < r.minx) && (p[1][0] < r.minx) && (p[2][0] < r.minx) && (p[3][0] < r.minx)) ||
1492 ((p[0][1] > r.maxy) && (p[1][1] > r.maxy) && (p[2][1] > r.maxy) && (p[3][1] > r.maxy)) ||
1493 ((p[0][1] < r.miny) && (p[1][1] < r.miny) && (p[2][1] < r.miny) && (p[3][1] < r.miny));
1496 static inline Real max_edges_cubic(const Point *const p)
1498 const Real x1 = p[1][0] - p[0][0];
1499 const Real y1 = p[1][1] - p[0][1];
1501 const Real x2 = p[2][0] - p[1][0];
1502 const Real y2 = p[2][1] - p[1][1];
1504 const Real x3 = p[3][0] - p[2][0];
1505 const Real y3 = p[3][1] - p[2][1];
1507 const Real d1 = x1*x1 + y1*y1;
1508 const Real d2 = x2*x2 + y2*y2;
1509 const Real d3 = x3*x3 + y3*y3;
1511 return max(max(d1,d2),d3);
1514 static inline Real max_edges_conic(const Point *const p)
1516 const Real x1 = p[1][0] - p[0][0];
1517 const Real y1 = p[1][1] - p[0][1];
1519 const Real x2 = p[2][0] - p[1][0];
1520 const Real y2 = p[2][1] - p[1][1];
1522 const Real d1 = x1*x1 + y1*y1;
1523 const Real d2 = x2*x2 + y2*y2;
1528 void Layer_Shape::PolySpan::conic_to(Real x1, Real y1, Real x, Real y)
1530 Point *current = arc;
1533 bool onsecond = false;
1535 arc[0] = Point(x,y);
1536 arc[1] = Point(x1,y1);
1537 arc[2] = Point(cur_x,cur_y);
1539 //just draw the line if it's outside
1540 if(clip_conic(arc,window))
1546 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1547 while(current >= arc)
1549 if(num >= MAX_SUBDIVISION_SIZE)
1551 warning("Curve subdivision somehow ran out of space while tessellating!");
1557 //if the curve is clipping then draw degenerate
1558 if(clip_conic(current,window))
1560 line_to(current[0][0],current[0][1]); //backwards so front is destination
1562 if(onsecond) level--;
1567 //if we are not at the level minimum
1568 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1570 Subd_Conic_Stack(current);
1571 current += 2; //cursor on second curve
1577 //split it again, if it's too big
1578 if(max_edges_conic(current) > 0.25) //distance of .5 (cover no more than half the pixel)
1580 Subd_Conic_Stack(current);
1581 current += 2; //cursor on second curve
1586 else //NOT TOO BIG? RENDER!!!
1588 //cur_x,cur_y = current[2], so we need to go 1,0
1589 line_to(current[1][0],current[1][1]);
1590 line_to(current[0][0],current[0][1]);
1593 if(onsecond) level--;
1600 void Layer_Shape::PolySpan::cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
1602 Point *current = arc;
1605 bool onsecond = false;
1607 arc[0] = Point(x,y);
1608 arc[1] = Point(x2,y2);
1609 arc[2] = Point(x1,y1);
1610 arc[3] = Point(cur_x,cur_y);
1612 //just draw the line if it's outside
1613 if(clip_cubic(arc,window))
1619 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1620 while(current >= arc) //once current goes below arc, there are no more curves left
1622 if(num >= MAX_SUBDIVISION_SIZE)
1624 warning("Curve subdivision somehow ran out of space while tessellating!");
1631 //if we are not at the level minimum
1632 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1634 Subd_Cubic_Stack(current);
1635 current += 3; //cursor on second curve
1641 //if the curve is clipping then draw degenerate
1642 if(clip_cubic(current,window))
1644 line_to(current[0][0],current[0][1]); //backwards so front is destination
1646 if(onsecond) level--;
1652 //split it again, if it's too big
1653 if(max_edges_cubic(current) > 0.25) //could use max_edges<3>
1655 Subd_Cubic_Stack(current);
1656 current += 3; //cursor on second curve
1661 else //NOT TOO BIG? RENDER!!!
1663 //cur_x,cur_y = current[3], so we need to go 2,1,0
1664 line_to(current[2][0],current[2][1]);
1665 line_to(current[1][0],current[1][1]);
1666 line_to(current[0][0],current[0][1]);
1669 if(onsecond) level--;
1676 //******************** LINE ALGORITHMS ****************************
1677 // THESE CALCULATE THE AREA AND THE COVER FOR THE MARKS, TO THEN SCAN CONVERT
1678 // - BROKEN UP INTO SCANLINES (draw_line - y intersections),
1679 // THEN THE COVER AND AREA PER TOUCHED PIXEL IS CALCULATED (draw_scanline - x intersections)
1680 void Layer_Shape::PolySpan::draw_scanline(int y, Real x1, Real fy1, Real x2, Real fy2)
1682 int ix1 = (int)floor(x1);
1683 int ix2 = (int)floor(x2);
1684 Real fx1 = x1 - ix1;
1685 Real fx2 = x2 - ix2;
1687 Real dx,dy,dydx,mult;
1692 //case horizontal line
1695 move_pen(ix2,y); //pen needs to be at the last coord
1699 //case all in same pixel
1700 if(ix1 == ix2) //impossible for degenerate case (covered by the previous cases)
1702 current.addcover(dy,(fx1 + fx2)*dy/2); //horizontal trapezoid area
1708 // ----> fx1...1 0...1 ... 0...1 0...fx2
1711 //set initial values
1712 //Iterate through the covered pixels
1713 mult = (1 - fx1)*dydx; //next y intersection diff value (at 1)
1716 current.addcover(mult,(1 + fx1)*mult/2); // fx1,fy1,1,fy@1 - starting trapezoidal area
1718 //move to the next pixel
1724 //set up for whole ones
1727 //trapezoid(0,y1,1,y1+dydx);
1728 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1730 //move to next pixel (+1)
1737 //final y-pos - last intersect pos
1739 current.addcover(mult,(0+fx2)*mult/2);
1742 // fx2...1 0...1 ... 0...1 0...fx1 <----
1743 //mult = (0 - fx1) * dy / dx;
1744 //neg sign sucked into dydx
1747 //set initial values
1748 //Iterate through the covered pixels
1749 mult = fx1*dydx; //next y intersection diff value
1752 current.addcover(mult,fx1*mult/2); // fx1,fy1,0,fy@0 - starting trapezoidal area
1754 //move to next pixel
1760 //set up for whole ones
1763 //trapezoid(0,y1,1,y1+dydx);
1764 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1766 //move to next pixel (-1)
1773 mult = fy2 - fy1; //final y-pos - last intersect pos
1775 current.addcover(mult,(fx2+1)*mult/2);
1779 void Layer_Shape::PolySpan::draw_line(Real x1, Real y1, Real x2, Real y2)
1781 int iy1 = (int)floor(y1);
1782 int iy2 = (int)floor(y2);
1783 Real fy1 = y1 - iy1;
1784 Real fy2 = y2 - iy2;
1786 assert(!isnan(fy1));
1787 assert(!isnan(fy2));
1789 Real dx,dy,dxdy,mult,x_from,x_to;
1791 const Real SLOPE_EPSILON = 1e-10;
1793 //case all one scanline
1796 draw_scanline(iy1,x1,y1,x2,y2);
1804 //case vertical line
1805 if(dx < SLOPE_EPSILON && dx > -SLOPE_EPSILON)
1807 //calc area and cover on vertical line
1810 // ----> fx1...1 0...1 ... 0...1 0...fx2
1813 int ix1 = (int)floor(x1);
1814 Real fx1 = x1 - ix1;
1819 current.addcover(sub,fx1*sub);
1824 //move pen to next pixel
1830 current.addcover(1,fx1);
1838 current.addcover(fy2,fy2*fx1);
1843 int ix1 = (int)floor(x1);
1844 Real fx1 = x1 - ix1;
1849 current.addcover(sub,fx1*sub);
1858 //accumulate in current pixel
1859 current.addcover(-1,-fx1);
1866 current.addcover(fy2-1,(fy2-1)*fx1);
1871 //case normal line - guaranteed dx != 0 && dy != 0
1873 //calculate the initial intersection with "next" scanline
1878 mult = (1 - fy1) * dxdy;
1880 //x intersect scanline
1882 draw_scanline(iy1,x1,fy1,x_from,1);
1887 move_pen((int)floor(x_from),iy1);
1891 //keep up on the x axis, and render the current scanline
1892 x_to = x_from + dxdy;
1893 draw_scanline(iy1,x_from,0,x_to,1);
1896 //move to next pixel
1898 move_pen((int)floor(x_from),iy1);
1901 //draw the last one, fractional
1902 draw_scanline(iy2,x_from,0,x2,fy2);
1910 //x intersect scanline
1912 draw_scanline(iy1,x1,fy1,x_from,0);
1917 move_pen((int)floor(x_from),iy1);
1921 x_to = x_from + dxdy;
1922 draw_scanline(iy1,x_from,1,x_to,0);
1926 move_pen((int)floor(x_from),iy1);
1928 //draw the last one, fractional
1929 draw_scanline(iy2,x_from,1,x2,fy2);
1933 //****** LAYER PEN OPERATIONS (move_to, line_to, etc.) ******
1934 void Layer_Shape::move_to(Real x, Real y)
1936 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1940 op.operation = Primitive::MOVE_TO;
1941 op.number = 1; //one point for now
1943 if(lastbyteop == Primitive::MOVE_TO)
1945 char *ptr = &bytestream[lastoppos];
1946 memcpy(ptr,&op,sizeof(op));
1947 memcpy(ptr+sizeof(op),&p,sizeof(p));
1949 else //make a new op
1951 lastbyteop = Primitive::MOVE_TO;
1952 lastoppos = bytestream.size();
1954 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
1955 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
1958 edge_table->move_to(x,y);
1961 void Layer_Shape::close()
1965 op.operation = Primitive::CLOSE;
1968 if(lastbyteop == Primitive::CLOSE)
1972 lastbyteop = Primitive::CLOSE;
1973 lastoppos = bytestream.size();
1975 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert header
1978 edge_table->close();
1979 //should not affect the bounding box since it would just be returning to old point...
1982 void Layer_Shape::endpath()
1986 op.operation = Primitive::END;
1989 if(lastbyteop == Primitive::END || lastbyteop == Primitive::NONE)
1993 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1));
1995 //should not affect the bounding box since it would just be returning to old point... if at all
1998 void Layer_Shape::line_to(Real x, Real y)
2003 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2007 op.operation = Primitive::LINE_TO;
2008 op.number = 1; //one point for now
2010 if(lastbyteop == Primitive::MOVE_TO || lastbyteop == Primitive::LINE_TO)
2012 //only need to insert the point
2013 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2015 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2016 prim->number++; //increment number of points in the list
2019 lastbyteop = Primitive::LINE_TO;
2020 lastoppos = bytestream.size();
2022 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2023 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2026 edge_table->line_to(x,y);
2029 void Layer_Shape::conic_to(Real x1, Real y1, Real x, Real y)
2031 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*2;
2036 op.operation = Primitive::CONIC_TO;
2037 op.number = 2; //2 points for now
2039 if(lastbyteop == Primitive::CONIC_TO)
2041 //only need to insert the new points
2042 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2043 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2045 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2046 prim->number += 2; //increment number of points in the list
2049 lastbyteop = Primitive::CONIC_TO;
2050 lastoppos = bytestream.size();
2052 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2053 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2054 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2057 edge_table->conic_to(x1,y1,x,y);
2060 void Layer_Shape::conic_to_smooth(Real x, Real y) //x1,y1 derived from current tangent
2062 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2066 op.operation = Primitive::CONIC_TO_SMOOTH;
2067 op.number = 1; //2 points for now
2069 if(lastbyteop == Primitive::CONIC_TO_SMOOTH)
2071 //only need to insert the new point
2072 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2074 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2075 prim->number += 1; //increment number of points in the list
2078 lastbyteop = Primitive::CONIC_TO_SMOOTH;
2079 lastoppos = bytestream.size();
2081 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2082 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2085 edge_table->conic_to_smooth(x,y);
2088 void Layer_Shape::curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
2090 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2096 op.operation = Primitive::CUBIC_TO;
2097 op.number = 3; //3 points for now
2099 if(lastbyteop == Primitive::CUBIC_TO)
2101 //only need to insert the new points
2102 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2103 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2104 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2106 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2107 prim->number += 3; //increment number of points in the list
2110 lastbyteop = Primitive::CUBIC_TO;
2111 lastoppos = bytestream.size();
2113 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2114 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2115 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2116 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2119 edge_table->curve_to(x1,y1,x2,y2,x,y);
2122 void Layer_Shape::curve_to_smooth(Real x2, Real y2, Real x, Real y) //x1,y1 derived from current tangent
2124 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2129 op.operation = Primitive::CUBIC_TO_SMOOTH;
2130 op.number = 2; //3 points for now
2132 if(lastbyteop == Primitive::CUBIC_TO_SMOOTH)
2134 //only need to insert the new points
2135 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2136 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2138 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2139 prim->number += 2; //increment number of points in the list
2142 lastbyteop = Primitive::CUBIC_TO_SMOOTH;
2143 lastoppos = bytestream.size();
2145 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2146 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2147 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2151 // ACCELERATED RENDER FUNCTION - TRANSLATE BYTE CODE INTO FUNCTION CALLS
2153 bool Layer_Shape::render_polyspan(Surface *surface, PolySpan &polyspan,
2154 Color::BlendMethod got_blend_method, Color::value_type got_amount) const
2156 Surface::alpha_pen p(surface->begin(),got_amount,_BlendFunc(got_blend_method));
2157 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2158 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2160 Real cover,area,alpha;
2167 if(cur_mark == end_mark)
2172 p.move_to(polyspan.window.minx,polyspan.window.miny);
2173 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2178 //fill initial rect / line
2181 //fill all the area above the first vertex
2182 p.move_to(polyspan.window.minx,polyspan.window.miny);
2183 y = polyspan.window.miny;
2184 int l = polyspan.window.maxx - polyspan.window.minx;
2186 p.put_block(cur_mark->y - polyspan.window.miny,l);
2188 //fill the area to the left of the first vertex on that line
2189 l = cur_mark->x - polyspan.window.minx;
2190 p.move_to(polyspan.window.minx,cur_mark->y);
2191 if(l) p.put_hline(l);
2201 area = cur_mark->area;
2202 cover += cur_mark->cover;
2204 //accumulate for the current pixel
2205 while(++cur_mark != polyspan.covers.end())
2207 if(y != cur_mark->y || x != cur_mark->x)
2210 area += cur_mark->area;
2211 cover += cur_mark->cover;
2214 //draw pixel - based on covered area
2215 if(area) //if we're ok, draw the current pixel
2217 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2218 if(invert) alpha = 1 - alpha;
2222 if(alpha >= .5) p.put_value();
2224 else if(alpha) p.put_value_alpha(alpha);
2230 //if we're done, don't use iterator and exit
2231 if(cur_mark == end_mark) break;
2233 //if there is no more live pixels on this line, goto next
2234 if(y != cur_mark->y)
2238 //fill the area at the end of the line
2239 p.put_hline(polyspan.window.maxx - x);
2241 //fill area at the beginning of the next line
2242 p.move_to(polyspan.window.minx,cur_mark->y);
2243 p.put_hline(cur_mark->x - polyspan.window.minx);
2251 //draw span to next pixel - based on total amount of pixel cover
2254 alpha = polyspan.ExtractAlpha(cover, winding_style);
2255 if(invert) alpha = 1 - alpha;
2259 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2261 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2265 //fill the after stuff
2268 //fill the area at the end of the line
2269 p.put_hline(polyspan.window.maxx - x);
2271 //fill area at the beginning of the next line
2272 p.move_to(polyspan.window.minx,y+1);
2273 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2279 bool Layer_Shape::render_polyspan(etl::surface<float> *surface, PolySpan &polyspan) const
2281 etl::surface<float>::pen p(surface->begin());
2282 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2283 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2285 Real cover,area,alpha;
2291 //the pen always writes 1 (unless told to do otherwise)
2294 if(cur_mark == end_mark)
2299 p.move_to(polyspan.window.minx,polyspan.window.miny);
2300 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2305 //fill initial rect / line
2308 //fill all the area above the first vertex
2309 p.move_to(polyspan.window.minx,polyspan.window.miny);
2310 y = polyspan.window.miny;
2311 int l = polyspan.window.maxx - polyspan.window.minx;
2313 p.put_block(cur_mark->y - polyspan.window.miny,l);
2315 //fill the area to the left of the first vertex on that line
2316 l = cur_mark->x - polyspan.window.minx;
2317 p.move_to(polyspan.window.minx,cur_mark->y);
2318 if(l) p.put_hline(l);
2327 area = cur_mark->area;
2328 cover += cur_mark->cover;
2330 //accumulate for the current pixel
2331 while(++cur_mark != polyspan.covers.end())
2333 if(y != cur_mark->y || x != cur_mark->x)
2336 area += cur_mark->area;
2337 cover += cur_mark->cover;
2340 //draw pixel - based on covered area
2341 if(area) //if we're ok, draw the current pixel
2343 alpha = 1 - polyspan.ExtractAlpha(cover - area, winding_style);
2346 if(alpha >= .5) p.put_value();
2348 else if(alpha) p.put_value(alpha);
2354 //if we're done, don't use iterator and exit
2355 if(cur_mark == end_mark) break;
2357 //if there is no more live pixels on this line, goto next
2358 if(y != cur_mark->y)
2360 //fill the area at the end of the line
2361 p.put_hline(polyspan.window.maxx - x);
2363 //fill area at the beginning of the next line
2364 p.move_to(polyspan.window.minx,cur_mark->y);
2365 p.put_hline(cur_mark->x - polyspan.window.minx);
2372 //draw span to next pixel - based on total amount of pixel cover
2375 alpha = 1 - polyspan.ExtractAlpha(cover, winding_style);
2378 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2380 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2384 //fill the area at the end of the line
2385 p.put_hline(polyspan.window.maxx - x);
2387 //fill area at the beginning of the next line
2388 p.move_to(polyspan.window.minx,y+1);
2389 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2399 area = cur_mark->area;
2400 cover += cur_mark->cover;
2402 //accumulate for the current pixel
2403 while(++cur_mark != polyspan.covers.end())
2405 if(y != cur_mark->y || x != cur_mark->x)
2408 area += cur_mark->area;
2409 cover += cur_mark->cover;
2412 //draw pixel - based on covered area
2413 if(area) //if we're ok, draw the current pixel
2415 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2418 if(alpha >= .5) p.put_value();
2420 else if(alpha) p.put_value(alpha);
2426 //if we're done, don't use iterator and exit
2427 if(cur_mark == end_mark) break;
2429 //if there is no more live pixels on this line, goto next
2430 if(y != cur_mark->y)
2437 //draw span to next pixel - based on total amount of pixel cover
2440 alpha = polyspan.ExtractAlpha(cover, winding_style);
2443 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2445 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2454 Layer_Shape::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
2456 const unsigned int w = renddesc.get_w();
2457 const unsigned int h = renddesc.get_h();
2459 const Real pw = abs(renddesc.get_pw());
2460 const Real ph = abs(renddesc.get_ph());
2462 //const Real OFFSET_EPSILON = 1e-8;
2463 SuperCallback stageone(cb,1,10000,15001+renddesc.get_h());
2464 SuperCallback stagetwo(cb,10000,10001+renddesc.get_h(),15001+renddesc.get_h());
2465 SuperCallback stagethree(cb,10001+renddesc.get_h(),15001+renddesc.get_h(),15001+renddesc.get_h());
2467 // Render what is behind us
2469 //clip if it satisfies the invert solid thing
2470 if(is_solid_color() && invert)
2472 Rect aabb = edge_table->aabb;
2473 Point tl = renddesc.get_tl() - offset;
2475 Real pw = renddesc.get_pw(),
2476 ph = renddesc.get_ph();
2480 Real pixelfeatherx = abs(feather/pw),
2481 pixelfeathery = abs(feather/ph);
2483 nrect.set_point((aabb.minx - tl[0])/pw,(aabb.miny - tl[1])/ph);
2484 nrect.expand((aabb.maxx - tl[0])/pw,(aabb.maxy - tl[1])/ph);
2486 RendDesc optdesc(renddesc);
2488 //make sure to expand so we gain subpixels rather than lose them
2489 nrect.minx = floor(nrect.minx-pixelfeatherx); nrect.miny = floor(nrect.miny-pixelfeathery);
2490 nrect.maxx = ceil(nrect.maxx+pixelfeatherx); nrect.maxy = ceil(nrect.maxy+pixelfeathery);
2492 //make sure the subwindow is clipped with our tile window (minimize useless drawing)
2493 set_intersect(nrect,nrect,Rect(0,0,renddesc.get_w(),renddesc.get_h()));
2495 //must resize the surface first
2496 surface->set_wh(renddesc.get_w(),renddesc.get_h());
2499 //only render anything if it's visible from our current tile
2502 //set the subwindow to the viewable pixels and render it to the subsurface
2503 optdesc.set_subwindow((int)nrect.minx, (int)nrect.miny,
2504 (int)(nrect.maxx - nrect.minx), (int)(nrect.maxy - nrect.miny));
2506 Surface optimizedbacksurf;
2507 if(!context.accelerated_render(&optimizedbacksurf,quality,optdesc,&stageone))
2510 //blit that onto the original surface so we can pretend that nothing ever happened
2511 Surface::pen p = surface->get_pen((int)nrect.minx,(int)nrect.miny);
2512 optimizedbacksurf.blit_to(p);
2516 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
2520 if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
2524 //we have to blur rather than be crappy
2526 //so make a separate surface
2527 RendDesc workdesc(renddesc);
2529 etl::surface<float> shapesurface;
2531 //the expanded size = 1/2 the size in each direction rounded up
2532 int halfsizex = (int) (abs(feather*.5/pw) + 3),
2533 halfsizey = (int) (abs(feather*.5/ph) + 3);
2535 //expand by 1/2 size in each direction on either side
2542 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2545 case Blur::FASTGAUSSIAN:
2552 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2555 case Blur::GAUSSIAN:
2557 #define GAUSSIAN_ADJUSTMENT (0.05)
2558 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
2559 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
2564 halfsizex = (int)(abs(pw)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2565 halfsizey = (int)(abs(ph)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2567 halfsizex = (halfsizex + 1)/2;
2568 halfsizey = (halfsizey + 1)/2;
2569 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
2575 shapesurface.set_wh(workdesc.get_w(),workdesc.get_h());
2576 shapesurface.clear();
2579 if(!render_shape(&shapesurface,quality,workdesc,&stagetwo))return false;
2582 Blur(feather,feather,blurtype,&stagethree)(shapesurface,workdesc.get_br()-workdesc.get_tl(),shapesurface);
2584 //blend with stuff below it...
2585 unsigned int u = halfsizex, v = halfsizey, x = 0, y = 0;
2586 for(y = 0; y < h; y++,v++)
2589 for(x = 0; x < w; x++,u++)
2591 float a = shapesurface[v][u];
2594 //a = floor(a*255+0.5f)/255;
2595 (*surface)[y][x]=Color::blend(color,(*surface)[y][x],a*get_amount(),get_blend_method());
2597 //else (*surface)[y][x] = worksurface[v][u];
2602 if(cb && !cb->amount_complete(100,100))
2604 synfig::warning("Layer_Shape: could not set amount complete");
2611 //might take out to reduce code size
2612 return render_shape(surface,true,quality,renddesc,&stagetwo);
2618 Layer_Shape::render_shape(Surface *surface,bool useblend,int /*quality*/,
2619 const RendDesc &renddesc, ProgressCallback *cb)const
2623 SuperCallback progress(cb,0,renddesc.get_h(),renddesc.get_h());
2625 // If our amount is set to zero, no need to render anything
2629 //test new polygon renderer
2631 // Width and Height of a pixel
2632 const int w = renddesc.get_w();
2633 const int h = renddesc.get_h();
2634 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2635 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2637 const Point tl = renddesc.get_tl();
2639 Vector tangent (0,0);
2643 // if the pixels are zero sized then we're too zoomed out to see anything
2644 if (pw == 0 || ph == 0)
2647 //optimization for tessellating only inside tiles
2648 span.window.minx = 0;
2649 span.window.miny = 0;
2650 span.window.maxx = w;
2651 span.window.maxy = h;
2653 //pointers for processing the bytestream
2654 const char *current = &bytestream[0];
2655 const char *end = &bytestream[bytestream.size()];
2657 int operation = Primitive::NONE;
2664 Real x,y,x1,y1,x2,y2;
2667 while(current < end)
2673 //get the op code safely
2674 curprim = (Primitive *)current;
2676 //advance past indices
2677 current += sizeof(Primitive);
2680 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2684 //get the relevant data
2685 operation = curprim->operation;
2686 number = curprim->number;
2688 if(operation == Primitive::END)
2691 if(operation == Primitive::CLOSE)
2693 if(span.notclosed())
2695 tangent[0] = span.close_x - span.cur_x;
2696 tangent[1] = span.close_y - span.cur_y;
2702 data = (Point*)current;
2703 current += sizeof(Point)*number;
2705 //check data positioning
2708 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2712 } catch(...) { synfig::error("Layer_Shape::render_shape()1: Caught an exception after %d loops, rethrowing...", tmp); throw; }
2714 //transfer all the data - RLE optimized
2715 for(curnum=0; curnum < number;)
2719 case Primitive::MOVE_TO:
2721 x = data[curnum][0];
2722 x = (x - tl[0] + offset[0])*pw;
2723 y = data[curnum][1];
2724 y = (y - tl[1] + offset[1])*ph;
2735 tangent[0] = x - span.cur_x;
2736 tangent[1] = y - span.cur_y;
2741 curnum++; //only advance one point
2746 case Primitive::LINE_TO:
2748 x = data[curnum][0];
2749 x = (x - tl[0] + offset[0])*pw;
2750 y = data[curnum][1];
2751 y = (y - tl[1] + offset[1])*ph;
2753 tangent[0] = x - span.cur_x;
2754 tangent[1] = y - span.cur_y;
2761 case Primitive::CONIC_TO:
2763 x = data[curnum+1][0];
2764 x = (x - tl[0] + offset[0])*pw;
2765 y = data[curnum+1][1];
2766 y = (y - tl[1] + offset[1])*ph;
2768 x1 = data[curnum][0];
2769 x1 = (x1 - tl[0] + offset[0])*pw;
2770 y1 = data[curnum][1];
2771 y1 = (y1 - tl[1] + offset[1])*ph;
2773 tangent[0] = 2*(x - x1);
2774 tangent[1] = 2*(y - y1);
2776 span.conic_to(x1,y1,x,y);
2781 case Primitive::CONIC_TO_SMOOTH:
2783 x = data[curnum][0];
2784 x = (x - tl[0] + offset[0])*pw;
2785 y = data[curnum][1];
2786 y = (y - tl[1] + offset[1])*ph;
2788 x1 = span.cur_x + tangent[0]/2;
2789 y1 = span.cur_y + tangent[1]/2;
2791 tangent[0] = 2*(x - x1);
2792 tangent[1] = 2*(y - y1);
2794 span.conic_to(x1,y1,x,y);
2800 case Primitive::CUBIC_TO:
2802 x = data[curnum+2][0];
2803 x = (x - tl[0] + offset[0])*pw;
2804 y = data[curnum+2][1];
2805 y = (y - tl[1] + offset[1])*ph;
2807 x2 = data[curnum+1][0];
2808 x2 = (x2 - tl[0] + offset[0])*pw;
2809 y2 = data[curnum+1][1];
2810 y2 = (y2 - tl[1] + offset[1])*ph;
2812 x1 = data[curnum][0];
2813 x1 = (x1 - tl[0] + offset[0])*pw;
2814 y1 = data[curnum][1];
2815 y1 = (y1 - tl[1] + offset[1])*ph;
2817 tangent[0] = 2*(x - x2);
2818 tangent[1] = 2*(y - y2);
2820 span.cubic_to(x1,y1,x2,y2,x,y);
2826 case Primitive::CUBIC_TO_SMOOTH:
2828 x = data[curnum+1][0];
2829 x = (x - tl[0] + offset[0])*pw;
2830 y = data[curnum+1][1];
2831 y = (y - tl[1] + offset[1])*ph;
2833 x2 = data[curnum][0];
2834 x2 = (x2 - tl[0] + offset[0])*pw;
2835 y2 = data[curnum][1];
2836 y2 = (y2 - tl[1] + offset[1])*ph;
2838 x1 = span.cur_x + tangent[0]/3.0;
2839 y1 = span.cur_y + tangent[1]/3.0;
2841 tangent[0] = 2*(x - x2);
2842 tangent[1] = 2*(y - y2);
2844 span.cubic_to(x1,y1,x2,y2,x,y);
2853 //sort the bastards so we can render everything
2856 return render_polyspan(surface, span,
2857 useblend?get_blend_method():Color::BLEND_STRAIGHT,
2858 useblend?get_amount():1.0);
2862 Layer_Shape::render_shape(etl::surface<float> *surface,int /*quality*/,
2863 const RendDesc &renddesc, ProgressCallback */*cb*/)const
2865 // If our amount is set to zero, no need to render anything
2869 //test new polygon renderer
2871 // Width and Height of a pixel
2872 const int w = renddesc.get_w();
2873 const int h = renddesc.get_h();
2874 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2875 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2877 const Point tl = renddesc.get_tl();
2879 Vector tangent (0,0);
2883 //optimization for tessellating only inside tiles
2884 span.window.minx = 0;
2885 span.window.miny = 0;
2886 span.window.maxx = w;
2887 span.window.maxy = h;
2889 //pointers for processing the bytestream
2890 const char *current = &bytestream[0];
2891 const char *end = &bytestream[bytestream.size()];
2893 int operation = Primitive::NONE;
2900 Real x,y,x1,y1,x2,y2;
2902 while(current < end)
2904 //get the op code safely
2905 curprim = (Primitive *)current;
2907 //advance past indices
2908 current += sizeof(Primitive);
2911 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2915 //get the relevant data
2916 operation = curprim->operation;
2917 number = curprim->number;
2919 if(operation == Primitive::END)
2922 if(operation == Primitive::CLOSE)
2924 if(span.notclosed())
2926 tangent[0] = span.close_x - span.cur_x;
2927 tangent[1] = span.close_y - span.cur_y;
2933 data = (Point*)current;
2934 current += sizeof(Point)*number;
2936 //check data positioning
2939 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2943 //transfer all the data
2944 for(curnum=0; curnum < number;)
2948 case Primitive::MOVE_TO:
2950 x = data[curnum][0];
2951 x = (x - tl[0] + offset[0])*pw;
2952 y = data[curnum][1];
2953 y = (y - tl[1] + offset[1])*ph;
2964 tangent[0] = x - span.cur_x;
2965 tangent[1] = y - span.cur_y;
2970 curnum++; //only advance one point
2975 case Primitive::LINE_TO:
2977 x = data[curnum][0];
2978 x = (x - tl[0] + offset[0])*pw;
2979 y = data[curnum][1];
2980 y = (y - tl[1] + offset[1])*ph;
2982 tangent[0] = x - span.cur_x;
2983 tangent[1] = y - span.cur_y;
2990 case Primitive::CONIC_TO:
2992 x = data[curnum+1][0];
2993 x = (x - tl[0] + offset[0])*pw;
2994 y = data[curnum+1][1];
2995 y = (y - tl[1] + offset[1])*ph;
2997 x1 = data[curnum][0];
2998 x1 = (x1 - tl[0] + offset[0])*pw;
2999 y1 = data[curnum][1];
3000 y1 = (y1 - tl[1] + offset[1])*ph;
3002 tangent[0] = 2*(x - x1);
3003 tangent[1] = 2*(y - y1);
3005 span.conic_to(x1,y1,x,y);
3010 case Primitive::CONIC_TO_SMOOTH:
3012 x = data[curnum][0];
3013 x = (x - tl[0] + offset[0])*pw;
3014 y = data[curnum][1];
3015 y = (y - tl[1] + offset[1])*ph;
3017 x1 = span.cur_x + tangent[0]/2;
3018 y1 = span.cur_y + tangent[1]/2;
3020 tangent[0] = 2*(x - x1);
3021 tangent[1] = 2*(y - y1);
3023 span.conic_to(x1,y1,x,y);
3029 case Primitive::CUBIC_TO:
3031 x = data[curnum+2][0];
3032 x = (x - tl[0] + offset[0])*pw;
3033 y = data[curnum+2][1];
3034 y = (y - tl[1] + offset[1])*ph;
3036 x2 = data[curnum+1][0];
3037 x2 = (x2 - tl[0] + offset[0])*pw;
3038 y2 = data[curnum+1][1];
3039 y2 = (y2 - tl[1] + offset[1])*ph;
3041 x1 = data[curnum][0];
3042 x1 = (x1 - tl[0] + offset[0])*pw;
3043 y1 = data[curnum][1];
3044 y1 = (y1 - tl[1] + offset[1])*ph;
3046 tangent[0] = 2*(x - x2);
3047 tangent[1] = 2*(y - y2);
3049 span.cubic_to(x1,y1,x2,y2,x,y);
3055 case Primitive::CUBIC_TO_SMOOTH:
3057 x = data[curnum+1][0];
3058 x = (x - tl[0] + offset[0])*pw;
3059 y = data[curnum+1][1];
3060 y = (y - tl[1] + offset[1])*ph;
3062 x2 = data[curnum][0];
3063 x2 = (x2 - tl[0] + offset[0])*pw;
3064 y2 = data[curnum][1];
3065 y2 = (y2 - tl[1] + offset[1])*ph;
3067 x1 = span.cur_x + tangent[0]/3.0;
3068 y1 = span.cur_y + tangent[1]/3.0;
3070 tangent[0] = 2*(x - x2);
3071 tangent[1] = 2*(y - y2);
3073 span.cubic_to(x1,y1,x2,y2,x,y);
3082 //sort the bastards so we can render everything
3085 return render_polyspan(surface, span);
3089 Layer_Shape::get_bounding_rect()const
3092 return Rect::full_plane();
3094 if (edge_table->initaabb)
3095 return Rect::zero();
3097 Rect bounds(edge_table->aabb+offset);
3098 bounds.expand(max((bounds.get_min() - bounds.get_max()).mag()*0.01,