mifpolycon.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <math.h>
  40. #include <X11/X.h>
  41. #include "gcstruct.h"
  42. #include "windowstr.h"
  43. #include "pixmapstr.h"
  44. #include "mifpoly.h"
  45. static int GetFPolyYBounds(register SppPointPtr pts, int n, double yFtrans,
  46. int *by, int *ty);
  47. #ifdef ICEILTEMPDECL
  48. ICEILTEMPDECL
  49. #endif
  50. /*
  51. * Written by Todd Newman; April. 1987.
  52. *
  53. * Fill a convex polygon. If the given polygon
  54. * is not convex, then the result is undefined.
  55. * The algorithm is to order the edges from smallest
  56. * y to largest by partitioning the array into a left
  57. * edge list and a right edge list. The algorithm used
  58. * to traverse each edge is digital differencing analyzer
  59. * line algorithm with y as the major axis. There's some funny linear
  60. * interpolation involved because of the subpixel postioning.
  61. */
  62. void
  63. miFillSppPoly(dst, pgc, count, ptsIn, xTrans, yTrans, xFtrans, yFtrans)
  64. DrawablePtr dst;
  65. GCPtr pgc;
  66. int count; /* number of points */
  67. SppPointPtr ptsIn; /* the points */
  68. int xTrans, yTrans; /* Translate each point by this */
  69. double xFtrans, yFtrans; /* translate before conversion
  70. by this amount. This provides
  71. a mechanism to match rounding
  72. errors with any shape that must
  73. meet the polygon exactly.
  74. */
  75. {
  76. double xl = 0.0, xr = 0.0, /* x vals of left and right edges */
  77. ml = 0.0, /* left edge slope */
  78. mr = 0.0, /* right edge slope */
  79. dy, /* delta y */
  80. i; /* loop counter */
  81. int y, /* current scanline */
  82. j,
  83. imin, /* index of vertex with smallest y */
  84. ymin, /* y-extents of polygon */
  85. ymax,
  86. *width,
  87. *FirstWidth, /* output buffer */
  88. *Marked; /* set if this vertex has been used */
  89. register int left, right, /* indices to first endpoints */
  90. nextleft,
  91. nextright; /* indices to second endpoints */
  92. DDXPointPtr ptsOut,
  93. FirstPoint; /* output buffer */
  94. if (pgc->miTranslate)
  95. {
  96. xTrans += dst->x;
  97. yTrans += dst->y;
  98. }
  99. imin = GetFPolyYBounds(ptsIn, count, yFtrans, &ymin, &ymax);
  100. y = ymax - ymin + 1;
  101. if ((count < 3) || (y <= 0))
  102. return;
  103. ptsOut = FirstPoint = (DDXPointPtr)ALLOCATE_LOCAL(sizeof(DDXPointRec) * y);
  104. width = FirstWidth = (int *) ALLOCATE_LOCAL(sizeof(int) * y);
  105. Marked = (int *) ALLOCATE_LOCAL(sizeof(int) * count);
  106. if(!ptsOut || !width || !Marked)
  107. {
  108. if (Marked) DEALLOCATE_LOCAL(Marked);
  109. if (width) DEALLOCATE_LOCAL(width);
  110. if (ptsOut) DEALLOCATE_LOCAL(ptsOut);
  111. return;
  112. }
  113. for(j = 0; j < count; j++)
  114. Marked[j] = 0;
  115. nextleft = nextright = imin;
  116. Marked[imin] = -1;
  117. y = ICEIL(ptsIn[nextleft].y + yFtrans);
  118. /*
  119. * loop through all edges of the polygon
  120. */
  121. do
  122. {
  123. /* add a left edge if we need to */
  124. if ((y > (ptsIn[nextleft].y + yFtrans) ||
  125. ISEQUAL(y, ptsIn[nextleft].y + yFtrans)) &&
  126. Marked[nextleft] != 1)
  127. {
  128. Marked[nextleft]++;
  129. left = nextleft++;
  130. /* find the next edge, considering the end conditions */
  131. if (nextleft >= count)
  132. nextleft = 0;
  133. /* now compute the starting point and slope */
  134. dy = ptsIn[nextleft].y - ptsIn[left].y;
  135. if (dy != 0.0)
  136. {
  137. ml = (ptsIn[nextleft].x - ptsIn[left].x) / dy;
  138. dy = y - (ptsIn[left].y + yFtrans);
  139. xl = (ptsIn[left].x + xFtrans) + ml * max(dy, 0);
  140. }
  141. }
  142. /* add a right edge if we need to */
  143. if ((y > ptsIn[nextright].y + yFtrans) ||
  144. (ISEQUAL(y, ptsIn[nextright].y + yFtrans)
  145. && Marked[nextright] != 1))
  146. {
  147. Marked[nextright]++;
  148. right = nextright--;
  149. /* find the next edge, considering the end conditions */
  150. if (nextright < 0)
  151. nextright = count - 1;
  152. /* now compute the starting point and slope */
  153. dy = ptsIn[nextright].y - ptsIn[right].y;
  154. if (dy != 0.0)
  155. {
  156. mr = (ptsIn[nextright].x - ptsIn[right].x) / dy;
  157. dy = y - (ptsIn[right].y + yFtrans);
  158. xr = (ptsIn[right].x + xFtrans) + mr * max(dy, 0);
  159. }
  160. }
  161. /*
  162. * generate scans to fill while we still have
  163. * a right edge as well as a left edge.
  164. */
  165. i = (min(ptsIn[nextleft].y, ptsIn[nextright].y) + yFtrans) - y;
  166. if (i < EPSILON)
  167. {
  168. if(Marked[nextleft] && Marked[nextright])
  169. {
  170. /* Arrgh, we're trapped! (no more points)
  171. * Out, we've got to get out of here before this decadence saps
  172. * our will completely! */
  173. break;
  174. }
  175. continue;
  176. }
  177. else
  178. {
  179. j = (int) i;
  180. if(!j)
  181. j++;
  182. }
  183. while (j > 0)
  184. {
  185. int cxl, cxr;
  186. ptsOut->y = (y) + yTrans;
  187. cxl = ICEIL(xl);
  188. cxr = ICEIL(xr);
  189. /* reverse the edges if necessary */
  190. if (xl < xr)
  191. {
  192. *(width++) = cxr - cxl;
  193. (ptsOut++)->x = cxl + xTrans;
  194. }
  195. else
  196. {
  197. *(width++) = cxl - cxr;
  198. (ptsOut++)->x = cxr + xTrans;
  199. }
  200. y++;
  201. /* increment down the edges */
  202. xl += ml;
  203. xr += mr;
  204. j--;
  205. }
  206. } while (y <= ymax);
  207. /* Finally, fill the spans we've collected */
  208. (*pgc->ops->FillSpans)(dst, pgc,
  209. ptsOut-FirstPoint, FirstPoint, FirstWidth, 1);
  210. DEALLOCATE_LOCAL(Marked);
  211. DEALLOCATE_LOCAL(FirstWidth);
  212. DEALLOCATE_LOCAL(FirstPoint);
  213. }
  214. /* Find the index of the point with the smallest y.also return the
  215. * smallest and largest y */
  216. static
  217. int
  218. GetFPolyYBounds(
  219. register SppPointPtr pts,
  220. int n,
  221. double yFtrans,
  222. int *by,
  223. int *ty)
  224. {
  225. SppPointPtr ptMin;
  226. double ymin, ymax;
  227. SppPointPtr ptsStart = pts;
  228. ptMin = pts;
  229. ymin = ymax = (pts++)->y;
  230. while (--n > 0) {
  231. if (pts->y < ymin)
  232. {
  233. ptMin = pts;
  234. ymin = pts->y;
  235. }
  236. if(pts->y > ymax)
  237. ymax = pts->y;
  238. pts++;
  239. }
  240. *by = ICEIL(ymin + yFtrans);
  241. *ty = ICEIL(ymax + yFtrans - 1);
  242. return(ptMin-ptsStart);
  243. }