PageRenderTime 59ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/utils/vnc/reflector/region.c

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