You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Region.c 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /************************************************************************
  2. Copyright 1987, 1988, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ************************************************************************/
  36. /*
  37. * The functions in this file implement the Region abstraction, similar to one
  38. * used in the X11 sample server. A Region is simply an area, as the name
  39. * implies, and is implemented as a "y-x-banded" array of rectangles. To
  40. * explain: Each Region is made up of a certain number of rectangles sorted
  41. * by y coordinate first, and then by x coordinate.
  42. *
  43. * Furthermore, the rectangles are banded such that every rectangle with a
  44. * given upper-left y coordinate (y1) will have the same lower-right y
  45. * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
  46. * will span the entire vertical distance of the band. This means that some
  47. * areas that could be merged into a taller rectangle will be represented as
  48. * several shorter rectangles to account for shorter rectangles to its left
  49. * or right but within its "vertical scope".
  50. *
  51. * An added constraint on the rectangles is that they must cover as much
  52. * horizontal area as possible. E.g. no two rectangles in a band are allowed
  53. * to touch.
  54. *
  55. * Whenever possible, bands will be merged together to cover a greater vertical
  56. * distance (and thus reduce the number of rectangles). Two bands can be merged
  57. * only if the bottom of one touches the top of the other and they have
  58. * rectangles in the same places (of the same width, of course). This maintains
  59. * the y-x-banding that's so nice to have...
  60. */
  61. #include <string.h>
  62. #include "Xlibint.h"
  63. #include "Xutil.h"
  64. #include "Xregion.h"
  65. #ifndef min
  66. #define min(a,b) (((a) < (b)) ? (a) : (b))
  67. #endif
  68. #ifndef max
  69. #define max(a,b) (((a) > (b)) ? (a) : (b))
  70. #endif
  71. #ifdef DEBUG
  72. #include <stdio.h>
  73. #define assert(expr) {if (!(expr)) fprintf(stderr,\
  74. "Assertion failed file %s, line %d: expr\n", __FILE__, __LINE__); }
  75. #else
  76. #define assert(expr)
  77. #endif
  78. typedef int (*overlapProcp)(
  79. register Region pReg,
  80. register BoxPtr r1,
  81. BoxPtr r1End,
  82. register BoxPtr r2,
  83. BoxPtr r2End,
  84. short y1,
  85. short y2);
  86. typedef int (*nonOverlapProcp)(
  87. register Region pReg,
  88. register BoxPtr r,
  89. BoxPtr rEnd,
  90. register short y1,
  91. register short y2);
  92. static void miRegionOp(
  93. register Region newReg, /* Place to store result */
  94. Region reg1, /* First region in operation */
  95. Region reg2, /* 2d region in operation */
  96. int (*overlapFunc)(
  97. register Region pReg,
  98. register BoxPtr r1,
  99. BoxPtr r1End,
  100. register BoxPtr r2,
  101. BoxPtr r2End,
  102. short y1,
  103. short y2), /* Function to call for over-
  104. * lapping bands */
  105. int (*nonOverlap1Func)(
  106. register Region pReg,
  107. register BoxPtr r,
  108. BoxPtr rEnd,
  109. register short y1,
  110. register short y2), /* Function to call for non-
  111. * overlapping bands in region
  112. * 1 */
  113. int (*nonOverlap2Func)(
  114. register Region pReg,
  115. register BoxPtr r,
  116. BoxPtr rEnd,
  117. register short y1,
  118. register short y2)); /* Function to call for non-
  119. * overlapping bands in region
  120. * 2 */
  121. /* Create a new empty region */
  122. Region
  123. XCreateRegion(void)
  124. {
  125. Region temp;
  126. if (! (temp = Xmalloc(sizeof( REGION ))))
  127. return (Region) NULL;
  128. if (! (temp->rects = Xmalloc(sizeof( BOX )))) {
  129. Xfree(temp);
  130. return (Region) NULL;
  131. }
  132. temp->numRects = 0;
  133. temp->extents.x1 = 0;
  134. temp->extents.y1 = 0;
  135. temp->extents.x2 = 0;
  136. temp->extents.y2 = 0;
  137. temp->size = 1;
  138. return( temp );
  139. }
  140. int
  141. XClipBox(
  142. Region r,
  143. XRectangle *rect)
  144. {
  145. rect->x = r->extents.x1;
  146. rect->y = r->extents.y1;
  147. rect->width = r->extents.x2 - r->extents.x1;
  148. rect->height = r->extents.y2 - r->extents.y1;
  149. return 1;
  150. }
  151. int
  152. XUnionRectWithRegion(
  153. register XRectangle *rect,
  154. Region source, Region dest)
  155. {
  156. REGION region;
  157. if (!rect->width || !rect->height)
  158. return 0;
  159. region.rects = &region.extents;
  160. region.numRects = 1;
  161. region.extents.x1 = rect->x;
  162. region.extents.y1 = rect->y;
  163. region.extents.x2 = rect->x + rect->width;
  164. region.extents.y2 = rect->y + rect->height;
  165. region.size = 1;
  166. return XUnionRegion(&region, source, dest);
  167. }
  168. /*-
  169. *-----------------------------------------------------------------------
  170. * miSetExtents --
  171. * Reset the extents of a region to what they should be. Called by
  172. * miSubtract and miIntersect b/c they can't figure it out along the
  173. * way or do so easily, as miUnion can.
  174. *
  175. * Results:
  176. * None.
  177. *
  178. * Side Effects:
  179. * The region's 'extents' structure is overwritten.
  180. *
  181. *-----------------------------------------------------------------------
  182. */
  183. static void
  184. miSetExtents (
  185. Region pReg)
  186. {
  187. register BoxPtr pBox,
  188. pBoxEnd,
  189. pExtents;
  190. if (pReg->numRects == 0)
  191. {
  192. pReg->extents.x1 = 0;
  193. pReg->extents.y1 = 0;
  194. pReg->extents.x2 = 0;
  195. pReg->extents.y2 = 0;
  196. return;
  197. }
  198. pExtents = &pReg->extents;
  199. pBox = pReg->rects;
  200. pBoxEnd = &pBox[pReg->numRects - 1];
  201. /*
  202. * Since pBox is the first rectangle in the region, it must have the
  203. * smallest y1 and since pBoxEnd is the last rectangle in the region,
  204. * it must have the largest y2, because of banding. Initialize x1 and
  205. * x2 from pBox and pBoxEnd, resp., as good things to initialize them
  206. * to...
  207. */
  208. pExtents->x1 = pBox->x1;
  209. pExtents->y1 = pBox->y1;
  210. pExtents->x2 = pBoxEnd->x2;
  211. pExtents->y2 = pBoxEnd->y2;
  212. assert(pExtents->y1 < pExtents->y2);
  213. while (pBox <= pBoxEnd)
  214. {
  215. if (pBox->x1 < pExtents->x1)
  216. {
  217. pExtents->x1 = pBox->x1;
  218. }
  219. if (pBox->x2 > pExtents->x2)
  220. {
  221. pExtents->x2 = pBox->x2;
  222. }
  223. pBox++;
  224. }
  225. assert(pExtents->x1 < pExtents->x2);
  226. }
  227. #if 0
  228. int
  229. XSetRegion(
  230. Display *dpy,
  231. GC gc,
  232. register Region r)
  233. {
  234. register int i;
  235. register XRectangle *xr, *pr;
  236. register BOX *pb;
  237. unsigned long total;
  238. LockDisplay (dpy);
  239. total = r->numRects * sizeof (XRectangle);
  240. if ((xr = (XRectangle *) _XAllocTemp(dpy, total))) {
  241. for (pr = xr, pb = r->rects, i = r->numRects; --i >= 0; pr++, pb++) {
  242. pr->x = pb->x1;
  243. pr->y = pb->y1;
  244. pr->width = pb->x2 - pb->x1;
  245. pr->height = pb->y2 - pb->y1;
  246. }
  247. }
  248. if (xr || !r->numRects)
  249. _XSetClipRectangles(dpy, gc, 0, 0, xr, r->numRects, YXBanded);
  250. if (xr)
  251. _XFreeTemp(dpy, (char *)xr, total);
  252. UnlockDisplay(dpy);
  253. SyncHandle();
  254. return 1;
  255. }
  256. #endif
  257. int
  258. XDestroyRegion(
  259. Region r)
  260. {
  261. Xfree( (char *) r->rects );
  262. Xfree( (char *) r );
  263. return 1;
  264. }
  265. /* TranslateRegion(pRegion, x, y)
  266. translates in place
  267. added by raymond
  268. */
  269. int
  270. XOffsetRegion(
  271. register Region pRegion,
  272. register int x,
  273. register int y)
  274. {
  275. register int nbox;
  276. register BOX *pbox;
  277. pbox = pRegion->rects;
  278. nbox = pRegion->numRects;
  279. while(nbox--)
  280. {
  281. pbox->x1 += x;
  282. pbox->x2 += x;
  283. pbox->y1 += y;
  284. pbox->y2 += y;
  285. pbox++;
  286. }
  287. pRegion->extents.x1 += x;
  288. pRegion->extents.x2 += x;
  289. pRegion->extents.y1 += y;
  290. pRegion->extents.y2 += y;
  291. return 1;
  292. }
  293. /*
  294. Utility procedure Compress:
  295. Replace r by the region r', where
  296. p in r' iff (Quantifer m <= dx) (p + m in r), and
  297. Quantifier is Exists if grow is TRUE, For all if grow is FALSE, and
  298. (x,y) + m = (x+m,y) if xdir is TRUE; (x,y+m) if xdir is FALSE.
  299. Thus, if xdir is TRUE and grow is FALSE, r is replaced by the region
  300. of all points p such that p and the next dx points on the same
  301. horizontal scan line are all in r. We do this using by noting
  302. that p is the head of a run of length 2^i + k iff p is the head
  303. of a run of length 2^i and p+2^i is the head of a run of length
  304. k. Thus, the loop invariant: s contains the region corresponding
  305. to the runs of length shift. r contains the region corresponding
  306. to the runs of length 1 + dxo & (shift-1), where dxo is the original
  307. value of dx. dx = dxo & ~(shift-1). As parameters, s and t are
  308. scratch regions, so that we don't have to allocate them on every
  309. call.
  310. */
  311. #define ZOpRegion(a,b,c) if (grow) XUnionRegion(a,b,c); \
  312. else XIntersectRegion(a,b,c)
  313. #define ZShiftRegion(a,b) if (xdir) XOffsetRegion(a,b,0); \
  314. else XOffsetRegion(a,0,b)
  315. #define ZCopyRegion(a,b) XUnionRegion(a,a,b)
  316. static void
  317. Compress(
  318. Region r, Region s, Region t,
  319. register unsigned dx,
  320. register int xdir, register int grow)
  321. {
  322. register unsigned shift = 1;
  323. ZCopyRegion(r, s);
  324. while (dx) {
  325. if (dx & shift) {
  326. ZShiftRegion(r, -(int)shift);
  327. ZOpRegion(r, s, r);
  328. dx -= shift;
  329. if (!dx) break;
  330. }
  331. ZCopyRegion(s, t);
  332. ZShiftRegion(s, -(int)shift);
  333. ZOpRegion(s, t, s);
  334. shift <<= 1;
  335. }
  336. }
  337. #undef ZOpRegion
  338. #undef ZShiftRegion
  339. #undef ZCopyRegion
  340. int
  341. XShrinkRegion(
  342. Region r,
  343. int dx, int dy)
  344. {
  345. Region s, t;
  346. int grow;
  347. if (!dx && !dy) return 0;
  348. if (! (s = XCreateRegion()) )
  349. return 0;
  350. if (! (t = XCreateRegion()) ) {
  351. XDestroyRegion(s);
  352. return 0;
  353. }
  354. if ((grow = (dx < 0))) dx = -dx;
  355. if (dx) Compress(r, s, t, (unsigned) 2*dx, TRUE, grow);
  356. if ((grow = (dy < 0))) dy = -dy;
  357. if (dy) Compress(r, s, t, (unsigned) 2*dy, FALSE, grow);
  358. XOffsetRegion(r, dx, dy);
  359. XDestroyRegion(s);
  360. XDestroyRegion(t);
  361. return 0;
  362. }
  363. /*======================================================================
  364. * Region Intersection
  365. *====================================================================*/
  366. /*-
  367. *-----------------------------------------------------------------------
  368. * miIntersectO --
  369. * Handle an overlapping band for miIntersect.
  370. *
  371. * Results:
  372. * None.
  373. *
  374. * Side Effects:
  375. * Rectangles may be added to the region.
  376. *
  377. *-----------------------------------------------------------------------
  378. */
  379. /* static void*/
  380. static int
  381. miIntersectO (
  382. register Region pReg,
  383. register BoxPtr r1,
  384. BoxPtr r1End,
  385. register BoxPtr r2,
  386. BoxPtr r2End,
  387. short y1,
  388. short y2)
  389. {
  390. register short x1;
  391. register short x2;
  392. register BoxPtr pNextRect;
  393. pNextRect = &pReg->rects[pReg->numRects];
  394. while ((r1 != r1End) && (r2 != r2End))
  395. {
  396. x1 = max(r1->x1,r2->x1);
  397. x2 = min(r1->x2,r2->x2);
  398. /*
  399. * If there's any overlap between the two rectangles, add that
  400. * overlap to the new region.
  401. * There's no need to check for subsumption because the only way
  402. * such a need could arise is if some region has two rectangles
  403. * right next to each other. Since that should never happen...
  404. */
  405. if (x1 < x2)
  406. {
  407. assert(y1<y2);
  408. MEMCHECK(pReg, pNextRect, pReg->rects);
  409. pNextRect->x1 = x1;
  410. pNextRect->y1 = y1;
  411. pNextRect->x2 = x2;
  412. pNextRect->y2 = y2;
  413. pReg->numRects += 1;
  414. pNextRect++;
  415. assert(pReg->numRects <= pReg->size);
  416. }
  417. /*
  418. * Need to advance the pointers. Shift the one that extends
  419. * to the right the least, since the other still has a chance to
  420. * overlap with that region's next rectangle, if you see what I mean.
  421. */
  422. if (r1->x2 < r2->x2)
  423. {
  424. r1++;
  425. }
  426. else if (r2->x2 < r1->x2)
  427. {
  428. r2++;
  429. }
  430. else
  431. {
  432. r1++;
  433. r2++;
  434. }
  435. }
  436. return 0; /* lint */
  437. }
  438. int
  439. XIntersectRegion(
  440. Region reg1,
  441. Region reg2, /* source regions */
  442. register Region newReg) /* destination Region */
  443. {
  444. /* check for trivial reject */
  445. if ( (!(reg1->numRects)) || (!(reg2->numRects)) ||
  446. (!EXTENTCHECK(&reg1->extents, &reg2->extents)))
  447. newReg->numRects = 0;
  448. else
  449. miRegionOp (newReg, reg1, reg2,
  450. miIntersectO, NULL, NULL);
  451. /*
  452. * Can't alter newReg's extents before we call miRegionOp because
  453. * it might be one of the source regions and miRegionOp depends
  454. * on the extents of those regions being the same. Besides, this
  455. * way there's no checking against rectangles that will be nuked
  456. * due to coalescing, so we have to examine fewer rectangles.
  457. */
  458. miSetExtents(newReg);
  459. return 1;
  460. }
  461. static int
  462. miRegionCopy(
  463. register Region dstrgn,
  464. register Region rgn)
  465. {
  466. if (dstrgn != rgn) /* don't want to copy to itself */
  467. {
  468. if (dstrgn->size < rgn->numRects)
  469. {
  470. if (dstrgn->rects)
  471. {
  472. BOX *prevRects = dstrgn->rects;
  473. dstrgn->rects = Xrealloc(dstrgn->rects,
  474. rgn->numRects * (sizeof(BOX)));
  475. if (! dstrgn->rects) {
  476. Xfree(prevRects);
  477. dstrgn->size = 0;
  478. return 0;
  479. }
  480. }
  481. dstrgn->size = rgn->numRects;
  482. }
  483. dstrgn->numRects = rgn->numRects;
  484. dstrgn->extents.x1 = rgn->extents.x1;
  485. dstrgn->extents.y1 = rgn->extents.y1;
  486. dstrgn->extents.x2 = rgn->extents.x2;
  487. dstrgn->extents.y2 = rgn->extents.y2;
  488. memcpy((char *) dstrgn->rects, (char *) rgn->rects,
  489. (int) (rgn->numRects * sizeof(BOX)));
  490. }
  491. return 1;
  492. }
  493. /*======================================================================
  494. * Generic Region Operator
  495. *====================================================================*/
  496. /*-
  497. *-----------------------------------------------------------------------
  498. * miCoalesce --
  499. * Attempt to merge the boxes in the current band with those in the
  500. * previous one. Used only by miRegionOp.
  501. *
  502. * Results:
  503. * The new index for the previous band.
  504. *
  505. * Side Effects:
  506. * If coalescing takes place:
  507. * - rectangles in the previous band will have their y2 fields
  508. * altered.
  509. * - pReg->numRects will be decreased.
  510. *
  511. *-----------------------------------------------------------------------
  512. */
  513. /* static int*/
  514. static int
  515. miCoalesce(
  516. register Region pReg, /* Region to coalesce */
  517. int prevStart, /* Index of start of previous band */
  518. int curStart) /* Index of start of current band */
  519. {
  520. register BoxPtr pPrevBox; /* Current box in previous band */
  521. register BoxPtr pCurBox; /* Current box in current band */
  522. register BoxPtr pRegEnd; /* End of region */
  523. int curNumRects; /* Number of rectangles in current
  524. * band */
  525. int prevNumRects; /* Number of rectangles in previous
  526. * band */
  527. int bandY1; /* Y1 coordinate for current band */
  528. pRegEnd = &pReg->rects[pReg->numRects];
  529. pPrevBox = &pReg->rects[prevStart];
  530. prevNumRects = curStart - prevStart;
  531. /*
  532. * Figure out how many rectangles are in the current band. Have to do
  533. * this because multiple bands could have been added in miRegionOp
  534. * at the end when one region has been exhausted.
  535. */
  536. pCurBox = &pReg->rects[curStart];
  537. bandY1 = pCurBox->y1;
  538. for (curNumRects = 0;
  539. (pCurBox != pRegEnd) && (pCurBox->y1 == bandY1);
  540. curNumRects++)
  541. {
  542. pCurBox++;
  543. }
  544. if (pCurBox != pRegEnd)
  545. {
  546. /*
  547. * If more than one band was added, we have to find the start
  548. * of the last band added so the next coalescing job can start
  549. * at the right place... (given when multiple bands are added,
  550. * this may be pointless -- see above).
  551. */
  552. pRegEnd--;
  553. while (pRegEnd[-1].y1 == pRegEnd->y1)
  554. {
  555. pRegEnd--;
  556. }
  557. curStart = pRegEnd - pReg->rects;
  558. pRegEnd = pReg->rects + pReg->numRects;
  559. }
  560. if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
  561. pCurBox -= curNumRects;
  562. /*
  563. * The bands may only be coalesced if the bottom of the previous
  564. * matches the top scanline of the current.
  565. */
  566. if (pPrevBox->y2 == pCurBox->y1)
  567. {
  568. /*
  569. * Make sure the bands have boxes in the same places. This
  570. * assumes that boxes have been added in such a way that they
  571. * cover the most area possible. I.e. two boxes in a band must
  572. * have some horizontal space between them.
  573. */
  574. do
  575. {
  576. if ((pPrevBox->x1 != pCurBox->x1) ||
  577. (pPrevBox->x2 != pCurBox->x2))
  578. {
  579. /*
  580. * The bands don't line up so they can't be coalesced.
  581. */
  582. return (curStart);
  583. }
  584. pPrevBox++;
  585. pCurBox++;
  586. prevNumRects -= 1;
  587. } while (prevNumRects != 0);
  588. pReg->numRects -= curNumRects;
  589. pCurBox -= curNumRects;
  590. pPrevBox -= curNumRects;
  591. /*
  592. * The bands may be merged, so set the bottom y of each box
  593. * in the previous band to that of the corresponding box in
  594. * the current band.
  595. */
  596. do
  597. {
  598. pPrevBox->y2 = pCurBox->y2;
  599. pPrevBox++;
  600. pCurBox++;
  601. curNumRects -= 1;
  602. } while (curNumRects != 0);
  603. /*
  604. * If only one band was added to the region, we have to backup
  605. * curStart to the start of the previous band.
  606. *
  607. * If more than one band was added to the region, copy the
  608. * other bands down. The assumption here is that the other bands
  609. * came from the same region as the current one and no further
  610. * coalescing can be done on them since it's all been done
  611. * already... curStart is already in the right place.
  612. */
  613. if (pCurBox == pRegEnd)
  614. {
  615. curStart = prevStart;
  616. }
  617. else
  618. {
  619. do
  620. {
  621. *pPrevBox++ = *pCurBox++;
  622. } while (pCurBox != pRegEnd);
  623. }
  624. }
  625. }
  626. return (curStart);
  627. }
  628. /*-
  629. *-----------------------------------------------------------------------
  630. * miRegionOp --
  631. * Apply an operation to two regions. Called by miUnion, miInverse,
  632. * miSubtract, miIntersect...
  633. *
  634. * Results:
  635. * None.
  636. *
  637. * Side Effects:
  638. * The new region is overwritten.
  639. *
  640. * Notes:
  641. * The idea behind this function is to view the two regions as sets.
  642. * Together they cover a rectangle of area that this function divides
  643. * into horizontal bands where points are covered only by one region
  644. * or by both. For the first case, the nonOverlapFunc is called with
  645. * each the band and the band's upper and lower extents. For the
  646. * second, the overlapFunc is called to process the entire band. It
  647. * is responsible for clipping the rectangles in the band, though
  648. * this function provides the boundaries.
  649. * At the end of each band, the new region is coalesced, if possible,
  650. * to reduce the number of rectangles in the region.
  651. *
  652. *-----------------------------------------------------------------------
  653. */
  654. /* static void*/
  655. static void
  656. miRegionOp(
  657. register Region newReg, /* Place to store result */
  658. Region reg1, /* First region in operation */
  659. Region reg2, /* 2d region in operation */
  660. int (*overlapFunc)(
  661. register Region pReg,
  662. register BoxPtr r1,
  663. BoxPtr r1End,
  664. register BoxPtr r2,
  665. BoxPtr r2End,
  666. short y1,
  667. short y2), /* Function to call for over-
  668. * lapping bands */
  669. int (*nonOverlap1Func)(
  670. register Region pReg,
  671. register BoxPtr r,
  672. BoxPtr rEnd,
  673. register short y1,
  674. register short y2), /* Function to call for non-
  675. * overlapping bands in region
  676. * 1 */
  677. int (*nonOverlap2Func)(
  678. register Region pReg,
  679. register BoxPtr r,
  680. BoxPtr rEnd,
  681. register short y1,
  682. register short y2)) /* Function to call for non-
  683. * overlapping bands in region
  684. * 2 */
  685. {
  686. register BoxPtr r1; /* Pointer into first region */
  687. register BoxPtr r2; /* Pointer into 2d region */
  688. BoxPtr r1End; /* End of 1st region */
  689. BoxPtr r2End; /* End of 2d region */
  690. register short ybot; /* Bottom of intersection */
  691. register short ytop; /* Top of intersection */
  692. BoxPtr oldRects; /* Old rects for newReg */
  693. int prevBand; /* Index of start of
  694. * previous band in newReg */
  695. int curBand; /* Index of start of current
  696. * band in newReg */
  697. register BoxPtr r1BandEnd; /* End of current band in r1 */
  698. register BoxPtr r2BandEnd; /* End of current band in r2 */
  699. short top; /* Top of non-overlapping
  700. * band */
  701. short bot; /* Bottom of non-overlapping
  702. * band */
  703. /*
  704. * Initialization:
  705. * set r1, r2, r1End and r2End appropriately, preserve the important
  706. * parts of the destination region until the end in case it's one of
  707. * the two source regions, then mark the "new" region empty, allocating
  708. * another array of rectangles for it to use.
  709. */
  710. r1 = reg1->rects;
  711. r2 = reg2->rects;
  712. r1End = r1 + reg1->numRects;
  713. r2End = r2 + reg2->numRects;
  714. oldRects = newReg->rects;
  715. EMPTY_REGION(newReg);
  716. /*
  717. * Allocate a reasonable number of rectangles for the new region. The idea
  718. * is to allocate enough so the individual functions don't need to
  719. * reallocate and copy the array, which is time consuming, yet we don't
  720. * have to worry about using too much memory. I hope to be able to
  721. * nuke the Xrealloc() at the end of this function eventually.
  722. */
  723. newReg->size = max(reg1->numRects,reg2->numRects) * 2;
  724. if (! (newReg->rects = Xmalloc (sizeof(BoxRec) * newReg->size))) {
  725. newReg->size = 0;
  726. return;
  727. }
  728. /*
  729. * Initialize ybot and ytop.
  730. * In the upcoming loop, ybot and ytop serve different functions depending
  731. * on whether the band being handled is an overlapping or non-overlapping
  732. * band.
  733. * In the case of a non-overlapping band (only one of the regions
  734. * has points in the band), ybot is the bottom of the most recent
  735. * intersection and thus clips the top of the rectangles in that band.
  736. * ytop is the top of the next intersection between the two regions and
  737. * serves to clip the bottom of the rectangles in the current band.
  738. * For an overlapping band (where the two regions intersect), ytop clips
  739. * the top of the rectangles of both regions and ybot clips the bottoms.
  740. */
  741. if (reg1->extents.y1 < reg2->extents.y1)
  742. ybot = reg1->extents.y1;
  743. else
  744. ybot = reg2->extents.y1;
  745. /*
  746. * prevBand serves to mark the start of the previous band so rectangles
  747. * can be coalesced into larger rectangles. qv. miCoalesce, above.
  748. * In the beginning, there is no previous band, so prevBand == curBand
  749. * (curBand is set later on, of course, but the first band will always
  750. * start at index 0). prevBand and curBand must be indices because of
  751. * the possible expansion, and resultant moving, of the new region's
  752. * array of rectangles.
  753. */
  754. prevBand = 0;
  755. do
  756. {
  757. curBand = newReg->numRects;
  758. /*
  759. * This algorithm proceeds one source-band (as opposed to a
  760. * destination band, which is determined by where the two regions
  761. * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
  762. * rectangle after the last one in the current band for their
  763. * respective regions.
  764. */
  765. r1BandEnd = r1;
  766. while ((r1BandEnd != r1End) && (r1BandEnd->y1 == r1->y1))
  767. {
  768. r1BandEnd++;
  769. }
  770. r2BandEnd = r2;
  771. while ((r2BandEnd != r2End) && (r2BandEnd->y1 == r2->y1))
  772. {
  773. r2BandEnd++;
  774. }
  775. /*
  776. * First handle the band that doesn't intersect, if any.
  777. *
  778. * Note that attention is restricted to one band in the
  779. * non-intersecting region at once, so if a region has n
  780. * bands between the current position and the next place it overlaps
  781. * the other, this entire loop will be passed through n times.
  782. */
  783. if (r1->y1 < r2->y1)
  784. {
  785. top = max(r1->y1,ybot);
  786. bot = min(r1->y2,r2->y1);
  787. if ((top != bot) && (nonOverlap1Func != NULL))
  788. {
  789. (* nonOverlap1Func) (newReg, r1, r1BandEnd, top, bot);
  790. }
  791. ytop = r2->y1;
  792. }
  793. else if (r2->y1 < r1->y1)
  794. {
  795. top = max(r2->y1,ybot);
  796. bot = min(r2->y2,r1->y1);
  797. if ((top != bot) && (nonOverlap2Func != NULL))
  798. {
  799. (* nonOverlap2Func) (newReg, r2, r2BandEnd, top, bot);
  800. }
  801. ytop = r1->y1;
  802. }
  803. else
  804. {
  805. ytop = r1->y1;
  806. }
  807. /*
  808. * If any rectangles got added to the region, try and coalesce them
  809. * with rectangles from the previous band. Note we could just do
  810. * this test in miCoalesce, but some machines incur a not
  811. * inconsiderable cost for function calls, so...
  812. */
  813. if (newReg->numRects != curBand)
  814. {
  815. prevBand = miCoalesce (newReg, prevBand, curBand);
  816. }
  817. /*
  818. * Now see if we've hit an intersecting band. The two bands only
  819. * intersect if ybot > ytop
  820. */
  821. ybot = min(r1->y2, r2->y2);
  822. curBand = newReg->numRects;
  823. if (ybot > ytop)
  824. {
  825. (* overlapFunc) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);
  826. }
  827. if (newReg->numRects != curBand)
  828. {
  829. prevBand = miCoalesce (newReg, prevBand, curBand);
  830. }
  831. /*
  832. * If we've finished with a band (y2 == ybot) we skip forward
  833. * in the region to the next band.
  834. */
  835. if (r1->y2 == ybot)
  836. {
  837. r1 = r1BandEnd;
  838. }
  839. if (r2->y2 == ybot)
  840. {
  841. r2 = r2BandEnd;
  842. }
  843. } while ((r1 != r1End) && (r2 != r2End));
  844. /*
  845. * Deal with whichever region still has rectangles left.
  846. */
  847. curBand = newReg->numRects;
  848. if (r1 != r1End)
  849. {
  850. if (nonOverlap1Func != NULL)
  851. {
  852. do
  853. {
  854. r1BandEnd = r1;
  855. while ((r1BandEnd < r1End) && (r1BandEnd->y1 == r1->y1))
  856. {
  857. r1BandEnd++;
  858. }
  859. (* nonOverlap1Func) (newReg, r1, r1BandEnd,
  860. max(r1->y1,ybot), r1->y2);
  861. r1 = r1BandEnd;
  862. } while (r1 != r1End);
  863. }
  864. }
  865. else if ((r2 != r2End) && (nonOverlap2Func != NULL))
  866. {
  867. do
  868. {
  869. r2BandEnd = r2;
  870. while ((r2BandEnd < r2End) && (r2BandEnd->y1 == r2->y1))
  871. {
  872. r2BandEnd++;
  873. }
  874. (* nonOverlap2Func) (newReg, r2, r2BandEnd,
  875. max(r2->y1,ybot), r2->y2);
  876. r2 = r2BandEnd;
  877. } while (r2 != r2End);
  878. }
  879. if (newReg->numRects != curBand)
  880. {
  881. (void) miCoalesce (newReg, prevBand, curBand);
  882. }
  883. /*
  884. * A bit of cleanup. To keep regions from growing without bound,
  885. * we shrink the array of rectangles to match the new number of
  886. * rectangles in the region. This never goes to 0, however...
  887. *
  888. * Only do this stuff if the number of rectangles allocated is more than
  889. * twice the number of rectangles in the region (a simple optimization...).
  890. */
  891. if (newReg->numRects < (newReg->size >> 1))
  892. {
  893. if (REGION_NOT_EMPTY(newReg))
  894. {
  895. BoxPtr prev_rects = newReg->rects;
  896. newReg->rects = Xrealloc (newReg->rects,
  897. sizeof(BoxRec) * newReg->numRects);
  898. if (! newReg->rects)
  899. newReg->rects = prev_rects;
  900. else
  901. newReg->size = newReg->numRects;
  902. }
  903. else
  904. {
  905. /*
  906. * No point in doing the extra work involved in an Xrealloc if
  907. * the region is empty
  908. */
  909. newReg->size = 1;
  910. Xfree(newReg->rects);
  911. newReg->rects = Xmalloc(sizeof(BoxRec));
  912. }
  913. }
  914. Xfree (oldRects);
  915. return;
  916. }
  917. /*======================================================================
  918. * Region Union
  919. *====================================================================*/
  920. /*-
  921. *-----------------------------------------------------------------------
  922. * miUnionNonO --
  923. * Handle a non-overlapping band for the union operation. Just
  924. * Adds the rectangles into the region. Doesn't have to check for
  925. * subsumption or anything.
  926. *
  927. * Results:
  928. * None.
  929. *
  930. * Side Effects:
  931. * pReg->numRects is incremented and the final rectangles overwritten
  932. * with the rectangles we're passed.
  933. *
  934. *-----------------------------------------------------------------------
  935. */
  936. /* static void*/
  937. static int
  938. miUnionNonO (
  939. register Region pReg,
  940. register BoxPtr r,
  941. BoxPtr rEnd,
  942. register short y1,
  943. register short y2)
  944. {
  945. register BoxPtr pNextRect;
  946. pNextRect = &pReg->rects[pReg->numRects];
  947. assert(y1 < y2);
  948. while (r != rEnd)
  949. {
  950. assert(r->x1 < r->x2);
  951. MEMCHECK(pReg, pNextRect, pReg->rects);
  952. pNextRect->x1 = r->x1;
  953. pNextRect->y1 = y1;
  954. pNextRect->x2 = r->x2;
  955. pNextRect->y2 = y2;
  956. pReg->numRects += 1;
  957. pNextRect++;
  958. assert(pReg->numRects<=pReg->size);
  959. r++;
  960. }
  961. return 0; /* lint */
  962. }
  963. /*-
  964. *-----------------------------------------------------------------------
  965. * miUnionO --
  966. * Handle an overlapping band for the union operation. Picks the
  967. * left-most rectangle each time and merges it into the region.
  968. *
  969. * Results:
  970. * None.
  971. *
  972. * Side Effects:
  973. * Rectangles are overwritten in pReg->rects and pReg->numRects will
  974. * be changed.
  975. *
  976. *-----------------------------------------------------------------------
  977. */
  978. /* static void*/
  979. static int
  980. miUnionO (
  981. register Region pReg,
  982. register BoxPtr r1,
  983. BoxPtr r1End,
  984. register BoxPtr r2,
  985. BoxPtr r2End,
  986. register short y1,
  987. register short y2)
  988. {
  989. register BoxPtr pNextRect;
  990. pNextRect = &pReg->rects[pReg->numRects];
  991. #define MERGERECT(r) \
  992. if ((pReg->numRects != 0) && \
  993. (pNextRect[-1].y1 == y1) && \
  994. (pNextRect[-1].y2 == y2) && \
  995. (pNextRect[-1].x2 >= r->x1)) \
  996. { \
  997. if (pNextRect[-1].x2 < r->x2) \
  998. { \
  999. pNextRect[-1].x2 = r->x2; \
  1000. assert(pNextRect[-1].x1<pNextRect[-1].x2); \
  1001. } \
  1002. } \
  1003. else \
  1004. { \
  1005. MEMCHECK(pReg, pNextRect, pReg->rects); \
  1006. pNextRect->y1 = y1; \
  1007. pNextRect->y2 = y2; \
  1008. pNextRect->x1 = r->x1; \
  1009. pNextRect->x2 = r->x2; \
  1010. pReg->numRects += 1; \
  1011. pNextRect += 1; \
  1012. } \
  1013. assert(pReg->numRects<=pReg->size);\
  1014. r++;
  1015. assert (y1<y2);
  1016. while ((r1 != r1End) && (r2 != r2End))
  1017. {
  1018. if (r1->x1 < r2->x1)
  1019. {
  1020. MERGERECT(r1);
  1021. }
  1022. else
  1023. {
  1024. MERGERECT(r2);
  1025. }
  1026. }
  1027. if (r1 != r1End)
  1028. {
  1029. do
  1030. {
  1031. MERGERECT(r1);
  1032. } while (r1 != r1End);
  1033. }
  1034. else while (r2 != r2End)
  1035. {
  1036. MERGERECT(r2);
  1037. }
  1038. return 0; /* lint */
  1039. }
  1040. int
  1041. XUnionRegion(
  1042. Region reg1,
  1043. Region reg2, /* source regions */
  1044. Region newReg) /* destination Region */
  1045. {
  1046. /* checks all the simple cases */
  1047. /*
  1048. * Region 1 and 2 are the same or region 1 is empty
  1049. */
  1050. if ( (reg1 == reg2) || (!(reg1->numRects)) )
  1051. {
  1052. if (newReg != reg2)
  1053. return miRegionCopy(newReg, reg2);
  1054. return 1;
  1055. }
  1056. /*
  1057. * if nothing to union (region 2 empty)
  1058. */
  1059. if (!(reg2->numRects))
  1060. {
  1061. if (newReg != reg1)
  1062. return miRegionCopy(newReg, reg1);
  1063. return 1;
  1064. }
  1065. /*
  1066. * Region 1 completely subsumes region 2
  1067. */
  1068. if ((reg1->numRects == 1) &&
  1069. (reg1->extents.x1 <= reg2->extents.x1) &&
  1070. (reg1->extents.y1 <= reg2->extents.y1) &&
  1071. (reg1->extents.x2 >= reg2->extents.x2) &&
  1072. (reg1->extents.y2 >= reg2->extents.y2))
  1073. {
  1074. if (newReg != reg1)
  1075. return miRegionCopy(newReg, reg1);
  1076. return 1;
  1077. }
  1078. /*
  1079. * Region 2 completely subsumes region 1
  1080. */
  1081. if ((reg2->numRects == 1) &&
  1082. (reg2->extents.x1 <= reg1->extents.x1) &&
  1083. (reg2->extents.y1 <= reg1->extents.y1) &&
  1084. (reg2->extents.x2 >= reg1->extents.x2) &&
  1085. (reg2->extents.y2 >= reg1->extents.y2))
  1086. {
  1087. if (newReg != reg2)
  1088. return miRegionCopy(newReg, reg2);
  1089. return 1;
  1090. }
  1091. miRegionOp (newReg, reg1, reg2, miUnionO,
  1092. miUnionNonO, miUnionNonO);
  1093. newReg->extents.x1 = min(reg1->extents.x1, reg2->extents.x1);
  1094. newReg->extents.y1 = min(reg1->extents.y1, reg2->extents.y1);
  1095. newReg->extents.x2 = max(reg1->extents.x2, reg2->extents.x2);
  1096. newReg->extents.y2 = max(reg1->extents.y2, reg2->extents.y2);
  1097. return 1;
  1098. }
  1099. /*======================================================================
  1100. * Region Subtraction
  1101. *====================================================================*/
  1102. /*-
  1103. *-----------------------------------------------------------------------
  1104. * miSubtractNonO --
  1105. * Deal with non-overlapping band for subtraction. Any parts from
  1106. * region 2 we discard. Anything from region 1 we add to the region.
  1107. *
  1108. * Results:
  1109. * None.
  1110. *
  1111. * Side Effects:
  1112. * pReg may be affected.
  1113. *
  1114. *-----------------------------------------------------------------------
  1115. */
  1116. /* static void*/
  1117. static int
  1118. miSubtractNonO1 (
  1119. register Region pReg,
  1120. register BoxPtr r,
  1121. BoxPtr rEnd,
  1122. register short y1,
  1123. register short y2)
  1124. {
  1125. register BoxPtr pNextRect;
  1126. pNextRect = &pReg->rects[pReg->numRects];
  1127. assert(y1<y2);
  1128. while (r != rEnd)
  1129. {
  1130. assert(r->x1<r->x2);
  1131. MEMCHECK(pReg, pNextRect, pReg->rects);
  1132. pNextRect->x1 = r->x1;
  1133. pNextRect->y1 = y1;
  1134. pNextRect->x2 = r->x2;
  1135. pNextRect->y2 = y2;
  1136. pReg->numRects += 1;
  1137. pNextRect++;
  1138. assert(pReg->numRects <= pReg->size);
  1139. r++;
  1140. }
  1141. return 0; /* lint */
  1142. }
  1143. /*-
  1144. *-----------------------------------------------------------------------
  1145. * miSubtractO --
  1146. * Overlapping band subtraction. x1 is the left-most point not yet
  1147. * checked.
  1148. *
  1149. * Results:
  1150. * None.
  1151. *
  1152. * Side Effects:
  1153. * pReg may have rectangles added to it.
  1154. *
  1155. *-----------------------------------------------------------------------
  1156. */
  1157. /* static void*/
  1158. static int
  1159. miSubtractO (
  1160. register Region pReg,
  1161. register BoxPtr r1,
  1162. BoxPtr r1End,
  1163. register BoxPtr r2,
  1164. BoxPtr r2End,
  1165. register short y1,
  1166. register short y2)
  1167. {
  1168. register BoxPtr pNextRect;
  1169. register int x1;
  1170. x1 = r1->x1;
  1171. assert(y1<y2);
  1172. pNextRect = &pReg->rects[pReg->numRects];
  1173. while ((r1 != r1End) && (r2 != r2End))
  1174. {
  1175. if (r2->x2 <= x1)
  1176. {
  1177. /*
  1178. * Subtrahend missed the boat: go to next subtrahend.
  1179. */
  1180. r2++;
  1181. }
  1182. else if (r2->x1 <= x1)
  1183. {
  1184. /*
  1185. * Subtrahend preceds minuend: nuke left edge of minuend.
  1186. */
  1187. x1 = r2->x2;
  1188. if (x1 >= r1->x2)
  1189. {
  1190. /*
  1191. * Minuend completely covered: advance to next minuend and
  1192. * reset left fence to edge of new minuend.
  1193. */
  1194. r1++;
  1195. if (r1 != r1End)
  1196. x1 = r1->x1;
  1197. }
  1198. else
  1199. {
  1200. /*
  1201. * Subtrahend now used up since it doesn't extend beyond
  1202. * minuend
  1203. */
  1204. r2++;
  1205. }
  1206. }
  1207. else if (r2->x1 < r1->x2)
  1208. {
  1209. /*
  1210. * Left part of subtrahend covers part of minuend: add uncovered
  1211. * part of minuend to region and skip to next subtrahend.
  1212. */
  1213. assert(x1<r2->x1);
  1214. MEMCHECK(pReg, pNextRect, pReg->rects);
  1215. pNextRect->x1 = x1;
  1216. pNextRect->y1 = y1;
  1217. pNextRect->x2 = r2->x1;
  1218. pNextRect->y2 = y2;
  1219. pReg->numRects += 1;
  1220. pNextRect++;
  1221. assert(pReg->numRects<=pReg->size);
  1222. x1 = r2->x2;
  1223. if (x1 >= r1->x2)
  1224. {
  1225. /*
  1226. * Minuend used up: advance to new...
  1227. */
  1228. r1++;
  1229. if (r1 != r1End)
  1230. x1 = r1->x1;
  1231. }
  1232. else
  1233. {
  1234. /*
  1235. * Subtrahend used up
  1236. */
  1237. r2++;
  1238. }
  1239. }
  1240. else
  1241. {
  1242. /*
  1243. * Minuend used up: add any remaining piece before advancing.
  1244. */
  1245. if (r1->x2 > x1)
  1246. {
  1247. MEMCHECK(pReg, pNextRect, pReg->rects);
  1248. pNextRect->x1 = x1;
  1249. pNextRect->y1 = y1;
  1250. pNextRect->x2 = r1->x2;
  1251. pNextRect->y2 = y2;
  1252. pReg->numRects += 1;
  1253. pNextRect++;
  1254. assert(pReg->numRects<=pReg->size);
  1255. }
  1256. r1++;
  1257. if (r1 != r1End)
  1258. x1 = r1->x1;
  1259. }
  1260. }
  1261. /*
  1262. * Add remaining minuend rectangles to region.
  1263. */
  1264. while (r1 != r1End)
  1265. {
  1266. assert(x1<r1->x2);
  1267. MEMCHECK(pReg, pNextRect, pReg->rects);
  1268. pNextRect->x1 = x1;
  1269. pNextRect->y1 = y1;
  1270. pNextRect->x2 = r1->x2;
  1271. pNextRect->y2 = y2;
  1272. pReg->numRects += 1;
  1273. pNextRect++;
  1274. assert(pReg->numRects<=pReg->size);
  1275. r1++;
  1276. if (r1 != r1End)
  1277. {
  1278. x1 = r1->x1;
  1279. }
  1280. }
  1281. return 0; /* lint */
  1282. }
  1283. /*-
  1284. *-----------------------------------------------------------------------
  1285. * miSubtract --
  1286. * Subtract regS from regM and leave the result in regD.
  1287. * S stands for subtrahend, M for minuend and D for difference.
  1288. *
  1289. * Results:
  1290. * TRUE.
  1291. *
  1292. * Side Effects:
  1293. * regD is overwritten.
  1294. *
  1295. *-----------------------------------------------------------------------
  1296. */
  1297. int
  1298. XSubtractRegion(
  1299. Region regM,
  1300. Region regS,
  1301. register Region regD)
  1302. {
  1303. /* check for trivial reject */
  1304. if ( (!(regM->numRects)) || (!(regS->numRects)) ||
  1305. (!EXTENTCHECK(&regM->extents, &regS->extents)) )
  1306. {
  1307. return miRegionCopy(regD, regM);
  1308. }
  1309. miRegionOp (regD, regM, regS, miSubtractO,
  1310. miSubtractNonO1, NULL);
  1311. /*
  1312. * Can't alter newReg's extents before we call miRegionOp because
  1313. * it might be one of the source regions and miRegionOp depends
  1314. * on the extents of those regions being the unaltered. Besides, this
  1315. * way there's no checking against rectangles that will be nuked
  1316. * due to coalescing, so we have to examine fewer rectangles.
  1317. */
  1318. miSetExtents (regD);
  1319. return 1;
  1320. }
  1321. int
  1322. XXorRegion(Region sra, Region srb, Region dr)
  1323. {
  1324. Region tra, trb;
  1325. if (! (tra = XCreateRegion()) )
  1326. return 0;
  1327. if (! (trb = XCreateRegion()) ) {
  1328. XDestroyRegion(tra);
  1329. return 0;
  1330. }
  1331. (void) XSubtractRegion(sra,srb,tra);
  1332. (void) XSubtractRegion(srb,sra,trb);
  1333. (void) XUnionRegion(tra,trb,dr);
  1334. XDestroyRegion(tra);
  1335. XDestroyRegion(trb);
  1336. return 0;
  1337. }
  1338. /*
  1339. * Check to see if the region is empty. Assumes a region is passed
  1340. * as a parameter
  1341. */
  1342. int
  1343. XEmptyRegion(
  1344. Region r)
  1345. {
  1346. if( r->numRects == 0 ) return TRUE;
  1347. else return FALSE;
  1348. }
  1349. /*
  1350. * Check to see if two regions are equal
  1351. */
  1352. int
  1353. XEqualRegion(Region r1, Region r2)
  1354. {
  1355. int i;
  1356. if( r1->numRects != r2->numRects ) return FALSE;
  1357. else if( r1->numRects == 0 ) return TRUE;
  1358. else if ( r1->extents.x1 != r2->extents.x1 ) return FALSE;
  1359. else if ( r1->extents.x2 != r2->extents.x2 ) return FALSE;
  1360. else if ( r1->extents.y1 != r2->extents.y1 ) return FALSE;
  1361. else if ( r1->extents.y2 != r2->extents.y2 ) return FALSE;
  1362. else for( i=0; i < r1->numRects; i++ ) {
  1363. if ( r1->rects[i].x1 != r2->rects[i].x1 ) return FALSE;
  1364. else if ( r1->rects[i].x2 != r2->rects[i].x2 ) return FALSE;
  1365. else if ( r1->rects[i].y1 != r2->rects[i].y1 ) return FALSE;
  1366. else if ( r1->rects[i].y2 != r2->rects[i].y2 ) return FALSE;
  1367. }
  1368. return TRUE;
  1369. }
  1370. int
  1371. XPointInRegion(
  1372. Region pRegion,
  1373. int x, int y)
  1374. {
  1375. int i;
  1376. if (pRegion->numRects == 0)
  1377. return FALSE;
  1378. if (!INBOX(pRegion->extents, x, y))
  1379. return FALSE;
  1380. for (i=0; i<pRegion->numRects; i++)
  1381. {
  1382. if (INBOX (pRegion->rects[i], x, y))
  1383. return TRUE;
  1384. }
  1385. return FALSE;
  1386. }
  1387. int
  1388. XRectInRegion(
  1389. register Region region,
  1390. int rx, int ry,
  1391. unsigned int rwidth, unsigned int rheight)
  1392. {
  1393. register BoxPtr pbox;
  1394. register BoxPtr pboxEnd;
  1395. Box rect;
  1396. register BoxPtr prect = &rect;
  1397. int partIn, partOut;
  1398. prect->x1 = rx;
  1399. prect->y1 = ry;
  1400. prect->x2 = rwidth + rx;
  1401. prect->y2 = rheight + ry;
  1402. /* this is (just) a useful optimization */
  1403. if ((region->numRects == 0) || !EXTENTCHECK(&region->extents, prect))
  1404. return(RectangleOut);
  1405. partOut = FALSE;
  1406. partIn = FALSE;
  1407. /* can stop when both partOut and partIn are TRUE, or we reach prect->y2 */
  1408. for (pbox = region->rects, pboxEnd = pbox + region->numRects;
  1409. pbox < pboxEnd;
  1410. pbox++)
  1411. {
  1412. if (pbox->y2 <= ry)
  1413. continue; /* getting up to speed or skipping remainder of band */
  1414. if (pbox->y1 > ry)
  1415. {
  1416. partOut = TRUE; /* missed part of rectangle above */
  1417. if (partIn || (pbox->y1 >= prect->y2))
  1418. break;
  1419. ry = pbox->y1; /* x guaranteed to be == prect->x1 */
  1420. }
  1421. if (pbox->x2 <= rx)
  1422. continue; /* not far enough over yet */
  1423. if (pbox->x1 > rx)
  1424. {
  1425. partOut = TRUE; /* missed part of rectangle to left */
  1426. if (partIn)
  1427. break;
  1428. }
  1429. if (pbox->x1 < prect->x2)
  1430. {
  1431. partIn = TRUE; /* definitely overlap */
  1432. if (partOut)
  1433. break;
  1434. }
  1435. if (pbox->x2 >= prect->x2)
  1436. {
  1437. ry = pbox->y2; /* finished with this band */
  1438. if (ry >= prect->y2)
  1439. break;
  1440. rx = prect->x1; /* reset x out to left again */
  1441. } else
  1442. {
  1443. /*
  1444. * Because boxes in a band are maximal width, if the first box
  1445. * to overlap the rectangle doesn't completely cover it in that
  1446. * band, the rectangle must be partially out, since some of it
  1447. * will be uncovered in that band. partIn will have been set true
  1448. * by now...
  1449. */
  1450. break;
  1451. }
  1452. }
  1453. return(partIn ? ((ry < prect->y2) ? RectanglePart : RectangleIn) :
  1454. RectangleOut);
  1455. }