summaryrefslogtreecommitdiff
path: root/dix/region.c
blob: fd7313990d05d016e876a26e1f646fe8a0ec97de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
/***********************************************************

Copyright 1987, 1988, 1989, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988, 1989 by
Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

/* The panoramix components contained the following notice */
/*****************************************************************

Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Digital Equipment Corporation
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization from Digital
Equipment Corporation.

******************************************************************/

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "regionstr.h"
#include <X11/Xprotostr.h>
#include <X11/Xfuncproto.h>
#include "gc.h"
#include <pixman.h>

#undef assert
#ifdef REGION_DEBUG
#define assert(expr) { \
            CARD32 *foo = NULL; \
            if (!(expr)) { \
                ErrorF("Assertion failed file %s, line %d: %s\n", \
                       __FILE__, __LINE__, #expr); \
                *foo = 0xdeadbeef; /* to get a backtrace */ \
            } \
        }
#else
#define assert(expr)
#endif

#define good(reg) assert(RegionIsValid(reg))

/*
 * The functions in this file implement the Region abstraction used extensively
 * throughout the X11 sample server. A Region is simply a set of disjoint
 * (non-overlapping) rectangles, plus an "extent" rectangle which is the
 * smallest single rectangle that contains all the non-overlapping rectangles.
 *
 * A Region is implemented as a "y-x-banded" array of rectangles.  This array
 * imposes two degrees of order.  First, all rectangles are sorted by top side
 * y coordinate first (y1), and then by left side x coordinate (x1).
 *
 * Furthermore, the rectangles are grouped into "bands".  Each rectangle in a
 * band has the same top y coordinate (y1), and each has the same bottom y
 * coordinate (y2).  Thus all rectangles in a band differ only in their left
 * and right side (x1 and x2).  Bands are implicit in the array of rectangles:
 * there is no separate list of band start pointers.
 *
 * The y-x band representation does not minimize rectangles.  In particular,
 * if a rectangle vertically crosses a band (the rectangle has scanlines in
 * the y1 to y2 area spanned by the band), then the rectangle may be broken
 * down into two or more smaller rectangles stacked one atop the other.
 *
 *  -----------				    -----------
 *  |         |				    |         |		    band 0
 *  |         |  --------		    -----------  --------
 *  |         |  |      |  in y-x banded    |         |  |      |   band 1
 *  |         |  |      |  form is	    |         |  |      |
 *  -----------  |      |		    -----------  --------
 *               |      |				 |      |   band 2
 *               --------				 --------
 *
 * An added constraint on the rectangles is that they must cover as much
 * horizontal area as possible: no two rectangles within a band are allowed
 * to touch.
 *
 * Whenever possible, bands will be merged together to cover a greater vertical
 * distance (and thus reduce the number of rectangles). Two bands can be merged
 * only if the bottom of one touches the top of the other and they have
 * rectangles in the same places (of the same width, of course).
 *
 * Adam de Boor wrote most of the original region code.  Joel McCormack
 * substantially modified or rewrote most of the core arithmetic routines,
 * and added RegionValidate in order to support several speed improvements
 * to miValidateTree.  Bob Scheifler changed the representation to be more
 * compact when empty or a single rectangle, and did a bunch of gratuitous
 * reformatting.
 */

/*  true iff two Boxes overlap */
#define EXTENTCHECK(r1,r2) \
      (!( ((r1)->x2 <= (r2)->x1)  || \
          ((r1)->x1 >= (r2)->x2)  || \
          ((r1)->y2 <= (r2)->y1)  || \
          ((r1)->y1 >= (r2)->y2) ) )

/* true iff (x,y) is in Box */
#define INBOX(r,x,y) \
      ( ((r)->x2 >  x) && \
        ((r)->x1 <= x) && \
        ((r)->y2 >  y) && \
        ((r)->y1 <= y) )

/* true iff Box r1 contains Box r2 */
#define SUBSUMES(r1,r2) \
      ( ((r1)->x1 <= (r2)->x1) && \
        ((r1)->x2 >= (r2)->x2) && \
        ((r1)->y1 <= (r2)->y1) && \
        ((r1)->y2 >= (r2)->y2) )

#define xfreeData(reg) if ((reg)->data && (reg)->data->size) free((reg)->data)

#define RECTALLOC_BAIL(pReg,n,bail) \
if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
    if (!RegionRectAlloc(pReg, n)) { goto bail; }

#define RECTALLOC(pReg,n) \
if (!(pReg)->data || (((pReg)->data->numRects + (n)) > (pReg)->data->size)) \
    if (!RegionRectAlloc(pReg, n)) { return FALSE; }

#define ADDRECT(pNextRect,nx1,ny1,nx2,ny2)	\
{						\
    pNextRect->x1 = nx1;			\
    pNextRect->y1 = ny1;			\
    pNextRect->x2 = nx2;			\
    pNextRect->y2 = ny2;			\
    pNextRect++;				\
}

#define NEWRECT(pReg,pNextRect,nx1,ny1,nx2,ny2)			\
{									\
    if (!(pReg)->data || ((pReg)->data->numRects == (pReg)->data->size))\
    {									\
	if (!RegionRectAlloc(pReg, 1))					\
	    return FALSE;						\
	pNextRect = RegionTop(pReg);					\
    }									\
    ADDRECT(pNextRect,nx1,ny1,nx2,ny2);					\
    pReg->data->numRects++;						\
    assert(pReg->data->numRects<=pReg->data->size);			\
}

