2 * GDI region objects. Shamelessly ripped out from the X11 distribution
3 * Thanks for the nice licence.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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...
102 #include "wine/debug.h"
104 WINE_DEFAULT_DEBUG_CHANNEL(region
);
113 /* GDI logical region object */
121 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
);
122 static BOOL
REGION_DeleteObject( HGDIOBJ handle
, void *obj
);
124 static const struct gdi_obj_funcs region_funcs
=
126 REGION_SelectObject
, /* pSelectObject */
127 NULL
, /* pGetObject16 */
128 NULL
, /* pGetObjectA */
129 NULL
, /* pGetObjectW */
130 NULL
, /* pUnrealizeObject */
131 REGION_DeleteObject
/* pDeleteObject */
134 /* 1 if two RECTs overlap.
135 * 0 if two RECTs do not overlap.
137 #define EXTENTCHECK(r1, r2) \
138 ((r1)->right > (r2)->left && \
139 (r1)->left < (r2)->right && \
140 (r1)->bottom > (r2)->top && \
141 (r1)->top < (r2)->bottom)
144 * Check to see if there is enough memory in the present region.
147 static inline int xmemcheck(WINEREGION
*reg
, LPRECT
*rect
, LPRECT
*firstrect
) {
148 if (reg
->numRects
>= (reg
->size
- 1)) {
149 *firstrect
= HeapReAlloc( GetProcessHeap(), 0, *firstrect
, (2 * (sizeof(RECT
)) * (reg
->size
)));
153 *rect
= (*firstrect
)+reg
->numRects
;
158 #define MEMCHECK(reg, rect, firstrect) xmemcheck(reg,&(rect),&(firstrect))
160 #define EMPTY_REGION(pReg) { \
161 (pReg)->numRects = 0; \
162 (pReg)->extents.left = (pReg)->extents.top = 0; \
163 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
166 #define REGION_NOT_EMPTY(pReg) pReg->numRects
168 #define INRECT(r, x, y) \
169 ( ( ((r).right > x)) && \
170 ( ((r).left <= x)) && \
171 ( ((r).bottom > y)) && \
176 * number of points to buffer before sending them off
177 * to scanlines() : Must be an even number
179 #define NUMPTSTOBUFFER 200
182 * used to allocate buffers for points and link
183 * the buffers together
186 typedef struct _POINTBLOCK
{
187 POINT pts
[NUMPTSTOBUFFER
];
188 struct _POINTBLOCK
*next
;
194 * This file contains a few macros to help track
195 * the edge of a filled object. The object is assumed
196 * to be filled in scanline order, and thus the
197 * algorithm used is an extension of Bresenham's line
198 * drawing algorithm which assumes that y is always the
200 * Since these pieces of code are the same for any filled shape,
201 * it is more convenient to gather the library in one
202 * place, but since these pieces of code are also in
203 * the inner loops of output primitives, procedure call
204 * overhead is out of the question.
205 * See the author for a derivation if needed.
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 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
229 int dx; /* local storage */ \
232 * if the edge is horizontal, then it is ignored \
233 * and assumed not to be processed. Otherwise, do this stuff. \
237 dx = (x2) - xStart; \
241 incr1 = -2 * dx + 2 * (dy) * m1; \
242 incr2 = -2 * dx + 2 * (dy) * m; \
243 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
247 incr1 = 2 * dx - 2 * (dy) * m1; \
248 incr2 = 2 * dx - 2 * (dy) * m; \
249 d = -2 * m * (dy) + 2 * dx; \
254 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
277 * This structure contains all of the information needed
278 * to run the bresenham algorithm.
279 * The variables may be hardcoded into the declarations
280 * instead of using this structure to make use of
281 * register declarations.
284 INT minor_axis
; /* minor axis */
285 INT d
; /* decision variable */
286 INT m
, m1
; /* slope and slope+1 */
287 INT incr1
, incr2
; /* error increments */
291 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
292 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
293 bres.m, bres.m1, bres.incr1, bres.incr2)
295 #define BRESINCRPGONSTRUCT(bres) \
296 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
301 * These are the data structures needed to scan
302 * convert regions. Two different scan conversion
303 * methods are available -- the even-odd method, and
304 * the winding number method.
305 * The even-odd rule states that a point is inside
306 * the polygon if a ray drawn from that point in any
307 * direction will pass through an odd number of
309 * By the winding number rule, a point is decided
310 * to be inside the polygon if a ray drawn from that
311 * point in any direction passes through a different
312 * number of clockwise and counter-clockwise path
315 * These data structures are adapted somewhat from
316 * the algorithm in (Foley/Van Dam) for scan converting
318 * The basic algorithm is to start at the top (smallest y)
319 * of the polygon, stepping down to the bottom of
320 * the polygon by incrementing the y coordinate. We
321 * keep a list of edges which the current scanline crosses,
322 * sorted by x. This list is called the Active Edge Table (AET)
323 * As we change the y-coordinate, we update each entry in
324 * in the active edge table to reflect the edges new xcoord.
325 * This list must be sorted at each scanline in case
326 * two edges intersect.
327 * We also keep a data structure known as the Edge Table (ET),
328 * which keeps track of all the edges which the current
329 * scanline has not yet reached. The ET is basically a
330 * list of ScanLineList structures containing a list of
331 * edges which are entered at a given scanline. There is one
332 * ScanLineList per scanline at which an edge is entered.
333 * When we enter a new edge, we move it from the ET to the AET.
335 * From the AET, we can implement the even-odd rule as in
337 * The winding number rule is a little trickier. We also
338 * keep the EdgeTableEntries in the AET linked by the
339 * nextWETE (winding EdgeTableEntry) link. This allows
340 * the edges to be linked just as before for updating
341 * purposes, but only uses the edges linked by the nextWETE
342 * link as edges representing spans of the polygon to
343 * drawn (as with the even-odd rule).
347 * for the winding number rule
350 #define COUNTERCLOCKWISE -1
352 typedef struct _EdgeTableEntry
{
353 INT ymax
; /* ycoord at which we exit this edge. */
354 BRESINFO bres
; /* Bresenham info to run the edge */
355 struct _EdgeTableEntry
*next
; /* next in the list */
356 struct _EdgeTableEntry
*back
; /* for insertion sort */
357 struct _EdgeTableEntry
*nextWETE
; /* for winding num rule */
358 int ClockWise
; /* flag for winding number rule */
362 typedef struct _ScanLineList
{
363 INT scanline
; /* the scanline represented */
364 EdgeTableEntry
*edgelist
; /* header node */
365 struct _ScanLineList
*next
; /* next in the list */
370 INT ymax
; /* ymax for the polygon */
371 INT ymin
; /* ymin for the polygon */
372 ScanLineList scanlines
; /* header node */
377 * Here is a struct to help with storage allocation
378 * so we can allocate a big chunk at a time, and then take
379 * pieces from this heap when we need to.
381 #define SLLSPERBLOCK 25
383 typedef struct _ScanLineListBlock
{
384 ScanLineList SLLs
[SLLSPERBLOCK
];
385 struct _ScanLineListBlock
*next
;
391 * a few macros for the inner loops of the fill code where
392 * performance considerations don't allow a procedure call.
394 * Evaluate the given edge at the given scanline.
395 * If the edge has expired, then we leave it and fix up
396 * the active edge table; otherwise, we increment the
397 * x value to be ready for the next scanline.
398 * The winding number rule is in effect, so we must notify
399 * the caller when the edge has been removed so he
400 * can reorder the Winding Active Edge Table.
402 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
403 if (pAET->ymax == y) { /* leaving this edge */ \
404 pPrevAET->next = pAET->next; \
405 pAET = pPrevAET->next; \
408 pAET->back = pPrevAET; \
411 BRESINCRPGONSTRUCT(pAET->bres); \
419 * Evaluate the given edge at the given scanline.
420 * If the edge has expired, then we leave it and fix up
421 * the active edge table; otherwise, we increment the
422 * x value to be ready for the next scanline.
423 * The even-odd rule is in effect.
425 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
426 if (pAET->ymax == y) { /* leaving this edge */ \
427 pPrevAET->next = pAET->next; \
428 pAET = pPrevAET->next; \
430 pAET->back = pPrevAET; \
433 BRESINCRPGONSTRUCT(pAET->bres); \
439 typedef void (*voidProcp
)();
441 /* Note the parameter order is different from the X11 equivalents */
443 static void REGION_CopyRegion(WINEREGION
*d
, WINEREGION
*s
);
444 static void REGION_IntersectRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
445 static void REGION_UnionRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
446 static void REGION_SubtractRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
447 static void REGION_XorRegion(WINEREGION
*d
, WINEREGION
*s1
, WINEREGION
*s2
);
448 static void REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
);
450 #define RGN_DEFAULT_RECTS 2
453 /***********************************************************************
456 inline static INT
get_region_type( const RGNOBJ
*obj
)
458 switch(obj
->rgn
->numRects
)
460 case 0: return NULLREGION
;
461 case 1: return SIMPLEREGION
;
462 default: return COMPLEXREGION
;
467 /***********************************************************************
469 * Outputs the contents of a WINEREGION
471 static void REGION_DumpRegion(WINEREGION
*pReg
)
473 RECT
*pRect
, *pRectEnd
= pReg
->rects
+ pReg
->numRects
;
475 TRACE("Region %p: %d,%d - %d,%d %d rects\n", pReg
,
476 pReg
->extents
.left
, pReg
->extents
.top
,
477 pReg
->extents
.right
, pReg
->extents
.bottom
, pReg
->numRects
);
478 for(pRect
= pReg
->rects
; pRect
< pRectEnd
; pRect
++)
479 TRACE("\t%d,%d - %d,%d\n", pRect
->left
, pRect
->top
,
480 pRect
->right
, pRect
->bottom
);
485 /***********************************************************************
486 * REGION_AllocWineRegion
487 * Create a new empty WINEREGION.
489 static WINEREGION
*REGION_AllocWineRegion( INT n
)
493 if ((pReg
= HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION
))))
495 if ((pReg
->rects
= HeapAlloc(GetProcessHeap(), 0, n
* sizeof( RECT
))))
501 HeapFree(GetProcessHeap(), 0, pReg
);
507 /***********************************************************************
508 * REGION_CreateRegion
509 * Create a new empty region.
511 static HRGN
REGION_CreateRegion( INT n
)
516 if(!(obj
= GDI_AllocObject( sizeof(RGNOBJ
), REGION_MAGIC
, &hrgn
, ®ion_funcs
))) return 0;
517 if(!(obj
->rgn
= REGION_AllocWineRegion(n
))) {
518 GDI_FreeObject( hrgn
, obj
);
521 GDI_ReleaseObj( hrgn
);
525 /***********************************************************************
526 * REGION_DestroyWineRegion
528 static void REGION_DestroyWineRegion( WINEREGION
* pReg
)
530 HeapFree( GetProcessHeap(), 0, pReg
->rects
);
531 HeapFree( GetProcessHeap(), 0, pReg
);
534 /***********************************************************************
535 * REGION_DeleteObject
537 static BOOL
REGION_DeleteObject( HGDIOBJ handle
, void *obj
)
541 TRACE(" %04x\n", handle
);
543 REGION_DestroyWineRegion( rgn
->rgn
);
544 return GDI_FreeObject( handle
, obj
);
547 /***********************************************************************
548 * REGION_SelectObject
550 static HGDIOBJ
REGION_SelectObject( HGDIOBJ handle
, void *obj
, HDC hdc
)
552 return (HGDIOBJ
)SelectClipRgn( hdc
, handle
);
555 /***********************************************************************
556 * OffsetRgn (GDI.101)
558 INT16 WINAPI
OffsetRgn16( HRGN16 hrgn
, INT16 x
, INT16 y
)
560 return OffsetRgn( hrgn
, x
, y
);
563 /***********************************************************************
564 * OffsetRgn (GDI32.@)
566 INT WINAPI
OffsetRgn( HRGN hrgn
, INT x
, INT y
)
568 RGNOBJ
* obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
);
571 TRACE("%04x %d,%d\n", hrgn
, x
, y
);
577 int nbox
= obj
->rgn
->numRects
;
578 RECT
*pbox
= obj
->rgn
->rects
;
588 obj
->rgn
->extents
.left
+= x
;
589 obj
->rgn
->extents
.right
+= x
;
590 obj
->rgn
->extents
.top
+= y
;
591 obj
->rgn
->extents
.bottom
+= y
;
594 ret
= get_region_type( obj
);
595 GDI_ReleaseObj( hrgn
);
600 /***********************************************************************
601 * GetRgnBox (GDI.134)
603 INT16 WINAPI
GetRgnBox16( HRGN16 hrgn
, LPRECT16 rect
)
606 INT16 ret
= (INT16
)GetRgnBox( hrgn
, &r
);
607 CONV_RECT32TO16( &r
, rect
);
611 /***********************************************************************
612 * GetRgnBox (GDI32.@)
614 INT WINAPI
GetRgnBox( HRGN hrgn
, LPRECT rect
)
616 RGNOBJ
* obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
);
620 TRACE(" %04x\n", hrgn
);
621 rect
->left
= obj
->rgn
->extents
.left
;
622 rect
->top
= obj
->rgn
->extents
.top
;
623 rect
->right
= obj
->rgn
->extents
.right
;
624 rect
->bottom
= obj
->rgn
->extents
.bottom
;
625 ret
= get_region_type( obj
);
626 GDI_ReleaseObj(hrgn
);
633 /***********************************************************************
634 * CreateRectRgn (GDI.64)
636 * NOTE: Doesn't call CreateRectRgn because of differences in SetRectRgn16/32
638 HRGN16 WINAPI
CreateRectRgn16(INT16 left
, INT16 top
, INT16 right
, INT16 bottom
)
642 if (!(hrgn
= (HRGN16
)REGION_CreateRegion(RGN_DEFAULT_RECTS
)))
645 SetRectRgn16(hrgn
, left
, top
, right
, bottom
);
650 /***********************************************************************
651 * CreateRectRgn (GDI32.@)
653 HRGN WINAPI
CreateRectRgn(INT left
, INT top
, INT right
, INT bottom
)
657 /* Allocate 2 rects by default to reduce the number of reallocs */
659 if (!(hrgn
= REGION_CreateRegion(RGN_DEFAULT_RECTS
)))
662 SetRectRgn(hrgn
, left
, top
, right
, bottom
);
666 /***********************************************************************
667 * CreateRectRgnIndirect (GDI.65)
669 HRGN16 WINAPI
CreateRectRgnIndirect16( const RECT16
* rect
)
671 return CreateRectRgn16( rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
675 /***********************************************************************
676 * CreateRectRgnIndirect (GDI32.@)
678 HRGN WINAPI
CreateRectRgnIndirect( const RECT
* rect
)
680 return CreateRectRgn( rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
684 /***********************************************************************
685 * SetRectRgn (GDI.172)
687 * NOTE: Win 3.1 sets region to empty if left > right
689 VOID WINAPI
SetRectRgn16( HRGN16 hrgn
, INT16 left
, INT16 top
,
690 INT16 right
, INT16 bottom
)
693 SetRectRgn( hrgn
, left
, top
, right
, bottom
);
695 SetRectRgn( hrgn
, 0, 0, 0, 0 );
699 /***********************************************************************
700 * SetRectRgn (GDI32.@)
702 * Allows either or both left and top to be greater than right or bottom.
704 BOOL WINAPI
SetRectRgn( HRGN hrgn
, INT left
, INT top
,
705 INT right
, INT bottom
)
709 TRACE(" %04x %d,%d-%d,%d\n",
710 hrgn
, left
, top
, right
, bottom
);
712 if (!(obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
))) return FALSE
;
714 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
715 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
717 if((left
!= right
) && (top
!= bottom
))
719 obj
->rgn
->rects
->left
= obj
->rgn
->extents
.left
= left
;
720 obj
->rgn
->rects
->top
= obj
->rgn
->extents
.top
= top
;
721 obj
->rgn
->rects
->right
= obj
->rgn
->extents
.right
= right
;
722 obj
->rgn
->rects
->bottom
= obj
->rgn
->extents
.bottom
= bottom
;
723 obj
->rgn
->numRects
= 1;
726 EMPTY_REGION(obj
->rgn
);
728 GDI_ReleaseObj( hrgn
);
733 /***********************************************************************
734 * CreateRoundRectRgn (GDI.444)
736 * If either ellipse dimension is zero we call CreateRectRgn16 for its
737 * `special' behaviour. -ve ellipse dimensions can result in GPFs under win3.1
738 * we just let CreateRoundRectRgn convert them to +ve values.
741 HRGN16 WINAPI
CreateRoundRectRgn16( INT16 left
, INT16 top
,
742 INT16 right
, INT16 bottom
,
743 INT16 ellipse_width
, INT16 ellipse_height
)
745 if( ellipse_width
== 0 || ellipse_height
== 0 )
746 return CreateRectRgn16( left
, top
, right
, bottom
);
748 return (HRGN16
)CreateRoundRectRgn( left
, top
, right
, bottom
,
749 ellipse_width
, ellipse_height
);
752 /***********************************************************************
753 * CreateRoundRectRgn (GDI32.@)
755 HRGN WINAPI
CreateRoundRectRgn( INT left
, INT top
,
756 INT right
, INT bottom
,
757 INT ellipse_width
, INT ellipse_height
)
761 int asq
, bsq
, d
, xd
, yd
;
764 /* Make the dimensions sensible */
766 if (left
> right
) { INT tmp
= left
; left
= right
; right
= tmp
; }
767 if (top
> bottom
) { INT tmp
= top
; top
= bottom
; bottom
= tmp
; }
769 ellipse_width
= abs(ellipse_width
);
770 ellipse_height
= abs(ellipse_height
);
772 /* Check parameters */
774 if (ellipse_width
> right
-left
) ellipse_width
= right
-left
;
775 if (ellipse_height
> bottom
-top
) ellipse_height
= bottom
-top
;
777 /* Check if we can do a normal rectangle instead */
779 if ((ellipse_width
< 2) || (ellipse_height
< 2))
780 return CreateRectRgn( left
, top
, right
, bottom
);
784 d
= (ellipse_height
< 128) ? ((3 * ellipse_height
) >> 2) : 64;
785 if (!(hrgn
= REGION_CreateRegion(d
))) return 0;
786 if (!(obj
= GDI_GetObjPtr( hrgn
, REGION_MAGIC
))) return 0;
787 TRACE("(%d,%d-%d,%d %dx%d): ret=%04x\n",
788 left
, top
, right
, bottom
, ellipse_width
, ellipse_height
, hrgn
);
790 /* Ellipse algorithm, based on an article by K. Porter */
791 /* in DDJ Graphics Programming Column, 8/89 */
793 asq
= ellipse_width
* ellipse_width
/ 4; /* a^2 */
794 bsq
= ellipse_height
* ellipse_height
/ 4; /* b^2 */
795 d
= bsq
- asq
* ellipse_height
/ 2 + asq
/ 4; /* b^2 - a^2b + a^2/4 */
797 yd
= asq
* ellipse_height
; /* 2a^2b */
799 rect
.left
= left
+ ellipse_width
/ 2;
800 rect
.right
= right
- ellipse_width
/ 2;
802 /* Loop to draw first half of quadrant */
806 if (d
> 0) /* if nearest pixel is toward the center */
808 /* move toward center */
810 rect
.bottom
= rect
.top
+ 1;
811 REGION_UnionRectWithRegion( &rect
, obj
->rgn
);
813 rect
.bottom
= rect
.top
+ 1;
814 REGION_UnionRectWithRegion( &rect
, obj
->rgn
);
818 rect
.left
--; /* next horiz point */
824 /* Loop to draw second half of quadrant */
826 d
+= (3 * (asq
-bsq
) / 2 - (xd
+yd
)) / 2;
829 /* next vertical point */
831 rect
.bottom
= rect
.top
+ 1;
832 REGION_UnionRectWithRegion( &rect
, obj
->rgn
);
834 rect
.bottom
= rect
.top
+ 1;
835 REGION_UnionRectWithRegion( &rect
, obj
->rgn
);
836 if (d
< 0) /* if nearest pixel is outside ellipse */
838 rect
.left
--; /* move away from center */
847 /* Add the inside rectangle */
852 rect
.bottom
= bottom
;
853 REGION_UnionRectWithRegion( &rect
, obj
->rgn
);
855 GDI_ReleaseObj( hrgn
);
860 /***********************************************************************
861 * CreateEllipticRgn (GDI.54)
863 HRGN16 WINAPI
CreateEllipticRgn16( INT16 left
, INT16 top
,
864 INT16 right
, INT16 bottom
)
866 return (HRGN16
)CreateRoundRectRgn( left
, top
, right
, bottom
,
867 right
-left
, bottom
-top
);
871 /***********************************************************************
872 * CreateEllipticRgn (GDI32.@)
874 HRGN WINAPI
CreateEllipticRgn( INT left
, INT top
,
875 INT right
, INT bottom
)
877 return CreateRoundRectRgn( left
, top
, right
, bottom
,
878 right
-left
, bottom
-top
);
882 /***********************************************************************
883 * CreateEllipticRgnIndirect (GDI.55)
885 HRGN16 WINAPI
CreateEllipticRgnIndirect16( const RECT16
*rect
)
887 return CreateRoundRectRgn( rect
->left
, rect
->top
, rect
->right
,
888 rect
->bottom
, rect
->right
- rect
->left
,
889 rect
->bottom
- rect
->top
);
893 /***********************************************************************
894 * CreateEllipticRgnIndirect (GDI32.@)
896 HRGN WINAPI
CreateEllipticRgnIndirect( const RECT
*rect
)
898 return CreateRoundRectRgn( rect
->left
, rect
->top
, rect
->right
,
899 rect
->bottom
, rect
->right
- rect
->left
,
900 rect
->bottom
- rect
->top
);
903 /***********************************************************************
904 * GetRegionData (GDI32.@)
906 * MSDN: GetRegionData, Return Values:
908 * "If the function succeeds and dwCount specifies an adequate number of bytes,
909 * the return value is always dwCount. If dwCount is too small or the function
910 * fails, the return value is 0. If lpRgnData is NULL, the return value is the
911 * required number of bytes.
913 * If the function fails, the return value is zero."
915 DWORD WINAPI
GetRegionData(HRGN hrgn
, DWORD count
, LPRGNDATA rgndata
)
918 RGNOBJ
*obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
);
920 TRACE(" %04x count = %ld, rgndata = %p\n",
921 hrgn
, count
, rgndata
);
925 size
= obj
->rgn
->numRects
* sizeof(RECT
);
926 if(count
< (size
+ sizeof(RGNDATAHEADER
)) || rgndata
== NULL
)
928 GDI_ReleaseObj( hrgn
);
929 if (rgndata
) /* buffer is too small, signal it by return 0 */
931 else /* user requested buffer size with rgndata NULL */
932 return size
+ sizeof(RGNDATAHEADER
);
935 rgndata
->rdh
.dwSize
= sizeof(RGNDATAHEADER
);
936 rgndata
->rdh
.iType
= RDH_RECTANGLES
;
937 rgndata
->rdh
.nCount
= obj
->rgn
->numRects
;
938 rgndata
->rdh
.nRgnSize
= size
;
939 rgndata
->rdh
.rcBound
.left
= obj
->rgn
->extents
.left
;
940 rgndata
->rdh
.rcBound
.top
= obj
->rgn
->extents
.top
;
941 rgndata
->rdh
.rcBound
.right
= obj
->rgn
->extents
.right
;
942 rgndata
->rdh
.rcBound
.bottom
= obj
->rgn
->extents
.bottom
;
944 memcpy( rgndata
->Buffer
, obj
->rgn
->rects
, size
);
946 GDI_ReleaseObj( hrgn
);
947 return size
+ sizeof(RGNDATAHEADER
);
950 /***********************************************************************
951 * GetRegionData (GDI.607)
952 * FIXME: is LPRGNDATA the same in Win16 and Win32 ?
954 DWORD WINAPI
GetRegionData16(HRGN16 hrgn
, DWORD count
, LPRGNDATA rgndata
)
956 return GetRegionData((HRGN
)hrgn
, count
, rgndata
);
959 /***********************************************************************
960 * ExtCreateRegion (GDI32.@)
963 HRGN WINAPI
ExtCreateRegion( const XFORM
* lpXform
, DWORD dwCount
, const RGNDATA
* rgndata
)
967 TRACE(" %p %ld %p = ", lpXform
, dwCount
, rgndata
);
970 WARN("(Xform not implemented - ignored)\n");
972 if( rgndata
->rdh
.iType
!= RDH_RECTANGLES
)
974 /* FIXME: We can use CreatePolyPolygonRgn() here
975 * for trapezoidal data */
977 WARN("(Unsupported region data)\n");
981 if( (hrgn
= REGION_CreateRegion( rgndata
->rdh
.nCount
)) )
983 RECT
*pCurRect
, *pEndRect
;
984 RGNOBJ
*obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
);
987 pEndRect
= (RECT
*)rgndata
->Buffer
+ rgndata
->rdh
.nCount
;
988 for(pCurRect
= (RECT
*)rgndata
->Buffer
; pCurRect
< pEndRect
; pCurRect
++)
989 REGION_UnionRectWithRegion( pCurRect
, obj
->rgn
);
990 GDI_ReleaseObj( hrgn
);
992 TRACE("%04x\n", hrgn
);
995 else ERR("Could not get pointer to newborn Region!\n");
1002 /***********************************************************************
1003 * PtInRegion (GDI.161)
1005 BOOL16 WINAPI
PtInRegion16( HRGN16 hrgn
, INT16 x
, INT16 y
)
1007 return PtInRegion( hrgn
, x
, y
);
1011 /***********************************************************************
1012 * PtInRegion (GDI32.@)
1014 BOOL WINAPI
PtInRegion( HRGN hrgn
, INT x
, INT y
)
1019 if ((obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
)))
1023 if (obj
->rgn
->numRects
> 0 && INRECT(obj
->rgn
->extents
, x
, y
))
1024 for (i
= 0; i
< obj
->rgn
->numRects
; i
++)
1025 if (INRECT (obj
->rgn
->rects
[i
], x
, y
))
1030 GDI_ReleaseObj( hrgn
);
1036 /***********************************************************************
1037 * RectInRegion (GDI.466)
1038 * RectInRegionOld (GDI.181)
1040 BOOL16 WINAPI
RectInRegion16( HRGN16 hrgn
, const RECT16
*rect
)
1044 CONV_RECT16TO32(rect
, &r32
);
1045 return (BOOL16
)RectInRegion(hrgn
, &r32
);
1049 /***********************************************************************
1050 * RectInRegion (GDI32.@)
1052 * Returns TRUE if rect is at least partly inside hrgn
1054 BOOL WINAPI
RectInRegion( HRGN hrgn
, const RECT
*rect
)
1059 if ((obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
)))
1061 RECT
*pCurRect
, *pRectEnd
;
1063 /* this is (just) a useful optimization */
1064 if ((obj
->rgn
->numRects
> 0) && EXTENTCHECK(&obj
->rgn
->extents
,
1067 for (pCurRect
= obj
->rgn
->rects
, pRectEnd
= pCurRect
+
1068 obj
->rgn
->numRects
; pCurRect
< pRectEnd
; pCurRect
++)
1070 if (pCurRect
->bottom
<= rect
->top
)
1071 continue; /* not far enough down yet */
1073 if (pCurRect
->top
>= rect
->bottom
)
1074 break; /* too far down */
1076 if (pCurRect
->right
<= rect
->left
)
1077 continue; /* not far enough over yet */
1079 if (pCurRect
->left
>= rect
->right
) {
1087 GDI_ReleaseObj(hrgn
);
1092 /***********************************************************************
1095 BOOL16 WINAPI
EqualRgn16( HRGN16 rgn1
, HRGN16 rgn2
)
1097 return EqualRgn( rgn1
, rgn2
);
1101 /***********************************************************************
1102 * EqualRgn (GDI32.@)
1104 BOOL WINAPI
EqualRgn( HRGN hrgn1
, HRGN hrgn2
)
1106 RGNOBJ
*obj1
, *obj2
;
1109 if ((obj1
= (RGNOBJ
*) GDI_GetObjPtr( hrgn1
, REGION_MAGIC
)))
1111 if ((obj2
= (RGNOBJ
*) GDI_GetObjPtr( hrgn2
, REGION_MAGIC
)))
1115 if ( obj1
->rgn
->numRects
!= obj2
->rgn
->numRects
) goto done
;
1116 if ( obj1
->rgn
->numRects
== 0 )
1122 if (obj1
->rgn
->extents
.left
!= obj2
->rgn
->extents
.left
) goto done
;
1123 if (obj1
->rgn
->extents
.right
!= obj2
->rgn
->extents
.right
) goto done
;
1124 if (obj1
->rgn
->extents
.top
!= obj2
->rgn
->extents
.top
) goto done
;
1125 if (obj1
->rgn
->extents
.bottom
!= obj2
->rgn
->extents
.bottom
) goto done
;
1126 for( i
= 0; i
< obj1
->rgn
->numRects
; i
++ )
1128 if (obj1
->rgn
->rects
[i
].left
!= obj2
->rgn
->rects
[i
].left
) goto done
;
1129 if (obj1
->rgn
->rects
[i
].right
!= obj2
->rgn
->rects
[i
].right
) goto done
;
1130 if (obj1
->rgn
->rects
[i
].top
!= obj2
->rgn
->rects
[i
].top
) goto done
;
1131 if (obj1
->rgn
->rects
[i
].bottom
!= obj2
->rgn
->rects
[i
].bottom
) goto done
;
1135 GDI_ReleaseObj(hrgn2
);
1137 GDI_ReleaseObj(hrgn1
);
1142 /***********************************************************************
1143 * REGION_UnionRectWithRegion
1144 * Adds a rectangle to a WINEREGION
1146 static void REGION_UnionRectWithRegion(const RECT
*rect
, WINEREGION
*rgn
)
1150 region
.rects
= ®ion
.extents
;
1151 region
.numRects
= 1;
1153 region
.extents
= *rect
;
1154 REGION_UnionRegion(rgn
, rgn
, ®ion
);
1158 /***********************************************************************
1159 * REGION_CreateFrameRgn
1161 * Create a region that is a frame around another region.
1162 * Expand all rectangles by +/- x and y, then subtract original region.
1164 BOOL
REGION_FrameRgn( HRGN hDest
, HRGN hSrc
, INT x
, INT y
)
1167 RGNOBJ
*srcObj
= (RGNOBJ
*) GDI_GetObjPtr( hSrc
, REGION_MAGIC
);
1169 if (!srcObj
) return FALSE
;
1170 if (srcObj
->rgn
->numRects
!= 0)
1172 RGNOBJ
* destObj
= (RGNOBJ
*) GDI_GetObjPtr( hDest
, REGION_MAGIC
);
1173 RECT
*pRect
, *pEndRect
;
1176 EMPTY_REGION( destObj
->rgn
);
1178 pEndRect
= srcObj
->rgn
->rects
+ srcObj
->rgn
->numRects
;
1179 for(pRect
= srcObj
->rgn
->rects
; pRect
< pEndRect
; pRect
++)
1181 tempRect
.left
= pRect
->left
- x
;
1182 tempRect
.top
= pRect
->top
- y
;
1183 tempRect
.right
= pRect
->right
+ x
;
1184 tempRect
.bottom
= pRect
->bottom
+ y
;
1185 REGION_UnionRectWithRegion( &tempRect
, destObj
->rgn
);
1187 REGION_SubtractRegion( destObj
->rgn
, destObj
->rgn
, srcObj
->rgn
);
1188 GDI_ReleaseObj ( hDest
);
1193 GDI_ReleaseObj( hSrc
);
1198 /***********************************************************************
1199 * CombineRgn (GDI.47)
1201 INT16 WINAPI
CombineRgn16(HRGN16 hDest
, HRGN16 hSrc1
, HRGN16 hSrc2
, INT16 mode
)
1203 return (INT16
)CombineRgn( hDest
, hSrc1
, hSrc2
, mode
);
1207 /***********************************************************************
1208 * CombineRgn (GDI32.@)
1210 * Note: The behavior is correct even if src and dest regions are the same.
1212 INT WINAPI
CombineRgn(HRGN hDest
, HRGN hSrc1
, HRGN hSrc2
, INT mode
)
1214 RGNOBJ
*destObj
= (RGNOBJ
*) GDI_GetObjPtr( hDest
, REGION_MAGIC
);
1217 TRACE(" %04x,%04x -> %04x mode=%x\n",
1218 hSrc1
, hSrc2
, hDest
, mode
);
1221 RGNOBJ
*src1Obj
= (RGNOBJ
*) GDI_GetObjPtr( hSrc1
, REGION_MAGIC
);
1225 TRACE("dump src1Obj:\n");
1226 if(TRACE_ON(region
))
1227 REGION_DumpRegion(src1Obj
->rgn
);
1228 if (mode
== RGN_COPY
)
1230 REGION_CopyRegion( destObj
->rgn
, src1Obj
->rgn
);
1231 result
= get_region_type( destObj
);
1235 RGNOBJ
*src2Obj
= (RGNOBJ
*) GDI_GetObjPtr( hSrc2
, REGION_MAGIC
);
1239 TRACE("dump src2Obj:\n");
1240 if(TRACE_ON(region
))
1241 REGION_DumpRegion(src2Obj
->rgn
);
1245 REGION_IntersectRegion( destObj
->rgn
, src1Obj
->rgn
, src2Obj
->rgn
);
1248 REGION_UnionRegion( destObj
->rgn
, src1Obj
->rgn
, src2Obj
->rgn
);
1251 REGION_XorRegion( destObj
->rgn
, src1Obj
->rgn
, src2Obj
->rgn
);
1254 REGION_SubtractRegion( destObj
->rgn
, src1Obj
->rgn
, src2Obj
->rgn
);
1257 result
= get_region_type( destObj
);
1258 GDI_ReleaseObj( hSrc2
);
1261 GDI_ReleaseObj( hSrc1
);
1263 TRACE("dump destObj:\n");
1264 if(TRACE_ON(region
))
1265 REGION_DumpRegion(destObj
->rgn
);
1267 GDI_ReleaseObj( hDest
);
1269 ERR("Invalid rgn=%04x\n", hDest
);
1274 /***********************************************************************
1276 * Re-calculate the extents of a region
1278 static void REGION_SetExtents (WINEREGION
*pReg
)
1280 RECT
*pRect
, *pRectEnd
, *pExtents
;
1282 if (pReg
->numRects
== 0)
1284 pReg
->extents
.left
= 0;
1285 pReg
->extents
.top
= 0;
1286 pReg
->extents
.right
= 0;
1287 pReg
->extents
.bottom
= 0;
1291 pExtents
= &pReg
->extents
;
1292 pRect
= pReg
->rects
;
1293 pRectEnd
= &pRect
[pReg
->numRects
- 1];
1296 * Since pRect is the first rectangle in the region, it must have the
1297 * smallest top and since pRectEnd is the last rectangle in the region,
1298 * it must have the largest bottom, because of banding. Initialize left and
1299 * right from pRect and pRectEnd, resp., as good things to initialize them
1302 pExtents
->left
= pRect
->left
;
1303 pExtents
->top
= pRect
->top
;
1304 pExtents
->right
= pRectEnd
->right
;
1305 pExtents
->bottom
= pRectEnd
->bottom
;
1307 while (pRect
<= pRectEnd
)
1309 if (pRect
->left
< pExtents
->left
)
1310 pExtents
->left
= pRect
->left
;
1311 if (pRect
->right
> pExtents
->right
)
1312 pExtents
->right
= pRect
->right
;
1317 /***********************************************************************
1320 static void REGION_CopyRegion(WINEREGION
*dst
, WINEREGION
*src
)
1322 if (dst
!= src
) /* don't want to copy to itself */
1324 if (dst
->size
< src
->numRects
)
1326 if (! (dst
->rects
= HeapReAlloc( GetProcessHeap(), 0, dst
->rects
,
1327 src
->numRects
* sizeof(RECT
) )))
1329 dst
->size
= src
->numRects
;
1331 dst
->numRects
= src
->numRects
;
1332 dst
->extents
.left
= src
->extents
.left
;
1333 dst
->extents
.top
= src
->extents
.top
;
1334 dst
->extents
.right
= src
->extents
.right
;
1335 dst
->extents
.bottom
= src
->extents
.bottom
;
1336 memcpy((char *) dst
->rects
, (char *) src
->rects
,
1337 (int) (src
->numRects
* sizeof(RECT
)));
1342 /***********************************************************************
1345 * Attempt to merge the rects in the current band with those in the
1346 * previous one. Used only by REGION_RegionOp.
1349 * The new index for the previous band.
1352 * If coalescing takes place:
1353 * - rectangles in the previous band will have their bottom fields
1355 * - pReg->numRects will be decreased.
1358 static INT
REGION_Coalesce (
1359 WINEREGION
*pReg
, /* Region to coalesce */
1360 INT prevStart
, /* Index of start of previous band */
1361 INT curStart
/* Index of start of current band */
1363 RECT
*pPrevRect
; /* Current rect in previous band */
1364 RECT
*pCurRect
; /* Current rect in current band */
1365 RECT
*pRegEnd
; /* End of region */
1366 INT curNumRects
; /* Number of rectangles in current band */
1367 INT prevNumRects
; /* Number of rectangles in previous band */
1368 INT bandtop
; /* top coordinate for current band */
1370 pRegEnd
= &pReg
->rects
[pReg
->numRects
];
1372 pPrevRect
= &pReg
->rects
[prevStart
];
1373 prevNumRects
= curStart
- prevStart
;
1376 * Figure out how many rectangles are in the current band. Have to do
1377 * this because multiple bands could have been added in REGION_RegionOp
1378 * at the end when one region has been exhausted.
1380 pCurRect
= &pReg
->rects
[curStart
];
1381 bandtop
= pCurRect
->top
;
1382 for (curNumRects
= 0;
1383 (pCurRect
!= pRegEnd
) && (pCurRect
->top
== bandtop
);
1389 if (pCurRect
!= pRegEnd
)
1392 * If more than one band was added, we have to find the start
1393 * of the last band added so the next coalescing job can start
1394 * at the right place... (given when multiple bands are added,
1395 * this may be pointless -- see above).
1398 while (pRegEnd
[-1].top
== pRegEnd
->top
)
1402 curStart
= pRegEnd
- pReg
->rects
;
1403 pRegEnd
= pReg
->rects
+ pReg
->numRects
;
1406 if ((curNumRects
== prevNumRects
) && (curNumRects
!= 0)) {
1407 pCurRect
-= curNumRects
;
1409 * The bands may only be coalesced if the bottom of the previous
1410 * matches the top scanline of the current.
1412 if (pPrevRect
->bottom
== pCurRect
->top
)
1415 * Make sure the bands have rects in the same places. This
1416 * assumes that rects have been added in such a way that they
1417 * cover the most area possible. I.e. two rects in a band must
1418 * have some horizontal space between them.
1422 if ((pPrevRect
->left
!= pCurRect
->left
) ||
1423 (pPrevRect
->right
!= pCurRect
->right
))
1426 * The bands don't line up so they can't be coalesced.
1433 } while (prevNumRects
!= 0);
1435 pReg
->numRects
-= curNumRects
;
1436 pCurRect
-= curNumRects
;
1437 pPrevRect
-= curNumRects
;
1440 * The bands may be merged, so set the bottom of each rect
1441 * in the previous band to that of the corresponding rect in
1446 pPrevRect
->bottom
= pCurRect
->bottom
;
1450 } while (curNumRects
!= 0);
1453 * If only one band was added to the region, we have to backup
1454 * curStart to the start of the previous band.
1456 * If more than one band was added to the region, copy the
1457 * other bands down. The assumption here is that the other bands
1458 * came from the same region as the current one and no further
1459 * coalescing can be done on them since it's all been done
1460 * already... curStart is already in the right place.
1462 if (pCurRect
== pRegEnd
)
1464 curStart
= prevStart
;
1470 *pPrevRect
++ = *pCurRect
++;
1471 } while (pCurRect
!= pRegEnd
);
1479 /***********************************************************************
1482 * Apply an operation to two regions. Called by REGION_Union,
1483 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1489 * The new region is overwritten.
1492 * The idea behind this function is to view the two regions as sets.
1493 * Together they cover a rectangle of area that this function divides
1494 * into horizontal bands where points are covered only by one region
1495 * or by both. For the first case, the nonOverlapFunc is called with
1496 * each the band and the band's upper and lower extents. For the
1497 * second, the overlapFunc is called to process the entire band. It
1498 * is responsible for clipping the rectangles in the band, though
1499 * this function provides the boundaries.
1500 * At the end of each band, the new region is coalesced, if possible,
1501 * to reduce the number of rectangles in the region.
1504 static void REGION_RegionOp(
1505 WINEREGION
*newReg
, /* Place to store result */
1506 WINEREGION
*reg1
, /* First region in operation */
1507 WINEREGION
*reg2
, /* 2nd region in operation */
1508 void (*overlapFunc
)(), /* Function to call for over-lapping bands */
1509 void (*nonOverlap1Func
)(), /* Function to call for non-overlapping bands in region 1 */
1510 void (*nonOverlap2Func
)() /* Function to call for non-overlapping bands in region 2 */
1512 RECT
*r1
; /* Pointer into first region */
1513 RECT
*r2
; /* Pointer into 2d region */
1514 RECT
*r1End
; /* End of 1st region */
1515 RECT
*r2End
; /* End of 2d region */
1516 INT ybot
; /* Bottom of intersection */
1517 INT ytop
; /* Top of intersection */
1518 RECT
*oldRects
; /* Old rects for newReg */
1519 INT prevBand
; /* Index of start of
1520 * previous band in newReg */
1521 INT curBand
; /* Index of start of current
1523 RECT
*r1BandEnd
; /* End of current band in r1 */
1524 RECT
*r2BandEnd
; /* End of current band in r2 */
1525 INT top
; /* Top of non-overlapping band */
1526 INT bot
; /* Bottom of non-overlapping band */
1530 * set r1, r2, r1End and r2End appropriately, preserve the important
1531 * parts of the destination region until the end in case it's one of
1532 * the two source regions, then mark the "new" region empty, allocating
1533 * another array of rectangles for it to use.
1537 r1End
= r1
+ reg1
->numRects
;
1538 r2End
= r2
+ reg2
->numRects
;
1542 * newReg may be one of the src regions so we can't empty it. We keep a
1543 * note of its rects pointer (so that we can free them later), preserve its
1544 * extents and simply set numRects to zero.
1547 oldRects
= newReg
->rects
;
1548 newReg
->numRects
= 0;
1551 * Allocate a reasonable number of rectangles for the new region. The idea
1552 * is to allocate enough so the individual functions don't need to
1553 * reallocate and copy the array, which is time consuming, yet we don't
1554 * have to worry about using too much memory. I hope to be able to
1555 * nuke the Xrealloc() at the end of this function eventually.
1557 newReg
->size
= max(reg1
->numRects
,reg2
->numRects
) * 2;
1559 if (! (newReg
->rects
= HeapAlloc( GetProcessHeap(), 0,
1560 sizeof(RECT
) * newReg
->size
)))
1567 * Initialize ybot and ytop.
1568 * In the upcoming loop, ybot and ytop serve different functions depending
1569 * on whether the band being handled is an overlapping or non-overlapping
1571 * In the case of a non-overlapping band (only one of the regions
1572 * has points in the band), ybot is the bottom of the most recent
1573 * intersection and thus clips the top of the rectangles in that band.
1574 * ytop is the top of the next intersection between the two regions and
1575 * serves to clip the bottom of the rectangles in the current band.
1576 * For an overlapping band (where the two regions intersect), ytop clips
1577 * the top of the rectangles of both regions and ybot clips the bottoms.
1579 if (reg1
->extents
.top
< reg2
->extents
.top
)
1580 ybot
= reg1
->extents
.top
;
1582 ybot
= reg2
->extents
.top
;
1585 * prevBand serves to mark the start of the previous band so rectangles
1586 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1587 * In the beginning, there is no previous band, so prevBand == curBand
1588 * (curBand is set later on, of course, but the first band will always
1589 * start at index 0). prevBand and curBand must be indices because of
1590 * the possible expansion, and resultant moving, of the new region's
1591 * array of rectangles.
1597 curBand
= newReg
->numRects
;
1600 * This algorithm proceeds one source-band (as opposed to a
1601 * destination band, which is determined by where the two regions
1602 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1603 * rectangle after the last one in the current band for their
1604 * respective regions.
1607 while ((r1BandEnd
!= r1End
) && (r1BandEnd
->top
== r1
->top
))
1613 while ((r2BandEnd
!= r2End
) && (r2BandEnd
->top
== r2
->top
))
1619 * First handle the band that doesn't intersect, if any.
1621 * Note that attention is restricted to one band in the
1622 * non-intersecting region at once, so if a region has n
1623 * bands between the current position and the next place it overlaps
1624 * the other, this entire loop will be passed through n times.
1626 if (r1
->top
< r2
->top
)
1628 top
= max(r1
->top
,ybot
);
1629 bot
= min(r1
->bottom
,r2
->top
);
1631 if ((top
!= bot
) && (nonOverlap1Func
!= (void (*)())NULL
))
1633 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
, top
, bot
);
1638 else if (r2
->top
< r1
->top
)
1640 top
= max(r2
->top
,ybot
);
1641 bot
= min(r2
->bottom
,r1
->top
);
1643 if ((top
!= bot
) && (nonOverlap2Func
!= (void (*)())NULL
))
1645 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
, top
, bot
);
1656 * If any rectangles got added to the region, try and coalesce them
1657 * with rectangles from the previous band. Note we could just do
1658 * this test in miCoalesce, but some machines incur a not
1659 * inconsiderable cost for function calls, so...
1661 if (newReg
->numRects
!= curBand
)
1663 prevBand
= REGION_Coalesce (newReg
, prevBand
, curBand
);
1667 * Now see if we've hit an intersecting band. The two bands only
1668 * intersect if ybot > ytop
1670 ybot
= min(r1
->bottom
, r2
->bottom
);
1671 curBand
= newReg
->numRects
;
1674 (* overlapFunc
) (newReg
, r1
, r1BandEnd
, r2
, r2BandEnd
, ytop
, ybot
);
1678 if (newReg
->numRects
!= curBand
)
1680 prevBand
= REGION_Coalesce (newReg
, prevBand
, curBand
);
1684 * If we've finished with a band (bottom == ybot) we skip forward
1685 * in the region to the next band.
1687 if (r1
->bottom
== ybot
)
1691 if (r2
->bottom
== ybot
)
1695 } while ((r1
!= r1End
) && (r2
!= r2End
));
1698 * Deal with whichever region still has rectangles left.
1700 curBand
= newReg
->numRects
;
1703 if (nonOverlap1Func
!= (void (*)())NULL
)
1708 while ((r1BandEnd
< r1End
) && (r1BandEnd
->top
== r1
->top
))
1712 (* nonOverlap1Func
) (newReg
, r1
, r1BandEnd
,
1713 max(r1
->top
,ybot
), r1
->bottom
);
1715 } while (r1
!= r1End
);
1718 else if ((r2
!= r2End
) && (nonOverlap2Func
!= (void (*)())NULL
))
1723 while ((r2BandEnd
< r2End
) && (r2BandEnd
->top
== r2
->top
))
1727 (* nonOverlap2Func
) (newReg
, r2
, r2BandEnd
,
1728 max(r2
->top
,ybot
), r2
->bottom
);
1730 } while (r2
!= r2End
);
1733 if (newReg
->numRects
!= curBand
)
1735 (void) REGION_Coalesce (newReg
, prevBand
, curBand
);
1739 * A bit of cleanup. To keep regions from growing without bound,
1740 * we shrink the array of rectangles to match the new number of
1741 * rectangles in the region. This never goes to 0, however...
1743 * Only do this stuff if the number of rectangles allocated is more than
1744 * twice the number of rectangles in the region (a simple optimization...).
1746 if ((newReg
->numRects
< (newReg
->size
>> 1)) && (newReg
->numRects
> 2))
1748 if (REGION_NOT_EMPTY(newReg
))
1750 RECT
*prev_rects
= newReg
->rects
;
1751 newReg
->size
= newReg
->numRects
;
1752 newReg
->rects
= HeapReAlloc( GetProcessHeap(), 0, newReg
->rects
,
1753 sizeof(RECT
) * newReg
->size
);
1754 if (! newReg
->rects
)
1755 newReg
->rects
= prev_rects
;
1760 * No point in doing the extra work involved in an Xrealloc if
1761 * the region is empty
1764 HeapFree( GetProcessHeap(), 0, newReg
->rects
);
1765 newReg
->rects
= HeapAlloc( GetProcessHeap(), 0, sizeof(RECT
) );
1768 HeapFree( GetProcessHeap(), 0, oldRects
);
1772 /***********************************************************************
1773 * Region Intersection
1774 ***********************************************************************/
1777 /***********************************************************************
1780 * Handle an overlapping band for REGION_Intersect.
1786 * Rectangles may be added to the region.
1789 static void REGION_IntersectO(WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
1790 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
1796 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1798 while ((r1
!= r1End
) && (r2
!= r2End
))
1800 left
= max(r1
->left
, r2
->left
);
1801 right
= min(r1
->right
, r2
->right
);
1804 * If there's any overlap between the two rectangles, add that
1805 * overlap to the new region.
1806 * There's no need to check for subsumption because the only way
1807 * such a need could arise is if some region has two rectangles
1808 * right next to each other. Since that should never happen...
1812 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1813 pNextRect
->left
= left
;
1814 pNextRect
->top
= top
;
1815 pNextRect
->right
= right
;
1816 pNextRect
->bottom
= bottom
;
1817 pReg
->numRects
+= 1;
1822 * Need to advance the pointers. Shift the one that extends
1823 * to the right the least, since the other still has a chance to
1824 * overlap with that region's next rectangle, if you see what I mean.
1826 if (r1
->right
< r2
->right
)
1830 else if (r2
->right
< r1
->right
)
1843 /***********************************************************************
1844 * REGION_IntersectRegion
1846 static void REGION_IntersectRegion(WINEREGION
*newReg
, WINEREGION
*reg1
,
1849 /* check for trivial reject */
1850 if ( (!(reg1
->numRects
)) || (!(reg2
->numRects
)) ||
1851 (!EXTENTCHECK(®1
->extents
, ®2
->extents
)))
1852 newReg
->numRects
= 0;
1854 REGION_RegionOp (newReg
, reg1
, reg2
,
1855 (voidProcp
) REGION_IntersectO
, (voidProcp
) NULL
, (voidProcp
) NULL
);
1858 * Can't alter newReg's extents before we call miRegionOp because
1859 * it might be one of the source regions and miRegionOp depends
1860 * on the extents of those regions being the same. Besides, this
1861 * way there's no checking against rectangles that will be nuked
1862 * due to coalescing, so we have to examine fewer rectangles.
1864 REGION_SetExtents(newReg
);
1867 /***********************************************************************
1869 ***********************************************************************/
1871 /***********************************************************************
1874 * Handle a non-overlapping band for the union operation. Just
1875 * Adds the rectangles into the region. Doesn't have to check for
1876 * subsumption or anything.
1882 * pReg->numRects is incremented and the final rectangles overwritten
1883 * with the rectangles we're passed.
1886 static void REGION_UnionNonO (WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
,
1887 INT top
, INT bottom
)
1891 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1895 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
1896 pNextRect
->left
= r
->left
;
1897 pNextRect
->top
= top
;
1898 pNextRect
->right
= r
->right
;
1899 pNextRect
->bottom
= bottom
;
1900 pReg
->numRects
+= 1;
1907 /***********************************************************************
1910 * Handle an overlapping band for the union operation. Picks the
1911 * left-most rectangle each time and merges it into the region.
1917 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1921 static void REGION_UnionO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
1922 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
1926 pNextRect
= &pReg
->rects
[pReg
->numRects
];
1928 #define MERGERECT(r) \
1929 if ((pReg->numRects != 0) && \
1930 (pNextRect[-1].top == top) && \
1931 (pNextRect[-1].bottom == bottom) && \
1932 (pNextRect[-1].right >= r->left)) \
1934 if (pNextRect[-1].right < r->right) \
1936 pNextRect[-1].right = r->right; \
1941 MEMCHECK(pReg, pNextRect, pReg->rects); \
1942 pNextRect->top = top; \
1943 pNextRect->bottom = bottom; \
1944 pNextRect->left = r->left; \
1945 pNextRect->right = r->right; \
1946 pReg->numRects += 1; \
1951 while ((r1
!= r1End
) && (r2
!= r2End
))
1953 if (r1
->left
< r2
->left
)
1968 } while (r1
!= r1End
);
1970 else while (r2
!= r2End
)
1977 /***********************************************************************
1978 * REGION_UnionRegion
1980 static void REGION_UnionRegion(WINEREGION
*newReg
, WINEREGION
*reg1
,
1983 /* checks all the simple cases */
1986 * Region 1 and 2 are the same or region 1 is empty
1988 if ( (reg1
== reg2
) || (!(reg1
->numRects
)) )
1991 REGION_CopyRegion(newReg
, reg2
);
1996 * if nothing to union (region 2 empty)
1998 if (!(reg2
->numRects
))
2001 REGION_CopyRegion(newReg
, reg1
);
2006 * Region 1 completely subsumes region 2
2008 if ((reg1
->numRects
== 1) &&
2009 (reg1
->extents
.left
<= reg2
->extents
.left
) &&
2010 (reg1
->extents
.top
<= reg2
->extents
.top
) &&
2011 (reg1
->extents
.right
>= reg2
->extents
.right
) &&
2012 (reg1
->extents
.bottom
>= reg2
->extents
.bottom
))
2015 REGION_CopyRegion(newReg
, reg1
);
2020 * Region 2 completely subsumes region 1
2022 if ((reg2
->numRects
== 1) &&
2023 (reg2
->extents
.left
<= reg1
->extents
.left
) &&
2024 (reg2
->extents
.top
<= reg1
->extents
.top
) &&
2025 (reg2
->extents
.right
>= reg1
->extents
.right
) &&
2026 (reg2
->extents
.bottom
>= reg1
->extents
.bottom
))
2029 REGION_CopyRegion(newReg
, reg2
);
2033 REGION_RegionOp (newReg
, reg1
, reg2
, (voidProcp
) REGION_UnionO
,
2034 (voidProcp
) REGION_UnionNonO
, (voidProcp
) REGION_UnionNonO
);
2036 newReg
->extents
.left
= min(reg1
->extents
.left
, reg2
->extents
.left
);
2037 newReg
->extents
.top
= min(reg1
->extents
.top
, reg2
->extents
.top
);
2038 newReg
->extents
.right
= max(reg1
->extents
.right
, reg2
->extents
.right
);
2039 newReg
->extents
.bottom
= max(reg1
->extents
.bottom
, reg2
->extents
.bottom
);
2042 /***********************************************************************
2043 * Region Subtraction
2044 ***********************************************************************/
2046 /***********************************************************************
2047 * REGION_SubtractNonO1
2049 * Deal with non-overlapping band for subtraction. Any parts from
2050 * region 2 we discard. Anything from region 1 we add to the region.
2056 * pReg may be affected.
2059 static void REGION_SubtractNonO1 (WINEREGION
*pReg
, RECT
*r
, RECT
*rEnd
,
2060 INT top
, INT bottom
)
2064 pNextRect
= &pReg
->rects
[pReg
->numRects
];
2068 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
2069 pNextRect
->left
= r
->left
;
2070 pNextRect
->top
= top
;
2071 pNextRect
->right
= r
->right
;
2072 pNextRect
->bottom
= bottom
;
2073 pReg
->numRects
+= 1;
2081 /***********************************************************************
2084 * Overlapping band subtraction. x1 is the left-most point not yet
2091 * pReg may have rectangles added to it.
2094 static void REGION_SubtractO (WINEREGION
*pReg
, RECT
*r1
, RECT
*r1End
,
2095 RECT
*r2
, RECT
*r2End
, INT top
, INT bottom
)
2101 pNextRect
= &pReg
->rects
[pReg
->numRects
];
2103 while ((r1
!= r1End
) && (r2
!= r2End
))
2105 if (r2
->right
<= left
)
2108 * Subtrahend missed the boat: go to next subtrahend.
2112 else if (r2
->left
<= left
)
2115 * Subtrahend preceeds minuend: nuke left edge of minuend.
2118 if (left
>= r1
->right
)
2121 * Minuend completely covered: advance to next minuend and
2122 * reset left fence to edge of new minuend.
2131 * Subtrahend now used up since it doesn't extend beyond
2137 else if (r2
->left
< r1
->right
)
2140 * Left part of subtrahend covers part of minuend: add uncovered
2141 * part of minuend to region and skip to next subtrahend.
2143 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
2144 pNextRect
->left
= left
;
2145 pNextRect
->top
= top
;
2146 pNextRect
->right
= r2
->left
;
2147 pNextRect
->bottom
= bottom
;
2148 pReg
->numRects
+= 1;
2151 if (left
>= r1
->right
)
2154 * Minuend used up: advance to new...
2163 * Subtrahend used up
2171 * Minuend used up: add any remaining piece before advancing.
2173 if (r1
->right
> left
)
2175 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
2176 pNextRect
->left
= left
;
2177 pNextRect
->top
= top
;
2178 pNextRect
->right
= r1
->right
;
2179 pNextRect
->bottom
= bottom
;
2180 pReg
->numRects
+= 1;
2189 * Add remaining minuend rectangles to region.
2193 MEMCHECK(pReg
, pNextRect
, pReg
->rects
);
2194 pNextRect
->left
= left
;
2195 pNextRect
->top
= top
;
2196 pNextRect
->right
= r1
->right
;
2197 pNextRect
->bottom
= bottom
;
2198 pReg
->numRects
+= 1;
2209 /***********************************************************************
2210 * REGION_SubtractRegion
2212 * Subtract regS from regM and leave the result in regD.
2213 * S stands for subtrahend, M for minuend and D for difference.
2219 * regD is overwritten.
2222 static void REGION_SubtractRegion(WINEREGION
*regD
, WINEREGION
*regM
,
2225 /* check for trivial reject */
2226 if ( (!(regM
->numRects
)) || (!(regS
->numRects
)) ||
2227 (!EXTENTCHECK(®M
->extents
, ®S
->extents
)) )
2229 REGION_CopyRegion(regD
, regM
);
2233 REGION_RegionOp (regD
, regM
, regS
, (voidProcp
) REGION_SubtractO
,
2234 (voidProcp
) REGION_SubtractNonO1
, (voidProcp
) NULL
);
2237 * Can't alter newReg's extents before we call miRegionOp because
2238 * it might be one of the source regions and miRegionOp depends
2239 * on the extents of those regions being the unaltered. Besides, this
2240 * way there's no checking against rectangles that will be nuked
2241 * due to coalescing, so we have to examine fewer rectangles.
2243 REGION_SetExtents (regD
);
2246 /***********************************************************************
2249 static void REGION_XorRegion(WINEREGION
*dr
, WINEREGION
*sra
,
2252 WINEREGION
*tra
, *trb
;
2254 if ((! (tra
= REGION_AllocWineRegion(sra
->numRects
+ 1))) ||
2255 (! (trb
= REGION_AllocWineRegion(srb
->numRects
+ 1))))
2257 REGION_SubtractRegion(tra
,sra
,srb
);
2258 REGION_SubtractRegion(trb
,srb
,sra
);
2259 REGION_UnionRegion(dr
,tra
,trb
);
2260 REGION_DestroyWineRegion(tra
);
2261 REGION_DestroyWineRegion(trb
);
2265 /**************************************************************************
2269 *************************************************************************/
2271 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2272 #define SMALL_COORDINATE 0x80000000
2274 /***********************************************************************
2275 * REGION_InsertEdgeInET
2277 * Insert the given edge into the edge table.
2278 * First we must find the correct bucket in the
2279 * Edge table, then find the right slot in the
2280 * bucket. Finally, we can insert it.
2283 static void REGION_InsertEdgeInET(EdgeTable
*ET
, EdgeTableEntry
*ETE
,
2284 INT scanline
, ScanLineListBlock
**SLLBlock
, INT
*iSLLBlock
)
2287 EdgeTableEntry
*start
, *prev
;
2288 ScanLineList
*pSLL
, *pPrevSLL
;
2289 ScanLineListBlock
*tmpSLLBlock
;
2292 * find the right bucket to put the edge into
2294 pPrevSLL
= &ET
->scanlines
;
2295 pSLL
= pPrevSLL
->next
;
2296 while (pSLL
&& (pSLL
->scanline
< scanline
))
2303 * reassign pSLL (pointer to ScanLineList) if necessary
2305 if ((!pSLL
) || (pSLL
->scanline
> scanline
))
2307 if (*iSLLBlock
> SLLSPERBLOCK
-1)
2309 tmpSLLBlock
= HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock
));
2312 WARN("Can't alloc SLLB\n");
2315 (*SLLBlock
)->next
= tmpSLLBlock
;
2316 tmpSLLBlock
->next
= (ScanLineListBlock
*)NULL
;
2317 *SLLBlock
= tmpSLLBlock
;
2320 pSLL
= &((*SLLBlock
)->SLLs
[(*iSLLBlock
)++]);
2322 pSLL
->next
= pPrevSLL
->next
;
2323 pSLL
->edgelist
= (EdgeTableEntry
*)NULL
;
2324 pPrevSLL
->next
= pSLL
;
2326 pSLL
->scanline
= scanline
;
2329 * now insert the edge in the right bucket
2331 prev
= (EdgeTableEntry
*)NULL
;
2332 start
= pSLL
->edgelist
;
2333 while (start
&& (start
->bres
.minor_axis
< ETE
->bres
.minor_axis
))
2336 start
= start
->next
;
2343 pSLL
->edgelist
= ETE
;
2346 /***********************************************************************
2347 * REGION_CreateEdgeTable
2349 * This routine creates the edge table for
2350 * scan converting polygons.
2351 * The Edge Table (ET) looks like:
2355 * | ymax | ScanLineLists
2356 * |scanline|-->------------>-------------->...
2357 * -------- |scanline| |scanline|
2358 * |edgelist| |edgelist|
2359 * --------- ---------
2363 * list of ETEs list of ETEs
2365 * where ETE is an EdgeTableEntry data structure,
2366 * and there is one ScanLineList per scanline at
2367 * which an edge is initially entered.
2370 static void REGION_CreateETandAET(const INT
*Count
, INT nbpolygons
,
2371 const POINT
*pts
, EdgeTable
*ET
, EdgeTableEntry
*AET
,
2372 EdgeTableEntry
*pETEs
, ScanLineListBlock
*pSLLBlock
)
2374 const POINT
*top
, *bottom
;
2375 const POINT
*PrevPt
, *CurrPt
, *EndPt
;
2382 * initialize the Active Edge Table
2384 AET
->next
= (EdgeTableEntry
*)NULL
;
2385 AET
->back
= (EdgeTableEntry
*)NULL
;
2386 AET
->nextWETE
= (EdgeTableEntry
*)NULL
;
2387 AET
->bres
.minor_axis
= SMALL_COORDINATE
;
2390 * initialize the Edge Table.
2392 ET
->scanlines
.next
= (ScanLineList
*)NULL
;
2393 ET
->ymax
= SMALL_COORDINATE
;
2394 ET
->ymin
= LARGE_COORDINATE
;
2395 pSLLBlock
->next
= (ScanLineListBlock
*)NULL
;
2398 for(poly
= 0; poly
< nbpolygons
; poly
++)
2400 count
= Count
[poly
];
2408 * for each vertex in the array of points.
2409 * In this loop we are dealing with two vertices at
2410 * a time -- these make up one edge of the polygon.
2417 * find out which point is above and which is below.
2419 if (PrevPt
->y
> CurrPt
->y
)
2421 bottom
= PrevPt
, top
= CurrPt
;
2422 pETEs
->ClockWise
= 0;
2426 bottom
= CurrPt
, top
= PrevPt
;
2427 pETEs
->ClockWise
= 1;
2431 * don't add horizontal edges to the Edge table.
2433 if (bottom
->y
!= top
->y
)
2435 pETEs
->ymax
= bottom
->y
-1;
2436 /* -1 so we don't get last scanline */
2439 * initialize integer edge algorithm
2441 dy
= bottom
->y
- top
->y
;
2442 BRESINITPGONSTRUCT(dy
, top
->x
, bottom
->x
, pETEs
->bres
);
2444 REGION_InsertEdgeInET(ET
, pETEs
, top
->y
, &pSLLBlock
,
2447 if (PrevPt
->y
> ET
->ymax
)
2448 ET
->ymax
= PrevPt
->y
;
2449 if (PrevPt
->y
< ET
->ymin
)
2450 ET
->ymin
= PrevPt
->y
;
2459 /***********************************************************************
2462 * This routine moves EdgeTableEntries from the
2463 * EdgeTable into the Active Edge Table,
2464 * leaving them sorted by smaller x coordinate.
2467 static void REGION_loadAET(EdgeTableEntry
*AET
, EdgeTableEntry
*ETEs
)
2469 EdgeTableEntry
*pPrevAET
;
2470 EdgeTableEntry
*tmp
;
2476 while (AET
&& (AET
->bres
.minor_axis
< ETEs
->bres
.minor_axis
))
2485 ETEs
->back
= pPrevAET
;
2486 pPrevAET
->next
= ETEs
;
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(EdgeTableEntry
*AET
)
2515 register EdgeTableEntry
*pWETE
;
2516 register int inside
= 1;
2517 register int isInside
= 0;
2519 AET
->nextWETE
= (EdgeTableEntry
*)NULL
;
2529 if ((!inside
&& !isInside
) ||
2530 ( inside
&& isInside
))
2532 pWETE
->nextWETE
= AET
;
2538 pWETE
->nextWETE
= (EdgeTableEntry
*)NULL
;
2541 /***********************************************************************
2542 * REGION_InsertionSort
2544 * Just a simple insertion sort using
2545 * pointers and back pointers to sort the Active
2549 static BOOL
REGION_InsertionSort(EdgeTableEntry
*AET
)
2551 EdgeTableEntry
*pETEchase
;
2552 EdgeTableEntry
*pETEinsert
;
2553 EdgeTableEntry
*pETEchaseBackTMP
;
2554 BOOL changed
= FALSE
;
2561 while (pETEchase
->back
->bres
.minor_axis
> AET
->bres
.minor_axis
)
2562 pETEchase
= pETEchase
->back
;
2565 if (pETEchase
!= pETEinsert
)
2567 pETEchaseBackTMP
= pETEchase
->back
;
2568 pETEinsert
->back
->next
= AET
;
2570 AET
->back
= pETEinsert
->back
;
2571 pETEinsert
->next
= pETEchase
;
2572 pETEchase
->back
->next
= pETEinsert
;
2573 pETEchase
->back
= pETEinsert
;
2574 pETEinsert
->back
= pETEchaseBackTMP
;
2581 /***********************************************************************
2582 * REGION_FreeStorage
2586 static void REGION_FreeStorage(ScanLineListBlock
*pSLLBlock
)
2588 ScanLineListBlock
*tmpSLLBlock
;
2592 tmpSLLBlock
= pSLLBlock
->next
;
2593 HeapFree( GetProcessHeap(), 0, pSLLBlock
);
2594 pSLLBlock
= tmpSLLBlock
;
2599 /***********************************************************************
2600 * REGION_PtsToRegion
2602 * Create an array of rectangles from a list of points.
2604 static int REGION_PtsToRegion(int numFullPtBlocks
, int iCurPtBlock
,
2605 POINTBLOCK
*FirstPtBlock
, WINEREGION
*reg
)
2609 POINTBLOCK
*CurPtBlock
;
2614 extents
= ®
->extents
;
2616 numRects
= ((numFullPtBlocks
* NUMPTSTOBUFFER
) + iCurPtBlock
) >> 1;
2618 if (!(reg
->rects
= HeapReAlloc( GetProcessHeap(), 0, reg
->rects
,
2619 sizeof(RECT
) * numRects
)))
2622 reg
->size
= numRects
;
2623 CurPtBlock
= FirstPtBlock
;
2624 rects
= reg
->rects
- 1;
2626 extents
->left
= LARGE_COORDINATE
, extents
->right
= SMALL_COORDINATE
;
2628 for ( ; numFullPtBlocks
>= 0; numFullPtBlocks
--) {
2629 /* the loop uses 2 points per iteration */
2630 i
= NUMPTSTOBUFFER
>> 1;
2631 if (!numFullPtBlocks
)
2632 i
= iCurPtBlock
>> 1;
2633 for (pts
= CurPtBlock
->pts
; i
--; pts
+= 2) {
2634 if (pts
->x
== pts
[1].x
)
2636 if (numRects
&& pts
->x
== rects
->left
&& pts
->y
== rects
->bottom
&&
2637 pts
[1].x
== rects
->right
&&
2638 (numRects
== 1 || rects
[-1].top
!= rects
->top
) &&
2639 (i
&& pts
[2].y
> pts
[1].y
)) {
2640 rects
->bottom
= pts
[1].y
+ 1;
2645 rects
->left
= pts
->x
; rects
->top
= pts
->y
;
2646 rects
->right
= pts
[1].x
; rects
->bottom
= pts
[1].y
+ 1;
2647 if (rects
->left
< extents
->left
)
2648 extents
->left
= rects
->left
;
2649 if (rects
->right
> extents
->right
)
2650 extents
->right
= rects
->right
;
2652 CurPtBlock
= CurPtBlock
->next
;
2656 extents
->top
= reg
->rects
->top
;
2657 extents
->bottom
= rects
->bottom
;
2662 extents
->bottom
= 0;
2664 reg
->numRects
= numRects
;
2669 /***********************************************************************
2670 * CreatePolyPolygonRgn (GDI32.@)
2672 HRGN WINAPI
CreatePolyPolygonRgn(const POINT
*Pts
, const INT
*Count
,
2673 INT nbpolygons
, INT mode
)
2678 register EdgeTableEntry
*pAET
; /* Active Edge Table */
2679 register INT y
; /* current scanline */
2680 register int iPts
= 0; /* number of pts in buffer */
2681 register EdgeTableEntry
*pWETE
; /* Winding Edge Table Entry*/
2682 register ScanLineList
*pSLL
; /* current scanLineList */
2683 register POINT
*pts
; /* output buffer */
2684 EdgeTableEntry
*pPrevAET
; /* ptr to previous AET */
2685 EdgeTable ET
; /* header node for ET */
2686 EdgeTableEntry AET
; /* header node for AET */
2687 EdgeTableEntry
*pETEs
; /* EdgeTableEntries pool */
2688 ScanLineListBlock SLLBlock
; /* header for scanlinelist */
2689 int fixWAET
= FALSE
;
2690 POINTBLOCK FirstPtBlock
, *curPtBlock
; /* PtBlock buffers */
2691 POINTBLOCK
*tmpPtBlock
;
2692 int numFullPtBlocks
= 0;
2695 if(!(hrgn
= REGION_CreateRegion(nbpolygons
)))
2697 obj
= (RGNOBJ
*) GDI_GetObjPtr( hrgn
, REGION_MAGIC
);
2700 /* special case a rectangle */
2702 if (((nbpolygons
== 1) && ((*Count
== 4) ||
2703 ((*Count
== 5) && (Pts
[4].x
== Pts
[0].x
) && (Pts
[4].y
== Pts
[0].y
)))) &&
2704 (((Pts
[0].y
== Pts
[1].y
) &&
2705 (Pts
[1].x
== Pts
[2].x
) &&
2706 (Pts
[2].y
== Pts
[3].y
) &&
2707 (Pts
[3].x
== Pts
[0].x
)) ||
2708 ((Pts
[0].x
== Pts
[1].x
) &&
2709 (Pts
[1].y
== Pts
[2].y
) &&
2710 (Pts
[2].x
== Pts
[3].x
) &&
2711 (Pts
[3].y
== Pts
[0].y
))))
2713 SetRectRgn( hrgn
, min(Pts
[0].x
, Pts
[2].x
), min(Pts
[0].y
, Pts
[2].y
),
2714 max(Pts
[0].x
, Pts
[2].x
), max(Pts
[0].y
, Pts
[2].y
) );
2715 GDI_ReleaseObj( hrgn
);
2719 for(poly
= total
= 0; poly
< nbpolygons
; poly
++)
2720 total
+= Count
[poly
];
2721 if (! (pETEs
= HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry
) * total
)))
2723 REGION_DeleteObject( hrgn
, obj
);
2726 pts
= FirstPtBlock
.pts
;
2727 REGION_CreateETandAET(Count
, nbpolygons
, Pts
, &ET
, &AET
, pETEs
, &SLLBlock
);
2728 pSLL
= ET
.scanlines
.next
;
2729 curPtBlock
= &FirstPtBlock
;
2731 if (mode
!= WINDING
) {
2735 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2737 * Add a new edge to the active edge table when we
2738 * get to the next edge.
2740 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2741 REGION_loadAET(&AET
, pSLL
->edgelist
);
2748 * for each active edge
2751 pts
->x
= pAET
->bres
.minor_axis
, pts
->y
= y
;
2755 * send out the buffer
2757 if (iPts
== NUMPTSTOBUFFER
) {
2758 tmpPtBlock
= HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK
));
2760 WARN("Can't alloc tPB\n");
2763 curPtBlock
->next
= tmpPtBlock
;
2764 curPtBlock
= tmpPtBlock
;
2765 pts
= curPtBlock
->pts
;
2769 EVALUATEEDGEEVENODD(pAET
, pPrevAET
, y
);
2771 REGION_InsertionSort(&AET
);
2778 for (y
= ET
.ymin
; y
< ET
.ymax
; y
++) {
2780 * Add a new edge to the active edge table when we
2781 * get to the next edge.
2783 if (pSLL
!= NULL
&& y
== pSLL
->scanline
) {
2784 REGION_loadAET(&AET
, pSLL
->edgelist
);
2785 REGION_computeWAET(&AET
);
2793 * for each active edge
2797 * add to the buffer only those edges that
2798 * are in the Winding active edge table.
2800 if (pWETE
== pAET
) {
2801 pts
->x
= pAET
->bres
.minor_axis
, pts
->y
= y
;
2805 * send out the buffer
2807 if (iPts
== NUMPTSTOBUFFER
) {
2808 tmpPtBlock
= HeapAlloc( GetProcessHeap(), 0,
2809 sizeof(POINTBLOCK
) );
2811 WARN("Can't alloc tPB\n");
2812 REGION_DeleteObject( hrgn
, obj
);
2815 curPtBlock
->next
= tmpPtBlock
;
2816 curPtBlock
= tmpPtBlock
;
2817 pts
= curPtBlock
->pts
;
2818 numFullPtBlocks
++; iPts
= 0;
2820 pWETE
= pWETE
->nextWETE
;
2822 EVALUATEEDGEWINDING(pAET
, pPrevAET
, y
, fixWAET
);
2826 * recompute the winding active edge table if
2827 * we just resorted or have exited an edge.
2829 if (REGION_InsertionSort(&AET
) || fixWAET
) {
2830 REGION_computeWAET(&AET
);
2835 REGION_FreeStorage(SLLBlock
.next
);
2836 REGION_PtsToRegion(numFullPtBlocks
, iPts
, &FirstPtBlock
, region
);
2838 for (curPtBlock
= FirstPtBlock
.next
; --numFullPtBlocks
>= 0;) {
2839 tmpPtBlock
= curPtBlock
->next
;
2840 HeapFree( GetProcessHeap(), 0, curPtBlock
);
2841 curPtBlock
= tmpPtBlock
;
2843 HeapFree( GetProcessHeap(), 0, pETEs
);
2844 GDI_ReleaseObj( hrgn
);
2849 /***********************************************************************
2850 * CreatePolygonRgn (GDI.63)
2852 HRGN16 WINAPI
CreatePolygonRgn16( const POINT16
* points
, INT16 count
,
2855 return CreatePolyPolygonRgn16( points
, &count
, 1, mode
);
2858 /***********************************************************************
2859 * CreatePolyPolygonRgn (GDI.451)
2861 HRGN16 WINAPI
CreatePolyPolygonRgn16( const POINT16
*points
,
2862 const INT16
*count
, INT16 nbpolygons
, INT16 mode
)
2869 for (i
= 0; i
< nbpolygons
; i
++)
2871 points32
= HeapAlloc( GetProcessHeap(), 0, npts
* sizeof(POINT
) );
2872 for (i
= 0; i
< npts
; i
++)
2873 CONV_POINT16TO32( &(points
[i
]), &(points32
[i
]) );
2875 count32
= HeapAlloc( GetProcessHeap(), 0, nbpolygons
* sizeof(INT
) );
2876 for (i
= 0; i
< nbpolygons
; i
++)
2877 count32
[i
] = count
[i
];
2878 hrgn
= CreatePolyPolygonRgn( points32
, count32
, nbpolygons
, mode
);
2879 HeapFree( GetProcessHeap(), 0, count32
);
2880 HeapFree( GetProcessHeap(), 0, points32
);
2884 /***********************************************************************
2885 * CreatePolygonRgn (GDI32.@)
2887 HRGN WINAPI
CreatePolygonRgn( const POINT
*points
, INT count
,
2890 return CreatePolyPolygonRgn( points
, &count
, 1, mode
);
2894 /***********************************************************************
2895 * GetRandomRgn [GDI32.@]
2898 * This function is documented in MSDN online
2900 INT WINAPI
GetRandomRgn(HDC hDC
, HRGN hRgn
, DWORD dwCode
)
2904 case 4: /* == SYSRGN ? */
2906 DC
*dc
= DC_GetDCPtr (hDC
);
2911 CombineRgn (hRgn
, dc
->hVisRgn
, 0, RGN_COPY
);
2913 * On Windows NT/2000,
2914 * the region returned is in screen coordinates.
2916 * the region returned is in window coordinates
2918 vi
.dwOSVersionInfoSize
= sizeof(vi
);
2919 if (GetVersionExA( &vi
) && vi
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
2920 GetDCOrgEx(hDC
, &org
);
2923 OffsetRgn (hRgn
, org
.x
, org
.y
);
2924 GDI_ReleaseObj( hDC
);
2928 return GetClipRgn (hDC, hRgn);
2931 WARN("Unknown dwCode %ld\n", dwCode
);
2938 /***********************************************************************
2939 * REGION_CropAndOffsetRegion
2941 static BOOL
REGION_CropAndOffsetRegion(const POINT
* off
, const RECT
*rect
, WINEREGION
*rgnSrc
, WINEREGION
* rgnDst
)
2944 if( !rect
) /* just copy and offset */
2947 if( rgnDst
== rgnSrc
)
2949 if( off
->x
|| off
->y
)
2950 xrect
= rgnDst
->rects
;
2955 xrect
= HeapReAlloc( GetProcessHeap(), 0, rgnDst
->rects
,
2956 rgnSrc
->size
* sizeof( RECT
));
2961 if( rgnDst
!= rgnSrc
)
2962 memcpy( rgnDst
, rgnSrc
, sizeof( WINEREGION
));
2964 if( off
->x
|| off
->y
)
2966 for( i
= 0; i
< rgnDst
->numRects
; i
++ )
2968 xrect
[i
].left
= rgnSrc
->rects
[i
].left
+ off
->x
;
2969 xrect
[i
].right
= rgnSrc
->rects
[i
].right
+ off
->x
;
2970 xrect
[i
].top
= rgnSrc
->rects
[i
].top
+ off
->y
;
2971 xrect
[i
].bottom
= rgnSrc
->rects
[i
].bottom
+ off
->y
;
2973 rgnDst
->extents
.left
+= off
->x
;
2974 rgnDst
->extents
.right
+= off
->x
;
2975 rgnDst
->extents
.top
+= off
->y
;
2976 rgnDst
->extents
.bottom
+= off
->y
;
2979 memcpy( xrect
, rgnSrc
->rects
, rgnDst
->numRects
* sizeof(RECT
));
2980 rgnDst
->rects
= xrect
;
2984 else if ((rect
->left
>= rect
->right
) ||
2985 (rect
->top
>= rect
->bottom
) ||
2986 !EXTENTCHECK(rect
, &rgnSrc
->extents
))
2989 if( !rgnDst
->rects
)
2991 rgnDst
->rects
= HeapAlloc(GetProcessHeap(), 0, RGN_DEFAULT_RECTS
* sizeof( RECT
));
2993 rgnDst
->size
= RGN_DEFAULT_RECTS
;
2998 TRACE("cropped to empty!\n");
2999 EMPTY_REGION(rgnDst
);
3001 else /* region box and clipping rect appear to intersect */
3004 INT i
, j
, clipa
, clipb
;
3005 INT left
= rgnSrc
->extents
.right
+ off
->x
;
3006 INT right
= rgnSrc
->extents
.left
+ off
->x
;
3008 for( clipa
= 0; rgnSrc
->rects
[clipa
].bottom
<= rect
->top
; clipa
++ )
3009 ; /* skip bands above the clipping rectangle */
3011 for( clipb
= clipa
; clipb
< rgnSrc
->numRects
; clipb
++ )
3012 if( rgnSrc
->rects
[clipb
].top
>= rect
->bottom
)
3013 break; /* and below it */
3015 /* clipa - index of the first rect in the first intersecting band
3016 * clipb - index of the last rect in the last intersecting band
3019 if((rgnDst
!= rgnSrc
) && (rgnDst
->size
< (i
= (clipb
- clipa
))))
3021 rgnDst
->rects
= HeapReAlloc( GetProcessHeap(), 0,
3022 rgnDst
->rects
, i
* sizeof(RECT
));
3023 if( !rgnDst
->rects
) return FALSE
;
3027 if( TRACE_ON(region
) )
3029 REGION_DumpRegion( rgnSrc
);
3030 TRACE("\tclipa = %i, clipb = %i\n", clipa
, clipb
);
3033 for( i
= clipa
, j
= 0; i
< clipb
; i
++ )
3035 /* i - src index, j - dst index, j is always <= i for obvious reasons */
3037 lpr
= rgnSrc
->rects
+ i
;
3038 if( lpr
->left
< rect
->right
&& lpr
->right
> rect
->left
)
3040 rgnDst
->rects
[j
].top
= lpr
->top
+ off
->y
;
3041 rgnDst
->rects
[j
].bottom
= lpr
->bottom
+ off
->y
;
3042 rgnDst
->rects
[j
].left
= ((lpr
->left
> rect
->left
) ? lpr
->left
: rect
->left
) + off
->x
;
3043 rgnDst
->rects
[j
].right
= ((lpr
->right
< rect
->right
) ? lpr
->right
: rect
->right
) + off
->x
;
3045 if( rgnDst
->rects
[j
].left
< left
) left
= rgnDst
->rects
[j
].left
;
3046 if( rgnDst
->rects
[j
].right
> right
) right
= rgnDst
->rects
[j
].right
;
3052 if( j
== 0 ) goto empty
;
3054 rgnDst
->extents
.left
= left
;
3055 rgnDst
->extents
.right
= right
;
3057 left
= rect
->top
+ off
->y
;
3058 right
= rect
->bottom
+ off
->y
;
3060 rgnDst
->numRects
= j
--;
3061 for( i
= 0; i
<= j
; i
++ ) /* fixup top band */
3062 if( rgnDst
->rects
[i
].top
< left
)
3063 rgnDst
->rects
[i
].top
= left
;
3067 for( i
= j
; i
>= 0; i
-- ) /* fixup bottom band */
3068 if( rgnDst
->rects
[i
].bottom
> right
)
3069 rgnDst
->rects
[i
].bottom
= right
;
3073 rgnDst
->extents
.top
= rgnDst
->rects
[0].top
;
3074 rgnDst
->extents
.bottom
= rgnDst
->rects
[j
].bottom
;
3076 if( TRACE_ON(region
) )
3079 REGION_DumpRegion( rgnDst
);
3086 /***********************************************************************
3090 * hSrc: Region to crop and offset.
3091 * lpRect: Clipping rectangle. Can be NULL (no clipping).
3092 * lpPt: Points to offset the cropped region. Can be NULL (no offset).
3094 * hDst: Region to hold the result (a new region is created if it's 0).
3095 * Allowed to be the same region as hSrc in which case everything
3096 * will be done in place, with no memory reallocations.
3098 * Returns: hDst if success, 0 otherwise.
3100 HRGN
REGION_CropRgn( HRGN hDst
, HRGN hSrc
, const RECT
*lpRect
, const POINT
*lpPt
)
3102 /* Optimization of the following generic code:
3107 h = CreateRectRgn( lpRect->left, lpRect->top, lpRect->right, lpRect->bottom );
3109 h = CreateRectRgn( 0, 0, 0, 0 );
3110 if( hDst == 0 ) hDst = h;
3112 CombineRgn( hDst, hSrc, h, RGN_AND );
3114 CombineRgn( hDst, hSrc, 0, RGN_COPY );
3116 OffsetRgn( hDst, lpPt->x, lpPt->y );
3123 RGNOBJ
*objSrc
= (RGNOBJ
*) GDI_GetObjPtr( hSrc
, REGION_MAGIC
);
3132 if (!(objDst
= (RGNOBJ
*) GDI_GetObjPtr( hDst
, REGION_MAGIC
)))
3137 rgnDst
= objDst
->rgn
;
3141 if ((rgnDst
= HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION
))))
3143 rgnDst
->size
= rgnDst
->numRects
= 0;
3144 rgnDst
->rects
= NULL
; /* back end will allocate exact number */
3150 POINT pt
= { 0, 0 };
3152 if( !lpPt
) lpPt
= &pt
;
3155 TRACE("src %p -> dst %p (%i,%i)-(%i,%i) by (%li,%li)\n", objSrc
->rgn
, rgnDst
,
3156 lpRect
->left
, lpRect
->top
, lpRect
->right
, lpRect
->bottom
, lpPt
->x
, lpPt
->y
);
3158 TRACE("src %p -> dst %p by (%li,%li)\n", objSrc
->rgn
, rgnDst
, lpPt
->x
, lpPt
->y
);
3160 if( REGION_CropAndOffsetRegion( lpPt
, lpRect
, objSrc
->rgn
, rgnDst
) == FALSE
)
3162 if( hDst
) /* existing rgn */
3164 GDI_ReleaseObj(hDst
);
3170 else if( hDst
== 0 )
3172 if (!(objDst
= GDI_AllocObject( sizeof(RGNOBJ
), REGION_MAGIC
,
3173 &hDst
, ®ion_funcs
)))
3177 HeapFree( GetProcessHeap(), 0, rgnDst
->rects
);
3178 HeapFree( GetProcessHeap(), 0, rgnDst
);
3181 objDst
->rgn
= rgnDst
;
3184 GDI_ReleaseObj(hDst
);
3188 GDI_ReleaseObj(hSrc
);
3194 /***********************************************************************
3195 * GetMetaRgn (GDI32.@)
3197 INT WINAPI
GetMetaRgn( HDC hdc
, HRGN hRgn
)
3205 /***********************************************************************
3206 * SetMetaRgn (GDI32.@)
3208 INT WINAPI
SetMetaRgn( HDC hdc
)