First import
[xorg_rtime.git] / xorg-server-1.4 / mi / mipolycon.c
blobe2d666e51cc578f8bd15b8723333fe1fce69e3b7
1 /***********************************************************
3 Copyright 1987, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
26 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28 All Rights Reserved
30 Permission to use, copy, modify, and distribute this software and its
31 documentation for any purpose and without fee is hereby granted,
32 provided that the above copyright notice appear in all copies and that
33 both that copyright notice and this permission notice appear in
34 supporting documentation, and that the name of Digital not be
35 used in advertising or publicity pertaining to distribution of the
36 software without specific, written prior permission.
38 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44 SOFTWARE.
46 ******************************************************************/
48 #ifdef HAVE_DIX_CONFIG_H
49 #include <dix-config.h>
50 #endif
52 #include "gcstruct.h"
53 #include "pixmap.h"
54 #include "mi.h"
55 #include "miscanfill.h"
57 static int getPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty);
60 * convexpoly.c
62 * Written by Brian Kelleher; Dec. 1985.
64 * Fill a convex polygon. If the given polygon
65 * is not convex, then the result is undefined.
66 * The algorithm is to order the edges from smallest
67 * y to largest by partitioning the array into a left
68 * edge list and a right edge list. The algorithm used
69 * to traverse each edge is an extension of Bresenham's
70 * line algorithm with y as the major axis.
71 * For a derivation of the algorithm, see the author of
72 * this code.
74 _X_EXPORT Bool
75 miFillConvexPoly(dst, pgc, count, ptsIn)
76 DrawablePtr dst;
77 GCPtr pgc;
78 int count; /* number of points */
79 DDXPointPtr ptsIn; /* the points */
81 int xl = 0, xr = 0; /* x vals of left and right edges */
82 int dl = 0, dr = 0; /* decision variables */
83 int ml = 0, m1l = 0;/* left edge slope and slope+1 */
84 int mr = 0, m1r = 0; /* right edge slope and slope+1 */
85 int incr1l = 0, incr2l = 0; /* left edge error increments */
86 int incr1r = 0, incr2r = 0; /* right edge error increments */
87 int dy; /* delta y */
88 int y; /* current scanline */
89 int left, right; /* indices to first endpoints */
90 int i; /* loop counter */
91 int nextleft, nextright; /* indices to second endpoints */
92 DDXPointPtr ptsOut, FirstPoint; /* output buffer */
93 int *width, *FirstWidth; /* output buffer */
94 int imin; /* index of smallest vertex (in y) */
95 int ymin; /* y-extents of polygon */
96 int ymax;
99 * find leftx, bottomy, rightx, topy, and the index
100 * of bottomy. Also translate the points.
102 imin = getPolyYBounds(ptsIn, count, &ymin, &ymax);
104 dy = ymax - ymin + 1;
105 if ((count < 3) || (dy < 0))
106 return(TRUE);
107 ptsOut = FirstPoint = (DDXPointPtr )ALLOCATE_LOCAL(sizeof(DDXPointRec)*dy);
108 width = FirstWidth = (int *)ALLOCATE_LOCAL(sizeof(int) * dy);
109 if(!FirstPoint || !FirstWidth)
111 if (FirstWidth) DEALLOCATE_LOCAL(FirstWidth);
112 if (FirstPoint) DEALLOCATE_LOCAL(FirstPoint);
113 return(FALSE);
116 nextleft = nextright = imin;
117 y = ptsIn[nextleft].y;
120 * loop through all edges of the polygon
122 do {
124 * add a left edge if we need to
126 if (ptsIn[nextleft].y == y) {
127 left = nextleft;
130 * find the next edge, considering the end
131 * conditions of the array.
133 nextleft++;
134 if (nextleft >= count)
135 nextleft = 0;
138 * now compute all of the random information
139 * needed to run the iterative algorithm.
141 BRESINITPGON(ptsIn[nextleft].y-ptsIn[left].y,
142 ptsIn[left].x,ptsIn[nextleft].x,
143 xl, dl, ml, m1l, incr1l, incr2l);
147 * add a right edge if we need to
149 if (ptsIn[nextright].y == y) {
150 right = nextright;
153 * find the next edge, considering the end
154 * conditions of the array.
156 nextright--;
157 if (nextright < 0)
158 nextright = count-1;
161 * now compute all of the random information
162 * needed to run the iterative algorithm.
164 BRESINITPGON(ptsIn[nextright].y-ptsIn[right].y,
165 ptsIn[right].x,ptsIn[nextright].x,
166 xr, dr, mr, m1r, incr1r, incr2r);
170 * generate scans to fill while we still have
171 * a right edge as well as a left edge.
173 i = min(ptsIn[nextleft].y, ptsIn[nextright].y) - y;
174 /* in case we're called with non-convex polygon */
175 if(i < 0)
177 DEALLOCATE_LOCAL(FirstWidth);
178 DEALLOCATE_LOCAL(FirstPoint);
179 return(TRUE);
181 while (i-- > 0)
183 ptsOut->y = y;
186 * reverse the edges if necessary
188 if (xl < xr)
190 *(width++) = xr - xl;
191 (ptsOut++)->x = xl;
193 else
195 *(width++) = xl - xr;
196 (ptsOut++)->x = xr;
198 y++;
200 /* increment down the edges */
201 BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
202 BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
204 } while (y != ymax);
207 * Finally, fill the <remaining> spans
209 (*pgc->ops->FillSpans)(dst, pgc,
210 ptsOut-FirstPoint,FirstPoint,FirstWidth,
212 DEALLOCATE_LOCAL(FirstWidth);
213 DEALLOCATE_LOCAL(FirstPoint);
214 return(TRUE);
219 * Find the index of the point with the smallest y.
221 static int
222 getPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty)
224 DDXPointPtr ptMin;
225 int ymin, ymax;
226 DDXPointPtr ptsStart = pts;
228 ptMin = pts;
229 ymin = ymax = (pts++)->y;
231 while (--n > 0) {
232 if (pts->y < ymin)
234 ptMin = pts;
235 ymin = pts->y;
237 if(pts->y > ymax)
238 ymax = pts->y;
240 pts++;
243 *by = ymin;
244 *ty = ymax;
245 return(ptMin-ptsStart);