2 * GDI region objects. Shamelessly ripped out from the X11 distribution
3 * Thanks for the nice license.
5 * Copyright 1993, 1994, 1995 Alexandre Julliard
6 * Modifications and additions: Copyright 1998 Huw Davies
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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.
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
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
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...
103 #include "gdi_private.h"
104 #include "wine/debug.h"
106 WINE_DEFAULT_DEBUG_CHANNEL(region);
108 /* GDI logical region object */
116 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc );
117 static BOOL REGION_DeleteObject( HGDIOBJ handle );
119 static const struct gdi_obj_funcs region_funcs =
121 REGION_SelectObject, /* pSelectObject */
122 NULL, /* pGetObjectA */
123 NULL, /* pGetObjectW */
124 NULL, /* pUnrealizeObject */
125 REGION_DeleteObject /* pDeleteObject */
128 /* 1 if two RECTs overlap.
129 * 0 if two RECTs do not overlap.
131 #define EXTENTCHECK(r1, r2) \
132 ((r1)->right > (r2)->left && \
133 (r1)->left < (r2)->right && \
134 (r1)->bottom > (r2)->top && \
135 (r1)->top < (r2)->bottom)
138 static BOOL add_rect( WINEREGION *reg, INT left, INT top, INT right, INT bottom )
141 if (reg->numRects >= reg->size)
143 RECT *newrects = HeapReAlloc( GetProcessHeap(), 0, reg->rects, 2 * sizeof(RECT) * reg->size );
144 if (!newrects) return FALSE;
145 reg->rects = newrects;
148 rect = reg->rects + reg->numRects++;
152 rect->bottom = bottom;
156 #define EMPTY_REGION(pReg) do { \
157 (pReg)->numRects = 0; \
158 (pReg)->extents.left = (pReg)->extents.top = 0; \
159 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
162 #define INRECT(r, x, y) \
163 ( ( ((r).right > x)) && \
164 ( ((r).left <= x)) && \
165 ( ((r).bottom > y)) && \
170 * number of points to buffer before sending them off
171 * to scanlines() : Must be an even number
173 #define NUMPTSTOBUFFER 200
176 * used to allocate buffers for points and link
177 * the buffers together
180 typedef struct _POINTBLOCK {
181 POINT pts[NUMPTSTOBUFFER];
182 struct _POINTBLOCK *next;
188 * This file contains a few macros to help track
189 * the edge of a filled object. The object is assumed
190 * to be filled in scanline order, and thus the
191 * algorithm used is an extension of Bresenham's line
192 * drawing algorithm which assumes that y is always the
194 * Since these pieces of code are the same for any filled shape,
195 * it is more convenient to gather the library in one
196 * place, but since these pieces of code are also in
197 * the inner loops of output primitives, procedure call
198 * overhead is out of the question.
199 * See the author for a derivation if needed.
204 * In scan converting polygons, we want to choose those pixels
205 * which are inside the polygon. Thus, we add .5 to the starting
206 * x coordinate for both left and right edges. Now we choose the
207 * first pixel which is inside the pgon for the left edge and the
208 * first pixel which is outside the pgon for the right edge.
209 * Draw the left pixel, but not the right.
211 * How to add .5 to the starting x coordinate:
212 * If the edge is moving to the right, then subtract dy from the
213 * error term from the general form of the algorithm.
214 * If the edge is moving to the left, then add dy to the error term.
216 * The reason for the difference between edges moving to the left
217 * and edges moving to the right is simple: If an edge is moving
218 * to the right, then we want the algorithm to flip immediately.
219 * If it is moving to the left, then we don't want it to flip until
220 * we traverse an entire pixel.
222 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
223 int dx; /* local storage */ \
226 * if the edge is horizontal, then it is ignored \
227 * and assumed not to be processed. Otherwise, do this stuff. \
231 dx = (x2) - xStart; \
235 incr1 = -2 * dx + 2 * (dy) * m1; \
236 incr2 = -2 * dx + 2 * (dy) * m; \
237 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
241 incr1 = 2 * dx - 2 * (dy) * m1; \
242 incr2 = 2 * dx - 2 * (dy) * m; \
243 d = -2 * m * (dy) + 2 * dx; \
248 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
271 * This structure contains all of the information needed
272 * to run the bresenham algorithm.
273 * The variables may be hardcoded into the declarations
274 * instead of using this structure to make use of
275 * register declarations.
278 INT minor_axis; /* minor axis */
279 INT d; /* decision variable */
280 INT m, m1; /* slope and slope+1 */
281 INT incr1, incr2; /* error increments */
285 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
286 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
287 bres.m, bres.m1, bres.incr1, bres.incr2)
289 #define BRESINCRPGONSTRUCT(bres) \
290 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
295 * These are the data structures needed to scan
296 * convert regions. Two different scan conversion
297 * methods are available -- the even-odd method, and
298 * the winding number method.
299 * The even-odd rule states that a point is inside
300 * the polygon if a ray drawn from that point in any
301 * direction will pass through an odd number of
303 * By the winding number rule, a point is decided
304 * to be inside the polygon if a ray drawn from that
305 * point in any direction passes through a different
306 * number of clockwise and counter-clockwise path
309 * These data structures are adapted somewhat from
310 * the algorithm in (Foley/Van Dam) for scan converting
312 * The basic algorithm is to start at the top (smallest y)
313 * of the polygon, stepping down to the bottom of
314 * the polygon by incrementing the y coordinate. We
315 * keep a list of edges which the current scanline crosses,
316 * sorted by x. This list is called the Active Edge Table (AET)
317 * As we change the y-coordinate, we update each entry in
318 * in the active edge table to reflect the edges new xcoord.
319 * This list must be sorted at each scanline in case
320 * two edges intersect.
321 * We also keep a data structure known as the Edge Table (ET),
322 * which keeps track of all the edges which the current
323 * scanline has not yet reached. The ET is basically a
324 * list of ScanLineList structures containing a list of
325 * edges which are entered at a given scanline. There is one
326 * ScanLineList per scanline at which an edge is entered.
327 * When we enter a new edge, we move it from the ET to the AET.
329 * From the AET, we can implement the even-odd rule as in
331 * The winding number rule is a little trickier. We also
332 * keep the EdgeTableEntries in the AET linked by the
333 * nextWETE (winding EdgeTableEntry) link. This allows
334 * the edges to be linked just as before for updating
335 * purposes, but only uses the edges linked by the nextWETE
336 * link as edges representing spans of the polygon to
337 * drawn (as with the even-odd rule).
341 * for the winding number rule
344 #define COUNTERCLOCKWISE -1
346 typedef struct _EdgeTableEntry {
347 INT ymax; /* ycoord at which we exit this edge. */
348 BRESINFO bres; /* Bresenham info to run the edge */
349 struct _EdgeTableEntry *next; /* next in the list */
350 struct _EdgeTableEntry *back; /* for insertion sort */
351 struct _EdgeTableEntry *nextWETE; /* for winding num rule */
352 int ClockWise; /* flag for winding number rule */
356 typedef struct _ScanLineList{
357 INT scanline; /* the scanline represented */
358 EdgeTableEntry *edgelist; /* header node */
359 struct _ScanLineList *next; /* next in the list */
364 INT ymax; /* ymax for the polygon */
365 INT ymin; /* ymin for the polygon */
366 ScanLineList scanlines; /* header node */
371 * Here is a struct to help with storage allocation
372 * so we can allocate a big chunk at a time, and then take
373 * pieces from this heap when we need to.
375 #define SLLSPERBLOCK 25
377 typedef struct _ScanLineListBlock {
378 ScanLineList SLLs[SLLSPERBLOCK];
379 struct _ScanLineListBlock *next;
385 * a few macros for the inner loops of the fill code where
386 * performance considerations don't allow a procedure call.
388 * Evaluate the given edge at the given scanline.
389 * If the edge has expired, then we leave it and fix up
390 * the active edge table; otherwise, we increment the
391 * x value to be ready for the next scanline.
392 * The winding number rule is in effect, so we must notify
393 * the caller when the edge has been removed so he
394 * can reorder the Winding Active Edge Table.
396 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
397 if (pAET->ymax == y) { /* leaving this edge */ \
398 pPrevAET->next = pAET->next; \
399 pAET = pPrevAET->next; \
402 pAET->back = pPrevAET; \
405 BRESINCRPGONSTRUCT(pAET->bres); \
413 * Evaluate the given edge at the given scanline.
414 * If the edge has expired, then we leave it and fix up
415 * the active edge table; otherwise, we increment the
416 * x value to be ready for the next scanline.
417 * The even-odd rule is in effect.
419 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
420 if (pAET->ymax == y) { /* leaving this edge */ \
421 pPrevAET->next = pAET->next; \
422 pAET = pPrevAET->next; \
424 pAET->back = pPrevAET; \
427 BRESINCRPGONSTRUCT(pAET->bres); \
433 /* Note the parameter order is different from the X11 equivalents */
435 static BOOL REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
436 static BOOL REGION_OffsetRegion(WINEREGION *d, WINEREGION *s, INT x, INT y);
437 static BOOL REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
438 static BOOL REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
439 static BOOL REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
440 static BOOL REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
441 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
443 #define RGN_DEFAULT_RECTS 2
446 /***********************************************************************
449 static inline INT get_region_type( const RGNOBJ *obj )
451 switch(obj->rgn.numRects)
453 case 0: return NULLREGION;
454 case 1: return SIMPLEREGION;
455 default: return COMPLEXREGION;
460 /***********************************************************************
462 * Outputs the contents of a WINEREGION
464 static void REGION_DumpRegion(WINEREGION *pReg)
466 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
468 TRACE("Region %p: %d,%d - %d,%d %d rects\n", pReg,
469 pReg->extents.left, pReg->extents.top,
470 pReg->extents.right, pReg->extents.bottom, pReg->numRects);
471 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
472 TRACE("\t%d,%d - %d,%d\n", pRect->left, pRect->top,
473 pRect->right, pRect->bottom);
478 /***********************************************************************
481 * Initialize a new empty region.
483 static BOOL init_region( WINEREGION *pReg, INT n )
485 if (!(pReg->rects = HeapAlloc(GetProcessHeap(), 0, n * sizeof( RECT )))) return FALSE;
491 /***********************************************************************
494 static void destroy_region( WINEREGION *pReg )
496 HeapFree( GetProcessHeap(), 0, pReg->rects );
499 /***********************************************************************
500 * REGION_DeleteObject
502 static BOOL REGION_DeleteObject( HGDIOBJ handle )
504 RGNOBJ *rgn = free_gdi_handle( handle );
506 if (!rgn) return FALSE;
507 HeapFree( GetProcessHeap(), 0, rgn->rgn.rects );
508 HeapFree( GetProcessHeap(), 0, rgn );
512 /***********************************************************************
513 * REGION_SelectObject
515 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc )
517 return ULongToHandle(SelectClipRgn( hdc, handle ));
521 /***********************************************************************
522 * REGION_OffsetRegion
523 * Offset a WINEREGION by x,y
525 static BOOL REGION_OffsetRegion( WINEREGION *rgn, WINEREGION *srcrgn, INT x, INT y )
529 if (!REGION_CopyRegion( rgn, srcrgn)) return FALSE;
532 int nbox = rgn->numRects;
533 RECT *pbox = rgn->rects;
543 rgn->extents.left += x;
544 rgn->extents.right += x;
545 rgn->extents.top += y;
546 rgn->extents.bottom += y;
552 /***********************************************************************
553 * OffsetRgn (GDI32.@)
555 * Moves a region by the specified X- and Y-axis offsets.
558 * hrgn [I] Region to offset.
559 * x [I] Offset right if positive or left if negative.
560 * y [I] Offset down if positive or up if negative.
564 * NULLREGION - The new region is empty.
565 * SIMPLEREGION - The new region can be represented by one rectangle.
566 * COMPLEXREGION - The new region can only be represented by more than
570 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
572 RGNOBJ * obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
575 TRACE("%p %d,%d\n", hrgn, x, y);
580 REGION_OffsetRegion( &obj->rgn, &obj->rgn, x, y);
582 ret = get_region_type( obj );
583 GDI_ReleaseObj( hrgn );
588 /***********************************************************************
589 * GetRgnBox (GDI32.@)
591 * Retrieves the bounding rectangle of the region. The bounding rectangle
592 * is the smallest rectangle that contains the entire region.
595 * hrgn [I] Region to retrieve bounding rectangle from.
596 * rect [O] Rectangle that will receive the coordinates of the bounding
600 * NULLREGION - The new region is empty.
601 * SIMPLEREGION - The new region can be represented by one rectangle.
602 * COMPLEXREGION - The new region can only be represented by more than
605 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
607 RGNOBJ * obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
611 rect->left = obj->rgn.extents.left;
612 rect->top = obj->rgn.extents.top;
613 rect->right = obj->rgn.extents.right;
614 rect->bottom = obj->rgn.extents.bottom;
615 TRACE("%p (%d,%d-%d,%d)\n", hrgn,
616 rect->left, rect->top, rect->right, rect->bottom);
617 ret = get_region_type( obj );
618 GDI_ReleaseObj(hrgn);
625 /***********************************************************************
626 * CreateRectRgn (GDI32.@)
628 * Creates a simple rectangular region.
631 * left [I] Left coordinate of rectangle.
632 * top [I] Top coordinate of rectangle.
633 * right [I] Right coordinate of rectangle.
634 * bottom [I] Bottom coordinate of rectangle.
637 * Success: Handle to region.
640 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
645 if (!(obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) ))) return 0;
647 /* Allocate 2 rects by default to reduce the number of reallocs */
648 if (!init_region( &obj->rgn, RGN_DEFAULT_RECTS ))
650 HeapFree( GetProcessHeap(), 0, obj );
653 if (!(hrgn = alloc_gdi_handle( &obj->header, OBJ_REGION, ®ion_funcs )))
655 HeapFree( GetProcessHeap(), 0, obj->rgn.rects );
656 HeapFree( GetProcessHeap(), 0, obj );
659 TRACE( "%d,%d-%d,%d returning %p\n", left, top, right, bottom, hrgn );
660 SetRectRgn(hrgn, left, top, right, bottom);
665 /***********************************************************************
666 * CreateRectRgnIndirect (GDI32.@)
668 * Creates a simple rectangular region.
671 * rect [I] Coordinates of rectangular region.
674 * Success: Handle to region.
677 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
679 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
683 /***********************************************************************
684 * SetRectRgn (GDI32.@)
686 * Sets a region to a simple rectangular region.
689 * hrgn [I] Region to convert.
690 * left [I] Left coordinate of rectangle.
691 * top [I] Top coordinate of rectangle.
692 * right [I] Right coordinate of rectangle.
693 * bottom [I] Bottom coordinate of rectangle.
700 * Allows either or both left and top to be greater than right or bottom.
702 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
703 INT right, INT bottom )
707 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
709 if (!(obj = GDI_GetObjPtr( hrgn, OBJ_REGION ))) return FALSE;
711 if (left > right) { INT tmp = left; left = right; right = tmp; }
712 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
714 if((left != right) && (top != bottom))
716 obj->rgn.rects->left = obj->rgn.extents.left = left;
717 obj->rgn.rects->top = obj->rgn.extents.top = top;
718 obj->rgn.rects->right = obj->rgn.extents.right = right;
719 obj->rgn.rects->bottom = obj->rgn.extents.bottom = bottom;
720 obj->rgn.numRects = 1;
723 EMPTY_REGION(&obj->rgn);
725 GDI_ReleaseObj( hrgn );
730 /***********************************************************************
731 * CreateRoundRectRgn (GDI32.@)
733 * Creates a rectangular region with rounded corners.
736 * left [I] Left coordinate of rectangle.
737 * top [I] Top coordinate of rectangle.
738 * right [I] Right coordinate of rectangle.
739 * bottom [I] Bottom coordinate of rectangle.
740 * ellipse_width [I] Width of the ellipse at each corner.
741 * ellipse_height [I] Height of the ellipse at each corner.
744 * Success: Handle to region.
748 * If ellipse_width or ellipse_height is less than 2 logical units then
749 * it is treated as though CreateRectRgn() was called instead.
751 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
752 INT right, INT bottom,
753 INT ellipse_width, INT ellipse_height )
757 int a, b, i, x, y, asq, bsq, dx, dy, err;
760 /* Make the dimensions sensible */
762 if (left > right) { INT tmp = left; left = right; right = tmp; }
763 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
764 /* the region is for the rectangle interior, but only at right and bottom for some reason */
768 ellipse_width = min( right - left, abs( ellipse_width ));
769 ellipse_height = min( bottom - top, abs( ellipse_height ));
771 /* Check if we can do a normal rectangle instead */
773 if ((ellipse_width < 2) || (ellipse_height < 2))
774 return CreateRectRgn( left, top, right, bottom );
776 if (!(obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) ))) return 0;
777 obj->rgn.size = ellipse_height;
778 obj->rgn.numRects = ellipse_height;
779 obj->rgn.extents.left = left;
780 obj->rgn.extents.top = top;
781 obj->rgn.extents.right = right;
782 obj->rgn.extents.bottom = bottom;
784 obj->rgn.rects = rects = HeapAlloc( GetProcessHeap(), 0, obj->rgn.size * sizeof(RECT) );
785 if (!rects) goto done;
787 /* based on an algorithm by Alois Zingl */
789 a = ellipse_width - 1;
790 b = ellipse_height - 1;
793 dx = 4 * b * b * (1 - a);
794 dy = 4 * a * a * (1 + (b % 2));
795 err = dx + dy + a * a * (b % 2);
798 y = ellipse_height / 2;
800 rects[y].left = left;
801 rects[y].right = right;
803 while (x <= ellipse_width / 2)
815 rects[y].left = left + x;
816 rects[y].right = right - x;
819 for (i = 0; i < ellipse_height / 2; i++)
821 rects[i].left = rects[b - i].left;
822 rects[i].right = rects[b - i].right;
823 rects[i].top = top + i;
824 rects[i].bottom = rects[i].top + 1;
826 rects[i - 1].bottom = bottom - ellipse_height + i; /* extend to bottom of rectangle */
827 for (; i < ellipse_height; i++)
829 rects[i].top = bottom - ellipse_height + i;
830 rects[i].bottom = rects[i].top + 1;
833 hrgn = alloc_gdi_handle( &obj->header, OBJ_REGION, ®ion_funcs );
835 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
836 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
840 HeapFree( GetProcessHeap(), 0, obj->rgn.rects );
841 HeapFree( GetProcessHeap(), 0, obj );
847 /***********************************************************************
848 * CreateEllipticRgn (GDI32.@)
850 * Creates an elliptical region.
853 * left [I] Left coordinate of bounding rectangle.
854 * top [I] Top coordinate of bounding rectangle.
855 * right [I] Right coordinate of bounding rectangle.
856 * bottom [I] Bottom coordinate of bounding rectangle.
859 * Success: Handle to region.
863 * This is a special case of CreateRoundRectRgn() where the width of the
864 * ellipse at each corner is equal to the width the rectangle and
865 * the same for the height.
867 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
868 INT right, INT bottom )
870 return CreateRoundRectRgn( left, top, right, bottom,
871 right-left, bottom-top );
875 /***********************************************************************
876 * CreateEllipticRgnIndirect (GDI32.@)
878 * Creates an elliptical region.
881 * rect [I] Pointer to bounding rectangle of the ellipse.
884 * Success: Handle to region.
888 * This is a special case of CreateRoundRectRgn() where the width of the
889 * ellipse at each corner is equal to the width the rectangle and
890 * the same for the height.
892 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
894 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
895 rect->bottom, rect->right - rect->left,
896 rect->bottom - rect->top );
899 /*********************************************************************
902 * Return the region data without making a copy. The caller
903 * must not alter anything and must call GDI_ReleaseObj() when
904 * they have finished with the data.
906 const WINEREGION *get_wine_region(HRGN rgn)
908 RGNOBJ *obj = GDI_GetObjPtr( rgn, OBJ_REGION );
909 if(!obj) return NULL;
913 /***********************************************************************
914 * GetRegionData (GDI32.@)
916 * Retrieves the data that specifies the region.
919 * hrgn [I] Region to retrieve the region data from.
920 * count [I] The size of the buffer pointed to by rgndata in bytes.
921 * rgndata [I] The buffer to receive data about the region.
924 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
925 * the number of bytes copied to the output buffer.
929 * The format of the Buffer member of RGNDATA is determined by the iType
930 * member of the region data header.
931 * Currently this is always RDH_RECTANGLES, which specifies that the format
932 * is the array of RECT's that specify the region. The length of the array
933 * is specified by the nCount member of the region data header.
935 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
938 RGNOBJ *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
940 TRACE(" %p count = %d, rgndata = %p\n", hrgn, count, rgndata);
944 size = obj->rgn.numRects * sizeof(RECT);
945 if(count < (size + sizeof(RGNDATAHEADER)) || rgndata == NULL)
947 GDI_ReleaseObj( hrgn );
948 if (rgndata) /* buffer is too small, signal it by return 0 */
950 else /* user requested buffer size with rgndata NULL */
951 return size + sizeof(RGNDATAHEADER);
954 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
955 rgndata->rdh.iType = RDH_RECTANGLES;
956 rgndata->rdh.nCount = obj->rgn.numRects;
957 rgndata->rdh.nRgnSize = size;
958 rgndata->rdh.rcBound.left = obj->rgn.extents.left;
959 rgndata->rdh.rcBound.top = obj->rgn.extents.top;
960 rgndata->rdh.rcBound.right = obj->rgn.extents.right;
961 rgndata->rdh.rcBound.bottom = obj->rgn.extents.bottom;
963 memcpy( rgndata->Buffer, obj->rgn.rects, size );
965 GDI_ReleaseObj( hrgn );
966 return size + sizeof(RGNDATAHEADER);
970 static void translate( POINT *pt, UINT count, const XFORM *xform )
976 pt->x = floor( x * xform->eM11 + y * xform->eM21 + xform->eDx + 0.5 );
977 pt->y = floor( x * xform->eM12 + y * xform->eM22 + xform->eDy + 0.5 );
983 /***********************************************************************
984 * ExtCreateRegion (GDI32.@)
986 * Creates a region as specified by the transformation data and region data.
989 * lpXform [I] World-space to logical-space transformation data.
990 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
991 * rgndata [I] Data that specifies the region.
994 * Success: Handle to region.
998 * See GetRegionData().
1000 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
1007 SetLastError( ERROR_INVALID_PARAMETER );
1011 if (rgndata->rdh.dwSize < sizeof(RGNDATAHEADER))
1014 /* XP doesn't care about the type */
1015 if( rgndata->rdh.iType != RDH_RECTANGLES )
1016 WARN("(Unsupported region data type: %u)\n", rgndata->rdh.iType);
1020 const RECT *pCurRect, *pEndRect;
1022 hrgn = CreateRectRgn( 0, 0, 0, 0 );
1024 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1025 for (pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1027 static const INT count = 4;
1031 pt[0].x = pCurRect->left;
1032 pt[0].y = pCurRect->top;
1033 pt[1].x = pCurRect->right;
1034 pt[1].y = pCurRect->top;
1035 pt[2].x = pCurRect->right;
1036 pt[2].y = pCurRect->bottom;
1037 pt[3].x = pCurRect->left;
1038 pt[3].y = pCurRect->bottom;
1040 translate( pt, 4, lpXform );
1041 poly_hrgn = CreatePolyPolygonRgn( pt, &count, 1, WINDING );
1042 CombineRgn( hrgn, hrgn, poly_hrgn, RGN_OR );
1043 DeleteObject( poly_hrgn );
1048 if (!(obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) ))) return 0;
1050 if (init_region( &obj->rgn, rgndata->rdh.nCount ))
1052 const RECT *pCurRect, *pEndRect;
1054 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1055 for(pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1057 if (pCurRect->left < pCurRect->right && pCurRect->top < pCurRect->bottom)
1059 if (!REGION_UnionRectWithRegion( pCurRect, &obj->rgn )) goto done;
1062 hrgn = alloc_gdi_handle( &obj->header, OBJ_REGION, ®ion_funcs );
1066 HeapFree( GetProcessHeap(), 0, obj );
1073 HeapFree( GetProcessHeap(), 0, obj->rgn.rects );
1074 HeapFree( GetProcessHeap(), 0, obj );
1076 TRACE("%p %d %p returning %p\n", lpXform, dwCount, rgndata, hrgn );
1081 /***********************************************************************
1082 * PtInRegion (GDI32.@)
1084 * Tests whether the specified point is inside a region.
1087 * hrgn [I] Region to test.
1088 * x [I] X-coordinate of point to test.
1089 * y [I] Y-coordinate of point to test.
1092 * Non-zero if the point is inside the region or zero otherwise.
1094 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
1099 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1103 if (obj->rgn.numRects > 0 && INRECT(obj->rgn.extents, x, y))
1104 for (i = 0; i < obj->rgn.numRects; i++)
1105 if (INRECT (obj->rgn.rects[i], x, y))
1110 GDI_ReleaseObj( hrgn );
1116 /***********************************************************************
1117 * RectInRegion (GDI32.@)
1119 * Tests if a rectangle is at least partly inside the specified region.
1122 * hrgn [I] Region to test.
1123 * rect [I] Rectangle to test.
1126 * Non-zero if the rectangle is partially inside the region or
1129 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
1135 /* swap the coordinates to make right >= left and bottom >= top */
1136 /* (region building rectangles are normalized the same way) */
1137 if( rect->top > rect->bottom) {
1138 rc.top = rect->bottom;
1139 rc.bottom = rect->top;
1142 rc.bottom = rect->bottom;
1144 if( rect->right < rect->left) {
1145 rc.right = rect->left;
1146 rc.left = rect->right;
1148 rc.right = rect->right;
1149 rc.left = rect->left;
1152 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1154 RECT *pCurRect, *pRectEnd;
1156 /* this is (just) a useful optimization */
1157 if ((obj->rgn.numRects > 0) && EXTENTCHECK(&obj->rgn.extents, &rc))
1159 for (pCurRect = obj->rgn.rects, pRectEnd = pCurRect +
1160 obj->rgn.numRects; pCurRect < pRectEnd; pCurRect++)
1162 if (pCurRect->bottom <= rc.top)
1163 continue; /* not far enough down yet */
1165 if (pCurRect->top >= rc.bottom)
1166 break; /* too far down */
1168 if (pCurRect->right <= rc.left)
1169 continue; /* not far enough over yet */
1171 if (pCurRect->left >= rc.right) {
1179 GDI_ReleaseObj(hrgn);
1184 /***********************************************************************
1185 * EqualRgn (GDI32.@)
1187 * Tests whether one region is identical to another.
1190 * hrgn1 [I] The first region to compare.
1191 * hrgn2 [I] The second region to compare.
1194 * Non-zero if both regions are identical or zero otherwise.
1196 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
1198 RGNOBJ *obj1, *obj2;
1201 if ((obj1 = GDI_GetObjPtr( hrgn1, OBJ_REGION )))
1203 if ((obj2 = GDI_GetObjPtr( hrgn2, OBJ_REGION )))
1207 if ( obj1->rgn.numRects != obj2->rgn.numRects ) goto done;
1208 if ( obj1->rgn.numRects == 0 )
1214 if (obj1->rgn.extents.left != obj2->rgn.extents.left) goto done;
1215 if (obj1->rgn.extents.right != obj2->rgn.extents.right) goto done;
1216 if (obj1->rgn.extents.top != obj2->rgn.extents.top) goto done;
1217 if (obj1->rgn.extents.bottom != obj2->rgn.extents.bottom) goto done;
1218 for( i = 0; i < obj1->rgn.numRects; i++ )
1220 if (obj1->rgn.rects[i].left != obj2->rgn.rects[i].left) goto done;
1221 if (obj1->rgn.rects[i].right != obj2->rgn.rects[i].right) goto done;
1222 if (obj1->rgn.rects[i].top != obj2->rgn.rects[i].top) goto done;
1223 if (obj1->rgn.rects[i].bottom != obj2->rgn.rects[i].bottom) goto done;
1227 GDI_ReleaseObj(hrgn2);
1229 GDI_ReleaseObj(hrgn1);
1234 /***********************************************************************
1235 * REGION_UnionRectWithRegion
1236 * Adds a rectangle to a WINEREGION
1238 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1242 region.rects = ®ion.extents;
1243 region.numRects = 1;
1245 region.extents = *rect;
1246 return REGION_UnionRegion(rgn, rgn, ®ion);
1250 BOOL add_rect_to_region( HRGN rgn, const RECT *rect )
1252 RGNOBJ *obj = GDI_GetObjPtr( rgn, OBJ_REGION );
1255 if (!obj) return FALSE;
1256 ret = REGION_UnionRectWithRegion( rect, &obj->rgn );
1257 GDI_ReleaseObj( rgn );
1261 /***********************************************************************
1262 * REGION_CreateFrameRgn
1264 * Create a region that is a frame around another region.
1265 * Compute the intersection of the region moved in all 4 directions
1266 * ( +x, -x, +y, -y) and subtract from the original.
1267 * The result looks slightly better than in Windows :)
1269 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1273 RGNOBJ* destObj = NULL;
1274 RGNOBJ *srcObj = GDI_GetObjPtr( hSrc, OBJ_REGION );
1276 tmprgn.rects = NULL;
1277 if (!srcObj) return FALSE;
1278 if (srcObj->rgn.numRects != 0)
1280 if (!(destObj = GDI_GetObjPtr( hDest, OBJ_REGION ))) goto done;
1281 if (!init_region( &tmprgn, srcObj->rgn.numRects )) goto done;
1283 if (!REGION_OffsetRegion( &destObj->rgn, &srcObj->rgn, -x, 0)) goto done;
1284 if (!REGION_OffsetRegion( &tmprgn, &srcObj->rgn, x, 0)) goto done;
1285 if (!REGION_IntersectRegion( &destObj->rgn, &destObj->rgn, &tmprgn )) goto done;
1286 if (!REGION_OffsetRegion( &tmprgn, &srcObj->rgn, 0, -y)) goto done;
1287 if (!REGION_IntersectRegion( &destObj->rgn, &destObj->rgn, &tmprgn )) goto done;
1288 if (!REGION_OffsetRegion( &tmprgn, &srcObj->rgn, 0, y)) goto done;
1289 if (!REGION_IntersectRegion( &destObj->rgn, &destObj->rgn, &tmprgn )) goto done;
1290 if (!REGION_SubtractRegion( &destObj->rgn, &srcObj->rgn, &destObj->rgn )) goto done;
1294 HeapFree( GetProcessHeap(), 0, tmprgn.rects );
1295 if (destObj) GDI_ReleaseObj ( hDest );
1296 GDI_ReleaseObj( hSrc );
1301 /***********************************************************************
1302 * CombineRgn (GDI32.@)
1304 * Combines two regions with the specified operation and stores the result
1305 * in the specified destination region.
1308 * hDest [I] The region that receives the combined result.
1309 * hSrc1 [I] The first source region.
1310 * hSrc2 [I] The second source region.
1311 * mode [I] The way in which the source regions will be combined. See notes.
1315 * NULLREGION - The new region is empty.
1316 * SIMPLEREGION - The new region can be represented by one rectangle.
1317 * COMPLEXREGION - The new region can only be represented by more than
1322 * The two source regions can be the same region.
1323 * The mode can be one of the following:
1324 *| RGN_AND - Intersection of the regions
1325 *| RGN_OR - Union of the regions
1326 *| RGN_XOR - Unions of the regions minus any intersection.
1327 *| RGN_DIFF - Difference (subtraction) of the regions.
1329 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1331 RGNOBJ *destObj = GDI_GetObjPtr( hDest, OBJ_REGION );
1334 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1337 RGNOBJ *src1Obj = GDI_GetObjPtr( hSrc1, OBJ_REGION );
1341 TRACE("dump src1Obj:\n");
1342 if(TRACE_ON(region))
1343 REGION_DumpRegion(&src1Obj->rgn);
1344 if (mode == RGN_COPY)
1346 if (REGION_CopyRegion( &destObj->rgn, &src1Obj->rgn ))
1347 result = get_region_type( destObj );
1351 RGNOBJ *src2Obj = GDI_GetObjPtr( hSrc2, OBJ_REGION );
1355 TRACE("dump src2Obj:\n");
1356 if(TRACE_ON(region))
1357 REGION_DumpRegion(&src2Obj->rgn);
1361 if (REGION_IntersectRegion( &destObj->rgn, &src1Obj->rgn, &src2Obj->rgn ))
1362 result = get_region_type( destObj );
1365 if (REGION_UnionRegion( &destObj->rgn, &src1Obj->rgn, &src2Obj->rgn ))
1366 result = get_region_type( destObj );
1369 if (REGION_XorRegion( &destObj->rgn, &src1Obj->rgn, &src2Obj->rgn ))
1370 result = get_region_type( destObj );
1373 if (REGION_SubtractRegion( &destObj->rgn, &src1Obj->rgn, &src2Obj->rgn ))
1374 result = get_region_type( destObj );
1377 GDI_ReleaseObj( hSrc2 );
1380 GDI_ReleaseObj( hSrc1 );
1382 TRACE("dump destObj:\n");
1383 if(TRACE_ON(region))
1384 REGION_DumpRegion(&destObj->rgn);
1386 GDI_ReleaseObj( hDest );
1391 /***********************************************************************
1393 * Re-calculate the extents of a region
1395 static void REGION_SetExtents (WINEREGION *pReg)
1397 RECT *pRect, *pRectEnd, *pExtents;
1399 if (pReg->numRects == 0)
1401 pReg->extents.left = 0;
1402 pReg->extents.top = 0;
1403 pReg->extents.right = 0;
1404 pReg->extents.bottom = 0;
1408 pExtents = &pReg->extents;
1409 pRect = pReg->rects;
1410 pRectEnd = &pRect[pReg->numRects - 1];
1413 * Since pRect is the first rectangle in the region, it must have the
1414 * smallest top and since pRectEnd is the last rectangle in the region,
1415 * it must have the largest bottom, because of banding. Initialize left and
1416 * right from pRect and pRectEnd, resp., as good things to initialize them
1419 pExtents->left = pRect->left;
1420 pExtents->top = pRect->top;
1421 pExtents->right = pRectEnd->right;
1422 pExtents->bottom = pRectEnd->bottom;
1424 while (pRect <= pRectEnd)
1426 if (pRect->left < pExtents->left)
1427 pExtents->left = pRect->left;
1428 if (pRect->right > pExtents->right)
1429 pExtents->right = pRect->right;
1434 /***********************************************************************
1437 static BOOL REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1439 if (dst != src) /* don't want to copy to itself */
1441 if (dst->size < src->numRects)
1443 RECT *rects = HeapReAlloc( GetProcessHeap(), 0, dst->rects, src->numRects * sizeof(RECT) );
1444 if (!rects) return FALSE;
1446 dst->size = src->numRects;
1448 dst->numRects = src->numRects;
1449 dst->extents.left = src->extents.left;
1450 dst->extents.top = src->extents.top;
1451 dst->extents.right = src->extents.right;
1452 dst->extents.bottom = src->extents.bottom;
1453 memcpy(dst->rects, src->rects, src->numRects * sizeof(RECT));
1458 /***********************************************************************
1459 * REGION_MirrorRegion
1461 static BOOL REGION_MirrorRegion( WINEREGION *dst, WINEREGION *src, int width )
1465 RECT *rects = HeapAlloc( GetProcessHeap(), 0, src->numRects * sizeof(RECT) );
1467 if (!rects) return FALSE;
1469 extents.left = width - src->extents.right;
1470 extents.right = width - src->extents.left;
1471 extents.top = src->extents.top;
1472 extents.bottom = src->extents.bottom;
1474 for (start = 0; start < src->numRects; start = end)
1476 /* find the end of the current band */
1477 for (end = start + 1; end < src->numRects; end++)
1478 if (src->rects[end].top != src->rects[end - 1].top) break;
1480 for (i = 0; i < end - start; i++)
1482 rects[start + i].left = width - src->rects[end - i - 1].right;
1483 rects[start + i].right = width - src->rects[end - i - 1].left;
1484 rects[start + i].top = src->rects[end - i - 1].top;
1485 rects[start + i].bottom = src->rects[end - i - 1].bottom;
1489 HeapFree( GetProcessHeap(), 0, dst->rects );
1491 dst->size = src->numRects;
1492 dst->numRects = src->numRects;
1493 dst->extents = extents;
1497 /***********************************************************************
1500 INT mirror_region( HRGN dst, HRGN src, INT width )
1502 RGNOBJ *src_rgn, *dst_rgn;
1505 if (!(src_rgn = GDI_GetObjPtr( src, OBJ_REGION ))) return ERROR;
1506 if ((dst_rgn = GDI_GetObjPtr( dst, OBJ_REGION )))
1508 if (REGION_MirrorRegion( &dst_rgn->rgn, &src_rgn->rgn, width )) ret = get_region_type( dst_rgn );
1509 GDI_ReleaseObj( dst_rgn );
1511 GDI_ReleaseObj( src_rgn );
1515 /***********************************************************************
1516 * MirrorRgn (GDI32.@)
1518 BOOL WINAPI MirrorRgn( HWND hwnd, HRGN hrgn )
1520 static const WCHAR user32W[] = {'u','s','e','r','3','2','.','d','l','l',0};
1521 static BOOL (WINAPI *pGetWindowRect)( HWND hwnd, LPRECT rect );
1524 /* yes, a HWND in gdi32, don't ask */
1525 if (!pGetWindowRect)
1527 HMODULE user32 = GetModuleHandleW(user32W);
1528 if (!user32) return FALSE;
1529 if (!(pGetWindowRect = (void *)GetProcAddress( user32, "GetWindowRect" ))) return FALSE;
1531 pGetWindowRect( hwnd, &rect );
1532 return mirror_region( hrgn, hrgn, rect.right - rect.left ) != ERROR;
1536 /***********************************************************************
1539 * Attempt to merge the rects in the current band with those in the
1540 * previous one. Used only by REGION_RegionOp.
1543 * The new index for the previous band.
1546 * If coalescing takes place:
1547 * - rectangles in the previous band will have their bottom fields
1549 * - pReg->numRects will be decreased.
1552 static INT REGION_Coalesce (
1553 WINEREGION *pReg, /* Region to coalesce */
1554 INT prevStart, /* Index of start of previous band */
1555 INT curStart /* Index of start of current band */
1557 RECT *pPrevRect; /* Current rect in previous band */
1558 RECT *pCurRect; /* Current rect in current band */
1559 RECT *pRegEnd; /* End of region */
1560 INT curNumRects; /* Number of rectangles in current band */
1561 INT prevNumRects; /* Number of rectangles in previous band */
1562 INT bandtop; /* top coordinate for current band */
1564 pRegEnd = &pReg->rects[pReg->numRects];
1566 pPrevRect = &pReg->rects[prevStart];
1567 prevNumRects = curStart - prevStart;
1570 * Figure out how many rectangles are in the current band. Have to do
1571 * this because multiple bands could have been added in REGION_RegionOp
1572 * at the end when one region has been exhausted.
1574 pCurRect = &pReg->rects[curStart];
1575 bandtop = pCurRect->top;
1576 for (curNumRects = 0;
1577 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1583 if (pCurRect != pRegEnd)
1586 * If more than one band was added, we have to find the start
1587 * of the last band added so the next coalescing job can start
1588 * at the right place... (given when multiple bands are added,
1589 * this may be pointless -- see above).
1592 while (pRegEnd[-1].top == pRegEnd->top)
1596 curStart = pRegEnd - pReg->rects;
1597 pRegEnd = pReg->rects + pReg->numRects;
1600 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1601 pCurRect -= curNumRects;
1603 * The bands may only be coalesced if the bottom of the previous
1604 * matches the top scanline of the current.
1606 if (pPrevRect->bottom == pCurRect->top)
1609 * Make sure the bands have rects in the same places. This
1610 * assumes that rects have been added in such a way that they
1611 * cover the most area possible. I.e. two rects in a band must
1612 * have some horizontal space between them.
1616 if ((pPrevRect->left != pCurRect->left) ||
1617 (pPrevRect->right != pCurRect->right))
1620 * The bands don't line up so they can't be coalesced.
1627 } while (prevNumRects != 0);
1629 pReg->numRects -= curNumRects;
1630 pCurRect -= curNumRects;
1631 pPrevRect -= curNumRects;
1634 * The bands may be merged, so set the bottom of each rect
1635 * in the previous band to that of the corresponding rect in
1640 pPrevRect->bottom = pCurRect->bottom;
1644 } while (curNumRects != 0);
1647 * If only one band was added to the region, we have to backup
1648 * curStart to the start of the previous band.
1650 * If more than one band was added to the region, copy the
1651 * other bands down. The assumption here is that the other bands
1652 * came from the same region as the current one and no further
1653 * coalescing can be done on them since it's all been done
1654 * already... curStart is already in the right place.
1656 if (pCurRect == pRegEnd)
1658 curStart = prevStart;
1664 *pPrevRect++ = *pCurRect++;
1665 } while (pCurRect != pRegEnd);
1673 /***********************************************************************
1676 * Apply an operation to two regions. Called by REGION_Union,
1677 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1683 * The new region is overwritten.
1686 * The idea behind this function is to view the two regions as sets.
1687 * Together they cover a rectangle of area that this function divides
1688 * into horizontal bands where points are covered only by one region
1689 * or by both. For the first case, the nonOverlapFunc is called with
1690 * each the band and the band's upper and lower extents. For the
1691 * second, the overlapFunc is called to process the entire band. It
1692 * is responsible for clipping the rectangles in the band, though
1693 * this function provides the boundaries.
1694 * At the end of each band, the new region is coalesced, if possible,
1695 * to reduce the number of rectangles in the region.
1698 static BOOL REGION_RegionOp(
1699 WINEREGION *destReg, /* Place to store result */
1700 WINEREGION *reg1, /* First region in operation */
1701 WINEREGION *reg2, /* 2nd region in operation */
1702 BOOL (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1703 BOOL (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1704 BOOL (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1707 RECT *r1; /* Pointer into first region */
1708 RECT *r2; /* Pointer into 2d region */
1709 RECT *r1End; /* End of 1st region */
1710 RECT *r2End; /* End of 2d region */
1711 INT ybot; /* Bottom of intersection */
1712 INT ytop; /* Top of intersection */
1713 INT prevBand; /* Index of start of
1714 * previous band in newReg */
1715 INT curBand; /* Index of start of current
1717 RECT *r1BandEnd; /* End of current band in r1 */
1718 RECT *r2BandEnd; /* End of current band in r2 */
1719 INT top; /* Top of non-overlapping band */
1720 INT bot; /* Bottom of non-overlapping band */
1724 * set r1, r2, r1End and r2End appropriately, preserve the important
1725 * parts of the destination region until the end in case it's one of
1726 * the two source regions, then mark the "new" region empty, allocating
1727 * another array of rectangles for it to use.
1731 r1End = r1 + reg1->numRects;
1732 r2End = r2 + reg2->numRects;
1735 * Allocate a reasonable number of rectangles for the new region. The idea
1736 * is to allocate enough so the individual functions don't need to
1737 * reallocate and copy the array, which is time consuming, yet we don't
1738 * have to worry about using too much memory. I hope to be able to
1739 * nuke the Xrealloc() at the end of this function eventually.
1741 if (!init_region( &newReg, max(reg1->numRects,reg2->numRects) * 2 )) return FALSE;
1744 * Initialize ybot and ytop.
1745 * In the upcoming loop, ybot and ytop serve different functions depending
1746 * on whether the band being handled is an overlapping or non-overlapping
1748 * In the case of a non-overlapping band (only one of the regions
1749 * has points in the band), ybot is the bottom of the most recent
1750 * intersection and thus clips the top of the rectangles in that band.
1751 * ytop is the top of the next intersection between the two regions and
1752 * serves to clip the bottom of the rectangles in the current band.
1753 * For an overlapping band (where the two regions intersect), ytop clips
1754 * the top of the rectangles of both regions and ybot clips the bottoms.
1756 if (reg1->extents.top < reg2->extents.top)
1757 ybot = reg1->extents.top;
1759 ybot = reg2->extents.top;
1762 * prevBand serves to mark the start of the previous band so rectangles
1763 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1764 * In the beginning, there is no previous band, so prevBand == curBand
1765 * (curBand is set later on, of course, but the first band will always
1766 * start at index 0). prevBand and curBand must be indices because of
1767 * the possible expansion, and resultant moving, of the new region's
1768 * array of rectangles.
1774 curBand = newReg.numRects;
1777 * This algorithm proceeds one source-band (as opposed to a
1778 * destination band, which is determined by where the two regions
1779 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1780 * rectangle after the last one in the current band for their
1781 * respective regions.
1784 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1790 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1796 * First handle the band that doesn't intersect, if any.
1798 * Note that attention is restricted to one band in the
1799 * non-intersecting region at once, so if a region has n
1800 * bands between the current position and the next place it overlaps
1801 * the other, this entire loop will be passed through n times.
1803 if (r1->top < r2->top)
1805 top = max(r1->top,ybot);
1806 bot = min(r1->bottom,r2->top);
1808 if ((top != bot) && (nonOverlap1Func != NULL))
1810 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, top, bot)) return FALSE;
1815 else if (r2->top < r1->top)
1817 top = max(r2->top,ybot);
1818 bot = min(r2->bottom,r1->top);
1820 if ((top != bot) && (nonOverlap2Func != NULL))
1822 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, top, bot)) return FALSE;
1833 * If any rectangles got added to the region, try and coalesce them
1834 * with rectangles from the previous band. Note we could just do
1835 * this test in miCoalesce, but some machines incur a not
1836 * inconsiderable cost for function calls, so...
1838 if (newReg.numRects != curBand)
1840 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1844 * Now see if we've hit an intersecting band. The two bands only
1845 * intersect if ybot > ytop
1847 ybot = min(r1->bottom, r2->bottom);
1848 curBand = newReg.numRects;
1851 if (!overlapFunc(&newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot)) return FALSE;
1854 if (newReg.numRects != curBand)
1856 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1860 * If we've finished with a band (bottom == ybot) we skip forward
1861 * in the region to the next band.
1863 if (r1->bottom == ybot)
1867 if (r2->bottom == ybot)
1871 } while ((r1 != r1End) && (r2 != r2End));
1874 * Deal with whichever region still has rectangles left.
1876 curBand = newReg.numRects;
1879 if (nonOverlap1Func != NULL)
1884 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1888 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom))
1891 } while (r1 != r1End);
1894 else if ((r2 != r2End) && (nonOverlap2Func != NULL))
1899 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1903 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom))
1906 } while (r2 != r2End);
1909 if (newReg.numRects != curBand)
1911 REGION_Coalesce (&newReg, prevBand, curBand);
1915 * A bit of cleanup. To keep regions from growing without bound,
1916 * we shrink the array of rectangles to match the new number of
1917 * rectangles in the region. This never goes to 0, however...
1919 * Only do this stuff if the number of rectangles allocated is more than
1920 * twice the number of rectangles in the region (a simple optimization...).
1922 if ((newReg.numRects < (newReg.size >> 1)) && (newReg.numRects > 2))
1924 RECT *new_rects = HeapReAlloc( GetProcessHeap(), 0, newReg.rects, newReg.numRects * sizeof(RECT) );
1927 newReg.rects = new_rects;
1928 newReg.size = newReg.numRects;
1931 HeapFree( GetProcessHeap(), 0, destReg->rects );
1932 destReg->rects = newReg.rects;
1933 destReg->size = newReg.size;
1934 destReg->numRects = newReg.numRects;
1938 /***********************************************************************
1939 * Region Intersection
1940 ***********************************************************************/
1943 /***********************************************************************
1946 * Handle an overlapping band for REGION_Intersect.
1952 * Rectangles may be added to the region.
1955 static BOOL REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1956 RECT *r2, RECT *r2End, INT top, INT bottom)
1961 while ((r1 != r1End) && (r2 != r2End))
1963 left = max(r1->left, r2->left);
1964 right = min(r1->right, r2->right);
1967 * If there's any overlap between the two rectangles, add that
1968 * overlap to the new region.
1969 * There's no need to check for subsumption because the only way
1970 * such a need could arise is if some region has two rectangles
1971 * right next to each other. Since that should never happen...
1975 if (!add_rect( pReg, left, top, right, bottom )) return FALSE;
1979 * Need to advance the pointers. Shift the one that extends
1980 * to the right the least, since the other still has a chance to
1981 * overlap with that region's next rectangle, if you see what I mean.
1983 if (r1->right < r2->right)
1987 else if (r2->right < r1->right)
2000 /***********************************************************************
2001 * REGION_IntersectRegion
2003 static BOOL REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
2006 /* check for trivial reject */
2007 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
2008 (!EXTENTCHECK(®1->extents, ®2->extents)))
2009 newReg->numRects = 0;
2011 if (!REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL)) return FALSE;
2014 * Can't alter newReg's extents before we call miRegionOp because
2015 * it might be one of the source regions and miRegionOp depends
2016 * on the extents of those regions being the same. Besides, this
2017 * way there's no checking against rectangles that will be nuked
2018 * due to coalescing, so we have to examine fewer rectangles.
2020 REGION_SetExtents(newReg);
2024 /***********************************************************************
2026 ***********************************************************************/
2028 /***********************************************************************
2031 * Handle a non-overlapping band for the union operation. Just
2032 * Adds the rectangles into the region. Doesn't have to check for
2033 * subsumption or anything.
2039 * pReg->numRects is incremented and the final rectangles overwritten
2040 * with the rectangles we're passed.
2043 static BOOL REGION_UnionNonO(WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2047 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2053 /***********************************************************************
2056 * Handle an overlapping band for the union operation. Picks the
2057 * left-most rectangle each time and merges it into the region.
2063 * Rectangles are overwritten in pReg->rects and pReg->numRects will
2067 static BOOL REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2068 RECT *r2, RECT *r2End, INT top, INT bottom)
2070 #define MERGERECT(r) \
2071 if ((pReg->numRects != 0) && \
2072 (pReg->rects[pReg->numRects-1].top == top) && \
2073 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
2074 (pReg->rects[pReg->numRects-1].right >= r->left)) \
2076 if (pReg->rects[pReg->numRects-1].right < r->right) \
2077 pReg->rects[pReg->numRects-1].right = r->right; \
2081 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2085 while ((r1 != r1End) && (r2 != r2End))
2087 if (r1->left < r2->left)
2102 } while (r1 != r1End);
2104 else while (r2 != r2End)
2112 /***********************************************************************
2113 * REGION_UnionRegion
2115 static BOOL REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1, WINEREGION *reg2)
2119 /* checks all the simple cases */
2122 * Region 1 and 2 are the same or region 1 is empty
2124 if ( (reg1 == reg2) || (!(reg1->numRects)) )
2127 ret = REGION_CopyRegion(newReg, reg2);
2132 * if nothing to union (region 2 empty)
2134 if (!(reg2->numRects))
2137 ret = REGION_CopyRegion(newReg, reg1);
2142 * Region 1 completely subsumes region 2
2144 if ((reg1->numRects == 1) &&
2145 (reg1->extents.left <= reg2->extents.left) &&
2146 (reg1->extents.top <= reg2->extents.top) &&
2147 (reg1->extents.right >= reg2->extents.right) &&
2148 (reg1->extents.bottom >= reg2->extents.bottom))
2151 ret = REGION_CopyRegion(newReg, reg1);
2156 * Region 2 completely subsumes region 1
2158 if ((reg2->numRects == 1) &&
2159 (reg2->extents.left <= reg1->extents.left) &&
2160 (reg2->extents.top <= reg1->extents.top) &&
2161 (reg2->extents.right >= reg1->extents.right) &&
2162 (reg2->extents.bottom >= reg1->extents.bottom))
2165 ret = REGION_CopyRegion(newReg, reg2);
2169 if ((ret = REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO)))
2171 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2172 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2173 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2174 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2179 /***********************************************************************
2180 * Region Subtraction
2181 ***********************************************************************/
2183 /***********************************************************************
2184 * REGION_SubtractNonO1
2186 * Deal with non-overlapping band for subtraction. Any parts from
2187 * region 2 we discard. Anything from region 1 we add to the region.
2193 * pReg may be affected.
2196 static BOOL REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2200 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2207 /***********************************************************************
2210 * Overlapping band subtraction. x1 is the left-most point not yet
2217 * pReg may have rectangles added to it.
2220 static BOOL REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2221 RECT *r2, RECT *r2End, INT top, INT bottom)
2223 INT left = r1->left;
2225 while ((r1 != r1End) && (r2 != r2End))
2227 if (r2->right <= left)
2230 * Subtrahend missed the boat: go to next subtrahend.
2234 else if (r2->left <= left)
2237 * Subtrahend precedes minuend: nuke left edge of minuend.
2240 if (left >= r1->right)
2243 * Minuend completely covered: advance to next minuend and
2244 * reset left fence to edge of new minuend.
2253 * Subtrahend now used up since it doesn't extend beyond
2259 else if (r2->left < r1->right)
2262 * Left part of subtrahend covers part of minuend: add uncovered
2263 * part of minuend to region and skip to next subtrahend.
2265 if (!add_rect( pReg, left, top, r2->left, bottom )) return FALSE;
2267 if (left >= r1->right)
2270 * Minuend used up: advance to new...
2279 * Subtrahend used up
2287 * Minuend used up: add any remaining piece before advancing.
2289 if (r1->right > left)
2291 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2300 * Add remaining minuend rectangles to region.
2304 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2314 /***********************************************************************
2315 * REGION_SubtractRegion
2317 * Subtract regS from regM and leave the result in regD.
2318 * S stands for subtrahend, M for minuend and D for difference.
2324 * regD is overwritten.
2327 static BOOL REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM, WINEREGION *regS )
2329 /* check for trivial reject */
2330 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2331 (!EXTENTCHECK(®M->extents, ®S->extents)) )
2332 return REGION_CopyRegion(regD, regM);
2334 if (!REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL))
2338 * Can't alter newReg's extents before we call miRegionOp because
2339 * it might be one of the source regions and miRegionOp depends
2340 * on the extents of those regions being the unaltered. Besides, this
2341 * way there's no checking against rectangles that will be nuked
2342 * due to coalescing, so we have to examine fewer rectangles.
2344 REGION_SetExtents (regD);
2348 /***********************************************************************
2351 static BOOL REGION_XorRegion(WINEREGION *dr, WINEREGION *sra, WINEREGION *srb)
2353 WINEREGION tra, trb;
2356 if (!init_region( &tra, sra->numRects + 1 )) return FALSE;
2357 if ((ret = init_region( &trb, srb->numRects + 1 )))
2359 ret = REGION_SubtractRegion(&tra,sra,srb) &&
2360 REGION_SubtractRegion(&trb,srb,sra) &&
2361 REGION_UnionRegion(dr,&tra,&trb);
2362 destroy_region(&trb);
2364 destroy_region(&tra);
2368 /**************************************************************************
2372 *************************************************************************/
2374 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2375 #define SMALL_COORDINATE 0x80000000
2377 /***********************************************************************
2378 * REGION_InsertEdgeInET
2380 * Insert the given edge into the edge table.
2381 * First we must find the correct bucket in the
2382 * Edge table, then find the right slot in the
2383 * bucket. Finally, we can insert it.
2386 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2387 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2390 EdgeTableEntry *start, *prev;
2391 ScanLineList *pSLL, *pPrevSLL;
2392 ScanLineListBlock *tmpSLLBlock;
2395 * find the right bucket to put the edge into
2397 pPrevSLL = &ET->scanlines;
2398 pSLL = pPrevSLL->next;
2399 while (pSLL && (pSLL->scanline < scanline))
2406 * reassign pSLL (pointer to ScanLineList) if necessary
2408 if ((!pSLL) || (pSLL->scanline > scanline))
2410 if (*iSLLBlock > SLLSPERBLOCK-1)
2412 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2415 WARN("Can't alloc SLLB\n");
2418 (*SLLBlock)->next = tmpSLLBlock;
2419 tmpSLLBlock->next = NULL;
2420 *SLLBlock = tmpSLLBlock;
2423 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2425 pSLL->next = pPrevSLL->next;
2426 pSLL->edgelist = NULL;
2427 pPrevSLL->next = pSLL;
2429 pSLL->scanline = scanline;
2432 * now insert the edge in the right bucket
2435 start = pSLL->edgelist;
2436 while (start && (start->bres.minor_axis < ETE->bres.minor_axis))
2439 start = start->next;
2446 pSLL->edgelist = ETE;
2449 /***********************************************************************
2450 * REGION_CreateEdgeTable
2452 * This routine creates the edge table for
2453 * scan converting polygons.
2454 * The Edge Table (ET) looks like:
2458 * | ymax | ScanLineLists
2459 * |scanline|-->------------>-------------->...
2460 * -------- |scanline| |scanline|
2461 * |edgelist| |edgelist|
2462 * --------- ---------
2466 * list of ETEs list of ETEs
2468 * where ETE is an EdgeTableEntry data structure,
2469 * and there is one ScanLineList per scanline at
2470 * which an edge is initially entered.
2473 static void REGION_CreateETandAET(const INT *Count, INT nbpolygons,
2474 const POINT *pts, EdgeTable *ET, EdgeTableEntry *AET,
2475 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
2477 const POINT *top, *bottom;
2478 const POINT *PrevPt, *CurrPt, *EndPt;
2485 * initialize the Active Edge Table
2489 AET->nextWETE = NULL;
2490 AET->bres.minor_axis = SMALL_COORDINATE;
2493 * initialize the Edge Table.
2495 ET->scanlines.next = NULL;
2496 ET->ymax = SMALL_COORDINATE;
2497 ET->ymin = LARGE_COORDINATE;
2498 pSLLBlock->next = NULL;
2501 for(poly = 0; poly < nbpolygons; poly++)
2503 count = Count[poly];
2511 * for each vertex in the array of points.
2512 * In this loop we are dealing with two vertices at
2513 * a time -- these make up one edge of the polygon.
2520 * find out which point is above and which is below.
2522 if (PrevPt->y > CurrPt->y)
2524 bottom = PrevPt, top = CurrPt;
2525 pETEs->ClockWise = 0;
2529 bottom = CurrPt, top = PrevPt;
2530 pETEs->ClockWise = 1;
2534 * don't add horizontal edges to the Edge table.
2536 if (bottom->y != top->y)
2538 pETEs->ymax = bottom->y-1;
2539 /* -1 so we don't get last scanline */
2542 * initialize integer edge algorithm
2544 dy = bottom->y - top->y;
2545 BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
2547 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
2550 if (PrevPt->y > ET->ymax)
2551 ET->ymax = PrevPt->y;
2552 if (PrevPt->y < ET->ymin)
2553 ET->ymin = PrevPt->y;
2562 /***********************************************************************
2565 * This routine moves EdgeTableEntries from the
2566 * EdgeTable into the Active Edge Table,
2567 * leaving them sorted by smaller x coordinate.
2570 static void REGION_loadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs)
2572 EdgeTableEntry *pPrevAET;
2573 EdgeTableEntry *tmp;
2579 while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis))
2588 ETEs->back = pPrevAET;
2589 pPrevAET->next = ETEs;
2596 /***********************************************************************
2597 * REGION_computeWAET
2599 * This routine links the AET by the
2600 * nextWETE (winding EdgeTableEntry) link for
2601 * use by the winding number rule. The final
2602 * Active Edge Table (AET) might look something
2606 * ---------- --------- ---------
2607 * |ymax | |ymax | |ymax |
2608 * | ... | |... | |... |
2609 * |next |->|next |->|next |->...
2610 * |nextWETE| |nextWETE| |nextWETE|
2611 * --------- --------- ^--------
2613 * V-------------------> V---> ...
2616 static void REGION_computeWAET(EdgeTableEntry *AET)
2618 register EdgeTableEntry *pWETE;
2619 register int inside = 1;
2620 register int isInside = 0;
2622 AET->nextWETE = NULL;
2632 if ((!inside && !isInside) ||
2633 ( inside && isInside))
2635 pWETE->nextWETE = AET;
2641 pWETE->nextWETE = NULL;
2644 /***********************************************************************
2645 * REGION_InsertionSort
2647 * Just a simple insertion sort using
2648 * pointers and back pointers to sort the Active
2652 static BOOL REGION_InsertionSort(EdgeTableEntry *AET)
2654 EdgeTableEntry *pETEchase;
2655 EdgeTableEntry *pETEinsert;
2656 EdgeTableEntry *pETEchaseBackTMP;
2657 BOOL changed = FALSE;
2664 while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis)
2665 pETEchase = pETEchase->back;
2668 if (pETEchase != pETEinsert)
2670 pETEchaseBackTMP = pETEchase->back;
2671 pETEinsert->back->next = AET;
2673 AET->back = pETEinsert->back;
2674 pETEinsert->next = pETEchase;
2675 pETEchase->back->next = pETEinsert;
2676 pETEchase->back = pETEinsert;
2677 pETEinsert->back = pETEchaseBackTMP;
2684 /***********************************************************************
2685 * REGION_FreeStorage
2689 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2691 ScanLineListBlock *tmpSLLBlock;
2695 tmpSLLBlock = pSLLBlock->next;
2696 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2697 pSLLBlock = tmpSLLBlock;
2702 /***********************************************************************
2703 * REGION_PtsToRegion
2705 * Create an array of rectangles from a list of points.
2707 static BOOL REGION_PtsToRegion(int numFullPtBlocks, int iCurPtBlock,
2708 POINTBLOCK *FirstPtBlock, WINEREGION *reg)
2712 POINTBLOCK *CurPtBlock;
2717 extents = ®->extents;
2719 numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1;
2720 if (!init_region( reg, numRects )) return FALSE;
2722 reg->size = numRects;
2723 CurPtBlock = FirstPtBlock;
2724 rects = reg->rects - 1;
2726 extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
2728 for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) {
2729 /* the loop uses 2 points per iteration */
2730 i = NUMPTSTOBUFFER >> 1;
2731 if (!numFullPtBlocks)
2732 i = iCurPtBlock >> 1;
2733 for (pts = CurPtBlock->pts; i--; pts += 2) {
2734 if (pts->x == pts[1].x)
2736 if (numRects && pts->x == rects->left && pts->y == rects->bottom &&
2737 pts[1].x == rects->right &&
2738 (numRects == 1 || rects[-1].top != rects->top) &&
2739 (i && pts[2].y > pts[1].y)) {
2740 rects->bottom = pts[1].y + 1;
2745 rects->left = pts->x; rects->top = pts->y;
2746 rects->right = pts[1].x; rects->bottom = pts[1].y + 1;
2747 if (rects->left < extents->left)
2748 extents->left = rects->left;
2749 if (rects->right > extents->right)
2750 extents->right = rects->right;
2752 CurPtBlock = CurPtBlock->next;
2756 extents->top = reg->rects->top;
2757 extents->bottom = rects->bottom;
2762 extents->bottom = 0;
2764 reg->numRects = numRects;
2769 /***********************************************************************
2770 * CreatePolyPolygonRgn (GDI32.@)
2772 HRGN WINAPI CreatePolyPolygonRgn(const POINT *Pts, const INT *Count,
2773 INT nbpolygons, INT mode)
2777 EdgeTableEntry *pAET; /* Active Edge Table */
2778 INT y; /* current scanline */
2779 int iPts = 0; /* number of pts in buffer */
2780 EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/
2781 ScanLineList *pSLL; /* current scanLineList */
2782 POINT *pts; /* output buffer */
2783 EdgeTableEntry *pPrevAET; /* ptr to previous AET */
2784 EdgeTable ET; /* header node for ET */
2785 EdgeTableEntry AET; /* header node for AET */
2786 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2787 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2788 int fixWAET = FALSE;
2789 POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */
2790 POINTBLOCK *tmpPtBlock;
2791 int numFullPtBlocks = 0;
2794 TRACE("%p, count %d, polygons %d, mode %d\n", Pts, *Count, nbpolygons, mode);
2796 /* special case a rectangle */
2798 if (((nbpolygons == 1) && ((*Count == 4) ||
2799 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2800 (((Pts[0].y == Pts[1].y) &&
2801 (Pts[1].x == Pts[2].x) &&
2802 (Pts[2].y == Pts[3].y) &&
2803 (Pts[3].x == Pts[0].x)) ||
2804 ((Pts[0].x == Pts[1].x) &&
2805 (Pts[1].y == Pts[2].y) &&
2806 (Pts[2].x == Pts[3].x) &&
2807 (Pts[3].y == Pts[0].y))))
2808 return CreateRectRgn( min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2809 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2811 for(poly = total = 0; poly < nbpolygons; poly++)
2812 total += Count[poly];
2813 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2816 pts = FirstPtBlock.pts;
2817 REGION_CreateETandAET(Count, nbpolygons, Pts, &ET, &AET, pETEs, &SLLBlock);
2818 pSLL = ET.scanlines.next;
2819 curPtBlock = &FirstPtBlock;
2821 if (mode != WINDING) {
2825 for (y = ET.ymin; y < ET.ymax; y++) {
2827 * Add a new edge to the active edge table when we
2828 * get to the next edge.
2830 if (pSLL != NULL && y == pSLL->scanline) {
2831 REGION_loadAET(&AET, pSLL->edgelist);
2838 * for each active edge
2841 pts->x = pAET->bres.minor_axis, pts->y = y;
2845 * send out the buffer
2847 if (iPts == NUMPTSTOBUFFER) {
2848 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK));
2849 if(!tmpPtBlock) goto done;
2850 curPtBlock->next = tmpPtBlock;
2851 curPtBlock = tmpPtBlock;
2852 pts = curPtBlock->pts;
2856 EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
2858 REGION_InsertionSort(&AET);
2865 for (y = ET.ymin; y < ET.ymax; y++) {
2867 * Add a new edge to the active edge table when we
2868 * get to the next edge.
2870 if (pSLL != NULL && y == pSLL->scanline) {
2871 REGION_loadAET(&AET, pSLL->edgelist);
2872 REGION_computeWAET(&AET);
2880 * for each active edge
2884 * add to the buffer only those edges that
2885 * are in the Winding active edge table.
2887 if (pWETE == pAET) {
2888 pts->x = pAET->bres.minor_axis, pts->y = y;
2892 * send out the buffer
2894 if (iPts == NUMPTSTOBUFFER) {
2895 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0,
2896 sizeof(POINTBLOCK) );
2897 if(!tmpPtBlock) goto done;
2898 curPtBlock->next = tmpPtBlock;
2899 curPtBlock = tmpPtBlock;
2900 pts = curPtBlock->pts;
2904 pWETE = pWETE->nextWETE;
2906 EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
2910 * recompute the winding active edge table if
2911 * we just resorted or have exited an edge.
2913 if (REGION_InsertionSort(&AET) || fixWAET) {
2914 REGION_computeWAET(&AET);
2920 if (!(obj = HeapAlloc( GetProcessHeap(), 0, sizeof(*obj) ))) goto done;
2922 if (!REGION_PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, &obj->rgn))
2924 HeapFree( GetProcessHeap(), 0, obj );
2927 if (!(hrgn = alloc_gdi_handle( &obj->header, OBJ_REGION, ®ion_funcs )))
2929 HeapFree( GetProcessHeap(), 0, obj->rgn.rects );
2930 HeapFree( GetProcessHeap(), 0, obj );
2934 REGION_FreeStorage(SLLBlock.next);
2935 for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) {
2936 tmpPtBlock = curPtBlock->next;
2937 HeapFree( GetProcessHeap(), 0, curPtBlock );
2938 curPtBlock = tmpPtBlock;
2940 HeapFree( GetProcessHeap(), 0, pETEs );
2945 /***********************************************************************
2946 * CreatePolygonRgn (GDI32.@)
2948 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count,
2951 return CreatePolyPolygonRgn( points, &count, 1, mode );