Separated 32 bit functions to shellole.c.
[wine/multimedia.git] / objects / region.c
blobd397cf35da4acd0bafbb342346326890976690ab
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 */
11 /************************************************************************
13 Copyright (c) 1987, 1988 X Consortium
15 Permission is hereby granted, free of charge, to any person obtaining a copy
16 of this software and associated documentation files (the "Software"), to deal
17 in the Software without restriction, including without limitation the rights
18 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 copies of the Software, and to permit persons to whom the Software is
20 furnished to do so, subject to the following conditions:
22 The above copyright notice and this permission notice shall be included in
23 all copies or substantial portions of the Software.
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
29 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 Except as contained in this notice, the name of the X Consortium shall not be
33 used in advertising or otherwise to promote the sale, use or other dealings
34 in this Software without prior written authorization from the X Consortium.
37 Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
39 All Rights Reserved
41 Permission to use, copy, modify, and distribute this software and its
42 documentation for any purpose and without fee is hereby granted,
43 provided that the above copyright notice appear in all copies and that
44 both that copyright notice and this permission notice appear in
45 supporting documentation, and that the name of Digital not be
46 used in advertising or publicity pertaining to distribution of the
47 software without specific, written prior permission.
49 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
50 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
51 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
52 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
53 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
54 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
55 SOFTWARE.
57 ************************************************************************/
59 * The functions in this file implement the Region abstraction, similar to one
60 * used in the X11 sample server. A Region is simply an area, as the name
61 * implies, and is implemented as a "y-x-banded" array of rectangles. To
62 * explain: Each Region is made up of a certain number of rectangles sorted
63 * by y coordinate first, and then by x coordinate.
65 * Furthermore, the rectangles are banded such that every rectangle with a
66 * given upper-left y coordinate (y1) will have the same lower-right y
67 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
68 * will span the entire vertical distance of the band. This means that some
69 * areas that could be merged into a taller rectangle will be represented as
70 * several shorter rectangles to account for shorter rectangles to its left
71 * or right but within its "vertical scope".
73 * An added constraint on the rectangles is that they must cover as much
74 * horizontal area as possible. E.g. no two rectangles in a band are allowed
75 * to touch.
77 * Whenever possible, bands will be merged together to cover a greater vertical
78 * distance (and thus reduce the number of rectangles). Two bands can be merged
79 * only if the bottom of one touches the top of the other and they have
80 * rectangles in the same places (of the same width, of course). This maintains
81 * the y-x-banding that's so nice to have...
84 #include <stdlib.h>
85 #include <string.h>
86 #include "windef.h"
87 #include "wingdi.h"
88 #include "winuser.h"
89 #include "debugtools.h"
90 #include "region.h"
91 #include "heap.h"
92 #include "dc.h"
94 DEFAULT_DEBUG_CHANNEL(region);
96 /* 1 if two RECTs overlap.
97 * 0 if two RECTs do not overlap.
99 #define EXTENTCHECK(r1, r2) \
100 ((r1)->right > (r2)->left && \
101 (r1)->left < (r2)->right && \
102 (r1)->bottom > (r2)->top && \
103 (r1)->top < (r2)->bottom)
106 * Check to see if there is enough memory in the present region.
108 #define MEMCHECK(reg, rect, firstrect){\
109 if ((reg)->numRects >= ((reg)->size - 1)){\
110 (firstrect) = HeapReAlloc( GetProcessHeap(), 0, \
111 (firstrect), (2 * (sizeof(RECT)) * ((reg)->size)));\
112 if ((firstrect) == 0)\
113 return;\
114 (reg)->size *= 2;\
115 (rect) = &(firstrect)[(reg)->numRects];\
119 #define EMPTY_REGION(pReg) { \
120 (pReg)->numRects = 0; \
121 (pReg)->extents.left = (pReg)->extents.top = 0; \
122 (pReg)->extents.right = (pReg)->extents.bottom = 0; \
123 (pReg)->type = NULLREGION; \
126 #define REGION_NOT_EMPTY(pReg) pReg->numRects
128 #define INRECT(r, x, y) \
129 ( ( ((r).right > x)) && \
130 ( ((r).left <= x)) && \
131 ( ((r).bottom > y)) && \
132 ( ((r).top <= y)) )
136 * number of points to buffer before sending them off
137 * to scanlines() : Must be an even number
139 #define NUMPTSTOBUFFER 200
142 * used to allocate buffers for points and link
143 * the buffers together
146 typedef struct _POINTBLOCK {
147 POINT pts[NUMPTSTOBUFFER];
148 struct _POINTBLOCK *next;
149 } POINTBLOCK;
154 * This file contains a few macros to help track
155 * the edge of a filled object. The object is assumed
156 * to be filled in scanline order, and thus the
157 * algorithm used is an extension of Bresenham's line
158 * drawing algorithm which assumes that y is always the
159 * major axis.
160 * Since these pieces of code are the same for any filled shape,
161 * it is more convenient to gather the library in one
162 * place, but since these pieces of code are also in
163 * the inner loops of output primitives, procedure call
164 * overhead is out of the question.
165 * See the author for a derivation if needed.
170 * In scan converting polygons, we want to choose those pixels
171 * which are inside the polygon. Thus, we add .5 to the starting
172 * x coordinate for both left and right edges. Now we choose the
173 * first pixel which is inside the pgon for the left edge and the
174 * first pixel which is outside the pgon for the right edge.
175 * Draw the left pixel, but not the right.
177 * How to add .5 to the starting x coordinate:
178 * If the edge is moving to the right, then subtract dy from the
179 * error term from the general form of the algorithm.
180 * If the edge is moving to the left, then add dy to the error term.
182 * The reason for the difference between edges moving to the left
183 * and edges moving to the right is simple: If an edge is moving
184 * to the right, then we want the algorithm to flip immediately.
185 * If it is moving to the left, then we don't want it to flip until
186 * we traverse an entire pixel.
188 #define BRESINITPGON(dy, x1, x2, xStart, d, m, m1, incr1, incr2) { \
189 int dx; /* local storage */ \
191 /* \
192 * if the edge is horizontal, then it is ignored \
193 * and assumed not to be processed. Otherwise, do this stuff. \
194 */ \
195 if ((dy) != 0) { \
196 xStart = (x1); \
197 dx = (x2) - xStart; \
198 if (dx < 0) { \
199 m = dx / (dy); \
200 m1 = m - 1; \
201 incr1 = -2 * dx + 2 * (dy) * m1; \
202 incr2 = -2 * dx + 2 * (dy) * m; \
203 d = 2 * m * (dy) - 2 * dx - 2 * (dy); \
204 } else { \
205 m = dx / (dy); \
206 m1 = m + 1; \
207 incr1 = 2 * dx - 2 * (dy) * m1; \
208 incr2 = 2 * dx - 2 * (dy) * m; \
209 d = -2 * m * (dy) + 2 * dx; \
214 #define BRESINCRPGON(d, minval, m, m1, incr1, incr2) { \
215 if (m1 > 0) { \
216 if (d > 0) { \
217 minval += m1; \
218 d += incr1; \
220 else { \
221 minval += m; \
222 d += incr2; \
224 } else {\
225 if (d >= 0) { \
226 minval += m1; \
227 d += incr1; \
229 else { \
230 minval += m; \
231 d += incr2; \
237 * This structure contains all of the information needed
238 * to run the bresenham algorithm.
239 * The variables may be hardcoded into the declarations
240 * instead of using this structure to make use of
241 * register declarations.
243 typedef struct {
244 INT minor_axis; /* minor axis */
245 INT d; /* decision variable */
246 INT m, m1; /* slope and slope+1 */
247 INT incr1, incr2; /* error increments */
248 } BRESINFO;
251 #define BRESINITPGONSTRUCT(dmaj, min1, min2, bres) \
252 BRESINITPGON(dmaj, min1, min2, bres.minor_axis, bres.d, \
253 bres.m, bres.m1, bres.incr1, bres.incr2)
255 #define BRESINCRPGONSTRUCT(bres) \
256 BRESINCRPGON(bres.d, bres.minor_axis, bres.m, bres.m1, bres.incr1, bres.incr2)
261 * These are the data structures needed to scan
262 * convert regions. Two different scan conversion
263 * methods are available -- the even-odd method, and
264 * the winding number method.
265 * The even-odd rule states that a point is inside
266 * the polygon if a ray drawn from that point in any
267 * direction will pass through an odd number of
268 * path segments.
269 * By the winding number rule, a point is decided
270 * to be inside the polygon if a ray drawn from that
271 * point in any direction passes through a different
272 * number of clockwise and counter-clockwise path
273 * segments.
275 * These data structures are adapted somewhat from
276 * the algorithm in (Foley/Van Dam) for scan converting
277 * polygons.
278 * The basic algorithm is to start at the top (smallest y)
279 * of the polygon, stepping down to the bottom of
280 * the polygon by incrementing the y coordinate. We
281 * keep a list of edges which the current scanline crosses,
282 * sorted by x. This list is called the Active Edge Table (AET)
283 * As we change the y-coordinate, we update each entry in
284 * in the active edge table to reflect the edges new xcoord.
285 * This list must be sorted at each scanline in case
286 * two edges intersect.
287 * We also keep a data structure known as the Edge Table (ET),
288 * which keeps track of all the edges which the current
289 * scanline has not yet reached. The ET is basically a
290 * list of ScanLineList structures containing a list of
291 * edges which are entered at a given scanline. There is one
292 * ScanLineList per scanline at which an edge is entered.
293 * When we enter a new edge, we move it from the ET to the AET.
295 * From the AET, we can implement the even-odd rule as in
296 * (Foley/Van Dam).
297 * The winding number rule is a little trickier. We also
298 * keep the EdgeTableEntries in the AET linked by the
299 * nextWETE (winding EdgeTableEntry) link. This allows
300 * the edges to be linked just as before for updating
301 * purposes, but only uses the edges linked by the nextWETE
302 * link as edges representing spans of the polygon to
303 * drawn (as with the even-odd rule).
307 * for the winding number rule
309 #define CLOCKWISE 1
310 #define COUNTERCLOCKWISE -1
312 typedef struct _EdgeTableEntry {
313 INT ymax; /* ycoord at which we exit this edge. */
314 BRESINFO bres; /* Bresenham info to run the edge */
315 struct _EdgeTableEntry *next; /* next in the list */
316 struct _EdgeTableEntry *back; /* for insertion sort */
317 struct _EdgeTableEntry *nextWETE; /* for winding num rule */
318 int ClockWise; /* flag for winding number rule */
319 } EdgeTableEntry;
322 typedef struct _ScanLineList{
323 INT scanline; /* the scanline represented */
324 EdgeTableEntry *edgelist; /* header node */
325 struct _ScanLineList *next; /* next in the list */
326 } ScanLineList;
329 typedef struct {
330 INT ymax; /* ymax for the polygon */
331 INT ymin; /* ymin for the polygon */
332 ScanLineList scanlines; /* header node */
333 } EdgeTable;
337 * Here is a struct to help with storage allocation
338 * so we can allocate a big chunk at a time, and then take
339 * pieces from this heap when we need to.
341 #define SLLSPERBLOCK 25
343 typedef struct _ScanLineListBlock {
344 ScanLineList SLLs[SLLSPERBLOCK];
345 struct _ScanLineListBlock *next;
346 } ScanLineListBlock;
351 * a few macros for the inner loops of the fill code where
352 * performance considerations don't allow a procedure call.
354 * Evaluate the given edge at the given scanline.
355 * If the edge has expired, then we leave it and fix up
356 * the active edge table; otherwise, we increment the
357 * x value to be ready for the next scanline.
358 * The winding number rule is in effect, so we must notify
359 * the caller when the edge has been removed so he
360 * can reorder the Winding Active Edge Table.
362 #define EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET) { \
363 if (pAET->ymax == y) { /* leaving this edge */ \
364 pPrevAET->next = pAET->next; \
365 pAET = pPrevAET->next; \
366 fixWAET = 1; \
367 if (pAET) \
368 pAET->back = pPrevAET; \
370 else { \
371 BRESINCRPGONSTRUCT(pAET->bres); \
372 pPrevAET = pAET; \
373 pAET = pAET->next; \
379 * Evaluate the given edge at the given scanline.
380 * If the edge has expired, then we leave it and fix up
381 * the active edge table; otherwise, we increment the
382 * x value to be ready for the next scanline.
383 * The even-odd rule is in effect.
385 #define EVALUATEEDGEEVENODD(pAET, pPrevAET, y) { \
386 if (pAET->ymax == y) { /* leaving this edge */ \
387 pPrevAET->next = pAET->next; \
388 pAET = pPrevAET->next; \
389 if (pAET) \
390 pAET->back = pPrevAET; \
392 else { \
393 BRESINCRPGONSTRUCT(pAET->bres); \
394 pPrevAET = pAET; \
395 pAET = pAET->next; \
399 typedef void (*voidProcp)();
401 /* Note the parameter order is different from the X11 equivalents */
403 static void REGION_CopyRegion(WINEREGION *d, WINEREGION *s);
404 static void REGION_IntersectRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
405 static void REGION_UnionRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
406 static void REGION_SubtractRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
407 static void REGION_XorRegion(WINEREGION *d, WINEREGION *s1, WINEREGION *s2);
408 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn);
410 #define RGN_DEFAULT_RECTS 2
412 /***********************************************************************
413 * REGION_DumpRegion
414 * Outputs the contents of a WINEREGION
416 static void REGION_DumpRegion(WINEREGION *pReg)
418 RECT *pRect, *pRectEnd = pReg->rects + pReg->numRects;
420 TRACE("Region %p: %d,%d - %d,%d %d rects\n", pReg,
421 pReg->extents.left, pReg->extents.top,
422 pReg->extents.right, pReg->extents.bottom, pReg->numRects);
423 for(pRect = pReg->rects; pRect < pRectEnd; pRect++)
424 TRACE("\t%d,%d - %d,%d\n", pRect->left, pRect->top,
425 pRect->right, pRect->bottom);
426 return;
430 /***********************************************************************
431 * REGION_AllocWineRegion
432 * Create a new empty WINEREGION.
434 static WINEREGION *REGION_AllocWineRegion( INT n )
436 WINEREGION *pReg;
438 if ((pReg = HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION ))))
440 if ((pReg->rects = HeapAlloc(GetProcessHeap(), 0, n * sizeof( RECT ))))
442 pReg->size = n;
443 EMPTY_REGION(pReg);
444 return pReg;
446 HeapFree(GetProcessHeap(), 0, pReg);
448 return NULL;
452 /***********************************************************************
453 * REGION_CreateRegion
454 * Create a new empty region.
456 static HRGN REGION_CreateRegion( INT n )
458 HRGN hrgn;
459 RGNOBJ *obj;
461 if(!(hrgn = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC )))
462 return 0;
463 obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
464 if(!(obj->rgn = REGION_AllocWineRegion(n))) {
465 GDI_FreeObject( hrgn );
466 return 0;
468 GDI_HEAP_UNLOCK( hrgn );
469 return hrgn;
473 /***********************************************************************
474 * REGION_DestroyWineRegion
476 static void REGION_DestroyWineRegion( WINEREGION* pReg )
478 HeapFree( GetProcessHeap(), 0, pReg->rects );
479 HeapFree( GetProcessHeap(), 0, pReg );
480 return;
483 /***********************************************************************
484 * REGION_DeleteObject
486 BOOL REGION_DeleteObject( HRGN hrgn, RGNOBJ * obj )
488 TRACE(" %04x\n", hrgn );
490 REGION_DestroyWineRegion( obj->rgn );
491 return GDI_FreeObject( hrgn );
494 /***********************************************************************
495 * OffsetRgn16 (GDI.101)
497 INT16 WINAPI OffsetRgn16( HRGN16 hrgn, INT16 x, INT16 y )
499 return OffsetRgn( hrgn, x, y );
502 /***********************************************************************
503 * OffsetRgn (GDI32.256)
505 INT WINAPI OffsetRgn( HRGN hrgn, INT x, INT y )
507 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
508 INT ret;
510 TRACE("%04x %d,%d\n", hrgn, x, y);
512 if (!obj)
513 return ERROR;
515 if(x || y) {
516 int nbox = obj->rgn->numRects;
517 RECT *pbox = obj->rgn->rects;
519 if(nbox) {
520 while(nbox--) {
521 pbox->left += x;
522 pbox->right += x;
523 pbox->top += y;
524 pbox->bottom += y;
525 pbox++;
527 obj->rgn->extents.left += x;
528 obj->rgn->extents.right += x;
529 obj->rgn->extents.top += y;
530 obj->rgn->extents.bottom += y;
533 ret = obj->rgn->type;
534 GDI_HEAP_UNLOCK( hrgn );
535 return ret;
539 /***********************************************************************
540 * GetRgnBox16 (GDI.134)
542 INT16 WINAPI GetRgnBox16( HRGN16 hrgn, LPRECT16 rect )
544 RECT r;
545 INT16 ret = (INT16)GetRgnBox( hrgn, &r );
546 CONV_RECT32TO16( &r, rect );
547 return ret;
550 /***********************************************************************
551 * GetRgnBox (GDI32.219)
553 INT WINAPI GetRgnBox( HRGN hrgn, LPRECT rect )
555 RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
556 if (obj)
558 INT ret;
559 TRACE(" %04x\n", hrgn );
560 rect->left = obj->rgn->extents.left;
561 rect->top = obj->rgn->extents.top;
562 rect->right = obj->rgn->extents.right;
563 rect->bottom = obj->rgn->extents.bottom;
564 ret = obj->rgn->type;
565 GDI_HEAP_UNLOCK(hrgn);
566 return ret;
568 return ERROR;
572 /***********************************************************************
573 * CreateRectRgn16 (GDI.64)
575 * NOTE: Doesn't call CreateRectRgn because of differences in SetRectRgn16/32
577 HRGN16 WINAPI CreateRectRgn16(INT16 left, INT16 top, INT16 right, INT16 bottom)
579 HRGN16 hrgn;
581 if (!(hrgn = (HRGN16)REGION_CreateRegion(RGN_DEFAULT_RECTS)))
582 return 0;
583 TRACE("\n");
584 SetRectRgn16(hrgn, left, top, right, bottom);
585 return hrgn;
589 /***********************************************************************
590 * CreateRectRgn (GDI32.59)
592 HRGN WINAPI CreateRectRgn(INT left, INT top, INT right, INT bottom)
594 HRGN hrgn;
596 /* Allocate 2 rects by default to reduce the number of reallocs */
598 if (!(hrgn = REGION_CreateRegion(RGN_DEFAULT_RECTS)))
599 return 0;
600 TRACE("\n");
601 SetRectRgn(hrgn, left, top, right, bottom);
602 return hrgn;
605 /***********************************************************************
606 * CreateRectRgnIndirect16 (GDI.65)
608 HRGN16 WINAPI CreateRectRgnIndirect16( const RECT16* rect )
610 return CreateRectRgn16( rect->left, rect->top, rect->right, rect->bottom );
614 /***********************************************************************
615 * CreateRectRgnIndirect (GDI32.60)
617 HRGN WINAPI CreateRectRgnIndirect( const RECT* rect )
619 return CreateRectRgn( rect->left, rect->top, rect->right, rect->bottom );
623 /***********************************************************************
624 * SetRectRgn16 (GDI.172)
626 * NOTE: Win 3.1 sets region to empty if left > right
628 VOID WINAPI SetRectRgn16( HRGN16 hrgn, INT16 left, INT16 top,
629 INT16 right, INT16 bottom )
631 if(left < right)
632 SetRectRgn( hrgn, left, top, right, bottom );
633 else
634 SetRectRgn( hrgn, 0, 0, 0, 0 );
638 /***********************************************************************
639 * SetRectRgn (GDI32.332)
641 * Allows either or both left and top to be greater than right or bottom.
643 BOOL WINAPI SetRectRgn( HRGN hrgn, INT left, INT top,
644 INT right, INT bottom )
646 RGNOBJ * obj;
648 TRACE(" %04x %d,%d-%d,%d\n",
649 hrgn, left, top, right, bottom );
651 if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
653 if (left > right) { INT tmp = left; left = right; right = tmp; }
654 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
656 if((left != right) && (top != bottom))
658 obj->rgn->rects->left = obj->rgn->extents.left = left;
659 obj->rgn->rects->top = obj->rgn->extents.top = top;
660 obj->rgn->rects->right = obj->rgn->extents.right = right;
661 obj->rgn->rects->bottom = obj->rgn->extents.bottom = bottom;
662 obj->rgn->numRects = 1;
663 obj->rgn->type = SIMPLEREGION;
665 else
666 EMPTY_REGION(obj->rgn);
668 GDI_HEAP_UNLOCK( hrgn );
669 return TRUE;
673 /***********************************************************************
674 * CreateRoundRectRgn16 (GDI.444)
676 * If either ellipse dimension is zero we call CreateRectRgn16 for its
677 * `special' behaviour. -ve ellipse dimensions can result in GPFs under win3.1
678 * we just let CreateRoundRectRgn convert them to +ve values.
681 HRGN16 WINAPI CreateRoundRectRgn16( INT16 left, INT16 top,
682 INT16 right, INT16 bottom,
683 INT16 ellipse_width, INT16 ellipse_height )
685 if( ellipse_width == 0 || ellipse_height == 0 )
686 return CreateRectRgn16( left, top, right, bottom );
687 else
688 return (HRGN16)CreateRoundRectRgn( left, top, right, bottom,
689 ellipse_width, ellipse_height );
692 /***********************************************************************
693 * CreateRoundRectRgn (GDI32.61)
695 HRGN WINAPI CreateRoundRectRgn( INT left, INT top,
696 INT right, INT bottom,
697 INT ellipse_width, INT ellipse_height )
699 RGNOBJ * obj;
700 HRGN hrgn;
701 int asq, bsq, d, xd, yd;
702 RECT rect;
704 /* Check if we can do a normal rectangle instead */
706 if ((ellipse_width == 0) || (ellipse_height == 0))
707 return CreateRectRgn( left, top, right, bottom );
709 /* Make the dimensions sensible */
711 if (left > right) { INT tmp = left; left = right; right = tmp; }
712 if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
714 ellipse_width = abs(ellipse_width);
715 ellipse_height = abs(ellipse_height);
717 /* Create region */
719 d = (ellipse_height < 128) ? ((3 * ellipse_height) >> 2) : 64;
720 if (!(hrgn = REGION_CreateRegion(d))) return 0;
721 obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
722 TRACE("(%d,%d-%d,%d %dx%d): ret=%04x\n",
723 left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
725 /* Check parameters */
727 if (ellipse_width > right-left) ellipse_width = right-left;
728 if (ellipse_height > bottom-top) ellipse_height = bottom-top;
730 /* Ellipse algorithm, based on an article by K. Porter */
731 /* in DDJ Graphics Programming Column, 8/89 */
733 asq = ellipse_width * ellipse_width / 4; /* a^2 */
734 bsq = ellipse_height * ellipse_height / 4; /* b^2 */
735 d = bsq - asq * ellipse_height / 2 + asq / 4; /* b^2 - a^2b + a^2/4 */
736 xd = 0;
737 yd = asq * ellipse_height; /* 2a^2b */
739 rect.left = left + ellipse_width / 2;
740 rect.right = right - ellipse_width / 2;
742 /* Loop to draw first half of quadrant */
744 while (xd < yd)
746 if (d > 0) /* if nearest pixel is toward the center */
748 /* move toward center */
749 rect.top = top++;
750 rect.bottom = rect.top + 1;
751 REGION_UnionRectWithRegion( &rect, obj->rgn );
752 rect.top = --bottom;
753 rect.bottom = rect.top + 1;
754 REGION_UnionRectWithRegion( &rect, obj->rgn );
755 yd -= 2*asq;
756 d -= yd;
758 rect.left--; /* next horiz point */
759 rect.right++;
760 xd += 2*bsq;
761 d += bsq + xd;
764 /* Loop to draw second half of quadrant */
766 d += (3 * (asq-bsq) / 2 - (xd+yd)) / 2;
767 while (yd >= 0)
769 /* next vertical point */
770 rect.top = top++;
771 rect.bottom = rect.top + 1;
772 REGION_UnionRectWithRegion( &rect, obj->rgn );
773 rect.top = --bottom;
774 rect.bottom = rect.top + 1;
775 REGION_UnionRectWithRegion( &rect, obj->rgn );
776 if (d < 0) /* if nearest pixel is outside ellipse */
778 rect.left--; /* move away from center */
779 rect.right++;
780 xd += 2*bsq;
781 d += xd;
783 yd -= 2*asq;
784 d += asq - yd;
787 /* Add the inside rectangle */
789 if (top <= bottom)
791 rect.top = top;
792 rect.bottom = bottom;
793 REGION_UnionRectWithRegion( &rect, obj->rgn );
795 obj->rgn->type = SIMPLEREGION; /* FIXME? */
796 GDI_HEAP_UNLOCK( hrgn );
797 return hrgn;
801 /***********************************************************************
802 * CreateEllipticRgn16 (GDI.54)
804 HRGN16 WINAPI CreateEllipticRgn16( INT16 left, INT16 top,
805 INT16 right, INT16 bottom )
807 return (HRGN16)CreateRoundRectRgn( left, top, right, bottom,
808 right-left, bottom-top );
812 /***********************************************************************
813 * CreateEllipticRgn (GDI32.39)
815 HRGN WINAPI CreateEllipticRgn( INT left, INT top,
816 INT right, INT bottom )
818 return CreateRoundRectRgn( left, top, right, bottom,
819 right-left, bottom-top );
823 /***********************************************************************
824 * CreateEllipticRgnIndirect16 (GDI.55)
826 HRGN16 WINAPI CreateEllipticRgnIndirect16( const RECT16 *rect )
828 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
829 rect->bottom, rect->right - rect->left,
830 rect->bottom - rect->top );
834 /***********************************************************************
835 * CreateEllipticRgnIndirect (GDI32.40)
837 HRGN WINAPI CreateEllipticRgnIndirect( const RECT *rect )
839 return CreateRoundRectRgn( rect->left, rect->top, rect->right,
840 rect->bottom, rect->right - rect->left,
841 rect->bottom - rect->top );
844 /***********************************************************************
845 * GetRegionData (GDI32.217)
848 DWORD WINAPI GetRegionData(HRGN hrgn, DWORD count, LPRGNDATA rgndata)
850 DWORD size;
851 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
853 TRACE(" %04x count = %ld, rgndata = %p\n",
854 hrgn, count, rgndata);
856 if(!obj) return 0;
858 size = obj->rgn->numRects * sizeof(RECT);
859 if(count < (size + sizeof(RGNDATAHEADER)) || rgndata == NULL)
861 GDI_HEAP_UNLOCK( hrgn );
862 return size + sizeof(RGNDATAHEADER);
865 rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
866 rgndata->rdh.iType = RDH_RECTANGLES;
867 rgndata->rdh.nCount = obj->rgn->numRects;
868 rgndata->rdh.nRgnSize = size;
869 rgndata->rdh.rcBound.left = obj->rgn->extents.left;
870 rgndata->rdh.rcBound.top = obj->rgn->extents.top;
871 rgndata->rdh.rcBound.right = obj->rgn->extents.right;
872 rgndata->rdh.rcBound.bottom = obj->rgn->extents.bottom;
874 memcpy( rgndata->Buffer, obj->rgn->rects, size );
876 GDI_HEAP_UNLOCK( hrgn );
877 return 1;
880 /***********************************************************************
881 * GetRegionData16 (GDI.607)
882 * FIXME: is LPRGNDATA the same in Win16 and Win32 ?
884 DWORD WINAPI GetRegionData16(HRGN16 hrgn, DWORD count, LPRGNDATA rgndata)
886 return GetRegionData((HRGN)hrgn, count, rgndata);
889 /***********************************************************************
890 * ExtCreateRegion (GDI32.94)
893 HRGN WINAPI ExtCreateRegion( const XFORM* lpXform, DWORD dwCount, const RGNDATA* rgndata)
895 HRGN hrgn;
897 TRACE(" %p %ld %p = ", lpXform, dwCount, rgndata );
899 if( lpXform )
900 WARN("(Xform not implemented - ignored) ");
902 if( rgndata->rdh.iType != RDH_RECTANGLES )
904 /* FIXME: We can use CreatePolyPolygonRgn() here
905 * for trapezoidal data */
907 WARN("(Unsupported region data) ");
908 goto fail;
911 if( (hrgn = REGION_CreateRegion( rgndata->rdh.nCount )) )
913 RECT *pCurRect, *pEndRect;
914 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
916 pEndRect = (RECT *)rgndata->Buffer + rgndata->rdh.nCount;
917 for(pCurRect = (RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
918 REGION_UnionRectWithRegion( pCurRect, obj->rgn );
919 GDI_HEAP_UNLOCK( hrgn );
921 TRACE("%04x\n", hrgn );
922 return hrgn;
924 fail:
925 WARN("Failed\n");
926 return 0;
929 /***********************************************************************
930 * PtInRegion16 (GDI.161)
932 BOOL16 WINAPI PtInRegion16( HRGN16 hrgn, INT16 x, INT16 y )
934 return PtInRegion( hrgn, x, y );
938 /***********************************************************************
939 * PtInRegion (GDI32.278)
941 BOOL WINAPI PtInRegion( HRGN hrgn, INT x, INT y )
943 RGNOBJ * obj;
945 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
947 BOOL ret = FALSE;
948 int i;
950 if (obj->rgn->numRects > 0 && INRECT(obj->rgn->extents, x, y))
951 for (i = 0; i < obj->rgn->numRects; i++)
952 if (INRECT (obj->rgn->rects[i], x, y))
953 ret = TRUE;
954 GDI_HEAP_UNLOCK( hrgn );
955 return ret;
957 return FALSE;
961 /***********************************************************************
962 * RectInRegion16 (GDI.181)
964 BOOL16 WINAPI RectInRegion16( HRGN16 hrgn, const RECT16 *rect )
966 RECT r32;
968 CONV_RECT16TO32(rect, &r32);
969 return (BOOL16)RectInRegion(hrgn, &r32);
973 /***********************************************************************
974 * RectInRegion (GDI32.281)
976 * Returns TRUE if rect is at least partly inside hrgn
978 BOOL WINAPI RectInRegion( HRGN hrgn, const RECT *rect )
980 RGNOBJ * obj;
982 if ((obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC )))
984 RECT *pCurRect, *pRectEnd;
985 BOOL ret = FALSE;
987 /* this is (just) a useful optimization */
988 if ((obj->rgn->numRects > 0) && EXTENTCHECK(&obj->rgn->extents,
989 rect))
991 for (pCurRect = obj->rgn->rects, pRectEnd = pCurRect +
992 obj->rgn->numRects; pCurRect < pRectEnd; pCurRect++)
994 if (pCurRect->bottom <= rect->top)
995 continue; /* not far enough down yet */
997 if (pCurRect->top >= rect->bottom) {
998 ret = FALSE; /* too far down */
999 break;
1002 if (pCurRect->right <= rect->left)
1003 continue; /* not far enough over yet */
1005 if (pCurRect->left >= rect->right) {
1006 continue;
1009 ret = TRUE;
1010 break;
1013 GDI_HEAP_UNLOCK(hrgn);
1014 return ret;
1016 return FALSE;
1019 /***********************************************************************
1020 * EqualRgn16 (GDI.72)
1022 BOOL16 WINAPI EqualRgn16( HRGN16 rgn1, HRGN16 rgn2 )
1024 return EqualRgn( rgn1, rgn2 );
1028 /***********************************************************************
1029 * EqualRgn (GDI32.90)
1031 BOOL WINAPI EqualRgn( HRGN hrgn1, HRGN hrgn2 )
1033 RGNOBJ *obj1, *obj2;
1034 BOOL ret = FALSE;
1036 if ((obj1 = (RGNOBJ *) GDI_GetObjPtr( hrgn1, REGION_MAGIC )))
1038 if ((obj2 = (RGNOBJ *) GDI_GetObjPtr( hrgn2, REGION_MAGIC )))
1040 int i;
1042 if ( obj1->rgn->numRects != obj2->rgn->numRects ) goto done;
1043 if ( obj1->rgn->numRects == 0 )
1045 ret = TRUE;
1046 goto done;
1049 if (obj1->rgn->extents.left != obj2->rgn->extents.left) goto done;
1050 if (obj1->rgn->extents.right != obj2->rgn->extents.right) goto done;
1051 if (obj1->rgn->extents.top != obj2->rgn->extents.top) goto done;
1052 if (obj1->rgn->extents.bottom != obj2->rgn->extents.bottom) goto done;
1053 for( i = 0; i < obj1->rgn->numRects; i++ )
1055 if (obj1->rgn->rects[i].left != obj2->rgn->rects[i].left) goto done;
1056 if (obj1->rgn->rects[i].right != obj2->rgn->rects[i].right) goto done;
1057 if (obj1->rgn->rects[i].top != obj2->rgn->rects[i].top) goto done;
1058 if (obj1->rgn->rects[i].bottom != obj2->rgn->rects[i].bottom) goto done;
1060 ret = TRUE;
1061 done:
1062 GDI_HEAP_UNLOCK(hrgn2);
1064 GDI_HEAP_UNLOCK(hrgn1);
1066 return ret;
1068 /***********************************************************************
1069 * REGION_UnionRectWithRegion
1070 * Adds a rectangle to a WINEREGION
1071 * See below for REGION_UnionRectWithRgn
1073 static void REGION_UnionRectWithRegion(const RECT *rect, WINEREGION *rgn)
1075 WINEREGION region;
1077 region.rects = &region.extents;
1078 region.numRects = 1;
1079 region.size = 1;
1080 region.type = SIMPLEREGION;
1081 region.extents = *rect;
1082 REGION_UnionRegion(rgn, rgn, &region);
1083 return;
1086 /***********************************************************************
1087 * REGION_UnionRectWithRgn
1088 * Adds a rectangle to a HRGN
1089 * A helper used by scroll.c
1091 BOOL REGION_UnionRectWithRgn( HRGN hrgn, const RECT *lpRect )
1093 RGNOBJ *obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
1095 if(!obj) return FALSE;
1096 REGION_UnionRectWithRegion( lpRect, obj->rgn );
1097 GDI_HEAP_UNLOCK(hrgn);
1098 return TRUE;
1101 /***********************************************************************
1102 * REGION_CreateFrameRgn
1104 * Create a region that is a frame around another region.
1105 * Expand all rectangles by +/- x and y, then subtract original region.
1107 BOOL REGION_FrameRgn( HRGN hDest, HRGN hSrc, INT x, INT y )
1109 BOOL bRet;
1110 RGNOBJ *srcObj = (RGNOBJ*) GDI_GetObjPtr( hSrc, REGION_MAGIC );
1112 if (srcObj->rgn->numRects != 0)
1114 RGNOBJ* destObj = (RGNOBJ*) GDI_GetObjPtr( hDest, REGION_MAGIC );
1115 RECT *pRect, *pEndRect;
1116 RECT tempRect;
1118 EMPTY_REGION( destObj->rgn );
1120 pEndRect = srcObj->rgn->rects + srcObj->rgn->numRects;
1121 for(pRect = srcObj->rgn->rects; pRect < pEndRect; pRect++)
1123 tempRect.left = pRect->left - x;
1124 tempRect.top = pRect->top - y;
1125 tempRect.right = pRect->right + x;
1126 tempRect.bottom = pRect->bottom + y;
1127 REGION_UnionRectWithRegion( &tempRect, destObj->rgn );
1129 REGION_SubtractRegion( destObj->rgn, destObj->rgn, srcObj->rgn );
1130 GDI_HEAP_UNLOCK ( hDest );
1131 bRet = TRUE;
1133 else
1134 bRet = FALSE;
1135 GDI_HEAP_UNLOCK( hSrc );
1136 return bRet;
1139 /***********************************************************************
1140 * REGION_LPTODP
1142 * Convert region to device co-ords for the supplied dc.
1144 BOOL REGION_LPTODP( HDC hdc, HRGN hDest, HRGN hSrc )
1146 RECT *pCurRect, *pEndRect;
1147 RGNOBJ *srcObj, *destObj;
1148 DC * dc = DC_GetDCPtr( hdc );
1149 RECT tmpRect;
1151 TRACE(" hdc=%04x dest=%04x src=%04x\n",
1152 hdc, hDest, hSrc) ;
1154 if (dc->w.MapMode == MM_TEXT) /* Requires only a translation */
1156 if( CombineRgn( hDest, hSrc, 0, RGN_COPY ) == ERROR ) return FALSE;
1157 OffsetRgn( hDest, dc->vportOrgX - dc->wndOrgX,
1158 dc->vportOrgY - dc->wndOrgY );
1159 return TRUE;
1162 if(!( srcObj = (RGNOBJ *) GDI_GetObjPtr( hSrc, REGION_MAGIC) ))
1163 return FALSE;
1164 if(!( destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC) ))
1166 GDI_HEAP_UNLOCK( hSrc );
1167 return FALSE;
1169 EMPTY_REGION( destObj->rgn );
1171 pEndRect = srcObj->rgn->rects + srcObj->rgn->numRects;
1172 for(pCurRect = srcObj->rgn->rects; pCurRect < pEndRect; pCurRect++)
1174 tmpRect = *pCurRect;
1175 tmpRect.left = XLPTODP( dc, tmpRect.left );
1176 tmpRect.top = YLPTODP( dc, tmpRect.top );
1177 tmpRect.right = XLPTODP( dc, tmpRect.right );
1178 tmpRect.bottom = YLPTODP( dc, tmpRect.bottom );
1179 REGION_UnionRectWithRegion( &tmpRect, destObj->rgn );
1182 GDI_HEAP_UNLOCK( hDest );
1183 GDI_HEAP_UNLOCK( hSrc );
1184 return TRUE;
1187 /***********************************************************************
1188 * CombineRgn16 (GDI.451)
1190 INT16 WINAPI CombineRgn16(HRGN16 hDest, HRGN16 hSrc1, HRGN16 hSrc2, INT16 mode)
1192 return (INT16)CombineRgn( hDest, hSrc1, hSrc2, mode );
1196 /***********************************************************************
1197 * CombineRgn (GDI32.19)
1199 * Note: The behavior is correct even if src and dest regions are the same.
1201 INT WINAPI CombineRgn(HRGN hDest, HRGN hSrc1, HRGN hSrc2, INT mode)
1203 RGNOBJ *destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC);
1204 INT result = ERROR;
1206 TRACE(" %04x,%04x -> %04x mode=%x\n",
1207 hSrc1, hSrc2, hDest, mode );
1208 if (destObj)
1210 RGNOBJ *src1Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc1, REGION_MAGIC);
1212 if (src1Obj)
1214 TRACE("dump:\n");
1215 if(TRACE_ON(region))
1216 REGION_DumpRegion(src1Obj->rgn);
1217 if (mode == RGN_COPY)
1219 REGION_CopyRegion( destObj->rgn, src1Obj->rgn );
1220 result = destObj->rgn->type;
1222 else
1224 RGNOBJ *src2Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc2, REGION_MAGIC);
1226 if (src2Obj)
1228 TRACE("dump:\n");
1229 if(TRACE_ON(region))
1230 REGION_DumpRegion(src2Obj->rgn);
1231 switch (mode)
1233 case RGN_AND:
1234 REGION_IntersectRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn);
1235 break;
1236 case RGN_OR:
1237 REGION_UnionRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1238 break;
1239 case RGN_XOR:
1240 REGION_XorRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1241 break;
1242 case RGN_DIFF:
1243 REGION_SubtractRegion( destObj->rgn, src1Obj->rgn, src2Obj->rgn );
1244 break;
1246 result = destObj->rgn->type;
1247 GDI_HEAP_UNLOCK( hSrc2 );
1250 GDI_HEAP_UNLOCK( hSrc1 );
1252 TRACE("dump:\n");
1253 if(TRACE_ON(region))
1254 REGION_DumpRegion(destObj->rgn);
1256 GDI_HEAP_UNLOCK( hDest );
1257 } else {
1258 ERR("Invalid rgn=%04x\n", hDest);
1260 return result;
1263 /***********************************************************************
1264 * REGION_SetExtents
1265 * Re-calculate the extents of a region
1267 static void REGION_SetExtents (WINEREGION *pReg)
1269 RECT *pRect, *pRectEnd, *pExtents;
1271 if (pReg->numRects == 0)
1273 pReg->extents.left = 0;
1274 pReg->extents.top = 0;
1275 pReg->extents.right = 0;
1276 pReg->extents.bottom = 0;
1277 return;
1280 pExtents = &pReg->extents;
1281 pRect = pReg->rects;
1282 pRectEnd = &pRect[pReg->numRects - 1];
1285 * Since pRect is the first rectangle in the region, it must have the
1286 * smallest top and since pRectEnd is the last rectangle in the region,
1287 * it must have the largest bottom, because of banding. Initialize left and
1288 * right from pRect and pRectEnd, resp., as good things to initialize them
1289 * to...
1291 pExtents->left = pRect->left;
1292 pExtents->top = pRect->top;
1293 pExtents->right = pRectEnd->right;
1294 pExtents->bottom = pRectEnd->bottom;
1296 while (pRect <= pRectEnd)
1298 if (pRect->left < pExtents->left)
1299 pExtents->left = pRect->left;
1300 if (pRect->right > pExtents->right)
1301 pExtents->right = pRect->right;
1302 pRect++;
1306 /***********************************************************************
1307 * REGION_CopyRegion
1309 static void REGION_CopyRegion(WINEREGION *dst, WINEREGION *src)
1311 if (dst != src) /* don't want to copy to itself */
1313 if (dst->size < src->numRects)
1315 if (! (dst->rects = HeapReAlloc( GetProcessHeap(), 0, dst->rects,
1316 src->numRects * sizeof(RECT) )))
1317 return;
1318 dst->size = src->numRects;
1320 dst->numRects = src->numRects;
1321 dst->extents.left = src->extents.left;
1322 dst->extents.top = src->extents.top;
1323 dst->extents.right = src->extents.right;
1324 dst->extents.bottom = src->extents.bottom;
1325 dst->type = src->type;
1327 memcpy((char *) dst->rects, (char *) src->rects,
1328 (int) (src->numRects * sizeof(RECT)));
1330 return;
1333 /***********************************************************************
1334 * REGION_Coalesce
1336 * Attempt to merge the rects in the current band with those in the
1337 * previous one. Used only by REGION_RegionOp.
1339 * Results:
1340 * The new index for the previous band.
1342 * Side Effects:
1343 * If coalescing takes place:
1344 * - rectangles in the previous band will have their bottom fields
1345 * altered.
1346 * - pReg->numRects will be decreased.
1349 static INT REGION_Coalesce (
1350 WINEREGION *pReg, /* Region to coalesce */
1351 INT prevStart, /* Index of start of previous band */
1352 INT curStart /* Index of start of current band */
1354 RECT *pPrevRect; /* Current rect in previous band */
1355 RECT *pCurRect; /* Current rect in current band */
1356 RECT *pRegEnd; /* End of region */
1357 INT curNumRects; /* Number of rectangles in current band */
1358 INT prevNumRects; /* Number of rectangles in previous band */
1359 INT bandtop; /* top coordinate for current band */
1361 pRegEnd = &pReg->rects[pReg->numRects];
1363 pPrevRect = &pReg->rects[prevStart];
1364 prevNumRects = curStart - prevStart;
1367 * Figure out how many rectangles are in the current band. Have to do
1368 * this because multiple bands could have been added in REGION_RegionOp
1369 * at the end when one region has been exhausted.
1371 pCurRect = &pReg->rects[curStart];
1372 bandtop = pCurRect->top;
1373 for (curNumRects = 0;
1374 (pCurRect != pRegEnd) && (pCurRect->top == bandtop);
1375 curNumRects++)
1377 pCurRect++;
1380 if (pCurRect != pRegEnd)
1383 * If more than one band was added, we have to find the start
1384 * of the last band added so the next coalescing job can start
1385 * at the right place... (given when multiple bands are added,
1386 * this may be pointless -- see above).
1388 pRegEnd--;
1389 while (pRegEnd[-1].top == pRegEnd->top)
1391 pRegEnd--;
1393 curStart = pRegEnd - pReg->rects;
1394 pRegEnd = pReg->rects + pReg->numRects;
1397 if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
1398 pCurRect -= curNumRects;
1400 * The bands may only be coalesced if the bottom of the previous
1401 * matches the top scanline of the current.
1403 if (pPrevRect->bottom == pCurRect->top)
1406 * Make sure the bands have rects in the same places. This
1407 * assumes that rects have been added in such a way that they
1408 * cover the most area possible. I.e. two rects in a band must
1409 * have some horizontal space between them.
1413 if ((pPrevRect->left != pCurRect->left) ||
1414 (pPrevRect->right != pCurRect->right))
1417 * The bands don't line up so they can't be coalesced.
1419 return (curStart);
1421 pPrevRect++;
1422 pCurRect++;
1423 prevNumRects -= 1;
1424 } while (prevNumRects != 0);
1426 pReg->numRects -= curNumRects;
1427 pCurRect -= curNumRects;
1428 pPrevRect -= curNumRects;
1431 * The bands may be merged, so set the bottom of each rect
1432 * in the previous band to that of the corresponding rect in
1433 * the current band.
1437 pPrevRect->bottom = pCurRect->bottom;
1438 pPrevRect++;
1439 pCurRect++;
1440 curNumRects -= 1;
1441 } while (curNumRects != 0);
1444 * If only one band was added to the region, we have to backup
1445 * curStart to the start of the previous band.
1447 * If more than one band was added to the region, copy the
1448 * other bands down. The assumption here is that the other bands
1449 * came from the same region as the current one and no further
1450 * coalescing can be done on them since it's all been done
1451 * already... curStart is already in the right place.
1453 if (pCurRect == pRegEnd)
1455 curStart = prevStart;
1457 else
1461 *pPrevRect++ = *pCurRect++;
1462 } while (pCurRect != pRegEnd);
1467 return (curStart);
1470 /***********************************************************************
1471 * REGION_RegionOp
1473 * Apply an operation to two regions. Called by REGION_Union,
1474 * REGION_Inverse, REGION_Subtract, REGION_Intersect...
1476 * Results:
1477 * None.
1479 * Side Effects:
1480 * The new region is overwritten.
1482 * Notes:
1483 * The idea behind this function is to view the two regions as sets.
1484 * Together they cover a rectangle of area that this function divides
1485 * into horizontal bands where points are covered only by one region
1486 * or by both. For the first case, the nonOverlapFunc is called with
1487 * each the band and the band's upper and lower extents. For the
1488 * second, the overlapFunc is called to process the entire band. It
1489 * is responsible for clipping the rectangles in the band, though
1490 * this function provides the boundaries.
1491 * At the end of each band, the new region is coalesced, if possible,
1492 * to reduce the number of rectangles in the region.
1495 static void REGION_RegionOp(
1496 WINEREGION *newReg, /* Place to store result */
1497 WINEREGION *reg1, /* First region in operation */
1498 WINEREGION *reg2, /* 2nd region in operation */
1499 void (*overlapFunc)(), /* Function to call for over-lapping bands */
1500 void (*nonOverlap1Func)(), /* Function to call for non-overlapping bands in region 1 */
1501 void (*nonOverlap2Func)() /* Function to call for non-overlapping bands in region 2 */
1503 RECT *r1; /* Pointer into first region */
1504 RECT *r2; /* Pointer into 2d region */
1505 RECT *r1End; /* End of 1st region */
1506 RECT *r2End; /* End of 2d region */
1507 INT ybot; /* Bottom of intersection */
1508 INT ytop; /* Top of intersection */
1509 RECT *oldRects; /* Old rects for newReg */
1510 INT prevBand; /* Index of start of
1511 * previous band in newReg */
1512 INT curBand; /* Index of start of current
1513 * band in newReg */
1514 RECT *r1BandEnd; /* End of current band in r1 */
1515 RECT *r2BandEnd; /* End of current band in r2 */
1516 INT top; /* Top of non-overlapping band */
1517 INT bot; /* Bottom of non-overlapping band */
1520 * Initialization:
1521 * set r1, r2, r1End and r2End appropriately, preserve the important
1522 * parts of the destination region until the end in case it's one of
1523 * the two source regions, then mark the "new" region empty, allocating
1524 * another array of rectangles for it to use.
1526 r1 = reg1->rects;
1527 r2 = reg2->rects;
1528 r1End = r1 + reg1->numRects;
1529 r2End = r2 + reg2->numRects;
1533 * newReg may be one of the src regions so we can't empty it. We keep a
1534 * note of its rects pointer (so that we can free them later), preserve its
1535 * extents and simply set numRects to zero.
1538 oldRects = newReg->rects;
1539 newReg->numRects = 0;
1542 * Allocate a reasonable number of rectangles for the new region. The idea
1543 * is to allocate enough so the individual functions don't need to
1544 * reallocate and copy the array, which is time consuming, yet we don't
1545 * have to worry about using too much memory. I hope to be able to
1546 * nuke the Xrealloc() at the end of this function eventually.
1548 newReg->size = max(reg1->numRects,reg2->numRects) * 2;
1550 if (! (newReg->rects = HeapAlloc( GetProcessHeap(), 0,
1551 sizeof(RECT) * newReg->size )))
1553 newReg->size = 0;
1554 return;
1558 * Initialize ybot and ytop.
1559 * In the upcoming loop, ybot and ytop serve different functions depending
1560 * on whether the band being handled is an overlapping or non-overlapping
1561 * band.
1562 * In the case of a non-overlapping band (only one of the regions
1563 * has points in the band), ybot is the bottom of the most recent
1564 * intersection and thus clips the top of the rectangles in that band.
1565 * ytop is the top of the next intersection between the two regions and
1566 * serves to clip the bottom of the rectangles in the current band.
1567 * For an overlapping band (where the two regions intersect), ytop clips
1568 * the top of the rectangles of both regions and ybot clips the bottoms.
1570 if (reg1->extents.top < reg2->extents.top)
1571 ybot = reg1->extents.top;
1572 else
1573 ybot = reg2->extents.top;
1576 * prevBand serves to mark the start of the previous band so rectangles
1577 * can be coalesced into larger rectangles. qv. miCoalesce, above.
1578 * In the beginning, there is no previous band, so prevBand == curBand
1579 * (curBand is set later on, of course, but the first band will always
1580 * start at index 0). prevBand and curBand must be indices because of
1581 * the possible expansion, and resultant moving, of the new region's
1582 * array of rectangles.
1584 prevBand = 0;
1588 curBand = newReg->numRects;
1591 * This algorithm proceeds one source-band (as opposed to a
1592 * destination band, which is determined by where the two regions
1593 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
1594 * rectangle after the last one in the current band for their
1595 * respective regions.
1597 r1BandEnd = r1;
1598 while ((r1BandEnd != r1End) && (r1BandEnd->top == r1->top))
1600 r1BandEnd++;
1603 r2BandEnd = r2;
1604 while ((r2BandEnd != r2End) && (r2BandEnd->top == r2->top))
1606 r2BandEnd++;
1610 * First handle the band that doesn't intersect, if any.
1612 * Note that attention is restricted to one band in the
1613 * non-intersecting region at once, so if a region has n
1614 * bands between the current position and the next place it overlaps
1615 * the other, this entire loop will be passed through n times.
1617 if (r1->top < r2->top)
1619 top = max(r1->top,ybot);
1620 bot = min(r1->bottom,r2->top);
1622 if ((top != bot) && (nonOverlap1Func != (void (*)())NULL))
1624 (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot);
1627 ytop = r2->top;
1629 else if (r2->top < r1->top)
1631 top = max(r2->top,ybot);
1632 bot = min(r2->bottom,r1->top);
1634 if ((top != bot) && (nonOverlap2Func != (void (*)())NULL))
1636 (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot);
1639 ytop = r1->top;
1641 else
1643 ytop = r1->top;
1647 * If any rectangles got added to the region, try and coalesce them
1648 * with rectangles from the previous band. Note we could just do
1649 * this test in miCoalesce, but some machines incur a not
1650 * inconsiderable cost for function calls, so...
1652 if (newReg->numRects != curBand)
1654 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1658 * Now see if we've hit an intersecting band. The two bands only
1659 * intersect if ybot > ytop
1661 ybot = min(r1->bottom, r2->bottom);
1662 curBand = newReg->numRects;
1663 if (ybot > ytop)
1665 (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);
1669 if (newReg->numRects != curBand)
1671 prevBand = REGION_Coalesce (newReg, prevBand, curBand);
1675 * If we've finished with a band (bottom == ybot) we skip forward
1676 * in the region to the next band.
1678 if (r1->bottom == ybot)
1680 r1 = r1BandEnd;
1682 if (r2->bottom == ybot)
1684 r2 = r2BandEnd;
1686 } while ((r1 != r1End) && (r2 != r2End));
1689 * Deal with whichever region still has rectangles left.
1691 curBand = newReg->numRects;
1692 if (r1 != r1End)
1694 if (nonOverlap1Func != (void (*)())NULL)
1698 r1BandEnd = r1;
1699 while ((r1BandEnd < r1End) && (r1BandEnd->top == r1->top))
1701 r1BandEnd++;
1703 (* nonOverlap1Func) (newReg, r1, r1BandEnd,
1704 max(r1->top,ybot), r1->bottom);
1705 r1 = r1BandEnd;
1706 } while (r1 != r1End);
1709 else if ((r2 != r2End) && (nonOverlap2Func != (void (*)())NULL))
1713 r2BandEnd = r2;
1714 while ((r2BandEnd < r2End) && (r2BandEnd->top == r2->top))
1716 r2BandEnd++;
1718 (* nonOverlap2Func) (newReg, r2, r2BandEnd,
1719 max(r2->top,ybot), r2->bottom);
1720 r2 = r2BandEnd;
1721 } while (r2 != r2End);
1724 if (newReg->numRects != curBand)
1726 (void) REGION_Coalesce (newReg, prevBand, curBand);
1730 * A bit of cleanup. To keep regions from growing without bound,
1731 * we shrink the array of rectangles to match the new number of
1732 * rectangles in the region. This never goes to 0, however...
1734 * Only do this stuff if the number of rectangles allocated is more than
1735 * twice the number of rectangles in the region (a simple optimization...).
1737 if ((newReg->numRects < (newReg->size >> 1)) && (newReg->numRects > 2))
1739 if (REGION_NOT_EMPTY(newReg))
1741 RECT *prev_rects = newReg->rects;
1742 newReg->size = newReg->numRects;
1743 newReg->rects = HeapReAlloc( GetProcessHeap(), 0, newReg->rects,
1744 sizeof(RECT) * newReg->size );
1745 if (! newReg->rects)
1746 newReg->rects = prev_rects;
1748 else
1751 * No point in doing the extra work involved in an Xrealloc if
1752 * the region is empty
1754 newReg->size = 1;
1755 HeapFree( GetProcessHeap(), 0, newReg->rects );
1756 newReg->rects = HeapAlloc( GetProcessHeap(), 0, sizeof(RECT) );
1759 HeapFree( GetProcessHeap(), 0, oldRects );
1760 return;
1763 /***********************************************************************
1764 * Region Intersection
1765 ***********************************************************************/
1768 /***********************************************************************
1769 * REGION_IntersectO
1771 * Handle an overlapping band for REGION_Intersect.
1773 * Results:
1774 * None.
1776 * Side Effects:
1777 * Rectangles may be added to the region.
1780 static void REGION_IntersectO(WINEREGION *pReg, RECT *r1, RECT *r1End,
1781 RECT *r2, RECT *r2End, INT top, INT bottom)
1784 INT left, right;
1785 RECT *pNextRect;
1787 pNextRect = &pReg->rects[pReg->numRects];
1789 while ((r1 != r1End) && (r2 != r2End))
1791 left = max(r1->left, r2->left);
1792 right = min(r1->right, r2->right);
1795 * If there's any overlap between the two rectangles, add that
1796 * overlap to the new region.
1797 * There's no need to check for subsumption because the only way
1798 * such a need could arise is if some region has two rectangles
1799 * right next to each other. Since that should never happen...
1801 if (left < right)
1803 MEMCHECK(pReg, pNextRect, pReg->rects);
1804 pNextRect->left = left;
1805 pNextRect->top = top;
1806 pNextRect->right = right;
1807 pNextRect->bottom = bottom;
1808 pReg->numRects += 1;
1809 pNextRect++;
1813 * Need to advance the pointers. Shift the one that extends
1814 * to the right the least, since the other still has a chance to
1815 * overlap with that region's next rectangle, if you see what I mean.
1817 if (r1->right < r2->right)
1819 r1++;
1821 else if (r2->right < r1->right)
1823 r2++;
1825 else
1827 r1++;
1828 r2++;
1831 return;
1834 /***********************************************************************
1835 * REGION_IntersectRegion
1837 static void REGION_IntersectRegion(WINEREGION *newReg, WINEREGION *reg1,
1838 WINEREGION *reg2)
1840 /* check for trivial reject */
1841 if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
1842 (!EXTENTCHECK(&reg1->extents, &reg2->extents)))
1843 newReg->numRects = 0;
1844 else
1845 REGION_RegionOp (newReg, reg1, reg2,
1846 (voidProcp) REGION_IntersectO, (voidProcp) NULL, (voidProcp) NULL);
1849 * Can't alter newReg's extents before we call miRegionOp because
1850 * it might be one of the source regions and miRegionOp depends
1851 * on the extents of those regions being the same. Besides, this
1852 * way there's no checking against rectangles that will be nuked
1853 * due to coalescing, so we have to examine fewer rectangles.
1855 REGION_SetExtents(newReg);
1856 newReg->type = (newReg->numRects) ?
1857 ((newReg->numRects > 1) ? COMPLEXREGION : SIMPLEREGION)
1858 : NULLREGION ;
1859 return;
1862 /***********************************************************************
1863 * Region Union
1864 ***********************************************************************/
1866 /***********************************************************************
1867 * REGION_UnionNonO
1869 * Handle a non-overlapping band for the union operation. Just
1870 * Adds the rectangles into the region. Doesn't have to check for
1871 * subsumption or anything.
1873 * Results:
1874 * None.
1876 * Side Effects:
1877 * pReg->numRects is incremented and the final rectangles overwritten
1878 * with the rectangles we're passed.
1881 static void REGION_UnionNonO (WINEREGION *pReg, RECT *r, RECT *rEnd,
1882 INT top, INT bottom)
1884 RECT *pNextRect;
1886 pNextRect = &pReg->rects[pReg->numRects];
1888 while (r != rEnd)
1890 MEMCHECK(pReg, pNextRect, pReg->rects);
1891 pNextRect->left = r->left;
1892 pNextRect->top = top;
1893 pNextRect->right = r->right;
1894 pNextRect->bottom = bottom;
1895 pReg->numRects += 1;
1896 pNextRect++;
1897 r++;
1899 return;
1902 /***********************************************************************
1903 * REGION_UnionO
1905 * Handle an overlapping band for the union operation. Picks the
1906 * left-most rectangle each time and merges it into the region.
1908 * Results:
1909 * None.
1911 * Side Effects:
1912 * Rectangles are overwritten in pReg->rects and pReg->numRects will
1913 * be changed.
1916 static void REGION_UnionO (WINEREGION *pReg, RECT *r1, RECT *r1End,
1917 RECT *r2, RECT *r2End, INT top, INT bottom)
1919 RECT *pNextRect;
1921 pNextRect = &pReg->rects[pReg->numRects];
1923 #define MERGERECT(r) \
1924 if ((pReg->numRects != 0) && \
1925 (pNextRect[-1].top == top) && \
1926 (pNextRect[-1].bottom == bottom) && \
1927 (pNextRect[-1].right >= r->left)) \
1929 if (pNextRect[-1].right < r->right) \
1931 pNextRect[-1].right = r->right; \
1934 else \
1936 MEMCHECK(pReg, pNextRect, pReg->rects); \
1937 pNextRect->top = top; \
1938 pNextRect->bottom = bottom; \
1939 pNextRect->left = r->left; \
1940 pNextRect->right = r->right; \
1941 pReg->numRects += 1; \
1942 pNextRect += 1; \
1944 r++;
1946 while ((r1 != r1End) && (r2 != r2End))
1948 if (r1->left < r2->left)
1950 MERGERECT(r1);
1952 else
1954 MERGERECT(r2);
1958 if (r1 != r1End)
1962 MERGERECT(r1);
1963 } while (r1 != r1End);
1965 else while (r2 != r2End)
1967 MERGERECT(r2);
1969 return;
1972 /***********************************************************************
1973 * REGION_UnionRegion
1975 static void REGION_UnionRegion(WINEREGION *newReg, WINEREGION *reg1,
1976 WINEREGION *reg2)
1978 /* checks all the simple cases */
1981 * Region 1 and 2 are the same or region 1 is empty
1983 if ( (reg1 == reg2) || (!(reg1->numRects)) )
1985 if (newReg != reg2)
1986 REGION_CopyRegion(newReg, reg2);
1987 return;
1991 * if nothing to union (region 2 empty)
1993 if (!(reg2->numRects))
1995 if (newReg != reg1)
1996 REGION_CopyRegion(newReg, reg1);
1997 return;
2001 * Region 1 completely subsumes region 2
2003 if ((reg1->numRects == 1) &&
2004 (reg1->extents.left <= reg2->extents.left) &&
2005 (reg1->extents.top <= reg2->extents.top) &&
2006 (reg1->extents.right >= reg2->extents.right) &&
2007 (reg1->extents.bottom >= reg2->extents.bottom))
2009 if (newReg != reg1)
2010 REGION_CopyRegion(newReg, reg1);
2011 return;
2015 * Region 2 completely subsumes region 1
2017 if ((reg2->numRects == 1) &&
2018 (reg2->extents.left <= reg1->extents.left) &&
2019 (reg2->extents.top <= reg1->extents.top) &&
2020 (reg2->extents.right >= reg1->extents.right) &&
2021 (reg2->extents.bottom >= reg1->extents.bottom))
2023 if (newReg != reg2)
2024 REGION_CopyRegion(newReg, reg2);
2025 return;
2028 REGION_RegionOp (newReg, reg1, reg2, (voidProcp) REGION_UnionO,
2029 (voidProcp) REGION_UnionNonO, (voidProcp) REGION_UnionNonO);
2031 newReg->extents.left = min(reg1->extents.left, reg2->extents.left);
2032 newReg->extents.top = min(reg1->extents.top, reg2->extents.top);
2033 newReg->extents.right = max(reg1->extents.right, reg2->extents.right);
2034 newReg->extents.bottom = max(reg1->extents.bottom, reg2->extents.bottom);
2035 newReg->type = (newReg->numRects) ?
2036 ((newReg->numRects > 1) ? COMPLEXREGION : SIMPLEREGION)
2037 : NULLREGION ;
2038 return;
2041 /***********************************************************************
2042 * Region Subtraction
2043 ***********************************************************************/
2045 /***********************************************************************
2046 * REGION_SubtractNonO1
2048 * Deal with non-overlapping band for subtraction. Any parts from
2049 * region 2 we discard. Anything from region 1 we add to the region.
2051 * Results:
2052 * None.
2054 * Side Effects:
2055 * pReg may be affected.
2058 static void REGION_SubtractNonO1 (WINEREGION *pReg, RECT *r, RECT *rEnd,
2059 INT top, INT bottom)
2061 RECT *pNextRect;
2063 pNextRect = &pReg->rects[pReg->numRects];
2065 while (r != rEnd)
2067 MEMCHECK(pReg, pNextRect, pReg->rects);
2068 pNextRect->left = r->left;
2069 pNextRect->top = top;
2070 pNextRect->right = r->right;
2071 pNextRect->bottom = bottom;
2072 pReg->numRects += 1;
2073 pNextRect++;
2074 r++;
2076 return;
2080 /***********************************************************************
2081 * REGION_SubtractO
2083 * Overlapping band subtraction. x1 is the left-most point not yet
2084 * checked.
2086 * Results:
2087 * None.
2089 * Side Effects:
2090 * pReg may have rectangles added to it.
2093 static void REGION_SubtractO (WINEREGION *pReg, RECT *r1, RECT *r1End,
2094 RECT *r2, RECT *r2End, INT top, INT bottom)
2096 RECT *pNextRect;
2097 INT left;
2099 left = r1->left;
2100 pNextRect = &pReg->rects[pReg->numRects];
2102 while ((r1 != r1End) && (r2 != r2End))
2104 if (r2->right <= left)
2107 * Subtrahend missed the boat: go to next subtrahend.
2109 r2++;
2111 else if (r2->left <= left)
2114 * Subtrahend preceeds minuend: nuke left edge of minuend.
2116 left = r2->right;
2117 if (left >= r1->right)
2120 * Minuend completely covered: advance to next minuend and
2121 * reset left fence to edge of new minuend.
2123 r1++;
2124 if (r1 != r1End)
2125 left = r1->left;
2127 else
2130 * Subtrahend now used up since it doesn't extend beyond
2131 * minuend
2133 r2++;
2136 else if (r2->left < r1->right)
2139 * Left part of subtrahend covers part of minuend: add uncovered
2140 * part of minuend to region and skip to next subtrahend.
2142 MEMCHECK(pReg, pNextRect, pReg->rects);
2143 pNextRect->left = left;
2144 pNextRect->top = top;
2145 pNextRect->right = r2->left;
2146 pNextRect->bottom = bottom;
2147 pReg->numRects += 1;
2148 pNextRect++;
2149 left = r2->right;
2150 if (left >= r1->right)
2153 * Minuend used up: advance to new...
2155 r1++;
2156 if (r1 != r1End)
2157 left = r1->left;
2159 else
2162 * Subtrahend used up
2164 r2++;
2167 else
2170 * Minuend used up: add any remaining piece before advancing.
2172 if (r1->right > left)
2174 MEMCHECK(pReg, pNextRect, pReg->rects);
2175 pNextRect->left = left;
2176 pNextRect->top = top;
2177 pNextRect->right = r1->right;
2178 pNextRect->bottom = bottom;
2179 pReg->numRects += 1;
2180 pNextRect++;
2182 r1++;
2183 left = r1->left;
2188 * Add remaining minuend rectangles to region.
2190 while (r1 != r1End)
2192 MEMCHECK(pReg, pNextRect, pReg->rects);
2193 pNextRect->left = left;
2194 pNextRect->top = top;
2195 pNextRect->right = r1->right;
2196 pNextRect->bottom = bottom;
2197 pReg->numRects += 1;
2198 pNextRect++;
2199 r1++;
2200 if (r1 != r1End)
2202 left = r1->left;
2205 return;
2208 /***********************************************************************
2209 * REGION_SubtractRegion
2211 * Subtract regS from regM and leave the result in regD.
2212 * S stands for subtrahend, M for minuend and D for difference.
2214 * Results:
2215 * TRUE.
2217 * Side Effects:
2218 * regD is overwritten.
2221 static void REGION_SubtractRegion(WINEREGION *regD, WINEREGION *regM,
2222 WINEREGION *regS )
2224 /* check for trivial reject */
2225 if ( (!(regM->numRects)) || (!(regS->numRects)) ||
2226 (!EXTENTCHECK(&regM->extents, &regS->extents)) )
2228 REGION_CopyRegion(regD, regM);
2229 return;
2232 REGION_RegionOp (regD, regM, regS, (voidProcp) REGION_SubtractO,
2233 (voidProcp) REGION_SubtractNonO1, (voidProcp) NULL);
2236 * Can't alter newReg's extents before we call miRegionOp because
2237 * it might be one of the source regions and miRegionOp depends
2238 * on the extents of those regions being the unaltered. Besides, this
2239 * way there's no checking against rectangles that will be nuked
2240 * due to coalescing, so we have to examine fewer rectangles.
2242 REGION_SetExtents (regD);
2243 regD->type = (regD->numRects) ?
2244 ((regD->numRects > 1) ? COMPLEXREGION : SIMPLEREGION)
2245 : NULLREGION ;
2246 return;
2249 /***********************************************************************
2250 * REGION_XorRegion
2252 static void REGION_XorRegion(WINEREGION *dr, WINEREGION *sra,
2253 WINEREGION *srb)
2255 WINEREGION *tra, *trb;
2257 if ((! (tra = REGION_AllocWineRegion(sra->numRects + 1))) ||
2258 (! (trb = REGION_AllocWineRegion(srb->numRects + 1))))
2259 return;
2260 REGION_SubtractRegion(tra,sra,srb);
2261 REGION_SubtractRegion(trb,srb,sra);
2262 REGION_UnionRegion(dr,tra,trb);
2263 REGION_DestroyWineRegion(tra);
2264 REGION_DestroyWineRegion(trb);
2265 return;
2268 /**************************************************************************
2270 * Poly Regions
2272 *************************************************************************/
2274 #define LARGE_COORDINATE 0x7fffffff /* FIXME */
2275 #define SMALL_COORDINATE 0x80000000
2277 /***********************************************************************
2278 * REGION_InsertEdgeInET
2280 * Insert the given edge into the edge table.
2281 * First we must find the correct bucket in the
2282 * Edge table, then find the right slot in the
2283 * bucket. Finally, we can insert it.
2286 static void REGION_InsertEdgeInET(EdgeTable *ET, EdgeTableEntry *ETE,
2287 INT scanline, ScanLineListBlock **SLLBlock, INT *iSLLBlock)
2290 EdgeTableEntry *start, *prev;
2291 ScanLineList *pSLL, *pPrevSLL;
2292 ScanLineListBlock *tmpSLLBlock;
2295 * find the right bucket to put the edge into
2297 pPrevSLL = &ET->scanlines;
2298 pSLL = pPrevSLL->next;
2299 while (pSLL && (pSLL->scanline < scanline))
2301 pPrevSLL = pSLL;
2302 pSLL = pSLL->next;
2306 * reassign pSLL (pointer to ScanLineList) if necessary
2308 if ((!pSLL) || (pSLL->scanline > scanline))
2310 if (*iSLLBlock > SLLSPERBLOCK-1)
2312 tmpSLLBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(ScanLineListBlock));
2313 if(!tmpSLLBlock)
2315 WARN("Can't alloc SLLB\n");
2316 return;
2318 (*SLLBlock)->next = tmpSLLBlock;
2319 tmpSLLBlock->next = (ScanLineListBlock *)NULL;
2320 *SLLBlock = tmpSLLBlock;
2321 *iSLLBlock = 0;
2323 pSLL = &((*SLLBlock)->SLLs[(*iSLLBlock)++]);
2325 pSLL->next = pPrevSLL->next;
2326 pSLL->edgelist = (EdgeTableEntry *)NULL;
2327 pPrevSLL->next = pSLL;
2329 pSLL->scanline = scanline;
2332 * now insert the edge in the right bucket
2334 prev = (EdgeTableEntry *)NULL;
2335 start = pSLL->edgelist;
2336 while (start && (start->bres.minor_axis < ETE->bres.minor_axis))
2338 prev = start;
2339 start = start->next;
2341 ETE->next = start;
2343 if (prev)
2344 prev->next = ETE;
2345 else
2346 pSLL->edgelist = ETE;
2349 /***********************************************************************
2350 * REGION_CreateEdgeTable
2352 * This routine creates the edge table for
2353 * scan converting polygons.
2354 * The Edge Table (ET) looks like:
2356 * EdgeTable
2357 * --------
2358 * | ymax | ScanLineLists
2359 * |scanline|-->------------>-------------->...
2360 * -------- |scanline| |scanline|
2361 * |edgelist| |edgelist|
2362 * --------- ---------
2363 * | |
2364 * | |
2365 * V V
2366 * list of ETEs list of ETEs
2368 * where ETE is an EdgeTableEntry data structure,
2369 * and there is one ScanLineList per scanline at
2370 * which an edge is initially entered.
2373 static void REGION_CreateETandAET(const INT *Count, INT nbpolygons,
2374 const POINT *pts, EdgeTable *ET, EdgeTableEntry *AET,
2375 EdgeTableEntry *pETEs, ScanLineListBlock *pSLLBlock)
2377 const POINT *top, *bottom;
2378 const POINT *PrevPt, *CurrPt, *EndPt;
2379 INT poly, count;
2380 int iSLLBlock = 0;
2381 int dy;
2385 * initialize the Active Edge Table
2387 AET->next = (EdgeTableEntry *)NULL;
2388 AET->back = (EdgeTableEntry *)NULL;
2389 AET->nextWETE = (EdgeTableEntry *)NULL;
2390 AET->bres.minor_axis = SMALL_COORDINATE;
2393 * initialize the Edge Table.
2395 ET->scanlines.next = (ScanLineList *)NULL;
2396 ET->ymax = SMALL_COORDINATE;
2397 ET->ymin = LARGE_COORDINATE;
2398 pSLLBlock->next = (ScanLineListBlock *)NULL;
2400 EndPt = pts - 1;
2401 for(poly = 0; poly < nbpolygons; poly++)
2403 count = Count[poly];
2404 EndPt += count;
2405 if(count < 2)
2406 continue;
2408 PrevPt = EndPt;
2411 * for each vertex in the array of points.
2412 * In this loop we are dealing with two vertices at
2413 * a time -- these make up one edge of the polygon.
2415 while (count--)
2417 CurrPt = pts++;
2420 * find out which point is above and which is below.
2422 if (PrevPt->y > CurrPt->y)
2424 bottom = PrevPt, top = CurrPt;
2425 pETEs->ClockWise = 0;
2427 else
2429 bottom = CurrPt, top = PrevPt;
2430 pETEs->ClockWise = 1;
2434 * don't add horizontal edges to the Edge table.
2436 if (bottom->y != top->y)
2438 pETEs->ymax = bottom->y-1;
2439 /* -1 so we don't get last scanline */
2442 * initialize integer edge algorithm
2444 dy = bottom->y - top->y;
2445 BRESINITPGONSTRUCT(dy, top->x, bottom->x, pETEs->bres);
2447 REGION_InsertEdgeInET(ET, pETEs, top->y, &pSLLBlock,
2448 &iSLLBlock);
2450 if (PrevPt->y > ET->ymax)
2451 ET->ymax = PrevPt->y;
2452 if (PrevPt->y < ET->ymin)
2453 ET->ymin = PrevPt->y;
2454 pETEs++;
2457 PrevPt = CurrPt;
2462 /***********************************************************************
2463 * REGION_loadAET
2465 * This routine moves EdgeTableEntries from the
2466 * EdgeTable into the Active Edge Table,
2467 * leaving them sorted by smaller x coordinate.
2470 static void REGION_loadAET(EdgeTableEntry *AET, EdgeTableEntry *ETEs)
2472 EdgeTableEntry *pPrevAET;
2473 EdgeTableEntry *tmp;
2475 pPrevAET = AET;
2476 AET = AET->next;
2477 while (ETEs)
2479 while (AET && (AET->bres.minor_axis < ETEs->bres.minor_axis))
2481 pPrevAET = AET;
2482 AET = AET->next;
2484 tmp = ETEs->next;
2485 ETEs->next = AET;
2486 if (AET)
2487 AET->back = ETEs;
2488 ETEs->back = pPrevAET;
2489 pPrevAET->next = ETEs;
2490 pPrevAET = ETEs;
2492 ETEs = tmp;
2496 /***********************************************************************
2497 * REGION_computeWAET
2499 * This routine links the AET by the
2500 * nextWETE (winding EdgeTableEntry) link for
2501 * use by the winding number rule. The final
2502 * Active Edge Table (AET) might look something
2503 * like:
2505 * AET
2506 * ---------- --------- ---------
2507 * |ymax | |ymax | |ymax |
2508 * | ... | |... | |... |
2509 * |next |->|next |->|next |->...
2510 * |nextWETE| |nextWETE| |nextWETE|
2511 * --------- --------- ^--------
2512 * | | |
2513 * V-------------------> V---> ...
2516 static void REGION_computeWAET(EdgeTableEntry *AET)
2518 register EdgeTableEntry *pWETE;
2519 register int inside = 1;
2520 register int isInside = 0;
2522 AET->nextWETE = (EdgeTableEntry *)NULL;
2523 pWETE = AET;
2524 AET = AET->next;
2525 while (AET)
2527 if (AET->ClockWise)
2528 isInside++;
2529 else
2530 isInside--;
2532 if ((!inside && !isInside) ||
2533 ( inside && isInside))
2535 pWETE->nextWETE = AET;
2536 pWETE = AET;
2537 inside = !inside;
2539 AET = AET->next;
2541 pWETE->nextWETE = (EdgeTableEntry *)NULL;
2544 /***********************************************************************
2545 * REGION_InsertionSort
2547 * Just a simple insertion sort using
2548 * pointers and back pointers to sort the Active
2549 * Edge Table.
2552 static BOOL REGION_InsertionSort(EdgeTableEntry *AET)
2554 EdgeTableEntry *pETEchase;
2555 EdgeTableEntry *pETEinsert;
2556 EdgeTableEntry *pETEchaseBackTMP;
2557 BOOL changed = FALSE;
2559 AET = AET->next;
2560 while (AET)
2562 pETEinsert = AET;
2563 pETEchase = AET;
2564 while (pETEchase->back->bres.minor_axis > AET->bres.minor_axis)
2565 pETEchase = pETEchase->back;
2567 AET = AET->next;
2568 if (pETEchase != pETEinsert)
2570 pETEchaseBackTMP = pETEchase->back;
2571 pETEinsert->back->next = AET;
2572 if (AET)
2573 AET->back = pETEinsert->back;
2574 pETEinsert->next = pETEchase;
2575 pETEchase->back->next = pETEinsert;
2576 pETEchase->back = pETEinsert;
2577 pETEinsert->back = pETEchaseBackTMP;
2578 changed = TRUE;
2581 return changed;
2584 /***********************************************************************
2585 * REGION_FreeStorage
2587 * Clean up our act.
2589 static void REGION_FreeStorage(ScanLineListBlock *pSLLBlock)
2591 ScanLineListBlock *tmpSLLBlock;
2593 while (pSLLBlock)
2595 tmpSLLBlock = pSLLBlock->next;
2596 HeapFree( GetProcessHeap(), 0, pSLLBlock );
2597 pSLLBlock = tmpSLLBlock;
2602 /***********************************************************************
2603 * REGION_PtsToRegion
2605 * Create an array of rectangles from a list of points.
2607 static int REGION_PtsToRegion(int numFullPtBlocks, int iCurPtBlock,
2608 POINTBLOCK *FirstPtBlock, WINEREGION *reg)
2610 RECT *rects;
2611 POINT *pts;
2612 POINTBLOCK *CurPtBlock;
2613 int i;
2614 RECT *extents;
2615 INT numRects;
2617 extents = &reg->extents;
2619 numRects = ((numFullPtBlocks * NUMPTSTOBUFFER) + iCurPtBlock) >> 1;
2621 if (!(reg->rects = HeapReAlloc( GetProcessHeap(), 0, reg->rects,
2622 sizeof(RECT) * numRects )))
2623 return(0);
2625 reg->size = numRects;
2626 CurPtBlock = FirstPtBlock;
2627 rects = reg->rects - 1;
2628 numRects = 0;
2629 extents->left = LARGE_COORDINATE, extents->right = SMALL_COORDINATE;
2631 for ( ; numFullPtBlocks >= 0; numFullPtBlocks--) {
2632 /* the loop uses 2 points per iteration */
2633 i = NUMPTSTOBUFFER >> 1;
2634 if (!numFullPtBlocks)
2635 i = iCurPtBlock >> 1;
2636 for (pts = CurPtBlock->pts; i--; pts += 2) {
2637 if (pts->x == pts[1].x)
2638 continue;
2639 if (numRects && pts->x == rects->left && pts->y == rects->bottom &&
2640 pts[1].x == rects->right &&
2641 (numRects == 1 || rects[-1].top != rects->top) &&
2642 (i && pts[2].y > pts[1].y)) {
2643 rects->bottom = pts[1].y + 1;
2644 continue;
2646 numRects++;
2647 rects++;
2648 rects->left = pts->x; rects->top = pts->y;
2649 rects->right = pts[1].x; rects->bottom = pts[1].y + 1;
2650 if (rects->left < extents->left)
2651 extents->left = rects->left;
2652 if (rects->right > extents->right)
2653 extents->right = rects->right;
2655 CurPtBlock = CurPtBlock->next;
2658 if (numRects) {
2659 extents->top = reg->rects->top;
2660 extents->bottom = rects->bottom;
2661 } else {
2662 extents->left = 0;
2663 extents->top = 0;
2664 extents->right = 0;
2665 extents->bottom = 0;
2667 reg->numRects = numRects;
2669 return(TRUE);
2672 /***********************************************************************
2673 * CreatePolyPolygonRgn (GDI32.57)
2675 HRGN WINAPI CreatePolyPolygonRgn(const POINT *Pts, const INT *Count,
2676 INT nbpolygons, INT mode)
2678 HRGN hrgn;
2679 RGNOBJ *obj;
2680 WINEREGION *region;
2681 register EdgeTableEntry *pAET; /* Active Edge Table */
2682 register INT y; /* current scanline */
2683 register int iPts = 0; /* number of pts in buffer */
2684 register EdgeTableEntry *pWETE; /* Winding Edge Table Entry*/
2685 register ScanLineList *pSLL; /* current scanLineList */
2686 register POINT *pts; /* output buffer */
2687 EdgeTableEntry *pPrevAET; /* ptr to previous AET */
2688 EdgeTable ET; /* header node for ET */
2689 EdgeTableEntry AET; /* header node for AET */
2690 EdgeTableEntry *pETEs; /* EdgeTableEntries pool */
2691 ScanLineListBlock SLLBlock; /* header for scanlinelist */
2692 int fixWAET = FALSE;
2693 POINTBLOCK FirstPtBlock, *curPtBlock; /* PtBlock buffers */
2694 POINTBLOCK *tmpPtBlock;
2695 int numFullPtBlocks = 0;
2696 INT poly, total;
2698 if(!(hrgn = REGION_CreateRegion(nbpolygons)))
2699 return 0;
2700 obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
2701 region = obj->rgn;
2703 /* special case a rectangle */
2705 if (((nbpolygons == 1) && ((*Count == 4) ||
2706 ((*Count == 5) && (Pts[4].x == Pts[0].x) && (Pts[4].y == Pts[0].y)))) &&
2707 (((Pts[0].y == Pts[1].y) &&
2708 (Pts[1].x == Pts[2].x) &&
2709 (Pts[2].y == Pts[3].y) &&
2710 (Pts[3].x == Pts[0].x)) ||
2711 ((Pts[0].x == Pts[1].x) &&
2712 (Pts[1].y == Pts[2].y) &&
2713 (Pts[2].x == Pts[3].x) &&
2714 (Pts[3].y == Pts[0].y))))
2716 SetRectRgn( hrgn, min(Pts[0].x, Pts[2].x), min(Pts[0].y, Pts[2].y),
2717 max(Pts[0].x, Pts[2].x), max(Pts[0].y, Pts[2].y) );
2718 GDI_HEAP_UNLOCK( hrgn );
2719 return hrgn;
2722 for(poly = total = 0; poly < nbpolygons; poly++)
2723 total += Count[poly];
2724 if (! (pETEs = HeapAlloc( GetProcessHeap(), 0, sizeof(EdgeTableEntry) * total )))
2726 REGION_DeleteObject( hrgn, obj );
2727 return 0;
2729 pts = FirstPtBlock.pts;
2730 REGION_CreateETandAET(Count, nbpolygons, Pts, &ET, &AET, pETEs, &SLLBlock);
2731 pSLL = ET.scanlines.next;
2732 curPtBlock = &FirstPtBlock;
2734 if (mode != WINDING) {
2736 * for each scanline
2738 for (y = ET.ymin; y < ET.ymax; y++) {
2740 * Add a new edge to the active edge table when we
2741 * get to the next edge.
2743 if (pSLL != NULL && y == pSLL->scanline) {
2744 REGION_loadAET(&AET, pSLL->edgelist);
2745 pSLL = pSLL->next;
2747 pPrevAET = &AET;
2748 pAET = AET.next;
2751 * for each active edge
2753 while (pAET) {
2754 pts->x = pAET->bres.minor_axis, pts->y = y;
2755 pts++, iPts++;
2758 * send out the buffer
2760 if (iPts == NUMPTSTOBUFFER) {
2761 tmpPtBlock = HeapAlloc( GetProcessHeap(), 0, sizeof(POINTBLOCK));
2762 if(!tmpPtBlock) {
2763 WARN("Can't alloc tPB\n");
2764 return 0;
2766 curPtBlock->next = tmpPtBlock;
2767 curPtBlock = tmpPtBlock;
2768 pts = curPtBlock->pts;
2769 numFullPtBlocks++;
2770 iPts = 0;
2772 EVALUATEEDGEEVENODD(pAET, pPrevAET, y);
2774 REGION_InsertionSort(&AET);
2777 else {
2779 * for each scanline
2781 for (y = ET.ymin; y < ET.ymax; y++) {
2783 * Add a new edge to the active edge table when we
2784 * get to the next edge.
2786 if (pSLL != NULL && y == pSLL->scanline) {
2787 REGION_loadAET(&AET, pSLL->edgelist);
2788 REGION_computeWAET(&AET);
2789 pSLL = pSLL->next;
2791 pPrevAET = &AET;
2792 pAET = AET.next;
2793 pWETE = pAET;
2796 * for each active edge
2798 while (pAET) {
2800 * add to the buffer only those edges that
2801 * are in the Winding active edge table.
2803 if (pWETE == 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,
2812 sizeof(POINTBLOCK) );
2813 if(!tmpPtBlock) {
2814 WARN("Can't alloc tPB\n");
2815 return 0;
2817 curPtBlock->next = tmpPtBlock;
2818 curPtBlock = tmpPtBlock;
2819 pts = curPtBlock->pts;
2820 numFullPtBlocks++; iPts = 0;
2822 pWETE = pWETE->nextWETE;
2824 EVALUATEEDGEWINDING(pAET, pPrevAET, y, fixWAET);
2828 * recompute the winding active edge table if
2829 * we just resorted or have exited an edge.
2831 if (REGION_InsertionSort(&AET) || fixWAET) {
2832 REGION_computeWAET(&AET);
2833 fixWAET = FALSE;
2837 REGION_FreeStorage(SLLBlock.next);
2838 REGION_PtsToRegion(numFullPtBlocks, iPts, &FirstPtBlock, region);
2839 region->type = (region->numRects) ?
2840 ((region->numRects > 1) ? COMPLEXREGION : SIMPLEREGION)
2841 : NULLREGION;
2843 for (curPtBlock = FirstPtBlock.next; --numFullPtBlocks >= 0;) {
2844 tmpPtBlock = curPtBlock->next;
2845 HeapFree( GetProcessHeap(), 0, curPtBlock );
2846 curPtBlock = tmpPtBlock;
2848 HeapFree( GetProcessHeap(), 0, pETEs );
2849 GDI_HEAP_UNLOCK( hrgn );
2850 return hrgn;
2854 /***********************************************************************
2855 * CreatePolygonRgn16 (GDI.63)
2857 HRGN16 WINAPI CreatePolygonRgn16( const POINT16 * points, INT16 count,
2858 INT16 mode )
2860 return CreatePolyPolygonRgn16( points, &count, 1, mode );
2863 /***********************************************************************
2864 * CreatePolyPolygonRgn16 (GDI.451)
2866 HRGN16 WINAPI CreatePolyPolygonRgn16( const POINT16 *points,
2867 const INT16 *count, INT16 nbpolygons, INT16 mode )
2869 HRGN hrgn;
2870 int i, npts = 0;
2871 INT *count32;
2872 POINT *points32;
2874 for (i = 0; i < nbpolygons; i++)
2875 npts += count[i];
2876 points32 = HeapAlloc( GetProcessHeap(), 0, npts * sizeof(POINT) );
2877 for (i = 0; i < npts; i++)
2878 CONV_POINT16TO32( &(points[i]), &(points32[i]) );
2880 count32 = HeapAlloc( GetProcessHeap(), 0, nbpolygons * sizeof(INT) );
2881 for (i = 0; i < nbpolygons; i++)
2882 count32[i] = count[i];
2883 hrgn = CreatePolyPolygonRgn( points32, count32, nbpolygons, mode );
2884 HeapFree( GetProcessHeap(), 0, count32 );
2885 HeapFree( GetProcessHeap(), 0, points32 );
2886 return hrgn;
2889 /***********************************************************************
2890 * CreatePolygonRgn (GDI32.58)
2892 HRGN WINAPI CreatePolygonRgn( const POINT *points, INT count,
2893 INT mode )
2895 return CreatePolyPolygonRgn( points, &count, 1, mode );
2899 /***********************************************************************
2900 * GetRandomRgn [GDI32.215]
2902 * NOTES
2903 * This function is documented in MSDN online
2905 INT WINAPI GetRandomRgn(HDC hDC, HRGN hRgn, DWORD dwCode)
2907 switch (dwCode)
2909 case 4: /* == SYSRGN ? */
2911 DC *dc = DC_GetDCPtr (hDC);
2912 OSVERSIONINFOA vi;
2913 POINT org;
2914 CombineRgn (hRgn, dc->w.hVisRgn, 0, RGN_COPY);
2916 * On Windows NT/2000,
2917 * the region returned is in screen coordinates.
2918 * On Windows 95/98,
2919 * the region returned is in window coordinates
2921 vi.dwOSVersionInfoSize = sizeof(vi);
2922 if (GetVersionExA( &vi ) && vi.dwPlatformId == VER_PLATFORM_WIN32_NT)
2923 GetDCOrgEx(hDC, &org);
2924 else
2925 org.x = org.y = 0;
2926 org.x -= dc->w.DCOrgX;
2927 org.y -= dc->w.DCOrgY;
2928 OffsetRgn (hRgn, org.x, org.y);
2930 return 1;
2932 /* case 1:
2933 return GetClipRgn (hDC, hRgn);
2935 default:
2936 WARN("Unknown dwCode %ld\n", dwCode);
2937 return -1;
2940 return -1;
2943 /***********************************************************************
2944 * REGION_CropAndOffsetRegion
2946 static BOOL REGION_CropAndOffsetRegion(const POINT* off, const RECT *rect, WINEREGION *rgnSrc, WINEREGION* rgnDst)
2949 if( !rect ) /* just copy and offset */
2951 RECT *xrect;
2952 if( rgnDst == rgnSrc )
2954 if( off->x || off->y )
2955 xrect = rgnDst->rects;
2956 else
2957 return TRUE;
2959 else
2960 xrect = HeapReAlloc( GetProcessHeap(), 0, rgnDst->rects,
2961 rgnSrc->size * sizeof( RECT ));
2962 if( xrect )
2964 INT i;
2966 if( rgnDst != rgnSrc )
2967 memcpy( rgnDst, rgnSrc, sizeof( WINEREGION ));
2969 if( off->x || off->y )
2971 for( i = 0; i < rgnDst->numRects; i++ )
2973 xrect[i].left = rgnSrc->rects[i].left + off->x;
2974 xrect[i].right = rgnSrc->rects[i].right + off->x;
2975 xrect[i].top = rgnSrc->rects[i].top + off->y;
2976 xrect[i].bottom = rgnSrc->rects[i].bottom + off->y;
2978 rgnDst->extents.left += off->x;
2979 rgnDst->extents.right += off->x;
2980 rgnDst->extents.top += off->y;
2981 rgnDst->extents.bottom += off->y;
2983 else
2984 memcpy( xrect, rgnSrc->rects, rgnDst->numRects * sizeof(RECT));
2985 rgnDst->rects = xrect;
2986 } else
2987 return FALSE;
2989 else if ((rect->left >= rect->right) ||
2990 (rect->top >= rect->bottom) ||
2991 !EXTENTCHECK(rect, &rgnSrc->extents))
2993 empty:
2994 if( !rgnDst->rects )
2996 rgnDst->rects = HeapAlloc(GetProcessHeap(), 0, RGN_DEFAULT_RECTS * sizeof( RECT ));
2997 if( rgnDst->rects )
2998 rgnDst->size = RGN_DEFAULT_RECTS;
2999 else
3000 return FALSE;
3003 TRACE("cropped to empty!\n");
3004 EMPTY_REGION(rgnDst);
3006 else /* region box and clipping rect appear to intersect */
3008 RECT *lpr;
3009 INT i, j, clipa, clipb;
3010 INT left = rgnSrc->extents.right + off->x;
3011 INT right = rgnSrc->extents.left + off->x;
3013 for( clipa = 0; rgnSrc->rects[clipa].bottom <= rect->top; clipa++ )
3014 ; /* skip bands above the clipping rectangle */
3016 for( clipb = clipa; clipb < rgnSrc->numRects; clipb++ )
3017 if( rgnSrc->rects[clipb].top >= rect->bottom )
3018 break; /* and below it */
3020 /* clipa - index of the first rect in the first intersecting band
3021 * clipb - index of the last rect in the last intersecting band
3024 if((rgnDst != rgnSrc) && (rgnDst->size < (i = (clipb - clipa))))
3026 rgnDst->rects = HeapReAlloc( GetProcessHeap(), 0,
3027 rgnDst->rects, i * sizeof(RECT));
3028 if( !rgnDst->rects ) return FALSE;
3029 rgnDst->size = i;
3032 if( TRACE_ON(region) )
3034 REGION_DumpRegion( rgnSrc );
3035 TRACE("\tclipa = %i, clipb = %i\n", clipa, clipb );
3038 for( i = clipa, j = 0; i < clipb ; i++ )
3040 /* i - src index, j - dst index, j is always <= i for obvious reasons */
3042 lpr = rgnSrc->rects + i;
3043 if( lpr->left < rect->right && lpr->right > rect->left )
3045 rgnDst->rects[j].top = lpr->top + off->y;
3046 rgnDst->rects[j].bottom = lpr->bottom + off->y;
3047 rgnDst->rects[j].left = ((lpr->left > rect->left) ? lpr->left : rect->left) + off->x;
3048 rgnDst->rects[j].right = ((lpr->right < rect->right) ? lpr->right : rect->right) + off->x;
3050 if( rgnDst->rects[j].left < left ) left = rgnDst->rects[j].left;
3051 if( rgnDst->rects[j].right > right ) right = rgnDst->rects[j].right;
3053 j++;
3057 if( j == 0 ) goto empty;
3059 rgnDst->extents.left = left;
3060 rgnDst->extents.right = right;
3062 left = rect->top + off->y;
3063 right = rect->bottom + off->y;
3065 rgnDst->numRects = j--;
3066 for( i = 0; i <= j; i++ ) /* fixup top band */
3067 if( rgnDst->rects[i].top < left )
3068 rgnDst->rects[i].top = left;
3069 else
3070 break;
3072 for( i = j; i >= 0; i-- ) /* fixup bottom band */
3073 if( rgnDst->rects[i].bottom > right )
3074 rgnDst->rects[i].bottom = right;
3075 else
3076 break;
3078 rgnDst->extents.top = rgnDst->rects[0].top;
3079 rgnDst->extents.bottom = rgnDst->rects[j].bottom;
3081 rgnDst->type = (j >= 1) ? COMPLEXREGION : SIMPLEREGION;
3083 if( TRACE_ON(region) )
3085 TRACE("result:\n");
3086 REGION_DumpRegion( rgnDst );
3090 return TRUE;
3093 /***********************************************************************
3094 * REGION_CropRgn
3097 * hSrc: Region to crop and offset.
3098 * lpRect: Clipping rectangle. Can be NULL (no clipping).
3099 * lpPt: Points to offset the cropped region. Can be NULL (no offset).
3101 * hDst: Region to hold the result (a new region is created if it's 0).
3102 * Allowed to be the same region as hSrc in which case everything
3103 * will be done in place, with no memory reallocations.
3105 * Returns: hDst if success, 0 otherwise.
3107 HRGN REGION_CropRgn( HRGN hDst, HRGN hSrc, const RECT *lpRect, const POINT *lpPt )
3109 /* Optimization of the following generic code:
3111 HRGN h;
3113 if( lpRect )
3114 h = CreateRectRgn( lpRect->left, lpRect->top, lpRect->right, lpRect->bottom );
3115 else
3116 h = CreateRectRgn( 0, 0, 0, 0 );
3117 if( hDst == 0 ) hDst = h;
3118 if( lpRect )
3119 CombineRgn( hDst, hSrc, h, RGN_AND );
3120 else
3121 CombineRgn( hDst, hSrc, 0, RGN_COPY );
3122 if( lpPt )
3123 OffsetRgn( hDst, lpPt->x, lpPt->y );
3124 if( hDst != h )
3125 DeleteObject( h );
3126 return hDst;
3130 RGNOBJ *objSrc = (RGNOBJ *) GDI_GetObjPtr( hSrc, REGION_MAGIC );
3132 if(objSrc)
3134 RGNOBJ *objDst;
3135 WINEREGION *rgnDst;
3137 if( hDst )
3139 if (!(objDst = (RGNOBJ *) GDI_GetObjPtr( hDst, REGION_MAGIC )))
3141 hDst = 0;
3142 goto done;
3144 rgnDst = objDst->rgn;
3146 else
3148 if ((rgnDst = HeapAlloc(GetProcessHeap(), 0, sizeof( WINEREGION ))))
3150 rgnDst->size = rgnDst->numRects = 0;
3151 rgnDst->rects = NULL; /* back end will allocate exact number */
3155 if( rgnDst )
3157 POINT pt = { 0, 0 };
3159 if( !lpPt ) lpPt = &pt;
3161 if( lpRect )
3162 TRACE("src %p -> dst %p (%i,%i)-(%i,%i) by (%li,%li)\n", objSrc->rgn, rgnDst,
3163 lpRect->left, lpRect->top, lpRect->right, lpRect->bottom, lpPt->x, lpPt->y );
3164 else
3165 TRACE("src %p -> dst %p by (%li,%li)\n", objSrc->rgn, rgnDst, lpPt->x, lpPt->y );
3167 if( REGION_CropAndOffsetRegion( lpPt, lpRect, objSrc->rgn, rgnDst ) == FALSE )
3169 if( hDst ) /* existing rgn */
3171 GDI_HEAP_UNLOCK(hDst);
3172 hDst = 0;
3173 goto done;
3175 goto fail;
3177 else if( hDst == 0 )
3179 if(!(hDst = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC )))
3181 fail:
3182 if( rgnDst->rects )
3183 HeapFree( GetProcessHeap(), 0, rgnDst->rects );
3184 HeapFree( GetProcessHeap(), 0, rgnDst );
3185 goto done;
3188 objDst = (RGNOBJ *) GDI_HEAP_LOCK( hDst );
3189 objDst->rgn = rgnDst;
3192 GDI_HEAP_UNLOCK(hDst);
3194 else hDst = 0;
3195 done:
3196 GDI_HEAP_UNLOCK(hSrc);
3197 return hDst;
3199 return 0;
3202 /***********************************************************************
3203 * GetMetaRgn (GDI.328)
3205 INT WINAPI GetMetaRgn( HDC hdc, HRGN hRgn )
3207 FIXME( "stub\n" );
3209 return 0;
3213 /***********************************************************************
3214 * SetMetaRgn (GDI.455)
3216 INT WINAPI SetMetaRgn( HDC hdc )
3218 FIXME( "stub\n" );
3220 return ERROR;