home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_Region.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  48KB  |  1,712 lines

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