wined3d: Print real unhandled D3DCMPFUNC value.
[wine/hacks.git] / dlls / gdi / region.c
blob13f2016a9cfc32b217b1996876dd37937cd73902
1 /*
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
7 * 1999 Alex Korobka
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /************************************************************************
26 Copyright (c) 1987, 1988 X Consortium
28 Permission is hereby granted, free of charge, to any person obtaining a copy
29 of this software and associated documentation files (the "Software"), to deal
30 in the Software without restriction, including without limitation the rights
31 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32 copies of the Software, and to permit persons to whom the Software is
33 furnished to do so, subject to the following conditions:
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 Except as contained in this notice, the name of the X Consortium shall not be
46 used in advertising or otherwise to promote the sale, use or other dealings
47 in this Software without prior written authorization from the X Consortium.
50 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
52 All Rights Reserved
54 Permission to use, copy, modify, and distribute this software and its
55 documentation for any purpose and without fee is hereby granted,
56 provided that the above copyright notice appear in all copies and that
57 both that copyright notice and this permission notice appear in
58 supporting documentation, and that the name of Digital not be
59 used in advertising or publicity pertaining to distribution of the
60 software without specific, written prior permission.
62 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
63 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
64 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
65 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
66 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
67 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68 SOFTWARE.
70 ************************************************************************/
72 * The functions in this file implement the Region abstraction, similar to one
73 * used in the X11 sample server. A Region is simply an area, as the name
74 * implies, and is implemented as a "y-x-banded" array of rectangles. To
75 * explain: Each Region is made up of a certain number of rectangles sorted
76 * by y coordinate first, and then by x coordinate.
78 * Furthermore, the rectangles are banded such that every rectangle with a
79 * given upper-left y coordinate (y1) will have the same lower-right y
80 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
81 * will span the entire vertical distance of the band. This means that some
82 * areas that could be merged into a taller rectangle will be represented as
83 * several shorter rectangles to account for shorter rectangles to its left
84 * or right but within its "vertical scope".
86 * An added constraint on the rectangles is that they must cover as much
87 * horizontal area as possible. E.g. no two rectangles in a band are allowed
88 * to touch.
90 * Whenever possible, bands will be merged together to cover a greater vertical
91 * distance (and thus reduce the number of rectangles). Two bands can be merged
92 * only if the bottom of one touches the top of the other and they have
93 * rectangles in the same places (of the same width, of course). This maintains
94 * the y-x-banding that's so nice to have...
97 #include <stdarg.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include "windef.h"
101 #include "winbase.h"
102 #include "wingdi.h"
103 #include "gdi.h"
104 #include "gdi_private.h"
105 #include "wine/debug.h"
107 WINE_DEFAULT_DEBUG_CHANNEL(region);
109 typedef struct {
110 INT size;
111 INT numRects;
112 RECT *rects;
113 RECT extents;
114 } WINEREGION;
116 /* GDI logical region object */
117 typedef struct
119 GDIOBJHDR header;
120 WINEREGION *rgn;
121 } RGNOBJ;
124 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
125 static BOOL REGION_DeleteObject( HGDIOBJ handle, void *obj );
127 static const struct gdi_obj_funcs region_funcs =
129 REGION_SelectObject, /* pSelectObject */
130 NULL, /* pGetObject16 */
131 NULL, /* pGetObjectA */
132 NULL, /* pGetObjectW */
133 NULL, /* pUnrealizeObject */
134 REGION_DeleteObject /* pDeleteObject */
137 /* 1 if two RECTs overlap.
138 * 0 if two RECTs do not overlap.
140 #define EXTENTCHECK(r1, r2) \
141 ((r1)->right > (r2)->left && \
142 (r1)->left < (r2)->right && \
143 (r1)->bottom > (r2)->top && \
144 (r1)->top < (r2)->bottom)
147 * Check to see if there is enough memory in the present region.
150 static inline int xmemcheck(WINEREGION *reg, LPRECT *rect, LPRECT *firstrect ) {
151 if (reg->numRects >= (reg->size - 1)) {
152 *firstrect = HeapReAlloc( GetProcessHeap(), 0, *firstrect, (2 * (sizeof(RECT)) * (reg->size)));
153 if (*firstrect == 0)
154 return 0;
155 reg->size *= 2;
156 *rect = (*firstrect)+reg->numRects;
158 return 1;
161 #define MEMCHECK(reg, rect, firstrect) xmemcheck(reg,&(rect),&(firstrect))
163 #define EMPTY_REGION(pReg) { \
164 (pReg)->numRects = 0; \
165 (pReg)->extents.left = (pReg)->extents.top = 0; \
166 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
169 #define REGION_NOT_EMPTY(pReg) pReg->numRects
171 #define INRECT(r, x, y) \
172 ( ( ((r).right > x)) && \
173 ( ((r).left <= x)) && \
174 ( ((r).bottom > y)) && \
175 ( ((r).top <= y)) )
179 * number of points to buffer before sending them off
180 * to scanlines() : Must be an even number
182 #define NUMPTSTOBUFFER 200
185 * used to allocate buffers for points and link
186 * the buffers together
189 typedef struct _POINTBLOCK {
190 POINT pts[NUMPTSTOBUFFER];
191 struct _POINTBLOCK *next;
192 } POINTBLOCK;
197 * This file contains a few macros to help track
198 * the edge of a filled object. The object is assumed
199 * to be filled in scanline order, and thus the
200 * algorithm used is an extension of Bresenham's line
201 * drawing algorithm which assumes that y is always the
202 * major axis.
203 * Since these pieces of code are the same for any filled shape,
204 * it is more convenient to gather the library in one
205 * place, but since these pieces of code are also in
206 * the inner loops of output primitives, procedure call
207 * overhead is out of the question.
208 * See the author for a derivation if needed.
213 * In scan converting polygons, we want to choose those pixels
214 * which are inside the polygon. Thus, we add .5 to the starting
215 * x coordinate for both left and right edges. Now we choose the
216 * first pixel which is inside the pgon for the left edge and the
217 * first pixel which is outside the pgon for the right edge.
218 * Draw the left pixel, but not the right.
220 * How to add .5 to the starting x coordinate:
221 * If the edge is moving to the right, then subtract dy from the
222 * error term from the general form of the algorithm.
223 * If the edge is moving to the left, then add dy to the error term.
225 * The reason for the difference between edges moving to the left
226 * and edges moving to the right is simple: If an edge is moving
227 * to the right, then we want the algorithm to flip immediately.
228 * If it is moving to the left, then we don't want it to flip until
229 * we traverse an entire pixel.
231 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
232 int dx; /* local storage */ \
234 /* \
235 * if the edge is horizontal, then it is ignored \
236 * and assumed not to be processed. Otherwise, do this stuff. \
237 */ \
238 if ((dy) != 0) { \
239 xStart = (x1); \
240 dx = (x2) - xStart; \
241 if (dx < 0) { \
242 m = dx / (dy); \
243 m1 = m - 1; \
244 incr1 = -2 * dx + 2 * (dy) * m1; \
245 incr2 = -2 * dx + 2 * (dy) * m; \
246 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
247 } else { \
248 m = dx / (dy); \
249 m1 = m + 1; \
250 incr1 = 2 * dx - 2 * (dy) * m1; \
251 incr2 = 2 * dx - 2 * (dy) * m; \
252 d = -2 * m * (dy) + 2 * dx; \
257 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
258 if (m1 > 0) { \
259 if (d > 0) { \
260 minval += m1; \
261 d += incr1; \
263 else { \
264 minval += m; \
265 d += incr2; \
267 } else {\
268 if (d >= 0) { \
269 minval += m1; \
270 d += incr1; \
272 else { \
273 minval += m; \
274 d += incr2; \
280 * This structure contains all of the information needed
281 * to run the bresenham algorithm.
282 * The variables may be hardcoded into the declarations
283 * instead of using this structure to make use of
284 * register declarations.
286 typedef struct {
287 INT minor_axis; /* minor axis */
288 INT d; /* decision variable */
289 INT m, m1; /* slope and slope+1 */
290 INT incr1, incr2; /* error increments */
291 } BRESINFO;
294 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
295 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
296 bres.m, bres.m1, bres.incr1, bres.incr2)
298 #define BRESINCRPGONSTRUCT(bres) \
299 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
304 * These are the data structures needed to scan
305 * convert regions. Two different scan conversion
306 * methods are available -- the even-odd method, and
307 * the winding number method.
308 * The even-odd rule states that a point is inside
309 * the polygon if a ray drawn from that point in any
310 * direction will pass through an odd number of
311 * path segments.
312 * By the winding number rule, a point is decided
313 * to be inside the polygon if a ray drawn from that
314 * point in any direction passes through a different
315 * number of clockwise and counter-clockwise path
316 * segments.
318 * These data structures are adapted somewhat from
319 * the algorithm in (Foley/Van Dam) for scan converting
320 * polygons.
321 * The basic algorithm is to start at the top (smallest y)
322 * of the polygon, stepping down to the bottom of
323 * the polygon by incrementing the y coordinate. We
324 * keep a list of edges which the current scanline crosses,
325 * sorted by x. This list is called the Active Edge Table (AET)
326 * As we change the y-coordinate, we update each entry in
327 * in the active edge table to reflect the edges new xcoord.
328 * This list must be sorted at each scanline in case
329 * two edges intersect.
330 * We also keep a data structure known as the Edge Table (ET),
331 * which keeps track of all the edges which the current
332 * scanline has not yet reached. The ET is basically a
333 * list of ScanLineList structures containing a list of
334 * edges which are entered at a given scanline. There is one
335 * ScanLineList per scanline at which an edge is entered.
336 * When we enter a new edge, we move it from the ET to the AET.
338 * From the AET, we can implement the even-odd rule as in
339 * (Foley/Van Dam).
340 * The winding number rule is a little trickier. We also
341 * keep the EdgeTableEntries in the AET linked by the
342 * nextWETE (winding EdgeTableEntry) link. This allows
343 * the edges to be linked just as before for updating
344 * purposes, but only uses the edges linked by the nextWETE
345 * link as edges representing spans of the polygon to
346 * drawn (as with the even-odd rule).
350 * for the winding number rule
352 #define CLOCKWISE 1
353 #define COUNTERCLOCKWISE -1
355 typedef struct _EdgeTableEntry {
356 INT ymax; /* ycoord at which we exit this edge. */
357 BRESINFO bres; /* Bresenham info to run the edge */
358 struct _EdgeTableEntry *next; /* next in the list */
359 struct _EdgeTableEntry *back; /* for insertion sort */
360 struct _EdgeTableEntry *nextWETE; /* for winding num rule */
361 int ClockWise; /* flag for winding number rule */
362 } EdgeTableEntry;
365 typedef struct _ScanLineList{
366 INT scanline; /* the scanline represented */
367 EdgeTableEntry *edgelist; /* header node */
368 struct _ScanLineList *next; /* next in the list */
369 } ScanLineList;
372 typedef struct {
373 INT ymax; /* ymax for the polygon */
374 INT ymin; /* ymin for the polygon */
375 ScanLineList scanlines; /* header node */
376 } EdgeTable;
380 * Here is a struct to help with storage allocation
381 * so we can allocate a big chunk at a time, and then take
382 * pieces from this heap when we need to.
384 #define SLLSPERBLOCK 25
386 typedef struct _ScanLineListBlock {
387 ScanLineList SLLs[SLLSPERBLOCK];
388 struct _ScanLineListBlock *next;
389 } ScanLineListBlock;
394 * a few macros for the inner loops of the fill code where
395 * performance considerations don't allow a procedure call.
397 * Evaluate the given edge at the given scanline.
398 * If the edge has expired, then we leave it and fix up
399 * the active edge table; otherwise, we increment the
400 * x value to be ready for the next scanline.
401 * The winding number rule is in effect, so we must notify
402 * the caller when the edge has been removed so he
403 * can reorder the Winding Active Edge Table.
405 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
406 if (pAET->ymax == y) { /* leaving this edge */ \
407 pPrevAET->next = pAET->next; \
408 pAET = pPrevAET->next; \
409 fixWAET = 1; \
410 if (pAET) \
411 pAET->back = pPrevAET; \
413 else { \
414 BRESINCRPGONSTRUCT(pAET->bres); \
415 pPrevAET = pAET; \
416 pAET = pAET->next; \
422 * Evaluate the given edge at the given scanline.
423 * If the edge has expired, then we leave it and fix up
424 * the active edge table; otherwise, we increment the
425 * x value to be ready for the next scanline.
426 * The even-odd rule is in effect.
428 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
429 if (pAET->ymax == y) { /* leaving this edge */ \
430 pPrevAET->next = pAET->next; \
431 pAET = pPrevAET->next; \
432 if (pAET) \
433 pAET->back = pPrevAET; \
435 else { \
436 BRESINCRPGONSTRUCT(pAET->bres); \
437 pPrevAET = pAET; \
438 pAET = pAET->next; \
442 /* Note the parameter order is different from the X11 equivalents */
444 static void REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
445 static void REGION_OffsetRegion(WINEREGION *d, WINEREGION *s, INT x, INT y);
446 static void REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
447 static void REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
448 static void REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
449 static void REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
450 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
452 #define RGN_DEFAULT_RECTS 2
455 /***********************************************************************
456 * get_region_type
458 inline static INT get_region_type( const RGNOBJ *obj )
460 switch(obj->rgn->numRects)
462 case 0: return NULLREGION;
463 case 1: return SIMPLEREGION;
464 default: return COMPLEXREGION;
469 /***********************************************************************
470 * REGION_DumpRegion
471 * Outputs the contents of a WINEREGION
473 static void REGION_DumpRegion(WINEREGION *pReg)
475 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
477 TRACE("Region %p: %ld,%ld - %ld,%ld %d rects\n", pReg,
478 pReg->extents.left, pReg->extents.top,
479 pReg->extents.right, pReg->extents.bottom, pReg->numRects);
480 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
481 TRACE("\t%ld,%ld - %ld,%ld\n", pRect->left, pRect->top,
482 pRect->right, pRect->bottom);
483 return;
487 /***********************************************************************
488 * REGION_AllocWineRegion
489 * Create a new empty WINEREGION.
491 static WINEREGION *REGION_AllocWineRegion( INT n )
493 WINEREGION *pReg;
495 if ((pReg = HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION ))))
497 if ((pReg->rects = HeapAlloc(GetProcessHeap(), 0, n * sizeof( RECT ))))
499 pReg->size = n;
500 EMPTY_REGION(pReg);
501 return pReg;
503 HeapFree(GetProcessHeap(), 0, pReg);
505 return NULL;
509 /***********************************************************************
510 * REGION_CreateRegion
511 * Create a new empty region.
513 static HRGN REGION_CreateRegion( INT n )
515 HRGN hrgn;
516 RGNOBJ *obj;
518 if(!(obj = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC, (HGDIOBJ *)&hrgn,
519 &region_funcs ))) return 0;
520 if(!(obj->rgn = REGION_AllocWineRegion(n))) {
521 GDI_FreeObject( hrgn, obj );
522 return 0;
524 GDI_ReleaseObj( hrgn );
525 return hrgn;
528 /***********************************************************************
529 * REGION_DestroyWineRegion
531 static void REGION_DestroyWineRegion( WINEREGION* pReg )
533 HeapFree( GetProcessHeap(), 0, pReg->rects );
534 HeapFree( GetProcessHeap(), 0, pReg );
537 /***********************************************************************
538 * REGION_DeleteObject
540 static BOOL REGION_DeleteObject( HGDIOBJ handle, void *obj )
542 RGNOBJ *rgn = obj;
544 TRACE(" %p\n", handle );
546 REGION_DestroyWineRegion( rgn->rgn );
547 return GDI_FreeObject( handle, obj );
550 /***********************************************************************
551 * REGION_SelectObject
553 static HGDIOBJ REGION_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
555 return (HGDIOBJ)SelectClipRgn( hdc, handle );
559 /***********************************************************************
560 * REGION_OffsetRegion
561 * Offset a WINEREGION by x,y
563 static void REGION_OffsetRegion( WINEREGION *rgn, WINEREGION *srcrgn,
564 INT x, INT y )
566 if( rgn != srcrgn)
567 REGION_CopyRegion( rgn, srcrgn);
568 if(x || y) {
569 int nbox = rgn->numRects;
570 RECT *pbox = rgn->rects;
572 if(nbox) {
573 while(nbox--) {
574 pbox->left += x;
575 pbox->right += x;
576 pbox->top += y;
577 pbox->bottom += y;
578 pbox++;
580 rgn->extents.left += x;
581 rgn->extents.right += x;
582 rgn->extents.top += y;
583 rgn->extents.bottom += y;
588 /***********************************************************************
589 * OffsetRgn (GDI32.@)
591 * Moves a region by the specified X- and Y-axis offsets.
593 * PARAMS
594 * hrgn [I] Region to offset.
595 * x [I] Offset right if positive or left if negative.
596 * y [I] Offset down if positive or up if negative.
598 * RETURNS
599 * Success:
600 * NULLREGION - The new region is empty.
601 * SIMPLEREGION - The new region can be represented by one rectangle.
602 * COMPLEXREGION - The new region can only be represented by more than
603 * one rectangle.
604 * Failure: ERROR
606 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
608 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
609 INT ret;
611 TRACE("%p %d,%d\n", hrgn, x, y);
613 if (!obj)
614 return ERROR;
616 REGION_OffsetRegion( obj->rgn, obj->rgn, x, y);
618 ret = get_region_type( obj );
619 GDI_ReleaseObj( hrgn );
620 return ret;
624 /***********************************************************************
625 * GetRgnBox (GDI32.@)
627 * Retrieves the bounding rectangle of the region. The bounding rectangle
628 * is the smallest rectangle that contains the entire region.
630 * PARAMS
631 * hrgn [I] Region to retrieve bounding rectangle from.
632 * rect [O] Rectangle that will receive the coordinates of the bounding
633 * rectangle.
635 * RETURNS
636 * NULLREGION - The new region is empty.
637 * SIMPLEREGION - The new region can be represented by one rectangle.
638 * COMPLEXREGION - The new region can only be represented by more than
639 * one rectangle.
641 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
643 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
644 if (obj)
646 INT ret;
647 rect->left = obj->rgn->extents.left;
648 rect->top = obj->rgn->extents.top;
649 rect->right = obj->rgn->extents.right;
650 rect->bottom = obj->rgn->extents.bottom;
651 TRACE("%p (%ld,%ld-%ld,%ld)\n", hrgn,
652 rect->left, rect->top, rect->right, rect->bottom);
653 ret = get_region_type( obj );
654 GDI_ReleaseObj(hrgn);
655 return ret;
657 return ERROR;
661 /***********************************************************************
662 * CreateRectRgn (GDI32.@)
664 * Creates a simple rectangular region.
666 * PARAMS
667 * left [I] Left coordinate of rectangle.
668 * top [I] Top coordinate of rectangle.
669 * right [I] Right coordinate of rectangle.
670 * bottom [I] Bottom coordinate of rectangle.
672 * RETURNS
673 * Success: Handle to region.
674 * Failure: NULL.
676 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
678 HRGN hrgn;
680 /* Allocate 2 rects by default to reduce the number of reallocs */
682 if (!(hrgn = REGION_CreateRegion(RGN_DEFAULT_RECTS)))
683 return 0;
684 TRACE("%d,%d-%d,%d\n", left, top, right, bottom);
685 SetRectRgn(hrgn, left, top, right, bottom);
686 return hrgn;
690 /***********************************************************************
691 * CreateRectRgnIndirect (GDI32.@)
693 * Creates a simple rectangular region.
695 * PARAMS
696 * rect [I] Coordinates of rectangular region.
698 * RETURNS
699 * Success: Handle to region.
700 * Failure: NULL.
702 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
704 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
708 /***********************************************************************
709 * SetRectRgn (GDI32.@)
711 * Sets a region to a simple rectangular region.
713 * PARAMS
714 * hrgn [I] Region to convert.
715 * left [I] Left coordinate of rectangle.
716 * top [I] Top coordinate of rectangle.
717 * right [I] Right coordinate of rectangle.
718 * bottom [I] Bottom coordinate of rectangle.
720 * RETURNS
721 * Success: Non-zero.
722 * Failure: Zero.
724 * NOTES
725 * Allows either or both left and top to be greater than right or bottom.
727 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
728 INT right, INT bottom )
730 RGNOBJ * obj;
732 TRACE("%p %d,%d-%d,%d\n", hrgn, left, top, right, bottom );
734 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
736 if (left > right) { INT tmp = left; left = right; right = tmp; }
737 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
739 if((left != right) && (top != bottom))
741 obj->rgn->rects->left = obj->rgn->extents.left = left;
742 obj->rgn->rects->top = obj->rgn->extents.top = top;
743 obj->rgn->rects->right = obj->rgn->extents.right = right;
744 obj->rgn->rects->bottom = obj->rgn->extents.bottom = bottom;
745 obj->rgn->numRects = 1;
747 else
748 EMPTY_REGION(obj->rgn);
750 GDI_ReleaseObj( hrgn );
751 return TRUE;
755 /***********************************************************************
756 * CreateRoundRectRgn (GDI32.@)
758 * Creates a rectangular region with rounded corners.
760 * PARAMS
761 * left [I] Left coordinate of rectangle.
762 * top [I] Top coordinate of rectangle.
763 * right [I] Right coordinate of rectangle.
764 * bottom [I] Bottom coordinate of rectangle.
765 * ellipse_width [I] Width of the ellipse at each corner.
766 * ellipse_height [I] Height of the ellipse at each corner.
768 * RETURNS
769 * Success: Handle to region.
770 * Failure: NULL.
772 * NOTES
773 * If ellipse_width or ellipse_height is less than 2 logical units then
774 * it is treated as though CreateRectRgn() was called instead.
776 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
777 INT right, INT bottom,
778 INT ellipse_width, INT ellipse_height )
780 RGNOBJ * obj;
781 HRGN hrgn;
782 int asq, bsq, d, xd, yd;
783 RECT rect;
785 /* Make the dimensions sensible */
787 if (left > right) { INT tmp = left; left = right; right = tmp; }
788 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
790 ellipse_width = abs(ellipse_width);
791 ellipse_height = abs(ellipse_height);
793 /* Check parameters */
795 if (ellipse_width > right-left) ellipse_width = right-left;
796 if (ellipse_height > bottom-top) ellipse_height = bottom-top;
798 /* Check if we can do a normal rectangle instead */
800 if ((ellipse_width < 2) || (ellipse_height < 2))
801 return CreateRectRgn( left, top, right, bottom );
803 /* Create region */
805 d = (ellipse_height < 128) ? ((3 * ellipse_height) >> 2) : 64;
806 if (!(hrgn = REGION_CreateRegion(d))) return 0;
807 if (!(obj = GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return 0;
808 TRACE("(%d,%d-%d,%d %dx%d): ret=%p\n",
809 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
811 /* Ellipse algorithm, based on an article by K. Porter */
812 /* in DDJ Graphics Programming Column, 8/89 */
814 asq = ellipse_width * ellipse_width / 4; /* a^2 */
815 bsq = ellipse_height * ellipse_height / 4; /* b^2 */
816 d = bsq - asq * ellipse_height / 2 + asq / 4; /* b^2 - a^2b + a^2/4 */
817 xd = 0;
818 yd = asq * ellipse_height; /* 2a^2b */
820 rect.left = left + ellipse_width / 2;
821 rect.right = right - ellipse_width / 2;
823 /* Loop to draw first half of quadrant */
825 while (xd < yd)
827 if (d > 0) /* if nearest pixel is toward the center */
829 /* move toward center */
830 rect.top = top++;
831 rect.bottom = rect.top + 1;
832 REGION_UnionRectWithRegion( &rect, obj->rgn );
833 rect.top = --bottom;
834 rect.bottom = rect.top + 1;
835 REGION_UnionRectWithRegion( &rect, obj->rgn );
836 yd -= 2*asq;
837 d -= yd;
839 rect.left--; /* next horiz point */
840 rect.right++;
841 xd += 2*bsq;
842 d += bsq + xd;
845 /* Loop to draw second half of quadrant */
847 d += (3 * (asq-bsq) / 2 - (xd+yd)) / 2;
848 while (yd >= 0)
850 /* next vertical point */
851 rect.top = top++;
852 rect.bottom = rect.top + 1;
853 REGION_UnionRectWithRegion( &rect, obj->rgn );
854 rect.top = --bottom;
855 rect.bottom = rect.top + 1;
856 REGION_UnionRectWithRegion( &rect, obj->rgn );
857 if (d < 0) /* if nearest pixel is outside ellipse */
859 rect.left--; /* move away from center */
860 rect.right++;
861 xd += 2*bsq;
862 d += xd;
864 yd -= 2*asq;
865 d += asq - yd;
868 /* Add the inside rectangle */
870 if (top <= bottom)
872 rect.top = top;
873 rect.bottom = bottom;
874 REGION_UnionRectWithRegion( &rect, obj->rgn );
876 GDI_ReleaseObj( hrgn );
877 return hrgn;
881 /***********************************************************************
882 * CreateEllipticRgn (GDI32.@)
884 * Creates an elliptical region.
886 * PARAMS
887 * left [I] Left coordinate of bounding rectangle.
888 * top [I] Top coordinate of bounding rectangle.
889 * right [I] Right coordinate of bounding rectangle.
890 * bottom [I] Bottom coordinate of bounding rectangle.
892 * RETURNS
893 * Success: Handle to region.
894 * Failure: NULL.
896 * NOTES
897 * This is a special case of CreateRoundRectRgn() where the width of the
898 * ellipse at each corner is equal to the width the the rectangle and
899 * the same for the height.
901 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
902 INT right, INT bottom )
904 return CreateRoundRectRgn( left, top, right, bottom,
905 right-left, bottom-top );
909 /***********************************************************************
910 * CreateEllipticRgnIndirect (GDI32.@)
912 * Creates an elliptical region.
914 * PARAMS
915 * rect [I] Pointer to bounding rectangle of the ellipse.
917 * RETURNS
918 * Success: Handle to region.
919 * Failure: NULL.
921 * NOTES
922 * This is a special case of CreateRoundRectRgn() where the width of the
923 * ellipse at each corner is equal to the width the the rectangle and
924 * the same for the height.
926 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
928 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
929 rect->bottom, rect->right - rect->left,
930 rect->bottom - rect->top );
933 /***********************************************************************
934 * GetRegionData (GDI32.@)
936 * Retrieves the data that specifies the region.
938 * PARAMS
939 * hrgn [I] Region to retrieve the region data from.
940 * count [I] The size of the buffer pointed to by rgndata in bytes.
941 * rgndata [I] The buffer to receive data about the region.
943 * RETURNS
944 * Success: If rgndata is NULL then the required number of bytes. Otherwise,
945 * the number of bytes copied to the output buffer.
946 * Failure: 0.
948 * NOTES
949 * The format of the Buffer member of RGNDATA is determined by the iType
950 * member of the region data header.
951 * Currently this is always RDH_RECTANGLES, which specifies that the format
952 * is the array of RECT's that specify the region. The length of the array
953 * is specified by the nCount member of the region data header.
955 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
957 DWORD size;
958 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
960 TRACE(" %p count = %ld, rgndata = %p\n", hrgn, count, rgndata);
962 if(!obj) return 0;
964 size = obj->rgn->numRects * sizeof(RECT);
965 if(count < (size + sizeof(RGNDATAHEADER)) || rgndata == NULL)
967 GDI_ReleaseObj( hrgn );
968 if (rgndata) /* buffer is too small, signal it by return 0 */
969 return 0;
970 else /* user requested buffer size with rgndata NULL */
971 return size + sizeof(RGNDATAHEADER);
974 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
975 rgndata->rdh.iType = RDH_RECTANGLES;
976 rgndata->rdh.nCount = obj->rgn->numRects;
977 rgndata->rdh.nRgnSize = size;
978 rgndata->rdh.rcBound.left = obj->rgn->extents.left;
979 rgndata->rdh.rcBound.top = obj->rgn->extents.top;
980 rgndata->rdh.rcBound.right = obj->rgn->extents.right;
981 rgndata->rdh.rcBound.bottom = obj->rgn->extents.bottom;
983 memcpy( rgndata->Buffer, obj->rgn->rects, size );
985 GDI_ReleaseObj( hrgn );
986 return size + sizeof(RGNDATAHEADER);
990 /***********************************************************************
991 * ExtCreateRegion (GDI32.@)
993 * Creates a region as specified by the transformation data and region data.
995 * PARAMS
996 * lpXform [I] World-space to logical-space transformation data.
997 * dwCount [I] Size of the data pointed to by rgndata, in bytes.
998 * rgndata [I] Data that specifes the region.
1000 * RETURNS
1001 * Success: Handle to region.
1002 * Failure: NULL.
1004 * NOTES
1005 * See GetRegionData().
1007 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
1009 HRGN hrgn;
1011 TRACE(" %p %ld %p\n", lpXform, dwCount, rgndata );
1013 if( lpXform )
1014 WARN("(Xform not implemented - ignored)\n");
1016 if( rgndata->rdh.iType != RDH_RECTANGLES )
1018 /* FIXME: We can use CreatePolyPolygonRgn() here
1019 * for trapezoidal data */
1021 WARN("(Unsupported region data type: %lu)\n", rgndata->rdh.iType);
1022 goto fail;
1025 if( (hrgn = REGION_CreateRegion( rgndata->rdh.nCount )) )
1027 RECT *pCurRect, *pEndRect;
1028 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
1030 if (obj) {
1031 pEndRect = (RECT *)rgndata->Buffer + rgndata->rdh.nCount;
1032 for(pCurRect = (RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
1034 if (pCurRect->left < pCurRect->right && pCurRect->top < pCurRect->bottom)
1035 REGION_UnionRectWithRegion( pCurRect, obj->rgn );
1037 GDI_ReleaseObj( hrgn );
1039 TRACE("-- %p\n", hrgn );
1040 return hrgn;
1042 else ERR("Could not get pointer to newborn Region!\n");
1044 fail:
1045 WARN("Failed\n");
1046 return 0;
1050 /***********************************************************************
1051 * PtInRegion (GDI32.@)
1053 * Tests whether the specified point is inside a region.
1055 * PARAMS
1056 * hrgn [I] Region to test.
1057 * x [I] X-coordinate of point to test.
1058 * y [I] Y-coordinate of point to test.
1060 * RETURNS
1061 * Non-zero if the point is inside the region or zero otherwise.
1063 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
1065 RGNOBJ * obj;
1066 BOOL ret = FALSE;
1068 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
1070 int i;
1072 if (obj->rgn->numRects > 0 && INRECT(obj->rgn->extents, x, y))
1073 for (i = 0; i < obj->rgn->numRects; i++)
1074 if (INRECT (obj->rgn->rects[i], x, y))
1076 ret = TRUE;
1077 break;
1079 GDI_ReleaseObj( hrgn );
1081 return ret;
1085 /***********************************************************************
1086 * RectInRegion (GDI32.@)
1088 * Tests if a rectangle is at least partly inside the specified region.
1090 * PARAMS
1091 * hrgn [I] Region to test.
1092 * rect [I] Rectangle to test.
1094 * RETURNS
1095 * Non-zero if the rectangle is partially inside the region or
1096 * zero otherwise.
1098 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
1100 RGNOBJ * obj;
1101 BOOL ret = FALSE;
1103 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
1105 RECT *pCurRect, *pRectEnd;
1107 /* this is (just) a useful optimization */
1108 if ((obj->rgn->numRects > 0) && EXTENTCHECK(&obj->rgn->extents,
1109 rect))
1111 for (pCurRect = obj->rgn->rects, pRectEnd = pCurRect +
1112 obj->rgn->numRects; pCurRect < pRectEnd; pCurRect++)
1114 if (pCurRect->bottom <= rect->top)
1115 continue; /* not far enough down yet */
1117 if (pCurRect->top >= rect->bottom)
1118 break; /* too far down */
1120 if (pCurRect->right <= rect->left)
1121 continue; /* not far enough over yet */
1123 if (pCurRect->left >= rect->right) {
1124 continue;
1127 ret = TRUE;
1128 break;
1131 GDI_ReleaseObj(hrgn);
1133 return ret;
1136 /***********************************************************************
1137 * EqualRgn (GDI32.@)
1139 * Tests whether one region is identical to another.
1141 * PARAMS
1142 * hrgn1 [I] The first region to compare.
1143 * hrgn2 [I] The second region to compare.
1145 * RETURNS
1146 * Non-zero if both regions are identical or zero otherwise.
1148 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
1150 RGNOBJ *obj1, *obj2;
1151 BOOL ret = FALSE;
1153 if ((obj1 = (RGNOBJ *) GDI_GetObjPtr( hrgn1, REGION_MAGIC )))
1155 if ((obj2 = (RGNOBJ *) GDI_GetObjPtr( hrgn2, REGION_MAGIC )))
1157 int i;
1159 if ( obj1->rgn->numRects != obj2->rgn->numRects ) goto done;
1160 if ( obj1->rgn->numRects == 0 )
1162 ret = TRUE;
1163 goto done;
1166 if (obj1->rgn->extents.left != obj2->rgn->extents.left) goto done;
1167 if (obj1->rgn->extents.right != obj2->rgn->extents.right) goto done;
1168 if (obj1->rgn->extents.top != obj2->rgn->extents.top) goto done;
1169 if (obj1->rgn->extents.bottom != obj2->rgn->extents.bottom) goto done;
1170 for( i = 0; i < obj1->rgn->numRects; i++ )
1172 if (obj1->rgn->rects[i].left != obj2->rgn->rects[i].left) goto done;
1173 if (obj1->rgn->rects[i].right != obj2->rgn->rects[i].right) goto done;
1174 if (obj1->rgn->rects[i].top != obj2->rgn->rects[i].top) goto done;
1175 if (obj1->rgn->rects[i].bottom != obj2->rgn->rects[i].bottom) goto done;
1177 ret = TRUE;
1178 done:
1179 GDI_ReleaseObj(hrgn2);
1181 GDI_ReleaseObj(hrgn1);
1183 return ret;
1186 /***********************************************************************
1187 * REGION_UnionRectWithRegion
1188 * Adds a rectangle to a WINEREGION
1190 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1192 WINEREGION region;
1194 region.rects = &region.extents;
1195 region.numRects = 1;
1196 region.size = 1;
1197 region.extents = *rect;
1198 REGION_UnionRegion(rgn, rgn, &region);
1202 /***********************************************************************
1203 * REGION_CreateFrameRgn
1205 * Create a region that is a frame around another region.
1206 * Compute the intersection of the region moved in all 4 directions
1207 * ( +x, -x, +y, -y) and subtract from the original.
1208 * The result looks slightly better than in Windows :)
1210 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1212 BOOL bRet;
1213 RGNOBJ *srcObj = (RGNOBJ*) GDI_GetObjPtr( hSrc, REGION_MAGIC );
1215 if (!srcObj) return FALSE;
1216 if (srcObj->rgn->numRects != 0)
1218 RGNOBJ* destObj = (RGNOBJ*) GDI_GetObjPtr( hDest, REGION_MAGIC );
1219 WINEREGION *tmprgn = REGION_AllocWineRegion( srcObj->rgn->numRects);
1221 REGION_OffsetRegion( destObj->rgn, srcObj->rgn, -x, 0);
1222 REGION_OffsetRegion( tmprgn, srcObj->rgn, x, 0);
1223 REGION_IntersectRegion( destObj->rgn, destObj->rgn, tmprgn);
1224 REGION_OffsetRegion( tmprgn, srcObj->rgn, 0, -y);
1225 REGION_IntersectRegion( destObj->rgn, destObj->rgn, tmprgn);
1226 REGION_OffsetRegion( tmprgn, srcObj->rgn, 0, y);
1227 REGION_IntersectRegion( destObj->rgn, destObj->rgn, tmprgn);
1228 REGION_SubtractRegion( destObj->rgn, srcObj->rgn, destObj->rgn);
1230 REGION_DestroyWineRegion(tmprgn);
1231 GDI_ReleaseObj ( hDest );
1232 bRet = TRUE;
1234 else
1235 bRet = FALSE;
1236 GDI_ReleaseObj( hSrc );
1237 return bRet;
1241 /***********************************************************************
1242 * CombineRgn (GDI32.@)
1244 * Combines two regions with the specifed operation and stores the result
1245 * in the specified destination region.
1247 * PARAMS
1248 * hDest [I] The region that receives the combined result.
1249 * hSrc1 [I] The first source region.
1250 * hSrc2 [I] The second source region.
1251 * mode [I] The way in which the source regions will be combined. See notes.
1253 * RETURNS
1254 * Success:
1255 * NULLREGION - The new region is empty.
1256 * SIMPLEREGION - The new region can be represented by one rectangle.
1257 * COMPLEXREGION - The new region can only be represented by more than
1258 * one rectangle.
1259 * Failure: ERROR
1261 * NOTES
1262 * The two source regions can be the same region.
1263 * The mode can be one of the following:
1264 *| RGN_AND - Intersection of the regions
1265 *| RGN_OR - Union of the regions
1266 *| RGN_XOR - Unions of the regions minus any intersection.
1267 *| RGN_DIFF - Difference (subtraction) of the regions.
1269 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1271 RGNOBJ *destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC);
1272 INT result = ERROR;
1274 TRACE(" %p,%p -> %p mode=%x\n", hSrc1, hSrc2, hDest, mode );
1275 if (destObj)
1277 RGNOBJ *src1Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc1, REGION_MAGIC);
1279 if (src1Obj)
1281 TRACE("dump src1Obj:\n");
1282 if(TRACE_ON(region))
1283 REGION_DumpRegion(src1Obj->rgn);
1284 if (mode == RGN_COPY)
1286 REGION_CopyRegion( destObj->rgn, src1Obj->rgn );
1287 result = get_region_type( destObj );
1289 else
1291 RGNOBJ *src2Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc2, REGION_MAGIC);
1293 if (src2Obj)
1295 TRACE("dump src2Obj:\n");
1296 if(TRACE_ON(region))
1297 REGION_DumpRegion(src2Obj->rgn);
1298 switch (mode)
1300 case RGN_AND:
1301 REGION_IntersectRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn);
1302 break;
1303 case RGN_OR:
1304 REGION_UnionRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1305 break;
1306 case RGN_XOR:
1307 REGION_XorRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1308 break;
1309 case RGN_DIFF:
1310 REGION_SubtractRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1311 break;
1313 result = get_region_type( destObj );
1314 GDI_ReleaseObj( hSrc2 );
1317 GDI_ReleaseObj( hSrc1 );
1319 TRACE("dump destObj:\n");
1320 if(TRACE_ON(region))
1321 REGION_DumpRegion(destObj->rgn);
1323 GDI_ReleaseObj( hDest );
1324 } else {
1325 ERR("Invalid rgn=%p\n", hDest);
1327 return result;
1330 /***********************************************************************
1331 * REGION_SetExtents
1332 * Re-calculate the extents of a region
1334 static void REGION_SetExtents (WINEREGION *pReg)
1336 RECT *pRect, *pRectEnd, *pExtents;
1338 if (pReg->numRects == 0)
1340 pReg->extents.left = 0;
1341 pReg->extents.top = 0;
1342 pReg->extents.right = 0;
1343 pReg->extents.bottom = 0;
1344 return;
1347 pExtents = &pReg->extents;
1348 pRect = pReg->rects;
1349 pRectEnd = &pRect[pReg->numRects - 1];
1352 * Since pRect is the first rectangle in the region, it must have the
1353 * smallest top and since pRectEnd is the last rectangle in the region,
1354 * it must have the largest bottom, because of banding. Initialize left and
1355 * right from pRect and pRectEnd, resp., as good things to initialize them
1356 * to...
1358 pExtents->left = pRect->left;
1359 pExtents->top = pRect->top;
1360 pExtents->right = pRectEnd->right;
1361 pExtents->bottom = pRectEnd->bottom;
1363 while (pRect <= pRectEnd)
1365 if (pRect->left < pExtents->left)
1366 pExtents->left = pRect->left;
1367 if (pRect->right > pExtents->right)
1368 pExtents->right = pRect->right;
1369 pRect++;
1373 /***********************************************************************
1374 * REGION_CopyRegion
1376 static void REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1378 if (dst != src) /* don't want to copy to itself */
1380 if (dst->size < src->numRects)
1382 if (! (dst->rects = HeapReAlloc( GetProcessHeap(), 0, dst->rects,
1383 src->numRects * sizeof(RECT) )))
1384 return;
1385 dst->size = src->numRects;
1387 dst->numRects = src->numRects;
1388 dst->extents.left = src->extents.left;
1389 dst->extents.top = src->extents.top;
1390 dst->extents.right = src->extents.right;
1391 dst->extents.bottom = src->extents.bottom;
1392 memcpy((char *) dst->rects, (char *) src->rects,
1393 (int) (src->numRects * sizeof(RECT)));
1395 return;
1398 /***********************************************************************
1399 * REGION_Coalesce
1401 * Attempt to merge the rects in the current band with those in the
1402 * previous one. Used only by REGION_RegionOp.
1404 * Results:
1405 * The new index for the previous band.
1407 * Side Effects:
1408 * If coalescing takes place:
1409 * - rectangles in the previous band will have their bottom fields
1410 * altered.
1411 * - pReg->numRects will be decreased.
1414 static INT REGION_Coalesce (
1415 WINEREGION *pReg, /* Region to coalesce */
1416 INT prevStart, /* Index of start of previous band */
1417 INT curStart /* Index of start of current band */
1419 RECT *pPrevRect; /* Current rect in previous band */
1420 RECT *pCurRect; /* Current rect in current band */
1421 RECT *pRegEnd; /* End of region */
1422 INT curNumRects; /* Number of rectangles in current band */
1423 INT prevNumRects; /* Number of rectangles in previous band */
1424 INT bandtop; /* top coordinate for current band */
1426 pRegEnd = &pReg->rects[pReg->numRects];
1428 pPrevRect = &pReg->rects[prevStart];
1429 prevNumRects = curStart - prevStart;
1432 * Figure out how many rectangles are in the current band. Have to do
1433 * this because multiple bands could have been added in REGION_RegionOp
1434 * at the end when one region has been exhausted.
1436 pCurRect = &pReg->rects[curStart];
1437 bandtop = pCurRect->top;
1438 for (curNumRects = 0;
1439 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1440 curNumRects++)
1442 pCurRect++;
1445 if (pCurRect != pRegEnd)
1448 * If more than one band was added, we have to find the start
1449 * of the last band added so the next coalescing job can start
1450 * at the right place... (given when multiple bands are added,
1451 * this may be pointless -- see above).
1453 pRegEnd--;
1454 while (pRegEnd[-1].top == pRegEnd->top)
1456 pRegEnd--;
1458 curStart = pRegEnd - pReg->rects;
1459 pRegEnd = pReg->rects + pReg->numRects;
1462 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1463 pCurRect -= curNumRects;
1465 * The bands may only be coalesced if the bottom of the previous
1466 * matches the top scanline of the current.
1468 if (pPrevRect->bottom == pCurRect->top)
1471 * Make sure the bands have rects in the same places. This
1472 * assumes that rects have been added in such a way that they
1473 * cover the most area possible. I.e. two rects in a band must
1474 * have some horizontal space between them.
1478 if ((pPrevRect->left != pCurRect->left) ||
1479 (pPrevRect->right != pCurRect->right))
1482 * The bands don't line up so they can't be coalesced.
1484 return (curStart);
1486 pPrevRect++;
1487 pCurRect++;
1488 prevNumRects -= 1;
1489 } while (prevNumRects != 0);
1491 pReg->numRects -= curNumRects;
1492 pCurRect -= curNumRects;
1493 pPrevRect -= curNumRects;
1496 * The bands may be merged, so set the bottom of each rect
1497 * in the previous band to that of the corresponding rect in
1498 * the current band.
1502 pPrevRect->bottom = pCurRect->bottom;
1503 pPrevRect++;
1504 pCurRect++;
1505 curNumRects -= 1;
1506 } while (curNumRects != 0);
1509 * If only one band was added to the region, we have to backup
1510 * curStart to the start of the previous band.
1512 * If more than one band was added to the region, copy the
1513 * other bands down. The assumption here is that the other bands
1514 * came from the same region as the current one and no further
1515 * coalescing can be done on them since it's all been done
1516 * already... curStart is already in the right place.
1518 if (pCurRect == pRegEnd)
1520 curStart = prevStart;
1522 else
1526 *pPrevRect++ = *pCurRect++;
1527 } while (pCurRect != pRegEnd);
1532 return (curStart);
1535 /***********************************************************************
1536 * REGION_RegionOp
1538 * Apply an operation to two regions. Called by REGION_Union,
1539 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1541 * Results:
1542 * None.
1544 * Side Effects:
1545 * The new region is overwritten.
1547 * Notes:
1548 * The idea behind this function is to view the two regions as sets.
1549 * Together they cover a rectangle of area that this function divides
1550 * into horizontal bands where points are covered only by one region
1551 * or by both. For the first case, the nonOverlapFunc is called with
1552 * each the band and the band's upper and lower extents. For the
1553 * second, the overlapFunc is called to process the entire band. It
1554 * is responsible for clipping the rectangles in the band, though
1555 * this function provides the boundaries.
1556 * At the end of each band, the new region is coalesced, if possible,
1557 * to reduce the number of rectangles in the region.
1560 static void REGION_RegionOp(
1561 WINEREGION *newReg, /* Place to store result */
1562 WINEREGION *reg1, /* First region in operation */
1563 WINEREGION *reg2, /* 2nd region in operation */
1564 void (*overlapFunc)(WINEREGION*, RECT*, RECT*, RECT*, RECT*, INT, INT), /* Function to call for over-lapping bands */
1565 void (*nonOverlap1Func)(WINEREGION*, RECT*, RECT*, INT, INT), /* Function to call for non-overlapping bands in region 1 */
1566 void (*nonOverlap2Func)(WINEREGION*, RECT*, RECT*, INT, INT) /* Function to call for non-overlapping bands in region 2 */
1568 RECT *r1; /* Pointer into first region */
1569 RECT *r2; /* Pointer into 2d region */
1570 RECT *r1End; /* End of 1st region */
1571 RECT *r2End; /* End of 2d region */
1572 INT ybot; /* Bottom of intersection */
1573 INT ytop; /* Top of intersection */
1574 RECT *oldRects; /* Old rects for newReg */
1575 INT prevBand; /* Index of start of
1576 * previous band in newReg */
1577 INT curBand; /* Index of start of current
1578 * band in newReg */
1579 RECT *r1BandEnd; /* End of current band in r1 */
1580 RECT *r2BandEnd; /* End of current band in r2 */
1581 INT top; /* Top of non-overlapping band */
1582 INT bot; /* Bottom of non-overlapping band */
1585 * Initialization:
1586 * set r1, r2, r1End and r2End appropriately, preserve the important
1587 * parts of the destination region until the end in case it's one of
1588 * the two source regions, then mark the "new" region empty, allocating
1589 * another array of rectangles for it to use.
1591 r1 = reg1->rects;
1592 r2 = reg2->rects;
1593 r1End = r1 + reg1->numRects;
1594 r2End = r2 + reg2->numRects;
1598 * newReg may be one of the src regions so we can't empty it. We keep a
1599 * note of its rects pointer (so that we can free them later), preserve its
1600 * extents and simply set numRects to zero.
1603 oldRects = newReg->rects;
1604 newReg->numRects = 0;
1607 * Allocate a reasonable number of rectangles for the new region. The idea
1608 * is to allocate enough so the individual functions don't need to
1609 * reallocate and copy the array, which is time consuming, yet we don't
1610 * have to worry about using too much memory. I hope to be able to
1611 * nuke the Xrealloc() at the end of this function eventually.
1613 newReg->size = max(reg1->numRects,reg2->numRects) * 2;
1615 if (! (newReg->rects = HeapAlloc( GetProcessHeap(), 0,
1616 sizeof(RECT) * newReg->size )))
1618 newReg->size = 0;
1619 return;
1623 * Initialize ybot and ytop.
1624 * In the upcoming loop, ybot and ytop serve different functions depending
1625 * on whether the band being handled is an overlapping or non-overlapping
1626 * band.
1627 * In the case of a non-overlapping band (only one of the regions
1628 * has points in the band), ybot is the bottom of the most recent
1629 * intersection and thus clips the top of the rectangles in that band.
1630 * ytop is the top of the next intersection between the two regions and
1631 * serves to clip the bottom of the rectangles in the current band.
1632 * For an overlapping band (where the two regions intersect), ytop clips
1633 * the top of the rectangles of both regions and ybot clips the bottoms.
1635 if (reg1->extents.top < reg2->extents.top)
1636 ybot = reg1->extents.top;
1637 else
1638 ybot = reg2->extents.top;
1641 * prevBand serves to mark the start of the previous band so rectangles
1642 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1643 * In the beginning, there is no previous band, so prevBand == curBand
1644 * (curBand is set later on, of course, but the first band will always
1645 * start at index 0). prevBand and curBand must be indices because of
1646 * the possible expansion, and resultant moving, of the new region's
1647 * array of rectangles.
1649 prevBand = 0;
1653 curBand = newReg->numRects;
1656 * This algorithm proceeds one source-band (as opposed to a
1657 * destination band, which is determined by where the two regions
1658 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1659 * rectangle after the last one in the current band for their
1660 * respective regions.
1662 r1BandEnd = r1;
1663 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1665 r1BandEnd++;
1668 r2BandEnd = r2;
1669 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1671 r2BandEnd++;
1675 * First handle the band that doesn't intersect, if any.
1677 * Note that attention is restricted to one band in the
1678 * non-intersecting region at once, so if a region has n
1679 * bands between the current position and the next place it overlaps
1680 * the other, this entire loop will be passed through n times.
1682 if (r1->top < r2->top)
1684 top = max(r1->top,ybot);
1685 bot = min(r1->bottom,r2->top);
1687 if ((top != bot) && (nonOverlap1Func != (void (*)())NULL))
1689 (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot);
1692 ytop = r2->top;
1694 else if (r2->top < r1->top)
1696 top = max(r2->top,ybot);
1697 bot = min(r2->bottom,r1->top);
1699 if ((top != bot) && (nonOverlap2Func != (void (*)())NULL))
1701 (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot);
1704 ytop = r1->top;
1706 else
1708 ytop = r1->top;
1712 * If any rectangles got added to the region, try and coalesce them
1713 * with rectangles from the previous band. Note we could just do
1714 * this test in miCoalesce, but some machines incur a not
1715 * inconsiderable cost for function calls, so...
1717 if (newReg->numRects != curBand)
1719 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1723 * Now see if we've hit an intersecting band. The two bands only
1724 * intersect if ybot > ytop
1726 ybot = min(r1->bottom, r2->bottom);
1727 curBand = newReg->numRects;
1728 if (ybot > ytop)
1730 (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);
1734 if (newReg->numRects != curBand)
1736 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1740 * If we've finished with a band (bottom == ybot) we skip forward
1741 * in the region to the next band.
1743 if (r1->bottom == ybot)
1745 r1 = r1BandEnd;
1747 if (r2->bottom == ybot)
1749 r2 = r2BandEnd;
1751 } while ((r1 != r1End) && (r2 != r2End));
1754 * Deal with whichever region still has rectangles left.
1756 curBand = newReg->numRects;
1757 if (r1 != r1End)
1759 if (nonOverlap1Func != (void (*)())NULL)
1763 r1BandEnd = r1;
1764 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1766 r1BandEnd++;
1768 (* nonOverlap1Func) (newReg, r1, r1BandEnd,
1769 max(r1->top,ybot), r1->bottom);
1770 r1 = r1BandEnd;
1771 } while (r1 != r1End);
1774 else if ((r2 != r2End) && (nonOverlap2Func != (void (*)())NULL))
1778 r2BandEnd = r2;
1779 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1781 r2BandEnd++;
1783 (* nonOverlap2Func) (newReg, r2, r2BandEnd,
1784 max(r2->top,ybot), r2->bottom);
1785 r2 = r2BandEnd;
1786 } while (r2 != r2End);
1789 if (newReg->numRects != curBand)
1791 (void) REGION_Coalesce (newReg, prevBand, curBand);
1795 * A bit of cleanup. To keep regions from growing without bound,
1796 * we shrink the array of rectangles to match the new number of
1797 * rectangles in the region. This never goes to 0, however...
1799 * Only do this stuff if the number of rectangles allocated is more than
1800 * twice the number of rectangles in the region (a simple optimization...).
1802 if ((newReg->numRects < (newReg->size >> 1)) && (newReg->numRects > 2))
1804 if (REGION_NOT_EMPTY(newReg))
1806 RECT *prev_rects = newReg->rects;
1807 newReg->size = newReg->numRects;
1808 newReg->rects = HeapReAlloc( GetProcessHeap(), 0, newReg->rects,
1809 sizeof(RECT) * newReg->size );
1810 if (! newReg->rects)
1811 newReg->rects = prev_rects;
1813 else
1816 * No point in doing the extra work involved in an Xrealloc if
1817 * the region is empty
1819 newReg->size = 1;
1820 HeapFree( GetProcessHeap(), 0, newReg->rects );
1821 newReg->rects = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT) );
1824 HeapFree( GetProcessHeap(), 0, oldRects );
1825 return;
1828 /***********************************************************************
1829 * Region Intersection
1830 ***********************************************************************/
1833 /***********************************************************************
1834 * REGION_IntersectO
1836 * Handle an overlapping band for REGION_Intersect.
1838 * Results:
1839 * None.
1841 * Side Effects:
1842 * Rectangles may be added to the region.
1845 static void REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1846 RECT *r2, RECT *r2End, INT top, INT bottom)
1849 INT left, right;
1850 RECT *pNextRect;
1852 pNextRect = &pReg->rects[pReg->numRects];
1854 while ((r1 != r1End) && (r2 != r2End))
1856 left = max(r1->left, r2->left);
1857 right = min(r1->right, r2->right);
1860 * If there's any overlap between the two rectangles, add that
1861 * overlap to the new region.
1862 * There's no need to check for subsumption because the only way
1863 * such a need could arise is if some region has two rectangles
1864 * right next to each other. Since that should never happen...
1866 if (left < right)
1868 MEMCHECK(pReg, pNextRect, pReg->rects);
1869 pNextRect->left = left;
1870 pNextRect->top = top;
1871 pNextRect->right = right;
1872 pNextRect->bottom = bottom;
1873 pReg->numRects += 1;
1874 pNextRect++;
1878 * Need to advance the pointers. Shift the one that extends
1879 * to the right the least, since the other still has a chance to
1880 * overlap with that region's next rectangle, if you see what I mean.
1882 if (r1->right < r2->right)
1884 r1++;
1886 else if (r2->right < r1->right)
1888 r2++;
1890 else
1892 r1++;
1893 r2++;
1896 return;
1899 /***********************************************************************
1900 * REGION_IntersectRegion
1902 static void REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1903 WINEREGION *reg2)
1905 /* check for trivial reject */
1906 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1907 (!EXTENTCHECK(&reg1->extents, &reg2->extents)))
1908 newReg->numRects = 0;
1909 else
1910 REGION_RegionOp (newReg, reg1, reg2, REGION_IntersectO, NULL, NULL);
1913 * Can't alter newReg's extents before we call miRegionOp because
1914 * it might be one of the source regions and miRegionOp depends
1915 * on the extents of those regions being the same. Besides, this
1916 * way there's no checking against rectangles that will be nuked
1917 * due to coalescing, so we have to examine fewer rectangles.
1919 REGION_SetExtents(newReg);
1922 /***********************************************************************
1923 * Region Union
1924 ***********************************************************************/
1926 /***********************************************************************
1927 * REGION_UnionNonO
1929 * Handle a non-overlapping band for the union operation. Just
1930 * Adds the rectangles into the region. Doesn't have to check for
1931 * subsumption or anything.
1933 * Results:
1934 * None.
1936 * Side Effects:
1937 * pReg->numRects is incremented and the final rectangles overwritten
1938 * with the rectangles we're passed.
1941 static void REGION_UnionNonO (WINEREGION *pReg, RECT *r, RECT *rEnd,
1942 INT top, INT bottom)
1944 RECT *pNextRect;
1946 pNextRect = &pReg->rects[pReg->numRects];
1948 while (r != rEnd)
1950 MEMCHECK(pReg, pNextRect, pReg->rects);
1951 pNextRect->left = r->left;
1952 pNextRect->top = top;
1953 pNextRect->right = r->right;
1954 pNextRect->bottom = bottom;
1955 pReg->numRects += 1;
1956 pNextRect++;
1957 r++;
1959 return;
1962 /***********************************************************************
1963 * REGION_UnionO
1965 * Handle an overlapping band for the union operation. Picks the
1966 * left-most rectangle each time and merges it into the region.
1968 * Results:
1969 * None.
1971 * Side Effects:
1972 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1973 * be changed.
1976 static void REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1977 RECT *r2, RECT *r2End, INT top, INT bottom)
1979 RECT *pNextRect;
1981 pNextRect = &pReg->rects[pReg->numRects];
1983 #define MERGERECT(r) \
1984 if ((pReg->numRects != 0) && \
1985 (pNextRect[-1].top == top) && \
1986 (pNextRect[-1].bottom == bottom) && \
1987 (pNextRect[-1].right >= r->left)) \
1989 if (pNextRect[-1].right < r->right) \
1991 pNextRect[-1].right = r->right; \
1994 else \
1996 MEMCHECK(pReg, pNextRect, pReg->rects); \
1997 pNextRect->top = top; \
1998 pNextRect->bottom = bottom; \
1999 pNextRect->left = r->left; \
2000 pNextRect->right = r->right; \
2001 pReg->numRects += 1; \
2002 pNextRect += 1; \
2004 r++;
2006 while ((r1 != r1End) && (r2 != r2End))
2008 if (r1->left < r2->left)
2010 MERGERECT(r1);
2012 else
2014 MERGERECT(r2);
2018 if (r1 != r1End)
2022 MERGERECT(r1);
2023 } while (r1 != r1End);
2025 else while (r2 != r2End)
2027 MERGERECT(r2);
2029 return;
2032 /***********************************************************************
2033 * REGION_UnionRegion
2035 static void REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1,
2036 WINEREGION *reg2)
2038 /* checks all the simple cases */
2041 * Region 1 and 2 are the same or region 1 is empty
2043 if ( (reg1 == reg2) || (!(reg1->numRects)) )
2045 if (newReg != reg2)
2046 REGION_CopyRegion(newReg, reg2);
2047 return;
2051 * if nothing to union (region 2 empty)
2053 if (!(reg2->numRects))
2055 if (newReg != reg1)
2056 REGION_CopyRegion(newReg, reg1);
2057 return;
2061 * Region 1 completely subsumes region 2
2063 if ((reg1->numRects == 1) &&
2064 (reg1->extents.left <= reg2->extents.left) &&
2065 (reg1->extents.top <= reg2->extents.top) &&
2066 (reg1->extents.right >= reg2->extents.right) &&
2067 (reg1->extents.bottom >= reg2->extents.bottom))
2069 if (newReg != reg1)
2070 REGION_CopyRegion(newReg, reg1);
2071 return;
2075 * Region 2 completely subsumes region 1
2077 if ((reg2->numRects == 1) &&
2078 (reg2->extents.left <= reg1->extents.left) &&
2079 (reg2->extents.top <= reg1->extents.top) &&
2080 (reg2->extents.right >= reg1->extents.right) &&
2081 (reg2->extents.bottom >= reg1->extents.bottom))
2083 if (newReg != reg2)
2084 REGION_CopyRegion(newReg, reg2);
2085 return;
2088 REGION_RegionOp (newReg, reg1, reg2, REGION_UnionO, REGION_UnionNonO, REGION_UnionNonO);
2090 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2091 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2092 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2093 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2096 /***********************************************************************
2097 * Region Subtraction
2098 ***********************************************************************/
2100 /***********************************************************************
2101 * REGION_SubtractNonO1
2103 * Deal with non-overlapping band for subtraction. Any parts from
2104 * region 2 we discard. Anything from region 1 we add to the region.
2106 * Results:
2107 * None.
2109 * Side Effects:
2110 * pReg may be affected.
2113 static void REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd,
2114 INT top, INT bottom)
2116 RECT *pNextRect;
2118 pNextRect = &pReg->rects[pReg->numRects];
2120 while (r != rEnd)
2122 MEMCHECK(pReg, pNextRect, pReg->rects);
2123 pNextRect->left = r->left;
2124 pNextRect->top = top;
2125 pNextRect->right = r->right;
2126 pNextRect->bottom = bottom;
2127 pReg->numRects += 1;
2128 pNextRect++;
2129 r++;
2131 return;
2135 /***********************************************************************
2136 * REGION_SubtractO
2138 * Overlapping band subtraction. x1 is the left-most point not yet
2139 * checked.
2141 * Results:
2142 * None.
2144 * Side Effects:
2145 * pReg may have rectangles added to it.
2148 static void REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2149 RECT *r2, RECT *r2End, INT top, INT bottom)
2151 RECT *pNextRect;
2152 INT left;
2154 left = r1->left;
2155 pNextRect = &pReg->rects[pReg->numRects];
2157 while ((r1 != r1End) && (r2 != r2End))
2159 if (r2->right <= left)
2162 * Subtrahend missed the boat: go to next subtrahend.
2164 r2++;
2166 else if (r2->left <= left)
2169 * Subtrahend precedes minuend: nuke left edge of minuend.
2171 left = r2->right;
2172 if (left >= r1->right)
2175 * Minuend completely covered: advance to next minuend and
2176 * reset left fence to edge of new minuend.
2178 r1++;
2179 if (r1 != r1End)
2180 left = r1->left;
2182 else
2185 * Subtrahend now used up since it doesn't extend beyond
2186 * minuend
2188 r2++;
2191 else if (r2->left < r1->right)
2194 * Left part of subtrahend covers part of minuend: add uncovered
2195 * part of minuend to region and skip to next subtrahend.
2197 MEMCHECK(pReg, pNextRect, pReg->rects);
2198 pNextRect->left = left;
2199 pNextRect->top = top;
2200 pNextRect->right = r2->left;
2201 pNextRect->bottom = bottom;
2202 pReg->numRects += 1;
2203 pNextRect++;
2204 left = r2->right;
2205 if (left >= r1->right)
2208 * Minuend used up: advance to new...
2210 r1++;
2211 if (r1 != r1End)
2212 left = r1->left;
2214 else
2217 * Subtrahend used up
2219 r2++;
2222 else
2225 * Minuend used up: add any remaining piece before advancing.
2227 if (r1->right > left)
2229 MEMCHECK(pReg, pNextRect, pReg->rects);
2230 pNextRect->left = left;
2231 pNextRect->top = top;
2232 pNextRect->right = r1->right;
2233 pNextRect->bottom = bottom;
2234 pReg->numRects += 1;
2235 pNextRect++;
2237 r1++;
2238 left = r1->left;
2243 * Add remaining minuend rectangles to region.
2245 while (r1 != r1End)
2247 MEMCHECK(pReg, pNextRect, pReg->rects);
2248 pNextRect->left = left;
2249 pNextRect->top = top;
2250 pNextRect->right = r1->right;
2251 pNextRect->bottom = bottom;
2252 pReg->numRects += 1;
2253 pNextRect++;
2254 r1++;
2255 if (r1 != r1End)
2257 left = r1->left;
2260 return;
2263 /***********************************************************************
2264 * REGION_SubtractRegion
2266 * Subtract regS from regM and leave the result in regD.
2267 * S stands for subtrahend, M for minuend and D for difference.
2269 * Results:
2270 * TRUE.
2272 * Side Effects:
2273 * regD is overwritten.
2276 static void REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM,
2277 WINEREGION *regS )
2279 /* check for trivial reject */
2280 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2281 (!EXTENTCHECK(&regM->extents, &regS->extents)) )
2283 REGION_CopyRegion(regD, regM);
2284 return;
2287 REGION_RegionOp (regD, regM, regS, REGION_SubtractO, REGION_SubtractNonO1, NULL);
2290 * Can't alter newReg's extents before we call miRegionOp because
2291 * it might be one of the source regions and miRegionOp depends
2292 * on the extents of those regions being the unaltered. Besides, this
2293 * way there's no checking against rectangles that will be nuked
2294 * due to coalescing, so we have to examine fewer rectangles.
2296 REGION_SetExtents (regD);
2299 /***********************************************************************
2300 * REGION_XorRegion
2302 static void REGION_XorRegion(WINEREGION *dr, WINEREGION *sra,
2303 WINEREGION *srb)
2305 WINEREGION *tra, *trb;
2307 if ((! (tra = REGION_AllocWineRegion(sra->numRects + 1))) ||
2308 (! (trb = REGION_AllocWineRegion(srb->numRects + 1))))
2309 return;
2310 REGION_SubtractRegion(tra,sra,srb);
2311 REGION_SubtractRegion(trb,srb,sra);
2312 REGION_UnionRegion(dr,tra,trb);
2313 REGION_DestroyWineRegion(tra);
2314 REGION_DestroyWineRegion(trb);
2315 return;
2318 /**************************************************************************
2320 * Poly Regions
2322 *************************************************************************/
2324 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2325 #define SMALL_COORDINATE 0x80000000
2327 /***********************************************************************
2328 * REGION_InsertEdgeInET
2330 * Insert the given edge into the edge table.
2331 * First we must find the correct bucket in the
2332 * Edge table, then find the right slot in the
2333 * bucket. Finally, we can insert it.
2336 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2337 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2340 EdgeTableEntry *start, *prev;
2341 ScanLineList *pSLL, *pPrevSLL;
2342 ScanLineListBlock *tmpSLLBlock;
2345 * find the right bucket to put the edge into
2347 pPrevSLL = &ET->scanlines;
2348 pSLL = pPrevSLL->next;
2349 while (pSLL && (pSLL->scanline < scanline))
2351 pPrevSLL = pSLL;
2352 pSLL = pSLL->next;
2356 * reassign pSLL (pointer to ScanLineList) if necessary
2358 if ((!pSLL) || (pSLL->scanline > scanline))
2360 if (*iSLLBlock > SLLSPERBLOCK-1)
2362 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2363 if(!tmpSLLBlock)
2365 WARN("Can't alloc SLLB\n");
2366 return;
2368 (*SLLBlock)->next = tmpSLLBlock;
2369 tmpSLLBlock->next = (ScanLineListBlock *)NULL;
2370 *SLLBlock = tmpSLLBlock;
2371 *iSLLBlock = 0;
2373 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2375 pSLL->next = pPrevSLL->next;
2376 pSLL->edgelist = (EdgeTableEntry *)NULL;
2377 pPrevSLL->next = pSLL;
2379 pSLL->scanline = scanline;
2382 * now insert the edge in the right bucket
2384 prev = (EdgeTableEntry *)NULL;
2385 start = pSLL->edgelist;
2386 while (start && (start->bres.minor_axis < ETE->bres.minor_axis))
2388 prev = start;
2389 start = start->next;
2391 ETE->next = start;
2393 if (prev)
2394 prev->next = ETE;
2395 else
2396 pSLL->edgelist = ETE;
2399 /***********************************************************************
2400 * REGION_CreateEdgeTable
2402 * This routine creates the edge table for
2403 * scan converting polygons.
2404 * The Edge Table (ET) looks like:
2406 * EdgeTable
2407 * --------
2408 * | ymax | ScanLineLists
2409 * |scanline|-->------------>-------------->...
2410 * -------- |scanline| |scanline|
2411 * |edgelist| |edgelist|
2412 * --------- ---------
2413 * | |
2414 * | |
2415 * V V
2416 * list of ETEs list of ETEs
2418 * where ETE is an EdgeTableEntry data structure,
2419 * and there is one ScanLineList per scanline at
2420 * which an edge is initially entered.
2423 static void REGION_CreateETandAET(const INT *Count, INT nbpolygons,
2424 const POINT *pts, EdgeTable *ET, EdgeTableEntry *AET,
2425 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
2427 const POINT *top, *bottom;
2428 const POINT *PrevPt, *CurrPt, *EndPt;
2429 INT poly, count;
2430 int iSLLBlock = 0;
2431 int dy;
2435 * initialize the Active Edge Table
2437 AET->next = (EdgeTableEntry *)NULL;
2438 AET->back = (EdgeTableEntry *)NULL;
2439 AET->nextWETE = (EdgeTableEntry *)NULL;
2440 AET->bres.minor_axis = SMALL_COORDINATE;
2443 * initialize the Edge Table.
2445 ET->scanlines.next = (ScanLineList *)NULL;
2446 ET->ymax = SMALL_COORDINATE;
2447 ET->ymin = LARGE_COORDINATE;
2448 pSLLBlock->next = (ScanLineListBlock *)NULL;
2450 EndPt = pts - 1;
2451 for(poly = 0; poly < nbpolygons; poly++)
2453 count = Count[poly];
2454 EndPt += count;
2455 if(count < 2)
2456 continue;
2458 PrevPt = EndPt;
2461 * for each vertex in the array of points.
2462 * In this loop we are dealing with two vertices at
2463 * a time -- these make up one edge of the polygon.
2465 while (count--)
2467 CurrPt = pts++;
2470 * find out which point is above and which is below.
2472 if (PrevPt->y > CurrPt->y)
2474 bottom = PrevPt, top = CurrPt;
2475 pETEs->ClockWise = 0;
2477 else
2479 bottom = CurrPt, top = PrevPt;
2480 pETEs->ClockWise = 1;
2484 * don't add horizontal edges to the Edge table.
2486 if (bottom->y != top->y)
2488 pETEs->ymax = bottom->y-1;
2489 /* -1 so we don't get last scanline */
2492 * initialize integer edge algorithm
2494 dy = bottom->y - top->y;
2495 BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
2497 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
2498 &iSLLBlock);
2500 if (PrevPt->y > ET->ymax)
2501 ET->ymax = PrevPt->y;
2502 if (PrevPt->y < ET->ymin)
2503 ET->ymin = PrevPt->y;
2504 pETEs++;
2507 PrevPt = CurrPt;
2512 /***********************************************************************
2513 * REGION_loadAET
2515 * This routine moves EdgeTableEntries from the
2516 * EdgeTable into the Active Edge Table,
2517 * leaving them sorted by smaller x coordinate.
2520 static void REGION_loadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs)
2522 EdgeTableEntry *pPrevAET;
2523 EdgeTableEntry *tmp;
2525 pPrevAET = AET;
2526 AET = AET->next;
2527 while (ETEs)
2529 while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis))
2531 pPrevAET = AET;
2532 AET = AET->next;
2534 tmp = ETEs->next;
2535 ETEs->next = AET;
2536 if (AET)
2537 AET->back = ETEs;
2538 ETEs->back = pPrevAET;
2539 pPrevAET->next = ETEs;
2540 pPrevAET = ETEs;
2542 ETEs = tmp;
2546 /***********************************************************************
2547 * REGION_computeWAET
2549 * This routine links the AET by the
2550 * nextWETE (winding EdgeTableEntry) link for
2551 * use by the winding number rule. The final
2552 * Active Edge Table (AET) might look something
2553 * like:
2555 * AET
2556 * ---------- --------- ---------
2557 * |ymax | |ymax | |ymax |
2558 * | ... | |... | |... |
2559 * |next |->|next |->|next |->...
2560 * |nextWETE| |nextWETE| |nextWETE|
2561 * --------- --------- ^--------
2562 * | | |
2563 * V-------------------> V---> ...
2566 static void REGION_computeWAET(EdgeTableEntry *AET)
2568 register EdgeTableEntry *pWETE;
2569 register int inside = 1;
2570 register int isInside = 0;
2572 AET->nextWETE = (EdgeTableEntry *)NULL;
2573 pWETE = AET;
2574 AET = AET->next;
2575 while (AET)
2577 if (AET->ClockWise)
2578 isInside++;
2579 else
2580 isInside--;
2582 if ((!inside && !isInside) ||
2583 ( inside && isInside))
2585 pWETE->nextWETE = AET;
2586 pWETE = AET;
2587 inside = !inside;
2589 AET = AET->next;
2591 pWETE->nextWETE = (EdgeTableEntry *)NULL;
2594 /***********************************************************************
2595 * REGION_InsertionSort
2597 * Just a simple insertion sort using
2598 * pointers and back pointers to sort the Active
2599 * Edge Table.
2602 static BOOL REGION_InsertionSort(EdgeTableEntry *AET)
2604 EdgeTableEntry *pETEchase;
2605 EdgeTableEntry *pETEinsert;
2606 EdgeTableEntry *pETEchaseBackTMP;
2607 BOOL changed = FALSE;
2609 AET = AET->next;
2610 while (AET)
2612 pETEinsert = AET;
2613 pETEchase = AET;
2614 while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis)
2615 pETEchase = pETEchase->back;
2617 AET = AET->next;
2618 if (pETEchase != pETEinsert)
2620 pETEchaseBackTMP = pETEchase->back;
2621 pETEinsert->back->next = AET;
2622 if (AET)
2623 AET->back = pETEinsert->back;
2624 pETEinsert->next = pETEchase;
2625 pETEchase->back->next = pETEinsert;
2626 pETEchase->back = pETEinsert;
2627 pETEinsert->back = pETEchaseBackTMP;
2628 changed = TRUE;
2631 return changed;
2634 /***********************************************************************
2635 * REGION_FreeStorage
2637 * Clean up our act.
2639 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2641 ScanLineListBlock *tmpSLLBlock;
2643 while (pSLLBlock)
2645 tmpSLLBlock = pSLLBlock->next;
2646 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2647 pSLLBlock = tmpSLLBlock;
2652 /***********************************************************************
2653 * REGION_PtsToRegion
2655 * Create an array of rectangles from a list of points.
2657 static int REGION_PtsToRegion(int numFullPtBlocks, int iCurPtBlock,
2658 POINTBLOCK *FirstPtBlock, WINEREGION *reg)
2660 RECT *rects;
2661 POINT *pts;
2662 POINTBLOCK *CurPtBlock;
2663 int i;
2664 RECT *extents;
2665 INT numRects;
2667 extents = &reg->extents;
2669 numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1;
2671 if (!(reg->rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects,
2672 sizeof(RECT) * numRects )))
2673 return(0);
2675 reg->size = numRects;
2676 CurPtBlock = FirstPtBlock;
2677 rects = reg->rects - 1;
2678 numRects = 0;
2679 extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
2681 for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) {
2682 /* the loop uses 2 points per iteration */
2683 i = NUMPTSTOBUFFER >> 1;
2684 if (!numFullPtBlocks)
2685 i = iCurPtBlock >> 1;
2686 for (pts = CurPtBlock->pts; i--; pts += 2) {
2687 if (pts->x == pts[1].x)
2688 continue;
2689 if (numRects && pts->x == rects->left && pts->y == rects->bottom &&
2690 pts[1].x == rects->right &&
2691 (numRects == 1 || rects[-1].top != rects->top) &&
2692 (i && pts[2].y > pts[1].y)) {
2693 rects->bottom = pts[1].y + 1;
2694 continue;
2696 numRects++;
2697 rects++;
2698 rects->left = pts->x; rects->top = pts->y;
2699 rects->right = pts[1].x; rects->bottom = pts[1].y + 1;
2700 if (rects->left < extents->left)
2701 extents->left = rects->left;
2702 if (rects->right > extents->right)
2703 extents->right = rects->right;
2705 CurPtBlock = CurPtBlock->next;
2708 if (numRects) {
2709 extents->top = reg->rects->top;
2710 extents->bottom = rects->bottom;
2711 } else {
2712 extents->left = 0;
2713 extents->top = 0;
2714 extents->right = 0;
2715 extents->bottom = 0;
2717 reg->numRects = numRects;
2719 return(TRUE);
2722 /***********************************************************************
2723 * CreatePolyPolygonRgn (GDI32.@)
2725 HRGN WINAPI CreatePolyPolygonRgn(const POINT *Pts, const INT *Count,
2726 INT nbpolygons, INT mode)
2728 HRGN hrgn;
2729 RGNOBJ *obj;
2730 WINEREGION *region;
2731 register EdgeTableEntry *pAET; /* Active Edge Table */
2732 register INT y; /* current scanline */
2733 register int iPts = 0; /* number of pts in buffer */
2734 register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/
2735 register ScanLineList *pSLL; /* current scanLineList */
2736 register POINT *pts; /* output buffer */
2737 EdgeTableEntry *pPrevAET; /* ptr to previous AET */
2738 EdgeTable ET; /* header node for ET */
2739 EdgeTableEntry AET; /* header node for AET */
2740 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2741 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2742 int fixWAET = FALSE;
2743 POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */
2744 POINTBLOCK *tmpPtBlock;
2745 int numFullPtBlocks = 0;
2746 INT poly, total;
2748 if(!(hrgn = REGION_CreateRegion(nbpolygons)))
2749 return 0;
2750 obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
2751 region = obj->rgn;
2753 /* special case a rectangle */
2755 if (((nbpolygons == 1) && ((*Count == 4) ||
2756 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2757 (((Pts[0].y == Pts[1].y) &&
2758 (Pts[1].x == Pts[2].x) &&
2759 (Pts[2].y == Pts[3].y) &&
2760 (Pts[3].x == Pts[0].x)) ||
2761 ((Pts[0].x == Pts[1].x) &&
2762 (Pts[1].y == Pts[2].y) &&
2763 (Pts[2].x == Pts[3].x) &&
2764 (Pts[3].y == Pts[0].y))))
2766 SetRectRgn( hrgn, min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2767 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2768 GDI_ReleaseObj( hrgn );
2769 return hrgn;
2772 for(poly = total = 0; poly < nbpolygons; poly++)
2773 total += Count[poly];
2774 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2776 REGION_DeleteObject( hrgn, obj );
2777 return 0;
2779 pts = FirstPtBlock.pts;
2780 REGION_CreateETandAET(Count, nbpolygons, Pts, &ET, &AET, pETEs, &SLLBlock);
2781 pSLL = ET.scanlines.next;
2782 curPtBlock = &FirstPtBlock;
2784 if (mode != WINDING) {
2786 * for each scanline
2788 for (y = ET.ymin; y < ET.ymax; y++) {
2790 * Add a new edge to the active edge table when we
2791 * get to the next edge.
2793 if (pSLL != NULL && y == pSLL->scanline) {
2794 REGION_loadAET(&AET, pSLL->edgelist);
2795 pSLL = pSLL->next;
2797 pPrevAET = &AET;
2798 pAET = AET.next;
2801 * for each active edge
2803 while (pAET) {
2804 pts->x = pAET->bres.minor_axis, pts->y = y;
2805 pts++, iPts++;
2808 * send out the buffer
2810 if (iPts == NUMPTSTOBUFFER) {
2811 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK));
2812 if(!tmpPtBlock) {
2813 WARN("Can't alloc tPB\n");
2814 HeapFree( GetProcessHeap(), 0, pETEs );
2815 return 0;
2817 curPtBlock->next = tmpPtBlock;
2818 curPtBlock = tmpPtBlock;
2819 pts = curPtBlock->pts;
2820 numFullPtBlocks++;
2821 iPts = 0;
2823 EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
2825 REGION_InsertionSort(&AET);
2828 else {
2830 * for each scanline
2832 for (y = ET.ymin; y < ET.ymax; y++) {
2834 * Add a new edge to the active edge table when we
2835 * get to the next edge.
2837 if (pSLL != NULL && y == pSLL->scanline) {
2838 REGION_loadAET(&AET, pSLL->edgelist);
2839 REGION_computeWAET(&AET);
2840 pSLL = pSLL->next;
2842 pPrevAET = &AET;
2843 pAET = AET.next;
2844 pWETE = pAET;
2847 * for each active edge
2849 while (pAET) {
2851 * add to the buffer only those edges that
2852 * are in the Winding active edge table.
2854 if (pWETE == pAET) {
2855 pts->x = pAET->bres.minor_axis, pts->y = y;
2856 pts++, iPts++;
2859 * send out the buffer
2861 if (iPts == NUMPTSTOBUFFER) {
2862 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0,
2863 sizeof(POINTBLOCK) );
2864 if(!tmpPtBlock) {
2865 WARN("Can't alloc tPB\n");
2866 REGION_DeleteObject( hrgn, obj );
2867 HeapFree( GetProcessHeap(), 0, pETEs );
2868 return 0;
2870 curPtBlock->next = tmpPtBlock;
2871 curPtBlock = tmpPtBlock;
2872 pts = curPtBlock->pts;
2873 numFullPtBlocks++; iPts = 0;
2875 pWETE = pWETE->nextWETE;
2877 EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
2881 * recompute the winding active edge table if
2882 * we just resorted or have exited an edge.
2884 if (REGION_InsertionSort(&AET) || fixWAET) {
2885 REGION_computeWAET(&AET);
2886 fixWAET = FALSE;
2890 REGION_FreeStorage(SLLBlock.next);
2891 REGION_PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region);
2893 for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) {
2894 tmpPtBlock = curPtBlock->next;
2895 HeapFree( GetProcessHeap(), 0, curPtBlock );
2896 curPtBlock = tmpPtBlock;
2898 HeapFree( GetProcessHeap(), 0, pETEs );
2899 GDI_ReleaseObj( hrgn );
2900 return hrgn;
2904 /***********************************************************************
2905 * CreatePolygonRgn (GDI32.@)
2907 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count,
2908 INT mode )
2910 return CreatePolyPolygonRgn( points, &count, 1, mode );