PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mi/mispans.c

#
C | 527 lines | 399 code | 51 blank | 77 comment | 79 complexity | 749419950165967126692cdfefe7c28d MD5 | raw file
Possible License(s): MIT
  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 *spanGroup)
  49. {
  50. spanGroup->size = 0;
  51. spanGroup->count = 0;
  52. spanGroup->group = NULL;
  53. spanGroup->ymin = MAXSHORT;
  54. spanGroup->ymax = MINSHORT;
  55. } /* InitSpanGroup */
  56. #define YMIN(spans) (spans->points[0].y)
  57. #define YMAX(spans) (spans->points[spans->count-1].y)
  58. static void miSubtractSpans (SpanGroup *spanGroup, Spans *sub)
  59. {
  60. int i, subCount, spansCount;
  61. int ymin, ymax, xmin, xmax;
  62. Spans *spans;
  63. DDXPointPtr subPt, spansPt;
  64. int *subWid, *spansWid;
  65. int extra;
  66. ymin = YMIN(sub);
  67. ymax = YMAX(sub);
  68. spans = spanGroup->group;
  69. for (i = spanGroup->count; i; i--, spans++) {
  70. if (YMIN(spans) <= ymax && ymin <= YMAX(spans)) {
  71. subCount = sub->count;
  72. subPt = sub->points;
  73. subWid = sub->widths;
  74. spansCount = spans->count;
  75. spansPt = spans->points;
  76. spansWid = spans->widths;
  77. extra = 0;
  78. for (;;)
  79. {
  80. while (spansCount && spansPt->y < subPt->y)
  81. {
  82. spansPt++; spansWid++; spansCount--;
  83. }
  84. if (!spansCount)
  85. break;
  86. while (subCount && subPt->y < spansPt->y)
  87. {
  88. subPt++; subWid++; subCount--;
  89. }
  90. if (!subCount)
  91. break;
  92. if (subPt->y == spansPt->y)
  93. {
  94. xmin = subPt->x;
  95. xmax = xmin + *subWid;
  96. if (xmin >= spansPt->x + *spansWid || spansPt->x >= xmax)
  97. {
  98. ;
  99. }
  100. else if (xmin <= spansPt->x)
  101. {
  102. if (xmax >= spansPt->x + *spansWid)
  103. {
  104. memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
  105. memmove (spansWid, spansWid + 1, sizeof *spansWid * (spansCount - 1));
  106. spansPt--;
  107. spansWid--;
  108. spans->count--;
  109. extra++;
  110. }
  111. else
  112. {
  113. *spansWid = *spansWid - (xmax - spansPt->x);
  114. spansPt->x = xmax;
  115. }
  116. }
  117. else
  118. {
  119. if (xmax >= spansPt->x + *spansWid)
  120. {
  121. *spansWid = xmin - spansPt->x;
  122. }
  123. else
  124. {
  125. if (!extra) {
  126. DDXPointPtr newPt;
  127. int *newwid;
  128. #define EXTRA 8
  129. newPt = (DDXPointPtr) realloc(spans->points, (spans->count + EXTRA) * sizeof (DDXPointRec));
  130. if (!newPt)
  131. break;
  132. spansPt = newPt + (spansPt - spans->points);
  133. spans->points = newPt;
  134. newwid = (int *) realloc(spans->widths, (spans->count + EXTRA) * sizeof (int));
  135. if (!newwid)
  136. break;
  137. spansWid = newwid + (spansWid - spans->widths);
  138. spans->widths = newwid;
  139. extra = EXTRA;
  140. }
  141. memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
  142. memmove (spansWid + 1, spansWid, sizeof *spansWid * (spansCount));
  143. spans->count++;
  144. extra--;
  145. *spansWid = xmin - spansPt->x;
  146. spansWid++;
  147. spansPt++;
  148. *spansWid = *spansWid - (xmax - spansPt->x);
  149. spansPt->x = xmax;
  150. }
  151. }
  152. }
  153. spansPt++; spansWid++; spansCount--;
  154. }
  155. }
  156. }
  157. }
  158. void miAppendSpans(SpanGroup *spanGroup, SpanGroup *otherGroup, Spans *spans)
  159. {
  160. int ymin, ymax;
  161. int spansCount;
  162. spansCount = spans->count;
  163. if (spansCount > 0) {
  164. if (spanGroup->size == spanGroup->count) {
  165. spanGroup->size = (spanGroup->size + 8) * 2;
  166. spanGroup->group = (Spans *)
  167. realloc(spanGroup->group, sizeof(Spans) * spanGroup->size);
  168. }
  169. spanGroup->group[spanGroup->count] = *spans;
  170. (spanGroup->count)++;
  171. ymin = spans->points[0].y;
  172. if (ymin < spanGroup->ymin) spanGroup->ymin = ymin;
  173. ymax = spans->points[spansCount - 1].y;
  174. if (ymax > spanGroup->ymax) spanGroup->ymax = ymax;
  175. if (otherGroup &&
  176. otherGroup->ymin < ymax &&
  177. ymin < otherGroup->ymax)
  178. {
  179. miSubtractSpans (otherGroup, spans);
  180. }
  181. }
  182. else
  183. {
  184. free(spans->points);
  185. free(spans->widths);
  186. }
  187. } /* AppendSpans */
  188. void miFreeSpanGroup(SpanGroup *spanGroup)
  189. {
  190. free(spanGroup->group);
  191. }
  192. static void QuickSortSpansX(
  193. DDXPointRec points[],
  194. int widths[],
  195. int numSpans )
  196. {
  197. int x;
  198. int i, j, m;
  199. DDXPointPtr r;
  200. /* Always called with numSpans > 1 */
  201. /* Sorts only by x, as all y should be the same */
  202. #define ExchangeSpans(a, b) \
  203. { \
  204. DDXPointRec tpt; \
  205. int tw; \
  206. \
  207. tpt = points[a]; points[a] = points[b]; points[b] = tpt; \
  208. tw = widths[a]; widths[a] = widths[b]; widths[b] = tw; \
  209. }
  210. do {
  211. if (numSpans < 9) {
  212. /* Do insertion sort */
  213. int xprev;
  214. xprev = points[0].x;
  215. i = 1;
  216. do { /* while i != numSpans */
  217. x = points[i].x;
  218. if (xprev > x) {
  219. /* points[i] is out of order. Move into proper location. */
  220. DDXPointRec tpt;
  221. int tw, k;
  222. for (j = 0; x >= points[j].x; j++) {}
  223. tpt = points[i];
  224. tw = widths[i];
  225. for (k = i; k != j; k--) {
  226. points[k] = points[k-1];
  227. widths[k] = widths[k-1];
  228. }
  229. points[j] = tpt;
  230. widths[j] = tw;
  231. x = points[i].x;
  232. } /* if out of order */
  233. xprev = x;
  234. i++;
  235. } while (i != numSpans);
  236. return;
  237. }
  238. /* Choose partition element, stick in location 0 */
  239. m = numSpans / 2;
  240. if (points[m].x > points[0].x) ExchangeSpans(m, 0);
  241. if (points[m].x > points[numSpans-1].x) ExchangeSpans(m, numSpans-1);
  242. if (points[m].x > points[0].x) ExchangeSpans(m, 0);
  243. x = points[0].x;
  244. /* Partition array */
  245. i = 0;
  246. j = numSpans;
  247. do {
  248. r = &(points[i]);
  249. do {
  250. r++;
  251. i++;
  252. } while (i != numSpans && r->x < x);
  253. r = &(points[j]);
  254. do {
  255. r--;
  256. j--;
  257. } while (x < r->x);
  258. if (i < j) ExchangeSpans(i, j);
  259. } while (i < j);
  260. /* Move partition element back to middle */
  261. ExchangeSpans(0, j);
  262. /* Recurse */
  263. if (numSpans-j-1 > 1)
  264. QuickSortSpansX(&points[j+1], &widths[j+1], numSpans-j-1);
  265. numSpans = j;
  266. } while (numSpans > 1);
  267. } /* QuickSortSpans */
  268. static int UniquifySpansX(
  269. Spans *spans,
  270. DDXPointRec *newPoints,
  271. int *newWidths )
  272. {
  273. int newx1, newx2, oldpt, i, y;
  274. DDXPointRec *oldPoints;
  275. int *oldWidths;
  276. int *startNewWidths;
  277. /* Always called with numSpans > 1 */
  278. /* Uniquify the spans, and stash them into newPoints and newWidths. Return the
  279. number of unique spans. */
  280. startNewWidths = newWidths;
  281. oldPoints = spans->points;
  282. oldWidths = spans->widths;
  283. y = oldPoints->y;
  284. newx1 = oldPoints->x;
  285. newx2 = newx1 + *oldWidths;
  286. for (i = spans->count-1; i != 0; i--) {
  287. oldPoints++;
  288. oldWidths++;
  289. oldpt = oldPoints->x;
  290. if (oldpt > newx2) {
  291. /* Write current span, start a new one */
  292. newPoints->x = newx1;
  293. newPoints->y = y;
  294. *newWidths = newx2 - newx1;
  295. newPoints++;
  296. newWidths++;
  297. newx1 = oldpt;
  298. newx2 = oldpt + *oldWidths;
  299. } else {
  300. /* extend current span, if old extends beyond new */
  301. oldpt = oldpt + *oldWidths;
  302. if (oldpt > newx2) newx2 = oldpt;
  303. }
  304. } /* for */
  305. /* Write final span */
  306. newPoints->x = newx1;
  307. *newWidths = newx2 - newx1;
  308. newPoints->y = y;
  309. return (newWidths - startNewWidths) + 1;
  310. } /* UniquifySpansX */
  311. static void
  312. miDisposeSpanGroup (SpanGroup *spanGroup)
  313. {
  314. int i;
  315. Spans *spans;
  316. for (i = 0; i < spanGroup->count; i++)
  317. {
  318. spans = spanGroup->group + i;
  319. free(spans->points);
  320. free(spans->widths);
  321. }
  322. }
  323. void miFillUniqueSpanGroup(DrawablePtr pDraw, GCPtr pGC, SpanGroup *spanGroup)
  324. {
  325. int i;
  326. Spans *spans;
  327. Spans *yspans;
  328. int *ysizes;
  329. int ymin, ylength;
  330. /* Outgoing spans for one big call to FillSpans */
  331. DDXPointPtr points;
  332. int *widths;
  333. int count;
  334. if (spanGroup->count == 0) return;
  335. if (spanGroup->count == 1) {
  336. /* Already should be sorted, unique */
  337. spans = spanGroup->group;
  338. (*pGC->ops->FillSpans)
  339. (pDraw, pGC, spans->count, spans->points, spans->widths, TRUE);
  340. free(spans->points);
  341. free(spans->widths);
  342. }
  343. else
  344. {
  345. /* Yuck. Gross. Radix sort into y buckets, then sort x and uniquify */
  346. /* This seems to be the fastest thing to do. I've tried sorting on
  347. both x and y at the same time rather than creating into all those
  348. y buckets, but it was somewhat slower. */
  349. ymin = spanGroup->ymin;
  350. ylength = spanGroup->ymax - ymin + 1;
  351. /* Allocate Spans for y buckets */
  352. yspans = malloc(ylength * sizeof(Spans));
  353. ysizes = malloc(ylength * sizeof (int));
  354. if (!yspans || !ysizes)
  355. {
  356. free(yspans);
  357. free(ysizes);
  358. miDisposeSpanGroup (spanGroup);
  359. return;
  360. }
  361. for (i = 0; i != ylength; i++) {
  362. ysizes[i] = 0;
  363. yspans[i].count = 0;
  364. yspans[i].points = NULL;
  365. yspans[i].widths = NULL;
  366. }
  367. /* Go through every single span and put it into the correct bucket */
  368. count = 0;
  369. for (i = 0, spans = spanGroup->group;
  370. i != spanGroup->count;
  371. i++, spans++) {
  372. int index;
  373. int j;
  374. for (j = 0, points = spans->points, widths = spans->widths;
  375. j != spans->count;
  376. j++, points++, widths++) {
  377. index = points->y - ymin;
  378. if (index >= 0 && index < ylength) {
  379. Spans *newspans = &(yspans[index]);
  380. if (newspans->count == ysizes[index]) {
  381. DDXPointPtr newpoints;
  382. int *newwidths;
  383. ysizes[index] = (ysizes[index] + 8) * 2;
  384. newpoints = (DDXPointPtr) realloc(
  385. newspans->points,
  386. ysizes[index] * sizeof(DDXPointRec));
  387. newwidths = (int *) realloc(
  388. newspans->widths,
  389. ysizes[index] * sizeof(int));
  390. if (!newpoints || !newwidths)
  391. {
  392. int i;
  393. for (i = 0; i < ylength; i++)
  394. {
  395. free(yspans[i].points);
  396. free(yspans[i].widths);
  397. }
  398. free(yspans);
  399. free(ysizes);
  400. free(newpoints);
  401. free(newwidths);
  402. miDisposeSpanGroup (spanGroup);
  403. return;
  404. }
  405. newspans->points = newpoints;
  406. newspans->widths = newwidths;
  407. }
  408. newspans->points[newspans->count] = *points;
  409. newspans->widths[newspans->count] = *widths;
  410. (newspans->count)++;
  411. } /* if y value of span in range */
  412. } /* for j through spans */
  413. count += spans->count;
  414. free(spans->points);
  415. spans->points = NULL;
  416. free(spans->widths);
  417. spans->widths = NULL;
  418. } /* for i thorough Spans */
  419. /* Now sort by x and uniquify each bucket into the final array */
  420. points = malloc(count * sizeof(DDXPointRec));
  421. widths = malloc(count * sizeof(int));
  422. if (!points || !widths)
  423. {
  424. int i;
  425. for (i = 0; i < ylength; i++)
  426. {
  427. free(yspans[i].points);
  428. free(yspans[i].widths);
  429. }
  430. free(yspans);
  431. free(ysizes);
  432. free(points);
  433. free(widths);
  434. return;
  435. }
  436. count = 0;
  437. for (i = 0; i != ylength; i++) {
  438. int ycount = yspans[i].count;
  439. if (ycount > 0) {
  440. if (ycount > 1) {
  441. QuickSortSpansX(yspans[i].points, yspans[i].widths, ycount);
  442. count += UniquifySpansX
  443. (&(yspans[i]), &(points[count]), &(widths[count]));
  444. } else {
  445. points[count] = yspans[i].points[0];
  446. widths[count] = yspans[i].widths[0];
  447. count++;
  448. }
  449. free(yspans[i].points);
  450. free(yspans[i].widths);
  451. }
  452. }
  453. (*pGC->ops->FillSpans) (pDraw, pGC, count, points, widths, TRUE);
  454. free(points);
  455. free(widths);
  456. free(yspans);
  457. free(ysizes); /* use (DE)xalloc for these? */
  458. }
  459. spanGroup->count = 0;
  460. spanGroup->ymin = MAXSHORT;
  461. spanGroup->ymax = MINSHORT;
  462. }