1 /* $XConsortium: curves.c,v 1.4 92/07/07 17:14:55 gildea Exp $ */
2 /* Copyright International Business Machines,Corp. 1991 */
3 /* All Rights Reserved */
5 /* License to use, copy, modify, and distribute this software */
6 /* and its documentation for any purpose and without fee is */
7 /* hereby granted, provided that licensee provides a license to */
8 /* IBM, Corp. to use, copy, modify, and distribute derivative */
9 /* works and their documentation for any purpose and without */
10 /* fee, that the above copyright notice appear in all copies */
11 /* and that both that copyright notice and this permission */
12 /* notice appear in supporting documentation, and that the name */
13 /* of IBM not be used in advertising or publicity pertaining to */
14 /* distribution of the software without specific, written prior */
17 /* IBM PROVIDES THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES */
18 /* OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT */
19 /* LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, */
20 /* FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT OF */
21 /* THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE QUALITY AND */
22 /* PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT */
23 /* OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF */
24 /* THE SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM) ASSUMES */
25 /* THE ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN */
26 /* NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR */
27 /* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING */
28 /* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF */
29 /* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT */
30 /* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS */
33 :h1.CURVES Module - Stepping Beziers
35 This module is responsible for "rasterizing"
36 third order curves. That is, it changes the high level curve
37 specification into a list of pels that that curve travels
55 :h3.Functions Provided to Other Modules
57 External entry points:
59 /*SHARED LINE(S) ORIGINATED HERE*/
62 Note that "stepping" and "flattening" are so similiar that they use the
63 same routine. When the "region" parameter is NULL, that is a flag that
64 we are flattening instead of stepping.
67 :h2.Bezier Third Order Curves
70 :h3.The "bezierinfo" Structure
72 This structure is used to store information used when we subdivide
77 struct region *region; /* the region being built or NULL */
78 struct fractpoint last; /* not used yet; maybe could save some work */
79 struct fractpoint origin; /* the origin of the bezier */
83 Checking for termination of the subdivision process:
84 This is the stupidest test in the world, just check if the coordinatewise
85 distance from an end control point to the next control point is less than
86 one half pel. If so, we must be done.
87 This returns 1 if the subdivision is terminated and 0 if you still need
91 static int BezierTerminationTest(xa,ya,xb,yb,xc,yc,xd,yd)
92 fractpel xa,ya,xb,yb,xc,yc,xd,yd;
96 dmax = MAX(dmax,ABS(ya - yb));
97 dmax = MAX(dmax,ABS(xd - xc));
98 dmax = MAX(dmax,ABS(yd - yc));
100 return(0); /* not done yet */
102 return(1); /* done */
106 :h3.StepBezierRecurse() - The Recursive Logic in StepBezier()
108 The recursion involves dividing the control polygon into two smaller
109 control polygons by finding the midpoints of the lines. This idea is
110 described in any graphics text book and its simplicity is what caused
111 Bezier to define his curves as he did. If the input region 'R' is NULL,
112 the result is a path that is the 'flattened' curve; otherwise StepBezier
113 returns nothing special.
115 static struct segment *StepBezierRecurse(I,xA,yA,xB,yB,xC,yC,xD,yD)
116 struct bezierinfo *I; /* Region under construction or NULL */
117 fractpel xA,yA; /* A control point */
118 fractpel xB,yB; /* B control point */
119 fractpel xC,yC; /* C control point */
120 fractpel xD,yD; /* D control point */
123 if (BezierTerminationTest(xA,yA,xB,yB,xC,yC,xD,yD))
125 if (I->region == NULL)
126 return(PathSegment(LINETYPE, xD - xA, yD - yA));
128 StepLine(I->region, I->origin.x + xA, I->origin.y + yA,
129 I->origin.x + xD, I->origin.y + yD);
138 fractpel xABCD,yABCD;
140 xAB = xA + xB; yAB = yA + yB;
141 xBC = xB + xC; yBC = yB + yC;
142 xCD = xC + xD; yCD = yC + yD;
144 xABC = xAB + xBC; yABC = yAB + yBC;
145 xBCD = xBC + xCD; yBCD = yBC + yCD;
147 xABCD = xABC + xBCD; yABCD = yABC + yBCD;
149 xAB >>= 1; yAB >>= 1;
150 xBC >>= 1; yBC >>= 1;
151 xCD >>= 1; yCD >>= 1;
152 xABC >>= 2; yABC >>= 2;
153 xBCD >>= 2; yBCD >>= 2;
154 xABCD >>= 3; yABCD >>= 3;
156 if (I->region == NULL)
159 StepBezierRecurse(I, xA, yA, xAB, yAB, xABC, yABC, xABCD, yABCD),
160 StepBezierRecurse(I, xABCD, yABCD, xBCD, yBCD, xCD, yCD, xD, yD)
166 StepBezierRecurse(I, xA, yA, xAB, yAB, xABC, yABC, xABCD, yABCD);
167 StepBezierRecurse(I, xABCD, yABCD, xBCD, yBCD, xCD, yCD, xD, yD);
174 :h3.TOOBIG() - Macro to Test if a Coordinate is Too Big to Bezier SubDivide Normally
176 Intermediate values in the Bezier subdivision are 8 times bigger than
177 the starting values. If this overflows, a 'long', we are in trouble:
180 #define BITS (sizeof(long)*8)
181 #define HIGHTEST(p) (((p)>>(BITS-4)) != 0) /* includes sign bit */
182 #define TOOBIG(xy) ((xy < 0) ? HIGHTEST(-xy) : HIGHTEST(xy))
185 :h3.StepBezier() - Produce Run Ends for a Bezier Curve
187 This is the entry point called from outside the module.
190 struct segment *StepBezier(R, xA, yA, xB, yB, xC, yC, xD, yD)
191 struct region *R; /* Region under construction or NULL */
192 fractpel xA,yA; /* A control point */
193 fractpel xB,yB; /* B control point */
194 fractpel xC,yC; /* C control point */
195 fractpel xD,yD; /* D control point */
197 struct bezierinfo Info;
210 if ( TOOBIG(xB) || TOOBIG(yB) || TOOBIG(xC) || TOOBIG(yC)
211 || TOOBIG(xD) || TOOBIG(yD) )
212 abort("Beziers this big not yet supported");
214 return(StepBezierRecurse(&Info,
215 (fractpel) 0, (fractpel) 0, xB, yB, xC, yC, xD, yD));