mipolycon.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "gcstruct.h"
  40. #include "pixmap.h"
  41. #include "mi.h"
  42. #include "miscanfill.h"
  43. static int getPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty);
  44. /*
  45. * convexpoly.c
  46. *
  47. * Written by Brian Kelleher; Dec. 1985.
  48. *
  49. * Fill a convex polygon. If the given polygon
  50. * is not convex, then the result is undefined.
  51. * The algorithm is to order the edges from smallest
  52. * y to largest by partitioning the array into a left
  53. * edge list and a right edge list. The algorithm used
  54. * to traverse each edge is an extension of Bresenham's
  55. * line algorithm with y as the major axis.
  56. * For a derivation of the algorithm, see the author of
  57. * this code.
  58. */
  59. _X_EXPORT Bool
  60. miFillConvexPoly(dst, pgc, count, ptsIn)
  61. DrawablePtr dst;
  62. GCPtr pgc;
  63. int count; /* number of points */
  64. DDXPointPtr ptsIn; /* the points */
  65. {
  66. int xl = 0, xr = 0; /* x vals of left and right edges */
  67. int dl = 0, dr = 0; /* decision variables */
  68. int ml = 0, m1l = 0;/* left edge slope and slope+1 */
  69. int mr = 0, m1r = 0; /* right edge slope and slope+1 */
  70. int incr1l = 0, incr2l = 0; /* left edge error increments */
  71. int incr1r = 0, incr2r = 0; /* right edge error increments */
  72. int dy; /* delta y */
  73. int y; /* current scanline */
  74. int left, right; /* indices to first endpoints */
  75. int i; /* loop counter */
  76. int nextleft, nextright; /* indices to second endpoints */
  77. DDXPointPtr ptsOut, FirstPoint; /* output buffer */
  78. int *width, *FirstWidth; /* output buffer */
  79. int imin; /* index of smallest vertex (in y) */
  80. int ymin; /* y-extents of polygon */
  81. int ymax;
  82. /*
  83. * find leftx, bottomy, rightx, topy, and the index
  84. * of bottomy. Also translate the points.
  85. */
  86. imin = getPolyYBounds(ptsIn, count, &ymin, &ymax);
  87. dy = ymax - ymin + 1;
  88. if ((count < 3) || (dy < 0))
  89. return(TRUE);
  90. ptsOut = FirstPoint = (DDXPointPtr )ALLOCATE_LOCAL(sizeof(DDXPointRec)*dy);
  91. width = FirstWidth = (int *)ALLOCATE_LOCAL(sizeof(int) * dy);
  92. if(!FirstPoint || !FirstWidth)
  93. {
  94. if (FirstWidth) DEALLOCATE_LOCAL(FirstWidth);
  95. if (FirstPoint) DEALLOCATE_LOCAL(FirstPoint);
  96. return(FALSE);
  97. }
  98. nextleft = nextright = imin;
  99. y = ptsIn[nextleft].y;
  100. /*
  101. * loop through all edges of the polygon
  102. */
  103. do {
  104. /*
  105. * add a left edge if we need to
  106. */
  107. if (ptsIn[nextleft].y == y) {
  108. left = nextleft;
  109. /*
  110. * find the next edge, considering the end
  111. * conditions of the array.
  112. */
  113. nextleft++;
  114. if (nextleft >= count)
  115. nextleft = 0;
  116. /*
  117. * now compute all of the random information
  118. * needed to run the iterative algorithm.
  119. */
  120. BRESINITPGON(ptsIn[nextleft].y-ptsIn[left].y,
  121. ptsIn[left].x,ptsIn[nextleft].x,
  122. xl, dl, ml, m1l, incr1l, incr2l);
  123. }
  124. /*
  125. * add a right edge if we need to
  126. */
  127. if (ptsIn[nextright].y == y) {
  128. right = nextright;
  129. /*
  130. * find the next edge, considering the end
  131. * conditions of the array.
  132. */
  133. nextright--;
  134. if (nextright < 0)
  135. nextright = count-1;
  136. /*
  137. * now compute all of the random information
  138. * needed to run the iterative algorithm.
  139. */
  140. BRESINITPGON(ptsIn[nextright].y-ptsIn[right].y,
  141. ptsIn[right].x,ptsIn[nextright].x,
  142. xr, dr, mr, m1r, incr1r, incr2r);
  143. }
  144. /*
  145. * generate scans to fill while we still have
  146. * a right edge as well as a left edge.
  147. */
  148. i = min(ptsIn[nextleft].y, ptsIn[nextright].y) - y;
  149. /* in case we're called with non-convex polygon */
  150. if(i < 0)
  151. {
  152. DEALLOCATE_LOCAL(FirstWidth);
  153. DEALLOCATE_LOCAL(FirstPoint);
  154. return(TRUE);
  155. }
  156. while (i-- > 0)
  157. {
  158. ptsOut->y = y;
  159. /*
  160. * reverse the edges if necessary
  161. */
  162. if (xl < xr)
  163. {
  164. *(width++) = xr - xl;
  165. (ptsOut++)->x = xl;
  166. }
  167. else
  168. {
  169. *(width++) = xl - xr;
  170. (ptsOut++)->x = xr;
  171. }
  172. y++;
  173. /* increment down the edges */
  174. BRESINCRPGON(dl, xl, ml, m1l, incr1l, incr2l);
  175. BRESINCRPGON(dr, xr, mr, m1r, incr1r, incr2r);
  176. }
  177. } while (y != ymax);
  178. /*
  179. * Finally, fill the <remaining> spans
  180. */
  181. (*pgc->ops->FillSpans)(dst, pgc,
  182. ptsOut-FirstPoint,FirstPoint,FirstWidth,
  183. 1);
  184. DEALLOCATE_LOCAL(FirstWidth);
  185. DEALLOCATE_LOCAL(FirstPoint);
  186. return(TRUE);
  187. }
  188. /*
  189. * Find the index of the point with the smallest y.
  190. */
  191. static int
  192. getPolyYBounds(DDXPointPtr pts, int n, int *by, int *ty)
  193. {
  194. DDXPointPtr ptMin;
  195. int ymin, ymax;
  196. DDXPointPtr ptsStart = pts;
  197. ptMin = pts;
  198. ymin = ymax = (pts++)->y;
  199. while (--n > 0) {
  200. if (pts->y < ymin)
  201. {
  202. ptMin = pts;
  203. ymin = pts->y;
  204. }
  205. if(pts->y > ymax)
  206. ymax = pts->y;
  207. pts++;
  208. }
  209. *by = ymin;
  210. *ty = ymax;
  211. return(ptMin-ptsStart);
  212. }