1706f2543Smrg/***********************************************************
2706f2543Smrg
3706f2543SmrgCopyright 1989, 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 1989 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
49706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
50706f2543Smrg#include <dix-config.h>
51706f2543Smrg#endif
52706f2543Smrg
53706f2543Smrg#include "misc.h"
54706f2543Smrg#include "pixmapstr.h"
55706f2543Smrg#include "gcstruct.h"
56706f2543Smrg#include "mispans.h"
57706f2543Smrg
58706f2543Smrg/*
59706f2543Smrg
60706f2543SmrgThese routines maintain lists of Spans, in order to implement the
61706f2543Smrg``touch-each-pixel-once'' rules of wide lines and arcs.
62706f2543Smrg
63706f2543SmrgWritten by Joel McCormack, Summer 1989.
64706f2543Smrg
65706f2543Smrg*/
66706f2543Smrg
67706f2543Smrg
68706f2543Smrgvoid miInitSpanGroup(SpanGroup *spanGroup)
69706f2543Smrg{
70706f2543Smrg    spanGroup->size = 0;
71706f2543Smrg    spanGroup->count = 0;
72706f2543Smrg    spanGroup->group = NULL;
73706f2543Smrg    spanGroup->ymin = MAXSHORT;
74706f2543Smrg    spanGroup->ymax = MINSHORT;
75706f2543Smrg} /* InitSpanGroup */
76706f2543Smrg
77706f2543Smrg#define YMIN(spans) (spans->points[0].y)
78706f2543Smrg#define YMAX(spans)  (spans->points[spans->count-1].y)
79706f2543Smrg
80706f2543Smrgstatic void miSubtractSpans (SpanGroup *spanGroup, Spans *sub)
81706f2543Smrg{
82706f2543Smrg    int		i, subCount, spansCount;
83706f2543Smrg    int		ymin, ymax, xmin, xmax;
84706f2543Smrg    Spans	*spans;
85706f2543Smrg    DDXPointPtr	subPt, spansPt;
86706f2543Smrg    int		*subWid, *spansWid;
87706f2543Smrg    int		extra;
88706f2543Smrg
89706f2543Smrg    ymin = YMIN(sub);
90706f2543Smrg    ymax = YMAX(sub);
91706f2543Smrg    spans = spanGroup->group;
92706f2543Smrg    for (i = spanGroup->count; i; i--, spans++) {
93706f2543Smrg	if (YMIN(spans) <= ymax && ymin <= YMAX(spans)) {
94706f2543Smrg	    subCount = sub->count;
95706f2543Smrg	    subPt = sub->points;
96706f2543Smrg	    subWid = sub->widths;
97706f2543Smrg	    spansCount = spans->count;
98706f2543Smrg	    spansPt = spans->points;
99706f2543Smrg	    spansWid = spans->widths;
100706f2543Smrg	    extra = 0;
101706f2543Smrg	    for (;;)
102706f2543Smrg 	    {
103706f2543Smrg		while (spansCount && spansPt->y < subPt->y)
104706f2543Smrg		{
105706f2543Smrg		    spansPt++;  spansWid++; spansCount--;
106706f2543Smrg		}
107706f2543Smrg		if (!spansCount)
108706f2543Smrg		    break;
109706f2543Smrg		while (subCount && subPt->y < spansPt->y)
110706f2543Smrg		{
111706f2543Smrg		    subPt++;	subWid++;   subCount--;
112706f2543Smrg		}
113706f2543Smrg		if (!subCount)
114706f2543Smrg		    break;
115706f2543Smrg		if (subPt->y == spansPt->y)
116706f2543Smrg		{
117706f2543Smrg		    xmin = subPt->x;
118706f2543Smrg		    xmax = xmin + *subWid;
119706f2543Smrg		    if (xmin >= spansPt->x + *spansWid || spansPt->x >= xmax)
120706f2543Smrg		    {
121706f2543Smrg			;
122706f2543Smrg		    }
123706f2543Smrg		    else if (xmin <= spansPt->x)
124706f2543Smrg		    {
125706f2543Smrg			if (xmax >= spansPt->x + *spansWid)
126706f2543Smrg			{
127706f2543Smrg			    memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
128706f2543Smrg			    memmove (spansWid, spansWid + 1, sizeof *spansWid * (spansCount - 1));
129706f2543Smrg			    spansPt--;
130706f2543Smrg			    spansWid--;
131706f2543Smrg			    spans->count--;
132706f2543Smrg			    extra++;
133706f2543Smrg			}
134706f2543Smrg			else
135706f2543Smrg			{
136706f2543Smrg			    *spansWid = *spansWid - (xmax - spansPt->x);
137706f2543Smrg			    spansPt->x = xmax;
138706f2543Smrg			}
139706f2543Smrg		    }
140706f2543Smrg		    else
141706f2543Smrg		    {
142706f2543Smrg			if (xmax >= spansPt->x + *spansWid)
143706f2543Smrg			{
144706f2543Smrg			    *spansWid = xmin - spansPt->x;
145706f2543Smrg			}
146706f2543Smrg			else
147706f2543Smrg			{
148706f2543Smrg			    if (!extra) {
149706f2543Smrg				DDXPointPtr newPt;
150706f2543Smrg				int	    *newwid;
151706f2543Smrg
152706f2543Smrg#define EXTRA 8
153706f2543Smrg				newPt = (DDXPointPtr) realloc(spans->points, (spans->count + EXTRA) * sizeof (DDXPointRec));
154706f2543Smrg				if (!newPt)
155706f2543Smrg				    break;
156706f2543Smrg				spansPt = newPt + (spansPt - spans->points);
157706f2543Smrg				spans->points = newPt;
158706f2543Smrg				newwid = (int *) realloc(spans->widths, (spans->count + EXTRA) * sizeof (int));
159706f2543Smrg				if (!newwid)
160706f2543Smrg				    break;
161706f2543Smrg				spansWid = newwid + (spansWid - spans->widths);
162706f2543Smrg				spans->widths = newwid;
163706f2543Smrg				extra = EXTRA;
164706f2543Smrg			    }
165706f2543Smrg			    memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
166706f2543Smrg			    memmove (spansWid + 1, spansWid, sizeof *spansWid * (spansCount));
167706f2543Smrg			    spans->count++;
168706f2543Smrg			    extra--;
169706f2543Smrg			    *spansWid = xmin - spansPt->x;
170706f2543Smrg			    spansWid++;
171706f2543Smrg			    spansPt++;
172706f2543Smrg			    *spansWid = *spansWid - (xmax - spansPt->x);
173706f2543Smrg			    spansPt->x = xmax;
174706f2543Smrg			}
175706f2543Smrg		    }
176706f2543Smrg		}
177706f2543Smrg		spansPt++;  spansWid++; spansCount--;
178706f2543Smrg	    }
179706f2543Smrg	}
180706f2543Smrg    }
181706f2543Smrg}
182706f2543Smrg
183706f2543Smrgvoid miAppendSpans(SpanGroup *spanGroup, SpanGroup *otherGroup, Spans *spans)
184706f2543Smrg{
185706f2543Smrg    int ymin, ymax;
186706f2543Smrg    int spansCount;
187706f2543Smrg
188706f2543Smrg    spansCount = spans->count;
189706f2543Smrg    if (spansCount > 0) {
190706f2543Smrg	if (spanGroup->size == spanGroup->count) {
191706f2543Smrg	    spanGroup->size = (spanGroup->size + 8) * 2;
192706f2543Smrg	    spanGroup->group = (Spans *)
193706f2543Smrg		realloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
194706f2543Smrg	 }
195706f2543Smrg
196706f2543Smrg	spanGroup->group[spanGroup->count] = *spans;
197706f2543Smrg	(spanGroup->count)++;
198706f2543Smrg	ymin = spans->points[0].y;
199706f2543Smrg	if (ymin < spanGroup->ymin) spanGroup->ymin = ymin;
200706f2543Smrg	ymax = spans->points[spansCount - 1].y;
201706f2543Smrg	if (ymax > spanGroup->ymax) spanGroup->ymax = ymax;
202706f2543Smrg	if (otherGroup &&
203706f2543Smrg	    otherGroup->ymin < ymax &&
204706f2543Smrg	    ymin < otherGroup->ymax)
205706f2543Smrg	{
206706f2543Smrg	    miSubtractSpans (otherGroup, spans);
207706f2543Smrg	}
208706f2543Smrg    }
209706f2543Smrg    else
210706f2543Smrg    {
211706f2543Smrg	free(spans->points);
212706f2543Smrg	free(spans->widths);
213706f2543Smrg    }
214706f2543Smrg} /* AppendSpans */
215706f2543Smrg
216706f2543Smrgvoid miFreeSpanGroup(SpanGroup *spanGroup)
217706f2543Smrg{
218706f2543Smrg    free(spanGroup->group);
219706f2543Smrg}
220706f2543Smrg
221706f2543Smrgstatic void QuickSortSpansX(
222706f2543Smrg    DDXPointRec points[],
223706f2543Smrg    int		widths[],
224706f2543Smrg    int		numSpans )
225706f2543Smrg{
226706f2543Smrg    int	    		x;
227706f2543Smrg    int	    		i, j, m;
228706f2543Smrg    DDXPointPtr 	r;
229706f2543Smrg
230706f2543Smrg/* Always called with numSpans > 1 */
231706f2543Smrg/* Sorts only by x, as all y should be the same */
232706f2543Smrg
233706f2543Smrg#define ExchangeSpans(a, b)				    \
234706f2543Smrg{							    \
235706f2543Smrg    DDXPointRec 	tpt;				    \
236706f2543Smrg    int    		tw;				    \
237706f2543Smrg							    \
238706f2543Smrg    tpt = points[a]; points[a] = points[b]; points[b] = tpt;    \
239706f2543Smrg    tw = widths[a]; widths[a] = widths[b]; widths[b] = tw;  \
240706f2543Smrg}
241706f2543Smrg
242706f2543Smrg    do {
243706f2543Smrg	if (numSpans < 9) {
244706f2543Smrg	    /* Do insertion sort */
245706f2543Smrg	    int xprev;
246706f2543Smrg
247706f2543Smrg	    xprev = points[0].x;
248706f2543Smrg	    i = 1;
249706f2543Smrg	    do { /* while i != numSpans */
250706f2543Smrg		x = points[i].x;
251706f2543Smrg		if (xprev > x) {
252706f2543Smrg		    /* points[i] is out of order.  Move into proper location. */
253706f2543Smrg		    DDXPointRec tpt;
254706f2543Smrg		    int	    tw, k;
255706f2543Smrg
256706f2543Smrg		    for (j = 0; x >= points[j].x; j++) {}
257706f2543Smrg		    tpt = points[i];
258706f2543Smrg		    tw  = widths[i];
259706f2543Smrg		    for (k = i; k != j; k--) {
260706f2543Smrg			points[k] = points[k-1];
261706f2543Smrg			widths[k] = widths[k-1];
262706f2543Smrg		    }
263706f2543Smrg		    points[j] = tpt;
264706f2543Smrg		    widths[j] = tw;
265706f2543Smrg		    x = points[i].x;
266706f2543Smrg		} /* if out of order */
267706f2543Smrg		xprev = x;
268706f2543Smrg		i++;
269706f2543Smrg	    } while (i != numSpans);
270706f2543Smrg	    return;
271706f2543Smrg	}
272706f2543Smrg
273706f2543Smrg	/* Choose partition element, stick in location 0 */
274706f2543Smrg	m = numSpans / 2;
275706f2543Smrg	if (points[m].x > points[0].x)		ExchangeSpans(m, 0);
276706f2543Smrg	if (points[m].x > points[numSpans-1].x) ExchangeSpans(m, numSpans-1);
277706f2543Smrg	if (points[m].x > points[0].x)		ExchangeSpans(m, 0);
278706f2543Smrg	x = points[0].x;
279706f2543Smrg
280706f2543Smrg        /* Partition array */
281706f2543Smrg        i = 0;
282706f2543Smrg        j = numSpans;
283706f2543Smrg        do {
284706f2543Smrg	    r = &(points[i]);
285706f2543Smrg	    do {
286706f2543Smrg		r++;
287706f2543Smrg		i++;
288706f2543Smrg            } while (i != numSpans && r->x < x);
289706f2543Smrg	    r = &(points[j]);
290706f2543Smrg	    do {
291706f2543Smrg		r--;
292706f2543Smrg		j--;
293706f2543Smrg            } while (x < r->x);
294706f2543Smrg            if (i < j) ExchangeSpans(i, j);
295706f2543Smrg        } while (i < j);
296706f2543Smrg
297706f2543Smrg        /* Move partition element back to middle */
298706f2543Smrg        ExchangeSpans(0, j);
299706f2543Smrg
300706f2543Smrg	/* Recurse */
301706f2543Smrg        if (numSpans-j-1 > 1)
302706f2543Smrg	    QuickSortSpansX(&points[j+1], &widths[j+1], numSpans-j-1);
303706f2543Smrg        numSpans = j;
304706f2543Smrg    } while (numSpans > 1);
305706f2543Smrg} /* QuickSortSpans */
306706f2543Smrg
307706f2543Smrg
308706f2543Smrgstatic int UniquifySpansX(
309706f2543Smrg    Spans	    	*spans,
310706f2543Smrg    DDXPointRec    	*newPoints,
311706f2543Smrg    int	    		*newWidths )
312706f2543Smrg{
313706f2543Smrg    int 		newx1, newx2, oldpt, i, y;
314706f2543Smrg    DDXPointRec		*oldPoints;
315706f2543Smrg    int	    		*oldWidths;
316706f2543Smrg    int			*startNewWidths;
317706f2543Smrg
318706f2543Smrg/* Always called with numSpans > 1 */
319706f2543Smrg/* Uniquify the spans, and stash them into newPoints and newWidths.  Return the
320706f2543Smrg   number of unique spans. */
321706f2543Smrg
322706f2543Smrg
323706f2543Smrg    startNewWidths = newWidths;
324706f2543Smrg
325706f2543Smrg    oldPoints = spans->points;
326706f2543Smrg    oldWidths = spans->widths;
327706f2543Smrg
328706f2543Smrg    y = oldPoints->y;
329706f2543Smrg    newx1 = oldPoints->x;
330706f2543Smrg    newx2 = newx1 + *oldWidths;
331706f2543Smrg
332706f2543Smrg    for (i = spans->count-1; i != 0; i--) {
333706f2543Smrg	oldPoints++;
334706f2543Smrg	oldWidths++;
335706f2543Smrg	oldpt = oldPoints->x;
336706f2543Smrg	if (oldpt > newx2) {
337706f2543Smrg	    /* Write current span, start a new one */
338706f2543Smrg	    newPoints->x = newx1;
339706f2543Smrg	    newPoints->y = y;
340706f2543Smrg	    *newWidths = newx2 - newx1;
341706f2543Smrg	    newPoints++;
342706f2543Smrg	    newWidths++;
343706f2543Smrg	    newx1 = oldpt;
344706f2543Smrg	    newx2 = oldpt + *oldWidths;
345706f2543Smrg	} else {
346706f2543Smrg	    /* extend current span, if old extends beyond new */
347706f2543Smrg	    oldpt = oldpt + *oldWidths;
348706f2543Smrg	    if (oldpt > newx2) newx2 = oldpt;
349706f2543Smrg	}
350706f2543Smrg    } /* for */
351706f2543Smrg
352706f2543Smrg    /* Write final span */
353706f2543Smrg    newPoints->x = newx1;
354706f2543Smrg    *newWidths = newx2 - newx1;
355706f2543Smrg    newPoints->y = y;
356706f2543Smrg
357706f2543Smrg    return (newWidths - startNewWidths) + 1;
358706f2543Smrg} /* UniquifySpansX */
359706f2543Smrg
360706f2543Smrgstatic void
361706f2543SmrgmiDisposeSpanGroup (SpanGroup *spanGroup)
362706f2543Smrg{
363706f2543Smrg    int	    i;
364706f2543Smrg    Spans   *spans;
365706f2543Smrg
366706f2543Smrg    for (i = 0; i < spanGroup->count; i++)
367706f2543Smrg    {
368706f2543Smrg	spans = spanGroup->group + i;
369706f2543Smrg	free(spans->points);
370706f2543Smrg	free(spans->widths);
371706f2543Smrg    }
372706f2543Smrg}
373706f2543Smrg
374706f2543Smrgvoid miFillUniqueSpanGroup(DrawablePtr pDraw, GCPtr pGC, SpanGroup *spanGroup)
375706f2543Smrg{
376706f2543Smrg    int    		i;
377706f2543Smrg    Spans  		*spans;
378706f2543Smrg    Spans  		*yspans;
379706f2543Smrg    int    		*ysizes;
380706f2543Smrg    int    		ymin, ylength;
381706f2543Smrg
382706f2543Smrg    /* Outgoing spans for one big call to FillSpans */
383706f2543Smrg    DDXPointPtr    	points;
384706f2543Smrg    int	    		*widths;
385706f2543Smrg    int	    		count;
386706f2543Smrg
387706f2543Smrg    if (spanGroup->count == 0) return;
388706f2543Smrg
389706f2543Smrg    if (spanGroup->count == 1) {
390706f2543Smrg	/* Already should be sorted, unique */
391706f2543Smrg	spans = spanGroup->group;
392706f2543Smrg	(*pGC->ops->FillSpans)
393706f2543Smrg	    (pDraw, pGC, spans->count, spans->points, spans->widths, TRUE);
394706f2543Smrg	free(spans->points);
395706f2543Smrg	free(spans->widths);
396706f2543Smrg    }
397706f2543Smrg    else
398706f2543Smrg    {
399706f2543Smrg	/* Yuck.  Gross.  Radix sort into y buckets, then sort x and uniquify */
400706f2543Smrg	/* This seems to be the fastest thing to do.  I've tried sorting on
401706f2543Smrg	   both x and y at the same time rather than creating into all those
402706f2543Smrg	   y buckets, but it was somewhat slower. */
403706f2543Smrg
404706f2543Smrg	ymin    = spanGroup->ymin;
405706f2543Smrg	ylength = spanGroup->ymax - ymin + 1;
406706f2543Smrg
407706f2543Smrg	/* Allocate Spans for y buckets */
408706f2543Smrg	yspans = malloc(ylength * sizeof(Spans));
409706f2543Smrg	ysizes = malloc(ylength * sizeof (int));
410706f2543Smrg
411706f2543Smrg	if (!yspans || !ysizes)
412706f2543Smrg	{
413706f2543Smrg	    free(yspans);
414706f2543Smrg	    free(ysizes);
415706f2543Smrg	    miDisposeSpanGroup (spanGroup);
416706f2543Smrg	    return;
417706f2543Smrg	}
418706f2543Smrg
419706f2543Smrg	for (i = 0; i != ylength; i++) {
420706f2543Smrg	    ysizes[i]        = 0;
421706f2543Smrg	    yspans[i].count  = 0;
422706f2543Smrg	    yspans[i].points = NULL;
423706f2543Smrg	    yspans[i].widths = NULL;
424706f2543Smrg	}
425706f2543Smrg
426706f2543Smrg	/* Go through every single span and put it into the correct bucket */
427706f2543Smrg	count = 0;
428706f2543Smrg	for (i = 0, spans = spanGroup->group;
429706f2543Smrg		i != spanGroup->count;
430706f2543Smrg		i++, spans++) {
431706f2543Smrg	    int		index;
432706f2543Smrg	    int		j;
433706f2543Smrg
434706f2543Smrg	    for (j = 0, points = spans->points, widths = spans->widths;
435706f2543Smrg		    j != spans->count;
436706f2543Smrg		    j++, points++, widths++) {
437706f2543Smrg		index = points->y - ymin;
438706f2543Smrg		if (index >= 0 && index < ylength) {
439706f2543Smrg		    Spans *newspans = &(yspans[index]);
440706f2543Smrg		    if (newspans->count == ysizes[index]) {
441706f2543Smrg			DDXPointPtr newpoints;
442706f2543Smrg			int	    *newwidths;
443706f2543Smrg			ysizes[index] = (ysizes[index] + 8) * 2;
444706f2543Smrg			newpoints = (DDXPointPtr) realloc(
445706f2543Smrg			    newspans->points,
446706f2543Smrg			    ysizes[index] * sizeof(DDXPointRec));
447706f2543Smrg			newwidths = (int *) realloc(
448706f2543Smrg			    newspans->widths,
449706f2543Smrg			    ysizes[index] * sizeof(int));
450706f2543Smrg			if (!newpoints || !newwidths)
451706f2543Smrg			{
452706f2543Smrg			    int	i;
453706f2543Smrg
454706f2543Smrg			    for (i = 0; i < ylength; i++)
455706f2543Smrg			    {
456706f2543Smrg				free(yspans[i].points);
457706f2543Smrg				free(yspans[i].widths);
458706f2543Smrg			    }
459706f2543Smrg			    free(yspans);
460706f2543Smrg			    free(ysizes);
461706f2543Smrg			    free(newpoints);
462706f2543Smrg			    free(newwidths);
463706f2543Smrg			    miDisposeSpanGroup (spanGroup);
464706f2543Smrg			    return;
465706f2543Smrg			}
466706f2543Smrg			newspans->points = newpoints;
467706f2543Smrg			newspans->widths = newwidths;
468706f2543Smrg		    }
469706f2543Smrg		    newspans->points[newspans->count] = *points;
470706f2543Smrg		    newspans->widths[newspans->count] = *widths;
471706f2543Smrg		    (newspans->count)++;
472706f2543Smrg		} /* if y value of span in range */
473706f2543Smrg	    } /* for j through spans */
474706f2543Smrg	    count += spans->count;
475706f2543Smrg	    free(spans->points);
476706f2543Smrg	    spans->points = NULL;
477706f2543Smrg	    free(spans->widths);
478706f2543Smrg	    spans->widths = NULL;
479706f2543Smrg	} /* for i thorough Spans */
480706f2543Smrg
481706f2543Smrg	/* Now sort by x and uniquify each bucket into the final array */
482706f2543Smrg	points = malloc(count * sizeof(DDXPointRec));
483706f2543Smrg	widths = malloc(count * sizeof(int));
484706f2543Smrg	if (!points || !widths)
485706f2543Smrg	{
486706f2543Smrg	    int	i;
487706f2543Smrg
488706f2543Smrg	    for (i = 0; i < ylength; i++)
489706f2543Smrg	    {
490706f2543Smrg		free(yspans[i].points);
491706f2543Smrg		free(yspans[i].widths);
492706f2543Smrg	    }
493706f2543Smrg	    free(yspans);
494706f2543Smrg	    free(ysizes);
495706f2543Smrg	    free(points);
496706f2543Smrg	    free(widths);
497706f2543Smrg	    return;
498706f2543Smrg	}
499706f2543Smrg	count = 0;
500706f2543Smrg	for (i = 0; i != ylength; i++) {
501706f2543Smrg	    int ycount = yspans[i].count;
502706f2543Smrg	    if (ycount > 0) {
503706f2543Smrg		if (ycount > 1) {
504706f2543Smrg		    QuickSortSpansX(yspans[i].points, yspans[i].widths, ycount);
505706f2543Smrg		    count += UniquifySpansX
506706f2543Smrg			(&(yspans[i]), &(points[count]), &(widths[count]));
507706f2543Smrg		} else {
508706f2543Smrg		    points[count] = yspans[i].points[0];
509706f2543Smrg		    widths[count] = yspans[i].widths[0];
510706f2543Smrg		    count++;
511706f2543Smrg		}
512706f2543Smrg		free(yspans[i].points);
513706f2543Smrg		free(yspans[i].widths);
514706f2543Smrg	    }
515706f2543Smrg	}
516706f2543Smrg
517706f2543Smrg	(*pGC->ops->FillSpans) (pDraw, pGC, count, points, widths, TRUE);
518706f2543Smrg	free(points);
519706f2543Smrg	free(widths);
520706f2543Smrg	free(yspans);
521706f2543Smrg	free(ysizes);		/* use (DE)xalloc for these? */
522706f2543Smrg    }
523706f2543Smrg
524706f2543Smrg    spanGroup->count = 0;
525706f2543Smrg    spanGroup->ymin = MAXSHORT;
526706f2543Smrg    spanGroup->ymax = MINSHORT;
527706f2543Smrg}
528