Change all head -1 to head -n 1 to be more POSIXly compliant.
[wine.git] / objects / region.c
blob8c66eae5818012be5fce661f7d7f274a0daab0cf
1 /*
2 * GDI region objects. Shamelessly ripped out from the X11 distribution
3 * Thanks for the nice licence.
5 * Copyright 1993, 1994, 1995 Alexandre Julliard
6 * Modifications and additions: Copyright 1998 Huw Davies
7 * 1999 Alex Korobka
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /************************************************************************
26 Copyright (c) 1987, 1988 X Consortium
28 Permission is hereby granted, free of charge, to any person obtaining a copy
29 of this software and associated documentation files (the "Software"), to deal
30 in the Software without restriction, including without limitation the rights
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 copies of the Software, and to permit persons to whom the Software is
33 furnished to do so, subject to the following conditions:
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 Except as contained in this notice, the name of the X Consortium shall not be
46 used in advertising or otherwise to promote the sale, use or other dealings
47 in this Software without prior written authorization from the X Consortium.
50 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
52 All Rights Reserved
54 Permission to use, copy, modify, and distribute this software and its
55 documentation for any purpose and without fee is hereby granted,
56 provided that the above copyright notice appear in all copies and that
57 both that copyright notice and this permission notice appear in
58 supporting documentation, and that the name of Digital not be
59 used in advertising or publicity pertaining to distribution of the
60 software without specific, written prior permission.
62 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
63 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
64 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
65 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
66 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
67 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68 SOFTWARE.
70 ************************************************************************/
72 * The functions in this file implement the Region abstraction, similar to one
73 * used in the X11 sample server. A Region is simply an area, as the name
74 * implies, and is implemented as a "y-x-banded" array of rectangles. To
75 * explain: Each Region is made up of a certain number of rectangles sorted
76 * by y coordinate first, and then by x coordinate.
78 * Furthermore, the rectangles are banded such that every rectangle with a
79 * given upper-left y coordinate (y1) will have the same lower-right y
80 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
81 * will span the entire vertical distance of the band. This means that some
82 * areas that could be merged into a taller rectangle will be represented as
83 * several shorter rectangles to account for shorter rectangles to its left
84 * or right but within its "vertical scope".
86 * An added constraint on the rectangles is that they must cover as much
87 * horizontal area as possible. E.g. no two rectangles in a band are allowed
88 * to touch.
90 * Whenever possible, bands will be merged together to cover a greater vertical
91 * distance (and thus reduce the number of rectangles). Two bands can be merged
92 * only if the bottom of one touches the top of the other and they have
93 * rectangles in the same places (of the same width, of course). This maintains
94 * the y-x-banding that's so nice to have...
97 #include <stdlib.h>
98 #include <string.h>
99 #include "windef.h"
100 #include "wingdi.h"
101 #include "gdi.h"
102 #include "wine/debug.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(region);
106 typedef struct {
107 INT size;
108 INT numRects;
109 RECT *rects;
110 RECT extents;
111 } WINEREGION;
113 /* GDI logical region object */
114 typedef struct
116 GDIOBJHDR header;
117 WINEREGION *rgn;
118 } RGNOBJ;
121 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
122 static BOOL REGION_DeleteObject( HGDIOBJ handle, void *obj );
124 static const struct gdi_obj_funcs region_funcs =
126 REGION_SelectObject, /* pSelectObject */
127 NULL, /* pGetObject16 */
128 NULL, /* pGetObjectA */
129 NULL, /* pGetObjectW */
130 NULL, /* pUnrealizeObject */
131 REGION_DeleteObject /* pDeleteObject */
134 /* 1 if two RECTs overlap.
135 * 0 if two RECTs do not overlap.
137 #define EXTENTCHECK(r1, r2) \
138 ((r1)->right > (r2)->left && \
139 (r1)->left < (r2)->right && \
140 (r1)->bottom > (r2)->top && \
141 (r1)->top < (r2)->bottom)
144 * Check to see if there is enough memory in the present region.
147 static inline int xmemcheck(WINEREGION *reg, LPRECT *rect, LPRECT *firstrect ) {
148 if (reg->numRects >= (reg->size - 1)) {
149 *firstrect = HeapReAlloc( GetProcessHeap(), 0, *firstrect, (2 * (sizeof(RECT)) * (reg->size)));
150 if (*firstrect == 0)
151 return 0;
152 reg->size *= 2;
153 *rect = (*firstrect)+reg->numRects;
155 return 1;
158 #define MEMCHECK(reg, rect, firstrect) xmemcheck(reg,&(rect),&(firstrect))
160 #define EMPTY_REGION(pReg) { \
161 (pReg)->numRects = 0; \
162 (pReg)->extents.left = (pReg)->extents.top = 0; \
163 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
166 #define REGION_NOT_EMPTY(pReg) pReg->numRects
168 #define INRECT(r, x, y) \
169 ( ( ((r).right > x)) && \
170 ( ((r).left <= x)) && \
171 ( ((r).bottom > y)) && \
172 ( ((r).top <= y)) )
176 * number of points to buffer before sending them off
177 * to scanlines() : Must be an even number
179 #define NUMPTSTOBUFFER 200
182 * used to allocate buffers for points and link
183 * the buffers together
186 typedef struct _POINTBLOCK {
187 POINT pts[NUMPTSTOBUFFER];
188 struct _POINTBLOCK *next;
189 } POINTBLOCK;
194 * This file contains a few macros to help track
195 * the edge of a filled object. The object is assumed
196 * to be filled in scanline order, and thus the
197 * algorithm used is an extension of Bresenham's line
198 * drawing algorithm which assumes that y is always the
199 * major axis.
200 * Since these pieces of code are the same for any filled shape,
201 * it is more convenient to gather the library in one
202 * place, but since these pieces of code are also in
203 * the inner loops of output primitives, procedure call
204 * overhead is out of the question.
205 * See the author for a derivation if needed.
210 * In scan converting polygons, we want to choose those pixels
211 * which are inside the polygon. Thus, we add .5 to the starting
212 * x coordinate for both left and right edges. Now we choose the
213 * first pixel which is inside the pgon for the left edge and the
214 * first pixel which is outside the pgon for the right edge.
215 * Draw the left pixel, but not the right.
217 * How to add .5 to the starting x coordinate:
218 * If the edge is moving to the right, then subtract dy from the
219 * error term from the general form of the algorithm.
220 * If the edge is moving to the left, then add dy to the error term.
222 * The reason for the difference between edges moving to the left
223 * and edges moving to the right is simple: If an edge is moving
224 * to the right, then we want the algorithm to flip immediately.
225 * If it is moving to the left, then we don't want it to flip until
226 * we traverse an entire pixel.
228 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
229 int dx; /* local storage */ \
231 /* \
232 * if the edge is horizontal, then it is ignored \
233 * and assumed not to be processed. Otherwise, do this stuff. \
234 */ \
235 if ((dy) != 0) { \
236 xStart = (x1); \
237 dx = (x2) - xStart; \
238 if (dx < 0) { \
239 m = dx / (dy); \
240 m1 = m - 1; \
241 incr1 = -2 * dx + 2 * (dy) * m1; \
242 incr2 = -2 * dx + 2 * (dy) * m; \
243 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
244 } else { \
245 m = dx / (dy); \
246 m1 = m + 1; \
247 incr1 = 2 * dx - 2 * (dy) * m1; \
248 incr2 = 2 * dx - 2 * (dy) * m; \
249 d = -2 * m * (dy) + 2 * dx; \
254 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
255 if (m1 > 0) { \
256 if (d > 0) { \
257 minval += m1; \
258 d += incr1; \
260 else { \
261 minval += m; \
262 d += incr2; \
264 } else {\
265 if (d >= 0) { \
266 minval += m1; \
267 d += incr1; \
269 else { \
270 minval += m; \
271 d += incr2; \
277 * This structure contains all of the information needed
278 * to run the bresenham algorithm.
279 * The variables may be hardcoded into the declarations
280 * instead of using this structure to make use of
281 * register declarations.
283 typedef struct {
284 INT minor_axis; /* minor axis */
285 INT d; /* decision variable */
286 INT m, m1; /* slope and slope+1 */
287 INT incr1, incr2; /* error increments */
288 } BRESINFO;
291 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
292 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
293 bres.m, bres.m1, bres.incr1, bres.incr2)
295 #define BRESINCRPGONSTRUCT(bres) \
296 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
301 * These are the data structures needed to scan
302 * convert regions. Two different scan conversion
303 * methods are available -- the even-odd method, and
304 * the winding number method.
305 * The even-odd rule states that a point is inside
306 * the polygon if a ray drawn from that point in any
307 * direction will pass through an odd number of
308 * path segments.
309 * By the winding number rule, a point is decided
310 * to be inside the polygon if a ray drawn from that
311 * point in any direction passes through a different
312 * number of clockwise and counter-clockwise path
313 * segments.
315 * These data structures are adapted somewhat from
316 * the algorithm in (Foley/Van Dam) for scan converting
317 * polygons.
318 * The basic algorithm is to start at the top (smallest y)
319 * of the polygon, stepping down to the bottom of
320 * the polygon by incrementing the y coordinate. We
321 * keep a list of edges which the current scanline crosses,
322 * sorted by x. This list is called the Active Edge Table (AET)
323 * As we change the y-coordinate, we update each entry in
324 * in the active edge table to reflect the edges new xcoord.
325 * This list must be sorted at each scanline in case
326 * two edges intersect.
327 * We also keep a data structure known as the Edge Table (ET),
328 * which keeps track of all the edges which the current
329 * scanline has not yet reached. The ET is basically a
330 * list of ScanLineList structures containing a list of
331 * edges which are entered at a given scanline. There is one
332 * ScanLineList per scanline at which an edge is entered.
333 * When we enter a new edge, we move it from the ET to the AET.
335 * From the AET, we can implement the even-odd rule as in
336 * (Foley/Van Dam).
337 * The winding number rule is a little trickier. We also
338 * keep the EdgeTableEntries in the AET linked by the
339 * nextWETE (winding EdgeTableEntry) link. This allows
340 * the edges to be linked just as before for updating
341 * purposes, but only uses the edges linked by the nextWETE
342 * link as edges representing spans of the polygon to
343 * drawn (as with the even-odd rule).
347 * for the winding number rule
349 #define CLOCKWISE 1
350 #define COUNTERCLOCKWISE -1
352 typedef struct _EdgeTableEntry {
353 INT ymax; /* ycoord at which we exit this edge. */
354 BRESINFO bres; /* Bresenham info to run the edge */
355 struct _EdgeTableEntry *next; /* next in the list */
356 struct _EdgeTableEntry *back; /* for insertion sort */
357 struct _EdgeTableEntry *nextWETE; /* for winding num rule */
358 int ClockWise; /* flag for winding number rule */
359 } EdgeTableEntry;
362 typedef struct _ScanLineList{
363 INT scanline; /* the scanline represented */
364 EdgeTableEntry *edgelist; /* header node */
365 struct _ScanLineList *next; /* next in the list */
366 } ScanLineList;
369 typedef struct {
370 INT ymax; /* ymax for the polygon */
371 INT ymin; /* ymin for the polygon */
372 ScanLineList scanlines; /* header node */
373 } EdgeTable;
377 * Here is a struct to help with storage allocation
378 * so we can allocate a big chunk at a time, and then take
379 * pieces from this heap when we need to.
381 #define SLLSPERBLOCK 25
383 typedef struct _ScanLineListBlock {
384 ScanLineList SLLs[SLLSPERBLOCK];
385 struct _ScanLineListBlock *next;
386 } ScanLineListBlock;
391 * a few macros for the inner loops of the fill code where
392 * performance considerations don't allow a procedure call.
394 * Evaluate the given edge at the given scanline.
395 * If the edge has expired, then we leave it and fix up
396 * the active edge table; otherwise, we increment the
397 * x value to be ready for the next scanline.
398 * The winding number rule is in effect, so we must notify
399 * the caller when the edge has been removed so he
400 * can reorder the Winding Active Edge Table.
402 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
403 if (pAET->ymax == y) { /* leaving this edge */ \
404 pPrevAET->next = pAET->next; \
405 pAET = pPrevAET->next; \
406 fixWAET = 1; \
407 if (pAET) \
408 pAET->back = pPrevAET; \
410 else { \
411 BRESINCRPGONSTRUCT(pAET->bres); \
412 pPrevAET = pAET; \
413 pAET = pAET->next; \
419 * Evaluate the given edge at the given scanline.
420 * If the edge has expired, then we leave it and fix up
421 * the active edge table; otherwise, we increment the
422 * x value to be ready for the next scanline.
423 * The even-odd rule is in effect.
425 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
426 if (pAET->ymax == y) { /* leaving this edge */ \
427 pPrevAET->next = pAET->next; \
428 pAET = pPrevAET->next; \
429 if (pAET) \
430 pAET->back = pPrevAET; \
432 else { \
433 BRESINCRPGONSTRUCT(pAET->bres); \
434 pPrevAET = pAET; \
435 pAET = pAET->next; \
439 typedef void (*voidProcp)();
441 /* Note the parameter order is different from the X11 equivalents */
443 static void REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
444 static void REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
445 static void REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
446 static void REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
447 static void REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
448 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
450 #define RGN_DEFAULT_RECTS 2
453 /***********************************************************************
454 * get_region_type
456 inline static INT get_region_type( const RGNOBJ *obj )
458 switch(obj->rgn->numRects)
460 case 0: return NULLREGION;
461 case 1: return SIMPLEREGION;
462 default: return COMPLEXREGION;
467 /***********************************************************************
468 * REGION_DumpRegion
469 * Outputs the contents of a WINEREGION
471 static void REGION_DumpRegion(WINEREGION *pReg)
473 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
475 TRACE("Region %p: %ld,%ld - %ld,%ld %d rects\n", pReg,
476 pReg->extents.left, pReg->extents.top,
477 pReg->extents.right, pReg->extents.bottom, pReg->numRects);
478 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
479 TRACE("\t%ld,%ld - %ld,%ld\n", pRect->left, pRect->top,
480 pRect->right, pRect->bottom);
481 return;
485 /***********************************************************************
486 * REGION_AllocWineRegion
487 * Create a new empty WINEREGION.
489 static WINEREGION *REGION_AllocWineRegion( INT n )
491 WINEREGION *pReg;
493 if ((pReg = HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION ))))
495 if ((pReg->rects = HeapAlloc(GetProcessHeap(), 0, n * sizeof( RECT ))))
497 pReg->size = n;
498 EMPTY_REGION(pReg);
499 return pReg;
501 HeapFree(GetProcessHeap(), 0, pReg);
503 return NULL;
507 /***********************************************************************
508 * REGION_CreateRegion
509 * Create a new empty region.
511 static HRGN REGION_CreateRegion( INT n )
513 HRGN hrgn;
514 RGNOBJ *obj;
516 if(!(obj = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC, (HGDIOBJ *)&hrgn,
517 &region_funcs ))) return 0;
518 if(!(obj->rgn = REGION_AllocWineRegion(n))) {
519 GDI_FreeObject( hrgn, obj );
520 return 0;
522 GDI_ReleaseObj( hrgn );
523 return hrgn;
526 /***********************************************************************
527 * REGION_DestroyWineRegion
529 static void REGION_DestroyWineRegion( WINEREGION* pReg )
531 HeapFree( GetProcessHeap(), 0, pReg->rects );
532 HeapFree( GetProcessHeap(), 0, pReg );
535 /***********************************************************************
536 * REGION_DeleteObject
538 static BOOL REGION_DeleteObject( HGDIOBJ handle, void *obj )
540 RGNOBJ *rgn = obj;
542 TRACE(" %p\n", handle );
544 REGION_DestroyWineRegion( rgn->rgn );
545 return GDI_FreeObject( handle, obj );
548 /***********************************************************************
549 * REGION_SelectObject
551 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
553 return (HGDIOBJ)SelectClipRgn( hdc, handle );
557 /***********************************************************************
558 * OffsetRgn (GDI32.@)
560 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
562 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
563 INT ret;
565 TRACE("%p %d,%d\n", hrgn, x, y);
567 if (!obj)
568 return ERROR;
570 if(x || y) {
571 int nbox = obj->rgn->numRects;
572 RECT *pbox = obj->rgn->rects;
574 if(nbox) {
575 while(nbox--) {
576 pbox->left += x;
577 pbox->right += x;
578 pbox->top += y;
579 pbox->bottom += y;
580 pbox++;
582 obj->rgn->extents.left += x;
583 obj->rgn->extents.right += x;
584 obj->rgn->extents.top += y;
585 obj->rgn->extents.bottom += y;
588 ret = get_region_type( obj );
589 GDI_ReleaseObj( hrgn );
590 return ret;
594 /***********************************************************************
595 * GetRgnBox (GDI32.@)
597 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
599 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
600 if (obj)
602 INT ret;
603 rect->left = obj->rgn->extents.left;
604 rect->top = obj->rgn->extents.top;
605 rect->right = obj->rgn->extents.right;
606 rect->bottom = obj->rgn->extents.bottom;
607 TRACE("%p (%ld,%ld-%ld,%ld)\n", hrgn,
608 rect->left, rect->top, rect->right, rect->bottom);
609 ret = get_region_type( obj );
610 GDI_ReleaseObj(hrgn);
611 return ret;
613 return ERROR;
617 /***********************************************************************
618 * CreateRectRgn (GDI32.@)
620 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
622 HRGN hrgn;
624 /* Allocate 2 rects by default to reduce the number of reallocs */
626 if (!(hrgn = REGION_CreateRegion(RGN_DEFAULT_RECTS)))
627 return 0;
628 TRACE("%d,%d-%d,%d\n", left, top, right, bottom);
629 SetRectRgn(hrgn, left, top, right, bottom);
630 return hrgn;
634 /***********************************************************************
635 * CreateRectRgnIndirect (GDI32.@)
637 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
639 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
643 /***********************************************************************
644 * SetRectRgn (GDI32.@)
646 * Allows either or both left and top to be greater than right or bottom.
648 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
649 INT right, INT bottom )
651 RGNOBJ * obj;
653 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
655 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
657 if (left > right) { INT tmp = left; left = right; right = tmp; }
658 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
660 if((left != right) && (top != bottom))
662 obj->rgn->rects->left = obj->rgn->extents.left = left;
663 obj->rgn->rects->top = obj->rgn->extents.top = top;
664 obj->rgn->rects->right = obj->rgn->extents.right = right;
665 obj->rgn->rects->bottom = obj->rgn->extents.bottom = bottom;
666 obj->rgn->numRects = 1;
668 else
669 EMPTY_REGION(obj->rgn);
671 GDI_ReleaseObj( hrgn );
672 return TRUE;
676 /***********************************************************************
677 * CreateRoundRectRgn (GDI32.@)
679 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
680 INT right, INT bottom,
681 INT ellipse_width, INT ellipse_height )
683 RGNOBJ * obj;
684 HRGN hrgn;
685 int asq, bsq, d, xd, yd;
686 RECT rect;
688 /* Make the dimensions sensible */
690 if (left > right) { INT tmp = left; left = right; right = tmp; }
691 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
693 ellipse_width = abs(ellipse_width);
694 ellipse_height = abs(ellipse_height);
696 /* Check parameters */
698 if (ellipse_width > right-left) ellipse_width = right-left;
699 if (ellipse_height > bottom-top) ellipse_height = bottom-top;
701 /* Check if we can do a normal rectangle instead */
703 if ((ellipse_width < 2) || (ellipse_height < 2))
704 return CreateRectRgn( left, top, right, bottom );
706 /* Create region */
708 d = (ellipse_height < 128) ? ((3 * ellipse_height) >> 2) : 64;
709 if (!(hrgn = REGION_CreateRegion(d))) return 0;
710 if (!(obj = GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return 0;
711 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
712 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
714 /* Ellipse algorithm, based on an article by K. Porter */
715 /* in DDJ Graphics Programming Column, 8/89 */
717 asq = ellipse_width * ellipse_width / 4; /* a^2 */
718 bsq = ellipse_height * ellipse_height / 4; /* b^2 */
719 d = bsq - asq * ellipse_height / 2 + asq / 4; /* b^2 - a^2b + a^2/4 */
720 xd = 0;
721 yd = asq * ellipse_height; /* 2a^2b */
723 rect.left = left + ellipse_width / 2;
724 rect.right = right - ellipse_width / 2;
726 /* Loop to draw first half of quadrant */
728 while (xd < yd)
730 if (d > 0) /* if nearest pixel is toward the center */
732 /* move toward center */
733 rect.top = top++;
734 rect.bottom = rect.top + 1;
735 REGION_UnionRectWithRegion( &rect, obj->rgn );
736 rect.top = --bottom;
737 rect.bottom = rect.top + 1;
738 REGION_UnionRectWithRegion( &rect, obj->rgn );
739 yd -= 2*asq;
740 d -= yd;
742 rect.left--; /* next horiz point */
743 rect.right++;
744 xd += 2*bsq;
745 d += bsq + xd;
748 /* Loop to draw second half of quadrant */
750 d += (3 * (asq-bsq) / 2 - (xd+yd)) / 2;
751 while (yd >= 0)
753 /* next vertical point */
754 rect.top = top++;
755 rect.bottom = rect.top + 1;
756 REGION_UnionRectWithRegion( &rect, obj->rgn );
757 rect.top = --bottom;
758 rect.bottom = rect.top + 1;
759 REGION_UnionRectWithRegion( &rect, obj->rgn );
760 if (d < 0) /* if nearest pixel is outside ellipse */
762 rect.left--; /* move away from center */
763 rect.right++;
764 xd += 2*bsq;
765 d += xd;
767 yd -= 2*asq;
768 d += asq - yd;
771 /* Add the inside rectangle */
773 if (top <= bottom)
775 rect.top = top;
776 rect.bottom = bottom;
777 REGION_UnionRectWithRegion( &rect, obj->rgn );
779 GDI_ReleaseObj( hrgn );
780 return hrgn;
784 /***********************************************************************
785 * CreateEllipticRgn (GDI32.@)
787 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
788 INT right, INT bottom )
790 return CreateRoundRectRgn( left, top, right, bottom,
791 right-left, bottom-top );
795 /***********************************************************************
796 * CreateEllipticRgnIndirect (GDI32.@)
798 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
800 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
801 rect->bottom, rect->right - rect->left,
802 rect->bottom - rect->top );
805 /***********************************************************************
806 * GetRegionData (GDI32.@)
808 * MSDN: GetRegionData, Return Values:
810 * "If the function succeeds and dwCount specifies an adequate number of bytes,
811 * the return value is always dwCount. If dwCount is too small or the function
812 * fails, the return value is 0. If lpRgnData is NULL, the return value is the
813 * required number of bytes.
815 * If the function fails, the return value is zero."
817 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
819 DWORD size;
820 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
822 TRACE(" %p count = %ld, rgndata = %p\n", hrgn, count, rgndata);
824 if(!obj) return 0;
826 size = obj->rgn->numRects * sizeof(RECT);
827 if(count < (size + sizeof(RGNDATAHEADER)) || rgndata == NULL)
829 GDI_ReleaseObj( hrgn );
830 if (rgndata) /* buffer is too small, signal it by return 0 */
831 return 0;
832 else /* user requested buffer size with rgndata NULL */
833 return size + sizeof(RGNDATAHEADER);
836 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
837 rgndata->rdh.iType = RDH_RECTANGLES;
838 rgndata->rdh.nCount = obj->rgn->numRects;
839 rgndata->rdh.nRgnSize = size;
840 rgndata->rdh.rcBound.left = obj->rgn->extents.left;
841 rgndata->rdh.rcBound.top = obj->rgn->extents.top;
842 rgndata->rdh.rcBound.right = obj->rgn->extents.right;
843 rgndata->rdh.rcBound.bottom = obj->rgn->extents.bottom;
845 memcpy( rgndata->Buffer, obj->rgn->rects, size );
847 GDI_ReleaseObj( hrgn );
848 return size + sizeof(RGNDATAHEADER);
852 /***********************************************************************
853 * ExtCreateRegion (GDI32.@)
856 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
858 HRGN hrgn;
860 TRACE(" %p %ld %p = ", lpXform, dwCount, rgndata );
862 if( lpXform )
863 WARN("(Xform not implemented - ignored)\n");
865 if( rgndata->rdh.iType != RDH_RECTANGLES )
867 /* FIXME: We can use CreatePolyPolygonRgn() here
868 * for trapezoidal data */
870 WARN("(Unsupported region data)\n");
871 goto fail;
874 if( (hrgn = REGION_CreateRegion( rgndata->rdh.nCount )) )
876 RECT *pCurRect, *pEndRect;
877 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
879 if (obj) {
880 pEndRect = (RECT *)rgndata->Buffer + rgndata->rdh.nCount;
881 for(pCurRect = (RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
882 REGION_UnionRectWithRegion( pCurRect, obj->rgn );
883 GDI_ReleaseObj( hrgn );
885 TRACE("%p\n", hrgn );
886 return hrgn;
888 else ERR("Could not get pointer to newborn Region!\n");
890 fail:
891 WARN("Failed\n");
892 return 0;
896 /***********************************************************************
897 * PtInRegion (GDI32.@)
899 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
901 RGNOBJ * obj;
902 BOOL ret = FALSE;
904 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
906 int i;
908 if (obj->rgn->numRects > 0 && INRECT(obj->rgn->extents, x, y))
909 for (i = 0; i < obj->rgn->numRects; i++)
910 if (INRECT (obj->rgn->rects[i], x, y))
912 ret = TRUE;
913 break;
915 GDI_ReleaseObj( hrgn );
917 return ret;
921 /***********************************************************************
922 * RectInRegion (GDI32.@)
924 * Returns TRUE if rect is at least partly inside hrgn
926 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
928 RGNOBJ * obj;
929 BOOL ret = FALSE;
931 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
933 RECT *pCurRect, *pRectEnd;
935 /* this is (just) a useful optimization */
936 if ((obj->rgn->numRects > 0) && EXTENTCHECK(&obj->rgn->extents,
937 rect))
939 for (pCurRect = obj->rgn->rects, pRectEnd = pCurRect +
940 obj->rgn->numRects; pCurRect < pRectEnd; pCurRect++)
942 if (pCurRect->bottom <= rect->top)
943 continue; /* not far enough down yet */
945 if (pCurRect->top >= rect->bottom)
946 break; /* too far down */
948 if (pCurRect->right <= rect->left)
949 continue; /* not far enough over yet */
951 if (pCurRect->left >= rect->right) {
952 continue;
955 ret = TRUE;
956 break;
959 GDI_ReleaseObj(hrgn);
961 return ret;
964 /***********************************************************************
965 * EqualRgn (GDI32.@)
967 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
969 RGNOBJ *obj1, *obj2;
970 BOOL ret = FALSE;
972 if ((obj1 = (RGNOBJ *) GDI_GetObjPtr( hrgn1, REGION_MAGIC )))
974 if ((obj2 = (RGNOBJ *) GDI_GetObjPtr( hrgn2, REGION_MAGIC )))
976 int i;
978 if ( obj1->rgn->numRects != obj2->rgn->numRects ) goto done;
979 if ( obj1->rgn->numRects == 0 )
981 ret = TRUE;
982 goto done;
985 if (obj1->rgn->extents.left != obj2->rgn->extents.left) goto done;
986 if (obj1->rgn->extents.right != obj2->rgn->extents.right) goto done;
987 if (obj1->rgn->extents.top != obj2->rgn->extents.top) goto done;
988 if (obj1->rgn->extents.bottom != obj2->rgn->extents.bottom) goto done;
989 for( i = 0; i < obj1->rgn->numRects; i++ )
991 if (obj1->rgn->rects[i].left != obj2->rgn->rects[i].left) goto done;
992 if (obj1->rgn->rects[i].right != obj2->rgn->rects[i].right) goto done;
993 if (obj1->rgn->rects[i].top != obj2->rgn->rects[i].top) goto done;
994 if (obj1->rgn->rects[i].bottom != obj2->rgn->rects[i].bottom) goto done;
996 ret = TRUE;
997 done:
998 GDI_ReleaseObj(hrgn2);
1000 GDI_ReleaseObj(hrgn1);
1002 return ret;
1005 /***********************************************************************
1006 * REGION_UnionRectWithRegion
1007 * Adds a rectangle to a WINEREGION
1009 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1011 WINEREGION region;
1013 region.rects = &region.extents;
1014 region.numRects = 1;
1015 region.size = 1;
1016 region.extents = *rect;
1017 REGION_UnionRegion(rgn, rgn, &region);
1021 /***********************************************************************
1022 * REGION_CreateFrameRgn
1024 * Create a region that is a frame around another region.
1025 * Expand all rectangles by +/- x and y, then subtract original region.
1027 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1029 BOOL bRet;
1030 RGNOBJ *srcObj = (RGNOBJ*) GDI_GetObjPtr( hSrc, REGION_MAGIC );
1032 if (!srcObj) return FALSE;
1033 if (srcObj->rgn->numRects != 0)
1035 RGNOBJ* destObj = (RGNOBJ*) GDI_GetObjPtr( hDest, REGION_MAGIC );
1036 RECT *pRect, *pEndRect;
1037 RECT tempRect;
1039 EMPTY_REGION( destObj->rgn );
1041 pEndRect = srcObj->rgn->rects + srcObj->rgn->numRects;
1042 for(pRect = srcObj->rgn->rects; pRect < pEndRect; pRect++)
1044 tempRect.left = pRect->left - x;
1045 tempRect.top = pRect->top - y;
1046 tempRect.right = pRect->right + x;
1047 tempRect.bottom = pRect->bottom + y;
1048 REGION_UnionRectWithRegion( &tempRect, destObj->rgn );
1050 REGION_SubtractRegion( destObj->rgn, destObj->rgn, srcObj->rgn );
1051 GDI_ReleaseObj ( hDest );
1052 bRet = TRUE;
1054 else
1055 bRet = FALSE;
1056 GDI_ReleaseObj( hSrc );
1057 return bRet;
1061 /***********************************************************************
1062 * CombineRgn (GDI32.@)
1064 * Note: The behavior is correct even if src and dest regions are the same.
1066 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1068 RGNOBJ *destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC);
1069 INT result = ERROR;
1071 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1072 if (destObj)
1074 RGNOBJ *src1Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc1, REGION_MAGIC);
1076 if (src1Obj)
1078 TRACE("dump src1Obj:\n");
1079 if(TRACE_ON(region))
1080 REGION_DumpRegion(src1Obj->rgn);
1081 if (mode == RGN_COPY)
1083 REGION_CopyRegion( destObj->rgn, src1Obj->rgn );
1084 result = get_region_type( destObj );
1086 else
1088 RGNOBJ *src2Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc2, REGION_MAGIC);
1090 if (src2Obj)
1092 TRACE("dump src2Obj:\n");
1093 if(TRACE_ON(region))
1094 REGION_DumpRegion(src2Obj->rgn);
1095 switch (mode)
1097 case RGN_AND:
1098 REGION_IntersectRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn);
1099 break;
1100 case RGN_OR:
1101 REGION_UnionRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1102 break;
1103 case RGN_XOR:
1104 REGION_XorRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1105 break;
1106 case RGN_DIFF:
1107 REGION_SubtractRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1108 break;
1110 result = get_region_type( destObj );
1111 GDI_ReleaseObj( hSrc2 );
1114 GDI_ReleaseObj( hSrc1 );
1116 TRACE("dump destObj:\n");
1117 if(TRACE_ON(region))
1118 REGION_DumpRegion(destObj->rgn);
1120 GDI_ReleaseObj( hDest );
1121 } else {
1122 ERR("Invalid rgn=%p\n", hDest);
1124 return result;
1127 /***********************************************************************
1128 * REGION_SetExtents
1129 * Re-calculate the extents of a region
1131 static void REGION_SetExtents (WINEREGION *pReg)
1133 RECT *pRect, *pRectEnd, *pExtents;
1135 if (pReg->numRects == 0)
1137 pReg->extents.left = 0;
1138 pReg->extents.top = 0;
1139 pReg->extents.right = 0;
1140 pReg->extents.bottom = 0;
1141 return;
1144 pExtents = &pReg->extents;
1145 pRect = pReg->rects;
1146 pRectEnd = &pRect[pReg->numRects - 1];
1149 * Since pRect is the first rectangle in the region, it must have the
1150 * smallest top and since pRectEnd is the last rectangle in the region,
1151 * it must have the largest bottom, because of banding. Initialize left and
1152 * right from pRect and pRectEnd, resp., as good things to initialize them
1153 * to...
1155 pExtents->left = pRect->left;
1156 pExtents->top = pRect->top;
1157 pExtents->right = pRectEnd->right;
1158 pExtents->bottom = pRectEnd->bottom;
1160 while (pRect <= pRectEnd)
1162 if (pRect->left < pExtents->left)
1163 pExtents->left = pRect->left;
1164 if (pRect->right > pExtents->right)
1165 pExtents->right = pRect->right;
1166 pRect++;
1170 /***********************************************************************
1171 * REGION_CopyRegion
1173 static void REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1175 if (dst != src) /* don't want to copy to itself */
1177 if (dst->size < src->numRects)
1179 if (! (dst->rects = HeapReAlloc( GetProcessHeap(), 0, dst->rects,
1180 src->numRects * sizeof(RECT) )))
1181 return;
1182 dst->size = src->numRects;
1184 dst->numRects = src->numRects;
1185 dst->extents.left = src->extents.left;
1186 dst->extents.top = src->extents.top;
1187 dst->extents.right = src->extents.right;
1188 dst->extents.bottom = src->extents.bottom;
1189 memcpy((char *) dst->rects, (char *) src->rects,
1190 (int) (src->numRects * sizeof(RECT)));
1192 return;
1195 /***********************************************************************
1196 * REGION_Coalesce
1198 * Attempt to merge the rects in the current band with those in the
1199 * previous one. Used only by REGION_RegionOp.
1201 * Results:
1202 * The new index for the previous band.
1204 * Side Effects:
1205 * If coalescing takes place:
1206 * - rectangles in the previous band will have their bottom fields
1207 * altered.
1208 * - pReg->numRects will be decreased.
1211 static INT REGION_Coalesce (
1212 WINEREGION *pReg, /* Region to coalesce */
1213 INT prevStart, /* Index of start of previous band */
1214 INT curStart /* Index of start of current band */
1216 RECT *pPrevRect; /* Current rect in previous band */
1217 RECT *pCurRect; /* Current rect in current band */
1218 RECT *pRegEnd; /* End of region */
1219 INT curNumRects; /* Number of rectangles in current band */
1220 INT prevNumRects; /* Number of rectangles in previous band */
1221 INT bandtop; /* top coordinate for current band */
1223 pRegEnd = &pReg->rects[pReg->numRects];
1225 pPrevRect = &pReg->rects[prevStart];
1226 prevNumRects = curStart - prevStart;
1229 * Figure out how many rectangles are in the current band. Have to do
1230 * this because multiple bands could have been added in REGION_RegionOp
1231 * at the end when one region has been exhausted.
1233 pCurRect = &pReg->rects[curStart];
1234 bandtop = pCurRect->top;
1235 for (curNumRects = 0;
1236 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1237 curNumRects++)
1239 pCurRect++;
1242 if (pCurRect != pRegEnd)
1245 * If more than one band was added, we have to find the start
1246 * of the last band added so the next coalescing job can start
1247 * at the right place... (given when multiple bands are added,
1248 * this may be pointless -- see above).
1250 pRegEnd--;
1251 while (pRegEnd[-1].top == pRegEnd->top)
1253 pRegEnd--;
1255 curStart = pRegEnd - pReg->rects;
1256 pRegEnd = pReg->rects + pReg->numRects;
1259 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1260 pCurRect -= curNumRects;
1262 * The bands may only be coalesced if the bottom of the previous
1263 * matches the top scanline of the current.
1265 if (pPrevRect->bottom == pCurRect->top)
1268 * Make sure the bands have rects in the same places. This
1269 * assumes that rects have been added in such a way that they
1270 * cover the most area possible. I.e. two rects in a band must
1271 * have some horizontal space between them.
1275 if ((pPrevRect->left != pCurRect->left) ||
1276 (pPrevRect->right != pCurRect->right))
1279 * The bands don't line up so they can't be coalesced.
1281 return (curStart);
1283 pPrevRect++;
1284 pCurRect++;
1285 prevNumRects -= 1;
1286 } while (prevNumRects != 0);
1288 pReg->numRects -= curNumRects;
1289 pCurRect -= curNumRects;
1290 pPrevRect -= curNumRects;
1293 * The bands may be merged, so set the bottom of each rect
1294 * in the previous band to that of the corresponding rect in
1295 * the current band.
1299 pPrevRect->bottom = pCurRect->bottom;
1300 pPrevRect++;
1301 pCurRect++;
1302 curNumRects -= 1;
1303 } while (curNumRects != 0);
1306 * If only one band was added to the region, we have to backup
1307 * curStart to the start of the previous band.
1309 * If more than one band was added to the region, copy the
1310 * other bands down. The assumption here is that the other bands
1311 * came from the same region as the current one and no further
1312 * coalescing can be done on them since it's all been done
1313 * already... curStart is already in the right place.
1315 if (pCurRect == pRegEnd)
1317 curStart = prevStart;
1319 else
1323 *pPrevRect++ = *pCurRect++;
1324 } while (pCurRect != pRegEnd);
1329 return (curStart);
1332 /***********************************************************************
1333 * REGION_RegionOp
1335 * Apply an operation to two regions. Called by REGION_Union,
1336 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1338 * Results:
1339 * None.
1341 * Side Effects:
1342 * The new region is overwritten.
1344 * Notes:
1345 * The idea behind this function is to view the two regions as sets.
1346 * Together they cover a rectangle of area that this function divides
1347 * into horizontal bands where points are covered only by one region
1348 * or by both. For the first case, the nonOverlapFunc is called with
1349 * each the band and the band's upper and lower extents. For the
1350 * second, the overlapFunc is called to process the entire band. It
1351 * is responsible for clipping the rectangles in the band, though
1352 * this function provides the boundaries.
1353 * At the end of each band, the new region is coalesced, if possible,
1354 * to reduce the number of rectangles in the region.
1357 static void REGION_RegionOp(
1358 WINEREGION *newReg, /* Place to store result */
1359 WINEREGION *reg1, /* First region in operation */
1360 WINEREGION *reg2, /* 2nd region in operation */
1361 void (*overlapFunc)(), /* Function to call for over-lapping bands */
1362 void (*nonOverlap1Func)(), /* Function to call for non-overlapping bands in region 1 */
1363 void (*nonOverlap2Func)() /* Function to call for non-overlapping bands in region 2 */
1365 RECT *r1; /* Pointer into first region */
1366 RECT *r2; /* Pointer into 2d region */
1367 RECT *r1End; /* End of 1st region */
1368 RECT *r2End; /* End of 2d region */
1369 INT ybot; /* Bottom of intersection */
1370 INT ytop; /* Top of intersection */
1371 RECT *oldRects; /* Old rects for newReg */
1372 INT prevBand; /* Index of start of
1373 * previous band in newReg */
1374 INT curBand; /* Index of start of current
1375 * band in newReg */
1376 RECT *r1BandEnd; /* End of current band in r1 */
1377 RECT *r2BandEnd; /* End of current band in r2 */
1378 INT top; /* Top of non-overlapping band */
1379 INT bot; /* Bottom of non-overlapping band */
1382 * Initialization:
1383 * set r1, r2, r1End and r2End appropriately, preserve the important
1384 * parts of the destination region until the end in case it's one of
1385 * the two source regions, then mark the "new" region empty, allocating
1386 * another array of rectangles for it to use.
1388 r1 = reg1->rects;
1389 r2 = reg2->rects;
1390 r1End = r1 + reg1->numRects;
1391 r2End = r2 + reg2->numRects;
1395 * newReg may be one of the src regions so we can't empty it. We keep a
1396 * note of its rects pointer (so that we can free them later), preserve its
1397 * extents and simply set numRects to zero.
1400 oldRects = newReg->rects;
1401 newReg->numRects = 0;
1404 * Allocate a reasonable number of rectangles for the new region. The idea
1405 * is to allocate enough so the individual functions don't need to
1406 * reallocate and copy the array, which is time consuming, yet we don't
1407 * have to worry about using too much memory. I hope to be able to
1408 * nuke the Xrealloc() at the end of this function eventually.
1410 newReg->size = max(reg1->numRects,reg2->numRects) * 2;
1412 if (! (newReg->rects = HeapAlloc( GetProcessHeap(), 0,
1413 sizeof(RECT) * newReg->size )))
1415 newReg->size = 0;
1416 return;
1420 * Initialize ybot and ytop.
1421 * In the upcoming loop, ybot and ytop serve different functions depending
1422 * on whether the band being handled is an overlapping or non-overlapping
1423 * band.
1424 * In the case of a non-overlapping band (only one of the regions
1425 * has points in the band), ybot is the bottom of the most recent
1426 * intersection and thus clips the top of the rectangles in that band.
1427 * ytop is the top of the next intersection between the two regions and
1428 * serves to clip the bottom of the rectangles in the current band.
1429 * For an overlapping band (where the two regions intersect), ytop clips
1430 * the top of the rectangles of both regions and ybot clips the bottoms.
1432 if (reg1->extents.top < reg2->extents.top)
1433 ybot = reg1->extents.top;
1434 else
1435 ybot = reg2->extents.top;
1438 * prevBand serves to mark the start of the previous band so rectangles
1439 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1440 * In the beginning, there is no previous band, so prevBand == curBand
1441 * (curBand is set later on, of course, but the first band will always
1442 * start at index 0). prevBand and curBand must be indices because of
1443 * the possible expansion, and resultant moving, of the new region's
1444 * array of rectangles.
1446 prevBand = 0;
1450 curBand = newReg->numRects;
1453 * This algorithm proceeds one source-band (as opposed to a
1454 * destination band, which is determined by where the two regions
1455 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1456 * rectangle after the last one in the current band for their
1457 * respective regions.
1459 r1BandEnd = r1;
1460 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1462 r1BandEnd++;
1465 r2BandEnd = r2;
1466 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1468 r2BandEnd++;
1472 * First handle the band that doesn't intersect, if any.
1474 * Note that attention is restricted to one band in the
1475 * non-intersecting region at once, so if a region has n
1476 * bands between the current position and the next place it overlaps
1477 * the other, this entire loop will be passed through n times.
1479 if (r1->top < r2->top)
1481 top = max(r1->top,ybot);
1482 bot = min(r1->bottom,r2->top);
1484 if ((top != bot) && (nonOverlap1Func != (void (*)())NULL))
1486 (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot);
1489 ytop = r2->top;
1491 else if (r2->top < r1->top)
1493 top = max(r2->top,ybot);
1494 bot = min(r2->bottom,r1->top);
1496 if ((top != bot) && (nonOverlap2Func != (void (*)())NULL))
1498 (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot);
1501 ytop = r1->top;
1503 else
1505 ytop = r1->top;
1509 * If any rectangles got added to the region, try and coalesce them
1510 * with rectangles from the previous band. Note we could just do
1511 * this test in miCoalesce, but some machines incur a not
1512 * inconsiderable cost for function calls, so...
1514 if (newReg->numRects != curBand)
1516 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1520 * Now see if we've hit an intersecting band. The two bands only
1521 * intersect if ybot > ytop
1523 ybot = min(r1->bottom, r2->bottom);
1524 curBand = newReg->numRects;
1525 if (ybot > ytop)
1527 (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);
1531 if (newReg->numRects != curBand)
1533 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1537 * If we've finished with a band (bottom == ybot) we skip forward
1538 * in the region to the next band.
1540 if (r1->bottom == ybot)
1542 r1 = r1BandEnd;
1544 if (r2->bottom == ybot)
1546 r2 = r2BandEnd;
1548 } while ((r1 != r1End) && (r2 != r2End));
1551 * Deal with whichever region still has rectangles left.
1553 curBand = newReg->numRects;
1554 if (r1 != r1End)
1556 if (nonOverlap1Func != (void (*)())NULL)
1560 r1BandEnd = r1;
1561 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1563 r1BandEnd++;
1565 (* nonOverlap1Func) (newReg, r1, r1BandEnd,
1566 max(r1->top,ybot), r1->bottom);
1567 r1 = r1BandEnd;
1568 } while (r1 != r1End);
1571 else if ((r2 != r2End) && (nonOverlap2Func != (void (*)())NULL))
1575 r2BandEnd = r2;
1576 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1578 r2BandEnd++;
1580 (* nonOverlap2Func) (newReg, r2, r2BandEnd,
1581 max(r2->top,ybot), r2->bottom);
1582 r2 = r2BandEnd;
1583 } while (r2 != r2End);
1586 if (newReg->numRects != curBand)
1588 (void) REGION_Coalesce (newReg, prevBand, curBand);
1592 * A bit of cleanup. To keep regions from growing without bound,
1593 * we shrink the array of rectangles to match the new number of
1594 * rectangles in the region. This never goes to 0, however...
1596 * Only do this stuff if the number of rectangles allocated is more than
1597 * twice the number of rectangles in the region (a simple optimization...).
1599 if ((newReg->numRects < (newReg->size >> 1)) && (newReg->numRects > 2))
1601 if (REGION_NOT_EMPTY(newReg))
1603 RECT *prev_rects = newReg->rects;
1604 newReg->size = newReg->numRects;
1605 newReg->rects = HeapReAlloc( GetProcessHeap(), 0, newReg->rects,
1606 sizeof(RECT) * newReg->size );
1607 if (! newReg->rects)
1608 newReg->rects = prev_rects;
1610 else
1613 * No point in doing the extra work involved in an Xrealloc if
1614 * the region is empty
1616 newReg->size = 1;
1617 HeapFree( GetProcessHeap(), 0, newReg->rects );
1618 newReg->rects = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT) );
1621 HeapFree( GetProcessHeap(), 0, oldRects );
1622 return;
1625 /***********************************************************************
1626 * Region Intersection
1627 ***********************************************************************/
1630 /***********************************************************************
1631 * REGION_IntersectO
1633 * Handle an overlapping band for REGION_Intersect.
1635 * Results:
1636 * None.
1638 * Side Effects:
1639 * Rectangles may be added to the region.
1642 static void REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1643 RECT *r2, RECT *r2End, INT top, INT bottom)
1646 INT left, right;
1647 RECT *pNextRect;
1649 pNextRect = &pReg->rects[pReg->numRects];
1651 while ((r1 != r1End) && (r2 != r2End))
1653 left = max(r1->left, r2->left);
1654 right = min(r1->right, r2->right);
1657 * If there's any overlap between the two rectangles, add that
1658 * overlap to the new region.
1659 * There's no need to check for subsumption because the only way
1660 * such a need could arise is if some region has two rectangles
1661 * right next to each other. Since that should never happen...
1663 if (left < right)
1665 MEMCHECK(pReg, pNextRect, pReg->rects);
1666 pNextRect->left = left;
1667 pNextRect->top = top;
1668 pNextRect->right = right;
1669 pNextRect->bottom = bottom;
1670 pReg->numRects += 1;
1671 pNextRect++;
1675 * Need to advance the pointers. Shift the one that extends
1676 * to the right the least, since the other still has a chance to
1677 * overlap with that region's next rectangle, if you see what I mean.
1679 if (r1->right < r2->right)
1681 r1++;
1683 else if (r2->right < r1->right)
1685 r2++;
1687 else
1689 r1++;
1690 r2++;
1693 return;
1696 /***********************************************************************
1697 * REGION_IntersectRegion
1699 static void REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1700 WINEREGION *reg2)
1702 /* check for trivial reject */
1703 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1704 (!EXTENTCHECK(&reg1->extents, &reg2->extents)))
1705 newReg->numRects = 0;
1706 else
1707 REGION_RegionOp (newReg, reg1, reg2,
1708 (voidProcp) REGION_IntersectO, (voidProcp) NULL, (voidProcp) NULL);
1711 * Can't alter newReg's extents before we call miRegionOp because
1712 * it might be one of the source regions and miRegionOp depends
1713 * on the extents of those regions being the same. Besides, this
1714 * way there's no checking against rectangles that will be nuked
1715 * due to coalescing, so we have to examine fewer rectangles.
1717 REGION_SetExtents(newReg);
1720 /***********************************************************************
1721 * Region Union
1722 ***********************************************************************/
1724 /***********************************************************************
1725 * REGION_UnionNonO
1727 * Handle a non-overlapping band for the union operation. Just
1728 * Adds the rectangles into the region. Doesn't have to check for
1729 * subsumption or anything.
1731 * Results:
1732 * None.
1734 * Side Effects:
1735 * pReg->numRects is incremented and the final rectangles overwritten
1736 * with the rectangles we're passed.
1739 static void REGION_UnionNonO (WINEREGION *pReg, RECT *r, RECT *rEnd,
1740 INT top, INT bottom)
1742 RECT *pNextRect;
1744 pNextRect = &pReg->rects[pReg->numRects];
1746 while (r != rEnd)
1748 MEMCHECK(pReg, pNextRect, pReg->rects);
1749 pNextRect->left = r->left;
1750 pNextRect->top = top;
1751 pNextRect->right = r->right;
1752 pNextRect->bottom = bottom;
1753 pReg->numRects += 1;
1754 pNextRect++;
1755 r++;
1757 return;
1760 /***********************************************************************
1761 * REGION_UnionO
1763 * Handle an overlapping band for the union operation. Picks the
1764 * left-most rectangle each time and merges it into the region.
1766 * Results:
1767 * None.
1769 * Side Effects:
1770 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1771 * be changed.
1774 static void REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1775 RECT *r2, RECT *r2End, INT top, INT bottom)
1777 RECT *pNextRect;
1779 pNextRect = &pReg->rects[pReg->numRects];
1781 #define MERGERECT(r) \
1782 if ((pReg->numRects != 0) && \
1783 (pNextRect[-1].top == top) && \
1784 (pNextRect[-1].bottom == bottom) && \
1785 (pNextRect[-1].right >= r->left)) \
1787 if (pNextRect[-1].right < r->right) \
1789 pNextRect[-1].right = r->right; \
1792 else \
1794 MEMCHECK(pReg, pNextRect, pReg->rects); \
1795 pNextRect->top = top; \
1796 pNextRect->bottom = bottom; \
1797 pNextRect->left = r->left; \
1798 pNextRect->right = r->right; \
1799 pReg->numRects += 1; \
1800 pNextRect += 1; \
1802 r++;
1804 while ((r1 != r1End) && (r2 != r2End))
1806 if (r1->left < r2->left)
1808 MERGERECT(r1);
1810 else
1812 MERGERECT(r2);
1816 if (r1 != r1End)
1820 MERGERECT(r1);
1821 } while (r1 != r1End);
1823 else while (r2 != r2End)
1825 MERGERECT(r2);
1827 return;
1830 /***********************************************************************
1831 * REGION_UnionRegion
1833 static void REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1,
1834 WINEREGION *reg2)
1836 /* checks all the simple cases */
1839 * Region 1 and 2 are the same or region 1 is empty
1841 if ( (reg1 == reg2) || (!(reg1->numRects)) )
1843 if (newReg != reg2)
1844 REGION_CopyRegion(newReg, reg2);
1845 return;
1849 * if nothing to union (region 2 empty)
1851 if (!(reg2->numRects))
1853 if (newReg != reg1)
1854 REGION_CopyRegion(newReg, reg1);
1855 return;
1859 * Region 1 completely subsumes region 2
1861 if ((reg1->numRects == 1) &&
1862 (reg1->extents.left <= reg2->extents.left) &&
1863 (reg1->extents.top <= reg2->extents.top) &&
1864 (reg1->extents.right >= reg2->extents.right) &&
1865 (reg1->extents.bottom >= reg2->extents.bottom))
1867 if (newReg != reg1)
1868 REGION_CopyRegion(newReg, reg1);
1869 return;
1873 * Region 2 completely subsumes region 1
1875 if ((reg2->numRects == 1) &&
1876 (reg2->extents.left <= reg1->extents.left) &&
1877 (reg2->extents.top <= reg1->extents.top) &&
1878 (reg2->extents.right >= reg1->extents.right) &&
1879 (reg2->extents.bottom >= reg1->extents.bottom))
1881 if (newReg != reg2)
1882 REGION_CopyRegion(newReg, reg2);
1883 return;
1886 REGION_RegionOp (newReg, reg1, reg2, (voidProcp) REGION_UnionO,
1887 (voidProcp) REGION_UnionNonO, (voidProcp) REGION_UnionNonO);
1889 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
1890 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
1891 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
1892 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
1895 /***********************************************************************
1896 * Region Subtraction
1897 ***********************************************************************/
1899 /***********************************************************************
1900 * REGION_SubtractNonO1
1902 * Deal with non-overlapping band for subtraction. Any parts from
1903 * region 2 we discard. Anything from region 1 we add to the region.
1905 * Results:
1906 * None.
1908 * Side Effects:
1909 * pReg may be affected.
1912 static void REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd,
1913 INT top, INT bottom)
1915 RECT *pNextRect;
1917 pNextRect = &pReg->rects[pReg->numRects];
1919 while (r != rEnd)
1921 MEMCHECK(pReg, pNextRect, pReg->rects);
1922 pNextRect->left = r->left;
1923 pNextRect->top = top;
1924 pNextRect->right = r->right;
1925 pNextRect->bottom = bottom;
1926 pReg->numRects += 1;
1927 pNextRect++;
1928 r++;
1930 return;
1934 /***********************************************************************
1935 * REGION_SubtractO
1937 * Overlapping band subtraction. x1 is the left-most point not yet
1938 * checked.
1940 * Results:
1941 * None.
1943 * Side Effects:
1944 * pReg may have rectangles added to it.
1947 static void REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1948 RECT *r2, RECT *r2End, INT top, INT bottom)
1950 RECT *pNextRect;
1951 INT left;
1953 left = r1->left;
1954 pNextRect = &pReg->rects[pReg->numRects];
1956 while ((r1 != r1End) && (r2 != r2End))
1958 if (r2->right <= left)
1961 * Subtrahend missed the boat: go to next subtrahend.
1963 r2++;
1965 else if (r2->left <= left)
1968 * Subtrahend preceeds minuend: nuke left edge of minuend.
1970 left = r2->right;
1971 if (left >= r1->right)
1974 * Minuend completely covered: advance to next minuend and
1975 * reset left fence to edge of new minuend.
1977 r1++;
1978 if (r1 != r1End)
1979 left = r1->left;
1981 else
1984 * Subtrahend now used up since it doesn't extend beyond
1985 * minuend
1987 r2++;
1990 else if (r2->left < r1->right)
1993 * Left part of subtrahend covers part of minuend: add uncovered
1994 * part of minuend to region and skip to next subtrahend.
1996 MEMCHECK(pReg, pNextRect, pReg->rects);
1997 pNextRect->left = left;
1998 pNextRect->top = top;
1999 pNextRect->right = r2->left;
2000 pNextRect->bottom = bottom;
2001 pReg->numRects += 1;
2002 pNextRect++;
2003 left = r2->right;
2004 if (left >= r1->right)
2007 * Minuend used up: advance to new...
2009 r1++;
2010 if (r1 != r1End)
2011 left = r1->left;
2013 else
2016 * Subtrahend used up
2018 r2++;
2021 else
2024 * Minuend used up: add any remaining piece before advancing.
2026 if (r1->right > left)
2028 MEMCHECK(pReg, pNextRect, pReg->rects);
2029 pNextRect->left = left;
2030 pNextRect->top = top;
2031 pNextRect->right = r1->right;
2032 pNextRect->bottom = bottom;
2033 pReg->numRects += 1;
2034 pNextRect++;
2036 r1++;
2037 left = r1->left;
2042 * Add remaining minuend rectangles to region.
2044 while (r1 != r1End)
2046 MEMCHECK(pReg, pNextRect, pReg->rects);
2047 pNextRect->left = left;
2048 pNextRect->top = top;
2049 pNextRect->right = r1->right;
2050 pNextRect->bottom = bottom;
2051 pReg->numRects += 1;
2052 pNextRect++;
2053 r1++;
2054 if (r1 != r1End)
2056 left = r1->left;
2059 return;
2062 /***********************************************************************
2063 * REGION_SubtractRegion
2065 * Subtract regS from regM and leave the result in regD.
2066 * S stands for subtrahend, M for minuend and D for difference.
2068 * Results:
2069 * TRUE.
2071 * Side Effects:
2072 * regD is overwritten.
2075 static void REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM,
2076 WINEREGION *regS )
2078 /* check for trivial reject */
2079 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2080 (!EXTENTCHECK(&regM->extents, &regS->extents)) )
2082 REGION_CopyRegion(regD, regM);
2083 return;
2086 REGION_RegionOp (regD, regM, regS, (voidProcp) REGION_SubtractO,
2087 (voidProcp) REGION_SubtractNonO1, (voidProcp) NULL);
2090 * Can't alter newReg's extents before we call miRegionOp because
2091 * it might be one of the source regions and miRegionOp depends
2092 * on the extents of those regions being the unaltered. Besides, this
2093 * way there's no checking against rectangles that will be nuked
2094 * due to coalescing, so we have to examine fewer rectangles.
2096 REGION_SetExtents (regD);
2099 /***********************************************************************
2100 * REGION_XorRegion
2102 static void REGION_XorRegion(WINEREGION *dr, WINEREGION *sra,
2103 WINEREGION *srb)
2105 WINEREGION *tra, *trb;
2107 if ((! (tra = REGION_AllocWineRegion(sra->numRects + 1))) ||
2108 (! (trb = REGION_AllocWineRegion(srb->numRects + 1))))
2109 return;
2110 REGION_SubtractRegion(tra,sra,srb);
2111 REGION_SubtractRegion(trb,srb,sra);
2112 REGION_UnionRegion(dr,tra,trb);
2113 REGION_DestroyWineRegion(tra);
2114 REGION_DestroyWineRegion(trb);
2115 return;
2118 /**************************************************************************
2120 * Poly Regions
2122 *************************************************************************/
2124 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2125 #define SMALL_COORDINATE 0x80000000
2127 /***********************************************************************
2128 * REGION_InsertEdgeInET
2130 * Insert the given edge into the edge table.
2131 * First we must find the correct bucket in the
2132 * Edge table, then find the right slot in the
2133 * bucket. Finally, we can insert it.
2136 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2137 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2140 EdgeTableEntry *start, *prev;
2141 ScanLineList *pSLL, *pPrevSLL;
2142 ScanLineListBlock *tmpSLLBlock;
2145 * find the right bucket to put the edge into
2147 pPrevSLL = &ET->scanlines;
2148 pSLL = pPrevSLL->next;
2149 while (pSLL && (pSLL->scanline < scanline))
2151 pPrevSLL = pSLL;
2152 pSLL = pSLL->next;
2156 * reassign pSLL (pointer to ScanLineList) if necessary
2158 if ((!pSLL) || (pSLL->scanline > scanline))
2160 if (*iSLLBlock > SLLSPERBLOCK-1)
2162 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2163 if(!tmpSLLBlock)
2165 WARN("Can't alloc SLLB\n");
2166 return;
2168 (*SLLBlock)->next = tmpSLLBlock;
2169 tmpSLLBlock->next = (ScanLineListBlock *)NULL;
2170 *SLLBlock = tmpSLLBlock;
2171 *iSLLBlock = 0;
2173 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2175 pSLL->next = pPrevSLL->next;
2176 pSLL->edgelist = (EdgeTableEntry *)NULL;
2177 pPrevSLL->next = pSLL;
2179 pSLL->scanline = scanline;
2182 * now insert the edge in the right bucket
2184 prev = (EdgeTableEntry *)NULL;
2185 start = pSLL->edgelist;
2186 while (start && (start->bres.minor_axis < ETE->bres.minor_axis))
2188 prev = start;
2189 start = start->next;
2191 ETE->next = start;
2193 if (prev)
2194 prev->next = ETE;
2195 else
2196 pSLL->edgelist = ETE;
2199 /***********************************************************************
2200 * REGION_CreateEdgeTable
2202 * This routine creates the edge table for
2203 * scan converting polygons.
2204 * The Edge Table (ET) looks like:
2206 * EdgeTable
2207 * --------
2208 * | ymax | ScanLineLists
2209 * |scanline|-->------------>-------------->...
2210 * -------- |scanline| |scanline|
2211 * |edgelist| |edgelist|
2212 * --------- ---------
2213 * | |
2214 * | |
2215 * V V
2216 * list of ETEs list of ETEs
2218 * where ETE is an EdgeTableEntry data structure,
2219 * and there is one ScanLineList per scanline at
2220 * which an edge is initially entered.
2223 static void REGION_CreateETandAET(const INT *Count, INT nbpolygons,
2224 const POINT *pts, EdgeTable *ET, EdgeTableEntry *AET,
2225 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
2227 const POINT *top, *bottom;
2228 const POINT *PrevPt, *CurrPt, *EndPt;
2229 INT poly, count;
2230 int iSLLBlock = 0;
2231 int dy;
2235 * initialize the Active Edge Table
2237 AET->next = (EdgeTableEntry *)NULL;
2238 AET->back = (EdgeTableEntry *)NULL;
2239 AET->nextWETE = (EdgeTableEntry *)NULL;
2240 AET->bres.minor_axis = SMALL_COORDINATE;
2243 * initialize the Edge Table.
2245 ET->scanlines.next = (ScanLineList *)NULL;
2246 ET->ymax = SMALL_COORDINATE;
2247 ET->ymin = LARGE_COORDINATE;
2248 pSLLBlock->next = (ScanLineListBlock *)NULL;
2250 EndPt = pts - 1;
2251 for(poly = 0; poly < nbpolygons; poly++)
2253 count = Count[poly];
2254 EndPt += count;
2255 if(count < 2)
2256 continue;
2258 PrevPt = EndPt;
2261 * for each vertex in the array of points.
2262 * In this loop we are dealing with two vertices at
2263 * a time -- these make up one edge of the polygon.
2265 while (count--)
2267 CurrPt = pts++;
2270 * find out which point is above and which is below.
2272 if (PrevPt->y > CurrPt->y)
2274 bottom = PrevPt, top = CurrPt;
2275 pETEs->ClockWise = 0;
2277 else
2279 bottom = CurrPt, top = PrevPt;
2280 pETEs->ClockWise = 1;
2284 * don't add horizontal edges to the Edge table.
2286 if (bottom->y != top->y)
2288 pETEs->ymax = bottom->y-1;
2289 /* -1 so we don't get last scanline */
2292 * initialize integer edge algorithm
2294 dy = bottom->y - top->y;
2295 BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
2297 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
2298 &iSLLBlock);
2300 if (PrevPt->y > ET->ymax)
2301 ET->ymax = PrevPt->y;
2302 if (PrevPt->y < ET->ymin)
2303 ET->ymin = PrevPt->y;
2304 pETEs++;
2307 PrevPt = CurrPt;
2312 /***********************************************************************
2313 * REGION_loadAET
2315 * This routine moves EdgeTableEntries from the
2316 * EdgeTable into the Active Edge Table,
2317 * leaving them sorted by smaller x coordinate.
2320 static void REGION_loadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs)
2322 EdgeTableEntry *pPrevAET;
2323 EdgeTableEntry *tmp;
2325 pPrevAET = AET;
2326 AET = AET->next;
2327 while (ETEs)
2329 while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis))
2331 pPrevAET = AET;
2332 AET = AET->next;
2334 tmp = ETEs->next;
2335 ETEs->next = AET;
2336 if (AET)
2337 AET->back = ETEs;
2338 ETEs->back = pPrevAET;
2339 pPrevAET->next = ETEs;
2340 pPrevAET = ETEs;
2342 ETEs = tmp;
2346 /***********************************************************************
2347 * REGION_computeWAET
2349 * This routine links the AET by the
2350 * nextWETE (winding EdgeTableEntry) link for
2351 * use by the winding number rule. The final
2352 * Active Edge Table (AET) might look something
2353 * like:
2355 * AET
2356 * ---------- --------- ---------
2357 * |ymax | |ymax | |ymax |
2358 * | ... | |... | |... |
2359 * |next |->|next |->|next |->...
2360 * |nextWETE| |nextWETE| |nextWETE|
2361 * --------- --------- ^--------
2362 * | | |
2363 * V-------------------> V---> ...
2366 static void REGION_computeWAET(EdgeTableEntry *AET)
2368 register EdgeTableEntry *pWETE;
2369 register int inside = 1;
2370 register int isInside = 0;
2372 AET->nextWETE = (EdgeTableEntry *)NULL;
2373 pWETE = AET;
2374 AET = AET->next;
2375 while (AET)
2377 if (AET->ClockWise)
2378 isInside++;
2379 else
2380 isInside--;
2382 if ((!inside && !isInside) ||
2383 ( inside && isInside))
2385 pWETE->nextWETE = AET;
2386 pWETE = AET;
2387 inside = !inside;
2389 AET = AET->next;
2391 pWETE->nextWETE = (EdgeTableEntry *)NULL;
2394 /***********************************************************************
2395 * REGION_InsertionSort
2397 * Just a simple insertion sort using
2398 * pointers and back pointers to sort the Active
2399 * Edge Table.
2402 static BOOL REGION_InsertionSort(EdgeTableEntry *AET)
2404 EdgeTableEntry *pETEchase;
2405 EdgeTableEntry *pETEinsert;
2406 EdgeTableEntry *pETEchaseBackTMP;
2407 BOOL changed = FALSE;
2409 AET = AET->next;
2410 while (AET)
2412 pETEinsert = AET;
2413 pETEchase = AET;
2414 while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis)
2415 pETEchase = pETEchase->back;
2417 AET = AET->next;
2418 if (pETEchase != pETEinsert)
2420 pETEchaseBackTMP = pETEchase->back;
2421 pETEinsert->back->next = AET;
2422 if (AET)
2423 AET->back = pETEinsert->back;
2424 pETEinsert->next = pETEchase;
2425 pETEchase->back->next = pETEinsert;
2426 pETEchase->back = pETEinsert;
2427 pETEinsert->back = pETEchaseBackTMP;
2428 changed = TRUE;
2431 return changed;
2434 /***********************************************************************
2435 * REGION_FreeStorage
2437 * Clean up our act.
2439 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2441 ScanLineListBlock *tmpSLLBlock;
2443 while (pSLLBlock)
2445 tmpSLLBlock = pSLLBlock->next;
2446 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2447 pSLLBlock = tmpSLLBlock;
2452 /***********************************************************************
2453 * REGION_PtsToRegion
2455 * Create an array of rectangles from a list of points.
2457 static int REGION_PtsToRegion(int numFullPtBlocks, int iCurPtBlock,
2458 POINTBLOCK *FirstPtBlock, WINEREGION *reg)
2460 RECT *rects;
2461 POINT *pts;
2462 POINTBLOCK *CurPtBlock;
2463 int i;
2464 RECT *extents;
2465 INT numRects;
2467 extents = &reg->extents;
2469 numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1;
2471 if (!(reg->rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects,
2472 sizeof(RECT) * numRects )))
2473 return(0);
2475 reg->size = numRects;
2476 CurPtBlock = FirstPtBlock;
2477 rects = reg->rects - 1;
2478 numRects = 0;
2479 extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
2481 for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) {
2482 /* the loop uses 2 points per iteration */
2483 i = NUMPTSTOBUFFER >> 1;
2484 if (!numFullPtBlocks)
2485 i = iCurPtBlock >> 1;
2486 for (pts = CurPtBlock->pts; i--; pts += 2) {
2487 if (pts->x == pts[1].x)
2488 continue;
2489 if (numRects && pts->x == rects->left && pts->y == rects->bottom &&
2490 pts[1].x == rects->right &&
2491 (numRects == 1 || rects[-1].top != rects->top) &&
2492 (i && pts[2].y > pts[1].y)) {
2493 rects->bottom = pts[1].y + 1;
2494 continue;
2496 numRects++;
2497 rects++;
2498 rects->left = pts->x; rects->top = pts->y;
2499 rects->right = pts[1].x; rects->bottom = pts[1].y + 1;
2500 if (rects->left < extents->left)
2501 extents->left = rects->left;
2502 if (rects->right > extents->right)
2503 extents->right = rects->right;
2505 CurPtBlock = CurPtBlock->next;
2508 if (numRects) {
2509 extents->top = reg->rects->top;
2510 extents->bottom = rects->bottom;
2511 } else {
2512 extents->left = 0;
2513 extents->top = 0;
2514 extents->right = 0;
2515 extents->bottom = 0;
2517 reg->numRects = numRects;
2519 return(TRUE);
2522 /***********************************************************************
2523 * CreatePolyPolygonRgn (GDI32.@)
2525 HRGN WINAPI CreatePolyPolygonRgn(const POINT *Pts, const INT *Count,
2526 INT nbpolygons, INT mode)
2528 HRGN hrgn;
2529 RGNOBJ *obj;
2530 WINEREGION *region;
2531 register EdgeTableEntry *pAET; /* Active Edge Table */
2532 register INT y; /* current scanline */
2533 register int iPts = 0; /* number of pts in buffer */
2534 register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/
2535 register ScanLineList *pSLL; /* current scanLineList */
2536 register POINT *pts; /* output buffer */
2537 EdgeTableEntry *pPrevAET; /* ptr to previous AET */
2538 EdgeTable ET; /* header node for ET */
2539 EdgeTableEntry AET; /* header node for AET */
2540 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2541 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2542 int fixWAET = FALSE;
2543 POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */
2544 POINTBLOCK *tmpPtBlock;
2545 int numFullPtBlocks = 0;
2546 INT poly, total;
2548 if(!(hrgn = REGION_CreateRegion(nbpolygons)))
2549 return 0;
2550 obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
2551 region = obj->rgn;
2553 /* special case a rectangle */
2555 if (((nbpolygons == 1) && ((*Count == 4) ||
2556 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2557 (((Pts[0].y == Pts[1].y) &&
2558 (Pts[1].x == Pts[2].x) &&
2559 (Pts[2].y == Pts[3].y) &&
2560 (Pts[3].x == Pts[0].x)) ||
2561 ((Pts[0].x == Pts[1].x) &&
2562 (Pts[1].y == Pts[2].y) &&
2563 (Pts[2].x == Pts[3].x) &&
2564 (Pts[3].y == Pts[0].y))))
2566 SetRectRgn( hrgn, min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2567 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2568 GDI_ReleaseObj( hrgn );
2569 return hrgn;
2572 for(poly = total = 0; poly < nbpolygons; poly++)
2573 total += Count[poly];
2574 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2576 REGION_DeleteObject( hrgn, obj );
2577 return 0;
2579 pts = FirstPtBlock.pts;
2580 REGION_CreateETandAET(Count, nbpolygons, Pts, &ET, &AET, pETEs, &SLLBlock);
2581 pSLL = ET.scanlines.next;
2582 curPtBlock = &FirstPtBlock;
2584 if (mode != WINDING) {
2586 * for each scanline
2588 for (y = ET.ymin; y < ET.ymax; y++) {
2590 * Add a new edge to the active edge table when we
2591 * get to the next edge.
2593 if (pSLL != NULL && y == pSLL->scanline) {
2594 REGION_loadAET(&AET, pSLL->edgelist);
2595 pSLL = pSLL->next;
2597 pPrevAET = &AET;
2598 pAET = AET.next;
2601 * for each active edge
2603 while (pAET) {
2604 pts->x = pAET->bres.minor_axis, pts->y = y;
2605 pts++, iPts++;
2608 * send out the buffer
2610 if (iPts == NUMPTSTOBUFFER) {
2611 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK));
2612 if(!tmpPtBlock) {
2613 WARN("Can't alloc tPB\n");
2614 return 0;
2616 curPtBlock->next = tmpPtBlock;
2617 curPtBlock = tmpPtBlock;
2618 pts = curPtBlock->pts;
2619 numFullPtBlocks++;
2620 iPts = 0;
2622 EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
2624 REGION_InsertionSort(&AET);
2627 else {
2629 * for each scanline
2631 for (y = ET.ymin; y < ET.ymax; y++) {
2633 * Add a new edge to the active edge table when we
2634 * get to the next edge.
2636 if (pSLL != NULL && y == pSLL->scanline) {
2637 REGION_loadAET(&AET, pSLL->edgelist);
2638 REGION_computeWAET(&AET);
2639 pSLL = pSLL->next;
2641 pPrevAET = &AET;
2642 pAET = AET.next;
2643 pWETE = pAET;
2646 * for each active edge
2648 while (pAET) {
2650 * add to the buffer only those edges that
2651 * are in the Winding active edge table.
2653 if (pWETE == pAET) {
2654 pts->x = pAET->bres.minor_axis, pts->y = y;
2655 pts++, iPts++;
2658 * send out the buffer
2660 if (iPts == NUMPTSTOBUFFER) {
2661 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0,
2662 sizeof(POINTBLOCK) );
2663 if(!tmpPtBlock) {
2664 WARN("Can't alloc tPB\n");
2665 REGION_DeleteObject( hrgn, obj );
2666 return 0;
2668 curPtBlock->next = tmpPtBlock;
2669 curPtBlock = tmpPtBlock;
2670 pts = curPtBlock->pts;
2671 numFullPtBlocks++; iPts = 0;
2673 pWETE = pWETE->nextWETE;
2675 EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
2679 * recompute the winding active edge table if
2680 * we just resorted or have exited an edge.
2682 if (REGION_InsertionSort(&AET) || fixWAET) {
2683 REGION_computeWAET(&AET);
2684 fixWAET = FALSE;
2688 REGION_FreeStorage(SLLBlock.next);
2689 REGION_PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region);
2691 for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) {
2692 tmpPtBlock = curPtBlock->next;
2693 HeapFree( GetProcessHeap(), 0, curPtBlock );
2694 curPtBlock = tmpPtBlock;
2696 HeapFree( GetProcessHeap(), 0, pETEs );
2697 GDI_ReleaseObj( hrgn );
2698 return hrgn;
2702 /***********************************************************************
2703 * CreatePolygonRgn (GDI32.@)
2705 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count,
2706 INT mode )
2708 return CreatePolyPolygonRgn( points, &count, 1, mode );