#define DOWNSIZE(reg,numRects)						 \
if (((numRects) < ((reg)->data->size >> 1)) && ((reg)->data->size > 50)) \
{									 \
    size_t NewSize = RegionSizeof(numRects);				 \
    RegDataPtr NewData =						 \
        (NewSize > 0) ? realloc((reg)->data, NewSize) : NULL ;		 \
    if (NewData)							 \
    {									 \
	NewData->size = (numRects);					 \
	(reg)->data = NewData;						 \
    }									 \
}

BoxRec RegionEmptyBox = { 0, 0, 0, 0 };
RegDataRec RegionEmptyData = { 0, 0 };

RegDataRec RegionBrokenData = { 0, 0 };
static RegionRec RegionBrokenRegion = { {0, 0, 0, 0}, &RegionBrokenData };

void
InitRegions(void)
{
    pixman_region_set_static_pointers(&RegionEmptyBox, &RegionEmptyData,
                                      &RegionBrokenData);
}

/*****************************************************************
 *   RegionCreate(rect, size)
 *     This routine does a simple malloc to make a structure of
 *     REGION of "size" number of rectangles.
 *****************************************************************/

RegionPtr
RegionCreate(BoxPtr rect, int size)
{
    RegionPtr pReg;

    pReg = (RegionPtr) malloc(sizeof(RegionRec));
    if (!pReg)
        return &RegionBrokenRegion;

    RegionInit(pReg, rect, size);

    return pReg;
}

void
RegionDestroy(RegionPtr pReg)
{
    pixman_region_fini(pReg);
    if (pReg != &RegionBrokenRegion)
        free(pReg);
}

RegionPtr
RegionDuplicate(RegionPtr pOld)
{
    RegionPtr   pNew;

    pNew = RegionCreate(&pOld->extents, 0);
    if (!pNew)
        return NULL;
    if (!RegionCopy(pNew, pOld)) {
        RegionDestroy(pNew);
        return NULL;
    }
    return pNew;
}

void
RegionPrint(RegionPtr rgn)
{
    int num, size;
    int i;
    BoxPtr rects;

    num = RegionNumRects(rgn);
    size = RegionSize(rgn);
    rects = RegionRects(rgn);
    ErrorF("[mi] num: %d size: %d\n", num, size);
    ErrorF("[mi] extents: %d %d %d %d\n",
           rgn->extents.x1, rgn->extents.y1, rgn->extents.x2, rgn->extents.y2);
    for (i = 0; i < num; i++)
        ErrorF("[mi] %d %d %d %d \n",
               rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2);
    ErrorF("[mi] \n");
}

#ifdef DEBUG
Bool
RegionIsValid(RegionPtr reg)
{
    int i, numRects;

    if ((reg->extents.x1 > reg->extents.x2) ||
        (reg->extents.y1 > reg->extents.y2))
        return FALSE;
    numRects = RegionNumRects(reg);
    if (!numRects)
        return ((reg->extents.x1 == reg->extents.x2) &&
                (reg->extents.y1 == reg->extents.y2) &&
                (reg->data->size || (reg->data == &RegionEmptyData)));
    else if (numRects == 1)
        return !reg->data;
    else {
        BoxPtr pboxP, pboxN;
        BoxRec box;

        pboxP = RegionRects(reg);
        box = *pboxP;
        box.y2 = pboxP[numRects - 1].y2;
        pboxN = pboxP + 1;
        for (i = numRects; --i > 0; pboxP++, pboxN++) {
            if ((pboxN->x1 >= pboxN->x2) || (pboxN->y1 >= pboxN->y2))
                return FALSE;
            if (pboxN->x1 < box.x1)
                box.x1 = pboxN->x1;
            if (pboxN->x2 > box.x2)
                box.x2 = pboxN->x2;
            if ((pboxN->y1 < pboxP->y1) ||
                ((pboxN->y1 == pboxP->y1) &&
                 ((pboxN->x1 < pboxP->x2) || (pboxN->y2 != pboxP->y2))))
                return FALSE;
        }
        return ((box.x1 == reg->extents.x1) &&
                (box.x2 == reg->extents.x2) &&
                (box.y1 == reg->extents.y1) && (box.y2 == reg->extents.y2));
    }
}
#endif                          /* DEBUG */

Bool
RegionBreak(RegionPtr pReg)
{
    xfreeData(pReg);
    pReg->extents = RegionEmptyBox;
    pReg->data = &RegionBrokenData;
    return FALSE;
}

Bool
RegionRectAlloc(RegionPtr pRgn, int n)
{
    RegDataPtr data;
    size_t rgnSize;

    if (!pRgn->data) {
        n++;
        rgnSize = RegionSizeof(n);
        pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL;
        if (!pRgn->data)
            return RegionBreak(pRgn);
        pRgn->data->numRects = 1;
        *RegionBoxptr(pRgn) = pRgn->extents;
    }
    else if (!pRgn->data->size) {
        rgnSize = RegionSizeof(n);
        pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL;
        if (!pRgn->data)
            return RegionBreak(pRgn);
        pRgn->data->numRects = 0;
    }
    else {
        if (n == 1) {
            n = pRgn->data->numRects;
            if (n > 500)        /* XXX pick numbers out of a hat */
                n = 250;
        }
        n += pRgn->data->numRects;
        rgnSize = RegionSizeof(n);
        data = (rgnSize > 0) ? realloc(pRgn->data, rgnSize) : NULL;
        if (!data)
            return RegionBreak(pRgn);
        pRgn->data = data;
    }
    pRgn->data->size = n;
    return TRUE;
}

