mispans.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /***********************************************************
  2. Copyright 1989, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include "misc.h"
  40. #include "pixmapstr.h"
  41. #include "gcstruct.h"
  42. #include "mispans.h"
  43. /*
  44. These routines maintain lists of Spans, in order to implement the
  45. ``touch-each-pixel-once'' rules of wide lines and arcs.
  46. Written by Joel McCormack, Summer 1989.
  47. */
  48. void miInitSpanGroup(spanGroup)
  49. SpanGroup *spanGroup;
  50. {
  51. spanGroup->size = 0;
  52. spanGroup->count = 0;
  53. spanGroup->group = NULL;
  54. spanGroup->ymin = MAXSHORT;
  55. spanGroup->ymax = MINSHORT;
  56. } /* InitSpanGroup */
  57. #define YMIN(spans) (spans->points[0].y)
  58. #define YMAX(spans) (spans->points[spans->count-1].y)
  59. void miSubtractSpans (spanGroup, sub)
  60. SpanGroup *spanGroup;
  61. Spans *sub;
  62. {
  63. int i, subCount, spansCount;
  64. int ymin, ymax, xmin, xmax;
  65. Spans *spans;
  66. DDXPointPtr subPt, spansPt;
  67. int *subWid, *spansWid;
  68. int extra;
  69. ymin = YMIN(sub);
  70. ymax = YMAX(sub);
  71. spans = spanGroup->group;
  72. for (i = spanGroup->count; i; i--, spans++) {
  73. if (YMIN(spans) <= ymax && ymin <= YMAX(spans)) {
  74. subCount = sub->count;
  75. subPt = sub->points;
  76. subWid = sub->widths;
  77. spansCount = spans->count;
  78. spansPt = spans->points;
  79. spansWid = spans->widths;
  80. extra = 0;
  81. for (;;)
  82. {
  83. while (spansCount && spansPt->y < subPt->y)
  84. {
  85. spansPt++; spansWid++; spansCount--;
  86. }
  87. if (!spansCount)
  88. break;
  89. while (subCount && subPt->y < spansPt->y)
  90. {
  91. subPt++; subWid++; subCount--;
  92. }
  93. if (!subCount)
  94. break;
  95. if (subPt->y == spansPt->y)
  96. {
  97. xmin = subPt->x;
  98. xmax = xmin + *subWid;
  99. if (xmin >= spansPt->x + *spansWid || spansPt->x >= xmax)
  100. {
  101. ;
  102. }
  103. else if (xmin <= spansPt->x)
  104. {
  105. if (xmax >= spansPt->x + *spansWid)
  106. {
  107. memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
  108. memmove (spansWid, spansWid + 1, sizeof *spansWid * (spansCount - 1));
  109. spansPt--;
  110. spansWid--;
  111. spans->count--;
  112. extra++;
  113. }
  114. else
  115. {
  116. *spansWid = *spansWid - (xmax - spansPt->x);
  117. spansPt->x = xmax;
  118. }
  119. }
  120. else
  121. {
  122. if (xmax >= spansPt->x + *spansWid)
  123. {
  124. *spansWid = xmin - spansPt->x;
  125. }
  126. else
  127. {
  128. if (!extra) {
  129. DDXPointPtr newPt;
  130. int *newwid;
  131. #define EXTRA 8
  132. newPt = (DDXPointPtr) realloc(spans->points, (spans->count + EXTRA) * sizeof (DDXPointRec));
  133. if (!newPt)
  134. break;
  135. spansPt = newPt + (spansPt - spans->points);
  136. spans->points = newPt;
  137. newwid = (int *) realloc(spans->widths, (spans->count + EXTRA) * sizeof (int));
  138. if (!newwid)
  139. break;
  140. spansWid = newwid + (spansWid - spans->widths);
  141. spans->widths = newwid;
  142. extra = EXTRA;
  143. }
  144. memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
  145. memmove (spansWid + 1, spansWid, sizeof *spansWid * (spansCount));
  146. spans->count++;
  147. extra--;
  148. *spansWid = xmin - spansPt->x;
  149. spansWid++;
  150. spansPt++;
  151. *spansWid = *spansWid - (xmax - spansPt->x);
  152. spansPt->x = xmax;
  153. }
  154. }
  155. }
  156. spansPt++; spansWid++; spansCount--;
  157. }
  158. }
  159. }
  160. }
  161. void miAppendSpans(spanGroup, otherGroup, spans)
  162. SpanGroup *spanGroup;
  163. SpanGroup *otherGroup;
  164. Spans *spans;
  165. {
  166. int ymin, ymax;
  167. int spansCount;
  168. spansCount = spans->count;
  169. if (spansCount > 0) {
  170. if (spanGroup->size == spanGroup->count) {
  171. spanGroup->size = (spanGroup->size + 8) * 2;
  172. spanGroup->group = (Spans *)
  173. realloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
  174. }
  175. spanGroup->group[spanGroup->count] = *spans;
  176. (spanGroup->count)++;
  177. ymin = spans->points[0].y;
  178. if (ymin < spanGroup->ymin) spanGroup->ymin = ymin;
  179. ymax = spans->points[spansCount - 1].y;
  180. if (ymax > spanGroup->ymax) spanGroup->ymax = ymax;
  181. if (otherGroup &&
  182. otherGroup->ymin < ymax &&
  183. ymin < otherGroup->ymax)
  184. {
  185. miSubtractSpans (otherGroup, spans);
  186. }
  187. }
  188. else
  189. {
  190. free(spans->points);
  191. free(spans->widths);
  192. }
  193. } /* AppendSpans */
  194. void miFreeSpanGroup(spanGroup)
  195. SpanGroup *spanGroup;
  196. {
  197. if (spanGroup->group != NULL) free(spanGroup->group);
  198. }
  199. static void QuickSortSpansX(
  200. register DDXPointRec points[],
  201. register int widths[],
  202. register int numSpans )
  203. {
  204. int x;
  205. int i, j, m;
  206. DDXPointPtr r;
  207. /* Always called with numSpans > 1 */
  208. /* Sorts only by x, as all y should be the same */
  209. #define ExchangeSpans(a, b) \
  210. { \
  211. DDXPointRec tpt; \
  212. int tw; \
  213. \
  214. tpt = points[a]; points[a] = points[b]; points[b] = tpt; \
  215. tw = widths[a]; widths[a] = widths[b]; widths[b] = tw; \
  216. }
  217. do {
  218. if (numSpans < 9) {
  219. /* Do insertion sort */
  220. int xprev;
  221. xprev = points[0].x;
  222. i = 1;
  223. do { /* while i != numSpans */
  224. x = points[i].x;
  225. if (xprev > x) {
  226. /* points[i] is out of order. Move into proper location. */
  227. DDXPointRec tpt;
  228. int tw, k;
  229. for (j = 0; x >= points[j].x; j++) {}
  230. tpt = points[i];
  231. tw = widths[i];
  232. for (k = i; k != j; k--) {
  233. points[k] = points[k-1];
  234. widths[k] = widths[k-1];
  235. }
  236. points[j] = tpt;
  237. widths[j] = tw;
  238. x = points[i].x;
  239. } /* if out of order */
  240. xprev = x;
  241. i++;
  242. } while (i != numSpans);
  243. return;
  244. }
  245. /* Choose partition element, stick in location 0 */
  246. m = numSpans / 2;
  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);
  250. x = points[0].x;
  251. /* Partition array */
  252. i = 0;
  253. j = numSpans;
  254. do {
  255. r = &(points[i]);
  256. do {
  257. r++;
  258. i++;
  259. } while (i != numSpans && r->x < x);
  260. r = &(points[j]);
  261. do {
  262. r--;
  263. j--;
  264. } while (x < r->x);
  265. if (i < j) ExchangeSpans(i, j);
  266. } while (i < j);
  267. /* Move partition element back to middle */
  268. ExchangeSpans(0, j);
  269. /* Recurse */
  270. if (numSpans-j-1 > 1)
  271. QuickSortSpansX(&points[j+1], &widths[j+1], numSpans-j-1);
  272. numSpans = j;
  273. } while (numSpans > 1);
  274. } /* QuickSortSpans */
  275. static int UniquifySpansX(
  276. Spans *spans,
  277. register DDXPointRec *newPoints,
  278. register int *newWidths )
  279. {
  280. int newx1, newx2, oldpt, i, y;
  281. DDXPointRec *oldPoints;
  282. int *oldWidths;
  283. int *startNewWidths;
  284. /* Always called with numSpans > 1 */
  285. /* Uniquify the spans, and stash them into newPoints and newWidths. Return the
  286. number of unique spans. */
  287. startNewWidths = newWidths;
  288. oldPoints = spans->points;
  289. oldWidths = spans->widths;
  290. y = oldPoints->y;
  291. newx1 = oldPoints->x;
  292. newx2 = newx1 + *oldWidths;
  293. for (i = spans->count-1; i != 0; i--) {
  294. oldPoints++;
  295. oldWidths++;
  296. oldpt = oldPoints->x;
  297. if (oldpt > newx2) {
  298. /* Write current span, start a new one */
  299. newPoints->x = newx1;
  300. newPoints->y = y;
  301. *newWidths = newx2 - newx1;
  302. newPoints++;
  303. newWidths++;
  304. newx1 = oldpt;
  305. newx2 = oldpt + *oldWidths;
  306. } else {
  307. /* extend current span, if old extends beyond new */
  308. oldpt = oldpt + *oldWidths;
  309. if (oldpt > newx2) newx2 = oldpt;
  310. }
  311. } /* for */
  312. /* Write final span */
  313. newPoints->x = newx1;
  314. *newWidths = newx2 - newx1;
  315. newPoints->y = y;
  316. return (newWidths - startNewWidths) + 1;
  317. } /* UniquifySpansX */
  318. void
  319. miDisposeSpanGroup (spanGroup)
  320. SpanGroup *spanGroup;
  321. {
  322. int i;
  323. Spans *spans;
  324. for (i = 0; i < spanGroup->count; i++)
  325. {
  326. spans = spanGroup->group + i;
  327. free(spans->points);
  328. free(spans->widths);
  329. }
  330. }
  331. void miFillUniqueSpanGroup(pDraw, pGC, spanGroup)
  332. DrawablePtr pDraw;
  333. GCPtr pGC;
  334. SpanGroup *spanGroup;
  335. {
  336. int i;
  337. Spans *spans;
  338. Spans *yspans;
  339. int *ysizes;
  340. int ymin, ylength;
  341. /* Outgoing spans for one big call to FillSpans */
  342. DDXPointPtr points;
  343. int *widths;
  344. int count;
  345. if (spanGroup->count == 0) return;
  346. if (spanGroup->count == 1) {
  347. /* Already should be sorted, unique */
  348. spans = spanGroup->group;
  349. (*pGC->ops->FillSpans)
  350. (pDraw, pGC, spans->count, spans->points, spans->widths, TRUE);
  351. free(spans->points);
  352. free(spans->widths);
  353. }
  354. else
  355. {
  356. /* Yuck. Gross. Radix sort into y buckets, then sort x and uniquify */
  357. /* This seems to be the fastest thing to do. I've tried sorting on
  358. both x and y at the same time rather than creating into all those
  359. y buckets, but it was somewhat slower. */
  360. ymin = spanGroup->ymin;
  361. ylength = spanGroup->ymax - ymin + 1;
  362. /* Allocate Spans for y buckets */
  363. yspans = malloc(ylength * sizeof(Spans));
  364. ysizes = malloc(ylength * sizeof (int));
  365. if (!yspans || !ysizes)
  366. {
  367. if (yspans)
  368. free(yspans);
  369. if (ysizes)
  370. free(ysizes);
  371. miDisposeSpanGroup (spanGroup);
  372. return;
  373. }
  374. for (i = 0; i != ylength; i++) {
  375. ysizes[i] = 0;
  376. yspans[i].count = 0;
  377. yspans[i].points = NULL;
  378. yspans[i].widths = NULL;
  379. }
  380. /* Go through every single span and put it into the correct bucket */
  381. count = 0;
  382. for (i = 0, spans = spanGroup->group;
  383. i != spanGroup->count;
  384. i++, spans++) {
  385. int index;
  386. int j;
  387. for (j = 0, points = spans->points, widths = spans->widths;
  388. j != spans->count;
  389. j++, points++, widths++) {
  390. index = points->y - ymin;
  391. if (index >= 0 && index < ylength) {
  392. Spans *newspans = &(yspans[index]);
  393. if (newspans->count == ysizes[index]) {
  394. DDXPointPtr newpoints;
  395. int *newwidths;
  396. ysizes[index] = (ysizes[index] + 8) * 2;
  397. newpoints = (DDXPointPtr) realloc(
  398. newspans->points,
  399. ysizes[index] * sizeof(DDXPointRec));
  400. newwidths = (int *) realloc(
  401. newspans->widths,
  402. ysizes[index] * sizeof(int));
  403. if (!newpoints || !newwidths)
  404. {
  405. int i;
  406. for (i = 0; i < ylength; i++)
  407. {
  408. free(yspans[i].points);
  409. free(yspans[i].widths);
  410. }
  411. free(yspans);
  412. free(ysizes);
  413. free(newpoints);
  414. free(newwidths);
  415. miDisposeSpanGroup (spanGroup);
  416. return;
  417. }
  418. newspans->points = newpoints;
  419. newspans->widths = newwidths;
  420. }
  421. newspans->points[newspans->count] = *points;
  422. newspans->widths[newspans->count] = *widths;
  423. (newspans->count)++;
  424. } /* if y value of span in range */
  425. } /* for j through spans */
  426. count += spans->count;
  427. free(spans->points);
  428. spans->points = NULL;
  429. free(spans->widths);
  430. spans->widths = NULL;
  431. } /* for i thorough Spans */
  432. /* Now sort by x and uniquify each bucket into the final array */
  433. points = malloc(count * sizeof(DDXPointRec));
  434. widths = (int *) malloc(count * sizeof(int));
  435. if (!points || !widths)
  436. {
  437. int i;
  438. for (i = 0; i < ylength; i++)
  439. {
  440. free(yspans[i].points);
  441. free(yspans[i].widths);
  442. }
  443. free(yspans);
  444. free(ysizes);
  445. if (points)
  446. free(points);
  447. if (widths)
  448. free(widths);
  449. return;
  450. }
  451. count = 0;
  452. for (i = 0; i != ylength; i++) {
  453. int ycount = yspans[i].count;
  454. if (ycount > 0) {
  455. if (ycount > 1) {
  456. QuickSortSpansX(yspans[i].points, yspans[i].widths, ycount);
  457. count += UniquifySpansX
  458. (&(yspans[i]), &(points[count]), &(widths[count]));
  459. } else {
  460. points[count] = yspans[i].points[0];
  461. widths[count] = yspans[i].widths[0];
  462. count++;
  463. }
  464. free(yspans[i].points);
  465. free(yspans[i].widths);
  466. }
  467. }
  468. (*pGC->ops->FillSpans) (pDraw, pGC, count, points, widths, TRUE);
  469. free(points);
  470. free(widths);
  471. free(yspans);
  472. free(ysizes); /* use (DE)ALLOCATE_LOCAL for these? */
  473. }
  474. spanGroup->count = 0;
  475. spanGroup->ymin = MAXSHORT;
  476. spanGroup->ymax = MINSHORT;
  477. }