PageRenderTime 305ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/common/lines.c

https://gitlab.com/victortoso/spice-common
C | 1389 lines | 809 code | 123 blank | 457 comment | 161 complexity | 7289faf613bac99906ad05e56db34c72 MD5 | raw file
  1. /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. /***********************************************************
  3. Copyright 1989, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. 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.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
  21. All Rights Reserved
  22. Permission to use, copy, modify, and distribute this software and its
  23. documentation for any purpose and without fee is hereby granted,
  24. provided that the above copyright notice appear in all copies and that
  25. both that copyright notice and this permission notice appear in
  26. supporting documentation, and that the name of Digital not be
  27. used in advertising or publicity pertaining to distribution of the
  28. software without specific, written prior permission.
  29. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  30. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  31. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  32. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  33. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  34. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  35. SOFTWARE.
  36. ******************************************************************/
  37. #ifdef HAVE_CONFIG_H
  38. #include <config.h>
  39. #endif
  40. #include <stdio.h>
  41. #include <spice/macros.h>
  42. #ifdef _XOPEN_SOURCE
  43. #include <math.h>
  44. #else
  45. #define _XOPEN_SOURCE /* to get prototype for hypot on some systems */
  46. #include <math.h>
  47. #undef _XOPEN_SOURCE
  48. #endif
  49. #include "lines.h"
  50. #include "mem.h"
  51. #define xalloc(i) spice_malloc(i)
  52. #define xrealloc(a,b) spice_realloc(a,b)
  53. #define xfree(i) free(i)
  54. typedef unsigned int CARD32;
  55. typedef int Boolean;
  56. typedef pixman_rectangle32_t xRectangle;
  57. typedef SpicePoint DDXPointRec;
  58. typedef DDXPointRec *DDXPointPtr;
  59. typedef struct lineGC *GCPtr;
  60. /* largest positive value that can fit into a component of a point.
  61. * Assumes that the point structure is {type x, y;} where type is
  62. * a signed type.
  63. */
  64. #define MAX_COORDINATE 2147483647
  65. #define MIN_COORDINATE -2147483647
  66. #define miZeroLine spice_canvas_zero_line
  67. #define miZeroDashLine spice_canvas_zero_dash_line
  68. #define miWideDash spice_canvas_wide_dash_line
  69. #define miWideLine spice_canvas_wide_line
  70. static inline int ICEIL (double x)
  71. {
  72. int _cTmp = (int)x;
  73. return ((x == _cTmp) || (x < 0.0)) ? _cTmp : _cTmp + 1;
  74. }
  75. typedef struct {
  76. int count; /* number of spans */
  77. DDXPointPtr points; /* pointer to list of start points */
  78. int *widths; /* pointer to list of widths */
  79. } Spans;
  80. typedef struct {
  81. int size; /* Total number of *Spans allocated */
  82. int count; /* Number of *Spans actually in group */
  83. Spans *group; /* List of Spans */
  84. int ymin, ymax; /* Min, max y values encountered */
  85. } SpanGroup;
  86. /* Initialize SpanGroup. MUST BE DONE before use. */
  87. static void miInitSpanGroup (SpanGroup * /*spanGroup */
  88. );
  89. /* Add a Spans to a SpanGroup. The spans MUST BE in y-sorted order */
  90. static void miAppendSpans (SpanGroup * /*spanGroup */ ,
  91. SpanGroup * /*otherGroup */ ,
  92. Spans * /*spans */
  93. );
  94. /* Paint a span group, insuring that each pixel is painted at most once */
  95. static void miFillUniqueSpanGroup (GCPtr /*pGC */ ,
  96. SpanGroup * /*spanGroup */ ,
  97. Boolean /* foreground */
  98. );
  99. /* Free up data in a span group. MUST BE DONE or you'll suffer memory leaks */
  100. static void miFreeSpanGroup (SpanGroup * /*spanGroup */
  101. );
  102. /* Rops which must use span groups */
  103. #define miSpansCarefulRop(rop) (((rop) & 0xc) == 0x8 || ((rop) & 0x3) == 0x2)
  104. #define miSpansEasyRop(rop) (!miSpansCarefulRop(rop))
  105. /*
  106. * Public definitions used for configuring basic pixelization aspects
  107. * of the sample implementation line-drawing routines provided in
  108. * {mfb,mi,cfb*} at run-time.
  109. */
  110. #define XDECREASING 4
  111. #define YDECREASING 2
  112. #define YMAJOR 1
  113. #define OCTANT1 (1 << (YDECREASING))
  114. #define OCTANT2 (1 << (YDECREASING|YMAJOR))
  115. #define OCTANT3 (1 << (XDECREASING|YDECREASING|YMAJOR))
  116. #define OCTANT4 (1 << (XDECREASING|YDECREASING))
  117. #define OCTANT5 (1 << (XDECREASING))
  118. #define OCTANT6 (1 << (XDECREASING|YMAJOR))
  119. #define OCTANT7 (1 << (YMAJOR))
  120. #define OCTANT8 (1 << (0))
  121. #define XMAJOROCTANTS (OCTANT1 | OCTANT4 | OCTANT5 | OCTANT8)
  122. #define DEFAULTZEROLINEBIAS (OCTANT2 | OCTANT3 | OCTANT4 | OCTANT5)
  123. /*
  124. * Devices can configure the rendering of routines in mi, mfb, and cfb*
  125. * by specifying a thin line bias to be applied to a particular screen
  126. * using the following function. The bias parameter is an OR'ing of
  127. * the appropriate OCTANT constants defined above to indicate which
  128. * octants to bias a line to prefer an axial step when the Bresenham
  129. * error term is exactly zero. The octants are mapped as follows:
  130. *
  131. * \ | /
  132. * \ 3 | 2 /
  133. * \ | /
  134. * 4 \ | / 1
  135. * \|/
  136. * -----------
  137. * /|\
  138. * 5 / | \ 8
  139. * / | \
  140. * / 6 | 7 \
  141. * / | \
  142. *
  143. * For more information, see "Ambiguities in Incremental Line Rastering,"
  144. * Jack E. Bresenham, IEEE CG&A, May 1987.
  145. */
  146. /*
  147. * Private definitions needed for drawing thin (zero width) lines
  148. * Used by the mi, mfb, and all cfb* components.
  149. */
  150. #define X_AXIS 0
  151. #define Y_AXIS 1
  152. #define OUT_LEFT 0x08
  153. #define OUT_RIGHT 0x04
  154. #define OUT_ABOVE 0x02
  155. #define OUT_BELOW 0x01
  156. #define OUTCODES(_result, _x, _y, _pbox) \
  157. if ( (_x) < (_pbox)->x1) (_result) |= OUT_LEFT; \
  158. else if ( (_x) >= (_pbox)->x2) (_result) |= OUT_RIGHT; \
  159. if ( (_y) < (_pbox)->y1) (_result) |= OUT_ABOVE; \
  160. else if ( (_y) >= (_pbox)->y2) (_result) |= OUT_BELOW;
  161. #define MIOUTCODES(outcode, x, y, xmin, ymin, xmax, ymax) \
  162. {\
  163. if (x < xmin) outcode |= OUT_LEFT;\
  164. if (x > xmax) outcode |= OUT_RIGHT;\
  165. if (y < ymin) outcode |= OUT_ABOVE;\
  166. if (y > ymax) outcode |= OUT_BELOW;\
  167. }
  168. #define SWAPINT(i, j) \
  169. { int _t = i; i = j; j = _t; }
  170. #define SWAPPT(i, j) \
  171. { DDXPointRec _t; _t = i; i = j; j = _t; }
  172. #define SWAPINT_PAIR(x1, y1, x2, y2)\
  173. { int t = x1; x1 = x2; x2 = t;\
  174. t = y1; y1 = y2; y2 = t;\
  175. }
  176. #define miGetZeroLineBias(_pScreen) (DEFAULTZEROLINEBIAS)
  177. #define CalcLineDeltas(_x1,_y1,_x2,_y2,_adx,_ady,_sx,_sy,_SX,_SY,_octant) \
  178. (_octant) = 0; \
  179. (_sx) = (_SX); \
  180. if (((_adx) = (_x2) - (_x1)) < 0) { \
  181. (_adx) = -(_adx); \
  182. (_sx = -(_sx)); \
  183. (_octant) |= XDECREASING; \
  184. } \
  185. (_sy) = (_SY); \
  186. if (((_ady) = (_y2) - (_y1)) < 0) { \
  187. (_ady) = -(_ady); \
  188. (_sy = -(_sy)); \
  189. (_octant) |= YDECREASING; \
  190. }
  191. #define SetYMajorOctant(_octant) ((_octant) |= YMAJOR)
  192. #define FIXUP_ERROR(_e, _octant, _bias) \
  193. (_e) -= (((_bias) >> (_octant)) & 1)
  194. #define IsXMajorOctant(_octant) (!((_octant) & YMAJOR))
  195. #define IsYMajorOctant(_octant) ((_octant) & YMAJOR)
  196. #define IsXDecreasingOctant(_octant) ((_octant) & XDECREASING)
  197. #define IsYDecreasingOctant(_octant) ((_octant) & YDECREASING)
  198. static int miZeroClipLine (int /*xmin */ ,
  199. int /*ymin */ ,
  200. int /*xmax */ ,
  201. int /*ymax */ ,
  202. int * /*new_x1 */ ,
  203. int * /*new_y1 */ ,
  204. int * /*new_x2 */ ,
  205. int * /*new_y2 */ ,
  206. unsigned int /*adx */ ,
  207. unsigned int /*ady */ ,
  208. int * /*pt1_clipped */ ,
  209. int * /*pt2_clipped */ ,
  210. int /*octant */ ,
  211. unsigned int /*bias */ ,
  212. int /*oc1 */ ,
  213. int /*oc2 */
  214. );
  215. /*
  216. * interface data to span-merging polygon filler
  217. */
  218. typedef struct _SpanData {
  219. SpanGroup fgGroup, bgGroup;
  220. } SpanDataRec, *SpanDataPtr;
  221. #define AppendSpanGroup(pGC, foreground, spanPtr, spanData) { \
  222. SpanGroup *group, *othergroup = NULL; \
  223. if (foreground) \
  224. { \
  225. group = &spanData->fgGroup; \
  226. if (pGC->lineStyle == LineDoubleDash) \
  227. othergroup = &spanData->bgGroup; \
  228. } \
  229. else \
  230. { \
  231. group = &spanData->bgGroup; \
  232. othergroup = &spanData->fgGroup; \
  233. } \
  234. miAppendSpans (group, othergroup, spanPtr); \
  235. }
  236. /*
  237. * Polygon edge description for integer wide-line routines
  238. */
  239. typedef struct _PolyEdge {
  240. int height; /* number of scanlines to process */
  241. int x; /* starting x coordinate */
  242. int stepx; /* fixed integral dx */
  243. int signdx; /* variable dx sign */
  244. int e; /* initial error term */
  245. int dy;
  246. int dx;
  247. } PolyEdgeRec, *PolyEdgePtr;
  248. #define SQSECANT 108.856472512142 /* 1/sin^2(11/2) - miter limit constant */
  249. /*
  250. * types for general polygon routines
  251. */
  252. typedef struct _PolyVertex {
  253. double x, y;
  254. } PolyVertexRec, *PolyVertexPtr;
  255. typedef struct _PolySlope {
  256. int dx, dy;
  257. double k; /* x0 * dy - y0 * dx */
  258. } PolySlopeRec, *PolySlopePtr;
  259. /*
  260. * Line face description for caps/joins
  261. */
  262. typedef struct _LineFace {
  263. double xa, ya;
  264. int dx, dy;
  265. int x, y;
  266. double k;
  267. } LineFaceRec, *LineFacePtr;
  268. /*
  269. * macros for polygon fillers
  270. */
  271. #define MIPOLYRELOADLEFT if (!left_height && left_count) { \
  272. left_height = left->height; \
  273. left_x = left->x; \
  274. left_stepx = left->stepx; \
  275. left_signdx = left->signdx; \
  276. left_e = left->e; \
  277. left_dy = left->dy; \
  278. left_dx = left->dx; \
  279. --left_count; \
  280. ++left; \
  281. }
  282. #define MIPOLYRELOADRIGHT if (!right_height && right_count) { \
  283. right_height = right->height; \
  284. right_x = right->x; \
  285. right_stepx = right->stepx; \
  286. right_signdx = right->signdx; \
  287. right_e = right->e; \
  288. right_dy = right->dy; \
  289. right_dx = right->dx; \
  290. --right_count; \
  291. ++right; \
  292. }
  293. #define MIPOLYSTEPLEFT left_x += left_stepx; \
  294. left_e += left_dx; \
  295. if (left_e > 0) \
  296. { \
  297. left_x += left_signdx; \
  298. left_e -= left_dy; \
  299. }
  300. #define MIPOLYSTEPRIGHT right_x += right_stepx; \
  301. right_e += right_dx; \
  302. if (right_e > 0) \
  303. { \
  304. right_x += right_signdx; \
  305. right_e -= right_dy; \
  306. }
  307. static void miRoundJoinClip (LineFacePtr /*pLeft */ ,
  308. LineFacePtr /*pRight */ ,
  309. PolyEdgePtr /*edge1 */ ,
  310. PolyEdgePtr /*edge2 */ ,
  311. int * /*y1 */ ,
  312. int * /*y2 */ ,
  313. Boolean * /*left1 */ ,
  314. Boolean * /*left2 */
  315. );
  316. static int miRoundCapClip (LineFacePtr /*face */ ,
  317. Boolean /*isInt */ ,
  318. PolyEdgePtr /*edge */ ,
  319. Boolean * /*leftEdge */
  320. );
  321. static int miPolyBuildEdge (double x0, double y0, double k, int dx, int dy,
  322. int xi, int yi, int left, PolyEdgePtr edge);
  323. static int miPolyBuildPoly (PolyVertexPtr vertices, PolySlopePtr slopes,
  324. int count, int xi, int yi, PolyEdgePtr left,
  325. PolyEdgePtr right, int *pnleft, int *pnright, int *h);
  326. static void
  327. miStepDash (int dist, /* distance to step */
  328. int *pDashIndex, /* current dash */
  329. unsigned char *pDash, /* dash list */
  330. int numInDashList, /* total length of dash list */
  331. int *pDashOffset /* offset into current dash */
  332. )
  333. {
  334. int dashIndex, dashOffset;
  335. int totallen;
  336. int i;
  337. dashIndex = *pDashIndex;
  338. dashOffset = *pDashOffset;
  339. if (dist < pDash[dashIndex] - dashOffset) {
  340. *pDashOffset = dashOffset + dist;
  341. return;
  342. }
  343. dist -= pDash[dashIndex] - dashOffset;
  344. if (++dashIndex == numInDashList)
  345. dashIndex = 0;
  346. totallen = 0;
  347. for (i = 0; i < numInDashList; i++)
  348. totallen += pDash[i];
  349. if (totallen > 0 && totallen <= dist)
  350. dist = dist % totallen;
  351. while (dist >= pDash[dashIndex]) {
  352. dist -= pDash[dashIndex];
  353. if (++dashIndex == numInDashList)
  354. dashIndex = 0;
  355. }
  356. *pDashIndex = dashIndex;
  357. *pDashOffset = dist;
  358. }
  359. /*
  360. These routines maintain lists of Spans, in order to implement the
  361. ``touch-each-pixel-once'' rules of wide lines and arcs.
  362. Written by Joel McCormack, Summer 1989.
  363. */
  364. static void
  365. miInitSpanGroup (SpanGroup * spanGroup)
  366. {
  367. spanGroup->size = 0;
  368. spanGroup->count = 0;
  369. spanGroup->group = NULL;
  370. spanGroup->ymin = MAX_COORDINATE;
  371. spanGroup->ymax = MIN_COORDINATE;
  372. } /* InitSpanGroup */
  373. #define YMIN(spans) (spans->points[0].y)
  374. #define YMAX(spans) (spans->points[spans->count-1].y)
  375. static void
  376. miSubtractSpans (SpanGroup * spanGroup, Spans * sub)
  377. {
  378. int i, subCount, spansCount;
  379. int ymin, ymax, xmin, xmax;
  380. Spans *spans;
  381. DDXPointPtr subPt, spansPt;
  382. int *subWid, *spansWid;
  383. int extra;
  384. ymin = YMIN (sub);
  385. ymax = YMAX (sub);
  386. spans = spanGroup->group;
  387. for (i = spanGroup->count; i; i--, spans++) {
  388. if (YMIN (spans) <= ymax && ymin <= YMAX (spans)) {
  389. subCount = sub->count;
  390. subPt = sub->points;
  391. subWid = sub->widths;
  392. spansCount = spans->count;
  393. spansPt = spans->points;
  394. spansWid = spans->widths;
  395. extra = 0;
  396. for (;;) {
  397. while (spansCount && spansPt->y < subPt->y) {
  398. spansPt++;
  399. spansWid++;
  400. spansCount--;
  401. }
  402. if (!spansCount)
  403. break;
  404. while (subCount && subPt->y < spansPt->y) {
  405. subPt++;
  406. subWid++;
  407. subCount--;
  408. }
  409. if (!subCount)
  410. break;
  411. if (subPt->y == spansPt->y) {
  412. xmin = subPt->x;
  413. xmax = xmin + *subWid;
  414. if (xmin >= spansPt->x + *spansWid || spansPt->x >= xmax) {
  415. ;
  416. } else if (xmin <= spansPt->x) {
  417. if (xmax >= spansPt->x + *spansWid) {
  418. memmove (spansPt, spansPt + 1, sizeof *spansPt * (spansCount - 1));
  419. memmove (spansWid, spansWid + 1, sizeof *spansWid * (spansCount - 1));
  420. spansPt--;
  421. spansWid--;
  422. spans->count--;
  423. extra++;
  424. } else {
  425. *spansWid = *spansWid - (xmax - spansPt->x);
  426. spansPt->x = xmax;
  427. }
  428. } else {
  429. if (xmax >= spansPt->x + *spansWid) {
  430. *spansWid = xmin - spansPt->x;
  431. } else {
  432. if (!extra) {
  433. DDXPointPtr newPt;
  434. int *newwid;
  435. #define EXTRA 8
  436. newPt = xrealloc (spans->points,
  437. (spans->count +
  438. EXTRA) * sizeof (DDXPointRec));
  439. if (!newPt)
  440. break;
  441. spansPt = newPt + (spansPt - spans->points);
  442. spans->points = newPt;
  443. newwid = xrealloc (spans->widths,
  444. (spans->count + EXTRA) * sizeof (int));
  445. if (!newwid)
  446. break;
  447. spansWid = newwid + (spansWid - spans->widths);
  448. spans->widths = newwid;
  449. extra = EXTRA;
  450. }
  451. memmove (spansPt + 1, spansPt, sizeof *spansPt * (spansCount));
  452. memmove (spansWid + 1, spansWid, sizeof *spansWid * (spansCount));
  453. spans->count++;
  454. extra--;
  455. *spansWid = xmin - spansPt->x;
  456. spansWid++;
  457. spansPt++;
  458. *spansWid = *spansWid - (xmax - spansPt->x);
  459. spansPt->x = xmax;
  460. }
  461. }
  462. }
  463. spansPt++;
  464. spansWid++;
  465. spansCount--;
  466. }
  467. }
  468. }
  469. }
  470. static void
  471. miAppendSpans (SpanGroup * spanGroup, SpanGroup * otherGroup, Spans * spans)
  472. {
  473. int ymin, ymax;
  474. int spansCount;
  475. spansCount = spans->count;
  476. if (spansCount > 0) {
  477. if (spanGroup->size == spanGroup->count) {
  478. spanGroup->size = (spanGroup->size + 8) * 2;
  479. spanGroup->group =
  480. xrealloc (spanGroup->group, sizeof (Spans) * spanGroup->size);
  481. }
  482. spanGroup->group[spanGroup->count] = *spans;
  483. (spanGroup->count)++;
  484. ymin = spans->points[0].y;
  485. if (ymin < spanGroup->ymin)
  486. spanGroup->ymin = ymin;
  487. ymax = spans->points[spansCount - 1].y;
  488. if (ymax > spanGroup->ymax)
  489. spanGroup->ymax = ymax;
  490. if (otherGroup && otherGroup->ymin < ymax && ymin < otherGroup->ymax) {
  491. miSubtractSpans (otherGroup, spans);
  492. }
  493. } else {
  494. xfree (spans->points);
  495. xfree (spans->widths);
  496. }
  497. } /* AppendSpans */
  498. static void
  499. miFreeSpanGroup (SpanGroup * spanGroup)
  500. {
  501. xfree (spanGroup->group);
  502. }
  503. static void
  504. QuickSortSpansX (DDXPointRec points[], int widths[], int numSpans)
  505. {
  506. int x;
  507. int i, j, m;
  508. DDXPointPtr r;
  509. /* Always called with numSpans > 1 */
  510. /* Sorts only by x, as all y should be the same */
  511. #define ExchangeSpans(a, b) \
  512. { \
  513. DDXPointRec tpt; \
  514. int tw; \
  515. \
  516. tpt = points[a]; points[a] = points[b]; points[b] = tpt; \
  517. tw = widths[a]; widths[a] = widths[b]; widths[b] = tw; \
  518. }
  519. do {
  520. if (numSpans < 9) {
  521. /* Do insertion sort */
  522. int xprev;
  523. xprev = points[0].x;
  524. i = 1;
  525. do { /* while i != numSpans */
  526. x = points[i].x;
  527. if (xprev > x) {
  528. /* points[i] is out of order. Move into proper location. */
  529. DDXPointRec tpt;
  530. int tw, k;
  531. for (j = 0; x >= points[j].x; j++) {
  532. }
  533. tpt = points[i];
  534. tw = widths[i];
  535. for (k = i; k != j; k--) {
  536. points[k] = points[k - 1];
  537. widths[k] = widths[k - 1];
  538. }
  539. points[j] = tpt;
  540. widths[j] = tw;
  541. x = points[i].x;
  542. } /* if out of order */
  543. xprev = x;
  544. i++;
  545. } while (i != numSpans);
  546. return;
  547. }
  548. /* Choose partition element, stick in location 0 */
  549. m = numSpans / 2;
  550. if (points[m].x > points[0].x)
  551. ExchangeSpans (m, 0);
  552. if (points[m].x > points[numSpans - 1].x)
  553. ExchangeSpans (m, numSpans - 1);
  554. if (points[m].x > points[0].x)
  555. ExchangeSpans (m, 0);
  556. x = points[0].x;
  557. /* Partition array */
  558. i = 0;
  559. j = numSpans;
  560. do {
  561. r = &(points[i]);
  562. do {
  563. r++;
  564. i++;
  565. } while (i != numSpans && r->x < x);
  566. r = &(points[j]);
  567. do {
  568. r--;
  569. j--;
  570. } while (x < r->x);
  571. if (i < j)
  572. ExchangeSpans (i, j);
  573. } while (i < j);
  574. /* Move partition element back to middle */
  575. ExchangeSpans (0, j);
  576. /* Recurse */
  577. if (numSpans - j - 1 > 1)
  578. QuickSortSpansX (&points[j + 1], &widths[j + 1], numSpans - j - 1);
  579. numSpans = j;
  580. } while (numSpans > 1);
  581. } /* QuickSortSpans */
  582. static int
  583. UniquifySpansX (Spans * spans, DDXPointRec * newPoints, int *newWidths)
  584. {
  585. int newx1, newx2, oldpt, i, y;
  586. DDXPointRec *oldPoints;
  587. int *oldWidths;
  588. int *startNewWidths;
  589. /* Always called with numSpans > 1 */
  590. /* Uniquify the spans, and stash them into newPoints and newWidths. Return the
  591. number of unique spans. */
  592. startNewWidths = newWidths;
  593. oldPoints = spans->points;
  594. oldWidths = spans->widths;
  595. y = oldPoints->y;
  596. newx1 = oldPoints->x;
  597. newx2 = newx1 + *oldWidths;
  598. for (i = spans->count - 1; i != 0; i--) {
  599. oldPoints++;
  600. oldWidths++;
  601. oldpt = oldPoints->x;
  602. if (oldpt > newx2) {
  603. /* Write current span, start a new one */
  604. newPoints->x = newx1;
  605. newPoints->y = y;
  606. *newWidths = newx2 - newx1;
  607. newPoints++;
  608. newWidths++;
  609. newx1 = oldpt;
  610. newx2 = oldpt + *oldWidths;
  611. } else {
  612. /* extend current span, if old extends beyond new */
  613. oldpt = oldpt + *oldWidths;
  614. if (oldpt > newx2)
  615. newx2 = oldpt;
  616. }
  617. } /* for */
  618. /* Write final span */
  619. newPoints->x = newx1;
  620. *newWidths = newx2 - newx1;
  621. newPoints->y = y;
  622. return (newWidths - startNewWidths) + 1;
  623. } /* UniquifySpansX */
  624. static void
  625. miDisposeSpanGroup (SpanGroup * spanGroup)
  626. {
  627. int i;
  628. Spans *spans;
  629. for (i = 0; i < spanGroup->count; i++) {
  630. spans = spanGroup->group + i;
  631. xfree (spans->points);
  632. xfree (spans->widths);
  633. }
  634. }
  635. static void
  636. miFillUniqueSpanGroup (GCPtr pGC, SpanGroup * spanGroup, Boolean foreground)
  637. {
  638. int i;
  639. Spans *spans;
  640. Spans *yspans;
  641. int *ysizes;
  642. int ymin, ylength;
  643. /* Outgoing spans for one big call to FillSpans */
  644. DDXPointPtr points;
  645. int *widths;
  646. int count;
  647. if (spanGroup->count == 0)
  648. return;
  649. if (spanGroup->count == 1) {
  650. /* Already should be sorted, unique */
  651. spans = spanGroup->group;
  652. (*pGC->ops->FillSpans)
  653. (pGC, spans->count, spans->points, spans->widths, TRUE, foreground);
  654. xfree (spans->points);
  655. xfree (spans->widths);
  656. } else {
  657. /* Yuck. Gross. Radix sort into y buckets, then sort x and uniquify */
  658. /* This seems to be the fastest thing to do. I've tried sorting on
  659. both x and y at the same time rather than creating into all those
  660. y buckets, but it was somewhat slower. */
  661. ymin = spanGroup->ymin;
  662. ylength = spanGroup->ymax - ymin + 1;
  663. /* Allocate Spans for y buckets */
  664. yspans = (Spans*)xalloc (ylength * sizeof (Spans));
  665. ysizes = (int *)xalloc (ylength * sizeof (int));
  666. if (!yspans || !ysizes) {
  667. xfree (yspans);
  668. xfree (ysizes);
  669. miDisposeSpanGroup (spanGroup);
  670. return;
  671. }
  672. for (i = 0; i != ylength; i++) {
  673. ysizes[i] = 0;
  674. yspans[i].count = 0;
  675. yspans[i].points = NULL;
  676. yspans[i].widths = NULL;
  677. }
  678. /* Go through every single span and put it into the correct bucket */
  679. count = 0;
  680. for (i = 0, spans = spanGroup->group; i != spanGroup->count; i++, spans++) {
  681. int index;
  682. int j;
  683. for (j = 0, points = spans->points, widths = spans->widths;
  684. j != spans->count; j++, points++, widths++) {
  685. index = points->y - ymin;
  686. if (index >= 0 && index < ylength) {
  687. Spans *newspans = &(yspans[index]);
  688. if (newspans->count == ysizes[index]) {
  689. DDXPointPtr newpoints;
  690. int *newwidths;
  691. ysizes[index] = (ysizes[index] + 8) * 2;
  692. newpoints = xrealloc (newspans->points,
  693. ysizes[index] * sizeof (DDXPointRec));
  694. newwidths = xrealloc (newspans->widths,
  695. ysizes[index] * sizeof (int));
  696. if (!newpoints || !newwidths) {
  697. for (i = 0; i < ylength; i++) {
  698. xfree (yspans[i].points);
  699. xfree (yspans[i].widths);
  700. }
  701. xfree (yspans);
  702. xfree (ysizes);
  703. xfree (newpoints);
  704. xfree (newwidths);
  705. miDisposeSpanGroup (spanGroup);
  706. return;
  707. }
  708. newspans->points = newpoints;
  709. newspans->widths = newwidths;
  710. }
  711. newspans->points[newspans->count] = *points;
  712. newspans->widths[newspans->count] = *widths;
  713. (newspans->count)++;
  714. } /* if y value of span in range */
  715. } /* for j through spans */
  716. count += spans->count;
  717. xfree (spans->points);
  718. spans->points = NULL;
  719. xfree (spans->widths);
  720. spans->widths = NULL;
  721. } /* for i thorough Spans */
  722. /* Now sort by x and uniquify each bucket into the final array */
  723. points = (DDXPointRec*)xalloc (count * sizeof (DDXPointRec));
  724. widths = (int *)xalloc (count * sizeof (int));
  725. if (!points || !widths) {
  726. for (i = 0; i < ylength; i++) {
  727. xfree (yspans[i].points);
  728. xfree (yspans[i].widths);
  729. }
  730. xfree (yspans);
  731. xfree (ysizes);
  732. xfree (points);
  733. xfree (widths);
  734. return;
  735. }
  736. count = 0;
  737. for (i = 0; i != ylength; i++) {
  738. int ycount = yspans[i].count;
  739. if (ycount > 0) {
  740. if (ycount > 1) {
  741. QuickSortSpansX (yspans[i].points, yspans[i].widths, ycount);
  742. count += UniquifySpansX (&(yspans[i]), &(points[count]), &(widths[count]));
  743. } else {
  744. points[count] = yspans[i].points[0];
  745. widths[count] = yspans[i].widths[0];
  746. count++;
  747. }
  748. xfree (yspans[i].points);
  749. xfree (yspans[i].widths);
  750. }
  751. }
  752. (*pGC->ops->FillSpans) (pGC, count, points, widths, TRUE, foreground);
  753. xfree (points);
  754. xfree (widths);
  755. xfree (yspans);
  756. xfree (ysizes); /* use (DE)xalloc for these? */
  757. }
  758. spanGroup->count = 0;
  759. spanGroup->ymin = MAX_COORDINATE;
  760. spanGroup->ymax = MIN_COORDINATE;
  761. }
  762. /*
  763. The bresenham error equation used in the mi/mfb/cfb line routines is:
  764. e = error
  765. dx = difference in raw X coordinates
  766. dy = difference in raw Y coordinates
  767. M = # of steps in X direction
  768. N = # of steps in Y direction
  769. B = 0 to prefer diagonal steps in a given octant,
  770. 1 to prefer axial steps in a given octant
  771. For X major lines:
  772. e = 2Mdy - 2Ndx - dx - B
  773. -2dx <= e < 0
  774. For Y major lines:
  775. e = 2Ndx - 2Mdy - dy - B
  776. -2dy <= e < 0
  777. At the start of the line, we have taken 0 X steps and 0 Y steps,
  778. so M = 0 and N = 0:
  779. X major e = 2Mdy - 2Ndx - dx - B
  780. = -dx - B
  781. Y major e = 2Ndx - 2Mdy - dy - B
  782. = -dy - B
  783. At the end of the line, we have taken dx X steps and dy Y steps,
  784. so M = dx and N = dy:
  785. X major e = 2Mdy - 2Ndx - dx - B
  786. = 2dxdy - 2dydx - dx - B
  787. = -dx - B
  788. Y major e = 2Ndx - 2Mdy - dy - B
  789. = 2dydx - 2dxdy - dy - B
  790. = -dy - B
  791. Thus, the error term is the same at the start and end of the line.
  792. Let us consider clipping an X coordinate. There are 4 cases which
  793. represent the two independent cases of clipping the start vs. the
  794. end of the line and an X major vs. a Y major line. In any of these
  795. cases, we know the number of X steps (M) and we wish to find the
  796. number of Y steps (N). Thus, we will solve our error term equation.
  797. If we are clipping the start of the line, we will find the smallest
  798. N that satisfies our error term inequality. If we are clipping the
  799. end of the line, we will find the largest number of Y steps that
  800. satisfies the inequality. In that case, since we are representing
  801. the Y steps as (dy - N), we will actually want to solve for the
  802. smallest N in that equation.
  803. Case 1: X major, starting X coordinate moved by M steps
  804. -2dx <= 2Mdy - 2Ndx - dx - B < 0
  805. 2Ndx <= 2Mdy - dx - B + 2dx 2Ndx > 2Mdy - dx - B
  806. 2Ndx <= 2Mdy + dx - B N > (2Mdy - dx - B) / 2dx
  807. N <= (2Mdy + dx - B) / 2dx
  808. Since we are trying to find the smallest N that satisfies these
  809. equations, we should use the > inequality to find the smallest:
  810. N = floor((2Mdy - dx - B) / 2dx) + 1
  811. = floor((2Mdy - dx - B + 2dx) / 2dx)
  812. = floor((2Mdy + dx - B) / 2dx)
  813. Case 1b: X major, ending X coordinate moved to M steps
  814. Same derivations as Case 1, but we want the largest N that satisfies
  815. the equations, so we use the <= inequality:
  816. N = floor((2Mdy + dx - B) / 2dx)
  817. Case 2: X major, ending X coordinate moved by M steps
  818. -2dx <= 2(dx - M)dy - 2(dy - N)dx - dx - B < 0
  819. -2dx <= 2dxdy - 2Mdy - 2dxdy + 2Ndx - dx - B < 0
  820. -2dx <= 2Ndx - 2Mdy - dx - B < 0
  821. 2Ndx >= 2Mdy + dx + B - 2dx 2Ndx < 2Mdy + dx + B
  822. 2Ndx >= 2Mdy - dx + B N < (2Mdy + dx + B) / 2dx
  823. N >= (2Mdy - dx + B) / 2dx
  824. Since we are trying to find the highest number of Y steps that
  825. satisfies these equations, we need to find the smallest N, so
  826. we should use the >= inequality to find the smallest:
  827. N = ceiling((2Mdy - dx + B) / 2dx)
  828. = floor((2Mdy - dx + B + 2dx - 1) / 2dx)
  829. = floor((2Mdy + dx + B - 1) / 2dx)
  830. Case 2b: X major, starting X coordinate moved to M steps from end
  831. Same derivations as Case 2, but we want the smallest number of Y
  832. steps, so we want the highest N, so we use the < inequality:
  833. N = ceiling((2Mdy + dx + B) / 2dx) - 1
  834. = floor((2Mdy + dx + B + 2dx - 1) / 2dx) - 1
  835. = floor((2Mdy + dx + B + 2dx - 1 - 2dx) / 2dx)
  836. = floor((2Mdy + dx + B - 1) / 2dx)
  837. Case 3: Y major, starting X coordinate moved by M steps
  838. -2dy <= 2Ndx - 2Mdy - dy - B < 0
  839. 2Ndx >= 2Mdy + dy + B - 2dy 2Ndx < 2Mdy + dy + B
  840. 2Ndx >= 2Mdy - dy + B N < (2Mdy + dy + B) / 2dx
  841. N >= (2Mdy - dy + B) / 2dx
  842. Since we are trying to find the smallest N that satisfies these
  843. equations, we should use the >= inequality to find the smallest:
  844. N = ceiling((2Mdy - dy + B) / 2dx)
  845. = floor((2Mdy - dy + B + 2dx - 1) / 2dx)
  846. = floor((2Mdy - dy + B - 1) / 2dx) + 1
  847. Case 3b: Y major, ending X coordinate moved to M steps
  848. Same derivations as Case 3, but we want the largest N that satisfies
  849. the equations, so we use the < inequality:
  850. N = ceiling((2Mdy + dy + B) / 2dx) - 1
  851. = floor((2Mdy + dy + B + 2dx - 1) / 2dx) - 1
  852. = floor((2Mdy + dy + B + 2dx - 1 - 2dx) / 2dx)
  853. = floor((2Mdy + dy + B - 1) / 2dx)
  854. Case 4: Y major, ending X coordinate moved by M steps
  855. -2dy <= 2(dy - N)dx - 2(dx - M)dy - dy - B < 0
  856. -2dy <= 2dxdy - 2Ndx - 2dxdy + 2Mdy - dy - B < 0
  857. -2dy <= 2Mdy - 2Ndx - dy - B < 0
  858. 2Ndx <= 2Mdy - dy - B + 2dy 2Ndx > 2Mdy - dy - B
  859. 2Ndx <= 2Mdy + dy - B N > (2Mdy - dy - B) / 2dx
  860. N <= (2Mdy + dy - B) / 2dx
  861. Since we are trying to find the highest number of Y steps that
  862. satisfies these equations, we need to find the smallest N, so
  863. we should use the > inequality to find the smallest:
  864. N = floor((2Mdy - dy - B) / 2dx) + 1
  865. Case 4b: Y major, starting X coordinate moved to M steps from end
  866. Same analysis as Case 4, but we want the smallest number of Y steps
  867. which means the largest N, so we use the <= inequality:
  868. N = floor((2Mdy + dy - B) / 2dx)
  869. Now let's try the Y coordinates, we have the same 4 cases.
  870. Case 5: X major, starting Y coordinate moved by N steps
  871. -2dx <= 2Mdy - 2Ndx - dx - B < 0
  872. 2Mdy >= 2Ndx + dx + B - 2dx 2Mdy < 2Ndx + dx + B
  873. 2Mdy >= 2Ndx - dx + B M < (2Ndx + dx + B) / 2dy
  874. M >= (2Ndx - dx + B) / 2dy
  875. Since we are trying to find the smallest M, we use the >= inequality:
  876. M = ceiling((2Ndx - dx + B) / 2dy)
  877. = floor((2Ndx - dx + B + 2dy - 1) / 2dy)
  878. = floor((2Ndx - dx + B - 1) / 2dy) + 1
  879. Case 5b: X major, ending Y coordinate moved to N steps
  880. Same derivations as Case 5, but we want the largest M that satisfies
  881. the equations, so we use the < inequality:
  882. M = ceiling((2Ndx + dx + B) / 2dy) - 1
  883. = floor((2Ndx + dx + B + 2dy - 1) / 2dy) - 1
  884. = floor((2Ndx + dx + B + 2dy - 1 - 2dy) / 2dy)
  885. = floor((2Ndx + dx + B - 1) / 2dy)
  886. Case 6: X major, ending Y coordinate moved by N steps
  887. -2dx <= 2(dx - M)dy - 2(dy - N)dx - dx - B < 0
  888. -2dx <= 2dxdy - 2Mdy - 2dxdy + 2Ndx - dx - B < 0
  889. -2dx <= 2Ndx - 2Mdy - dx - B < 0
  890. 2Mdy <= 2Ndx - dx - B + 2dx 2Mdy > 2Ndx - dx - B
  891. 2Mdy <= 2Ndx + dx - B M > (2Ndx - dx - B) / 2dy
  892. M <= (2Ndx + dx - B) / 2dy
  893. Largest # of X steps means smallest M, so use the > inequality:
  894. M = floor((2Ndx - dx - B) / 2dy) + 1
  895. Case 6b: X major, starting Y coordinate moved to N steps from end
  896. Same derivations as Case 6, but we want the smallest # of X steps
  897. which means the largest M, so use the <= inequality:
  898. M = floor((2Ndx + dx - B) / 2dy)
  899. Case 7: Y major, starting Y coordinate moved by N steps
  900. -2dy <= 2Ndx - 2Mdy - dy - B < 0
  901. 2Mdy <= 2Ndx - dy - B + 2dy 2Mdy > 2Ndx - dy - B
  902. 2Mdy <= 2Ndx + dy - B M > (2Ndx - dy - B) / 2dy
  903. M <= (2Ndx + dy - B) / 2dy
  904. To find the smallest M, use the > inequality:
  905. M = floor((2Ndx - dy - B) / 2dy) + 1
  906. = floor((2Ndx - dy - B + 2dy) / 2dy)
  907. = floor((2Ndx + dy - B) / 2dy)
  908. Case 7b: Y major, ending Y coordinate moved to N steps
  909. Same derivations as Case 7, but we want the largest M that satisfies
  910. the equations, so use the <= inequality:
  911. M = floor((2Ndx + dy - B) / 2dy)
  912. Case 8: Y major, ending Y coordinate moved by N steps
  913. -2dy <= 2(dy - N)dx - 2(dx - M)dy - dy - B < 0
  914. -2dy <= 2dxdy - 2Ndx - 2dxdy + 2Mdy - dy - B < 0
  915. -2dy <= 2Mdy - 2Ndx - dy - B < 0
  916. 2Mdy >= 2Ndx + dy + B - 2dy 2Mdy < 2Ndx + dy + B
  917. 2Mdy >= 2Ndx - dy + B M < (2Ndx + dy + B) / 2dy
  918. M >= (2Ndx - dy + B) / 2dy
  919. To find the highest X steps, find the smallest M, use the >= inequality:
  920. M = ceiling((2Ndx - dy + B) / 2dy)
  921. = floor((2Ndx - dy + B + 2dy - 1) / 2dy)
  922. = floor((2Ndx + dy + B - 1) / 2dy)
  923. Case 8b: Y major, starting Y coordinate moved to N steps from the end
  924. Same derivations as Case 8, but we want to find the smallest # of X
  925. steps which means the largest M, so we use the < inequality:
  926. M = ceiling((2Ndx + dy + B) / 2dy) - 1
  927. = floor((2Ndx + dy + B + 2dy - 1) / 2dy) - 1
  928. = floor((2Ndx + dy + B + 2dy - 1 - 2dy) / 2dy)
  929. = floor((2Ndx + dy + B - 1) / 2dy)
  930. So, our equations are:
  931. 1: X major move x1 to x1+M floor((2Mdy + dx - B) / 2dx)
  932. 1b: X major move x2 to x1+M floor((2Mdy + dx - B) / 2dx)
  933. 2: X major move x2 to x2-M floor((2Mdy + dx + B - 1) / 2dx)
  934. 2b: X major move x1 to x2-M floor((2Mdy + dx + B - 1) / 2dx)
  935. 3: Y major move x1 to x1+M floor((2Mdy - dy + B - 1) / 2dx) + 1
  936. 3b: Y major move x2 to x1+M floor((2Mdy + dy + B - 1) / 2dx)
  937. 4: Y major move x2 to x2-M floor((2Mdy - dy - B) / 2dx) + 1
  938. 4b: Y major move x1 to x2-M floor((2Mdy + dy - B) / 2dx)
  939. 5: X major move y1 to y1+N floor((2Ndx - dx + B - 1) / 2dy) + 1
  940. 5b: X major move y2 to y1+N floor((2Ndx + dx + B - 1) / 2dy)
  941. 6: X major move y2 to y2-N floor((2Ndx - dx - B) / 2dy) + 1
  942. 6b: X major move y1 to y2-N floor((2Ndx + dx - B) / 2dy)
  943. 7: Y major move y1 to y1+N floor((2Ndx + dy - B) / 2dy)
  944. 7b: Y major move y2 to y1+N floor((2Ndx + dy - B) / 2dy)
  945. 8: Y major move y2 to y2-N floor((2Ndx + dy + B - 1) / 2dy)
  946. 8b: Y major move y1 to y2-N floor((2Ndx + dy + B - 1) / 2dy)
  947. We have the following constraints on all of the above terms:
  948. 0 < M,N <= 2^15 2^15 can be imposed by miZeroClipLine
  949. 0 <= dx/dy <= 2^16 - 1
  950. 0 <= B <= 1
  951. The floor in all of the above equations can be accomplished with a
  952. simple C divide operation provided that both numerator and denominator
  953. are positive.
  954. Since dx,dy >= 0 and since moving an X coordinate implies that dx != 0
  955. and moving a Y coordinate implies dy != 0, we know that the denominators
  956. are all > 0.
  957. For all lines, (-B) and (B-1) are both either 0 or -1, depending on the
  958. bias. Thus, we have to show that the 2MNdxy +/- dxy terms are all >= 1
  959. or > 0 to prove that the numerators are positive (or zero).
  960. For X Major lines we know that dx > 0 and since 2Mdy is >= 0 due to the
  961. constraints, the first four equations all have numerators >= 0.
  962. For the second four equations, M > 0, so 2Mdy >= 2dy so (2Mdy - dy) >= dy
  963. So (2Mdy - dy) > 0, since they are Y major lines. Also, (2Mdy + dy) >= 3dy
  964. or (2Mdy + dy) > 0. So all of their numerators are >= 0.
  965. For the third set of four equations, N > 0, so 2Ndx >= 2dx so (2Ndx - dx)
  966. >= dx > 0. Similarly (2Ndx + dx) >= 3dx > 0. So all numerators >= 0.
  967. For the fourth set of equations, dy > 0 and 2Ndx >= 0, so all numerators
  968. are > 0.
  969. To consider overflow, consider the case of 2 * M,N * dx,dy + dx,dy. This
  970. is bounded <= 2 * 2^15 * (2^16 - 1) + (2^16 - 1)
  971. <= 2^16 * (2^16 - 1) + (2^16 - 1)
  972. <= 2^32 - 2^16 + 2^16 - 1
  973. <= 2^32 - 1
  974. Since the (-B) and (B-1) terms are all 0 or -1, the maximum value of
  975. the numerator is therefore (2^32 - 1), which does not overflow an unsigned
  976. 32 bit variable.
  977. */
  978. /* Bit codes for the terms of the 16 clipping equations defined below. */
  979. #define T_2NDX (1 << 0)
  980. #define T_2MDY (0) /* implicit term */
  981. #define T_DXNOTY (1 << 1)
  982. #define T_DYNOTX (0) /* implicit term */
  983. #define T_SUBDXORY (1 << 2)
  984. #define T_ADDDX (T_DXNOTY) /* composite term */
  985. #define T_SUBDX (T_DXNOTY | T_SUBDXORY) /* composite term */
  986. #define T_ADDDY (T_DYNOTX) /* composite term */
  987. #define T_SUBDY (T_DYNOTX | T_SUBDXORY) /* composite term */
  988. #define T_BIASSUBONE (1 << 3)
  989. #define T_SUBBIAS (0) /* implicit term */
  990. #define T_DIV2DX (1 << 4)
  991. #define T_DIV2DY (0) /* implicit term */
  992. #define T_ADDONE (1 << 5)
  993. /* Bit masks defining the 16 equations used in miZeroClipLine. */
  994. #define EQN1 (T_2MDY | T_ADDDX | T_SUBBIAS | T_DIV2DX)
  995. #define EQN1B (T_2MDY | T_ADDDX | T_SUBBIAS | T_DIV2DX)
  996. #define EQN2 (T_2MDY | T_ADDDX | T_BIASSUBONE | T_DIV2DX)
  997. #define EQN2B (T_2MDY | T_ADDDX | T_BIASSUBONE | T_DIV2DX)
  998. #define EQN3 (T_2MDY | T_SUBDY | T_BIASSUBONE | T_DIV2DX | T_ADDONE)
  999. #define EQN3B (T_2MDY | T_ADDDY | T_BIASSUBONE | T_DIV2DX)
  1000. #define EQN4 (T_2MDY | T_SUBDY | T_SUBBIAS | T_DIV2DX | T_ADDONE)
  1001. #define EQN4B (T_2MDY | T_ADDDY | T_SUBBIAS | T_DIV2DX)
  1002. #define EQN5 (T_2NDX | T_SUBDX | T_BIASSUBONE | T_DIV2DY | T_ADDONE)
  1003. #define EQN5B (T_2NDX | T_ADDDX | T_BIASSUBONE | T_DIV2DY)
  1004. #define EQN6 (T_2NDX | T_SUBDX | T_SUBBIAS | T_DIV2DY | T_ADDONE)
  1005. #define EQN6B (T_2NDX | T_ADDDX | T_SUBBIAS | T_DIV2DY)
  1006. #define EQN7 (T_2NDX | T_ADDDY | T_SUBBIAS | T_DIV2DY)
  1007. #define EQN7B (T_2NDX | T_ADDDY | T_SUBBIAS | T_DIV2DY)
  1008. #define EQN8 (T_2NDX | T_ADDDY | T_BIASSUBONE | T_DIV2DY)
  1009. #define EQN8B (T_2NDX | T_ADDDY | T_BIASSUBONE | T_DIV2DY)
  1010. /* miZeroClipLine
  1011. *
  1012. * returns: 1 for partially clipped line
  1013. * -1 for completely clipped line
  1014. *
  1015. */
  1016. static int
  1017. miZeroClipLine (int xmin, int ymin, int xmax, int ymax,
  1018. int *new_x1, int *new_y1, int *new_x2, int *new_y2,
  1019. unsigned int adx, unsigned int ady,
  1020. int *pt1_clipped, int *pt2_clipped, int octant, unsigned int bias, int oc1, int oc2)
  1021. {
  1022. int swapped = 0;
  1023. int clipDone = 0;
  1024. CARD32 utmp = 0;
  1025. int clip1, clip2;
  1026. int x1, y1, x2, y2;
  1027. int x1_orig, y1_orig, x2_orig, y2_orig;
  1028. int xmajor;
  1029. int negslope = 0, anchorval = 0;
  1030. unsigned int eqn = 0;
  1031. x1 = x1_orig = *new_x1;
  1032. y1 = y1_orig = *new_y1;
  1033. x2 = x2_orig = *new_x2;
  1034. y2 = y2_orig = *new_y2;
  1035. clip1 = 0;
  1036. clip2 = 0;
  1037. xmajor = IsXMajorOctant (octant);
  1038. bias = ((bias >> octant) & 1);
  1039. while (1) {
  1040. if ((oc1 & oc2) != 0) { /* trivial reject */
  1041. clipDone = -1;
  1042. clip1 = oc1;
  1043. clip2 = oc2;
  1044. break;
  1045. } else if ((oc1 | oc2) == 0) { /* trivial accept */
  1046. clipDone = 1;
  1047. if (swapped) {
  1048. SWAPINT_PAIR (x1, y1, x2, y2);
  1049. SWAPINT (clip1, clip2);
  1050. }
  1051. break;
  1052. } else { /* have to clip */
  1053. /* only clip one point at a time */
  1054. if (oc1 == 0) {
  1055. SWAPINT_PAIR (x1, y1, x2, y2);
  1056. SWAPINT_PAIR (x1_orig, y1_orig, x2_orig, y2_orig);
  1057. SWAPINT (oc1, oc2);
  1058. SWAPINT (clip1, clip2);
  1059. swapped = !swapped;
  1060. }
  1061. clip1 |= oc1;
  1062. if (oc1 & OUT_LEFT) {
  1063. negslope = IsYDecreasingOctant (octant);
  1064. utmp = xmin - x1_orig;
  1065. if (utmp <= 32767) { /* clip based on near endpt */
  1066. if (xmajor)
  1067. eqn = (swapped) ? EQN2 : EQN1;
  1068. else
  1069. eqn = (swapped) ? EQN4 : EQN3;
  1070. anchorval = y1_orig;
  1071. } else { /* clip based on far endpt */
  1072. utmp = x2_orig - xmin;
  1073. if (xmajor)
  1074. eqn = (swapped) ? EQN1B : EQN2B;
  1075. else
  1076. eqn = (swapped) ? EQN3B : EQN4B;
  1077. anchorval = y2_orig;
  1078. negslope = !negslope;
  1079. }
  1080. x1 = xmin;
  1081. } else if (oc1 & OUT_ABOVE) {
  1082. negslope = IsXDecreasingOctant (octant);
  1083. utmp = ymin - y1_orig;
  1084. if (utmp <= 32767) { /* clip based on near endpt */
  1085. if (xmajor)
  1086. eqn = (swapped) ? EQN6 : EQN5;
  1087. else
  1088. eqn = (swapped) ? EQN8 : EQN7;
  1089. anchorval = x1_orig;
  1090. } else { /* clip based on far endpt */
  1091. utmp = y2_orig - ymin;
  1092. if (xmajor)
  1093. eqn = (swapped) ? EQN5B : EQN6B;
  1094. else
  1095. eqn = (swapped) ? EQN7B : EQN8B;
  1096. anchorval = x2_orig;
  1097. negslope = !negslope;
  1098. }
  1099. y1 = ymin;
  1100. } else if (oc1 & OUT_RIGHT) {
  1101. negslope = IsYDecreasingOctant (octant);
  1102. utmp = x1_orig - xmax;
  1103. if (utmp <= 32767) { /* clip based on near endpt */
  1104. if (xmajor)
  1105. eqn = (swapped) ? EQN2 : EQN1;
  1106. else
  1107. eqn = (swapped) ? EQN4 : EQN3;
  1108. anchorval = y1_orig;
  1109. } else { /* clip based on far endpt */
  1110. /*
  1111. * Technically since the equations can handle
  1112. * utmp == 32768, this overflow code isn't
  1113. * needed since X11 protocol can't generate
  1114. * a line which goes more than 32768 pixels
  1115. * to the right of a clip rectangle.
  1116. */
  1117. utmp = xmax - x2_orig;
  1118. if (xmajor)
  1119. eqn = (swapped) ? EQN1B : EQN2B;
  1120. else
  1121. eqn = (swapped) ? EQN3B : EQN4B;
  1122. anchorval = y2_orig;
  1123. negslope = !negslope;
  1124. }
  1125. x1 = xmax;
  1126. } else if (oc1 & OUT_BELOW) {
  1127. negslope = IsXDecreasingOctant (octant);
  1128. utmp = y1_orig - ymax;
  1129. if (utmp <= 32767) { /* clip based on near endpt */
  1130. if (xmajor)
  1131. eqn = (swapped) ? EQN6 : EQN5;
  1132. else
  1133. eqn = (swapped) ? EQN8 : EQN7;
  1134. anchorval = x1_orig;
  1135. } else { /* clip based on far endpt */
  1136. /*
  1137. * Technically since the equations can handle
  1138. * utmp == 32768, this overflow code isn't
  1139. * needed since X11 protocol can't generate
  1140. * a line which goes more than 32768 pixels
  1141. * below the bottom of a clip rectangle.
  1142. */
  1143. utmp = ymax - y2_orig;
  1144. if (xmajor)
  1145. eqn = (swapped) ? EQN5B : EQN6B;
  1146. else
  1147. eqn = (swapped) ? EQN7B : EQN8B;
  1148. anchorval = x2_orig;
  1149. negslope = !negslope;
  1150. }
  1151. y1 = ymax;
  1152. }
  1153. if (swapped)
  1154. negslope = !negslope;
  1155. utmp <<= 1; /* utmp = 2N or 2M */
  1156. if (eqn & T_2NDX)
  1157. utmp = (utmp * adx);
  1158. else /* (eqn & T_2MDY) */
  1159. utmp = (utmp * ady);
  1160. if (eqn & T_DXNOTY)
  1161. if (eqn & T_SUBDXORY)
  1162. utmp -= adx;
  1163. else
  1164. utmp += adx;
  1165. else /* (eqn & T_DYNOTX) */ if (eqn & T_SUBDXORY)
  1166. utmp -= ady;
  1167. else
  1168. utmp += ady;