/*======================================================================
 *	    Generic Region Operator
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * RegionCoalesce --
 *	Attempt to merge the boxes in the current band with those in the
 *	previous one.  We are guaranteed that the current band extends to
 *      the end of the rects array.  Used only by RegionOp.
 *
 * Results:
 *	The new index for the previous band.
 *
 * Side Effects:
 *	If coalescing takes place:
 *	    - rectangles in the previous band will have their y2 fields
 *	      altered.
 *	    - pReg->data->numRects will be decreased.
 *
 *-----------------------------------------------------------------------
 */
_X_INLINE static int
RegionCoalesce(RegionPtr pReg,  /* Region to coalesce                */
               int prevStart,   /* Index of start of previous band   */
               int curStart)
{                               /* Index of start of current band    */
    BoxPtr pPrevBox;            /* Current box in previous band      */
    BoxPtr pCurBox;             /* Current box in current band       */
    int numRects;               /* Number rectangles in both bands   */
    int y2;                     /* Bottom of current band            */

    /*
     * Figure out how many rectangles are in the band.
     */
    numRects = curStart - prevStart;
    assert(numRects == pReg->data->numRects - curStart);

    if (!numRects)
        return curStart;

    /*
     * The bands may only be coalesced if the bottom of the previous
     * matches the top scanline of the current.
     */
    pPrevBox = RegionBox(pReg, prevStart);
    pCurBox = RegionBox(pReg, curStart);
    if (pPrevBox->y2 != pCurBox->y1)
        return curStart;

    /*
     * Make sure the bands have boxes in the same places. This
     * assumes that boxes have been added in such a way that they
     * cover the most area possible. I.e. two boxes in a band must
     * have some horizontal space between them.
     */
    y2 = pCurBox->y2;

    do {
        if ((pPrevBox->x1 != pCurBox->x1) || (pPrevBox->x2 != pCurBox->x2)) {
            return curStart;
        }
        pPrevBox++;
        pCurBox++;
        numRects--;
    } while (numRects);

    /*
     * The bands may be merged, so set the bottom y of each box
     * in the previous band to the bottom y of the current band.
     */
    numRects = curStart - prevStart;
    pReg->data->numRects -= numRects;
    do {
        pPrevBox--;
        pPrevBox->y2 = y2;
        numRects--;
    } while (numRects);
    return prevStart;
}

/* Quicky macro to avoid trivial reject procedure calls to RegionCoalesce */

#define Coalesce(newReg, prevBand, curBand)				\
    if (curBand - prevBand == newReg->data->numRects - curBand) {	\
	prevBand = RegionCoalesce(newReg, prevBand, curBand);		\
    } else {								\
	prevBand = curBand;						\
    }

/*-
 *-----------------------------------------------------------------------
 * RegionAppendNonO --
 *	Handle a non-overlapping band for the union and subtract operations.
 *      Just adds the (top/bottom-clipped) rectangles into the region.
 *      Doesn't have to check for subsumption or anything.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	pReg->data->numRects is incremented and the rectangles overwritten
 *	with the rectangles we're passed.
 *
 *-----------------------------------------------------------------------
 */

_X_INLINE static Bool
RegionAppendNonO(RegionPtr pReg, BoxPtr r, BoxPtr rEnd, int y1, int y2)
{
    BoxPtr pNextRect;
    int newRects;

    newRects = rEnd - r;

    assert(y1 < y2);
    assert(newRects != 0);

    /* Make sure we have enough space for all rectangles to be added */
    RECTALLOC(pReg, newRects);
    pNextRect = RegionTop(pReg);
    pReg->data->numRects += newRects;
    do {
        assert(r->x1 < r->x2);
        ADDRECT(pNextRect, r->x1, y1, r->x2, y2);
        r++;
    } while (r != rEnd);

    return TRUE;
}

#define FindBand(r, rBandEnd, rEnd, ry1)		    \
{							    \
    ry1 = r->y1;					    \
    rBandEnd = r+1;					    \
    while ((rBandEnd != rEnd) && (rBandEnd->y1 == ry1)) {   \
	rBandEnd++;					    \
    }							    \
}

#define	AppendRegions(newReg, r, rEnd)					\
{									\
    int newRects;							\
    if ((newRects = rEnd - r)) {					\
	RECTALLOC(newReg, newRects);					\
	memmove((char *)RegionTop(newReg),(char *)r, 			\
              newRects * sizeof(BoxRec));				\
	newReg->data->numRects += newRects;				\
    }									\
}

/*-
 *-----------------------------------------------------------------------
 * RegionOp --
 *	Apply an operation to two regions. Called by RegionUnion, RegionInverse,
 *	RegionSubtract, RegionIntersect....  Both regions MUST have at least one
 *      rectangle, and cannot be the same object.
 *
 * Results:
 *	TRUE if successful.
 *
 * Side Effects:
 *	The new region is overwritten.
 *	pOverlap set to TRUE if overlapFunc ever returns TRUE.
 *
 * Notes:
 *	The idea behind this function is to view the two regions as sets.
 *	Together they cover a rectangle of area that this function divides
 *	into horizontal bands where points are covered only by one region
 *	or by both. For the first case, the nonOverlapFunc is called with
 *	each the band and the band's upper and lower extents. For the
 *	second, the overlapFunc is called to process the entire band. It
 *	is responsible for clipping the rectangles in the band, though
 *	this function provides the boundaries.
 *	At the end of each band, the new region is coalesced, if possible,
 *	to reduce the number of rectangles in the region.
 *
 *-----------------------------------------------------------------------
 */

