1706f2543Smrg/***********************************************************
2706f2543Smrg
3706f2543SmrgCopyright 1987, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included in
12706f2543Smrgall copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15706f2543SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16706f2543SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17706f2543SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18706f2543SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19706f2543SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20706f2543Smrg
21706f2543SmrgExcept as contained in this notice, the name of The Open Group shall not be
22706f2543Smrgused in advertising or otherwise to promote the sale, use or other dealings
23706f2543Smrgin this Software without prior written authorization from The Open Group.
24706f2543Smrg
25706f2543Smrg
26706f2543SmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27706f2543Smrg
28706f2543Smrg                        All Rights Reserved
29706f2543Smrg
30706f2543SmrgPermission to use, copy, modify, and distribute this software and its
31706f2543Smrgdocumentation for any purpose and without fee is hereby granted,
32706f2543Smrgprovided that the above copyright notice appear in all copies and that
33706f2543Smrgboth that copyright notice and this permission notice appear in
34706f2543Smrgsupporting documentation, and that the name of Digital not be
35706f2543Smrgused in advertising or publicity pertaining to distribution of the
36706f2543Smrgsoftware without specific, written prior permission.
37706f2543Smrg
38706f2543SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39706f2543SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40706f2543SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41706f2543SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42706f2543SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43706f2543SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44706f2543SmrgSOFTWARE.
45706f2543Smrg
46706f2543Smrg******************************************************************/
47706f2543Smrg
48706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
49706f2543Smrg#include <dix-config.h>
50706f2543Smrg#endif
51706f2543Smrg
52706f2543Smrg#include "gcstruct.h"
53706f2543Smrg#include "pixmap.h"
54706f2543Smrg#include "mi.h"
55706f2543Smrg#include "miscanfill.h"
56706f2543Smrg
57706f2543Smrgstatic int getPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty);
58706f2543Smrg
59706f2543Smrg/*
60706f2543Smrg *     convexpoly.c
61706f2543Smrg *
62706f2543Smrg *     Written by Brian Kelleher; Dec. 1985.
63706f2543Smrg *
64706f2543Smrg *     Fill a convex polygon.  If the given polygon
65706f2543Smrg *     is not convex, then the result is undefined.
66706f2543Smrg *     The algorithm is to order the edges from smallest
67706f2543Smrg *     y to largest by partitioning the array into a left
68706f2543Smrg *     edge list and a right edge list.  The algorithm used
69706f2543Smrg *     to traverse each edge is an extension of Bresenham's
70706f2543Smrg *     line algorithm with y as the major axis.
71706f2543Smrg *     For a derivation of the algorithm, see the author of
72706f2543Smrg *     this code.
73706f2543Smrg */
74706f2543SmrgBool
75706f2543SmrgmiFillConvexPoly(
76706f2543Smrg    DrawablePtr dst,
77706f2543Smrg    GCPtr	pgc,
78706f2543Smrg    int		count,                /* number of points        */
79706f2543Smrg    DDXPointPtr ptsIn                 /* the points              */
80706f2543Smrg    )
81706f2543Smrg{
82706f2543Smrg    int xl = 0, xr = 0; /* x vals of left and right edges */
83706f2543Smrg    int dl = 0, dr = 0; /* decision variables             */
84706f2543Smrg    int ml = 0, m1l = 0;/* left edge slope and slope+1    */
85706f2543Smrg    int mr = 0, m1r = 0;         /* right edge slope and slope+1   */
86706f2543Smrg    int incr1l = 0, incr2l = 0;  /* left edge error increments     */
87706f2543Smrg    int incr1r = 0, incr2r = 0;  /* right edge error increments    */
88706f2543Smrg    int dy;                      /* delta y                        */
89706f2543Smrg    int y;                       /* current scanline               */
90706f2543Smrg    int left, right;             /* indices to first endpoints     */
91706f2543Smrg    int i;                       /* loop counter                   */
92706f2543Smrg    int nextleft, nextright;     /* indices to second endpoints    */
93706f2543Smrg    DDXPointPtr ptsOut, FirstPoint; /* output buffer               */
94706f2543Smrg    int *width, *FirstWidth;     /* output buffer                  */
95706f2543Smrg    int imin;                    /* index of smallest vertex (in y) */
96706f2543Smrg    int ymin;                    /* y-extents of polygon            */
97706f2543Smrg    int ymax;
98706f2543Smrg
99706f2543Smrg    /*
100706f2543Smrg     *  find leftx, bottomy, rightx, topy, and the index
101706f2543Smrg     *  of bottomy. Also translate the points.
102706f2543Smrg     */
103706f2543Smrg    imin = getPolyYBounds(ptsIn, count, &ymin, &ymax);
104706f2543Smrg
105706f2543Smrg    dy = ymax - ymin + 1;
106706f2543Smrg    if ((count < 3) || (dy < 0))
107706f2543Smrg	return TRUE;
108706f2543Smrg    ptsOut = FirstPoint = malloc(sizeof(DDXPointRec)*dy);
109706f2543Smrg    width = FirstWidth = malloc(sizeof(int) * dy);
110706f2543Smrg    if(!FirstPoint || !FirstWidth)
111706f2543Smrg    {
112706f2543Smrg	free(FirstWidth);
113706f2543Smrg	free(FirstPoint);
114706f2543Smrg	return FALSE;
115706f2543Smrg    }
116706f2543Smrg
117706f2543Smrg    nextleft = nextright = imin;
118706f2543Smrg    y = ptsIn[nextleft].y;
119706f2543Smrg
120706f2543Smrg    /*
121706f2543Smrg     *  loop through all edges of the polygon
122706f2543Smrg     */
123706f2543Smrg    do {
124706f2543Smrg        /*
125706f2543Smrg         *  add a left edge if we need to
126706f2543Smrg         */
127706f2543Smrg        if (ptsIn[nextleft].y == y) {
128706f2543Smrg            left = nextleft;
129706f2543Smrg
130706f2543Smrg            /*
131706f2543Smrg             *  find the next edge, considering the end
132706f2543Smrg             *  conditions of the array.
133706f2543Smrg             */
134706f2543Smrg            nextleft++;
135706f2543Smrg            if (nextleft >= count)
136706f2543Smrg                nextleft = 0;
137706f2543Smrg
138706f2543Smrg            /*
139706f2543Smrg             *  now compute all of the random information
140706f2543Smrg             *  needed to run the iterative algorithm.
141706f2543Smrg             */
142706f2543Smrg            BRESINITPGON(ptsIn[nextleft].y-ptsIn[left].y,
143706f2543Smrg                         ptsIn[left].x,ptsIn[nextleft].x,
144706f2543Smrg                         xl, dl, ml, m1l, incr1l, incr2l);
145706f2543Smrg        }
146706f2543Smrg
147706f2543Smrg        /*
148706f2543Smrg         *  add a right edge if we need to
149706f2543Smrg         */
150706f2543Smrg        if (ptsIn[nextright].y == y) {
151706f2543Smrg            right = nextright;
152706f2543Smrg
153706f2543Smrg            /*
154706f2543Smrg             *  find the next edge, considering the end
155706f2543Smrg             *  conditions of the array.
156706f2543Smrg             */
157706f2543Smrg            nextright--;
158706f2543Smrg            if (nextright < 0)
159706f2543Smrg                nextright = count-1;
160706f2543Smrg
161706f2543Smrg            /*
162706f2543Smrg             *  now compute all of the random information
163706f2543Smrg             *  needed to run the iterative algorithm.
164706f2543Smrg             */
165706f2543Smrg            BRESINITPGON(ptsIn[nextright].y-ptsIn[right].y,
166706f2543Smrg                         ptsIn[right].x,ptsIn[nextright].x,
167706f2543Smrg                         xr, dr, mr, m1r, incr1r, incr2r);
168706f2543Smrg        }
169706f2543Smrg
170706f2543Smrg        /*
171706f2543Smrg         *  generate scans to fill while we still have
172706f2543Smrg         *  a right edge as well as a left edge.
173706f2543Smrg         */
174706f2543Smrg        i = min(ptsIn[nextleft].y, ptsIn[nextright].y) - y;
175706f2543Smrg	/* in case we're called with non-convex polygon */
176706f2543Smrg	if(i < 0)
177706f2543Smrg        {
178706f2543Smrg	    free(FirstWidth);
179706f2543Smrg	    free(FirstPoint);
180706f2543Smrg	    return TRUE;
181706f2543Smrg	}
182706f2543Smrg        while (i-- > 0)
183706f2543Smrg        {
184706f2543Smrg            ptsOut->y = y;
185706f2543Smrg
186706f2543Smrg            /*
187706f2543Smrg             *  reverse the edges if necessary
188706f2543Smrg             */
189706f2543Smrg            if (xl < xr)
190706f2543Smrg            {
191706f2543Smrg                *(width++) = xr - xl;
192706f2543Smrg                (ptsOut++)->x = xl;
193706f2543Smrg            }
194706f2543Smrg            else
195706f2543Smrg            {
196706f2543Smrg                *(width++) = xl - xr;
197706f2543Smrg                (ptsOut++)->x = xr;
198706f2543Smrg            }
199706f2543Smrg            y++;
200706f2543Smrg
201706f2543Smrg            /* increment down the edges */
202706f2543Smrg            BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
203706f2543Smrg            BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
204706f2543Smrg        }
205706f2543Smrg    }  while (y != ymax);
206706f2543Smrg
207706f2543Smrg    /*
208706f2543Smrg     * Finally, fill the <remaining> spans
209706f2543Smrg     */
210706f2543Smrg    (*pgc->ops->FillSpans)(dst, pgc,
211706f2543Smrg		      ptsOut-FirstPoint,FirstPoint,FirstWidth,
212706f2543Smrg		      1);
213706f2543Smrg    free(FirstWidth);
214706f2543Smrg    free(FirstPoint);
215706f2543Smrg    return TRUE;
216706f2543Smrg}
217706f2543Smrg
218706f2543Smrg
219706f2543Smrg/*
220706f2543Smrg *     Find the index of the point with the smallest y.
221706f2543Smrg */
222706f2543Smrgstatic int
223706f2543SmrggetPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty)
224706f2543Smrg{
225706f2543Smrg    DDXPointPtr ptMin;
226706f2543Smrg    int ymin, ymax;
227706f2543Smrg    DDXPointPtr ptsStart = pts;
228706f2543Smrg
229706f2543Smrg    ptMin = pts;
230706f2543Smrg    ymin = ymax = (pts++)->y;
231706f2543Smrg
232706f2543Smrg    while (--n > 0) {
233706f2543Smrg        if (pts->y < ymin)
234706f2543Smrg	{
235706f2543Smrg            ptMin = pts;
236706f2543Smrg            ymin = pts->y;
237706f2543Smrg        }
238706f2543Smrg	if(pts->y > ymax)
239706f2543Smrg            ymax = pts->y;
240706f2543Smrg
241706f2543Smrg        pts++;
242706f2543Smrg    }
243706f2543Smrg
244706f2543Smrg    *by = ymin;
245706f2543Smrg    *ty = ymax;
246706f2543Smrg    return ptMin-ptsStart;
247706f2543Smrg}
248