PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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 preceeds minuend: nuke left edge of minuend.
  1621. */
  1622. x1 = r2->x2;
  1623. if (x1 >= r1->x2)
  1624. {
  1625. /*
  1626. * Minuend completely covered: advance to next minuend and
  1627. * reset left fence to edge of new minuend.
  1628. */
  1629. r1++;
  1630. if (r1 != r1End)
  1631. x1 = r1->x1;
  1632. }
  1633. else
  1634. {
  1635. /*
  1636. * Subtrahend now used up since it doesn't extend beyond
  1637. * minuend
  1638. */
  1639. r2++;
  1640. }
  1641. }
  1642. else if (r2->x1 < r1->x2)
  1643. {
  1644. /*
  1645. * Left part of subtrahend covers part of minuend: add uncovered
  1646. * part of minuend to region and skip to next subtrahend.
  1647. */
  1648. assert(x1<r2->x1);
  1649. NEWRECT(pReg, pNextRect, x1, y1, r2->x1, y2);
  1650. x1 = r2->x2;
  1651. if (x1 >= r1->x2)
  1652. {
  1653. /*
  1654. * Minuend used up: advance to new...
  1655. */
  1656. r1++;
  1657. if (r1 != r1End)
  1658. x1 = r1->x1;
  1659. }
  1660. else
  1661. {
  1662. /*
  1663. * Subtrahend used up
  1664. */
  1665. r2++;
  1666. }
  1667. }
  1668. else
  1669. {
  1670. /*
  1671. * Minuend used up: add any remaining piece before advancing.
  1672. */
  1673. if (r1->x2 > x1)
  1674. NEWRECT(pReg, pNextRect, x1, y1, r1->x2, y2);
  1675. r1++;
  1676. if (r1 != r1End)
  1677. x1 = r1->x1;
  1678. }
  1679. } while ((r1 != r1End) && (r2 != r2End));
  1680. /*
  1681. * Add remaining minuend rectangles to region.
  1682. */
  1683. while (r1 != r1End)
  1684. {
  1685. assert(x1<r1->x2);
  1686. NEWRECT(pReg, pNextRect, x1, y1, r1->x2, y2);
  1687. r1++;
  1688. if (r1 != r1End)
  1689. x1 = r1->x1;
  1690. }
  1691. return TRUE;
  1692. }
  1693. /*-
  1694. *-----------------------------------------------------------------------
  1695. * miSubtract --
  1696. * Subtract regS from regM and leave the result in regD.
  1697. * S stands for subtrahend, M for minuend and D for difference.
  1698. *
  1699. * Results:
  1700. * TRUE if successful.
  1701. *
  1702. * Side Effects:
  1703. * regD is overwritten.
  1704. *
  1705. *-----------------------------------------------------------------------
  1706. */
  1707. Bool
  1708. miSubtract(regD, regM, regS)
  1709. register RegionPtr regD;
  1710. register RegionPtr regM;
  1711. register RegionPtr regS;
  1712. {
  1713. Bool overlap; /* result ignored */
  1714. good(regM);
  1715. good(regS);
  1716. good(regD);
  1717. /* check for trivial rejects */
  1718. if (REGION_NIL(regM) || REGION_NIL(regS) ||
  1719. !EXTENTCHECK(&regM->extents, &regS->extents))
  1720. {
  1721. if (REGION_NAR (regS))
  1722. return miRegionBreak (regD);
  1723. return miRegionCopy(regD, regM);
  1724. }
  1725. else if (regM == regS)
  1726. {
  1727. xfreeData(regD);
  1728. regD->extents.x2 = regD->extents.x1;
  1729. regD->extents.y2 = regD->extents.y1;
  1730. regD->data = &miEmptyData;
  1731. return TRUE;
  1732. }
  1733. /* Add those rectangles in region 1 that aren't in region 2,
  1734. do yucky substraction for overlaps, and
  1735. just throw away rectangles in region 2 that aren't in region 1 */
  1736. if (!miRegionOp(regD, regM, regS, miSubtractO, TRUE, FALSE, &overlap))
  1737. return FALSE;
  1738. /*
  1739. * Can't alter RegD's extents before we call miRegionOp because
  1740. * it might be one of the source regions and miRegionOp depends
  1741. * on the extents of those regions being unaltered. Besides, this
  1742. * way there's no checking against rectangles that will be nuked
  1743. * due to coalescing, so we have to examine fewer rectangles.
  1744. */
  1745. miSetExtents(regD);
  1746. good(regD);
  1747. return TRUE;
  1748. }
  1749. /*======================================================================
  1750. * Region Inversion
  1751. *====================================================================*/
  1752. /*-
  1753. *-----------------------------------------------------------------------
  1754. * miInverse --
  1755. * Take a region and a box and return a region that is everything
  1756. * in the box but not in the region. The careful reader will note
  1757. * that this is the same as subtracting the region from the box...
  1758. *
  1759. * Results:
  1760. * TRUE.
  1761. *
  1762. * Side Effects:
  1763. * newReg is overwritten.
  1764. *
  1765. *-----------------------------------------------------------------------
  1766. */
  1767. Bool
  1768. miInverse(newReg, reg1, invRect)
  1769. RegionPtr newReg; /* Destination region */
  1770. RegionPtr reg1; /* Region to invert */
  1771. BoxPtr invRect; /* Bounding box for inversion */
  1772. {
  1773. RegionRec invReg; /* Quick and dirty region made from the
  1774. * bounding box */
  1775. Bool overlap; /* result ignored */
  1776. good(reg1);
  1777. good(newReg);
  1778. /* check for trivial rejects */
  1779. if (REGION_NIL(reg1) || !EXTENTCHECK(invRect, &reg1->extents))
  1780. {
  1781. if (REGION_NAR(reg1))
  1782. return miRegionBreak (newReg);
  1783. newReg->extents = *invRect;
  1784. xfreeData(newReg);
  1785. newReg->data = (RegDataPtr)NULL;
  1786. return TRUE;
  1787. }
  1788. /* Add those rectangles in region 1 that aren't in region 2,
  1789. do yucky substraction for overlaps, and
  1790. just throw away rectangles in region 2 that aren't in region 1 */
  1791. invReg.extents = *invRect;
  1792. invReg.data = (RegDataPtr)NULL;
  1793. if (!miRegionOp(newReg, &invReg, reg1, miSubtractO, TRUE, FALSE, &overlap))
  1794. return FALSE;
  1795. /*
  1796. * Can't alter newReg's extents before we call miRegionOp because
  1797. * it might be one of the source regions and miRegionOp depends
  1798. * on the extents of those regions being unaltered. Besides, this
  1799. * way there's no checking against rectangles that will be nuked
  1800. * due to coalescing, so we have to examine fewer rectangles.
  1801. */
  1802. miSetExtents(newReg);
  1803. good(newReg);
  1804. return TRUE;
  1805. }
  1806. /*
  1807. * RectIn(region, rect)
  1808. * This routine takes a pointer to a region and a pointer to a box
  1809. * and determines if the box is outside/inside/partly inside the region.
  1810. *
  1811. * The idea is to travel through the list of rectangles trying to cover the
  1812. * passed box with them. Anytime a piece of the rectangle isn't covered
  1813. * by a band of rectangles, partOut is set TRUE. Any time a rectangle in
  1814. * the region covers part of the box, partIn is set TRUE. The process ends
  1815. * when either the box has been completely covered (we reached a band that
  1816. * doesn't overlap the box, partIn is TRUE and partOut is false), the
  1817. * box has been partially covered (partIn == partOut == TRUE -- because of
  1818. * the banding, the first time this is true we know the box is only
  1819. * partially in the region) or is outside the region (we reached a band
  1820. * that doesn't overlap the box at all and partIn is false)
  1821. */
  1822. int
  1823. miRectIn(region, prect)
  1824. register RegionPtr region;
  1825. register BoxPtr prect;
  1826. {
  1827. register int x;
  1828. register int y;
  1829. register BoxPtr pbox;
  1830. register BoxPtr pboxEnd;
  1831. int partIn, partOut;
  1832. int numRects;
  1833. good(region);
  1834. numRects = REGION_NUM_RECTS(region);
  1835. /* useful optimization */
  1836. if (!numRects || !EXTENTCHECK(&region->extents, prect))
  1837. return(rgnOUT);
  1838. if (numRects == 1)
  1839. {
  1840. /* We know that it must be rgnIN or rgnPART */
  1841. if (SUBSUMES(&region->extents, prect))
  1842. return(rgnIN);
  1843. else
  1844. return(rgnPART);
  1845. }
  1846. partOut = FALSE;
  1847. partIn = FALSE;
  1848. /* (x,y) starts at upper left of rect, moving to the right and down */
  1849. x = prect->x1;
  1850. y = prect->y1;
  1851. /* can stop when both partOut and partIn are TRUE, or we reach prect->y2 */
  1852. for (pbox = REGION_BOXPTR(region), pboxEnd = pbox + numRects;
  1853. pbox != pboxEnd;
  1854. pbox++)
  1855. {
  1856. if (pbox->y2 <= y)
  1857. continue; /* getting up to speed or skipping remainder of band */
  1858. if (pbox->y1 > y)
  1859. {
  1860. partOut = TRUE; /* missed part of rectangle above */
  1861. if (partIn || (pbox->y1 >= prect->y2))
  1862. break;
  1863. y = pbox->y1; /* x guaranteed to be == prect->x1 */
  1864. }
  1865. if (pbox->x2 <= x)
  1866. continue; /* not far enough over yet */
  1867. if (pbox->x1 > x)
  1868. {
  1869. partOut = TRUE; /* missed part of rectangle to left */
  1870. if (partIn)
  1871. break;
  1872. }
  1873. if (pbox->x1 < prect->x2)
  1874. {
  1875. partIn = TRUE; /* definitely overlap */
  1876. if (partOut)
  1877. break;
  1878. }
  1879. if (pbox->x2 >= prect->x2)
  1880. {
  1881. y = pbox->y2; /* finished with this band */
  1882. if (y >= prect->y2)
  1883. break;
  1884. x = prect->x1; /* reset x out to left again */
  1885. }
  1886. else
  1887. {
  1888. /*
  1889. * Because boxes in a band are maximal width, if the first box
  1890. * to overlap the rectangle doesn't completely cover it in that
  1891. * band, the rectangle must be partially out, since some of it
  1892. * will be uncovered in that band. partIn will have been set true
  1893. * by now...
  1894. */
  1895. partOut = TRUE;
  1896. break;
  1897. }
  1898. }
  1899. return(partIn ? ((y < prect->y2) ? rgnPART : rgnIN) : rgnOUT);
  1900. }
  1901. /* TranslateRegion(pReg, x, y)
  1902. translates in place
  1903. */
  1904. void
  1905. miTranslateRegion(pReg, x, y)
  1906. register RegionPtr pReg;
  1907. register int x;
  1908. register int y;
  1909. {
  1910. int x1, x2, y1, y2;
  1911. register int nbox;
  1912. register BoxPtr pbox;
  1913. good(pReg);
  1914. pReg->extents.x1 = x1 = pReg->extents.x1 + x;
  1915. pReg->extents.y1 = y1 = pReg->extents.y1 + y;
  1916. pReg->extents.x2 = x2 = pReg->extents.x2 + x;
  1917. pReg->extents.y2 = y2 = pReg->extents.y2 + y;
  1918. if (((x1 - MINSHORT)|(y1 - MINSHORT)|(MAXSHORT - x2)|(MAXSHORT - y2)) >= 0)
  1919. {
  1920. if (pReg->data && (nbox = pReg->data->numRects))
  1921. {
  1922. for (pbox = REGION_BOXPTR(pReg); nbox--; pbox++)
  1923. {
  1924. pbox->x1 += x;
  1925. pbox->y1 += y;
  1926. pbox->x2 += x;
  1927. pbox->y2 += y;
  1928. }
  1929. }
  1930. return;
  1931. }
  1932. if (((x2 - MINSHORT)|(y2 - MINSHORT)|(MAXSHORT - x1)|(MAXSHORT - y1)) <= 0)
  1933. {
  1934. pReg->extents.x2 = pReg->extents.x1;
  1935. pReg->extents.y2 = pReg->extents.y1;
  1936. xfreeData(pReg);
  1937. pReg->data = &miEmptyData;
  1938. return;
  1939. }
  1940. if (x1 < MINSHORT)
  1941. pReg->extents.x1 = MINSHORT;
  1942. else if (x2 > MAXSHORT)
  1943. pReg->extents.x2 = MAXSHORT;
  1944. if (y1 < MINSHORT)
  1945. pReg->extents.y1 = MINSHORT;
  1946. else if (y2 > MAXSHORT)
  1947. pReg->extents.y2 = MAXSHORT;
  1948. if (pReg->data && (nbox = pReg->data->numRects))
  1949. {
  1950. register BoxPtr pboxout;
  1951. for (pboxout = pbox = REGION_BOXPTR(pReg); nbox--; pbox++)
  1952. {
  1953. pboxout->x1 = x1 = pbox->x1 + x;
  1954. pboxout->y1 = y1 = pbox->y1 + y;
  1955. pboxout->x2 = x2 = pbox->x2 + x;
  1956. pboxout->y2 = y2 = pbox->y2 + y;
  1957. if (((x2 - MINSHORT)|(y2 - MINSHORT)|
  1958. (MAXSHORT - x1)|(MAXSHORT - y1)) <= 0)
  1959. {
  1960. pReg->data->numRects--;
  1961. continue;
  1962. }
  1963. if (x1 < MINSHORT)
  1964. pboxout->x1 = MINSHORT;
  1965. else if (x2 > MAXSHORT)
  1966. pboxout->x2 = MAXSHORT;
  1967. if (y1 < MINSHORT)
  1968. pboxout->y1 = MINSHORT;
  1969. else if (y2 > MAXSHORT)
  1970. pboxout->y2 = MAXSHORT;
  1971. pboxout++;
  1972. }
  1973. if (pboxout != pbox)
  1974. {
  1975. if (pReg->data->numRects == 1)
  1976. {
  1977. pReg->extents = *REGION_BOXPTR(pReg);
  1978. xfreeData(pReg);
  1979. pReg->data = (RegDataPtr)NULL;
  1980. }
  1981. else
  1982. miSetExtents(pReg);
  1983. }
  1984. }
  1985. }
  1986. Bool
  1987. miRegionDataCopy(
  1988. register RegionPtr dst,
  1989. register RegionPtr src)
  1990. {
  1991. good(dst);
  1992. good(src);
  1993. if (dst->data)
  1994. return TRUE;
  1995. if (dst == src)
  1996. return TRUE;
  1997. if (!src->data || !src->data->size)
  1998. {
  1999. xfreeData(dst);
  2000. dst->data = (RegDataPtr)NULL;
  2001. return TRUE;
  2002. }
  2003. if (!dst->data || (dst->data->size < src->data->numRects))
  2004. {
  2005. xfreeData(dst);
  2006. dst->data = xallocData(src->data->numRects);
  2007. if (!dst->data)
  2008. return miRegionBreak (dst);
  2009. }
  2010. dst->data->size = src->data->size;
  2011. dst->data->numRects = src->data->numRects;
  2012. return TRUE;
  2013. }
  2014. void
  2015. miRegionReset(pReg, pBox)
  2016. RegionPtr pReg;
  2017. BoxPtr pBox;
  2018. {
  2019. good(pReg);
  2020. assert(pBox->x1<=pBox->x2);
  2021. assert(pBox->y1<=pBox->y2);
  2022. pReg->extents = *pBox;
  2023. xfreeData(pReg);
  2024. pReg->data = (RegDataPtr)NULL;
  2025. }
  2026. Bool
  2027. miPointInRegion(pReg, x, y, box)
  2028. register RegionPtr pReg;
  2029. register int x, y;
  2030. BoxPtr box; /* "return" value */
  2031. {
  2032. register BoxPtr pbox, pboxEnd;
  2033. int numRects;
  2034. good(pReg);
  2035. numRects = REGION_NUM_RECTS(pReg);
  2036. if (!numRects || !INBOX(&pReg->extents, x, y))
  2037. return(FALSE);
  2038. if (numRects == 1)
  2039. {
  2040. *box = pReg->extents;
  2041. return(TRUE);
  2042. }
  2043. for (pbox = REGION_BOXPTR(pReg), pboxEnd = pbox + numRects;
  2044. pbox != pboxEnd;
  2045. pbox++)
  2046. {
  2047. if (y >= pbox->y2)
  2048. continue; /* not there yet */
  2049. if ((y < pbox->y1) || (x < pbox->x1))
  2050. break; /* missed it */
  2051. if (x >= pbox->x2)
  2052. continue; /* not there yet */
  2053. *box = *pbox;
  2054. return(TRUE);
  2055. }
  2056. return(FALSE);
  2057. }
  2058. Bool
  2059. miRegionNotEmpty(pReg)
  2060. RegionPtr pReg;
  2061. {
  2062. good(pReg);
  2063. return(!REGION_NIL(pReg));
  2064. }
  2065. Bool
  2066. miRegionBroken(RegionPtr pReg)
  2067. {
  2068. good(pReg);
  2069. return (REGION_NAR(pReg));
  2070. }
  2071. void
  2072. miRegionEmpty(pReg)
  2073. RegionPtr pReg;
  2074. {
  2075. good(pReg);
  2076. xfreeData(pReg);
  2077. pReg->extents.x2 = pReg->extents.x1;
  2078. pReg->extents.y2 = pReg->extents.y1;
  2079. pReg->data = &miEmptyData;
  2080. }
  2081. BoxPtr
  2082. miRegionExtents(pReg)
  2083. RegionPtr pReg;
  2084. {
  2085. good(pReg);
  2086. return(&pReg->extents);
  2087. }
  2088. #define ExchangeSpans(a, b) \
  2089. { \
  2090. DDXPointRec tpt; \
  2091. register int tw; \
  2092. \
  2093. tpt = spans[a]; spans[a] = spans[b]; spans[b] = tpt; \
  2094. tw = widths[a]; widths[a] = widths[b]; widths[b] = tw; \
  2095. }
  2096. /* ||| I should apply the merge sort code to rectangle sorting above, and see
  2097. if mapping time can be improved. But right now I've been at work 12 hours,
  2098. so forget it.
  2099. */
  2100. static void QuickSortSpans(
  2101. register DDXPointRec spans[],
  2102. register int widths[],
  2103. register int numSpans)
  2104. {
  2105. register int y;
  2106. register int i, j, m;
  2107. register DDXPointPtr r;
  2108. /* Always called with numSpans > 1 */
  2109. /* Sorts only by y, doesn't bother to sort by x */
  2110. do
  2111. {
  2112. if (numSpans < 9)
  2113. {
  2114. /* Do insertion sort */
  2115. register int yprev;
  2116. yprev = spans[0].y;
  2117. i = 1;
  2118. do
  2119. { /* while i != numSpans */
  2120. y = spans[i].y;
  2121. if (yprev > y)
  2122. {
  2123. /* spans[i] is out of order. Move into proper location. */
  2124. DDXPointRec tpt;
  2125. int tw, k;
  2126. for (j = 0; y >= spans[j].y; j++) {}
  2127. tpt = spans[i];
  2128. tw = widths[i];
  2129. for (k = i; k != j; k--)
  2130. {
  2131. spans[k] = spans[k-1];
  2132. widths[k] = widths[k-1];
  2133. }
  2134. spans[j] = tpt;
  2135. widths[j] = tw;
  2136. y = spans[i].y;
  2137. } /* if out of order */
  2138. yprev = y;
  2139. i++;
  2140. } while (i != numSpans);
  2141. return;
  2142. }
  2143. /* Choose partition element, stick in location 0 */
  2144. m = numSpans / 2;
  2145. if (spans[m].y > spans[0].y) ExchangeSpans(m, 0);
  2146. if (spans[m].y > spans[numSpans-1].y) ExchangeSpans(m, numSpans-1);
  2147. if (spans[m].y > spans[0].y) ExchangeSpans(m, 0);
  2148. y = spans[0].y;
  2149. /* Partition array */
  2150. i = 0;
  2151. j = numSpans;
  2152. do
  2153. {
  2154. r = &(spans[i]);
  2155. do
  2156. {
  2157. r++;
  2158. i++;
  2159. } while (i != numSpans && r->y < y);
  2160. r = &(spans[j]);
  2161. do
  2162. {
  2163. r--;
  2164. j--;
  2165. } while (y < r->y);
  2166. if (i < j)
  2167. ExchangeSpans(i, j);
  2168. } while (i < j);
  2169. /* Move partition element back to middle */
  2170. ExchangeSpans(0, j);
  2171. /* Recurse */
  2172. if (numSpans-j-1 > 1)
  2173. QuickSortSpans(&spans[j+1], &widths[j+1], numSpans-j-1);
  2174. numSpans = j;
  2175. } while (numSpans > 1);
  2176. }
  2177. #define NextBand() \
  2178. { \
  2179. clipy1 = pboxBandStart->y1; \
  2180. clipy2 = pboxBandStart->y2; \
  2181. pboxBandEnd = pboxBandStart + 1; \
  2182. while (pboxBandEnd != pboxLast && pboxBandEnd->y1 == clipy1) { \
  2183. pboxBandEnd++; \
  2184. } \
  2185. for (; ppt != pptLast && ppt->y < clipy1; ppt++, pwidth++) {} \
  2186. }
  2187. /*
  2188. Clip a list of scanlines to a region. The caller has allocated the
  2189. space. FSorted is non-zero if the scanline origins are in ascending
  2190. order.
  2191. returns the number of new, clipped scanlines.
  2192. */
  2193. int
  2194. miClipSpans(
  2195. RegionPtr prgnDst,
  2196. register DDXPointPtr ppt,
  2197. register int *pwidth,
  2198. int nspans,
  2199. register DDXPointPtr pptNew,
  2200. int *pwidthNew,
  2201. int fSorted)
  2202. {
  2203. register DDXPointPtr pptLast;
  2204. int *pwidthNewStart; /* the vengeance of Xerox! */
  2205. register int y, x1, x2;
  2206. register int numRects;
  2207. good(prgnDst);
  2208. pptLast = ppt + nspans;
  2209. pwidthNewStart = pwidthNew;
  2210. if (!prgnDst->data)
  2211. {
  2212. /* Do special fast code with clip boundaries in registers(?) */
  2213. /* It doesn't pay much to make use of fSorted in this case,
  2214. so we lump everything together. */
  2215. register int clipx1, clipx2, clipy1, clipy2;
  2216. clipx1 = prgnDst->extents.x1;
  2217. clipy1 = prgnDst->extents.y1;
  2218. clipx2 = prgnDst->extents.x2;
  2219. clipy2 = prgnDst->extents.y2;
  2220. for (; ppt != pptLast; ppt++, pwidth++)
  2221. {
  2222. y = ppt->y;
  2223. x1 = ppt->x;
  2224. if (clipy1 <= y && y < clipy2)
  2225. {
  2226. x2 = x1 + *pwidth;
  2227. if (x1 < clipx1) x1 = clipx1;
  2228. if (x2 > clipx2) x2 = clipx2;
  2229. if (x1 < x2)
  2230. {
  2231. /* part of span in clip rectangle */
  2232. pptNew->x = x1;
  2233. pptNew->y = y;
  2234. *pwidthNew = x2 - x1;
  2235. pptNew++;
  2236. pwidthNew++;
  2237. }
  2238. }
  2239. } /* end for */
  2240. }
  2241. else if ((numRects = prgnDst->data->numRects))
  2242. {
  2243. /* Have to clip against many boxes */
  2244. BoxPtr pboxBandStart, pboxBandEnd;
  2245. register BoxPtr pbox;
  2246. register BoxPtr pboxLast;
  2247. register int clipy1, clipy2;
  2248. /* In this case, taking advantage of sorted spans gains more than
  2249. the sorting costs. */
  2250. if ((! fSorted) && (nspans > 1))
  2251. QuickSortSpans(ppt, pwidth, nspans);
  2252. pboxBandStart = REGION_BOXPTR(prgnDst);
  2253. pboxLast = pboxBandStart + numRects;
  2254. NextBand();
  2255. for (; ppt != pptLast; )
  2256. {
  2257. y = ppt->y;
  2258. if (y < clipy2)
  2259. {
  2260. /* span is in the current band */
  2261. pbox = pboxBandStart;
  2262. x1 = ppt->x;
  2263. x2 = x1 + *pwidth;
  2264. do
  2265. { /* For each box in band */
  2266. register int newx1, newx2;
  2267. newx1 = x1;
  2268. newx2 = x2;
  2269. if (newx1 < pbox->x1) newx1 = pbox->x1;
  2270. if (newx2 > pbox->x2) newx2 = pbox->x2;
  2271. if (newx1 < newx2)
  2272. {
  2273. /* Part of span in clip rectangle */
  2274. pptNew->x = newx1;
  2275. pptNew->y = y;
  2276. *pwidthNew = newx2 - newx1;
  2277. pptNew++;
  2278. pwidthNew++;
  2279. }
  2280. pbox++;
  2281. } while (pbox != pboxBandEnd);
  2282. ppt++;
  2283. pwidth++;
  2284. }
  2285. else
  2286. {
  2287. /* Move to next band, adjust ppt as needed */
  2288. pboxBandStart = pboxBandEnd;
  2289. if (pboxBandStart == pboxLast)
  2290. break; /* We're completely done */
  2291. NextBand();
  2292. }
  2293. }
  2294. }
  2295. return (pwidthNew - pwidthNewStart);
  2296. }
  2297. /* find the band in a region with the most rectangles */
  2298. int
  2299. miFindMaxBand(prgn)
  2300. RegionPtr prgn;
  2301. {
  2302. register int nbox;
  2303. register BoxPtr pbox;
  2304. register int nThisBand;
  2305. register int nMaxBand = 0;
  2306. short yThisBand;
  2307. good(prgn);
  2308. nbox = REGION_NUM_RECTS(prgn);
  2309. pbox = REGION_RECTS(prgn);
  2310. while(nbox > 0)
  2311. {
  2312. yThisBand = pbox->y1;
  2313. nThisBand = 0;
  2314. while((nbox > 0) && (pbox->y1 == yThisBand))
  2315. {
  2316. nbox--;
  2317. pbox++;
  2318. nThisBand++;
  2319. }
  2320. if (nThisBand > nMaxBand)
  2321. nMaxBand = nThisBand;
  2322. }
  2323. return (nMaxBand);
  2324. }