PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/mi/mifpolycon.c

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