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 $ */
49 #include <string.h> /* for memmove */
53 These routines maintain lists of Spans, in order to implement the
54 ``touch-each-pixel-once'' rules of wide lines and arcs.
56 Written by Joel McCormack, Summer 1989.
61 void miInitSpanGroup(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, sub)
78 int i, subCount, spansCount;
79 int ymin, ymax, xmin, xmax;
81 GdkSpan* subPt, *spansPt;
86 spans = spanGroup->group;
87 for (i = spanGroup->count; i; i--, spans++) {
88 if (YMIN(spans) <= ymax && ymin <= YMAX(spans)) {
89 subCount = sub->count;
91 spansCount = spans->count;
92 spansPt = spans->points;
96 while (spansCount && spansPt->y < subPt->y)
98 spansPt++; spansCount--;
102 while (subCount && subPt->y < spansPt->y)
108 if (subPt->y == spansPt->y)
111 xmax = xmin + subPt->width;
112 if (xmin >= (spansPt->x + spansPt->width) || spansPt->x >= xmax)
116 else if (xmin <= spansPt->x)
118 if (xmax >= (spansPt->x + spansPt->width))
120 g_memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
127 spansPt->width -= xmax - spansPt->x;
133 if (xmax >= (spansPt->x + spansPt->width))
135 spansPt->width = xmin - spansPt->x;
143 newPt = (GdkSpan*) g_realloc (spans->points, (spans->count + EXTRA) * sizeof (GdkSpan));
146 spansPt = newPt + (spansPt - spans->points);
147 spans->points = newPt;
150 g_memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
153 spansPt->width = xmin - spansPt->x;
155 spansPt->width -= xmax - spansPt->x;
160 spansPt++; spansCount--;
166 void miAppendSpans(spanGroup, otherGroup, spans)
167 SpanGroup *spanGroup;
168 SpanGroup *otherGroup;
171 register int ymin, ymax;
172 register int spansCount;
174 spansCount = spans->count;
175 if (spansCount > 0) {
176 if (spanGroup->size == spanGroup->count) {
177 spanGroup->size = (spanGroup->size + 8) * 2;
178 spanGroup->group = (Spans *)
179 g_realloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
182 spanGroup->group[spanGroup->count] = *spans;
183 (spanGroup->count)++;
184 ymin = spans->points[0].y;
185 if (ymin < spanGroup->ymin) spanGroup->ymin = ymin;
186 ymax = spans->points[spansCount - 1].y;
187 if (ymax > spanGroup->ymax) spanGroup->ymax = ymax;
189 otherGroup->ymin < ymax &&
190 ymin < otherGroup->ymax)
192 miSubtractSpans (otherGroup, spans);
197 g_free (spans->points);
201 void miFreeSpanGroup(spanGroup)
202 SpanGroup *spanGroup;
204 if (spanGroup->group != NULL) g_free(spanGroup->group);
207 static void QuickSortSpansX(points, numSpans)
208 register GdkSpan points[];
209 register int numSpans;
212 register int i, j, m;
215 /* Always called with numSpans > 1 */
216 /* Sorts only by x, as all y should be the same */
218 #define ExchangeSpans(a, b) \
222 tpt = points[a]; points[a] = points[b]; points[b] = tpt; \
227 /* Do insertion sort */
232 do { /* while i != numSpans */
235 /* points[i] is out of order. Move into proper location. */
239 for (j = 0; x >= points[j].x; j++) {}
241 for (k = i; k != j; k--) {
242 points[k] = points[k-1];
246 } /* if out of order */
249 } while (i != numSpans);
253 /* Choose partition element, stick in location 0 */
255 if (points[m].x > points[0].x) ExchangeSpans(m, 0);
256 if (points[m].x > points[numSpans-1].x) ExchangeSpans(m, numSpans-1);
257 if (points[m].x > points[0].x) ExchangeSpans(m, 0);
260 /* Partition array */
268 } while (i != numSpans && r->x < x);
274 if (i < j) ExchangeSpans(i, j);
277 /* Move partition element back to middle */
281 if (numSpans-j-1 > 1)
282 QuickSortSpansX(&points[j+1], numSpans-j-1);
284 } while (numSpans > 1);
285 } /* QuickSortSpans */
288 static int UniquifySpansX(spans, newPoints, newWidths)
290 register GdkSpan *newPoints;
292 register int newx1, newx2, oldpt, i, y;
293 GdkSpan *oldPoints, *startNewPoints = newPoints;
295 /* Always called with numSpans > 1 */
296 /* Uniquify the spans, and stash them into newPoints and newWidths. Return the
297 number of unique spans. */
300 oldPoints = spans->points;
303 newx1 = oldPoints->x;
304 newx2 = newx1 + oldPoints->width;
306 for (i = spans->count-1; i != 0; i--) {
308 oldpt = oldPoints->x;
310 /* Write current span, start a new one */
311 newPoints->x = newx1;
313 newPoints->width = newx2 - newx1;
316 newx2 = oldpt + oldPoints->width;
318 /* extend current span, if old extends beyond new */
319 oldpt = oldpt + oldPoints->width;
320 if (oldpt > newx2) newx2 = oldpt;
324 /* Write final span */
325 newPoints->x = newx1;
326 newPoints->width = newx2 - newx1;
329 return (newPoints - startNewPoints) + 1;
330 } /* UniquifySpansX */
333 miDisposeSpanGroup (spanGroup)
334 SpanGroup *spanGroup;
339 for (i = 0; i < spanGroup->count; i++)
341 spans = spanGroup->group + i;
342 g_free (spans->points);
346 void miFillUniqueSpanGroup(pDraw, pGC, spanGroup)
349 SpanGroup *spanGroup;
352 register Spans *spans;
353 register Spans *yspans;
354 register int *ysizes;
355 register int ymin, ylength;
357 /* Outgoing spans for one big call to FillSpans */
358 register GdkSpan* points;
361 if (spanGroup->count == 0) return;
363 if (spanGroup->count == 1) {
364 /* Already should be sorted, unique */
365 spans = spanGroup->group;
366 gdk_fb_fill_spans(pDraw, pGC, spans->points, spans->count, TRUE);
367 g_free(spans->points);
371 /* Yuck. Gross. Radix sort into y buckets, then sort x and uniquify */
372 /* This seems to be the fastest thing to do. I've tried sorting on
373 both x and y at the same time rather than creating into all those
374 y buckets, but it was somewhat slower. */
376 ymin = spanGroup->ymin;
377 ylength = spanGroup->ymax - ymin + 1;
379 /* Allocate Spans for y buckets */
380 yspans = (Spans *) g_malloc(ylength * sizeof(Spans));
381 ysizes = (int *) g_malloc(ylength * sizeof (int));
383 if (!yspans || !ysizes)
389 miDisposeSpanGroup (spanGroup);
393 for (i = 0; i != ylength; i++) {
396 yspans[i].points = NULL;
399 /* Go through every single span and put it into the correct bucket */
401 for (i = 0, spans = spanGroup->group;
402 i != spanGroup->count;
407 for (j = 0, points = spans->points;
410 index = points->y - ymin;
411 if (index >= 0 && index < ylength) {
412 Spans *newspans = &(yspans[index]);
413 if (newspans->count == ysizes[index]) {
415 ysizes[index] = (ysizes[index] + 8) * 2;
416 newpoints = (GdkSpan*) g_realloc(
418 ysizes[index] * sizeof(GdkSpan));
423 for (i = 0; i < ylength; i++)
425 g_free (yspans[i].points);
429 miDisposeSpanGroup (spanGroup);
432 newspans->points = newpoints;
434 newspans->points[newspans->count] = *points;
436 } /* if y value of span in range */
437 } /* for j through spans */
438 count += spans->count;
439 g_free(spans->points);
440 spans->points = NULL;
441 } /* for i thorough Spans */
443 /* Now sort by x and uniquify each bucket into the final array */
444 points = (GdkSpan*) g_malloc(count * sizeof(GdkSpan));
449 for (i = 0; i < ylength; i++)
451 g_free (yspans[i].points);
460 for (i = 0; i != ylength; i++) {
461 int ycount = yspans[i].count;
464 QuickSortSpansX(yspans[i].points, ycount);
465 count += UniquifySpansX
466 (&(yspans[i]), &(points[count]));
468 points[count] = yspans[i].points[0];
471 g_free(yspans[i].points);
475 gdk_fb_fill_spans(pDraw, pGC, points, count, TRUE);
478 g_free(ysizes); /* use (DE)ALLOCATE_LOCAL for these? */
481 spanGroup->count = 0;
482 spanGroup->ymin = SHRT_MAX;
483 spanGroup->ymax = SHRT_MIN;
487 void miFillSpanGroup(pDraw, pGC, spanGroup)
490 SpanGroup *spanGroup;
493 register Spans *spans;
495 for (i = 0, spans = spanGroup->group; i != spanGroup->count; i++, spans++) {
496 gdk_fb_fill_spans(pDraw, pGC, spans->points, spans->count, TRUE);
497 g_free(spans->points);
500 spanGroup->count = 0;
501 spanGroup->ymin = SHRT_MAX;
502 spanGroup->ymax = SHRT_MIN;
503 } /* FillSpanGroup */