mizerclip.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /***********************************************************
  2. Copyright 1987, 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 1987 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 <X11/X.h>
  40. #include "misc.h"
  41. #include "scrnintstr.h"
  42. #include "gcstruct.h"
  43. #include "windowstr.h"
  44. #include "pixmap.h"
  45. #include "mi.h"
  46. #include "miline.h"
  47. /*
  48. The bresenham error equation used in the mi/mfb/cfb line routines is:
  49. e = error
  50. dx = difference in raw X coordinates
  51. dy = difference in raw Y coordinates
  52. M = # of steps in X direction
  53. N = # of steps in Y direction
  54. B = 0 to prefer diagonal steps in a given octant,
  55. 1 to prefer axial steps in a given octant
  56. For X major lines:
  57. e = 2Mdy - 2Ndx - dx - B
  58. -2dx <= e < 0
  59. For Y major lines:
  60. e = 2Ndx - 2Mdy - dy - B
  61. -2dy <= e < 0
  62. At the start of the line, we have taken 0 X steps and 0 Y steps,
  63. so M = 0 and N = 0:
  64. X major e = 2Mdy - 2Ndx - dx - B
  65. = -dx - B
  66. Y major e = 2Ndx - 2Mdy - dy - B
  67. = -dy - B
  68. At the end of the line, we have taken dx X steps and dy Y steps,
  69. so M = dx and N = dy:
  70. X major e = 2Mdy - 2Ndx - dx - B
  71. = 2dxdy - 2dydx - dx - B
  72. = -dx - B
  73. Y major e = 2Ndx - 2Mdy - dy - B
  74. = 2dydx - 2dxdy - dy - B
  75. = -dy - B
  76. Thus, the error term is the same at the start and end of the line.
  77. Let us consider clipping an X coordinate. There are 4 cases which
  78. represent the two independent cases of clipping the start vs. the
  79. end of the line and an X major vs. a Y major line. In any of these
  80. cases, we know the number of X steps (M) and we wish to find the
  81. number of Y steps (N). Thus, we will solve our error term equation.
  82. If we are clipping the start of the line, we will find the smallest
  83. N that satisfies our error term inequality. If we are clipping the
  84. end of the line, we will find the largest number of Y steps that
  85. satisfies the inequality. In that case, since we are representing
  86. the Y steps as (dy - N), we will actually want to solve for the
  87. smallest N in that equation.
  88. Case 1: X major, starting X coordinate moved by M steps
  89. -2dx <= 2Mdy - 2Ndx - dx - B < 0
  90. 2Ndx <= 2Mdy - dx - B + 2dx 2Ndx > 2Mdy - dx - B
  91. 2Ndx <= 2Mdy + dx - B N > (2Mdy - dx - B) / 2dx
  92. N <= (2Mdy + dx - B) / 2dx
  93. Since we are trying to find the smallest N that satisfies these
  94. equations, we should use the > inequality to find the smallest:
  95. N = floor((2Mdy - dx - B) / 2dx) + 1
  96. = floor((2Mdy - dx - B + 2dx) / 2dx)
  97. = floor((2Mdy + dx - B) / 2dx)
  98. Case 1b: X major, ending X coordinate moved to M steps
  99. Same derivations as Case 1, but we want the largest N that satisfies
  100. the equations, so we use the <= inequality:
  101. N = floor((2Mdy + dx - B) / 2dx)
  102. Case 2: X major, ending X coordinate moved by M steps
  103. -2dx <= 2(dx - M)dy - 2(dy - N)dx - dx - B < 0
  104. -2dx <= 2dxdy - 2Mdy - 2dxdy + 2Ndx - dx - B < 0
  105. -2dx <= 2Ndx - 2Mdy - dx - B < 0
  106. 2Ndx >= 2Mdy + dx + B - 2dx 2Ndx < 2Mdy + dx + B
  107. 2Ndx >= 2Mdy - dx + B N < (2Mdy + dx + B) / 2dx
  108. N >= (2Mdy - dx + B) / 2dx
  109. Since we are trying to find the highest number of Y steps that
  110. satisfies these equations, we need to find the smallest N, so
  111. we should use the >= inequality to find the smallest:
  112. N = ceiling((2Mdy - dx + B) / 2dx)
  113. = floor((2Mdy - dx + B + 2dx - 1) / 2dx)
  114. = floor((2Mdy + dx + B - 1) / 2dx)
  115. Case 2b: X major, starting X coordinate moved to M steps from end
  116. Same derivations as Case 2, but we want the smallest number of Y
  117. steps, so we want the highest N, so we use the < inequality:
  118. N = ceiling((2Mdy + dx + B) / 2dx) - 1
  119. = floor((2Mdy + dx + B + 2dx - 1) / 2dx) - 1
  120. = floor((2Mdy + dx + B + 2dx - 1 - 2dx) / 2dx)
  121. = floor((2Mdy + dx + B - 1) / 2dx)
  122. Case 3: Y major, starting X coordinate moved by M steps
  123. -2dy <= 2Ndx - 2Mdy - dy - B < 0
  124. 2Ndx >= 2Mdy + dy + B - 2dy 2Ndx < 2Mdy + dy + B
  125. 2Ndx >= 2Mdy - dy + B N < (2Mdy + dy + B) / 2dx
  126. N >= (2Mdy - dy + B) / 2dx
  127. Since we are trying to find the smallest N that satisfies these
  128. equations, we should use the >= inequality to find the smallest:
  129. N = ceiling((2Mdy - dy + B) / 2dx)
  130. = floor((2Mdy - dy + B + 2dx - 1) / 2dx)
  131. = floor((2Mdy - dy + B - 1) / 2dx) + 1
  132. Case 3b: Y major, ending X coordinate moved to M steps
  133. Same derivations as Case 3, but we want the largest N that satisfies
  134. the equations, so we use the < inequality:
  135. N = ceiling((2Mdy + dy + B) / 2dx) - 1
  136. = floor((2Mdy + dy + B + 2dx - 1) / 2dx) - 1
  137. = floor((2Mdy + dy + B + 2dx - 1 - 2dx) / 2dx)
  138. = floor((2Mdy + dy + B - 1) / 2dx)
  139. Case 4: Y major, ending X coordinate moved by M steps
  140. -2dy <= 2(dy - N)dx - 2(dx - M)dy - dy - B < 0
  141. -2dy <= 2dxdy - 2Ndx - 2dxdy + 2Mdy - dy - B < 0
  142. -2dy <= 2Mdy - 2Ndx - dy - B < 0
  143. 2Ndx <= 2Mdy - dy - B + 2dy 2Ndx > 2Mdy - dy - B
  144. 2Ndx <= 2Mdy + dy - B N > (2Mdy - dy - B) / 2dx
  145. N <= (2Mdy + dy - B) / 2dx
  146. Since we are trying to find the highest number of Y steps that
  147. satisfies these equations, we need to find the smallest N, so
  148. we should use the > inequality to find the smallest:
  149. N = floor((2Mdy - dy - B) / 2dx) + 1
  150. Case 4b: Y major, starting X coordinate moved to M steps from end
  151. Same analysis as Case 4, but we want the smallest number of Y steps
  152. which means the largest N, so we use the <= inequality:
  153. N = floor((2Mdy + dy - B) / 2dx)
  154. Now let's try the Y coordinates, we have the same 4 cases.
  155. Case 5: X major, starting Y coordinate moved by N steps
  156. -2dx <= 2Mdy - 2Ndx - dx - B < 0
  157. 2Mdy >= 2Ndx + dx + B - 2dx 2Mdy < 2Ndx + dx + B
  158. 2Mdy >= 2Ndx - dx + B M < (2Ndx + dx + B) / 2dy
  159. M >= (2Ndx - dx + B) / 2dy
  160. Since we are trying to find the smallest M, we use the >= inequality:
  161. M = ceiling((2Ndx - dx + B) / 2dy)
  162. = floor((2Ndx - dx + B + 2dy - 1) / 2dy)
  163. = floor((2Ndx - dx + B - 1) / 2dy) + 1
  164. Case 5b: X major, ending Y coordinate moved to N steps
  165. Same derivations as Case 5, but we want the largest M that satisfies
  166. the equations, so we use the < inequality:
  167. M = ceiling((2Ndx + dx + B) / 2dy) - 1
  168. = floor((2Ndx + dx + B + 2dy - 1) / 2dy) - 1
  169. = floor((2Ndx + dx + B + 2dy - 1 - 2dy) / 2dy)
  170. = floor((2Ndx + dx + B - 1) / 2dy)
  171. Case 6: X major, ending Y coordinate moved by N steps
  172. -2dx <= 2(dx - M)dy - 2(dy - N)dx - dx - B < 0
  173. -2dx <= 2dxdy - 2Mdy - 2dxdy + 2Ndx - dx - B < 0
  174. -2dx <= 2Ndx - 2Mdy - dx - B < 0
  175. 2Mdy <= 2Ndx - dx - B + 2dx 2Mdy > 2Ndx - dx - B
  176. 2Mdy <= 2Ndx + dx - B M > (2Ndx - dx - B) / 2dy
  177. M <= (2Ndx + dx - B) / 2dy
  178. Largest # of X steps means smallest M, so use the > inequality:
  179. M = floor((2Ndx - dx - B) / 2dy) + 1
  180. Case 6b: X major, starting Y coordinate moved to N steps from end
  181. Same derivations as Case 6, but we want the smallest # of X steps
  182. which means the largest M, so use the <= inequality:
  183. M = floor((2Ndx + dx - B) / 2dy)
  184. Case 7: Y major, starting Y coordinate moved by N steps
  185. -2dy <= 2Ndx - 2Mdy - dy - B < 0
  186. 2Mdy <= 2Ndx - dy - B + 2dy 2Mdy > 2Ndx - dy - B
  187. 2Mdy <= 2Ndx + dy - B M > (2Ndx - dy - B) / 2dy
  188. M <= (2Ndx + dy - B) / 2dy
  189. To find the smallest M, use the > inequality:
  190. M = floor((2Ndx - dy - B) / 2dy) + 1
  191. = floor((2Ndx - dy - B + 2dy) / 2dy)
  192. = floor((2Ndx + dy - B) / 2dy)
  193. Case 7b: Y major, ending Y coordinate moved to N steps
  194. Same derivations as Case 7, but we want the largest M that satisfies
  195. the equations, so use the <= inequality:
  196. M = floor((2Ndx + dy - B) / 2dy)
  197. Case 8: Y major, ending Y coordinate moved by N steps
  198. -2dy <= 2(dy - N)dx - 2(dx - M)dy - dy - B < 0
  199. -2dy <= 2dxdy - 2Ndx - 2dxdy + 2Mdy - dy - B < 0
  200. -2dy <= 2Mdy - 2Ndx - dy - B < 0
  201. 2Mdy >= 2Ndx + dy + B - 2dy 2Mdy < 2Ndx + dy + B
  202. 2Mdy >= 2Ndx - dy + B M < (2Ndx + dy + B) / 2dy
  203. M >= (2Ndx - dy + B) / 2dy
  204. To find the highest X steps, find the smallest M, use the >= inequality:
  205. M = ceiling((2Ndx - dy + B) / 2dy)
  206. = floor((2Ndx - dy + B + 2dy - 1) / 2dy)
  207. = floor((2Ndx + dy + B - 1) / 2dy)
  208. Case 8b: Y major, starting Y coordinate moved to N steps from the end
  209. Same derivations as Case 8, but we want to find the smallest # of X
  210. steps which means the largest M, so we use the < inequality:
  211. M = ceiling((2Ndx + dy + B) / 2dy) - 1
  212. = floor((2Ndx + dy + B + 2dy - 1) / 2dy) - 1
  213. = floor((2Ndx + dy + B + 2dy - 1 - 2dy) / 2dy)
  214. = floor((2Ndx + dy + B - 1) / 2dy)
  215. So, our equations are:
  216. 1: X major move x1 to x1+M floor((2Mdy + dx - B) / 2dx)
  217. 1b: X major move x2 to x1+M floor((2Mdy + dx - B) / 2dx)
  218. 2: X major move x2 to x2-M floor((2Mdy + dx + B - 1) / 2dx)
  219. 2b: X major move x1 to x2-M floor((2Mdy + dx + B - 1) / 2dx)
  220. 3: Y major move x1 to x1+M floor((2Mdy - dy + B - 1) / 2dx) + 1
  221. 3b: Y major move x2 to x1+M floor((2Mdy + dy + B - 1) / 2dx)
  222. 4: Y major move x2 to x2-M floor((2Mdy - dy - B) / 2dx) + 1
  223. 4b: Y major move x1 to x2-M floor((2Mdy + dy - B) / 2dx)
  224. 5: X major move y1 to y1+N floor((2Ndx - dx + B - 1) / 2dy) + 1
  225. 5b: X major move y2 to y1+N floor((2Ndx + dx + B - 1) / 2dy)
  226. 6: X major move y2 to y2-N floor((2Ndx - dx - B) / 2dy) + 1
  227. 6b: X major move y1 to y2-N floor((2Ndx + dx - B) / 2dy)
  228. 7: Y major move y1 to y1+N floor((2Ndx + dy - B) / 2dy)
  229. 7b: Y major move y2 to y1+N floor((2Ndx + dy - B) / 2dy)
  230. 8: Y major move y2 to y2-N floor((2Ndx + dy + B - 1) / 2dy)
  231. 8b: Y major move y1 to y2-N floor((2Ndx + dy + B - 1) / 2dy)
  232. We have the following constraints on all of the above terms:
  233. 0 < M,N <= 2^15 2^15 can be imposed by miZeroClipLine
  234. 0 <= dx/dy <= 2^16 - 1
  235. 0 <= B <= 1
  236. The floor in all of the above equations can be accomplished with a
  237. simple C divide operation provided that both numerator and denominator
  238. are positive.
  239. Since dx,dy >= 0 and since moving an X coordinate implies that dx != 0
  240. and moving a Y coordinate implies dy != 0, we know that the denominators
  241. are all > 0.
  242. For all lines, (-B) and (B-1) are both either 0 or -1, depending on the
  243. bias. Thus, we have to show that the 2MNdxy +/- dxy terms are all >= 1
  244. or > 0 to prove that the numerators are positive (or zero).
  245. For X Major lines we know that dx > 0 and since 2Mdy is >= 0 due to the
  246. constraints, the first four equations all have numerators >= 0.
  247. For the second four equations, M > 0, so 2Mdy >= 2dy so (2Mdy - dy) >= dy
  248. So (2Mdy - dy) > 0, since they are Y major lines. Also, (2Mdy + dy) >= 3dy
  249. or (2Mdy + dy) > 0. So all of their numerators are >= 0.
  250. For the third set of four equations, N > 0, so 2Ndx >= 2dx so (2Ndx - dx)
  251. >= dx > 0. Similarly (2Ndx + dx) >= 3dx > 0. So all numerators >= 0.
  252. For the fourth set of equations, dy > 0 and 2Ndx >= 0, so all numerators
  253. are > 0.
  254. To consider overflow, consider the case of 2 * M,N * dx,dy + dx,dy. This
  255. is bounded <= 2 * 2^15 * (2^16 - 1) + (2^16 - 1)
  256. <= 2^16 * (2^16 - 1) + (2^16 - 1)
  257. <= 2^32 - 2^16 + 2^16 - 1
  258. <= 2^32 - 1
  259. Since the (-B) and (B-1) terms are all 0 or -1, the maximum value of
  260. the numerator is therefore (2^32 - 1), which does not overflow an unsigned
  261. 32 bit variable.
  262. */
  263. /* Bit codes for the terms of the 16 clipping equations defined below. */
  264. #define T_2NDX (1 << 0)
  265. #define T_2MDY (0) /* implicit term */
  266. #define T_DXNOTY (1 << 1)
  267. #define T_DYNOTX (0) /* implicit term */
  268. #define T_SUBDXORY (1 << 2)
  269. #define T_ADDDX (T_DXNOTY) /* composite term */
  270. #define T_SUBDX (T_DXNOTY | T_SUBDXORY) /* composite term */
  271. #define T_ADDDY (T_DYNOTX) /* composite term */
  272. #define T_SUBDY (T_DYNOTX | T_SUBDXORY) /* composite term */
  273. #define T_BIASSUBONE (1 << 3)
  274. #define T_SUBBIAS (0) /* implicit term */
  275. #define T_DIV2DX (1 << 4)
  276. #define T_DIV2DY (0) /* implicit term */
  277. #define T_ADDONE (1 << 5)
  278. /* Bit masks defining the 16 equations used in miZeroClipLine. */
  279. #define EQN1 (T_2MDY | T_ADDDX | T_SUBBIAS | T_DIV2DX)
  280. #define EQN1B (T_2MDY | T_ADDDX | T_SUBBIAS | T_DIV2DX)
  281. #define EQN2 (T_2MDY | T_ADDDX | T_BIASSUBONE | T_DIV2DX)
  282. #define EQN2B (T_2MDY | T_ADDDX | T_BIASSUBONE | T_DIV2DX)
  283. #define EQN3 (T_2MDY | T_SUBDY | T_BIASSUBONE | T_DIV2DX | T_ADDONE)
  284. #define EQN3B (T_2MDY | T_ADDDY | T_BIASSUBONE | T_DIV2DX)
  285. #define EQN4 (T_2MDY | T_SUBDY | T_SUBBIAS | T_DIV2DX | T_ADDONE)
  286. #define EQN4B (T_2MDY | T_ADDDY | T_SUBBIAS | T_DIV2DX)
  287. #define EQN5 (T_2NDX | T_SUBDX | T_BIASSUBONE | T_DIV2DY | T_ADDONE)
  288. #define EQN5B (T_2NDX | T_ADDDX | T_BIASSUBONE | T_DIV2DY)
  289. #define EQN6 (T_2NDX | T_SUBDX | T_SUBBIAS | T_DIV2DY | T_ADDONE)
  290. #define EQN6B (T_2NDX | T_ADDDX | T_SUBBIAS | T_DIV2DY)
  291. #define EQN7 (T_2NDX | T_ADDDY | T_SUBBIAS | T_DIV2DY)
  292. #define EQN7B (T_2NDX | T_ADDDY | T_SUBBIAS | T_DIV2DY)
  293. #define EQN8 (T_2NDX | T_ADDDY | T_BIASSUBONE | T_DIV2DY)
  294. #define EQN8B (T_2NDX | T_ADDDY | T_BIASSUBONE | T_DIV2DY)
  295. /* miZeroClipLine
  296. *
  297. * returns: 1 for partially clipped line
  298. * -1 for completely clipped line
  299. *
  300. */
  301. _X_EXPORT int
  302. miZeroClipLine(xmin, ymin, xmax, ymax,
  303. new_x1, new_y1, new_x2, new_y2,
  304. adx, ady,
  305. pt1_clipped, pt2_clipped, octant, bias, oc1, oc2)
  306. int xmin, ymin, xmax, ymax;
  307. int *new_x1, *new_y1, *new_x2, *new_y2;
  308. int *pt1_clipped, *pt2_clipped;
  309. unsigned int adx, ady;
  310. int octant;
  311. unsigned int bias;
  312. int oc1, oc2;
  313. {
  314. int swapped = 0;
  315. int clipDone = 0;
  316. CARD32 utmp = 0;
  317. int clip1, clip2;
  318. int x1, y1, x2, y2;
  319. int x1_orig, y1_orig, x2_orig, y2_orig;
  320. int xmajor;
  321. int negslope = 0, anchorval = 0;
  322. unsigned int eqn = 0;
  323. x1 = x1_orig = *new_x1;
  324. y1 = y1_orig = *new_y1;
  325. x2 = x2_orig = *new_x2;
  326. y2 = y2_orig = *new_y2;
  327. clip1 = 0;
  328. clip2 = 0;
  329. xmajor = IsXMajorOctant(octant);
  330. bias = ((bias >> octant) & 1);
  331. while (1)
  332. {
  333. if ((oc1 & oc2) != 0) /* trivial reject */
  334. {
  335. clipDone = -1;
  336. clip1 = oc1;
  337. clip2 = oc2;
  338. break;
  339. }
  340. else if ((oc1 | oc2) == 0) /* trivial accept */
  341. {
  342. clipDone = 1;
  343. if (swapped)
  344. {
  345. SWAPINT_PAIR(x1, y1, x2, y2);
  346. SWAPINT(clip1, clip2);
  347. }
  348. break;
  349. }
  350. else /* have to clip */
  351. {
  352. /* only clip one point at a time */
  353. if (oc1 == 0)
  354. {
  355. SWAPINT_PAIR(x1, y1, x2, y2);
  356. SWAPINT_PAIR(x1_orig, y1_orig, x2_orig, y2_orig);
  357. SWAPINT(oc1, oc2);
  358. SWAPINT(clip1, clip2);
  359. swapped = !swapped;
  360. }
  361. clip1 |= oc1;
  362. if (oc1 & OUT_LEFT)
  363. {
  364. negslope = IsYDecreasingOctant(octant);
  365. utmp = xmin - x1_orig;
  366. if (utmp <= 32767) /* clip based on near endpt */
  367. {
  368. if (xmajor)
  369. eqn = (swapped) ? EQN2 : EQN1;
  370. else
  371. eqn = (swapped) ? EQN4 : EQN3;
  372. anchorval = y1_orig;
  373. }
  374. else /* clip based on far endpt */
  375. {
  376. utmp = x2_orig - xmin;
  377. if (xmajor)
  378. eqn = (swapped) ? EQN1B : EQN2B;
  379. else
  380. eqn = (swapped) ? EQN3B : EQN4B;
  381. anchorval = y2_orig;
  382. negslope = !negslope;
  383. }
  384. x1 = xmin;
  385. }
  386. else if (oc1 & OUT_ABOVE)
  387. {
  388. negslope = IsXDecreasingOctant(octant);
  389. utmp = ymin - y1_orig;
  390. if (utmp <= 32767) /* clip based on near endpt */
  391. {
  392. if (xmajor)
  393. eqn = (swapped) ? EQN6 : EQN5;
  394. else
  395. eqn = (swapped) ? EQN8 : EQN7;
  396. anchorval = x1_orig;
  397. }
  398. else /* clip based on far endpt */
  399. {
  400. utmp = y2_orig - ymin;
  401. if (xmajor)
  402. eqn = (swapped) ? EQN5B : EQN6B;
  403. else
  404. eqn = (swapped) ? EQN7B : EQN8B;
  405. anchorval = x2_orig;
  406. negslope = !negslope;
  407. }
  408. y1 = ymin;
  409. }
  410. else if (oc1 & OUT_RIGHT)
  411. {
  412. negslope = IsYDecreasingOctant(octant);
  413. utmp = x1_orig - xmax;
  414. if (utmp <= 32767) /* clip based on near endpt */
  415. {
  416. if (xmajor)
  417. eqn = (swapped) ? EQN2 : EQN1;
  418. else
  419. eqn = (swapped) ? EQN4 : EQN3;
  420. anchorval = y1_orig;
  421. }
  422. else /* clip based on far endpt */
  423. {
  424. /*
  425. * Technically since the equations can handle
  426. * utmp == 32768, this overflow code isn't
  427. * needed since X11 protocol can't generate
  428. * a line which goes more than 32768 pixels
  429. * to the right of a clip rectangle.
  430. */
  431. utmp = xmax - x2_orig;
  432. if (xmajor)
  433. eqn = (swapped) ? EQN1B : EQN2B;
  434. else
  435. eqn = (swapped) ? EQN3B : EQN4B;
  436. anchorval = y2_orig;
  437. negslope = !negslope;
  438. }
  439. x1 = xmax;
  440. }
  441. else if (oc1 & OUT_BELOW)
  442. {
  443. negslope = IsXDecreasingOctant(octant);
  444. utmp = y1_orig - ymax;
  445. if (utmp <= 32767) /* clip based on near endpt */
  446. {
  447. if (xmajor)
  448. eqn = (swapped) ? EQN6 : EQN5;
  449. else
  450. eqn = (swapped) ? EQN8 : EQN7;
  451. anchorval = x1_orig;
  452. }
  453. else /* clip based on far endpt */
  454. {
  455. /*
  456. * Technically since the equations can handle
  457. * utmp == 32768, this overflow code isn't
  458. * needed since X11 protocol can't generate
  459. * a line which goes more than 32768 pixels
  460. * below the bottom of a clip rectangle.
  461. */
  462. utmp = ymax - y2_orig;
  463. if (xmajor)
  464. eqn = (swapped) ? EQN5B : EQN6B;
  465. else
  466. eqn = (swapped) ? EQN7B : EQN8B;
  467. anchorval = x2_orig;
  468. negslope = !negslope;
  469. }
  470. y1 = ymax;
  471. }
  472. if (swapped)
  473. negslope = !negslope;
  474. utmp <<= 1; /* utmp = 2N or 2M */
  475. if (eqn & T_2NDX)
  476. utmp = (utmp * adx);
  477. else /* (eqn & T_2MDY) */
  478. utmp = (utmp * ady);
  479. if (eqn & T_DXNOTY)
  480. if (eqn & T_SUBDXORY)
  481. utmp -= adx;
  482. else
  483. utmp += adx;
  484. else /* (eqn & T_DYNOTX) */
  485. if (eqn & T_SUBDXORY)
  486. utmp -= ady;
  487. else
  488. utmp += ady;
  489. if (eqn & T_BIASSUBONE)
  490. utmp += bias - 1;
  491. else /* (eqn & T_SUBBIAS) */
  492. utmp -= bias;
  493. if (eqn & T_DIV2DX)
  494. utmp /= (adx << 1);
  495. else /* (eqn & T_DIV2DY) */
  496. utmp /= (ady << 1);
  497. if (eqn & T_ADDONE)
  498. utmp++;
  499. if (negslope)
  500. utmp = -utmp;
  501. if (eqn & T_2NDX) /* We are calculating X steps */
  502. x1 = anchorval + utmp;
  503. else /* else, Y steps */
  504. y1 = anchorval + utmp;
  505. oc1 = 0;
  506. MIOUTCODES(oc1, x1, y1, xmin, ymin, xmax, ymax);
  507. }
  508. }
  509. *new_x1 = x1;
  510. *new_y1 = y1;
  511. *new_x2 = x2;
  512. *new_y2 = y2;
  513. *pt1_clipped = clip1;
  514. *pt2_clipped = clip2;
  515. return clipDone;
  516. }