1 /* === S Y N F I G ========================================================= */
2 /*! \file layer_shape.cpp
3 ** \brief Template Header
5 ** $Id: layer_shape.cpp,v 1.2 2005/01/24 03:08:18 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "layer_shape.h"
36 #include "paramdesc.h"
40 #include "valuenode.h"
44 #include "curve_helper.h"
52 /* === U S I N G =========================================================== */
54 using namespace synfig;
58 /* === G L O B A L S ======================================================= */
60 SYNFIG_LAYER_INIT(Layer_Shape);
61 SYNFIG_LAYER_SET_NAME(Layer_Shape,"shape");
62 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_Shape,_("Shape"));
63 SYNFIG_LAYER_SET_CATEGORY(Layer_Shape,_("Internal"));
64 SYNFIG_LAYER_SET_VERSION(Layer_Shape,"0.1");
65 SYNFIG_LAYER_SET_CVS_ID(Layer_Shape,"$Id: layer_shape.cpp,v 1.2 2005/01/24 03:08:18 darco Exp $");
70 inline bool IsZero(const T &n)
72 return (n < EPSILON) && (n > -EPSILON);
75 /* === C L A S S E S ======================================================= */
77 //Assumes 64 byte aligned structures if at all
88 MOVE_TO = 0, //(x,y)+ after first point treated as line_to
89 CLOSE, // NOT RUNLENGTH enabled
90 LINE_TO, //(x,y)+ continuous func
91 CONIC_TO, //(x1,y1,x,y)+ " "
92 CONIC_TO_SMOOTH, //(x,y)+ " "
93 CUBIC_TO, //(x1,y1,x2,y2,x,y)+ " "
94 CUBIC_TO_SMOOTH, //(x2,y2,x,y)+ " "
99 //******** CURVE FUNCTIONS *****************
100 const int MAX_SUBDIVISION_SIZE = 64;
101 const int MIN_SUBDIVISION_DRAW_LEVELS = 4;
103 static void Subd_Conic_Stack(Point *arc)
121 arc[4][0] = arc[2][0];
124 a = arc[1][0] = (arc[0][0] + b)/2;
125 b = arc[3][0] = (arc[4][0] + b)/2;
126 arc[2][0] = (a + b)/2;
129 arc[4][1] = arc[2][1];
132 a = arc[1][1] = (arc[0][1] + b)/2;
133 b = arc[3][1] = (arc[4][1] + b)/2;
134 arc[2][1] = (a + b)/2;
140 arc[3] = (arc[2] + arc[1])/2;
141 arc[1] = (arc[0] + arc[1])/2;
143 arc[2] = (arc[1] + arc[3])/2;
149 static void Subd_Cubic_Stack(Point *arc)
158 * 1+2 b * 0+3*1+3*2+3
164 0.1 2.3 -> 0.1 2 3 4 5.6
168 arc[6][0] = arc[3][0];
173 a = arc[1][0] = (arc[0][0] + b)/2;
175 c = arc[5][0] = (arc[6][0] + c)/2;
177 a = arc[2][0] = (a + b)/2;
178 b = arc[4][0] = (b + c)/2;
180 arc[3][0] = (a + b)/2;
183 arc[6][1] = arc[3][1];
188 a = arc[1][1] = (arc[0][1] + b)/2;
190 c = arc[5][1] = (arc[6][1] + c)/2;
192 a = arc[2][1] = (a + b)/2;
193 b = arc[4][1] = (b + c)/2;
195 arc[3][1] = (a + b)/2;
202 //backwards to avoid overwriting
203 arc[5] = (arc[2] + arc[3])/2;
204 temp = (arc[1] + arc[2])/2;
205 arc[1] = (arc[0] + arc[1])/2;
207 arc[4] = (temp + arc[5])/2;
208 arc[2] = (arc[1] + temp)/2;
210 arc[3] = (arc[2] + arc[4])/2;
215 //************** PARAMETRIC RENDERER SUPPORT STRUCTURES ****************
222 vector<Point> pointlist;
224 MonoSegment(int dir = 0, Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
234 int intersect(Real x,Real y) const
236 if((y < aabb.miny) | (y > aabb.maxy) | (x < aabb.minx)) return 0;
237 if(x > aabb.maxx) return ydir;
240 //int size = pointlist.size();
241 //vector<Point>::const_iterator end = pointlist.end();
242 vector<Point>::const_iterator p = pointlist.begin();
244 //assumes that the rect culled away anything that would be beyond the edges
247 while(y > (*++p)[1]);
251 while(y < (*++p)[1]);
254 //for the loop to break there must have been a slope (straight line would do nothing)
255 //vector<Point>::const_iterator p1 = p-1;
256 Real dy = p[-1][1] - p[0][1];
257 Real dx = p[-1][0] - p[0][0];
261 Real xi = p[0][0] + (y - p[0][1]) * dx / dy;
262 return (x > xi)*ydir;
268 Rect aabb; //not necessarily as effective - can only reject values
269 vector<Point> pointlist; //run length - p0, p1, p2, p3 = p10, p11, p12, p13 = p20 ...
270 vector<char> degrees;
272 CurveArray(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
274 aabb.set(x0,y0,x1,y1);
277 void reset(Real x0 = 0, Real x1 = 0, Real y0 = 0, Real y1 = 0)
279 aabb.set(x0,y0,x1,y1);
286 return degrees.size();
291 reset(m[0],m[0],m[1],m[1]);
292 pointlist.push_back(m);
295 void AddCubic(Point p1, Point p2, Point dest)
297 aabb.expand(p1[0],p1[1]);
298 aabb.expand(p2[0],p2[1]);
299 aabb.expand(dest[0],dest[1]);
301 pointlist.push_back(p1);
302 pointlist.push_back(p2);
303 pointlist.push_back(dest);
305 degrees.push_back(3);
308 void AddConic(Point p1, Point dest)
310 aabb.expand(p1[0],p1[1]);
311 aabb.expand(dest[0],dest[1]);
313 pointlist.push_back(p1);
314 pointlist.push_back(dest);
316 degrees.push_back(2);
319 static int intersect_conic(Real x, Real y, Point *p, int level = 0)
321 Real ymin,ymax,xmin,xmax;
324 //sort the overall curve ys - degenerate detection
325 ymin = min(p[0][1],p[2][1]);
326 ymax = max(p[0][1],p[2][1]);
328 xmin = min(min(p[0][0],p[1][0]),p[2][0]);
329 xmax = max(max(p[0][0],p[1][0]),p[2][0]);
331 //to the left, to the right and out of range y, or completely out of range y
332 if( x < xmin ) return 0;
333 if( x > xmax & (y > ymax | y < ymin) ) return 0;
334 if( (y > ymax & y > p[1][1]) | (y < ymin & y < p[1][1]) ) return 0;
336 //degenerate line max
337 if(ymin == ymax == p[1][1])
340 //degenerate accept - to the right and crossing the base line
343 return (y <= ymax & y >= ymin);
346 //solve for curve = y
349 //0 roots - 0 intersection
350 //1 root - get x, and figure out x
351 //2 roots (non-double root) - get 2 xs, and count xs to the left
353 //for conic we can assume 1 intersection for monotonic curve
354 Real a = p[2][1] - 2*p[1][1] + p[0][1],
355 b = 2*p[1][1] - 2*p[0][1],
358 Real t1 = -1, t2 = -1;
363 if(b == 0) return 0; //may not need this check
365 t1 = - c / b; //bt + c = 0 solved
368 //2 degree polynomial
369 Real b2_4ac = b*b - 4*a*c;
371 //if there are double/no roots - no intersections (in real #s that is)
377 b2_4ac = sqrt(b2_4ac);
379 t1 = (-b - b2_4ac) / 2*a,
380 t2 = (-b + b2_4ac) / 2*a;
383 //calculate number of intersections
384 if(t1 >= 0 & t1 <= 1)
387 const Real invt = 1 - t;
389 //find x val and it counts if it's to the left of the point
390 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
391 const Real dy_t = 2*a*t + b;
395 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
399 if(t2 >= 0 & t2 <= 1)
402 const Real invt = 1 - t;
404 //find x val and it counts if it's to the left of the point
405 const Real xi = invt*invt*p[0][0] + 2*t*invt*p[1][0] + t*t*p[2][0];
406 const Real dy_t = 2*a*t + b;
410 intersects += (x >= xi) * ( dy_t > 0 ? 1 : -1);
417 static int quadratic_eqn(Real a, Real b, Real c, Real *t0, Real *t1)
419 const Real b2_4ac = b*b - 4*a*c;
421 //degenerate reject (can't take sqrt)
427 const Real sqrtb2_4ac = sqrt(b2_4ac);
428 const Real signb = b < 0 ? -1 : 1;
429 const Real q = - 0.5 * (b + signb * sqrtb2_4ac);
434 return sqrtb2_4ac == 0 ? 1 : 2;
437 //Newton-Raphson root polishing (we don't care about bounds, assumes very near the desired root)
438 static Real polish_cubicroot(Real a, Real b, Real c, Real d, Real t, Real *dpdt)
440 const Real cn[4] = {a,b,c,d};
441 Real p,dp,newt,oldpmag=FLT_MAX;
443 //eval cubic eqn and it's derivative
449 for(int i = 2; i < 4; i++)
457 synfig::warning("polish_cubicroot: Derivative should not vanish!!!");
463 if(newt == t || fabs(p) >= oldpmag)
474 static int intersect_cubic(Real x, Real y, Point *p, int level = 0)
476 const Real INVALIDROOT = -FLT_MAX;
477 Real ymin,ymax,xmin,xmax;
478 Real ymin2,ymax2,ymintot,ymaxtot;
481 //sort the overall curve ys and xs - degenerate detection
483 //open span for the two end points
484 ymin = min(p[0][1],p[3][1]);
485 ymax = max(p[0][1],p[3][1]);
488 ymin2 = min(p[1][1],p[2][1]);
489 ymax2 = max(p[1][1],p[2][1]);
491 ymintot = min(ymin,ymin2);
492 ymaxtot = max(ymax,ymax2);
494 //the entire curve control polygon is in this x range
495 xmin = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
496 xmax = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][0]));
498 //outside all y boundaries (no intersect)
499 if( (y > ymaxtot) || (y < ymintot) ) return 0;
501 //left of curve (no intersect)
502 if(x < xmin) return 0;
504 //right of curve (and outside base range)
507 if( (y > ymax) | (y < ymin) ) return 0;
509 //degenerate accept - to the right and inside the [ymin,ymax] range (already rejected if out of range)
510 const Real n = p[3][1] - p[0][1];
512 //extract the sign from the value (we need valid data)
513 return n < 0 ? -1 : 1;
516 //degenerate horizontal line max -- doesn't happen enough to check for
517 if( ymintot == ymaxtot ) return 0;
520 // can have 0,1,2, or 3 real roots
521 // if any of them are double then reject the two...
523 // y-coefficients for f_y(t) - y = 0
524 Real a = p[3][1] - 3*p[2][1] + 3*p[1][1] - p[0][1],
525 b = 3*p[2][1] - 6*p[1][1] + 3*p[0][1],
526 c = 3*p[1][1] - 3*p[0][1],
529 Real ax = p[3][0] - 3*p[2][0] + 3*p[1][0] - p[0][0],
530 bx = 3*p[2][0] - 6*p[1][0] + 3*p[0][0],
531 cx = 3*p[1][0] - 3*p[0][0],
534 Real t1 = INVALIDROOT, t2 = INVALIDROOT, t3 = INVALIDROOT, t, dydt;
544 t1 = - d / c; //equation devolved into: ct + d = 0 - solve...
547 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
548 if(quadratic_eqn(a,b,c,&t1,&t2) != 2) return 0;
554 //algorithm courtesy of Numerical Recipes in C (algorithm copied from pg. 184/185)
559 //if cn is 0 (or really really close), then we can simplify this...
564 //0 roots = 0 intersections, 1 root = 2 intersections at the same place (0 effective)
565 if(quadratic_eqn(a,b,c,&t1,&t2) != 2)
567 t1 = t2 = INVALIDROOT;
572 //otherwise run the normal cubic root equation
573 Real Q = (an*an - 3.0*bn) / 9.0;
574 Real R = ((2.0*an*an - 9.0*bn)*an + 27.0*cn)/54.0;
578 Real theta = acos(R / sqrt(Q*Q*Q));
580 t1 = -2.0*sqrt(Q)*cos(theta/3) - an/3.0;
581 t2 = -2.0*sqrt(Q)*cos((theta+2*PI)/3.0) - an/3.0;
582 t3 = -2.0*sqrt(Q)*cos((theta-2*PI)/3.0) - an/3.0;
584 //don't need to reorder,l just need to eliminate double/triple roots
585 //if(t3 == t2 & t1 == t2) t2 = t3 = INVALIDROOT;
586 if(t3 == t2) t2 = t3 = INVALIDROOT;
587 if(t1 == t2) t1 = t2 = INVALIDROOT;
588 if(t1 == t3) t1 = t3 = INVALIDROOT;
591 Real signR = R < 0 ? -1 : 1;
592 Real A = - signR * pow(signR*R + sqrt(R*R - Q*Q*Q),1/3.0);
598 //single real root in this case
599 t1 = (A + B) - an/3.0;
604 //if(t1 != INVALIDROOT)
606 t = t1;//polish_cubicroot(a,b,c,d,t1,&dydt);
609 //const Real invt = 1 - t;
611 //find x val and it counts if it's to the left of the point
612 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
613 dydt = (3*a*t + 2*b)*t + c;
617 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
622 //if(t2 != INVALIDROOT)
624 t = t2;//polish_cubicroot(a,b,c,d,t2,&dydt);
627 //const Real invt = 1 - t;
629 //find x val and it counts if it's to the left of the point
630 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
631 dydt = (3*a*t + 2*b)*t + c;
635 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
640 //if(t3 != INVALIDROOT)
642 t = t3;//polish_cubicroot(a,b,c,d,t3,&dydt);
645 //const Real invt = 1 - t;
647 //find x val and it counts if it's to the left of the point
648 const Real xi = ((ax*t + bx)*t + cx)*t + dx;
649 dydt = (3*a*t + 2*b)*t + c;
653 intersects += (x >= xi) * ( dydt > 0 ? 1 : -1);
661 int intersect(Real x,Real y, Point *table) const
663 if((y < aabb.miny) | (y > aabb.maxy) | (x < aabb.minx)) return 0;
665 int i, curdeg, intersects = 0;
666 const int numcurves = degrees.size();
668 vector<Point>::const_iterator p = pointlist.begin();
670 for(i=0; i < numcurves; i++)
680 table[2] = *p; //we want to include the last point for the next curve
682 intersects += intersect_conic(x,y,table);
692 table[3] = *p; //we want to include the last point for the next curve
694 intersects += intersect_cubic(x,y,table);
701 warning("Invalid degree (%d) inserted into the list (index: %d)\n", curdeg, i);
711 struct Layer_Shape::Intersector
718 enum IntersectorFlags
731 Real close_x,close_y;
733 vector<MonoSegment> segs; //monotonically increasing
734 vector<CurveArray> curves; //big array of consecutive curves
746 return (flags & NotClosed) | (cur_x != close_x) | (cur_y != close_y);
749 void move_to(Real x, Real y)
756 tangent[0] = tangent[1] = 0;
762 }else aabb.expand(x,y);
767 void line_to(Real x, Real y)
769 int dir = (y > cur_y)*1 + (-1)*(y < cur_y);
771 //check for context (if not line start a new segment)
772 //if we're not in line mode (cover's 0 set case), or if directions are different (not valid for 0 direction)
773 if(prim != TYPE_LINE || (dir && segs.back().ydir != dir))
775 MonoSegment seg(dir,x,x,y,y);
777 seg.aabb.expand(cur_x,cur_y);
778 seg.pointlist.push_back(Point(cur_x,cur_y));
779 seg.pointlist.push_back(Point(x,y));
782 //add to the last segment, because it works
785 segs.back().pointlist.push_back(Point(x,y));
786 segs.back().aabb.expand(x,y);
793 aabb.expand(x,y); //expand the entire things bounding box
795 tangent[0] = x - cur_x;
796 tangent[1] = x - cur_y;
802 void conic_to_smooth(Real x, Real y)
804 const Real x1 = tangent[0]/2.0 + cur_x;
805 const Real y1 = tangent[1]/2.0 + cur_y;
810 void conic_to(Real x1, Real y1, Real x, Real y)
812 //if we're not already a curve start one
813 if(prim != TYPE_CURVE)
817 c.Start(Point(cur_x,cur_y));
818 c.AddConic(Point(x1,y1),Point(x,y));
823 curves.back().AddConic(Point(x1,y1),Point(x,y));
832 tangent[0] = 2*(x - x1);
833 tangent[1] = 2*(y - y1);
839 void curve_to_smooth(Real x2, Real y2, Real x, Real y)
841 Real x1 = tangent[0]/3.0 + cur_x;
842 Real y1 = tangent[1]/3.0 + cur_y;
844 curve_to(x1,y1,x2,y2,x,y);
847 void curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
849 //if we're not already a curve start one
850 if(prim != TYPE_CURVE)
854 c.Start(Point(cur_x,cur_y));
855 c.AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
860 curves.back().AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
866 //expand bounding box around ALL of it
871 tangent[0] = 3*(x - x2);
872 tangent[1] = 3*(y - y2);
880 if(flags & NotClosed)
882 if(cur_x != close_x || cur_y != close_y)
884 line_to(close_x,close_y);
891 //assumes the line to count the intersections with is (-1,0)
892 int intersect (Real x, Real y) const
896 vector<MonoSegment>::const_iterator s = segs.begin();
897 vector<CurveArray>::const_iterator c = curves.begin();
899 Point memory[3*MAX_SUBDIVISION_SIZE + 1];
901 for(i = 0; i < segs.size(); i++,s++)
903 inter += s->intersect(x,y);
906 for(i=0; i < curves.size(); i++,c++)
907 inter += c->intersect(x,y,memory);
912 //intersect an arbitrary line
913 //int intersect (Real x, Real y, Real vx, Real vy) {return 0;}
921 cur_x = cur_y = close_x = close_y = 0;
923 tangent[0] = tangent[1] = 0;
928 //*********** SCANLINE RENDERER SUPPORT STRUCTURES ***************
935 PenMark(int xin, int yin, Real c, Real a)
936 :y(yin),x(xin),cover(c),area(a) {}
938 void set(int xin, int yin, Real c, Real a) { y = yin; x = xin; cover = c; area = a; }
940 void setcoord(int xin, int yin) { y = yin; x = xin; }
942 void setcover(Real c, Real a) { cover = c; area = a; }
943 void addcover(Real c, Real a) { cover += c; area += a; }
945 bool operator < (const PenMark &rhs) const
947 return y == rhs.y ? x < rhs.x : y < rhs.y;
951 typedef rect<int> ContextRect;
953 class Layer_Shape::PolySpan
956 typedef deque<PenMark> cover_array;
958 Point arc[3*MAX_SUBDIVISION_SIZE + 1];
965 //ending position of last primitive
969 //starting position of current primitive list
973 //flags for the current segment
976 //the window that will be drawn (used for clipping)
979 //for assignment to flags value
986 //default constructor - 0 everything
987 PolySpan() :current(0,0,0,0),flags(NotSorted)
989 cur_x = cur_y = close_x = close_y = 0;
993 bool notclosed() const
995 return (flags & NotClosed) | (cur_x != close_x) | (cur_y != close_y);
998 //0 out all the variables involved in processing
1002 cur_x = cur_y = close_x = close_y = 0;
1004 current.set(0,0,0,0);
1008 //add the current cell, but only if there is information to add
1011 if(current.cover || current.area)
1013 covers.push_back(current);
1017 //move to the next cell (cover values 0 initially), keeping the current if necessary
1018 void move_pen(int x, int y)
1020 if(y != current.y | x != current.x)
1023 current.set(x,y,0,0);
1027 //close the primitives with a line (or rendering will not work as expected)
1030 if(flags & NotClosed)
1032 if(cur_x != close_x || cur_y != close_y)
1034 line_to(close_x,close_y);
1036 current.setcover(0,0);
1038 flags &= ~NotClosed;
1042 // Not recommended - destroys any separation of spans currently held
1045 sort(covers.begin(),covers.end());
1049 //will sort the marks if they are not sorted
1052 if(flags & NotSorted)
1054 //only sort the open index
1056 current.setcover(0,0);
1058 sort(covers.begin() + open_index,covers.end());
1059 flags &= ~NotSorted;
1063 //encapsulate the current sublist of marks (used for drawing)
1064 void encapsulate_current()
1066 //sort the current list then reposition the open list section
1068 open_index = covers.size();
1071 //move to start a new primitive list (enclose the last primitive if need be)
1072 void move_to(Real x, Real y)
1077 move_pen((int)floor(x),(int)floor(y));
1078 close_y = cur_y = y;
1079 close_x = cur_x = x;
1082 //primitive_to functions
1083 void line_to(Real x, Real y);
1084 void conic_to(Real x1, Real y1, Real x, Real y);
1085 void cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y);
1087 void draw_scanline(int y, Real x1, Real y1, Real x2, Real y2);
1088 void draw_line(Real x1, Real y1, Real x2, Real y2);
1090 Real ExtractAlpha(Real area)
1092 //non-zero winding style
1093 if(area < 0) area = -area;
1094 if(area > 1) area = 1;
1096 //even-odd winding style
1097 /*if(area < 0) area = -area;
1099 while(area > 2) area -= 2;
1100 if(area > 1) area = 1-area; //want pyramid like thing
1102 //broken? - yep broken
1108 /* === M E T H O D S ======================================================= */
1110 Layer_Shape::Layer_Shape(const Real &a, const Color::BlendMethod m):
1111 Layer_Composite (a,m),
1112 edge_table (new Intersector),
1113 color (Color::black()),
1117 blurtype (Blur::FASTGAUSSIAN),
1120 lastbyteop (Primitive::NONE),
1125 Layer_Shape::~Layer_Shape()
1131 Layer_Shape::clear()
1133 edge_table->clear();
1138 Layer_Shape::set_param(const String & param, const ValueBase &value)
1147 return Layer_Composite::set_param(param,value);
1151 Layer_Shape::get_param(const String ¶m)const
1163 return Layer_Composite::get_param(param);
1167 Layer_Shape::get_param_vocab()const
1169 Layer::Vocab ret(Layer_Composite::get_param_vocab());
1171 ret.push_back(ParamDesc("color")
1172 .set_local_name(_("Color"))
1173 .set_description(_("Layer_Shape Color"))
1175 ret.push_back(ParamDesc("offset")
1176 .set_local_name(_("Position"))
1178 ret.push_back(ParamDesc("invert")
1179 .set_local_name(_("Invert"))
1181 ret.push_back(ParamDesc("antialias")
1182 .set_local_name(_("Antialiasing"))
1184 ret.push_back(ParamDesc("feather")
1185 .set_local_name(_("Feather"))
1188 ret.push_back(ParamDesc("blurtype")
1189 .set_local_name(_("Type of Feather"))
1190 .set_description(_("Type of feathering to use"))
1192 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
1193 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
1194 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
1195 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
1196 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
1202 synfig::Layer::Handle
1203 Layer_Shape::hit_check(synfig::Context context, const synfig::Point &p)const
1205 Point pos(p-offset);
1207 int intercepts = edge_table->intersect(pos[0],pos[1]);
1209 // If we have an odd number of intercepts, we are inside.
1210 // If we have an even number of intercepts, we are outside.
1211 bool intersect = ((!!intercepts) ^ invert);
1213 if(get_amount() == 0 || get_blend_method() == Color::BLEND_ALPHA_OVER)
1220 synfig::Layer::Handle tmp;
1221 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(p)))
1223 if(Color::is_onto(get_blend_method()))
1225 //if there's something in the lower layer then we're set...
1226 if(!context.hit_check(p).empty())
1227 return const_cast<Layer_Shape*>(this);
1228 }else if(get_blend_method() == Color::BLEND_ALPHA_OVER)
1230 synfig::info("layer_shape::hit_check - we've got alphaover");
1231 //if there's something in the lower layer then we're set...
1232 if(color.get_a() < 0.1 && get_amount() > .9)
1234 synfig::info("layer_shape::hit_check - can see through us... so nothing");
1236 }else return context.hit_check(p);
1238 return const_cast<Layer_Shape*>(this);
1241 return context.hit_check(p);
1245 Layer_Shape::get_color(Context context, const Point &p)const
1250 pp = Blur(feather,feather,blurtype)(p);
1252 Point pos(pp-offset);
1254 int intercepts = edge_table->intersect(pos[0],pos[1]);
1256 // If we have an odd number of intercepts, we are inside.
1257 // If we have an even number of intercepts, we are outside.
1258 bool intersect = ((!!intercepts) ^ invert);
1261 return context.get_color(pp);
1263 //Ok, we're inside... bummmm ba bum buM...
1264 if(get_blend_method() == Color::BLEND_STRAIGHT && get_amount() == 1)
1267 return Color::blend(color,context.get_color(p),get_amount(),get_blend_method());
1270 //************** SCANLINE RENDERING *********************
1271 void Layer_Shape::PolySpan::line_to(Real x, Real y)
1274 bool afterx = false;
1276 const Real xin(x), yin(y);
1278 Real dx = x - cur_x;
1279 Real dy = y - cur_y;
1283 //outside y - ignore entirely
1284 if( (cur_y >= window.maxy & y >= window.maxy)
1285 |(cur_y < window.miny & y < window.miny) )
1290 else //not degenerate - more complicated
1292 if(dy > 0) //be sure it's not tooooo small
1294 // cur_y ... window.miny ... window.maxy ... y
1296 //initial degenerate - initial clip
1297 if(cur_y < window.miny)
1299 //new clipped start point (must also move pen)
1300 n[2] = cur_x + (window.miny - cur_y) * dx / dy;
1303 cur_y = window.miny;
1304 move_pen((int)floor(cur_x),window.miny);
1307 //generate data for the ending clipped info
1310 //intial line to intersection (and degenerate)
1311 n[2] = x + (window.maxy - y) * dx / dy;
1320 //initial degenerate - initial clip
1321 if(cur_y > window.maxy)
1323 //new clipped start point (must also move pen)
1324 n[2] = cur_x + (window.maxy - cur_y) * dx / dy;
1327 cur_y = window.maxy;
1328 move_pen((int)floor(cur_x),window.maxy);
1331 //generate data for the ending clipped info
1334 //intial line to intersection (and degenerate)
1335 n[2] = x + (window.miny - y) * dx / dy;
1343 //all degenerate - but require bounded clipped values
1344 if( (cur_x >= window.maxx && x >= window.maxx)
1345 ||(cur_x < window.minx && x < window.minx) )
1347 //clip both vertices - but only needed in the x direction
1348 cur_x = max(cur_x, (Real)window.minx);
1349 cur_x = min(cur_x, (Real)window.maxx);
1351 //clip the dest values - y is already clipped
1352 x = max(x,(Real)window.minx);
1353 x = min(x,(Real)window.maxx);
1355 //must start at new point...
1356 move_pen((int)floor(cur_x),(int)floor(cur_y));
1358 draw_line(cur_x,cur_y,x,y);
1368 //initial degenerate - initial clip
1369 if(cur_x < window.minx)
1371 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1372 n[2] = cur_y + (window.minx - cur_x) * dy / dx;
1374 move_pen(window.minx,(int)floor(cur_y));
1375 draw_line(window.minx,cur_y,window.minx,n[2]);
1377 cur_x = window.minx;
1381 //generate data for the ending clipped info
1384 //intial line to intersection (and degenerate)
1385 n[2] = y + (window.maxx - x) * dy / dx;
1397 //initial degenerate - initial clip
1398 if(cur_x > window.maxx)
1400 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1401 n[2] = cur_y + (window.maxx - cur_x) * dy / dx;
1403 move_pen(window.maxx,(int)floor(cur_y));
1404 draw_line(window.maxx,cur_y,window.maxx,n[2]);
1406 cur_x = window.maxx;
1410 //generate data for the ending clipped info
1413 //intial line to intersection (and degenerate)
1414 n[2] = y + (window.minx - x) * dy / dx;
1426 move_pen((int)floor(cur_x),(int)floor(cur_y));
1427 //draw the relevant line (clipped)
1428 draw_line(cur_x,cur_y,x,y);
1432 draw_line(x,y,n[0],n[1]);
1439 } catch(...) { synfig::error("line_to: cur_x=%f, cur_y=%f, x=%f, y=%f", cur_x, cur_y, x, y); throw; }
1441 flags |= NotClosed|NotSorted;
1444 static inline bool clip_conic(const Point *const p, const ContextRect &r)
1446 const Real minx = min(min(p[0][0],p[1][0]),p[2][0]);
1447 const Real miny = min(min(p[0][1],p[1][1]),p[2][1]);
1448 const Real maxx = max(max(p[0][0],p[1][0]),p[2][0]);
1449 const Real maxy = max(max(p[0][1],p[1][1]),p[2][1]);
1451 return (minx > r.maxx) |
1457 static inline bool clip_cubic(const Point *const p, const ContextRect &r)
1459 /*const Real minx = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
1460 const Real miny = min(min(p[0][1],p[1][1]),min(p[2][1],p[3][1]));
1461 const Real maxx = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][1]));
1462 const Real maxy = max(max(p[0][1],p[1][1]),max(p[2][1],p[3][1]));
1464 return (minx > r.maxx) ||
1469 return ((p[0][0] > r.maxx) & (p[1][0] > r.maxx) & (p[2][0] > r.maxx) & (p[3][0] > r.maxx)) |
1470 ((p[0][0] < r.minx) & (p[1][0] < r.minx) & (p[2][0] < r.minx) & (p[3][0] < r.minx)) |
1471 ((p[0][1] > r.maxy) & (p[1][1] > r.maxy) & (p[2][1] > r.maxy) & (p[3][1] > r.maxy)) |
1472 ((p[0][1] < r.miny) & (p[1][1] < r.miny) & (p[2][1] < r.miny) & (p[3][1] < r.miny));
1475 static inline Real max_edges_cubic(const Point *const p)
1477 const Real x1 = p[1][0] - p[0][0];
1478 const Real y1 = p[1][1] - p[0][1];
1480 const Real x2 = p[2][0] - p[1][0];
1481 const Real y2 = p[2][1] - p[1][1];
1483 const Real x3 = p[3][0] - p[2][0];
1484 const Real y3 = p[3][1] - p[2][1];
1486 const Real d1 = x1*x1 + y1*y1;
1487 const Real d2 = x2*x2 + y2*y2;
1488 const Real d3 = x3*x3 + y3*y3;
1490 return max(max(d1,d2),d3);
1493 static inline Real max_edges_conic(const Point *const p)
1495 const Real x1 = p[1][0] - p[0][0];
1496 const Real y1 = p[1][1] - p[0][1];
1498 const Real x2 = p[2][0] - p[1][0];
1499 const Real y2 = p[2][1] - p[1][1];
1501 const Real d1 = x1*x1 + y1*y1;
1502 const Real d2 = x2*x2 + y2*y2;
1507 void Layer_Shape::PolySpan::conic_to(Real x1, Real y1, Real x, Real y)
1509 Point *current = arc;
1512 bool onsecond = false;
1514 arc[0] = Point(x,y);
1515 arc[1] = Point(x1,y1);
1516 arc[2] = Point(cur_x,cur_y);
1518 //just draw the line if it's outside
1519 if(clip_conic(arc,window))
1525 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1526 while(current >= arc)
1528 if(num >= MAX_SUBDIVISION_SIZE)
1530 warning("Curve subdivision somehow ran out of space while tesselating!");
1536 //if the curve is clipping then draw degenerate
1537 if(clip_conic(current,window))
1539 line_to(current[0][0],current[0][1]); //backwards so front is destination
1541 if(onsecond) level--;
1546 //if we are not at the level minimum
1547 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1549 Subd_Conic_Stack(current);
1550 current += 2; //cursor on second curve
1556 //split it again, if it's too big
1557 if(max_edges_conic(current) > 0.25) //distance of .5 (cover no more than half the pixel)
1559 Subd_Conic_Stack(current);
1560 current += 2; //cursor on second curve
1565 else //NOT TOO BIG? RENDER!!!
1567 //cur_x,cur_y = current[2], so we need to go 1,0
1568 line_to(current[1][0],current[1][1]);
1569 line_to(current[0][0],current[0][1]);
1572 if(onsecond) level--;
1579 void Layer_Shape::PolySpan::cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
1581 Point *current = arc;
1584 bool onsecond = false;
1586 arc[0] = Point(x,y);
1587 arc[1] = Point(x2,y2);
1588 arc[2] = Point(x1,y1);
1589 arc[3] = Point(cur_x,cur_y);
1591 //just draw the line if it's outside
1592 if(clip_cubic(arc,window))
1598 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1599 while(current >= arc) //once current goes below arc, there are no more curves left
1601 if(num >= MAX_SUBDIVISION_SIZE)
1603 warning("Curve subdivision somehow ran out of space while tesselating!");
1610 //if we are not at the level minimum
1611 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1613 Subd_Cubic_Stack(current);
1614 current += 3; //cursor on second curve
1620 //if the curve is clipping then draw degenerate
1621 if(clip_cubic(current,window))
1623 line_to(current[0][0],current[0][1]); //backwards so front is destination
1625 if(onsecond) level--;
1631 //split it again, if it's too big
1632 if(max_edges_cubic(current) > 0.25) //could use max_edges<3>
1634 Subd_Cubic_Stack(current);
1635 current += 3; //cursor on second curve
1640 else //NOT TOO BIG? RENDER!!!
1642 //cur_x,cur_y = current[3], so we need to go 2,1,0
1643 line_to(current[2][0],current[2][1]);
1644 line_to(current[1][0],current[1][1]);
1645 line_to(current[0][0],current[0][1]);
1648 if(onsecond) level--;
1655 //******************** LINE ALGORITHMS ****************************
1656 // THESE CALCULATE THE AREA AND THE COVER FOR THE MARKS, TO THEN SCAN CONVERT
1657 // - BROKEN UP INTO SCANLINES (draw_line - y intersections),
1658 // THEN THE COVER AND AREA PER TOUCHED PIXEL IS CALCULATED (draw_scanline - x intersections)
1659 void Layer_Shape::PolySpan::draw_scanline(int y, Real x1, Real fy1, Real x2, Real fy2)
1661 int ix1 = (int)floor(x1);
1662 int ix2 = (int)floor(x2);
1663 Real fx1 = x1 - ix1;
1664 Real fx2 = x2 - ix2;
1666 Real dx,dy,dydx,mult;
1671 //case horizontal line
1674 move_pen(ix2,y); //pen needs to be at the last coord
1678 //case all in same pixel
1679 if(ix1 == ix2) //impossible for degenerate case (covered by the previous cases)
1681 current.addcover(dy,(fx1 + fx2)*dy/2); //horizontal trapazoid area
1687 // ----> fx1...1 0...1 ... 0...1 0...fx2
1690 //set initial values
1691 //Iterate through the covered pixels
1692 mult = (1 - fx1)*dydx; //next y intersection diff value (at 1)
1695 current.addcover(mult,(1 + fx1)*mult/2); // fx1,fy1,1,fy@1 - starting trapazoidal area
1697 //move to the next pixel
1703 //set up for whole ones
1706 //trapezoid(0,y1,1,y1+dydx);
1707 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1709 //move to next pixel (+1)
1716 //final y-pos - last intersect pos
1718 current.addcover(mult,(0+fx2)*mult/2);
1721 // fx2...1 0...1 ... 0...1 0...fx1 <----
1722 //mult = (0 - fx1) * dy / dx;
1723 //neg sign sucked into dydx
1726 //set initial values
1727 //Iterate through the covered pixels
1728 mult = fx1*dydx; //next y intersection diff value
1731 current.addcover(mult,fx1*mult/2); // fx1,fy1,0,fy@0 - starting trapazoidal area
1733 //move to next pixel
1739 //set up for whole ones
1742 //trapezoid(0,y1,1,y1+dydx);
1743 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1745 //move to next pixel (-1)
1752 mult = fy2 - fy1; //final y-pos - last intersect pos
1754 current.addcover(mult,(fx2+1)*mult/2);
1758 void Layer_Shape::PolySpan::draw_line(Real x1, Real y1, Real x2, Real y2)
1760 int iy1 = (int)floor(y1);
1761 int iy2 = (int)floor(y2);
1762 Real fy1 = y1 - iy1;
1763 Real fy2 = y2 - iy2;
1765 assert(!isnan(fy1));
1766 assert(!isnan(fy2));
1768 Real dx,dy,dxdy,mult,x_from,x_to;
1770 const Real SLOPE_EPSILON = 1e-10;
1772 //case all one scanline
1775 draw_scanline(iy1,x1,y1,x2,y2);
1783 //case vertical line
1784 if(dx < SLOPE_EPSILON && dx > -SLOPE_EPSILON)
1786 //calc area and cover on vertical line
1789 // ----> fx1...1 0...1 ... 0...1 0...fx2
1792 int ix1 = (int)floor(x1);
1793 Real fx1 = x1 - ix1;
1798 current.addcover(sub,fx1*sub);
1803 //move pen to next pixel
1809 current.addcover(1,fx1);
1817 current.addcover(fy2,fy2*fx1);
1822 int ix1 = (int)floor(x1);
1823 Real fx1 = x1 - ix1;
1828 current.addcover(sub,fx1*sub);
1837 //accumulate in current pixel
1838 current.addcover(-1,-fx1);
1845 current.addcover(fy2-1,(fy2-1)*fx1);
1850 //case normal line - guaranteed dx != 0 && dy != 0
1852 //calculate the initial intersection with "next" scanline
1857 mult = (1 - fy1) * dxdy;
1859 //x interset scanline
1861 draw_scanline(iy1,x1,fy1,x_from,1);
1866 move_pen((int)floor(x_from),iy1);
1870 //keep up on the x axis, and render the current scanline
1871 x_to = x_from + dxdy;
1872 draw_scanline(iy1,x_from,0,x_to,1);
1875 //move to next pixel
1877 move_pen((int)floor(x_from),iy1);
1880 //draw the last one, fractional
1881 draw_scanline(iy2,x_from,0,x2,fy2);
1889 //x interset scanline
1891 draw_scanline(iy1,x1,fy1,x_from,0);
1896 move_pen((int)floor(x_from),iy1);
1900 x_to = x_from + dxdy;
1901 draw_scanline(iy1,x_from,1,x_to,0);
1905 move_pen((int)floor(x_from),iy1);
1907 //draw the last one, fractional
1908 draw_scanline(iy2,x_from,1,x2,fy2);
1912 //****** LAYER PEN OPERATIONS (move_to, line_to, etc.) ******
1913 void Layer_Shape::move_to(Real x, Real y)
1915 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1919 op.operation = Primitive::MOVE_TO;
1920 op.number = 1; //one point for now
1922 if(lastbyteop == Primitive::MOVE_TO)
1924 char *ptr = &bytestream[lastoppos];
1925 memcpy(ptr,&op,sizeof(op));
1926 memcpy(ptr+sizeof(op),&p,sizeof(p));
1928 else //make a new op
1930 lastbyteop = Primitive::MOVE_TO;
1931 lastoppos = bytestream.size();
1933 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
1934 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
1937 edge_table->move_to(x,y);
1940 void Layer_Shape::close()
1944 op.operation = Primitive::CLOSE;
1947 if(lastbyteop == Primitive::CLOSE)
1951 lastbyteop = Primitive::CLOSE;
1952 lastoppos = bytestream.size();
1954 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert header
1957 edge_table->close();
1958 //should not affect the bounding box since it would just be returning to old point...
1961 void Layer_Shape::endpath()
1965 op.operation = Primitive::END;
1968 if(lastbyteop == Primitive::END | lastbyteop == Primitive::NONE)
1972 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1));
1974 //should not affect the bounding box since it would just be returning to old point... if at all
1977 void Layer_Shape::line_to(Real x, Real y)
1982 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1986 op.operation = Primitive::LINE_TO;
1987 op.number = 1; //one point for now
1989 if(lastbyteop == Primitive::MOVE_TO | lastbyteop == Primitive::LINE_TO)
1991 //only need to insert the point
1992 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
1994 Primitive * prim = (Primitive *)&bytestream[lastoppos];
1995 prim->number++; //increment number of points in the list
1998 lastbyteop = Primitive::LINE_TO;
1999 lastoppos = bytestream.size();
2001 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2002 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2005 edge_table->line_to(x,y);
2008 void Layer_Shape::conic_to(Real x1, Real y1, Real x, Real y)
2010 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*2;
2015 op.operation = Primitive::CONIC_TO;
2016 op.number = 2; //2 points for now
2018 if(lastbyteop == Primitive::CONIC_TO)
2020 //only need to insert the new points
2021 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2022 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2024 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2025 prim->number += 2; //increment number of points in the list
2028 lastbyteop = Primitive::CONIC_TO;
2029 lastoppos = bytestream.size();
2031 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2032 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2033 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2036 edge_table->conic_to(x1,y1,x,y);
2039 void Layer_Shape::conic_to_smooth(Real x, Real y) //x1,y1 derived from current tangent
2041 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2045 op.operation = Primitive::CONIC_TO_SMOOTH;
2046 op.number = 1; //2 points for now
2048 if(lastbyteop == Primitive::CONIC_TO_SMOOTH)
2050 //only need to insert the new point
2051 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2053 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2054 prim->number += 1; //increment number of points in the list
2057 lastbyteop = Primitive::CONIC_TO_SMOOTH;
2058 lastoppos = bytestream.size();
2060 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2061 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2064 edge_table->conic_to_smooth(x,y);
2067 void Layer_Shape::curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
2069 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2075 op.operation = Primitive::CUBIC_TO;
2076 op.number = 3; //3 points for now
2078 if(lastbyteop == Primitive::CUBIC_TO)
2080 //only need to insert the new points
2081 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2082 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2083 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2085 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2086 prim->number += 3; //increment number of points in the list
2089 lastbyteop = Primitive::CUBIC_TO;
2090 lastoppos = bytestream.size();
2092 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2093 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2094 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2095 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2098 edge_table->curve_to(x1,y1,x2,y2,x,y);
2101 void Layer_Shape::curve_to_smooth(Real x2, Real y2, Real x, Real y) //x1,y1 derived from current tangent
2103 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2108 op.operation = Primitive::CUBIC_TO_SMOOTH;
2109 op.number = 2; //3 points for now
2111 if(lastbyteop == Primitive::CUBIC_TO_SMOOTH)
2113 //only need to insert the new points
2114 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2115 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2117 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2118 prim->number += 2; //increment number of points in the list
2121 lastbyteop = Primitive::CUBIC_TO_SMOOTH;
2122 lastoppos = bytestream.size();
2124 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2125 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2126 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2130 // ACCELERATED RENDER FUNCTION - TRANSLATE BYTE CODE INTO FUNCTION CALLS
2132 bool Layer_Shape::render_polyspan(Surface *surface, PolySpan &polyspan,
2133 Color::BlendMethod got_blend_method, Color::value_type got_amount) const
2135 Surface::alpha_pen p(surface->begin(),got_amount,_BlendFunc(got_blend_method));
2136 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2137 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2139 Real cover,area,alpha;
2146 if(cur_mark == end_mark)
2151 p.move_to(polyspan.window.minx,polyspan.window.miny);
2152 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2157 //fill initial rect / line
2160 //fill all the area above the first vertex
2161 p.move_to(polyspan.window.minx,polyspan.window.miny);
2162 y = polyspan.window.miny;
2163 int l = polyspan.window.maxx - polyspan.window.minx;
2165 p.put_block(cur_mark->y - polyspan.window.miny,l);
2167 //fill the area to the left of the first vertex on that line
2168 l = cur_mark->x - polyspan.window.minx;
2169 p.move_to(polyspan.window.minx,cur_mark->y);
2170 if(l) p.put_hline(l);
2180 area = cur_mark->area;
2181 cover += cur_mark->cover;
2183 //accumulate for the current pixel
2184 while(++cur_mark != polyspan.covers.end())
2186 if(y != cur_mark->y | x != cur_mark->x)
2189 area += cur_mark->area;
2190 cover += cur_mark->cover;
2193 //draw pixel - based on covered area
2194 if(area) //if we're ok, draw the current pixel
2196 alpha = polyspan.ExtractAlpha(cover - area);
2197 if(invert) alpha = 1 - alpha;
2201 if(alpha >= .5) p.put_value();
2203 else if(alpha) p.put_value_alpha(alpha);
2209 //if we're done, don't use iterator and exit
2210 if(cur_mark == end_mark) break;
2212 //if there is no more live pixels on this line, goto next
2213 if(y != cur_mark->y)
2217 //fill the area at the end of the line
2218 p.put_hline(polyspan.window.maxx - x);
2220 //fill area at the beginning of the next line
2221 p.move_to(polyspan.window.minx,cur_mark->y);
2222 p.put_hline(cur_mark->x - polyspan.window.minx);
2230 //draw span to next pixel - based on total amount of pixel cover
2233 alpha = polyspan.ExtractAlpha(cover);
2234 if(invert) alpha = 1 - alpha;
2238 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2240 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2244 //fill the after stuff
2247 //fill the area at the end of the line
2248 p.put_hline(polyspan.window.maxx - x);
2250 //fill area at the beginning of the next line
2251 p.move_to(polyspan.window.minx,y+1);
2252 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2258 bool Layer_Shape::render_polyspan(etl::surface<float> *surface, PolySpan &polyspan) const
2260 etl::surface<float>::pen p(surface->begin());
2261 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2262 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2264 Real cover,area,alpha;
2270 //the pen always writes 1 (unless told to do otherwise)
2273 if(cur_mark == end_mark)
2278 p.move_to(polyspan.window.minx,polyspan.window.miny);
2279 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2284 //fill initial rect / line
2287 //fill all the area above the first vertex
2288 p.move_to(polyspan.window.minx,polyspan.window.miny);
2289 y = polyspan.window.miny;
2290 int l = polyspan.window.maxx - polyspan.window.minx;
2292 p.put_block(cur_mark->y - polyspan.window.miny,l);
2294 //fill the area to the left of the first vertex on that line
2295 l = cur_mark->x - polyspan.window.minx;
2296 p.move_to(polyspan.window.minx,cur_mark->y);
2297 if(l) p.put_hline(l);
2306 area = cur_mark->area;
2307 cover += cur_mark->cover;
2309 //accumulate for the current pixel
2310 while(++cur_mark != polyspan.covers.end())
2312 if(y != cur_mark->y | x != cur_mark->x)
2315 area += cur_mark->area;
2316 cover += cur_mark->cover;
2319 //draw pixel - based on covered area
2320 if(area) //if we're ok, draw the current pixel
2322 alpha = 1 - polyspan.ExtractAlpha(cover - area);
2325 if(alpha >= .5) p.put_value();
2327 else if(alpha) p.put_value(alpha);
2333 //if we're done, don't use iterator and exit
2334 if(cur_mark == end_mark) break;
2336 //if there is no more live pixels on this line, goto next
2337 if(y != cur_mark->y)
2339 //fill the area at the end of the line
2340 p.put_hline(polyspan.window.maxx - x);
2342 //fill area at the beginning of the next line
2343 p.move_to(polyspan.window.minx,cur_mark->y);
2344 p.put_hline(cur_mark->x - polyspan.window.minx);
2351 //draw span to next pixel - based on total amount of pixel cover
2354 alpha = 1 - polyspan.ExtractAlpha(cover);
2357 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2359 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2363 //fill the area at the end of the line
2364 p.put_hline(polyspan.window.maxx - x);
2366 //fill area at the beginning of the next line
2367 p.move_to(polyspan.window.minx,y+1);
2368 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2378 area = cur_mark->area;
2379 cover += cur_mark->cover;
2381 //accumulate for the current pixel
2382 while(++cur_mark != polyspan.covers.end())
2384 if(y != cur_mark->y | x != cur_mark->x)
2387 area += cur_mark->area;
2388 cover += cur_mark->cover;
2391 //draw pixel - based on covered area
2392 if(area) //if we're ok, draw the current pixel
2394 alpha = polyspan.ExtractAlpha(cover - area);
2397 if(alpha >= .5) p.put_value();
2399 else if(alpha) p.put_value(alpha);
2405 //if we're done, don't use iterator and exit
2406 if(cur_mark == end_mark) break;
2408 //if there is no more live pixels on this line, goto next
2409 if(y != cur_mark->y)
2416 //draw span to next pixel - based on total amount of pixel cover
2419 alpha = polyspan.ExtractAlpha(cover);
2422 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2424 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2433 Layer_Shape::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
2435 const unsigned int w = renddesc.get_w();
2436 const unsigned int h = renddesc.get_h();
2438 const Real pw = abs(renddesc.get_pw());
2439 const Real ph = abs(renddesc.get_ph());
2441 //const Real OFFSET_EPSILON = 1e-8;
2442 SuperCallback stageone(cb,1,10000,15001+renddesc.get_h());
2443 SuperCallback stagetwo(cb,10000,10001+renddesc.get_h(),15001+renddesc.get_h());
2444 SuperCallback stagethree(cb,10001+renddesc.get_h(),15001+renddesc.get_h(),15001+renddesc.get_h());
2446 // Render what is behind us
2448 //clip if it satisfies the invert solid thing
2449 if(is_solid_color() && invert)
2451 Rect aabb = edge_table->aabb;
2452 Point tl = renddesc.get_tl() - offset;
2454 Real pw = renddesc.get_pw(),
2455 ph = renddesc.get_ph();
2459 Real pixelfeatherx = abs(feather/pw),
2460 pixelfeathery = abs(feather/ph);
2462 nrect.set_point((aabb.minx - tl[0])/pw,(aabb.miny - tl[1])/ph);
2463 nrect.expand((aabb.maxx - tl[0])/pw,(aabb.maxy - tl[1])/ph);
2465 RendDesc optdesc(renddesc);
2467 //make sure to expand so we gain subpixels rather than lose them
2468 nrect.minx = floor(nrect.minx-pixelfeatherx); nrect.miny = floor(nrect.miny-pixelfeathery);
2469 nrect.maxx = ceil(nrect.maxx+pixelfeatherx); nrect.maxy = ceil(nrect.maxy+pixelfeathery);
2471 //make sure the subwindow is clipped with our tile window (minimize useless drawing)
2472 set_intersect(nrect,nrect,Rect(0,0,renddesc.get_w(),renddesc.get_h()));
2474 //must resize the surface first
2475 surface->set_wh(renddesc.get_w(),renddesc.get_h());
2478 //only render anything if it's visible from our current tile
2481 //set the subwindow to the viewable pixels and render it to the subsurface
2482 optdesc.set_subwindow((int)nrect.minx, (int)nrect.miny,
2483 (int)(nrect.maxx - nrect.minx), (int)(nrect.maxy - nrect.miny));
2485 Surface optimizedbacksurf;
2486 if(!context.accelerated_render(&optimizedbacksurf,quality,optdesc,&stageone))
2489 //blit that onto the original surface so we can pretend that nothing ever happened
2490 Surface::pen p = surface->get_pen((int)nrect.minx,(int)nrect.miny);
2491 optimizedbacksurf.blit_to(p);
2495 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
2499 if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
2503 //we have to blur rather than be crappy
2505 //so make a separate surface
2506 RendDesc workdesc(renddesc);
2508 etl::surface<float> shapesurface;
2510 //the expanded size = 1/2 the size in each direction rounded up
2511 int halfsizex = (int) (abs(feather*.5/pw) + 3),
2512 halfsizey = (int) (abs(feather*.5/ph) + 3);
2514 //expand by 1/2 size in each direction on either side
2521 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2524 case Blur::FASTGAUSSIAN:
2531 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2534 case Blur::GAUSSIAN:
2536 #define GAUSSIAN_ADJUSTMENT (0.05)
2537 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
2538 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
2543 halfsizex = (int)(abs(pw)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2544 halfsizey = (int)(abs(ph)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2546 halfsizex = (halfsizex + 1)/2;
2547 halfsizey = (halfsizey + 1)/2;
2548 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
2554 shapesurface.set_wh(workdesc.get_w(),workdesc.get_h());
2555 shapesurface.clear();
2558 if(!render_shape(&shapesurface,quality,workdesc,&stagetwo))return false;
2561 Blur(feather,feather,blurtype,&stagethree)(shapesurface,workdesc.get_br()-workdesc.get_tl(),shapesurface);
2563 //blend with stuff below it...
2564 unsigned int u = halfsizex, v = halfsizey, x = 0, y = 0;
2565 for(y = 0; y < h; y++,v++)
2568 for(x = 0; x < w; x++,u++)
2570 float a = shapesurface[v][u];
2573 //a = floor(a*255+0.5f)/255;
2574 (*surface)[y][x]=Color::blend(color,(*surface)[y][x],a*get_amount(),get_blend_method());
2576 //else (*surface)[y][x] = worksurface[v][u];
2581 if(cb && !cb->amount_complete(100,100))
2583 synfig::warning("Layer_Shape: could not set amount complete");
2590 //might take out to reduce code size
2591 return render_shape(surface,true,quality,renddesc,&stagetwo);
2597 Layer_Shape::render_shape(Surface *surface,bool useblend,int quality,
2598 const RendDesc &renddesc, ProgressCallback *cb)const
2602 SuperCallback progress(cb,0,renddesc.get_h(),renddesc.get_h());
2604 // If our amount is set to zero, no need to render anything
2608 //test new polygon renderer
2610 // Width and Height of a pixel
2611 const int w = renddesc.get_w();
2612 const int h = renddesc.get_h();
2613 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2614 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2616 const Point tl = renddesc.get_tl();
2618 Vector tangent (0,0);
2622 //optimization for tesselating only inside tiles
2623 span.window.minx = 0;
2624 span.window.miny = 0;
2625 span.window.maxx = w;
2626 span.window.maxy = h;
2628 //pointers for processing the bytestream
2629 const char *current = &bytestream[0];
2630 const char *end = &bytestream[bytestream.size()];
2632 int operation = Primitive::NONE;
2639 Real x,y,x1,y1,x2,y2;
2642 while(current < end)
2648 //get the op code safely
2649 curprim = (Primitive *)current;
2651 //advance past indices
2652 current += sizeof(Primitive);
2655 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2659 //get the relevant data
2660 operation = curprim->operation;
2661 number = curprim->number;
2663 if(operation == Primitive::END)
2666 if(operation == Primitive::CLOSE)
2668 if(span.notclosed())
2670 tangent[0] = span.close_x - span.cur_x;
2671 tangent[1] = span.close_y - span.cur_y;
2677 data = (Point*)current;
2678 current += sizeof(Point)*number;
2680 //check data positioning
2683 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2687 } catch(...) { synfig::error("Layer_Shape::render_shape()1: Caught an exception after %d loops, rethrowing...", tmp); throw; }
2689 //transfer all the data - RLE optimized
2690 for(curnum=0; curnum < number;)
2694 case Primitive::MOVE_TO:
2696 x = data[curnum][0];
2697 x = (x - tl[0] + offset[0])*pw;
2698 y = data[curnum][1];
2699 y = (y - tl[1] + offset[1])*ph;
2710 tangent[0] = x - span.cur_x;
2711 tangent[1] = y - span.cur_y;
2716 curnum++; //only advance one point
2721 case Primitive::LINE_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;
2728 tangent[0] = x - span.cur_x;
2729 tangent[1] = y - span.cur_y;
2736 case Primitive::CONIC_TO:
2738 x = data[curnum+1][0];
2739 x = (x - tl[0] + offset[0])*pw;
2740 y = data[curnum+1][1];
2741 y = (y - tl[1] + offset[1])*ph;
2743 x1 = data[curnum][0];
2744 x1 = (x1 - tl[0] + offset[0])*pw;
2745 y1 = data[curnum][1];
2746 y1 = (y1 - tl[1] + offset[1])*ph;
2748 tangent[0] = 2*(x - x1);
2749 tangent[1] = 2*(y - y1);
2751 span.conic_to(x1,y1,x,y);
2756 case Primitive::CONIC_TO_SMOOTH:
2758 x = data[curnum][0];
2759 x = (x - tl[0] + offset[0])*pw;
2760 y = data[curnum][1];
2761 y = (y - tl[1] + offset[1])*ph;
2763 x1 = span.cur_x + tangent[0]/2;
2764 y1 = span.cur_y + tangent[1]/2;
2766 tangent[0] = 2*(x - x1);
2767 tangent[1] = 2*(y - y1);
2769 span.conic_to(x1,y1,x,y);
2775 case Primitive::CUBIC_TO:
2777 x = data[curnum+2][0];
2778 x = (x - tl[0] + offset[0])*pw;
2779 y = data[curnum+2][1];
2780 y = (y - tl[1] + offset[1])*ph;
2782 x2 = data[curnum+1][0];
2783 x2 = (x2 - tl[0] + offset[0])*pw;
2784 y2 = data[curnum+1][1];
2785 y2 = (y2 - tl[1] + offset[1])*ph;
2787 x1 = data[curnum][0];
2788 x1 = (x1 - tl[0] + offset[0])*pw;
2789 y1 = data[curnum][1];
2790 y1 = (y1 - tl[1] + offset[1])*ph;
2792 tangent[0] = 2*(x - x2);
2793 tangent[1] = 2*(y - y2);
2795 span.cubic_to(x1,y1,x2,y2,x,y);
2801 case Primitive::CUBIC_TO_SMOOTH:
2803 x = data[curnum+1][0];
2804 x = (x - tl[0] + offset[0])*pw;
2805 y = data[curnum+1][1];
2806 y = (y - tl[1] + offset[1])*ph;
2808 x2 = data[curnum][0];
2809 x2 = (x2 - tl[0] + offset[0])*pw;
2810 y2 = data[curnum][1];
2811 y2 = (y2 - tl[1] + offset[1])*ph;
2813 x1 = span.cur_x + tangent[0]/3.0;
2814 y1 = span.cur_y + tangent[1]/3.0;
2816 tangent[0] = 2*(x - x2);
2817 tangent[1] = 2*(y - y2);
2819 span.cubic_to(x1,y1,x2,y2,x,y);
2828 //sort the bastards so we can render everything
2831 return render_polyspan(surface, span,
2832 useblend?get_blend_method():Color::BLEND_STRAIGHT,
2833 useblend?get_amount():1.0);
2837 Layer_Shape::render_shape(surface<float> *surface,int quality,
2838 const RendDesc &renddesc, ProgressCallback *cb)const
2840 // If our amount is set to zero, no need to render anything
2844 //test new polygon renderer
2846 // Width and Height of a pixel
2847 const int w = renddesc.get_w();
2848 const int h = renddesc.get_h();
2849 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2850 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2852 const Point tl = renddesc.get_tl();
2854 Vector tangent (0,0);
2858 //optimization for tesselating only inside tiles
2859 span.window.minx = 0;
2860 span.window.miny = 0;
2861 span.window.maxx = w;
2862 span.window.maxy = h;
2864 //pointers for processing the bytestream
2865 const char *current = &bytestream[0];
2866 const char *end = &bytestream[bytestream.size()];
2868 int operation = Primitive::NONE;
2875 Real x,y,x1,y1,x2,y2;
2877 while(current < end)
2879 //get the op code safely
2880 curprim = (Primitive *)current;
2882 //advance past indices
2883 current += sizeof(Primitive);
2886 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2890 //get the relevant data
2891 operation = curprim->operation;
2892 number = curprim->number;
2894 if(operation == Primitive::END)
2897 if(operation == Primitive::CLOSE)
2899 if(span.notclosed())
2901 tangent[0] = span.close_x - span.cur_x;
2902 tangent[1] = span.close_y - span.cur_y;
2908 data = (Point*)current;
2909 current += sizeof(Point)*number;
2911 //check data positioning
2914 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2918 //transfer all the data
2919 for(curnum=0; curnum < number;)
2923 case Primitive::MOVE_TO:
2925 x = data[curnum][0];
2926 x = (x - tl[0] + offset[0])*pw;
2927 y = data[curnum][1];
2928 y = (y - tl[1] + offset[1])*ph;
2939 tangent[0] = x - span.cur_x;
2940 tangent[1] = y - span.cur_y;
2945 curnum++; //only advance one point
2950 case Primitive::LINE_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;
2957 tangent[0] = x - span.cur_x;
2958 tangent[1] = y - span.cur_y;
2965 case Primitive::CONIC_TO:
2967 x = data[curnum+1][0];
2968 x = (x - tl[0] + offset[0])*pw;
2969 y = data[curnum+1][1];
2970 y = (y - tl[1] + offset[1])*ph;
2972 x1 = data[curnum][0];
2973 x1 = (x1 - tl[0] + offset[0])*pw;
2974 y1 = data[curnum][1];
2975 y1 = (y1 - tl[1] + offset[1])*ph;
2977 tangent[0] = 2*(x - x1);
2978 tangent[1] = 2*(y - y1);
2980 span.conic_to(x1,y1,x,y);
2985 case Primitive::CONIC_TO_SMOOTH:
2987 x = data[curnum][0];
2988 x = (x - tl[0] + offset[0])*pw;
2989 y = data[curnum][1];
2990 y = (y - tl[1] + offset[1])*ph;
2992 x1 = span.cur_x + tangent[0]/2;
2993 y1 = span.cur_y + tangent[1]/2;
2995 tangent[0] = 2*(x - x1);
2996 tangent[1] = 2*(y - y1);
2998 span.conic_to(x1,y1,x,y);
3004 case Primitive::CUBIC_TO:
3006 x = data[curnum+2][0];
3007 x = (x - tl[0] + offset[0])*pw;
3008 y = data[curnum+2][1];
3009 y = (y - tl[1] + offset[1])*ph;
3011 x2 = data[curnum+1][0];
3012 x2 = (x2 - tl[0] + offset[0])*pw;
3013 y2 = data[curnum+1][1];
3014 y2 = (y2 - tl[1] + offset[1])*ph;
3016 x1 = data[curnum][0];
3017 x1 = (x1 - tl[0] + offset[0])*pw;
3018 y1 = data[curnum][1];
3019 y1 = (y1 - tl[1] + offset[1])*ph;
3021 tangent[0] = 2*(x - x2);
3022 tangent[1] = 2*(y - y2);
3024 span.cubic_to(x1,y1,x2,y2,x,y);
3030 case Primitive::CUBIC_TO_SMOOTH:
3032 x = data[curnum+1][0];
3033 x = (x - tl[0] + offset[0])*pw;
3034 y = data[curnum+1][1];
3035 y = (y - tl[1] + offset[1])*ph;
3037 x2 = data[curnum][0];
3038 x2 = (x2 - tl[0] + offset[0])*pw;
3039 y2 = data[curnum][1];
3040 y2 = (y2 - tl[1] + offset[1])*ph;
3042 x1 = span.cur_x + tangent[0]/3.0;
3043 y1 = span.cur_y + tangent[1]/3.0;
3045 tangent[0] = 2*(x - x2);
3046 tangent[1] = 2*(y - y2);
3048 span.cubic_to(x1,y1,x2,y2,x,y);
3057 //sort the bastards so we can render everything
3060 return render_polyspan(surface, span);
3064 Layer_Shape::get_bounding_rect()const
3067 return Rect::full_plane();
3069 Rect bounds(edge_table->aabb+offset);
3070 bounds.expand(max((bounds.get_min()-bounds.get_max()).mag()*0.01,feather));