1 /* === S Y N F I G ========================================================= */
2 /*! \file layer_shape.cpp
3 ** \brief Template Header
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$");
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
715 //! true iff aabb hasn't been initialised yet
720 enum IntersectorFlags
733 Real close_x,close_y;
735 vector<MonoSegment> segs; //monotonically increasing
736 vector<CurveArray> curves; //big array of consecutive curves
748 return (flags & NotClosed) | (cur_x != close_x) | (cur_y != close_y);
751 void move_to(Real x, Real y)
758 tangent[0] = tangent[1] = 0;
764 }else aabb.expand(x,y);
769 void line_to(Real x, Real y)
771 int dir = (y > cur_y)*1 + (-1)*(y < cur_y);
773 //check for context (if not line start a new segment)
774 //if we're not in line mode (covers 0 set case), or if directions are different (not valid for 0 direction)
775 if(prim != TYPE_LINE || (dir && segs.back().ydir != dir))
777 MonoSegment seg(dir,x,x,y,y);
779 seg.aabb.expand(cur_x,cur_y);
780 seg.pointlist.push_back(Point(cur_x,cur_y));
781 seg.pointlist.push_back(Point(x,y));
784 //add to the last segment, because it works
787 segs.back().pointlist.push_back(Point(x,y));
788 segs.back().aabb.expand(x,y);
795 aabb.expand(x,y); //expand the entire thing's bounding box
797 tangent[0] = x - cur_x;
798 tangent[1] = x - cur_y;
804 void conic_to_smooth(Real x, Real y)
806 const Real x1 = tangent[0]/2.0 + cur_x;
807 const Real y1 = tangent[1]/2.0 + cur_y;
812 void conic_to(Real x1, Real y1, Real x, Real y)
814 //if we're not already a curve start one
815 if(prim != TYPE_CURVE)
819 c.Start(Point(cur_x,cur_y));
820 c.AddConic(Point(x1,y1),Point(x,y));
825 curves.back().AddConic(Point(x1,y1),Point(x,y));
834 tangent[0] = 2*(x - x1);
835 tangent[1] = 2*(y - y1);
841 void curve_to_smooth(Real x2, Real y2, Real x, Real y)
843 Real x1 = tangent[0]/3.0 + cur_x;
844 Real y1 = tangent[1]/3.0 + cur_y;
846 curve_to(x1,y1,x2,y2,x,y);
849 void curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
851 //if we're not already a curve start one
852 if(prim != TYPE_CURVE)
856 c.Start(Point(cur_x,cur_y));
857 c.AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
862 curves.back().AddCubic(Point(x1,y1),Point(x2,y2),Point(x,y));
868 //expand bounding box around ALL of it
873 tangent[0] = 3*(x - x2);
874 tangent[1] = 3*(y - y2);
882 if(flags & NotClosed)
884 if(cur_x != close_x || cur_y != close_y)
886 line_to(close_x,close_y);
893 //assumes the line to count the intersections with is (-1,0)
894 int intersect (Real x, Real y) const
898 vector<MonoSegment>::const_iterator s = segs.begin();
899 vector<CurveArray>::const_iterator c = curves.begin();
901 Point memory[3*MAX_SUBDIVISION_SIZE + 1];
903 for(i = 0; i < segs.size(); i++,s++)
905 inter += s->intersect(x,y);
908 for(i=0; i < curves.size(); i++,c++)
909 inter += c->intersect(x,y,memory);
914 //intersect an arbitrary line
915 //int intersect (Real x, Real y, Real vx, Real vy) {return 0;}
923 cur_x = cur_y = close_x = close_y = 0;
925 tangent[0] = tangent[1] = 0;
930 //*********** SCANLINE RENDERER SUPPORT STRUCTURES ***************
937 PenMark(int xin, int yin, Real c, Real a)
938 :y(yin),x(xin),cover(c),area(a) {}
940 void set(int xin, int yin, Real c, Real a) { y = yin; x = xin; cover = c; area = a; }
942 void setcoord(int xin, int yin) { y = yin; x = xin; }
944 void setcover(Real c, Real a) { cover = c; area = a; }
945 void addcover(Real c, Real a) { cover += c; area += a; }
947 bool operator < (const PenMark &rhs) const
949 return y == rhs.y ? x < rhs.x : y < rhs.y;
953 typedef rect<int> ContextRect;
955 class Layer_Shape::PolySpan
958 typedef deque<PenMark> cover_array;
960 Point arc[3*MAX_SUBDIVISION_SIZE + 1];
967 //ending position of last primitive
971 //starting position of current primitive list
975 //flags for the current segment
978 //the window that will be drawn (used for clipping)
981 //for assignment to flags value
988 //default constructor - 0 everything
989 PolySpan() :current(0,0,0,0),flags(NotSorted)
991 cur_x = cur_y = close_x = close_y = 0;
995 bool notclosed() const
997 return (flags & NotClosed) | (cur_x != close_x) | (cur_y != close_y);
1000 //0 out all the variables involved in processing
1004 cur_x = cur_y = close_x = close_y = 0;
1006 current.set(0,0,0,0);
1010 //add the current cell, but only if there is information to add
1013 if(current.cover || current.area)
1015 covers.push_back(current);
1019 //move to the next cell (cover values 0 initially), keeping the current if necessary
1020 void move_pen(int x, int y)
1022 if(y != current.y | x != current.x)
1025 current.set(x,y,0,0);
1029 //close the primitives with a line (or rendering will not work as expected)
1032 if(flags & NotClosed)
1034 if(cur_x != close_x || cur_y != close_y)
1036 line_to(close_x,close_y);
1038 current.setcover(0,0);
1040 flags &= ~NotClosed;
1044 // Not recommended - destroys any separation of spans currently held
1047 sort(covers.begin(),covers.end());
1051 //will sort the marks if they are not sorted
1054 if(flags & NotSorted)
1056 //only sort the open index
1058 current.setcover(0,0);
1060 sort(covers.begin() + open_index,covers.end());
1061 flags &= ~NotSorted;
1065 //encapsulate the current sublist of marks (used for drawing)
1066 void encapsulate_current()
1068 //sort the current list then reposition the open list section
1070 open_index = covers.size();
1073 //move to start a new primitive list (enclose the last primitive if need be)
1074 void move_to(Real x, Real y)
1079 move_pen((int)floor(x),(int)floor(y));
1080 close_y = cur_y = y;
1081 close_x = cur_x = x;
1084 //primitive_to functions
1085 void line_to(Real x, Real y);
1086 void conic_to(Real x1, Real y1, Real x, Real y);
1087 void cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y);
1089 void draw_scanline(int y, Real x1, Real y1, Real x2, Real y2);
1090 void draw_line(Real x1, Real y1, Real x2, Real y2);
1092 Real ExtractAlpha(Real area)
1094 //non-zero winding style
1095 if(area < 0) area = -area;
1096 if(area > 1) area = 1;
1098 //even-odd winding style
1099 /*if(area < 0) area = -area;
1101 while(area > 2) area -= 2;
1102 if(area > 1) area = 1-area; //want pyramid like thing
1104 //broken? - yep broken
1110 /* === M E T H O D S ======================================================= */
1112 Layer_Shape::Layer_Shape(const Real &a, const Color::BlendMethod m):
1113 Layer_Composite (a,m),
1114 edge_table (new Intersector),
1115 color (Color::black()),
1119 blurtype (Blur::FASTGAUSSIAN),
1122 lastbyteop (Primitive::NONE),
1127 Layer_Shape::~Layer_Shape()
1133 Layer_Shape::clear()
1135 edge_table->clear();
1140 Layer_Shape::set_param(const String & param, const ValueBase &value)
1149 return Layer_Composite::set_param(param,value);
1153 Layer_Shape::get_param(const String ¶m)const
1165 return Layer_Composite::get_param(param);
1169 Layer_Shape::get_param_vocab()const
1171 Layer::Vocab ret(Layer_Composite::get_param_vocab());
1173 ret.push_back(ParamDesc("color")
1174 .set_local_name(_("Color"))
1175 .set_description(_("Layer_Shape Color"))
1177 ret.push_back(ParamDesc("offset")
1178 .set_local_name(_("Position"))
1180 ret.push_back(ParamDesc("invert")
1181 .set_local_name(_("Invert"))
1183 ret.push_back(ParamDesc("antialias")
1184 .set_local_name(_("Antialiasing"))
1186 ret.push_back(ParamDesc("feather")
1187 .set_local_name(_("Feather"))
1190 ret.push_back(ParamDesc("blurtype")
1191 .set_local_name(_("Type of Feather"))
1192 .set_description(_("Type of feathering to use"))
1194 .add_enum_value(Blur::BOX,"box",_("Box Blur"))
1195 .add_enum_value(Blur::FASTGAUSSIAN,"fastgaussian",_("Fast Gaussian Blur"))
1196 .add_enum_value(Blur::CROSS,"cross",_("Cross-Hatch Blur"))
1197 .add_enum_value(Blur::GAUSSIAN,"gaussian",_("Gaussian Blur"))
1198 .add_enum_value(Blur::DISC,"disc",_("Disc Blur"))
1204 synfig::Layer::Handle
1205 Layer_Shape::hit_check(synfig::Context context, const synfig::Point &p)const
1207 Point pos(p-offset);
1209 int intercepts = edge_table->intersect(pos[0],pos[1]);
1211 // If we have an odd number of intercepts, we are inside.
1212 // If we have an even number of intercepts, we are outside.
1213 bool intersect = ((!!intercepts) ^ invert);
1215 if(get_amount() == 0 || get_blend_method() == Color::BLEND_ALPHA_OVER)
1222 synfig::Layer::Handle tmp;
1223 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(p)))
1225 if(Color::is_onto(get_blend_method()))
1227 //if there's something in the lower layer then we're set...
1228 if(!context.hit_check(p).empty())
1229 return const_cast<Layer_Shape*>(this);
1230 }else if(get_blend_method() == Color::BLEND_ALPHA_OVER)
1232 synfig::info("layer_shape::hit_check - we've got alphaover");
1233 //if there's something in the lower layer then we're set...
1234 if(color.get_a() < 0.1 && get_amount() > .9)
1236 synfig::info("layer_shape::hit_check - can see through us... so nothing");
1238 }else return context.hit_check(p);
1240 return const_cast<Layer_Shape*>(this);
1243 return context.hit_check(p);
1247 Layer_Shape::get_color(Context context, const Point &p)const
1252 pp = Blur(feather,feather,blurtype)(p);
1254 Point pos(pp-offset);
1256 int intercepts = edge_table->intersect(pos[0],pos[1]);
1258 // If we have an odd number of intercepts, we are inside.
1259 // If we have an even number of intercepts, we are outside.
1260 bool intersect = ((!!intercepts) ^ invert);
1263 return context.get_color(pp);
1265 //Ok, we're inside... bummmm ba bum buM...
1266 if(get_blend_method() == Color::BLEND_STRAIGHT && get_amount() == 1)
1269 return Color::blend(color,context.get_color(p),get_amount(),get_blend_method());
1272 //************** SCANLINE RENDERING *********************
1273 void Layer_Shape::PolySpan::line_to(Real x, Real y)
1276 bool afterx = false;
1278 const Real xin(x), yin(y);
1280 Real dx = x - cur_x;
1281 Real dy = y - cur_y;
1285 //outside y - ignore entirely
1286 if( (cur_y >= window.maxy & y >= window.maxy)
1287 |(cur_y < window.miny & y < window.miny) )
1292 else //not degenerate - more complicated
1294 if(dy > 0) //be sure it's not tooooo small
1296 // cur_y ... window.miny ... window.maxy ... y
1298 //initial degenerate - initial clip
1299 if(cur_y < window.miny)
1301 //new clipped start point (must also move pen)
1302 n[2] = cur_x + (window.miny - cur_y) * dx / dy;
1305 cur_y = window.miny;
1306 move_pen((int)floor(cur_x),window.miny);
1309 //generate data for the ending clipped info
1312 //intial line to intersection (and degenerate)
1313 n[2] = x + (window.maxy - y) * dx / dy;
1322 //initial degenerate - initial clip
1323 if(cur_y > window.maxy)
1325 //new clipped start point (must also move pen)
1326 n[2] = cur_x + (window.maxy - cur_y) * dx / dy;
1329 cur_y = window.maxy;
1330 move_pen((int)floor(cur_x),window.maxy);
1333 //generate data for the ending clipped info
1336 //intial line to intersection (and degenerate)
1337 n[2] = x + (window.miny - y) * dx / dy;
1345 //all degenerate - but require bounded clipped values
1346 if( (cur_x >= window.maxx && x >= window.maxx)
1347 ||(cur_x < window.minx && x < window.minx) )
1349 //clip both vertices - but only needed in the x direction
1350 cur_x = max(cur_x, (Real)window.minx);
1351 cur_x = min(cur_x, (Real)window.maxx);
1353 //clip the dest values - y is already clipped
1354 x = max(x,(Real)window.minx);
1355 x = min(x,(Real)window.maxx);
1357 //must start at new point...
1358 move_pen((int)floor(cur_x),(int)floor(cur_y));
1360 draw_line(cur_x,cur_y,x,y);
1370 //initial degenerate - initial clip
1371 if(cur_x < window.minx)
1373 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1374 n[2] = cur_y + (window.minx - cur_x) * dy / dx;
1376 move_pen(window.minx,(int)floor(cur_y));
1377 draw_line(window.minx,cur_y,window.minx,n[2]);
1379 cur_x = window.minx;
1383 //generate data for the ending clipped info
1386 //intial line to intersection (and degenerate)
1387 n[2] = y + (window.maxx - x) * dy / dx;
1399 //initial degenerate - initial clip
1400 if(cur_x > window.maxx)
1402 //need to draw an initial segment from clippedx,cur_y to clippedx,intersecty
1403 n[2] = cur_y + (window.maxx - cur_x) * dy / dx;
1405 move_pen(window.maxx,(int)floor(cur_y));
1406 draw_line(window.maxx,cur_y,window.maxx,n[2]);
1408 cur_x = window.maxx;
1412 //generate data for the ending clipped info
1415 //intial line to intersection (and degenerate)
1416 n[2] = y + (window.minx - x) * dy / dx;
1428 move_pen((int)floor(cur_x),(int)floor(cur_y));
1429 //draw the relevant line (clipped)
1430 draw_line(cur_x,cur_y,x,y);
1434 draw_line(x,y,n[0],n[1]);
1441 } catch(...) { synfig::error("line_to: cur_x=%f, cur_y=%f, x=%f, y=%f", cur_x, cur_y, x, y); throw; }
1443 flags |= NotClosed|NotSorted;
1446 static inline bool clip_conic(const Point *const p, const ContextRect &r)
1448 const Real minx = min(min(p[0][0],p[1][0]),p[2][0]);
1449 const Real miny = min(min(p[0][1],p[1][1]),p[2][1]);
1450 const Real maxx = max(max(p[0][0],p[1][0]),p[2][0]);
1451 const Real maxy = max(max(p[0][1],p[1][1]),p[2][1]);
1453 return (minx > r.maxx) |
1459 static inline bool clip_cubic(const Point *const p, const ContextRect &r)
1461 /*const Real minx = min(min(p[0][0],p[1][0]),min(p[2][0],p[3][0]));
1462 const Real miny = min(min(p[0][1],p[1][1]),min(p[2][1],p[3][1]));
1463 const Real maxx = max(max(p[0][0],p[1][0]),max(p[2][0],p[3][1]));
1464 const Real maxy = max(max(p[0][1],p[1][1]),max(p[2][1],p[3][1]));
1466 return (minx > r.maxx) ||
1471 return ((p[0][0] > r.maxx) & (p[1][0] > r.maxx) & (p[2][0] > r.maxx) & (p[3][0] > r.maxx)) |
1472 ((p[0][0] < r.minx) & (p[1][0] < r.minx) & (p[2][0] < r.minx) & (p[3][0] < r.minx)) |
1473 ((p[0][1] > r.maxy) & (p[1][1] > r.maxy) & (p[2][1] > r.maxy) & (p[3][1] > r.maxy)) |
1474 ((p[0][1] < r.miny) & (p[1][1] < r.miny) & (p[2][1] < r.miny) & (p[3][1] < r.miny));
1477 static inline Real max_edges_cubic(const Point *const p)
1479 const Real x1 = p[1][0] - p[0][0];
1480 const Real y1 = p[1][1] - p[0][1];
1482 const Real x2 = p[2][0] - p[1][0];
1483 const Real y2 = p[2][1] - p[1][1];
1485 const Real x3 = p[3][0] - p[2][0];
1486 const Real y3 = p[3][1] - p[2][1];
1488 const Real d1 = x1*x1 + y1*y1;
1489 const Real d2 = x2*x2 + y2*y2;
1490 const Real d3 = x3*x3 + y3*y3;
1492 return max(max(d1,d2),d3);
1495 static inline Real max_edges_conic(const Point *const p)
1497 const Real x1 = p[1][0] - p[0][0];
1498 const Real y1 = p[1][1] - p[0][1];
1500 const Real x2 = p[2][0] - p[1][0];
1501 const Real y2 = p[2][1] - p[1][1];
1503 const Real d1 = x1*x1 + y1*y1;
1504 const Real d2 = x2*x2 + y2*y2;
1509 void Layer_Shape::PolySpan::conic_to(Real x1, Real y1, Real x, Real y)
1511 Point *current = arc;
1514 bool onsecond = false;
1516 arc[0] = Point(x,y);
1517 arc[1] = Point(x1,y1);
1518 arc[2] = Point(cur_x,cur_y);
1520 //just draw the line if it's outside
1521 if(clip_conic(arc,window))
1527 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1528 while(current >= arc)
1530 if(num >= MAX_SUBDIVISION_SIZE)
1532 warning("Curve subdivision somehow ran out of space while tesselating!");
1538 //if the curve is clipping then draw degenerate
1539 if(clip_conic(current,window))
1541 line_to(current[0][0],current[0][1]); //backwards so front is destination
1543 if(onsecond) level--;
1548 //if we are not at the level minimum
1549 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1551 Subd_Conic_Stack(current);
1552 current += 2; //cursor on second curve
1558 //split it again, if it's too big
1559 if(max_edges_conic(current) > 0.25) //distance of .5 (cover no more than half the pixel)
1561 Subd_Conic_Stack(current);
1562 current += 2; //cursor on second curve
1567 else //NOT TOO BIG? RENDER!!!
1569 //cur_x,cur_y = current[2], so we need to go 1,0
1570 line_to(current[1][0],current[1][1]);
1571 line_to(current[0][0],current[0][1]);
1574 if(onsecond) level--;
1581 void Layer_Shape::PolySpan::cubic_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
1583 Point *current = arc;
1586 bool onsecond = false;
1588 arc[0] = Point(x,y);
1589 arc[1] = Point(x2,y2);
1590 arc[2] = Point(x1,y1);
1591 arc[3] = Point(cur_x,cur_y);
1593 //just draw the line if it's outside
1594 if(clip_cubic(arc,window))
1600 //Ok so it's not super degenerate, subdivide and draw (run through minimum subdivision levels first)
1601 while(current >= arc) //once current goes below arc, there are no more curves left
1603 if(num >= MAX_SUBDIVISION_SIZE)
1605 warning("Curve subdivision somehow ran out of space while tesselating!");
1612 //if we are not at the level minimum
1613 if(level < MIN_SUBDIVISION_DRAW_LEVELS)
1615 Subd_Cubic_Stack(current);
1616 current += 3; //cursor on second curve
1622 //if the curve is clipping then draw degenerate
1623 if(clip_cubic(current,window))
1625 line_to(current[0][0],current[0][1]); //backwards so front is destination
1627 if(onsecond) level--;
1633 //split it again, if it's too big
1634 if(max_edges_cubic(current) > 0.25) //could use max_edges<3>
1636 Subd_Cubic_Stack(current);
1637 current += 3; //cursor on second curve
1642 else //NOT TOO BIG? RENDER!!!
1644 //cur_x,cur_y = current[3], so we need to go 2,1,0
1645 line_to(current[2][0],current[2][1]);
1646 line_to(current[1][0],current[1][1]);
1647 line_to(current[0][0],current[0][1]);
1650 if(onsecond) level--;
1657 //******************** LINE ALGORITHMS ****************************
1658 // THESE CALCULATE THE AREA AND THE COVER FOR THE MARKS, TO THEN SCAN CONVERT
1659 // - BROKEN UP INTO SCANLINES (draw_line - y intersections),
1660 // THEN THE COVER AND AREA PER TOUCHED PIXEL IS CALCULATED (draw_scanline - x intersections)
1661 void Layer_Shape::PolySpan::draw_scanline(int y, Real x1, Real fy1, Real x2, Real fy2)
1663 int ix1 = (int)floor(x1);
1664 int ix2 = (int)floor(x2);
1665 Real fx1 = x1 - ix1;
1666 Real fx2 = x2 - ix2;
1668 Real dx,dy,dydx,mult;
1673 //case horizontal line
1676 move_pen(ix2,y); //pen needs to be at the last coord
1680 //case all in same pixel
1681 if(ix1 == ix2) //impossible for degenerate case (covered by the previous cases)
1683 current.addcover(dy,(fx1 + fx2)*dy/2); //horizontal trapazoid area
1689 // ----> fx1...1 0...1 ... 0...1 0...fx2
1692 //set initial values
1693 //Iterate through the covered pixels
1694 mult = (1 - fx1)*dydx; //next y intersection diff value (at 1)
1697 current.addcover(mult,(1 + fx1)*mult/2); // fx1,fy1,1,fy@1 - starting trapazoidal area
1699 //move to the next pixel
1705 //set up for whole ones
1708 //trapezoid(0,y1,1,y1+dydx);
1709 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1711 //move to next pixel (+1)
1718 //final y-pos - last intersect pos
1720 current.addcover(mult,(0+fx2)*mult/2);
1723 // fx2...1 0...1 ... 0...1 0...fx1 <----
1724 //mult = (0 - fx1) * dy / dx;
1725 //neg sign sucked into dydx
1728 //set initial values
1729 //Iterate through the covered pixels
1730 mult = fx1*dydx; //next y intersection diff value
1733 current.addcover(mult,fx1*mult/2); // fx1,fy1,0,fy@0 - starting trapazoidal area
1735 //move to next pixel
1741 //set up for whole ones
1744 //trapezoid(0,y1,1,y1+dydx);
1745 current.addcover(dydx,dydx/2); //accumulated area 1/2 the cover
1747 //move to next pixel (-1)
1754 mult = fy2 - fy1; //final y-pos - last intersect pos
1756 current.addcover(mult,(fx2+1)*mult/2);
1760 void Layer_Shape::PolySpan::draw_line(Real x1, Real y1, Real x2, Real y2)
1762 int iy1 = (int)floor(y1);
1763 int iy2 = (int)floor(y2);
1764 Real fy1 = y1 - iy1;
1765 Real fy2 = y2 - iy2;
1767 assert(!isnan(fy1));
1768 assert(!isnan(fy2));
1770 Real dx,dy,dxdy,mult,x_from,x_to;
1772 const Real SLOPE_EPSILON = 1e-10;
1774 //case all one scanline
1777 draw_scanline(iy1,x1,y1,x2,y2);
1785 //case vertical line
1786 if(dx < SLOPE_EPSILON && dx > -SLOPE_EPSILON)
1788 //calc area and cover on vertical line
1791 // ----> fx1...1 0...1 ... 0...1 0...fx2
1794 int ix1 = (int)floor(x1);
1795 Real fx1 = x1 - ix1;
1800 current.addcover(sub,fx1*sub);
1805 //move pen to next pixel
1811 current.addcover(1,fx1);
1819 current.addcover(fy2,fy2*fx1);
1824 int ix1 = (int)floor(x1);
1825 Real fx1 = x1 - ix1;
1830 current.addcover(sub,fx1*sub);
1839 //accumulate in current pixel
1840 current.addcover(-1,-fx1);
1847 current.addcover(fy2-1,(fy2-1)*fx1);
1852 //case normal line - guaranteed dx != 0 && dy != 0
1854 //calculate the initial intersection with "next" scanline
1859 mult = (1 - fy1) * dxdy;
1861 //x interset scanline
1863 draw_scanline(iy1,x1,fy1,x_from,1);
1868 move_pen((int)floor(x_from),iy1);
1872 //keep up on the x axis, and render the current scanline
1873 x_to = x_from + dxdy;
1874 draw_scanline(iy1,x_from,0,x_to,1);
1877 //move to next pixel
1879 move_pen((int)floor(x_from),iy1);
1882 //draw the last one, fractional
1883 draw_scanline(iy2,x_from,0,x2,fy2);
1891 //x interset scanline
1893 draw_scanline(iy1,x1,fy1,x_from,0);
1898 move_pen((int)floor(x_from),iy1);
1902 x_to = x_from + dxdy;
1903 draw_scanline(iy1,x_from,1,x_to,0);
1907 move_pen((int)floor(x_from),iy1);
1909 //draw the last one, fractional
1910 draw_scanline(iy2,x_from,1,x2,fy2);
1914 //****** LAYER PEN OPERATIONS (move_to, line_to, etc.) ******
1915 void Layer_Shape::move_to(Real x, Real y)
1917 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1921 op.operation = Primitive::MOVE_TO;
1922 op.number = 1; //one point for now
1924 if(lastbyteop == Primitive::MOVE_TO)
1926 char *ptr = &bytestream[lastoppos];
1927 memcpy(ptr,&op,sizeof(op));
1928 memcpy(ptr+sizeof(op),&p,sizeof(p));
1930 else //make a new op
1932 lastbyteop = Primitive::MOVE_TO;
1933 lastoppos = bytestream.size();
1935 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
1936 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
1939 edge_table->move_to(x,y);
1942 void Layer_Shape::close()
1946 op.operation = Primitive::CLOSE;
1949 if(lastbyteop == Primitive::CLOSE)
1953 lastbyteop = Primitive::CLOSE;
1954 lastoppos = bytestream.size();
1956 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert header
1959 edge_table->close();
1960 //should not affect the bounding box since it would just be returning to old point...
1963 void Layer_Shape::endpath()
1967 op.operation = Primitive::END;
1970 if(lastbyteop == Primitive::END | lastbyteop == Primitive::NONE)
1974 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1));
1976 //should not affect the bounding box since it would just be returning to old point... if at all
1979 void Layer_Shape::line_to(Real x, Real y)
1984 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
1988 op.operation = Primitive::LINE_TO;
1989 op.number = 1; //one point for now
1991 if(lastbyteop == Primitive::MOVE_TO | lastbyteop == Primitive::LINE_TO)
1993 //only need to insert the point
1994 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
1996 Primitive * prim = (Primitive *)&bytestream[lastoppos];
1997 prim->number++; //increment number of points in the list
2000 lastbyteop = Primitive::LINE_TO;
2001 lastoppos = bytestream.size();
2003 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2004 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2007 edge_table->line_to(x,y);
2010 void Layer_Shape::conic_to(Real x1, Real y1, Real x, Real y)
2012 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*2;
2017 op.operation = Primitive::CONIC_TO;
2018 op.number = 2; //2 points for now
2020 if(lastbyteop == Primitive::CONIC_TO)
2022 //only need to insert the new points
2023 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2024 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2026 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2027 prim->number += 2; //increment number of points in the list
2030 lastbyteop = Primitive::CONIC_TO;
2031 lastoppos = bytestream.size();
2033 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2034 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2035 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2038 edge_table->conic_to(x1,y1,x,y);
2041 void Layer_Shape::conic_to_smooth(Real x, Real y) //x1,y1 derived from current tangent
2043 //const int sizeblock = sizeof(Primitive)+sizeof(Point);
2047 op.operation = Primitive::CONIC_TO_SMOOTH;
2048 op.number = 1; //2 points for now
2050 if(lastbyteop == Primitive::CONIC_TO_SMOOTH)
2052 //only need to insert the new point
2053 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2055 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2056 prim->number += 1; //increment number of points in the list
2059 lastbyteop = Primitive::CONIC_TO_SMOOTH;
2060 lastoppos = bytestream.size();
2062 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2063 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2066 edge_table->conic_to_smooth(x,y);
2069 void Layer_Shape::curve_to(Real x1, Real y1, Real x2, Real y2, Real x, Real y)
2071 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2077 op.operation = Primitive::CUBIC_TO;
2078 op.number = 3; //3 points for now
2080 if(lastbyteop == Primitive::CUBIC_TO)
2082 //only need to insert the new points
2083 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1));
2084 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2085 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2087 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2088 prim->number += 3; //increment number of points in the list
2091 lastbyteop = Primitive::CUBIC_TO;
2092 lastoppos = bytestream.size();
2094 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2095 bytestream.insert(bytestream.end(),(char*)&p1,(char*)(&p1+1)); //insert the bytes for data
2096 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2097 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2100 edge_table->curve_to(x1,y1,x2,y2,x,y);
2103 void Layer_Shape::curve_to_smooth(Real x2, Real y2, Real x, Real y) //x1,y1 derived from current tangent
2105 //const int sizeblock = sizeof(Primitive)+sizeof(Point)*3;
2110 op.operation = Primitive::CUBIC_TO_SMOOTH;
2111 op.number = 2; //3 points for now
2113 if(lastbyteop == Primitive::CUBIC_TO_SMOOTH)
2115 //only need to insert the new points
2116 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1));
2117 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1));
2119 Primitive * prim = (Primitive *)&bytestream[lastoppos];
2120 prim->number += 2; //increment number of points in the list
2123 lastbyteop = Primitive::CUBIC_TO_SMOOTH;
2124 lastoppos = bytestream.size();
2126 bytestream.insert(bytestream.end(),(char*)&op,(char*)(&op+1)); //insert the bytes for the header
2127 bytestream.insert(bytestream.end(),(char*)&p2,(char*)(&p2+1)); //insert the bytes for data
2128 bytestream.insert(bytestream.end(),(char*)&p,(char*)(&p+1)); //insert the bytes for data
2132 // ACCELERATED RENDER FUNCTION - TRANSLATE BYTE CODE INTO FUNCTION CALLS
2134 bool Layer_Shape::render_polyspan(Surface *surface, PolySpan &polyspan,
2135 Color::BlendMethod got_blend_method, Color::value_type got_amount) const
2137 Surface::alpha_pen p(surface->begin(),got_amount,_BlendFunc(got_blend_method));
2138 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2139 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2141 Real cover,area,alpha;
2148 if(cur_mark == end_mark)
2153 p.move_to(polyspan.window.minx,polyspan.window.miny);
2154 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2159 //fill initial rect / line
2162 //fill all the area above the first vertex
2163 p.move_to(polyspan.window.minx,polyspan.window.miny);
2164 y = polyspan.window.miny;
2165 int l = polyspan.window.maxx - polyspan.window.minx;
2167 p.put_block(cur_mark->y - polyspan.window.miny,l);
2169 //fill the area to the left of the first vertex on that line
2170 l = cur_mark->x - polyspan.window.minx;
2171 p.move_to(polyspan.window.minx,cur_mark->y);
2172 if(l) p.put_hline(l);
2182 area = cur_mark->area;
2183 cover += cur_mark->cover;
2185 //accumulate for the current pixel
2186 while(++cur_mark != polyspan.covers.end())
2188 if(y != cur_mark->y | x != cur_mark->x)
2191 area += cur_mark->area;
2192 cover += cur_mark->cover;
2195 //draw pixel - based on covered area
2196 if(area) //if we're ok, draw the current pixel
2198 alpha = polyspan.ExtractAlpha(cover - area);
2199 if(invert) alpha = 1 - alpha;
2203 if(alpha >= .5) p.put_value();
2205 else if(alpha) p.put_value_alpha(alpha);
2211 //if we're done, don't use iterator and exit
2212 if(cur_mark == end_mark) break;
2214 //if there is no more live pixels on this line, goto next
2215 if(y != cur_mark->y)
2219 //fill the area at the end of the line
2220 p.put_hline(polyspan.window.maxx - x);
2222 //fill area at the beginning of the next line
2223 p.move_to(polyspan.window.minx,cur_mark->y);
2224 p.put_hline(cur_mark->x - polyspan.window.minx);
2232 //draw span to next pixel - based on total amount of pixel cover
2235 alpha = polyspan.ExtractAlpha(cover);
2236 if(invert) alpha = 1 - alpha;
2240 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2242 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2246 //fill the after stuff
2249 //fill the area at the end of the line
2250 p.put_hline(polyspan.window.maxx - x);
2252 //fill area at the beginning of the next line
2253 p.move_to(polyspan.window.minx,y+1);
2254 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2260 bool Layer_Shape::render_polyspan(etl::surface<float> *surface, PolySpan &polyspan) const
2262 etl::surface<float>::pen p(surface->begin());
2263 PolySpan::cover_array::iterator cur_mark = polyspan.covers.begin();
2264 PolySpan::cover_array::iterator end_mark = polyspan.covers.end();
2266 Real cover,area,alpha;
2272 //the pen always writes 1 (unless told to do otherwise)
2275 if(cur_mark == end_mark)
2280 p.move_to(polyspan.window.minx,polyspan.window.miny);
2281 p.put_block(polyspan.window.maxy - polyspan.window.miny,polyspan.window.maxx - polyspan.window.minx);
2286 //fill initial rect / line
2289 //fill all the area above the first vertex
2290 p.move_to(polyspan.window.minx,polyspan.window.miny);
2291 y = polyspan.window.miny;
2292 int l = polyspan.window.maxx - polyspan.window.minx;
2294 p.put_block(cur_mark->y - polyspan.window.miny,l);
2296 //fill the area to the left of the first vertex on that line
2297 l = cur_mark->x - polyspan.window.minx;
2298 p.move_to(polyspan.window.minx,cur_mark->y);
2299 if(l) p.put_hline(l);
2308 area = cur_mark->area;
2309 cover += cur_mark->cover;
2311 //accumulate for the current pixel
2312 while(++cur_mark != polyspan.covers.end())
2314 if(y != cur_mark->y | x != cur_mark->x)
2317 area += cur_mark->area;
2318 cover += cur_mark->cover;
2321 //draw pixel - based on covered area
2322 if(area) //if we're ok, draw the current pixel
2324 alpha = 1 - polyspan.ExtractAlpha(cover - area);
2327 if(alpha >= .5) p.put_value();
2329 else if(alpha) p.put_value(alpha);
2335 //if we're done, don't use iterator and exit
2336 if(cur_mark == end_mark) break;
2338 //if there is no more live pixels on this line, goto next
2339 if(y != cur_mark->y)
2341 //fill the area at the end of the line
2342 p.put_hline(polyspan.window.maxx - x);
2344 //fill area at the beginning of the next line
2345 p.move_to(polyspan.window.minx,cur_mark->y);
2346 p.put_hline(cur_mark->x - polyspan.window.minx);
2353 //draw span to next pixel - based on total amount of pixel cover
2356 alpha = 1 - polyspan.ExtractAlpha(cover);
2359 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2361 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2365 //fill the area at the end of the line
2366 p.put_hline(polyspan.window.maxx - x);
2368 //fill area at the beginning of the next line
2369 p.move_to(polyspan.window.minx,y+1);
2370 p.put_block(polyspan.window.maxy - y - 1,polyspan.window.maxx - polyspan.window.minx);
2380 area = cur_mark->area;
2381 cover += cur_mark->cover;
2383 //accumulate for the current pixel
2384 while(++cur_mark != polyspan.covers.end())
2386 if(y != cur_mark->y | x != cur_mark->x)
2389 area += cur_mark->area;
2390 cover += cur_mark->cover;
2393 //draw pixel - based on covered area
2394 if(area) //if we're ok, draw the current pixel
2396 alpha = polyspan.ExtractAlpha(cover - area);
2399 if(alpha >= .5) p.put_value();
2401 else if(alpha) p.put_value(alpha);
2407 //if we're done, don't use iterator and exit
2408 if(cur_mark == end_mark) break;
2410 //if there is no more live pixels on this line, goto next
2411 if(y != cur_mark->y)
2418 //draw span to next pixel - based on total amount of pixel cover
2421 alpha = polyspan.ExtractAlpha(cover);
2424 if(alpha >= .5) p.put_hline(cur_mark->x - x);
2426 else if(alpha) p.put_hline(cur_mark->x - x,alpha);
2435 Layer_Shape::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
2437 const unsigned int w = renddesc.get_w();
2438 const unsigned int h = renddesc.get_h();
2440 const Real pw = abs(renddesc.get_pw());
2441 const Real ph = abs(renddesc.get_ph());
2443 //const Real OFFSET_EPSILON = 1e-8;
2444 SuperCallback stageone(cb,1,10000,15001+renddesc.get_h());
2445 SuperCallback stagetwo(cb,10000,10001+renddesc.get_h(),15001+renddesc.get_h());
2446 SuperCallback stagethree(cb,10001+renddesc.get_h(),15001+renddesc.get_h(),15001+renddesc.get_h());
2448 // Render what is behind us
2450 //clip if it satisfies the invert solid thing
2451 if(is_solid_color() && invert)
2453 Rect aabb = edge_table->aabb;
2454 Point tl = renddesc.get_tl() - offset;
2456 Real pw = renddesc.get_pw(),
2457 ph = renddesc.get_ph();
2461 Real pixelfeatherx = abs(feather/pw),
2462 pixelfeathery = abs(feather/ph);
2464 nrect.set_point((aabb.minx - tl[0])/pw,(aabb.miny - tl[1])/ph);
2465 nrect.expand((aabb.maxx - tl[0])/pw,(aabb.maxy - tl[1])/ph);
2467 RendDesc optdesc(renddesc);
2469 //make sure to expand so we gain subpixels rather than lose them
2470 nrect.minx = floor(nrect.minx-pixelfeatherx); nrect.miny = floor(nrect.miny-pixelfeathery);
2471 nrect.maxx = ceil(nrect.maxx+pixelfeatherx); nrect.maxy = ceil(nrect.maxy+pixelfeathery);
2473 //make sure the subwindow is clipped with our tile window (minimize useless drawing)
2474 set_intersect(nrect,nrect,Rect(0,0,renddesc.get_w(),renddesc.get_h()));
2476 //must resize the surface first
2477 surface->set_wh(renddesc.get_w(),renddesc.get_h());
2480 //only render anything if it's visible from our current tile
2483 //set the subwindow to the viewable pixels and render it to the subsurface
2484 optdesc.set_subwindow((int)nrect.minx, (int)nrect.miny,
2485 (int)(nrect.maxx - nrect.minx), (int)(nrect.maxy - nrect.miny));
2487 Surface optimizedbacksurf;
2488 if(!context.accelerated_render(&optimizedbacksurf,quality,optdesc,&stageone))
2491 //blit that onto the original surface so we can pretend that nothing ever happened
2492 Surface::pen p = surface->get_pen((int)nrect.minx,(int)nrect.miny);
2493 optimizedbacksurf.blit_to(p);
2497 if(!context.accelerated_render(surface,quality,renddesc,&stageone))
2501 if(cb && !cb->amount_complete(10000,10001+renddesc.get_h())) return false;
2505 //we have to blur rather than be crappy
2507 //so make a separate surface
2508 RendDesc workdesc(renddesc);
2510 etl::surface<float> shapesurface;
2512 //the expanded size = 1/2 the size in each direction rounded up
2513 int halfsizex = (int) (abs(feather*.5/pw) + 3),
2514 halfsizey = (int) (abs(feather*.5/ph) + 3);
2516 //expand by 1/2 size in each direction on either side
2523 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2526 case Blur::FASTGAUSSIAN:
2533 workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),w+2*max(1,halfsizex),h+2*max(1,halfsizey));
2536 case Blur::GAUSSIAN:
2538 #define GAUSSIAN_ADJUSTMENT (0.05)
2539 Real pw = (Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
2540 Real ph = (Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
2545 halfsizex = (int)(abs(pw)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2546 halfsizey = (int)(abs(ph)*feather*GAUSSIAN_ADJUSTMENT+0.5);
2548 halfsizex = (halfsizex + 1)/2;
2549 halfsizey = (halfsizey + 1)/2;
2550 workdesc.set_subwindow( -halfsizex, -halfsizey, w+2*halfsizex, h+2*halfsizey );
2556 shapesurface.set_wh(workdesc.get_w(),workdesc.get_h());
2557 shapesurface.clear();
2560 if(!render_shape(&shapesurface,quality,workdesc,&stagetwo))return false;
2563 Blur(feather,feather,blurtype,&stagethree)(shapesurface,workdesc.get_br()-workdesc.get_tl(),shapesurface);
2565 //blend with stuff below it...
2566 unsigned int u = halfsizex, v = halfsizey, x = 0, y = 0;
2567 for(y = 0; y < h; y++,v++)
2570 for(x = 0; x < w; x++,u++)
2572 float a = shapesurface[v][u];
2575 //a = floor(a*255+0.5f)/255;
2576 (*surface)[y][x]=Color::blend(color,(*surface)[y][x],a*get_amount(),get_blend_method());
2578 //else (*surface)[y][x] = worksurface[v][u];
2583 if(cb && !cb->amount_complete(100,100))
2585 synfig::warning("Layer_Shape: could not set amount complete");
2592 //might take out to reduce code size
2593 return render_shape(surface,true,quality,renddesc,&stagetwo);
2599 Layer_Shape::render_shape(Surface *surface,bool useblend,int quality,
2600 const RendDesc &renddesc, ProgressCallback *cb)const
2604 SuperCallback progress(cb,0,renddesc.get_h(),renddesc.get_h());
2606 // If our amount is set to zero, no need to render anything
2610 //test new polygon renderer
2612 // Width and Height of a pixel
2613 const int w = renddesc.get_w();
2614 const int h = renddesc.get_h();
2615 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2616 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2618 const Point tl = renddesc.get_tl();
2620 Vector tangent (0,0);
2624 //optimization for tesselating only inside tiles
2625 span.window.minx = 0;
2626 span.window.miny = 0;
2627 span.window.maxx = w;
2628 span.window.maxy = h;
2630 //pointers for processing the bytestream
2631 const char *current = &bytestream[0];
2632 const char *end = &bytestream[bytestream.size()];
2634 int operation = Primitive::NONE;
2641 Real x,y,x1,y1,x2,y2;
2644 while(current < end)
2650 //get the op code safely
2651 curprim = (Primitive *)current;
2653 //advance past indices
2654 current += sizeof(Primitive);
2657 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2661 //get the relevant data
2662 operation = curprim->operation;
2663 number = curprim->number;
2665 if(operation == Primitive::END)
2668 if(operation == Primitive::CLOSE)
2670 if(span.notclosed())
2672 tangent[0] = span.close_x - span.cur_x;
2673 tangent[1] = span.close_y - span.cur_y;
2679 data = (Point*)current;
2680 current += sizeof(Point)*number;
2682 //check data positioning
2685 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2689 } catch(...) { synfig::error("Layer_Shape::render_shape()1: Caught an exception after %d loops, rethrowing...", tmp); throw; }
2691 //transfer all the data - RLE optimized
2692 for(curnum=0; curnum < number;)
2696 case Primitive::MOVE_TO:
2698 x = data[curnum][0];
2699 x = (x - tl[0] + offset[0])*pw;
2700 y = data[curnum][1];
2701 y = (y - tl[1] + offset[1])*ph;
2712 tangent[0] = x - span.cur_x;
2713 tangent[1] = y - span.cur_y;
2718 curnum++; //only advance one point
2723 case Primitive::LINE_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;
2730 tangent[0] = x - span.cur_x;
2731 tangent[1] = y - span.cur_y;
2738 case Primitive::CONIC_TO:
2740 x = data[curnum+1][0];
2741 x = (x - tl[0] + offset[0])*pw;
2742 y = data[curnum+1][1];
2743 y = (y - tl[1] + offset[1])*ph;
2745 x1 = data[curnum][0];
2746 x1 = (x1 - tl[0] + offset[0])*pw;
2747 y1 = data[curnum][1];
2748 y1 = (y1 - tl[1] + offset[1])*ph;
2750 tangent[0] = 2*(x - x1);
2751 tangent[1] = 2*(y - y1);
2753 span.conic_to(x1,y1,x,y);
2758 case Primitive::CONIC_TO_SMOOTH:
2760 x = data[curnum][0];
2761 x = (x - tl[0] + offset[0])*pw;
2762 y = data[curnum][1];
2763 y = (y - tl[1] + offset[1])*ph;
2765 x1 = span.cur_x + tangent[0]/2;
2766 y1 = span.cur_y + tangent[1]/2;
2768 tangent[0] = 2*(x - x1);
2769 tangent[1] = 2*(y - y1);
2771 span.conic_to(x1,y1,x,y);
2777 case Primitive::CUBIC_TO:
2779 x = data[curnum+2][0];
2780 x = (x - tl[0] + offset[0])*pw;
2781 y = data[curnum+2][1];
2782 y = (y - tl[1] + offset[1])*ph;
2784 x2 = data[curnum+1][0];
2785 x2 = (x2 - tl[0] + offset[0])*pw;
2786 y2 = data[curnum+1][1];
2787 y2 = (y2 - tl[1] + offset[1])*ph;
2789 x1 = data[curnum][0];
2790 x1 = (x1 - tl[0] + offset[0])*pw;
2791 y1 = data[curnum][1];
2792 y1 = (y1 - tl[1] + offset[1])*ph;
2794 tangent[0] = 2*(x - x2);
2795 tangent[1] = 2*(y - y2);
2797 span.cubic_to(x1,y1,x2,y2,x,y);
2803 case Primitive::CUBIC_TO_SMOOTH:
2805 x = data[curnum+1][0];
2806 x = (x - tl[0] + offset[0])*pw;
2807 y = data[curnum+1][1];
2808 y = (y - tl[1] + offset[1])*ph;
2810 x2 = data[curnum][0];
2811 x2 = (x2 - tl[0] + offset[0])*pw;
2812 y2 = data[curnum][1];
2813 y2 = (y2 - tl[1] + offset[1])*ph;
2815 x1 = span.cur_x + tangent[0]/3.0;
2816 y1 = span.cur_y + tangent[1]/3.0;
2818 tangent[0] = 2*(x - x2);
2819 tangent[1] = 2*(y - y2);
2821 span.cubic_to(x1,y1,x2,y2,x,y);
2830 //sort the bastards so we can render everything
2833 return render_polyspan(surface, span,
2834 useblend?get_blend_method():Color::BLEND_STRAIGHT,
2835 useblend?get_amount():1.0);
2839 Layer_Shape::render_shape(surface<float> *surface,int quality,
2840 const RendDesc &renddesc, ProgressCallback *cb)const
2842 // If our amount is set to zero, no need to render anything
2846 //test new polygon renderer
2848 // Width and Height of a pixel
2849 const int w = renddesc.get_w();
2850 const int h = renddesc.get_h();
2851 const Real pw = renddesc.get_w()/(renddesc.get_br()[0]-renddesc.get_tl()[0]);
2852 const Real ph = renddesc.get_h()/(renddesc.get_br()[1]-renddesc.get_tl()[1]);
2854 const Point tl = renddesc.get_tl();
2856 Vector tangent (0,0);
2860 //optimization for tesselating only inside tiles
2861 span.window.minx = 0;
2862 span.window.miny = 0;
2863 span.window.maxx = w;
2864 span.window.maxy = h;
2866 //pointers for processing the bytestream
2867 const char *current = &bytestream[0];
2868 const char *end = &bytestream[bytestream.size()];
2870 int operation = Primitive::NONE;
2877 Real x,y,x1,y1,x2,y2;
2879 while(current < end)
2881 //get the op code safely
2882 curprim = (Primitive *)current;
2884 //advance past indices
2885 current += sizeof(Primitive);
2888 warning("Layer_Shape::accelerated_render - Error in the byte stream, not enough space for next declaration");
2892 //get the relevant data
2893 operation = curprim->operation;
2894 number = curprim->number;
2896 if(operation == Primitive::END)
2899 if(operation == Primitive::CLOSE)
2901 if(span.notclosed())
2903 tangent[0] = span.close_x - span.cur_x;
2904 tangent[1] = span.close_y - span.cur_y;
2910 data = (Point*)current;
2911 current += sizeof(Point)*number;
2913 //check data positioning
2916 warning("Layer_Shape::accelerated_render - Error in the byte stream, in sufficient data space for declared number of points");
2920 //transfer all the data
2921 for(curnum=0; curnum < number;)
2925 case Primitive::MOVE_TO:
2927 x = data[curnum][0];
2928 x = (x - tl[0] + offset[0])*pw;
2929 y = data[curnum][1];
2930 y = (y - tl[1] + offset[1])*ph;
2941 tangent[0] = x - span.cur_x;
2942 tangent[1] = y - span.cur_y;
2947 curnum++; //only advance one point
2952 case Primitive::LINE_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;
2959 tangent[0] = x - span.cur_x;
2960 tangent[1] = y - span.cur_y;
2967 case Primitive::CONIC_TO:
2969 x = data[curnum+1][0];
2970 x = (x - tl[0] + offset[0])*pw;
2971 y = data[curnum+1][1];
2972 y = (y - tl[1] + offset[1])*ph;
2974 x1 = data[curnum][0];
2975 x1 = (x1 - tl[0] + offset[0])*pw;
2976 y1 = data[curnum][1];
2977 y1 = (y1 - tl[1] + offset[1])*ph;
2979 tangent[0] = 2*(x - x1);
2980 tangent[1] = 2*(y - y1);
2982 span.conic_to(x1,y1,x,y);
2987 case Primitive::CONIC_TO_SMOOTH:
2989 x = data[curnum][0];
2990 x = (x - tl[0] + offset[0])*pw;
2991 y = data[curnum][1];
2992 y = (y - tl[1] + offset[1])*ph;
2994 x1 = span.cur_x + tangent[0]/2;
2995 y1 = span.cur_y + tangent[1]/2;
2997 tangent[0] = 2*(x - x1);
2998 tangent[1] = 2*(y - y1);
3000 span.conic_to(x1,y1,x,y);
3006 case Primitive::CUBIC_TO:
3008 x = data[curnum+2][0];
3009 x = (x - tl[0] + offset[0])*pw;
3010 y = data[curnum+2][1];
3011 y = (y - tl[1] + offset[1])*ph;
3013 x2 = data[curnum+1][0];
3014 x2 = (x2 - tl[0] + offset[0])*pw;
3015 y2 = data[curnum+1][1];
3016 y2 = (y2 - tl[1] + offset[1])*ph;
3018 x1 = data[curnum][0];
3019 x1 = (x1 - tl[0] + offset[0])*pw;
3020 y1 = data[curnum][1];
3021 y1 = (y1 - tl[1] + offset[1])*ph;
3023 tangent[0] = 2*(x - x2);
3024 tangent[1] = 2*(y - y2);
3026 span.cubic_to(x1,y1,x2,y2,x,y);
3032 case Primitive::CUBIC_TO_SMOOTH:
3034 x = data[curnum+1][0];
3035 x = (x - tl[0] + offset[0])*pw;
3036 y = data[curnum+1][1];
3037 y = (y - tl[1] + offset[1])*ph;
3039 x2 = data[curnum][0];
3040 x2 = (x2 - tl[0] + offset[0])*pw;
3041 y2 = data[curnum][1];
3042 y2 = (y2 - tl[1] + offset[1])*ph;
3044 x1 = span.cur_x + tangent[0]/3.0;
3045 y1 = span.cur_y + tangent[1]/3.0;
3047 tangent[0] = 2*(x - x2);
3048 tangent[1] = 2*(y - y2);
3050 span.cubic_to(x1,y1,x2,y2,x,y);
3059 //sort the bastards so we can render everything
3062 return render_polyspan(surface, span);
3066 Layer_Shape::get_bounding_rect()const
3069 return Rect::full_plane();
3071 if (edge_table->initaabb)
3072 return Rect::zero();
3074 Rect bounds(edge_table->aabb+offset);
3075 bounds.expand(max((bounds.get_min() - bounds.get_max()).mag()*0.01,