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
257 //for the loop to break there must have been a slope (straight line would do nothing)
258 //vector<Point>::const_iterator p1 = p-1;
259 Real dy = p[-1][1] - p[0][1];
260 Real dx = p[-1][0] - p[0][0];
264 Real xi = p[0][0] + (y - p[0][1]) * dx / dy;
265 return (x > xi)*ydir;
271 Rect aabb; //not necessarily as effective - can only reject values
272 vector<Point> pointlist; //run length - p0, p1, p2, p3 = p10, p11, p12, p13 = p20 ...
273 vector<char> degrees;
275 CurveArray(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
277 aabb.set(x0,y0,x1,y1);
280 void reset(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
282 aabb.set(x0,y0,x1,y1);
289 return degrees.size();
294 reset(m[0],m[0],m[1],m[1]);
295 pointlist.push_back(m);
298 void AddCubic(Point p1, Point p2, Point dest)
300 aabb.expand(p1[0],p1[1]);
301 aabb.expand(p2[0],p2[1]);
302 aabb.expand(dest[0],dest[1]);
304 pointlist.push_back(p1);
305 pointlist.push_back(p2);
306 pointlist.push_back(dest);
308 degrees.push_back(3);
311 void AddConic(Point p1, Point dest)
313 aabb.expand(p1[0],p1[1]);
314 aabb.expand(dest[0],dest[1]);
316 pointlist.push_back(p1);
317 pointlist.push_back(dest);
319 degrees.push_back(2);
322 static int intersect_conic(Real x, Real y, Point *p, int /*level*/ = 0)
324 Real ymin,ymax,xmin,xmax;
327 //sort the overall curve ys - degenerate detection
328 ymin = min(p[0][1],p[2][1]);
329 ymax = max(p[0][1],p[2][1]);
331 xmin = min(min(p[0][0],p[1][0]),p[2][0]);
332 xmax = max(max(p[0][0],p[1][0]),p[2][0]);
334 //to the left, to the right and out of range y, or completely out of range y
335 if( x < xmin ) return 0;
336 if( x > xmax && (y > ymax || y < ymin) ) return 0;
337 if( (y > ymax && y > p[1][1]) || (y < ymin && y < p[1][1]) ) return 0;
339 //degenerate line max
340 if(ymin == ymax && ymax == p[1][1])
343 //degenerate accept - to the right and crossing the base line
346 return (y <= ymax && y >= ymin);
349 //solve for curve = y
352 //0 roots - 0 intersection
353 //1 root - get x, and figure out x
354 //2 roots (non-double root) - get 2 xs, and count xs to the left
356 //for conic we can assume 1 intersection for monotonic curve
357 Real a = p[2][1] - 2*p[1][1] + p[0][1],
358 b = 2*p[1][1] - 2*p[0][1],
361 Real t1 = -1, t2 = -1;
366 if(b == 0) return 0; //may not need this check
368 t1 = - c / b; //bt + c = 0 solved
371 //2 degree polynomial
372 Real b2_4ac = b*b - 4*a*c;
374 //if there are double/no roots - no intersections (in real #s that is)
380 b2_4ac = sqrt(b2_4ac);
382 t1 = (-b - b2_4ac) / 2*a,
383 t2 = (-b + b2_4ac) / 2*a;
386 //calculate number of intersections
387 if(t1 >= 0 && t1 <= 1)
390 const Real invt = 1 - t;
392 //find x val and it counts if it's to the left of the point
393 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
394 const Real dy_t = 2*a*t + b;
398 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
402 if(t2 >= 0 && t2 <= 1)
405 const Real invt = 1 - t;
407 //find x val and it counts if it's to the left of the point
408 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
409 const Real dy_t = 2*a*t + b;
413 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
420 static int quadratic_eqn(Real a, Real b, Real c, Real *t0, Real *t1)
422 const Real b2_4ac = b*b - 4*a*c;
424 //degenerate reject (can't take sqrt)
430 const Real sqrtb2_4ac = sqrt(b2_4ac);
431 const Real signb = b < 0 ? -1 : 1;
432 const Real q = - 0.5 * (b + signb * sqrtb2_4ac);
437 return sqrtb2_4ac == 0 ? 1 : 2;
440 //Newton-Raphson root polishing (we don't care about bounds, assumes very near the desired root)
441 static Real polish_cubicroot(Real a, Real b, Real c, Real d, Real t, Real *dpdt)
443 const Real cn[4] = {a,b,c,d};
444 Real p,dp,newt,oldpmag=FLT_MAX;
446 //eval cubic eqn and its derivative
452 for(int i = 2; i < 4; i++)
460 synfig::warning("polish_cubicroot: Derivative should not vanish!!!");
466 if(newt == t || fabs(p) >= oldpmag)
477 static int intersect_cubic(Real x, Real y, Point *p, int /*level*/ = 0)
479 const Real INVALIDROOT = -FLT_MAX;
480 Real ymin,ymax,xmin,xmax;
481 Real ymin2,ymax2,ymintot,ymaxtot;
484 //sort the overall curve ys and xs - degenerate detection
486 //open span for the two end points
487 ymin = min(p[0][1],p[3][1]);
488 ymax = max(p[0][1],p[3][1]);
491 ymin2 = min(p[1][1],p[2][1]);
492 ymax2 = max(p[1][1],p[2][1]);
494 ymintot = min(ymin,ymin2);
495 ymaxtot = max(ymax,ymax2);
497 //the entire curve control polygon is in this x range
498 xmin = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
499 xmax = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][0]));
501 //outside all y boundaries (no intersect)
502 if( (y > ymaxtot) || (y < ymintot) ) return 0;
504 //left of curve (no intersect)
505 if(x < xmin) return 0;
507 //right of curve (and outside base range)
510 if( (y > ymax) || (y < ymin) ) return 0;
512 //degenerate accept - to the right and inside the [ymin,ymax] range (already rejected if out of range)
513 const Real n = p[3][1] - p[0][1];
515 //extract the sign from the value (we need valid data)
516 return n < 0 ? -1 : 1;
519 //degenerate horizontal line max -- doesn't happen enough to check for
520 if( ymintot == ymaxtot ) return 0;
523 // can have 0,1,2, or 3 real roots
524 // if any of them are double then reject the two...
526 // y-coefficients for f_y(t) - y = 0
527 Real a = p[3][1] - 3*p[2][1] + 3*p[1][1] - p[0][1],
528 b = 3*p[2][1] - 6*p[1][1] + 3*p[0][1],
529 c = 3*p[1][1] - 3*p[0][1],
532 Real ax = p[3][0] - 3*p[2][0] + 3*p[1][0] - p[0][0],
533 bx = 3*p[2][0] - 6*p[1][0] + 3*p[0][0],
534 cx = 3*p[1][0] - 3*p[0][0],
537 Real t1 = INVALIDROOT, t2 = INVALIDROOT, t3 = INVALIDROOT, t, dydt;
547 t1 = - d / c; //equation devolved into: ct + d = 0 - solve...
550 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
551 if(quadratic_eqn(a,b,c,&t1,&t2) != 2) return 0;
557 //algorithm courtesy of Numerical Recipes in C (algorithm copied from pg. 184/185)
562 //if cn is 0 (or really really close), then we can simplify this...
567 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
568 if(quadratic_eqn(a,b,c,&t1,&t2) != 2)
570 t1 = t2 = INVALIDROOT;
575 //otherwise run the normal cubic root equation
576 Real Q = (an*an - 3.0*bn) / 9.0;
577 Real R = ((2.0*an*an - 9.0*bn)*an + 27.0*cn)/54.0;
581 Real theta = acos(R / sqrt(Q*Q*Q));
583 t1 = -2.0*sqrt(Q)*cos(theta/3) - an/3.0;
584 t2 = -2.0*sqrt(Q)*cos((theta+2*PI)/3.0) - an/3.0;
585 t3 = -2.0*sqrt(Q)*cos((theta-2*PI)/3.0) - an/3.0;
587 //don't need to reorder,l just need to eliminate double/triple roots
588 //if(t3 == t2 && t1 == t2) t2 = t3 = INVALIDROOT;
589 if(t3 == t2) t2 = t3 = INVALIDROOT;
590 if(t1 == t2) t1 = t2 = INVALIDROOT;
591 if(t1 == t3) t1 = t3 = INVALIDROOT;
594 Real signR = R < 0 ? -1 : 1;
595 Real A = - signR * pow(signR*R + sqrt(R*R - Q*Q*Q),1/3.0);
601 //single real root in this case
602 t1 = (A + B) - an/3.0;
607 //if(t1 != INVALIDROOT)
609 t = t1;//polish_cubicroot(a,b,c,d,t1,&dydt);
612 //const Real invt = 1 - t;
614 //find x val and it counts if it's to the left of the point
615 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
616 dydt = (3*a*t + 2*b)*t + c;
620 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
625 //if(t2 != INVALIDROOT)
627 t = t2;//polish_cubicroot(a,b,c,d,t2,&dydt);
630 //const Real invt = 1 - t;
632 //find x val and it counts if it's to the left of the point
633 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
634 dydt = (3*a*t + 2*b)*t + c;
638 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
643 //if(t3 != INVALIDROOT)
645 t = t3;//polish_cubicroot(a,b,c,d,t3,&dydt);
648 //const Real invt = 1 - t;
650 //find x val and it counts if it's to the left of the point
651 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
652 dydt = (3*a*t + 2*b)*t + c;
656 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
664 int intersect(Real x,Real y, Point *table) const
666 if((y < aabb.miny) || (y > aabb.maxy) || (x < aabb.minx)) return 0;
668 int i, curdeg, intersects = 0;
669 const int numcurves = degrees.size();
671 vector<Point>::const_iterator p = pointlist.begin();
673 for(i=0; i < numcurves; i++)
683 table[2] = *p; //we want to include the last point for the next curve
685 intersects += intersect_conic(x,y,table);
695 table[3] = *p; //we want to include the last point for the next curve
697 intersects += intersect_cubic(x,y,table);
704 warning("Invalid degree (%d) inserted into the list (index: %d)\n", curdeg, i);
714 struct Layer_Shape::Intersector
718 //! true iff aabb hasn't been initialized yet
723 enum IntersectorFlags
736 Real close_x,close_y;
738 vector<MonoSegment> segs; //monotonically increasing
739 vector<CurveArray> curves; //big array of consecutive curves
751 return (flags & NotClosed) || (cur_x != close_x) || (cur_y != close_y);
754 void move_to(Real x, Real y)
761 tangent[0] = tangent[1] = 0;
767 }else aabb.expand(x,y);
772 void line_to(Real x, Real y)
774 int dir = (y > cur_y)*1 + (-1)*(y < cur_y);
776 //check for context (if not line start a new segment)
777 //if we're not in line mode (covers 0 set case), or if directions are different (not valid for 0 direction)
778 if(prim != TYPE_LINE || (dir && segs.back().ydir != dir))
780 MonoSegment seg(dir,x,x,y,y);
782 seg.aabb.expand(cur_x,cur_y);
783 seg.pointlist.push_back(Point(cur_x,cur_y));
784 seg.pointlist.push_back(Point(x,y));
787 //add to the last segment, because it works
790 segs.back().pointlist.push_back(Point(x,y));
791 segs.back().aabb.expand(x,y);
798 aabb.expand(x,y); //expand the entire thing's bounding box
800 tangent[0] = x - cur_x;
801 tangent[1] = x - cur_y;
807 void conic_to_smooth(Real x, Real y)
809 const Real x1 = tangent[0]/2.0 + cur_x;
810 const Real y1 = tangent[1]/2.0 + cur_y;
815 void conic_to(Real x1, Real y1, Real x, Real y)
817 //if we're not already a curve start one
818 if(prim != TYPE_CURVE)
822 c.Start(Point(cur_x,cur_y));
823 c.AddConic(Point(x1,y1),Point(x,y));
828 curves.back().AddConic(Point(x1,y1),Point(x,y));
837 tangent[0] = 2*(x - x1);
838 tangent[1] = 2*(y - y1);
844 void curve_to_smooth(Real x2, Real y2, Real x, Real y)
846 Real x1 = tangent[0]/3.0 + cur_x;
847 Real y1 = tangent[1]/3.0 + cur_y;
849 curve_to(x1,y1,x2,y2,x,y);
852 void curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
854 //if we're not already a curve start one
855 if(prim != TYPE_CURVE)
859 c.Start(Point(cur_x,cur_y));
860 c.AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
865 curves.back().AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
871 //expand bounding box around ALL of it
876 tangent[0] = 3*(x - x2);
877 tangent[1] = 3*(y - y2);
885 if(flags & NotClosed)
887 if(cur_x != close_x || cur_y != close_y)
889 line_to(close_x,close_y);
896 //assumes the line to count the intersections with is (-1,0)
897 int intersect (Real x, Real y) const
901 vector<MonoSegment>::const_iterator s = segs.begin();
902 vector<CurveArray>::const_iterator c = curves.begin();
904 Point memory[3*MAX_SUBDIVISION_SIZE + 1];
906 for(i = 0; i < segs.size(); i++,s++)
908 inter += s->intersect(x,y);
911 for(i=0; i < curves.size(); i++,c++)
912 inter += c->intersect(x,y,memory);
917 //intersect an arbitrary line
918 //int intersect (Real x, Real y, Real vx, Real vy) {return 0;}
926 cur_x = cur_y = close_x = close_y = 0;
928 tangent[0] = tangent[1] = 0;
933 //*********** SCANLINE RENDERER SUPPORT STRUCTURES ***************
940 PenMark(int xin, int yin, Real c, Real a)
941 :y(yin),x(xin),cover(c),area(a) {}
943 void set(int xin, int yin, Real c, Real a) { y = yin; x = xin; cover = c; area = a; }
945 void setcoord(int xin, int yin) { y = yin; x = xin; }
947 void setcover(Real c, Real a) { cover = c; area = a; }
948 void addcover(Real c, Real a) { cover += c; area += a; }
950 bool operator < (const PenMark &rhs) const
952 return y == rhs.y ? x < rhs.x : y < rhs.y;
956 typedef rect<int> ContextRect;
958 class Layer_Shape::PolySpan
961 typedef deque<PenMark> cover_array;
963 Point arc[3*MAX_SUBDIVISION_SIZE + 1];
970 //ending position of last primitive
974 //starting position of current primitive list
978 //flags for the current segment
981 //the window that will be drawn (used for clipping)
984 //for assignment to flags value
991 //default constructor - 0 everything
992 PolySpan() :current(0,0,0,0),flags(NotSorted)
994 cur_x = cur_y = close_x = close_y = 0;
998 bool notclosed() const
1000 return (flags & NotClosed) || (cur_x != close_x) || (cur_y != close_y);
1003 //0 out all the variables involved in processing
1007 cur_x = cur_y = close_x = close_y = 0;
1009 current.set(0,0,0,0);
1013 //add the current cell, but only if there is information to add
1016 if(current.cover || current.area)
1018 covers.push_back(current);
1022 //move to the next cell (cover values 0 initially), keeping the current if necessary
1023 void move_pen(int x, int y)
1025 if(y != current.y || x != current.x)
1028 current.set(x,y,0,0);
1032 //close the primitives with a line (or rendering will not work as expected)
1035 if(flags & NotClosed)
1037 if(cur_x != close_x || cur_y != close_y)
1039 line_to(close_x,close_y);
1041 current.setcover(0,0);
1043 flags &= ~NotClosed;
1047 // Not recommended - destroys any separation of spans currently held
1050 sort(covers.begin(),covers.end());
1054 //will sort the marks if they are not sorted
1057 if(flags & NotSorted)
1059 //only sort the open index
1061 current.setcover(0,0);
1063 sort(covers.begin() + open_index,covers.end());
1064 flags &= ~NotSorted;
1068 //encapsulate the current sublist of marks (used for drawing)
1069 void encapsulate_current()
1071 //sort the current list then reposition the open list section
1073 open_index = covers.size();
1076 //move to start a new primitive list (enclose the last primitive if need be)
1077 void move_to(Real x, Real y)
1082 move_pen((int)floor(x),(int)floor(y));
1083 close_y = cur_y = y;
1084 close_x = cur_x = x;
1087 //primitive_to functions
1088 void line_to(Real x, Real y);
1089 void conic_to(Real x1, Real y1, Real x, Real y);
1090 void cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y);
1092 void draw_scanline(int y, Real x1, Real y1, Real x2, Real y2);
1093 void draw_line(Real x1, Real y1, Real x2, Real y2);
1095 Real ExtractAlpha(Real area, WindingStyle winding_style)
1100 if (winding_style == WINDING_NON_ZERO)
1102 // non-zero winding style
1106 else // if (winding_style == WINDING_EVEN_ODD)
1108 // even-odd winding style
1112 // want pyramid like thing
1121 /* === M E T H O D S ======================================================= */
1123 Layer_Shape::Layer_Shape(const Real &a, const Color::BlendMethod m):
1124 Layer_Composite (a,m),
1125 edge_table (new Intersector),
1126 color (Color::black()),
1130 blurtype (Blur::FASTGAUSSIAN),
1132 winding_style (WINDING_NON_ZERO),
1134 lastbyteop (Primitive::NONE),
1139 Layer_Shape::~Layer_Shape()
1145 Layer_Shape::clear()
1147 edge_table->clear();
1152 Layer_Shape::set_param(const String & param, const ValueBase &value)
1154 IMPORT_PLUS(color, { if (color.get_a() == 0) { if (converted_blend_) {
1155 set_blend_method(Color::BLEND_ALPHA_OVER);
1156 color.set_a(1); } else transparent_color_ = true; } });
1162 IMPORT(winding_style);
1164 return Layer_Composite::set_param(param,value);
1168 Layer_Shape::get_param(const String ¶m)const
1176 EXPORT(winding_style);
1181 return Layer_Composite::get_param(param);
1185 Layer_Shape::get_param_vocab()const
1187 Layer::Vocab ret(Layer_Composite::get_param_vocab());
1189 ret.push_back(ParamDesc("color")
1190 .set_local_name(_("Color"))
1191 .set_description(_("Layer_Shape Color"))
1193 ret.push_back(ParamDesc("offset")
1194 .set_local_name(_("Offset"))
1196 ret.push_back(ParamDesc("invert")
1197 .set_local_name(_("Invert"))
1199 ret.push_back(ParamDesc("antialias")
1200 .set_local_name(_("Antialiasing"))
1202 ret.push_back(ParamDesc("feather")
1203 .set_local_name(_("Feather"))
1206 ret.push_back(ParamDesc("blurtype")
1207 .set_local_name(_("Type of Feather"))
1208 .set_description(_("Type of feathering to use"))
1210 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
1211 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
1212 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
1213 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
1214 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
1216 ret.push_back(ParamDesc("winding_style")
1217 .set_local_name(_("Winding Style"))
1218 .set_description(_("Winding style to use"))
1220 .add_enum_value(WINDING_NON_ZERO,"nonzero",_("Non Zero"))
1221 .add_enum_value(WINDING_EVEN_ODD,"evenodd",_("Even/Odd"))
1227 synfig::Layer::Handle
1228 Layer_Shape::hit_check(synfig::Context context, const synfig::Point &p)const
1230 Point pos(p-offset);
1232 int intercepts = edge_table->intersect(pos[0],pos[1]);
1234 // If we have an odd number of intercepts, we are inside.
1235 // If we have an even number of intercepts, we are outside.
1236 bool intersect = ((!!intercepts) ^ invert);
1238 if(get_amount() == 0 || get_blend_method() == Color::BLEND_ALPHA_OVER)
1245 synfig::Layer::Handle tmp;
1246 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(p)))
1248 if(Color::is_onto(get_blend_method()))
1250 //if there's something in the lower layer then we're set...
1251 if(!context.hit_check(p).empty())
1252 return const_cast<Layer_Shape*>(this);
1253 }else if(get_blend_method() == Color::BLEND_ALPHA_OVER)
1255 synfig::info("layer_shape::hit_check - we've got alphaover");
1256 //if there's something in the lower layer then we're set...
1257 if(color.get_a() < 0.1 && get_amount() > .9)
1259 synfig::info("layer_shape::hit_check - can see through us... so nothing");
1261 }else return context.hit_check(p);
1263 return const_cast<Layer_Shape*>(this);
1266 return context.hit_check(p);
1270 Layer_Shape::get_color(Context context, const Point &p)const
1275 pp = Blur(feather,feather,blurtype)(p);
1277 Point pos(pp-offset);
1279 int intercepts = edge_table->intersect(pos[0],pos[1]);
1281 // If we have an odd number of intercepts, we are inside.
1282 // If we have an even number of intercepts, we are outside.
1283 bool intersect = ((!!intercepts) ^ invert);
1286 return context.get_color(pp);
1288 //Ok, we're inside... bummmm ba bum buM...
1289 if(get_blend_method() == Color::BLEND_STRAIGHT && get_amount() == 1)
1292 return Color::blend(color,context.get_color(p),get_amount(),get_blend_method());
1295 //************** SCANLINE RENDERING *********************
1296 void Layer_Shape::PolySpan::line_to(Real x, Real y)
1298 Real n[4] = {0,0,0,0};
1299 bool afterx = false;
1301 const Real xin(x), yin(y);
1303 Real dx = x - cur_x;
1304 Real dy = y - cur_y;
1308 //outside y - ignore entirely
1309 if( (cur_y >= window.maxy && y >= window.maxy)
1310 ||(cur_y < window.miny && y < window.miny) )
1315 else //not degenerate - more complicated
1317 if(dy > 0) //be sure it's not tooooo small
1319 // cur_y ... window.miny ... window.maxy ... y
1321 //initial degenerate - initial clip
1322 if(cur_y < window.miny)
1324 //new clipped start point (must also move pen)
1325 n[2] = cur_x + (window.miny - cur_y) * dx / dy;
1328 cur_y = window.miny;
1329 move_pen((int)floor(cur_x),window.miny);
1332 //generate data for the ending clipped info
1335 //initial line to intersection (and degenerate)
1336 n[2] = x + (window.maxy - y) * dx / dy;
1345 //initial degenerate - initial clip
1346 if(cur_y > window.maxy)
1348 //new clipped start point (must also move pen)
1349 n[2] = cur_x + (window.maxy - cur_y) * dx / dy;
1352 cur_y = window.maxy;
1353 move_pen((int)floor(cur_x),window.maxy);
1356 //generate data for the ending clipped info
1359 //initial line to intersection (and degenerate)
1360 n[2] = x + (window.miny - y) * dx / dy;
1368 //all degenerate - but require bounded clipped values
1369 if( (cur_x >= window.maxx && x >= window.maxx)
1370 ||(cur_x < window.minx && x < window.minx) )
1372 //clip both vertices - but only needed in the x direction
1373 cur_x = max(cur_x, (Real)window.minx);
1374 cur_x = min(cur_x, (Real)window.maxx);
1376 //clip the dest values - y is already clipped
1377 x = max(x,(Real)window.minx);
1378 x = min(x,(Real)window.maxx);
1380 //must start at new point...
1381 move_pen((int)floor(cur_x),(int)floor(cur_y));
1383 draw_line(cur_x,cur_y,x,y);
1393 //initial degenerate - initial clip
1394 if(cur_x < window.minx)
1396 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1397 n[2] = cur_y + (window.minx - cur_x) * dy / dx;
1399 move_pen(window.minx,(int)floor(cur_y));
1400 draw_line(window.minx,cur_y,window.minx,n[2]);
1402 cur_x = window.minx;
1406 //generate data for the ending clipped info
1409 //initial line to intersection (and degenerate)
1410 n[2] = y + (window.maxx - x) * dy / dx;
1422 //initial degenerate - initial clip
1423 if(cur_x > window.maxx)
1425 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1426 n[2] = cur_y + (window.maxx - cur_x) * dy / dx;
1428 move_pen(window.maxx,(int)floor(cur_y));
1429 draw_line(window.maxx,cur_y,window.maxx,n[2]);
1431 cur_x = window.maxx;
1435 //generate data for the ending clipped info
1438 //initial line to intersection (and degenerate)
1439 n[2] = y + (window.minx - x) * dy / dx;
1451 move_pen((int)floor(cur_x),(int)floor(cur_y));
1452 //draw the relevant line (clipped)
1453 draw_line(cur_x,cur_y,x,y);
1457 draw_line(x,y,n[0],n[1]);
1464 } catch(...) { synfig::error("line_to: cur_x=%f, cur_y=%f, x=%f, y=%f", cur_x, cur_y, x, y); throw; }
1466 flags |= NotClosed|NotSorted;
1469 static inline bool clip_conic(const Point *const p, const ContextRect &r)
1471 const Real minx = min(min(p[0][0],p[1][0]),p[2][0]);
1472 const Real miny = min(min(p[0][1],p[1][1]),p[2][1]);
1473 const Real maxx = max(max(p[0][0],p[1][0]),p[2][0]);
1474 const Real maxy = max(max(p[0][1],p[1][1]),p[2][1]);
1476 return (minx > r.maxx) ||
1482 static inline bool clip_cubic(const Point *const p, const ContextRect &r)
1484 /*const Real minx = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
1485 const Real miny = min(min(p[0][1],p[1][1]),min(p[2][1],p[3][1]));
1486 const Real maxx = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][1]));
1487 const Real maxy = max(max(p[0][1],p[1][1]),max(p[2][1],p[3][1]));
1489 return (minx > r.maxx) ||
1494 return ((p[0][0] > r.maxx) && (p[1][0] > r.maxx) && (p[2][0] > r.maxx) && (p[3][0] > r.maxx)) ||
1495 ((p[0][0] < r.minx) && (p[1][0] < r.minx) && (p[2][0] < r.minx) && (p[3][0] < r.minx)) ||
1496 ((p[0][1] > r.maxy) && (p[1][1] > r.maxy) && (p[2][1] > r.maxy) && (p[3][1] > r.maxy)) ||
1497 ((p[0][1] < r.miny) && (p[1][1] < r.miny) && (p[2][1] < r.miny) && (p[3][1] < r.miny));
1500 static inline Real max_edges_cubic(const Point *const p)
1502 const Real x1 = p[1][0] - p[0][0];
1503 const Real y1 = p[1][1] - p[0][1];
1505 const Real x2 = p[2][0] - p[1][0];
1506 const Real y2 = p[2][1] - p[1][1];
1508 const Real x3 = p[3][0] - p[2][0];
1509 const Real y3 = p[3][1] - p[2][1];
1511 const Real d1 = x1*x1 + y1*y1;
1512 const Real d2 = x2*x2 + y2*y2;
1513 const Real d3 = x3*x3 + y3*y3;
1515 return max(max(d1,d2),d3);
1518 static inline Real max_edges_conic(const Point *const p)
1520 const Real x1 = p[1][0] - p[0][0];
1521 const Real y1 = p[1][1] - p[0][1];
1523 const Real x2 = p[2][0] - p[1][0];
1524 const Real y2 = p[2][1] - p[1][1];
1526 const Real d1 = x1*x1 + y1*y1;
1527 const Real d2 = x2*x2 + y2*y2;
1532 void Layer_Shape::PolySpan::conic_to(Real x1, Real y1, Real x, Real y)
1534 Point *current = arc;
1537 bool onsecond = false;
1539 arc[0] = Point(x,y);
1540 arc[1] = Point(x1,y1);
1541 arc[2] = Point(cur_x,cur_y);
1543 //just draw the line if it's outside
1544 if(clip_conic(arc,window))
1550 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1551 while(current >= arc)
1553 if(num >= MAX_SUBDIVISION_SIZE)
1555 warning("Curve subdivision somehow ran out of space while tessellating!");
1561 //if the curve is clipping then draw degenerate
1562 if(clip_conic(current,window))
1564 line_to(current[0][0],current[0][1]); //backwards so front is destination
1566 if(onsecond) level--;
1571 //if we are not at the level minimum
1572 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1574 Subd_Conic_Stack(current);
1575 current += 2; //cursor on second curve
1581 //split it again, if it's too big
1582 if(max_edges_conic(current) > 0.25) //distance of .5 (cover no more than half the pixel)
1584 Subd_Conic_Stack(current);
1585 current += 2; //cursor on second curve
1590 else //NOT TOO BIG? RENDER!!!
1592 //cur_x,cur_y = current[2], so we need to go 1,0
1593 line_to(current[1][0],current[1][1]);
1594 line_to(current[0][0],current[0][1]);
1597 if(onsecond) level--;
1604 void Layer_Shape::PolySpan::cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
1606 Point *current = arc;
1609 bool onsecond = false;
1611 arc[0] = Point(x,y);
1612 arc[1] = Point(x2,y2);
1613 arc[2] = Point(x1,y1);
1614 arc[3] = Point(cur_x,cur_y);
1616 //just draw the line if it's outside
1617 if(clip_cubic(arc,window))
1623 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1624 while(current >= arc) //once current goes below arc, there are no more curves left
1626 if(num >= MAX_SUBDIVISION_SIZE)
1628 warning("Curve subdivision somehow ran out of space while tessellating!");
1635 //if we are not at the level minimum
1636 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1638 Subd_Cubic_Stack(current);
1639 current += 3; //cursor on second curve
1645 //if the curve is clipping then draw degenerate
1646 if(clip_cubic(current,window))
1648 line_to(current[0][0],current[0][1]); //backwards so front is destination
1650 if(onsecond) level--;
1656 //split it again, if it's too big
1657 if(max_edges_cubic(current) > 0.25) //could use max_edges<3>
1659 Subd_Cubic_Stack(current);
1660 current += 3; //cursor on second curve
1665 else //NOT TOO BIG? RENDER!!!
1667 //cur_x,cur_y = current[3], so we need to go 2,1,0
1668 line_to(current[2][0],current[2][1]);
1669 line_to(current[1][0],current[1][1]);
1670 line_to(current[0][0],current[0][1]);
1673 if(onsecond) level--;
1680 //******************** LINE ALGORITHMS ****************************
1681 // THESE CALCULATE THE AREA AND THE COVER FOR THE MARKS, TO THEN SCAN CONVERT
1682 // - BROKEN UP INTO SCANLINES (draw_line - y intersections),
1683 // THEN THE COVER AND AREA PER TOUCHED PIXEL IS CALCULATED (draw_scanline - x intersections)
1684 void Layer_Shape::PolySpan::draw_scanline(int y, Real x1, Real fy1, Real x2, Real fy2)
1686 int ix1 = (int)floor(x1);
1687 int ix2 = (int)floor(x2);
1688 Real fx1 = x1 - ix1;
1689 Real fx2 = x2 - ix2;
1691 Real dx,dy,dydx,mult;
1696 //case horizontal line
1699 move_pen(ix2,y); //pen needs to be at the last coord
1703 //case all in same pixel
1704 if(ix1 == ix2) //impossible for degenerate case (covered by the previous cases)
1706 current.addcover(dy,(fx1 + fx2)*dy/2); //horizontal trapezoid area
1712 // ----> fx1...1 0...1 ... 0...1 0...fx2
1715 //set initial values
1716 //Iterate through the covered pixels
1717 mult = (1 - fx1)*dydx; //next y intersection diff value (at 1)
1720 current.addcover(mult,(1 + fx1)*mult/2); // fx1,fy1,1,fy@1 - starting trapezoidal area
1722 //move to the next pixel
1728 //set up for whole ones
1731 //trapezoid(0,y1,1,y1+dydx);
1732 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1734 //move to next pixel (+1)
1741 //final y-pos - last intersect pos
1743 current.addcover(mult,(0+fx2)*mult/2);
1746 // fx2...1 0...1 ... 0...1 0...fx1 <----
1747 //mult = (0 - fx1) * dy / dx;
1748 //neg sign sucked into dydx
1751 //set initial values
1752 //Iterate through the covered pixels
1753 mult = fx1*dydx; //next y intersection diff value
1756 current.addcover(mult,fx1*mult/2); // fx1,fy1,0,fy@0 - starting trapezoidal area
1758 //move to next pixel
1764 //set up for whole ones
1767 //trapezoid(0,y1,1,y1+dydx);
1768 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1770 //move to next pixel (-1)
1777 mult = fy2 - fy1; //final y-pos - last intersect pos
1779 current.addcover(mult,(fx2+1)*mult/2);
1783 void Layer_Shape::PolySpan::draw_line(Real x1, Real y1, Real x2, Real y2)
1785 int iy1 = (int)floor(y1);
1786 int iy2 = (int)floor(y2);
1787 Real fy1 = y1 - iy1;
1788 Real fy2 = y2 - iy2;
1790 assert(!isnan(fy1));
1791 assert(!isnan(fy2));
1793 Real dx,dy,dxdy,mult,x_from,x_to;
1795 const Real SLOPE_EPSILON = 1e-10;
1797 //case all one scanline
1800 draw_scanline(iy1,x1,y1,x2,y2);
1808 //case vertical line
1809 if(dx < SLOPE_EPSILON && dx > -SLOPE_EPSILON)
1811 //calc area and cover on vertical line
1814 // ----> fx1...1 0...1 ... 0...1 0...fx2
1817 int ix1 = (int)floor(x1);
1818 Real fx1 = x1 - ix1;
1823 current.addcover(sub,fx1*sub);
1828 //move pen to next pixel
1834 current.addcover(1,fx1);
1842 current.addcover(fy2,fy2*fx1);
1847 int ix1 = (int)floor(x1);
1848 Real fx1 = x1 - ix1;
1853 current.addcover(sub,fx1*sub);
1862 //accumulate in current pixel
1863 current.addcover(-1,-fx1);
1870 current.addcover(fy2-1,(fy2-1)*fx1);
1875 //case normal line - guaranteed dx != 0 && dy != 0
1877 //calculate the initial intersection with "next" scanline
1882 mult = (1 - fy1) * dxdy;
1884 //x intersect scanline
1886 draw_scanline(iy1,x1,fy1,x_from,1);
1891 move_pen((int)floor(x_from),iy1);
1895 //keep up on the x axis, and render the current scanline
1896 x_to = x_from + dxdy;
1897 draw_scanline(iy1,x_from,0,x_to,1);
1900 //move to next pixel
1902 move_pen((int)floor(x_from),iy1);
1905 //draw the last one, fractional
1906 draw_scanline(iy2,x_from,0,x2,fy2);
1914 //x intersect scanline
1916 draw_scanline(iy1,x1,fy1,x_from,0);
1921 move_pen((int)floor(x_from),iy1);
1925 x_to = x_from + dxdy;
1926 draw_scanline(iy1,x_from,1,x_to,0);
1930 move_pen((int)floor(x_from),iy1);
1932 //draw the last one, fractional
1933 draw_scanline(iy2,x_from,1,x2,fy2);
1937 //****** LAYER PEN OPERATIONS (move_to, line_to, etc.) ******
1938 void Layer_Shape::move_to(Real x, Real y)
1940 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1944 op.operation = Primitive::MOVE_TO;
1945 op.number = 1; //one point for now
1947 if(lastbyteop == Primitive::MOVE_TO)
1949 char *ptr = &bytestream[lastoppos];
1950 memcpy(ptr,&op,sizeof(op));
1951 memcpy(ptr+sizeof(op),&p,sizeof(p));
1953 else //make a new op
1955 lastbyteop = Primitive::MOVE_TO;
1956 lastoppos = bytestream.size();
1958 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
1959 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
1962 edge_table->move_to(x,y);
1965 void Layer_Shape::close()
1969 op.operation = Primitive::CLOSE;
1972 if(lastbyteop == Primitive::CLOSE)
1976 lastbyteop = Primitive::CLOSE;
1977 lastoppos = bytestream.size();
1979 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert header
1982 edge_table->close();
1983 //should not affect the bounding box since it would just be returning to old point...
1986 void Layer_Shape::endpath()
1990 op.operation = Primitive::END;
1993 if(lastbyteop == Primitive::END || lastbyteop == Primitive::NONE)
1997 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1));
1999 //should not affect the bounding box since it would just be returning to old point... if at all
2002 void Layer_Shape::line_to(Real x, Real y)
2007 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2011 op.operation = Primitive::LINE_TO;
2012 op.number = 1; //one point for now
2014 if(lastbyteop == Primitive::MOVE_TO || lastbyteop == Primitive::LINE_TO)
2016 //only need to insert the point
2017 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2019 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2020 prim->number++; //increment number of points in the list
2023 lastbyteop = Primitive::LINE_TO;
2024 lastoppos = bytestream.size();
2026 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2027 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2030 edge_table->line_to(x,y);
2033 void Layer_Shape::conic_to(Real x1, Real y1, Real x, Real y)
2035 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*2;
2040 op.operation = Primitive::CONIC_TO;
2041 op.number = 2; //2 points for now
2043 if(lastbyteop == Primitive::CONIC_TO)
2045 //only need to insert the new points
2046 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2047 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2049 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2050 prim->number += 2; //increment number of points in the list
2053 lastbyteop = Primitive::CONIC_TO;
2054 lastoppos = bytestream.size();
2056 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2057 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2058 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2061 edge_table->conic_to(x1,y1,x,y);
2064 void Layer_Shape::conic_to_smooth(Real x, Real y) //x1,y1 derived from current tangent
2066 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2070 op.operation = Primitive::CONIC_TO_SMOOTH;
2071 op.number = 1; //2 points for now
2073 if(lastbyteop == Primitive::CONIC_TO_SMOOTH)
2075 //only need to insert the new point
2076 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2078 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2079 prim->number += 1; //increment number of points in the list
2082 lastbyteop = Primitive::CONIC_TO_SMOOTH;
2083 lastoppos = bytestream.size();
2085 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2086 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2089 edge_table->conic_to_smooth(x,y);
2092 void Layer_Shape::curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
2094 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2100 op.operation = Primitive::CUBIC_TO;
2101 op.number = 3; //3 points for now
2103 if(lastbyteop == Primitive::CUBIC_TO)
2105 //only need to insert the new points
2106 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2107 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2108 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2110 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2111 prim->number += 3; //increment number of points in the list
2114 lastbyteop = Primitive::CUBIC_TO;
2115 lastoppos = bytestream.size();
2117 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2118 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2119 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2120 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2123 edge_table->curve_to(x1,y1,x2,y2,x,y);
2126 void Layer_Shape::curve_to_smooth(Real x2, Real y2, Real x, Real y) //x1,y1 derived from current tangent
2128 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2133 op.operation = Primitive::CUBIC_TO_SMOOTH;
2134 op.number = 2; //3 points for now
2136 if(lastbyteop == Primitive::CUBIC_TO_SMOOTH)
2138 //only need to insert the new points
2139 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2140 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2142 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2143 prim->number += 2; //increment number of points in the list
2146 lastbyteop = Primitive::CUBIC_TO_SMOOTH;
2147 lastoppos = bytestream.size();
2149 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2150 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2151 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2155 // ACCELERATED RENDER FUNCTION - TRANSLATE BYTE CODE INTO FUNCTION CALLS
2157 bool Layer_Shape::render_polyspan(Surface *surface, PolySpan &polyspan,
2158 Color::BlendMethod got_blend_method, Color::value_type got_amount) const
2160 Surface::alpha_pen p(surface->begin(),got_amount,_BlendFunc(got_blend_method));
2161 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2162 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2164 Real cover,area,alpha;
2171 if(cur_mark == end_mark)
2176 p.move_to(polyspan.window.minx,polyspan.window.miny);
2177 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2182 //fill initial rect / line
2185 //fill all the area above the first vertex
2186 p.move_to(polyspan.window.minx,polyspan.window.miny);
2187 y = polyspan.window.miny;
2188 int l = polyspan.window.maxx - polyspan.window.minx;
2190 p.put_block(cur_mark->y - polyspan.window.miny,l);
2192 //fill the area to the left of the first vertex on that line
2193 l = cur_mark->x - polyspan.window.minx;
2194 p.move_to(polyspan.window.minx,cur_mark->y);
2195 if(l) p.put_hline(l);
2205 area = cur_mark->area;
2206 cover += cur_mark->cover;
2208 //accumulate for the current pixel
2209 while(++cur_mark != polyspan.covers.end())
2211 if(y != cur_mark->y || x != cur_mark->x)
2214 area += cur_mark->area;
2215 cover += cur_mark->cover;
2218 //draw pixel - based on covered area
2219 if(area) //if we're ok, draw the current pixel
2221 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2222 if(invert) alpha = 1 - alpha;
2226 if(alpha >= .5) p.put_value();
2228 else if(alpha) p.put_value_alpha(alpha);
2234 //if we're done, don't use iterator and exit
2235 if(cur_mark == end_mark) break;
2237 //if there is no more live pixels on this line, goto next
2238 if(y != cur_mark->y)
2242 //fill the area at the end of the line
2243 p.put_hline(polyspan.window.maxx - x);
2245 //fill area at the beginning of the next line
2246 p.move_to(polyspan.window.minx,cur_mark->y);
2247 p.put_hline(cur_mark->x - polyspan.window.minx);
2255 //draw span to next pixel - based on total amount of pixel cover
2258 alpha = polyspan.ExtractAlpha(cover, winding_style);
2259 if(invert) alpha = 1 - alpha;
2263 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2265 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2269 //fill the after stuff
2272 //fill the area at the end of the line
2273 p.put_hline(polyspan.window.maxx - x);
2275 //fill area at the beginning of the next line
2276 p.move_to(polyspan.window.minx,y+1);
2277 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2283 bool Layer_Shape::render_polyspan(etl::surface<float> *surface, PolySpan &polyspan) const
2285 etl::surface<float>::pen p(surface->begin());
2286 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2287 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2289 Real cover,area,alpha;
2295 //the pen always writes 1 (unless told to do otherwise)
2298 if(cur_mark == end_mark)
2303 p.move_to(polyspan.window.minx,polyspan.window.miny);
2304 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2309 //fill initial rect / line
2312 //fill all the area above the first vertex
2313 p.move_to(polyspan.window.minx,polyspan.window.miny);
2314 y = polyspan.window.miny;
2315 int l = polyspan.window.maxx - polyspan.window.minx;
2317 p.put_block(cur_mark->y - polyspan.window.miny,l);
2319 //fill the area to the left of the first vertex on that line
2320 l = cur_mark->x - polyspan.window.minx;
2321 p.move_to(polyspan.window.minx,cur_mark->y);
2322 if(l) p.put_hline(l);
2331 area = cur_mark->area;
2332 cover += cur_mark->cover;
2334 //accumulate for the current pixel
2335 while(++cur_mark != polyspan.covers.end())
2337 if(y != cur_mark->y || x != cur_mark->x)
2340 area += cur_mark->area;
2341 cover += cur_mark->cover;
2344 //draw pixel - based on covered area
2345 if(area) //if we're ok, draw the current pixel
2347 alpha = 1 - polyspan.ExtractAlpha(cover - area, winding_style);
2350 if(alpha >= .5) p.put_value();
2352 else if(alpha) p.put_value(alpha);
2358 //if we're done, don't use iterator and exit
2359 if(cur_mark == end_mark) break;
2361 //if there is no more live pixels on this line, goto next
2362 if(y != cur_mark->y)
2364 //fill the area at the end of the line
2365 p.put_hline(polyspan.window.maxx - x);
2367 //fill area at the beginning of the next line
2368 p.move_to(polyspan.window.minx,cur_mark->y);
2369 p.put_hline(cur_mark->x - polyspan.window.minx);
2376 //draw span to next pixel - based on total amount of pixel cover
2379 alpha = 1 - polyspan.ExtractAlpha(cover, winding_style);
2382 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2384 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2388 //fill the area at the end of the line
2389 p.put_hline(polyspan.window.maxx - x);
2391 //fill area at the beginning of the next line
2392 p.move_to(polyspan.window.minx,y+1);
2393 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2403 area = cur_mark->area;
2404 cover += cur_mark->cover;
2406 //accumulate for the current pixel
2407 while(++cur_mark != polyspan.covers.end())
2409 if(y != cur_mark->y || x != cur_mark->x)
2412 area += cur_mark->area;
2413 cover += cur_mark->cover;
2416 //draw pixel - based on covered area
2417 if(area) //if we're ok, draw the current pixel
2419 alpha = polyspan.ExtractAlpha(cover - area, winding_style);
2422 if(alpha >= .5) p.put_value();
2424 else if(alpha) p.put_value(alpha);
2430 //if we're done, don't use iterator and exit
2431 if(cur_mark == end_mark) break;
2433 //if there is no more live pixels on this line, goto next
2434 if(y != cur_mark->y)
2441 //draw span to next pixel - based on total amount of pixel cover
2444 alpha = polyspan.ExtractAlpha(cover, winding_style);
2447 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2449 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2458 Layer_Shape::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
2460 const unsigned int w = renddesc.get_w();
2461 const unsigned int h = renddesc.get_h();
2463 const Real pw = abs(renddesc.get_pw());
2464 const Real ph = abs(renddesc.get_ph());
2466 //const Real OFFSET_EPSILON = 1e-8;
2467 SuperCallback stageone(cb,1,10000,15001+renddesc.get_h());
2468 SuperCallback stagetwo(cb,10000,10001+renddesc.get_h(),15001+renddesc.get_h());
2469 SuperCallback stagethree(cb,10001+renddesc.get_h(),15001+renddesc.get_h(),15001+renddesc.get_h());
2471 // Render what is behind us
2473 //clip if it satisfies the invert solid thing
2474 if(is_solid_color() && invert)
2476 Rect aabb = edge_table->aabb;
2477 Point tl = renddesc.get_tl() - offset;
2479 Real pw = renddesc.get_pw(),
2480 ph = renddesc.get_ph();
2484 Real pixelfeatherx = quality == 10 ? 0 : abs(feather/pw),
2485 pixelfeathery = quality == 10 ? 0 : abs(feather/ph);
2487 nrect.set_point((aabb.minx - tl[0])/pw,(aabb.miny - tl[1])/ph);
2488 nrect.expand((aabb.maxx - tl[0])/pw,(aabb.maxy - tl[1])/ph);
2490 RendDesc optdesc(renddesc);
2492 //make sure to expand so we gain subpixels rather than lose them
2493 nrect.minx = floor(nrect.minx-pixelfeatherx); nrect.miny = floor(nrect.miny-pixelfeathery);
2494 nrect.maxx = ceil(nrect.maxx+pixelfeatherx); nrect.maxy = ceil(nrect.maxy+pixelfeathery);
2496 //make sure the subwindow is clipped with our tile window (minimize useless drawing)
2497 set_intersect(nrect,nrect,Rect(0,0,renddesc.get_w(),renddesc.get_h()));
2499 //must resize the surface first
2500 surface->set_wh(renddesc.get_w(),renddesc.get_h());
2503 //only render anything if it's visible from our current tile
2506 //set the subwindow to the viewable pixels and render it to the subsurface
2507 optdesc.set_subwindow((int)nrect.minx, (int)nrect.miny,
2508 (int)(nrect.maxx - nrect.minx), (int)(nrect.maxy - nrect.miny));
2510 Surface optimizedbacksurf;
2511 if(!context.accelerated_render(&optimizedbacksurf,quality,optdesc,&stageone))
2514 //blit that onto the original surface so we can pretend that nothing ever happened
2515 Surface::pen p = surface->get_pen((int)nrect.minx,(int)nrect.miny);
2516 optimizedbacksurf.blit_to(p);
2520 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
2524 if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
2526 if(feather && quality != 10)
2528 //we have to blur rather than be crappy
2530 //so make a separate surface
2531 RendDesc workdesc(renddesc);
2533 etl::surface<float> shapesurface;
2535 //the expanded size = 1/2 the size in each direction rounded up
2536 int halfsizex = (int) (abs(feather*.5/pw) + 3),
2537 halfsizey = (int) (abs(feather*.5/ph) + 3);
2539 //expand by 1/2 size in each direction on either side
2546 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2549 case Blur::FASTGAUSSIAN:
2556 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2559 case Blur::GAUSSIAN:
2561 #define GAUSSIAN_ADJUSTMENT (0.05)
2562 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
2563 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
2568 halfsizex = (int)(abs(pw)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2569 halfsizey = (int)(abs(ph)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2571 halfsizex = (halfsizex + 1)/2;
2572 halfsizey = (halfsizey + 1)/2;
2573 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
2579 shapesurface.set_wh(workdesc.get_w(),workdesc.get_h());
2580 shapesurface.clear();
2583 if(!render_shape(&shapesurface,quality,workdesc,&stagetwo))return false;
2586 Blur(feather,feather,blurtype,&stagethree)(shapesurface,workdesc.get_br()-workdesc.get_tl(),shapesurface);
2588 //blend with stuff below it...
2589 unsigned int u = halfsizex, v = halfsizey, x = 0, y = 0;
2590 for(y = 0; y < h; y++,v++)
2593 for(x = 0; x < w; x++,u++)
2595 float a = shapesurface[v][u];
2598 //a = floor(a*255+0.5f)/255;
2599 (*surface)[y][x]=Color::blend(color,(*surface)[y][x],a*get_amount(),get_blend_method());
2601 //else (*surface)[y][x] = worksurface[v][u];
2606 if(cb && !cb->amount_complete(100,100))
2608 synfig::warning("Layer_Shape: could not set amount complete");
2615 //might take out to reduce code size
2616 return render_shape(surface,true,quality,renddesc,&stagetwo);
2622 Layer_Shape::render_shape(Surface *surface,bool useblend,int /*quality*/,
2623 const RendDesc &renddesc, ProgressCallback *cb)const
2627 SuperCallback progress(cb,0,renddesc.get_h(),renddesc.get_h());
2629 // If our amount is set to zero, no need to render anything
2633 //test new polygon renderer
2635 // Width and Height of a pixel
2636 const int w = renddesc.get_w();
2637 const int h = renddesc.get_h();
2638 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2639 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2641 const Point tl = renddesc.get_tl();
2643 Vector tangent (0,0);
2647 // if the pixels are zero sized then we're too zoomed out to see anything
2648 if (pw == 0 || ph == 0)
2651 //optimization for tessellating only inside tiles
2652 span.window.minx = 0;
2653 span.window.miny = 0;
2654 span.window.maxx = w;
2655 span.window.maxy = h;
2657 //pointers for processing the bytestream
2658 const char *current = &bytestream[0];
2659 const char *end = &bytestream[bytestream.size()];
2661 int operation = Primitive::NONE;
2668 Real x,y,x1,y1,x2,y2;
2671 while(current < end)
2677 //get the op code safely
2678 curprim = (Primitive *)current;
2680 //advance past indices
2681 current += sizeof(Primitive);
2684 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2688 //get the relevant data
2689 operation = curprim->operation;
2690 number = curprim->number;
2692 if(operation == Primitive::END)
2695 if(operation == Primitive::CLOSE)
2697 if(span.notclosed())
2699 tangent[0] = span.close_x - span.cur_x;
2700 tangent[1] = span.close_y - span.cur_y;
2706 data = (Point*)current;
2707 current += sizeof(Point)*number;
2709 //check data positioning
2712 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2716 } catch(...) { synfig::error("Layer_Shape::render_shape()1: Caught an exception after %d loops, rethrowing...", tmp); throw; }
2718 //transfer all the data - RLE optimized
2719 for(curnum=0; curnum < number;)
2723 case Primitive::MOVE_TO:
2725 x = data[curnum][0];
2726 x = (x - tl[0] + offset[0])*pw;
2727 y = data[curnum][1];
2728 y = (y - tl[1] + offset[1])*ph;
2739 tangent[0] = x - span.cur_x;
2740 tangent[1] = y - span.cur_y;
2745 curnum++; //only advance one point
2750 case Primitive::LINE_TO:
2752 x = data[curnum][0];
2753 x = (x - tl[0] + offset[0])*pw;
2754 y = data[curnum][1];
2755 y = (y - tl[1] + offset[1])*ph;
2757 tangent[0] = x - span.cur_x;
2758 tangent[1] = y - span.cur_y;
2765 case Primitive::CONIC_TO:
2767 x = data[curnum+1][0];
2768 x = (x - tl[0] + offset[0])*pw;
2769 y = data[curnum+1][1];
2770 y = (y - tl[1] + offset[1])*ph;
2772 x1 = data[curnum][0];
2773 x1 = (x1 - tl[0] + offset[0])*pw;
2774 y1 = data[curnum][1];
2775 y1 = (y1 - tl[1] + offset[1])*ph;
2777 tangent[0] = 2*(x - x1);
2778 tangent[1] = 2*(y - y1);
2780 span.conic_to(x1,y1,x,y);
2785 case Primitive::CONIC_TO_SMOOTH:
2787 x = data[curnum][0];
2788 x = (x - tl[0] + offset[0])*pw;
2789 y = data[curnum][1];
2790 y = (y - tl[1] + offset[1])*ph;
2792 x1 = span.cur_x + tangent[0]/2;
2793 y1 = span.cur_y + tangent[1]/2;
2795 tangent[0] = 2*(x - x1);
2796 tangent[1] = 2*(y - y1);
2798 span.conic_to(x1,y1,x,y);
2804 case Primitive::CUBIC_TO:
2806 x = data[curnum+2][0];
2807 x = (x - tl[0] + offset[0])*pw;
2808 y = data[curnum+2][1];
2809 y = (y - tl[1] + offset[1])*ph;
2811 x2 = data[curnum+1][0];
2812 x2 = (x2 - tl[0] + offset[0])*pw;
2813 y2 = data[curnum+1][1];
2814 y2 = (y2 - tl[1] + offset[1])*ph;
2816 x1 = data[curnum][0];
2817 x1 = (x1 - tl[0] + offset[0])*pw;
2818 y1 = data[curnum][1];
2819 y1 = (y1 - tl[1] + offset[1])*ph;
2821 tangent[0] = 2*(x - x2);
2822 tangent[1] = 2*(y - y2);
2824 span.cubic_to(x1,y1,x2,y2,x,y);
2830 case Primitive::CUBIC_TO_SMOOTH:
2832 x = data[curnum+1][0];
2833 x = (x - tl[0] + offset[0])*pw;
2834 y = data[curnum+1][1];
2835 y = (y - tl[1] + offset[1])*ph;
2837 x2 = data[curnum][0];
2838 x2 = (x2 - tl[0] + offset[0])*pw;
2839 y2 = data[curnum][1];
2840 y2 = (y2 - tl[1] + offset[1])*ph;
2842 x1 = span.cur_x + tangent[0]/3.0;
2843 y1 = span.cur_y + tangent[1]/3.0;
2845 tangent[0] = 2*(x - x2);
2846 tangent[1] = 2*(y - y2);
2848 span.cubic_to(x1,y1,x2,y2,x,y);
2857 //sort the bastards so we can render everything
2860 return render_polyspan(surface, span,
2861 useblend?get_blend_method():Color::BLEND_STRAIGHT,
2862 useblend?get_amount():1.0);
2866 Layer_Shape::render_shape(etl::surface<float> *surface,int /*quality*/,
2867 const RendDesc &renddesc, ProgressCallback */*cb*/)const
2869 // If our amount is set to zero, no need to render anything
2873 //test new polygon renderer
2875 // Width and Height of a pixel
2876 const int w = renddesc.get_w();
2877 const int h = renddesc.get_h();
2878 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2879 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2881 const Point tl = renddesc.get_tl();
2883 Vector tangent (0,0);
2887 //optimization for tessellating only inside tiles
2888 span.window.minx = 0;
2889 span.window.miny = 0;
2890 span.window.maxx = w;
2891 span.window.maxy = h;
2893 //pointers for processing the bytestream
2894 const char *current = &bytestream[0];
2895 const char *end = &bytestream[bytestream.size()];
2897 int operation = Primitive::NONE;
2904 Real x,y,x1,y1,x2,y2;
2906 while(current < end)
2908 //get the op code safely
2909 curprim = (Primitive *)current;
2911 //advance past indices
2912 current += sizeof(Primitive);
2915 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2919 //get the relevant data
2920 operation = curprim->operation;
2921 number = curprim->number;
2923 if(operation == Primitive::END)
2926 if(operation == Primitive::CLOSE)
2928 if(span.notclosed())
2930 tangent[0] = span.close_x - span.cur_x;
2931 tangent[1] = span.close_y - span.cur_y;
2937 data = (Point*)current;
2938 current += sizeof(Point)*number;
2940 //check data positioning
2943 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2947 //transfer all the data
2948 for(curnum=0; curnum < number;)
2952 case Primitive::MOVE_TO:
2954 x = data[curnum][0];
2955 x = (x - tl[0] + offset[0])*pw;
2956 y = data[curnum][1];
2957 y = (y - tl[1] + offset[1])*ph;
2968 tangent[0] = x - span.cur_x;
2969 tangent[1] = y - span.cur_y;
2974 curnum++; //only advance one point
2979 case Primitive::LINE_TO:
2981 x = data[curnum][0];
2982 x = (x - tl[0] + offset[0])*pw;
2983 y = data[curnum][1];
2984 y = (y - tl[1] + offset[1])*ph;
2986 tangent[0] = x - span.cur_x;
2987 tangent[1] = y - span.cur_y;
2994 case Primitive::CONIC_TO:
2996 x = data[curnum+1][0];
2997 x = (x - tl[0] + offset[0])*pw;
2998 y = data[curnum+1][1];
2999 y = (y - tl[1] + offset[1])*ph;
3001 x1 = data[curnum][0];
3002 x1 = (x1 - tl[0] + offset[0])*pw;
3003 y1 = data[curnum][1];
3004 y1 = (y1 - tl[1] + offset[1])*ph;
3006 tangent[0] = 2*(x - x1);
3007 tangent[1] = 2*(y - y1);
3009 span.conic_to(x1,y1,x,y);
3014 case Primitive::CONIC_TO_SMOOTH:
3016 x = data[curnum][0];
3017 x = (x - tl[0] + offset[0])*pw;
3018 y = data[curnum][1];
3019 y = (y - tl[1] + offset[1])*ph;
3021 x1 = span.cur_x + tangent[0]/2;
3022 y1 = span.cur_y + tangent[1]/2;
3024 tangent[0] = 2*(x - x1);
3025 tangent[1] = 2*(y - y1);
3027 span.conic_to(x1,y1,x,y);
3033 case Primitive::CUBIC_TO:
3035 x = data[curnum+2][0];
3036 x = (x - tl[0] + offset[0])*pw;
3037 y = data[curnum+2][1];
3038 y = (y - tl[1] + offset[1])*ph;
3040 x2 = data[curnum+1][0];
3041 x2 = (x2 - tl[0] + offset[0])*pw;
3042 y2 = data[curnum+1][1];
3043 y2 = (y2 - tl[1] + offset[1])*ph;
3045 x1 = data[curnum][0];
3046 x1 = (x1 - tl[0] + offset[0])*pw;
3047 y1 = data[curnum][1];
3048 y1 = (y1 - tl[1] + offset[1])*ph;
3050 tangent[0] = 2*(x - x2);
3051 tangent[1] = 2*(y - y2);
3053 span.cubic_to(x1,y1,x2,y2,x,y);
3059 case Primitive::CUBIC_TO_SMOOTH:
3061 x = data[curnum+1][0];
3062 x = (x - tl[0] + offset[0])*pw;
3063 y = data[curnum+1][1];
3064 y = (y - tl[1] + offset[1])*ph;
3066 x2 = data[curnum][0];
3067 x2 = (x2 - tl[0] + offset[0])*pw;
3068 y2 = data[curnum][1];
3069 y2 = (y2 - tl[1] + offset[1])*ph;
3071 x1 = span.cur_x + tangent[0]/3.0;
3072 y1 = span.cur_y + tangent[1]/3.0;
3074 tangent[0] = 2*(x - x2);
3075 tangent[1] = 2*(y - y2);
3077 span.cubic_to(x1,y1,x2,y2,x,y);
3086 //sort the bastards so we can render everything
3089 return render_polyspan(surface, span);
3093 Layer_Shape::get_bounding_rect()const
3096 return Rect::full_plane();
3098 if (edge_table->initaabb)
3099 return Rect::zero();
3101 Rect bounds(edge_table->aabb+offset);
3102 bounds.expand(max((bounds.get_min() - bounds.get_max()).mag()*0.01,