typedef Bool (*OverlapProcPtr) (RegionPtr pReg,
                                BoxPtr r1,
                                BoxPtr r1End,
                                BoxPtr r2,
                                BoxPtr r2End,
                                short y1, short y2, Bool *pOverlap);

static Bool
RegionOp(RegionPtr newReg,      /* Place to store result         */
         RegionPtr reg1,        /* First region in operation     */
         RegionPtr reg2,        /* 2d region in operation        */
         OverlapProcPtr overlapFunc,    /* Function to call for over-
                                         * lapping bands                 */
         Bool appendNon1,       /* Append non-overlapping bands  */
         /* in region 1 ? */
         Bool appendNon2,       /* Append non-overlapping bands  */
         /* in region 2 ? */
         Bool *pOverlap)
{
    BoxPtr r1;                  /* Pointer into first region     */
    BoxPtr r2;                  /* Pointer into 2d region        */
    BoxPtr r1End;               /* End of 1st region             */
    BoxPtr r2End;               /* End of 2d region              */
    short ybot;                 /* Bottom of intersection        */
    short ytop;                 /* Top of intersection           */
    RegDataPtr oldData;         /* Old data for newReg           */
    int prevBand;               /* Index of start of
                                 * previous band in newReg       */
    int curBand;                /* Index of start of current
                                 * band in newReg                */
    BoxPtr r1BandEnd;           /* End of current band in r1     */
    BoxPtr r2BandEnd;           /* End of current band in r2     */
    short top;                  /* Top of non-overlapping band   */
    short bot;                  /* Bottom of non-overlapping band */
    int r1y1;                   /* Temps for r1->y1 and r2->y1   */
    int r2y1;
    int newSize;
    int numRects;

    /*
     * Break any region computed from a broken region
     */
    if (RegionNar(reg1) || RegionNar(reg2))
        return RegionBreak(newReg);

    /*
     * Initialization:
     *  set r1, r2, r1End and r2End appropriately, save the rectangles
     * of the destination region until the end in case it's one of
     * the two source regions, then mark the "new" region empty, allocating
     * another array of rectangles for it to use.
     */

    r1 = RegionRects(reg1);
    newSize = RegionNumRects(reg1);
    r1End = r1 + newSize;
    numRects = RegionNumRects(reg2);
    r2 = RegionRects(reg2);
    r2End = r2 + numRects;
    assert(r1 != r1End);
    assert(r2 != r2End);

    oldData = NULL;
    if (((newReg == reg1) && (newSize > 1)) ||
        ((newReg == reg2) && (numRects > 1))) {
        oldData = newReg->data;
        newReg->data = &RegionEmptyData;
    }
    /* guess at new size */
    if (numRects > newSize)
        newSize = numRects;
    newSize <<= 1;
    if (!newReg->data)
        newReg->data = &RegionEmptyData;
    else if (newReg->data->size)
        newReg->data->numRects = 0;
    if (newSize > newReg->data->size)
        if (!RegionRectAlloc(newReg, newSize))
            return FALSE;

    /*
     * Initialize ybot.
     * In the upcoming loop, ybot and ytop serve different functions depending
     * on whether the band being handled is an overlapping or non-overlapping
     * band.
     *  In the case of a non-overlapping band (only one of the regions
     * has points in the band), ybot is the bottom of the most recent
     * intersection and thus clips the top of the rectangles in that band.
     * ytop is the top of the next intersection between the two regions and
     * serves to clip the bottom of the rectangles in the current band.
     *  For an overlapping band (where the two regions intersect), ytop clips
     * the top of the rectangles of both regions and ybot clips the bottoms.
     */

    ybot = min(r1->y1, r2->y1);

    /*
     * prevBand serves to mark the start of the previous band so rectangles
     * can be coalesced into larger rectangles. qv. RegionCoalesce, above.
     * In the beginning, there is no previous band, so prevBand == curBand
     * (curBand is set later on, of course, but the first band will always
     * start at index 0). prevBand and curBand must be indices because of
     * the possible expansion, and resultant moving, of the new region's
     * array of rectangles.
     */
    prevBand = 0;

    do {
        /*
         * This algorithm proceeds one source-band (as opposed to a
         * destination band, which is determined by where the two regions
         * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
         * rectangle after the last one in the current band for their
         * respective regions.
         */
        assert(r1 != r1End);
        assert(r2 != r2End);

        FindBand(r1, r1BandEnd, r1End, r1y1);
        FindBand(r2, r2BandEnd, r2End, r2y1);

        /*
         * First handle the band that doesn't intersect, if any.
         *
         * Note that attention is restricted to one band in the
         * non-intersecting region at once, so if a region has n
         * bands between the current position and the next place it overlaps
         * the other, this entire loop will be passed through n times.
         */
        if (r1y1 < r2y1) {
            if (appendNon1) {
                top = max(r1y1, ybot);
                bot = min(r1->y2, r2y1);
                if (top != bot) {
                    curBand = newReg->data->numRects;
                    RegionAppendNonO(newReg, r1, r1BandEnd, top, bot);
                    Coalesce(newReg, prevBand, curBand);
                }
            }
            ytop = r2y1;
        }
        else if (r2y1 < r1y1) {
            if (appendNon2) {
                top = max(r2y1, ybot);
                bot = min(r2->y2, r1y1);
                if (top != bot) {
                    curBand = newReg->data->numRects;
                    RegionAppendNonO(newReg, r2, r2BandEnd, top, bot);
                    Coalesce(newReg, prevBand, curBand);
                }
            }
            ytop = r1y1;
        }
        else {
            ytop = r1y1;
        }

        /*
         * Now see if we've hit an intersecting band. The two bands only
         * intersect if ybot > ytop
         */
        ybot = min(r1->y2, r2->y2);
        if (ybot > ytop) {
            curBand = newReg->data->numRects;
            (*overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot,
                            pOverlap);
            Coalesce(newReg, prevBand, curBand);
        }

        /*
         * If we've finished with a band (y2 == ybot) we skip forward
         * in the region to the next band.
         */
        if (r1->y2 == ybot)
            r1 = r1BandEnd;
        if (r2->y2 == ybot)
            r2 = r2BandEnd;

    } while (r1 != r1End && r2 != r2End);

    /*
     * Deal with whichever region (if any) still has rectangles left.
     *
     * We only need to worry about banding and coalescing for the very first
     * band left.  After that, we can just group all remaining boxes,
     * regardless of how many bands, into one final append to the list.
     */

    if ((r1 != r1End) && appendNon1) {
        /* Do first nonOverlap1Func call, which may be able to coalesce */
        FindBand(r1, r1BandEnd, r1End, r1y1);
        curBand = newReg->data->numRects;
        RegionAppendNonO(newReg, r1, r1BandEnd, max(r1y1, ybot), r1->y2);
        Coalesce(newReg, prevBand, curBand);
        /* Just append the rest of the boxes  */
        AppendRegions(newReg, r1BandEnd, r1End);

    }
    else if ((r2 != r2End) && appendNon2) {
        /* Do first nonOverlap2Func call, which may be able to coalesce */
        FindBand(r2, r2BandEnd, r2End, r2y1);
        curBand = newReg->data->numRects;
        RegionAppendNonO(newReg, r2, r2BandEnd, max(r2y1, ybot), r2->y2);
        Coalesce(newReg, prevBand, curBand);
        /* Append rest of boxes */
        AppendRegions(newReg, r2BandEnd, r2End);
    }

    free(oldData);

    if (!(numRects = newReg->data->numRects)) {
        xfreeData(newReg);
        newReg->data = &RegionEmptyData;
    }
    else if (numRects == 1) {
        newReg->extents = *RegionBoxptr(newReg);
        xfreeData(newReg);
        newReg->data = NULL;
    }
    else {
        DOWNSIZE(newReg, numRects);
    }

    return TRUE;
}

