PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/dix/region.c

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