Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1274 lines
44 KiB

  1. /*************************************************************************\
  2. * Module Name: Lines.c
  3. *
  4. * Contains the code for drawing short fractional endpoint lines and
  5. * longer lines with strips. There is also a separate x86 Asm version
  6. * of this code.
  7. *
  8. * Copyright (c) 1990-1995 Microsoft Corporation
  9. * Copyright (c) 1992 Digital Equipment Corporation
  10. \**************************************************************************/
  11. #include "precomp.h"
  12. ///////////////////////////////////////////////////////////////////////
  13. // We have to be careful of arithmetic overflow in a number of places.
  14. // Fortunately, the compiler is guaranteed to natively support 64-bit
  15. // signed LONGLONGs and 64-bit unsigned DWORDLONGs.
  16. //
  17. // UUInt32x32To64(a, b) is a macro defined in 'winnt.h' that multiplies
  18. // two 32-bit ULONGs to produce a 64-bit DWORDLONG result.
  19. //
  20. // UInt64By32To32 is our own macro to divide a 64-bit DWORDLONG by
  21. // a 32-bit ULONG to produce a 32-bit ULONG result.
  22. //
  23. // UInt64Mod32To32 is our own macro to modulus a 64-bit DWORDLONG by
  24. // a 32-bit ULONG to produce a 32-bit ULONG result.
  25. //
  26. // 64 bit divides are usually very expensive. Since it's very rare
  27. // that we'll get lines where the upper 32 bits of the 64 bit result
  28. // are used, we can almost always use 32-bit ULONG divides. We still
  29. // must correctly handle the larger cases:
  30. #define UInt64Div32To32(a, b) \
  31. ((((DWORDLONG)(a)) > ULONG_MAX) ? \
  32. (ULONG)((DWORDLONG)(a) / (ULONG)(b)) : \
  33. (ULONG)((ULONG)(a) / (ULONG)(b)))
  34. #define UInt64Mod32To32(a, b) \
  35. ((((DWORDLONG)(a)) > ULONG_MAX) ? \
  36. (ULONG)((DWORDLONG)(a) % (ULONG)(b)) : \
  37. (ULONG)((ULONG)(a) % (ULONG)(b)))
  38. #define SWAPL(x,y,t) {t = x; x = y; y = t;}
  39. FLONG gaflRound[] = {
  40. FL_H_ROUND_DOWN | FL_V_ROUND_DOWN, // no flips
  41. FL_H_ROUND_DOWN | FL_V_ROUND_DOWN, // FL_FLIP_D
  42. FL_H_ROUND_DOWN, // FL_FLIP_V
  43. FL_V_ROUND_DOWN, // FL_FLIP_V | FL_FLIP_D
  44. FL_V_ROUND_DOWN, // FL_FLIP_SLOPE_ONE
  45. 0xbaadf00d, // FL_FLIP_SLOPE_ONE | FL_FLIP_D
  46. FL_H_ROUND_DOWN, // FL_FLIP_SLOPE_ONE | FL_FLIP_V
  47. 0xbaadf00d // FL_FLIP_SLOPE_ONE | FL_FLIP_V | FL_FLIP_D
  48. };
  49. /******************************Public*Routine******************************\
  50. * BOOL bLines(ppdev, pptfxFirst, pptfxBuf, cptfx, pls,
  51. * prclClip, apfn[], flStart)
  52. *
  53. * Computes the DDA for the line and gets ready to draw it. Puts the
  54. * pixel data into an array of strips, and calls a strip routine to
  55. * do the actual drawing.
  56. *
  57. * Doing NT Lines Right
  58. * --------------------
  59. *
  60. * In NT, all lines are given to the device driver in fractional
  61. * coordinates, in a 28.4 fixed point format. The lower 4 bits are
  62. * fractional for sub-pixel positioning.
  63. *
  64. * Note that you CANNOT! just round the coordinates to integers
  65. * and pass the results to your favorite integer Bresenham routine!!
  66. * (Unless, of course, you have such a high resolution device that
  67. * nobody will notice -- not likely for a display device.) The
  68. * fractions give a more accurate rendering of the line -- this is
  69. * important for things like our Bezier curves, which would have 'kinks'
  70. * if the points in its polyline approximation were rounded to integers.
  71. *
  72. * Unfortunately, for fractional lines there is more setup work to do
  73. * a DDA than for integer lines. However, the main loop is exactly
  74. * the same (and can be done entirely with 32 bit math).
  75. *
  76. * If You've Got Hardware That Does Bresenham
  77. * ------------------------------------------
  78. *
  79. * A lot of hardware limits DDA error terms to 'n' bits. With fractional
  80. * coordinates, 4 bits are given to the fractional part, letting
  81. * you draw in hardware only those lines that lie entirely in a 2^(n-4)
  82. * by 2^(n-4) pixel space.
  83. *
  84. * And you still have to correctly draw those lines with coordinates
  85. * outside that space! Remember that the screen is only a viewport
  86. * onto a 28.4 by 28.4 space -- if any part of the line is visible
  87. * you MUST render it precisely, regardless of where the end points lie.
  88. * So even if you do it in software, somewhere you'll have to have a
  89. * 32 bit DDA routine.
  90. *
  91. * Our Implementation
  92. * ------------------
  93. *
  94. * We employ a run length slice algorithm: our DDA calculates the
  95. * number of pixels that are in each row (or 'strip') of pixels.
  96. *
  97. * We've separated the running of the DDA and the drawing of pixels:
  98. * we run the DDA for several iterations and store the results in
  99. * a 'strip' buffer (which are the lengths of consecutive pixel rows of
  100. * the line), then we crank up a 'strip drawer' that will draw all the
  101. * strips in the buffer.
  102. *
  103. * We also employ a 'half-flip' to reduce the number of strip
  104. * iterations we need to do in the DDA and strip drawing loops: when a
  105. * (normalized) line's slope is more than 1/2, we do a final flip
  106. * about the line y = (1/2)x. So now, instead of each strip being
  107. * consecutive horizontal or vertical pixel rows, each strip is composed
  108. * of those pixels aligned in 45 degree rows. So a line like (0, 0) to
  109. * (128, 128) would generate only one strip.
  110. *
  111. * We also always draw only left-to-right.
  112. *
  113. * Styled lines may have arbitrary style patterns. We specially
  114. * optimize the default patterns (and call them 'masked' styles).
  115. *
  116. * The DDA Derivation
  117. * ------------------
  118. *
  119. * Here is how I like to think of the DDA calculation.
  120. *
  121. * We employ Knuth's "diamond rule": rendering a one-pixel-wide line
  122. * can be thought of as dragging a one-pixel-wide by one-pixel-high
  123. * diamond along the true line. Pixel centers lie on the integer
  124. * coordinates, and so we light any pixel whose center gets covered
  125. * by the "drag" region (John D. Hobby, Journal of the Association
  126. * for Computing Machinery, Vol. 36, No. 2, April 1989, pp. 209-229).
  127. *
  128. * We must define which pixel gets lit when the true line falls
  129. * exactly half-way between two pixels. In this case, we follow
  130. * the rule: when two pels are equidistant, the upper or left pel
  131. * is illuminated, unless the slope is exactly one, in which case
  132. * the upper or right pel is illuminated. (So we make the edges
  133. * of the diamond exclusive, except for the top and left vertices,
  134. * which are inclusive, unless we have slope one.)
  135. *
  136. * This metric decides what pixels should be on any line BEFORE it is
  137. * flipped around for our calculation. Having a consistent metric
  138. * this way will let our lines blend nicely with our curves. The
  139. * metric also dictates that we will never have one pixel turned on
  140. * directly above another that's turned on. We will also never have
  141. * a gap; i.e., there will be exactly one pixel turned on for each
  142. * column between the start and end points. All that remains to be
  143. * done is to decide how many pixels should be turned on for each row.
  144. *
  145. * So lines we draw will consist of varying numbers of pixels on
  146. * successive rows, for example:
  147. *
  148. * ******
  149. * *****
  150. * ******
  151. * *****
  152. *
  153. * We'll call each set of pixels on a row a "strip".
  154. *
  155. * (Please remember that our coordinate space has the origin as the
  156. * upper left pixel on the screen; postive y is down and positive x
  157. * is right.)
  158. *
  159. * Device coordinates are specified as fixed point 28.4 numbers,
  160. * where the first 28 bits are the integer coordinate, and the last
  161. * 4 bits are the fraction. So coordinates may be thought of as
  162. * having the form (x, y) = (M/F, N/F) where F is the constant scaling
  163. * factor F = 2^4 = 16, and M and N are 32 bit integers.
  164. *
  165. * Consider the line from (M0/F, N0/F) to (M1/F, N1/F) which runs
  166. * left-to-right and whose slope is in the first octant, and let
  167. * dM = M1 - M0 and dN = N1 - N0. Then dM >= 0, dN >= 0 and dM >= dN.
  168. *
  169. * Since the slope of the line is less than 1, the edges of the
  170. * drag region are created by the top and bottom vertices of the
  171. * diamond. At any given pixel row y of the line, we light those
  172. * pixels whose centers are between the left and right edges.
  173. *
  174. * Let mL(n) denote the line representing the left edge of the drag
  175. * region. On pixel row j, the column of the first pixel to be
  176. * lit is
  177. *
  178. * iL(j) = ceiling( mL(j * F) / F)
  179. *
  180. * Since the line's slope is less than one:
  181. *
  182. * iL(j) = ceiling( mL([j + 1/2] F) / F )
  183. *
  184. * Recall the formula for our line:
  185. *
  186. * n(m) = (dN / dM) (m - M0) + N0
  187. *
  188. * m(n) = (dM / dN) (n - N0) + M0
  189. *
  190. * Since the line's slope is less than one, the line representing
  191. * the left edge of the drag region is the original line offset
  192. * by 1/2 pixel in the y direction:
  193. *
  194. * mL(n) = (dM / dN) (n - F/2 - N0) + M0
  195. *
  196. * From this we can figure out the column of the first pixel that
  197. * will be lit on row j, being careful of rounding (if the left
  198. * edge lands exactly on an integer point, the pixel at that
  199. * point is not lit because of our rounding convention):
  200. *
  201. * iL(j) = floor( mL(j F) / F ) + 1
  202. *
  203. * = floor( ((dM / dN) (j F - F/2 - N0) + M0) / F ) + 1
  204. *
  205. * = floor( F dM j - F/2 dM - N0 dM + dN M0) / F dN ) + 1
  206. *
  207. * F dM j - [ dM (N0 + F/2) - dN M0 ]
  208. * = floor( ---------------------------------- ) + 1
  209. * F dN
  210. *
  211. * dM j - [ dM (N0 + F/2) - dN M0 ] / F
  212. * = floor( ------------------------------------ ) + 1 (1)
  213. * dN
  214. *
  215. * = floor( (dM j + alpha) / dN ) + 1
  216. *
  217. * where
  218. *
  219. * alpha = - [ dM (N0 + F/2) - dN M0 ] / F
  220. *
  221. * We use equation (1) to calculate the DDA: there are iL(j+1) - iL(j)
  222. * pixels in row j. Because we are always calculating iL(j) for
  223. * integer quantities of j, we note that the only fractional term
  224. * is constant, and so we can 'throw away' the fractional bits of
  225. * alpha:
  226. *
  227. * beta = floor( - [ dM (N0 + F/2) - dN M0 ] / F ) (2)
  228. *
  229. * so
  230. *
  231. * iL(j) = floor( (dM j + beta) / dN ) + 1 (3)
  232. *
  233. * for integers j.
  234. *
  235. * Note if iR(j) is the line's rightmost pixel on row j, that
  236. * iR(j) = iL(j + 1) - 1.
  237. *
  238. * Similarly, rewriting equation (1) as a function of column i,
  239. * we can determine, given column i, on which pixel row j is the line
  240. * lit:
  241. *
  242. * dN i + [ dM (N0 + F/2) - dN M0 ] / F
  243. * j(i) = ceiling( ------------------------------------ ) - 1
  244. * dM
  245. *
  246. * Floors are easier to compute, so we can rewrite this:
  247. *
  248. * dN i + [ dM (N0 + F/2) - dN M0 ] / F + dM - 1/F
  249. * j(i) = floor( ----------------------------------------------- ) - 1
  250. * dM
  251. *
  252. * dN i + [ dM (N0 + F/2) - dN M0 ] / F + dM - 1/F - dM
  253. * = floor( ---------------------------------------------------- )
  254. * dM
  255. *
  256. * dN i + [ dM (N0 + F/2) - dN M0 - 1 ] / F
  257. * = floor( ---------------------------------------- )
  258. * dM
  259. *
  260. * We can once again wave our hands and throw away the fractional bits
  261. * of the remainder term:
  262. *
  263. * j(i) = floor( (dN i + gamma) / dM ) (4)
  264. *
  265. * where
  266. *
  267. * gamma = floor( [ dM (N0 + F/2) - dN M0 - 1 ] / F ) (5)
  268. *
  269. * We now note that
  270. *
  271. * beta = -gamma - 1 = ~gamma (6)
  272. *
  273. * To draw the pixels of the line, we could evaluate (3) on every scan
  274. * line to determine where the strip starts. Of course, we don't want
  275. * to do that because that would involve a multiply and divide for every
  276. * scan. So we do everything incrementally.
  277. *
  278. * We would like to easily compute c , the number of pixels on scan j:
  279. * j
  280. *
  281. * c = iL(j + 1) - iL(j)
  282. * j
  283. *
  284. * = floor((dM (j + 1) + beta) / dN) - floor((dM j + beta) / dN) (7)
  285. *
  286. * This may be rewritten as
  287. *
  288. * c = floor(i + r / dN) - floor(i + r / dN) (8)
  289. * j j+1 j+1 j j
  290. *
  291. * where i , i are integers and r < dN, r < dN.
  292. * j j+1 j j+1
  293. *
  294. * Rewriting (7) again:
  295. *
  296. * c = floor(i + r / dN + dM / dN) - floor(i + r / dN)
  297. * j j j j j
  298. *
  299. *
  300. * = floor((r + dM) / dN) - floor(r / dN)
  301. * j j
  302. *
  303. * This may be rewritten as
  304. *
  305. * c = dI + floor((r + dR) / dN) - floor(r / dN)
  306. * j j j
  307. *
  308. * where dI + dR / dN = dM / dN, dI is an integer and dR < dN.
  309. *
  310. * r is the remainder (or "error") term in the DDA loop: r / dN
  311. * j j
  312. * is the exact fraction of a pixel at which the strip ends. To go
  313. * on to the next scan and compute c we need to know r .
  314. * j+1 j+1
  315. *
  316. * So in the main loop of the DDA:
  317. *
  318. * c = dI + floor((r + dR) / dN) and r = (r + dR) % dN
  319. * j j j+1 j
  320. *
  321. * and we know r < dN, r < dN, and dR < dN.
  322. * j j+1
  323. *
  324. * We have derived the DDA only for lines in the first octant; to
  325. * handle other octants we do the common trick of flipping the line
  326. * to the first octant by first making the line left-to-right by
  327. * exchanging the end-points, then flipping about the lines y = 0 and
  328. * y = x, as necessary. We must record the transformation so we can
  329. * undo them later.
  330. *
  331. * We must also be careful of how the flips affect our rounding. If
  332. * to get the line to the first octant we flipped about x = 0, we now
  333. * have to be careful to round a y value of 1/2 up instead of down as
  334. * we would for a line originally in the first octant (recall that
  335. * "In the case where two pels are equidistant, the upper or left
  336. * pel is illuminated...").
  337. *
  338. * To account for this rounding when running the DDA, we shift the line
  339. * (or not) in the y direction by the smallest amount possible. That
  340. * takes care of rounding for the DDA, but we still have to be careful
  341. * about the rounding when determining the first and last pixels to be
  342. * lit in the line.
  343. *
  344. * Determining The First And Last Pixels In The Line
  345. * -------------------------------------------------
  346. *
  347. * Fractional coordinates also make it harder to determine which pixels
  348. * will be the first and last ones in the line. We've already taken
  349. * the fractional coordinates into account in calculating the DDA, but
  350. * the DDA cannot tell us which are the end pixels because it is quite
  351. * happy to calculate pixels on the line from minus infinity to positive
  352. * infinity.
  353. *
  354. * The diamond rule determines the start and end pixels. (Recall that
  355. * the sides are exclusive except for the left and top vertices.)
  356. * This convention can be thought of in another way: there are diamonds
  357. * around the pixels, and wherever the true line crosses a diamond,
  358. * that pel is illuminated.
  359. *
  360. * Consider a line where we've done the flips to the first octant, and the
  361. * floor of the start coordinates is the origin:
  362. *
  363. * +-----------------------> +x
  364. * |
  365. * | 0 1
  366. * | 0123456789abcdef
  367. * |
  368. * | 0 00000000?1111111
  369. * | 1 00000000 1111111
  370. * | 2 0000000 111111
  371. * | 3 000000 11111
  372. * | 4 00000 ** 1111
  373. * | 5 0000 ****1
  374. * | 6 000 1***
  375. * | 7 00 1 ****
  376. * | 8 ? ***
  377. * | 9 22 3 ****
  378. * | a 222 33 ***
  379. * | b 2222 333 ****
  380. * | c 22222 3333 **
  381. * | d 222222 33333
  382. * | e 2222222 333333
  383. * | f 22222222 3333333
  384. * |
  385. * | 2 3
  386. * v
  387. * +y
  388. *
  389. * If the start of the line lands on the diamond around pixel 0 (shown by
  390. * the '0' region here), pixel 0 is the first pel in the line. The same
  391. * is true for the other pels.
  392. *
  393. * A little more work has to be done if the line starts in the
  394. * 'nether-land' between the diamonds (as illustrated by the '*' line):
  395. * the first pel lit is the first diamond crossed by the line (pixel 1 in
  396. * our example). This calculation is determined by the DDA or slope of
  397. * the line.
  398. *
  399. * If the line starts exactly half way between two adjacent pixels
  400. * (denoted here by the '?' spots), the first pixel is determined by our
  401. * round-down convention (and is dependent on the flips done to
  402. * normalize the line).
  403. *
  404. * Last Pel Exclusive
  405. * ------------------
  406. *
  407. * To eliminate repeatedly lit pels between continuous connected lines,
  408. * we employ a last-pel exclusive convention: if the line ends exactly on
  409. * the diamond around a pel, that pel is not lit. (This eliminates the
  410. * checks we had in the old code to see if we were re-lighting pels.)
  411. *
  412. * The Half Flip
  413. * -------------
  414. *
  415. * To make our run length algorithm more efficient, we employ a "half
  416. * flip". If after normalizing to the first octant, the slope is more
  417. * than 1/2, we subtract the y coordinate from the x coordinate. This
  418. * has the effect of reflecting the coordinates through the line of slope
  419. * 1/2. Note that the diagonal gets mapped into the x-axis after a half
  420. * flip.
  421. *
  422. * How Many Bits Do We Need, Anyway?
  423. * ---------------------------------
  424. *
  425. * Note that if the line is visible on your screen, you must light up
  426. * exactly the correct pixels, no matter where in the 28.4 x 28.4 device
  427. * space the end points of the line lie (meaning you must handle 32 bit
  428. * DDAs, you can certainly have optimized cases for lesser DDAs).
  429. *
  430. * We move the origin to (floor(M0 / F), floor(N0 / F)), so when we
  431. * calculate gamma from (5), we know that 0 <= M0, N0 < F. And we
  432. * are in the first octant, so dM >= dN. Then we know that gamma can
  433. * be in the range [(-1/2)dM, (3/2)dM]. The DDI guarantees us that
  434. * valid lines will have dM and dN values at most 31 bits (unsigned)
  435. * of significance. So gamma requires 33 bits of significance (we store
  436. * this as a 64 bit number for convenience).
  437. *
  438. * When running through the DDA loop, r + dR can have a value in the
  439. * j
  440. * range 0 <= r < 2 dN; thus the result must be a 32 bit unsigned value.
  441. * j
  442. *
  443. * Testing Lines
  444. * -------------
  445. *
  446. * To be NT compliant, a display driver must exactly adhere to GIQ,
  447. * which means that for any given line, the driver must light exactly
  448. * the same pels as does GDI. This can be tested using the Guiman tool
  449. * provided elsewhere in the DDK, and 'ZTest', which draws random lines
  450. * on the screen and to a bitmap, and compares the results.
  451. *
  452. * If You've Got Line Hardware
  453. * ---------------------------
  454. *
  455. * If your hardware already adheres to GIQ, you're all set. Otherwise
  456. * you'll want to look at the sample code and read the following:
  457. *
  458. * 1) You'll want to special case integer-only lines, since they require
  459. * less processing time and are more common (CAD programs will probably
  460. * only ever give integer lines). GDI does not provide a flag saying
  461. * that all lines in a path are integer lines; consequently, you will
  462. * have to explicitly check every line.
  463. *
  464. * 2) You are required to correctly draw any line in the 28.4 device
  465. * space that intersects the viewport. If you have less than 32 bits
  466. * of significance in the hardware for the Bresenham terms, extremely
  467. * long lines would overflow the hardware. For such (rare) cases, you
  468. * can fall back to strip-drawing code (or if your display is a frame
  469. * buffer, fall back to the engine).
  470. *
  471. * 3) If you can explicitly set the Bresenham terms in your hardware, you
  472. * can draw non-integer lines using the hardware. If your hardware has
  473. * 'n' bits of precision, you can draw GIQ lines that are up to 2^(n-5)
  474. * pels long (4 bits are required for the fractional part, and one bit is
  475. * used as a sign bit). Note that integer lines don't require the 4
  476. * fractional bits, so if you special case them as in 1), you can do
  477. * integer lines that are up to 2^(n - 1) pels long. See the
  478. * 'bHardwareLine' routine for an example.
  479. *
  480. \**************************************************************************/
  481. BOOL bLines(
  482. PDEV* ppdev,
  483. POINTFIX* pptfxFirst, // Start of first line
  484. POINTFIX* pptfxBuf, // Pointer to buffer of all remaining lines
  485. RUN* prun, // Pointer to runs if doing complex clipping
  486. ULONG cptfx, // Number of points in pptfxBuf or number of runs
  487. // in prun
  488. LINESTATE* pls, // Colour and style info
  489. RECTL* prclClip, // Pointer to clip rectangle if doing simple clipping
  490. PFNSTRIP apfn[], // Array of strip functions
  491. FLONG flStart) // Flags for each line, which is a combination of:
  492. // FL_SIMPLE_CLIP
  493. // FL_COMPLEX_CLIP
  494. // FL_STYLED
  495. // FL_LAST_PEL_INCLUSIVE
  496. // - Should be set only for all integer lines,
  497. // and can't be used with FL_COMPLEX_CLIP
  498. {
  499. ULONG M0;
  500. ULONG dM;
  501. ULONG N0;
  502. ULONG dN;
  503. ULONG dN_Original;
  504. FLONG fl;
  505. LONG x;
  506. LONG y;
  507. LONGLONG llBeta;
  508. LONGLONG llGamma;
  509. LONGLONG dl;
  510. LONGLONG ll;
  511. ULONG ulDelta;
  512. ULONG x0;
  513. ULONG y0;
  514. ULONG x1;
  515. ULONG cStylePels; // Major length of line in pixels for styling
  516. ULONG xStart;
  517. POINTL ptlStart;
  518. STRIP strip;
  519. PFNSTRIP pfn;
  520. LONG cPels;
  521. LONG* plStrip;
  522. LONG* plStripEnd;
  523. LONG cStripsInNextRun;
  524. POINTFIX* pptfxBufEnd = pptfxBuf + cptfx; // Last point in path record
  525. STYLEPOS spThis; // Style pos for this line
  526. do {
  527. /***********************************************************************\
  528. * Start the DDA calculations. *
  529. \***********************************************************************/
  530. M0 = (LONG) pptfxFirst->x;
  531. dM = (LONG) pptfxBuf->x;
  532. N0 = (LONG) pptfxFirst->y;
  533. dN = (LONG) pptfxBuf->y;
  534. fl = flStart;
  535. if ((LONG) M0 > (LONG) dM)
  536. {
  537. // Ensure that we run left-to-right:
  538. register ULONG ulTmp;
  539. SWAPL(M0, dM, ulTmp);
  540. SWAPL(N0, dN, ulTmp);
  541. fl |= FL_FLIP_H;
  542. }
  543. // Compute the delta dx. The DDI says we can never have a valid delta
  544. // with a magnitued more than 2^31 - 1, but GDI never actually checks
  545. // its transforms. So we have to check for this case to avoid overflow:
  546. dM -= M0;
  547. if ((LONG) dM < 0)
  548. {
  549. goto Next_Line;
  550. }
  551. if ((LONG) dN < (LONG) N0)
  552. {
  553. // Line runs from bottom to top, so flip across y = 0:
  554. N0 = -(LONG) N0;
  555. dN = -(LONG) dN;
  556. fl |= FL_FLIP_V;
  557. }
  558. dN -= N0;
  559. if ((LONG) dN < 0)
  560. {
  561. goto Next_Line;
  562. }
  563. // We now have a line running left-to-right, top-to-bottom from (M0, N0)
  564. // to (M0 + dM, N0 + dN):
  565. if (dN >= dM)
  566. {
  567. if (dN == dM)
  568. {
  569. // Have to special case slopes of one:
  570. fl |= FL_FLIP_SLOPE_ONE;
  571. }
  572. else
  573. {
  574. // Since line has slope greater than 1, flip across x = y:
  575. register ULONG ulTmp;
  576. SWAPL(dM, dN, ulTmp);
  577. SWAPL(M0, N0, ulTmp);
  578. fl |= FL_FLIP_D;
  579. }
  580. }
  581. fl |= gaflRound[(fl & FL_ROUND_MASK) >> FL_ROUND_SHIFT];
  582. x = LFLOOR((LONG) M0);
  583. y = LFLOOR((LONG) N0);
  584. M0 = FXFRAC(M0);
  585. N0 = FXFRAC(N0);
  586. // Calculate the remainder term [ dM * (N0 + F/2) - M0 * dN ]:
  587. llGamma = UInt32x32To64(dM, N0 + F/2) - UInt32x32To64(M0, dN);
  588. if (fl & FL_V_ROUND_DOWN) // Adjust so y = 1/2 rounds down
  589. {
  590. llGamma--;
  591. }
  592. llGamma >>= FLOG2;
  593. llBeta = ~llGamma;
  594. /***********************************************************************\
  595. * Figure out which pixels are at the ends of the line. *
  596. \***********************************************************************/
  597. // The toughest part of GIQ is determining the start and end pels.
  598. //
  599. // Our approach here is to calculate x0 and x1 (the inclusive start
  600. // and end columns of the line respectively, relative to our normalized
  601. // origin). Then x1 - x0 + 1 is the number of pels in the line. The
  602. // start point is easily calculated by plugging x0 into our line equation
  603. // (which takes care of whether y = 1/2 rounds up or down in value)
  604. // getting y0, and then undoing the normalizing flips to get back
  605. // into device space.
  606. //
  607. // We look at the fractional parts of the coordinates of the start and
  608. // end points, and call them (M0, N0) and (M1, N1) respectively, where
  609. // 0 <= M0, N0, M1, N1 < 16. We plot (M0, N0) on the following grid
  610. // to determine x0:
  611. //
  612. // +-----------------------> +x
  613. // |
  614. // | 0 1
  615. // | 0123456789abcdef
  616. // |
  617. // | 0 ........?xxxxxxx
  618. // | 1 ..........xxxxxx
  619. // | 2 ...........xxxxx
  620. // | 3 ............xxxx
  621. // | 4 .............xxx
  622. // | 5 ..............xx
  623. // | 6 ...............x
  624. // | 7 ................
  625. // | 8 ................
  626. // | 9 ......**........
  627. // | a ........****...x
  628. // | b ............****
  629. // | c .............xxx****
  630. // | d ............xxxx ****
  631. // | e ...........xxxxx ****
  632. // | f ..........xxxxxx
  633. // |
  634. // | 2 3
  635. // v
  636. //
  637. // +y
  638. //
  639. // This grid accounts for the appropriate rounding of GIQ and last-pel
  640. // exclusion. If (M0, N0) lands on an 'x', x0 = 2. If (M0, N0) lands
  641. // on a '.', x0 = 1. If (M0, N0) lands on a '?', x0 rounds up or down,
  642. // depending on what flips have been done to normalize the line.
  643. //
  644. // For the end point, if (M1, N1) lands on an 'x', x1 =
  645. // floor((M0 + dM) / 16) + 1. If (M1, N1) lands on a '.', x1 =
  646. // floor((M0 + dM)). If (M1, N1) lands on a '?', x1 rounds up or down,
  647. // depending on what flips have been done to normalize the line.
  648. //
  649. // Lines of exactly slope one require a special case for both the start
  650. // and end. For example, if the line ends such that (M1, N1) is (9, 1),
  651. // the line has gone exactly through (8, 0) -- which may be considered
  652. // to be part of 'x' because of rounding! So slopes of exactly slope
  653. // one going through (8, 0) must also be considered as belonging in 'x'.
  654. //
  655. // For lines that go left-to-right, we have the following grid:
  656. //
  657. // +-----------------------> +x
  658. // |
  659. // | 0 1
  660. // | 0123456789abcdef
  661. // |
  662. // | 0 xxxxxxxx?.......
  663. // | 1 xxxxxxx.........
  664. // | 2 xxxxxx..........
  665. // | 3 xxxxx...........
  666. // | 4 xxxx............
  667. // | 5 xxx.............
  668. // | 6 xx..............
  669. // | 7 x...............
  670. // | 8 x...............
  671. // | 9 x.....**........
  672. // | a xx......****....
  673. // | b xxx.........****
  674. // | c xxxx............****
  675. // | d xxxxx........... ****
  676. // | e xxxxxx.......... ****
  677. // | f xxxxxxx.........
  678. // |
  679. // | 2 3
  680. // v
  681. //
  682. // +y
  683. //
  684. // This grid accounts for the appropriate rounding of GIQ and last-pel
  685. // exclusion. If (M0, N0) lands on an 'x', x0 = 0. If (M0, N0) lands
  686. // on a '.', x0 = 1. If (M0, N0) lands on a '?', x0 rounds up or down,
  687. // depending on what flips have been done to normalize the line.
  688. //
  689. // For the end point, if (M1, N1) lands on an 'x', x1 =
  690. // floor((M0 + dM) / 16) - 1. If (M1, N1) lands on a '.', x1 =
  691. // floor((M0 + dM)). If (M1, N1) lands on a '?', x1 rounds up or down,
  692. // depending on what flips have been done to normalize the line.
  693. //
  694. // Lines of exactly slope one must be handled similarly to the right-to-
  695. // left case.
  696. {
  697. // Calculate x0, x1
  698. ULONG N1 = FXFRAC(N0 + dN);
  699. ULONG M1 = FXFRAC(M0 + dM);
  700. x1 = LFLOOR(M0 + dM);
  701. if (fl & FL_LAST_PEL_INCLUSIVE)
  702. {
  703. // It sure is easy to compute the first pel when lines have only
  704. // integer coordinates and are last-pel inclusive:
  705. x0 = 0;
  706. y0 = 0;
  707. // Last-pel inclusive lines that are exactly one pixel long
  708. // have a 'delta-x' and 'delta-y' equal to zero. The problem is
  709. // that our clip code assumes that 'delta-x' is always non-zero
  710. // (since it never happens with last-pel exclusive lines). As
  711. // an inelegant solution, we simply modify 'delta-x' in this
  712. // case -- because the line is exactly one pixel long, changing
  713. // the slope will obviously have no effect on rasterization.
  714. if (x1 == 0)
  715. {
  716. dM = 1;
  717. llGamma = 0;
  718. llBeta = ~llGamma;
  719. }
  720. }
  721. else
  722. {
  723. if (fl & FL_FLIP_H)
  724. {
  725. // ---------------------------------------------------------------
  726. // Line runs right-to-left: <----
  727. // Compute x1:
  728. if (N1 == 0)
  729. {
  730. if (LROUND(M1, fl & FL_H_ROUND_DOWN))
  731. {
  732. x1++;
  733. }
  734. }
  735. else if (abs((LONG) (N1 - F/2)) + M1 > F)
  736. {
  737. x1++;
  738. }
  739. if ((fl & (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  740. == (FL_FLIP_SLOPE_ONE))
  741. {
  742. // Have to special-case diagonal lines going through our
  743. // the point exactly equidistant between two horizontal
  744. // pixels, if we're supposed to round x=1/2 down:
  745. if ((N1 > 0) && (M1 == N1 + 8))
  746. x1++;
  747. // Don't you love special cases? Is this a rhetorical question?
  748. if ((N0 > 0) && (M0 == N0 + 8))
  749. {
  750. x0 = 2;
  751. ulDelta = dN;
  752. goto right_to_left_compute_y0;
  753. }
  754. }
  755. // Compute x0:
  756. x0 = 1;
  757. ulDelta = 0;
  758. if (N0 == 0)
  759. {
  760. if (LROUND(M0, fl & FL_H_ROUND_DOWN))
  761. {
  762. x0 = 2;
  763. ulDelta = dN;
  764. }
  765. }
  766. else if (abs((LONG) (N0 - F/2)) + M0 > F)
  767. {
  768. x0 = 2;
  769. ulDelta = dN;
  770. }
  771. // Compute y0:
  772. right_to_left_compute_y0:
  773. y0 = 0;
  774. ll = llGamma + (LONGLONG) ulDelta;
  775. if (ll >= (LONGLONG) (2 * dM - dN))
  776. y0 = 2;
  777. else if (ll >= (LONGLONG) (dM - dN))
  778. y0 = 1;
  779. }
  780. else
  781. {
  782. // ---------------------------------------------------------------
  783. // Line runs left-to-right: ---->
  784. // Compute x1:
  785. if (!(fl & FL_LAST_PEL_INCLUSIVE))
  786. x1--;
  787. if (M1 > 0)
  788. {
  789. if (N1 == 0)
  790. {
  791. if (LROUND(M1, fl & FL_H_ROUND_DOWN))
  792. x1++;
  793. }
  794. else if (abs((LONG) (N1 - F/2)) <= (LONG) M1)
  795. {
  796. x1++;
  797. }
  798. }
  799. if ((fl & (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  800. == (FL_FLIP_SLOPE_ONE | FL_H_ROUND_DOWN))
  801. {
  802. // Have to special-case diagonal lines going through our
  803. // the point exactly equidistant between two horizontal
  804. // pixels, if we're supposed to round x=1/2 down:
  805. if ((M1 > 0) && (N1 == M1 + 8))
  806. x1--;
  807. if ((M0 > 0) && (N0 == M0 + 8))
  808. {
  809. x0 = 0;
  810. goto left_to_right_compute_y0;
  811. }
  812. }
  813. // Compute x0:
  814. x0 = 0;
  815. if (M0 > 0)
  816. {
  817. if (N0 == 0)
  818. {
  819. if (LROUND(M0, fl & FL_H_ROUND_DOWN))
  820. x0 = 1;
  821. }
  822. else if (abs((LONG) (N0 - F/2)) <= (LONG) M0)
  823. {
  824. x0 = 1;
  825. }
  826. }
  827. // Compute y0:
  828. left_to_right_compute_y0:
  829. y0 = 0;
  830. if (llGamma >= (LONGLONG) (dM - (dN & (-(LONG) x0))))
  831. {
  832. y0 = 1;
  833. }
  834. }
  835. }
  836. }
  837. cStylePels = x1 - x0 + 1;
  838. if ((LONG) cStylePels <= 0)
  839. goto Next_Line;
  840. xStart = x0;
  841. /***********************************************************************\
  842. * Complex clipping. *
  843. \***********************************************************************/
  844. if (fl & FL_COMPLEX_CLIP)
  845. {
  846. dN_Original = dN;
  847. Continue_Complex_Clipping:
  848. if (fl & FL_FLIP_H)
  849. {
  850. // Line runs right-to-left <-----
  851. x0 = xStart + cStylePels - prun->iStop - 1;
  852. x1 = xStart + cStylePels - prun->iStart - 1;
  853. }
  854. else
  855. {
  856. // Line runs left-to-right ----->
  857. x0 = xStart + prun->iStart;
  858. x1 = xStart + prun->iStop;
  859. }
  860. prun++;
  861. // Reset some variables we'll nuke a little later:
  862. dN = dN_Original;
  863. pls->spNext = pls->spComplex;
  864. // No overflow since large integer math is used. Both values
  865. // will be positive:
  866. dl = UInt32x32To64(x0, dN) + llGamma;
  867. // y0 = dl / dM:
  868. y0 = UInt64Div32To32(dl, dM);
  869. ASSERTDD((LONG) y0 >= 0, "y0 weird: Goofed up end pel calc?");
  870. }
  871. /***********************************************************************\
  872. * Simple rectangular clipping. *
  873. \***********************************************************************/
  874. if (fl & FL_SIMPLE_CLIP)
  875. {
  876. ULONG y1;
  877. LONG xRight;
  878. LONG xLeft;
  879. LONG yBottom;
  880. LONG yTop;
  881. // Note that y0 and y1 are actually the lower and upper bounds,
  882. // respectively, of the y coordinates of the line (the line may
  883. // have actually shrunk due to first/last pel clipping).
  884. //
  885. // Also note that x0, y0 are not necessarily zero.
  886. RECTL* prcl = &prclClip[(fl & FL_RECTLCLIP_MASK) >>
  887. FL_RECTLCLIP_SHIFT];
  888. // Normalize to the same point we've normalized for the DDA
  889. // calculations:
  890. xRight = prcl->right - x;
  891. xLeft = prcl->left - x;
  892. yBottom = prcl->bottom - y;
  893. yTop = prcl->top - y;
  894. if (yBottom <= (LONG) y0 ||
  895. xRight <= (LONG) x0 ||
  896. xLeft > (LONG) x1)
  897. {
  898. Totally_Clipped:
  899. if (fl & FL_STYLED)
  900. {
  901. pls->spNext += cStylePels;
  902. if (pls->spNext >= pls->spTotal2)
  903. pls->spNext %= pls->spTotal2;
  904. }
  905. goto Next_Line;
  906. }
  907. if ((LONG) x1 >= xRight)
  908. x1 = xRight - 1;
  909. // We have to know the correct y1, which we haven't bothered to
  910. // calculate up until now. This multiply and divide is quite
  911. // expensive; we could replace it with code similar to that which
  912. // we used for computing y0.
  913. //
  914. // The reason why we need the actual value, and not an upper
  915. // bounds guess like y1 = LFLOOR(dM) + 2 is that we have to be
  916. // careful when calculating x(y) that y0 <= y <= y1, otherwise
  917. // we can overflow on the divide (which, needless to say, is very
  918. // bad).
  919. dl = UInt32x32To64(x1, dN) + llGamma;
  920. // y1 = dl / dM:
  921. y1 = UInt64Div32To32(dl, dM);
  922. if (yTop > (LONG) y1)
  923. goto Totally_Clipped;
  924. if (yBottom <= (LONG) y1)
  925. {
  926. y1 = yBottom;
  927. dl = UInt32x32To64(y1, dM) + llBeta;
  928. // x1 = dl / dN:
  929. x1 = UInt64Div32To32(dl, dN);
  930. }
  931. // At this point, we've taken care of calculating the intercepts
  932. // with the right and bottom edges. Now we work on the left and
  933. // top edges:
  934. if (xLeft > (LONG) x0)
  935. {
  936. x0 = xLeft;
  937. dl = UInt32x32To64(x0, dN) + llGamma;
  938. // y0 = dl / dM;
  939. y0 = UInt64Div32To32(dl, dM);
  940. if (yBottom <= (LONG) y0)
  941. goto Totally_Clipped;
  942. }
  943. if (yTop > (LONG) y0)
  944. {
  945. y0 = yTop;
  946. dl = UInt32x32To64(y0, dM) + llBeta;
  947. // x0 = dl / dN + 1;
  948. x0 = UInt64Div32To32(dl, dN) + 1;
  949. if (xRight <= (LONG) x0)
  950. goto Totally_Clipped;
  951. }
  952. ASSERTDD(x0 <= x1, "Improper rectangle clip");
  953. }
  954. /***********************************************************************\
  955. * Done clipping. Unflip if necessary. *
  956. \***********************************************************************/
  957. ptlStart.x = x + x0;
  958. ptlStart.y = y + y0;
  959. if (fl & FL_FLIP_D)
  960. {
  961. register LONG lTmp;
  962. SWAPL(ptlStart.x, ptlStart.y, lTmp);
  963. }
  964. if (fl & FL_FLIP_V)
  965. {
  966. ptlStart.y = -ptlStart.y;
  967. }
  968. cPels = x1 - x0 + 1;
  969. /***********************************************************************\
  970. * Style calculations. *
  971. \***********************************************************************/
  972. if (fl & FL_STYLED)
  973. {
  974. STYLEPOS sp;
  975. spThis = pls->spNext;
  976. pls->spNext += cStylePels;
  977. {
  978. if (pls->spNext >= pls->spTotal2)
  979. pls->spNext %= pls->spTotal2;
  980. if (fl & FL_FLIP_H)
  981. sp = pls->spNext - x0 + xStart;
  982. else
  983. sp = spThis + x0 - xStart;
  984. ASSERTDD(fl & FL_STYLED, "Oops");
  985. // Normalize our target style position:
  986. if ((sp < 0) || (sp >= pls->spTotal2))
  987. {
  988. sp %= pls->spTotal2;
  989. // The modulus of a negative number is not well-defined
  990. // in C -- if it's negative we'll adjust it so that it's
  991. // back in the range [0, spTotal2):
  992. if (sp < 0)
  993. sp += pls->spTotal2;
  994. }
  995. // Since we always draw the line left-to-right, but styling is
  996. // always done in the direction of the original line, we have
  997. // to figure out where we are in the style array for the left
  998. // edge of this line.
  999. if (fl & FL_FLIP_H)
  1000. {
  1001. // Line originally ran right-to-left:
  1002. sp = -sp;
  1003. if (sp < 0)
  1004. sp += pls->spTotal2;
  1005. pls->ulStyleMask = ~pls->ulStartMask;
  1006. pls->pspStart = &pls->aspRtoL[0];
  1007. pls->pspEnd = &pls->aspRtoL[pls->cStyle - 1];
  1008. }
  1009. else
  1010. {
  1011. // Line originally ran left-to-right:
  1012. pls->ulStyleMask = pls->ulStartMask;
  1013. pls->pspStart = &pls->aspLtoR[0];
  1014. pls->pspEnd = &pls->aspLtoR[pls->cStyle - 1];
  1015. }
  1016. if (sp >= pls->spTotal)
  1017. {
  1018. sp -= pls->spTotal;
  1019. if (pls->cStyle & 1)
  1020. pls->ulStyleMask = ~pls->ulStyleMask;
  1021. }
  1022. pls->psp = pls->pspStart;
  1023. while (sp >= *pls->psp)
  1024. sp -= *pls->psp++;
  1025. ASSERTDD(pls->psp <= pls->pspEnd,
  1026. "Flew off into NeverNeverLand");
  1027. pls->spRemaining = *pls->psp - sp;
  1028. if ((pls->psp - pls->pspStart) & 1)
  1029. pls->ulStyleMask = ~pls->ulStyleMask;
  1030. }
  1031. }
  1032. plStrip = &strip.alStrips[0];
  1033. plStripEnd = &strip.alStrips[STRIP_MAX]; // Is exclusive
  1034. cStripsInNextRun = 0x7fffffff;
  1035. strip.ptlStart = ptlStart;
  1036. // Now, run the DDA starting at (ptlStart.x, ptlStart.y)!
  1037. strip.flFlips = fl;
  1038. pfn = apfn[(fl & FL_STRIP_MASK) >> FL_STRIP_SHIFT];
  1039. // Now calculate the DDA variables needed to figure out how many pixels
  1040. // go in the very first strip:
  1041. {
  1042. register LONG i;
  1043. register ULONG dI;
  1044. register ULONG dR;
  1045. ULONG r;
  1046. if (dN == 0)
  1047. i = 0x7fffffff;
  1048. else
  1049. {
  1050. dl = UInt32x32To64(y0 + 1, dM) + llBeta;
  1051. ASSERTDD(dl >= 0, "Oops!");
  1052. // i = (dl / dN) - x0 + 1;
  1053. // r = (dl % dN);
  1054. i = UInt64Div32To32(dl, dN);
  1055. r = UInt64Mod32To32(dl, dN);
  1056. i = i - x0 + 1;
  1057. dI = dM / dN;
  1058. dR = dM % dN; // 0 <= dR < dN
  1059. ASSERTDD(dI > 0, "Weird dI");
  1060. }
  1061. ASSERTDD(i > 0 && i <= 0x7fffffff, "Weird initial strip length");
  1062. ASSERTDD(cPels > 0, "Zero pel line");
  1063. /***********************************************************************\
  1064. * Run the DDA! *
  1065. \***********************************************************************/
  1066. while(TRUE)
  1067. {
  1068. cPels -= i;
  1069. if (cPels <= 0)
  1070. break;
  1071. *plStrip++ = i;
  1072. if (plStrip == plStripEnd)
  1073. {
  1074. strip.cStrips = (LONG)(plStrip - &strip.alStrips[0]);
  1075. (*pfn)(ppdev, &strip, pls);
  1076. plStrip = &strip.alStrips[0];
  1077. }
  1078. i = dI;
  1079. r += dR;
  1080. if (r >= dN)
  1081. {
  1082. r -= dN;
  1083. i++;
  1084. }
  1085. }
  1086. *plStrip++ = cPels + i;
  1087. strip.cStrips = (LONG)(plStrip - &strip.alStrips[0]);
  1088. (*pfn)(ppdev, &strip, pls);
  1089. }
  1090. Next_Line:
  1091. if (fl & FL_COMPLEX_CLIP)
  1092. {
  1093. cptfx--;
  1094. if (cptfx != 0)
  1095. goto Continue_Complex_Clipping;
  1096. break;
  1097. }
  1098. else
  1099. {
  1100. pptfxFirst = pptfxBuf;
  1101. pptfxBuf++;
  1102. }
  1103. } while (pptfxBuf < pptfxBufEnd);
  1104. return(TRUE);
  1105. }