/*-
 *-----------------------------------------------------------------------
 * RegionSetExtents --
 *	Reset the extents of a region to what they should be. Called by
 *	Subtract and Intersect as they can't figure it out along the
 *	way or do so easily, as Union can.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The region's 'extents' structure is overwritten.
 *
 *-----------------------------------------------------------------------
 */
static void
RegionSetExtents(RegionPtr pReg)
{
    BoxPtr pBox, pBoxEnd;

    if (!pReg->data)
        return;
    if (!pReg->data->size) {
        pReg->extents.x2 = pReg->extents.x1;
        pReg->extents.y2 = pReg->extents.y1;
        return;
    }

    pBox = RegionBoxptr(pReg);
    pBoxEnd = RegionEnd(pReg);

    /*
     * Since pBox is the first rectangle in the region, it must have the
     * smallest y1 and since pBoxEnd is the last rectangle in the region,
     * it must have the largest y2, because of banding. Initialize x1 and
     * x2 from  pBox and pBoxEnd, resp., as good things to initialize them
     * to...
     */
    pReg->extents.x1 = pBox->x1;
    pReg->extents.y1 = pBox->y1;
    pReg->extents.x2 = pBoxEnd->x2;
    pReg->extents.y2 = pBoxEnd->y2;

    assert(pReg->extents.y1 < pReg->extents.y2);
    while (pBox <= pBoxEnd) {
        if (pBox->x1 < pReg->extents.x1)
            pReg->extents.x1 = pBox->x1;
        if (pBox->x2 > pReg->extents.x2)
            pReg->extents.x2 = pBox->x2;
        pBox++;
    };

    assert(pReg->extents.x1 < pReg->extents.x2);
}

/*======================================================================
 *	    Region Intersection
 *====================================================================*/
/*-
 *-----------------------------------------------------------------------
 * RegionIntersectO --
 *	Handle an overlapping band for RegionIntersect.
 *
 * Results:
 *	TRUE if successful.
 *
 * Side Effects:
 *	Rectangles may be added to the region.
 *
 *-----------------------------------------------------------------------
 */
 /*ARGSUSED*/
#define MERGERECT(r)						\
{								\
    if (r->x1 <= x2) {						\
	/* Merge with current rectangle */			\
	if (r->x1 < x2) *pOverlap = TRUE;				\
	if (x2 < r->x2) x2 = r->x2;				\
    } else {							\
	/* Add current rectangle, start new one */		\
	NEWRECT(pReg, pNextRect, x1, y1, x2, y2);		\
	x1 = r->x1;						\
	x2 = r->x2;						\
    }								\
    r++;							\
}
/*======================================================================
 *	    Region Union
 *====================================================================*/
/*-
 *-----------------------------------------------------------------------
 * RegionUnionO --
 *	Handle an overlapping band for the union operation. Picks the
 *	left-most rectangle each time and merges it into the region.
 *
 * Results:
 *	TRUE if successful.
 *
 * Side Effects:
 *	pReg is overwritten.
 *	pOverlap is set to TRUE if any boxes overlap.
 *
 *-----------------------------------------------------------------------
 */
    static Bool
