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

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