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...
104 #include "gdi_private.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(region
);
110 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, HDC hdc
);
111 static BOOL
REGION_DeleteObject( HGDIOBJ handle
);
113 static const struct gdi_obj_funcs region_funcs
=
115 REGION_SelectObject
, /* pSelectObject */
116 NULL
, /* pGetObjectA */
117 NULL
, /* pGetObjectW */
118 NULL
, /* pUnrealizeObject */
119 REGION_DeleteObject
/* pDeleteObject */
122 /* Check if two RECTs overlap. */
123 static inline BOOL
overlapping( const RECT
*r1
, const RECT
*r2
)
125 return (r1
->right
> r2
->left
&& r1
->left
< r2
->right
&&
126 r1
->bottom
> r2
->top
&& r1
->top
< r2
->bottom
);
129 static BOOL
grow_region( WINEREGION
*rgn
, int size
)
133 if (size
<= rgn
->size
) return TRUE
;
135 if (rgn
->rects
== rgn
->rects_buf
)
137 new_rects
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(RECT
) );
138 if (!new_rects
) return FALSE
;
139 memcpy( new_rects
, rgn
->rects
, rgn
->numRects
* sizeof(RECT
) );
143 new_rects
= HeapReAlloc( GetProcessHeap(), 0, rgn
->rects
, size
* sizeof(RECT
) );
144 if (!new_rects
) return FALSE
;
146 rgn
->rects
= new_rects
;
151 static BOOL
add_rect( WINEREGION
*reg
, INT left
, INT top
, INT right
, INT bottom
)
154 if (reg
->numRects
>= reg
->size
&& !grow_region( reg
, 2 * reg
->size
))
157 rect
= reg
->rects
+ reg
->numRects
++;
161 rect
->bottom
= bottom
;
165 static inline void empty_region( WINEREGION
*reg
)
168 reg
->extents
.left
= reg
->extents
.top
= reg
->extents
.right
= reg
->extents
.bottom
= 0;
171 static inline BOOL
is_in_rect( const RECT
*rect
, int x
, int y
)
173 return (rect
->right
> x
&& rect
->left
<= x
&& rect
->bottom
> y
&& rect
->top
<= y
);
178 * This file contains a few macros to help track
179 * the edge of a filled object. The object is assumed
180 * to be filled in scanline order, and thus the
181 * algorithm used is an extension of Bresenham's line
182 * drawing algorithm which assumes that y is always the
184 * Since these pieces of code are the same for any filled shape,
185 * it is more convenient to gather the library in one
186 * place, but since these pieces of code are also in
187 * the inner loops of output primitives, procedure call
188 * overhead is out of the question.
189 * See the author for a derivation if needed.
194 * This structure contains all of the information needed
195 * to run the bresenham algorithm.
196 * The variables may be hardcoded into the declarations
197 * instead of using this structure to make use of
198 * register declarations.
202 INT minor_axis
; /* minor axis */
203 INT d
; /* decision variable */
204 INT m
, m1
; /* slope and slope+1 */
205 INT incr1
, incr2
; /* error increments */
210 * In scan converting polygons, we want to choose those pixels
211 * which are inside the polygon. Thus, we add .5 to the starting
212 * x coordinate for both left and right edges. Now we choose the
213 * first pixel which is inside the pgon for the left edge and the
214 * first pixel which is outside the pgon for the right edge.
215 * Draw the left pixel, but not the right.
217 * How to add .5 to the starting x coordinate:
218 * If the edge is moving to the right, then subtract dy from the
219 * error term from the general form of the algorithm.
220 * If the edge is moving to the left, then add dy to the error term.
222 * The reason for the difference between edges moving to the left
223 * and edges moving to the right is simple: If an edge is moving
224 * to the right, then we want the algorithm to flip immediately.
225 * If it is moving to the left, then we don't want it to flip until
226 * we traverse an entire pixel.
228 static inline void bres_init_polygon( int dy
, int x1
, int x2
, struct bres_info
*bres
)
233 * if the edge is horizontal, then it is ignored
234 * and assumed not to be processed. Otherwise, do this stuff.
238 bres
->minor_axis
= x1
;
243 bres
->m1
= bres
->m
- 1;
244 bres
->incr1
= -2 * dx
+ 2 * dy
* bres
->m1
;
245 bres
->incr2
= -2 * dx
+ 2 * dy
* bres
->m
;
246 bres
->d
= 2 * bres
->m
* dy
- 2 * dx
- 2 * dy
;
251 bres
->m1
= bres
->m
+ 1;
252 bres
->incr1
= 2 * dx
- 2 * dy
* bres
->m1
;
253 bres
->incr2
= 2 * dx
- 2 * dy
* bres
->m
;
254 bres
->d
= -2 * bres
->m
* dy
+ 2 * dx
;
258 static inline void bres_incr_polygon( struct bres_info
*bres
)
262 bres
->minor_axis
+= bres
->m1
;
263 bres
->d
+= bres
->incr1
;
266 bres
->minor_axis
+= bres
->m
;
267 bres
->d
+= bres
->incr2
;
271 bres
->minor_axis
+= bres
->m1
;
272 bres
->d
+= bres
->incr1
;
275 bres
->minor_axis
+= bres
->m
;
276 bres
->d
+= bres
->incr2
;
283 * These are the data structures needed to scan
284 * convert regions. Two different scan conversion
285 * methods are available -- the even-odd method, and
286 * the winding number method.
287 * The even-odd rule states that a point is inside
288 * the polygon if a ray drawn from that point in any
289 * direction will pass through an odd number of
291 * By the winding number rule, a point is decided
292 * to be inside the polygon if a ray drawn from that
293 * point in any direction passes through a different
294 * number of clockwise and counter-clockwise path
297 * These data structures are adapted somewhat from
298 * the algorithm in (Foley/Van Dam) for scan converting
300 * The basic algorithm is to start at the top (smallest y)
301 * of the polygon, stepping down to the bottom of
302 * the polygon by incrementing the y coordinate. We
303 * keep a list of edges which the current scanline crosses,
304 * sorted by x. This list is called the Active Edge Table (AET)
305 * As we change the y-coordinate, we update each entry in
306 * in the active edge table to reflect the edges new xcoord.
307 * This list must be sorted at each scanline in case
308 * two edges intersect.
309 * We also keep a data structure known as the Edge Table (ET),
310 * which keeps track of all the edges which the current
311 * scanline has not yet reached. The ET is basically a
312 * list of ScanLineList structures containing a list of
313 * edges which are entered at a given scanline. There is one
314 * ScanLineList per scanline at which an edge is entered.
315 * When we enter a new edge, we move it from the ET to the AET.
317 * From the AET, we can implement the even-odd rule as in
319 * The winding number rule is a little trickier. We also
320 * keep the EdgeTableEntries in the AET linked by the
321 * nextWETE (winding EdgeTableEntry) link. This allows
322 * the edges to be linked just as before for updating
323 * purposes, but only uses the edges linked by the nextWETE
324 * link as edges representing spans of the polygon to
325 * drawn (as with the even-odd rule).
328 typedef struct edge_table_entry
{
330 struct list winding_entry
;
331 INT ymax
; /* ycoord at which we exit this edge. */
332 struct bres_info bres
; /* Bresenham info to run the edge */
333 int ClockWise
; /* flag for winding number rule */
337 typedef struct _ScanLineList
{
338 struct list edgelist
;
339 INT scanline
; /* the scanline represented */
340 struct _ScanLineList
*next
; /* next in the list */
345 INT ymax
; /* ymax for the polygon */
346 INT ymin
; /* ymin for the polygon */
347 ScanLineList scanlines
; /* header node */
352 * Here is a struct to help with storage allocation
353 * so we can allocate a big chunk at a time, and then take
354 * pieces from this heap when we need to.
356 #define SLLSPERBLOCK 25
358 typedef struct _ScanLineListBlock
{
359 ScanLineList SLLs
[SLLSPERBLOCK
];
360 struct _ScanLineListBlock
*next
;
364 /* Note the parameter order is different from the X11 equivalents */
366 static BOOL
REGION_CopyRegion(WINEREGION
*d
, WINEREGION
*s
);
367 static BOOL
REGION_OffsetRegion(WINEREGION
*d
, WINEREGION
*s
, INT x
, INT y
);
368 static BOOL
REGION_IntersectRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
369 static BOOL
REGION_UnionRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
370 static BOOL
REGION_SubtractRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
371 static BOOL
REGION_XorRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
372 static BOOL
REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
);
374 /***********************************************************************
377 static inline INT
get_region_type( const WINEREGION
*obj
)
379 switch(obj
->numRects
)
381 case 0: return NULLREGION
;
382 case 1: return SIMPLEREGION
;
383 default: return COMPLEXREGION
;
388 /***********************************************************************
390 * Outputs the contents of a WINEREGION
392 static void REGION_DumpRegion(WINEREGION
*pReg
)
394 RECT
*pRect
, *pRectEnd
= pReg
->rects
+ pReg
->numRects
;
396 TRACE("Region %p: %s %d rects\n", pReg
, wine_dbgstr_rect(&pReg
->extents
), pReg
->numRects
);
397 for(pRect
= pReg
->rects
; pRect
< pRectEnd
; pRect
++)
398 TRACE("\t%s\n", wine_dbgstr_rect(pRect
));
403 /***********************************************************************
406 * Initialize a new empty region.
408 static BOOL
init_region( WINEREGION
*pReg
, INT n
)
410 n
= max( n
, RGN_DEFAULT_RECTS
);
412 if (n
> RGN_DEFAULT_RECTS
)
414 if (n
> INT_MAX
/ sizeof(RECT
)) return FALSE
;
415 if (!(pReg
->rects
= HeapAlloc( GetProcessHeap(), 0, n
* sizeof( RECT
) )))
419 pReg
->rects
= pReg
->rects_buf
;
426 /***********************************************************************
429 static void destroy_region( WINEREGION
*pReg
)
431 if (pReg
->rects
!= pReg
->rects_buf
)
432 HeapFree( GetProcessHeap(), 0, pReg
->rects
);
435 /***********************************************************************
438 static void free_region( WINEREGION
*rgn
)
440 destroy_region( rgn
);
441 HeapFree( GetProcessHeap(), 0, rgn
);
444 /***********************************************************************
447 static WINEREGION
*alloc_region( INT n
)
449 WINEREGION
*rgn
= HeapAlloc( GetProcessHeap(), 0, sizeof(*rgn
) );
451 if (rgn
&& !init_region( rgn
, n
))
459 /************************************************************
462 * Move rectangles from src to dst leaving src with no rectangles.
464 static inline void move_rects( WINEREGION
*dst
, WINEREGION
*src
)
466 destroy_region( dst
);
467 if (src
->rects
== src
->rects_buf
)
469 dst
->rects
= dst
->rects_buf
;
470 memcpy( dst
->rects
, src
->rects
, src
->numRects
* sizeof(RECT
) );
473 dst
->rects
= src
->rects
;
474 dst
->size
= src
->size
;
475 dst
->numRects
= src
->numRects
;
476 init_region( src
, 0 );
479 /***********************************************************************
480 * REGION_DeleteObject
482 static BOOL
REGION_DeleteObject( HGDIOBJ handle
)
484 WINEREGION
*rgn
= free_gdi_handle( handle
);
486 if (!rgn
) return FALSE
;
491 /***********************************************************************
492 * REGION_SelectObject
494 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, HDC hdc
)
496 return ULongToHandle(SelectClipRgn( hdc
, handle
));
500 /***********************************************************************
501 * REGION_OffsetRegion
502 * Offset a WINEREGION by x,y
504 static BOOL
REGION_OffsetRegion( WINEREGION
*rgn
, WINEREGION
*srcrgn
, INT x
, INT y
)
508 if (!REGION_CopyRegion( rgn
, srcrgn
)) return FALSE
;
511 int nbox
= rgn
->numRects
;
512 RECT
*pbox
= rgn
->rects
;
522 rgn
->extents
.left
+= x
;
523 rgn
->extents
.right
+= x
;
524 rgn
->extents
.top
+= y
;
525 rgn
->extents
.bottom
+= y
;
531 /***********************************************************************
532 * OffsetRgn (GDI32.@)
534 * Moves a region by the specified X- and Y-axis offsets.
537 * hrgn [I] Region to offset.
538 * x [I] Offset right if positive or left if negative.
539 * y [I] Offset down if positive or up if negative.
543 * NULLREGION - The new region is empty.
544 * SIMPLEREGION - The new region can be represented by one rectangle.
545 * COMPLEXREGION - The new region can only be represented by more than
549 INT WINAPI
OffsetRgn( HRGN hrgn
, INT x
, INT y
)
551 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
554 TRACE("%p %d,%d\n", hrgn
, x
, y
);
559 REGION_OffsetRegion( obj
, obj
, x
, y
);
561 ret
= get_region_type( obj
);
562 GDI_ReleaseObj( hrgn
);
567 /***********************************************************************
568 * GetRgnBox (GDI32.@)
570 * Retrieves the bounding rectangle of the region. The bounding rectangle
571 * is the smallest rectangle that contains the entire region.
574 * hrgn [I] Region to retrieve bounding rectangle from.
575 * rect [O] Rectangle that will receive the coordinates of the bounding
579 * NULLREGION - The new region is empty.
580 * SIMPLEREGION - The new region can be represented by one rectangle.
581 * COMPLEXREGION - The new region can only be represented by more than
584 INT WINAPI
GetRgnBox( HRGN hrgn
, LPRECT rect
)
586 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
590 rect
->left
= obj
->extents
.left
;
591 rect
->top
= obj
->extents
.top
;
592 rect
->right
= obj
->extents
.right
;
593 rect
->bottom
= obj
->extents
.bottom
;
594 TRACE("%p %s\n", hrgn
, wine_dbgstr_rect(rect
));
595 ret
= get_region_type( obj
);
596 GDI_ReleaseObj(hrgn
);
603 /***********************************************************************
604 * CreateRectRgn (GDI32.@)
606 * Creates a simple rectangular region.
609 * left [I] Left coordinate of rectangle.
610 * top [I] Top coordinate of rectangle.
611 * right [I] Right coordinate of rectangle.
612 * bottom [I] Bottom coordinate of rectangle.
615 * Success: Handle to region.
618 HRGN WINAPI
CreateRectRgn(INT left
, INT top
, INT right
, INT bottom
)
623 if (!(obj
= alloc_region( RGN_DEFAULT_RECTS
))) return 0;
625 if (!(hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
)))
630 TRACE( "%d,%d-%d,%d returning %p\n", left
, top
, right
, bottom
, hrgn
);
631 SetRectRgn(hrgn
, left
, top
, right
, bottom
);
636 /***********************************************************************
637 * CreateRectRgnIndirect (GDI32.@)
639 * Creates a simple rectangular region.
642 * rect [I] Coordinates of rectangular region.
645 * Success: Handle to region.
648 HRGN WINAPI
CreateRectRgnIndirect( const RECT
* rect
)
650 return CreateRectRgn( rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
654 /***********************************************************************
655 * SetRectRgn (GDI32.@)
657 * Sets a region to a simple rectangular region.
660 * hrgn [I] Region to convert.
661 * left [I] Left coordinate of rectangle.
662 * top [I] Top coordinate of rectangle.
663 * right [I] Right coordinate of rectangle.
664 * bottom [I] Bottom coordinate of rectangle.
671 * Allows either or both left and top to be greater than right or bottom.
673 BOOL WINAPI
SetRectRgn( HRGN hrgn
, INT left
, INT top
,
674 INT right
, INT bottom
)
678 TRACE("%p %d,%d-%d,%d\n", hrgn
, left
, top
, right
, bottom
);
680 if (!(obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
))) return FALSE
;
682 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
683 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
685 if((left
!= right
) && (top
!= bottom
))
687 obj
->rects
->left
= obj
->extents
.left
= left
;
688 obj
->rects
->top
= obj
->extents
.top
= top
;
689 obj
->rects
->right
= obj
->extents
.right
= right
;
690 obj
->rects
->bottom
= obj
->extents
.bottom
= bottom
;
696 GDI_ReleaseObj( hrgn
);
701 /***********************************************************************
702 * CreateRoundRectRgn (GDI32.@)
704 * Creates a rectangular region with rounded corners.
707 * left [I] Left coordinate of rectangle.
708 * top [I] Top coordinate of rectangle.
709 * right [I] Right coordinate of rectangle.
710 * bottom [I] Bottom coordinate of rectangle.
711 * ellipse_width [I] Width of the ellipse at each corner.
712 * ellipse_height [I] Height of the ellipse at each corner.
715 * Success: Handle to region.
719 * If ellipse_width or ellipse_height is less than 2 logical units then
720 * it is treated as though CreateRectRgn() was called instead.
722 HRGN WINAPI
CreateRoundRectRgn( INT left
, INT top
,
723 INT right
, INT bottom
,
724 INT ellipse_width
, INT ellipse_height
)
729 INT64 asq
, bsq
, dx
, dy
, err
;
732 /* Make the dimensions sensible */
734 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
735 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
736 /* the region is for the rectangle interior, but only at right and bottom for some reason */
740 ellipse_width
= min( right
- left
, abs( ellipse_width
));
741 ellipse_height
= min( bottom
- top
, abs( ellipse_height
));
743 /* Check if we can do a normal rectangle instead */
745 if ((ellipse_width
< 2) || (ellipse_height
< 2))
746 return CreateRectRgn( left
, top
, right
, bottom
);
748 if (!(obj
= alloc_region( ellipse_height
))) return 0;
749 obj
->numRects
= ellipse_height
;
750 obj
->extents
.left
= left
;
751 obj
->extents
.top
= top
;
752 obj
->extents
.right
= right
;
753 obj
->extents
.bottom
= bottom
;
756 /* based on an algorithm by Alois Zingl */
758 a
= ellipse_width
- 1;
759 b
= ellipse_height
- 1;
760 asq
= (INT64
)8 * a
* a
;
761 bsq
= (INT64
)8 * b
* b
;
762 dx
= (INT64
)4 * b
* b
* (1 - a
);
763 dy
= (INT64
)4 * a
* a
* (1 + (b
% 2));
764 err
= dx
+ dy
+ a
* a
* (b
% 2);
767 y
= ellipse_height
/ 2;
769 rects
[y
].left
= left
;
770 rects
[y
].right
= right
;
772 while (x
<= ellipse_width
/ 2)
784 rects
[y
].left
= left
+ x
;
785 rects
[y
].right
= right
- x
;
788 for (i
= 0; i
< ellipse_height
/ 2; i
++)
790 rects
[i
].left
= rects
[b
- i
].left
;
791 rects
[i
].right
= rects
[b
- i
].right
;
792 rects
[i
].top
= top
+ i
;
793 rects
[i
].bottom
= rects
[i
].top
+ 1;
795 for (; i
< ellipse_height
; i
++)
797 rects
[i
].top
= bottom
- ellipse_height
+ i
;
798 rects
[i
].bottom
= rects
[i
].top
+ 1;
800 rects
[ellipse_height
/ 2].top
= top
+ ellipse_height
/ 2; /* extend to top of rectangle */
802 hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
);
804 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
805 left
, top
, right
, bottom
, ellipse_width
, ellipse_height
, hrgn
);
806 if (!hrgn
) free_region( obj
);
811 /***********************************************************************
812 * CreateEllipticRgn (GDI32.@)
814 * Creates an elliptical region.
817 * left [I] Left coordinate of bounding rectangle.
818 * top [I] Top coordinate of bounding rectangle.
819 * right [I] Right coordinate of bounding rectangle.
820 * bottom [I] Bottom coordinate of bounding rectangle.
823 * Success: Handle to region.
827 * This is a special case of CreateRoundRectRgn() where the width of the
828 * ellipse at each corner is equal to the width the rectangle and
829 * the same for the height.
831 HRGN WINAPI
CreateEllipticRgn( INT left
, INT top
,
832 INT right
, INT bottom
)
834 return CreateRoundRectRgn( left
, top
, right
, bottom
,
835 right
-left
, bottom
-top
);
839 /***********************************************************************
840 * CreateEllipticRgnIndirect (GDI32.@)
842 * Creates an elliptical region.
845 * rect [I] Pointer to bounding rectangle of the ellipse.
848 * Success: Handle to region.
852 * This is a special case of CreateRoundRectRgn() where the width of the
853 * ellipse at each corner is equal to the width the rectangle and
854 * the same for the height.
856 HRGN WINAPI
CreateEllipticRgnIndirect( const RECT
*rect
)
858 return CreateRoundRectRgn( rect
->left
, rect
->top
, rect
->right
,
859 rect
->bottom
, rect
->right
- rect
->left
,
860 rect
->bottom
- rect
->top
);
863 /***********************************************************************
864 * GetRegionData (GDI32.@)
866 * Retrieves the data that specifies the region.
869 * hrgn [I] Region to retrieve the region data from.
870 * count [I] The size of the buffer pointed to by rgndata in bytes.
871 * rgndata [I] The buffer to receive data about the region.
874 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
875 * the number of bytes copied to the output buffer.
879 * The format of the Buffer member of RGNDATA is determined by the iType
880 * member of the region data header.
881 * Currently this is always RDH_RECTANGLES, which specifies that the format
882 * is the array of RECT's that specify the region. The length of the array
883 * is specified by the nCount member of the region data header.
885 DWORD WINAPI
GetRegionData(HRGN hrgn
, DWORD count
, LPRGNDATA rgndata
)
888 WINEREGION
*obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
);
890 TRACE(" %p count = %d, rgndata = %p\n", hrgn
, count
, rgndata
);
894 size
= obj
->numRects
* sizeof(RECT
);
895 if (!rgndata
|| count
< FIELD_OFFSET(RGNDATA
, Buffer
[size
]))
897 GDI_ReleaseObj( hrgn
);
898 if (rgndata
) /* buffer is too small, signal it by return 0 */
900 /* user requested buffer size with NULL rgndata */
901 return FIELD_OFFSET(RGNDATA
, Buffer
[size
]);
904 rgndata
->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
905 rgndata
->rdh
.iType
= RDH_RECTANGLES
;
906 rgndata
->rdh
.nCount
= obj
->numRects
;
907 rgndata
->rdh
.nRgnSize
= size
;
908 rgndata
->rdh
.rcBound
.left
= obj
->extents
.left
;
909 rgndata
->rdh
.rcBound
.top
= obj
->extents
.top
;
910 rgndata
->rdh
.rcBound
.right
= obj
->extents
.right
;
911 rgndata
->rdh
.rcBound
.bottom
= obj
->extents
.bottom
;
913 memcpy( rgndata
->Buffer
, obj
->rects
, size
);
915 GDI_ReleaseObj( hrgn
);
916 return FIELD_OFFSET(RGNDATA
, Buffer
[size
]);
920 static void translate( POINT
*pt
, UINT count
, const XFORM
*xform
)
926 pt
->x
= floor( x
* xform
->eM11
+ y
* xform
->eM21
+ xform
->eDx
+ 0.5 );
927 pt
->y
= floor( x
* xform
->eM12
+ y
* xform
->eM22
+ xform
->eDy
+ 0.5 );
933 /***********************************************************************
934 * ExtCreateRegion (GDI32.@)
936 * Creates a region as specified by the transformation data and region data.
939 * lpXform [I] World-space to logical-space transformation data.
940 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
941 * rgndata [I] Data that specifies the region.
944 * Success: Handle to region.
948 * See GetRegionData().
950 HRGN WINAPI
ExtCreateRegion( const XFORM
* lpXform
, DWORD dwCount
, const RGNDATA
* rgndata
)
954 const RECT
*pCurRect
, *pEndRect
;
958 SetLastError( ERROR_INVALID_PARAMETER
);
962 if (rgndata
->rdh
.dwSize
< sizeof(RGNDATAHEADER
))
965 /* XP doesn't care about the type */
966 if( rgndata
->rdh
.iType
!= RDH_RECTANGLES
)
967 WARN("(Unsupported region data type: %u)\n", rgndata
->rdh
.iType
);
971 const RECT
*pCurRect
, *pEndRect
;
973 hrgn
= CreateRectRgn( 0, 0, 0, 0 );
975 pEndRect
= (const RECT
*)rgndata
->Buffer
+ rgndata
->rdh
.nCount
;
976 for (pCurRect
= (const RECT
*)rgndata
->Buffer
; pCurRect
< pEndRect
; pCurRect
++)
978 static const INT count
= 4;
982 pt
[0].x
= pCurRect
->left
;
983 pt
[0].y
= pCurRect
->top
;
984 pt
[1].x
= pCurRect
->right
;
985 pt
[1].y
= pCurRect
->top
;
986 pt
[2].x
= pCurRect
->right
;
987 pt
[2].y
= pCurRect
->bottom
;
988 pt
[3].x
= pCurRect
->left
;
989 pt
[3].y
= pCurRect
->bottom
;
991 translate( pt
, 4, lpXform
);
992 poly_hrgn
= CreatePolyPolygonRgn( pt
, &count
, 1, WINDING
);
993 CombineRgn( hrgn
, hrgn
, poly_hrgn
, RGN_OR
);
994 DeleteObject( poly_hrgn
);
999 if (!(obj
= alloc_region( rgndata
->rdh
.nCount
))) return 0;
1001 pEndRect
= (const RECT
*)rgndata
->Buffer
+ rgndata
->rdh
.nCount
;
1002 for(pCurRect
= (const RECT
*)rgndata
->Buffer
; pCurRect
< pEndRect
; pCurRect
++)
1004 if (pCurRect
->left
< pCurRect
->right
&& pCurRect
->top
< pCurRect
->bottom
)
1006 if (!REGION_UnionRectWithRegion( pCurRect
, obj
)) goto done
;
1009 hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
);
1012 if (!hrgn
) free_region( obj
);
1014 TRACE("%p %d %p returning %p\n", lpXform
, dwCount
, rgndata
, hrgn
);
1019 /***********************************************************************
1020 * PtInRegion (GDI32.@)
1022 * Tests whether the specified point is inside a region.
1025 * hrgn [I] Region to test.
1026 * x [I] X-coordinate of point to test.
1027 * y [I] Y-coordinate of point to test.
1030 * Non-zero if the point is inside the region or zero otherwise.
1032 BOOL WINAPI
PtInRegion( HRGN hrgn
, INT x
, INT y
)
1037 if ((obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
)))
1039 if (obj
->numRects
> 0 && is_in_rect( &obj
->extents
, x
, y
))
1040 region_find_pt( obj
, x
, y
, &ret
);
1041 GDI_ReleaseObj( hrgn
);
1047 /***********************************************************************
1048 * RectInRegion (GDI32.@)
1050 * Tests if a rectangle is at least partly inside the specified region.
1053 * hrgn [I] Region to test.
1054 * rect [I] Rectangle to test.
1057 * Non-zero if the rectangle is partially inside the region or
1060 BOOL WINAPI
RectInRegion( HRGN hrgn
, const RECT
*rect
)
1067 /* swap the coordinates to make right >= left and bottom >= top */
1068 /* (region building rectangles are normalized the same way) */
1072 if ((obj
= GDI_GetObjPtr( hrgn
, OBJ_REGION
)))
1074 if ((obj
->numRects
> 0) && overlapping(&obj
->extents
, &rc
))
1076 for (i
= region_find_pt( obj
, rc
.left
, rc
.top
, &ret
); !ret
&& i
< obj
->numRects
; i
++ )
1078 if (obj
->rects
[i
].bottom
<= rc
.top
)
1079 continue; /* not far enough down yet */
1081 if (obj
->rects
[i
].top
>= rc
.bottom
)
1082 break; /* too far down */
1084 if (obj
->rects
[i
].right
<= rc
.left
)
1085 continue; /* not far enough over yet */
1087 if (obj
->rects
[i
].left
>= rc
.right
)
1093 GDI_ReleaseObj(hrgn
);
1098 /***********************************************************************
1099 * EqualRgn (GDI32.@)
1101 * Tests whether one region is identical to another.
1104 * hrgn1 [I] The first region to compare.
1105 * hrgn2 [I] The second region to compare.
1108 * Non-zero if both regions are identical or zero otherwise.
1110 BOOL WINAPI
EqualRgn( HRGN hrgn1
, HRGN hrgn2
)
1112 WINEREGION
*obj1
, *obj2
;
1115 if ((obj1
= GDI_GetObjPtr( hrgn1
, OBJ_REGION
)))
1117 if ((obj2
= GDI_GetObjPtr( hrgn2
, OBJ_REGION
)))
1121 if ( obj1
->numRects
!= obj2
->numRects
) goto done
;
1122 if ( obj1
->numRects
== 0 )
1128 if (obj1
->extents
.left
!= obj2
->extents
.left
) goto done
;
1129 if (obj1
->extents
.right
!= obj2
->extents
.right
) goto done
;
1130 if (obj1
->extents
.top
!= obj2
->extents
.top
) goto done
;
1131 if (obj1
->extents
.bottom
!= obj2
->extents
.bottom
) goto done
;
1132 for( i
= 0; i
< obj1
->numRects
; i
++ )
1134 if (obj1
->rects
[i
].left
!= obj2
->rects
[i
].left
) goto done
;
1135 if (obj1
->rects
[i
].right
!= obj2
->rects
[i
].right
) goto done
;
1136 if (obj1
->rects
[i
].top
!= obj2
->rects
[i
].top
) goto done
;
1137 if (obj1
->rects
[i
].bottom
!= obj2
->rects
[i
].bottom
) goto done
;
1141 GDI_ReleaseObj(hrgn2
);
1143 GDI_ReleaseObj(hrgn1
);
1148 /***********************************************************************
1149 * REGION_UnionRectWithRegion
1150 * Adds a rectangle to a WINEREGION
1152 static BOOL
REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
)
1156 init_region( ®ion
, 1 );
1157 region
.numRects
= 1;
1158 region
.extents
= *region
.rects
= *rect
;
1159 return REGION_UnionRegion(rgn
, rgn
, ®ion
);
1163 BOOL
add_rect_to_region( HRGN rgn
, const RECT
*rect
)
1165 WINEREGION
*obj
= GDI_GetObjPtr( rgn
, OBJ_REGION
);
1168 if (!obj
) return FALSE
;
1169 ret
= REGION_UnionRectWithRegion( rect
, obj
);
1170 GDI_ReleaseObj( rgn
);
1174 /***********************************************************************
1175 * REGION_CreateFrameRgn
1177 * Create a region that is a frame around another region.
1178 * Compute the intersection of the region moved in all 4 directions
1179 * ( +x, -x, +y, -y) and subtract from the original.
1180 * The result looks slightly better than in Windows :)
1182 BOOL
REGION_FrameRgn( HRGN hDest
, HRGN hSrc
, INT x
, INT y
)
1186 WINEREGION
* destObj
= NULL
;
1187 WINEREGION
*srcObj
= GDI_GetObjPtr( hSrc
, OBJ_REGION
);
1189 tmprgn
.rects
= NULL
;
1190 if (!srcObj
) return FALSE
;
1191 if (srcObj
->numRects
!= 0)
1193 if (!(destObj
= GDI_GetObjPtr( hDest
, OBJ_REGION
))) goto done
;
1194 if (!init_region( &tmprgn
, srcObj
->numRects
)) goto done
;
1196 if (!REGION_OffsetRegion( destObj
, srcObj
, -x
, 0)) goto done
;
1197 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, x
, 0)) goto done
;
1198 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1199 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, 0, -y
)) goto done
;
1200 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1201 if (!REGION_OffsetRegion( &tmprgn
, srcObj
, 0, y
)) goto done
;
1202 if (!REGION_IntersectRegion( destObj
, destObj
, &tmprgn
)) goto done
;
1203 if (!REGION_SubtractRegion( destObj
, srcObj
, destObj
)) goto done
;
1207 destroy_region( &tmprgn
);
1208 if (destObj
) GDI_ReleaseObj ( hDest
);
1209 GDI_ReleaseObj( hSrc
);
1214 /***********************************************************************
1215 * CombineRgn (GDI32.@)
1217 * Combines two regions with the specified operation and stores the result
1218 * in the specified destination region.
1221 * hDest [I] The region that receives the combined result.
1222 * hSrc1 [I] The first source region.
1223 * hSrc2 [I] The second source region.
1224 * mode [I] The way in which the source regions will be combined. See notes.
1228 * NULLREGION - The new region is empty.
1229 * SIMPLEREGION - The new region can be represented by one rectangle.
1230 * COMPLEXREGION - The new region can only be represented by more than
1235 * The two source regions can be the same region.
1236 * The mode can be one of the following:
1237 *| RGN_AND - Intersection of the regions
1238 *| RGN_OR - Union of the regions
1239 *| RGN_XOR - Unions of the regions minus any intersection.
1240 *| RGN_DIFF - Difference (subtraction) of the regions.
1242 INT WINAPI
CombineRgn(HRGN hDest
, HRGN hSrc1
, HRGN hSrc2
, INT mode
)
1244 WINEREGION
*destObj
= GDI_GetObjPtr( hDest
, OBJ_REGION
);
1247 TRACE(" %p,%p -> %p mode=%x\n", hSrc1
, hSrc2
, hDest
, mode
);
1250 WINEREGION
*src1Obj
= GDI_GetObjPtr( hSrc1
, OBJ_REGION
);
1254 TRACE("dump src1Obj:\n");
1255 if(TRACE_ON(region
))
1256 REGION_DumpRegion(src1Obj
);
1257 if (mode
== RGN_COPY
)
1259 if (REGION_CopyRegion( destObj
, src1Obj
))
1260 result
= get_region_type( destObj
);
1264 WINEREGION
*src2Obj
= GDI_GetObjPtr( hSrc2
, OBJ_REGION
);
1268 TRACE("dump src2Obj:\n");
1269 if(TRACE_ON(region
))
1270 REGION_DumpRegion(src2Obj
);
1274 if (REGION_IntersectRegion( destObj
, src1Obj
, src2Obj
))
1275 result
= get_region_type( destObj
);
1278 if (REGION_UnionRegion( destObj
, src1Obj
, src2Obj
))
1279 result
= get_region_type( destObj
);
1282 if (REGION_XorRegion( destObj
, src1Obj
, src2Obj
))
1283 result
= get_region_type( destObj
);
1286 if (REGION_SubtractRegion( destObj
, src1Obj
, src2Obj
))
1287 result
= get_region_type( destObj
);
1290 GDI_ReleaseObj( hSrc2
);
1293 GDI_ReleaseObj( hSrc1
);
1295 TRACE("dump destObj:\n");
1296 if(TRACE_ON(region
))
1297 REGION_DumpRegion(destObj
);
1299 GDI_ReleaseObj( hDest
);
1304 /***********************************************************************
1306 * Re-calculate the extents of a region
1308 static void REGION_SetExtents (WINEREGION
*pReg
)
1310 RECT
*pRect
, *pRectEnd
, *pExtents
;
1312 if (pReg
->numRects
== 0)
1314 pReg
->extents
.left
= 0;
1315 pReg
->extents
.top
= 0;
1316 pReg
->extents
.right
= 0;
1317 pReg
->extents
.bottom
= 0;
1321 pExtents
= &pReg
->extents
;
1322 pRect
= pReg
->rects
;
1323 pRectEnd
= &pRect
[pReg
->numRects
- 1];
1326 * Since pRect is the first rectangle in the region, it must have the
1327 * smallest top and since pRectEnd is the last rectangle in the region,
1328 * it must have the largest bottom, because of banding. Initialize left and
1329 * right from pRect and pRectEnd, resp., as good things to initialize them
1332 pExtents
->left
= pRect
->left
;
1333 pExtents
->top
= pRect
->top
;
1334 pExtents
->right
= pRectEnd
->right
;
1335 pExtents
->bottom
= pRectEnd
->bottom
;
1337 while (pRect
<= pRectEnd
)
1339 if (pRect
->left
< pExtents
->left
)
1340 pExtents
->left
= pRect
->left
;
1341 if (pRect
->right
> pExtents
->right
)
1342 pExtents
->right
= pRect
->right
;
1347 /***********************************************************************
1350 static BOOL
REGION_CopyRegion(WINEREGION
*dst
, WINEREGION
*src
)
1352 if (dst
!= src
) /* don't want to copy to itself */
1354 if (dst
->size
< src
->numRects
&& !grow_region( dst
, src
->numRects
))
1357 dst
->numRects
= src
->numRects
;
1358 dst
->extents
.left
= src
->extents
.left
;
1359 dst
->extents
.top
= src
->extents
.top
;
1360 dst
->extents
.right
= src
->extents
.right
;
1361 dst
->extents
.bottom
= src
->extents
.bottom
;
1362 memcpy(dst
->rects
, src
->rects
, src
->numRects
* sizeof(RECT
));
1367 /***********************************************************************
1368 * REGION_MirrorRegion
1370 static BOOL
REGION_MirrorRegion( WINEREGION
*dst
, WINEREGION
*src
, int width
)
1379 if (!grow_region( dst
, src
->numRects
)) return FALSE
;
1381 dst
->numRects
= src
->numRects
;
1385 if (!init_region( &tmp
, src
->numRects
)) return FALSE
;
1387 tmp
.numRects
= src
->numRects
;
1390 extents
.left
= width
- src
->extents
.right
;
1391 extents
.right
= width
- src
->extents
.left
;
1392 extents
.top
= src
->extents
.top
;
1393 extents
.bottom
= src
->extents
.bottom
;
1395 for (start
= 0; start
< src
->numRects
; start
= end
)
1397 /* find the end of the current band */
1398 for (end
= start
+ 1; end
< src
->numRects
; end
++)
1399 if (src
->rects
[end
].top
!= src
->rects
[end
- 1].top
) break;
1401 for (i
= 0; i
< end
- start
; i
++)
1403 rects
[start
+ i
].left
= width
- src
->rects
[end
- i
- 1].right
;
1404 rects
[start
+ i
].right
= width
- src
->rects
[end
- i
- 1].left
;
1405 rects
[start
+ i
].top
= src
->rects
[end
- i
- 1].top
;
1406 rects
[start
+ i
].bottom
= src
->rects
[end
- i
- 1].bottom
;
1411 move_rects( dst
, &tmp
);
1413 dst
->extents
= extents
;
1417 /***********************************************************************
1420 INT
mirror_region( HRGN dst
, HRGN src
, INT width
)
1422 WINEREGION
*src_rgn
, *dst_rgn
;
1425 if (!(src_rgn
= GDI_GetObjPtr( src
, OBJ_REGION
))) return ERROR
;
1426 if ((dst_rgn
= GDI_GetObjPtr( dst
, OBJ_REGION
)))
1428 if (REGION_MirrorRegion( dst_rgn
, src_rgn
, width
)) ret
= get_region_type( dst_rgn
);
1429 GDI_ReleaseObj( dst_rgn
);
1431 GDI_ReleaseObj( src_rgn
);
1435 /***********************************************************************
1436 * MirrorRgn (GDI32.@)
1438 BOOL WINAPI
MirrorRgn( HWND hwnd
, HRGN hrgn
)
1440 static const WCHAR user32W
[] = {'u','s','e','r','3','2','.','d','l','l',0};
1441 static BOOL (WINAPI
*pGetWindowRect
)( HWND hwnd
, LPRECT rect
);
1444 /* yes, a HWND in gdi32, don't ask */
1445 if (!pGetWindowRect
)
1447 HMODULE user32
= GetModuleHandleW(user32W
);
1448 if (!user32
) return FALSE
;
1449 if (!(pGetWindowRect
= (void *)GetProcAddress( user32
, "GetWindowRect" ))) return FALSE
;
1451 pGetWindowRect( hwnd
, &rect
);
1452 return mirror_region( hrgn
, hrgn
, rect
.right
- rect
.left
) != ERROR
;
1456 /***********************************************************************
1459 * Attempt to merge the rects in the current band with those in the
1460 * previous one. Used only by REGION_RegionOp.
1463 * The new index for the previous band.
1466 * If coalescing takes place:
1467 * - rectangles in the previous band will have their bottom fields
1469 * - pReg->numRects will be decreased.
1472 static INT
REGION_Coalesce (
1473 WINEREGION
*pReg
, /* Region to coalesce */
1474 INT prevStart
, /* Index of start of previous band */
1475 INT curStart
/* Index of start of current band */
1477 RECT
*pPrevRect
; /* Current rect in previous band */
1478 RECT
*pCurRect
; /* Current rect in current band */
1479 RECT
*pRegEnd
; /* End of region */
1480 INT curNumRects
; /* Number of rectangles in current band */
1481 INT prevNumRects
; /* Number of rectangles in previous band */
1482 INT bandtop
; /* top coordinate for current band */
1484 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
1486 pPrevRect
= &pReg
->rects
[prevStart
];
1487 prevNumRects
= curStart
- prevStart
;
1490 * Figure out how many rectangles are in the current band. Have to do
1491 * this because multiple bands could have been added in REGION_RegionOp
1492 * at the end when one region has been exhausted.
1494 pCurRect
= &pReg
->rects
[curStart
];
1495 bandtop
= pCurRect
->top
;
1496 for (curNumRects
= 0;
1497 (pCurRect
!= pRegEnd
) && (pCurRect
->top
== bandtop
);
1503 if (pCurRect
!= pRegEnd
)
1506 * If more than one band was added, we have to find the start
1507 * of the last band added so the next coalescing job can start
1508 * at the right place... (given when multiple bands are added,
1509 * this may be pointless -- see above).
1512 while (pRegEnd
[-1].top
== pRegEnd
->top
)
1516 curStart
= pRegEnd
- pReg
->rects
;
1517 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
1520 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0)) {
1521 pCurRect
-= curNumRects
;
1523 * The bands may only be coalesced if the bottom of the previous
1524 * matches the top scanline of the current.
1526 if (pPrevRect
->bottom
== pCurRect
->top
)
1529 * Make sure the bands have rects in the same places. This
1530 * assumes that rects have been added in such a way that they
1531 * cover the most area possible. I.e. two rects in a band must
1532 * have some horizontal space between them.
1536 if ((pPrevRect
->left
!= pCurRect
->left
) ||
1537 (pPrevRect
->right
!= pCurRect
->right
))
1540 * The bands don't line up so they can't be coalesced.
1547 } while (prevNumRects
!= 0);
1549 pReg
->numRects
-= curNumRects
;
1550 pCurRect
-= curNumRects
;
1551 pPrevRect
-= curNumRects
;
1554 * The bands may be merged, so set the bottom of each rect
1555 * in the previous band to that of the corresponding rect in
1560 pPrevRect
->bottom
= pCurRect
->bottom
;
1564 } while (curNumRects
!= 0);
1567 * If only one band was added to the region, we have to backup
1568 * curStart to the start of the previous band.
1570 * If more than one band was added to the region, copy the
1571 * other bands down. The assumption here is that the other bands
1572 * came from the same region as the current one and no further
1573 * coalescing can be done on them since it's all been done
1574 * already... curStart is already in the right place.
1576 if (pCurRect
== pRegEnd
)
1578 curStart
= prevStart
;
1584 *pPrevRect
++ = *pCurRect
++;
1585 } while (pCurRect
!= pRegEnd
);
1593 /**********************************************************************
1596 * To keep regions from growing without bound, shrink the array of rectangles
1597 * to match the new number of rectangles in the region.
1599 * Only do this if the number of rectangles allocated is more than
1600 * twice the number of rectangles in the region.
1602 static void REGION_compact( WINEREGION
*reg
)
1604 if ((reg
->numRects
< reg
->size
/ 2) && (reg
->numRects
> RGN_DEFAULT_RECTS
))
1606 RECT
*new_rects
= HeapReAlloc( GetProcessHeap(), 0, reg
->rects
, reg
->numRects
* sizeof(RECT
) );
1609 reg
->rects
= new_rects
;
1610 reg
->size
= reg
->numRects
;
1615 /***********************************************************************
1618 * Apply an operation to two regions. Called by REGION_Union,
1619 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1625 * The new region is overwritten.
1628 * The idea behind this function is to view the two regions as sets.
1629 * Together they cover a rectangle of area that this function divides
1630 * into horizontal bands where points are covered only by one region
1631 * or by both. For the first case, the nonOverlapFunc is called with
1632 * each the band and the band's upper and lower extents. For the
1633 * second, the overlapFunc is called to process the entire band. It
1634 * is responsible for clipping the rectangles in the band, though
1635 * this function provides the boundaries.
1636 * At the end of each band, the new region is coalesced, if possible,
1637 * to reduce the number of rectangles in the region.
1640 static BOOL
REGION_RegionOp(
1641 WINEREGION
*destReg
, /* Place to store result */
1642 WINEREGION
*reg1
, /* First region in operation */
1643 WINEREGION
*reg2
, /* 2nd region in operation */
1644 BOOL (*overlapFunc
)(WINEREGION
*, RECT
*, RECT
*, RECT
*, RECT
*, INT
, INT
), /* Function to call for over-lapping bands */
1645 BOOL (*nonOverlap1Func
)(WINEREGION
*, RECT
*, RECT
*, INT
, INT
), /* Function to call for non-overlapping bands in region 1 */
1646 BOOL (*nonOverlap2Func
)(WINEREGION
*, RECT
*, RECT
*, INT
, INT
) /* Function to call for non-overlapping bands in region 2 */
1649 RECT
*r1
; /* Pointer into first region */
1650 RECT
*r2
; /* Pointer into 2d region */
1651 RECT
*r1End
; /* End of 1st region */
1652 RECT
*r2End
; /* End of 2d region */
1653 INT ybot
; /* Bottom of intersection */
1654 INT ytop
; /* Top of intersection */
1655 INT prevBand
; /* Index of start of
1656 * previous band in newReg */
1657 INT curBand
; /* Index of start of current
1659 RECT
*r1BandEnd
; /* End of current band in r1 */
1660 RECT
*r2BandEnd
; /* End of current band in r2 */
1661 INT top
; /* Top of non-overlapping band */
1662 INT bot
; /* Bottom of non-overlapping band */
1666 * set r1, r2, r1End and r2End appropriately, preserve the important
1667 * parts of the destination region until the end in case it's one of
1668 * the two source regions, then mark the "new" region empty, allocating
1669 * another array of rectangles for it to use.
1673 r1End
= r1
+ reg1
->numRects
;
1674 r2End
= r2
+ reg2
->numRects
;
1677 * Allocate a reasonable number of rectangles for the new region. The idea
1678 * is to allocate enough so the individual functions don't need to
1679 * reallocate and copy the array, which is time consuming, yet we don't
1680 * have to worry about using too much memory. I hope to be able to
1681 * nuke the Xrealloc() at the end of this function eventually.
1683 if (!init_region( &newReg
, max(reg1
->numRects
,reg2
->numRects
) * 2 )) return FALSE
;
1686 * Initialize ybot and ytop.
1687 * In the upcoming loop, ybot and ytop serve different functions depending
1688 * on whether the band being handled is an overlapping or non-overlapping
1690 * In the case of a non-overlapping band (only one of the regions
1691 * has points in the band), ybot is the bottom of the most recent
1692 * intersection and thus clips the top of the rectangles in that band.
1693 * ytop is the top of the next intersection between the two regions and
1694 * serves to clip the bottom of the rectangles in the current band.
1695 * For an overlapping band (where the two regions intersect), ytop clips
1696 * the top of the rectangles of both regions and ybot clips the bottoms.
1698 if (reg1
->extents
.top
< reg2
->extents
.top
)
1699 ybot
= reg1
->extents
.top
;
1701 ybot
= reg2
->extents
.top
;
1704 * prevBand serves to mark the start of the previous band so rectangles
1705 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1706 * In the beginning, there is no previous band, so prevBand == curBand
1707 * (curBand is set later on, of course, but the first band will always
1708 * start at index 0). prevBand and curBand must be indices because of
1709 * the possible expansion, and resultant moving, of the new region's
1710 * array of rectangles.
1716 curBand
= newReg
.numRects
;
1719 * This algorithm proceeds one source-band (as opposed to a
1720 * destination band, which is determined by where the two regions
1721 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1722 * rectangle after the last one in the current band for their
1723 * respective regions.
1726 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->top
== r1
->top
))
1732 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->top
== r2
->top
))
1738 * First handle the band that doesn't intersect, if any.
1740 * Note that attention is restricted to one band in the
1741 * non-intersecting region at once, so if a region has n
1742 * bands between the current position and the next place it overlaps
1743 * the other, this entire loop will be passed through n times.
1745 if (r1
->top
< r2
->top
)
1747 top
= max(r1
->top
,ybot
);
1748 bot
= min(r1
->bottom
,r2
->top
);
1750 if ((top
!= bot
) && (nonOverlap1Func
!= NULL
))
1752 if (!nonOverlap1Func(&newReg
, r1
, r1BandEnd
, top
, bot
)) return FALSE
;
1757 else if (r2
->top
< r1
->top
)
1759 top
= max(r2
->top
,ybot
);
1760 bot
= min(r2
->bottom
,r1
->top
);
1762 if ((top
!= bot
) && (nonOverlap2Func
!= NULL
))
1764 if (!nonOverlap2Func(&newReg
, r2
, r2BandEnd
, top
, bot
)) return FALSE
;
1775 * If any rectangles got added to the region, try and coalesce them
1776 * with rectangles from the previous band. Note we could just do
1777 * this test in miCoalesce, but some machines incur a not
1778 * inconsiderable cost for function calls, so...
1780 if (newReg
.numRects
!= curBand
)
1782 prevBand
= REGION_Coalesce (&newReg
, prevBand
, curBand
);
1786 * Now see if we've hit an intersecting band. The two bands only
1787 * intersect if ybot > ytop
1789 ybot
= min(r1
->bottom
, r2
->bottom
);
1790 curBand
= newReg
.numRects
;
1793 if (!overlapFunc(&newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
)) return FALSE
;
1796 if (newReg
.numRects
!= curBand
)
1798 prevBand
= REGION_Coalesce (&newReg
, prevBand
, curBand
);
1802 * If we've finished with a band (bottom == ybot) we skip forward
1803 * in the region to the next band.
1805 if (r1
->bottom
== ybot
)
1809 if (r2
->bottom
== ybot
)
1813 } while ((r1
!= r1End
) && (r2
!= r2End
));
1816 * Deal with whichever region still has rectangles left.
1818 curBand
= newReg
.numRects
;
1821 if (nonOverlap1Func
!= NULL
)
1826 while ((r1BandEnd
< r1End
) && (r1BandEnd
->top
== r1
->top
))
1830 if (!nonOverlap1Func(&newReg
, r1
, r1BandEnd
, max(r1
->top
,ybot
), r1
->bottom
))
1833 } while (r1
!= r1End
);
1836 else if ((r2
!= r2End
) && (nonOverlap2Func
!= NULL
))
1841 while ((r2BandEnd
< r2End
) && (r2BandEnd
->top
== r2
->top
))
1845 if (!nonOverlap2Func(&newReg
, r2
, r2BandEnd
, max(r2
->top
,ybot
), r2
->bottom
))
1848 } while (r2
!= r2End
);
1851 if (newReg
.numRects
!= curBand
)
1853 REGION_Coalesce (&newReg
, prevBand
, curBand
);
1856 REGION_compact( &newReg
);
1857 move_rects( destReg
, &newReg
);
1861 /***********************************************************************
1862 * Region Intersection
1863 ***********************************************************************/
1866 /***********************************************************************
1869 * Handle an overlapping band for REGION_Intersect.
1875 * Rectangles may be added to the region.
1878 static BOOL
REGION_IntersectO(WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
1879 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
1884 while ((r1
!= r1End
) && (r2
!= r2End
))
1886 left
= max(r1
->left
, r2
->left
);
1887 right
= min(r1
->right
, r2
->right
);
1890 * If there's any overlap between the two rectangles, add that
1891 * overlap to the new region.
1892 * There's no need to check for subsumption because the only way
1893 * such a need could arise is if some region has two rectangles
1894 * right next to each other. Since that should never happen...
1898 if (!add_rect( pReg
, left
, top
, right
, bottom
)) return FALSE
;
1902 * Need to advance the pointers. Shift the one that extends
1903 * to the right the least, since the other still has a chance to
1904 * overlap with that region's next rectangle, if you see what I mean.
1906 if (r1
->right
< r2
->right
)
1910 else if (r2
->right
< r1
->right
)
1923 /***********************************************************************
1924 * REGION_IntersectRegion
1926 static BOOL
REGION_IntersectRegion(WINEREGION
*newReg
, WINEREGION
*reg1
,
1929 /* check for trivial reject */
1930 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
1931 (!overlapping(®1
->extents
, ®2
->extents
)))
1932 newReg
->numRects
= 0;
1934 if (!REGION_RegionOp (newReg
, reg1
, reg2
, REGION_IntersectO
, NULL
, NULL
)) return FALSE
;
1937 * Can't alter newReg's extents before we call miRegionOp because
1938 * it might be one of the source regions and miRegionOp depends
1939 * on the extents of those regions being the same. Besides, this
1940 * way there's no checking against rectangles that will be nuked
1941 * due to coalescing, so we have to examine fewer rectangles.
1943 REGION_SetExtents(newReg
);
1947 /***********************************************************************
1949 ***********************************************************************/
1951 /***********************************************************************
1954 * Handle a non-overlapping band for the union operation. Just
1955 * Adds the rectangles into the region. Doesn't have to check for
1956 * subsumption or anything.
1962 * pReg->numRects is incremented and the final rectangles overwritten
1963 * with the rectangles we're passed.
1966 static BOOL
REGION_UnionNonO(WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
, INT top
, INT bottom
)
1970 if (!add_rect( pReg
, r
->left
, top
, r
->right
, bottom
)) return FALSE
;
1976 /***********************************************************************
1979 * Handle an overlapping band for the union operation. Picks the
1980 * left-most rectangle each time and merges it into the region.
1986 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1990 static BOOL
REGION_UnionO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
1991 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
1993 #define MERGERECT(r) \
1994 if ((pReg->numRects != 0) && \
1995 (pReg->rects[pReg->numRects-1].top == top) && \
1996 (pReg->rects[pReg->numRects-1].bottom == bottom) && \
1997 (pReg->rects[pReg->numRects-1].right >= r->left)) \
1999 if (pReg->rects[pReg->numRects-1].right < r->right) \
2000 pReg->rects[pReg->numRects-1].right = r->right; \
2004 if (!add_rect( pReg, r->left, top, r->right, bottom )) return FALSE; \
2008 while ((r1
!= r1End
) && (r2
!= r2End
))
2010 if (r1
->left
< r2
->left
)
2025 } while (r1
!= r1End
);
2027 else while (r2
!= r2End
)
2035 /***********************************************************************
2036 * REGION_UnionRegion
2038 static BOOL
REGION_UnionRegion(WINEREGION
*newReg
, WINEREGION
*reg1
, WINEREGION
*reg2
)
2042 /* checks all the simple cases */
2045 * Region 1 and 2 are the same or region 1 is empty
2047 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
2050 ret
= REGION_CopyRegion(newReg
, reg2
);
2055 * if nothing to union (region 2 empty)
2057 if (!(reg2
->numRects
))
2060 ret
= REGION_CopyRegion(newReg
, reg1
);
2065 * Region 1 completely subsumes region 2
2067 if ((reg1
->numRects
== 1) &&
2068 (reg1
->extents
.left
<= reg2
->extents
.left
) &&
2069 (reg1
->extents
.top
<= reg2
->extents
.top
) &&
2070 (reg1
->extents
.right
>= reg2
->extents
.right
) &&
2071 (reg1
->extents
.bottom
>= reg2
->extents
.bottom
))
2074 ret
= REGION_CopyRegion(newReg
, reg1
);
2079 * Region 2 completely subsumes region 1
2081 if ((reg2
->numRects
== 1) &&
2082 (reg2
->extents
.left
<= reg1
->extents
.left
) &&
2083 (reg2
->extents
.top
<= reg1
->extents
.top
) &&
2084 (reg2
->extents
.right
>= reg1
->extents
.right
) &&
2085 (reg2
->extents
.bottom
>= reg1
->extents
.bottom
))
2088 ret
= REGION_CopyRegion(newReg
, reg2
);
2092 if ((ret
= REGION_RegionOp (newReg
, reg1
, reg2
, REGION_UnionO
, REGION_UnionNonO
, REGION_UnionNonO
)))
2094 newReg
->extents
.left
= min(reg1
->extents
.left
, reg2
->extents
.left
);
2095 newReg
->extents
.top
= min(reg1
->extents
.top
, reg2
->extents
.top
);
2096 newReg
->extents
.right
= max(reg1
->extents
.right
, reg2
->extents
.right
);
2097 newReg
->extents
.bottom
= max(reg1
->extents
.bottom
, reg2
->extents
.bottom
);
2102 /***********************************************************************
2103 * Region Subtraction
2104 ***********************************************************************/
2106 /***********************************************************************
2107 * REGION_SubtractNonO1
2109 * Deal with non-overlapping band for subtraction. Any parts from
2110 * region 2 we discard. Anything from region 1 we add to the region.
2116 * pReg may be affected.
2119 static BOOL
REGION_SubtractNonO1 (WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
, INT top
, INT bottom
)
2123 if (!add_rect( pReg
, r
->left
, top
, r
->right
, bottom
)) return FALSE
;
2130 /***********************************************************************
2133 * Overlapping band subtraction. x1 is the left-most point not yet
2140 * pReg may have rectangles added to it.
2143 static BOOL
REGION_SubtractO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
2144 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
2146 INT left
= r1
->left
;
2148 while ((r1
!= r1End
) && (r2
!= r2End
))
2150 if (r2
->right
<= left
)
2153 * Subtrahend missed the boat: go to next subtrahend.
2157 else if (r2
->left
<= left
)
2160 * Subtrahend precedes minuend: nuke left edge of minuend.
2163 if (left
>= r1
->right
)
2166 * Minuend completely covered: advance to next minuend and
2167 * reset left fence to edge of new minuend.
2176 * Subtrahend now used up since it doesn't extend beyond
2182 else if (r2
->left
< r1
->right
)
2185 * Left part of subtrahend covers part of minuend: add uncovered
2186 * part of minuend to region and skip to next subtrahend.
2188 if (!add_rect( pReg
, left
, top
, r2
->left
, bottom
)) return FALSE
;
2190 if (left
>= r1
->right
)
2193 * Minuend used up: advance to new...
2202 * Subtrahend used up
2210 * Minuend used up: add any remaining piece before advancing.
2212 if (r1
->right
> left
)
2214 if (!add_rect( pReg
, left
, top
, r1
->right
, bottom
)) return FALSE
;
2223 * Add remaining minuend rectangles to region.
2227 if (!add_rect( pReg
, left
, top
, r1
->right
, bottom
)) return FALSE
;
2237 /***********************************************************************
2238 * REGION_SubtractRegion
2240 * Subtract regS from regM and leave the result in regD.
2241 * S stands for subtrahend, M for minuend and D for difference.
2247 * regD is overwritten.
2250 static BOOL
REGION_SubtractRegion(WINEREGION
*regD
, WINEREGION
*regM
, WINEREGION
*regS
)
2252 /* check for trivial reject */
2253 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
2254 (!overlapping(®M
->extents
, ®S
->extents
)) )
2255 return REGION_CopyRegion(regD
, regM
);
2257 if (!REGION_RegionOp (regD
, regM
, regS
, REGION_SubtractO
, REGION_SubtractNonO1
, NULL
))
2261 * Can't alter newReg's extents before we call miRegionOp because
2262 * it might be one of the source regions and miRegionOp depends
2263 * on the extents of those regions being the unaltered. Besides, this
2264 * way there's no checking against rectangles that will be nuked
2265 * due to coalescing, so we have to examine fewer rectangles.
2267 REGION_SetExtents (regD
);
2271 /***********************************************************************
2274 static BOOL
REGION_XorRegion(WINEREGION
*dr
, WINEREGION
*sra
, WINEREGION
*srb
)
2276 WINEREGION tra
, trb
;
2279 if (!init_region( &tra
, sra
->numRects
+ 1 )) return FALSE
;
2280 if ((ret
= init_region( &trb
, srb
->numRects
+ 1 )))
2282 ret
= REGION_SubtractRegion(&tra
,sra
,srb
) &&
2283 REGION_SubtractRegion(&trb
,srb
,sra
) &&
2284 REGION_UnionRegion(dr
,&tra
,&trb
);
2285 destroy_region(&trb
);
2287 destroy_region(&tra
);
2291 /**************************************************************************
2295 *************************************************************************/
2297 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2298 #define SMALL_COORDINATE 0x80000000
2300 /***********************************************************************
2301 * REGION_InsertEdgeInET
2303 * Insert the given edge into the edge table.
2304 * First we must find the correct bucket in the
2305 * Edge table, then find the right slot in the
2306 * bucket. Finally, we can insert it.
2309 static void REGION_InsertEdgeInET(EdgeTable
*ET
, EdgeTableEntry
*ETE
,
2310 INT scanline
, ScanLineListBlock
**SLLBlock
, INT
*iSLLBlock
)
2314 ScanLineList
*pSLL
, *pPrevSLL
;
2315 ScanLineListBlock
*tmpSLLBlock
;
2318 * find the right bucket to put the edge into
2320 pPrevSLL
= &ET
->scanlines
;
2321 pSLL
= pPrevSLL
->next
;
2322 while (pSLL
&& (pSLL
->scanline
< scanline
))
2329 * reassign pSLL (pointer to ScanLineList) if necessary
2331 if ((!pSLL
) || (pSLL
->scanline
> scanline
))
2333 if (*iSLLBlock
> SLLSPERBLOCK
-1)
2335 tmpSLLBlock
= HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock
));
2338 WARN("Can't alloc SLLB\n");
2341 (*SLLBlock
)->next
= tmpSLLBlock
;
2342 tmpSLLBlock
->next
= NULL
;
2343 *SLLBlock
= tmpSLLBlock
;
2346 pSLL
= &((*SLLBlock
)->SLLs
[(*iSLLBlock
)++]);
2348 pSLL
->next
= pPrevSLL
->next
;
2349 list_init( &pSLL
->edgelist
);
2350 pPrevSLL
->next
= pSLL
;
2352 pSLL
->scanline
= scanline
;
2355 * now insert the edge in the right bucket
2357 LIST_FOR_EACH( ptr
, &pSLL
->edgelist
)
2359 struct edge_table_entry
*entry
= LIST_ENTRY( ptr
, struct edge_table_entry
, entry
);
2360 if (entry
->bres
.minor_axis
>= ETE
->bres
.minor_axis
) break;
2362 list_add_before( ptr
, &ETE
->entry
);
2365 /***********************************************************************
2366 * REGION_CreateEdgeTable
2368 * This routine creates the edge table for
2369 * scan converting polygons.
2370 * The Edge Table (ET) looks like:
2374 * | ymax | ScanLineLists
2375 * |scanline|-->------------>-------------->...
2376 * -------- |scanline| |scanline|
2377 * |edgelist| |edgelist|
2378 * --------- ---------
2382 * list of ETEs list of ETEs
2384 * where ETE is an EdgeTableEntry data structure,
2385 * and there is one ScanLineList per scanline at
2386 * which an edge is initially entered.
2389 static unsigned int REGION_CreateEdgeTable(const INT
*Count
, INT nbpolygons
,
2390 const POINT
*pts
, EdgeTable
*ET
,
2391 EdgeTableEntry
*pETEs
, ScanLineListBlock
*pSLLBlock
,
2392 const RECT
*clip_rect
)
2394 const POINT
*top
, *bottom
;
2395 const POINT
*PrevPt
, *CurrPt
, *EndPt
;
2398 unsigned int dy
, total
= 0;
2401 * initialize the Edge Table.
2403 ET
->scanlines
.next
= NULL
;
2404 ET
->ymax
= SMALL_COORDINATE
;
2405 ET
->ymin
= LARGE_COORDINATE
;
2406 pSLLBlock
->next
= NULL
;
2409 for(poly
= 0; poly
< nbpolygons
; poly
++)
2411 count
= Count
[poly
];
2419 * for each vertex in the array of points.
2420 * In this loop we are dealing with two vertices at
2421 * a time -- these make up one edge of the polygon.
2423 for ( ; count
; PrevPt
= CurrPt
, count
--)
2428 * find out which point is above and which is below.
2430 if (PrevPt
->y
> CurrPt
->y
)
2432 bottom
= PrevPt
, top
= CurrPt
;
2433 pETEs
->ClockWise
= 0;
2437 bottom
= CurrPt
, top
= PrevPt
;
2438 pETEs
->ClockWise
= 1;
2442 * don't add horizontal edges to the Edge table.
2444 if (bottom
->y
== top
->y
) continue;
2445 if (clip_rect
&& (top
->y
>= clip_rect
->bottom
|| bottom
->y
<= clip_rect
->top
)) continue;
2446 pETEs
->ymax
= bottom
->y
-1; /* -1 so we don't get last scanline */
2449 * initialize integer edge algorithm
2451 dy
= bottom
->y
- top
->y
;
2452 bres_init_polygon(dy
, top
->x
, bottom
->x
, &pETEs
->bres
);
2454 if (clip_rect
) dy
= min( bottom
->y
, clip_rect
->bottom
) - max( top
->y
, clip_rect
->top
);
2455 if (total
+ dy
< total
) return 0; /* overflow */
2458 REGION_InsertEdgeInET(ET
, pETEs
, top
->y
, &pSLLBlock
, &iSLLBlock
);
2460 if (top
->y
< ET
->ymin
) ET
->ymin
= top
->y
;
2461 if (bottom
->y
> ET
->ymax
) ET
->ymax
= bottom
->y
;
2468 /***********************************************************************
2471 * This routine moves EdgeTableEntries from the
2472 * EdgeTable into the Active Edge Table,
2473 * leaving them sorted by smaller x coordinate.
2476 static void REGION_loadAET( struct list
*AET
, struct list
*ETEs
)
2478 struct edge_table_entry
*ptr
, *next
, *entry
;
2479 struct list
*active
;
2481 LIST_FOR_EACH_ENTRY_SAFE( ptr
, next
, ETEs
, struct edge_table_entry
, entry
)
2483 LIST_FOR_EACH( active
, AET
)
2485 entry
= LIST_ENTRY( active
, struct edge_table_entry
, entry
);
2486 if (entry
->bres
.minor_axis
>= ptr
->bres
.minor_axis
) break;
2488 list_remove( &ptr
->entry
);
2489 list_add_before( active
, &ptr
->entry
);
2493 /***********************************************************************
2494 * REGION_computeWAET
2496 * This routine links the AET by the
2497 * nextWETE (winding EdgeTableEntry) link for
2498 * use by the winding number rule. The final
2499 * Active Edge Table (AET) might look something
2503 * ---------- --------- ---------
2504 * |ymax | |ymax | |ymax |
2505 * | ... | |... | |... |
2506 * |next |->|next |->|next |->...
2507 * |nextWETE| |nextWETE| |nextWETE|
2508 * --------- --------- ^--------
2510 * V-------------------> V---> ...
2513 static void REGION_computeWAET( struct list
*AET
, struct list
*WETE
)
2515 struct edge_table_entry
*active
;
2520 LIST_FOR_EACH_ENTRY( active
, AET
, struct edge_table_entry
, entry
)
2522 if (active
->ClockWise
)
2527 if ((!inside
&& !isInside
) || (inside
&& isInside
))
2529 list_add_tail( WETE
, &active
->winding_entry
);
2535 /***********************************************************************
2538 * Update the Active Edge Table for the next scan line and sort it again.
2540 static inline BOOL
next_scanline( struct list
*AET
, int y
)
2542 struct edge_table_entry
*active
, *next
, *insert
;
2543 BOOL changed
= FALSE
;
2545 LIST_FOR_EACH_ENTRY_SAFE( active
, next
, AET
, struct edge_table_entry
, entry
)
2547 if (active
->ymax
== y
) /* leaving this edge */
2549 list_remove( &active
->entry
);
2552 else bres_incr_polygon( &active
->bres
);
2554 LIST_FOR_EACH_ENTRY_SAFE( active
, next
, AET
, struct edge_table_entry
, entry
)
2556 LIST_FOR_EACH_ENTRY( insert
, AET
, struct edge_table_entry
, entry
)
2558 if (insert
== active
) break;
2559 if (insert
->bres
.minor_axis
> active
->bres
.minor_axis
) break;
2561 if (insert
== active
) continue;
2562 list_remove( &active
->entry
);
2563 list_add_before( &insert
->entry
, &active
->entry
);
2569 /***********************************************************************
2570 * REGION_FreeStorage
2574 static void REGION_FreeStorage(ScanLineListBlock
*pSLLBlock
)
2576 ScanLineListBlock
*tmpSLLBlock
;
2580 tmpSLLBlock
= pSLLBlock
->next
;
2581 HeapFree( GetProcessHeap(), 0, pSLLBlock
);
2582 pSLLBlock
= tmpSLLBlock
;
2586 /***********************************************************************
2587 * create_polypolygon_region
2589 * Helper for CreatePolyPolygonRgn.
2591 HRGN
create_polypolygon_region( const POINT
*Pts
, const INT
*Count
, INT nbpolygons
, INT mode
,
2592 const RECT
*clip_rect
)
2595 WINEREGION
*obj
= NULL
;
2596 INT y
; /* current scanline */
2597 struct list WETE
, *pWETE
; /* Winding Edge Table */
2598 ScanLineList
*pSLL
; /* current scanLineList */
2599 EdgeTable ET
; /* header node for ET */
2600 struct list AET
; /* header for AET */
2601 EdgeTableEntry
*pETEs
; /* EdgeTableEntries pool */
2602 ScanLineListBlock SLLBlock
; /* header for scanlinelist */
2603 struct edge_table_entry
*active
;
2604 unsigned int nb_points
;
2605 int cur_band
= 0, prev_band
= 0;
2606 INT i
, poly
, total
, first
= 1;
2608 TRACE("%p, count %d, polygons %d, mode %d\n", Pts
, *Count
, nbpolygons
, mode
);
2610 /* special case a rectangle */
2612 if (((nbpolygons
== 1) && ((*Count
== 4) ||
2613 ((*Count
== 5) && (Pts
[4].x
== Pts
[0].x
) && (Pts
[4].y
== Pts
[0].y
)))) &&
2614 (((Pts
[0].y
== Pts
[1].y
) &&
2615 (Pts
[1].x
== Pts
[2].x
) &&
2616 (Pts
[2].y
== Pts
[3].y
) &&
2617 (Pts
[3].x
== Pts
[0].x
)) ||
2618 ((Pts
[0].x
== Pts
[1].x
) &&
2619 (Pts
[1].y
== Pts
[2].y
) &&
2620 (Pts
[2].x
== Pts
[3].x
) &&
2621 (Pts
[3].y
== Pts
[0].y
))))
2622 return CreateRectRgn( min(Pts
[0].x
, Pts
[2].x
), min(Pts
[0].y
, Pts
[2].y
),
2623 max(Pts
[0].x
, Pts
[2].x
), max(Pts
[0].y
, Pts
[2].y
) );
2625 for(poly
= total
= 0; poly
< nbpolygons
; poly
++)
2626 total
+= Count
[poly
];
2627 if (! (pETEs
= HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry
) * total
)))
2630 nb_points
= REGION_CreateEdgeTable( Count
, nbpolygons
, Pts
, &ET
, pETEs
, &SLLBlock
, clip_rect
);
2631 if (!(obj
= alloc_region( nb_points
/ 2 )))
2634 if (clip_rect
) ET
.ymax
= min( ET
.ymax
, clip_rect
->bottom
);
2636 pSLL
= ET
.scanlines
.next
;
2637 if (mode
!= WINDING
) {
2641 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2643 * Add a new edge to the active edge table when we
2644 * get to the next edge.
2646 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2647 REGION_loadAET(&AET
, &pSLL
->edgelist
);
2651 if (!clip_rect
|| y
>= clip_rect
->top
)
2653 LIST_FOR_EACH_ENTRY( active
, &AET
, struct edge_table_entry
, entry
)
2657 obj
->rects
[obj
->numRects
].left
= active
->bres
.minor_axis
;
2658 obj
->rects
[obj
->numRects
].top
= y
;
2660 else if (obj
->rects
[obj
->numRects
].left
!= active
->bres
.minor_axis
)
2662 obj
->rects
[obj
->numRects
].right
= active
->bres
.minor_axis
;
2663 obj
->rects
[obj
->numRects
].bottom
= y
+ 1;
2670 next_scanline( &AET
, y
);
2674 prev_band
= REGION_Coalesce( obj
, prev_band
, cur_band
);
2675 cur_band
= obj
->numRects
;
2683 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2685 * Add a new edge to the active edge table when we
2686 * get to the next edge.
2688 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2689 REGION_loadAET(&AET
, &pSLL
->edgelist
);
2690 REGION_computeWAET( &AET
, &WETE
);
2693 pWETE
= list_head( &WETE
);
2696 * for each active edge
2698 if (!clip_rect
|| y
>= clip_rect
->top
)
2700 LIST_FOR_EACH_ENTRY( active
, &AET
, struct edge_table_entry
, entry
)
2703 * add to the buffer only those edges that
2704 * are in the Winding active edge table.
2706 if (pWETE
== &active
->winding_entry
)
2710 obj
->rects
[obj
->numRects
].left
= active
->bres
.minor_axis
;
2711 obj
->rects
[obj
->numRects
].top
= y
;
2713 else if (obj
->rects
[obj
->numRects
].left
!= active
->bres
.minor_axis
)
2715 obj
->rects
[obj
->numRects
].right
= active
->bres
.minor_axis
;
2716 obj
->rects
[obj
->numRects
].bottom
= y
+ 1;
2720 pWETE
= list_next( &WETE
, pWETE
);
2726 * recompute the winding active edge table if
2727 * we just resorted or have exited an edge.
2729 if (next_scanline( &AET
, y
)) REGION_computeWAET( &AET
, &WETE
);
2733 prev_band
= REGION_Coalesce( obj
, prev_band
, cur_band
);
2734 cur_band
= obj
->numRects
;
2739 assert( obj
->numRects
<= nb_points
/ 2 );
2743 obj
->extents
.left
= INT_MAX
;
2744 obj
->extents
.right
= INT_MIN
;
2745 obj
->extents
.top
= obj
->rects
[0].top
;
2746 obj
->extents
.bottom
= obj
->rects
[obj
->numRects
-1].bottom
;
2747 for (i
= 0; i
< obj
->numRects
; i
++)
2749 obj
->extents
.left
= min( obj
->extents
.left
, obj
->rects
[i
].left
);
2750 obj
->extents
.right
= max( obj
->extents
.right
, obj
->rects
[i
].right
);
2753 REGION_compact( obj
);
2755 if (!(hrgn
= alloc_gdi_handle( obj
, OBJ_REGION
, ®ion_funcs
)))
2759 REGION_FreeStorage(SLLBlock
.next
);
2760 HeapFree( GetProcessHeap(), 0, pETEs
);
2765 /***********************************************************************
2766 * CreatePolyPolygonRgn (GDI32.@)
2768 HRGN WINAPI
CreatePolyPolygonRgn( const POINT
*pts
, const INT
*count
, INT nbpolygons
, INT mode
)
2770 return create_polypolygon_region( pts
, count
, nbpolygons
, mode
, NULL
);
2774 /***********************************************************************
2775 * CreatePolygonRgn (GDI32.@)
2777 HRGN WINAPI
CreatePolygonRgn( const POINT
*points
, INT count
, INT mode
)
2779 return create_polypolygon_region( points
, &count
, 1, mode
, NULL
);