RegionUnionO(RegionPtr pReg,
             BoxPtr r1,
             BoxPtr r1End,
             BoxPtr r2, BoxPtr r2End, short y1, short y2, Bool *pOverlap)
{
    BoxPtr pNextRect;
    int x1;                     /* left and right side of current union */
    int x2;

    assert(y1 < y2);
    assert(r1 != r1End && r2 != r2End);

    pNextRect = RegionTop(pReg);

    /* Start off current rectangle */
    if (r1->x1 < r2->x1) {
        x1 = r1->x1;
        x2 = r1->x2;
        r1++;
    }
    else {
        x1 = r2->x1;
        x2 = r2->x2;
        r2++;
    }
    while (r1 != r1End && r2 != r2End) {
        if (r1->x1 < r2->x1)
            MERGERECT(r1)
            else
            MERGERECT(r2);
    }

    /* Finish off whoever (if any) is left */
    if (r1 != r1End) {
        do {
            MERGERECT(r1);
        } while (r1 != r1End);
    }
    else if (r2 != r2End) {
        do {
            MERGERECT(r2);
        } while (r2 != r2End);
    }

    /* Add current rectangle */
    NEWRECT(pReg, pNextRect, x1, y1, x2, y2);

    return TRUE;
}

/*======================================================================
 *	    Batch Rectangle Union
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * RegionAppend --
 *
 *      "Append" the rgn rectangles onto the end of dstrgn, maintaining
 *      knowledge of YX-banding when it's easy.  Otherwise, dstrgn just
 *      becomes a non-y-x-banded random collection of rectangles, and not
 *      yet a true region.  After a sequence of appends, the caller must
 *      call RegionValidate to ensure that a valid region is constructed.
 *
 * Results:
 *	TRUE if successful.
 *
 * Side Effects:
 *      dstrgn is modified if rgn has rectangles.
 *
 */
Bool
RegionAppend(RegionPtr dstrgn, RegionPtr rgn)
{
    int numRects, dnumRects, size;
    BoxPtr new, old;
    Bool prepend;

    if (RegionNar(rgn))
        return RegionBreak(dstrgn);

    if (!rgn->data && (dstrgn->data == &RegionEmptyData)) {
        dstrgn->extents = rgn->extents;
        dstrgn->data = NULL;
        return TRUE;
    }

    numRects = RegionNumRects(rgn);
    if (!numRects)
        return TRUE;
    prepend = FALSE;
    size = numRects;
    dnumRects = RegionNumRects(dstrgn);
    if (!dnumRects && (size < 200))
        size = 200;             /* XXX pick numbers out of a hat */
    RECTALLOC(dstrgn, size);
    old = RegionRects(rgn);
    if (!dnumRects)
        dstrgn->extents = rgn->extents;
    else if (dstrgn->extents.x2 > dstrgn->extents.x1) {
        BoxPtr first, last;

        first = old;
        last = RegionBoxptr(dstrgn) + (dnumRects - 1);
        if ((first->y1 > last->y2) ||
            ((first->y1 == last->y1) && (first->y2 == last->y2) &&
             (first->x1 > last->x2))) {
            if (rgn->extents.x1 < dstrgn->extents.x1)
                dstrgn->extents.x1 = rgn->extents.x1;
            if (rgn->extents.x2 > dstrgn->extents.x2)
                dstrgn->extents.x2 = rgn->extents.x2;
            dstrgn->extents.y2 = rgn->extents.y2;
        }
        else {
            first = RegionBoxptr(dstrgn);
            last = old + (numRects - 1);
            if ((first->y1 > last->y2) ||
                ((first->y1 == last->y1) && (first->y2 == last->y2) &&
                 (first->x1 > last->x2))) {
                prepend = TRUE;
                if (rgn->extents.x1 < dstrgn->extents.x1)
                    dstrgn->extents.x1 = rgn->extents.x1;
                if (rgn->extents.x2 > dstrgn->extents.x2)
                    dstrgn->extents.x2 = rgn->extents.x2;
                dstrgn->extents.y1 = rgn->extents.y1;
            }
            else
                dstrgn->extents.x2 = dstrgn->extents.x1;
        }
    }
    if (prepend) {
        new = RegionBox(dstrgn, numRects);
        if (dnumRects == 1)
            *new = *RegionBoxptr(dstrgn);
        else
            memmove((char *) new, (char *) RegionBoxptr(dstrgn),
                    dnumRects * sizeof(BoxRec));
        new = RegionBoxptr(dstrgn);
    }
    else
        new = RegionBoxptr(dstrgn) + dnumRects;
    if (numRects == 1)
        *new = *old;
    else
        memmove((char *) new, (char *) old, numRects * sizeof(BoxRec));
    dstrgn->data->numRects += numRects;
    return TRUE;
}

#define ExchangeRects(a, b) \
{			    \
    BoxRec     t;	    \
    t = rects[a];	    \
    rects[a] = rects[b];    \
    rects[b] = t;	    \
}

