PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/nx-3.5.0/nx-X11/programs/Xserver/mi/miregion.c

#
C | 2563 lines | 1722 code | 222 blank | 619 comment | 487 complexity | ca3fd0f2c25c1c90d48ab99c5335c910 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file