hidclass.sys: Pass sizeof(packet) as input for IOCTL_HID_SET_OUTPUT_REPORT.
[wine.git] / dlls / gdi32 / region.c
blob4df48209de58dfd578e975a5b08f00df01055a79
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 "ntgdi_private.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(region);
110 static BOOL REGION_DeleteObject( HGDIOBJ handle );
112 static const struct gdi_obj_funcs region_funcs =
114 NULL, /* pGetObjectW */
115 NULL, /* pUnrealizeObject */
116 REGION_DeleteObject /* pDeleteObject */
119 /* Check if two RECTs overlap. */
120 static inline BOOL overlapping( const RECT *r1, const RECT *r2 )
122 return (r1->right > r2->left && r1->left < r2->right &&
123 r1->bottom > r2->top && r1->top < r2->bottom);
126 static BOOL grow_region( WINEREGION *rgn, int size )
128 RECT *new_rects;
130 if (size <= rgn->size) return TRUE;
132 if (rgn->rects == rgn->rects_buf)
134 new_rects = HeapAlloc( GetProcessHeap(), 0, size * sizeof(RECT) );
135 if (!new_rects) return FALSE;
136 memcpy( new_rects, rgn->rects, rgn->numRects * sizeof(RECT) );
138 else
140 new_rects = HeapReAlloc( GetProcessHeap(), 0, rgn->rects, size * sizeof(RECT) );
141 if (!new_rects) return FALSE;
143 rgn->rects = new_rects;
144 rgn->size = size;
145 return TRUE;
148 static BOOL add_rect( WINEREGION *reg, INT left, INT top, INT right, INT bottom )
150 RECT *rect;
151 if (reg->numRects >= reg->size && !grow_region( reg, 2 * reg->size ))
152 return FALSE;
154 rect = reg->rects + reg->numRects++;
155 rect->left = left;
156 rect->top = top;
157 rect->right = right;
158 rect->bottom = bottom;
159 return TRUE;
162 static inline void empty_region( WINEREGION *reg )
164 reg->numRects = 0;
165 reg->extents.left = reg->extents.top = reg->extents.right = reg->extents.bottom = 0;
168 static inline BOOL is_in_rect( const RECT *rect, int x, int y )
170 return (rect->right > x && rect->left <= x && rect->bottom > y && rect->top <= y);
175 * This file contains a few macros to help track
176 * the edge of a filled object. The object is assumed
177 * to be filled in scanline order, and thus the
178 * algorithm used is an extension of Bresenham's line
179 * drawing algorithm which assumes that y is always the
180 * major axis.
181 * Since these pieces of code are the same for any filled shape,
182 * it is more convenient to gather the library in one
183 * place, but since these pieces of code are also in
184 * the inner loops of output primitives, procedure call
185 * overhead is out of the question.
186 * See the author for a derivation if needed.
191 * This structure contains all of the information needed
192 * to run the bresenham algorithm.
193 * The variables may be hardcoded into the declarations
194 * instead of using this structure to make use of
195 * register declarations.
197 struct bres_info
199 INT minor_axis; /* minor axis */
200 INT d; /* decision variable */
201 INT m, m1; /* slope and slope+1 */
202 INT incr1, incr2; /* error increments */
207 * In scan converting polygons, we want to choose those pixels
208 * which are inside the polygon. Thus, we add .5 to the starting
209 * x coordinate for both left and right edges. Now we choose the
210 * first pixel which is inside the pgon for the left edge and the
211 * first pixel which is outside the pgon for the right edge.
212 * Draw the left pixel, but not the right.
214 * How to add .5 to the starting x coordinate:
215 * If the edge is moving to the right, then subtract dy from the
216 * error term from the general form of the algorithm.
217 * If the edge is moving to the left, then add dy to the error term.
219 * The reason for the difference between edges moving to the left
220 * and edges moving to the right is simple: If an edge is moving
221 * to the right, then we want the algorithm to flip immediately.
222 * If it is moving to the left, then we don't want it to flip until
223 * we traverse an entire pixel.
225 static inline void bres_init_polygon( int dy, int x1, int x2, struct bres_info *bres )
227 int dx;
230 * if the edge is horizontal, then it is ignored
231 * and assumed not to be processed. Otherwise, do this stuff.
233 if (!dy) return;
235 bres->minor_axis = x1;
236 dx = x2 - x1;
237 if (dx < 0)
239 bres->m = dx / dy;
240 bres->m1 = bres->m - 1;
241 bres->incr1 = -2 * dx + 2 * dy * bres->m1;
242 bres->incr2 = -2 * dx + 2 * dy * bres->m;
243 bres->d = 2 * bres->m * dy - 2 * dx - 2 * dy;
245 else
247 bres->m = dx / (dy);
248 bres->m1 = bres->m + 1;
249 bres->incr1 = 2 * dx - 2 * dy * bres->m1;
250 bres->incr2 = 2 * dx - 2 * dy * bres->m;
251 bres->d = -2 * bres->m * dy + 2 * dx;
255 static inline void bres_incr_polygon( struct bres_info *bres )
257 if (bres->m1 > 0) {
258 if (bres->d > 0) {
259 bres->minor_axis += bres->m1;
260 bres->d += bres->incr1;
262 else {
263 bres->minor_axis += bres->m;
264 bres->d += bres->incr2;
266 } else {
267 if (bres->d >= 0) {
268 bres->minor_axis += bres->m1;
269 bres->d += bres->incr1;
271 else {
272 bres->minor_axis += bres->m;
273 bres->d += bres->incr2;
280 * These are the data structures needed to scan
281 * convert regions. Two different scan conversion
282 * methods are available -- the even-odd method, and
283 * the winding number method.
284 * The even-odd rule states that a point is inside
285 * the polygon if a ray drawn from that point in any
286 * direction will pass through an odd number of
287 * path segments.
288 * By the winding number rule, a point is decided
289 * to be inside the polygon if a ray drawn from that
290 * point in any direction passes through a different
291 * number of clockwise and counter-clockwise path
292 * segments.
294 * These data structures are adapted somewhat from
295 * the algorithm in (Foley/Van Dam) for scan converting
296 * polygons.
297 * The basic algorithm is to start at the top (smallest y)
298 * of the polygon, stepping down to the bottom of
299 * the polygon by incrementing the y coordinate. We
300 * keep a list of edges which the current scanline crosses,
301 * sorted by x. This list is called the Active Edge Table (AET)
302 * As we change the y-coordinate, we update each entry in
303 * in the active edge table to reflect the edges new xcoord.
304 * This list must be sorted at each scanline in case
305 * two edges intersect.
306 * We also keep a data structure known as the Edge Table (ET),
307 * which keeps track of all the edges which the current
308 * scanline has not yet reached. The ET is basically a
309 * list of ScanLineList structures containing a list of
310 * edges which are entered at a given scanline. There is one
311 * ScanLineList per scanline at which an edge is entered.
312 * When we enter a new edge, we move it from the ET to the AET.
314 * From the AET, we can implement the even-odd rule as in
315 * (Foley/Van Dam).
316 * The winding number rule is a little trickier. We also
317 * keep the EdgeTableEntries in the AET linked by the
318 * nextWETE (winding EdgeTableEntry) link. This allows
319 * the edges to be linked just as before for updating
320 * purposes, but only uses the edges linked by the nextWETE
321 * link as edges representing spans of the polygon to
322 * drawn (as with the even-odd rule).
325 typedef struct edge_table_entry {
326 struct list entry;
327 struct list winding_entry;
328 INT ymax; /* ycoord at which we exit this edge. */
329 struct bres_info bres; /* Bresenham info to run the edge */
330 int ClockWise; /* flag for winding number rule */
331 } EdgeTableEntry;
334 typedef struct _ScanLineList{
335 struct list edgelist;
336 INT scanline; /* the scanline represented */
337 struct _ScanLineList *next; /* next in the list */
338 } ScanLineList;
341 typedef struct {
342 INT ymax; /* ymax for the polygon */
343 INT ymin; /* ymin for the polygon */
344 ScanLineList scanlines; /* header node */
345 } EdgeTable;
349 * Here is a struct to help with storage allocation
350 * so we can allocate a big chunk at a time, and then take
351 * pieces from this heap when we need to.
353 #define SLLSPERBLOCK 25
355 typedef struct _ScanLineListBlock {
356 ScanLineList SLLs[SLLSPERBLOCK];
357 struct _ScanLineListBlock *next;
358 } ScanLineListBlock;
361 /* Note the parameter order is different from the X11 equivalents */
363 static BOOL REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
364 static BOOL REGION_OffsetRegion(WINEREGION *d, WINEREGION *s, INT x, INT y);
365 static BOOL REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
366 static BOOL REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
367 static BOOL REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
368 static BOOL REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
369 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
371 /***********************************************************************
372 * get_region_type
374 static inline INT get_region_type( const WINEREGION *obj )
376 switch(obj->numRects)
378 case 0: return NULLREGION;
379 case 1: return SIMPLEREGION;
380 default: return COMPLEXREGION;
385 /***********************************************************************
386 * REGION_DumpRegion
387 * Outputs the contents of a WINEREGION
389 static void REGION_DumpRegion(WINEREGION *pReg)
391 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
393 TRACE("Region %p: %s %d rects\n", pReg, wine_dbgstr_rect(&pReg->extents), pReg->numRects);
394 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
395 TRACE("\t%s\n", wine_dbgstr_rect(pRect));
396 return;
400 /***********************************************************************
401 * init_region
403 * Initialize a new empty region.
405 static BOOL init_region( WINEREGION *pReg, INT n )
407 n = max( n, RGN_DEFAULT_RECTS );
409 if (n > RGN_DEFAULT_RECTS)
411 if (n > INT_MAX / sizeof(RECT)) return FALSE;
412 if (!(pReg->rects = HeapAlloc( GetProcessHeap(), 0, n * sizeof( RECT ) )))
413 return FALSE;
415 else
416 pReg->rects = pReg->rects_buf;
418 pReg->size = n;
419 empty_region(pReg);
420 return TRUE;
423 /***********************************************************************
424 * destroy_region
426 static void destroy_region( WINEREGION *pReg )
428 if (pReg->rects != pReg->rects_buf)
429 HeapFree( GetProcessHeap(), 0, pReg->rects );
432 /***********************************************************************
433 * free_region
435 static void free_region( WINEREGION *rgn )
437 destroy_region( rgn );
438 HeapFree( GetProcessHeap(), 0, rgn );
441 /***********************************************************************
442 * alloc_region
444 static WINEREGION *alloc_region( INT n )
446 WINEREGION *rgn = HeapAlloc( GetProcessHeap(), 0, sizeof(*rgn) );
448 if (rgn && !init_region( rgn, n ))
450 free_region( rgn );
451 rgn = NULL;
453 return rgn;
456 /************************************************************
457 * move_rects
459 * Move rectangles from src to dst leaving src with no rectangles.
461 static inline void move_rects( WINEREGION *dst, WINEREGION *src )
463 destroy_region( dst );
464 if (src->rects == src->rects_buf)
466 dst->rects = dst->rects_buf;
467 memcpy( dst->rects, src->rects, src->numRects * sizeof(RECT) );
469 else
470 dst->rects = src->rects;
471 dst->size = src->size;
472 dst->numRects = src->numRects;
473 init_region( src, 0 );
476 /***********************************************************************
477 * REGION_DeleteObject
479 static BOOL REGION_DeleteObject( HGDIOBJ handle )
481 WINEREGION *rgn = free_gdi_handle( handle );
483 if (!rgn) return FALSE;
484 free_region( rgn );
485 return TRUE;
489 /***********************************************************************
490 * REGION_OffsetRegion
491 * Offset a WINEREGION by x,y
493 static BOOL REGION_OffsetRegion( WINEREGION *rgn, WINEREGION *srcrgn, INT x, INT y )
495 if( rgn != srcrgn)
497 if (!REGION_CopyRegion( rgn, srcrgn)) return FALSE;
499 if(x || y) {
500 int nbox = rgn->numRects;
501 RECT *pbox = rgn->rects;
503 if(nbox) {
504 while(nbox--) {
505 pbox->left += x;
506 pbox->right += x;
507 pbox->top += y;
508 pbox->bottom += y;
509 pbox++;
511 rgn->extents.left += x;
512 rgn->extents.right += x;
513 rgn->extents.top += y;
514 rgn->extents.bottom += y;
517 return TRUE;
520 /***********************************************************************
521 * NtGdiOffsetRgn (win32u.@)
523 * Moves a region by the specified X- and Y-axis offsets.
525 * PARAMS
526 * hrgn [I] Region to offset.
527 * x [I] Offset right if positive or left if negative.
528 * y [I] Offset down if positive or up if negative.
530 * RETURNS
531 * Success:
532 * NULLREGION - The new region is empty.
533 * SIMPLEREGION - The new region can be represented by one rectangle.
534 * COMPLEXREGION - The new region can only be represented by more than
535 * one rectangle.
536 * Failure: ERROR
538 INT WINAPI NtGdiOffsetRgn( HRGN hrgn, INT x, INT y )
540 WINEREGION *obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION );
541 INT ret;
543 TRACE("%p %d,%d\n", hrgn, x, y);
545 if (!obj)
546 return ERROR;
548 REGION_OffsetRegion( obj, obj, x, y);
550 ret = get_region_type( obj );
551 GDI_ReleaseObj( hrgn );
552 return ret;
556 /***********************************************************************
557 * NtGdiGetRgnBox (win32u.@)
559 * Retrieves the bounding rectangle of the region. The bounding rectangle
560 * is the smallest rectangle that contains the entire region.
562 * PARAMS
563 * hrgn [I] Region to retrieve bounding rectangle from.
564 * rect [O] Rectangle that will receive the coordinates of the bounding
565 * rectangle.
567 * RETURNS
568 * NULLREGION - The new region is empty.
569 * SIMPLEREGION - The new region can be represented by one rectangle.
570 * COMPLEXREGION - The new region can only be represented by more than
571 * one rectangle.
573 INT WINAPI NtGdiGetRgnBox( HRGN hrgn, RECT *rect )
575 WINEREGION *obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION );
576 if (obj)
578 INT ret;
579 rect->left = obj->extents.left;
580 rect->top = obj->extents.top;
581 rect->right = obj->extents.right;
582 rect->bottom = obj->extents.bottom;
583 TRACE("%p %s\n", hrgn, wine_dbgstr_rect(rect));
584 ret = get_region_type( obj );
585 GDI_ReleaseObj(hrgn);
586 return ret;
588 return ERROR;
592 /***********************************************************************
593 * NtGdiCreateRectRgn (win32u.@)
595 * Creates a simple rectangular region.
597 * PARAMS
598 * left [I] Left coordinate of rectangle.
599 * top [I] Top coordinate of rectangle.
600 * right [I] Right coordinate of rectangle.
601 * bottom [I] Bottom coordinate of rectangle.
603 * RETURNS
604 * Success: Handle to region.
605 * Failure: NULL.
607 HRGN WINAPI NtGdiCreateRectRgn( INT left, INT top, INT right, INT bottom )
609 HRGN hrgn;
610 WINEREGION *obj;
612 if (!(obj = alloc_region( RGN_DEFAULT_RECTS ))) return 0;
614 if (!(hrgn = alloc_gdi_handle( &obj->obj, NTGDI_OBJ_REGION, &region_funcs )))
616 free_region( obj );
617 return 0;
619 TRACE( "%d,%d-%d,%d returning %p\n", left, top, right, bottom, hrgn );
620 NtGdiSetRectRgn( hrgn, left, top, right, bottom );
621 return hrgn;
625 /***********************************************************************
626 * NtGdiSetRectRgn (win32u.@)
628 * Sets a region to a simple rectangular region.
630 * PARAMS
631 * hrgn [I] Region to convert.
632 * left [I] Left coordinate of rectangle.
633 * top [I] Top coordinate of rectangle.
634 * right [I] Right coordinate of rectangle.
635 * bottom [I] Bottom coordinate of rectangle.
637 * RETURNS
638 * Success: Non-zero.
639 * Failure: Zero.
641 * NOTES
642 * Allows either or both left and top to be greater than right or bottom.
644 BOOL WINAPI NtGdiSetRectRgn( HRGN hrgn, INT left, INT top, INT right, INT bottom )
646 WINEREGION *obj;
648 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
650 if (!(obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION ))) return FALSE;
652 if (left > right) { INT tmp = left; left = right; right = tmp; }
653 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
655 if((left != right) && (top != bottom))
657 obj->rects->left = obj->extents.left = left;
658 obj->rects->top = obj->extents.top = top;
659 obj->rects->right = obj->extents.right = right;
660 obj->rects->bottom = obj->extents.bottom = bottom;
661 obj->numRects = 1;
663 else
664 empty_region(obj);
666 GDI_ReleaseObj( hrgn );
667 return TRUE;
671 /***********************************************************************
672 * NtGdiCreateRoundRectRgn (win32u.@)
674 * Creates a rectangular region with rounded corners.
676 * PARAMS
677 * left [I] Left coordinate of rectangle.
678 * top [I] Top coordinate of rectangle.
679 * right [I] Right coordinate of rectangle.
680 * bottom [I] Bottom coordinate of rectangle.
681 * ellipse_width [I] Width of the ellipse at each corner.
682 * ellipse_height [I] Height of the ellipse at each corner.
684 * RETURNS
685 * Success: Handle to region.
686 * Failure: NULL.
688 * NOTES
689 * If ellipse_width or ellipse_height is less than 2 logical units then
690 * it is treated as though CreateRectRgn() was called instead.
692 HRGN WINAPI NtGdiCreateRoundRectRgn( INT left, INT top, INT right, INT bottom,
693 INT ellipse_width, INT ellipse_height )
695 WINEREGION *obj;
696 HRGN hrgn = 0;
697 int a, b, i, x, y;
698 INT64 asq, bsq, dx, dy, err;
699 RECT *rects;
701 /* Make the dimensions sensible */
703 if (left > right) { INT tmp = left; left = right; right = tmp; }
704 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
705 /* the region is for the rectangle interior, but only at right and bottom for some reason */
706 right--;
707 bottom--;
709 ellipse_width = min( right - left, abs( ellipse_width ));
710 ellipse_height = min( bottom - top, abs( ellipse_height ));
712 /* Check if we can do a normal rectangle instead */
714 if ((ellipse_width < 2) || (ellipse_height < 2))
715 return NtGdiCreateRectRgn( left, top, right, bottom );
717 if (!(obj = alloc_region( ellipse_height ))) return 0;
718 obj->numRects = ellipse_height;
719 obj->extents.left = left;
720 obj->extents.top = top;
721 obj->extents.right = right;
722 obj->extents.bottom = bottom;
723 rects = obj->rects;
725 /* based on an algorithm by Alois Zingl */
727 a = ellipse_width - 1;
728 b = ellipse_height - 1;
729 asq = (INT64)8 * a * a;
730 bsq = (INT64)8 * b * b;
731 dx = (INT64)4 * b * b * (1 - a);
732 dy = (INT64)4 * a * a * (1 + (b % 2));
733 err = dx + dy + a * a * (b % 2);
735 x = 0;
736 y = ellipse_height / 2;
738 rects[y].left = left;
739 rects[y].right = right;
741 while (x <= ellipse_width / 2)
743 INT64 e2 = 2 * err;
744 if (e2 >= dx)
746 x++;
747 err += dx += bsq;
749 if (e2 <= dy)
751 y++;
752 err += dy += asq;
753 rects[y].left = left + x;
754 rects[y].right = right - x;
757 for (i = 0; i < ellipse_height / 2; i++)
759 rects[i].left = rects[b - i].left;
760 rects[i].right = rects[b - i].right;
761 rects[i].top = top + i;
762 rects[i].bottom = rects[i].top + 1;
764 for (; i < ellipse_height; i++)
766 rects[i].top = bottom - ellipse_height + i;
767 rects[i].bottom = rects[i].top + 1;
769 rects[ellipse_height / 2].top = top + ellipse_height / 2; /* extend to top of rectangle */
771 hrgn = alloc_gdi_handle( &obj->obj, NTGDI_OBJ_REGION, &region_funcs );
773 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
774 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
775 if (!hrgn) free_region( obj );
776 return hrgn;
780 /***********************************************************************
781 * NtGdiCreateEllipticRgn (win32u.@)
783 * Creates an elliptical region.
785 * PARAMS
786 * left [I] Left coordinate of bounding rectangle.
787 * top [I] Top coordinate of bounding rectangle.
788 * right [I] Right coordinate of bounding rectangle.
789 * bottom [I] Bottom coordinate of bounding rectangle.
791 * RETURNS
792 * Success: Handle to region.
793 * Failure: NULL.
795 * NOTES
796 * This is a special case of CreateRoundRectRgn() where the width of the
797 * ellipse at each corner is equal to the width the rectangle and
798 * the same for the height.
800 HRGN WINAPI NtGdiCreateEllipticRgn( INT left, INT top, INT right, INT bottom )
802 return NtGdiCreateRoundRectRgn( left, top, right, bottom,
803 right - left, bottom - top );
807 /***********************************************************************
808 * NtGdiGetRegionData (win32u.@)
810 * Retrieves the data that specifies the region.
812 * PARAMS
813 * hrgn [I] Region to retrieve the region data from.
814 * count [I] The size of the buffer pointed to by rgndata in bytes.
815 * rgndata [I] The buffer to receive data about the region.
817 * RETURNS
818 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
819 * the number of bytes copied to the output buffer.
820 * Failure: 0.
822 * NOTES
823 * The format of the Buffer member of RGNDATA is determined by the iType
824 * member of the region data header.
825 * Currently this is always RDH_RECTANGLES, which specifies that the format
826 * is the array of RECT's that specify the region. The length of the array
827 * is specified by the nCount member of the region data header.
829 DWORD WINAPI NtGdiGetRegionData( HRGN hrgn, DWORD count, RGNDATA *rgndata )
831 DWORD size;
832 WINEREGION *obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION );
834 TRACE(" %p count = %d, rgndata = %p\n", hrgn, count, rgndata);
836 if(!obj) return 0;
838 size = obj->numRects * sizeof(RECT);
839 if (!rgndata || count < FIELD_OFFSET(RGNDATA, Buffer[size]))
841 GDI_ReleaseObj( hrgn );
842 if (rgndata) /* buffer is too small, signal it by return 0 */
843 return 0;
844 /* user requested buffer size with NULL rgndata */
845 return FIELD_OFFSET(RGNDATA, Buffer[size]);
848 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
849 rgndata->rdh.iType = RDH_RECTANGLES;
850 rgndata->rdh.nCount = obj->numRects;
851 rgndata->rdh.nRgnSize = size;
852 rgndata->rdh.rcBound.left = obj->extents.left;
853 rgndata->rdh.rcBound.top = obj->extents.top;
854 rgndata->rdh.rcBound.right = obj->extents.right;
855 rgndata->rdh.rcBound.bottom = obj->extents.bottom;
857 memcpy( rgndata->Buffer, obj->rects, size );
859 GDI_ReleaseObj( hrgn );
860 return FIELD_OFFSET(RGNDATA, Buffer[size]);
864 static void translate( POINT *pt, UINT count, const XFORM *xform )
866 while (count--)
868 double x = pt->x;
869 double y = pt->y;
870 pt->x = floor( x * xform->eM11 + y * xform->eM21 + xform->eDx + 0.5 );
871 pt->y = floor( x * xform->eM12 + y * xform->eM22 + xform->eDy + 0.5 );
872 pt++;
877 /***********************************************************************
878 * NtGdiExtCreateRegion (win32u.@)
880 * Creates a region as specified by the transformation data and region data.
882 * PARAMS
883 * lpXform [I] World-space to logical-space transformation data.
884 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
885 * rgndata [I] Data that specifies the region.
887 * RETURNS
888 * Success: Handle to region.
889 * Failure: NULL.
891 * NOTES
892 * See GetRegionData().
894 HRGN WINAPI NtGdiExtCreateRegion( const XFORM *xform, DWORD count, const RGNDATA *rgndata )
896 HRGN hrgn = 0;
897 WINEREGION *obj;
898 const RECT *pCurRect, *pEndRect;
900 if (!rgndata || rgndata->rdh.dwSize < sizeof(RGNDATAHEADER))
901 return 0;
903 /* XP doesn't care about the type */
904 if( rgndata->rdh.iType != RDH_RECTANGLES )
905 WARN("(Unsupported region data type: %u)\n", rgndata->rdh.iType);
907 if (xform)
909 const RECT *pCurRect, *pEndRect;
911 hrgn = NtGdiCreateRectRgn( 0, 0, 0, 0 );
913 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
914 for (pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
916 static const INT count = 4;
917 HRGN poly_hrgn;
918 POINT pt[4];
920 pt[0].x = pCurRect->left;
921 pt[0].y = pCurRect->top;
922 pt[1].x = pCurRect->right;
923 pt[1].y = pCurRect->top;
924 pt[2].x = pCurRect->right;
925 pt[2].y = pCurRect->bottom;
926 pt[3].x = pCurRect->left;
927 pt[3].y = pCurRect->bottom;
929 translate( pt, 4, xform );
930 poly_hrgn = CreatePolyPolygonRgn( pt, &count, 1, WINDING );
931 NtGdiCombineRgn( hrgn, hrgn, poly_hrgn, RGN_OR );
932 DeleteObject( poly_hrgn );
934 return hrgn;
937 if (!(obj = alloc_region( rgndata->rdh.nCount ))) return 0;
939 pEndRect = (const RECT *)rgndata->Buffer + rgndata->rdh.nCount;
940 for(pCurRect = (const RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
942 if (pCurRect->left < pCurRect->right && pCurRect->top < pCurRect->bottom)
944 if (!REGION_UnionRectWithRegion( pCurRect, obj )) goto done;
947 hrgn = alloc_gdi_handle( &obj->obj, NTGDI_OBJ_REGION, &region_funcs );
949 done:
950 if (!hrgn) free_region( obj );
952 TRACE("%p %d %p returning %p\n", xform, count, rgndata, hrgn );
953 return hrgn;
957 /***********************************************************************
958 * NtGdiPtInRegion (win32u.@)
960 * Tests whether the specified point is inside a region.
962 * PARAMS
963 * hrgn [I] Region to test.
964 * x [I] X-coordinate of point to test.
965 * y [I] Y-coordinate of point to test.
967 * RETURNS
968 * Non-zero if the point is inside the region or zero otherwise.
970 BOOL WINAPI NtGdiPtInRegion( HRGN hrgn, INT x, INT y )
972 WINEREGION *obj;
973 BOOL ret = FALSE;
975 if ((obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION )))
977 if (obj->numRects > 0 && is_in_rect( &obj->extents, x, y ))
978 region_find_pt( obj, x, y, &ret );
979 GDI_ReleaseObj( hrgn );
981 return ret;
985 /***********************************************************************
986 * NtGdiRectInRegion (win32u.@)
988 * Tests if a rectangle is at least partly inside the specified region.
990 * PARAMS
991 * hrgn [I] Region to test.
992 * rect [I] Rectangle to test.
994 * RETURNS
995 * Non-zero if the rectangle is partially inside the region or
996 * zero otherwise.
998 BOOL WINAPI NtGdiRectInRegion( HRGN hrgn, const RECT *rect )
1000 WINEREGION *obj;
1001 BOOL ret = FALSE;
1002 RECT rc;
1003 int i;
1005 /* swap the coordinates to make right >= left and bottom >= top */
1006 /* (region building rectangles are normalized the same way) */
1007 rc = *rect;
1008 order_rect( &rc );
1010 if ((obj = GDI_GetObjPtr( hrgn, NTGDI_OBJ_REGION )))
1012 if ((obj->numRects > 0) && overlapping(&obj->extents, &rc))
1014 for (i = region_find_pt( obj, rc.left, rc.top, &ret ); !ret && i < obj->numRects; i++ )
1016 if (obj->rects[i].bottom <= rc.top)
1017 continue; /* not far enough down yet */
1019 if (obj->rects[i].top >= rc.bottom)
1020 break; /* too far down */
1022 if (obj->rects[i].right <= rc.left)
1023 continue; /* not far enough over yet */
1025 if (obj->rects[i].left >= rc.right)
1026 continue;
1028 ret = TRUE;
1031 GDI_ReleaseObj(hrgn);
1033 return ret;
1036 /***********************************************************************
1037 * NtGdiEqualRgn (win32u.@)
1039 * Tests whether one region is identical to another.
1041 * PARAMS
1042 * hrgn1 [I] The first region to compare.
1043 * hrgn2 [I] The second region to compare.
1045 * RETURNS
1046 * Non-zero if both regions are identical or zero otherwise.
1048 BOOL WINAPI NtGdiEqualRgn( HRGN hrgn1, HRGN hrgn2 )
1050 WINEREGION *obj1, *obj2;
1051 BOOL ret = FALSE;
1053 if ((obj1 = GDI_GetObjPtr( hrgn1, NTGDI_OBJ_REGION )))
1055 if ((obj2 = GDI_GetObjPtr( hrgn2, NTGDI_OBJ_REGION )))
1057 int i;
1059 if ( obj1->numRects != obj2->numRects ) goto done;
1060 if ( obj1->numRects == 0 )
1062 ret = TRUE;
1063 goto done;
1066 if (obj1->extents.left != obj2->extents.left) goto done;
1067 if (obj1->extents.right != obj2->extents.right) goto done;
1068 if (obj1->extents.top != obj2->extents.top) goto done;
1069 if (obj1->extents.bottom != obj2->extents.bottom) goto done;
1070 for( i = 0; i < obj1->numRects; i++ )
1072 if (obj1->rects[i].left != obj2->rects[i].left) goto done;
1073 if (obj1->rects[i].right != obj2->rects[i].right) goto done;
1074 if (obj1->rects[i].top != obj2->rects[i].top) goto done;
1075 if (obj1->rects[i].bottom != obj2->rects[i].bottom) goto done;
1077 ret = TRUE;
1078 done:
1079 GDI_ReleaseObj(hrgn2);
1081 GDI_ReleaseObj(hrgn1);
1083 return ret;
1086 /***********************************************************************
1087 * REGION_UnionRectWithRegion
1088 * Adds a rectangle to a WINEREGION
1090 static BOOL REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1092 WINEREGION region;
1094 init_region( &region, 1 );
1095 region.numRects = 1;
1096 region.extents = *region.rects = *rect;
1097 return REGION_UnionRegion(rgn, rgn, &region);
1101 BOOL add_rect_to_region( HRGN rgn, const RECT *rect )
1103 WINEREGION *obj = GDI_GetObjPtr( rgn, NTGDI_OBJ_REGION );
1104 BOOL ret;
1106 if (!obj) return FALSE;
1107 ret = REGION_UnionRectWithRegion( rect, obj );
1108 GDI_ReleaseObj( rgn );
1109 return ret;
1112 /***********************************************************************
1113 * REGION_CreateFrameRgn
1115 * Create a region that is a frame around another region.
1116 * Compute the intersection of the region moved in all 4 directions
1117 * ( +x, -x, +y, -y) and subtract from the original.
1118 * The result looks slightly better than in Windows :)
1120 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1122 WINEREGION tmprgn;
1123 BOOL bRet = FALSE;
1124 WINEREGION* destObj = NULL;
1125 WINEREGION *srcObj = GDI_GetObjPtr( hSrc, NTGDI_OBJ_REGION );
1127 tmprgn.rects = NULL;
1128 if (!srcObj) return FALSE;
1129 if (srcObj->numRects != 0)
1131 if (!(destObj = GDI_GetObjPtr( hDest, NTGDI_OBJ_REGION ))) goto done;
1132 if (!init_region( &tmprgn, srcObj->numRects )) goto done;
1134 if (!REGION_OffsetRegion( destObj, srcObj, -x, 0)) goto done;
1135 if (!REGION_OffsetRegion( &tmprgn, srcObj, x, 0)) goto done;
1136 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1137 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, -y)) goto done;
1138 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1139 if (!REGION_OffsetRegion( &tmprgn, srcObj, 0, y)) goto done;
1140 if (!REGION_IntersectRegion( destObj, destObj, &tmprgn )) goto done;
1141 if (!REGION_SubtractRegion( destObj, srcObj, destObj )) goto done;
1142 bRet = TRUE;
1144 done:
1145 destroy_region( &tmprgn );
1146 if (destObj) GDI_ReleaseObj ( hDest );
1147 GDI_ReleaseObj( hSrc );
1148 return bRet;
1152 /***********************************************************************
1153 * NtGdiCombineRgn (win32u.@)
1155 * Combines two regions with the specified operation and stores the result
1156 * in the specified destination region.
1158 * PARAMS
1159 * hDest [I] The region that receives the combined result.
1160 * hSrc1 [I] The first source region.
1161 * hSrc2 [I] The second source region.
1162 * mode [I] The way in which the source regions will be combined. See notes.
1164 * RETURNS
1165 * Success:
1166 * NULLREGION - The new region is empty.
1167 * SIMPLEREGION - The new region can be represented by one rectangle.
1168 * COMPLEXREGION - The new region can only be represented by more than
1169 * one rectangle.
1170 * Failure: ERROR
1172 * NOTES
1173 * The two source regions can be the same region.
1174 * The mode can be one of the following:
1175 *| RGN_AND - Intersection of the regions
1176 *| RGN_OR - Union of the regions
1177 *| RGN_XOR - Unions of the regions minus any intersection.
1178 *| RGN_DIFF - Difference (subtraction) of the regions.
1180 INT WINAPI NtGdiCombineRgn( HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode )
1182 WINEREGION *destObj = GDI_GetObjPtr( hDest, NTGDI_OBJ_REGION );
1183 INT result = ERROR;
1185 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1186 if (destObj)
1188 WINEREGION *src1Obj = GDI_GetObjPtr( hSrc1, NTGDI_OBJ_REGION );
1190 if (src1Obj)
1192 TRACE("dump src1Obj:\n");
1193 if(TRACE_ON(region))
1194 REGION_DumpRegion(src1Obj);
1195 if (mode == RGN_COPY)
1197 if (REGION_CopyRegion( destObj, src1Obj ))
1198 result = get_region_type( destObj );
1200 else
1202 WINEREGION *src2Obj = GDI_GetObjPtr( hSrc2, NTGDI_OBJ_REGION );
1204 if (src2Obj)
1206 TRACE("dump src2Obj:\n");
1207 if(TRACE_ON(region))
1208 REGION_DumpRegion(src2Obj);
1209 switch (mode)
1211 case RGN_AND:
1212 if (REGION_IntersectRegion( destObj, src1Obj, src2Obj ))
1213 result = get_region_type( destObj );
1214 break;
1215 case RGN_OR:
1216 if (REGION_UnionRegion( destObj, src1Obj, src2Obj ))
1217 result = get_region_type( destObj );
1218 break;
1219 case RGN_XOR:
1220 if (REGION_XorRegion( destObj, src1Obj, src2Obj ))
1221 result = get_region_type( destObj );
1222 break;
1223 case RGN_DIFF:
1224 if (REGION_SubtractRegion( destObj, src1Obj, src2Obj ))
1225 result = get_region_type( destObj );
1226 break;
1228 GDI_ReleaseObj( hSrc2 );
1231 GDI_ReleaseObj( hSrc1 );
1233 TRACE("dump destObj:\n");
1234 if(TRACE_ON(region))
1235 REGION_DumpRegion(destObj);
1237 GDI_ReleaseObj( hDest );
1239 return result;
1242 /***********************************************************************
1243 * REGION_SetExtents
1244 * Re-calculate the extents of a region
1246 static void REGION_SetExtents (WINEREGION *pReg)
1248 RECT *pRect, *pRectEnd, *pExtents;
1250 if (pReg->numRects == 0)
1252 pReg->extents.left = 0;
1253 pReg->extents.top = 0;
1254 pReg->extents.right = 0;
1255 pReg->extents.bottom = 0;
1256 return;
1259 pExtents = &pReg->extents;
1260 pRect = pReg->rects;
1261 pRectEnd = &pRect[pReg->numRects - 1];
1264 * Since pRect is the first rectangle in the region, it must have the
1265 * smallest top and since pRectEnd is the last rectangle in the region,
1266 * it must have the largest bottom, because of banding. Initialize left and
1267 * right from pRect and pRectEnd, resp., as good things to initialize them
1268 * to...
1270 pExtents->left = pRect->left;
1271 pExtents->top = pRect->top;
1272 pExtents->right = pRectEnd->right;
1273 pExtents->bottom = pRectEnd->bottom;
1275 while (pRect <= pRectEnd)
1277 if (pRect->left < pExtents->left)
1278 pExtents->left = pRect->left;
1279 if (pRect->right > pExtents->right)
1280 pExtents->right = pRect->right;
1281 pRect++;
1285 /***********************************************************************
1286 * REGION_CopyRegion
1288 static BOOL REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1290 if (dst != src) /* don't want to copy to itself */
1292 if (dst->size < src->numRects && !grow_region( dst, src->numRects ))
1293 return FALSE;
1295 dst->numRects = src->numRects;
1296 dst->extents.left = src->extents.left;
1297 dst->extents.top = src->extents.top;
1298 dst->extents.right = src->extents.right;
1299 dst->extents.bottom = src->extents.bottom;
1300 memcpy(dst->rects, src->rects, src->numRects * sizeof(RECT));
1302 return TRUE;
1305 /***********************************************************************
1306 * REGION_MirrorRegion
1308 static BOOL REGION_MirrorRegion( WINEREGION *dst, WINEREGION *src, int width )
1310 int i, start, end;
1311 RECT extents;
1312 RECT *rects;
1313 WINEREGION tmp;
1315 if (dst != src)
1317 if (!grow_region( dst, src->numRects )) return FALSE;
1318 rects = dst->rects;
1319 dst->numRects = src->numRects;
1321 else
1323 if (!init_region( &tmp, src->numRects )) return FALSE;
1324 rects = tmp.rects;
1325 tmp.numRects = src->numRects;
1328 extents.left = width - src->extents.right;
1329 extents.right = width - src->extents.left;
1330 extents.top = src->extents.top;
1331 extents.bottom = src->extents.bottom;
1333 for (start = 0; start < src->numRects; start = end)
1335 /* find the end of the current band */
1336 for (end = start + 1; end < src->numRects; end++)
1337 if (src->rects[end].top != src->rects[end - 1].top) break;
1339 for (i = 0; i < end - start; i++)
1341 rects[start + i].left = width - src->rects[end - i - 1].right;
1342 rects[start + i].right = width - src->rects[end - i - 1].left;
1343 rects[start + i].top = src->rects[end - i - 1].top;
1344 rects[start + i].bottom = src->rects[end - i - 1].bottom;
1348 if (dst == src)
1349 move_rects( dst, &tmp );
1351 dst->extents = extents;
1352 return TRUE;
1355 /***********************************************************************
1356 * mirror_region
1358 INT mirror_region( HRGN dst, HRGN src, INT width )
1360 WINEREGION *src_rgn, *dst_rgn;
1361 INT ret = ERROR;
1363 if (!(src_rgn = GDI_GetObjPtr( src, NTGDI_OBJ_REGION ))) return ERROR;
1364 if ((dst_rgn = GDI_GetObjPtr( dst, NTGDI_OBJ_REGION )))
1366 if (REGION_MirrorRegion( dst_rgn, src_rgn, width )) ret = get_region_type( dst_rgn );
1367 GDI_ReleaseObj( dst_rgn );
1369 GDI_ReleaseObj( src_rgn );
1370 return ret;
1373 /***********************************************************************
1374 * MirrorRgn (GDI32.@)
1376 BOOL WINAPI MirrorRgn( HWND hwnd, HRGN hrgn )
1378 static BOOL (WINAPI *pGetWindowRect)( HWND hwnd, LPRECT rect );
1379 RECT rect;
1381 /* yes, a HWND in gdi32, don't ask */
1382 if (!pGetWindowRect)
1384 HMODULE user32 = GetModuleHandleW(L"user32.dll");
1385 if (!user32) return FALSE;
1386 if (!(pGetWindowRect = (void *)GetProcAddress( user32, "GetWindowRect" ))) return FALSE;
1388 pGetWindowRect( hwnd, &rect );
1389 return mirror_region( hrgn, hrgn, rect.right - rect.left ) != ERROR;
1393 /***********************************************************************
1394 * REGION_Coalesce
1396 * Attempt to merge the rects in the current band with those in the
1397 * previous one. Used only by REGION_RegionOp.
1399 * Results:
1400 * The new index for the previous band.
1402 * Side Effects:
1403 * If coalescing takes place:
1404 * - rectangles in the previous band will have their bottom fields
1405 * altered.
1406 * - pReg->numRects will be decreased.
1409 static INT REGION_Coalesce (
1410 WINEREGION *pReg, /* Region to coalesce */
1411 INT prevStart, /* Index of start of previous band */
1412 INT curStart /* Index of start of current band */
1414 RECT *pPrevRect; /* Current rect in previous band */
1415 RECT *pCurRect; /* Current rect in current band */
1416 RECT *pRegEnd; /* End of region */
1417 INT curNumRects; /* Number of rectangles in current band */
1418 INT prevNumRects; /* Number of rectangles in previous band */
1419 INT bandtop; /* top coordinate for current band */
1421 pRegEnd = &pReg->rects[pReg->numRects];
1423 pPrevRect = &pReg->rects[prevStart];
1424 prevNumRects = curStart - prevStart;
1427 * Figure out how many rectangles are in the current band. Have to do
1428 * this because multiple bands could have been added in REGION_RegionOp
1429 * at the end when one region has been exhausted.
1431 pCurRect = &pReg->rects[curStart];
1432 bandtop = pCurRect->top;
1433 for (curNumRects = 0;
1434 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1435 curNumRects++)
1437 pCurRect++;
1440 if (pCurRect != pRegEnd)
1443 * If more than one band was added, we have to find the start
1444 * of the last band added so the next coalescing job can start
1445 * at the right place... (given when multiple bands are added,
1446 * this may be pointless -- see above).
1448 pRegEnd--;
1449 while (pRegEnd[-1].top == pRegEnd->top)
1451 pRegEnd--;
1453 curStart = pRegEnd - pReg->rects;
1454 pRegEnd = pReg->rects + pReg->numRects;
1457 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1458 pCurRect -= curNumRects;
1460 * The bands may only be coalesced if the bottom of the previous
1461 * matches the top scanline of the current.
1463 if (pPrevRect->bottom == pCurRect->top)
1466 * Make sure the bands have rects in the same places. This
1467 * assumes that rects have been added in such a way that they
1468 * cover the most area possible. I.e. two rects in a band must
1469 * have some horizontal space between them.
1473 if ((pPrevRect->left != pCurRect->left) ||
1474 (pPrevRect->right != pCurRect->right))
1477 * The bands don't line up so they can't be coalesced.
1479 return (curStart);
1481 pPrevRect++;
1482 pCurRect++;
1483 prevNumRects -= 1;
1484 } while (prevNumRects != 0);
1486 pReg->numRects -= curNumRects;
1487 pCurRect -= curNumRects;
1488 pPrevRect -= curNumRects;
1491 * The bands may be merged, so set the bottom of each rect
1492 * in the previous band to that of the corresponding rect in
1493 * the current band.
1497 pPrevRect->bottom = pCurRect->bottom;
1498 pPrevRect++;
1499 pCurRect++;
1500 curNumRects -= 1;
1501 } while (curNumRects != 0);
1504 * If only one band was added to the region, we have to backup
1505 * curStart to the start of the previous band.
1507 * If more than one band was added to the region, copy the
1508 * other bands down. The assumption here is that the other bands
1509 * came from the same region as the current one and no further
1510 * coalescing can be done on them since it's all been done
1511 * already... curStart is already in the right place.
1513 if (pCurRect == pRegEnd)
1515 curStart = prevStart;
1517 else
1521 *pPrevRect++ = *pCurRect++;
1522 } while (pCurRect != pRegEnd);
1527 return (curStart);
1530 /**********************************************************************
1531 * REGION_compact
1533 * To keep regions from growing without bound, shrink the array of rectangles
1534 * to match the new number of rectangles in the region.
1536 * Only do this if the number of rectangles allocated is more than
1537 * twice the number of rectangles in the region.
1539 static void REGION_compact( WINEREGION *reg )
1541 if ((reg->numRects < reg->size / 2) && (reg->numRects > RGN_DEFAULT_RECTS))
1543 RECT *new_rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects, reg->numRects * sizeof(RECT) );
1544 if (new_rects)
1546 reg->rects = new_rects;
1547 reg->size = reg->numRects;
1552 /***********************************************************************
1553 * REGION_RegionOp
1555 * Apply an operation to two regions. Called by REGION_Union,
1556 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1558 * Results:
1559 * None.
1561 * Side Effects:
1562 * The new region is overwritten.
1564 * Notes:
1565 * The idea behind this function is to view the two regions as sets.
1566 * Together they cover a rectangle of area that this function divides
1567 * into horizontal bands where points are covered only by one region
1568 * or by both. For the first case, the nonOverlapFunc is called with
1569 * each the band and the band's upper and lower extents. For the
1570 * second, the overlapFunc is called to process the entire band. It
1571 * is responsible for clipping the rectangles in the band, though
1572 * this function provides the boundaries.
1573 * At the end of each band, the new region is coalesced, if possible,
1574 * to reduce the number of rectangles in the region.
1577 static BOOL REGION_RegionOp(
1578 WINEREGION *destReg, /* Place to store result */
1579 WINEREGION *reg1, /* First region in operation */
1580 WINEREGION *reg2, /* 2nd region in operation */
1581 BOOL (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1582 BOOL (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1583 BOOL (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1585 WINEREGION newReg;
1586 RECT *r1; /* Pointer into first region */
1587 RECT *r2; /* Pointer into 2d region */
1588 RECT *r1End; /* End of 1st region */
1589 RECT *r2End; /* End of 2d region */
1590 INT ybot; /* Bottom of intersection */
1591 INT ytop; /* Top of intersection */
1592 INT prevBand; /* Index of start of
1593 * previous band in newReg */
1594 INT curBand; /* Index of start of current
1595 * band in newReg */
1596 RECT *r1BandEnd; /* End of current band in r1 */
1597 RECT *r2BandEnd; /* End of current band in r2 */
1598 INT top; /* Top of non-overlapping band */
1599 INT bot; /* Bottom of non-overlapping band */
1602 * Initialization:
1603 * set r1, r2, r1End and r2End appropriately, preserve the important
1604 * parts of the destination region until the end in case it's one of
1605 * the two source regions, then mark the "new" region empty, allocating
1606 * another array of rectangles for it to use.
1608 r1 = reg1->rects;
1609 r2 = reg2->rects;
1610 r1End = r1 + reg1->numRects;
1611 r2End = r2 + reg2->numRects;
1614 * Allocate a reasonable number of rectangles for the new region. The idea
1615 * is to allocate enough so the individual functions don't need to
1616 * reallocate and copy the array, which is time consuming, yet we don't
1617 * have to worry about using too much memory. I hope to be able to
1618 * nuke the Xrealloc() at the end of this function eventually.
1620 if (!init_region( &newReg, max(reg1->numRects,reg2->numRects) * 2 )) return FALSE;
1623 * Initialize ybot and ytop.
1624 * In the upcoming loop, ybot and ytop serve different functions depending
1625 * on whether the band being handled is an overlapping or non-overlapping
1626 * band.
1627 * In the case of a non-overlapping band (only one of the regions
1628 * has points in the band), ybot is the bottom of the most recent
1629 * intersection and thus clips the top of the rectangles in that band.
1630 * ytop is the top of the next intersection between the two regions and
1631 * serves to clip the bottom of the rectangles in the current band.
1632 * For an overlapping band (where the two regions intersect), ytop clips
1633 * the top of the rectangles of both regions and ybot clips the bottoms.
1635 if (reg1->extents.top < reg2->extents.top)
1636 ybot = reg1->extents.top;
1637 else
1638 ybot = reg2->extents.top;
1641 * prevBand serves to mark the start of the previous band so rectangles
1642 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1643 * In the beginning, there is no previous band, so prevBand == curBand
1644 * (curBand is set later on, of course, but the first band will always
1645 * start at index 0). prevBand and curBand must be indices because of
1646 * the possible expansion, and resultant moving, of the new region's
1647 * array of rectangles.
1649 prevBand = 0;
1653 curBand = newReg.numRects;
1656 * This algorithm proceeds one source-band (as opposed to a
1657 * destination band, which is determined by where the two regions
1658 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1659 * rectangle after the last one in the current band for their
1660 * respective regions.
1662 r1BandEnd = r1;
1663 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1665 r1BandEnd++;
1668 r2BandEnd = r2;
1669 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1671 r2BandEnd++;
1675 * First handle the band that doesn't intersect, if any.
1677 * Note that attention is restricted to one band in the
1678 * non-intersecting region at once, so if a region has n
1679 * bands between the current position and the next place it overlaps
1680 * the other, this entire loop will be passed through n times.
1682 if (r1->top < r2->top)
1684 top = max(r1->top,ybot);
1685 bot = min(r1->bottom,r2->top);
1687 if ((top != bot) && (nonOverlap1Func != NULL))
1689 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, top, bot)) return FALSE;
1692 ytop = r2->top;
1694 else if (r2->top < r1->top)
1696 top = max(r2->top,ybot);
1697 bot = min(r2->bottom,r1->top);
1699 if ((top != bot) && (nonOverlap2Func != NULL))
1701 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, top, bot)) return FALSE;
1704 ytop = r1->top;
1706 else
1708 ytop = r1->top;
1712 * If any rectangles got added to the region, try and coalesce them
1713 * with rectangles from the previous band. Note we could just do
1714 * this test in miCoalesce, but some machines incur a not
1715 * inconsiderable cost for function calls, so...
1717 if (newReg.numRects != curBand)
1719 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1723 * Now see if we've hit an intersecting band. The two bands only
1724 * intersect if ybot > ytop
1726 ybot = min(r1->bottom, r2->bottom);
1727 curBand = newReg.numRects;
1728 if (ybot > ytop)
1730 if (!overlapFunc(&newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot)) return FALSE;
1733 if (newReg.numRects != curBand)
1735 prevBand = REGION_Coalesce (&newReg, prevBand, curBand);
1739 * If we've finished with a band (bottom == ybot) we skip forward
1740 * in the region to the next band.
1742 if (r1->bottom == ybot)
1744 r1 = r1BandEnd;
1746 if (r2->bottom == ybot)
1748 r2 = r2BandEnd;
1750 } while ((r1 != r1End) && (r2 != r2End));
1753 * Deal with whichever region still has rectangles left.
1755 curBand = newReg.numRects;
1756 if (r1 != r1End)
1758 if (nonOverlap1Func != NULL)
1762 r1BandEnd = r1;
1763 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1765 r1BandEnd++;
1767 if (!nonOverlap1Func(&newReg, r1, r1BandEnd, max(r1->top,ybot), r1->bottom))
1768 return FALSE;
1769 r1 = r1BandEnd;
1770 } while (r1 != r1End);
1773 else if ((r2 != r2End) && (nonOverlap2Func != NULL))
1777 r2BandEnd = r2;
1778 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1780 r2BandEnd++;
1782 if (!nonOverlap2Func(&newReg, r2, r2BandEnd, max(r2->top,ybot), r2->bottom))
1783 return FALSE;
1784 r2 = r2BandEnd;
1785 } while (r2 != r2End);
1788 if (newReg.numRects != curBand)
1790 REGION_Coalesce (&newReg, prevBand, curBand);
1793 REGION_compact( &newReg );
1794 move_rects( destReg, &newReg );
1795 return TRUE;
1798 /***********************************************************************
1799 * Region Intersection
1800 ***********************************************************************/
1803 /***********************************************************************
1804 * REGION_IntersectO
1806 * Handle an overlapping band for REGION_Intersect.
1808 * Results:
1809 * None.
1811 * Side Effects:
1812 * Rectangles may be added to the region.
1815 static BOOL REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1816 RECT *r2, RECT *r2End, INT top, INT bottom)
1819 INT left, right;
1821 while ((r1 != r1End) && (r2 != r2End))
1823 left = max(r1->left, r2->left);
1824 right = min(r1->right, r2->right);
1827 * If there's any overlap between the two rectangles, add that
1828 * overlap to the new region.
1829 * There's no need to check for subsumption because the only way
1830 * such a need could arise is if some region has two rectangles
1831 * right next to each other. Since that should never happen...
1833 if (left < right)
1835 if (!add_rect( pReg, left, top, right, bottom )) return FALSE;
1839 * Need to advance the pointers. Shift the one that extends
1840 * to the right the least, since the other still has a chance to
1841 * overlap with that region's next rectangle, if you see what I mean.
1843 if (r1->right < r2->right)
1845 r1++;
1847 else if (r2->right < r1->right)
1849 r2++;
1851 else
1853 r1++;
1854 r2++;
1857 return TRUE;
1860 /***********************************************************************
1861 * REGION_IntersectRegion
1863 static BOOL REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1864 WINEREGION *reg2)
1866 /* check for trivial reject */
1867 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1868 (!overlapping(&reg1->extents, &reg2->extents)))
1869 newReg->numRects = 0;
1870 else
1871 if (!REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL)) return FALSE;
1874 * Can't alter newReg's extents before we call miRegionOp because
1875 * it might be one of the source regions and miRegionOp depends
1876 * on the extents of those regions being the same. Besides, this
1877 * way there's no checking against rectangles that will be nuked
1878 * due to coalescing, so we have to examine fewer rectangles.
1880 REGION_SetExtents(newReg);
1881 return TRUE;
1884 /***********************************************************************
1885 * Region Union
1886 ***********************************************************************/
1888 /***********************************************************************
1889 * REGION_UnionNonO
1891 * Handle a non-overlapping band for the union operation. Just
1892 * Adds the rectangles into the region. Doesn't have to check for
1893 * subsumption or anything.
1895 * Results:
1896 * None.
1898 * Side Effects:
1899 * pReg->numRects is incremented and the final rectangles overwritten
1900 * with the rectangles we're passed.
1903 static BOOL REGION_UnionNonO(WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
1905 while (r != rEnd)
1907 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
1908 r++;
1910 return TRUE;
1913 /***********************************************************************
1914 * REGION_UnionO
1916 * Handle an overlapping band for the union operation. Picks the
1917 * left-most rectangle each time and merges it into the region.
1919 * Results:
1920 * None.
1922 * Side Effects:
1923 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1924 * be changed.
1927 static BOOL REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1928 RECT *r2, RECT *r2End, INT top, INT bottom)
1930 #define MERGERECT(r) \
1931 if ((pReg->numRects != 0) && \
1932 (pReg->rects[pReg->numRects-1].top == top) && \
1933 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
1934 (pReg->rects[pReg->numRects-1].right >= r->left)) \
1936 if (pReg->rects[pReg->numRects-1].right < r->right) \
1937 pReg->rects[pReg->numRects-1].right = r->right; \
1939 else \
1941 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
1943 r++;
1945 while ((r1 != r1End) && (r2 != r2End))
1947 if (r1->left < r2->left)
1949 MERGERECT(r1);
1951 else
1953 MERGERECT(r2);
1957 if (r1 != r1End)
1961 MERGERECT(r1);
1962 } while (r1 != r1End);
1964 else while (r2 != r2End)
1966 MERGERECT(r2);
1968 return TRUE;
1969 #undef MERGERECT
1972 /***********************************************************************
1973 * REGION_UnionRegion
1975 static BOOL REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1, WINEREGION *reg2)
1977 BOOL ret = TRUE;
1979 /* checks all the simple cases */
1982 * Region 1 and 2 are the same or region 1 is empty
1984 if ( (reg1 == reg2) || (!(reg1->numRects)) )
1986 if (newReg != reg2)
1987 ret = REGION_CopyRegion(newReg, reg2);
1988 return ret;
1992 * if nothing to union (region 2 empty)
1994 if (!(reg2->numRects))
1996 if (newReg != reg1)
1997 ret = REGION_CopyRegion(newReg, reg1);
1998 return ret;
2002 * Region 1 completely subsumes region 2
2004 if ((reg1->numRects == 1) &&
2005 (reg1->extents.left <= reg2->extents.left) &&
2006 (reg1->extents.top <= reg2->extents.top) &&
2007 (reg1->extents.right >= reg2->extents.right) &&
2008 (reg1->extents.bottom >= reg2->extents.bottom))
2010 if (newReg != reg1)
2011 ret = REGION_CopyRegion(newReg, reg1);
2012 return ret;
2016 * Region 2 completely subsumes region 1
2018 if ((reg2->numRects == 1) &&
2019 (reg2->extents.left <= reg1->extents.left) &&
2020 (reg2->extents.top <= reg1->extents.top) &&
2021 (reg2->extents.right >= reg1->extents.right) &&
2022 (reg2->extents.bottom >= reg1->extents.bottom))
2024 if (newReg != reg2)
2025 ret = REGION_CopyRegion(newReg, reg2);
2026 return ret;
2029 if ((ret = REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO)))
2031 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2032 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2033 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2034 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2036 return ret;
2039 /***********************************************************************
2040 * Region Subtraction
2041 ***********************************************************************/
2043 /***********************************************************************
2044 * REGION_SubtractNonO1
2046 * Deal with non-overlapping band for subtraction. Any parts from
2047 * region 2 we discard. Anything from region 1 we add to the region.
2049 * Results:
2050 * None.
2052 * Side Effects:
2053 * pReg may be affected.
2056 static BOOL REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd, INT top, INT bottom)
2058 while (r != rEnd)
2060 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE;
2061 r++;
2063 return TRUE;
2067 /***********************************************************************
2068 * REGION_SubtractO
2070 * Overlapping band subtraction. x1 is the left-most point not yet
2071 * checked.
2073 * Results:
2074 * None.
2076 * Side Effects:
2077 * pReg may have rectangles added to it.
2080 static BOOL REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2081 RECT *r2, RECT *r2End, INT top, INT bottom)
2083 INT left = r1->left;
2085 while ((r1 != r1End) && (r2 != r2End))
2087 if (r2->right <= left)
2090 * Subtrahend missed the boat: go to next subtrahend.
2092 r2++;
2094 else if (r2->left <= left)
2097 * Subtrahend precedes minuend: nuke left edge of minuend.
2099 left = r2->right;
2100 if (left >= r1->right)
2103 * Minuend completely covered: advance to next minuend and
2104 * reset left fence to edge of new minuend.
2106 r1++;
2107 if (r1 != r1End)
2108 left = r1->left;
2110 else
2113 * Subtrahend now used up since it doesn't extend beyond
2114 * minuend
2116 r2++;
2119 else if (r2->left < r1->right)
2122 * Left part of subtrahend covers part of minuend: add uncovered
2123 * part of minuend to region and skip to next subtrahend.
2125 if (!add_rect( pReg, left, top, r2->left, bottom )) return FALSE;
2126 left = r2->right;
2127 if (left >= r1->right)
2130 * Minuend used up: advance to new...
2132 r1++;
2133 if (r1 != r1End)
2134 left = r1->left;
2136 else
2139 * Subtrahend used up
2141 r2++;
2144 else
2147 * Minuend used up: add any remaining piece before advancing.
2149 if (r1->right > left)
2151 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2153 r1++;
2154 if (r1 != r1End)
2155 left = r1->left;
2160 * Add remaining minuend rectangles to region.
2162 while (r1 != r1End)
2164 if (!add_rect( pReg, left, top, r1->right, bottom )) return FALSE;
2165 r1++;
2166 if (r1 != r1End)
2168 left = r1->left;
2171 return TRUE;
2174 /***********************************************************************
2175 * REGION_SubtractRegion
2177 * Subtract regS from regM and leave the result in regD.
2178 * S stands for subtrahend, M for minuend and D for difference.
2180 * Results:
2181 * TRUE.
2183 * Side Effects:
2184 * regD is overwritten.
2187 static BOOL REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM, WINEREGION *regS )
2189 /* check for trivial reject */
2190 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2191 (!overlapping(&regM->extents, &regS->extents)) )
2192 return REGION_CopyRegion(regD, regM);
2194 if (!REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL))
2195 return FALSE;
2198 * Can't alter newReg's extents before we call miRegionOp because
2199 * it might be one of the source regions and miRegionOp depends
2200 * on the extents of those regions being the unaltered. Besides, this
2201 * way there's no checking against rectangles that will be nuked
2202 * due to coalescing, so we have to examine fewer rectangles.
2204 REGION_SetExtents (regD);
2205 return TRUE;
2208 /***********************************************************************
2209 * REGION_XorRegion
2211 static BOOL REGION_XorRegion(WINEREGION *dr, WINEREGION *sra, WINEREGION *srb)
2213 WINEREGION tra, trb;
2214 BOOL ret;
2216 if (!init_region( &tra, sra->numRects + 1 )) return FALSE;
2217 if ((ret = init_region( &trb, srb->numRects + 1 )))
2219 ret = REGION_SubtractRegion(&tra,sra,srb) &&
2220 REGION_SubtractRegion(&trb,srb,sra) &&
2221 REGION_UnionRegion(dr,&tra,&trb);
2222 destroy_region(&trb);
2224 destroy_region(&tra);
2225 return ret;
2228 /**************************************************************************
2230 * Poly Regions
2232 *************************************************************************/
2234 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2235 #define SMALL_COORDINATE 0x80000000
2237 /***********************************************************************
2238 * REGION_InsertEdgeInET
2240 * Insert the given edge into the edge table.
2241 * First we must find the correct bucket in the
2242 * Edge table, then find the right slot in the
2243 * bucket. Finally, we can insert it.
2246 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2247 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2250 struct list *ptr;
2251 ScanLineList *pSLL, *pPrevSLL;
2252 ScanLineListBlock *tmpSLLBlock;
2255 * find the right bucket to put the edge into
2257 pPrevSLL = &ET->scanlines;
2258 pSLL = pPrevSLL->next;
2259 while (pSLL && (pSLL->scanline < scanline))
2261 pPrevSLL = pSLL;
2262 pSLL = pSLL->next;
2266 * reassign pSLL (pointer to ScanLineList) if necessary
2268 if ((!pSLL) || (pSLL->scanline > scanline))
2270 if (*iSLLBlock > SLLSPERBLOCK-1)
2272 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2273 if(!tmpSLLBlock)
2275 WARN("Can't alloc SLLB\n");
2276 return;
2278 (*SLLBlock)->next = tmpSLLBlock;
2279 tmpSLLBlock->next = NULL;
2280 *SLLBlock = tmpSLLBlock;
2281 *iSLLBlock = 0;
2283 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2285 pSLL->next = pPrevSLL->next;
2286 list_init( &pSLL->edgelist );
2287 pPrevSLL->next = pSLL;
2289 pSLL->scanline = scanline;
2292 * now insert the edge in the right bucket
2294 LIST_FOR_EACH( ptr, &pSLL->edgelist )
2296 struct edge_table_entry *entry = LIST_ENTRY( ptr, struct edge_table_entry, entry );
2297 if (entry->bres.minor_axis >= ETE->bres.minor_axis) break;
2299 list_add_before( ptr, &ETE->entry );
2302 /***********************************************************************
2303 * REGION_CreateEdgeTable
2305 * This routine creates the edge table for
2306 * scan converting polygons.
2307 * The Edge Table (ET) looks like:
2309 * EdgeTable
2310 * --------
2311 * | ymax | ScanLineLists
2312 * |scanline|-->------------>-------------->...
2313 * -------- |scanline| |scanline|
2314 * |edgelist| |edgelist|
2315 * --------- ---------
2316 * | |
2317 * | |
2318 * V V
2319 * list of ETEs list of ETEs
2321 * where ETE is an EdgeTableEntry data structure,
2322 * and there is one ScanLineList per scanline at
2323 * which an edge is initially entered.
2326 static unsigned int REGION_CreateEdgeTable(const INT *Count, INT nbpolygons,
2327 const POINT *pts, EdgeTable *ET,
2328 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock,
2329 const RECT *clip_rect)
2331 const POINT *top, *bottom;
2332 const POINT *PrevPt, *CurrPt, *EndPt;
2333 INT poly, count;
2334 int iSLLBlock = 0;
2335 unsigned int dy, total = 0;
2338 * initialize the Edge Table.
2340 ET->scanlines.next = NULL;
2341 ET->ymax = SMALL_COORDINATE;
2342 ET->ymin = LARGE_COORDINATE;
2343 pSLLBlock->next = NULL;
2345 EndPt = pts - 1;
2346 for(poly = 0; poly < nbpolygons; poly++)
2348 count = Count[poly];
2349 EndPt += count;
2350 if(count < 2)
2351 continue;
2353 PrevPt = EndPt;
2356 * for each vertex in the array of points.
2357 * In this loop we are dealing with two vertices at
2358 * a time -- these make up one edge of the polygon.
2360 for ( ; count; PrevPt = CurrPt, count--)
2362 CurrPt = pts++;
2365 * find out which point is above and which is below.
2367 if (PrevPt->y > CurrPt->y)
2369 bottom = PrevPt, top = CurrPt;
2370 pETEs->ClockWise = 0;
2372 else
2374 bottom = CurrPt, top = PrevPt;
2375 pETEs->ClockWise = 1;
2379 * don't add horizontal edges to the Edge table.
2381 if (bottom->y == top->y) continue;
2382 if (clip_rect && (top->y >= clip_rect->bottom || bottom->y <= clip_rect->top)) continue;
2383 pETEs->ymax = bottom->y-1; /* -1 so we don't get last scanline */
2386 * initialize integer edge algorithm
2388 dy = bottom->y - top->y;
2389 bres_init_polygon(dy, top->x, bottom->x, &pETEs->bres);
2391 if (clip_rect) dy = min( bottom->y, clip_rect->bottom ) - max( top->y, clip_rect->top );
2392 if (total + dy < total) return 0; /* overflow */
2393 total += dy;
2395 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock, &iSLLBlock);
2397 if (top->y < ET->ymin) ET->ymin = top->y;
2398 if (bottom->y > ET->ymax) ET->ymax = bottom->y;
2399 pETEs++;
2402 return total;
2405 /***********************************************************************
2406 * REGION_loadAET
2408 * This routine moves EdgeTableEntries from the
2409 * EdgeTable into the Active Edge Table,
2410 * leaving them sorted by smaller x coordinate.
2413 static void REGION_loadAET( struct list *AET, struct list *ETEs )
2415 struct edge_table_entry *ptr, *next, *entry;
2416 struct list *active;
2418 LIST_FOR_EACH_ENTRY_SAFE( ptr, next, ETEs, struct edge_table_entry, entry )
2420 LIST_FOR_EACH( active, AET )
2422 entry = LIST_ENTRY( active, struct edge_table_entry, entry );
2423 if (entry->bres.minor_axis >= ptr->bres.minor_axis) break;
2425 list_remove( &ptr->entry );
2426 list_add_before( active, &ptr->entry );
2430 /***********************************************************************
2431 * REGION_computeWAET
2433 * This routine links the AET by the
2434 * nextWETE (winding EdgeTableEntry) link for
2435 * use by the winding number rule. The final
2436 * Active Edge Table (AET) might look something
2437 * like:
2439 * AET
2440 * ---------- --------- ---------
2441 * |ymax | |ymax | |ymax |
2442 * | ... | |... | |... |
2443 * |next |->|next |->|next |->...
2444 * |nextWETE| |nextWETE| |nextWETE|
2445 * --------- --------- ^--------
2446 * | | |
2447 * V-------------------> V---> ...
2450 static void REGION_computeWAET( struct list *AET, struct list *WETE )
2452 struct edge_table_entry *active;
2453 BOOL inside = TRUE;
2454 int isInside = 0;
2456 list_init( WETE );
2457 LIST_FOR_EACH_ENTRY( active, AET, struct edge_table_entry, entry )
2459 if (active->ClockWise)
2460 isInside++;
2461 else
2462 isInside--;
2464 if ((!inside && !isInside) || (inside && isInside))
2466 list_add_tail( WETE, &active->winding_entry );
2467 inside = !inside;
2472 /***********************************************************************
2473 * next_scanline
2475 * Update the Active Edge Table for the next scan line and sort it again.
2477 static inline BOOL next_scanline( struct list *AET, int y )
2479 struct edge_table_entry *active, *next, *insert;
2480 BOOL changed = FALSE;
2482 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2484 if (active->ymax == y) /* leaving this edge */
2486 list_remove( &active->entry );
2487 changed = TRUE;
2489 else bres_incr_polygon( &active->bres );
2491 LIST_FOR_EACH_ENTRY_SAFE( active, next, AET, struct edge_table_entry, entry )
2493 LIST_FOR_EACH_ENTRY( insert, AET, struct edge_table_entry, entry )
2495 if (insert == active) break;
2496 if (insert->bres.minor_axis > active->bres.minor_axis) break;
2498 if (insert == active) continue;
2499 list_remove( &active->entry );
2500 list_add_before( &insert->entry, &active->entry );
2501 changed = TRUE;
2503 return changed;
2506 /***********************************************************************
2507 * REGION_FreeStorage
2509 * Clean up our act.
2511 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2513 ScanLineListBlock *tmpSLLBlock;
2515 while (pSLLBlock)
2517 tmpSLLBlock = pSLLBlock->next;
2518 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2519 pSLLBlock = tmpSLLBlock;
2523 static void scan_convert( WINEREGION *obj, EdgeTable *ET, INT mode, const RECT *clip_rect )
2525 struct list AET;
2526 ScanLineList *pSLL;
2527 struct edge_table_entry *active;
2528 INT i, y, first = 1, cur_band = 0, prev_band = 0;
2530 if (clip_rect) ET->ymax = min( ET->ymax, clip_rect->bottom );
2532 list_init( &AET );
2533 pSLL = ET->scanlines.next;
2535 if (mode != WINDING)
2537 for (y = ET->ymin; y < ET->ymax; y++)
2539 /* Add a new edge to the active edge table when we get to the next edge. */
2540 if (pSLL != NULL && y == pSLL->scanline)
2542 REGION_loadAET(&AET, &pSLL->edgelist);
2543 pSLL = pSLL->next;
2546 if (!clip_rect || y >= clip_rect->top)
2548 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2550 if (first)
2552 obj->rects[obj->numRects].left = active->bres.minor_axis;
2553 obj->rects[obj->numRects].top = y;
2554 obj->rects[obj->numRects].bottom = y + 1;
2556 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2558 /* create new rect only if we can't merge with the previous one */
2559 if (!obj->numRects || obj->rects[obj->numRects-1].top != y ||
2560 obj->rects[obj->numRects-1].right < obj->rects[obj->numRects].left)
2561 obj->numRects++;
2562 obj->rects[obj->numRects-1].right = active->bres.minor_axis;
2564 first = !first;
2568 next_scanline( &AET, y );
2570 if (obj->numRects)
2572 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2573 cur_band = obj->numRects;
2577 else /* mode == WINDING */
2579 struct list WETE, *pWETE;
2581 for (y = ET->ymin; y < ET->ymax; y++)
2583 /* Add a new edge to the active edge table when we get to the next edge. */
2584 if (pSLL != NULL && y == pSLL->scanline)
2586 REGION_loadAET(&AET, &pSLL->edgelist);
2587 REGION_computeWAET( &AET, &WETE );
2588 pSLL = pSLL->next;
2590 pWETE = list_head( &WETE );
2592 if (!clip_rect || y >= clip_rect->top)
2594 LIST_FOR_EACH_ENTRY( active, &AET, struct edge_table_entry, entry )
2596 /* Add to the buffer only those edges that are in the Winding active edge table. */
2597 if (pWETE == &active->winding_entry)
2599 if (first)
2601 obj->rects[obj->numRects].left = active->bres.minor_axis;
2602 obj->rects[obj->numRects].top = y;
2604 else if (obj->rects[obj->numRects].left != active->bres.minor_axis)
2606 obj->rects[obj->numRects].right = active->bres.minor_axis;
2607 obj->rects[obj->numRects].bottom = y + 1;
2608 obj->numRects++;
2610 first = !first;
2611 pWETE = list_next( &WETE, pWETE );
2616 /* Recompute the winding active edge table if we just resorted or have exited an edge. */
2617 if (next_scanline( &AET, y )) REGION_computeWAET( &AET, &WETE );
2619 if (obj->numRects)
2621 prev_band = REGION_Coalesce( obj, prev_band, cur_band );
2622 cur_band = obj->numRects;
2627 assert( obj->numRects <= obj->size );
2629 if (obj->numRects)
2631 obj->extents.left = INT_MAX;
2632 obj->extents.right = INT_MIN;
2633 obj->extents.top = obj->rects[0].top;
2634 obj->extents.bottom = obj->rects[obj->numRects-1].bottom;
2635 for (i = 0; i < obj->numRects; i++)
2637 obj->extents.left = min( obj->extents.left, obj->rects[i].left );
2638 obj->extents.right = max( obj->extents.right, obj->rects[i].right );
2641 REGION_compact( obj );
2644 /***********************************************************************
2645 * create_polypolygon_region
2647 * Helper for CreatePolyPolygonRgn.
2649 HRGN create_polypolygon_region( const POINT *Pts, const INT *Count, INT nbpolygons, INT mode,
2650 const RECT *clip_rect )
2652 HRGN hrgn = 0;
2653 WINEREGION *obj = NULL;
2654 EdgeTable ET; /* header node for ET */
2655 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2656 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2657 unsigned int nb_points;
2658 INT poly, total;
2660 TRACE("%p, count %d, polygons %d, mode %d\n", Pts, *Count, nbpolygons, mode);
2662 /* special case a rectangle */
2664 if (((nbpolygons == 1) && ((*Count == 4) ||
2665 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2666 (((Pts[0].y == Pts[1].y) &&
2667 (Pts[1].x == Pts[2].x) &&
2668 (Pts[2].y == Pts[3].y) &&
2669 (Pts[3].x == Pts[0].x)) ||
2670 ((Pts[0].x == Pts[1].x) &&
2671 (Pts[1].y == Pts[2].y) &&
2672 (Pts[2].x == Pts[3].x) &&
2673 (Pts[3].y == Pts[0].y))))
2674 return NtGdiCreateRectRgn( min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2675 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2677 for(poly = total = 0; poly < nbpolygons; poly++)
2678 total += Count[poly];
2679 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2680 return 0;
2682 nb_points = REGION_CreateEdgeTable( Count, nbpolygons, Pts, &ET, pETEs, &SLLBlock, clip_rect );
2683 if ((obj = alloc_region( nb_points / 2 )))
2685 if (nb_points) scan_convert( obj, &ET, mode, clip_rect );
2687 if (!(hrgn = alloc_gdi_handle( &obj->obj, NTGDI_OBJ_REGION, &region_funcs )))
2688 free_region( obj );
2691 REGION_FreeStorage(SLLBlock.next);
2692 HeapFree( GetProcessHeap(), 0, pETEs );
2693 return hrgn;