static void
QuickSortRects(BoxRec rects[], int numRects)
{
    int y1;
    int x1;
    int i, j;
    BoxPtr r;

    /* Always called with numRects > 1 */

    do {
        if (numRects == 2) {
            if (rects[0].y1 > rects[1].y1 ||
                (rects[0].y1 == rects[1].y1 && rects[0].x1 > rects[1].x1))
                ExchangeRects(0, 1);
            return;
        }

        /* Choose partition element, stick in location 0 */
        ExchangeRects(0, numRects >> 1);
        y1 = rects[0].y1;
        x1 = rects[0].x1;

        /* Partition array */
        i = 0;
        j = numRects;
        do {
            r = &(rects[i]);
            do {
                r++;
                i++;
            } while (i != numRects &&
                     (r->y1 < y1 || (r->y1 == y1 && r->x1 < x1)));
            r = &(rects[j]);
            do {
                r--;
                j--;
            } while (y1 < r->y1 || (y1 == r->y1 && x1 < r->x1));
            if (i < j)
                ExchangeRects(i, j);
        } while (i < j);

        /* Move partition element back to middle */
        ExchangeRects(0, j);

        /* Recurse */
        if (numRects - j - 1 > 1)
            QuickSortRects(&rects[j + 1], numRects - j - 1);
        numRects = j;
    } while (numRects > 1);
}

/*-
 *-----------------------------------------------------------------------
 * RegionValidate --
 *
 *      Take a ``region'' which is a non-y-x-banded random collection of
 *      rectangles, and compute a nice region which is the union of all the
 *      rectangles.
 *
 * Results:
 *	TRUE if successful.
 *
 * Side Effects:
 *      The passed-in ``region'' may be modified.
 *	pOverlap set to TRUE if any retangles overlapped, else FALSE;
 *
 * Strategy:
 *      Step 1. Sort the rectangles into ascending order with primary key y1
 *		and secondary key x1.
 *
 *      Step 2. Split the rectangles into the minimum number of proper y-x
 *		banded regions.  This may require horizontally merging
 *		rectangles, and vertically coalescing bands.  With any luck,
 *		this step in an identity tranformation (ala the Box widget),
 *		or a coalescing into 1 box (ala Menus).
 *
 *	Step 3. Merge the separate regions down to a single region by calling
 *		Union.  Maximize the work each Union call does by using
 *		a binary merge.
 *
 *-----------------------------------------------------------------------
 */

Bool
RegionValidate(RegionPtr badreg, Bool *pOverlap)
{
    /* Descriptor for regions under construction  in Step 2. */
    typedef struct {
        RegionRec reg;
        int prevBand;
        int curBand;
    } RegionInfo;

    int numRects;               /* Original numRects for badreg         */
    RegionInfo *ri;             /* Array of current regions             */
    int numRI;                  /* Number of entries used in ri         */
    int sizeRI;                 /* Number of entries available in ri    */
    int i;                      /* Index into rects                     */
    int j;                      /* Index into ri                        */
    RegionInfo *rit;            /* &ri[j]                                */
    RegionPtr reg;              /* ri[j].reg                     */
    BoxPtr box;                 /* Current box in rects                 */
    BoxPtr riBox;               /* Last box in ri[j].reg                */
    RegionPtr hreg;             /* ri[j_half].reg                        */
    Bool ret = TRUE;

    *pOverlap = FALSE;
    if (!badreg->data) {
        good(badreg);
        return TRUE;
    }
    numRects = badreg->data->numRects;
    if (!numRects) {
        if (RegionNar(badreg))
            return FALSE;
        good(badreg);
        return TRUE;
    }
    if (badreg->extents.x1 < badreg->extents.x2) {
        if ((numRects) == 1) {
            xfreeData(badreg);
            badreg->data = (RegDataPtr) NULL;
        }
        else {
            DOWNSIZE(badreg, numRects);
        }
        good(badreg);
        return TRUE;
    }

    /* Step 1: Sort the rects array into ascending (y1, x1) order */
    QuickSortRects(RegionBoxptr(badreg), numRects);

    /* Step 2: Scatter the sorted array into the minimum number of regions */

    /* Set up the first region to be the first rectangle in badreg */
    /* Note that step 2 code will never overflow the ri[0].reg rects array */
    ri = (RegionInfo *) malloc(4 * sizeof(RegionInfo));
    if (!ri)
        return RegionBreak(badreg);
    sizeRI = 4;
    numRI = 1;
    ri[0].prevBand = 0;
    ri[0].curBand = 0;
    ri[0].reg = *badreg;
    box = RegionBoxptr(&ri[0].reg);
    ri[0].reg.extents = *box;
    ri[0].reg.data->numRects = 1;

    /* Now scatter rectangles into the minimum set of valid regions.  If the
       next rectangle to be added to a region would force an existing rectangle
       in the region to be split up in order to maintain y-x banding, just
       forget it.  Try the next region.  If it doesn't fit cleanly into any
       region, make a new one. */

    for (i = numRects; --i > 0;) {
        box++;
        /* Look for a region to append box to */
        for (j = numRI, rit = ri; --j >= 0; rit++) {
            reg = &rit->reg;
            riBox = RegionEnd(reg);

            if (box->y1 == riBox->y1 && box->y2 == riBox->y2) {
                /* box is in same band as riBox.  Merge or append it */
                if (box->x1 <= riBox->x2) {
                    /* Merge it with riBox */
                    if (box->x1 < riBox->x2)
                        *pOverlap = TRUE;
                    if (box->x2 > riBox->x2)
                        riBox->x2 = box->x2;
                }
                else {
                    RECTALLOC_BAIL(reg, 1, bail);
                    *RegionTop(reg) = *box;
                    reg->data->numRects++;
                }
                goto NextRect;  /* So sue me */
            }
            else if (box->y1 >= riBox->y2) {
                /* Put box into new band */
                if (reg->extents.x2 < riBox->x2)
                    reg->extents.x2 = riBox->x2;
                if (reg->extents.x1 > box->x1)
                    reg->extents.x1 = box->x1;
                Coalesce(reg, rit->prevBand, rit->curBand);
                rit->curBand = reg->data->numRects;
                RECTALLOC_BAIL(reg, 1, bail);
                *RegionTop(reg) = *box;
                reg->data->numRects++;
                goto NextRect;
            }
            /* Well, this region was inappropriate.  Try the next one. */
        }                       /* for j */

        /* Uh-oh.  No regions were appropriate.  Create a new one. */
        if (sizeRI == numRI) {
            /* Oops, allocate space for new region information */
            sizeRI <<= 1;
            rit = (RegionInfo *) reallocarray(ri, sizeRI, sizeof(RegionInfo));
            if (!rit)
                goto bail;
            ri = rit;
            rit = &ri[numRI];
        }
        numRI++;
        rit->prevBand = 0;
        rit->curBand = 0;
        rit->reg.extents = *box;
        rit->reg.data = NULL;
        if (!RegionRectAlloc(&rit->reg, (i + numRI) / numRI))   /* MUST force allocation */
            goto bail;
 NextRect:;
    }                           /* for i */

    /* Make a final pass over each region in order to Coalesce and set
       extents.x2 and extents.y2 */

    for (j = numRI, rit = ri; --j >= 0; rit++) {
        reg = &rit->reg;
        riBox = RegionEnd(reg);
        reg->extents.y2 = riBox->y2;
        if (reg->extents.x2 < riBox->x2)
            reg->extents.x2 = riBox->x2;
        Coalesce(reg, rit->prevBand, rit->curBand);
        if (reg->data->numRects == 1) { /* keep unions happy below */
            xfreeData(reg);
            reg->data = NULL;
        }
    }

    /* Step 3: Union all regions into a single region */
    while (numRI > 1) {
        int half = numRI / 2;

        for (j = numRI & 1; j < (half + (numRI & 1)); j++) {
            reg = &ri[j].reg;
            hreg = &ri[j + half].reg;
            if (!RegionOp(reg, reg, hreg, RegionUnionO, TRUE, TRUE, pOverlap))
                ret = FALSE;
            if (hreg->extents.x1 < reg->extents.x1)
                reg->extents.x1 = hreg->extents.x1;
            if (hreg->extents.y1 < reg->extents.y1)
                reg->extents.y1 = hreg->extents.y1;
            if (hreg->extents.x2 > reg->extents.x2)
                reg->extents.x2 = hreg->extents.x2;
            if (hreg->extents.y2 > reg->extents.y2)
                reg->extents.y2 = hreg->extents.y2;
            xfreeData(hreg);
        }
        numRI -= half;
    }
    *badreg = ri[0].reg;
    free(ri);
    good(badreg);
    return ret;
 bail:
    for (i = 0; i < numRI; i++)
        xfreeData(&ri[i].reg);
    free(ri);
    return RegionBreak(badreg);
}

