1
/* $XFree86: xc/programs/Xserver/mi/mispans.c,v 3.1 1998/10/04 09:39:33 dawes Exp $ */
2
/***********************************************************
4
Copyright 1989, 1998 The Open Group
8
The above copyright notice and this permission notice shall be included in
9
all copies or substantial portions of the Software.
11
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
15
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18
Except as contained in this notice, the name of The Open Group shall not be
19
used in advertising or otherwise to promote the sale, use or other dealings
20
in this Software without prior written authorization from The Open Group.
23
Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
27
Permission to use, copy, modify, and distribute this software and its
28
documentation for any purpose and without fee is hereby granted,
29
provided that the above copyright notice appear in all copies and that
30
both that copyright notice and this permission notice appear in
31
supporting documentation, and that the name of Digital not be
32
used in advertising or publicity pertaining to distribution of the
33
software without specific, written prior permission.
35
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43
******************************************************************/
45
/* $TOG: mispans.c /main/7 1998/02/09 14:48:44 kaleb $ */
50
#include <string.h> /* for memmove */
54
These routines maintain lists of Spans, in order to implement the
55
``touch-each-pixel-once'' rules of wide lines and arcs.
57
Written by Joel McCormack, Summer 1989.
62
void miInitSpanGroup(SpanGroup *spanGroup)
66
spanGroup->group = NULL;
67
spanGroup->ymin = SHRT_MAX;
68
spanGroup->ymax = SHRT_MIN;
71
#define YMIN(spans) (spans->points[0].y)
72
#define YMAX(spans) (spans->points[spans->count-1].y)
74
void miSubtractSpans (SpanGroup *spanGroup, Spans *sub)
76
int i, subCount, spansCount;
77
int ymin, ymax, xmin, xmax;
79
GdkSpan* subPt, *spansPt;
84
spans = spanGroup->group;
85
for (i = spanGroup->count; i; i--, spans++) {
86
if (YMIN(spans) <= ymax && ymin <= YMAX(spans)) {
87
subCount = sub->count;
89
spansCount = spans->count;
90
spansPt = spans->points;
94
while (spansCount && spansPt->y < subPt->y)
96
spansPt++; spansCount--;
100
while (subCount && subPt->y < spansPt->y)
106
if (subPt->y == spansPt->y)
109
xmax = xmin + subPt->width;
110
if (xmin >= (spansPt->x + spansPt->width) || spansPt->x >= xmax)
114
else if (xmin <= spansPt->x)
116
if (xmax >= (spansPt->x + spansPt->width))
118
g_memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
125
spansPt->width -= xmax - spansPt->x;
131
if (xmax >= (spansPt->x + spansPt->width))
133
spansPt->width = xmin - spansPt->x;
141
newPt = (GdkSpan*) g_realloc (spans->points, (spans->count + EXTRA) * sizeof (GdkSpan));
144
spansPt = newPt + (spansPt - spans->points);
145
spans->points = newPt;
148
g_memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
151
spansPt->width = xmin - spansPt->x;
153
spansPt->width -= xmax - spansPt->x;
158
spansPt++; spansCount--;
164
void miAppendSpans(SpanGroup *spanGroup, SpanGroup *otherGroup, Spans *spans)
166
register int ymin, ymax;
167
register int spansCount;
169
spansCount = spans->count;
170
if (spansCount > 0) {
171
if (spanGroup->size == spanGroup->count) {
172
spanGroup->size = (spanGroup->size + 8) * 2;
173
spanGroup->group = (Spans *)
174
g_realloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
177
spanGroup->group[spanGroup->count] = *spans;
178
(spanGroup->count)++;
179
ymin = spans->points[0].y;
180
if (ymin < spanGroup->ymin) spanGroup->ymin = ymin;
181
ymax = spans->points[spansCount - 1].y;
182
if (ymax > spanGroup->ymax) spanGroup->ymax = ymax;
184
otherGroup->ymin < ymax &&
185
ymin < otherGroup->ymax)
187
miSubtractSpans (otherGroup, spans);
192
g_free (spans->points);
196
void miFreeSpanGroup(SpanGroup *spanGroup)
198
if (spanGroup->group != NULL) g_free(spanGroup->group);
201
static void QuickSortSpansX(register GdkSpan points[], register int numSpans)
204
register int i, j, m;
207
/* Always called with numSpans > 1 */
208
/* Sorts only by x, as all y should be the same */
210
#define ExchangeSpans(a, b) \
214
tpt = points[a]; points[a] = points[b]; points[b] = tpt; \
219
/* Do insertion sort */
224
do { /* while i != numSpans */
227
/* points[i] is out of order. Move into proper location. */
231
for (j = 0; x >= points[j].x; j++) {}
233
for (k = i; k != j; k--) {
234
points[k] = points[k-1];
238
} /* if out of order */
241
} while (i != numSpans);
245
/* Choose partition element, stick in location 0 */
247
if (points[m].x > points[0].x) ExchangeSpans(m, 0);
248
if (points[m].x > points[numSpans-1].x) ExchangeSpans(m, numSpans-1);
249
if (points[m].x > points[0].x) ExchangeSpans(m, 0);
252
/* Partition array */
260
} while (i != numSpans && r->x < x);
266
if (i < j) ExchangeSpans(i, j);
269
/* Move partition element back to middle */
273
if (numSpans-j-1 > 1)
274
QuickSortSpansX(&points[j+1], numSpans-j-1);
276
} while (numSpans > 1);
277
} /* QuickSortSpans */
280
static int UniquifySpansX(Spans *spans, register GdkSpan *newPoints)
282
register int newx1, newx2, oldpt, i, y;
283
GdkSpan *oldPoints, *startNewPoints = newPoints;
285
/* Always called with numSpans > 1 */
286
/* Uniquify the spans, and stash them into newPoints and newWidths. Return the
287
number of unique spans. */
290
oldPoints = spans->points;
293
newx1 = oldPoints->x;
294
newx2 = newx1 + oldPoints->width;
296
for (i = spans->count-1; i != 0; i--) {
298
oldpt = oldPoints->x;
300
/* Write current span, start a new one */
301
newPoints->x = newx1;
303
newPoints->width = newx2 - newx1;
306
newx2 = oldpt + oldPoints->width;
308
/* extend current span, if old extends beyond new */
309
oldpt = oldpt + oldPoints->width;
310
if (oldpt > newx2) newx2 = oldpt;
314
/* Write final span */
315
newPoints->x = newx1;
316
newPoints->width = newx2 - newx1;
319
return (newPoints - startNewPoints) + 1;
320
} /* UniquifySpansX */
323
miDisposeSpanGroup (SpanGroup *spanGroup)
328
for (i = 0; i < spanGroup->count; i++)
330
spans = spanGroup->group + i;
331
g_free (spans->points);
335
void miFillUniqueSpanGroup(GdkDrawable *pDraw, GdkGC *pGC, SpanGroup *spanGroup)
338
register Spans *spans;
339
register Spans *yspans;
340
register int *ysizes;
341
register int ymin, ylength;
343
/* Outgoing spans for one big call to FillSpans */
344
register GdkSpan* points;
347
if (spanGroup->count == 0) return;
349
if (spanGroup->count == 1) {
350
/* Already should be sorted, unique */
351
spans = spanGroup->group;
352
gdk_fb_fill_spans(pDraw, pGC, spans->points, spans->count, TRUE);
353
g_free(spans->points);
357
/* Yuck. Gross. Radix sort into y buckets, then sort x and uniquify */
358
/* This seems to be the fastest thing to do. I've tried sorting on
359
both x and y at the same time rather than creating into all those
360
y buckets, but it was somewhat slower. */
362
ymin = spanGroup->ymin;
363
ylength = spanGroup->ymax - ymin + 1;
365
/* Allocate Spans for y buckets */
366
yspans = (Spans *) g_malloc(ylength * sizeof(Spans));
367
ysizes = (int *) g_malloc(ylength * sizeof (int));
369
if (!yspans || !ysizes)
375
miDisposeSpanGroup (spanGroup);
379
for (i = 0; i != ylength; i++) {
382
yspans[i].points = NULL;
385
/* Go through every single span and put it into the correct bucket */
387
for (i = 0, spans = spanGroup->group;
388
i != spanGroup->count;
393
for (j = 0, points = spans->points;
396
index = points->y - ymin;
397
if (index >= 0 && index < ylength) {
398
Spans *newspans = &(yspans[index]);
399
if (newspans->count == ysizes[index]) {
401
ysizes[index] = (ysizes[index] + 8) * 2;
402
newpoints = (GdkSpan*) g_realloc(
404
ysizes[index] * sizeof(GdkSpan));
409
for (i = 0; i < ylength; i++)
411
g_free (yspans[i].points);
415
miDisposeSpanGroup (spanGroup);
418
newspans->points = newpoints;
420
newspans->points[newspans->count] = *points;
422
} /* if y value of span in range */
423
} /* for j through spans */
424
count += spans->count;
425
g_free(spans->points);
426
spans->points = NULL;
427
} /* for i thorough Spans */
429
/* Now sort by x and uniquify each bucket into the final array */
430
points = (GdkSpan*) g_malloc(count * sizeof(GdkSpan));
435
for (i = 0; i < ylength; i++)
437
g_free (yspans[i].points);
446
for (i = 0; i != ylength; i++) {
447
int ycount = yspans[i].count;
450
QuickSortSpansX(yspans[i].points, ycount);
451
count += UniquifySpansX
452
(&(yspans[i]), &(points[count]));
454
points[count] = yspans[i].points[0];
457
g_free(yspans[i].points);
461
gdk_fb_fill_spans(pDraw, pGC, points, count, TRUE);
464
g_free(ysizes); /* use (DE)ALLOCATE_LOCAL for these? */
467
spanGroup->count = 0;
468
spanGroup->ymin = SHRT_MAX;
469
spanGroup->ymax = SHRT_MIN;
473
void miFillSpanGroup(GdkDrawable *pDraw, GdkGC *pGC, SpanGroup *spanGroup)
476
register Spans *spans;
478
for (i = 0, spans = spanGroup->group; i != spanGroup->count; i++, spans++) {
479
gdk_fb_fill_spans(pDraw, pGC, spans->points, spans->count, TRUE);
480
g_free(spans->points);
483
spanGroup->count = 0;
484
spanGroup->ymin = SHRT_MAX;
485
spanGroup->ymax = SHRT_MIN;
486
} /* FillSpanGroup */