wbemdisp: Implement GetObjectText_().
[wine/zf.git] / dlls / gdi32 / region.c
blobb3764b31442a151195cee07acf37217a8568d196
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 <assert.h>
98 #include <stdarg.h>
99 #include <stdlib.h>
100 #include <string.h>
101 #include "windef.h"
102 #include "winbase.h"
103 #include "wingdi.h"
104 #include "gdi_private.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(region);
110 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc );
111 static BOOL REGION_DeleteObject( HGDIOBJ handle );
113 static const struct gdi_obj_funcs region_funcs =
115 REGION_SelectObject, /* pSelectObject */
116 NULL, /* pGetObjectA */
117 NULL, /* pGetObjectW */
118 NULL, /* pUnrealizeObject */
119 REGION_DeleteObject /* pDeleteObject */
122 /* Check if two RECTs overlap. */
123 static inline BOOL overlapping( const RECT *r1, const RECT *r2 )
125 return (r1->right > r2->left && r1->left < r2->right &&
126 r1->bottom > r2->top && r1->top < r2->bottom);
129 static BOOL grow_region( WINEREGION *rgn, int size )
131 RECT *new_rects;
133 if (size <= rgn->size) return TRUE;
135 if (rgn->rects == rgn->rects_buf)
137 new_rects = HeapAlloc( GetProcessHeap(), 0, size * sizeof(RECT) );
138 if (!new_rects) return FALSE;
139 memcpy( new_rects, rgn->rects, rgn->numRects * sizeof(RECT) );
141 else
143 new_rects = HeapReAlloc( GetProcessHeap(), 0, rgn->rects, size * sizeof(RECT) );
144 if (!new_rects) return FALSE;
146 rgn->rects = new_rects;
147 rgn->size = size;
148 return TRUE;
151 static BOOL add_rect( WINEREGION *reg, INT left, INT top, INT right, INT bottom )
153 RECT *rect;
154 if (reg->numRects >= reg->size && !grow_region( reg, 2 * reg->size ))
155 return FALSE;
157 rect = reg->rects + reg->numRects++;
158 rect->left = left;
159 rect->top = top;
160 rect->right = right;
161 rect->bottom = bottom;
162 return TRUE;
165 static inline void empty_region( WINEREGION *reg )
167 reg->numRects = 0;
168 reg->extents.left = reg->extents.top = reg->extents.right = reg->extents.bottom = 0;
171 static inline BOOL is_in_rect( const RECT *rect, int x, int y )
173 return (rect->right > x && rect->left <= x && rect->bottom > y && rect->top <= y);
178 * This file contains a few macros to help track
179 * the edge of a filled object. The object is assumed
180 * to be filled in scanline order, and thus the
181 * algorithm used is an extension of Bresenham's line
182 * drawing algorithm which assumes that y is always the
183 * major axis.
184 * Since these pieces of code are the same for any filled shape,
185 * it is more convenient to gather the library in one
186 * place, but since these pieces of code are also in
187 * the inner loops of output primitives, procedure call
188 * overhead is out of the question.
189 * See the author for a derivation if needed.
194 * This structure contains all of the information needed
195 * to run the bresenham algorithm.
196 * The variables may be hardcoded into the declarations
197 * instead of using this structure to make use of
198 * register declarations.
200 struct bres_info
202 INT minor_axis; /* minor axis */
203 INT d; /* decision variable */
204 INT m, m1; /* slope and slope+1 */
205 INT incr1, incr2; /* error increments */
210 * In scan converting polygons, we want to choose those pixels
211 * which are inside the polygon. Thus, we add .5 to the starting
212 * x coordinate for both left and right edges. Now we choose the
213 * first pixel which is inside the pgon for the left edge and the
214 * first pixel which is outside the pgon for the right edge.
215 * Draw the left pixel, but not the right.
217 * How to add .5 to the starting x coordinate:
218 * If the edge is moving to the right, then subtract dy from the
219 * error term from the general form of the algorithm.
220 * If the edge is moving to the left, then add dy to the error term.
222 * The reason for the difference between edges moving to the left
223 * and edges moving to the right is simple: If an edge is moving
224 * to the right, then we want the algorithm to flip immediately.
225 * If it is moving to the left, then we don't want it to flip until
226 * we traverse an entire pixel.
228 static inline void bres_init_polygon( int dy, int x1, int x2, struct bres_info *bres )
230 int dx;
233 * if the edge is horizontal, then it is ignored
234 * and assumed not to be processed. Otherwise, do this stuff.
236 if (!dy) return;
238 bres->minor_axis = x1;
239 dx = x2 - x1;
240 if (dx < 0)
242 bres->m = dx / dy;
243 bres->m1 = bres->m - 1;
244 bres->incr1 = -2 * dx + 2 * dy * bres->m1;
245 bres->incr2 = -2 * dx + 2 * dy * bres->m;
246 bres->d = 2 * bres->m * dy - 2 * dx - 2 * dy;
248 else
250 bres->m = dx / (dy);
251 bres->m1 = bres->m + 1;
252 bres->incr1 = 2 * dx - 2 * dy * bres->m1;
253 bres->incr2 = 2 * dx - 2 * dy * bres->m;
254 bres->d = -2 * bres->m * dy + 2 * dx;
258 static inline void bres_incr_polygon( struct bres_info *bres )
260 if (bres->m1 > 0) {
261 if (bres->d > 0) {
262 bres->minor_axis += bres->m1;
263 bres->d += bres->incr1;
265 else {
266 bres->minor_axis += bres->m;
267 bres->d += bres->incr2;
269 } else {
270 if (bres->d >= 0) {
271 bres->minor_axis += bres->m1;
272 bres->d += bres->incr1;
274 else {
275 bres->minor_axis += bres->m;
276 bres->d += bres->incr2;
283 * These are the data structures needed to scan
284 * convert regions. Two different scan conversion
285 * methods are available -- the even-odd method, and
286 * the winding number method.
287 * The even-odd rule states that a point is inside
288 * the polygon if a ray drawn from that point in any
289 * direction will pass through an odd number of
290 * path segments.
291 * By the winding number rule, a point is decided
292 * to be inside the polygon if a ray drawn from that
293 * point in any direction passes through a different
294 * number of clockwise and counter-clockwise path
295 * segments.
297 * These data structures are adapted somewhat from
298 * the algorithm in (Foley/Van Dam) for scan converting
299 * polygons.
300 * The basic algorithm is to start at the top (smallest y)
301 * of the polygon, stepping down to the bottom of
302 * the polygon by incrementing the y coordinate. We
303 * keep a list of edges which the current scanline crosses,
304 * sorted by x. This list is called the Active Edge Table (AET)
305 * As we change the y-coordinate, we update each entry in
306 * in the active edge table to reflect the edges new xcoord.
307 * This list must be sorted at each scanline in case
308 * two edges intersect.
309 * We also keep a data structure known as the Edge Table (ET),
310 * which keeps track of all the edges which the current
311 * scanline has not yet reached. The ET is basically a
312 * list of ScanLineList structures containing a list of
313 * edges which are entered at a given scanline. There is one
314 * ScanLineList per scanline at which an edge is entered.
315 * When we enter a new edge, we move it from the ET to the AET.
317 * From the AET, we can implement the even-odd rule as in
318 * (Foley/Van Dam).
319 * The winding number rule is a little trickier. We also
320 * keep the EdgeTableEntries in the AET linked by the
321 * nextWETE (winding EdgeTableEntry) link. This allows
322 * the edges to be linked just as before for updating
323 * purposes, but only uses the edges linked by the nextWETE
324 * link as edges representing spans of the polygon to
325 * drawn (as with the even-odd rule).
328 typedef struct edge_table_entry {
329 struct list entry;
330 struct list winding_entry;
331 INT ymax; /* ycoord at which we exit this edge. */
332 struct bres_info bres; /* Bresenham info to run the edge */
333 int ClockWise; /* flag for winding number rule */
334 } EdgeTableEntry;
337 typedef struct _ScanLineList{
338 struct list edgelist;
339 INT scanline; /* the scanline represented */
340 struct _ScanLineList *next; /* next in the list */
341 } ScanLineList;
344 typedef struct {
345 INT ymax; /* ymax for the polygon */
346 INT ymin; /* ymin for the polygon */
347 ScanLineList scanlines; /* header node */
348 } EdgeTable;
352 * Here is a struct to help with storage allocation
353 * so we can allocate a big chunk at a time, and then take
354 * pieces from this heap when we need to.
356 #define SLLSPERBLOCK 25
358 typedef struct _ScanLineListBlock {
359 ScanLineList SLLs[SLLSPERBLOCK];
360 struct _ScanLineListBlock *next;
361 } ScanLineListBlock;
364 /* Note the parameter order is different from the X11 equivalents */
366 static BOOL REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
367 static BOOL REGION_OffsetRegion(WINEREGION *d, WINEREGION *s, INT x, INT y);
368 static BOOL REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
369 static BOOL REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
370 static BOOL REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
371 static BOOL REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
372 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
374 /***********************************************************************
375 * get_region_type
377 static inline INT get_region_type( const WINEREGION *obj )
379 switch(obj->numRects)
381 case 0: return NULLREGION;
382 case 1: return SIMPLEREGION;
383 default: return COMPLEXREGION;
388 /***********************************************************************
389 * REGION_DumpRegion
390 * Outputs the contents of a WINEREGION
392 static void REGION_DumpRegion(WINEREGION *pReg)
394 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
396 TRACE("Region %p: %s %d rects\n", pReg, wine_dbgstr_rect(&pReg->extents), pReg->numRects);
397 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
398 TRACE("\t%s\n", wine_dbgstr_rect(pRect));
399 return;
403 /***********************************************************************
404 * init_region
406 * Initialize a new empty region.
408 static BOOL init_region( WINEREGION *pReg, INT n )
410 n = max( n, RGN_DEFAULT_RECTS );
412 if (n > RGN_DEFAULT_RECTS)
414 if (n > INT_MAX / sizeof(RECT)) return FALSE;
415 if (!(pReg->rects = HeapAlloc( GetProcessHeap(), 0, n * sizeof( RECT ) )))
416 return FALSE;
418 else
419 pReg->rects = pReg->rects_buf;
421 pReg->size = n;
422 empty_region(pReg);
423 return TRUE;
426 /***********************************************************************
427 * destroy_region
429 static void destroy_region( WINEREGION *pReg )
431 if (pReg->rects != pReg->rects_buf)
432 HeapFree( GetProcessHeap(), 0, pReg->rects );
435 /***********************************************************************
436 * free_region
438 static void free_region( WINEREGION *rgn )
440 destroy_region( rgn );
441 HeapFree( GetProcessHeap(), 0, rgn );
444 /***********************************************************************
445 * alloc_region
447 static WINEREGION *alloc_region( INT n )
449 WINEREGION *rgn = HeapAlloc( GetProcessHeap(), 0, sizeof(*rgn) );
451 if (rgn && !init_region( rgn, n ))
453 free_region( rgn );
454 rgn = NULL;
456 return rgn;
459 /************************************************************
460 * move_rects
462 * Move rectangles from src to dst leaving src with no rectangles.
464 static inline void move_rects( WINEREGION *dst, WINEREGION *src )
466 destroy_region( dst );
467 if (src->rects == src->rects_buf)
469 dst->rects = dst->rects_buf;
470 memcpy( dst->rects, src->rects, src->numRects * sizeof(RECT) );
472 else
473 dst->rects = src->rects;
474 dst->size = src->size;
475 dst->numRects = src->numRects;
476 init_region( src, 0 );
479 /***********************************************************************
480 * REGION_DeleteObject
482 static BOOL REGION_DeleteObject( HGDIOBJ handle )
484 WINEREGION *rgn = free_gdi_handle( handle );
486 if (!rgn) return FALSE;
487 free_region( rgn );
488 return TRUE;
491 /***********************************************************************
492 * REGION_SelectObject
494 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, HDC hdc )
496 return ULongToHandle(SelectClipRgn( hdc, handle ));
500 /***********************************************************************
501 * REGION_OffsetRegion
502 * Offset a WINEREGION by x,y
504 static BOOL REGION_OffsetRegion( WINEREGION *rgn, WINEREGION *srcrgn, INT x, INT y )
506 if( rgn != srcrgn)
508 if (!REGION_CopyRegion( rgn, srcrgn)) return FALSE;
510 if(x || y) {
511 int nbox = rgn->numRects;
512 RECT *pbox = rgn->rects;
514 if(nbox) {
515 while(nbox--) {
516 pbox->left += x;
517 pbox->right += x;
518 pbox->top += y;
519 pbox->bottom += y;
520 pbox++;
522 rgn->extents.left += x;
523 rgn->extents.right += x;
524 rgn->extents.top += y;
525 rgn->extents.bottom += y;
528 return TRUE;
531 /***********************************************************************
532 * OffsetRgn (GDI32.@)
534 * Moves a region by the specified X- and Y-axis offsets.
536 * PARAMS
537 * hrgn [I] Region to offset.
538 * x [I] Offset right if positive or left if negative.
539 * y [I] Offset down if positive or up if negative.
541 * RETURNS
542 * Success:
543 * NULLREGION - The new region is empty.
544 * SIMPLEREGION - The new region can be represented by one rectangle.
545 * COMPLEXREGION - The new region can only be represented by more than
546 * one rectangle.
547 * Failure: ERROR
549 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
551 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
552 INT ret;
554 TRACE("%p %d,%d\n", hrgn, x, y);
556 if (!obj)
557 return ERROR;
559 REGION_OffsetRegion( obj, obj, x, y);
561 ret = get_region_type( obj );
562 GDI_ReleaseObj( hrgn );
563 return ret;
567 /***********************************************************************
568 * GetRgnBox (GDI32.@)
570 * Retrieves the bounding rectangle of the region. The bounding rectangle
571 * is the smallest rectangle that contains the entire region.
573 * PARAMS
574 * hrgn [I] Region to retrieve bounding rectangle from.
575 * rect [O] Rectangle that will receive the coordinates of the bounding
576 * rectangle.
578 * RETURNS
579 * NULLREGION - The new region is empty.
580 * SIMPLEREGION - The new region can be represented by one rectangle.
581 * COMPLEXREGION - The new region can only be represented by more than
582 * one rectangle.
584 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
586 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
587 if (obj)
589 INT ret;
590 rect->left = obj->extents.left;
591 rect->top = obj->extents.top;
592 rect->right = obj->extents.right;
593 rect->bottom = obj->extents.bottom;
594 TRACE("%p %s\n", hrgn, wine_dbgstr_rect(rect));
595 ret = get_region_type( obj );
596 GDI_ReleaseObj(hrgn);
597 return ret;
599 return ERROR;
603 /***********************************************************************
604 * CreateRectRgn (GDI32.@)
606 * Creates a simple rectangular region.
608 * PARAMS
609 * left [I] Left coordinate of rectangle.
610 * top [I] Top coordinate of rectangle.
611 * right [I] Right coordinate of rectangle.
612 * bottom [I] Bottom coordinate of rectangle.
614 * RETURNS
615 * Success: Handle to region.
616 * Failure: NULL.
618 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
620 HRGN hrgn;
621 WINEREGION *obj;
623 if (!(obj = alloc_region( RGN_DEFAULT_RECTS ))) return 0;
625 if (!(hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs )))
627 free_region( obj );
628 return 0;
630 TRACE( "%d,%d-%d,%d returning %p\n", left, top, right, bottom, hrgn );
631 SetRectRgn(hrgn, left, top, right, bottom);
632 return hrgn;
636 /***********************************************************************
637 * CreateRectRgnIndirect (GDI32.@)
639 * Creates a simple rectangular region.
641 * PARAMS
642 * rect [I] Coordinates of rectangular region.
644 * RETURNS
645 * Success: Handle to region.
646 * Failure: NULL.
648 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
650 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
654 /***********************************************************************
655 * SetRectRgn (GDI32.@)
657 * Sets a region to a simple rectangular region.
659 * PARAMS
660 * hrgn [I] Region to convert.
661 * left [I] Left coordinate of rectangle.
662 * top [I] Top coordinate of rectangle.
663 * right [I] Right coordinate of rectangle.
664 * bottom [I] Bottom coordinate of rectangle.
666 * RETURNS
667 * Success: Non-zero.
668 * Failure: Zero.
670 * NOTES
671 * Allows either or both left and top to be greater than right or bottom.
673 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
674 INT right, INT bottom )
676 WINEREGION *obj;
678 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
680 if (!(obj = GDI_GetObjPtr( hrgn, OBJ_REGION ))) return FALSE;
682 if (left > right) { INT tmp = left; left = right; right = tmp; }
683 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
685 if((left != right) && (top != bottom))
687 obj->rects->left = obj->extents.left = left;
688 obj->rects->top = obj->extents.top = top;
689 obj->rects->right = obj->extents.right = right;
690 obj->rects->bottom = obj->extents.bottom = bottom;
691 obj->numRects = 1;
693 else
694 empty_region(obj);
696 GDI_ReleaseObj( hrgn );
697 return TRUE;
701 /***********************************************************************
702 * CreateRoundRectRgn (GDI32.@)
704 * Creates a rectangular region with rounded corners.
706 * PARAMS
707 * left [I] Left coordinate of rectangle.
708 * top [I] Top coordinate of rectangle.
709 * right [I] Right coordinate of rectangle.
710 * bottom [I] Bottom coordinate of rectangle.
711 * ellipse_width [I] Width of the ellipse at each corner.
712 * ellipse_height [I] Height of the ellipse at each corner.
714 * RETURNS
715 * Success: Handle to region.
716 * Failure: NULL.
718 * NOTES
719 * If ellipse_width or ellipse_height is less than 2 logical units then
720 * it is treated as though CreateRectRgn() was called instead.
722 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
723 INT right, INT bottom,
724 INT ellipse_width, INT ellipse_height )
726 WINEREGION *obj;
727 HRGN hrgn = 0;
728 int a, b, i, x, y;
729 INT64 asq, bsq, dx, dy, err;
730 RECT *rects;
732 /* Make the dimensions sensible */
734 if (left > right) { INT tmp = left; left = right; right = tmp; }
735 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
736 /* the region is for the rectangle interior, but only at right and bottom for some reason */
737 right--;
738 bottom--;
740 ellipse_width = min( right - left, abs( ellipse_width ));
741 ellipse_height = min( bottom - top, abs( ellipse_height ));
743 /* Check if we can do a normal rectangle instead */
745 if ((ellipse_width < 2) || (ellipse_height < 2))
746 return CreateRectRgn( left, top, right, bottom );
748 if (!(obj = alloc_region( ellipse_height ))) return 0;
749 obj->numRects = ellipse_height;
750 obj->extents.left = left;
751 obj->extents.top = top;
752 obj->extents.right = right;
753 obj->extents.bottom = bottom;
754 rects = obj->rects;
756 /* based on an algorithm by Alois Zingl */
758 a = ellipse_width - 1;
759 b = ellipse_height - 1;
760 asq = (INT64)8 * a * a;
761 bsq = (INT64)8 * b * b;
762 dx = (INT64)4 * b * b * (1 - a);
763 dy = (INT64)4 * a * a * (1 + (b % 2));
764 err = dx + dy + a * a * (b % 2);
766 x = 0;
767 y = ellipse_height / 2;
769 rects[y].left = left;
770 rects[y].right = right;
772 while (x <= ellipse_width / 2)
774 INT64 e2 = 2 * err;
775 if (e2 >= dx)
777 x++;
778 err += dx += bsq;
780 if (e2 <= dy)
782 y++;
783 err += dy += asq;
784 rects[y].left = left + x;
785 rects[y].right = right - x;
788 for (i = 0; i < ellipse_height / 2; i++)
790 rects[i].left = rects[b - i].left;
791 rects[i].right = rects[b - i].right;
792 rects[i].top = top + i;
793 rects[i].bottom = rects[i].top + 1;
795 for (; i < ellipse_height; i++)
797 rects[i].top = bottom - ellipse_height + i;
798 rects[i].bottom = rects[i].top + 1;
800 rects[ellipse_height / 2].top = top + ellipse_height / 2; /* extend to top of rectangle */
802 hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs );
804 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
805 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
806 if (!hrgn) free_region( obj );
807 return hrgn;
811 /***********************************************************************
812 * CreateEllipticRgn (GDI32.@)
814 * Creates an elliptical region.
816 * PARAMS
817 * left [I] Left coordinate of bounding rectangle.
818 * top [I] Top coordinate of bounding rectangle.
819 * right [I] Right coordinate of bounding rectangle.
820 * bottom [I] Bottom coordinate of bounding rectangle.
822 * RETURNS
823 * Success: Handle to region.
824 * Failure: NULL.
826 * NOTES
827 * This is a special case of CreateRoundRectRgn() where the width of the
828 * ellipse at each corner is equal to the width the rectangle and
829 * the same for the height.
831 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
832 INT right, INT bottom )
834 return CreateRoundRectRgn( left, top, right, bottom,
835 right-left, bottom-top );
839 /***********************************************************************
840 * CreateEllipticRgnIndirect (GDI32.@)
842 * Creates an elliptical region.
844 * PARAMS
845 * rect [I] Pointer to bounding rectangle of the ellipse.
847 * RETURNS
848 * Success: Handle to region.
849 * Failure: NULL.
851 * NOTES
852 * This is a special case of CreateRoundRectRgn() where the width of the
853 * ellipse at each corner is equal to the width the rectangle and
854 * the same for the height.
856 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
858 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
859 rect->bottom, rect->right - rect->left,
860 rect->bottom - rect->top );
863 /***********************************************************************
864 * GetRegionData (GDI32.@)
866 * Retrieves the data that specifies the region.
868 * PARAMS
869 * hrgn [I] Region to retrieve the region data from.
870 * count [I] The size of the buffer pointed to by rgndata in bytes.
871 * rgndata [I] The buffer to receive data about the region.
873 * RETURNS
874 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
875 * the number of bytes copied to the output buffer.
876 * Failure: 0.
878 * NOTES
879 * The format of the Buffer member of RGNDATA is determined by the iType
880 * member of the region data header.
881 * Currently this is always RDH_RECTANGLES, which specifies that the format
882 * is the array of RECT's that specify the region. The length of the array
883 * is specified by the nCount member of the region data header.
885 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
887 DWORD size;
888 WINEREGION *obj = GDI_GetObjPtr( hrgn, OBJ_REGION );
890 TRACE(" %p count = %d, rgndata = %p\n", hrgn, count, rgndata);
892 if(!obj) return 0;
894 size = obj->numRects * sizeof(RECT);
895 if (!rgndata || count < FIELD_OFFSET(RGNDATA, Buffer[size]))
897 GDI_ReleaseObj( hrgn );
898 if (rgndata) /* buffer is too small, signal it by return 0 */
899 return 0;
900 /* user requested buffer size with NULL rgndata */
901 return FIELD_OFFSET(RGNDATA, Buffer[size]);
904 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
905 rgndata->rdh.iType = RDH_RECTANGLES;
906 rgndata->rdh.nCount = obj->numRects;
907 rgndata->rdh.nRgnSize = size;
908 rgndata->rdh.rcBound.left = obj->extents.left;
909 rgndata->rdh.rcBound.top = obj->extents.top;
910 rgndata->rdh.rcBound.right = obj->extents.right;
911 rgndata->rdh.rcBound.bottom = obj->extents.bottom;
913 memcpy( rgndata->Buffer, obj->rects, size );
915 GDI_ReleaseObj( hrgn );
916 return FIELD_OFFSET(RGNDATA, Buffer[size]);
920 static void translate( POINT *pt, UINT count, const XFORM *xform )
922 while (count--)
924 double x = pt->x;
925 double y = pt->y;
926 pt->x = floor( x * xform->eM11 + y * xform->eM21 + xform->eDx + 0.5 );
927 pt->y = floor( x * xform->eM12 + y * xform->eM22 + xform->eDy + 0.5 );
928 pt++;
933 /***********************************************************************
934 * ExtCreateRegion (GDI32.@)
936 * Creates a region as specified by the transformation data and region data.
938 * PARAMS
939 * lpXform [I] World-space to logical-space transformation data.
940 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
941 * rgndata [I] Data that specifies the region.
943 * RETURNS
944 * Success: Handle to region.
945 * Failure: NULL.
947 * NOTES
948 * See GetRegionData().
950 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
952 HRGN hrgn = 0;
953 WINEREGION *obj;
954 const RECT *pCurRect, *pEndRect;
956 if (!rgndata)
958 SetLastError( ERROR_INVALID_PARAMETER );
959 return 0;
962 if (rgndata->rdh.dwSize < sizeof(RGNDATAHEADER))
963 return 0;
965 /* XP doesn't care about the type */
966 if( rgndata->rdh.iType != RDH_RECTANGLES )
967 WARN("(Unsupported region data type: %u)\n", rgndata->rdh.iType);
969 if (lpXform)
971 const RECT *pCurRect, *pEndRect;
973 hrgn = CreateRectRgn( 0, 0, 0, 0 );
975 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
976 for (pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
978 static const INT count = 4;
979 HRGN poly_hrgn;
980 POINT pt[4];
982 pt[0].x = pCurRect->left;
983 pt[0].y = pCurRect->top;
984 pt[1].x = pCurRect->right;
985 pt[1].y = pCurRect->top;
986 pt[2].x = pCurRect->right;
987 pt[2].y = pCurRect->bottom;
988 pt[3].x = pCurRect->left;
989 pt[3].y = pCurRect->bottom;
991 translate( pt, 4, lpXform );
992 poly_hrgn = CreatePolyPolygonRgn( pt, &count, 1, WINDING );
993 CombineRgn( hrgn, hrgn, poly_hrgn, RGN_OR );
994 DeleteObject( poly_hrgn );
996 return hrgn;
999 if (!(obj = alloc_region( rgndata->rdh.nCount ))) return 0;
1001 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1002 for(pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1004 if (pCurRect->left < pCurRect->right && pCurRect->top < pCurRect->bottom)
1006 if (!REGION_UnionRectWithRegion( pCurRect, obj )) goto done;
1009 hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs );
1011 done:
1012 if (!hrgn) free_region( obj );
1014 TRACE("%p %d %p returning %p\n", lpXform, dwCount, rgndata, hrgn );
1015 return hrgn;
1019 /***********************************************************************
1020 * PtInRegion (GDI32.@)
1022 * Tests whether the specified point is inside a region.
1024 * PARAMS
1025 * hrgn [I] Region to test.
1026 * x [I] X-coordinate of point to test.
1027 * y [I] Y-coordinate of point to test.
1029 * RETURNS
1030 * Non-zero if the point is inside the region or zero otherwise.
1032 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
1034 WINEREGION *obj;
1035 BOOL ret = FALSE;
1037 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1039 if (obj->numRects > 0 && is_in_rect( &obj->extents, x, y ))
1040 region_find_pt( obj, x, y, &ret );
1041 GDI_ReleaseObj( hrgn );
1043 return ret;
1047 /***********************************************************************
1048 * RectInRegion (GDI32.@)
1050 * Tests if a rectangle is at least partly inside the specified region.
1052 * PARAMS
1053 * hrgn [I] Region to test.
1054 * rect [I] Rectangle to test.
1056 * RETURNS
1057 * Non-zero if the rectangle is partially inside the region or
1058 * zero otherwise.
1060 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
1062 WINEREGION *obj;
1063 BOOL ret = FALSE;
1064 RECT rc;
1065 int i;
1067 /* swap the coordinates to make right >= left and bottom >= top */
1068 /* (region building rectangles are normalized the same way) */
1069 rc = *rect;
1070 order_rect( &rc );
1072 if ((obj = GDI_GetObjPtr( hrgn, OBJ_REGION )))
1074 if ((obj->numRects > 0) && overlapping(&obj->extents, &rc))
1076 for (i = region_find_pt( obj, rc.left, rc.top, &ret ); !ret && i < obj->numRects; i++ )
1078 if (obj->rects[i].bottom <= rc.top)
1079 continue; /* not far enough down yet */
1081 if (obj->rects[i].top >= rc.bottom)
1082 break; /* too far down */
1084 if (obj->rects[i].right <= rc.left)
1085 continue; /* not far enough over yet */
1087 if (obj->rects[i].left >= rc.right)
1088 continue;
1090 ret = TRUE;
1093 GDI_ReleaseObj(hrgn);
1095 return ret;
1098 /***********************************************************************
1099 * EqualRgn (GDI32.@)
1101 * Tests whether one region is identical to another.
1103 * PARAMS
1104 * hrgn1 [I] The first region to compare.
1105 * hrgn2 [I] The second region to compare.
1107 * RETURNS
1108 * Non-zero if both regions are identical or zero otherwise.
1110 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
1112 WINEREGION *obj1, *obj2;
1113 BOOL ret = FALSE;
1115 if ((obj1 = GDI_GetObjPtr( hrgn1, OBJ_REGION )))
1117 if ((obj2 = GDI_GetObjPtr( hrgn2, OBJ_REGION )))
1119 int i;
1121 if ( obj1->numRects != obj2->numRects ) goto done;
1122 if ( obj1->numRects == 0 )
1124 ret = TRUE;
1125 goto done;
1128 if (obj1->extents.left != obj2->extents.left) goto done;
1129 if (obj1->extents.right != obj2->extents.right) goto done;
1130 if (obj1->extents.top != obj2->extents.top) goto done;
1131 if (obj1->extents.bottom != obj2->extents.bottom) goto done;
1132 for( i = 0; i < obj1->numRects; i++ )
1134 if (obj1->rects[i].left != obj2->rects[i].left) goto done;
1135 if (obj1->rects[i].right != obj2->rects[i].right) goto done;
1136 if (obj1->rects[i].top != obj2->rects[i].top) goto done;
1137 if (obj1->rects[i].bottom != obj2->rects[i].bottom) goto done;
1139 ret = TRUE;
1140 done:
1141 GDI_ReleaseObj(hrgn2);
1143 GDI_ReleaseObj(hrgn1);
1145 return ret;
1148 /***********************************************************************
1149 * REGION_UnionRectWithRegion
1150 * Adds a rectangle to a WINEREGION
1152 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1154 WINEREGION region;
1156 init_region( &region, 1 );
1157 region.numRects = 1;
1158 region.extents = *region.rects = *rect;
1159 return REGION_UnionRegion(rgn, rgn, &region);
1163 BOOL add_rect_to_region( HRGN rgn, const RECT *rect )
1165 WINEREGION *obj = GDI_GetObjPtr( rgn, OBJ_REGION );
1166 BOOL ret;
1168 if (!obj) return FALSE;
1169 ret = REGION_UnionRectWithRegion( rect, obj );
1170 GDI_ReleaseObj( rgn );
1171 return ret;
1174 /***********************************************************************
1175 * REGION_CreateFrameRgn
1177 * Create a region that is a frame around another region.
1178 * Compute the intersection of the region moved in all 4 directions
1179 * ( +x, -x, +y, -y) and subtract from the original.
1180 * The result looks slightly better than in Windows :)
1182 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1184 WINEREGION tmprgn;
1185 BOOL bRet = FALSE;
1186 WINEREGION* destObj = NULL;
1187 WINEREGION *srcObj = GDI_GetObjPtr( hSrc, OBJ_REGION );
1189 tmprgn.rects = NULL;
1190 if (!srcObj) return FALSE;
1191 if (srcObj->numRects != 0)
1193 if (!(destObj = GDI_GetObjPtr( hDest, OBJ_REGION ))) goto done;
1194 if (!init_region( &tmprgn, srcObj->numRects )) goto done;
1196 if (!REGION_OffsetRegion( destObj, srcObj, -x, 0)) goto done;
1197 if (!REGION_OffsetRegion( &tmprgn, srcObj, x, 0)) goto done;
1198 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1199 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, -y)) goto done;
1200 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1201 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, y)) goto done;
1202 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1203 if (!REGION_SubtractRegion( destObj, srcObj, destObj )) goto done;
1204 bRet = TRUE;
1206 done:
1207 destroy_region( &tmprgn );
1208 if (destObj) GDI_ReleaseObj ( hDest );
1209 GDI_ReleaseObj( hSrc );
1210 return bRet;
1214 /***********************************************************************
1215 * CombineRgn (GDI32.@)
1217 * Combines two regions with the specified operation and stores the result
1218 * in the specified destination region.
1220 * PARAMS
1221 * hDest [I] The region that receives the combined result.
1222 * hSrc1 [I] The first source region.
1223 * hSrc2 [I] The second source region.
1224 * mode [I] The way in which the source regions will be combined. See notes.
1226 * RETURNS
1227 * Success:
1228 * NULLREGION - The new region is empty.
1229 * SIMPLEREGION - The new region can be represented by one rectangle.
1230 * COMPLEXREGION - The new region can only be represented by more than
1231 * one rectangle.
1232 * Failure: ERROR
1234 * NOTES
1235 * The two source regions can be the same region.
1236 * The mode can be one of the following:
1237 *| RGN_AND - Intersection of the regions
1238 *| RGN_OR - Union of the regions
1239 *| RGN_XOR - Unions of the regions minus any intersection.
1240 *| RGN_DIFF - Difference (subtraction) of the regions.
1242 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1244 WINEREGION *destObj = GDI_GetObjPtr( hDest, OBJ_REGION );
1245 INT result = ERROR;
1247 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1248 if (destObj)
1250 WINEREGION *src1Obj = GDI_GetObjPtr( hSrc1, OBJ_REGION );
1252 if (src1Obj)
1254 TRACE("dump src1Obj:\n");
1255 if(TRACE_ON(region))
1256 REGION_DumpRegion(src1Obj);
1257 if (mode == RGN_COPY)
1259 if (REGION_CopyRegion( destObj, src1Obj ))
1260 result = get_region_type( destObj );
1262 else
1264 WINEREGION *src2Obj = GDI_GetObjPtr( hSrc2, OBJ_REGION );
1266 if (src2Obj)
1268 TRACE("dump src2Obj:\n");
1269 if(TRACE_ON(region))
1270 REGION_DumpRegion(src2Obj);
1271 switch (mode)
1273 case RGN_AND:
1274 if (REGION_IntersectRegion( destObj, src1Obj, src2Obj ))
1275 result = get_region_type( destObj );
1276 break;
1277 case RGN_OR:
1278 if (REGION_UnionRegion( destObj, src1Obj, src2Obj ))
1279 result = get_region_type( destObj );
1280 break;
1281 case RGN_XOR:
1282 if (REGION_XorRegion( destObj, src1Obj, src2Obj ))
1283 result = get_region_type( destObj );
1284 break;
1285 case RGN_DIFF:
1286 if (REGION_SubtractRegion( destObj, src1Obj, src2Obj ))
1287 result = get_region_type( destObj );
1288 break;
1290 GDI_ReleaseObj( hSrc2 );
1293 GDI_ReleaseObj( hSrc1 );
1295 TRACE("dump destObj:\n");
1296 if(TRACE_ON(region))
1297 REGION_DumpRegion(destObj);
1299 GDI_ReleaseObj( hDest );
1301 return result;
1304 /***********************************************************************
1305 * REGION_SetExtents
1306 * Re-calculate the extents of a region
1308 static void REGION_SetExtents (WINEREGION *pReg)
1310 RECT *pRect, *pRectEnd, *pExtents;
1312 if (pReg->numRects == 0)
1314 pReg->extents.left = 0;
1315 pReg->extents.top = 0;
1316 pReg->extents.right = 0;
1317 pReg->extents.bottom = 0;
1318 return;
1321 pExtents = &pReg->extents;
1322 pRect = pReg->rects;
1323 pRectEnd = &pRect[pReg->numRects - 1];
1326 * Since pRect is the first rectangle in the region, it must have the
1327 * smallest top and since pRectEnd is the last rectangle in the region,
1328 * it must have the largest bottom, because of banding. Initialize left and
1329 * right from pRect and pRectEnd, resp., as good things to initialize them
1330 * to...
1332 pExtents->left = pRect->left;
1333 pExtents->top = pRect->top;
1334 pExtents->right = pRectEnd->right;
1335 pExtents->bottom = pRectEnd->bottom;
1337 while (pRect <= pRectEnd)
1339 if (pRect->left < pExtents->left)
1340 pExtents->left = pRect->left;
1341 if (pRect->right > pExtents->right)
1342 pExtents->right = pRect->right;
1343 pRect++;
1347 /***********************************************************************
1348 * REGION_CopyRegion
1350 static BOOL REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1352 if (dst != src) /* don't want to copy to itself */
1354 if (dst->size < src->numRects && !grow_region( dst, src->numRects ))
1355 return FALSE;
1357 dst->numRects = src->numRects;
1358 dst->extents.left = src->extents.left;
1359 dst->extents.top = src->extents.top;
1360 dst->extents.right = src->extents.right;
1361 dst->extents.bottom = src->extents.bottom;
1362 memcpy(dst->rects, src->rects, src->numRects * sizeof(RECT));
1364 return TRUE;
1367 /***********************************************************************
1368 * REGION_MirrorRegion
1370 static BOOL REGION_MirrorRegion( WINEREGION *dst, WINEREGION *src, int width )
1372 int i, start, end;
1373 RECT extents;
1374 RECT *rects;
1375 WINEREGION tmp;
1377 if (dst != src)
1379 if (!grow_region( dst, src->numRects )) return FALSE;
1380 rects = dst->rects;
1381 dst->numRects = src->numRects;
1383 else
1385 if (!init_region( &tmp, src->numRects )) return FALSE;
1386 rects = tmp.rects;
1387 tmp.numRects = src->numRects;
1390 extents.left = width - src->extents.right;
1391 extents.right = width - src->extents.left;
1392 extents.top = src->extents.top;
1393 extents.bottom = src->extents.bottom;
1395 for (start = 0; start < src->numRects; start = end)
1397 /* find the end of the current band */
1398 for (end = start + 1; end < src->numRects; end++)
1399 if (src->rects[end].top != src->rects[end - 1].top) break;
1401 for (i = 0; i < end - start; i++)
1403 rects[start + i].left = width - src->rects[end - i - 1].right;
1404 rects[start + i].right = width - src->rects[end - i - 1].left;
1405 rects[start + i].top = src->rects[end - i - 1].top;
1406 rects[start + i].bottom = src->rects[end - i - 1].bottom;
1410 if (dst == src)
1411 move_rects( dst, &tmp );
1413 dst->extents = extents;
1414 return TRUE;
1417 /***********************************************************************
1418 * mirror_region
1420 INT mirror_region( HRGN dst, HRGN src, INT width )
1422 WINEREGION *src_rgn, *dst_rgn;
1423 INT ret = ERROR;
1425 if (!(src_rgn = GDI_GetObjPtr( src, OBJ_REGION ))) return ERROR;
1426 if ((dst_rgn = GDI_GetObjPtr( dst, OBJ_REGION )))
1428 if (REGION_MirrorRegion( dst_rgn, src_rgn, width )) ret = get_region_type( dst_rgn );
1429 GDI_ReleaseObj( dst_rgn );
1431 GDI_ReleaseObj( src_rgn );
1432 return ret;
1435 /***********************************************************************
1436 * MirrorRgn (GDI32.@)
1438 BOOL WINAPI MirrorRgn( HWND hwnd, HRGN hrgn )
1440 static BOOL (WINAPI *pGetWindowRect)( HWND hwnd, LPRECT rect );
1441 RECT rect;
1443 /* yes, a HWND in gdi32, don't ask */
1444 if (!pGetWindowRect)
1446 HMODULE user32 = GetModuleHandleW(L"user32.dll");
1447 if (!user32) return FALSE;
1448 if (!(pGetWindowRect = (void *)GetProcAddress( user32, "GetWindowRect" ))) return FALSE;
1450 pGetWindowRect( hwnd, &rect );
1451 return mirror_region( hrgn, hrgn, rect.right - rect.left ) != ERROR;
1455 /***********************************************************************
1456 * REGION_Coalesce
1458 * Attempt to merge the rects in the current band with those in the
1459 * previous one. Used only by REGION_RegionOp.
1461 * Results:
1462 * The new index for the previous band.
1464 * Side Effects:
1465 * If coalescing takes place:
1466 * - rectangles in the previous band will have their bottom fields
1467 * altered.
1468 * - pReg->numRects will be decreased.
1471 static INT REGION_Coalesce (
1472 WINEREGION *pReg, /* Region to coalesce */
1473 INT prevStart, /* Index of start of previous band */
1474 INT curStart /* Index of start of current band */
1476 RECT *pPrevRect; /* Current rect in previous band */
1477 RECT *pCurRect; /* Current rect in current band */
1478 RECT *pRegEnd; /* End of region */
1479 INT curNumRects; /* Number of rectangles in current band */
1480 INT prevNumRects; /* Number of rectangles in previous band */
1481 INT bandtop; /* top coordinate for current band */
1483 pRegEnd = &pReg->rects[pReg->numRects];
1485 pPrevRect = &pReg->rects[prevStart];
1486 prevNumRects = curStart - prevStart;
1489 * Figure out how many rectangles are in the current band. Have to do
1490 * this because multiple bands could have been added in REGION_RegionOp
1491 * at the end when one region has been exhausted.
1493 pCurRect = &pReg->rects[curStart];
1494 bandtop = pCurRect->top;
1495 for (curNumRects = 0;
1496 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1497 curNumRects++)
1499 pCurRect++;
1502 if (pCurRect != pRegEnd)
1505 * If more than one band was added, we have to find the start
1506 * of the last band added so the next coalescing job can start
1507 * at the right place... (given when multiple bands are added,
1508 * this may be pointless -- see above).
1510 pRegEnd--;
1511 while (pRegEnd[-1].top == pRegEnd->top)
1513 pRegEnd--;
1515 curStart = pRegEnd - pReg->rects;
1516 pRegEnd = pReg->rects + pReg->numRects;
1519 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1520 pCurRect -= curNumRects;
1522 * The bands may only be coalesced if the bottom of the previous
1523 * matches the top scanline of the current.
1525 if (pPrevRect->bottom == pCurRect->top)
1528 * Make sure the bands have rects in the same places. This
1529 * assumes that rects have been added in such a way that they
1530 * cover the most area possible. I.e. two rects in a band must
1531 * have some horizontal space between them.
1535 if ((pPrevRect->left != pCurRect->left) ||
1536 (pPrevRect->right != pCurRect->right))
1539 * The bands don't line up so they can't be coalesced.
1541 return (curStart);
1543 pPrevRect++;
1544 pCurRect++;
1545 prevNumRects -= 1;
1546 } while (prevNumRects != 0);
1548 pReg->numRects -= curNumRects;
1549 pCurRect -= curNumRects;
1550 pPrevRect -= curNumRects;
1553 * The bands may be merged, so set the bottom of each rect
1554 * in the previous band to that of the corresponding rect in
1555 * the current band.
1559 pPrevRect->bottom = pCurRect->bottom;
1560 pPrevRect++;
1561 pCurRect++;
1562 curNumRects -= 1;
1563 } while (curNumRects != 0);
1566 * If only one band was added to the region, we have to backup
1567 * curStart to the start of the previous band.
1569 * If more than one band was added to the region, copy the
1570 * other bands down. The assumption here is that the other bands
1571 * came from the same region as the current one and no further
1572 * coalescing can be done on them since it's all been done
1573 * already... curStart is already in the right place.
1575 if (pCurRect == pRegEnd)
1577 curStart = prevStart;
1579 else
1583 *pPrevRect++ = *pCurRect++;
1584 } while (pCurRect != pRegEnd);
1589 return (curStart);
1592 /**********************************************************************
1593 * REGION_compact
1595 * To keep regions from growing without bound, shrink the array of rectangles
1596 * to match the new number of rectangles in the region.
1598 * Only do this if the number of rectangles allocated is more than
1599 * twice the number of rectangles in the region.
1601 static void REGION_compact( WINEREGION *reg )
1603 if ((reg->numRects < reg->size / 2) && (reg->numRects > RGN_DEFAULT_RECTS))
1605 RECT *new_rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects, reg->numRects * sizeof(RECT) );
1606 if (new_rects)
1608 reg->rects = new_rects;
1609 reg->size = reg->numRects;
1614 /***********************************************************************
1615 * REGION_RegionOp
1617 * Apply an operation to two regions. Called by REGION_Union,
1618 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1620 * Results:
1621 * None.
1623 * Side Effects:
1624 * The new region is overwritten.
1626 * Notes:
1627 * The idea behind this function is to view the two regions as sets.
1628 * Together they cover a rectangle of area that this function divides
1629 * into horizontal bands where points are covered only by one region
1630 * or by both. For the first case, the nonOverlapFunc is called with
1631 * each the band and the band's upper and lower extents. For the
1632 * second, the overlapFunc is called to process the entire band. It
1633 * is responsible for clipping the rectangles in the band, though
1634 * this function provides the boundaries.
1635 * At the end of each band, the new region is coalesced, if possible,
1636 * to reduce the number of rectangles in the region.
1639 static BOOL REGION_RegionOp(
1640 WINEREGION *destReg, /* Place to store result */
1641 WINEREGION *reg1, /* First region in operation */
1642 WINEREGION *reg2, /* 2nd region in operation */
1643 BOOL (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1644 BOOL (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1645 BOOL (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1647 WINEREGION newReg;
1648 RECT *r1; /* Pointer into first region */
1649 RECT *r2; /* Pointer into 2d region */
1650 RECT *r1End; /* End of 1st region */
1651 RECT *r2End; /* End of 2d region */
1652 INT ybot; /* Bottom of intersection */
1653 INT ytop; /* Top of intersection */
1654 INT prevBand; /* Index of start of
1655 * previous band in newReg */
1656 INT curBand; /* Index of start of current
1657 * band in newReg */
1658 RECT *r1BandEnd; /* End of current band in r1 */
1659 RECT *r2BandEnd; /* End of current band in r2 */
1660 INT top; /* Top of non-overlapping band */
1661 INT bot; /* Bottom of non-overlapping band */
1664 * Initialization:
1665 * set r1, r2, r1End and r2End appropriately, preserve the important
1666 * parts of the destination region until the end in case it's one of
1667 * the two source regions, then mark the "new" region empty, allocating
1668 * another array of rectangles for it to use.
1670 r1 = reg1->rects;
1671 r2 = reg2->rects;
1672 r1End = r1 + reg1->numRects;
1673 r2End = r2 + reg2->numRects;
1676 * Allocate a reasonable number of rectangles for the new region. The idea
1677 * is to allocate enough so the individual functions don't need to
1678 * reallocate and copy the array, which is time consuming, yet we don't
1679 * have to worry about using too much memory. I hope to be able to
1680 * nuke the Xrealloc() at the end of this function eventually.
1682 if (!init_region( &newReg, max(reg1->numRects,reg2->numRects) * 2 )) return FALSE;
1685 * Initialize ybot and ytop.
1686 * In the upcoming loop, ybot and ytop serve different functions depending
1687 * on whether the band being handled is an overlapping or non-overlapping
1688 * band.
1689 * In the case of a non-overlapping band (only one of the regions
1690 * has points in the band), ybot is the bottom of the most recent
1691 * intersection and thus clips the top of the rectangles in that band.
1692 * ytop is the top of the next intersection between the two regions and
1693 * serves to clip the bottom of the rectangles in the current band.
1694 * For an overlapping band (where the two regions intersect), ytop clips
1695 * the top of the rectangles of both regions and ybot clips the bottoms.
1697 if (reg1->extents.top < reg2->extents.top)
1698 ybot = reg1->extents.top;
1699 else
1700 ybot = reg2->extents.top;
1703 * prevBand serves to mark the start of the previous band so rectangles
1704 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1705 * In the beginning, there is no previous band, so prevBand == curBand
1706 * (curBand is set later on, of course, but the first band will always
1707 * start at index 0). prevBand and curBand must be indices because of
1708 * the possible expansion, and resultant moving, of the new region's
1709 * array of rectangles.
1711 prevBand = 0;
1715 curBand = newReg.numRects;
1718 * This algorithm proceeds one source-band (as opposed to a
1719 * destination band, which is determined by where the two regions
1720 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1721 * rectangle after the last one in the current band for their
1722 * respective regions.
1724 r1BandEnd = r1;
1725 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1727 r1BandEnd++;
1730 r2BandEnd = r2;
1731 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1733 r2BandEnd++;
1737 * First handle the band that doesn't intersect, if any.
1739 * Note that attention is restricted to one band in the
1740 * non-intersecting region at once, so if a region has n
1741 * bands between the current position and the next place it overlaps
1742 * the other, this entire loop will be passed through n times.
1744 if (r1->top < r2->top)
1746 top = max(r1->top,ybot);
1747 bot = min(r1->bottom,r2->top);
1749 if ((top != bot) && (nonOverlap1Func != NULL))
1751 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, top, bot)) return FALSE;
1754 ytop = r2->top;
1756 else if (r2->top < r1->top)
1758 top = max(r2->top,ybot);
1759 bot = min(r2->bottom,r1->top);
1761 if ((top != bot) && (nonOverlap2Func != NULL))
1763 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, top, bot)) return FALSE;
1766 ytop = r1->top;
1768 else
1770 ytop = r1->top;
1774 * If any rectangles got added to the region, try and coalesce them
1775 * with rectangles from the previous band. Note we could just do
1776 * this test in miCoalesce, but some machines incur a not
1777 * inconsiderable cost for function calls, so...
1779 if (newReg.numRects != curBand)
1781 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1785 * Now see if we've hit an intersecting band. The two bands only
1786 * intersect if ybot > ytop
1788 ybot = min(r1->bottom, r2->bottom);
1789 curBand = newReg.numRects;
1790 if (ybot > ytop)
1792 if (!overlapFunc(&newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot)) return FALSE;
1795 if (newReg.numRects != curBand)
1797 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1801 * If we've finished with a band (bottom == ybot) we skip forward
1802 * in the region to the next band.
1804 if (r1->bottom == ybot)
1806 r1 = r1BandEnd;
1808 if (r2->bottom == ybot)
1810 r2 = r2BandEnd;
1812 } while ((r1 != r1End) && (r2 != r2End));
1815 * Deal with whichever region still has rectangles left.
1817 curBand = newReg.numRects;
1818 if (r1 != r1End)
1820 if (nonOverlap1Func != NULL)
1824 r1BandEnd = r1;
1825 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1827 r1BandEnd++;
1829 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom))
1830 return FALSE;
1831 r1 = r1BandEnd;
1832 } while (r1 != r1End);
1835 else if ((r2 != r2End) && (nonOverlap2Func != NULL))
1839 r2BandEnd = r2;
1840 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1842 r2BandEnd++;
1844 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom))
1845 return FALSE;
1846 r2 = r2BandEnd;
1847 } while (r2 != r2End);
1850 if (newReg.numRects != curBand)
1852 REGION_Coalesce (&newReg, prevBand, curBand);
1855 REGION_compact( &newReg );
1856 move_rects( destReg, &newReg );
1857 return TRUE;
1860 /***********************************************************************
1861 * Region Intersection
1862 ***********************************************************************/
1865 /***********************************************************************
1866 * REGION_IntersectO
1868 * Handle an overlapping band for REGION_Intersect.
1870 * Results:
1871 * None.
1873 * Side Effects:
1874 * Rectangles may be added to the region.
1877 static BOOL REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1878 RECT *r2, RECT *r2End, INT top, INT bottom)
1881 INT left, right;
1883 while ((r1 != r1End) && (r2 != r2End))
1885 left = max(r1->left, r2->left);
1886 right = min(r1->right, r2->right);
1889 * If there's any overlap between the two rectangles, add that
1890 * overlap to the new region.
1891 * There's no need to check for subsumption because the only way
1892 * such a need could arise is if some region has two rectangles
1893 * right next to each other. Since that should never happen...
1895 if (left < right)
1897 if (!add_rect( pReg, left, top, right, bottom )) return FALSE;
1901 * Need to advance the pointers. Shift the one that extends
1902 * to the right the least, since the other still has a chance to
1903 * overlap with that region's next rectangle, if you see what I mean.
1905 if (r1->right < r2->right)
1907 r1++;
1909 else if (r2->right < r1->right)
1911 r2++;
1913 else
1915 r1++;
1916 r2++;
1919 return TRUE;
1922 /***********************************************************************
1923 * REGION_IntersectRegion
1925 static BOOL REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1926 WINEREGION *reg2)
1928 /* check for trivial reject */
1929 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1930 (!overlapping(&reg1->extents, &reg2->extents)))
1931 newReg->numRects = 0;
1932 else
1933 if (!REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL)) return FALSE;
1936 * Can't alter newReg's extents before we call miRegionOp because
1937 * it might be one of the source regions and miRegionOp depends
1938 * on the extents of those regions being the same. Besides, this
1939 * way there's no checking against rectangles that will be nuked
1940 * due to coalescing, so we have to examine fewer rectangles.
1942 REGION_SetExtents(newReg);
1943 return TRUE;
1946 /***********************************************************************
1947 * Region Union
1948 ***********************************************************************/
1950 /***********************************************************************
1951 * REGION_UnionNonO
1953 * Handle a non-overlapping band for the union operation. Just
1954 * Adds the rectangles into the region. Doesn't have to check for
1955 * subsumption or anything.
1957 * Results:
1958 * None.
1960 * Side Effects:
1961 * pReg->numRects is incremented and the final rectangles overwritten
1962 * with the rectangles we're passed.
1965 static BOOL REGION_UnionNonO(WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
1967 while (r != rEnd)
1969 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
1970 r++;
1972 return TRUE;
1975 /***********************************************************************
1976 * REGION_UnionO
1978 * Handle an overlapping band for the union operation. Picks the
1979 * left-most rectangle each time and merges it into the region.
1981 * Results:
1982 * None.
1984 * Side Effects:
1985 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1986 * be changed.
1989 static BOOL REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1990 RECT *r2, RECT *r2End, INT top, INT bottom)
1992 #define MERGERECT(r) \
1993 if ((pReg->numRects != 0) && \
1994 (pReg->rects[pReg->numRects-1].top == top) && \
1995 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
1996 (pReg->rects[pReg->numRects-1].right >= r->left)) \
1998 if (pReg->rects[pReg->numRects-1].right < r->right) \
1999 pReg->rects[pReg->numRects-1].right = r->right; \
2001 else \
2003 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2005 r++;
2007 while ((r1 != r1End) && (r2 != r2End))
2009 if (r1->left < r2->left)
2011 MERGERECT(r1);
2013 else
2015 MERGERECT(r2);
2019 if (r1 != r1End)
2023 MERGERECT(r1);
2024 } while (r1 != r1End);
2026 else while (r2 != r2End)
2028 MERGERECT(r2);
2030 return TRUE;
2031 #undef MERGERECT
2034 /***********************************************************************
2035 * REGION_UnionRegion
2037 static BOOL REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1, WINEREGION *reg2)
2039 BOOL ret = TRUE;
2041 /* checks all the simple cases */
2044 * Region 1 and 2 are the same or region 1 is empty
2046 if ( (reg1 == reg2) || (!(reg1->numRects)) )
2048 if (newReg != reg2)
2049 ret = REGION_CopyRegion(newReg, reg2);
2050 return ret;
2054 * if nothing to union (region 2 empty)
2056 if (!(reg2->numRects))
2058 if (newReg != reg1)
2059 ret = REGION_CopyRegion(newReg, reg1);
2060 return ret;
2064 * Region 1 completely subsumes region 2
2066 if ((reg1->numRects == 1) &&
2067 (reg1->extents.left <= reg2->extents.left) &&
2068 (reg1->extents.top <= reg2->extents.top) &&
2069 (reg1->extents.right >= reg2->extents.right) &&
2070 (reg1->extents.bottom >= reg2->extents.bottom))
2072 if (newReg != reg1)
2073 ret = REGION_CopyRegion(newReg, reg1);
2074 return ret;
2078 * Region 2 completely subsumes region 1
2080 if ((reg2->numRects == 1) &&
2081 (reg2->extents.left <= reg1->extents.left) &&
2082 (reg2->extents.top <= reg1->extents.top) &&
2083 (reg2->extents.right >= reg1->extents.right) &&
2084 (reg2->extents.bottom >= reg1->extents.bottom))
2086 if (newReg != reg2)
2087 ret = REGION_CopyRegion(newReg, reg2);
2088 return ret;
2091 if ((ret = REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO)))
2093 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2094 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2095 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2096 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2098 return ret;
2101 /***********************************************************************
2102 * Region Subtraction
2103 ***********************************************************************/
2105 /***********************************************************************
2106 * REGION_SubtractNonO1
2108 * Deal with non-overlapping band for subtraction. Any parts from
2109 * region 2 we discard. Anything from region 1 we add to the region.
2111 * Results:
2112 * None.
2114 * Side Effects:
2115 * pReg may be affected.
2118 static BOOL REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2120 while (r != rEnd)
2122 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2123 r++;
2125 return TRUE;
2129 /***********************************************************************
2130 * REGION_SubtractO
2132 * Overlapping band subtraction. x1 is the left-most point not yet
2133 * checked.
2135 * Results:
2136 * None.
2138 * Side Effects:
2139 * pReg may have rectangles added to it.
2142 static BOOL REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2143 RECT *r2, RECT *r2End, INT top, INT bottom)
2145 INT left = r1->left;
2147 while ((r1 != r1End) && (r2 != r2End))
2149 if (r2->right <= left)
2152 * Subtrahend missed the boat: go to next subtrahend.
2154 r2++;
2156 else if (r2->left <= left)
2159 * Subtrahend precedes minuend: nuke left edge of minuend.
2161 left = r2->right;
2162 if (left >= r1->right)
2165 * Minuend completely covered: advance to next minuend and
2166 * reset left fence to edge of new minuend.
2168 r1++;
2169 if (r1 != r1End)
2170 left = r1->left;
2172 else
2175 * Subtrahend now used up since it doesn't extend beyond
2176 * minuend
2178 r2++;
2181 else if (r2->left < r1->right)
2184 * Left part of subtrahend covers part of minuend: add uncovered
2185 * part of minuend to region and skip to next subtrahend.
2187 if (!add_rect( pReg, left, top, r2->left, bottom )) return FALSE;
2188 left = r2->right;
2189 if (left >= r1->right)
2192 * Minuend used up: advance to new...
2194 r1++;
2195 if (r1 != r1End)
2196 left = r1->left;
2198 else
2201 * Subtrahend used up
2203 r2++;
2206 else
2209 * Minuend used up: add any remaining piece before advancing.
2211 if (r1->right > left)
2213 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2215 r1++;
2216 if (r1 != r1End)
2217 left = r1->left;
2222 * Add remaining minuend rectangles to region.
2224 while (r1 != r1End)
2226 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2227 r1++;
2228 if (r1 != r1End)
2230 left = r1->left;
2233 return TRUE;
2236 /***********************************************************************
2237 * REGION_SubtractRegion
2239 * Subtract regS from regM and leave the result in regD.
2240 * S stands for subtrahend, M for minuend and D for difference.
2242 * Results:
2243 * TRUE.
2245 * Side Effects:
2246 * regD is overwritten.
2249 static BOOL REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM, WINEREGION *regS )
2251 /* check for trivial reject */
2252 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2253 (!overlapping(&regM->extents, &regS->extents)) )
2254 return REGION_CopyRegion(regD, regM);
2256 if (!REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL))
2257 return FALSE;
2260 * Can't alter newReg's extents before we call miRegionOp because
2261 * it might be one of the source regions and miRegionOp depends
2262 * on the extents of those regions being the unaltered. Besides, this
2263 * way there's no checking against rectangles that will be nuked
2264 * due to coalescing, so we have to examine fewer rectangles.
2266 REGION_SetExtents (regD);
2267 return TRUE;
2270 /***********************************************************************
2271 * REGION_XorRegion
2273 static BOOL REGION_XorRegion(WINEREGION *dr, WINEREGION *sra, WINEREGION *srb)
2275 WINEREGION tra, trb;
2276 BOOL ret;
2278 if (!init_region( &tra, sra->numRects + 1 )) return FALSE;
2279 if ((ret = init_region( &trb, srb->numRects + 1 )))
2281 ret = REGION_SubtractRegion(&tra,sra,srb) &&
2282 REGION_SubtractRegion(&trb,srb,sra) &&
2283 REGION_UnionRegion(dr,&tra,&trb);
2284 destroy_region(&trb);
2286 destroy_region(&tra);
2287 return ret;
2290 /**************************************************************************
2292 * Poly Regions
2294 *************************************************************************/
2296 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2297 #define SMALL_COORDINATE 0x80000000
2299 /***********************************************************************
2300 * REGION_InsertEdgeInET
2302 * Insert the given edge into the edge table.
2303 * First we must find the correct bucket in the
2304 * Edge table, then find the right slot in the
2305 * bucket. Finally, we can insert it.
2308 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2309 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2312 struct list *ptr;
2313 ScanLineList *pSLL, *pPrevSLL;
2314 ScanLineListBlock *tmpSLLBlock;
2317 * find the right bucket to put the edge into
2319 pPrevSLL = &ET->scanlines;
2320 pSLL = pPrevSLL->next;
2321 while (pSLL && (pSLL->scanline < scanline))
2323 pPrevSLL = pSLL;
2324 pSLL = pSLL->next;
2328 * reassign pSLL (pointer to ScanLineList) if necessary
2330 if ((!pSLL) || (pSLL->scanline > scanline))
2332 if (*iSLLBlock > SLLSPERBLOCK-1)
2334 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2335 if(!tmpSLLBlock)
2337 WARN("Can't alloc SLLB\n");
2338 return;
2340 (*SLLBlock)->next = tmpSLLBlock;
2341 tmpSLLBlock->next = NULL;
2342 *SLLBlock = tmpSLLBlock;
2343 *iSLLBlock = 0;
2345 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2347 pSLL->next = pPrevSLL->next;
2348 list_init( &pSLL->edgelist );
2349 pPrevSLL->next = pSLL;
2351 pSLL->scanline = scanline;
2354 * now insert the edge in the right bucket
2356 LIST_FOR_EACH( ptr, &pSLL->edgelist )
2358 struct edge_table_entry *entry = LIST_ENTRY( ptr, struct edge_table_entry, entry );
2359 if (entry->bres.minor_axis >= ETE->bres.minor_axis) break;
2361 list_add_before( ptr, &ETE->entry );
2364 /***********************************************************************
2365 * REGION_CreateEdgeTable
2367 * This routine creates the edge table for
2368 * scan converting polygons.
2369 * The Edge Table (ET) looks like:
2371 * EdgeTable
2372 * --------
2373 * | ymax | ScanLineLists
2374 * |scanline|-->------------>-------------->...
2375 * -------- |scanline| |scanline|
2376 * |edgelist| |edgelist|
2377 * --------- ---------
2378 * | |
2379 * | |
2380 * V V
2381 * list of ETEs list of ETEs
2383 * where ETE is an EdgeTableEntry data structure,
2384 * and there is one ScanLineList per scanline at
2385 * which an edge is initially entered.
2388 static unsigned int REGION_CreateEdgeTable(const INT *Count, INT nbpolygons,
2389 const POINT *pts, EdgeTable *ET,
2390 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock,
2391 const RECT *clip_rect)
2393 const POINT *top, *bottom;
2394 const POINT *PrevPt, *CurrPt, *EndPt;
2395 INT poly, count;
2396 int iSLLBlock = 0;
2397 unsigned int dy, total = 0;
2400 * initialize the Edge Table.
2402 ET->scanlines.next = NULL;
2403 ET->ymax = SMALL_COORDINATE;
2404 ET->ymin = LARGE_COORDINATE;
2405 pSLLBlock->next = NULL;
2407 EndPt = pts - 1;
2408 for(poly = 0; poly < nbpolygons; poly++)
2410 count = Count[poly];
2411 EndPt += count;
2412 if(count < 2)
2413 continue;
2415 PrevPt = EndPt;
2418 * for each vertex in the array of points.
2419 * In this loop we are dealing with two vertices at
2420 * a time -- these make up one edge of the polygon.
2422 for ( ; count; PrevPt = CurrPt, count--)
2424 CurrPt = pts++;
2427 * find out which point is above and which is below.
2429 if (PrevPt->y > CurrPt->y)
2431 bottom = PrevPt, top = CurrPt;
2432 pETEs->ClockWise = 0;
2434 else
2436 bottom = CurrPt, top = PrevPt;
2437 pETEs->ClockWise = 1;
2441 * don't add horizontal edges to the Edge table.
2443 if (bottom->y == top->y) continue;
2444 if (clip_rect && (top->y >= clip_rect->bottom || bottom->y <= clip_rect->top)) continue;
2445 pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */
2448 * initialize integer edge algorithm
2450 dy = bottom->y - top->y;
2451 bres_init_polygon(dy, top->x, bottom->x, &pETEs->bres);
2453 if (clip_rect) dy = min( bottom->y, clip_rect->bottom ) - max( top->y, clip_rect->top );
2454 if (total + dy < total) return 0; /* overflow */
2455 total += dy;
2457 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock);
2459 if (top->y < ET->ymin) ET->ymin = top->y;
2460 if (bottom->y > ET->ymax) ET->ymax = bottom->y;
2461 pETEs++;
2464 return total;
2467 /***********************************************************************
2468 * REGION_loadAET
2470 * This routine moves EdgeTableEntries from the
2471 * EdgeTable into the Active Edge Table,
2472 * leaving them sorted by smaller x coordinate.
2475 static void REGION_loadAET( struct list *AET, struct list *ETEs )
2477 struct edge_table_entry *ptr, *next, *entry;
2478 struct list *active;
2480 LIST_FOR_EACH_ENTRY_SAFE( ptr, next, ETEs, struct edge_table_entry, entry )
2482 LIST_FOR_EACH( active, AET )
2484 entry = LIST_ENTRY( active, struct edge_table_entry, entry );
2485 if (entry->bres.minor_axis >= ptr->bres.minor_axis) break;
2487 list_remove( &ptr->entry );
2488 list_add_before( active, &ptr->entry );
2492 /***********************************************************************
2493 * REGION_computeWAET
2495 * This routine links the AET by the
2496 * nextWETE (winding EdgeTableEntry) link for
2497 * use by the winding number rule. The final
2498 * Active Edge Table (AET) might look something
2499 * like:
2501 * AET
2502 * ---------- --------- ---------
2503 * |ymax | |ymax | |ymax |
2504 * | ... | |... | |... |
2505 * |next |->|next |->|next |->...
2506 * |nextWETE| |nextWETE| |nextWETE|
2507 * --------- --------- ^--------
2508 * | | |
2509 * V-------------------> V---> ...
2512 static void REGION_computeWAET( struct list *AET, struct list *WETE )
2514 struct edge_table_entry *active;
2515 BOOL inside = TRUE;
2516 int isInside = 0;
2518 list_init( WETE );
2519 LIST_FOR_EACH_ENTRY( active, AET, struct edge_table_entry, entry )
2521 if (active->ClockWise)
2522 isInside++;
2523 else
2524 isInside--;
2526 if ((!inside && !isInside) || (inside && isInside))
2528 list_add_tail( WETE, &active->winding_entry );
2529 inside = !inside;
2534 /***********************************************************************
2535 * next_scanline
2537 * Update the Active Edge Table for the next scan line and sort it again.
2539 static inline BOOL next_scanline( struct list *AET, int y )
2541 struct edge_table_entry *active, *next, *insert;
2542 BOOL changed = FALSE;
2544 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2546 if (active->ymax == y) /* leaving this edge */
2548 list_remove( &active->entry );
2549 changed = TRUE;
2551 else bres_incr_polygon( &active->bres );
2553 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2555 LIST_FOR_EACH_ENTRY( insert, AET, struct edge_table_entry, entry )
2557 if (insert == active) break;
2558 if (insert->bres.minor_axis > active->bres.minor_axis) break;
2560 if (insert == active) continue;
2561 list_remove( &active->entry );
2562 list_add_before( &insert->entry, &active->entry );
2563 changed = TRUE;
2565 return changed;
2568 /***********************************************************************
2569 * REGION_FreeStorage
2571 * Clean up our act.
2573 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2575 ScanLineListBlock *tmpSLLBlock;
2577 while (pSLLBlock)
2579 tmpSLLBlock = pSLLBlock->next;
2580 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2581 pSLLBlock = tmpSLLBlock;
2585 static void scan_convert( WINEREGION *obj, EdgeTable *ET, INT mode, const RECT *clip_rect )
2587 struct list AET;
2588 ScanLineList *pSLL;
2589 struct edge_table_entry *active;
2590 INT i, y, first = 1, cur_band = 0, prev_band = 0;
2592 if (clip_rect) ET->ymax = min( ET->ymax, clip_rect->bottom );
2594 list_init( &AET );
2595 pSLL = ET->scanlines.next;
2597 if (mode != WINDING)
2599 for (y = ET->ymin; y < ET->ymax; y++)
2601 /* Add a new edge to the active edge table when we get to the next edge. */
2602 if (pSLL != NULL && y == pSLL->scanline)
2604 REGION_loadAET(&AET, &pSLL->edgelist);
2605 pSLL = pSLL->next;
2608 if (!clip_rect || y >= clip_rect->top)
2610 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2612 if (first)
2614 obj->rects[obj->numRects].left = active->bres.minor_axis;
2615 obj->rects[obj->numRects].top = y;
2616 obj->rects[obj->numRects].bottom = y + 1;
2618 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2620 /* create new rect only if we can't merge with the previous one */
2621 if (!obj->numRects || obj->rects[obj->numRects-1].top != y ||
2622 obj->rects[obj->numRects-1].right < obj->rects[obj->numRects].left)
2623 obj->numRects++;
2624 obj->rects[obj->numRects-1].right = active->bres.minor_axis;
2626 first = !first;
2630 next_scanline( &AET, y );
2632 if (obj->numRects)
2634 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2635 cur_band = obj->numRects;
2639 else /* mode == WINDING */
2641 struct list WETE, *pWETE;
2643 for (y = ET->ymin; y < ET->ymax; y++)
2645 /* Add a new edge to the active edge table when we get to the next edge. */
2646 if (pSLL != NULL && y == pSLL->scanline)
2648 REGION_loadAET(&AET, &pSLL->edgelist);
2649 REGION_computeWAET( &AET, &WETE );
2650 pSLL = pSLL->next;
2652 pWETE = list_head( &WETE );
2654 if (!clip_rect || y >= clip_rect->top)
2656 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2658 /* Add to the buffer only those edges that are in the Winding active edge table. */
2659 if (pWETE == &active->winding_entry)
2661 if (first)
2663 obj->rects[obj->numRects].left = active->bres.minor_axis;
2664 obj->rects[obj->numRects].top = y;
2666 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2668 obj->rects[obj->numRects].right = active->bres.minor_axis;
2669 obj->rects[obj->numRects].bottom = y + 1;
2670 obj->numRects++;
2672 first = !first;
2673 pWETE = list_next( &WETE, pWETE );
2678 /* Recompute the winding active edge table if we just resorted or have exited an edge. */
2679 if (next_scanline( &AET, y )) REGION_computeWAET( &AET, &WETE );
2681 if (obj->numRects)
2683 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2684 cur_band = obj->numRects;
2689 assert( obj->numRects <= obj->size );
2691 if (obj->numRects)
2693 obj->extents.left = INT_MAX;
2694 obj->extents.right = INT_MIN;
2695 obj->extents.top = obj->rects[0].top;
2696 obj->extents.bottom = obj->rects[obj->numRects-1].bottom;
2697 for (i = 0; i < obj->numRects; i++)
2699 obj->extents.left = min( obj->extents.left, obj->rects[i].left );
2700 obj->extents.right = max( obj->extents.right, obj->rects[i].right );
2703 REGION_compact( obj );
2706 /***********************************************************************
2707 * create_polypolygon_region
2709 * Helper for CreatePolyPolygonRgn.
2711 HRGN create_polypolygon_region( const POINT *Pts, const INT *Count, INT nbpolygons, INT mode,
2712 const RECT *clip_rect )
2714 HRGN hrgn = 0;
2715 WINEREGION *obj = NULL;
2716 EdgeTable ET; /* header node for ET */
2717 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2718 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2719 unsigned int nb_points;
2720 INT poly, total;
2722 TRACE("%p, count %d, polygons %d, mode %d\n", Pts, *Count, nbpolygons, mode);
2724 /* special case a rectangle */
2726 if (((nbpolygons == 1) && ((*Count == 4) ||
2727 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2728 (((Pts[0].y == Pts[1].y) &&
2729 (Pts[1].x == Pts[2].x) &&
2730 (Pts[2].y == Pts[3].y) &&
2731 (Pts[3].x == Pts[0].x)) ||
2732 ((Pts[0].x == Pts[1].x) &&
2733 (Pts[1].y == Pts[2].y) &&
2734 (Pts[2].x == Pts[3].x) &&
2735 (Pts[3].y == Pts[0].y))))
2736 return CreateRectRgn( min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2737 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2739 for(poly = total = 0; poly < nbpolygons; poly++)
2740 total += Count[poly];
2741 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2742 return 0;
2744 nb_points = REGION_CreateEdgeTable( Count, nbpolygons, Pts, &ET, pETEs, &SLLBlock, clip_rect );
2745 if ((obj = alloc_region( nb_points / 2 )))
2747 if (nb_points) scan_convert( obj, &ET, mode, clip_rect );
2749 if (!(hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs )))
2750 free_region( obj );
2753 REGION_FreeStorage(SLLBlock.next);
2754 HeapFree( GetProcessHeap(), 0, pETEs );
2755 return hrgn;
2759 /***********************************************************************
2760 * CreatePolyPolygonRgn (GDI32.@)
2762 HRGN WINAPI CreatePolyPolygonRgn( const POINT *pts, const INT *count, INT nbpolygons, INT mode )
2764 return create_polypolygon_region( pts, count, nbpolygons, mode, NULL );
2768 /***********************************************************************
2769 * CreatePolygonRgn (GDI32.@)
2771 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count, INT mode )
2773 return create_polypolygon_region( points, &count, 1, mode, NULL );