Debian Codegraph @

src/Poly.c

raw
1 /*
2 *
3 * Copyright © 2002 Keith Packard
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of Keith Packard not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. Keith Packard makes no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
14 *
15 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 #include "Xrenderint.h"
28
29 typedef struct _Edge Edge;
30
31 struct _Edge {
32 XLineFixed edge;
33 XFixed current_x;
34 Bool clockWise;
35 Edge *next, *prev;
36 };
37
38 static int
39 CompareEdge (const void *o1, const void *o2)
40 {
41 const Edge *e1 = o1, *e2 = o2;
42
43 return e1->edge.p1.y - e2->edge.p1.y;
44 }
45
46 static XFixed
47 XRenderComputeX (XLineFixed *line, XFixed y)
48 {
49 XFixed dx = line->p2.x - line->p1.x;
50 double ex = (double) (y - line->p1.y) * (double) dx;
51 XFixed dy = line->p2.y - line->p1.y;
52
53 return (XFixed) line->p1.x + (XFixed) (ex / dy);
54 }
55
56 static double
57 XRenderComputeInverseSlope (XLineFixed *l)
58 {
59 return (XFixedToDouble (l->p2.x - l->p1.x) /
60 XFixedToDouble (l->p2.y - l->p1.y));
61 }
62
63 static double
64 XRenderComputeXIntercept (XLineFixed *l, double inverse_slope)
65 {
66 return XFixedToDouble (l->p1.x) - inverse_slope * XFixedToDouble (l->p1.y);
67 }
68
69 static XFixed
70 XRenderComputeIntersect (XLineFixed *l1, XLineFixed *l2)
71 {
72 /*
73 * x = m1y + b1
74 * x = m2y + b2
75 * m1y + b1 = m2y + b2
76 * y * (m1 - m2) = b2 - b1
77 * y = (b2 - b1) / (m1 - m2)
78 */
79 double m1 = XRenderComputeInverseSlope (l1);
80 double b1 = XRenderComputeXIntercept (l1, m1);
81 double m2 = XRenderComputeInverseSlope (l2);
82 double b2 = XRenderComputeXIntercept (l2, m2);
83
84 if ( m1 == m2 ) return XDoubleToFixed(32766); /* lines don't intersect */
85
86 return XDoubleToFixed ((b2 - b1) / (m1 - m2));
87 }
88
89 static int
90 XRenderComputeTrapezoids (Edge *edges,
91 int nedges,
92 int winding _X_UNUSED,
93 XTrapezoid *traps,
94 int *ntraps,
95 int maxtraps)
96 {
97 int ok = 1, inactive;
98 Edge *active;
99 Edge *e, *en, *next;
100 XFixed y, next_y, intersect;
101
102 qsort (edges, (size_t) nedges, sizeof (Edge), CompareEdge);
103
104 *ntraps = 0;
105 y = edges[0].edge.p1.y;
106 active = NULL;
107 inactive = 0;
108 while (active || inactive < nedges)
109 {
110 /* insert new active edges into list */
111 while (inactive < nedges)
112 {
113 e = &edges[inactive];
114 if (e->edge.p1.y > y)
115 break;
116 /* move this edge into the active list */
117 inactive++;
118 e->next = active;
119 e->prev = NULL;
120 if (active)
121 active->prev = e;
122 active = e;
123 }
124 /* compute x coordinates along this group */
125 for (e = active; e; e = e->next)
126 e->current_x = XRenderComputeX (&e->edge, y);
127
128 /* sort active list */
129 for (e = active; e; e = next)
130 {
131 next = e->next;
132 /*
133 * Find one later in the list that belongs before the
134 * current one
135 */
136 for (en = next; en; en = en->next)
137 {
138 if (en->current_x < e->current_x ||
139 (en->current_x == e->current_x &&
140 en->edge.p2.x < e->edge.p2.x))
141 {
142 /*
143 * insert en before e
144 *
145 * extract en
146 */
147 en->prev->next = en->next;
148 if (en->next)
149 en->next->prev = en->prev;
150 /*
151 * insert en
152 */
153 if (e->prev)
154 e->prev->next = en;
155 else
156 active = en;
157 en->prev = e->prev;
158 e->prev = en;
159 en->next = e;
160 /*
161 * start over at en
162 */
163 next = en;
164 break;
165 }
166 }
167 }
168 #if 0
169 printf ("y: %6.3g:", y / 65536.0);
170 for (e = active; e; e = e->next)
171 {
172 printf (" %6.3g", e->current_x / 65536.0);
173 }
174 printf ("\n");
175 #endif
176 /* find next inflection point */
177 next_y = active->edge.p2.y;
178 for (e = active; e; e = en)
179 {
180 if (e->edge.p2.y < next_y)
181 next_y = e->edge.p2.y;
182 en = e->next;
183 /* check intersect */
184 if (en && e->edge.p2.x > en->edge.p2.x)
185 {
186 intersect = XRenderComputeIntersect (&e->edge, &e->next->edge);
187 /* make sure this point is below the actual intersection */
188 intersect = intersect + 1;
189 if (intersect < next_y && intersect > y)
190 next_y = intersect;
191 }
192 }
193 /* check next inactive point */
194 if (inactive < nedges && edges[inactive].edge.p1.y < next_y)
195 next_y = edges[inactive].edge.p1.y;
196
197 /* walk the list generating trapezoids */
198 for (e = active; e && (en = e->next); e = en->next)
199 {
200 traps->top = y;
201 traps->bottom = next_y;
202 traps->left = e->edge;
203 traps->right = en->edge;
204 traps++;
205 (*ntraps)++;
206 if ( --maxtraps <= 0 ) {
207 ok = 0;
208 break;
209 }
210 }
211
212 y = next_y;
213
214 /* delete inactive edges from list */
215 for (e = active; e; e = next)
216 {
217 next = e->next;
218 if (e->edge.p2.y <= y)
219 {
220 if (e->prev)
221 e->prev->next = e->next;
222 else
223 active = e->next;
224 if (e->next)
225 e->next->prev = e->prev;
226 }
227 }
228 }
229 return ok;
230 }
231
232 void
233 XRenderCompositeDoublePoly (Display *dpy,
234 int op,
235 Picture src,
236 Picture dst,
237 _Xconst XRenderPictFormat *maskFormat,
238 int xSrc,
239 int ySrc,
240 int xDst _X_UNUSED,
241 int yDst _X_UNUSED,
242 _Xconst XPointDouble *fpoints,
243 int npoints,
244 int winding)
245 {
246 Edge *edges;
247 XTrapezoid *traps;
248 int i, nedges, ntraps;
249 XFixed prevx = 0, prevy = 0, firstx = 0, firsty = 0;
250 XFixed top = 0, bottom = 0; /* GCCism */
251
252 edges = Xmalloc (((size_t) npoints * sizeof (Edge)) +
253 ((size_t) (npoints * npoints) * sizeof (XTrapezoid)));
254 if (!edges)
255 return;
256 traps = (XTrapezoid *) (edges + npoints);
257 nedges = 0;
258 for (i = 0; i <= npoints; i++)
259 {
260 XFixed x, y;
261
262 if (i == npoints)
263 {
264 x = firstx;
265 y = firsty;
266 }
267 else
268 {
269 x = XDoubleToFixed (fpoints[i].x);
270 y = XDoubleToFixed (fpoints[i].y);
271 }
272 if (i)
273 {
274 if (y < top)
275 top = y;
276 else if (y > bottom)
277 bottom = y;
278 if (prevy < y)
279 {
280 edges[nedges].edge.p1.x = prevx;
281 edges[nedges].edge.p1.y = prevy;
282 edges[nedges].edge.p2.x = x;
283 edges[nedges].edge.p2.y = y;
284 edges[nedges].clockWise = True;
285 nedges++;
286 }
287 else if (prevy > y)
288 {
289 edges[nedges].edge.p1.x = x;
290 edges[nedges].edge.p1.y = y;
291 edges[nedges].edge.p2.x = prevx;
292 edges[nedges].edge.p2.y = prevy;
293 edges[nedges].clockWise = False;
294 nedges++;
295 }
296 /* drop horizontal edges */
297 }
298 else
299 {
300 top = y;
301 bottom = y;
302 firstx = x;
303 firsty = y;
304 }
305 prevx = x;
306 prevy = y;
307 }
308 if ( XRenderComputeTrapezoids (edges, nedges, winding, traps, &ntraps, npoints * npoints )) {
309 /* XXX adjust xSrc/xDst */
310 XRenderCompositeTrapezoids (dpy, op, src, dst, maskFormat, xSrc, ySrc, traps, ntraps);
311 }
312 Xfree (edges);
313 }