miscanfill.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. #ifdef HAVE_DIX_CONFIG_H
  23. #include <dix-config.h>
  24. #endif
  25. #ifndef SCANFILLINCLUDED
  26. #define SCANFILLINCLUDED
  27. /*
  28. * scanfill.h
  29. *
  30. * Written by Brian Kelleher; Jan 1985
  31. *
  32. * This file contains a few macros to help track
  33. * the edge of a filled object. The object is assumed
  34. * to be filled in scanline order, and thus the
  35. * algorithm used is an extension of Bresenham's line
  36. * drawing algorithm which assumes that y is always the
  37. * major axis.
  38. * Since these pieces of code are the same for any filled shape,
  39. * it is more convenient to gather the library in one
  40. * place, but since these pieces of code are also in
  41. * the inner loops of output primitives, procedure call
  42. * overhead is out of the question.
  43. * See the author for a derivation if needed.
  44. */
  45. /*
  46. * In scan converting polygons, we want to choose those pixels
  47. * which are inside the polygon. Thus, we add .5 to the starting
  48. * x coordinate for both left and right edges. Now we choose the
  49. * first pixel which is inside the pgon for the left edge and the
  50. * first pixel which is outside the pgon for the right edge.
  51. * Draw the left pixel, but not the right.
  52. *
  53. * How to add .5 to the starting x coordinate:
  54. * If the edge is moving to the right, then subtract dy from the
  55. * error term from the general form of the algorithm.
  56. * If the edge is moving to the left, then add dy to the error term.
  57. *
  58. * The reason for the difference between edges moving to the left
  59. * and edges moving to the right is simple: If an edge is moving
  60. * to the right, then we want the algorithm to flip immediately.
  61. * If it is moving to the left, then we don't want it to flip until
  62. * we traverse an entire pixel.
  63. */
  64. #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
  65. int dx; /* local storage */ \
  66. \
  67. /* \
  68. * if the edge is horizontal, then it is ignored \
  69. * and assumed not to be processed. Otherwise, do this stuff. \
  70. */ \
  71. if ((dy) != 0) { \
  72. xStart = (x1); \
  73. dx = (x2) - xStart; \
  74. if (dx < 0) { \
  75. m = dx / (dy); \
  76. m1 = m - 1; \
  77. incr1 = -2 * dx + 2 * (dy) * m1; \
  78. incr2 = -2 * dx + 2 * (dy) * m; \
  79. d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
  80. } else { \
  81. m = dx / (dy); \
  82. m1 = m + 1; \
  83. incr1 = 2 * dx - 2 * (dy) * m1; \
  84. incr2 = 2 * dx - 2 * (dy) * m; \
  85. d = -2 * m * (dy) + 2 * dx; \
  86. } \
  87. } \
  88. }
  89. #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
  90. if (m1 > 0) { \
  91. if (d > 0) { \
  92. minval += m1; \
  93. d += incr1; \
  94. } \
  95. else { \
  96. minval += m; \
  97. d += incr2; \
  98. } \
  99. } else {\
  100. if (d >= 0) { \
  101. minval += m1; \
  102. d += incr1; \
  103. } \
  104. else { \
  105. minval += m; \
  106. d += incr2; \
  107. } \
  108. } \
  109. }
  110. /*
  111. * This structure contains all of the information needed
  112. * to run the bresenham algorithm.
  113. * The variables may be hardcoded into the declarations
  114. * instead of using this structure to make use of
  115. * register declarations.
  116. */
  117. typedef struct {
  118. int minor; /* minor axis */
  119. int d; /* decision variable */
  120. int m, m1; /* slope and slope+1 */
  121. int incr1, incr2; /* error increments */
  122. } BRESINFO;
  123. #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
  124. BRESINITPGON(dmaj, min1, min2, bres.minor, bres.d, \
  125. bres.m, bres.m1, bres.incr1, bres.incr2)
  126. #define BRESINCRPGONSTRUCT(bres) \
  127. BRESINCRPGON(bres.d, bres.minor, bres.m, bres.m1, bres.incr1, bres.incr2)
  128. #endif