server: Use server_get_file_info for all info classes not implemented on client side.
[wine.git] / dlls / gdi32 / region.c
blobcad6428dc9b3c772458499d0cefd0612bf670061
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 const WCHAR user32W[] = {'u','s','e','r','3','2','.','d','l','l',0};
1441 static BOOL (WINAPI *pGetWindowRect)( HWND hwnd, LPRECT rect );
1442 RECT rect;
1444 /* yes, a HWND in gdi32, don't ask */
1445 if (!pGetWindowRect)
1447 HMODULE user32 = GetModuleHandleW(user32W);
1448 if (!user32) return FALSE;
1449 if (!(pGetWindowRect = (void *)GetProcAddress( user32, "GetWindowRect" ))) return FALSE;
1451 pGetWindowRect( hwnd, &rect );
1452 return mirror_region( hrgn, hrgn, rect.right - rect.left ) != ERROR;
1456 /***********************************************************************
1457 * REGION_Coalesce
1459 * Attempt to merge the rects in the current band with those in the
1460 * previous one. Used only by REGION_RegionOp.
1462 * Results:
1463 * The new index for the previous band.
1465 * Side Effects:
1466 * If coalescing takes place:
1467 * - rectangles in the previous band will have their bottom fields
1468 * altered.
1469 * - pReg->numRects will be decreased.
1472 static INT REGION_Coalesce (
1473 WINEREGION *pReg, /* Region to coalesce */
1474 INT prevStart, /* Index of start of previous band */
1475 INT curStart /* Index of start of current band */
1477 RECT *pPrevRect; /* Current rect in previous band */
1478 RECT *pCurRect; /* Current rect in current band */
1479 RECT *pRegEnd; /* End of region */
1480 INT curNumRects; /* Number of rectangles in current band */
1481 INT prevNumRects; /* Number of rectangles in previous band */
1482 INT bandtop; /* top coordinate for current band */
1484 pRegEnd = &pReg->rects[pReg->numRects];
1486 pPrevRect = &pReg->rects[prevStart];
1487 prevNumRects = curStart - prevStart;
1490 * Figure out how many rectangles are in the current band. Have to do
1491 * this because multiple bands could have been added in REGION_RegionOp
1492 * at the end when one region has been exhausted.
1494 pCurRect = &pReg->rects[curStart];
1495 bandtop = pCurRect->top;
1496 for (curNumRects = 0;
1497 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1498 curNumRects++)
1500 pCurRect++;
1503 if (pCurRect != pRegEnd)
1506 * If more than one band was added, we have to find the start
1507 * of the last band added so the next coalescing job can start
1508 * at the right place... (given when multiple bands are added,
1509 * this may be pointless -- see above).
1511 pRegEnd--;
1512 while (pRegEnd[-1].top == pRegEnd->top)
1514 pRegEnd--;
1516 curStart = pRegEnd - pReg->rects;
1517 pRegEnd = pReg->rects + pReg->numRects;
1520 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1521 pCurRect -= curNumRects;
1523 * The bands may only be coalesced if the bottom of the previous
1524 * matches the top scanline of the current.
1526 if (pPrevRect->bottom == pCurRect->top)
1529 * Make sure the bands have rects in the same places. This
1530 * assumes that rects have been added in such a way that they
1531 * cover the most area possible. I.e. two rects in a band must
1532 * have some horizontal space between them.
1536 if ((pPrevRect->left != pCurRect->left) ||
1537 (pPrevRect->right != pCurRect->right))
1540 * The bands don't line up so they can't be coalesced.
1542 return (curStart);
1544 pPrevRect++;
1545 pCurRect++;
1546 prevNumRects -= 1;
1547 } while (prevNumRects != 0);
1549 pReg->numRects -= curNumRects;
1550 pCurRect -= curNumRects;
1551 pPrevRect -= curNumRects;
1554 * The bands may be merged, so set the bottom of each rect
1555 * in the previous band to that of the corresponding rect in
1556 * the current band.
1560 pPrevRect->bottom = pCurRect->bottom;
1561 pPrevRect++;
1562 pCurRect++;
1563 curNumRects -= 1;
1564 } while (curNumRects != 0);
1567 * If only one band was added to the region, we have to backup
1568 * curStart to the start of the previous band.
1570 * If more than one band was added to the region, copy the
1571 * other bands down. The assumption here is that the other bands
1572 * came from the same region as the current one and no further
1573 * coalescing can be done on them since it's all been done
1574 * already... curStart is already in the right place.
1576 if (pCurRect == pRegEnd)
1578 curStart = prevStart;
1580 else
1584 *pPrevRect++ = *pCurRect++;
1585 } while (pCurRect != pRegEnd);
1590 return (curStart);
1593 /**********************************************************************
1594 * REGION_compact
1596 * To keep regions from growing without bound, shrink the array of rectangles
1597 * to match the new number of rectangles in the region.
1599 * Only do this if the number of rectangles allocated is more than
1600 * twice the number of rectangles in the region.
1602 static void REGION_compact( WINEREGION *reg )
1604 if ((reg->numRects < reg->size / 2) && (reg->numRects > RGN_DEFAULT_RECTS))
1606 RECT *new_rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects, reg->numRects * sizeof(RECT) );
1607 if (new_rects)
1609 reg->rects = new_rects;
1610 reg->size = reg->numRects;
1615 /***********************************************************************
1616 * REGION_RegionOp
1618 * Apply an operation to two regions. Called by REGION_Union,
1619 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1621 * Results:
1622 * None.
1624 * Side Effects:
1625 * The new region is overwritten.
1627 * Notes:
1628 * The idea behind this function is to view the two regions as sets.
1629 * Together they cover a rectangle of area that this function divides
1630 * into horizontal bands where points are covered only by one region
1631 * or by both. For the first case, the nonOverlapFunc is called with
1632 * each the band and the band's upper and lower extents. For the
1633 * second, the overlapFunc is called to process the entire band. It
1634 * is responsible for clipping the rectangles in the band, though
1635 * this function provides the boundaries.
1636 * At the end of each band, the new region is coalesced, if possible,
1637 * to reduce the number of rectangles in the region.
1640 static BOOL REGION_RegionOp(
1641 WINEREGION *destReg, /* Place to store result */
1642 WINEREGION *reg1, /* First region in operation */
1643 WINEREGION *reg2, /* 2nd region in operation */
1644 BOOL (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1645 BOOL (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1646 BOOL (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1648 WINEREGION newReg;
1649 RECT *r1; /* Pointer into first region */
1650 RECT *r2; /* Pointer into 2d region */
1651 RECT *r1End; /* End of 1st region */
1652 RECT *r2End; /* End of 2d region */
1653 INT ybot; /* Bottom of intersection */
1654 INT ytop; /* Top of intersection */
1655 INT prevBand; /* Index of start of
1656 * previous band in newReg */
1657 INT curBand; /* Index of start of current
1658 * band in newReg */
1659 RECT *r1BandEnd; /* End of current band in r1 */
1660 RECT *r2BandEnd; /* End of current band in r2 */
1661 INT top; /* Top of non-overlapping band */
1662 INT bot; /* Bottom of non-overlapping band */
1665 * Initialization:
1666 * set r1, r2, r1End and r2End appropriately, preserve the important
1667 * parts of the destination region until the end in case it's one of
1668 * the two source regions, then mark the "new" region empty, allocating
1669 * another array of rectangles for it to use.
1671 r1 = reg1->rects;
1672 r2 = reg2->rects;
1673 r1End = r1 + reg1->numRects;
1674 r2End = r2 + reg2->numRects;
1677 * Allocate a reasonable number of rectangles for the new region. The idea
1678 * is to allocate enough so the individual functions don't need to
1679 * reallocate and copy the array, which is time consuming, yet we don't
1680 * have to worry about using too much memory. I hope to be able to
1681 * nuke the Xrealloc() at the end of this function eventually.
1683 if (!init_region( &newReg, max(reg1->numRects,reg2->numRects) * 2 )) return FALSE;
1686 * Initialize ybot and ytop.
1687 * In the upcoming loop, ybot and ytop serve different functions depending
1688 * on whether the band being handled is an overlapping or non-overlapping
1689 * band.
1690 * In the case of a non-overlapping band (only one of the regions
1691 * has points in the band), ybot is the bottom of the most recent
1692 * intersection and thus clips the top of the rectangles in that band.
1693 * ytop is the top of the next intersection between the two regions and
1694 * serves to clip the bottom of the rectangles in the current band.
1695 * For an overlapping band (where the two regions intersect), ytop clips
1696 * the top of the rectangles of both regions and ybot clips the bottoms.
1698 if (reg1->extents.top < reg2->extents.top)
1699 ybot = reg1->extents.top;
1700 else
1701 ybot = reg2->extents.top;
1704 * prevBand serves to mark the start of the previous band so rectangles
1705 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1706 * In the beginning, there is no previous band, so prevBand == curBand
1707 * (curBand is set later on, of course, but the first band will always
1708 * start at index 0). prevBand and curBand must be indices because of
1709 * the possible expansion, and resultant moving, of the new region's
1710 * array of rectangles.
1712 prevBand = 0;
1716 curBand = newReg.numRects;
1719 * This algorithm proceeds one source-band (as opposed to a
1720 * destination band, which is determined by where the two regions
1721 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1722 * rectangle after the last one in the current band for their
1723 * respective regions.
1725 r1BandEnd = r1;
1726 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1728 r1BandEnd++;
1731 r2BandEnd = r2;
1732 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1734 r2BandEnd++;
1738 * First handle the band that doesn't intersect, if any.
1740 * Note that attention is restricted to one band in the
1741 * non-intersecting region at once, so if a region has n
1742 * bands between the current position and the next place it overlaps
1743 * the other, this entire loop will be passed through n times.
1745 if (r1->top < r2->top)
1747 top = max(r1->top,ybot);
1748 bot = min(r1->bottom,r2->top);
1750 if ((top != bot) && (nonOverlap1Func != NULL))
1752 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, top, bot)) return FALSE;
1755 ytop = r2->top;
1757 else if (r2->top < r1->top)
1759 top = max(r2->top,ybot);
1760 bot = min(r2->bottom,r1->top);
1762 if ((top != bot) && (nonOverlap2Func != NULL))
1764 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, top, bot)) return FALSE;
1767 ytop = r1->top;
1769 else
1771 ytop = r1->top;
1775 * If any rectangles got added to the region, try and coalesce them
1776 * with rectangles from the previous band. Note we could just do
1777 * this test in miCoalesce, but some machines incur a not
1778 * inconsiderable cost for function calls, so...
1780 if (newReg.numRects != curBand)
1782 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1786 * Now see if we've hit an intersecting band. The two bands only
1787 * intersect if ybot > ytop
1789 ybot = min(r1->bottom, r2->bottom);
1790 curBand = newReg.numRects;
1791 if (ybot > ytop)
1793 if (!overlapFunc(&newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot)) return FALSE;
1796 if (newReg.numRects != curBand)
1798 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1802 * If we've finished with a band (bottom == ybot) we skip forward
1803 * in the region to the next band.
1805 if (r1->bottom == ybot)
1807 r1 = r1BandEnd;
1809 if (r2->bottom == ybot)
1811 r2 = r2BandEnd;
1813 } while ((r1 != r1End) && (r2 != r2End));
1816 * Deal with whichever region still has rectangles left.
1818 curBand = newReg.numRects;
1819 if (r1 != r1End)
1821 if (nonOverlap1Func != NULL)
1825 r1BandEnd = r1;
1826 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1828 r1BandEnd++;
1830 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom))
1831 return FALSE;
1832 r1 = r1BandEnd;
1833 } while (r1 != r1End);
1836 else if ((r2 != r2End) && (nonOverlap2Func != NULL))
1840 r2BandEnd = r2;
1841 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1843 r2BandEnd++;
1845 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom))
1846 return FALSE;
1847 r2 = r2BandEnd;
1848 } while (r2 != r2End);
1851 if (newReg.numRects != curBand)
1853 REGION_Coalesce (&newReg, prevBand, curBand);
1856 REGION_compact( &newReg );
1857 move_rects( destReg, &newReg );
1858 return TRUE;
1861 /***********************************************************************
1862 * Region Intersection
1863 ***********************************************************************/
1866 /***********************************************************************
1867 * REGION_IntersectO
1869 * Handle an overlapping band for REGION_Intersect.
1871 * Results:
1872 * None.
1874 * Side Effects:
1875 * Rectangles may be added to the region.
1878 static BOOL REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1879 RECT *r2, RECT *r2End, INT top, INT bottom)
1882 INT left, right;
1884 while ((r1 != r1End) && (r2 != r2End))
1886 left = max(r1->left, r2->left);
1887 right = min(r1->right, r2->right);
1890 * If there's any overlap between the two rectangles, add that
1891 * overlap to the new region.
1892 * There's no need to check for subsumption because the only way
1893 * such a need could arise is if some region has two rectangles
1894 * right next to each other. Since that should never happen...
1896 if (left < right)
1898 if (!add_rect( pReg, left, top, right, bottom )) return FALSE;
1902 * Need to advance the pointers. Shift the one that extends
1903 * to the right the least, since the other still has a chance to
1904 * overlap with that region's next rectangle, if you see what I mean.
1906 if (r1->right < r2->right)
1908 r1++;
1910 else if (r2->right < r1->right)
1912 r2++;
1914 else
1916 r1++;
1917 r2++;
1920 return TRUE;
1923 /***********************************************************************
1924 * REGION_IntersectRegion
1926 static BOOL REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1927 WINEREGION *reg2)
1929 /* check for trivial reject */
1930 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1931 (!overlapping(&reg1->extents, &reg2->extents)))
1932 newReg->numRects = 0;
1933 else
1934 if (!REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL)) return FALSE;
1937 * Can't alter newReg's extents before we call miRegionOp because
1938 * it might be one of the source regions and miRegionOp depends
1939 * on the extents of those regions being the same. Besides, this
1940 * way there's no checking against rectangles that will be nuked
1941 * due to coalescing, so we have to examine fewer rectangles.
1943 REGION_SetExtents(newReg);
1944 return TRUE;
1947 /***********************************************************************
1948 * Region Union
1949 ***********************************************************************/
1951 /***********************************************************************
1952 * REGION_UnionNonO
1954 * Handle a non-overlapping band for the union operation. Just
1955 * Adds the rectangles into the region. Doesn't have to check for
1956 * subsumption or anything.
1958 * Results:
1959 * None.
1961 * Side Effects:
1962 * pReg->numRects is incremented and the final rectangles overwritten
1963 * with the rectangles we're passed.
1966 static BOOL REGION_UnionNonO(WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
1968 while (r != rEnd)
1970 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
1971 r++;
1973 return TRUE;
1976 /***********************************************************************
1977 * REGION_UnionO
1979 * Handle an overlapping band for the union operation. Picks the
1980 * left-most rectangle each time and merges it into the region.
1982 * Results:
1983 * None.
1985 * Side Effects:
1986 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1987 * be changed.
1990 static BOOL REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1991 RECT *r2, RECT *r2End, INT top, INT bottom)
1993 #define MERGERECT(r) \
1994 if ((pReg->numRects != 0) && \
1995 (pReg->rects[pReg->numRects-1].top == top) && \
1996 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
1997 (pReg->rects[pReg->numRects-1].right >= r->left)) \
1999 if (pReg->rects[pReg->numRects-1].right < r->right) \
2000 pReg->rects[pReg->numRects-1].right = r->right; \
2002 else \
2004 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2006 r++;
2008 while ((r1 != r1End) && (r2 != r2End))
2010 if (r1->left < r2->left)
2012 MERGERECT(r1);
2014 else
2016 MERGERECT(r2);
2020 if (r1 != r1End)
2024 MERGERECT(r1);
2025 } while (r1 != r1End);
2027 else while (r2 != r2End)
2029 MERGERECT(r2);
2031 return TRUE;
2032 #undef MERGERECT
2035 /***********************************************************************
2036 * REGION_UnionRegion
2038 static BOOL REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1, WINEREGION *reg2)
2040 BOOL ret = TRUE;
2042 /* checks all the simple cases */
2045 * Region 1 and 2 are the same or region 1 is empty
2047 if ( (reg1 == reg2) || (!(reg1->numRects)) )
2049 if (newReg != reg2)
2050 ret = REGION_CopyRegion(newReg, reg2);
2051 return ret;
2055 * if nothing to union (region 2 empty)
2057 if (!(reg2->numRects))
2059 if (newReg != reg1)
2060 ret = REGION_CopyRegion(newReg, reg1);
2061 return ret;
2065 * Region 1 completely subsumes region 2
2067 if ((reg1->numRects == 1) &&
2068 (reg1->extents.left <= reg2->extents.left) &&
2069 (reg1->extents.top <= reg2->extents.top) &&
2070 (reg1->extents.right >= reg2->extents.right) &&
2071 (reg1->extents.bottom >= reg2->extents.bottom))
2073 if (newReg != reg1)
2074 ret = REGION_CopyRegion(newReg, reg1);
2075 return ret;
2079 * Region 2 completely subsumes region 1
2081 if ((reg2->numRects == 1) &&
2082 (reg2->extents.left <= reg1->extents.left) &&
2083 (reg2->extents.top <= reg1->extents.top) &&
2084 (reg2->extents.right >= reg1->extents.right) &&
2085 (reg2->extents.bottom >= reg1->extents.bottom))
2087 if (newReg != reg2)
2088 ret = REGION_CopyRegion(newReg, reg2);
2089 return ret;
2092 if ((ret = REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO)))
2094 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2095 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2096 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2097 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2099 return ret;
2102 /***********************************************************************
2103 * Region Subtraction
2104 ***********************************************************************/
2106 /***********************************************************************
2107 * REGION_SubtractNonO1
2109 * Deal with non-overlapping band for subtraction. Any parts from
2110 * region 2 we discard. Anything from region 1 we add to the region.
2112 * Results:
2113 * None.
2115 * Side Effects:
2116 * pReg may be affected.
2119 static BOOL REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2121 while (r != rEnd)
2123 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2124 r++;
2126 return TRUE;
2130 /***********************************************************************
2131 * REGION_SubtractO
2133 * Overlapping band subtraction. x1 is the left-most point not yet
2134 * checked.
2136 * Results:
2137 * None.
2139 * Side Effects:
2140 * pReg may have rectangles added to it.
2143 static BOOL REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2144 RECT *r2, RECT *r2End, INT top, INT bottom)
2146 INT left = r1->left;
2148 while ((r1 != r1End) && (r2 != r2End))
2150 if (r2->right <= left)
2153 * Subtrahend missed the boat: go to next subtrahend.
2155 r2++;
2157 else if (r2->left <= left)
2160 * Subtrahend precedes minuend: nuke left edge of minuend.
2162 left = r2->right;
2163 if (left >= r1->right)
2166 * Minuend completely covered: advance to next minuend and
2167 * reset left fence to edge of new minuend.
2169 r1++;
2170 if (r1 != r1End)
2171 left = r1->left;
2173 else
2176 * Subtrahend now used up since it doesn't extend beyond
2177 * minuend
2179 r2++;
2182 else if (r2->left < r1->right)
2185 * Left part of subtrahend covers part of minuend: add uncovered
2186 * part of minuend to region and skip to next subtrahend.
2188 if (!add_rect( pReg, left, top, r2->left, bottom )) return FALSE;
2189 left = r2->right;
2190 if (left >= r1->right)
2193 * Minuend used up: advance to new...
2195 r1++;
2196 if (r1 != r1End)
2197 left = r1->left;
2199 else
2202 * Subtrahend used up
2204 r2++;
2207 else
2210 * Minuend used up: add any remaining piece before advancing.
2212 if (r1->right > left)
2214 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2216 r1++;
2217 if (r1 != r1End)
2218 left = r1->left;
2223 * Add remaining minuend rectangles to region.
2225 while (r1 != r1End)
2227 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2228 r1++;
2229 if (r1 != r1End)
2231 left = r1->left;
2234 return TRUE;
2237 /***********************************************************************
2238 * REGION_SubtractRegion
2240 * Subtract regS from regM and leave the result in regD.
2241 * S stands for subtrahend, M for minuend and D for difference.
2243 * Results:
2244 * TRUE.
2246 * Side Effects:
2247 * regD is overwritten.
2250 static BOOL REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM, WINEREGION *regS )
2252 /* check for trivial reject */
2253 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2254 (!overlapping(&regM->extents, &regS->extents)) )
2255 return REGION_CopyRegion(regD, regM);
2257 if (!REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL))
2258 return FALSE;
2261 * Can't alter newReg's extents before we call miRegionOp because
2262 * it might be one of the source regions and miRegionOp depends
2263 * on the extents of those regions being the unaltered. Besides, this
2264 * way there's no checking against rectangles that will be nuked
2265 * due to coalescing, so we have to examine fewer rectangles.
2267 REGION_SetExtents (regD);
2268 return TRUE;
2271 /***********************************************************************
2272 * REGION_XorRegion
2274 static BOOL REGION_XorRegion(WINEREGION *dr, WINEREGION *sra, WINEREGION *srb)
2276 WINEREGION tra, trb;
2277 BOOL ret;
2279 if (!init_region( &tra, sra->numRects + 1 )) return FALSE;
2280 if ((ret = init_region( &trb, srb->numRects + 1 )))
2282 ret = REGION_SubtractRegion(&tra,sra,srb) &&
2283 REGION_SubtractRegion(&trb,srb,sra) &&
2284 REGION_UnionRegion(dr,&tra,&trb);
2285 destroy_region(&trb);
2287 destroy_region(&tra);
2288 return ret;
2291 /**************************************************************************
2293 * Poly Regions
2295 *************************************************************************/
2297 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2298 #define SMALL_COORDINATE 0x80000000
2300 /***********************************************************************
2301 * REGION_InsertEdgeInET
2303 * Insert the given edge into the edge table.
2304 * First we must find the correct bucket in the
2305 * Edge table, then find the right slot in the
2306 * bucket. Finally, we can insert it.
2309 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2310 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2313 struct list *ptr;
2314 ScanLineList *pSLL, *pPrevSLL;
2315 ScanLineListBlock *tmpSLLBlock;
2318 * find the right bucket to put the edge into
2320 pPrevSLL = &ET->scanlines;
2321 pSLL = pPrevSLL->next;
2322 while (pSLL && (pSLL->scanline < scanline))
2324 pPrevSLL = pSLL;
2325 pSLL = pSLL->next;
2329 * reassign pSLL (pointer to ScanLineList) if necessary
2331 if ((!pSLL) || (pSLL->scanline > scanline))
2333 if (*iSLLBlock > SLLSPERBLOCK-1)
2335 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2336 if(!tmpSLLBlock)
2338 WARN("Can't alloc SLLB\n");
2339 return;
2341 (*SLLBlock)->next = tmpSLLBlock;
2342 tmpSLLBlock->next = NULL;
2343 *SLLBlock = tmpSLLBlock;
2344 *iSLLBlock = 0;
2346 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2348 pSLL->next = pPrevSLL->next;
2349 list_init( &pSLL->edgelist );
2350 pPrevSLL->next = pSLL;
2352 pSLL->scanline = scanline;
2355 * now insert the edge in the right bucket
2357 LIST_FOR_EACH( ptr, &pSLL->edgelist )
2359 struct edge_table_entry *entry = LIST_ENTRY( ptr, struct edge_table_entry, entry );
2360 if (entry->bres.minor_axis >= ETE->bres.minor_axis) break;
2362 list_add_before( ptr, &ETE->entry );
2365 /***********************************************************************
2366 * REGION_CreateEdgeTable
2368 * This routine creates the edge table for
2369 * scan converting polygons.
2370 * The Edge Table (ET) looks like:
2372 * EdgeTable
2373 * --------
2374 * | ymax | ScanLineLists
2375 * |scanline|-->------------>-------------->...
2376 * -------- |scanline| |scanline|
2377 * |edgelist| |edgelist|
2378 * --------- ---------
2379 * | |
2380 * | |
2381 * V V
2382 * list of ETEs list of ETEs
2384 * where ETE is an EdgeTableEntry data structure,
2385 * and there is one ScanLineList per scanline at
2386 * which an edge is initially entered.
2389 static unsigned int REGION_CreateEdgeTable(const INT *Count, INT nbpolygons,
2390 const POINT *pts, EdgeTable *ET,
2391 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock,
2392 const RECT *clip_rect)
2394 const POINT *top, *bottom;
2395 const POINT *PrevPt, *CurrPt, *EndPt;
2396 INT poly, count;
2397 int iSLLBlock = 0;
2398 unsigned int dy, total = 0;
2401 * initialize the Edge Table.
2403 ET->scanlines.next = NULL;
2404 ET->ymax = SMALL_COORDINATE;
2405 ET->ymin = LARGE_COORDINATE;
2406 pSLLBlock->next = NULL;
2408 EndPt = pts - 1;
2409 for(poly = 0; poly < nbpolygons; poly++)
2411 count = Count[poly];
2412 EndPt += count;
2413 if(count < 2)
2414 continue;
2416 PrevPt = EndPt;
2419 * for each vertex in the array of points.
2420 * In this loop we are dealing with two vertices at
2421 * a time -- these make up one edge of the polygon.
2423 for ( ; count; PrevPt = CurrPt, count--)
2425 CurrPt = pts++;
2428 * find out which point is above and which is below.
2430 if (PrevPt->y > CurrPt->y)
2432 bottom = PrevPt, top = CurrPt;
2433 pETEs->ClockWise = 0;
2435 else
2437 bottom = CurrPt, top = PrevPt;
2438 pETEs->ClockWise = 1;
2442 * don't add horizontal edges to the Edge table.
2444 if (bottom->y == top->y) continue;
2445 if (clip_rect && (top->y >= clip_rect->bottom || bottom->y <= clip_rect->top)) continue;
2446 pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */
2449 * initialize integer edge algorithm
2451 dy = bottom->y - top->y;
2452 bres_init_polygon(dy, top->x, bottom->x, &pETEs->bres);
2454 if (clip_rect) dy = min( bottom->y, clip_rect->bottom ) - max( top->y, clip_rect->top );
2455 if (total + dy < total) return 0; /* overflow */
2456 total += dy;
2458 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock);
2460 if (top->y < ET->ymin) ET->ymin = top->y;
2461 if (bottom->y > ET->ymax) ET->ymax = bottom->y;
2462 pETEs++;
2465 return total;
2468 /***********************************************************************
2469 * REGION_loadAET
2471 * This routine moves EdgeTableEntries from the
2472 * EdgeTable into the Active Edge Table,
2473 * leaving them sorted by smaller x coordinate.
2476 static void REGION_loadAET( struct list *AET, struct list *ETEs )
2478 struct edge_table_entry *ptr, *next, *entry;
2479 struct list *active;
2481 LIST_FOR_EACH_ENTRY_SAFE( ptr, next, ETEs, struct edge_table_entry, entry )
2483 LIST_FOR_EACH( active, AET )
2485 entry = LIST_ENTRY( active, struct edge_table_entry, entry );
2486 if (entry->bres.minor_axis >= ptr->bres.minor_axis) break;
2488 list_remove( &ptr->entry );
2489 list_add_before( active, &ptr->entry );
2493 /***********************************************************************
2494 * REGION_computeWAET
2496 * This routine links the AET by the
2497 * nextWETE (winding EdgeTableEntry) link for
2498 * use by the winding number rule. The final
2499 * Active Edge Table (AET) might look something
2500 * like:
2502 * AET
2503 * ---------- --------- ---------
2504 * |ymax | |ymax | |ymax |
2505 * | ... | |... | |... |
2506 * |next |->|next |->|next |->...
2507 * |nextWETE| |nextWETE| |nextWETE|
2508 * --------- --------- ^--------
2509 * | | |
2510 * V-------------------> V---> ...
2513 static void REGION_computeWAET( struct list *AET, struct list *WETE )
2515 struct edge_table_entry *active;
2516 BOOL inside = TRUE;
2517 int isInside = 0;
2519 list_init( WETE );
2520 LIST_FOR_EACH_ENTRY( active, AET, struct edge_table_entry, entry )
2522 if (active->ClockWise)
2523 isInside++;
2524 else
2525 isInside--;
2527 if ((!inside && !isInside) || (inside && isInside))
2529 list_add_tail( WETE, &active->winding_entry );
2530 inside = !inside;
2535 /***********************************************************************
2536 * next_scanline
2538 * Update the Active Edge Table for the next scan line and sort it again.
2540 static inline BOOL next_scanline( struct list *AET, int y )
2542 struct edge_table_entry *active, *next, *insert;
2543 BOOL changed = FALSE;
2545 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2547 if (active->ymax == y) /* leaving this edge */
2549 list_remove( &active->entry );
2550 changed = TRUE;
2552 else bres_incr_polygon( &active->bres );
2554 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2556 LIST_FOR_EACH_ENTRY( insert, AET, struct edge_table_entry, entry )
2558 if (insert == active) break;
2559 if (insert->bres.minor_axis > active->bres.minor_axis) break;
2561 if (insert == active) continue;
2562 list_remove( &active->entry );
2563 list_add_before( &insert->entry, &active->entry );
2564 changed = TRUE;
2566 return changed;
2569 /***********************************************************************
2570 * REGION_FreeStorage
2572 * Clean up our act.
2574 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2576 ScanLineListBlock *tmpSLLBlock;
2578 while (pSLLBlock)
2580 tmpSLLBlock = pSLLBlock->next;
2581 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2582 pSLLBlock = tmpSLLBlock;
2586 /***********************************************************************
2587 * create_polypolygon_region
2589 * Helper for CreatePolyPolygonRgn.
2591 HRGN create_polypolygon_region( const POINT *Pts, const INT *Count, INT nbpolygons, INT mode,
2592 const RECT *clip_rect )
2594 HRGN hrgn = 0;
2595 WINEREGION *obj = NULL;
2596 INT y; /* current scanline */
2597 struct list WETE, *pWETE; /* Winding Edge Table */
2598 ScanLineList *pSLL; /* current scanLineList */
2599 EdgeTable ET; /* header node for ET */
2600 struct list AET; /* header for AET */
2601 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2602 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2603 struct edge_table_entry *active;
2604 unsigned int nb_points;
2605 int cur_band = 0, prev_band = 0;
2606 INT i, poly, total, first = 1;
2608 TRACE("%p, count %d, polygons %d, mode %d\n", Pts, *Count, nbpolygons, mode);
2610 /* special case a rectangle */
2612 if (((nbpolygons == 1) && ((*Count == 4) ||
2613 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2614 (((Pts[0].y == Pts[1].y) &&
2615 (Pts[1].x == Pts[2].x) &&
2616 (Pts[2].y == Pts[3].y) &&
2617 (Pts[3].x == Pts[0].x)) ||
2618 ((Pts[0].x == Pts[1].x) &&
2619 (Pts[1].y == Pts[2].y) &&
2620 (Pts[2].x == Pts[3].x) &&
2621 (Pts[3].y == Pts[0].y))))
2622 return CreateRectRgn( min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2623 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2625 for(poly = total = 0; poly < nbpolygons; poly++)
2626 total += Count[poly];
2627 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2628 return 0;
2630 if (!(nb_points = REGION_CreateEdgeTable( Count, nbpolygons, Pts, &ET, pETEs, &SLLBlock, clip_rect )))
2631 goto done;
2632 if (!(obj = alloc_region( nb_points / 2 )))
2633 goto done;
2635 if (clip_rect) ET.ymax = min( ET.ymax, clip_rect->bottom );
2636 list_init( &AET );
2637 pSLL = ET.scanlines.next;
2638 if (mode != WINDING) {
2640 * for each scanline
2642 for (y = ET.ymin; y < ET.ymax; y++) {
2644 * Add a new edge to the active edge table when we
2645 * get to the next edge.
2647 if (pSLL != NULL && y == pSLL->scanline) {
2648 REGION_loadAET(&AET, &pSLL->edgelist);
2649 pSLL = pSLL->next;
2652 if (!clip_rect || y >= clip_rect->top)
2654 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2656 if (first)
2658 obj->rects[obj->numRects].left = active->bres.minor_axis;
2659 obj->rects[obj->numRects].top = y;
2661 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2663 obj->rects[obj->numRects].right = active->bres.minor_axis;
2664 obj->rects[obj->numRects].bottom = y + 1;
2665 obj->numRects++;
2667 first = !first;
2671 next_scanline( &AET, y );
2673 if (obj->numRects)
2675 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2676 cur_band = obj->numRects;
2680 else {
2682 * for each scanline
2684 for (y = ET.ymin; y < ET.ymax; y++) {
2686 * Add a new edge to the active edge table when we
2687 * get to the next edge.
2689 if (pSLL != NULL && y == pSLL->scanline) {
2690 REGION_loadAET(&AET, &pSLL->edgelist);
2691 REGION_computeWAET( &AET, &WETE );
2692 pSLL = pSLL->next;
2694 pWETE = list_head( &WETE );
2697 * for each active edge
2699 if (!clip_rect || y >= clip_rect->top)
2701 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2704 * add to the buffer only those edges that
2705 * are in the Winding active edge table.
2707 if (pWETE == &active->winding_entry)
2709 if (first)
2711 obj->rects[obj->numRects].left = active->bres.minor_axis;
2712 obj->rects[obj->numRects].top = y;
2714 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2716 obj->rects[obj->numRects].right = active->bres.minor_axis;
2717 obj->rects[obj->numRects].bottom = y + 1;
2718 obj->numRects++;
2720 first = !first;
2721 pWETE = list_next( &WETE, pWETE );
2727 * recompute the winding active edge table if
2728 * we just resorted or have exited an edge.
2730 if (next_scanline( &AET, y )) REGION_computeWAET( &AET, &WETE );
2732 if (obj->numRects)
2734 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2735 cur_band = obj->numRects;
2740 assert( obj->numRects <= nb_points / 2 );
2742 if (obj->numRects)
2744 obj->extents.left = INT_MAX;
2745 obj->extents.right = INT_MIN;
2746 obj->extents.top = obj->rects[0].top;
2747 obj->extents.bottom = obj->rects[obj->numRects-1].bottom;
2748 for (i = 0; i < obj->numRects; i++)
2750 obj->extents.left = min( obj->extents.left, obj->rects[i].left );
2751 obj->extents.right = max( obj->extents.right, obj->rects[i].right );
2754 REGION_compact( obj );
2756 if (!(hrgn = alloc_gdi_handle( obj, OBJ_REGION, &region_funcs )))
2757 free_region( obj );
2759 done:
2760 REGION_FreeStorage(SLLBlock.next);
2761 HeapFree( GetProcessHeap(), 0, pETEs );
2762 return hrgn;
2766 /***********************************************************************
2767 * CreatePolyPolygonRgn (GDI32.@)
2769 HRGN WINAPI CreatePolyPolygonRgn( const POINT *pts, const INT *count, INT nbpolygons, INT mode )
2771 return create_polypolygon_region( pts, count, nbpolygons, mode, NULL );
2775 /***********************************************************************
2776 * CreatePolygonRgn (GDI32.@)
2778 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count, INT mode )
2780 return create_polypolygon_region( points, &count, 1, mode, NULL );