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#ifdef HAVE_DIX_CONFIG_H
48706f2543Smrg#include <dix-config.h>
49706f2543Smrg#endif
50706f2543Smrg
51706f2543Smrg#include <X11/X.h>
52706f2543Smrg#include "gcstruct.h"
53706f2543Smrg#include "miscanfill.h"
54706f2543Smrg#include "mipoly.h"
55706f2543Smrg#include "pixmap.h"
56706f2543Smrg#include "mi.h"
57706f2543Smrg
58706f2543Smrg/*
59706f2543Smrg *
60706f2543Smrg *     Written by Brian Kelleher;  Oct. 1985
61706f2543Smrg *
62706f2543Smrg *     Routine to fill a polygon.  Two fill rules are
63706f2543Smrg *     supported: frWINDING and frEVENODD.
64706f2543Smrg *
65706f2543Smrg *     See fillpoly.h for a complete description of the algorithm.
66706f2543Smrg */
67706f2543Smrg
68706f2543SmrgBool
69706f2543SmrgmiFillGeneralPoly(
70706f2543Smrg    DrawablePtr dst,
71706f2543Smrg    GCPtr	pgc,
72706f2543Smrg    int		count,              /* number of points        */
73706f2543Smrg    DDXPointPtr ptsIn               /* the points              */
74706f2543Smrg    )
75706f2543Smrg{
76706f2543Smrg    EdgeTableEntry *pAET;  /* the Active Edge Table   */
77706f2543Smrg    int y;                 /* the current scanline    */
78706f2543Smrg    int nPts = 0;          /* number of pts in buffer */
79706f2543Smrg    EdgeTableEntry *pWETE; /* Winding Edge Table      */
80706f2543Smrg    ScanLineList *pSLL;    /* Current ScanLineList    */
81706f2543Smrg    DDXPointPtr ptsOut;      /* ptr to output buffers   */
82706f2543Smrg    int *width;
83706f2543Smrg    DDXPointRec FirstPoint[NUMPTSTOBUFFER]; /* the output buffers */
84706f2543Smrg    int FirstWidth[NUMPTSTOBUFFER];
85706f2543Smrg    EdgeTableEntry *pPrevAET;       /* previous AET entry      */
86706f2543Smrg    EdgeTable ET;                   /* Edge Table header node  */
87706f2543Smrg    EdgeTableEntry AET;             /* Active ET header node   */
88706f2543Smrg    EdgeTableEntry *pETEs;          /* Edge Table Entries buff */
89706f2543Smrg    ScanLineListBlock SLLBlock;     /* header for ScanLineList */
90706f2543Smrg    int fixWAET = 0;
91706f2543Smrg
92706f2543Smrg    if (count < 3)
93706f2543Smrg	return TRUE;
94706f2543Smrg
95706f2543Smrg    if(!(pETEs = malloc(sizeof(EdgeTableEntry) * count)))
96706f2543Smrg	return FALSE;
97706f2543Smrg    ptsOut = FirstPoint;
98706f2543Smrg    width = FirstWidth;
99706f2543Smrg    if (!miCreateETandAET(count, ptsIn, &ET, &AET, pETEs, &SLLBlock))
100706f2543Smrg    {
101706f2543Smrg	free(pETEs);
102706f2543Smrg	return FALSE;
103706f2543Smrg    }
104706f2543Smrg    pSLL = ET.scanlines.next;
105706f2543Smrg
106706f2543Smrg    if (pgc->fillRule == EvenOddRule)
107706f2543Smrg    {
108706f2543Smrg        /*
109706f2543Smrg         *  for each scanline
110706f2543Smrg         */
111706f2543Smrg        for (y = ET.ymin; y < ET.ymax; y++)
112706f2543Smrg        {
113706f2543Smrg            /*
114706f2543Smrg             *  Add a new edge to the active edge table when we
115706f2543Smrg             *  get to the next edge.
116706f2543Smrg             */
117706f2543Smrg            if (pSLL && y == pSLL->scanline)
118706f2543Smrg            {
119706f2543Smrg                miloadAET(&AET, pSLL->edgelist);
120706f2543Smrg                pSLL = pSLL->next;
121706f2543Smrg            }
122706f2543Smrg            pPrevAET = &AET;
123706f2543Smrg            pAET = AET.next;
124706f2543Smrg
125706f2543Smrg            /*
126706f2543Smrg             *  for each active edge
127706f2543Smrg             */
128706f2543Smrg            while (pAET)
129706f2543Smrg            {
130706f2543Smrg                ptsOut->x = pAET->bres.minor;
131706f2543Smrg		ptsOut++->y = y;
132706f2543Smrg                *width++ = pAET->next->bres.minor - pAET->bres.minor;
133706f2543Smrg                nPts++;
134706f2543Smrg
135706f2543Smrg                /*
136706f2543Smrg                 *  send out the buffer when its full
137706f2543Smrg                 */
138706f2543Smrg                if (nPts == NUMPTSTOBUFFER)
139706f2543Smrg		{
140706f2543Smrg		    (*pgc->ops->FillSpans)(dst, pgc,
141706f2543Smrg				      nPts, FirstPoint, FirstWidth,
142706f2543Smrg				      1);
143706f2543Smrg                    ptsOut = FirstPoint;
144706f2543Smrg                    width = FirstWidth;
145706f2543Smrg                    nPts = 0;
146706f2543Smrg                }
147706f2543Smrg                EVALUATEEDGEEVENODD(pAET, pPrevAET, y)
148706f2543Smrg                EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
149706f2543Smrg            }
150706f2543Smrg            miInsertionSort(&AET);
151706f2543Smrg        }
152706f2543Smrg    }
153706f2543Smrg    else      /* default to WindingNumber */
154706f2543Smrg    {
155706f2543Smrg        /*
156706f2543Smrg         *  for each scanline
157706f2543Smrg         */
158706f2543Smrg        for (y = ET.ymin; y < ET.ymax; y++)
159706f2543Smrg        {
160706f2543Smrg            /*
161706f2543Smrg             *  Add a new edge to the active edge table when we
162706f2543Smrg             *  get to the next edge.
163706f2543Smrg             */
164706f2543Smrg            if (pSLL && y == pSLL->scanline)
165706f2543Smrg            {
166706f2543Smrg                miloadAET(&AET, pSLL->edgelist);
167706f2543Smrg                micomputeWAET(&AET);
168706f2543Smrg                pSLL = pSLL->next;
169706f2543Smrg            }
170706f2543Smrg            pPrevAET = &AET;
171706f2543Smrg            pAET = AET.next;
172706f2543Smrg            pWETE = pAET;
173706f2543Smrg
174706f2543Smrg            /*
175706f2543Smrg             *  for each active edge
176706f2543Smrg             */
177706f2543Smrg            while (pAET)
178706f2543Smrg            {
179706f2543Smrg                /*
180706f2543Smrg                 *  if the next edge in the active edge table is
181706f2543Smrg                 *  also the next edge in the winding active edge
182706f2543Smrg                 *  table.
183706f2543Smrg                 */
184706f2543Smrg                if (pWETE == pAET)
185706f2543Smrg                {
186706f2543Smrg                    ptsOut->x = pAET->bres.minor;
187706f2543Smrg		    ptsOut++->y = y;
188706f2543Smrg                    *width++ = pAET->nextWETE->bres.minor - pAET->bres.minor;
189706f2543Smrg                    nPts++;
190706f2543Smrg
191706f2543Smrg                    /*
192706f2543Smrg                     *  send out the buffer
193706f2543Smrg                     */
194706f2543Smrg                    if (nPts == NUMPTSTOBUFFER)
195706f2543Smrg                    {
196706f2543Smrg			(*pgc->ops->FillSpans)(dst, pgc, nPts, FirstPoint,
197706f2543Smrg			                  FirstWidth, 1);
198706f2543Smrg                        ptsOut = FirstPoint;
199706f2543Smrg                        width  = FirstWidth;
200706f2543Smrg                        nPts = 0;
201706f2543Smrg                    }
202706f2543Smrg
203706f2543Smrg                    pWETE = pWETE->nextWETE;
204706f2543Smrg                    while (pWETE != pAET)
205706f2543Smrg                        EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
206706f2543Smrg                    pWETE = pWETE->nextWETE;
207706f2543Smrg                }
208706f2543Smrg                EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
209706f2543Smrg            }
210706f2543Smrg
211706f2543Smrg            /*
212706f2543Smrg             *  reevaluate the Winding active edge table if we
213706f2543Smrg             *  just had to resort it or if we just exited an edge.
214706f2543Smrg             */
215706f2543Smrg            if (miInsertionSort(&AET) || fixWAET)
216706f2543Smrg            {
217706f2543Smrg                micomputeWAET(&AET);
218706f2543Smrg                fixWAET = 0;
219706f2543Smrg            }
220706f2543Smrg        }
221706f2543Smrg    }
222706f2543Smrg
223706f2543Smrg    /*
224706f2543Smrg     *     Get any spans that we missed by buffering
225706f2543Smrg     */
226706f2543Smrg    (*pgc->ops->FillSpans)(dst, pgc, nPts, FirstPoint, FirstWidth, 1);
227706f2543Smrg    free(pETEs);
228706f2543Smrg    miFreeStorage(SLLBlock.next);
229706f2543Smrg    return TRUE;
230706f2543Smrg}
231