RegionPtr
RegionFromRects(int nrects, xRectangle *prect, int ctype)
{

    RegionPtr pRgn;
    size_t rgnSize;
    RegDataPtr pData;
    BoxPtr pBox;
    int i;
    int x1, y1, x2, y2;

    pRgn = RegionCreate(NullBox, 0);
    if (RegionNar(pRgn))
        return pRgn;
    if (!nrects)
        return pRgn;
    if (nrects == 1) {
        x1 = prect->x;
        y1 = prect->y;
        if ((x2 = x1 + (int) prect->width) > MAXSHORT)
            x2 = MAXSHORT;
        if ((y2 = y1 + (int) prect->height) > MAXSHORT)
            y2 = MAXSHORT;
        if (x1 != x2 && y1 != y2) {
            pRgn->extents.x1 = x1;
            pRgn->extents.y1 = y1;
            pRgn->extents.x2 = x2;
            pRgn->extents.y2 = y2;
            pRgn->data = NULL;
        }
        return pRgn;
    }
    rgnSize = RegionSizeof(nrects);
    pData = (rgnSize > 0) ? malloc(rgnSize) : NULL;
    if (!pData) {
        RegionBreak(pRgn);
        return pRgn;
    }
    pBox = (BoxPtr) (pData + 1);
    for (i = nrects; --i >= 0; prect++) {
        x1 = prect->x;
        y1 = prect->y;
        if ((x2 = x1 + (int) prect->width) > MAXSHORT)
            x2 = MAXSHORT;
        if ((y2 = y1 + (int) prect->height) > MAXSHORT)
            y2 = MAXSHORT;
        if (x1 != x2 && y1 != y2) {
            pBox->x1 = x1;
            pBox->y1 = y1;
            pBox->x2 = x2;
            pBox->y2 = y2;
            pBox++;
        }
    }
    if (pBox != (BoxPtr) (pData + 1)) {
        pData->size = nrects;
        pData->numRects = pBox - (BoxPtr) (pData + 1);
        pRgn->data = pData;
        if (ctype != CT_YXBANDED) {
            Bool overlap;       /* result ignored */

            pRgn->extents.x1 = pRgn->extents.x2 = 0;
            RegionValidate(pRgn, &overlap);
        }
        else
            RegionSetExtents(pRgn);
        good(pRgn);
    }
    else {
        free(pData);
    }
    return pRgn;
}