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)
1152 IMPORT_PLUS(color, { if (color.get_a() == 0) if (converted_blend_) {
1153 set_blend_method(Color::BLEND_ALPHA_OVER);
1154 color.set_a(1); } else transparent_color_ = true; });
1160 IMPORT(winding_style);
1162 return Layer_Composite::set_param(param,value);
1166 Layer_Shape::get_param(const String ¶m)const
1174 EXPORT(winding_style);
1179 return Layer_Composite::get_param(param);
1183 Layer_Shape::get_param_vocab()const
1185 Layer::Vocab ret(Layer_Composite::get_param_vocab());
1187 ret.push_back(ParamDesc("color")
1188 .set_local_name(_("Color"))
1189 .set_description(_("Layer_Shape Color"))
1191 ret.push_back(ParamDesc("offset")
1192 .set_local_name(_("Position"))
1194 ret.push_back(ParamDesc("invert")
1195 .set_local_name(_("Invert"))
1197 ret.push_back(ParamDesc("antialias")
1198 .set_local_name(_("Antialiasing"))
1200 ret.push_back(ParamDesc("feather")
1201 .set_local_name(_("Feather"))
1204 ret.push_back(ParamDesc("blurtype")
1205 .set_local_name(_("Type of Feather"))
1206 .set_description(_("Type of feathering to use"))
1208 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
1209 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
1210 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
1211 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
1212 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
1214 ret.push_back(ParamDesc("winding_style")
1215 .set_local_name(_("Winding Style"))
1216 .set_description(_("Winding style to use"))
1218 .add_enum_value(WINDING_NON_ZERO,"nonzero",_("Non Zero"))
1219 .add_enum_value(WINDING_EVEN_ODD,"evenodd",_("Even/Odd"))
1225 synfig::Layer::Handle
1226 Layer_Shape::hit_check(synfig::Context context, const synfig::Point &p)const
1228 Point pos(p-offset);
1230 int intercepts = edge_table->intersect(pos[0],pos[1]);
1232 // If we have an odd number of intercepts, we are inside.
1233 // If we have an even number of intercepts, we are outside.
1234 bool intersect = ((!!intercepts) ^ invert);
1236 if(get_amount() == 0 || get_blend_method() == Color::BLEND_ALPHA_OVER)
1243 synfig::Layer::Handle tmp;
1244 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(p)))
1246 if(Color::is_onto(get_blend_method()))
1248 //if there's something in the lower layer then we're set...
1249 if(!context.hit_check(p).empty())
1250 return const_cast<Layer_Shape*>(this);
1251 }else if(get_blend_method() == Color::BLEND_ALPHA_OVER)
1253 synfig::info("layer_shape::hit_check - we've got alphaover");
1254 //if there's something in the lower layer then we're set...
1255 if(color.get_a() < 0.1 && get_amount() > .9)
1257 synfig::info("layer_shape::hit_check - can see through us... so nothing");
1259 }else return context.hit_check(p);
1261 return const_cast<Layer_Shape*>(this);
1264 return context.hit_check(p);
1268 Layer_Shape::get_color(Context context, const Point &p)const
1273 pp = Blur(feather,feather,blurtype)(p);
1275 Point pos(pp-offset);
1277 int intercepts = edge_table->intersect(pos[0],pos[1]);
1279 // If we have an odd number of intercepts, we are inside.
1280 // If we have an even number of intercepts, we are outside.
1281 bool intersect = ((!!intercepts) ^ invert);
1284 return context.get_color(pp);
1286 //Ok, we're inside... bummmm ba bum buM...
1287 if(get_blend_method() == Color::BLEND_STRAIGHT && get_amount() == 1)
1290 return Color::blend(color,context.get_color(p),get_amount(),get_blend_method());
1293 //************** SCANLINE RENDERING *********************
1294 void Layer_Shape::PolySpan::line_to(Real x, Real y)
1296 Real n[4] = {0,0,0,0};
1297 bool afterx = false;
1299 const Real xin(x), yin(y);
1301 Real dx = x - cur_x;
1302 Real dy = y - cur_y;
1306 //outside y - ignore entirely
1307 if( (cur_y >= window.maxy && y >= window.maxy)
1308 ||(cur_y < window.miny && y < window.miny) )
1313 else //not degenerate - more complicated
1315 if(dy > 0) //be sure it's not tooooo small
1317 // cur_y ... window.miny ... window.maxy ... y
1319 //initial degenerate - initial clip
1320 if(cur_y < window.miny)
1322 //new clipped start point (must also move pen)
1323 n[2] = cur_x + (window.miny - cur_y) * dx / dy;
1326 cur_y = window.miny;
1327 move_pen((int)floor(cur_x),window.miny);
1330 //generate data for the ending clipped info
1333 //initial line to intersection (and degenerate)
1334 n[2] = x + (window.maxy - y) * dx / dy;
1343 //initial degenerate - initial clip
1344 if(cur_y > window.maxy)
1346 //new clipped start point (must also move pen)
1347 n[2] = cur_x + (window.maxy - cur_y) * dx / dy;
1350 cur_y = window.maxy;
1351 move_pen((int)floor(cur_x),window.maxy);
1354 //generate data for the ending clipped info
1357 //initial line to intersection (and degenerate)
1358 n[2] = x + (window.miny - y) * dx / dy;
1366 //all degenerate - but require bounded clipped values
1367 if( (cur_x >= window.maxx && x >= window.maxx)
1368 ||(cur_x < window.minx && x < window.minx) )
1370 //clip both vertices - but only needed in the x direction
1371 cur_x = max(cur_x, (Real)window.minx);
1372 cur_x = min(cur_x, (Real)window.maxx);
1374 //clip the dest values - y is already clipped
1375 x = max(x,(Real)window.minx);
1376 x = min(x,(Real)window.maxx);
1378 //must start at new point...
1379 move_pen((int)floor(cur_x),(int)floor(cur_y));
1381 draw_line(cur_x,cur_y,x,y);
1391 //initial degenerate - initial clip
1392 if(cur_x < window.minx)
1394 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1395 n[2] = cur_y + (window.minx - cur_x) * dy / dx;
1397 move_pen(window.minx,(int)floor(cur_y));
1398 draw_line(window.minx,cur_y,window.minx,n[2]);
1400 cur_x = window.minx;
1404 //generate data for the ending clipped info
1407 //initial line to intersection (and degenerate)
1408 n[2] = y + (window.maxx - x) * dy / dx;
1420 //initial degenerate - initial clip
1421 if(cur_x > window.maxx)
1423 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1424 n[2] = cur_y + (window.maxx - cur_x) * dy / dx;
1426 move_pen(window.maxx,(int)floor(cur_y));
1427 draw_line(window.maxx,cur_y,window.maxx,n[2]);
1429 cur_x = window.maxx;
1433 //generate data for the ending clipped info
1436 //initial line to intersection (and degenerate)
1437 n[2] = y + (window.minx - x) * dy / dx;
1449 move_pen((int)floor(cur_x),(int)floor(cur_y));
1450 //draw the relevant line (clipped)
1451 draw_line(cur_x,cur_y,x,y);
1455 draw_line(x,y,n[0],n[1]);
1462 } catch(...) { synfig::error("line_to: cur_x=%f, cur_y=%f, x=%f, y=%f", cur_x, cur_y, x, y); throw; }
1464 flags |= NotClosed|NotSorted;
1467 static inline bool clip_conic(const Point *const p, const ContextRect &r)
1469 const Real minx = min(min(p[0][0],p[1][0]),p[2][0]);
1470 const Real miny = min(min(p[0][1],p[1][1]),p[2][1]);
1471 const Real maxx = max(max(p[0][0],p[1][0]),p[2][0]);
1472 const Real maxy = max(max(p[0][1],p[1][1]),p[2][1]);
1474 return (minx > r.maxx) ||
1480 static inline bool clip_cubic(const Point *const p, const ContextRect &r)
1482 /*const Real minx = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
1483 const Real miny = min(min(p[0][1],p[1][1]),min(p[2][1],p[3][1]));
1484 const Real maxx = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][1]));
1485 const Real maxy = max(max(p[0][1],p[1][1]),max(p[2][1],p[3][1]));
1487 return (minx > r.maxx) ||
1492 return ((p[0][0] > r.maxx) && (p[1][0] > r.maxx) && (p[2][0] > r.maxx) && (p[3][0] > r.maxx)) ||
1493 ((p[0][0] < r.minx) && (p[1][0] < r.minx) && (p[2][0] < r.minx) && (p[3][0] < r.minx)) ||
1494 ((p[0][1] > r.maxy) && (p[1][1] > r.maxy) && (p[2][1] > r.maxy) && (p[3][1] > r.maxy)) ||
1495 ((p[0][1] < r.miny) && (p[1][1] < r.miny) && (p[2][1] < r.miny) && (p[3][1] < r.miny));
1498 static inline Real max_edges_cubic(const Point *const p)
1500 const Real x1 = p[1][0] - p[0][0];
1501 const Real y1 = p[1][1] - p[0][1];
1503 const Real x2 = p[2][0] - p[1][0];
1504 const Real y2 = p[2][1] - p[1][1];
1506 const Real x3 = p[3][0] - p[2][0];
1507 const Real y3 = p[3][1] - p[2][1];
1509 const Real d1 = x1*x1 + y1*y1;
1510 const Real d2 = x2*x2 + y2*y2;
1511 const Real d3 = x3*x3 + y3*y3;
1513 return max(max(d1,d2),d3);
1516 static inline Real max_edges_conic(const Point *const p)
1518 const Real x1 = p[1][0] - p[0][0];
1519 const Real y1 = p[1][1] - p[0][1];
1521 const Real x2 = p[2][0] - p[1][0];
1522 const Real y2 = p[2][1] - p[1][1];
1524 const Real d1 = x1*x1 + y1*y1;
1525 const Real d2 = x2*x2 + y2*y2;
1530 void Layer_Shape::PolySpan::conic_to(Real x1, Real y1, Real x, Real y)
1532 Point *current = arc;
1535 bool onsecond = false;
1537 arc[0] = Point(x,y);
1538 arc[1] = Point(x1,y1);
1539 arc[2] = Point(cur_x,cur_y);
1541 //just draw the line if it's outside
1542 if(clip_conic(arc,window))
1548 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1549 while(current >= arc)
1551 if(num >= MAX_SUBDIVISION_SIZE)
1553 warning("Curve subdivision somehow ran out of space while tessellating!");
1559 //if the curve is clipping then draw degenerate
1560 if(clip_conic(current,window))
1562 line_to(current[0][0],current[0][1]); //backwards so front is destination
1564 if(onsecond) level--;
1569 //if we are not at the level minimum
1570 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1572 Subd_Conic_Stack(current);
1573 current += 2; //cursor on second curve
1579 //split it again, if it's too big
1580 if(max_edges_conic(current) > 0.25) //distance of .5 (cover no more than half the pixel)
1582 Subd_Conic_Stack(current);
1583 current += 2; //cursor on second curve
1588 else //NOT TOO BIG? RENDER!!!
1590 //cur_x,cur_y = current[2], so we need to go 1,0
1591 line_to(current[1][0],current[1][1]);
1592 line_to(current[0][0],current[0][1]);
1595 if(onsecond) level--;
1602 void Layer_Shape::PolySpan::cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
1604 Point *current = arc;
1607 bool onsecond = false;
1609 arc[0] = Point(x,y);
1610 arc[1] = Point(x2,y2);
1611 arc[2] = Point(x1,y1);
1612 arc[3] = Point(cur_x,cur_y);
1614 //just draw the line if it's outside
1615 if(clip_cubic(arc,window))
1621 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1622 while(current >= arc) //once current goes below arc, there are no more curves left
1624 if(num >= MAX_SUBDIVISION_SIZE)
1626 warning("Curve subdivision somehow ran out of space while tessellating!");
1633 //if we are not at the level minimum
1634 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1636 Subd_Cubic_Stack(current);
1637 current += 3; //cursor on second curve
1643 //if the curve is clipping then draw degenerate
1644 if(clip_cubic(current,window))
1646 line_to(current[0][0],current[0][1]); //backwards so front is destination
1648 if(onsecond) level--;
1654 //split it again, if it's too big
1655 if(max_edges_cubic(current) > 0.25) //could use max_edges<3>
1657 Subd_Cubic_Stack(current);
1658 current += 3; //cursor on second curve
1663 else //NOT TOO BIG? RENDER!!!
1665 //cur_x,cur_y = current[3], so we need to go 2,1,0
1666 line_to(current[2][0],current[2][1]);
1667 line_to(current[1][0],current[1][1]);
1668 line_to(current[0][0],current[0][1]);
1671 if(onsecond) level--;
1678 //******************** LINE ALGORITHMS ****************************
1679 // THESE CALCULATE THE AREA AND THE COVER FOR THE MARKS, TO THEN SCAN CONVERT
1680 // - BROKEN UP INTO SCANLINES (draw_line - y intersections),
1681 // THEN THE COVER AND AREA PER TOUCHED PIXEL IS CALCULATED (draw_scanline - x intersections)
1682 void Layer_Shape::PolySpan::draw_scanline(int y, Real x1, Real fy1, Real x2, Real fy2)
1684 int ix1 = (int)floor(x1);
1685 int ix2 = (int)floor(x2);
1686 Real fx1 = x1 - ix1;
1687 Real fx2 = x2 - ix2;
1689 Real dx,dy,dydx,mult;
1694 //case horizontal line
1697 move_pen(ix2,y); //pen needs to be at the last coord
1701 //case all in same pixel
1702 if(ix1 == ix2) //impossible for degenerate case (covered by the previous cases)
1704 current.addcover(dy,(fx1 + fx2)*dy/2); //horizontal trapezoid area
1710 // ----> fx1...1 0...1 ... 0...1 0...fx2
1713 //set initial values
1714 //Iterate through the covered pixels
1715 mult = (1 - fx1)*dydx; //next y intersection diff value (at 1)
1718 current.addcover(mult,(1 + fx1)*mult/2); // fx1,fy1,1,fy@1 - starting trapezoidal area
1720 //move to the next pixel
1726 //set up for whole ones
1729 //trapezoid(0,y1,1,y1+dydx);
1730 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1732 //move to next pixel (+1)
1739 //final y-pos - last intersect pos
1741 current.addcover(mult,(0+fx2)*mult/2);
1744 // fx2...1 0...1 ... 0...1 0...fx1 <----
1745 //mult = (0 - fx1) * dy / dx;
1746 //neg sign sucked into dydx
1749 //set initial values
1750 //Iterate through the covered pixels
1751 mult = fx1*dydx; //next y intersection diff value
1754 current.addcover(mult,fx1*mult/2); // fx1,fy1,0,fy@0 - starting trapezoidal area
1756 //move to next pixel
1762 //set up for whole ones
1765 //trapezoid(0,y1,1,y1+dydx);
1766 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1768 //move to next pixel (-1)
1775 mult = fy2 - fy1; //final y-pos - last intersect pos
1777 current.addcover(mult,(fx2+1)*mult/2);
1781 void Layer_Shape::PolySpan::draw_line(Real x1, Real y1, Real x2, Real y2)
1783 int iy1 = (int)floor(y1);
1784 int iy2 = (int)floor(y2);
1785 Real fy1 = y1 - iy1;
1786 Real fy2 = y2 - iy2;
1788 assert(!isnan(fy1));
1789 assert(!isnan(fy2));
1791 Real dx,dy,dxdy,mult,x_from,x_to;
1793 const Real SLOPE_EPSILON = 1e-10;
1795 //case all one scanline
1798 draw_scanline(iy1,x1,y1,x2,y2);
1806 //case vertical line
1807 if(dx < SLOPE_EPSILON && dx > -SLOPE_EPSILON)
1809 //calc area and cover on vertical line
1812 // ----> fx1...1 0...1 ... 0...1 0...fx2
1815 int ix1 = (int)floor(x1);
1816 Real fx1 = x1 - ix1;
1821 current.addcover(sub,fx1*sub);
1826 //move pen to next pixel
1832 current.addcover(1,fx1);
1840 current.addcover(fy2,fy2*fx1);
1845 int ix1 = (int)floor(x1);
1846 Real fx1 = x1 - ix1;
1851 current.addcover(sub,fx1*sub);
1860 //accumulate in current pixel
1861 current.addcover(-1,-fx1);
1868 current.addcover(fy2-1,(fy2-1)*fx1);
1873 //case normal line - guaranteed dx != 0 && dy != 0
1875 //calculate the initial intersection with "next" scanline
1880 mult = (1 - fy1) * dxdy;
1882 //x intersect scanline
1884 draw_scanline(iy1,x1,fy1,x_from,1);
1889 move_pen((int)floor(x_from),iy1);
1893 //keep up on the x axis, and render the current scanline
1894 x_to = x_from + dxdy;
1895 draw_scanline(iy1,x_from,0,x_to,1);
1898 //move to next pixel
1900 move_pen((int)floor(x_from),iy1);
1903 //draw the last one, fractional
1904 draw_scanline(iy2,x_from,0,x2,fy2);
1912 //x intersect scanline
1914 draw_scanline(iy1,x1,fy1,x_from,0);
1919 move_pen((int)floor(x_from),iy1);
1923 x_to = x_from + dxdy;
1924 draw_scanline(iy1,x_from,1,x_to,0);
1928 move_pen((int)floor(x_from),iy1);
1930 //draw the last one, fractional
1931 draw_scanline(iy2,x_from,1,x2,fy2);
1935 //****** LAYER PEN OPERATIONS (move_to, line_to, etc.) ******
1936 void Layer_Shape::move_to(Real x, Real y)
1938 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1942 op.operation = Primitive::MOVE_TO;
1943 op.number = 1; //one point for now
1945 if(lastbyteop == Primitive::MOVE_TO)
1947 char *ptr = &bytestream[lastoppos];
1948 memcpy(ptr,&op,sizeof(op));
1949 memcpy(ptr+sizeof(op),&p,sizeof(p));
1951 else //make a new op
1953 lastbyteop = Primitive::MOVE_TO;
1954 lastoppos = bytestream.size();
1956 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
1957 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
1960 edge_table->move_to(x,y);
1963 void Layer_Shape::close()
1967 op.operation = Primitive::CLOSE;
1970 if(lastbyteop == Primitive::CLOSE)
1974 lastbyteop = Primitive::CLOSE;
1975 lastoppos = bytestream.size();
1977 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert header
1980 edge_table->close();
1981 //should not affect the bounding box since it would just be returning to old point...
1984 void Layer_Shape::endpath()
1988 op.operation = Primitive::END;
1991 if(lastbyteop == Primitive::END || lastbyteop == Primitive::NONE)
1995 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1));
1997 //should not affect the bounding box since it would just be returning to old point... if at all
2000 void Layer_Shape::line_to(Real x, Real y)
2005 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2009 op.operation = Primitive::LINE_TO;
2010 op.number = 1; //one point for now
2012 if(lastbyteop == Primitive::MOVE_TO || lastbyteop == Primitive::LINE_TO)
2014 //only need to insert the point
2015 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2017 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2018 prim->number++; //increment number of points in the list
2021 lastbyteop = Primitive::LINE_TO;
2022 lastoppos = bytestream.size();
2024 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2025 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2028 edge_table->line_to(x,y);
2031 void Layer_Shape::conic_to(Real x1, Real y1, Real x, Real y)
2033 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*2;
2038 op.operation = Primitive::CONIC_TO;
2039 op.number = 2; //2 points for now
2041 if(lastbyteop == Primitive::CONIC_TO)
2043 //only need to insert the new points
2044 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2045 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2047 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2048 prim->number += 2; //increment number of points in the list
2051 lastbyteop = Primitive::CONIC_TO;
2052 lastoppos = bytestream.size();
2054 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2055 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2056 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2059 edge_table->conic_to(x1,y1,x,y);
2062 void Layer_Shape::conic_to_smooth(Real x, Real y) //x1,y1 derived from current tangent
2064 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2068 op.operation = Primitive::CONIC_TO_SMOOTH;
2069 op.number = 1; //2 points for now
2071 if(lastbyteop == Primitive::CONIC_TO_SMOOTH)
2073 //only need to insert the new point
2074 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2076 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2077 prim->number += 1; //increment number of points in the list
2080 lastbyteop = Primitive::CONIC_TO_SMOOTH;
2081 lastoppos = bytestream.size();
2083 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2084 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2087 edge_table->conic_to_smooth(x,y);
2090 void Layer_Shape::curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
2092 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2098 op.operation = Primitive::CUBIC_TO;
2099 op.number = 3; //3 points for now
2101 if(lastbyteop == Primitive::CUBIC_TO)
2103 //only need to insert the new points
2104 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2105 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2106 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2108 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2109 prim->number += 3; //increment number of points in the list
2112 lastbyteop = Primitive::CUBIC_TO;
2113 lastoppos = bytestream.size();
2115 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2116 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2117 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2118 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2121 edge_table->curve_to(x1,y1,x2,y2,x,y);
2124 void Layer_Shape::curve_to_smooth(Real x2, Real y2, Real x, Real y) //x1,y1 derived from current tangent
2126 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2131 op.operation = Primitive::CUBIC_TO_SMOOTH;
2132 op.number = 2; //3 points for now
2134 if(lastbyteop == Primitive::CUBIC_TO_SMOOTH)
2136 //only need to insert the new points
2137 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2138 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2140 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2141 prim->number += 2; //increment number of points in the list
2144 lastbyteop = Primitive::CUBIC_TO_SMOOTH;
2145 lastoppos = bytestream.size();
2147 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2148 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2149 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2153 // ACCELERATED RENDER FUNCTION - TRANSLATE BYTE CODE INTO FUNCTION CALLS
2155 bool Layer_Shape::render_polyspan(Surface *surface, PolySpan &polyspan,
2156 Color::BlendMethod got_blend_method, Color::value_type got_amount) const
2158 Surface::alpha_pen p(surface->begin(),got_amount,_BlendFunc(got_blend_method));
2159 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2160 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2162 Real cover,area,alpha;
2169 if(cur_mark == end_mark)
2174 p.move_to(polyspan.window.minx,polyspan.window.miny);
2175 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2180 //fill initial rect / line
2183 //fill all the area above the first vertex
2184 p.move_to(polyspan.window.minx,polyspan.window.miny);
2185 y = polyspan.window.miny;
2186 int l = polyspan.window.maxx - polyspan.window.minx;
2188 p.put_block(cur_mark->y - polyspan.window.miny,l);
2190 //fill the area to the left of the first vertex on that line
2191 l = cur_mark->x - polyspan.window.minx;
2192 p.move_to(polyspan.window.minx,cur_mark->y);
2193 if(l) p.put_hline(l);
2203 area = cur_mark->area;
2204 cover += cur_mark->cover;
2206 //accumulate for the current pixel
2207 while(++cur_mark != polyspan.covers.end())
2209 if(y != cur_mark->y || x != cur_mark->x)
2212 area += cur_mark->area;
2213 cover += cur_mark->cover;
2216 //draw pixel - based on covered area
2217 if(area) //if we're ok, draw the current pixel
2219 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2220 if(invert) alpha = 1 - alpha;
2224 if(alpha >= .5) p.put_value();
2226 else if(alpha) p.put_value_alpha(alpha);
2232 //if we're done, don't use iterator and exit
2233 if(cur_mark == end_mark) break;
2235 //if there is no more live pixels on this line, goto next
2236 if(y != cur_mark->y)
2240 //fill the area at the end of the line
2241 p.put_hline(polyspan.window.maxx - x);
2243 //fill area at the beginning of the next line
2244 p.move_to(polyspan.window.minx,cur_mark->y);
2245 p.put_hline(cur_mark->x - polyspan.window.minx);
2253 //draw span to next pixel - based on total amount of pixel cover
2256 alpha = polyspan.ExtractAlpha(cover, winding_style);
2257 if(invert) alpha = 1 - alpha;
2261 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2263 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2267 //fill the after stuff
2270 //fill the area at the end of the line
2271 p.put_hline(polyspan.window.maxx - x);
2273 //fill area at the beginning of the next line
2274 p.move_to(polyspan.window.minx,y+1);
2275 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2281 bool Layer_Shape::render_polyspan(etl::surface<float> *surface, PolySpan &polyspan) const
2283 etl::surface<float>::pen p(surface->begin());
2284 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2285 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2287 Real cover,area,alpha;
2293 //the pen always writes 1 (unless told to do otherwise)
2296 if(cur_mark == end_mark)
2301 p.move_to(polyspan.window.minx,polyspan.window.miny);
2302 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2307 //fill initial rect / line
2310 //fill all the area above the first vertex
2311 p.move_to(polyspan.window.minx,polyspan.window.miny);
2312 y = polyspan.window.miny;
2313 int l = polyspan.window.maxx - polyspan.window.minx;
2315 p.put_block(cur_mark->y - polyspan.window.miny,l);
2317 //fill the area to the left of the first vertex on that line
2318 l = cur_mark->x - polyspan.window.minx;
2319 p.move_to(polyspan.window.minx,cur_mark->y);
2320 if(l) p.put_hline(l);
2329 area = cur_mark->area;
2330 cover += cur_mark->cover;
2332 //accumulate for the current pixel
2333 while(++cur_mark != polyspan.covers.end())
2335 if(y != cur_mark->y || x != cur_mark->x)
2338 area += cur_mark->area;
2339 cover += cur_mark->cover;
2342 //draw pixel - based on covered area
2343 if(area) //if we're ok, draw the current pixel
2345 alpha = 1 - polyspan.ExtractAlpha(cover - area, winding_style);
2348 if(alpha >= .5) p.put_value();
2350 else if(alpha) p.put_value(alpha);
2356 //if we're done, don't use iterator and exit
2357 if(cur_mark == end_mark) break;
2359 //if there is no more live pixels on this line, goto next
2360 if(y != cur_mark->y)
2362 //fill the area at the end of the line
2363 p.put_hline(polyspan.window.maxx - x);
2365 //fill area at the beginning of the next line
2366 p.move_to(polyspan.window.minx,cur_mark->y);
2367 p.put_hline(cur_mark->x - polyspan.window.minx);
2374 //draw span to next pixel - based on total amount of pixel cover
2377 alpha = 1 - polyspan.ExtractAlpha(cover, winding_style);
2380 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2382 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2386 //fill the area at the end of the line
2387 p.put_hline(polyspan.window.maxx - x);
2389 //fill area at the beginning of the next line
2390 p.move_to(polyspan.window.minx,y+1);
2391 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2401 area = cur_mark->area;
2402 cover += cur_mark->cover;
2404 //accumulate for the current pixel
2405 while(++cur_mark != polyspan.covers.end())
2407 if(y != cur_mark->y || x != cur_mark->x)
2410 area += cur_mark->area;
2411 cover += cur_mark->cover;
2414 //draw pixel - based on covered area
2415 if(area) //if we're ok, draw the current pixel
2417 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2420 if(alpha >= .5) p.put_value();
2422 else if(alpha) p.put_value(alpha);
2428 //if we're done, don't use iterator and exit
2429 if(cur_mark == end_mark) break;
2431 //if there is no more live pixels on this line, goto next
2432 if(y != cur_mark->y)
2439 //draw span to next pixel - based on total amount of pixel cover
2442 alpha = polyspan.ExtractAlpha(cover, winding_style);
2445 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2447 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2456 Layer_Shape::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
2458 const unsigned int w = renddesc.get_w();
2459 const unsigned int h = renddesc.get_h();
2461 const Real pw = abs(renddesc.get_pw());
2462 const Real ph = abs(renddesc.get_ph());
2464 //const Real OFFSET_EPSILON = 1e-8;
2465 SuperCallback stageone(cb,1,10000,15001+renddesc.get_h());
2466 SuperCallback stagetwo(cb,10000,10001+renddesc.get_h(),15001+renddesc.get_h());
2467 SuperCallback stagethree(cb,10001+renddesc.get_h(),15001+renddesc.get_h(),15001+renddesc.get_h());
2469 // Render what is behind us
2471 //clip if it satisfies the invert solid thing
2472 if(is_solid_color() && invert)
2474 Rect aabb = edge_table->aabb;
2475 Point tl = renddesc.get_tl() - offset;
2477 Real pw = renddesc.get_pw(),
2478 ph = renddesc.get_ph();
2482 Real pixelfeatherx = quality == 10 ? 0 : abs(feather/pw),
2483 pixelfeathery = quality == 10 ? 0 : abs(feather/ph);
2485 nrect.set_point((aabb.minx - tl[0])/pw,(aabb.miny - tl[1])/ph);
2486 nrect.expand((aabb.maxx - tl[0])/pw,(aabb.maxy - tl[1])/ph);
2488 RendDesc optdesc(renddesc);
2490 //make sure to expand so we gain subpixels rather than lose them
2491 nrect.minx = floor(nrect.minx-pixelfeatherx); nrect.miny = floor(nrect.miny-pixelfeathery);
2492 nrect.maxx = ceil(nrect.maxx+pixelfeatherx); nrect.maxy = ceil(nrect.maxy+pixelfeathery);
2494 //make sure the subwindow is clipped with our tile window (minimize useless drawing)
2495 set_intersect(nrect,nrect,Rect(0,0,renddesc.get_w(),renddesc.get_h()));
2497 //must resize the surface first
2498 surface->set_wh(renddesc.get_w(),renddesc.get_h());
2501 //only render anything if it's visible from our current tile
2504 //set the subwindow to the viewable pixels and render it to the subsurface
2505 optdesc.set_subwindow((int)nrect.minx, (int)nrect.miny,
2506 (int)(nrect.maxx - nrect.minx), (int)(nrect.maxy - nrect.miny));
2508 Surface optimizedbacksurf;
2509 if(!context.accelerated_render(&optimizedbacksurf,quality,optdesc,&stageone))
2512 //blit that onto the original surface so we can pretend that nothing ever happened
2513 Surface::pen p = surface->get_pen((int)nrect.minx,(int)nrect.miny);
2514 optimizedbacksurf.blit_to(p);
2518 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
2522 if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
2524 if(feather && quality != 10)
2526 //we have to blur rather than be crappy
2528 //so make a separate surface
2529 RendDesc workdesc(renddesc);
2531 etl::surface<float> shapesurface;
2533 //the expanded size = 1/2 the size in each direction rounded up
2534 int halfsizex = (int) (abs(feather*.5/pw) + 3),
2535 halfsizey = (int) (abs(feather*.5/ph) + 3);
2537 //expand by 1/2 size in each direction on either side
2544 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2547 case Blur::FASTGAUSSIAN:
2554 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2557 case Blur::GAUSSIAN:
2559 #define GAUSSIAN_ADJUSTMENT (0.05)
2560 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
2561 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
2566 halfsizex = (int)(abs(pw)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2567 halfsizey = (int)(abs(ph)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2569 halfsizex = (halfsizex + 1)/2;
2570 halfsizey = (halfsizey + 1)/2;
2571 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
2577 shapesurface.set_wh(workdesc.get_w(),workdesc.get_h());
2578 shapesurface.clear();
2581 if(!render_shape(&shapesurface,quality,workdesc,&stagetwo))return false;
2584 Blur(feather,feather,blurtype,&stagethree)(shapesurface,workdesc.get_br()-workdesc.get_tl(),shapesurface);
2586 //blend with stuff below it...
2587 unsigned int u = halfsizex, v = halfsizey, x = 0, y = 0;
2588 for(y = 0; y < h; y++,v++)
2591 for(x = 0; x < w; x++,u++)
2593 float a = shapesurface[v][u];
2596 //a = floor(a*255+0.5f)/255;
2597 (*surface)[y][x]=Color::blend(color,(*surface)[y][x],a*get_amount(),get_blend_method());
2599 //else (*surface)[y][x] = worksurface[v][u];
2604 if(cb && !cb->amount_complete(100,100))
2606 synfig::warning("Layer_Shape: could not set amount complete");
2613 //might take out to reduce code size
2614 return render_shape(surface,true,quality,renddesc,&stagetwo);
2620 Layer_Shape::render_shape(Surface *surface,bool useblend,int /*quality*/,
2621 const RendDesc &renddesc, ProgressCallback *cb)const
2625 SuperCallback progress(cb,0,renddesc.get_h(),renddesc.get_h());
2627 // If our amount is set to zero, no need to render anything
2631 //test new polygon renderer
2633 // Width and Height of a pixel
2634 const int w = renddesc.get_w();
2635 const int h = renddesc.get_h();
2636 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2637 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2639 const Point tl = renddesc.get_tl();
2641 Vector tangent (0,0);
2645 // if the pixels are zero sized then we're too zoomed out to see anything
2646 if (pw == 0 || ph == 0)
2649 //optimization for tessellating only inside tiles
2650 span.window.minx = 0;
2651 span.window.miny = 0;
2652 span.window.maxx = w;
2653 span.window.maxy = h;
2655 //pointers for processing the bytestream
2656 const char *current = &bytestream[0];
2657 const char *end = &bytestream[bytestream.size()];
2659 int operation = Primitive::NONE;
2666 Real x,y,x1,y1,x2,y2;
2669 while(current < end)
2675 //get the op code safely
2676 curprim = (Primitive *)current;
2678 //advance past indices
2679 current += sizeof(Primitive);
2682 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2686 //get the relevant data
2687 operation = curprim->operation;
2688 number = curprim->number;
2690 if(operation == Primitive::END)
2693 if(operation == Primitive::CLOSE)
2695 if(span.notclosed())
2697 tangent[0] = span.close_x - span.cur_x;
2698 tangent[1] = span.close_y - span.cur_y;
2704 data = (Point*)current;
2705 current += sizeof(Point)*number;
2707 //check data positioning
2710 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2714 } catch(...) { synfig::error("Layer_Shape::render_shape()1: Caught an exception after %d loops, rethrowing...", tmp); throw; }
2716 //transfer all the data - RLE optimized
2717 for(curnum=0; curnum < number;)
2721 case Primitive::MOVE_TO:
2723 x = data[curnum][0];
2724 x = (x - tl[0] + offset[0])*pw;
2725 y = data[curnum][1];
2726 y = (y - tl[1] + offset[1])*ph;
2737 tangent[0] = x - span.cur_x;
2738 tangent[1] = y - span.cur_y;
2743 curnum++; //only advance one point
2748 case Primitive::LINE_TO:
2750 x = data[curnum][0];
2751 x = (x - tl[0] + offset[0])*pw;
2752 y = data[curnum][1];
2753 y = (y - tl[1] + offset[1])*ph;
2755 tangent[0] = x - span.cur_x;
2756 tangent[1] = y - span.cur_y;
2763 case Primitive::CONIC_TO:
2765 x = data[curnum+1][0];
2766 x = (x - tl[0] + offset[0])*pw;
2767 y = data[curnum+1][1];
2768 y = (y - tl[1] + offset[1])*ph;
2770 x1 = data[curnum][0];
2771 x1 = (x1 - tl[0] + offset[0])*pw;
2772 y1 = data[curnum][1];
2773 y1 = (y1 - tl[1] + offset[1])*ph;
2775 tangent[0] = 2*(x - x1);
2776 tangent[1] = 2*(y - y1);
2778 span.conic_to(x1,y1,x,y);
2783 case Primitive::CONIC_TO_SMOOTH:
2785 x = data[curnum][0];
2786 x = (x - tl[0] + offset[0])*pw;
2787 y = data[curnum][1];
2788 y = (y - tl[1] + offset[1])*ph;
2790 x1 = span.cur_x + tangent[0]/2;
2791 y1 = span.cur_y + tangent[1]/2;
2793 tangent[0] = 2*(x - x1);
2794 tangent[1] = 2*(y - y1);
2796 span.conic_to(x1,y1,x,y);
2802 case Primitive::CUBIC_TO:
2804 x = data[curnum+2][0];
2805 x = (x - tl[0] + offset[0])*pw;
2806 y = data[curnum+2][1];
2807 y = (y - tl[1] + offset[1])*ph;
2809 x2 = data[curnum+1][0];
2810 x2 = (x2 - tl[0] + offset[0])*pw;
2811 y2 = data[curnum+1][1];
2812 y2 = (y2 - tl[1] + offset[1])*ph;
2814 x1 = data[curnum][0];
2815 x1 = (x1 - tl[0] + offset[0])*pw;
2816 y1 = data[curnum][1];
2817 y1 = (y1 - tl[1] + offset[1])*ph;
2819 tangent[0] = 2*(x - x2);
2820 tangent[1] = 2*(y - y2);
2822 span.cubic_to(x1,y1,x2,y2,x,y);
2828 case Primitive::CUBIC_TO_SMOOTH:
2830 x = data[curnum+1][0];
2831 x = (x - tl[0] + offset[0])*pw;
2832 y = data[curnum+1][1];
2833 y = (y - tl[1] + offset[1])*ph;
2835 x2 = data[curnum][0];
2836 x2 = (x2 - tl[0] + offset[0])*pw;
2837 y2 = data[curnum][1];
2838 y2 = (y2 - tl[1] + offset[1])*ph;
2840 x1 = span.cur_x + tangent[0]/3.0;
2841 y1 = span.cur_y + tangent[1]/3.0;
2843 tangent[0] = 2*(x - x2);
2844 tangent[1] = 2*(y - y2);
2846 span.cubic_to(x1,y1,x2,y2,x,y);
2855 //sort the bastards so we can render everything
2858 return render_polyspan(surface, span,
2859 useblend?get_blend_method():Color::BLEND_STRAIGHT,
2860 useblend?get_amount():1.0);
2864 Layer_Shape::render_shape(etl::surface<float> *surface,int /*quality*/,
2865 const RendDesc &renddesc, ProgressCallback */*cb*/)const
2867 // If our amount is set to zero, no need to render anything
2871 //test new polygon renderer
2873 // Width and Height of a pixel
2874 const int w = renddesc.get_w();
2875 const int h = renddesc.get_h();
2876 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2877 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2879 const Point tl = renddesc.get_tl();
2881 Vector tangent (0,0);
2885 //optimization for tessellating only inside tiles
2886 span.window.minx = 0;
2887 span.window.miny = 0;
2888 span.window.maxx = w;
2889 span.window.maxy = h;
2891 //pointers for processing the bytestream
2892 const char *current = &bytestream[0];
2893 const char *end = &bytestream[bytestream.size()];
2895 int operation = Primitive::NONE;
2902 Real x,y,x1,y1,x2,y2;
2904 while(current < end)
2906 //get the op code safely
2907 curprim = (Primitive *)current;
2909 //advance past indices
2910 current += sizeof(Primitive);
2913 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2917 //get the relevant data
2918 operation = curprim->operation;
2919 number = curprim->number;
2921 if(operation == Primitive::END)
2924 if(operation == Primitive::CLOSE)
2926 if(span.notclosed())
2928 tangent[0] = span.close_x - span.cur_x;
2929 tangent[1] = span.close_y - span.cur_y;
2935 data = (Point*)current;
2936 current += sizeof(Point)*number;
2938 //check data positioning
2941 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2945 //transfer all the data
2946 for(curnum=0; curnum < number;)
2950 case Primitive::MOVE_TO:
2952 x = data[curnum][0];
2953 x = (x - tl[0] + offset[0])*pw;
2954 y = data[curnum][1];
2955 y = (y - tl[1] + offset[1])*ph;
2966 tangent[0] = x - span.cur_x;
2967 tangent[1] = y - span.cur_y;
2972 curnum++; //only advance one point
2977 case Primitive::LINE_TO:
2979 x = data[curnum][0];
2980 x = (x - tl[0] + offset[0])*pw;
2981 y = data[curnum][1];
2982 y = (y - tl[1] + offset[1])*ph;
2984 tangent[0] = x - span.cur_x;
2985 tangent[1] = y - span.cur_y;
2992 case Primitive::CONIC_TO:
2994 x = data[curnum+1][0];
2995 x = (x - tl[0] + offset[0])*pw;
2996 y = data[curnum+1][1];
2997 y = (y - tl[1] + offset[1])*ph;
2999 x1 = data[curnum][0];
3000 x1 = (x1 - tl[0] + offset[0])*pw;
3001 y1 = data[curnum][1];
3002 y1 = (y1 - tl[1] + offset[1])*ph;
3004 tangent[0] = 2*(x - x1);
3005 tangent[1] = 2*(y - y1);
3007 span.conic_to(x1,y1,x,y);
3012 case Primitive::CONIC_TO_SMOOTH:
3014 x = data[curnum][0];
3015 x = (x - tl[0] + offset[0])*pw;
3016 y = data[curnum][1];
3017 y = (y - tl[1] + offset[1])*ph;
3019 x1 = span.cur_x + tangent[0]/2;
3020 y1 = span.cur_y + tangent[1]/2;
3022 tangent[0] = 2*(x - x1);
3023 tangent[1] = 2*(y - y1);
3025 span.conic_to(x1,y1,x,y);
3031 case Primitive::CUBIC_TO:
3033 x = data[curnum+2][0];
3034 x = (x - tl[0] + offset[0])*pw;
3035 y = data[curnum+2][1];
3036 y = (y - tl[1] + offset[1])*ph;
3038 x2 = data[curnum+1][0];
3039 x2 = (x2 - tl[0] + offset[0])*pw;
3040 y2 = data[curnum+1][1];
3041 y2 = (y2 - tl[1] + offset[1])*ph;
3043 x1 = data[curnum][0];
3044 x1 = (x1 - tl[0] + offset[0])*pw;
3045 y1 = data[curnum][1];
3046 y1 = (y1 - tl[1] + offset[1])*ph;
3048 tangent[0] = 2*(x - x2);
3049 tangent[1] = 2*(y - y2);
3051 span.cubic_to(x1,y1,x2,y2,x,y);
3057 case Primitive::CUBIC_TO_SMOOTH:
3059 x = data[curnum+1][0];
3060 x = (x - tl[0] + offset[0])*pw;
3061 y = data[curnum+1][1];
3062 y = (y - tl[1] + offset[1])*ph;
3064 x2 = data[curnum][0];
3065 x2 = (x2 - tl[0] + offset[0])*pw;
3066 y2 = data[curnum][1];
3067 y2 = (y2 - tl[1] + offset[1])*ph;
3069 x1 = span.cur_x + tangent[0]/3.0;
3070 y1 = span.cur_y + tangent[1]/3.0;
3072 tangent[0] = 2*(x - x2);
3073 tangent[1] = 2*(y - y2);
3075 span.cubic_to(x1,y1,x2,y2,x,y);
3084 //sort the bastards so we can render everything
3087 return render_polyspan(surface, span);
3091 Layer_Shape::get_bounding_rect()const
3094 return Rect::full_plane();
3096 if (edge_table->initaabb)
3097 return Rect::zero();
3099 Rect bounds(edge_table->aabb+offset);
3100 bounds.expand(max((bounds.get_min() - bounds.get_max()).mag()*0.01,