xref: /OK3568_Linux_fs/external/xserver/dix/region.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************************************************
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun Copyright 1987, 1988, 1989, 1998  The Open Group
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun Permission to use, copy, modify, distribute, and sell this software and its
6*4882a593Smuzhiyun documentation for any purpose is hereby granted without fee, provided that
7*4882a593Smuzhiyun the above copyright notice appear in all copies and that both that
8*4882a593Smuzhiyun copyright notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation.
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun all copies or substantial portions of the Software.
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17*4882a593Smuzhiyun OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18*4882a593Smuzhiyun AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19*4882a593Smuzhiyun CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun Except as contained in this notice, the name of The Open Group shall not be
22*4882a593Smuzhiyun used in advertising or otherwise to promote the sale, use or other dealings
23*4882a593Smuzhiyun in this Software without prior written authorization from The Open Group.
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun Copyright 1987, 1988, 1989 by
27*4882a593Smuzhiyun Digital Equipment Corporation, Maynard, Massachusetts.
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun                         All Rights Reserved
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this software and its
32*4882a593Smuzhiyun documentation for any purpose and without fee is hereby granted,
33*4882a593Smuzhiyun provided that the above copyright notice appear in all copies and that
34*4882a593Smuzhiyun both that copyright notice and this permission notice appear in
35*4882a593Smuzhiyun supporting documentation, and that the name of Digital not be
36*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution of the
37*4882a593Smuzhiyun software without specific, written prior permission.
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40*4882a593Smuzhiyun ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41*4882a593Smuzhiyun DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42*4882a593Smuzhiyun ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43*4882a593Smuzhiyun WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44*4882a593Smuzhiyun ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45*4882a593Smuzhiyun SOFTWARE.
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun ******************************************************************/
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /* The panoramix components contained the following notice */
50*4882a593Smuzhiyun /*****************************************************************
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun Permission is hereby granted, free of charge, to any person obtaining a copy
55*4882a593Smuzhiyun of this software and associated documentation files (the "Software"), to deal
56*4882a593Smuzhiyun in the Software without restriction, including without limitation the rights
57*4882a593Smuzhiyun to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
58*4882a593Smuzhiyun copies of the Software.
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun The above copyright notice and this permission notice shall be included in
61*4882a593Smuzhiyun all copies or substantial portions of the Software.
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
64*4882a593Smuzhiyun IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
65*4882a593Smuzhiyun FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
66*4882a593Smuzhiyun DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
67*4882a593Smuzhiyun BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
68*4882a593Smuzhiyun WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
69*4882a593Smuzhiyun IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun Except as contained in this notice, the name of Digital Equipment Corporation
72*4882a593Smuzhiyun shall not be used in advertising or otherwise to promote the sale, use or other
73*4882a593Smuzhiyun dealings in this Software without prior written authorization from Digital
74*4882a593Smuzhiyun Equipment Corporation.
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun ******************************************************************/
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
79*4882a593Smuzhiyun #include <dix-config.h>
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #include "regionstr.h"
83*4882a593Smuzhiyun #include <X11/Xprotostr.h>
84*4882a593Smuzhiyun #include <X11/Xfuncproto.h>
85*4882a593Smuzhiyun #include "gc.h"
86*4882a593Smuzhiyun #include <pixman.h>
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun #undef assert
89*4882a593Smuzhiyun #ifdef REGION_DEBUG
90*4882a593Smuzhiyun #define assert(expr) { \
91*4882a593Smuzhiyun             CARD32 *foo = NULL; \
92*4882a593Smuzhiyun             if (!(expr)) { \
93*4882a593Smuzhiyun                 ErrorF("Assertion failed file %s, line %d: %s\n", \
94*4882a593Smuzhiyun                        __FILE__, __LINE__, #expr); \
95*4882a593Smuzhiyun                 *foo = 0xdeadbeef; /* to get a backtrace */ \
96*4882a593Smuzhiyun             } \
97*4882a593Smuzhiyun         }
98*4882a593Smuzhiyun #else
99*4882a593Smuzhiyun #define assert(expr)
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #define good(reg) assert(RegionIsValid(reg))
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun  * The functions in this file implement the Region abstraction used extensively
106*4882a593Smuzhiyun  * throughout the X11 sample server. A Region is simply a set of disjoint
107*4882a593Smuzhiyun  * (non-overlapping) rectangles, plus an "extent" rectangle which is the
108*4882a593Smuzhiyun  * smallest single rectangle that contains all the non-overlapping rectangles.
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * A Region is implemented as a "y-x-banded" array of rectangles.  This array
111*4882a593Smuzhiyun  * imposes two degrees of order.  First, all rectangles are sorted by top side
112*4882a593Smuzhiyun  * y coordinate first (y1), and then by left side x coordinate (x1).
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * Furthermore, the rectangles are grouped into "bands".  Each rectangle in a
115*4882a593Smuzhiyun  * band has the same top y coordinate (y1), and each has the same bottom y
116*4882a593Smuzhiyun  * coordinate (y2).  Thus all rectangles in a band differ only in their left
117*4882a593Smuzhiyun  * and right side (x1 and x2).  Bands are implicit in the array of rectangles:
118*4882a593Smuzhiyun  * there is no separate list of band start pointers.
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * The y-x band representation does not minimize rectangles.  In particular,
121*4882a593Smuzhiyun  * if a rectangle vertically crosses a band (the rectangle has scanlines in
122*4882a593Smuzhiyun  * the y1 to y2 area spanned by the band), then the rectangle may be broken
123*4882a593Smuzhiyun  * down into two or more smaller rectangles stacked one atop the other.
124*4882a593Smuzhiyun  *
125*4882a593Smuzhiyun  *  -----------				    -----------
126*4882a593Smuzhiyun  *  |         |				    |         |		    band 0
127*4882a593Smuzhiyun  *  |         |  --------		    -----------  --------
128*4882a593Smuzhiyun  *  |         |  |      |  in y-x banded    |         |  |      |   band 1
129*4882a593Smuzhiyun  *  |         |  |      |  form is	    |         |  |      |
130*4882a593Smuzhiyun  *  -----------  |      |		    -----------  --------
131*4882a593Smuzhiyun  *               |      |				 |      |   band 2
132*4882a593Smuzhiyun  *               --------				 --------
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  * An added constraint on the rectangles is that they must cover as much
135*4882a593Smuzhiyun  * horizontal area as possible: no two rectangles within a band are allowed
136*4882a593Smuzhiyun  * to touch.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  * Whenever possible, bands will be merged together to cover a greater vertical
139*4882a593Smuzhiyun  * distance (and thus reduce the number of rectangles). Two bands can be merged
140*4882a593Smuzhiyun  * only if the bottom of one touches the top of the other and they have
141*4882a593Smuzhiyun  * rectangles in the same places (of the same width, of course).
142*4882a593Smuzhiyun  *
143*4882a593Smuzhiyun  * Adam de Boor wrote most of the original region code.  Joel McCormack
144*4882a593Smuzhiyun  * substantially modified or rewrote most of the core arithmetic routines,
145*4882a593Smuzhiyun  * and added RegionValidate in order to support several speed improvements
146*4882a593Smuzhiyun  * to miValidateTree.  Bob Scheifler changed the representation to be more
147*4882a593Smuzhiyun  * compact when empty or a single rectangle, and did a bunch of gratuitous
148*4882a593Smuzhiyun  * reformatting.
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /*  true iff two Boxes overlap */
152*4882a593Smuzhiyun #define EXTENTCHECK(r1,r2) \
153*4882a593Smuzhiyun       (!( ((r1)->x2 <= (r2)->x1)  || \
154*4882a593Smuzhiyun           ((r1)->x1 >= (r2)->x2)  || \
155*4882a593Smuzhiyun           ((r1)->y2 <= (r2)->y1)  || \
156*4882a593Smuzhiyun           ((r1)->y1 >= (r2)->y2) ) )
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* true iff (x,y) is in Box */
159*4882a593Smuzhiyun #define INBOX(r,x,y) \
160*4882a593Smuzhiyun       ( ((r)->x2 >  x) && \
161*4882a593Smuzhiyun         ((r)->x1 <= x) && \
162*4882a593Smuzhiyun         ((r)->y2 >  y) && \
163*4882a593Smuzhiyun         ((r)->y1 <= y) )
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* true iff Box r1 contains Box r2 */
166*4882a593Smuzhiyun #define SUBSUMES(r1,r2) \
167*4882a593Smuzhiyun       ( ((r1)->x1 <= (r2)->x1) && \
168*4882a593Smuzhiyun         ((r1)->x2 >= (r2)->x2) && \
169*4882a593Smuzhiyun         ((r1)->y1 <= (r2)->y1) && \
170*4882a593Smuzhiyun         ((r1)->y2 >= (r2)->y2) )
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #define xfreeData(reg) if ((reg)->data && (reg)->data->size) free((reg)->data)
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun #define RECTALLOC_BAIL(pReg,n,bail) \
175*4882a593Smuzhiyun if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
176*4882a593Smuzhiyun     if (!RegionRectAlloc(pReg, n)) { goto bail; }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #define RECTALLOC(pReg,n) \
179*4882a593Smuzhiyun if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
180*4882a593Smuzhiyun     if (!RegionRectAlloc(pReg, n)) { return FALSE; }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun #define ADDRECT(pNextRect,nx1,ny1,nx2,ny2)	\
183*4882a593Smuzhiyun {						\
184*4882a593Smuzhiyun     pNextRect->x1 = nx1;			\
185*4882a593Smuzhiyun     pNextRect->y1 = ny1;			\
186*4882a593Smuzhiyun     pNextRect->x2 = nx2;			\
187*4882a593Smuzhiyun     pNextRect->y2 = ny2;			\
188*4882a593Smuzhiyun     pNextRect++;				\
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun #define NEWRECT(pReg,pNextRect,nx1,ny1,nx2,ny2)			\
192*4882a593Smuzhiyun {									\
193*4882a593Smuzhiyun     if (!(pReg)->data || ((pReg)->data->numRects == (pReg)->data->size))\
194*4882a593Smuzhiyun     {									\
195*4882a593Smuzhiyun 	if (!RegionRectAlloc(pReg, 1))					\
196*4882a593Smuzhiyun 	    return FALSE;						\
197*4882a593Smuzhiyun 	pNextRect = RegionTop(pReg);					\
198*4882a593Smuzhiyun     }									\
199*4882a593Smuzhiyun     ADDRECT(pNextRect,nx1,ny1,nx2,ny2);					\
200*4882a593Smuzhiyun     pReg->data->numRects++;						\
201*4882a593Smuzhiyun     assert(pReg->data->numRects<=pReg->data->size);			\
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun #define DOWNSIZE(reg,numRects)						 \
205*4882a593Smuzhiyun if (((numRects) < ((reg)->data->size >> 1)) && ((reg)->data->size > 50)) \
206*4882a593Smuzhiyun {									 \
207*4882a593Smuzhiyun     size_t NewSize = RegionSizeof(numRects);				 \
208*4882a593Smuzhiyun     RegDataPtr NewData =						 \
209*4882a593Smuzhiyun         (NewSize > 0) ? realloc((reg)->data, NewSize) : NULL ;		 \
210*4882a593Smuzhiyun     if (NewData)							 \
211*4882a593Smuzhiyun     {									 \
212*4882a593Smuzhiyun 	NewData->size = (numRects);					 \
213*4882a593Smuzhiyun 	(reg)->data = NewData;						 \
214*4882a593Smuzhiyun     }									 \
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun BoxRec RegionEmptyBox = { 0, 0, 0, 0 };
218*4882a593Smuzhiyun RegDataRec RegionEmptyData = { 0, 0 };
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun RegDataRec RegionBrokenData = { 0, 0 };
221*4882a593Smuzhiyun static RegionRec RegionBrokenRegion = { {0, 0, 0, 0}, &RegionBrokenData };
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun void
InitRegions(void)224*4882a593Smuzhiyun InitRegions(void)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun     pixman_region_set_static_pointers(&RegionEmptyBox, &RegionEmptyData,
227*4882a593Smuzhiyun                                       &RegionBrokenData);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /*****************************************************************
231*4882a593Smuzhiyun  *   RegionCreate(rect, size)
232*4882a593Smuzhiyun  *     This routine does a simple malloc to make a structure of
233*4882a593Smuzhiyun  *     REGION of "size" number of rectangles.
234*4882a593Smuzhiyun  *****************************************************************/
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun RegionPtr
RegionCreate(BoxPtr rect,int size)237*4882a593Smuzhiyun RegionCreate(BoxPtr rect, int size)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun     RegionPtr pReg;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun     pReg = (RegionPtr) malloc(sizeof(RegionRec));
242*4882a593Smuzhiyun     if (!pReg)
243*4882a593Smuzhiyun         return &RegionBrokenRegion;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun     RegionInit(pReg, rect, size);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun     return pReg;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun void
RegionDestroy(RegionPtr pReg)251*4882a593Smuzhiyun RegionDestroy(RegionPtr pReg)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun     pixman_region_fini(pReg);
254*4882a593Smuzhiyun     if (pReg != &RegionBrokenRegion)
255*4882a593Smuzhiyun         free(pReg);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun RegionPtr
RegionDuplicate(RegionPtr pOld)259*4882a593Smuzhiyun RegionDuplicate(RegionPtr pOld)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun     RegionPtr   pNew;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun     pNew = RegionCreate(&pOld->extents, 0);
264*4882a593Smuzhiyun     if (!pNew)
265*4882a593Smuzhiyun         return NULL;
266*4882a593Smuzhiyun     if (!RegionCopy(pNew, pOld)) {
267*4882a593Smuzhiyun         RegionDestroy(pNew);
268*4882a593Smuzhiyun         return NULL;
269*4882a593Smuzhiyun     }
270*4882a593Smuzhiyun     return pNew;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun void
RegionPrint(RegionPtr rgn)274*4882a593Smuzhiyun RegionPrint(RegionPtr rgn)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun     int num, size;
277*4882a593Smuzhiyun     int i;
278*4882a593Smuzhiyun     BoxPtr rects;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun     num = RegionNumRects(rgn);
281*4882a593Smuzhiyun     size = RegionSize(rgn);
282*4882a593Smuzhiyun     rects = RegionRects(rgn);
283*4882a593Smuzhiyun     ErrorF("[mi] num: %d size: %d\n", num, size);
284*4882a593Smuzhiyun     ErrorF("[mi] extents: %d %d %d %d\n",
285*4882a593Smuzhiyun            rgn->extents.x1, rgn->extents.y1, rgn->extents.x2, rgn->extents.y2);
286*4882a593Smuzhiyun     for (i = 0; i < num; i++)
287*4882a593Smuzhiyun         ErrorF("[mi] %d %d %d %d \n",
288*4882a593Smuzhiyun                rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2);
289*4882a593Smuzhiyun     ErrorF("[mi] \n");
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun #ifdef DEBUG
293*4882a593Smuzhiyun Bool
RegionIsValid(RegionPtr reg)294*4882a593Smuzhiyun RegionIsValid(RegionPtr reg)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun     int i, numRects;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun     if ((reg->extents.x1 > reg->extents.x2) ||
299*4882a593Smuzhiyun         (reg->extents.y1 > reg->extents.y2))
300*4882a593Smuzhiyun         return FALSE;
301*4882a593Smuzhiyun     numRects = RegionNumRects(reg);
302*4882a593Smuzhiyun     if (!numRects)
303*4882a593Smuzhiyun         return ((reg->extents.x1 == reg->extents.x2) &&
304*4882a593Smuzhiyun                 (reg->extents.y1 == reg->extents.y2) &&
305*4882a593Smuzhiyun                 (reg->data->size || (reg->data == &RegionEmptyData)));
306*4882a593Smuzhiyun     else if (numRects == 1)
307*4882a593Smuzhiyun         return !reg->data;
308*4882a593Smuzhiyun     else {
309*4882a593Smuzhiyun         BoxPtr pboxP, pboxN;
310*4882a593Smuzhiyun         BoxRec box;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun         pboxP = RegionRects(reg);
313*4882a593Smuzhiyun         box = *pboxP;
314*4882a593Smuzhiyun         box.y2 = pboxP[numRects - 1].y2;
315*4882a593Smuzhiyun         pboxN = pboxP + 1;
316*4882a593Smuzhiyun         for (i = numRects; --i > 0; pboxP++, pboxN++) {
317*4882a593Smuzhiyun             if ((pboxN->x1 >= pboxN->x2) || (pboxN->y1 >= pboxN->y2))
318*4882a593Smuzhiyun                 return FALSE;
319*4882a593Smuzhiyun             if (pboxN->x1 < box.x1)
320*4882a593Smuzhiyun                 box.x1 = pboxN->x1;
321*4882a593Smuzhiyun             if (pboxN->x2 > box.x2)
322*4882a593Smuzhiyun                 box.x2 = pboxN->x2;
323*4882a593Smuzhiyun             if ((pboxN->y1 < pboxP->y1) ||
324*4882a593Smuzhiyun                 ((pboxN->y1 == pboxP->y1) &&
325*4882a593Smuzhiyun                  ((pboxN->x1 < pboxP->x2) || (pboxN->y2 != pboxP->y2))))
326*4882a593Smuzhiyun                 return FALSE;
327*4882a593Smuzhiyun         }
328*4882a593Smuzhiyun         return ((box.x1 == reg->extents.x1) &&
329*4882a593Smuzhiyun                 (box.x2 == reg->extents.x2) &&
330*4882a593Smuzhiyun                 (box.y1 == reg->extents.y1) && (box.y2 == reg->extents.y2));
331*4882a593Smuzhiyun     }
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun #endif                          /* DEBUG */
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun Bool
RegionBreak(RegionPtr pReg)336*4882a593Smuzhiyun RegionBreak(RegionPtr pReg)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun     xfreeData(pReg);
339*4882a593Smuzhiyun     pReg->extents = RegionEmptyBox;
340*4882a593Smuzhiyun     pReg->data = &RegionBrokenData;
341*4882a593Smuzhiyun     return FALSE;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun Bool
RegionRectAlloc(RegionPtr pRgn,int n)345*4882a593Smuzhiyun RegionRectAlloc(RegionPtr pRgn, int n)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun     RegDataPtr data;
348*4882a593Smuzhiyun     size_t rgnSize;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun     if (!pRgn->data) {
351*4882a593Smuzhiyun         n++;
352*4882a593Smuzhiyun         rgnSize = RegionSizeof(n);
353*4882a593Smuzhiyun         pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL;
354*4882a593Smuzhiyun         if (!pRgn->data)
355*4882a593Smuzhiyun             return RegionBreak(pRgn);
356*4882a593Smuzhiyun         pRgn->data->numRects = 1;
357*4882a593Smuzhiyun         *RegionBoxptr(pRgn) = pRgn->extents;
358*4882a593Smuzhiyun     }
359*4882a593Smuzhiyun     else if (!pRgn->data->size) {
360*4882a593Smuzhiyun         rgnSize = RegionSizeof(n);
361*4882a593Smuzhiyun         pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL;
362*4882a593Smuzhiyun         if (!pRgn->data)
363*4882a593Smuzhiyun             return RegionBreak(pRgn);
364*4882a593Smuzhiyun         pRgn->data->numRects = 0;
365*4882a593Smuzhiyun     }
366*4882a593Smuzhiyun     else {
367*4882a593Smuzhiyun         if (n == 1) {
368*4882a593Smuzhiyun             n = pRgn->data->numRects;
369*4882a593Smuzhiyun             if (n > 500)        /* XXX pick numbers out of a hat */
370*4882a593Smuzhiyun                 n = 250;
371*4882a593Smuzhiyun         }
372*4882a593Smuzhiyun         n += pRgn->data->numRects;
373*4882a593Smuzhiyun         rgnSize = RegionSizeof(n);
374*4882a593Smuzhiyun         data = (rgnSize > 0) ? realloc(pRgn->data, rgnSize) : NULL;
375*4882a593Smuzhiyun         if (!data)
376*4882a593Smuzhiyun             return RegionBreak(pRgn);
377*4882a593Smuzhiyun         pRgn->data = data;
378*4882a593Smuzhiyun     }
379*4882a593Smuzhiyun     pRgn->data->size = n;
380*4882a593Smuzhiyun     return TRUE;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun /*======================================================================
384*4882a593Smuzhiyun  *	    Generic Region Operator
385*4882a593Smuzhiyun  *====================================================================*/
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun /*-
388*4882a593Smuzhiyun  *-----------------------------------------------------------------------
389*4882a593Smuzhiyun  * RegionCoalesce --
390*4882a593Smuzhiyun  *	Attempt to merge the boxes in the current band with those in the
391*4882a593Smuzhiyun  *	previous one.  We are guaranteed that the current band extends to
392*4882a593Smuzhiyun  *      the end of the rects array.  Used only by RegionOp.
393*4882a593Smuzhiyun  *
394*4882a593Smuzhiyun  * Results:
395*4882a593Smuzhiyun  *	The new index for the previous band.
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * Side Effects:
398*4882a593Smuzhiyun  *	If coalescing takes place:
399*4882a593Smuzhiyun  *	    - rectangles in the previous band will have their y2 fields
400*4882a593Smuzhiyun  *	      altered.
401*4882a593Smuzhiyun  *	    - pReg->data->numRects will be decreased.
402*4882a593Smuzhiyun  *
403*4882a593Smuzhiyun  *-----------------------------------------------------------------------
404*4882a593Smuzhiyun  */
405*4882a593Smuzhiyun _X_INLINE static int
RegionCoalesce(RegionPtr pReg,int prevStart,int curStart)406*4882a593Smuzhiyun RegionCoalesce(RegionPtr pReg,  /* Region to coalesce                */
407*4882a593Smuzhiyun                int prevStart,   /* Index of start of previous band   */
408*4882a593Smuzhiyun                int curStart)
409*4882a593Smuzhiyun {                               /* Index of start of current band    */
410*4882a593Smuzhiyun     BoxPtr pPrevBox;            /* Current box in previous band      */
411*4882a593Smuzhiyun     BoxPtr pCurBox;             /* Current box in current band       */
412*4882a593Smuzhiyun     int numRects;               /* Number rectangles in both bands   */
413*4882a593Smuzhiyun     int y2;                     /* Bottom of current band            */
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun     /*
416*4882a593Smuzhiyun      * Figure out how many rectangles are in the band.
417*4882a593Smuzhiyun      */
418*4882a593Smuzhiyun     numRects = curStart - prevStart;
419*4882a593Smuzhiyun     assert(numRects == pReg->data->numRects - curStart);
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun     if (!numRects)
422*4882a593Smuzhiyun         return curStart;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun     /*
425*4882a593Smuzhiyun      * The bands may only be coalesced if the bottom of the previous
426*4882a593Smuzhiyun      * matches the top scanline of the current.
427*4882a593Smuzhiyun      */
428*4882a593Smuzhiyun     pPrevBox = RegionBox(pReg, prevStart);
429*4882a593Smuzhiyun     pCurBox = RegionBox(pReg, curStart);
430*4882a593Smuzhiyun     if (pPrevBox->y2 != pCurBox->y1)
431*4882a593Smuzhiyun         return curStart;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun     /*
434*4882a593Smuzhiyun      * Make sure the bands have boxes in the same places. This
435*4882a593Smuzhiyun      * assumes that boxes have been added in such a way that they
436*4882a593Smuzhiyun      * cover the most area possible. I.e. two boxes in a band must
437*4882a593Smuzhiyun      * have some horizontal space between them.
438*4882a593Smuzhiyun      */
439*4882a593Smuzhiyun     y2 = pCurBox->y2;
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun     do {
442*4882a593Smuzhiyun         if ((pPrevBox->x1 != pCurBox->x1) || (pPrevBox->x2 != pCurBox->x2)) {
443*4882a593Smuzhiyun             return curStart;
444*4882a593Smuzhiyun         }
445*4882a593Smuzhiyun         pPrevBox++;
446*4882a593Smuzhiyun         pCurBox++;
447*4882a593Smuzhiyun         numRects--;
448*4882a593Smuzhiyun     } while (numRects);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun     /*
451*4882a593Smuzhiyun      * The bands may be merged, so set the bottom y of each box
452*4882a593Smuzhiyun      * in the previous band to the bottom y of the current band.
453*4882a593Smuzhiyun      */
454*4882a593Smuzhiyun     numRects = curStart - prevStart;
455*4882a593Smuzhiyun     pReg->data->numRects -= numRects;
456*4882a593Smuzhiyun     do {
457*4882a593Smuzhiyun         pPrevBox--;
458*4882a593Smuzhiyun         pPrevBox->y2 = y2;
459*4882a593Smuzhiyun         numRects--;
460*4882a593Smuzhiyun     } while (numRects);
461*4882a593Smuzhiyun     return prevStart;
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /* Quicky macro to avoid trivial reject procedure calls to RegionCoalesce */
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun #define Coalesce(newReg, prevBand, curBand)				\
467*4882a593Smuzhiyun     if (curBand - prevBand == newReg->data->numRects - curBand) {	\
468*4882a593Smuzhiyun 	prevBand = RegionCoalesce(newReg, prevBand, curBand);		\
469*4882a593Smuzhiyun     } else {								\
470*4882a593Smuzhiyun 	prevBand = curBand;						\
471*4882a593Smuzhiyun     }
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun /*-
474*4882a593Smuzhiyun  *-----------------------------------------------------------------------
475*4882a593Smuzhiyun  * RegionAppendNonO --
476*4882a593Smuzhiyun  *	Handle a non-overlapping band for the union and subtract operations.
477*4882a593Smuzhiyun  *      Just adds the (top/bottom-clipped) rectangles into the region.
478*4882a593Smuzhiyun  *      Doesn't have to check for subsumption or anything.
479*4882a593Smuzhiyun  *
480*4882a593Smuzhiyun  * Results:
481*4882a593Smuzhiyun  *	None.
482*4882a593Smuzhiyun  *
483*4882a593Smuzhiyun  * Side Effects:
484*4882a593Smuzhiyun  *	pReg->data->numRects is incremented and the rectangles overwritten
485*4882a593Smuzhiyun  *	with the rectangles we're passed.
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  *-----------------------------------------------------------------------
488*4882a593Smuzhiyun  */
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun _X_INLINE static Bool
RegionAppendNonO(RegionPtr pReg,BoxPtr r,BoxPtr rEnd,int y1,int y2)491*4882a593Smuzhiyun RegionAppendNonO(RegionPtr pReg, BoxPtr r, BoxPtr rEnd, int y1, int y2)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun     BoxPtr pNextRect;
494*4882a593Smuzhiyun     int newRects;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun     newRects = rEnd - r;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun     assert(y1 < y2);
499*4882a593Smuzhiyun     assert(newRects != 0);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun     /* Make sure we have enough space for all rectangles to be added */
502*4882a593Smuzhiyun     RECTALLOC(pReg, newRects);
503*4882a593Smuzhiyun     pNextRect = RegionTop(pReg);
504*4882a593Smuzhiyun     pReg->data->numRects += newRects;
505*4882a593Smuzhiyun     do {
506*4882a593Smuzhiyun         assert(r->x1 < r->x2);
507*4882a593Smuzhiyun         ADDRECT(pNextRect, r->x1, y1, r->x2, y2);
508*4882a593Smuzhiyun         r++;
509*4882a593Smuzhiyun     } while (r != rEnd);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun     return TRUE;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun #define FindBand(r, rBandEnd, rEnd, ry1)		    \
515*4882a593Smuzhiyun {							    \
516*4882a593Smuzhiyun     ry1 = r->y1;					    \
517*4882a593Smuzhiyun     rBandEnd = r+1;					    \
518*4882a593Smuzhiyun     while ((rBandEnd != rEnd) && (rBandEnd->y1 == ry1)) {   \
519*4882a593Smuzhiyun 	rBandEnd++;					    \
520*4882a593Smuzhiyun     }							    \
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun #define	AppendRegions(newReg, r, rEnd)					\
524*4882a593Smuzhiyun {									\
525*4882a593Smuzhiyun     int newRects;							\
526*4882a593Smuzhiyun     if ((newRects = rEnd - r)) {					\
527*4882a593Smuzhiyun 	RECTALLOC(newReg, newRects);					\
528*4882a593Smuzhiyun 	memmove((char *)RegionTop(newReg),(char *)r, 			\
529*4882a593Smuzhiyun               newRects * sizeof(BoxRec));				\
530*4882a593Smuzhiyun 	newReg->data->numRects += newRects;				\
531*4882a593Smuzhiyun     }									\
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun /*-
535*4882a593Smuzhiyun  *-----------------------------------------------------------------------
536*4882a593Smuzhiyun  * RegionOp --
537*4882a593Smuzhiyun  *	Apply an operation to two regions. Called by RegionUnion, RegionInverse,
538*4882a593Smuzhiyun  *	RegionSubtract, RegionIntersect....  Both regions MUST have at least one
539*4882a593Smuzhiyun  *      rectangle, and cannot be the same object.
540*4882a593Smuzhiyun  *
541*4882a593Smuzhiyun  * Results:
542*4882a593Smuzhiyun  *	TRUE if successful.
543*4882a593Smuzhiyun  *
544*4882a593Smuzhiyun  * Side Effects:
545*4882a593Smuzhiyun  *	The new region is overwritten.
546*4882a593Smuzhiyun  *	pOverlap set to TRUE if overlapFunc ever returns TRUE.
547*4882a593Smuzhiyun  *
548*4882a593Smuzhiyun  * Notes:
549*4882a593Smuzhiyun  *	The idea behind this function is to view the two regions as sets.
550*4882a593Smuzhiyun  *	Together they cover a rectangle of area that this function divides
551*4882a593Smuzhiyun  *	into horizontal bands where points are covered only by one region
552*4882a593Smuzhiyun  *	or by both. For the first case, the nonOverlapFunc is called with
553*4882a593Smuzhiyun  *	each the band and the band's upper and lower extents. For the
554*4882a593Smuzhiyun  *	second, the overlapFunc is called to process the entire band. It
555*4882a593Smuzhiyun  *	is responsible for clipping the rectangles in the band, though
556*4882a593Smuzhiyun  *	this function provides the boundaries.
557*4882a593Smuzhiyun  *	At the end of each band, the new region is coalesced, if possible,
558*4882a593Smuzhiyun  *	to reduce the number of rectangles in the region.
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  *-----------------------------------------------------------------------
561*4882a593Smuzhiyun  */
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun typedef Bool (*OverlapProcPtr) (RegionPtr pReg,
564*4882a593Smuzhiyun                                 BoxPtr r1,
565*4882a593Smuzhiyun                                 BoxPtr r1End,
566*4882a593Smuzhiyun                                 BoxPtr r2,
567*4882a593Smuzhiyun                                 BoxPtr r2End,
568*4882a593Smuzhiyun                                 short y1, short y2, Bool *pOverlap);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun static Bool
RegionOp(RegionPtr newReg,RegionPtr reg1,RegionPtr reg2,OverlapProcPtr overlapFunc,Bool appendNon1,Bool appendNon2,Bool * pOverlap)571*4882a593Smuzhiyun RegionOp(RegionPtr newReg,      /* Place to store result         */
572*4882a593Smuzhiyun          RegionPtr reg1,        /* First region in operation     */
573*4882a593Smuzhiyun          RegionPtr reg2,        /* 2d region in operation        */
574*4882a593Smuzhiyun          OverlapProcPtr overlapFunc,    /* Function to call for over-
575*4882a593Smuzhiyun                                          * lapping bands                 */
576*4882a593Smuzhiyun          Bool appendNon1,       /* Append non-overlapping bands  */
577*4882a593Smuzhiyun          /* in region 1 ? */
578*4882a593Smuzhiyun          Bool appendNon2,       /* Append non-overlapping bands  */
579*4882a593Smuzhiyun          /* in region 2 ? */
580*4882a593Smuzhiyun          Bool *pOverlap)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun     BoxPtr r1;                  /* Pointer into first region     */
583*4882a593Smuzhiyun     BoxPtr r2;                  /* Pointer into 2d region        */
584*4882a593Smuzhiyun     BoxPtr r1End;               /* End of 1st region             */
585*4882a593Smuzhiyun     BoxPtr r2End;               /* End of 2d region              */
586*4882a593Smuzhiyun     short ybot;                 /* Bottom of intersection        */
587*4882a593Smuzhiyun     short ytop;                 /* Top of intersection           */
588*4882a593Smuzhiyun     RegDataPtr oldData;         /* Old data for newReg           */
589*4882a593Smuzhiyun     int prevBand;               /* Index of start of
590*4882a593Smuzhiyun                                  * previous band in newReg       */
591*4882a593Smuzhiyun     int curBand;                /* Index of start of current
592*4882a593Smuzhiyun                                  * band in newReg                */
593*4882a593Smuzhiyun     BoxPtr r1BandEnd;           /* End of current band in r1     */
594*4882a593Smuzhiyun     BoxPtr r2BandEnd;           /* End of current band in r2     */
595*4882a593Smuzhiyun     short top;                  /* Top of non-overlapping band   */
596*4882a593Smuzhiyun     short bot;                  /* Bottom of non-overlapping band */
597*4882a593Smuzhiyun     int r1y1;                   /* Temps for r1->y1 and r2->y1   */
598*4882a593Smuzhiyun     int r2y1;
599*4882a593Smuzhiyun     int newSize;
600*4882a593Smuzhiyun     int numRects;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun     /*
603*4882a593Smuzhiyun      * Break any region computed from a broken region
604*4882a593Smuzhiyun      */
605*4882a593Smuzhiyun     if (RegionNar(reg1) || RegionNar(reg2))
606*4882a593Smuzhiyun         return RegionBreak(newReg);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun     /*
609*4882a593Smuzhiyun      * Initialization:
610*4882a593Smuzhiyun      *  set r1, r2, r1End and r2End appropriately, save the rectangles
611*4882a593Smuzhiyun      * of the destination region until the end in case it's one of
612*4882a593Smuzhiyun      * the two source regions, then mark the "new" region empty, allocating
613*4882a593Smuzhiyun      * another array of rectangles for it to use.
614*4882a593Smuzhiyun      */
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun     r1 = RegionRects(reg1);
617*4882a593Smuzhiyun     newSize = RegionNumRects(reg1);
618*4882a593Smuzhiyun     r1End = r1 + newSize;
619*4882a593Smuzhiyun     numRects = RegionNumRects(reg2);
620*4882a593Smuzhiyun     r2 = RegionRects(reg2);
621*4882a593Smuzhiyun     r2End = r2 + numRects;
622*4882a593Smuzhiyun     assert(r1 != r1End);
623*4882a593Smuzhiyun     assert(r2 != r2End);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun     oldData = NULL;
626*4882a593Smuzhiyun     if (((newReg == reg1) && (newSize > 1)) ||
627*4882a593Smuzhiyun         ((newReg == reg2) && (numRects > 1))) {
628*4882a593Smuzhiyun         oldData = newReg->data;
629*4882a593Smuzhiyun         newReg->data = &RegionEmptyData;
630*4882a593Smuzhiyun     }
631*4882a593Smuzhiyun     /* guess at new size */
632*4882a593Smuzhiyun     if (numRects > newSize)
633*4882a593Smuzhiyun         newSize = numRects;
634*4882a593Smuzhiyun     newSize <<= 1;
635*4882a593Smuzhiyun     if (!newReg->data)
636*4882a593Smuzhiyun         newReg->data = &RegionEmptyData;
637*4882a593Smuzhiyun     else if (newReg->data->size)
638*4882a593Smuzhiyun         newReg->data->numRects = 0;
639*4882a593Smuzhiyun     if (newSize > newReg->data->size)
640*4882a593Smuzhiyun         if (!RegionRectAlloc(newReg, newSize))
641*4882a593Smuzhiyun             return FALSE;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun     /*
644*4882a593Smuzhiyun      * Initialize ybot.
645*4882a593Smuzhiyun      * In the upcoming loop, ybot and ytop serve different functions depending
646*4882a593Smuzhiyun      * on whether the band being handled is an overlapping or non-overlapping
647*4882a593Smuzhiyun      * band.
648*4882a593Smuzhiyun      *  In the case of a non-overlapping band (only one of the regions
649*4882a593Smuzhiyun      * has points in the band), ybot is the bottom of the most recent
650*4882a593Smuzhiyun      * intersection and thus clips the top of the rectangles in that band.
651*4882a593Smuzhiyun      * ytop is the top of the next intersection between the two regions and
652*4882a593Smuzhiyun      * serves to clip the bottom of the rectangles in the current band.
653*4882a593Smuzhiyun      *  For an overlapping band (where the two regions intersect), ytop clips
654*4882a593Smuzhiyun      * the top of the rectangles of both regions and ybot clips the bottoms.
655*4882a593Smuzhiyun      */
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun     ybot = min(r1->y1, r2->y1);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun     /*
660*4882a593Smuzhiyun      * prevBand serves to mark the start of the previous band so rectangles
661*4882a593Smuzhiyun      * can be coalesced into larger rectangles. qv. RegionCoalesce, above.
662*4882a593Smuzhiyun      * In the beginning, there is no previous band, so prevBand == curBand
663*4882a593Smuzhiyun      * (curBand is set later on, of course, but the first band will always
664*4882a593Smuzhiyun      * start at index 0). prevBand and curBand must be indices because of
665*4882a593Smuzhiyun      * the possible expansion, and resultant moving, of the new region's
666*4882a593Smuzhiyun      * array of rectangles.
667*4882a593Smuzhiyun      */
668*4882a593Smuzhiyun     prevBand = 0;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun     do {
671*4882a593Smuzhiyun         /*
672*4882a593Smuzhiyun          * This algorithm proceeds one source-band (as opposed to a
673*4882a593Smuzhiyun          * destination band, which is determined by where the two regions
674*4882a593Smuzhiyun          * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
675*4882a593Smuzhiyun          * rectangle after the last one in the current band for their
676*4882a593Smuzhiyun          * respective regions.
677*4882a593Smuzhiyun          */
678*4882a593Smuzhiyun         assert(r1 != r1End);
679*4882a593Smuzhiyun         assert(r2 != r2End);
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun         FindBand(r1, r1BandEnd, r1End, r1y1);
682*4882a593Smuzhiyun         FindBand(r2, r2BandEnd, r2End, r2y1);
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun         /*
685*4882a593Smuzhiyun          * First handle the band that doesn't intersect, if any.
686*4882a593Smuzhiyun          *
687*4882a593Smuzhiyun          * Note that attention is restricted to one band in the
688*4882a593Smuzhiyun          * non-intersecting region at once, so if a region has n
689*4882a593Smuzhiyun          * bands between the current position and the next place it overlaps
690*4882a593Smuzhiyun          * the other, this entire loop will be passed through n times.
691*4882a593Smuzhiyun          */
692*4882a593Smuzhiyun         if (r1y1 < r2y1) {
693*4882a593Smuzhiyun             if (appendNon1) {
694*4882a593Smuzhiyun                 top = max(r1y1, ybot);
695*4882a593Smuzhiyun                 bot = min(r1->y2, r2y1);
696*4882a593Smuzhiyun                 if (top != bot) {
697*4882a593Smuzhiyun                     curBand = newReg->data->numRects;
698*4882a593Smuzhiyun                     RegionAppendNonO(newReg, r1, r1BandEnd, top, bot);
699*4882a593Smuzhiyun                     Coalesce(newReg, prevBand, curBand);
700*4882a593Smuzhiyun                 }
701*4882a593Smuzhiyun             }
702*4882a593Smuzhiyun             ytop = r2y1;
703*4882a593Smuzhiyun         }
704*4882a593Smuzhiyun         else if (r2y1 < r1y1) {
705*4882a593Smuzhiyun             if (appendNon2) {
706*4882a593Smuzhiyun                 top = max(r2y1, ybot);
707*4882a593Smuzhiyun                 bot = min(r2->y2, r1y1);
708*4882a593Smuzhiyun                 if (top != bot) {
709*4882a593Smuzhiyun                     curBand = newReg->data->numRects;
710*4882a593Smuzhiyun                     RegionAppendNonO(newReg, r2, r2BandEnd, top, bot);
711*4882a593Smuzhiyun                     Coalesce(newReg, prevBand, curBand);
712*4882a593Smuzhiyun                 }
713*4882a593Smuzhiyun             }
714*4882a593Smuzhiyun             ytop = r1y1;
715*4882a593Smuzhiyun         }
716*4882a593Smuzhiyun         else {
717*4882a593Smuzhiyun             ytop = r1y1;
718*4882a593Smuzhiyun         }
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun         /*
721*4882a593Smuzhiyun          * Now see if we've hit an intersecting band. The two bands only
722*4882a593Smuzhiyun          * intersect if ybot > ytop
723*4882a593Smuzhiyun          */
724*4882a593Smuzhiyun         ybot = min(r1->y2, r2->y2);
725*4882a593Smuzhiyun         if (ybot > ytop) {
726*4882a593Smuzhiyun             curBand = newReg->data->numRects;
727*4882a593Smuzhiyun             (*overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot,
728*4882a593Smuzhiyun                             pOverlap);
729*4882a593Smuzhiyun             Coalesce(newReg, prevBand, curBand);
730*4882a593Smuzhiyun         }
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun         /*
733*4882a593Smuzhiyun          * If we've finished with a band (y2 == ybot) we skip forward
734*4882a593Smuzhiyun          * in the region to the next band.
735*4882a593Smuzhiyun          */
736*4882a593Smuzhiyun         if (r1->y2 == ybot)
737*4882a593Smuzhiyun             r1 = r1BandEnd;
738*4882a593Smuzhiyun         if (r2->y2 == ybot)
739*4882a593Smuzhiyun             r2 = r2BandEnd;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun     } while (r1 != r1End && r2 != r2End);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun     /*
744*4882a593Smuzhiyun      * Deal with whichever region (if any) still has rectangles left.
745*4882a593Smuzhiyun      *
746*4882a593Smuzhiyun      * We only need to worry about banding and coalescing for the very first
747*4882a593Smuzhiyun      * band left.  After that, we can just group all remaining boxes,
748*4882a593Smuzhiyun      * regardless of how many bands, into one final append to the list.
749*4882a593Smuzhiyun      */
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun     if ((r1 != r1End) && appendNon1) {
752*4882a593Smuzhiyun         /* Do first nonOverlap1Func call, which may be able to coalesce */
753*4882a593Smuzhiyun         FindBand(r1, r1BandEnd, r1End, r1y1);
754*4882a593Smuzhiyun         curBand = newReg->data->numRects;
755*4882a593Smuzhiyun         RegionAppendNonO(newReg, r1, r1BandEnd, max(r1y1, ybot), r1->y2);
756*4882a593Smuzhiyun         Coalesce(newReg, prevBand, curBand);
757*4882a593Smuzhiyun         /* Just append the rest of the boxes  */
758*4882a593Smuzhiyun         AppendRegions(newReg, r1BandEnd, r1End);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun     }
761*4882a593Smuzhiyun     else if ((r2 != r2End) && appendNon2) {
762*4882a593Smuzhiyun         /* Do first nonOverlap2Func call, which may be able to coalesce */
763*4882a593Smuzhiyun         FindBand(r2, r2BandEnd, r2End, r2y1);
764*4882a593Smuzhiyun         curBand = newReg->data->numRects;
765*4882a593Smuzhiyun         RegionAppendNonO(newReg, r2, r2BandEnd, max(r2y1, ybot), r2->y2);
766*4882a593Smuzhiyun         Coalesce(newReg, prevBand, curBand);
767*4882a593Smuzhiyun         /* Append rest of boxes */
768*4882a593Smuzhiyun         AppendRegions(newReg, r2BandEnd, r2End);
769*4882a593Smuzhiyun     }
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun     free(oldData);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun     if (!(numRects = newReg->data->numRects)) {
774*4882a593Smuzhiyun         xfreeData(newReg);
775*4882a593Smuzhiyun         newReg->data = &RegionEmptyData;
776*4882a593Smuzhiyun     }
777*4882a593Smuzhiyun     else if (numRects == 1) {
778*4882a593Smuzhiyun         newReg->extents = *RegionBoxptr(newReg);
779*4882a593Smuzhiyun         xfreeData(newReg);
780*4882a593Smuzhiyun         newReg->data = NULL;
781*4882a593Smuzhiyun     }
782*4882a593Smuzhiyun     else {
783*4882a593Smuzhiyun         DOWNSIZE(newReg, numRects);
784*4882a593Smuzhiyun     }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun     return TRUE;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /*-
790*4882a593Smuzhiyun  *-----------------------------------------------------------------------
791*4882a593Smuzhiyun  * RegionSetExtents --
792*4882a593Smuzhiyun  *	Reset the extents of a region to what they should be. Called by
793*4882a593Smuzhiyun  *	Subtract and Intersect as they can't figure it out along the
794*4882a593Smuzhiyun  *	way or do so easily, as Union can.
795*4882a593Smuzhiyun  *
796*4882a593Smuzhiyun  * Results:
797*4882a593Smuzhiyun  *	None.
798*4882a593Smuzhiyun  *
799*4882a593Smuzhiyun  * Side Effects:
800*4882a593Smuzhiyun  *	The region's 'extents' structure is overwritten.
801*4882a593Smuzhiyun  *
802*4882a593Smuzhiyun  *-----------------------------------------------------------------------
803*4882a593Smuzhiyun  */
804*4882a593Smuzhiyun static void
RegionSetExtents(RegionPtr pReg)805*4882a593Smuzhiyun RegionSetExtents(RegionPtr pReg)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun     BoxPtr pBox, pBoxEnd;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun     if (!pReg->data)
810*4882a593Smuzhiyun         return;
811*4882a593Smuzhiyun     if (!pReg->data->size) {
812*4882a593Smuzhiyun         pReg->extents.x2 = pReg->extents.x1;
813*4882a593Smuzhiyun         pReg->extents.y2 = pReg->extents.y1;
814*4882a593Smuzhiyun         return;
815*4882a593Smuzhiyun     }
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun     pBox = RegionBoxptr(pReg);
818*4882a593Smuzhiyun     pBoxEnd = RegionEnd(pReg);
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun     /*
821*4882a593Smuzhiyun      * Since pBox is the first rectangle in the region, it must have the
822*4882a593Smuzhiyun      * smallest y1 and since pBoxEnd is the last rectangle in the region,
823*4882a593Smuzhiyun      * it must have the largest y2, because of banding. Initialize x1 and
824*4882a593Smuzhiyun      * x2 from  pBox and pBoxEnd, resp., as good things to initialize them
825*4882a593Smuzhiyun      * to...
826*4882a593Smuzhiyun      */
827*4882a593Smuzhiyun     pReg->extents.x1 = pBox->x1;
828*4882a593Smuzhiyun     pReg->extents.y1 = pBox->y1;
829*4882a593Smuzhiyun     pReg->extents.x2 = pBoxEnd->x2;
830*4882a593Smuzhiyun     pReg->extents.y2 = pBoxEnd->y2;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun     assert(pReg->extents.y1 < pReg->extents.y2);
833*4882a593Smuzhiyun     while (pBox <= pBoxEnd) {
834*4882a593Smuzhiyun         if (pBox->x1 < pReg->extents.x1)
835*4882a593Smuzhiyun             pReg->extents.x1 = pBox->x1;
836*4882a593Smuzhiyun         if (pBox->x2 > pReg->extents.x2)
837*4882a593Smuzhiyun             pReg->extents.x2 = pBox->x2;
838*4882a593Smuzhiyun         pBox++;
839*4882a593Smuzhiyun     };
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun     assert(pReg->extents.x1 < pReg->extents.x2);
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun /*======================================================================
845*4882a593Smuzhiyun  *	    Region Intersection
846*4882a593Smuzhiyun  *====================================================================*/
847*4882a593Smuzhiyun /*-
848*4882a593Smuzhiyun  *-----------------------------------------------------------------------
849*4882a593Smuzhiyun  * RegionIntersectO --
850*4882a593Smuzhiyun  *	Handle an overlapping band for RegionIntersect.
851*4882a593Smuzhiyun  *
852*4882a593Smuzhiyun  * Results:
853*4882a593Smuzhiyun  *	TRUE if successful.
854*4882a593Smuzhiyun  *
855*4882a593Smuzhiyun  * Side Effects:
856*4882a593Smuzhiyun  *	Rectangles may be added to the region.
857*4882a593Smuzhiyun  *
858*4882a593Smuzhiyun  *-----------------------------------------------------------------------
859*4882a593Smuzhiyun  */
860*4882a593Smuzhiyun  /*ARGSUSED*/
861*4882a593Smuzhiyun #define MERGERECT(r)						\
862*4882a593Smuzhiyun {								\
863*4882a593Smuzhiyun     if (r->x1 <= x2) {						\
864*4882a593Smuzhiyun 	/* Merge with current rectangle */			\
865*4882a593Smuzhiyun 	if (r->x1 < x2) *pOverlap = TRUE;				\
866*4882a593Smuzhiyun 	if (x2 < r->x2) x2 = r->x2;				\
867*4882a593Smuzhiyun     } else {							\
868*4882a593Smuzhiyun 	/* Add current rectangle, start new one */		\
869*4882a593Smuzhiyun 	NEWRECT(pReg, pNextRect, x1, y1, x2, y2);		\
870*4882a593Smuzhiyun 	x1 = r->x1;						\
871*4882a593Smuzhiyun 	x2 = r->x2;						\
872*4882a593Smuzhiyun     }								\
873*4882a593Smuzhiyun     r++;							\
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun /*======================================================================
876*4882a593Smuzhiyun  *	    Region Union
877*4882a593Smuzhiyun  *====================================================================*/
878*4882a593Smuzhiyun /*-
879*4882a593Smuzhiyun  *-----------------------------------------------------------------------
880*4882a593Smuzhiyun  * RegionUnionO --
881*4882a593Smuzhiyun  *	Handle an overlapping band for the union operation. Picks the
882*4882a593Smuzhiyun  *	left-most rectangle each time and merges it into the region.
883*4882a593Smuzhiyun  *
884*4882a593Smuzhiyun  * Results:
885*4882a593Smuzhiyun  *	TRUE if successful.
886*4882a593Smuzhiyun  *
887*4882a593Smuzhiyun  * Side Effects:
888*4882a593Smuzhiyun  *	pReg is overwritten.
889*4882a593Smuzhiyun  *	pOverlap is set to TRUE if any boxes overlap.
890*4882a593Smuzhiyun  *
891*4882a593Smuzhiyun  *-----------------------------------------------------------------------
892*4882a593Smuzhiyun  */
893*4882a593Smuzhiyun     static Bool
RegionUnionO(RegionPtr pReg,BoxPtr r1,BoxPtr r1End,BoxPtr r2,BoxPtr r2End,short y1,short y2,Bool * pOverlap)894*4882a593Smuzhiyun RegionUnionO(RegionPtr pReg,
895*4882a593Smuzhiyun              BoxPtr r1,
896*4882a593Smuzhiyun              BoxPtr r1End,
897*4882a593Smuzhiyun              BoxPtr r2, BoxPtr r2End, short y1, short y2, Bool *pOverlap)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun     BoxPtr pNextRect;
900*4882a593Smuzhiyun     int x1;                     /* left and right side of current union */
901*4882a593Smuzhiyun     int x2;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun     assert(y1 < y2);
904*4882a593Smuzhiyun     assert(r1 != r1End && r2 != r2End);
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun     pNextRect = RegionTop(pReg);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun     /* Start off current rectangle */
909*4882a593Smuzhiyun     if (r1->x1 < r2->x1) {
910*4882a593Smuzhiyun         x1 = r1->x1;
911*4882a593Smuzhiyun         x2 = r1->x2;
912*4882a593Smuzhiyun         r1++;
913*4882a593Smuzhiyun     }
914*4882a593Smuzhiyun     else {
915*4882a593Smuzhiyun         x1 = r2->x1;
916*4882a593Smuzhiyun         x2 = r2->x2;
917*4882a593Smuzhiyun         r2++;
918*4882a593Smuzhiyun     }
919*4882a593Smuzhiyun     while (r1 != r1End && r2 != r2End) {
920*4882a593Smuzhiyun         if (r1->x1 < r2->x1)
921*4882a593Smuzhiyun             MERGERECT(r1)
922*4882a593Smuzhiyun             else
923*4882a593Smuzhiyun             MERGERECT(r2);
924*4882a593Smuzhiyun     }
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun     /* Finish off whoever (if any) is left */
927*4882a593Smuzhiyun     if (r1 != r1End) {
928*4882a593Smuzhiyun         do {
929*4882a593Smuzhiyun             MERGERECT(r1);
930*4882a593Smuzhiyun         } while (r1 != r1End);
931*4882a593Smuzhiyun     }
932*4882a593Smuzhiyun     else if (r2 != r2End) {
933*4882a593Smuzhiyun         do {
934*4882a593Smuzhiyun             MERGERECT(r2);
935*4882a593Smuzhiyun         } while (r2 != r2End);
936*4882a593Smuzhiyun     }
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun     /* Add current rectangle */
939*4882a593Smuzhiyun     NEWRECT(pReg, pNextRect, x1, y1, x2, y2);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun     return TRUE;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun /*======================================================================
945*4882a593Smuzhiyun  *	    Batch Rectangle Union
946*4882a593Smuzhiyun  *====================================================================*/
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun /*-
949*4882a593Smuzhiyun  *-----------------------------------------------------------------------
950*4882a593Smuzhiyun  * RegionAppend --
951*4882a593Smuzhiyun  *
952*4882a593Smuzhiyun  *      "Append" the rgn rectangles onto the end of dstrgn, maintaining
953*4882a593Smuzhiyun  *      knowledge of YX-banding when it's easy.  Otherwise, dstrgn just
954*4882a593Smuzhiyun  *      becomes a non-y-x-banded random collection of rectangles, and not
955*4882a593Smuzhiyun  *      yet a true region.  After a sequence of appends, the caller must
956*4882a593Smuzhiyun  *      call RegionValidate to ensure that a valid region is constructed.
957*4882a593Smuzhiyun  *
958*4882a593Smuzhiyun  * Results:
959*4882a593Smuzhiyun  *	TRUE if successful.
960*4882a593Smuzhiyun  *
961*4882a593Smuzhiyun  * Side Effects:
962*4882a593Smuzhiyun  *      dstrgn is modified if rgn has rectangles.
963*4882a593Smuzhiyun  *
964*4882a593Smuzhiyun  */
965*4882a593Smuzhiyun Bool
RegionAppend(RegionPtr dstrgn,RegionPtr rgn)966*4882a593Smuzhiyun RegionAppend(RegionPtr dstrgn, RegionPtr rgn)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun     int numRects, dnumRects, size;
969*4882a593Smuzhiyun     BoxPtr new, old;
970*4882a593Smuzhiyun     Bool prepend;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun     if (RegionNar(rgn))
973*4882a593Smuzhiyun         return RegionBreak(dstrgn);
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun     if (!rgn->data && (dstrgn->data == &RegionEmptyData)) {
976*4882a593Smuzhiyun         dstrgn->extents = rgn->extents;
977*4882a593Smuzhiyun         dstrgn->data = NULL;
978*4882a593Smuzhiyun         return TRUE;
979*4882a593Smuzhiyun     }
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun     numRects = RegionNumRects(rgn);
982*4882a593Smuzhiyun     if (!numRects)
983*4882a593Smuzhiyun         return TRUE;
984*4882a593Smuzhiyun     prepend = FALSE;
985*4882a593Smuzhiyun     size = numRects;
986*4882a593Smuzhiyun     dnumRects = RegionNumRects(dstrgn);
987*4882a593Smuzhiyun     if (!dnumRects && (size < 200))
988*4882a593Smuzhiyun         size = 200;             /* XXX pick numbers out of a hat */
989*4882a593Smuzhiyun     RECTALLOC(dstrgn, size);
990*4882a593Smuzhiyun     old = RegionRects(rgn);
991*4882a593Smuzhiyun     if (!dnumRects)
992*4882a593Smuzhiyun         dstrgn->extents = rgn->extents;
993*4882a593Smuzhiyun     else if (dstrgn->extents.x2 > dstrgn->extents.x1) {
994*4882a593Smuzhiyun         BoxPtr first, last;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun         first = old;
997*4882a593Smuzhiyun         last = RegionBoxptr(dstrgn) + (dnumRects - 1);
998*4882a593Smuzhiyun         if ((first->y1 > last->y2) ||
999*4882a593Smuzhiyun             ((first->y1 == last->y1) && (first->y2 == last->y2) &&
1000*4882a593Smuzhiyun              (first->x1 > last->x2))) {
1001*4882a593Smuzhiyun             if (rgn->extents.x1 < dstrgn->extents.x1)
1002*4882a593Smuzhiyun                 dstrgn->extents.x1 = rgn->extents.x1;
1003*4882a593Smuzhiyun             if (rgn->extents.x2 > dstrgn->extents.x2)
1004*4882a593Smuzhiyun                 dstrgn->extents.x2 = rgn->extents.x2;
1005*4882a593Smuzhiyun             dstrgn->extents.y2 = rgn->extents.y2;
1006*4882a593Smuzhiyun         }
1007*4882a593Smuzhiyun         else {
1008*4882a593Smuzhiyun             first = RegionBoxptr(dstrgn);
1009*4882a593Smuzhiyun             last = old + (numRects - 1);
1010*4882a593Smuzhiyun             if ((first->y1 > last->y2) ||
1011*4882a593Smuzhiyun                 ((first->y1 == last->y1) && (first->y2 == last->y2) &&
1012*4882a593Smuzhiyun                  (first->x1 > last->x2))) {
1013*4882a593Smuzhiyun                 prepend = TRUE;
1014*4882a593Smuzhiyun                 if (rgn->extents.x1 < dstrgn->extents.x1)
1015*4882a593Smuzhiyun                     dstrgn->extents.x1 = rgn->extents.x1;
1016*4882a593Smuzhiyun                 if (rgn->extents.x2 > dstrgn->extents.x2)
1017*4882a593Smuzhiyun                     dstrgn->extents.x2 = rgn->extents.x2;
1018*4882a593Smuzhiyun                 dstrgn->extents.y1 = rgn->extents.y1;
1019*4882a593Smuzhiyun             }
1020*4882a593Smuzhiyun             else
1021*4882a593Smuzhiyun                 dstrgn->extents.x2 = dstrgn->extents.x1;
1022*4882a593Smuzhiyun         }
1023*4882a593Smuzhiyun     }
1024*4882a593Smuzhiyun     if (prepend) {
1025*4882a593Smuzhiyun         new = RegionBox(dstrgn, numRects);
1026*4882a593Smuzhiyun         if (dnumRects == 1)
1027*4882a593Smuzhiyun             *new = *RegionBoxptr(dstrgn);
1028*4882a593Smuzhiyun         else
1029*4882a593Smuzhiyun             memmove((char *) new, (char *) RegionBoxptr(dstrgn),
1030*4882a593Smuzhiyun                     dnumRects * sizeof(BoxRec));
1031*4882a593Smuzhiyun         new = RegionBoxptr(dstrgn);
1032*4882a593Smuzhiyun     }
1033*4882a593Smuzhiyun     else
1034*4882a593Smuzhiyun         new = RegionBoxptr(dstrgn) + dnumRects;
1035*4882a593Smuzhiyun     if (numRects == 1)
1036*4882a593Smuzhiyun         *new = *old;
1037*4882a593Smuzhiyun     else
1038*4882a593Smuzhiyun         memmove((char *) new, (char *) old, numRects * sizeof(BoxRec));
1039*4882a593Smuzhiyun     dstrgn->data->numRects += numRects;
1040*4882a593Smuzhiyun     return TRUE;
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun #define ExchangeRects(a, b) \
1044*4882a593Smuzhiyun {			    \
1045*4882a593Smuzhiyun     BoxRec     t;	    \
1046*4882a593Smuzhiyun     t = rects[a];	    \
1047*4882a593Smuzhiyun     rects[a] = rects[b];    \
1048*4882a593Smuzhiyun     rects[b] = t;	    \
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun static void
QuickSortRects(BoxRec rects[],int numRects)1052*4882a593Smuzhiyun QuickSortRects(BoxRec rects[], int numRects)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun     int y1;
1055*4882a593Smuzhiyun     int x1;
1056*4882a593Smuzhiyun     int i, j;
1057*4882a593Smuzhiyun     BoxPtr r;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun     /* Always called with numRects > 1 */
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun     do {
1062*4882a593Smuzhiyun         if (numRects == 2) {
1063*4882a593Smuzhiyun             if (rects[0].y1 > rects[1].y1 ||
1064*4882a593Smuzhiyun                 (rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1))
1065*4882a593Smuzhiyun                 ExchangeRects(0, 1);
1066*4882a593Smuzhiyun             return;
1067*4882a593Smuzhiyun         }
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun         /* Choose partition element, stick in location 0 */
1070*4882a593Smuzhiyun         ExchangeRects(0, numRects >> 1);
1071*4882a593Smuzhiyun         y1 = rects[0].y1;
1072*4882a593Smuzhiyun         x1 = rects[0].x1;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun         /* Partition array */
1075*4882a593Smuzhiyun         i = 0;
1076*4882a593Smuzhiyun         j = numRects;
1077*4882a593Smuzhiyun         do {
1078*4882a593Smuzhiyun             r = &(rects[i]);
1079*4882a593Smuzhiyun             do {
1080*4882a593Smuzhiyun                 r++;
1081*4882a593Smuzhiyun                 i++;
1082*4882a593Smuzhiyun             } while (i != numRects &&
1083*4882a593Smuzhiyun                      (r->y1 < y1 || (r->y1 == y1 && r->x1 < x1)));
1084*4882a593Smuzhiyun             r = &(rects[j]);
1085*4882a593Smuzhiyun             do {
1086*4882a593Smuzhiyun                 r--;
1087*4882a593Smuzhiyun                 j--;
1088*4882a593Smuzhiyun             } while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1));
1089*4882a593Smuzhiyun             if (i < j)
1090*4882a593Smuzhiyun                 ExchangeRects(i, j);
1091*4882a593Smuzhiyun         } while (i < j);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun         /* Move partition element back to middle */
1094*4882a593Smuzhiyun         ExchangeRects(0, j);
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun         /* Recurse */
1097*4882a593Smuzhiyun         if (numRects - j - 1 > 1)
1098*4882a593Smuzhiyun             QuickSortRects(&rects[j + 1], numRects - j - 1);
1099*4882a593Smuzhiyun         numRects = j;
1100*4882a593Smuzhiyun     } while (numRects > 1);
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun /*-
1104*4882a593Smuzhiyun  *-----------------------------------------------------------------------
1105*4882a593Smuzhiyun  * RegionValidate --
1106*4882a593Smuzhiyun  *
1107*4882a593Smuzhiyun  *      Take a ``region'' which is a non-y-x-banded random collection of
1108*4882a593Smuzhiyun  *      rectangles, and compute a nice region which is the union of all the
1109*4882a593Smuzhiyun  *      rectangles.
1110*4882a593Smuzhiyun  *
1111*4882a593Smuzhiyun  * Results:
1112*4882a593Smuzhiyun  *	TRUE if successful.
1113*4882a593Smuzhiyun  *
1114*4882a593Smuzhiyun  * Side Effects:
1115*4882a593Smuzhiyun  *      The passed-in ``region'' may be modified.
1116*4882a593Smuzhiyun  *	pOverlap set to TRUE if any retangles overlapped, else FALSE;
1117*4882a593Smuzhiyun  *
1118*4882a593Smuzhiyun  * Strategy:
1119*4882a593Smuzhiyun  *      Step 1. Sort the rectangles into ascending order with primary key y1
1120*4882a593Smuzhiyun  *		and secondary key x1.
1121*4882a593Smuzhiyun  *
1122*4882a593Smuzhiyun  *      Step 2. Split the rectangles into the minimum number of proper y-x
1123*4882a593Smuzhiyun  *		banded regions.  This may require horizontally merging
1124*4882a593Smuzhiyun  *		rectangles, and vertically coalescing bands.  With any luck,
1125*4882a593Smuzhiyun  *		this step in an identity tranformation (ala the Box widget),
1126*4882a593Smuzhiyun  *		or a coalescing into 1 box (ala Menus).
1127*4882a593Smuzhiyun  *
1128*4882a593Smuzhiyun  *	Step 3. Merge the separate regions down to a single region by calling
1129*4882a593Smuzhiyun  *		Union.  Maximize the work each Union call does by using
1130*4882a593Smuzhiyun  *		a binary merge.
1131*4882a593Smuzhiyun  *
1132*4882a593Smuzhiyun  *-----------------------------------------------------------------------
1133*4882a593Smuzhiyun  */
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun Bool
RegionValidate(RegionPtr badreg,Bool * pOverlap)1136*4882a593Smuzhiyun RegionValidate(RegionPtr badreg, Bool *pOverlap)
1137*4882a593Smuzhiyun {
1138*4882a593Smuzhiyun     /* Descriptor for regions under construction  in Step 2. */
1139*4882a593Smuzhiyun     typedef struct {
1140*4882a593Smuzhiyun         RegionRec reg;
1141*4882a593Smuzhiyun         int prevBand;
1142*4882a593Smuzhiyun         int curBand;
1143*4882a593Smuzhiyun     } RegionInfo;
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun     int numRects;               /* Original numRects for badreg         */
1146*4882a593Smuzhiyun     RegionInfo *ri;             /* Array of current regions             */
1147*4882a593Smuzhiyun     int numRI;                  /* Number of entries used in ri         */
1148*4882a593Smuzhiyun     int sizeRI;                 /* Number of entries available in ri    */
1149*4882a593Smuzhiyun     int i;                      /* Index into rects                     */
1150*4882a593Smuzhiyun     int j;                      /* Index into ri                        */
1151*4882a593Smuzhiyun     RegionInfo *rit;            /* &ri[j]                                */
1152*4882a593Smuzhiyun     RegionPtr reg;              /* ri[j].reg                     */
1153*4882a593Smuzhiyun     BoxPtr box;                 /* Current box in rects                 */
1154*4882a593Smuzhiyun     BoxPtr riBox;               /* Last box in ri[j].reg                */
1155*4882a593Smuzhiyun     RegionPtr hreg;             /* ri[j_half].reg                        */
1156*4882a593Smuzhiyun     Bool ret = TRUE;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun     *pOverlap = FALSE;
1159*4882a593Smuzhiyun     if (!badreg->data) {
1160*4882a593Smuzhiyun         good(badreg);
1161*4882a593Smuzhiyun         return TRUE;
1162*4882a593Smuzhiyun     }
1163*4882a593Smuzhiyun     numRects = badreg->data->numRects;
1164*4882a593Smuzhiyun     if (!numRects) {
1165*4882a593Smuzhiyun         if (RegionNar(badreg))
1166*4882a593Smuzhiyun             return FALSE;
1167*4882a593Smuzhiyun         good(badreg);
1168*4882a593Smuzhiyun         return TRUE;
1169*4882a593Smuzhiyun     }
1170*4882a593Smuzhiyun     if (badreg->extents.x1 < badreg->extents.x2) {
1171*4882a593Smuzhiyun         if ((numRects) == 1) {
1172*4882a593Smuzhiyun             xfreeData(badreg);
1173*4882a593Smuzhiyun             badreg->data = (RegDataPtr) NULL;
1174*4882a593Smuzhiyun         }
1175*4882a593Smuzhiyun         else {
1176*4882a593Smuzhiyun             DOWNSIZE(badreg, numRects);
1177*4882a593Smuzhiyun         }
1178*4882a593Smuzhiyun         good(badreg);
1179*4882a593Smuzhiyun         return TRUE;
1180*4882a593Smuzhiyun     }
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun     /* Step 1: Sort the rects array into ascending (y1, x1) order */
1183*4882a593Smuzhiyun     QuickSortRects(RegionBoxptr(badreg), numRects);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun     /* Step 2: Scatter the sorted array into the minimum number of regions */
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun     /* Set up the first region to be the first rectangle in badreg */
1188*4882a593Smuzhiyun     /* Note that step 2 code will never overflow the ri[0].reg rects array */
1189*4882a593Smuzhiyun     ri = (RegionInfo *) malloc(4 * sizeof(RegionInfo));
1190*4882a593Smuzhiyun     if (!ri)
1191*4882a593Smuzhiyun         return RegionBreak(badreg);
1192*4882a593Smuzhiyun     sizeRI = 4;
1193*4882a593Smuzhiyun     numRI = 1;
1194*4882a593Smuzhiyun     ri[0].prevBand = 0;
1195*4882a593Smuzhiyun     ri[0].curBand = 0;
1196*4882a593Smuzhiyun     ri[0].reg = *badreg;
1197*4882a593Smuzhiyun     box = RegionBoxptr(&ri[0].reg);
1198*4882a593Smuzhiyun     ri[0].reg.extents = *box;
1199*4882a593Smuzhiyun     ri[0].reg.data->numRects = 1;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun     /* Now scatter rectangles into the minimum set of valid regions.  If the
1202*4882a593Smuzhiyun        next rectangle to be added to a region would force an existing rectangle
1203*4882a593Smuzhiyun        in the region to be split up in order to maintain y-x banding, just
1204*4882a593Smuzhiyun        forget it.  Try the next region.  If it doesn't fit cleanly into any
1205*4882a593Smuzhiyun        region, make a new one. */
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun     for (i = numRects; --i > 0;) {
1208*4882a593Smuzhiyun         box++;
1209*4882a593Smuzhiyun         /* Look for a region to append box to */
1210*4882a593Smuzhiyun         for (j = numRI, rit = ri; --j >= 0; rit++) {
1211*4882a593Smuzhiyun             reg = &rit->reg;
1212*4882a593Smuzhiyun             riBox = RegionEnd(reg);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun             if (box->y1 == riBox->y1 && box->y2 == riBox->y2) {
1215*4882a593Smuzhiyun                 /* box is in same band as riBox.  Merge or append it */
1216*4882a593Smuzhiyun                 if (box->x1 <= riBox->x2) {
1217*4882a593Smuzhiyun                     /* Merge it with riBox */
1218*4882a593Smuzhiyun                     if (box->x1 < riBox->x2)
1219*4882a593Smuzhiyun                         *pOverlap = TRUE;
1220*4882a593Smuzhiyun                     if (box->x2 > riBox->x2)
1221*4882a593Smuzhiyun                         riBox->x2 = box->x2;
1222*4882a593Smuzhiyun                 }
1223*4882a593Smuzhiyun                 else {
1224*4882a593Smuzhiyun                     RECTALLOC_BAIL(reg, 1, bail);
1225*4882a593Smuzhiyun                     *RegionTop(reg) = *box;
1226*4882a593Smuzhiyun                     reg->data->numRects++;
1227*4882a593Smuzhiyun                 }
1228*4882a593Smuzhiyun                 goto NextRect;  /* So sue me */
1229*4882a593Smuzhiyun             }
1230*4882a593Smuzhiyun             else if (box->y1 >= riBox->y2) {
1231*4882a593Smuzhiyun                 /* Put box into new band */
1232*4882a593Smuzhiyun                 if (reg->extents.x2 < riBox->x2)
1233*4882a593Smuzhiyun                     reg->extents.x2 = riBox->x2;
1234*4882a593Smuzhiyun                 if (reg->extents.x1 > box->x1)
1235*4882a593Smuzhiyun                     reg->extents.x1 = box->x1;
1236*4882a593Smuzhiyun                 Coalesce(reg, rit->prevBand, rit->curBand);
1237*4882a593Smuzhiyun                 rit->curBand = reg->data->numRects;
1238*4882a593Smuzhiyun                 RECTALLOC_BAIL(reg, 1, bail);
1239*4882a593Smuzhiyun                 *RegionTop(reg) = *box;
1240*4882a593Smuzhiyun                 reg->data->numRects++;
1241*4882a593Smuzhiyun                 goto NextRect;
1242*4882a593Smuzhiyun             }
1243*4882a593Smuzhiyun             /* Well, this region was inappropriate.  Try the next one. */
1244*4882a593Smuzhiyun         }                       /* for j */
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun         /* Uh-oh.  No regions were appropriate.  Create a new one. */
1247*4882a593Smuzhiyun         if (sizeRI == numRI) {
1248*4882a593Smuzhiyun             /* Oops, allocate space for new region information */
1249*4882a593Smuzhiyun             sizeRI <<= 1;
1250*4882a593Smuzhiyun             rit = (RegionInfo *) reallocarray(ri, sizeRI, sizeof(RegionInfo));
1251*4882a593Smuzhiyun             if (!rit)
1252*4882a593Smuzhiyun                 goto bail;
1253*4882a593Smuzhiyun             ri = rit;
1254*4882a593Smuzhiyun             rit = &ri[numRI];
1255*4882a593Smuzhiyun         }
1256*4882a593Smuzhiyun         numRI++;
1257*4882a593Smuzhiyun         rit->prevBand = 0;
1258*4882a593Smuzhiyun         rit->curBand = 0;
1259*4882a593Smuzhiyun         rit->reg.extents = *box;
1260*4882a593Smuzhiyun         rit->reg.data = NULL;
1261*4882a593Smuzhiyun         if (!RegionRectAlloc(&rit->reg, (i + numRI) / numRI))   /* MUST force allocation */
1262*4882a593Smuzhiyun             goto bail;
1263*4882a593Smuzhiyun  NextRect:;
1264*4882a593Smuzhiyun     }                           /* for i */
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun     /* Make a final pass over each region in order to Coalesce and set
1267*4882a593Smuzhiyun        extents.x2 and extents.y2 */
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun     for (j = numRI, rit = ri; --j >= 0; rit++) {
1270*4882a593Smuzhiyun         reg = &rit->reg;
1271*4882a593Smuzhiyun         riBox = RegionEnd(reg);
1272*4882a593Smuzhiyun         reg->extents.y2 = riBox->y2;
1273*4882a593Smuzhiyun         if (reg->extents.x2 < riBox->x2)
1274*4882a593Smuzhiyun             reg->extents.x2 = riBox->x2;
1275*4882a593Smuzhiyun         Coalesce(reg, rit->prevBand, rit->curBand);
1276*4882a593Smuzhiyun         if (reg->data->numRects == 1) { /* keep unions happy below */
1277*4882a593Smuzhiyun             xfreeData(reg);
1278*4882a593Smuzhiyun             reg->data = NULL;
1279*4882a593Smuzhiyun         }
1280*4882a593Smuzhiyun     }
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun     /* Step 3: Union all regions into a single region */
1283*4882a593Smuzhiyun     while (numRI > 1) {
1284*4882a593Smuzhiyun         int half = numRI / 2;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun         for (j = numRI & 1; j < (half + (numRI & 1)); j++) {
1287*4882a593Smuzhiyun             reg = &ri[j].reg;
1288*4882a593Smuzhiyun             hreg = &ri[j + half].reg;
1289*4882a593Smuzhiyun             if (!RegionOp(reg, reg, hreg, RegionUnionO, TRUE, TRUE, pOverlap))
1290*4882a593Smuzhiyun                 ret = FALSE;
1291*4882a593Smuzhiyun             if (hreg->extents.x1 < reg->extents.x1)
1292*4882a593Smuzhiyun                 reg->extents.x1 = hreg->extents.x1;
1293*4882a593Smuzhiyun             if (hreg->extents.y1 < reg->extents.y1)
1294*4882a593Smuzhiyun                 reg->extents.y1 = hreg->extents.y1;
1295*4882a593Smuzhiyun             if (hreg->extents.x2 > reg->extents.x2)
1296*4882a593Smuzhiyun                 reg->extents.x2 = hreg->extents.x2;
1297*4882a593Smuzhiyun             if (hreg->extents.y2 > reg->extents.y2)
1298*4882a593Smuzhiyun                 reg->extents.y2 = hreg->extents.y2;
1299*4882a593Smuzhiyun             xfreeData(hreg);
1300*4882a593Smuzhiyun         }
1301*4882a593Smuzhiyun         numRI -= half;
1302*4882a593Smuzhiyun     }
1303*4882a593Smuzhiyun     *badreg = ri[0].reg;
1304*4882a593Smuzhiyun     free(ri);
1305*4882a593Smuzhiyun     good(badreg);
1306*4882a593Smuzhiyun     return ret;
1307*4882a593Smuzhiyun  bail:
1308*4882a593Smuzhiyun     for (i = 0; i < numRI; i++)
1309*4882a593Smuzhiyun         xfreeData(&ri[i].reg);
1310*4882a593Smuzhiyun     free(ri);
1311*4882a593Smuzhiyun     return RegionBreak(badreg);
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun RegionPtr
RegionFromRects(int nrects,xRectangle * prect,int ctype)1315*4882a593Smuzhiyun RegionFromRects(int nrects, xRectangle *prect, int ctype)
1316*4882a593Smuzhiyun {
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun     RegionPtr pRgn;
1319*4882a593Smuzhiyun     size_t rgnSize;
1320*4882a593Smuzhiyun     RegDataPtr pData;
1321*4882a593Smuzhiyun     BoxPtr pBox;
1322*4882a593Smuzhiyun     int i;
1323*4882a593Smuzhiyun     int x1, y1, x2, y2;
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun     pRgn = RegionCreate(NullBox, 0);
1326*4882a593Smuzhiyun     if (RegionNar(pRgn))
1327*4882a593Smuzhiyun         return pRgn;
1328*4882a593Smuzhiyun     if (!nrects)
1329*4882a593Smuzhiyun         return pRgn;
1330*4882a593Smuzhiyun     if (nrects == 1) {
1331*4882a593Smuzhiyun         x1 = prect->x;
1332*4882a593Smuzhiyun         y1 = prect->y;
1333*4882a593Smuzhiyun         if ((x2 = x1 + (int) prect->width) > MAXSHORT)
1334*4882a593Smuzhiyun             x2 = MAXSHORT;
1335*4882a593Smuzhiyun         if ((y2 = y1 + (int) prect->height) > MAXSHORT)
1336*4882a593Smuzhiyun             y2 = MAXSHORT;
1337*4882a593Smuzhiyun         if (x1 != x2 && y1 != y2) {
1338*4882a593Smuzhiyun             pRgn->extents.x1 = x1;
1339*4882a593Smuzhiyun             pRgn->extents.y1 = y1;
1340*4882a593Smuzhiyun             pRgn->extents.x2 = x2;
1341*4882a593Smuzhiyun             pRgn->extents.y2 = y2;
1342*4882a593Smuzhiyun             pRgn->data = NULL;
1343*4882a593Smuzhiyun         }
1344*4882a593Smuzhiyun         return pRgn;
1345*4882a593Smuzhiyun     }
1346*4882a593Smuzhiyun     rgnSize = RegionSizeof(nrects);
1347*4882a593Smuzhiyun     pData = (rgnSize > 0) ? malloc(rgnSize) : NULL;
1348*4882a593Smuzhiyun     if (!pData) {
1349*4882a593Smuzhiyun         RegionBreak(pRgn);
1350*4882a593Smuzhiyun         return pRgn;
1351*4882a593Smuzhiyun     }
1352*4882a593Smuzhiyun     pBox = (BoxPtr) (pData + 1);
1353*4882a593Smuzhiyun     for (i = nrects; --i >= 0; prect++) {
1354*4882a593Smuzhiyun         x1 = prect->x;
1355*4882a593Smuzhiyun         y1 = prect->y;
1356*4882a593Smuzhiyun         if ((x2 = x1 + (int) prect->width) > MAXSHORT)
1357*4882a593Smuzhiyun             x2 = MAXSHORT;
1358*4882a593Smuzhiyun         if ((y2 = y1 + (int) prect->height) > MAXSHORT)
1359*4882a593Smuzhiyun             y2 = MAXSHORT;
1360*4882a593Smuzhiyun         if (x1 != x2 && y1 != y2) {
1361*4882a593Smuzhiyun             pBox->x1 = x1;
1362*4882a593Smuzhiyun             pBox->y1 = y1;
1363*4882a593Smuzhiyun             pBox->x2 = x2;
1364*4882a593Smuzhiyun             pBox->y2 = y2;
1365*4882a593Smuzhiyun             pBox++;
1366*4882a593Smuzhiyun         }
1367*4882a593Smuzhiyun     }
1368*4882a593Smuzhiyun     if (pBox != (BoxPtr) (pData + 1)) {
1369*4882a593Smuzhiyun         pData->size = nrects;
1370*4882a593Smuzhiyun         pData->numRects = pBox - (BoxPtr) (pData + 1);
1371*4882a593Smuzhiyun         pRgn->data = pData;
1372*4882a593Smuzhiyun         if (ctype != CT_YXBANDED) {
1373*4882a593Smuzhiyun             Bool overlap;       /* result ignored */
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun             pRgn->extents.x1 = pRgn->extents.x2 = 0;
1376*4882a593Smuzhiyun             RegionValidate(pRgn, &overlap);
1377*4882a593Smuzhiyun         }
1378*4882a593Smuzhiyun         else
1379*4882a593Smuzhiyun             RegionSetExtents(pRgn);
1380*4882a593Smuzhiyun         good(pRgn);
1381*4882a593Smuzhiyun     }
1382*4882a593Smuzhiyun     else {
1383*4882a593Smuzhiyun         free(pData);
1384*4882a593Smuzhiyun     }
1385*4882a593Smuzhiyun     return pRgn;
1386*4882a593Smuzhiyun }
1387