ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / gdi32 / region.c
blobe9bed1802352d67da0fa05de48db50122500915e
1 /*
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
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., 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.
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 <stdarg.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include "windef.h"
101 #include "winbase.h"
102 #include "wingdi.h"
103 #include "gdi_private.h"
104 #include "wine/debug.h"
106 WINE_DEFAULT_DEBUG_CHANNEL(region);
109 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc );
110 static BOOL REGION_DeleteObject( HGDIOBJ handle );
112 static const struct gdi_obj_funcs region_funcs =
114 REGION_SelectObject, /* pSelectObject */
115 NULL, /* pGetObjectA */
116 NULL, /* pGetObjectW */
117 NULL, /* pUnrealizeObject */
118 REGION_DeleteObject /* pDeleteObject */
121 /* Check if two RECTs overlap. */
122 static inline BOOL overlapping( const RECT *r1, const RECT *r2 )
124 return (r1->right > r2->left && r1->left < r2->right &&
125 r1->bottom > r2->top && r1->top < r2->bottom);
128 static BOOL grow_region( WINEREGION *rgn, int size )
130 RECT *new_rects;
132 if (size <= rgn->size) return TRUE;
134 if (rgn->rects == rgn->rects_buf)
136 new_rects = HeapAlloc( GetProcessHeap(), 0, size * sizeof(RECT) );
137 if (!new_rects) return FALSE;
138 memcpy( new_rects, rgn->rects, rgn->numRects * sizeof(RECT) );
140 else
142 new_rects = HeapReAlloc( GetProcessHeap(), 0, rgn->rects, size * sizeof(RECT) );
143 if (!new_rects) return FALSE;
145 rgn->rects = new_rects;
146 rgn->size = size;
147 return TRUE;
150 static BOOL add_rect( WINEREGION *reg, INT left, INT top, INT right, INT bottom )
152 RECT *rect;
153 if (reg->numRects >= reg->size && !grow_region( reg, 2 * reg->size ))
154 return FALSE;
156 rect = reg->rects + reg->numRects++;
157 rect->left = left;
158 rect->top = top;
159 rect->right = right;
160 rect->bottom = bottom;
161 return TRUE;
164 static inline void empty_region( WINEREGION *reg )
166 reg->numRects = 0;
167 reg->extents.left = reg->extents.top = reg->extents.right = reg->extents.bottom = 0;
170 static inline BOOL is_in_rect( const RECT *rect, int x, int y )
172 return (rect->right > x && rect->left <= x && rect->bottom > y && rect->top <= y);
176 * used to allocate buffers for points and link
177 * the buffers together
180 struct point_block
182 int count, size;
183 struct point_block *next;
184 POINT pts[1]; /* Variable sized array - must be last. */
187 static struct point_block *add_point( struct point_block *block, int x, int y )
189 if (block->count == block->size)
191 struct point_block *new;
192 int size = block->size * 2;
193 new = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( struct point_block, pts[size] ) );
194 if (!new) return NULL;
195 block->next = new;
196 new->count = 0;
197 new->size = size;
198 new->next = NULL;
199 block = new;
201 block->pts[block->count].x = x;
202 block->pts[block->count].y = y;
203 block->count++;
204 return block;
207 static void free_point_blocks( struct point_block *block )
209 while (block)
211 struct point_block *tmp = block->next;
212 HeapFree( GetProcessHeap(), 0, block );
213 block = tmp;
219 * This file contains a few macros to help track
220 * the edge of a filled object. The object is assumed
221 * to be filled in scanline order, and thus the
222 * algorithm used is an extension of Bresenham's line
223 * drawing algorithm which assumes that y is always the
224 * major axis.
225 * Since these pieces of code are the same for any filled shape,
226 * it is more convenient to gather the library in one
227 * place, but since these pieces of code are also in
228 * the inner loops of output primitives, procedure call
229 * overhead is out of the question.
230 * See the author for a derivation if needed.
235 * This structure contains all of the information needed
236 * to run the bresenham algorithm.
237 * The variables may be hardcoded into the declarations
238 * instead of using this structure to make use of
239 * register declarations.
241 struct bres_info
243 INT minor_axis; /* minor axis */
244 INT d; /* decision variable */
245 INT m, m1; /* slope and slope+1 */
246 INT incr1, incr2; /* error increments */
251 * In scan converting polygons, we want to choose those pixels
252 * which are inside the polygon. Thus, we add .5 to the starting
253 * x coordinate for both left and right edges. Now we choose the
254 * first pixel which is inside the pgon for the left edge and the
255 * first pixel which is outside the pgon for the right edge.
256 * Draw the left pixel, but not the right.
258 * How to add .5 to the starting x coordinate:
259 * If the edge is moving to the right, then subtract dy from the
260 * error term from the general form of the algorithm.
261 * If the edge is moving to the left, then add dy to the error term.
263 * The reason for the difference between edges moving to the left
264 * and edges moving to the right is simple: If an edge is moving
265 * to the right, then we want the algorithm to flip immediately.
266 * If it is moving to the left, then we don't want it to flip until
267 * we traverse an entire pixel.
269 static inline void bres_init_polygon( int dy, int x1, int x2, struct bres_info *bres )
271 int dx;
274 * if the edge is horizontal, then it is ignored
275 * and assumed not to be processed. Otherwise, do this stuff.
277 if (!dy) return;
279 bres->minor_axis = x1;
280 dx = x2 - x1;
281 if (dx < 0)
283 bres->m = dx / dy;
284 bres->m1 = bres->m - 1;
285 bres->incr1 = -2 * dx + 2 * dy * bres->m1;
286 bres->incr2 = -2 * dx + 2 * dy * bres->m;
287 bres->d = 2 * bres->m * dy - 2 * dx - 2 * dy;
289 else
291 bres->m = dx / (dy);
292 bres->m1 = bres->m + 1;
293 bres->incr1 = 2 * dx - 2 * dy * bres->m1;
294 bres->incr2 = 2 * dx - 2 * dy * bres->m;
295 bres->d = -2 * bres->m * dy + 2 * dx;
299 static inline void bres_incr_polygon( struct bres_info *bres )
301 if (bres->m1 > 0) {
302 if (bres->d > 0) {
303 bres->minor_axis += bres->m1;
304 bres->d += bres->incr1;
306 else {
307 bres->minor_axis += bres->m;
308 bres->d += bres->incr2;
310 } else {
311 if (bres->d >= 0) {
312 bres->minor_axis += bres->m1;
313 bres->d += bres->incr1;
315 else {
316 bres->minor_axis += bres->m;
317 bres->d += bres->incr2;
324 * These are the data structures needed to scan
325 * convert regions. Two different scan conversion
326 * methods are available -- the even-odd method, and
327 * the winding number method.
328 * The even-odd rule states that a point is inside
329 * the polygon if a ray drawn from that point in any
330 * direction will pass through an odd number of
331 * path segments.
332 * By the winding number rule, a point is decided
333 * to be inside the polygon if a ray drawn from that
334 * point in any direction passes through a different
335 * number of clockwise and counter-clockwise path
336 * segments.
338 * These data structures are adapted somewhat from
339 * the algorithm in (Foley/Van Dam) for scan converting
340 * polygons.
341 * The basic algorithm is to start at the top (smallest y)
342 * of the polygon, stepping down to the bottom of
343 * the polygon by incrementing the y coordinate. We
344 * keep a list of edges which the current scanline crosses,
345 * sorted by x. This list is called the Active Edge Table (AET)
346 * As we change the y-coordinate, we update each entry in
347 * in the active edge table to reflect the edges new xcoord.
348 * This list must be sorted at each scanline in case
349 * two edges intersect.
350 * We also keep a data structure known as the Edge Table (ET),
351 * which keeps track of all the edges which the current
352 * scanline has not yet reached. The ET is basically a
353 * list of ScanLineList structures containing a list of
354 * edges which are entered at a given scanline. There is one
355 * ScanLineList per scanline at which an edge is entered.
356 * When we enter a new edge, we move it from the ET to the AET.
358 * From the AET, we can implement the even-odd rule as in
359 * (Foley/Van Dam).
360 * The winding number rule is a little trickier. We also
361 * keep the EdgeTableEntries in the AET linked by the
362 * nextWETE (winding EdgeTableEntry) link. This allows
363 * the edges to be linked just as before for updating
364 * purposes, but only uses the edges linked by the nextWETE
365 * link as edges representing spans of the polygon to
366 * drawn (as with the even-odd rule).
369 typedef struct edge_table_entry {
370 struct list entry;
371 struct list winding_entry;
372 INT ymax; /* ycoord at which we exit this edge. */
373 struct bres_info bres; /* Bresenham info to run the edge */
374 int ClockWise; /* flag for winding number rule */
375 } EdgeTableEntry;
378 typedef struct _ScanLineList{
379 struct list edgelist;
380 INT scanline; /* the scanline represented */
381 struct _ScanLineList *next; /* next in the list */
382 } ScanLineList;
385 typedef struct {
386 INT ymax; /* ymax for the polygon */
387 INT ymin; /* ymin for the polygon */
388 ScanLineList scanlines; /* header node */
389 } EdgeTable;
393 * Here is a struct to help with storage allocation
394 * so we can allocate a big chunk at a time, and then take
395 * pieces from this heap when we need to.
397 #define SLLSPERBLOCK 25
399 typedef struct _ScanLineListBlock {
400 ScanLineList SLLs[SLLSPERBLOCK];
401 struct _ScanLineListBlock *next;
402 } ScanLineListBlock;
405 /* Note the parameter order is different from the X11 equivalents */
407 static BOOL REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
408 static BOOL REGION_OffsetRegion(WINEREGION *d, WINEREGION *s, INT x, INT y);
409 static BOOL REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
410 static BOOL REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
411 static BOOL REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
412 static BOOL REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
413 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
415 /***********************************************************************
416 * get_region_type
418 static inline INT get_region_type( const WINEREGION *obj )
420 switch(obj->numRects)
422 case 0: return NULLREGION;
423 case 1: return SIMPLEREGION;
424 default: return COMPLEXREGION;
429 /***********************************************************************
430 * REGION_DumpRegion
431 * Outputs the contents of a WINEREGION
433 static void REGION_DumpRegion(WINEREGION *pReg)
435 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
437 TRACE("Region %p: %s %d rects\n", pReg, wine_dbgstr_rect(&pReg->extents), pReg->numRects);
438 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
439 TRACE("\t%s\n", wine_dbgstr_rect(pRect));
440 return;
444 /***********************************************************************
445 * init_region
447 * Initialize a new empty region.
449 static BOOL init_region( WINEREGION *pReg, INT n )
451 n = max( n, RGN_DEFAULT_RECTS );
453 if (n > RGN_DEFAULT_RECTS)
455 if (!(pReg->rects = HeapAlloc( GetProcessHeap(), 0, n * sizeof( RECT ) )))
456 return FALSE;
458 else
459 pReg->rects = pReg->rects_buf;
461 pReg->size = n;
462 empty_region(pReg);
463 return TRUE;
466 /***********************************************************************
467 * destroy_region
469 static void destroy_region( WINEREGION *pReg )
471 if (pReg->rects != pReg->rects_buf)
472 HeapFree( GetProcessHeap(), 0, pReg->rects );
475 /***********************************************************************
476 * free_region
478 static void free_region( WINEREGION *rgn )
480 destroy_region( rgn );
481 HeapFree( GetProcessHeap(), 0, rgn );
484 /***********************************************************************
485 * alloc_region
487 static WINEREGION *alloc_region( INT n )
489 WINEREGION *rgn = HeapAlloc( GetProcessHeap(), 0, sizeof(*rgn) );
491 if (rgn && !init_region( rgn, n ))
493 free_region( rgn );
494 rgn = NULL;
496 return rgn;
499 /************************************************************
500 * move_rects
502 * Move rectangles from src to dst leaving src with no rectangles.
504 static inline void move_rects( WINEREGION *dst, WINEREGION *src )
506 destroy_region( dst );
507 if (src->rects == src->rects_buf)
509 dst->rects = dst->rects_buf;
510 memcpy( dst->rects, src->rects, src->numRects * sizeof(RECT) );
512 else
513 dst->rects = src->rects;
514 dst->size = src->size;
515 dst->numRects = src->numRects;
516 init_region( src, 0 );
519 /***********************************************************************
520 * REGION_DeleteObject
522 static BOOL REGION_DeleteObject( HGDIOBJ handle )
524 WINEREGION *rgn = free_gdi_handle( handle );
526 if (!rgn) return FALSE;
527 free_region( rgn );
528 return TRUE;
531 /***********************************************************************
532 * REGION_SelectObject
534 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc )
536 return ULongToHandle(SelectClipRgn( hdc, handle ));
540 /***********************************************************************
541 * REGION_OffsetRegion
542 * Offset a WINEREGION by x,y
544 static BOOL REGION_OffsetRegion( WINEREGION *rgn, WINEREGION *srcrgn, INT x, INT y )
546 if( rgn != srcrgn)
548 if (!REGION_CopyRegion( rgn, srcrgn)) return FALSE;
550 if(x || y) {
551 int nbox = rgn->numRects;
552 RECT *pbox = rgn->rects;
554 if(nbox) {
555 while(nbox--) {
556 pbox->left += x;
557 pbox->right += x;
558 pbox->top += y;
559 pbox->bottom += y;
560 pbox++;
562 rgn->extents.left += x;
563 rgn->extents.right += x;
564 rgn->extents.top += y;
565 rgn->extents.bottom += y;
568 return TRUE;
571 /***********************************************************************
572 * OffsetRgn (GDI32.@)
574 * Moves a region by the specified X- and Y-axis offsets.
576 * PARAMS
577 * hrgn [I] Region to offset.
578 * x [I] Offset right if positive or left if negative.
579 * y [I] Offset down if positive or up if negative.
581 * RETURNS
582 * Success:
583 * NULLREGION - The new region is empty.
584 * SIMPLEREGION - The new region can be represented by one rectangle.
585 * COMPLEXREGION - The new region can only be represented by more than
586 * one rectangle.
587 * Failure: ERROR
589 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
591 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
592 INT ret;
594 TRACE("%p %d,%d\n", hrgn, x, y);
596 if (!obj)
597 return ERROR;
599 REGION_OffsetRegion( obj, obj, x, y);
601 ret = get_region_type( obj );
602 GDI_ReleaseObj( hrgn );
603 return ret;
607 /***********************************************************************
608 * GetRgnBox (GDI32.@)
610 * Retrieves the bounding rectangle of the region. The bounding rectangle
611 * is the smallest rectangle that contains the entire region.
613 * PARAMS
614 * hrgn [I] Region to retrieve bounding rectangle from.
615 * rect [O] Rectangle that will receive the coordinates of the bounding
616 * rectangle.
618 * RETURNS
619 * NULLREGION - The new region is empty.
620 * SIMPLEREGION - The new region can be represented by one rectangle.
621 * COMPLEXREGION - The new region can only be represented by more than
622 * one rectangle.
624 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
626 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
627 if (obj)
629 INT ret;
630 rect->left = obj->extents.left;
631 rect->top = obj->extents.top;
632 rect->right = obj->extents.right;
633 rect->bottom = obj->extents.bottom;
634 TRACE("%p %s\n", hrgn, wine_dbgstr_rect(rect));
635 ret = get_region_type( obj );
636 GDI_ReleaseObj(hrgn);
637 return ret;
639 return ERROR;
643 /***********************************************************************
644 * CreateRectRgn (GDI32.@)
646 * Creates a simple rectangular region.
648 * PARAMS
649 * left [I] Left coordinate of rectangle.
650 * top [I] Top coordinate of rectangle.
651 * right [I] Right coordinate of rectangle.
652 * bottom [I] Bottom coordinate of rectangle.
654 * RETURNS
655 * Success: Handle to region.
656 * Failure: NULL.
658 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
660 HRGN hrgn;
661 WINEREGION *obj;
663 if (!(obj = alloc_region( RGN_DEFAULT_RECTS ))) return 0;
665 if (!(hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs )))
667 free_region( obj );
668 return 0;
670 TRACE( "%d,%d-%d,%d returning %p\n", left, top, right, bottom, hrgn );
671 SetRectRgn(hrgn, left, top, right, bottom);
672 return hrgn;
676 /***********************************************************************
677 * CreateRectRgnIndirect (GDI32.@)
679 * Creates a simple rectangular region.
681 * PARAMS
682 * rect [I] Coordinates of rectangular region.
684 * RETURNS
685 * Success: Handle to region.
686 * Failure: NULL.
688 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
690 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
694 /***********************************************************************
695 * SetRectRgn (GDI32.@)
697 * Sets a region to a simple rectangular region.
699 * PARAMS
700 * hrgn [I] Region to convert.
701 * left [I] Left coordinate of rectangle.
702 * top [I] Top coordinate of rectangle.
703 * right [I] Right coordinate of rectangle.
704 * bottom [I] Bottom coordinate of rectangle.
706 * RETURNS
707 * Success: Non-zero.
708 * Failure: Zero.
710 * NOTES
711 * Allows either or both left and top to be greater than right or bottom.
713 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
714 INT right, INT bottom )
716 WINEREGION *obj;
718 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
720 if (!(obj = GDI_GetObjPtr( hrgn, OBJ_REGION ))) return FALSE;
722 if (left > right) { INT tmp = left; left = right; right = tmp; }
723 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
725 if((left != right) && (top != bottom))
727 obj->rects->left = obj->extents.left = left;
728 obj->rects->top = obj->extents.top = top;
729 obj->rects->right = obj->extents.right = right;
730 obj->rects->bottom = obj->extents.bottom = bottom;
731 obj->numRects = 1;
733 else
734 empty_region(obj);
736 GDI_ReleaseObj( hrgn );
737 return TRUE;
741 /***********************************************************************
742 * CreateRoundRectRgn (GDI32.@)
744 * Creates a rectangular region with rounded corners.
746 * PARAMS
747 * left [I] Left coordinate of rectangle.
748 * top [I] Top coordinate of rectangle.
749 * right [I] Right coordinate of rectangle.
750 * bottom [I] Bottom coordinate of rectangle.
751 * ellipse_width [I] Width of the ellipse at each corner.
752 * ellipse_height [I] Height of the ellipse at each corner.
754 * RETURNS
755 * Success: Handle to region.
756 * Failure: NULL.
758 * NOTES
759 * If ellipse_width or ellipse_height is less than 2 logical units then
760 * it is treated as though CreateRectRgn() was called instead.
762 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
763 INT right, INT bottom,
764 INT ellipse_width, INT ellipse_height )
766 WINEREGION *obj;
767 HRGN hrgn = 0;
768 int a, b, i, x, y;
769 INT64 asq, bsq, dx, dy, err;
770 RECT *rects;
772 /* Make the dimensions sensible */
774 if (left > right) { INT tmp = left; left = right; right = tmp; }
775 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
776 /* the region is for the rectangle interior, but only at right and bottom for some reason */
777 right--;
778 bottom--;
780 ellipse_width = min( right - left, abs( ellipse_width ));
781 ellipse_height = min( bottom - top, abs( ellipse_height ));
783 /* Check if we can do a normal rectangle instead */
785 if ((ellipse_width < 2) || (ellipse_height < 2))
786 return CreateRectRgn( left, top, right, bottom );
788 if (!(obj = alloc_region( ellipse_height ))) return 0;
789 obj->numRects = ellipse_height;
790 obj->extents.left = left;
791 obj->extents.top = top;
792 obj->extents.right = right;
793 obj->extents.bottom = bottom;
794 rects = obj->rects;
796 /* based on an algorithm by Alois Zingl */
798 a = ellipse_width - 1;
799 b = ellipse_height - 1;
800 asq = (INT64)8 * a * a;
801 bsq = (INT64)8 * b * b;
802 dx = (INT64)4 * b * b * (1 - a);
803 dy = (INT64)4 * a * a * (1 + (b % 2));
804 err = dx + dy + a * a * (b % 2);
806 x = 0;
807 y = ellipse_height / 2;
809 rects[y].left = left;
810 rects[y].right = right;
812 while (x <= ellipse_width / 2)
814 INT64 e2 = 2 * err;
815 if (e2 >= dx)
817 x++;
818 err += dx += bsq;
820 if (e2 <= dy)
822 y++;
823 err += dy += asq;
824 rects[y].left = left + x;
825 rects[y].right = right - x;
828 for (i = 0; i < ellipse_height / 2; i++)
830 rects[i].left = rects[b - i].left;
831 rects[i].right = rects[b - i].right;
832 rects[i].top = top + i;
833 rects[i].bottom = rects[i].top + 1;
835 for (; i < ellipse_height; i++)
837 rects[i].top = bottom - ellipse_height + i;
838 rects[i].bottom = rects[i].top + 1;
840 rects[ellipse_height / 2].top = top + ellipse_height / 2; /* extend to top of rectangle */
842 hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs );
844 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
845 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
846 if (!hrgn) free_region( obj );
847 return hrgn;
851 /***********************************************************************
852 * CreateEllipticRgn (GDI32.@)
854 * Creates an elliptical region.
856 * PARAMS
857 * left [I] Left coordinate of bounding rectangle.
858 * top [I] Top coordinate of bounding rectangle.
859 * right [I] Right coordinate of bounding rectangle.
860 * bottom [I] Bottom coordinate of bounding rectangle.
862 * RETURNS
863 * Success: Handle to region.
864 * Failure: NULL.
866 * NOTES
867 * This is a special case of CreateRoundRectRgn() where the width of the
868 * ellipse at each corner is equal to the width the rectangle and
869 * the same for the height.
871 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
872 INT right, INT bottom )
874 return CreateRoundRectRgn( left, top, right, bottom,
875 right-left, bottom-top );
879 /***********************************************************************
880 * CreateEllipticRgnIndirect (GDI32.@)
882 * Creates an elliptical region.
884 * PARAMS
885 * rect [I] Pointer to bounding rectangle of the ellipse.
887 * RETURNS
888 * Success: Handle to region.
889 * Failure: NULL.
891 * NOTES
892 * This is a special case of CreateRoundRectRgn() where the width of the
893 * ellipse at each corner is equal to the width the rectangle and
894 * the same for the height.
896 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
898 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
899 rect->bottom, rect->right - rect->left,
900 rect->bottom - rect->top );
903 /***********************************************************************
904 * GetRegionData (GDI32.@)
906 * Retrieves the data that specifies the region.
908 * PARAMS
909 * hrgn [I] Region to retrieve the region data from.
910 * count [I] The size of the buffer pointed to by rgndata in bytes.
911 * rgndata [I] The buffer to receive data about the region.
913 * RETURNS
914 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
915 * the number of bytes copied to the output buffer.
916 * Failure: 0.
918 * NOTES
919 * The format of the Buffer member of RGNDATA is determined by the iType
920 * member of the region data header.
921 * Currently this is always RDH_RECTANGLES, which specifies that the format
922 * is the array of RECT's that specify the region. The length of the array
923 * is specified by the nCount member of the region data header.
925 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
927 DWORD size;
928 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
930 TRACE(" %p count = %d, rgndata = %p\n", hrgn, count, rgndata);
932 if(!obj) return 0;
934 size = obj->numRects * sizeof(RECT);
935 if (!rgndata || count < FIELD_OFFSET(RGNDATA, Buffer[size]))
937 GDI_ReleaseObj( hrgn );
938 if (rgndata) /* buffer is too small, signal it by return 0 */
939 return 0;
940 /* user requested buffer size with NULL rgndata */
941 return FIELD_OFFSET(RGNDATA, Buffer[size]);
944 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
945 rgndata->rdh.iType = RDH_RECTANGLES;
946 rgndata->rdh.nCount = obj->numRects;
947 rgndata->rdh.nRgnSize = size;
948 rgndata->rdh.rcBound.left = obj->extents.left;
949 rgndata->rdh.rcBound.top = obj->extents.top;
950 rgndata->rdh.rcBound.right = obj->extents.right;
951 rgndata->rdh.rcBound.bottom = obj->extents.bottom;
953 memcpy( rgndata->Buffer, obj->rects, size );
955 GDI_ReleaseObj( hrgn );
956 return FIELD_OFFSET(RGNDATA, Buffer[size]);
960 static void translate( POINT *pt, UINT count, const XFORM *xform )
962 while (count--)
964 double x = pt->x;
965 double y = pt->y;
966 pt->x = floor( x * xform->eM11 + y * xform->eM21 + xform->eDx + 0.5 );
967 pt->y = floor( x * xform->eM12 + y * xform->eM22 + xform->eDy + 0.5 );
968 pt++;
973 /***********************************************************************
974 * ExtCreateRegion (GDI32.@)
976 * Creates a region as specified by the transformation data and region data.
978 * PARAMS
979 * lpXform [I] World-space to logical-space transformation data.
980 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
981 * rgndata [I] Data that specifies the region.
983 * RETURNS
984 * Success: Handle to region.
985 * Failure: NULL.
987 * NOTES
988 * See GetRegionData().
990 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
992 HRGN hrgn = 0;
993 WINEREGION *obj;
994 const RECT *pCurRect, *pEndRect;
996 if (!rgndata)
998 SetLastError( ERROR_INVALID_PARAMETER );
999 return 0;
1002 if (rgndata->rdh.dwSize < sizeof(RGNDATAHEADER))
1003 return 0;
1005 /* XP doesn't care about the type */
1006 if( rgndata->rdh.iType != RDH_RECTANGLES )
1007 WARN("(Unsupported region data type: %u)\n", rgndata->rdh.iType);
1009 if (lpXform)
1011 const RECT *pCurRect, *pEndRect;
1013 hrgn = CreateRectRgn( 0, 0, 0, 0 );
1015 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1016 for (pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1018 static const INT count = 4;
1019 HRGN poly_hrgn;
1020 POINT pt[4];
1022 pt[0].x = pCurRect->left;
1023 pt[0].y = pCurRect->top;
1024 pt[1].x = pCurRect->right;
1025 pt[1].y = pCurRect->top;
1026 pt[2].x = pCurRect->right;
1027 pt[2].y = pCurRect->bottom;
1028 pt[3].x = pCurRect->left;
1029 pt[3].y = pCurRect->bottom;
1031 translate( pt, 4, lpXform );
1032 poly_hrgn = CreatePolyPolygonRgn( pt, &count, 1, WINDING );
1033 CombineRgn( hrgn, hrgn, poly_hrgn, RGN_OR );
1034 DeleteObject( poly_hrgn );
1036 return hrgn;
1039 if (!(obj = alloc_region( rgndata->rdh.nCount ))) return 0;
1041 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1042 for(pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1044 if (pCurRect->left < pCurRect->right && pCurRect->top < pCurRect->bottom)
1046 if (!REGION_UnionRectWithRegion( pCurRect, obj )) goto done;
1049 hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs );
1051 done:
1052 if (!hrgn) free_region( obj );
1054 TRACE("%p %d %p returning %p\n", lpXform, dwCount, rgndata, hrgn );
1055 return hrgn;
1059 /***********************************************************************
1060 * PtInRegion (GDI32.@)
1062 * Tests whether the specified point is inside a region.
1064 * PARAMS
1065 * hrgn [I] Region to test.
1066 * x [I] X-coordinate of point to test.
1067 * y [I] Y-coordinate of point to test.
1069 * RETURNS
1070 * Non-zero if the point is inside the region or zero otherwise.
1072 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
1074 WINEREGION *obj;
1075 BOOL ret = FALSE;
1077 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1079 if (obj->numRects > 0 && is_in_rect( &obj->extents, x, y ))
1080 region_find_pt( obj, x, y, &ret );
1081 GDI_ReleaseObj( hrgn );
1083 return ret;
1087 /***********************************************************************
1088 * RectInRegion (GDI32.@)
1090 * Tests if a rectangle is at least partly inside the specified region.
1092 * PARAMS
1093 * hrgn [I] Region to test.
1094 * rect [I] Rectangle to test.
1096 * RETURNS
1097 * Non-zero if the rectangle is partially inside the region or
1098 * zero otherwise.
1100 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
1102 WINEREGION *obj;
1103 BOOL ret = FALSE;
1104 RECT rc;
1105 int i;
1107 /* swap the coordinates to make right >= left and bottom >= top */
1108 /* (region building rectangles are normalized the same way) */
1109 rc = *rect;
1110 order_rect( &rc );
1112 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1114 if ((obj->numRects > 0) && overlapping(&obj->extents, &rc))
1116 for (i = region_find_pt( obj, rc.left, rc.top, &ret ); !ret && i < obj->numRects; i++ )
1118 if (obj->rects[i].bottom <= rc.top)
1119 continue; /* not far enough down yet */
1121 if (obj->rects[i].top >= rc.bottom)
1122 break; /* too far down */
1124 if (obj->rects[i].right <= rc.left)
1125 continue; /* not far enough over yet */
1127 if (obj->rects[i].left >= rc.right)
1128 continue;
1130 ret = TRUE;
1133 GDI_ReleaseObj(hrgn);
1135 return ret;
1138 /***********************************************************************
1139 * EqualRgn (GDI32.@)
1141 * Tests whether one region is identical to another.
1143 * PARAMS
1144 * hrgn1 [I] The first region to compare.
1145 * hrgn2 [I] The second region to compare.
1147 * RETURNS
1148 * Non-zero if both regions are identical or zero otherwise.
1150 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
1152 WINEREGION *obj1, *obj2;
1153 BOOL ret = FALSE;
1155 if ((obj1 = GDI_GetObjPtr( hrgn1, OBJ_REGION )))
1157 if ((obj2 = GDI_GetObjPtr( hrgn2, OBJ_REGION )))
1159 int i;
1161 if ( obj1->numRects != obj2->numRects ) goto done;
1162 if ( obj1->numRects == 0 )
1164 ret = TRUE;
1165 goto done;
1168 if (obj1->extents.left != obj2->extents.left) goto done;
1169 if (obj1->extents.right != obj2->extents.right) goto done;
1170 if (obj1->extents.top != obj2->extents.top) goto done;
1171 if (obj1->extents.bottom != obj2->extents.bottom) goto done;
1172 for( i = 0; i < obj1->numRects; i++ )
1174 if (obj1->rects[i].left != obj2->rects[i].left) goto done;
1175 if (obj1->rects[i].right != obj2->rects[i].right) goto done;
1176 if (obj1->rects[i].top != obj2->rects[i].top) goto done;
1177 if (obj1->rects[i].bottom != obj2->rects[i].bottom) goto done;
1179 ret = TRUE;
1180 done:
1181 GDI_ReleaseObj(hrgn2);
1183 GDI_ReleaseObj(hrgn1);
1185 return ret;
1188 /***********************************************************************
1189 * REGION_UnionRectWithRegion
1190 * Adds a rectangle to a WINEREGION
1192 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1194 WINEREGION region;
1196 init_region( &region, 1 );
1197 region.numRects = 1;
1198 region.extents = *region.rects = *rect;
1199 return REGION_UnionRegion(rgn, rgn, &region);
1203 BOOL add_rect_to_region( HRGN rgn, const RECT *rect )
1205 WINEREGION *obj = GDI_GetObjPtr( rgn, OBJ_REGION );
1206 BOOL ret;
1208 if (!obj) return FALSE;
1209 ret = REGION_UnionRectWithRegion( rect, obj );
1210 GDI_ReleaseObj( rgn );
1211 return ret;
1214 /***********************************************************************
1215 * REGION_CreateFrameRgn
1217 * Create a region that is a frame around another region.
1218 * Compute the intersection of the region moved in all 4 directions
1219 * ( +x, -x, +y, -y) and subtract from the original.
1220 * The result looks slightly better than in Windows :)
1222 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1224 WINEREGION tmprgn;
1225 BOOL bRet = FALSE;
1226 WINEREGION* destObj = NULL;
1227 WINEREGION *srcObj = GDI_GetObjPtr( hSrc, OBJ_REGION );
1229 tmprgn.rects = NULL;
1230 if (!srcObj) return FALSE;
1231 if (srcObj->numRects != 0)
1233 if (!(destObj = GDI_GetObjPtr( hDest, OBJ_REGION ))) goto done;
1234 if (!init_region( &tmprgn, srcObj->numRects )) goto done;
1236 if (!REGION_OffsetRegion( destObj, srcObj, -x, 0)) goto done;
1237 if (!REGION_OffsetRegion( &tmprgn, srcObj, x, 0)) goto done;
1238 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1239 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, -y)) goto done;
1240 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1241 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, y)) goto done;
1242 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1243 if (!REGION_SubtractRegion( destObj, srcObj, destObj )) goto done;
1244 bRet = TRUE;
1246 done:
1247 destroy_region( &tmprgn );
1248 if (destObj) GDI_ReleaseObj ( hDest );
1249 GDI_ReleaseObj( hSrc );
1250 return bRet;
1254 /***********************************************************************
1255 * CombineRgn (GDI32.@)
1257 * Combines two regions with the specified operation and stores the result
1258 * in the specified destination region.
1260 * PARAMS
1261 * hDest [I] The region that receives the combined result.
1262 * hSrc1 [I] The first source region.
1263 * hSrc2 [I] The second source region.
1264 * mode [I] The way in which the source regions will be combined. See notes.
1266 * RETURNS
1267 * Success:
1268 * NULLREGION - The new region is empty.
1269 * SIMPLEREGION - The new region can be represented by one rectangle.
1270 * COMPLEXREGION - The new region can only be represented by more than
1271 * one rectangle.
1272 * Failure: ERROR
1274 * NOTES
1275 * The two source regions can be the same region.
1276 * The mode can be one of the following:
1277 *| RGN_AND - Intersection of the regions
1278 *| RGN_OR - Union of the regions
1279 *| RGN_XOR - Unions of the regions minus any intersection.
1280 *| RGN_DIFF - Difference (subtraction) of the regions.
1282 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1284 WINEREGION *destObj = GDI_GetObjPtr( hDest, OBJ_REGION );
1285 INT result = ERROR;
1287 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1288 if (destObj)
1290 WINEREGION *src1Obj = GDI_GetObjPtr( hSrc1, OBJ_REGION );
1292 if (src1Obj)
1294 TRACE("dump src1Obj:\n");
1295 if(TRACE_ON(region))
1296 REGION_DumpRegion(src1Obj);
1297 if (mode == RGN_COPY)
1299 if (REGION_CopyRegion( destObj, src1Obj ))
1300 result = get_region_type( destObj );
1302 else
1304 WINEREGION *src2Obj = GDI_GetObjPtr( hSrc2, OBJ_REGION );
1306 if (src2Obj)
1308 TRACE("dump src2Obj:\n");
1309 if(TRACE_ON(region))
1310 REGION_DumpRegion(src2Obj);
1311 switch (mode)
1313 case RGN_AND:
1314 if (REGION_IntersectRegion( destObj, src1Obj, src2Obj ))
1315 result = get_region_type( destObj );
1316 break;
1317 case RGN_OR:
1318 if (REGION_UnionRegion( destObj, src1Obj, src2Obj ))
1319 result = get_region_type( destObj );
1320 break;
1321 case RGN_XOR:
1322 if (REGION_XorRegion( destObj, src1Obj, src2Obj ))
1323 result = get_region_type( destObj );
1324 break;
1325 case RGN_DIFF:
1326 if (REGION_SubtractRegion( destObj, src1Obj, src2Obj ))
1327 result = get_region_type( destObj );
1328 break;
1330 GDI_ReleaseObj( hSrc2 );
1333 GDI_ReleaseObj( hSrc1 );
1335 TRACE("dump destObj:\n");
1336 if(TRACE_ON(region))
1337 REGION_DumpRegion(destObj);
1339 GDI_ReleaseObj( hDest );
1341 return result;
1344 /***********************************************************************
1345 * REGION_SetExtents
1346 * Re-calculate the extents of a region
1348 static void REGION_SetExtents (WINEREGION *pReg)
1350 RECT *pRect, *pRectEnd, *pExtents;
1352 if (pReg->numRects == 0)
1354 pReg->extents.left = 0;
1355 pReg->extents.top = 0;
1356 pReg->extents.right = 0;
1357 pReg->extents.bottom = 0;
1358 return;
1361 pExtents = &pReg->extents;
1362 pRect = pReg->rects;
1363 pRectEnd = &pRect[pReg->numRects - 1];
1366 * Since pRect is the first rectangle in the region, it must have the
1367 * smallest top and since pRectEnd is the last rectangle in the region,
1368 * it must have the largest bottom, because of banding. Initialize left and
1369 * right from pRect and pRectEnd, resp., as good things to initialize them
1370 * to...
1372 pExtents->left = pRect->left;
1373 pExtents->top = pRect->top;
1374 pExtents->right = pRectEnd->right;
1375 pExtents->bottom = pRectEnd->bottom;
1377 while (pRect <= pRectEnd)
1379 if (pRect->left < pExtents->left)
1380 pExtents->left = pRect->left;
1381 if (pRect->right > pExtents->right)
1382 pExtents->right = pRect->right;
1383 pRect++;
1387 /***********************************************************************
1388 * REGION_CopyRegion
1390 static BOOL REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1392 if (dst != src) /* don't want to copy to itself */
1394 if (dst->size < src->numRects && !grow_region( dst, src->numRects ))
1395 return FALSE;
1397 dst->numRects = src->numRects;
1398 dst->extents.left = src->extents.left;
1399 dst->extents.top = src->extents.top;
1400 dst->extents.right = src->extents.right;
1401 dst->extents.bottom = src->extents.bottom;
1402 memcpy(dst->rects, src->rects, src->numRects * sizeof(RECT));
1404 return TRUE;
1407 /***********************************************************************
1408 * REGION_MirrorRegion
1410 static BOOL REGION_MirrorRegion( WINEREGION *dst, WINEREGION *src, int width )
1412 int i, start, end;
1413 RECT extents;
1414 RECT *rects;
1415 WINEREGION tmp;
1417 if (dst != src)
1419 if (!grow_region( dst, src->numRects )) return FALSE;
1420 rects = dst->rects;
1421 dst->numRects = src->numRects;
1423 else
1425 if (!init_region( &tmp, src->numRects )) return FALSE;
1426 rects = tmp.rects;
1427 tmp.numRects = src->numRects;
1430 extents.left = width - src->extents.right;
1431 extents.right = width - src->extents.left;
1432 extents.top = src->extents.top;
1433 extents.bottom = src->extents.bottom;
1435 for (start = 0; start < src->numRects; start = end)
1437 /* find the end of the current band */
1438 for (end = start + 1; end < src->numRects; end++)
1439 if (src->rects[end].top != src->rects[end - 1].top) break;
1441 for (i = 0; i < end - start; i++)
1443 rects[start + i].left = width - src->rects[end - i - 1].right;
1444 rects[start + i].right = width - src->rects[end - i - 1].left;
1445 rects[start + i].top = src->rects[end - i - 1].top;
1446 rects[start + i].bottom = src->rects[end - i - 1].bottom;
1450 if (dst == src)
1451 move_rects( dst, &tmp );
1453 dst->extents = extents;
1454 return TRUE;
1457 /***********************************************************************
1458 * mirror_region
1460 INT mirror_region( HRGN dst, HRGN src, INT width )
1462 WINEREGION *src_rgn, *dst_rgn;
1463 INT ret = ERROR;
1465 if (!(src_rgn = GDI_GetObjPtr( src, OBJ_REGION ))) return ERROR;
1466 if ((dst_rgn = GDI_GetObjPtr( dst, OBJ_REGION )))
1468 if (REGION_MirrorRegion( dst_rgn, src_rgn, width )) ret = get_region_type( dst_rgn );
1469 GDI_ReleaseObj( dst_rgn );
1471 GDI_ReleaseObj( src_rgn );
1472 return ret;
1475 /***********************************************************************
1476 * MirrorRgn (GDI32.@)
1478 BOOL WINAPI MirrorRgn( HWND hwnd, HRGN hrgn )
1480 static const WCHAR user32W[] = {'u','s','e','r','3','2','.','d','l','l',0};
1481 static BOOL (WINAPI *pGetWindowRect)( HWND hwnd, LPRECT rect );
1482 RECT rect;
1484 /* yes, a HWND in gdi32, don't ask */
1485 if (!pGetWindowRect)
1487 HMODULE user32 = GetModuleHandleW(user32W);
1488 if (!user32) return FALSE;
1489 if (!(pGetWindowRect = (void *)GetProcAddress( user32, "GetWindowRect" ))) return FALSE;
1491 pGetWindowRect( hwnd, &rect );
1492 return mirror_region( hrgn, hrgn, rect.right - rect.left ) != ERROR;
1496 /***********************************************************************
1497 * REGION_Coalesce
1499 * Attempt to merge the rects in the current band with those in the
1500 * previous one. Used only by REGION_RegionOp.
1502 * Results:
1503 * The new index for the previous band.
1505 * Side Effects:
1506 * If coalescing takes place:
1507 * - rectangles in the previous band will have their bottom fields
1508 * altered.
1509 * - pReg->numRects will be decreased.
1512 static INT REGION_Coalesce (
1513 WINEREGION *pReg, /* Region to coalesce */
1514 INT prevStart, /* Index of start of previous band */
1515 INT curStart /* Index of start of current band */
1517 RECT *pPrevRect; /* Current rect in previous band */
1518 RECT *pCurRect; /* Current rect in current band */
1519 RECT *pRegEnd; /* End of region */
1520 INT curNumRects; /* Number of rectangles in current band */
1521 INT prevNumRects; /* Number of rectangles in previous band */
1522 INT bandtop; /* top coordinate for current band */
1524 pRegEnd = &pReg->rects[pReg->numRects];
1526 pPrevRect = &pReg->rects[prevStart];
1527 prevNumRects = curStart - prevStart;
1530 * Figure out how many rectangles are in the current band. Have to do
1531 * this because multiple bands could have been added in REGION_RegionOp
1532 * at the end when one region has been exhausted.
1534 pCurRect = &pReg->rects[curStart];
1535 bandtop = pCurRect->top;
1536 for (curNumRects = 0;
1537 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1538 curNumRects++)
1540 pCurRect++;
1543 if (pCurRect != pRegEnd)
1546 * If more than one band was added, we have to find the start
1547 * of the last band added so the next coalescing job can start
1548 * at the right place... (given when multiple bands are added,
1549 * this may be pointless -- see above).
1551 pRegEnd--;
1552 while (pRegEnd[-1].top == pRegEnd->top)
1554 pRegEnd--;
1556 curStart = pRegEnd - pReg->rects;
1557 pRegEnd = pReg->rects + pReg->numRects;
1560 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1561 pCurRect -= curNumRects;
1563 * The bands may only be coalesced if the bottom of the previous
1564 * matches the top scanline of the current.
1566 if (pPrevRect->bottom == pCurRect->top)
1569 * Make sure the bands have rects in the same places. This
1570 * assumes that rects have been added in such a way that they
1571 * cover the most area possible. I.e. two rects in a band must
1572 * have some horizontal space between them.
1576 if ((pPrevRect->left != pCurRect->left) ||
1577 (pPrevRect->right != pCurRect->right))
1580 * The bands don't line up so they can't be coalesced.
1582 return (curStart);
1584 pPrevRect++;
1585 pCurRect++;
1586 prevNumRects -= 1;
1587 } while (prevNumRects != 0);
1589 pReg->numRects -= curNumRects;
1590 pCurRect -= curNumRects;
1591 pPrevRect -= curNumRects;
1594 * The bands may be merged, so set the bottom of each rect
1595 * in the previous band to that of the corresponding rect in
1596 * the current band.
1600 pPrevRect->bottom = pCurRect->bottom;
1601 pPrevRect++;
1602 pCurRect++;
1603 curNumRects -= 1;
1604 } while (curNumRects != 0);
1607 * If only one band was added to the region, we have to backup
1608 * curStart to the start of the previous band.
1610 * If more than one band was added to the region, copy the
1611 * other bands down. The assumption here is that the other bands
1612 * came from the same region as the current one and no further
1613 * coalescing can be done on them since it's all been done
1614 * already... curStart is already in the right place.
1616 if (pCurRect == pRegEnd)
1618 curStart = prevStart;
1620 else
1624 *pPrevRect++ = *pCurRect++;
1625 } while (pCurRect != pRegEnd);
1630 return (curStart);
1633 /**********************************************************************
1634 * REGION_compact
1636 * To keep regions from growing without bound, shrink the array of rectangles
1637 * to match the new number of rectangles in the region.
1639 * Only do this if the number of rectangles allocated is more than
1640 * twice the number of rectangles in the region.
1642 static void REGION_compact( WINEREGION *reg )
1644 if ((reg->numRects < reg->size / 2) && (reg->numRects > RGN_DEFAULT_RECTS))
1646 RECT *new_rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects, reg->numRects * sizeof(RECT) );
1647 if (new_rects)
1649 reg->rects = new_rects;
1650 reg->size = reg->numRects;
1655 /***********************************************************************
1656 * REGION_RegionOp
1658 * Apply an operation to two regions. Called by REGION_Union,
1659 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1661 * Results:
1662 * None.
1664 * Side Effects:
1665 * The new region is overwritten.
1667 * Notes:
1668 * The idea behind this function is to view the two regions as sets.
1669 * Together they cover a rectangle of area that this function divides
1670 * into horizontal bands where points are covered only by one region
1671 * or by both. For the first case, the nonOverlapFunc is called with
1672 * each the band and the band's upper and lower extents. For the
1673 * second, the overlapFunc is called to process the entire band. It
1674 * is responsible for clipping the rectangles in the band, though
1675 * this function provides the boundaries.
1676 * At the end of each band, the new region is coalesced, if possible,
1677 * to reduce the number of rectangles in the region.
1680 static BOOL REGION_RegionOp(
1681 WINEREGION *destReg, /* Place to store result */
1682 WINEREGION *reg1, /* First region in operation */
1683 WINEREGION *reg2, /* 2nd region in operation */
1684 BOOL (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1685 BOOL (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1686 BOOL (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1688 WINEREGION newReg;
1689 RECT *r1; /* Pointer into first region */
1690 RECT *r2; /* Pointer into 2d region */
1691 RECT *r1End; /* End of 1st region */
1692 RECT *r2End; /* End of 2d region */
1693 INT ybot; /* Bottom of intersection */
1694 INT ytop; /* Top of intersection */
1695 INT prevBand; /* Index of start of
1696 * previous band in newReg */
1697 INT curBand; /* Index of start of current
1698 * band in newReg */
1699 RECT *r1BandEnd; /* End of current band in r1 */
1700 RECT *r2BandEnd; /* End of current band in r2 */
1701 INT top; /* Top of non-overlapping band */
1702 INT bot; /* Bottom of non-overlapping band */
1705 * Initialization:
1706 * set r1, r2, r1End and r2End appropriately, preserve the important
1707 * parts of the destination region until the end in case it's one of
1708 * the two source regions, then mark the "new" region empty, allocating
1709 * another array of rectangles for it to use.
1711 r1 = reg1->rects;
1712 r2 = reg2->rects;
1713 r1End = r1 + reg1->numRects;
1714 r2End = r2 + reg2->numRects;
1717 * Allocate a reasonable number of rectangles for the new region. The idea
1718 * is to allocate enough so the individual functions don't need to
1719 * reallocate and copy the array, which is time consuming, yet we don't
1720 * have to worry about using too much memory. I hope to be able to
1721 * nuke the Xrealloc() at the end of this function eventually.
1723 if (!init_region( &newReg, max(reg1->numRects,reg2->numRects) * 2 )) return FALSE;
1726 * Initialize ybot and ytop.
1727 * In the upcoming loop, ybot and ytop serve different functions depending
1728 * on whether the band being handled is an overlapping or non-overlapping
1729 * band.
1730 * In the case of a non-overlapping band (only one of the regions
1731 * has points in the band), ybot is the bottom of the most recent
1732 * intersection and thus clips the top of the rectangles in that band.
1733 * ytop is the top of the next intersection between the two regions and
1734 * serves to clip the bottom of the rectangles in the current band.
1735 * For an overlapping band (where the two regions intersect), ytop clips
1736 * the top of the rectangles of both regions and ybot clips the bottoms.
1738 if (reg1->extents.top < reg2->extents.top)
1739 ybot = reg1->extents.top;
1740 else
1741 ybot = reg2->extents.top;
1744 * prevBand serves to mark the start of the previous band so rectangles
1745 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1746 * In the beginning, there is no previous band, so prevBand == curBand
1747 * (curBand is set later on, of course, but the first band will always
1748 * start at index 0). prevBand and curBand must be indices because of
1749 * the possible expansion, and resultant moving, of the new region's
1750 * array of rectangles.
1752 prevBand = 0;
1756 curBand = newReg.numRects;
1759 * This algorithm proceeds one source-band (as opposed to a
1760 * destination band, which is determined by where the two regions
1761 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1762 * rectangle after the last one in the current band for their
1763 * respective regions.
1765 r1BandEnd = r1;
1766 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1768 r1BandEnd++;
1771 r2BandEnd = r2;
1772 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1774 r2BandEnd++;
1778 * First handle the band that doesn't intersect, if any.
1780 * Note that attention is restricted to one band in the
1781 * non-intersecting region at once, so if a region has n
1782 * bands between the current position and the next place it overlaps
1783 * the other, this entire loop will be passed through n times.
1785 if (r1->top < r2->top)
1787 top = max(r1->top,ybot);
1788 bot = min(r1->bottom,r2->top);
1790 if ((top != bot) && (nonOverlap1Func != NULL))
1792 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, top, bot)) return FALSE;
1795 ytop = r2->top;
1797 else if (r2->top < r1->top)
1799 top = max(r2->top,ybot);
1800 bot = min(r2->bottom,r1->top);
1802 if ((top != bot) && (nonOverlap2Func != NULL))
1804 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, top, bot)) return FALSE;
1807 ytop = r1->top;
1809 else
1811 ytop = r1->top;
1815 * If any rectangles got added to the region, try and coalesce them
1816 * with rectangles from the previous band. Note we could just do
1817 * this test in miCoalesce, but some machines incur a not
1818 * inconsiderable cost for function calls, so...
1820 if (newReg.numRects != curBand)
1822 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1826 * Now see if we've hit an intersecting band. The two bands only
1827 * intersect if ybot > ytop
1829 ybot = min(r1->bottom, r2->bottom);
1830 curBand = newReg.numRects;
1831 if (ybot > ytop)
1833 if (!overlapFunc(&newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot)) return FALSE;
1836 if (newReg.numRects != curBand)
1838 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1842 * If we've finished with a band (bottom == ybot) we skip forward
1843 * in the region to the next band.
1845 if (r1->bottom == ybot)
1847 r1 = r1BandEnd;
1849 if (r2->bottom == ybot)
1851 r2 = r2BandEnd;
1853 } while ((r1 != r1End) && (r2 != r2End));
1856 * Deal with whichever region still has rectangles left.
1858 curBand = newReg.numRects;
1859 if (r1 != r1End)
1861 if (nonOverlap1Func != NULL)
1865 r1BandEnd = r1;
1866 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1868 r1BandEnd++;
1870 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom))
1871 return FALSE;
1872 r1 = r1BandEnd;
1873 } while (r1 != r1End);
1876 else if ((r2 != r2End) && (nonOverlap2Func != NULL))
1880 r2BandEnd = r2;
1881 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1883 r2BandEnd++;
1885 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom))
1886 return FALSE;
1887 r2 = r2BandEnd;
1888 } while (r2 != r2End);
1891 if (newReg.numRects != curBand)
1893 REGION_Coalesce (&newReg, prevBand, curBand);
1896 REGION_compact( &newReg );
1897 move_rects( destReg, &newReg );
1898 return TRUE;
1901 /***********************************************************************
1902 * Region Intersection
1903 ***********************************************************************/
1906 /***********************************************************************
1907 * REGION_IntersectO
1909 * Handle an overlapping band for REGION_Intersect.
1911 * Results:
1912 * None.
1914 * Side Effects:
1915 * Rectangles may be added to the region.
1918 static BOOL REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1919 RECT *r2, RECT *r2End, INT top, INT bottom)
1922 INT left, right;
1924 while ((r1 != r1End) && (r2 != r2End))
1926 left = max(r1->left, r2->left);
1927 right = min(r1->right, r2->right);
1930 * If there's any overlap between the two rectangles, add that
1931 * overlap to the new region.
1932 * There's no need to check for subsumption because the only way
1933 * such a need could arise is if some region has two rectangles
1934 * right next to each other. Since that should never happen...
1936 if (left < right)
1938 if (!add_rect( pReg, left, top, right, bottom )) return FALSE;
1942 * Need to advance the pointers. Shift the one that extends
1943 * to the right the least, since the other still has a chance to
1944 * overlap with that region's next rectangle, if you see what I mean.
1946 if (r1->right < r2->right)
1948 r1++;
1950 else if (r2->right < r1->right)
1952 r2++;
1954 else
1956 r1++;
1957 r2++;
1960 return TRUE;
1963 /***********************************************************************
1964 * REGION_IntersectRegion
1966 static BOOL REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1967 WINEREGION *reg2)
1969 /* check for trivial reject */
1970 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1971 (!overlapping(&reg1->extents, &reg2->extents)))
1972 newReg->numRects = 0;
1973 else
1974 if (!REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL)) return FALSE;
1977 * Can't alter newReg's extents before we call miRegionOp because
1978 * it might be one of the source regions and miRegionOp depends
1979 * on the extents of those regions being the same. Besides, this
1980 * way there's no checking against rectangles that will be nuked
1981 * due to coalescing, so we have to examine fewer rectangles.
1983 REGION_SetExtents(newReg);
1984 return TRUE;
1987 /***********************************************************************
1988 * Region Union
1989 ***********************************************************************/
1991 /***********************************************************************
1992 * REGION_UnionNonO
1994 * Handle a non-overlapping band for the union operation. Just
1995 * Adds the rectangles into the region. Doesn't have to check for
1996 * subsumption or anything.
1998 * Results:
1999 * None.
2001 * Side Effects:
2002 * pReg->numRects is incremented and the final rectangles overwritten
2003 * with the rectangles we're passed.
2006 static BOOL REGION_UnionNonO(WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2008 while (r != rEnd)
2010 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2011 r++;
2013 return TRUE;
2016 /***********************************************************************
2017 * REGION_UnionO
2019 * Handle an overlapping band for the union operation. Picks the
2020 * left-most rectangle each time and merges it into the region.
2022 * Results:
2023 * None.
2025 * Side Effects:
2026 * Rectangles are overwritten in pReg->rects and pReg->numRects will
2027 * be changed.
2030 static BOOL REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2031 RECT *r2, RECT *r2End, INT top, INT bottom)
2033 #define MERGERECT(r) \
2034 if ((pReg->numRects != 0) && \
2035 (pReg->rects[pReg->numRects-1].top == top) && \
2036 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
2037 (pReg->rects[pReg->numRects-1].right >= r->left)) \
2039 if (pReg->rects[pReg->numRects-1].right < r->right) \
2040 pReg->rects[pReg->numRects-1].right = r->right; \
2042 else \
2044 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2046 r++;
2048 while ((r1 != r1End) && (r2 != r2End))
2050 if (r1->left < r2->left)
2052 MERGERECT(r1);
2054 else
2056 MERGERECT(r2);
2060 if (r1 != r1End)
2064 MERGERECT(r1);
2065 } while (r1 != r1End);
2067 else while (r2 != r2End)
2069 MERGERECT(r2);
2071 return TRUE;
2072 #undef MERGERECT
2075 /***********************************************************************
2076 * REGION_UnionRegion
2078 static BOOL REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1, WINEREGION *reg2)
2080 BOOL ret = TRUE;
2082 /* checks all the simple cases */
2085 * Region 1 and 2 are the same or region 1 is empty
2087 if ( (reg1 == reg2) || (!(reg1->numRects)) )
2089 if (newReg != reg2)
2090 ret = REGION_CopyRegion(newReg, reg2);
2091 return ret;
2095 * if nothing to union (region 2 empty)
2097 if (!(reg2->numRects))
2099 if (newReg != reg1)
2100 ret = REGION_CopyRegion(newReg, reg1);
2101 return ret;
2105 * Region 1 completely subsumes region 2
2107 if ((reg1->numRects == 1) &&
2108 (reg1->extents.left <= reg2->extents.left) &&
2109 (reg1->extents.top <= reg2->extents.top) &&
2110 (reg1->extents.right >= reg2->extents.right) &&
2111 (reg1->extents.bottom >= reg2->extents.bottom))
2113 if (newReg != reg1)
2114 ret = REGION_CopyRegion(newReg, reg1);
2115 return ret;
2119 * Region 2 completely subsumes region 1
2121 if ((reg2->numRects == 1) &&
2122 (reg2->extents.left <= reg1->extents.left) &&
2123 (reg2->extents.top <= reg1->extents.top) &&
2124 (reg2->extents.right >= reg1->extents.right) &&
2125 (reg2->extents.bottom >= reg1->extents.bottom))
2127 if (newReg != reg2)
2128 ret = REGION_CopyRegion(newReg, reg2);
2129 return ret;
2132 if ((ret = REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO)))
2134 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2135 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2136 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2137 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2139 return ret;
2142 /***********************************************************************
2143 * Region Subtraction
2144 ***********************************************************************/
2146 /***********************************************************************
2147 * REGION_SubtractNonO1
2149 * Deal with non-overlapping band for subtraction. Any parts from
2150 * region 2 we discard. Anything from region 1 we add to the region.
2152 * Results:
2153 * None.
2155 * Side Effects:
2156 * pReg may be affected.
2159 static BOOL REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2161 while (r != rEnd)
2163 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2164 r++;
2166 return TRUE;
2170 /***********************************************************************
2171 * REGION_SubtractO
2173 * Overlapping band subtraction. x1 is the left-most point not yet
2174 * checked.
2176 * Results:
2177 * None.
2179 * Side Effects:
2180 * pReg may have rectangles added to it.
2183 static BOOL REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2184 RECT *r2, RECT *r2End, INT top, INT bottom)
2186 INT left = r1->left;
2188 while ((r1 != r1End) && (r2 != r2End))
2190 if (r2->right <= left)
2193 * Subtrahend missed the boat: go to next subtrahend.
2195 r2++;
2197 else if (r2->left <= left)
2200 * Subtrahend precedes minuend: nuke left edge of minuend.
2202 left = r2->right;
2203 if (left >= r1->right)
2206 * Minuend completely covered: advance to next minuend and
2207 * reset left fence to edge of new minuend.
2209 r1++;
2210 if (r1 != r1End)
2211 left = r1->left;
2213 else
2216 * Subtrahend now used up since it doesn't extend beyond
2217 * minuend
2219 r2++;
2222 else if (r2->left < r1->right)
2225 * Left part of subtrahend covers part of minuend: add uncovered
2226 * part of minuend to region and skip to next subtrahend.
2228 if (!add_rect( pReg, left, top, r2->left, bottom )) return FALSE;
2229 left = r2->right;
2230 if (left >= r1->right)
2233 * Minuend used up: advance to new...
2235 r1++;
2236 if (r1 != r1End)
2237 left = r1->left;
2239 else
2242 * Subtrahend used up
2244 r2++;
2247 else
2250 * Minuend used up: add any remaining piece before advancing.
2252 if (r1->right > left)
2254 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2256 r1++;
2257 if (r1 != r1End)
2258 left = r1->left;
2263 * Add remaining minuend rectangles to region.
2265 while (r1 != r1End)
2267 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2268 r1++;
2269 if (r1 != r1End)
2271 left = r1->left;
2274 return TRUE;
2277 /***********************************************************************
2278 * REGION_SubtractRegion
2280 * Subtract regS from regM and leave the result in regD.
2281 * S stands for subtrahend, M for minuend and D for difference.
2283 * Results:
2284 * TRUE.
2286 * Side Effects:
2287 * regD is overwritten.
2290 static BOOL REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM, WINEREGION *regS )
2292 /* check for trivial reject */
2293 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2294 (!overlapping(&regM->extents, &regS->extents)) )
2295 return REGION_CopyRegion(regD, regM);
2297 if (!REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL))
2298 return FALSE;
2301 * Can't alter newReg's extents before we call miRegionOp because
2302 * it might be one of the source regions and miRegionOp depends
2303 * on the extents of those regions being the unaltered. Besides, this
2304 * way there's no checking against rectangles that will be nuked
2305 * due to coalescing, so we have to examine fewer rectangles.
2307 REGION_SetExtents (regD);
2308 return TRUE;
2311 /***********************************************************************
2312 * REGION_XorRegion
2314 static BOOL REGION_XorRegion(WINEREGION *dr, WINEREGION *sra, WINEREGION *srb)
2316 WINEREGION tra, trb;
2317 BOOL ret;
2319 if (!init_region( &tra, sra->numRects + 1 )) return FALSE;
2320 if ((ret = init_region( &trb, srb->numRects + 1 )))
2322 ret = REGION_SubtractRegion(&tra,sra,srb) &&
2323 REGION_SubtractRegion(&trb,srb,sra) &&
2324 REGION_UnionRegion(dr,&tra,&trb);
2325 destroy_region(&trb);
2327 destroy_region(&tra);
2328 return ret;
2331 /**************************************************************************
2333 * Poly Regions
2335 *************************************************************************/
2337 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2338 #define SMALL_COORDINATE 0x80000000
2340 /***********************************************************************
2341 * REGION_InsertEdgeInET
2343 * Insert the given edge into the edge table.
2344 * First we must find the correct bucket in the
2345 * Edge table, then find the right slot in the
2346 * bucket. Finally, we can insert it.
2349 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2350 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2353 struct list *ptr;
2354 ScanLineList *pSLL, *pPrevSLL;
2355 ScanLineListBlock *tmpSLLBlock;
2358 * find the right bucket to put the edge into
2360 pPrevSLL = &ET->scanlines;
2361 pSLL = pPrevSLL->next;
2362 while (pSLL && (pSLL->scanline < scanline))
2364 pPrevSLL = pSLL;
2365 pSLL = pSLL->next;
2369 * reassign pSLL (pointer to ScanLineList) if necessary
2371 if ((!pSLL) || (pSLL->scanline > scanline))
2373 if (*iSLLBlock > SLLSPERBLOCK-1)
2375 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2376 if(!tmpSLLBlock)
2378 WARN("Can't alloc SLLB\n");
2379 return;
2381 (*SLLBlock)->next = tmpSLLBlock;
2382 tmpSLLBlock->next = NULL;
2383 *SLLBlock = tmpSLLBlock;
2384 *iSLLBlock = 0;
2386 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2388 pSLL->next = pPrevSLL->next;
2389 list_init( &pSLL->edgelist );
2390 pPrevSLL->next = pSLL;
2392 pSLL->scanline = scanline;
2395 * now insert the edge in the right bucket
2397 LIST_FOR_EACH( ptr, &pSLL->edgelist )
2399 struct edge_table_entry *entry = LIST_ENTRY( ptr, struct edge_table_entry, entry );
2400 if (entry->bres.minor_axis >= ETE->bres.minor_axis) break;
2402 list_add_before( ptr, &ETE->entry );
2405 /***********************************************************************
2406 * REGION_CreateEdgeTable
2408 * This routine creates the edge table for
2409 * scan converting polygons.
2410 * The Edge Table (ET) looks like:
2412 * EdgeTable
2413 * --------
2414 * | ymax | ScanLineLists
2415 * |scanline|-->------------>-------------->...
2416 * -------- |scanline| |scanline|
2417 * |edgelist| |edgelist|
2418 * --------- ---------
2419 * | |
2420 * | |
2421 * V V
2422 * list of ETEs list of ETEs
2424 * where ETE is an EdgeTableEntry data structure,
2425 * and there is one ScanLineList per scanline at
2426 * which an edge is initially entered.
2429 static void REGION_CreateEdgeTable(const INT *Count, INT nbpolygons,
2430 const POINT *pts, EdgeTable *ET,
2431 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
2433 const POINT *top, *bottom;
2434 const POINT *PrevPt, *CurrPt, *EndPt;
2435 INT poly, count;
2436 int iSLLBlock = 0;
2437 int dy;
2440 * initialize the Edge Table.
2442 ET->scanlines.next = NULL;
2443 ET->ymax = SMALL_COORDINATE;
2444 ET->ymin = LARGE_COORDINATE;
2445 pSLLBlock->next = NULL;
2447 EndPt = pts - 1;
2448 for(poly = 0; poly < nbpolygons; poly++)
2450 count = Count[poly];
2451 EndPt += count;
2452 if(count < 2)
2453 continue;
2455 PrevPt = EndPt;
2458 * for each vertex in the array of points.
2459 * In this loop we are dealing with two vertices at
2460 * a time -- these make up one edge of the polygon.
2462 while (count--)
2464 CurrPt = pts++;
2467 * find out which point is above and which is below.
2469 if (PrevPt->y > CurrPt->y)
2471 bottom = PrevPt, top = CurrPt;
2472 pETEs->ClockWise = 0;
2474 else
2476 bottom = CurrPt, top = PrevPt;
2477 pETEs->ClockWise = 1;
2481 * don't add horizontal edges to the Edge table.
2483 if (bottom->y != top->y)
2485 pETEs->ymax = bottom->y-1;
2486 /* -1 so we don't get last scanline */
2489 * initialize integer edge algorithm
2491 dy = bottom->y - top->y;
2492 bres_init_polygon(dy, top->x, bottom->x, &pETEs->bres);
2494 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
2495 &iSLLBlock);
2497 if (PrevPt->y > ET->ymax)
2498 ET->ymax = PrevPt->y;
2499 if (PrevPt->y < ET->ymin)
2500 ET->ymin = PrevPt->y;
2501 pETEs++;
2504 PrevPt = CurrPt;
2509 /***********************************************************************
2510 * REGION_loadAET
2512 * This routine moves EdgeTableEntries from the
2513 * EdgeTable into the Active Edge Table,
2514 * leaving them sorted by smaller x coordinate.
2517 static void REGION_loadAET( struct list *AET, struct list *ETEs )
2519 struct edge_table_entry *ptr, *next, *entry;
2520 struct list *active;
2522 LIST_FOR_EACH_ENTRY_SAFE( ptr, next, ETEs, struct edge_table_entry, entry )
2524 LIST_FOR_EACH( active, AET )
2526 entry = LIST_ENTRY( active, struct edge_table_entry, entry );
2527 if (entry->bres.minor_axis >= ptr->bres.minor_axis) break;
2529 list_remove( &ptr->entry );
2530 list_add_before( active, &ptr->entry );
2534 /***********************************************************************
2535 * REGION_computeWAET
2537 * This routine links the AET by the
2538 * nextWETE (winding EdgeTableEntry) link for
2539 * use by the winding number rule. The final
2540 * Active Edge Table (AET) might look something
2541 * like:
2543 * AET
2544 * ---------- --------- ---------
2545 * |ymax | |ymax | |ymax |
2546 * | ... | |... | |... |
2547 * |next |->|next |->|next |->...
2548 * |nextWETE| |nextWETE| |nextWETE|
2549 * --------- --------- ^--------
2550 * | | |
2551 * V-------------------> V---> ...
2554 static void REGION_computeWAET( struct list *AET, struct list *WETE )
2556 struct edge_table_entry *active;
2557 BOOL inside = TRUE;
2558 int isInside = 0;
2560 list_init( WETE );
2561 LIST_FOR_EACH_ENTRY( active, AET, struct edge_table_entry, entry )
2563 if (active->ClockWise)
2564 isInside++;
2565 else
2566 isInside--;
2568 if ((!inside && !isInside) || (inside && isInside))
2570 list_add_tail( WETE, &active->winding_entry );
2571 inside = !inside;
2576 /***********************************************************************
2577 * REGION_InsertionSort
2579 * Just a simple insertion sort to sort the Active Edge Table.
2582 static BOOL REGION_InsertionSort( struct list *AET )
2584 struct edge_table_entry *active, *next, *insert;
2585 BOOL changed = FALSE;
2587 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2589 LIST_FOR_EACH_ENTRY( insert, AET, struct edge_table_entry, entry )
2591 if (insert == active) break;
2592 if (insert->bres.minor_axis > active->bres.minor_axis) break;
2594 if (insert == active) continue;
2595 list_remove( &active->entry );
2596 list_add_before( &insert->entry, &active->entry );
2597 changed = TRUE;
2599 return changed;
2602 /***********************************************************************
2603 * REGION_FreeStorage
2605 * Clean up our act.
2607 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2609 ScanLineListBlock *tmpSLLBlock;
2611 while (pSLLBlock)
2613 tmpSLLBlock = pSLLBlock->next;
2614 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2615 pSLLBlock = tmpSLLBlock;
2620 /***********************************************************************
2621 * REGION_PtsToRegion
2623 * Create an array of rectangles from a list of points.
2625 static WINEREGION *REGION_PtsToRegion( struct point_block *FirstPtBlock )
2627 POINT *pts;
2628 struct point_block *pb;
2629 int i, size, cur_band = 0, prev_band = 0;
2630 RECT *extents;
2631 WINEREGION *reg;
2633 for (pb = FirstPtBlock, size = 0; pb; pb = pb->next) size += pb->count;
2634 if (!(reg = alloc_region( size ))) return NULL;
2636 extents = &reg->extents;
2637 extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
2639 for (pb = FirstPtBlock; pb; pb = pb->next)
2641 /* the loop uses 2 points per iteration */
2642 i = pb->count / 2;
2643 for (pts = pb->pts; i--; pts += 2) {
2644 if (pts->x == pts[1].x)
2645 continue;
2647 if (reg->numRects && pts[0].y != reg->rects[cur_band].top)
2649 prev_band = REGION_Coalesce( reg, prev_band, cur_band );
2650 cur_band = reg->numRects;
2653 add_rect( reg, pts[0].x, pts[0].y, pts[1].x, pts[1].y + 1 );
2654 if (pts[0].x < extents->left)
2655 extents->left = pts[0].x;
2656 if (pts[1].x > extents->right)
2657 extents->right = pts[1].x;
2661 if (reg->numRects) {
2662 REGION_Coalesce( reg, prev_band, cur_band );
2663 extents->top = reg->rects[0].top;
2664 extents->bottom = reg->rects[reg->numRects-1].bottom;
2665 } else {
2666 extents->left = 0;
2667 extents->top = 0;
2668 extents->right = 0;
2669 extents->bottom = 0;
2671 REGION_compact( reg );
2673 return reg;
2676 /* Number of points in the first point buffer. Must be an even number. */
2677 #define NUMPTSTOBUFFER 200
2679 /***********************************************************************
2680 * CreatePolyPolygonRgn (GDI32.@)
2682 HRGN WINAPI CreatePolyPolygonRgn(const POINT *Pts, const INT *Count,
2683 INT nbpolygons, INT mode)
2685 HRGN hrgn = 0;
2686 WINEREGION *obj;
2687 INT y; /* current scanline */
2688 struct list WETE, *pWETE; /* Winding Edge Table */
2689 ScanLineList *pSLL; /* current scanLineList */
2690 EdgeTable ET; /* header node for ET */
2691 struct list AET; /* header for AET */
2692 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2693 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2694 BOOL fixWAET = FALSE;
2695 char first_blk_buf[FIELD_OFFSET( struct point_block, pts[NUMPTSTOBUFFER] )];
2696 struct point_block *first_block = (struct point_block *)first_blk_buf, *block;
2697 struct edge_table_entry *active, *next;
2698 INT poly, total;
2700 TRACE("%p, count %d, polygons %d, mode %d\n", Pts, *Count, nbpolygons, mode);
2702 /* special case a rectangle */
2704 if (((nbpolygons == 1) && ((*Count == 4) ||
2705 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2706 (((Pts[0].y == Pts[1].y) &&
2707 (Pts[1].x == Pts[2].x) &&
2708 (Pts[2].y == Pts[3].y) &&
2709 (Pts[3].x == Pts[0].x)) ||
2710 ((Pts[0].x == Pts[1].x) &&
2711 (Pts[1].y == Pts[2].y) &&
2712 (Pts[2].x == Pts[3].x) &&
2713 (Pts[3].y == Pts[0].y))))
2714 return CreateRectRgn( min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2715 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2717 for(poly = total = 0; poly < nbpolygons; poly++)
2718 total += Count[poly];
2719 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2720 return 0;
2722 REGION_CreateEdgeTable(Count, nbpolygons, Pts, &ET, pETEs, &SLLBlock);
2723 list_init( &AET );
2724 pSLL = ET.scanlines.next;
2725 block = first_block;
2726 first_block->count = 0;
2727 first_block->size = NUMPTSTOBUFFER;
2728 first_block->next = NULL;
2730 if (mode != WINDING) {
2732 * for each scanline
2734 for (y = ET.ymin; y < ET.ymax; y++) {
2736 * Add a new edge to the active edge table when we
2737 * get to the next edge.
2739 if (pSLL != NULL && y == pSLL->scanline) {
2740 REGION_loadAET(&AET, &pSLL->edgelist);
2741 pSLL = pSLL->next;
2744 LIST_FOR_EACH_ENTRY_SAFE( active, next, &AET, struct edge_table_entry, entry )
2746 block = add_point( block, active->bres.minor_axis, y );
2747 if (!block) goto done;
2749 if (active->ymax == y) /* leaving this edge */
2750 list_remove( &active->entry );
2751 else
2752 bres_incr_polygon( &active->bres );
2754 REGION_InsertionSort(&AET);
2757 else {
2759 * for each scanline
2761 for (y = ET.ymin; y < ET.ymax; y++) {
2763 * Add a new edge to the active edge table when we
2764 * get to the next edge.
2766 if (pSLL != NULL && y == pSLL->scanline) {
2767 REGION_loadAET(&AET, &pSLL->edgelist);
2768 REGION_computeWAET( &AET, &WETE );
2769 pSLL = pSLL->next;
2771 pWETE = list_head( &WETE );
2774 * for each active edge
2776 LIST_FOR_EACH_ENTRY_SAFE( active, next, &AET, struct edge_table_entry, entry )
2779 * add to the buffer only those edges that
2780 * are in the Winding active edge table.
2782 if (pWETE == &active->winding_entry) {
2783 block = add_point( block, active->bres.minor_axis, y );
2784 if (!block) goto done;
2785 pWETE = list_next( &WETE, pWETE );
2787 if (active->ymax == y) /* leaving this edge */
2789 list_remove( &active->entry );
2790 fixWAET = TRUE;
2792 else
2793 bres_incr_polygon( &active->bres );
2797 * recompute the winding active edge table if
2798 * we just resorted or have exited an edge.
2800 if (REGION_InsertionSort(&AET) || fixWAET) {
2801 REGION_computeWAET( &AET, &WETE );
2802 fixWAET = FALSE;
2807 if (!(obj = REGION_PtsToRegion( first_block ))) goto done;
2808 if (!(hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs )))
2809 free_region( obj );
2811 done:
2812 REGION_FreeStorage(SLLBlock.next);
2813 free_point_blocks( first_block->next );
2814 HeapFree( GetProcessHeap(), 0, pETEs );
2815 return hrgn;
2819 /***********************************************************************
2820 * CreatePolygonRgn (GDI32.@)
2822 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count,
2823 INT mode )
2825 return CreatePolyPolygonRgn( points, &count, 1, mode );