2 * GDI region objects. Shamelessly ripped out from the X11 distribution
3 * Thanks for the nice license.
5 * Copyright 1993, 1994, 1995 Alexandre Julliard
6 * Modifications and additions: Copyright 1998 Huw Davies
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /************************************************************************
26 Copyright (c) 1987, 1988 X Consortium
28 Permission is hereby granted, free of charge, to any person obtaining a copy
29 of this software and associated documentation files (the "Software"), to deal
30 in the Software without restriction, including without limitation the rights
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 copies of the Software, and to permit persons to whom the Software is
33 furnished to do so, subject to the following conditions:
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 Except as contained in this notice, the name of the X Consortium shall not be
46 used in advertising or otherwise to promote the sale, use or other dealings
47 in this Software without prior written authorization from the X Consortium.
50 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
54 Permission to use, copy, modify, and distribute this software and its
55 documentation for any purpose and without fee is hereby granted,
56 provided that the above copyright notice appear in all copies and that
57 both that copyright notice and this permission notice appear in
58 supporting documentation, and that the name of Digital not be
59 used in advertising or publicity pertaining to distribution of the
60 software without specific, written prior permission.
62 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
63 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
64 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
65 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
66 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
67 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
70 ************************************************************************/
72 * The functions in this file implement the Region abstraction, similar to one
73 * used in the X11 sample server. A Region is simply an area, as the name
74 * implies, and is implemented as a "y-x-banded" array of rectangles. To
75 * explain: Each Region is made up of a certain number of rectangles sorted
76 * by y coordinate first, and then by x coordinate.
78 * Furthermore, the rectangles are banded such that every rectangle with a
79 * given upper-left y coordinate (y1) will have the same lower-right y
80 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
81 * will span the entire vertical distance of the band. This means that some
82 * areas that could be merged into a taller rectangle will be represented as
83 * several shorter rectangles to account for shorter rectangles to its left
84 * or right but within its "vertical scope".
86 * An added constraint on the rectangles is that they must cover as much
87 * horizontal area as possible. E.g. no two rectangles in a band are allowed
90 * Whenever possible, bands will be merged together to cover a greater vertical
91 * distance (and thus reduce the number of rectangles). Two bands can be merged
92 * only if the bottom of one touches the top of the other and they have
93 * rectangles in the same places (of the same width, of course). This maintains
94 * the y-x-banding that's so nice to have...
103 #include "gdi_private.h"
104 #include "wine/debug.h"
106 WINE_DEFAULT_DEBUG_CHANNEL(region
);
109 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, HDC hdc
);
110 static BOOL
REGION_DeleteObject( HGDIOBJ handle
);
112 static const struct gdi_obj_funcs region_funcs
=
114 REGION_SelectObject
, /* pSelectObject */
115 NULL
, /* pGetObjectA */
116 NULL
, /* pGetObjectW */
117 NULL
, /* pUnrealizeObject */
118 REGION_DeleteObject
/* pDeleteObject */
121 /* 1 if two RECTs overlap.
122 * 0 if two RECTs do not overlap.
124 #define EXTENTCHECK(r1, r2) \
125 ((r1)->right > (r2)->left && \
126 (r1)->left < (r2)->right && \
127 (r1)->bottom > (r2)->top && \
128 (r1)->top < (r2)->bottom)
131 static BOOL
add_rect( WINEREGION
*reg
, INT left
, INT top
, INT right
, INT bottom
)
134 if (reg
->numRects
>= reg
->size
)
136 RECT
*newrects
= HeapReAlloc( GetProcessHeap(), 0, reg
->rects
, 2 * sizeof(RECT
) * reg
->size
);
137 if (!newrects
) return FALSE
;
138 reg
->rects
= newrects
;
141 rect
= reg
->rects
+ reg
->numRects
++;
145 rect
->bottom
= bottom
;
149 #define EMPTY_REGION(pReg) do { \
150 (pReg)->numRects = 0; \
151 (pReg)->extents.left = (pReg)->extents.top = 0; \
152 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
155 #define INRECT(r, x, y) \
156 ( ( ((r).right > x)) && \
157 ( ((r).left <= x)) && \
158 ( ((r).bottom > y)) && \
163 * number of points to buffer before sending them off
164 * to scanlines() : Must be an even number
166 #define NUMPTSTOBUFFER 200
169 * used to allocate buffers for points and link
170 * the buffers together
173 typedef struct _POINTBLOCK
{
174 POINT pts
[NUMPTSTOBUFFER
];
175 struct _POINTBLOCK
*next
;
181 * This file contains a few macros to help track
182 * the edge of a filled object. The object is assumed
183 * to be filled in scanline order, and thus the
184 * algorithm used is an extension of Bresenham's line
185 * drawing algorithm which assumes that y is always the
187 * Since these pieces of code are the same for any filled shape,
188 * it is more convenient to gather the library in one
189 * place, but since these pieces of code are also in
190 * the inner loops of output primitives, procedure call
191 * overhead is out of the question.
192 * See the author for a derivation if needed.
197 * In scan converting polygons, we want to choose those pixels
198 * which are inside the polygon. Thus, we add .5 to the starting
199 * x coordinate for both left and right edges. Now we choose the
200 * first pixel which is inside the pgon for the left edge and the
201 * first pixel which is outside the pgon for the right edge.
202 * Draw the left pixel, but not the right.
204 * How to add .5 to the starting x coordinate:
205 * If the edge is moving to the right, then subtract dy from the
206 * error term from the general form of the algorithm.
207 * If the edge is moving to the left, then add dy to the error term.
209 * The reason for the difference between edges moving to the left
210 * and edges moving to the right is simple: If an edge is moving
211 * to the right, then we want the algorithm to flip immediately.
212 * If it is moving to the left, then we don't want it to flip until
213 * we traverse an entire pixel.
215 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
216 int dx; /* local storage */ \
219 * if the edge is horizontal, then it is ignored \
220 * and assumed not to be processed. Otherwise, do this stuff. \
224 dx = (x2) - xStart; \
228 incr1 = -2 * dx + 2 * (dy) * m1; \
229 incr2 = -2 * dx + 2 * (dy) * m; \
230 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
234 incr1 = 2 * dx - 2 * (dy) * m1; \
235 incr2 = 2 * dx - 2 * (dy) * m; \
236 d = -2 * m * (dy) + 2 * dx; \
241 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
264 * This structure contains all of the information needed
265 * to run the bresenham algorithm.
266 * The variables may be hardcoded into the declarations
267 * instead of using this structure to make use of
268 * register declarations.
271 INT minor_axis
; /* minor axis */
272 INT d
; /* decision variable */
273 INT m
, m1
; /* slope and slope+1 */
274 INT incr1
, incr2
; /* error increments */
278 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
279 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
280 bres.m, bres.m1, bres.incr1, bres.incr2)
282 #define BRESINCRPGONSTRUCT(bres) \
283 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
288 * These are the data structures needed to scan
289 * convert regions. Two different scan conversion
290 * methods are available -- the even-odd method, and
291 * the winding number method.
292 * The even-odd rule states that a point is inside
293 * the polygon if a ray drawn from that point in any
294 * direction will pass through an odd number of
296 * By the winding number rule, a point is decided
297 * to be inside the polygon if a ray drawn from that
298 * point in any direction passes through a different
299 * number of clockwise and counter-clockwise path
302 * These data structures are adapted somewhat from
303 * the algorithm in (Foley/Van Dam) for scan converting
305 * The basic algorithm is to start at the top (smallest y)
306 * of the polygon, stepping down to the bottom of
307 * the polygon by incrementing the y coordinate. We
308 * keep a list of edges which the current scanline crosses,
309 * sorted by x. This list is called the Active Edge Table (AET)
310 * As we change the y-coordinate, we update each entry in
311 * in the active edge table to reflect the edges new xcoord.
312 * This list must be sorted at each scanline in case
313 * two edges intersect.
314 * We also keep a data structure known as the Edge Table (ET),
315 * which keeps track of all the edges which the current
316 * scanline has not yet reached. The ET is basically a
317 * list of ScanLineList structures containing a list of
318 * edges which are entered at a given scanline. There is one
319 * ScanLineList per scanline at which an edge is entered.
320 * When we enter a new edge, we move it from the ET to the AET.
322 * From the AET, we can implement the even-odd rule as in
324 * The winding number rule is a little trickier. We also
325 * keep the EdgeTableEntries in the AET linked by the
326 * nextWETE (winding EdgeTableEntry) link. This allows
327 * the edges to be linked just as before for updating
328 * purposes, but only uses the edges linked by the nextWETE
329 * link as edges representing spans of the polygon to
330 * drawn (as with the even-odd rule).
334 * for the winding number rule
337 #define COUNTERCLOCKWISE -1
339 typedef struct _EdgeTableEntry
{
340 INT ymax
; /* ycoord at which we exit this edge. */
341 BRESINFO bres
; /* Bresenham info to run the edge */
342 struct _EdgeTableEntry
*next
; /* next in the list */
343 struct _EdgeTableEntry
*back
; /* for insertion sort */
344 struct _EdgeTableEntry
*nextWETE
; /* for winding num rule */
345 int ClockWise
; /* flag for winding number rule */
349 typedef struct _ScanLineList
{
350 INT scanline
; /* the scanline represented */
351 EdgeTableEntry
*edgelist
; /* header node */
352 struct _ScanLineList
*next
; /* next in the list */
357 INT ymax
; /* ymax for the polygon */
358 INT ymin
; /* ymin for the polygon */
359 ScanLineList scanlines
; /* header node */
364 * Here is a struct to help with storage allocation
365 * so we can allocate a big chunk at a time, and then take
366 * pieces from this heap when we need to.
368 #define SLLSPERBLOCK 25
370 typedef struct _ScanLineListBlock
{
371 ScanLineList SLLs
[SLLSPERBLOCK
];
372 struct _ScanLineListBlock
*next
;
378 * a few macros for the inner loops of the fill code where
379 * performance considerations don't allow a procedure call.
381 * Evaluate the given edge at the given scanline.
382 * If the edge has expired, then we leave it and fix up
383 * the active edge table; otherwise, we increment the
384 * x value to be ready for the next scanline.
385 * The winding number rule is in effect, so we must notify
386 * the caller when the edge has been removed so he
387 * can reorder the Winding Active Edge Table.
389 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
390 if (pAET->ymax == y) { /* leaving this edge */ \
391 pPrevAET->next = pAET->next; \
392 pAET = pPrevAET->next; \
395 pAET->back = pPrevAET; \
398 BRESINCRPGONSTRUCT(pAET->bres); \
406 * Evaluate the given edge at the given scanline.
407 * If the edge has expired, then we leave it and fix up
408 * the active edge table; otherwise, we increment the
409 * x value to be ready for the next scanline.
410 * The even-odd rule is in effect.
412 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
413 if (pAET->ymax == y) { /* leaving this edge */ \
414 pPrevAET->next = pAET->next; \
415 pAET = pPrevAET->next; \
417 pAET->back = pPrevAET; \
420 BRESINCRPGONSTRUCT(pAET->bres); \
426 /* Note the parameter order is different from the X11 equivalents */
428 static BOOL
REGION_CopyRegion(WINEREGION
*d
, WINEREGION
*s
);
429 static BOOL
REGION_OffsetRegion(WINEREGION
*d
, WINEREGION
*s
, INT x
, INT y
);
430 static BOOL
REGION_IntersectRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
431 static BOOL
REGION_UnionRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
432 static BOOL
REGION_SubtractRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
433 static BOOL
REGION_XorRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
434 static BOOL
REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
);
436 #define RGN_DEFAULT_RECTS 2
439 /***********************************************************************
442 static inline INT
get_region_type( const WINEREGION
*obj
)
444 switch(obj
->numRects
)
446 case 0: return NULLREGION
;
447 case 1: return SIMPLEREGION
;
448 default: return COMPLEXREGION
;
453 /***********************************************************************
455 * Outputs the contents of a WINEREGION
457 static void REGION_DumpRegion(WINEREGION
*pReg
)
459 RECT
*pRect
, *pRectEnd
= pReg
->rects
+ pReg
->numRects
;
461 TRACE("Region %p: %d,%d - %d,%d %d rects\n", pReg
,
462 pReg
->extents
.left
, pReg
->extents
.top
,
463 pReg
->extents
.right
, pReg
->extents
.bottom
, pReg
->numRects
);
464 for(pRect
= pReg
->rects
; pRect
< pRectEnd
; pRect
++)
465 TRACE("\t%d,%d - %d,%d\n", pRect
->left
, pRect
->top
,
466 pRect
->right
, pRect
->bottom
);
471 /***********************************************************************
474 * Initialize a new empty region.
476 static BOOL
init_region( WINEREGION
*pReg
, INT n
)
478 if (!(pReg
->rects
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof( RECT
)))) return FALSE
;
484 /***********************************************************************
487 static void destroy_region( WINEREGION
*pReg
)
489 HeapFree( GetProcessHeap(), 0, pReg
->rects
);
492 /***********************************************************************
493 * REGION_DeleteObject
495 static BOOL
REGION_DeleteObject( HGDIOBJ handle
)
497 WINEREGION
*rgn
= free_gdi_handle( handle
);
499 if (!rgn
) return FALSE
;
500 HeapFree( GetProcessHeap(), 0, rgn
->rects
);
501 HeapFree( GetProcessHeap(), 0, rgn
);
505 /***********************************************************************
506 * REGION_SelectObject
508 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, HDC hdc
)
510 return ULongToHandle(SelectClipRgn( hdc
, handle
));
514 /***********************************************************************
515 * REGION_OffsetRegion
516 * Offset a WINEREGION by x,y
518 static BOOL
REGION_OffsetRegion( WINEREGION
*rgn
, WINEREGION
*srcrgn
, INT x
, INT y
)
522 if (!REGION_CopyRegion( rgn
, srcrgn
)) return FALSE
;
525 int nbox
= rgn
->numRects
;
526 RECT
*pbox
= rgn
->rects
;
536 rgn
->extents
.left
+= x
;
537 rgn
->extents
.right
+= x
;
538 rgn
->extents
.top
+= y
;
539 rgn
->extents
.bottom
+= y
;
545 /***********************************************************************
546 * OffsetRgn (GDI32.@)
548 * Moves a region by the specified X- and Y-axis offsets.
551 * hrgn [I] Region to offset.
552 * x [I] Offset right if positive or left if negative.
553 * y [I] Offset down if positive or up if negative.
557 * NULLREGION - The new region is empty.
558 * SIMPLEREGION - The new region can be represented by one rectangle.
559 * COMPLEXREGION - The new region can only be represented by more than
563 INT WINAPI
OffsetRgn( HRGN hrgn
, INT x
, INT y
)
565 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
568 TRACE("%p %d,%d\n", hrgn
, x
, y
);
573 REGION_OffsetRegion( obj
, obj
, x
, y
);
575 ret
= get_region_type( obj
);
576 GDI_ReleaseObj( hrgn
);
581 /***********************************************************************
582 * GetRgnBox (GDI32.@)
584 * Retrieves the bounding rectangle of the region. The bounding rectangle
585 * is the smallest rectangle that contains the entire region.
588 * hrgn [I] Region to retrieve bounding rectangle from.
589 * rect [O] Rectangle that will receive the coordinates of the bounding
593 * NULLREGION - The new region is empty.
594 * SIMPLEREGION - The new region can be represented by one rectangle.
595 * COMPLEXREGION - The new region can only be represented by more than
598 INT WINAPI
GetRgnBox( HRGN hrgn
, LPRECT rect
)
600 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
604 rect
->left
= obj
->extents
.left
;
605 rect
->top
= obj
->extents
.top
;
606 rect
->right
= obj
->extents
.right
;
607 rect
->bottom
= obj
->extents
.bottom
;
608 TRACE("%p (%d,%d-%d,%d)\n", hrgn
,
609 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
610 ret
= get_region_type( obj
);
611 GDI_ReleaseObj(hrgn
);
618 /***********************************************************************
619 * CreateRectRgn (GDI32.@)
621 * Creates a simple rectangular region.
624 * left [I] Left coordinate of rectangle.
625 * top [I] Top coordinate of rectangle.
626 * right [I] Right coordinate of rectangle.
627 * bottom [I] Bottom coordinate of rectangle.
630 * Success: Handle to region.
633 HRGN WINAPI
CreateRectRgn(INT left
, INT top
, INT right
, INT bottom
)
638 if (!(obj
= HeapAlloc( GetProcessHeap(), 0, sizeof(*obj
) ))) return 0;
640 /* Allocate 2 rects by default to reduce the number of reallocs */
641 if (!init_region( obj
, RGN_DEFAULT_RECTS
))
643 HeapFree( GetProcessHeap(), 0, obj
);
646 if (!(hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
)))
648 HeapFree( GetProcessHeap(), 0, obj
->rects
);
649 HeapFree( GetProcessHeap(), 0, obj
);
652 TRACE( "%d,%d-%d,%d returning %p\n", left
, top
, right
, bottom
, hrgn
);
653 SetRectRgn(hrgn
, left
, top
, right
, bottom
);
658 /***********************************************************************
659 * CreateRectRgnIndirect (GDI32.@)
661 * Creates a simple rectangular region.
664 * rect [I] Coordinates of rectangular region.
667 * Success: Handle to region.
670 HRGN WINAPI
CreateRectRgnIndirect( const RECT
* rect
)
672 return CreateRectRgn( rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
676 /***********************************************************************
677 * SetRectRgn (GDI32.@)
679 * Sets a region to a simple rectangular region.
682 * hrgn [I] Region to convert.
683 * left [I] Left coordinate of rectangle.
684 * top [I] Top coordinate of rectangle.
685 * right [I] Right coordinate of rectangle.
686 * bottom [I] Bottom coordinate of rectangle.
693 * Allows either or both left and top to be greater than right or bottom.
695 BOOL WINAPI
SetRectRgn( HRGN hrgn
, INT left
, INT top
,
696 INT right
, INT bottom
)
700 TRACE("%p %d,%d-%d,%d\n", hrgn
, left
, top
, right
, bottom
);
702 if (!(obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
))) return FALSE
;
704 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
705 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
707 if((left
!= right
) && (top
!= bottom
))
709 obj
->rects
->left
= obj
->extents
.left
= left
;
710 obj
->rects
->top
= obj
->extents
.top
= top
;
711 obj
->rects
->right
= obj
->extents
.right
= right
;
712 obj
->rects
->bottom
= obj
->extents
.bottom
= bottom
;
718 GDI_ReleaseObj( hrgn
);
723 /***********************************************************************
724 * CreateRoundRectRgn (GDI32.@)
726 * Creates a rectangular region with rounded corners.
729 * left [I] Left coordinate of rectangle.
730 * top [I] Top coordinate of rectangle.
731 * right [I] Right coordinate of rectangle.
732 * bottom [I] Bottom coordinate of rectangle.
733 * ellipse_width [I] Width of the ellipse at each corner.
734 * ellipse_height [I] Height of the ellipse at each corner.
737 * Success: Handle to region.
741 * If ellipse_width or ellipse_height is less than 2 logical units then
742 * it is treated as though CreateRectRgn() was called instead.
744 HRGN WINAPI
CreateRoundRectRgn( INT left
, INT top
,
745 INT right
, INT bottom
,
746 INT ellipse_width
, INT ellipse_height
)
751 INT64 asq
, bsq
, dx
, dy
, err
;
754 /* Make the dimensions sensible */
756 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
757 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
758 /* the region is for the rectangle interior, but only at right and bottom for some reason */
762 ellipse_width
= min( right
- left
, abs( ellipse_width
));
763 ellipse_height
= min( bottom
- top
, abs( ellipse_height
));
765 /* Check if we can do a normal rectangle instead */
767 if ((ellipse_width
< 2) || (ellipse_height
< 2))
768 return CreateRectRgn( left
, top
, right
, bottom
);
770 if (!(obj
= HeapAlloc( GetProcessHeap(), 0, sizeof(*obj
) ))) return 0;
771 obj
->size
= ellipse_height
;
772 obj
->numRects
= ellipse_height
;
773 obj
->extents
.left
= left
;
774 obj
->extents
.top
= top
;
775 obj
->extents
.right
= right
;
776 obj
->extents
.bottom
= bottom
;
778 obj
->rects
= rects
= HeapAlloc( GetProcessHeap(), 0, obj
->size
* sizeof(RECT
) );
779 if (!rects
) goto done
;
781 /* based on an algorithm by Alois Zingl */
783 a
= ellipse_width
- 1;
784 b
= ellipse_height
- 1;
785 asq
= (INT64
)8 * a
* a
;
786 bsq
= (INT64
)8 * b
* b
;
787 dx
= (INT64
)4 * b
* b
* (1 - a
);
788 dy
= (INT64
)4 * a
* a
* (1 + (b
% 2));
789 err
= dx
+ dy
+ a
* a
* (b
% 2);
792 y
= ellipse_height
/ 2;
794 rects
[y
].left
= left
;
795 rects
[y
].right
= right
;
797 while (x
<= ellipse_width
/ 2)
809 rects
[y
].left
= left
+ x
;
810 rects
[y
].right
= right
- x
;
813 for (i
= 0; i
< ellipse_height
/ 2; i
++)
815 rects
[i
].left
= rects
[b
- i
].left
;
816 rects
[i
].right
= rects
[b
- i
].right
;
817 rects
[i
].top
= top
+ i
;
818 rects
[i
].bottom
= rects
[i
].top
+ 1;
820 for (; i
< ellipse_height
; i
++)
822 rects
[i
].top
= bottom
- ellipse_height
+ i
;
823 rects
[i
].bottom
= rects
[i
].top
+ 1;
825 rects
[ellipse_height
/ 2].top
= top
+ ellipse_height
/ 2; /* extend to top of rectangle */
827 hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
);
829 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
830 left
, top
, right
, bottom
, ellipse_width
, ellipse_height
, hrgn
);
834 HeapFree( GetProcessHeap(), 0, obj
->rects
);
835 HeapFree( GetProcessHeap(), 0, obj
);
841 /***********************************************************************
842 * CreateEllipticRgn (GDI32.@)
844 * Creates an elliptical region.
847 * left [I] Left coordinate of bounding rectangle.
848 * top [I] Top coordinate of bounding rectangle.
849 * right [I] Right coordinate of bounding rectangle.
850 * bottom [I] Bottom coordinate of bounding rectangle.
853 * Success: Handle to region.
857 * This is a special case of CreateRoundRectRgn() where the width of the
858 * ellipse at each corner is equal to the width the rectangle and
859 * the same for the height.
861 HRGN WINAPI
CreateEllipticRgn( INT left
, INT top
,
862 INT right
, INT bottom
)
864 return CreateRoundRectRgn( left
, top
, right
, bottom
,
865 right
-left
, bottom
-top
);
869 /***********************************************************************
870 * CreateEllipticRgnIndirect (GDI32.@)
872 * Creates an elliptical region.
875 * rect [I] Pointer to bounding rectangle of the ellipse.
878 * Success: Handle to region.
882 * This is a special case of CreateRoundRectRgn() where the width of the
883 * ellipse at each corner is equal to the width the rectangle and
884 * the same for the height.
886 HRGN WINAPI
CreateEllipticRgnIndirect( const RECT
*rect
)
888 return CreateRoundRectRgn( rect
->left
, rect
->top
, rect
->right
,
889 rect
->bottom
, rect
->right
- rect
->left
,
890 rect
->bottom
- rect
->top
);
893 /***********************************************************************
894 * GetRegionData (GDI32.@)
896 * Retrieves the data that specifies the region.
899 * hrgn [I] Region to retrieve the region data from.
900 * count [I] The size of the buffer pointed to by rgndata in bytes.
901 * rgndata [I] The buffer to receive data about the region.
904 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
905 * the number of bytes copied to the output buffer.
909 * The format of the Buffer member of RGNDATA is determined by the iType
910 * member of the region data header.
911 * Currently this is always RDH_RECTANGLES, which specifies that the format
912 * is the array of RECT's that specify the region. The length of the array
913 * is specified by the nCount member of the region data header.
915 DWORD WINAPI
GetRegionData(HRGN hrgn
, DWORD count
, LPRGNDATA rgndata
)
918 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
920 TRACE(" %p count = %d, rgndata = %p\n", hrgn
, count
, rgndata
);
924 size
= obj
->numRects
* sizeof(RECT
);
925 if(count
< (size
+ sizeof(RGNDATAHEADER
)) || rgndata
== NULL
)
927 GDI_ReleaseObj( hrgn
);
928 if (rgndata
) /* buffer is too small, signal it by return 0 */
930 else /* user requested buffer size with rgndata NULL */
931 return size
+ sizeof(RGNDATAHEADER
);
934 rgndata
->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
935 rgndata
->rdh
.iType
= RDH_RECTANGLES
;
936 rgndata
->rdh
.nCount
= obj
->numRects
;
937 rgndata
->rdh
.nRgnSize
= size
;
938 rgndata
->rdh
.rcBound
.left
= obj
->extents
.left
;
939 rgndata
->rdh
.rcBound
.top
= obj
->extents
.top
;
940 rgndata
->rdh
.rcBound
.right
= obj
->extents
.right
;
941 rgndata
->rdh
.rcBound
.bottom
= obj
->extents
.bottom
;
943 memcpy( rgndata
->Buffer
, obj
->rects
, size
);
945 GDI_ReleaseObj( hrgn
);
946 return size
+ sizeof(RGNDATAHEADER
);
950 static void translate( POINT
*pt
, UINT count
, const XFORM
*xform
)
956 pt
->x
= floor( x
* xform
->eM11
+ y
* xform
->eM21
+ xform
->eDx
+ 0.5 );
957 pt
->y
= floor( x
* xform
->eM12
+ y
* xform
->eM22
+ xform
->eDy
+ 0.5 );
963 /***********************************************************************
964 * ExtCreateRegion (GDI32.@)
966 * Creates a region as specified by the transformation data and region data.
969 * lpXform [I] World-space to logical-space transformation data.
970 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
971 * rgndata [I] Data that specifies the region.
974 * Success: Handle to region.
978 * See GetRegionData().
980 HRGN WINAPI
ExtCreateRegion( const XFORM
* lpXform
, DWORD dwCount
, const RGNDATA
* rgndata
)
987 SetLastError( ERROR_INVALID_PARAMETER
);
991 if (rgndata
->rdh
.dwSize
< sizeof(RGNDATAHEADER
))
994 /* XP doesn't care about the type */
995 if( rgndata
->rdh
.iType
!= RDH_RECTANGLES
)
996 WARN("(Unsupported region data type: %u)\n", rgndata
->rdh
.iType
);
1000 const RECT
*pCurRect
, *pEndRect
;
1002 hrgn
= CreateRectRgn( 0, 0, 0, 0 );
1004 pEndRect
= (const RECT
*)rgndata
->Buffer
+ rgndata
->rdh
.nCount
;
1005 for (pCurRect
= (const RECT
*)rgndata
->Buffer
; pCurRect
< pEndRect
; pCurRect
++)
1007 static const INT count
= 4;
1011 pt
[0].x
= pCurRect
->left
;
1012 pt
[0].y
= pCurRect
->top
;
1013 pt
[1].x
= pCurRect
->right
;
1014 pt
[1].y
= pCurRect
->top
;
1015 pt
[2].x
= pCurRect
->right
;
1016 pt
[2].y
= pCurRect
->bottom
;
1017 pt
[3].x
= pCurRect
->left
;
1018 pt
[3].y
= pCurRect
->bottom
;
1020 translate( pt
, 4, lpXform
);
1021 poly_hrgn
= CreatePolyPolygonRgn( pt
, &count
, 1, WINDING
);
1022 CombineRgn( hrgn
, hrgn
, poly_hrgn
, RGN_OR
);
1023 DeleteObject( poly_hrgn
);
1028 if (!(obj
= HeapAlloc( GetProcessHeap(), 0, sizeof(*obj
) ))) return 0;
1030 if (init_region( obj
, rgndata
->rdh
.nCount
))
1032 const RECT
*pCurRect
, *pEndRect
;
1034 pEndRect
= (const RECT
*)rgndata
->Buffer
+ rgndata
->rdh
.nCount
;
1035 for(pCurRect
= (const RECT
*)rgndata
->Buffer
; pCurRect
< pEndRect
; pCurRect
++)
1037 if (pCurRect
->left
< pCurRect
->right
&& pCurRect
->top
< pCurRect
->bottom
)
1039 if (!REGION_UnionRectWithRegion( pCurRect
, obj
)) goto done
;
1042 hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
);
1046 HeapFree( GetProcessHeap(), 0, obj
);
1053 HeapFree( GetProcessHeap(), 0, obj
->rects
);
1054 HeapFree( GetProcessHeap(), 0, obj
);
1056 TRACE("%p %d %p returning %p\n", lpXform
, dwCount
, rgndata
, hrgn
);
1061 /***********************************************************************
1062 * PtInRegion (GDI32.@)
1064 * Tests whether the specified point is inside a region.
1067 * hrgn [I] Region to test.
1068 * x [I] X-coordinate of point to test.
1069 * y [I] Y-coordinate of point to test.
1072 * Non-zero if the point is inside the region or zero otherwise.
1074 BOOL WINAPI
PtInRegion( HRGN hrgn
, INT x
, INT y
)
1079 if ((obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
)))
1083 if (obj
->numRects
> 0 && INRECT(obj
->extents
, x
, y
))
1084 for (i
= 0; i
< obj
->numRects
; i
++)
1085 if (INRECT (obj
->rects
[i
], x
, y
))
1090 GDI_ReleaseObj( hrgn
);
1096 /***********************************************************************
1097 * RectInRegion (GDI32.@)
1099 * Tests if a rectangle is at least partly inside the specified region.
1102 * hrgn [I] Region to test.
1103 * rect [I] Rectangle to test.
1106 * Non-zero if the rectangle is partially inside the region or
1109 BOOL WINAPI
RectInRegion( HRGN hrgn
, const RECT
*rect
)
1115 /* swap the coordinates to make right >= left and bottom >= top */
1116 /* (region building rectangles are normalized the same way) */
1117 if( rect
->top
> rect
->bottom
) {
1118 rc
.top
= rect
->bottom
;
1119 rc
.bottom
= rect
->top
;
1122 rc
.bottom
= rect
->bottom
;
1124 if( rect
->right
< rect
->left
) {
1125 rc
.right
= rect
->left
;
1126 rc
.left
= rect
->right
;
1128 rc
.right
= rect
->right
;
1129 rc
.left
= rect
->left
;
1132 if ((obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
)))
1134 RECT
*pCurRect
, *pRectEnd
;
1136 /* this is (just) a useful optimization */
1137 if ((obj
->numRects
> 0) && EXTENTCHECK(&obj
->extents
, &rc
))
1139 for (pCurRect
= obj
->rects
, pRectEnd
= pCurRect
+
1140 obj
->numRects
; pCurRect
< pRectEnd
; pCurRect
++)
1142 if (pCurRect
->bottom
<= rc
.top
)
1143 continue; /* not far enough down yet */
1145 if (pCurRect
->top
>= rc
.bottom
)
1146 break; /* too far down */
1148 if (pCurRect
->right
<= rc
.left
)
1149 continue; /* not far enough over yet */
1151 if (pCurRect
->left
>= rc
.right
) {
1159 GDI_ReleaseObj(hrgn
);
1164 /***********************************************************************
1165 * EqualRgn (GDI32.@)
1167 * Tests whether one region is identical to another.
1170 * hrgn1 [I] The first region to compare.
1171 * hrgn2 [I] The second region to compare.
1174 * Non-zero if both regions are identical or zero otherwise.
1176 BOOL WINAPI
EqualRgn( HRGN hrgn1
, HRGN hrgn2
)
1178 WINEREGION
*obj1
, *obj2
;
1181 if ((obj1
= GDI_GetObjPtr( hrgn1
, OBJ_REGION
)))
1183 if ((obj2
= GDI_GetObjPtr( hrgn2
, OBJ_REGION
)))
1187 if ( obj1
->numRects
!= obj2
->numRects
) goto done
;
1188 if ( obj1
->numRects
== 0 )
1194 if (obj1
->extents
.left
!= obj2
->extents
.left
) goto done
;
1195 if (obj1
->extents
.right
!= obj2
->extents
.right
) goto done
;
1196 if (obj1
->extents
.top
!= obj2
->extents
.top
) goto done
;
1197 if (obj1
->extents
.bottom
!= obj2
->extents
.bottom
) goto done
;
1198 for( i
= 0; i
< obj1
->numRects
; i
++ )
1200 if (obj1
->rects
[i
].left
!= obj2
->rects
[i
].left
) goto done
;
1201 if (obj1
->rects
[i
].right
!= obj2
->rects
[i
].right
) goto done
;
1202 if (obj1
->rects
[i
].top
!= obj2
->rects
[i
].top
) goto done
;
1203 if (obj1
->rects
[i
].bottom
!= obj2
->rects
[i
].bottom
) goto done
;
1207 GDI_ReleaseObj(hrgn2
);
1209 GDI_ReleaseObj(hrgn1
);
1214 /***********************************************************************
1215 * REGION_UnionRectWithRegion
1216 * Adds a rectangle to a WINEREGION
1218 static BOOL
REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
)
1222 region
.rects
= ®ion
.extents
;
1223 region
.numRects
= 1;
1225 region
.extents
= *rect
;
1226 return REGION_UnionRegion(rgn
, rgn
, ®ion
);
1230 BOOL
add_rect_to_region( HRGN rgn
, const RECT
*rect
)
1232 WINEREGION
*obj
= GDI_GetObjPtr( rgn
, OBJ_REGION
);
1235 if (!obj
) return FALSE
;
1236 ret
= REGION_UnionRectWithRegion( rect
, obj
);
1237 GDI_ReleaseObj( rgn
);
1241 /***********************************************************************
1242 * REGION_CreateFrameRgn
1244 * Create a region that is a frame around another region.
1245 * Compute the intersection of the region moved in all 4 directions
1246 * ( +x, -x, +y, -y) and subtract from the original.
1247 * The result looks slightly better than in Windows :)
1249 BOOL
REGION_FrameRgn( HRGN hDest
, HRGN hSrc
, INT x
, INT y
)
1253 WINEREGION
* destObj
= NULL
;
1254 WINEREGION
*srcObj
= GDI_GetObjPtr( hSrc
, OBJ_REGION
);
1256 tmprgn
.rects
= NULL
;
1257 if (!srcObj
) return FALSE
;
1258 if (srcObj
->numRects
!= 0)
1260 if (!(destObj
= GDI_GetObjPtr( hDest
, OBJ_REGION
))) goto done
;
1261 if (!init_region( &tmprgn
, srcObj
->numRects
)) goto done
;
1263 if (!REGION_OffsetRegion( destObj
, srcObj
, -x
, 0)) goto done
;
1264 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, x
, 0)) goto done
;
1265 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1266 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, 0, -y
)) goto done
;
1267 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1268 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, 0, y
)) goto done
;
1269 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1270 if (!REGION_SubtractRegion( destObj
, srcObj
, destObj
)) goto done
;
1274 HeapFree( GetProcessHeap(), 0, tmprgn
.rects
);
1275 if (destObj
) GDI_ReleaseObj ( hDest
);
1276 GDI_ReleaseObj( hSrc
);
1281 /***********************************************************************
1282 * CombineRgn (GDI32.@)
1284 * Combines two regions with the specified operation and stores the result
1285 * in the specified destination region.
1288 * hDest [I] The region that receives the combined result.
1289 * hSrc1 [I] The first source region.
1290 * hSrc2 [I] The second source region.
1291 * mode [I] The way in which the source regions will be combined. See notes.
1295 * NULLREGION - The new region is empty.
1296 * SIMPLEREGION - The new region can be represented by one rectangle.
1297 * COMPLEXREGION - The new region can only be represented by more than
1302 * The two source regions can be the same region.
1303 * The mode can be one of the following:
1304 *| RGN_AND - Intersection of the regions
1305 *| RGN_OR - Union of the regions
1306 *| RGN_XOR - Unions of the regions minus any intersection.
1307 *| RGN_DIFF - Difference (subtraction) of the regions.
1309 INT WINAPI
CombineRgn(HRGN hDest
, HRGN hSrc1
, HRGN hSrc2
, INT mode
)
1311 WINEREGION
*destObj
= GDI_GetObjPtr( hDest
, OBJ_REGION
);
1314 TRACE(" %p,%p -> %p mode=%x\n", hSrc1
, hSrc2
, hDest
, mode
);
1317 WINEREGION
*src1Obj
= GDI_GetObjPtr( hSrc1
, OBJ_REGION
);
1321 TRACE("dump src1Obj:\n");
1322 if(TRACE_ON(region
))
1323 REGION_DumpRegion(src1Obj
);
1324 if (mode
== RGN_COPY
)
1326 if (REGION_CopyRegion( destObj
, src1Obj
))
1327 result
= get_region_type( destObj
);
1331 WINEREGION
*src2Obj
= GDI_GetObjPtr( hSrc2
, OBJ_REGION
);
1335 TRACE("dump src2Obj:\n");
1336 if(TRACE_ON(region
))
1337 REGION_DumpRegion(src2Obj
);
1341 if (REGION_IntersectRegion( destObj
, src1Obj
, src2Obj
))
1342 result
= get_region_type( destObj
);
1345 if (REGION_UnionRegion( destObj
, src1Obj
, src2Obj
))
1346 result
= get_region_type( destObj
);
1349 if (REGION_XorRegion( destObj
, src1Obj
, src2Obj
))
1350 result
= get_region_type( destObj
);
1353 if (REGION_SubtractRegion( destObj
, src1Obj
, src2Obj
))
1354 result
= get_region_type( destObj
);
1357 GDI_ReleaseObj( hSrc2
);
1360 GDI_ReleaseObj( hSrc1
);
1362 TRACE("dump destObj:\n");
1363 if(TRACE_ON(region
))
1364 REGION_DumpRegion(destObj
);
1366 GDI_ReleaseObj( hDest
);
1371 /***********************************************************************
1373 * Re-calculate the extents of a region
1375 static void REGION_SetExtents (WINEREGION
*pReg
)
1377 RECT
*pRect
, *pRectEnd
, *pExtents
;
1379 if (pReg
->numRects
== 0)
1381 pReg
->extents
.left
= 0;
1382 pReg
->extents
.top
= 0;
1383 pReg
->extents
.right
= 0;
1384 pReg
->extents
.bottom
= 0;
1388 pExtents
= &pReg
->extents
;
1389 pRect
= pReg
->rects
;
1390 pRectEnd
= &pRect
[pReg
->numRects
- 1];
1393 * Since pRect is the first rectangle in the region, it must have the
1394 * smallest top and since pRectEnd is the last rectangle in the region,
1395 * it must have the largest bottom, because of banding. Initialize left and
1396 * right from pRect and pRectEnd, resp., as good things to initialize them
1399 pExtents
->left
= pRect
->left
;
1400 pExtents
->top
= pRect
->top
;
1401 pExtents
->right
= pRectEnd
->right
;
1402 pExtents
->bottom
= pRectEnd
->bottom
;
1404 while (pRect
<= pRectEnd
)
1406 if (pRect
->left
< pExtents
->left
)
1407 pExtents
->left
= pRect
->left
;
1408 if (pRect
->right
> pExtents
->right
)
1409 pExtents
->right
= pRect
->right
;
1414 /***********************************************************************
1417 static BOOL
REGION_CopyRegion(WINEREGION
*dst
, WINEREGION
*src
)
1419 if (dst
!= src
) /* don't want to copy to itself */
1421 if (dst
->size
< src
->numRects
)
1423 RECT
*rects
= HeapReAlloc( GetProcessHeap(), 0, dst
->rects
, src
->numRects
* sizeof(RECT
) );
1424 if (!rects
) return FALSE
;
1426 dst
->size
= src
->numRects
;
1428 dst
->numRects
= src
->numRects
;
1429 dst
->extents
.left
= src
->extents
.left
;
1430 dst
->extents
.top
= src
->extents
.top
;
1431 dst
->extents
.right
= src
->extents
.right
;
1432 dst
->extents
.bottom
= src
->extents
.bottom
;
1433 memcpy(dst
->rects
, src
->rects
, src
->numRects
* sizeof(RECT
));
1438 /***********************************************************************
1439 * REGION_MirrorRegion
1441 static BOOL
REGION_MirrorRegion( WINEREGION
*dst
, WINEREGION
*src
, int width
)
1445 RECT
*rects
= HeapAlloc( GetProcessHeap(), 0, src
->numRects
* sizeof(RECT
) );
1447 if (!rects
) return FALSE
;
1449 extents
.left
= width
- src
->extents
.right
;
1450 extents
.right
= width
- src
->extents
.left
;
1451 extents
.top
= src
->extents
.top
;
1452 extents
.bottom
= src
->extents
.bottom
;
1454 for (start
= 0; start
< src
->numRects
; start
= end
)
1456 /* find the end of the current band */
1457 for (end
= start
+ 1; end
< src
->numRects
; end
++)
1458 if (src
->rects
[end
].top
!= src
->rects
[end
- 1].top
) break;
1460 for (i
= 0; i
< end
- start
; i
++)
1462 rects
[start
+ i
].left
= width
- src
->rects
[end
- i
- 1].right
;
1463 rects
[start
+ i
].right
= width
- src
->rects
[end
- i
- 1].left
;
1464 rects
[start
+ i
].top
= src
->rects
[end
- i
- 1].top
;
1465 rects
[start
+ i
].bottom
= src
->rects
[end
- i
- 1].bottom
;
1469 HeapFree( GetProcessHeap(), 0, dst
->rects
);
1471 dst
->size
= src
->numRects
;
1472 dst
->numRects
= src
->numRects
;
1473 dst
->extents
= extents
;
1477 /***********************************************************************
1480 INT
mirror_region( HRGN dst
, HRGN src
, INT width
)
1482 WINEREGION
*src_rgn
, *dst_rgn
;
1485 if (!(src_rgn
= GDI_GetObjPtr( src
, OBJ_REGION
))) return ERROR
;
1486 if ((dst_rgn
= GDI_GetObjPtr( dst
, OBJ_REGION
)))
1488 if (REGION_MirrorRegion( dst_rgn
, src_rgn
, width
)) ret
= get_region_type( dst_rgn
);
1489 GDI_ReleaseObj( dst_rgn
);
1491 GDI_ReleaseObj( src_rgn
);
1495 /***********************************************************************
1496 * MirrorRgn (GDI32.@)
1498 BOOL WINAPI
MirrorRgn( HWND hwnd
, HRGN hrgn
)
1500 static const WCHAR user32W
[] = {'u','s','e','r','3','2','.','d','l','l',0};
1501 static BOOL (WINAPI
*pGetWindowRect
)( HWND hwnd
, LPRECT rect
);
1504 /* yes, a HWND in gdi32, don't ask */
1505 if (!pGetWindowRect
)
1507 HMODULE user32
= GetModuleHandleW(user32W
);
1508 if (!user32
) return FALSE
;
1509 if (!(pGetWindowRect
= (void *)GetProcAddress( user32
, "GetWindowRect" ))) return FALSE
;
1511 pGetWindowRect( hwnd
, &rect
);
1512 return mirror_region( hrgn
, hrgn
, rect
.right
- rect
.left
) != ERROR
;
1516 /***********************************************************************
1519 * Attempt to merge the rects in the current band with those in the
1520 * previous one. Used only by REGION_RegionOp.
1523 * The new index for the previous band.
1526 * If coalescing takes place:
1527 * - rectangles in the previous band will have their bottom fields
1529 * - pReg->numRects will be decreased.
1532 static INT
REGION_Coalesce (
1533 WINEREGION
*pReg
, /* Region to coalesce */
1534 INT prevStart
, /* Index of start of previous band */
1535 INT curStart
/* Index of start of current band */
1537 RECT
*pPrevRect
; /* Current rect in previous band */
1538 RECT
*pCurRect
; /* Current rect in current band */
1539 RECT
*pRegEnd
; /* End of region */
1540 INT curNumRects
; /* Number of rectangles in current band */
1541 INT prevNumRects
; /* Number of rectangles in previous band */
1542 INT bandtop
; /* top coordinate for current band */
1544 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
1546 pPrevRect
= &pReg
->rects
[prevStart
];
1547 prevNumRects
= curStart
- prevStart
;
1550 * Figure out how many rectangles are in the current band. Have to do
1551 * this because multiple bands could have been added in REGION_RegionOp
1552 * at the end when one region has been exhausted.
1554 pCurRect
= &pReg
->rects
[curStart
];
1555 bandtop
= pCurRect
->top
;
1556 for (curNumRects
= 0;
1557 (pCurRect
!= pRegEnd
) && (pCurRect
->top
== bandtop
);
1563 if (pCurRect
!= pRegEnd
)
1566 * If more than one band was added, we have to find the start
1567 * of the last band added so the next coalescing job can start
1568 * at the right place... (given when multiple bands are added,
1569 * this may be pointless -- see above).
1572 while (pRegEnd
[-1].top
== pRegEnd
->top
)
1576 curStart
= pRegEnd
- pReg
->rects
;
1577 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
1580 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0)) {
1581 pCurRect
-= curNumRects
;
1583 * The bands may only be coalesced if the bottom of the previous
1584 * matches the top scanline of the current.
1586 if (pPrevRect
->bottom
== pCurRect
->top
)
1589 * Make sure the bands have rects in the same places. This
1590 * assumes that rects have been added in such a way that they
1591 * cover the most area possible. I.e. two rects in a band must
1592 * have some horizontal space between them.
1596 if ((pPrevRect
->left
!= pCurRect
->left
) ||
1597 (pPrevRect
->right
!= pCurRect
->right
))
1600 * The bands don't line up so they can't be coalesced.
1607 } while (prevNumRects
!= 0);
1609 pReg
->numRects
-= curNumRects
;
1610 pCurRect
-= curNumRects
;
1611 pPrevRect
-= curNumRects
;
1614 * The bands may be merged, so set the bottom of each rect
1615 * in the previous band to that of the corresponding rect in
1620 pPrevRect
->bottom
= pCurRect
->bottom
;
1624 } while (curNumRects
!= 0);
1627 * If only one band was added to the region, we have to backup
1628 * curStart to the start of the previous band.
1630 * If more than one band was added to the region, copy the
1631 * other bands down. The assumption here is that the other bands
1632 * came from the same region as the current one and no further
1633 * coalescing can be done on them since it's all been done
1634 * already... curStart is already in the right place.
1636 if (pCurRect
== pRegEnd
)
1638 curStart
= prevStart
;
1644 *pPrevRect
++ = *pCurRect
++;
1645 } while (pCurRect
!= pRegEnd
);
1653 /***********************************************************************
1656 * Apply an operation to two regions. Called by REGION_Union,
1657 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1663 * The new region is overwritten.
1666 * The idea behind this function is to view the two regions as sets.
1667 * Together they cover a rectangle of area that this function divides
1668 * into horizontal bands where points are covered only by one region
1669 * or by both. For the first case, the nonOverlapFunc is called with
1670 * each the band and the band's upper and lower extents. For the
1671 * second, the overlapFunc is called to process the entire band. It
1672 * is responsible for clipping the rectangles in the band, though
1673 * this function provides the boundaries.
1674 * At the end of each band, the new region is coalesced, if possible,
1675 * to reduce the number of rectangles in the region.
1678 static BOOL
REGION_RegionOp(
1679 WINEREGION
*destReg
, /* Place to store result */
1680 WINEREGION
*reg1
, /* First region in operation */
1681 WINEREGION
*reg2
, /* 2nd region in operation */
1682 BOOL (*overlapFunc
)(WINEREGION
*, RECT
*, RECT
*, RECT
*, RECT
*, INT
, INT
), /* Function to call for over-lapping bands */
1683 BOOL (*nonOverlap1Func
)(WINEREGION
*, RECT
*, RECT
*, INT
, INT
), /* Function to call for non-overlapping bands in region 1 */
1684 BOOL (*nonOverlap2Func
)(WINEREGION
*, RECT
*, RECT
*, INT
, INT
) /* Function to call for non-overlapping bands in region 2 */
1687 RECT
*r1
; /* Pointer into first region */
1688 RECT
*r2
; /* Pointer into 2d region */
1689 RECT
*r1End
; /* End of 1st region */
1690 RECT
*r2End
; /* End of 2d region */
1691 INT ybot
; /* Bottom of intersection */
1692 INT ytop
; /* Top of intersection */
1693 INT prevBand
; /* Index of start of
1694 * previous band in newReg */
1695 INT curBand
; /* Index of start of current
1697 RECT
*r1BandEnd
; /* End of current band in r1 */
1698 RECT
*r2BandEnd
; /* End of current band in r2 */
1699 INT top
; /* Top of non-overlapping band */
1700 INT bot
; /* Bottom of non-overlapping band */
1704 * set r1, r2, r1End and r2End appropriately, preserve the important
1705 * parts of the destination region until the end in case it's one of
1706 * the two source regions, then mark the "new" region empty, allocating
1707 * another array of rectangles for it to use.
1711 r1End
= r1
+ reg1
->numRects
;
1712 r2End
= r2
+ reg2
->numRects
;
1715 * Allocate a reasonable number of rectangles for the new region. The idea
1716 * is to allocate enough so the individual functions don't need to
1717 * reallocate and copy the array, which is time consuming, yet we don't
1718 * have to worry about using too much memory. I hope to be able to
1719 * nuke the Xrealloc() at the end of this function eventually.
1721 if (!init_region( &newReg
, max(reg1
->numRects
,reg2
->numRects
) * 2 )) return FALSE
;
1724 * Initialize ybot and ytop.
1725 * In the upcoming loop, ybot and ytop serve different functions depending
1726 * on whether the band being handled is an overlapping or non-overlapping
1728 * In the case of a non-overlapping band (only one of the regions
1729 * has points in the band), ybot is the bottom of the most recent
1730 * intersection and thus clips the top of the rectangles in that band.
1731 * ytop is the top of the next intersection between the two regions and
1732 * serves to clip the bottom of the rectangles in the current band.
1733 * For an overlapping band (where the two regions intersect), ytop clips
1734 * the top of the rectangles of both regions and ybot clips the bottoms.
1736 if (reg1
->extents
.top
< reg2
->extents
.top
)
1737 ybot
= reg1
->extents
.top
;
1739 ybot
= reg2
->extents
.top
;
1742 * prevBand serves to mark the start of the previous band so rectangles
1743 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1744 * In the beginning, there is no previous band, so prevBand == curBand
1745 * (curBand is set later on, of course, but the first band will always
1746 * start at index 0). prevBand and curBand must be indices because of
1747 * the possible expansion, and resultant moving, of the new region's
1748 * array of rectangles.
1754 curBand
= newReg
.numRects
;
1757 * This algorithm proceeds one source-band (as opposed to a
1758 * destination band, which is determined by where the two regions
1759 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1760 * rectangle after the last one in the current band for their
1761 * respective regions.
1764 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->top
== r1
->top
))
1770 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->top
== r2
->top
))
1776 * First handle the band that doesn't intersect, if any.
1778 * Note that attention is restricted to one band in the
1779 * non-intersecting region at once, so if a region has n
1780 * bands between the current position and the next place it overlaps
1781 * the other, this entire loop will be passed through n times.
1783 if (r1
->top
< r2
->top
)
1785 top
= max(r1
->top
,ybot
);
1786 bot
= min(r1
->bottom
,r2
->top
);
1788 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1790 if (!nonOverlap1Func(&newReg
, r1
, r1BandEnd
, top
, bot
)) return FALSE
;
1795 else if (r2
->top
< r1
->top
)
1797 top
= max(r2
->top
,ybot
);
1798 bot
= min(r2
->bottom
,r1
->top
);
1800 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1802 if (!nonOverlap2Func(&newReg
, r2
, r2BandEnd
, top
, bot
)) return FALSE
;
1813 * If any rectangles got added to the region, try and coalesce them
1814 * with rectangles from the previous band. Note we could just do
1815 * this test in miCoalesce, but some machines incur a not
1816 * inconsiderable cost for function calls, so...
1818 if (newReg
.numRects
!= curBand
)
1820 prevBand
= REGION_Coalesce (&newReg
, prevBand
, curBand
);
1824 * Now see if we've hit an intersecting band. The two bands only
1825 * intersect if ybot > ytop
1827 ybot
= min(r1
->bottom
, r2
->bottom
);
1828 curBand
= newReg
.numRects
;
1831 if (!overlapFunc(&newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
)) return FALSE
;
1834 if (newReg
.numRects
!= curBand
)
1836 prevBand
= REGION_Coalesce (&newReg
, prevBand
, curBand
);
1840 * If we've finished with a band (bottom == ybot) we skip forward
1841 * in the region to the next band.
1843 if (r1
->bottom
== ybot
)
1847 if (r2
->bottom
== ybot
)
1851 } while ((r1
!= r1End
) && (r2
!= r2End
));
1854 * Deal with whichever region still has rectangles left.
1856 curBand
= newReg
.numRects
;
1859 if (nonOverlap1Func
!= NULL
)
1864 while ((r1BandEnd
< r1End
) && (r1BandEnd
->top
== r1
->top
))
1868 if (!nonOverlap1Func(&newReg
, r1
, r1BandEnd
, max(r1
->top
,ybot
), r1
->bottom
))
1871 } while (r1
!= r1End
);
1874 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1879 while ((r2BandEnd
< r2End
) && (r2BandEnd
->top
== r2
->top
))
1883 if (!nonOverlap2Func(&newReg
, r2
, r2BandEnd
, max(r2
->top
,ybot
), r2
->bottom
))
1886 } while (r2
!= r2End
);
1889 if (newReg
.numRects
!= curBand
)
1891 REGION_Coalesce (&newReg
, prevBand
, curBand
);
1895 * A bit of cleanup. To keep regions from growing without bound,
1896 * we shrink the array of rectangles to match the new number of
1897 * rectangles in the region. This never goes to 0, however...
1899 * Only do this stuff if the number of rectangles allocated is more than
1900 * twice the number of rectangles in the region (a simple optimization...).
1902 if ((newReg
.numRects
< (newReg
.size
>> 1)) && (newReg
.numRects
> 2))
1904 RECT
*new_rects
= HeapReAlloc( GetProcessHeap(), 0, newReg
.rects
, newReg
.numRects
* sizeof(RECT
) );
1907 newReg
.rects
= new_rects
;
1908 newReg
.size
= newReg
.numRects
;
1911 HeapFree( GetProcessHeap(), 0, destReg
->rects
);
1912 destReg
->rects
= newReg
.rects
;
1913 destReg
->size
= newReg
.size
;
1914 destReg
->numRects
= newReg
.numRects
;
1918 /***********************************************************************
1919 * Region Intersection
1920 ***********************************************************************/
1923 /***********************************************************************
1926 * Handle an overlapping band for REGION_Intersect.
1932 * Rectangles may be added to the region.
1935 static BOOL
REGION_IntersectO(WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
1936 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
1941 while ((r1
!= r1End
) && (r2
!= r2End
))
1943 left
= max(r1
->left
, r2
->left
);
1944 right
= min(r1
->right
, r2
->right
);
1947 * If there's any overlap between the two rectangles, add that
1948 * overlap to the new region.
1949 * There's no need to check for subsumption because the only way
1950 * such a need could arise is if some region has two rectangles
1951 * right next to each other. Since that should never happen...
1955 if (!add_rect( pReg
, left
, top
, right
, bottom
)) return FALSE
;
1959 * Need to advance the pointers. Shift the one that extends
1960 * to the right the least, since the other still has a chance to
1961 * overlap with that region's next rectangle, if you see what I mean.
1963 if (r1
->right
< r2
->right
)
1967 else if (r2
->right
< r1
->right
)
1980 /***********************************************************************
1981 * REGION_IntersectRegion
1983 static BOOL
REGION_IntersectRegion(WINEREGION
*newReg
, WINEREGION
*reg1
,
1986 /* check for trivial reject */
1987 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
1988 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
1989 newReg
->numRects
= 0;
1991 if (!REGION_RegionOp (newReg
, reg1
, reg2
, REGION_IntersectO
, NULL
, NULL
)) return FALSE
;
1994 * Can't alter newReg's extents before we call miRegionOp because
1995 * it might be one of the source regions and miRegionOp depends
1996 * on the extents of those regions being the same. Besides, this
1997 * way there's no checking against rectangles that will be nuked
1998 * due to coalescing, so we have to examine fewer rectangles.
2000 REGION_SetExtents(newReg
);
2004 /***********************************************************************
2006 ***********************************************************************/
2008 /***********************************************************************
2011 * Handle a non-overlapping band for the union operation. Just
2012 * Adds the rectangles into the region. Doesn't have to check for
2013 * subsumption or anything.
2019 * pReg->numRects is incremented and the final rectangles overwritten
2020 * with the rectangles we're passed.
2023 static BOOL
REGION_UnionNonO(WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
, INT top
, INT bottom
)
2027 if (!add_rect( pReg
, r
->left
, top
, r
->right
, bottom
)) return FALSE
;
2033 /***********************************************************************
2036 * Handle an overlapping band for the union operation. Picks the
2037 * left-most rectangle each time and merges it into the region.
2043 * Rectangles are overwritten in pReg->rects and pReg->numRects will
2047 static BOOL
REGION_UnionO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
2048 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
2050 #define MERGERECT(r) \
2051 if ((pReg->numRects != 0) && \
2052 (pReg->rects[pReg->numRects-1].top == top) && \
2053 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
2054 (pReg->rects[pReg->numRects-1].right >= r->left)) \
2056 if (pReg->rects[pReg->numRects-1].right < r->right) \
2057 pReg->rects[pReg->numRects-1].right = r->right; \
2061 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2065 while ((r1
!= r1End
) && (r2
!= r2End
))
2067 if (r1
->left
< r2
->left
)
2082 } while (r1
!= r1End
);
2084 else while (r2
!= r2End
)
2092 /***********************************************************************
2093 * REGION_UnionRegion
2095 static BOOL
REGION_UnionRegion(WINEREGION
*newReg
, WINEREGION
*reg1
, WINEREGION
*reg2
)
2099 /* checks all the simple cases */
2102 * Region 1 and 2 are the same or region 1 is empty
2104 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
2107 ret
= REGION_CopyRegion(newReg
, reg2
);
2112 * if nothing to union (region 2 empty)
2114 if (!(reg2
->numRects
))
2117 ret
= REGION_CopyRegion(newReg
, reg1
);
2122 * Region 1 completely subsumes region 2
2124 if ((reg1
->numRects
== 1) &&
2125 (reg1
->extents
.left
<= reg2
->extents
.left
) &&
2126 (reg1
->extents
.top
<= reg2
->extents
.top
) &&
2127 (reg1
->extents
.right
>= reg2
->extents
.right
) &&
2128 (reg1
->extents
.bottom
>= reg2
->extents
.bottom
))
2131 ret
= REGION_CopyRegion(newReg
, reg1
);
2136 * Region 2 completely subsumes region 1
2138 if ((reg2
->numRects
== 1) &&
2139 (reg2
->extents
.left
<= reg1
->extents
.left
) &&
2140 (reg2
->extents
.top
<= reg1
->extents
.top
) &&
2141 (reg2
->extents
.right
>= reg1
->extents
.right
) &&
2142 (reg2
->extents
.bottom
>= reg1
->extents
.bottom
))
2145 ret
= REGION_CopyRegion(newReg
, reg2
);
2149 if ((ret
= REGION_RegionOp (newReg
, reg1
, reg2
, REGION_UnionO
, REGION_UnionNonO
, REGION_UnionNonO
)))
2151 newReg
->extents
.left
= min(reg1
->extents
.left
, reg2
->extents
.left
);
2152 newReg
->extents
.top
= min(reg1
->extents
.top
, reg2
->extents
.top
);
2153 newReg
->extents
.right
= max(reg1
->extents
.right
, reg2
->extents
.right
);
2154 newReg
->extents
.bottom
= max(reg1
->extents
.bottom
, reg2
->extents
.bottom
);
2159 /***********************************************************************
2160 * Region Subtraction
2161 ***********************************************************************/
2163 /***********************************************************************
2164 * REGION_SubtractNonO1
2166 * Deal with non-overlapping band for subtraction. Any parts from
2167 * region 2 we discard. Anything from region 1 we add to the region.
2173 * pReg may be affected.
2176 static BOOL
REGION_SubtractNonO1 (WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
, INT top
, INT bottom
)
2180 if (!add_rect( pReg
, r
->left
, top
, r
->right
, bottom
)) return FALSE
;
2187 /***********************************************************************
2190 * Overlapping band subtraction. x1 is the left-most point not yet
2197 * pReg may have rectangles added to it.
2200 static BOOL
REGION_SubtractO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
2201 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
2203 INT left
= r1
->left
;
2205 while ((r1
!= r1End
) && (r2
!= r2End
))
2207 if (r2
->right
<= left
)
2210 * Subtrahend missed the boat: go to next subtrahend.
2214 else if (r2
->left
<= left
)
2217 * Subtrahend precedes minuend: nuke left edge of minuend.
2220 if (left
>= r1
->right
)
2223 * Minuend completely covered: advance to next minuend and
2224 * reset left fence to edge of new minuend.
2233 * Subtrahend now used up since it doesn't extend beyond
2239 else if (r2
->left
< r1
->right
)
2242 * Left part of subtrahend covers part of minuend: add uncovered
2243 * part of minuend to region and skip to next subtrahend.
2245 if (!add_rect( pReg
, left
, top
, r2
->left
, bottom
)) return FALSE
;
2247 if (left
>= r1
->right
)
2250 * Minuend used up: advance to new...
2259 * Subtrahend used up
2267 * Minuend used up: add any remaining piece before advancing.
2269 if (r1
->right
> left
)
2271 if (!add_rect( pReg
, left
, top
, r1
->right
, bottom
)) return FALSE
;
2280 * Add remaining minuend rectangles to region.
2284 if (!add_rect( pReg
, left
, top
, r1
->right
, bottom
)) return FALSE
;
2294 /***********************************************************************
2295 * REGION_SubtractRegion
2297 * Subtract regS from regM and leave the result in regD.
2298 * S stands for subtrahend, M for minuend and D for difference.
2304 * regD is overwritten.
2307 static BOOL
REGION_SubtractRegion(WINEREGION
*regD
, WINEREGION
*regM
, WINEREGION
*regS
)
2309 /* check for trivial reject */
2310 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
2311 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
2312 return REGION_CopyRegion(regD
, regM
);
2314 if (!REGION_RegionOp (regD
, regM
, regS
, REGION_SubtractO
, REGION_SubtractNonO1
, NULL
))
2318 * Can't alter newReg's extents before we call miRegionOp because
2319 * it might be one of the source regions and miRegionOp depends
2320 * on the extents of those regions being the unaltered. Besides, this
2321 * way there's no checking against rectangles that will be nuked
2322 * due to coalescing, so we have to examine fewer rectangles.
2324 REGION_SetExtents (regD
);
2328 /***********************************************************************
2331 static BOOL
REGION_XorRegion(WINEREGION
*dr
, WINEREGION
*sra
, WINEREGION
*srb
)
2333 WINEREGION tra
, trb
;
2336 if (!init_region( &tra
, sra
->numRects
+ 1 )) return FALSE
;
2337 if ((ret
= init_region( &trb
, srb
->numRects
+ 1 )))
2339 ret
= REGION_SubtractRegion(&tra
,sra
,srb
) &&
2340 REGION_SubtractRegion(&trb
,srb
,sra
) &&
2341 REGION_UnionRegion(dr
,&tra
,&trb
);
2342 destroy_region(&trb
);
2344 destroy_region(&tra
);
2348 /**************************************************************************
2352 *************************************************************************/
2354 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2355 #define SMALL_COORDINATE 0x80000000
2357 /***********************************************************************
2358 * REGION_InsertEdgeInET
2360 * Insert the given edge into the edge table.
2361 * First we must find the correct bucket in the
2362 * Edge table, then find the right slot in the
2363 * bucket. Finally, we can insert it.
2366 static void REGION_InsertEdgeInET(EdgeTable
*ET
, EdgeTableEntry
*ETE
,
2367 INT scanline
, ScanLineListBlock
**SLLBlock
, INT
*iSLLBlock
)
2370 EdgeTableEntry
*start
, *prev
;
2371 ScanLineList
*pSLL
, *pPrevSLL
;
2372 ScanLineListBlock
*tmpSLLBlock
;
2375 * find the right bucket to put the edge into
2377 pPrevSLL
= &ET
->scanlines
;
2378 pSLL
= pPrevSLL
->next
;
2379 while (pSLL
&& (pSLL
->scanline
< scanline
))
2386 * reassign pSLL (pointer to ScanLineList) if necessary
2388 if ((!pSLL
) || (pSLL
->scanline
> scanline
))
2390 if (*iSLLBlock
> SLLSPERBLOCK
-1)
2392 tmpSLLBlock
= HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock
));
2395 WARN("Can't alloc SLLB\n");
2398 (*SLLBlock
)->next
= tmpSLLBlock
;
2399 tmpSLLBlock
->next
= NULL
;
2400 *SLLBlock
= tmpSLLBlock
;
2403 pSLL
= &((*SLLBlock
)->SLLs
[(*iSLLBlock
)++]);
2405 pSLL
->next
= pPrevSLL
->next
;
2406 pSLL
->edgelist
= NULL
;
2407 pPrevSLL
->next
= pSLL
;
2409 pSLL
->scanline
= scanline
;
2412 * now insert the edge in the right bucket
2415 start
= pSLL
->edgelist
;
2416 while (start
&& (start
->bres
.minor_axis
< ETE
->bres
.minor_axis
))
2419 start
= start
->next
;
2426 pSLL
->edgelist
= ETE
;
2429 /***********************************************************************
2430 * REGION_CreateEdgeTable
2432 * This routine creates the edge table for
2433 * scan converting polygons.
2434 * The Edge Table (ET) looks like:
2438 * | ymax | ScanLineLists
2439 * |scanline|-->------------>-------------->...
2440 * -------- |scanline| |scanline|
2441 * |edgelist| |edgelist|
2442 * --------- ---------
2446 * list of ETEs list of ETEs
2448 * where ETE is an EdgeTableEntry data structure,
2449 * and there is one ScanLineList per scanline at
2450 * which an edge is initially entered.
2453 static void REGION_CreateETandAET(const INT
*Count
, INT nbpolygons
,
2454 const POINT
*pts
, EdgeTable
*ET
, EdgeTableEntry
*AET
,
2455 EdgeTableEntry
*pETEs
, ScanLineListBlock
*pSLLBlock
)
2457 const POINT
*top
, *bottom
;
2458 const POINT
*PrevPt
, *CurrPt
, *EndPt
;
2465 * initialize the Active Edge Table
2469 AET
->nextWETE
= NULL
;
2470 AET
->bres
.minor_axis
= SMALL_COORDINATE
;
2473 * initialize the Edge Table.
2475 ET
->scanlines
.next
= NULL
;
2476 ET
->ymax
= SMALL_COORDINATE
;
2477 ET
->ymin
= LARGE_COORDINATE
;
2478 pSLLBlock
->next
= NULL
;
2481 for(poly
= 0; poly
< nbpolygons
; poly
++)
2483 count
= Count
[poly
];
2491 * for each vertex in the array of points.
2492 * In this loop we are dealing with two vertices at
2493 * a time -- these make up one edge of the polygon.
2500 * find out which point is above and which is below.
2502 if (PrevPt
->y
> CurrPt
->y
)
2504 bottom
= PrevPt
, top
= CurrPt
;
2505 pETEs
->ClockWise
= 0;
2509 bottom
= CurrPt
, top
= PrevPt
;
2510 pETEs
->ClockWise
= 1;
2514 * don't add horizontal edges to the Edge table.
2516 if (bottom
->y
!= top
->y
)
2518 pETEs
->ymax
= bottom
->y
-1;
2519 /* -1 so we don't get last scanline */
2522 * initialize integer edge algorithm
2524 dy
= bottom
->y
- top
->y
;
2525 BRESINITPGONSTRUCT(dy
, top
->x
, bottom
->x
, pETEs
->bres
);
2527 REGION_InsertEdgeInET(ET
, pETEs
, top
->y
, &pSLLBlock
,
2530 if (PrevPt
->y
> ET
->ymax
)
2531 ET
->ymax
= PrevPt
->y
;
2532 if (PrevPt
->y
< ET
->ymin
)
2533 ET
->ymin
= PrevPt
->y
;
2542 /***********************************************************************
2545 * This routine moves EdgeTableEntries from the
2546 * EdgeTable into the Active Edge Table,
2547 * leaving them sorted by smaller x coordinate.
2550 static void REGION_loadAET(EdgeTableEntry
*AET
, EdgeTableEntry
*ETEs
)
2552 EdgeTableEntry
*pPrevAET
;
2553 EdgeTableEntry
*tmp
;
2559 while (AET
&& (AET
->bres
.minor_axis
< ETEs
->bres
.minor_axis
))
2568 ETEs
->back
= pPrevAET
;
2569 pPrevAET
->next
= ETEs
;
2576 /***********************************************************************
2577 * REGION_computeWAET
2579 * This routine links the AET by the
2580 * nextWETE (winding EdgeTableEntry) link for
2581 * use by the winding number rule. The final
2582 * Active Edge Table (AET) might look something
2586 * ---------- --------- ---------
2587 * |ymax | |ymax | |ymax |
2588 * | ... | |... | |... |
2589 * |next |->|next |->|next |->...
2590 * |nextWETE| |nextWETE| |nextWETE|
2591 * --------- --------- ^--------
2593 * V-------------------> V---> ...
2596 static void REGION_computeWAET(EdgeTableEntry
*AET
)
2598 EdgeTableEntry
*pWETE
;
2602 AET
->nextWETE
= NULL
;
2612 if ((!inside
&& !isInside
) ||
2613 ( inside
&& isInside
))
2615 pWETE
->nextWETE
= AET
;
2621 pWETE
->nextWETE
= NULL
;
2624 /***********************************************************************
2625 * REGION_InsertionSort
2627 * Just a simple insertion sort using
2628 * pointers and back pointers to sort the Active
2632 static BOOL
REGION_InsertionSort(EdgeTableEntry
*AET
)
2634 EdgeTableEntry
*pETEchase
;
2635 EdgeTableEntry
*pETEinsert
;
2636 EdgeTableEntry
*pETEchaseBackTMP
;
2637 BOOL changed
= FALSE
;
2644 while (pETEchase
->back
->bres
.minor_axis
> AET
->bres
.minor_axis
)
2645 pETEchase
= pETEchase
->back
;
2648 if (pETEchase
!= pETEinsert
)
2650 pETEchaseBackTMP
= pETEchase
->back
;
2651 pETEinsert
->back
->next
= AET
;
2653 AET
->back
= pETEinsert
->back
;
2654 pETEinsert
->next
= pETEchase
;
2655 pETEchase
->back
->next
= pETEinsert
;
2656 pETEchase
->back
= pETEinsert
;
2657 pETEinsert
->back
= pETEchaseBackTMP
;
2664 /***********************************************************************
2665 * REGION_FreeStorage
2669 static void REGION_FreeStorage(ScanLineListBlock
*pSLLBlock
)
2671 ScanLineListBlock
*tmpSLLBlock
;
2675 tmpSLLBlock
= pSLLBlock
->next
;
2676 HeapFree( GetProcessHeap(), 0, pSLLBlock
);
2677 pSLLBlock
= tmpSLLBlock
;
2682 /***********************************************************************
2683 * REGION_PtsToRegion
2685 * Create an array of rectangles from a list of points.
2687 static BOOL
REGION_PtsToRegion(int numFullPtBlocks
, int iCurPtBlock
,
2688 POINTBLOCK
*FirstPtBlock
, WINEREGION
*reg
)
2692 POINTBLOCK
*CurPtBlock
;
2697 extents
= ®
->extents
;
2699 numRects
= ((numFullPtBlocks
* NUMPTSTOBUFFER
) + iCurPtBlock
) >> 1;
2700 if (!init_region( reg
, numRects
)) return FALSE
;
2702 reg
->size
= numRects
;
2703 CurPtBlock
= FirstPtBlock
;
2704 rects
= reg
->rects
- 1;
2706 extents
->left
= LARGE_COORDINATE
, extents
->right
= SMALL_COORDINATE
;
2708 for ( ; numFullPtBlocks
>= 0; numFullPtBlocks
--) {
2709 /* the loop uses 2 points per iteration */
2710 i
= NUMPTSTOBUFFER
>> 1;
2711 if (!numFullPtBlocks
)
2712 i
= iCurPtBlock
>> 1;
2713 for (pts
= CurPtBlock
->pts
; i
--; pts
+= 2) {
2714 if (pts
->x
== pts
[1].x
)
2716 if (numRects
&& pts
->x
== rects
->left
&& pts
->y
== rects
->bottom
&&
2717 pts
[1].x
== rects
->right
&&
2718 (numRects
== 1 || rects
[-1].top
!= rects
->top
) &&
2719 (i
&& pts
[2].y
> pts
[1].y
)) {
2720 rects
->bottom
= pts
[1].y
+ 1;
2725 rects
->left
= pts
->x
; rects
->top
= pts
->y
;
2726 rects
->right
= pts
[1].x
; rects
->bottom
= pts
[1].y
+ 1;
2727 if (rects
->left
< extents
->left
)
2728 extents
->left
= rects
->left
;
2729 if (rects
->right
> extents
->right
)
2730 extents
->right
= rects
->right
;
2732 CurPtBlock
= CurPtBlock
->next
;
2736 extents
->top
= reg
->rects
->top
;
2737 extents
->bottom
= rects
->bottom
;
2742 extents
->bottom
= 0;
2744 reg
->numRects
= numRects
;
2749 /***********************************************************************
2750 * CreatePolyPolygonRgn (GDI32.@)
2752 HRGN WINAPI
CreatePolyPolygonRgn(const POINT
*Pts
, const INT
*Count
,
2753 INT nbpolygons
, INT mode
)
2757 EdgeTableEntry
*pAET
; /* Active Edge Table */
2758 INT y
; /* current scanline */
2759 int iPts
= 0; /* number of pts in buffer */
2760 EdgeTableEntry
*pWETE
; /* Winding Edge Table Entry*/
2761 ScanLineList
*pSLL
; /* current scanLineList */
2762 POINT
*pts
; /* output buffer */
2763 EdgeTableEntry
*pPrevAET
; /* ptr to previous AET */
2764 EdgeTable ET
; /* header node for ET */
2765 EdgeTableEntry AET
; /* header node for AET */
2766 EdgeTableEntry
*pETEs
; /* EdgeTableEntries pool */
2767 ScanLineListBlock SLLBlock
; /* header for scanlinelist */
2768 int fixWAET
= FALSE
;
2769 POINTBLOCK FirstPtBlock
, *curPtBlock
; /* PtBlock buffers */
2770 POINTBLOCK
*tmpPtBlock
;
2771 int numFullPtBlocks
= 0;
2774 TRACE("%p, count %d, polygons %d, mode %d\n", Pts
, *Count
, nbpolygons
, mode
);
2776 /* special case a rectangle */
2778 if (((nbpolygons
== 1) && ((*Count
== 4) ||
2779 ((*Count
== 5) && (Pts
[4].x
== Pts
[0].x
) && (Pts
[4].y
== Pts
[0].y
)))) &&
2780 (((Pts
[0].y
== Pts
[1].y
) &&
2781 (Pts
[1].x
== Pts
[2].x
) &&
2782 (Pts
[2].y
== Pts
[3].y
) &&
2783 (Pts
[3].x
== Pts
[0].x
)) ||
2784 ((Pts
[0].x
== Pts
[1].x
) &&
2785 (Pts
[1].y
== Pts
[2].y
) &&
2786 (Pts
[2].x
== Pts
[3].x
) &&
2787 (Pts
[3].y
== Pts
[0].y
))))
2788 return CreateRectRgn( min(Pts
[0].x
, Pts
[2].x
), min(Pts
[0].y
, Pts
[2].y
),
2789 max(Pts
[0].x
, Pts
[2].x
), max(Pts
[0].y
, Pts
[2].y
) );
2791 for(poly
= total
= 0; poly
< nbpolygons
; poly
++)
2792 total
+= Count
[poly
];
2793 if (! (pETEs
= HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry
) * total
)))
2796 pts
= FirstPtBlock
.pts
;
2797 REGION_CreateETandAET(Count
, nbpolygons
, Pts
, &ET
, &AET
, pETEs
, &SLLBlock
);
2798 pSLL
= ET
.scanlines
.next
;
2799 curPtBlock
= &FirstPtBlock
;
2801 if (mode
!= WINDING
) {
2805 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2807 * Add a new edge to the active edge table when we
2808 * get to the next edge.
2810 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2811 REGION_loadAET(&AET
, pSLL
->edgelist
);
2818 * for each active edge
2821 pts
->x
= pAET
->bres
.minor_axis
, pts
->y
= y
;
2825 * send out the buffer
2827 if (iPts
== NUMPTSTOBUFFER
) {
2828 tmpPtBlock
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK
));
2829 if(!tmpPtBlock
) goto done
;
2830 curPtBlock
->next
= tmpPtBlock
;
2831 curPtBlock
= tmpPtBlock
;
2832 pts
= curPtBlock
->pts
;
2836 EVALUATEEDGEEVENODD(pAET
, pPrevAET
, y
);
2838 REGION_InsertionSort(&AET
);
2845 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2847 * Add a new edge to the active edge table when we
2848 * get to the next edge.
2850 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2851 REGION_loadAET(&AET
, pSLL
->edgelist
);
2852 REGION_computeWAET(&AET
);
2860 * for each active edge
2864 * add to the buffer only those edges that
2865 * are in the Winding active edge table.
2867 if (pWETE
== pAET
) {
2868 pts
->x
= pAET
->bres
.minor_axis
, pts
->y
= y
;
2872 * send out the buffer
2874 if (iPts
== NUMPTSTOBUFFER
) {
2875 tmpPtBlock
= HeapAlloc( GetProcessHeap(), 0,
2876 sizeof(POINTBLOCK
) );
2877 if(!tmpPtBlock
) goto done
;
2878 curPtBlock
->next
= tmpPtBlock
;
2879 curPtBlock
= tmpPtBlock
;
2880 pts
= curPtBlock
->pts
;
2884 pWETE
= pWETE
->nextWETE
;
2886 EVALUATEEDGEWINDING(pAET
, pPrevAET
, y
, fixWAET
);
2890 * recompute the winding active edge table if
2891 * we just resorted or have exited an edge.
2893 if (REGION_InsertionSort(&AET
) || fixWAET
) {
2894 REGION_computeWAET(&AET
);
2900 if (!(obj
= HeapAlloc( GetProcessHeap(), 0, sizeof(*obj
) ))) goto done
;
2902 if (!REGION_PtsToRegion(numFullPtBlocks
, iPts
, &FirstPtBlock
, obj
))
2904 HeapFree( GetProcessHeap(), 0, obj
);
2907 if (!(hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
)))
2909 HeapFree( GetProcessHeap(), 0, obj
->rects
);
2910 HeapFree( GetProcessHeap(), 0, obj
);
2914 REGION_FreeStorage(SLLBlock
.next
);
2915 for (curPtBlock
= FirstPtBlock
.next
; --numFullPtBlocks
>= 0;) {
2916 tmpPtBlock
= curPtBlock
->next
;
2917 HeapFree( GetProcessHeap(), 0, curPtBlock
);
2918 curPtBlock
= tmpPtBlock
;
2920 HeapFree( GetProcessHeap(), 0, pETEs
);
2925 /***********************************************************************
2926 * CreatePolygonRgn (GDI32.@)
2928 HRGN WINAPI
CreatePolygonRgn( const POINT
*points
, INT count
,
2931 return CreatePolyPolygonRgn( points
, &count
, 1, mode
);