]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/mi/mipolycon.c
Import X server from vnc-3.3.7.
[rdpsrv] / Xserver / programs / Xserver / mi / mipolycon.c
1 /***********************************************************
2
3 Copyright (c) 1987  X Consortium
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
25
26
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28
29                         All Rights Reserved
30
31 Permission to use, copy, modify, and distribute this software and its 
32 documentation for any purpose and without fee is hereby granted, 
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in 
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.  
38
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 SOFTWARE.
46
47 ******************************************************************/
48 /* $XConsortium: mipolycon.c,v 5.1 94/04/17 20:27:43 keith Exp $ */
49 #include "gcstruct.h"
50 #include "pixmap.h"
51 #include "miscanfill.h"
52
53 static int getPolyYBounds();
54
55 /*
56  *     convexpoly.c
57  *
58  *     Written by Brian Kelleher; Dec. 1985.
59  *
60  *     Fill a convex polygon.  If the given polygon
61  *     is not convex, then the result is undefined.
62  *     The algorithm is to order the edges from smallest
63  *     y to largest by partitioning the array into a left
64  *     edge list and a right edge list.  The algorithm used
65  *     to traverse each edge is an extension of Bresenham's
66  *     line algorithm with y as the major axis.
67  *     For a derivation of the algorithm, see the author of
68  *     this code.
69  */
70 Bool
71 miFillConvexPoly(dst, pgc, count, ptsIn)
72     DrawablePtr dst;
73     GCPtr       pgc;
74     int         count;                /* number of points        */
75     DDXPointPtr ptsIn;                /* the points              */
76 {
77     register int xl, xr;        /* x vals of left and right edges */
78     register int dl, dr;        /* decision variables             */
79     register int ml, m1l;       /* left edge slope and slope+1    */
80     int mr, m1r;                /* right edge slope and slope+1   */
81     int incr1l, incr2l;         /* left edge error increments     */
82     int incr1r, incr2r;         /* right edge error increments    */
83     int dy;                     /* delta y                        */
84     int y;                      /* current scanline               */
85     int left, right;            /* indices to first endpoints     */
86     int i;                      /* loop counter                   */
87     int nextleft, nextright;    /* indices to second endpoints    */
88     DDXPointPtr ptsOut, FirstPoint; /* output buffer               */
89     int *width, *FirstWidth;    /* output buffer                  */
90     int imin;                   /* index of smallest vertex (in y) */
91     int ymin;                   /* y-extents of polygon            */
92     int ymax;
93
94     /*
95      *  find leftx, bottomy, rightx, topy, and the index
96      *  of bottomy. Also translate the points.
97      */
98     imin = getPolyYBounds(ptsIn, count, &ymin, &ymax);
99
100     dy = ymax - ymin + 1;
101     if ((count < 3) || (dy < 0))
102         return(TRUE);
103     ptsOut = FirstPoint = (DDXPointPtr )ALLOCATE_LOCAL(sizeof(DDXPointRec)*dy);
104     width = FirstWidth = (int *)ALLOCATE_LOCAL(sizeof(int) * dy);
105     if(!FirstPoint || !FirstWidth)
106     {
107         if (FirstWidth) DEALLOCATE_LOCAL(FirstWidth);
108         if (FirstPoint) DEALLOCATE_LOCAL(FirstPoint);
109         return(FALSE);
110     }
111
112     nextleft = nextright = imin;
113     y = ptsIn[nextleft].y;
114
115     /*
116      *  loop through all edges of the polygon
117      */
118     do {
119         /*
120          *  add a left edge if we need to
121          */
122         if (ptsIn[nextleft].y == y) {
123             left = nextleft;
124
125             /*
126              *  find the next edge, considering the end
127              *  conditions of the array.
128              */
129             nextleft++;
130             if (nextleft >= count)
131                 nextleft = 0;
132
133             /*
134              *  now compute all of the random information
135              *  needed to run the iterative algorithm.
136              */
137             BRESINITPGON(ptsIn[nextleft].y-ptsIn[left].y,
138                          ptsIn[left].x,ptsIn[nextleft].x,
139                          xl, dl, ml, m1l, incr1l, incr2l);
140         }
141
142         /*
143          *  add a right edge if we need to
144          */
145         if (ptsIn[nextright].y == y) {
146             right = nextright;
147
148             /*
149              *  find the next edge, considering the end
150              *  conditions of the array.
151              */
152             nextright--;
153             if (nextright < 0)
154                 nextright = count-1;
155
156             /*
157              *  now compute all of the random information
158              *  needed to run the iterative algorithm.
159              */
160             BRESINITPGON(ptsIn[nextright].y-ptsIn[right].y,
161                          ptsIn[right].x,ptsIn[nextright].x,
162                          xr, dr, mr, m1r, incr1r, incr2r);
163         }
164
165         /*
166          *  generate scans to fill while we still have
167          *  a right edge as well as a left edge.
168          */
169         i = min(ptsIn[nextleft].y, ptsIn[nextright].y) - y;
170         /* in case we're called with non-convex polygon */
171         if(i < 0)
172         {
173             DEALLOCATE_LOCAL(FirstWidth);
174             DEALLOCATE_LOCAL(FirstPoint);
175             return(TRUE);
176         }
177         while (i-- > 0) 
178         {
179             ptsOut->y = y;
180
181             /*
182              *  reverse the edges if necessary
183              */
184             if (xl < xr) 
185             {
186                 *(width++) = xr - xl;
187                 (ptsOut++)->x = xl;
188             }
189             else 
190             {
191                 *(width++) = xl - xr;
192                 (ptsOut++)->x = xr;
193             }
194             y++;
195
196             /* increment down the edges */
197             BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
198             BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
199         }
200     }  while (y != ymax);
201
202     /*
203      * Finally, fill the <remaining> spans
204      */
205     (*pgc->ops->FillSpans)(dst, pgc, 
206                       ptsOut-FirstPoint,FirstPoint,FirstWidth,
207                       1);
208     DEALLOCATE_LOCAL(FirstWidth);
209     DEALLOCATE_LOCAL(FirstPoint);
210     return(TRUE);
211 }
212
213 \f
214 /*
215  *     Find the index of the point with the smallest y.
216  */
217 static
218 int
219 getPolyYBounds(pts, n, by, ty)
220     DDXPointPtr pts;
221     int n;
222     int *by, *ty;
223 {
224     register DDXPointPtr ptMin;
225     int ymin, ymax;
226     DDXPointPtr ptsStart = pts;
227
228     ptMin = pts;
229     ymin = ymax = (pts++)->y;
230
231     while (--n > 0) {
232         if (pts->y < ymin)
233         {
234             ptMin = pts;
235             ymin = pts->y;
236         }
237         if(pts->y > ymax)
238             ymax = pts->y;
239
240         pts++;
241     }
242
243     *by = ymin;
244     *ty = ymax;
245     return(ptMin-ptsStart);
246 }