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 44KB

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