gdkregion-generic.c 49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/* $TOG: Region.c /main/31 1998/02/06 17:50:22 kaleb $ */
/************************************************************************

Copyright 1987, 1988, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

************************************************************************/
/* $XFree86: xc/lib/X11/Region.c,v 1.5 1999/05/09 10:50:01 dawes Exp $ */
/*
 * The functions in this file implement the Region abstraction, similar to one
 * used in the X11 sample server. A Region is simply an area, as the name
 * implies, and is implemented as a "y-x-banded" array of rectangles. To
 * explain: Each Region is made up of a certain number of rectangles sorted
 * by y coordinate first, and then by x coordinate.
 *
 * Furthermore, the rectangles are banded such that every rectangle with a
 * given upper-left y coordinate (y1) will have the same lower-right y
 * coordinate (y2) and vice versa. If a rectangle has scanlines in a band, it
 * will span the entire vertical distance of the band. This means that some
 * areas that could be merged into a taller rectangle will be represented as
 * several shorter rectangles to account for shorter rectangles to its left
 * or right but within its "vertical scope".
 *
 * An added constraint on the rectangles is that they must cover as much
 * horizontal area as possible. E.g. no two rectangles in a band are allowed
 * to touch.
 *
 * Whenever possible, bands will be merged together to cover a greater vertical
 * distance (and thus reduce the number of rectangles). Two bands can be merged
 * only if the bottom of one touches the top of the other and they have
 * rectangles in the same places (of the same width, of course). This maintains
 * the y-x-banding that's so nice to have...
 */

71
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
72 73
#include <stdlib.h>
#include <string.h>
74 75
#include <gdkregion.h>
#include "gdkregion-generic.h"
76
#include "gdkalias.h"
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
typedef void (* overlapFunc)    (GdkRegion    *pReg,
                                 GdkRegionBox *r1,
                                 GdkRegionBox *r1End,
                                 GdkRegionBox *r2,
                                 GdkRegionBox *r2End,
                                 gint          y1,
                                 gint          y2);
typedef void (* nonOverlapFunc) (GdkRegion    *pReg,
                                 GdkRegionBox *r,
                                 GdkRegionBox *rEnd,
                                 gint          y1,
                                 gint          y2);

static void miRegionCopy (GdkRegion       *dstrgn,
			  const GdkRegion *rgn);
static void miRegionOp   (GdkRegion       *newReg,
			  GdkRegion       *reg1,
			  const GdkRegion *reg2,
			  overlapFunc      overlapFn,
			  nonOverlapFunc   nonOverlap1Fn,
			  nonOverlapFunc   nonOverlap2Fn);
99
static void miSetExtents (GdkRegion       *pReg);
100

101 102 103 104 105 106 107
/**
 * gdk_region_new:
 *
 * Creates a new empty #GdkRegion.
 *
 * Returns: a new empty #GdkRegion
 */
108
GdkRegion *
Matthias Clasen's avatar
Matthias Clasen committed
109
gdk_region_new (void)
110 111 112
{
  GdkRegion *temp;

113
  temp = g_slice_new (GdkRegion);
114 115

  temp->numRects = 0;
116
  temp->rects = &temp->extents;
117 118 119 120 121 122 123 124 125
  temp->extents.x1 = 0;
  temp->extents.y1 = 0;
  temp->extents.x2 = 0;
  temp->extents.y2 = 0;
  temp->size = 1;
  
  return temp;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
GdkRegion *
_gdk_region_new_from_yxbanded_rects (GdkRectangle *rects,
				     int num_rects)
{
  GdkRegion *temp;
  int i;

  temp = g_slice_new (GdkRegion);

  temp->rects = g_new (GdkRegionBox, num_rects);
  temp->size = num_rects;
  temp->numRects = num_rects;
  for (i = 0; i < num_rects; i++)
    {
      temp->rects[i].x1 = rects[i].x;
      temp->rects[i].y1 = rects[i].y;
      temp->rects[i].x2 = rects[i].x + rects[i].width;
      temp->rects[i].y2 = rects[i].y + rects[i].height;
    }
  miSetExtents (temp);  
  
  return temp;
}


Havoc Pennington's avatar
Havoc Pennington committed
151 152 153 154 155 156 157 158
/**
 * gdk_region_rectangle:
 * @rectangle: a #GdkRectangle
 * 
 * Creates a new region containing the area @rectangle.
 * 
 * Return value: a new region
 **/
159
GdkRegion *
160
gdk_region_rectangle (const GdkRectangle *rectangle)
161 162 163
{
  GdkRegion *temp;

164 165
  g_return_val_if_fail (rectangle != NULL, NULL);

166 167 168
  if (rectangle->width <= 0 || rectangle->height <= 0)
    return gdk_region_new();

169
  temp = g_slice_new (GdkRegion);
170 171

  temp->numRects = 1;
172 173 174 175 176
  temp->rects = &temp->extents;
  temp->extents.x1 = rectangle->x;
  temp->extents.y1 = rectangle->y;
  temp->extents.x2 = rectangle->x + rectangle->width;
  temp->extents.y2 = rectangle->y + rectangle->height;
177 178 179 180 181
  temp->size = 1;
  
  return temp;
}

Havoc Pennington's avatar
Havoc Pennington committed
182 183 184 185 186 187 188 189
/**
 * gdk_region_copy:
 * @region: a #GdkRegion
 * 
 * Copies @region, creating an identical new region.
 * 
 * Return value: a new region identical to @region
 **/
190
GdkRegion *
191
gdk_region_copy (const GdkRegion *region)
192 193 194
{
  GdkRegion *temp;

195 196
  g_return_val_if_fail (region != NULL, NULL);

197
  temp = gdk_region_new ();
198

199
  miRegionCopy (temp, region);
200 201 202 203

  return temp;
}

204 205 206 207 208
/**
 * gdk_region_get_clipbox:
 * @region: a #GdkRegion
 * @rectangle: return location for the clipbox
 *
Matthias Clasen's avatar
Matthias Clasen committed
209 210
 * Obtains the smallest rectangle which includes the entire #GdkRegion.
 *
211
 */
212
void
213 214
gdk_region_get_clipbox (const GdkRegion *region,
			GdkRectangle    *rectangle)
215
{
216 217
  g_return_if_fail (region != NULL);
  g_return_if_fail (rectangle != NULL);
218
  
219 220 221 222
  rectangle->x = region->extents.x1;
  rectangle->y = region->extents.y1;
  rectangle->width = region->extents.x2 - region->extents.x1;
  rectangle->height = region->extents.y2 - region->extents.y1;
223 224
}

225 226 227 228

/**
 * gdk_region_get_rectangles:
 * @region: a #GdkRegion
229
 * @rectangles: (array length=n_rectangles) (transfer container): return location for an array of rectangles
230 231 232 233 234 235
 * @n_rectangles: length of returned array
 *
 * Obtains the area covered by the region as a list of rectangles.
 * The array returned in @rectangles must be freed with g_free().
 **/
void
236 237 238
gdk_region_get_rectangles (const GdkRegion  *region,
                           GdkRectangle    **rectangles,
                           gint             *n_rectangles)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
  gint i;
  
  g_return_if_fail (region != NULL);
  g_return_if_fail (rectangles != NULL);
  g_return_if_fail (n_rectangles != NULL);
  
  *n_rectangles = region->numRects;
  *rectangles = g_new (GdkRectangle, region->numRects);

  for (i = 0; i < region->numRects; i++)
    {
      GdkRegionBox rect;
      rect = region->rects[i];
      (*rectangles)[i].x = rect.x1;
      (*rectangles)[i].y = rect.y1;
      (*rectangles)[i].width = rect.x2 - rect.x1;
      (*rectangles)[i].height = rect.y2 - rect.y1;
    }
}

260 261 262 263 264 265 266 267 268
/**
 * gdk_region_union_with_rect:
 * @region: a #GdkRegion.
 * @rect: a #GdkRectangle.
 * 
 * Sets the area of @region to the union of the areas of @region and
 * @rect. The resulting area is the set of pixels contained in
 * either @region or @rect.
 **/
269
void
270 271
gdk_region_union_with_rect (GdkRegion          *region,
			    const GdkRectangle *rect)
272 273 274
{
  GdkRegion tmp_region;

275 276 277
  g_return_if_fail (region != NULL);
  g_return_if_fail (rect != NULL);

278
  if (rect->width <= 0 || rect->height <= 0)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    return;
    
  tmp_region.rects = &tmp_region.extents;
  tmp_region.numRects = 1;
  tmp_region.extents.x1 = rect->x;
  tmp_region.extents.y1 = rect->y;
  tmp_region.extents.x2 = rect->x + rect->width;
  tmp_region.extents.y2 = rect->y + rect->height;
  tmp_region.size = 1;

  gdk_region_union (region, &tmp_region);
}

/*-
 *-----------------------------------------------------------------------
 * miSetExtents --
 *	Reset the extents of a region to what they should be. Called by
 *	miSubtract and miIntersect b/c they can't figure it out along the
 *	way or do so easily, as miUnion can.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The region's 'extents' structure is overwritten.
 *
 *-----------------------------------------------------------------------
 */
static void
miSetExtents (GdkRegion *pReg)
{
  GdkRegionBox *pBox, *pBoxEnd, *pExtents;

  if (pReg->numRects == 0)
    {
      pReg->extents.x1 = 0;
      pReg->extents.y1 = 0;
      pReg->extents.x2 = 0;
      pReg->extents.y2 = 0;
      return;
    }
320
  
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  pExtents = &pReg->extents;
  pBox = pReg->rects;
  pBoxEnd = &pBox[pReg->numRects - 1];

    /*
     * Since pBox is the first rectangle in the region, it must have the
     * smallest y1 and since pBoxEnd is the last rectangle in the region,
     * it must have the largest y2, because of banding. Initialize x1 and
     * x2 from  pBox and pBoxEnd, resp., as good things to initialize them
     * to...
     */
  pExtents->x1 = pBox->x1;
  pExtents->y1 = pBox->y1;
  pExtents->x2 = pBoxEnd->x2;
  pExtents->y2 = pBoxEnd->y2;

337
  g_assert(pExtents->y1 < pExtents->y2);
338 339 340 341 342 343 344 345 346 347 348 349
  while (pBox <= pBoxEnd)
    {
      if (pBox->x1 < pExtents->x1)
	{
	  pExtents->x1 = pBox->x1;
	}
      if (pBox->x2 > pExtents->x2)
	{
	  pExtents->x2 = pBox->x2;
	}
      pBox++;
    }
350
  g_assert(pExtents->x1 < pExtents->x2);
351 352
}

353 354 355 356 357 358
/**
 * gdk_region_destroy:
 * @region: a #GdkRegion
 *
 * Destroys a #GdkRegion.
 */
359
void
360
gdk_region_destroy (GdkRegion *region)
361
{
362
  g_return_if_fail (region != NULL);
363

364 365 366 367
  if (region->rects != &region->extents)
    g_free (region->rects);
  g_slice_free (GdkRegion, region);
}
368 369


370 371 372 373 374 375 376 377
/**
 * gdk_region_offset:
 * @region: a #GdkRegion
 * @dx: the distance to move the region horizontally
 * @dy: the distance to move the region vertically
 *
 * Moves a region the specified distance.
 */
378 379 380 381 382 383 384 385
void
gdk_region_offset (GdkRegion *region,
		   gint       x,
		   gint       y)
{
  int nbox;
  GdkRegionBox *pbox;

386 387
  g_return_if_fail (region != NULL);

388 389 390 391 392 393 394 395 396 397 398
  pbox = region->rects;
  nbox = region->numRects;

  while(nbox--)
    {
      pbox->x1 += x;
      pbox->x2 += x;
      pbox->y1 += y;
      pbox->y2 += y;
      pbox++;
    }
399 400 401 402 403 404 405
  if (region->rects != &region->extents)
    {
      region->extents.x1 += x;
      region->extents.x2 += x;
      region->extents.y1 += y;
      region->extents.y2 += y;
    }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

/* 
   Utility procedure Compress:
   Replace r by the region r', where 
     p in r' iff (Quantifer m <= dx) (p + m in r), and
     Quantifier is Exists if grow is TRUE, For all if grow is FALSE, and
     (x,y) + m = (x+m,y) if xdir is TRUE; (x,y+m) if xdir is FALSE.

   Thus, if xdir is TRUE and grow is FALSE, r is replaced by the region
   of all points p such that p and the next dx points on the same
   horizontal scan line are all in r.  We do this using by noting
   that p is the head of a run of length 2^i + k iff p is the head
   of a run of length 2^i and p+2^i is the head of a run of length
   k. Thus, the loop invariant: s contains the region corresponding
   to the runs of length shift.  r contains the region corresponding
   to the runs of length 1 + dxo & (shift-1), where dxo is the original
   value of dx.  dx = dxo & ~(shift-1).  As parameters, s and t are
   scratch regions, so that we don't have to allocate them on every
   call.
*/

#define ZOpRegion(a,b) if (grow) gdk_region_union (a, b); \
			 else gdk_region_intersect (a,b)
#define ZShiftRegion(a,b) if (xdir) gdk_region_offset (a,b,0); \
			  else gdk_region_offset (a,0,b)

static void
Compress(GdkRegion *r,
	 GdkRegion *s,
	 GdkRegion *t,
	 guint      dx,
	 int        xdir,
	 int        grow)
{
  guint shift = 1;

  miRegionCopy (s, r);
  while (dx)
    {
      if (dx & shift)
	{
	  ZShiftRegion(r, -(int)shift);
	  ZOpRegion(r, s);
	  dx -= shift;
	  if (!dx) break;
        }
      miRegionCopy (t, s);
      ZShiftRegion(s, -(int)shift);
      ZOpRegion(s, t);
      shift <<= 1;
    }
}

#undef ZOpRegion
#undef ZShiftRegion
#undef ZCopyRegion

464 465 466 467 468 469 470 471 472
/**
 * gdk_region_shrink:
 * @region: a #GdkRegion
 * @dx: the number of pixels to shrink the region horizontally
 * @dy: the number of pixels to shrink the region vertically
 *
 * Resizes a region by the specified amount.
 * Positive values shrink the region. Negative values expand it.
 */
473
void
474
gdk_region_shrink (GdkRegion *region,
475 476 477 478 479 480
		   int        dx,
		   int        dy)
{
  GdkRegion *s, *t;
  int grow;

481
  g_return_if_fail (region != NULL);
482

483 484 485 486 487 488 489 490 491 492
  if (!dx && !dy)
    return;

  s = gdk_region_new ();
  t = gdk_region_new ();

  grow = (dx < 0);
  if (grow)
    dx = -dx;
  if (dx)
493
     Compress(region, s, t, (unsigned) 2*dx, TRUE, grow);
494 495 496 497 498
     
  grow = (dy < 0);
  if (grow)
    dy = -dy;
  if (dy)
499
     Compress(region, s, t, (unsigned) 2*dy, FALSE, grow);
500
  
501
  gdk_region_offset (region, dx, dy);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  gdk_region_destroy (s);
  gdk_region_destroy (t);
}


/*======================================================================
 *	    Region Intersection
 *====================================================================*/
/*-
 *-----------------------------------------------------------------------
 * miIntersectO --
 *	Handle an overlapping band for miIntersect.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Rectangles may be added to the region.
 *
 *-----------------------------------------------------------------------
 */
/* static void*/
static void
miIntersectO (GdkRegion    *pReg,
	      GdkRegionBox *r1,
	      GdkRegionBox *r1End,
	      GdkRegionBox *r2,
	      GdkRegionBox *r2End,
	      gint          y1,
	      gint          y2)
{
  int  	x1;
  int  	x2;
  GdkRegionBox *pNextRect;

  pNextRect = &pReg->rects[pReg->numRects];

  while ((r1 != r1End) && (r2 != r2End))
    {
      x1 = MAX (r1->x1,r2->x1);
      x2 = MIN (r1->x2,r2->x2);

      /*
       * If there's any overlap between the two rectangles, add that
       * overlap to the new region.
       * There's no need to check for subsumption because the only way
       * such a need could arise is if some region has two rectangles
       * right next to each other. Since that should never happen...
       */
      if (x1 < x2)
	{
553
	  g_assert (y1<y2);
554 555 556 557 558 559 560 561

	  MEMCHECK (pReg, pNextRect, pReg->rects);
	  pNextRect->x1 = x1;
	  pNextRect->y1 = y1;
	  pNextRect->x2 = x2;
	  pNextRect->y2 = y2;
	  pReg->numRects += 1;
	  pNextRect++;
562
	  g_assert (pReg->numRects <= pReg->size);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	}

      /*
       * Need to advance the pointers. Shift the one that extends
       * to the right the least, since the other still has a chance to
       * overlap with that region's next rectangle, if you see what I mean.
       */
      if (r1->x2 < r2->x2)
	{
	  r1++;
	}
      else if (r2->x2 < r1->x2)
	{
	  r2++;
	}
      else
	{
	  r1++;
	  r2++;
	}
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
586 587 588 589 590
/**
 * gdk_region_intersect:
 * @source1: a #GdkRegion
 * @source2: another #GdkRegion
 *
591 592 593
 * Sets the area of @source1 to the intersection of the areas of @source1
 * and @source2. The resulting area is the set of pixels contained in
 * both @source1 and @source2.
Havoc Pennington's avatar
Havoc Pennington committed
594
 **/
595
void
596 597
gdk_region_intersect (GdkRegion       *source1,
		      const GdkRegion *source2)
598
{
599 600
  g_return_if_fail (source1 != NULL);
  g_return_if_fail (source2 != NULL);
601
  
602
  /* check for trivial reject */
603 604 605
  if ((!(source1->numRects)) || (!(source2->numRects))  ||
      (!EXTENTCHECK(&source1->extents, &source2->extents)))
    source1->numRects = 0;
606
  else
607
    miRegionOp (source1, source1, source2,
608 609 610
    		miIntersectO, (nonOverlapFunc) NULL, (nonOverlapFunc) NULL);
    
  /*
611
   * Can't alter source1's extents before miRegionOp depends on the
612 613 614 615
   * extents of the regions being unchanged. Besides, this way there's
   * no checking against rectangles that will be nuked due to
   * coalescing, so we have to examine fewer rectangles.
   */
616
  miSetExtents(source1);
617 618 619
}

static void
620 621
miRegionCopy (GdkRegion       *dstrgn,
	      const GdkRegion *rgn)
622 623 624 625 626
{
  if (dstrgn != rgn) /*  don't want to copy to itself */
    {  
      if (dstrgn->size < rgn->numRects)
        {
627 628 629 630
	  if (dstrgn->rects != &dstrgn->extents)
	    g_free (dstrgn->rects);

	  dstrgn->rects = g_new (GdkRegionBox, rgn->numRects);
631 632
	  dstrgn->size = rgn->numRects;
	}
633

634
      dstrgn->numRects = rgn->numRects;
635
      dstrgn->extents = rgn->extents;
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

      memcpy (dstrgn->rects, rgn->rects, rgn->numRects * sizeof (GdkRegionBox));
    }
}


/*======================================================================
 *	    Generic Region Operator
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * miCoalesce --
 *	Attempt to merge the boxes in the current band with those in the
 *	previous one. Used only by miRegionOp.
 *
 * Results:
 *	The new index for the previous band.
 *
 * Side Effects:
 *	If coalescing takes place:
 *	    - rectangles in the previous band will have their y2 fields
 *	      altered.
 *	    - pReg->numRects will be decreased.
 *
 *-----------------------------------------------------------------------
 */
/* static int*/
static int
miCoalesce (GdkRegion *pReg,         /* Region to coalesce */
	    gint       prevStart,    /* Index of start of previous band */
	    gint       curStart)     /* Index of start of current band */
{
  GdkRegionBox *pPrevBox;   	/* Current box in previous band */
  GdkRegionBox *pCurBox;    	/* Current box in current band */
  GdkRegionBox *pRegEnd;    	/* End of region */
  int	    	curNumRects;	/* Number of rectangles in current
				 * band */
  int	    	prevNumRects;	/* Number of rectangles in previous
				 * band */
  int	    	bandY1;	    	/* Y1 coordinate for current band */

  pRegEnd = &pReg->rects[pReg->numRects];

  pPrevBox = &pReg->rects[prevStart];
  prevNumRects = curStart - prevStart;

    /*
     * Figure out how many rectangles are in the current band. Have to do
     * this because multiple bands could have been added in miRegionOp
     * at the end when one region has been exhausted.
     */
  pCurBox = &pReg->rects[curStart];
  bandY1 = pCurBox->y1;
  for (curNumRects = 0;
       (pCurBox != pRegEnd) && (pCurBox->y1 == bandY1);
       curNumRects++)
    {
      pCurBox++;
    }
    
  if (pCurBox != pRegEnd)
    {
      /*
       * If more than one band was added, we have to find the start
       * of the last band added so the next coalescing job can start
       * at the right place... (given when multiple bands are added,
       * this may be pointless -- see above).
       */
      pRegEnd--;
      while (pRegEnd[-1].y1 == pRegEnd->y1)
	{
	  pRegEnd--;
	}
      curStart = pRegEnd - pReg->rects;
      pRegEnd = pReg->rects + pReg->numRects;
    }
	
  if ((curNumRects == prevNumRects) && (curNumRects != 0)) {
    pCurBox -= curNumRects;
    /*
     * The bands may only be coalesced if the bottom of the previous
     * matches the top scanline of the current.
     */
    if (pPrevBox->y2 == pCurBox->y1)
      {
	/*
	 * Make sure the bands have boxes in the same places. This
	 * assumes that boxes have been added in such a way that they
	 * cover the most area possible. I.e. two boxes in a band must
	 * have some horizontal space between them.
	 */
	do
	  {
	    if ((pPrevBox->x1 != pCurBox->x1) ||
		(pPrevBox->x2 != pCurBox->x2))
	      {
		/*
		 * The bands don't line up so they can't be coalesced.
		 */
		return (curStart);
	      }
	    pPrevBox++;
	    pCurBox++;
	    prevNumRects -= 1;
	  } while (prevNumRects != 0);

	pReg->numRects -= curNumRects;
	pCurBox -= curNumRects;
	pPrevBox -= curNumRects;

	/*
	 * The bands may be merged, so set the bottom y of each box
	 * in the previous band to that of the corresponding box in
	 * the current band.
	 */
	do
	  {
	    pPrevBox->y2 = pCurBox->y2;
	    pPrevBox++;
	    pCurBox++;
	    curNumRects -= 1;
	  }
	while (curNumRects != 0);

	/*
	 * If only one band was added to the region, we have to backup
	 * curStart to the start of the previous band.
	 *
	 * If more than one band was added to the region, copy the
	 * other bands down. The assumption here is that the other bands
	 * came from the same region as the current one and no further
	 * coalescing can be done on them since it's all been done
	 * already... curStart is already in the right place.
	 */
	if (pCurBox == pRegEnd)
	  {
	    curStart = prevStart;
	  }
	else
	  {
	    do
	      {
		*pPrevBox++ = *pCurBox++;
	      }
	    while (pCurBox != pRegEnd);
	  }
	    
      }
  }
  return curStart;
}

/*-
 *-----------------------------------------------------------------------
 * miRegionOp --
 *	Apply an operation to two regions. Called by miUnion, miInverse,
 *	miSubtract, miIntersect...
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The new region is overwritten.
 *
 * Notes:
 *	The idea behind this function is to view the two regions as sets.
 *	Together they cover a rectangle of area that this function divides
 *	into horizontal bands where points are covered only by one region
 *	or by both. For the first case, the nonOverlapFunc is called with
 *	each the band and the band's upper and lower extents. For the
 *	second, the overlapFunc is called to process the entire band. It
 *	is responsible for clipping the rectangles in the band, though
 *	this function provides the boundaries.
 *	At the end of each band, the new region is coalesced, if possible,
 *	to reduce the number of rectangles in the region.
 *
 *-----------------------------------------------------------------------
 */
/* static void*/
static void
817 818 819 820
miRegionOp(GdkRegion       *newReg,
	   GdkRegion       *reg1,
	   const GdkRegion *reg2,
	   overlapFunc      overlapFn,          /* Function to call for over-
821
						 * lapping bands */
822
	   nonOverlapFunc   nonOverlap1Fn,	/* Function to call for non-
823 824
						 * overlapping bands in region
						 * 1 */
825
	   nonOverlapFunc   nonOverlap2Fn)	/* Function to call for non-
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
						 * overlapping bands in region
						 * 2 */
{
    GdkRegionBox *r1; 	    	    	/* Pointer into first region */
    GdkRegionBox *r2; 	    	    	/* Pointer into 2d region */
    GdkRegionBox *r1End;    	    	/* End of 1st region */
    GdkRegionBox *r2End;    	    	/* End of 2d region */
    int    	  ybot;	    	    	/* Bottom of intersection */
    int  	  ytop;	    	    	/* Top of intersection */
    GdkRegionBox *oldRects;   	    	/* Old rects for newReg */
    int	    	  prevBand;   	    	/* Index of start of
					 * previous band in newReg */
    int	  	  curBand;    	    	/* Index of start of current
					 * band in newReg */
    GdkRegionBox *r1BandEnd;  	    	/* End of current band in r1 */
    GdkRegionBox *r2BandEnd;  	    	/* End of current band in r2 */
    int     	  top;	    	    	/* Top of non-overlapping
					 * band */
    int     	  bot;	    	    	/* Bottom of non-overlapping
					 * band */
    
    /*
     * Initialization:
     *	set r1, r2, r1End and r2End appropriately, preserve the important
     * parts of the destination region until the end in case it's one of
     * the two source regions, then mark the "new" region empty, allocating
     * another array of rectangles for it to use.
     */
    r1 = reg1->rects;
    r2 = reg2->rects;
    r1End = r1 + reg1->numRects;
    r2End = r2 + reg2->numRects;
    
    oldRects = newReg->rects;
    
    EMPTY_REGION(newReg);

    /*
     * Allocate a reasonable number of rectangles for the new region. The idea
     * is to allocate enough so the individual functions don't need to
     * reallocate and copy the array, which is time consuming, yet we don't
     * have to worry about using too much memory. I hope to be able to
     * nuke the Xrealloc() at the end of this function eventually.
     */
    newReg->size = MAX (reg1->numRects, reg2->numRects) * 2;
    newReg->rects = g_new (GdkRegionBox, newReg->size);
    
    /*
     * Initialize ybot and ytop.
     * In the upcoming loop, ybot and ytop serve different functions depending
     * on whether the band being handled is an overlapping or non-overlapping
     * band.
     * 	In the case of a non-overlapping band (only one of the regions
     * has points in the band), ybot is the bottom of the most recent
     * intersection and thus clips the top of the rectangles in that band.
     * ytop is the top of the next intersection between the two regions and
     * serves to clip the bottom of the rectangles in the current band.
     *	For an overlapping band (where the two regions intersect), ytop clips
     * the top of the rectangles of both regions and ybot clips the bottoms.
     */
    if (reg1->extents.y1 < reg2->extents.y1)
      ybot = reg1->extents.y1;
    else
      ybot = reg2->extents.y1;
    
    /*
     * prevBand serves to mark the start of the previous band so rectangles
     * can be coalesced into larger rectangles. qv. miCoalesce, above.
     * In the beginning, there is no previous band, so prevBand == curBand
     * (curBand is set later on, of course, but the first band will always
     * start at index 0). prevBand and curBand must be indices because of
     * the possible expansion, and resultant moving, of the new region's
     * array of rectangles.
     */
    prevBand = 0;
    
    do
      {
	curBand = newReg->numRects;

	/*
	 * This algorithm proceeds one source-band (as opposed to a
	 * destination band, which is determined by where the two regions
	 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
	 * rectangle after the last one in the current band for their
	 * respective regions.
	 */
	r1BandEnd = r1;
	while ((r1BandEnd != r1End) && (r1BandEnd->y1 == r1->y1))
	  {
	    r1BandEnd++;
	  }
	
	r2BandEnd = r2;
	while ((r2BandEnd != r2End) && (r2BandEnd->y1 == r2->y1))
	  {
	    r2BandEnd++;
	  }
	
	/*
	 * First handle the band that doesn't intersect, if any.
	 *
	 * Note that attention is restricted to one band in the
	 * non-intersecting region at once, so if a region has n
	 * bands between the current position and the next place it overlaps
	 * the other, this entire loop will be passed through n times.
	 */
	if (r1->y1 < r2->y1)
	  {
	    top = MAX (r1->y1,ybot);
	    bot = MIN (r1->y2,r2->y1);

	    if ((top != bot) && (nonOverlap1Fn != (void (*)())NULL))
	      {
		(* nonOverlap1Fn) (newReg, r1, r1BandEnd, top, bot);
	      }

	    ytop = r2->y1;
	  }
	else if (r2->y1 < r1->y1)
	  {
	    top = MAX (r2->y1,ybot);
	    bot = MIN (r2->y2,r1->y1);

	    if ((top != bot) && (nonOverlap2Fn != (void (*)())NULL))
	      {
		(* nonOverlap2Fn) (newReg, r2, r2BandEnd, top, bot);
	      }

	    ytop = r1->y1;
	  }
	else
	  {
	    ytop = r1->y1;
	  }

	/*
	 * If any rectangles got added to the region, try and coalesce them
	 * with rectangles from the previous band. Note we could just do
	 * this test in miCoalesce, but some machines incur a not
	 * inconsiderable cost for function calls, so...
	 */
	if (newReg->numRects != curBand)
	  {
	    prevBand = miCoalesce (newReg, prevBand, curBand);
	  }

	/*
	 * Now see if we've hit an intersecting band. The two bands only
	 * intersect if ybot > ytop
	 */
	ybot = MIN (r1->y2, r2->y2);
	curBand = newReg->numRects;
	if (ybot > ytop)
	  {
	    (* overlapFn) (newReg, r1, r1BandEnd, r2, r2BandEnd, ytop, ybot);

	  }
	
	if (newReg->numRects != curBand)
	  {
	    prevBand = miCoalesce (newReg, prevBand, curBand);
	  }

	/*
	 * If we've finished with a band (y2 == ybot) we skip forward
	 * in the region to the next band.
	 */
	if (r1->y2 == ybot)
	  {
	    r1 = r1BandEnd;
	  }
	if (r2->y2 == ybot)
	  {
	    r2 = r2BandEnd;
	  }
      } while ((r1 != r1End) && (r2 != r2End));

    /*
     * Deal with whichever region still has rectangles left.
     */
    curBand = newReg->numRects;
    if (r1 != r1End)
      {
	if (nonOverlap1Fn != (nonOverlapFunc )NULL)
	  {
	    do
	      {
		r1BandEnd = r1;
		while ((r1BandEnd < r1End) && (r1BandEnd->y1 == r1->y1))
		  {
		    r1BandEnd++;
		  }
		(* nonOverlap1Fn) (newReg, r1, r1BandEnd,
				     MAX (r1->y1,ybot), r1->y2);
		r1 = r1BandEnd;
	      } while (r1 != r1End);
	  }
      }
    else if ((r2 != r2End) && (nonOverlap2Fn != (nonOverlapFunc) NULL))
      {
	do
	  {
	    r2BandEnd = r2;
	    while ((r2BandEnd < r2End) && (r2BandEnd->y1 == r2->y1))
	      {
		r2BandEnd++;
	      }
	    (* nonOverlap2Fn) (newReg, r2, r2BandEnd,
			       MAX (r2->y1,ybot), r2->y2);
	    r2 = r2BandEnd;
	  } while (r2 != r2End);
      }

    if (newReg->numRects != curBand)
    {
      (void) miCoalesce (newReg, prevBand, curBand);
    }

    /*
     * A bit of cleanup. To keep regions from growing without bound,
     * we shrink the array of rectangles to match the new number of
     * rectangles in the region. This never goes to 0, however...
     *
     * Only do this stuff if the number of rectangles allocated is more than
     * twice the number of rectangles in the region (a simple optimization...).
     */
    if (newReg->numRects < (newReg->size >> 1))
      {
	if (REGION_NOT_EMPTY (newReg))
	  {
	    newReg->size = newReg->numRects;
	    newReg->rects = g_renew (GdkRegionBox, newReg->rects, newReg->size);
	  }
	else
	  {
	    /*
	     * No point in doing the extra work involved in an Xrealloc if
	     * the region is empty
	     */
	    newReg->size = 1;
	    g_free (newReg->rects);
1068
	    newReg->rects = &newReg->extents;
1069 1070
	  }
      }
1071 1072 1073

    if (oldRects != &newReg->extents)
      g_free (oldRects);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
}


/*======================================================================
 *	    Region Union
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * miUnionNonO --
 *	Handle a non-overlapping band for the union operation. Just
 *	Adds the rectangles into the region. Doesn't have to check for
 *	subsumption or anything.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	pReg->numRects is incremented and the final rectangles overwritten
 *	with the rectangles we're passed.
 *
 *-----------------------------------------------------------------------
 */
static void
miUnionNonO (GdkRegion    *pReg,
	     GdkRegionBox *r,
	     GdkRegionBox *rEnd,
	     gint          y1,
	     gint          y2)
{
  GdkRegionBox *pNextRect;

  pNextRect = &pReg->rects[pReg->numRects];

1108
  g_assert(y1 < y2);
1109 1110 1111

  while (r != rEnd)
    {
1112
      g_assert(r->x1 < r->x2);
1113 1114 1115 1116 1117 1118 1119 1120
      MEMCHECK(pReg, pNextRect, pReg->rects);
      pNextRect->x1 = r->x1;
      pNextRect->y1 = y1;
      pNextRect->x2 = r->x2;
      pNextRect->y2 = y2;
      pReg->numRects += 1;
      pNextRect++;

1121
      g_assert(pReg->numRects<=pReg->size);
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
      r++;
    }
}


/*-
 *-----------------------------------------------------------------------
 * miUnionO --
 *	Handle an overlapping band for the union operation. Picks the
 *	left-most rectangle each time and merges it into the region.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	Rectangles are overwritten in pReg->rects and pReg->numRects will
 *	be changed.
 *
 *-----------------------------------------------------------------------
 */

/* static void*/
static void
miUnionO (GdkRegion *pReg,
	  GdkRegionBox *r1,
	  GdkRegionBox *r1End,
	  GdkRegionBox *r2,
	  GdkRegionBox *r2End,
	  gint          y1,
	  gint          y2)
{
  GdkRegionBox *	pNextRect;
    
  pNextRect = &pReg->rects[pReg->numRects];

#define MERGERECT(r) 					\
    if ((pReg->numRects != 0) &&  			\
	(pNextRect[-1].y1 == y1) &&  			\
	(pNextRect[-1].y2 == y2) &&  			\
	(pNextRect[-1].x2 >= r->x1))  			\
      {  						\
	if (pNextRect[-1].x2 < r->x2)  			\
	  {  						\
	    pNextRect[-1].x2 = r->x2;  			\
1166
	    g_assert(pNextRect[-1].x1<pNextRect[-1].x2); 	\
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	  }  						\
      }  						\
    else  						\
      {  						\
	MEMCHECK(pReg, pNextRect, pReg->rects); 	\
	pNextRect->y1 = y1;  				\
	pNextRect->y2 = y2;  				\
	pNextRect->x1 = r->x1;  			\
	pNextRect->x2 = r->x2;  			\
	pReg->numRects += 1;  				\
        pNextRect += 1;  				\
      }  						\
1179
    g_assert(pReg->numRects<=pReg->size);			\
1180 1181
    r++;
    
1182
    g_assert (y1<y2);
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    while ((r1 != r1End) && (r2 != r2End))
    {
	if (r1->x1 < r2->x1)
	{
	    MERGERECT(r1);
	}
	else
	{
	    MERGERECT(r2);
	}
    }
    
    if (r1 != r1End)
    {
	do
	{
	    MERGERECT(r1);
	} while (r1 != r1End);
    }
    else while (r2 != r2End)
    {
	MERGERECT(r2);
    }
}

1208 1209 1210 1211 1212 1213 1214 1215 1216
/**
 * gdk_region_union:
 * @source1:  a #GdkRegion
 * @source2: a #GdkRegion 
 * 
 * Sets the area of @source1 to the union of the areas of @source1 and
 * @source2. The resulting area is the set of pixels contained in
 * either @source1 or @source2.
 **/
1217
void
1218 1219
gdk_region_union (GdkRegion       *source1,
		  const GdkRegion *source2)
1220
{
1221 1222
  g_return_if_fail (source1 != NULL);
  g_return_if_fail (source2 != NULL);
1223
  
1224 1225
  /*  checks all the simple cases */

1226 1227 1228 1229
  /*
   * source1 and source2 are the same or source2 is empty
   */
  if ((source1 == source2) || (!(source2->numRects)))
1230 1231
    return;

1232 1233 1234 1235
  /* 
   * source1 is empty
   */
  if (!(source1->numRects))
1236
    {
1237
      miRegionCopy (source1, source2);
1238 1239
      return;
    }
1240
  
1241
  /*
1242 1243 1244 1245 1246 1247 1248
   * source1 completely subsumes source2
   */
  if ((source1->numRects == 1) && 
      (source1->extents.x1 <= source2->extents.x1) &&
      (source1->extents.y1 <= source2->extents.y1) &&
      (source1->extents.x2 >= source2->extents.x2) &&
      (source1->extents.y2 >= source2->extents.y2))
1249 1250 1251
    return;

  /*
1252 1253 1254 1255 1256 1257 1258
   * source2 completely subsumes source1
   */
  if ((source2->numRects == 1) && 
      (source2->extents.x1 <= source1->extents.x1) &&
      (source2->extents.y1 <= source1->extents.y1) &&
      (source2->extents.x2 >= source1->extents.x2) &&
      (source2->extents.y2 >= source1->extents.y2))
1259
    {
1260
      miRegionCopy(source1, source2);
1261 1262 1263
      return;
    }

1264
  miRegionOp (source1, source1, source2, miUnionO, 
1265 1266
	      miUnionNonO, miUnionNonO);

1267 1268 1269 1270
  source1->extents.x1 = MIN (source1->extents.x1, source2->extents.x1);
  source1->extents.y1 = MIN (source1->extents.y1, source2->extents.y1);
  source1->extents.x2 = MAX (source1->extents.x2, source2->extents.x2);
  source1->extents.y2 = MAX (source1->extents.y2, source2->extents.y2);
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
}


/*======================================================================
 * 	    	  Region Subtraction
 *====================================================================*/

/*-
 *-----------------------------------------------------------------------
 * miSubtractNonO --
 *	Deal with non-overlapping band for subtraction. Any parts from
 *	region 2 we discard. Anything from region 1 we add to the region.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	pReg may be affected.
 *
 *-----------------------------------------------------------------------
 */
/* static void*/
static void
miSubtractNonO1 (GdkRegion    *pReg,
		 GdkRegionBox *r,
		 GdkRegionBox *rEnd,
		 gint          y1,
		 gint          y2)
{
  GdkRegionBox *	pNextRect;
	
  pNextRect = &pReg->rects[pReg->numRects];
	
1304
  g_assert(y1<y2);
1305 1306 1307

  while (r != rEnd)
    {
1308
      g_assert (r->x1<r->x2);
1309 1310 1311 1312 1313 1314 1315 1316
      MEMCHECK (pReg, pNextRect, pReg->rects);
      pNextRect->x1 = r->x1;
      pNextRect->y1 = y1;
      pNextRect->x2 = r->x2;
      pNextRect->y2 = y2;
      pReg->numRects += 1;
      pNextRect++;

1317
      g_assert (pReg->numRects <= pReg->size);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

      r++;
    }
}

/*-
 *-----------------------------------------------------------------------
 * miSubtractO --
 *	Overlapping band subtraction. x1 is the left-most point not yet
 *	checked.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	pReg may have rectangles added to it.
 *
 *-----------------------------------------------------------------------
 */
/* static void*/
static void
miSubtractO (GdkRegion    *pReg,
	     GdkRegionBox *r1,
	     GdkRegionBox *r1End,
	     GdkRegionBox *r2,
	     GdkRegionBox *r2End,
	     gint          y1,
	     gint          y2)
{
  GdkRegionBox *	pNextRect;
  int  	x1;
    
  x1 = r1->x1;
    
1352
  g_assert(y1<y2);
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
  pNextRect = &pReg->rects[pReg->numRects];

  while ((r1 != r1End) && (r2 != r2End))
    {
      if (r2->x2 <= x1)
	{
	  /*
	   * Subtrahend missed the boat: go to next subtrahend.
	   */
	  r2++;
	}
      else if (r2->x1 <= x1)
	{
	  /*
	   * Subtrahend preceeds minuend: nuke left edge of minuend.
	   */
	  x1 = r2->x2;
	  if (x1 >= r1->x2)
	    {
	      /*
	       * Minuend completely covered: advance to next minuend and
	       * reset left fence to edge of new minuend.
	       */
	      r1++;
	      if (r1 != r1End)
		x1 = r1->x1;
	    }
	  else
	    {
	      /*
	       * Subtrahend now used up since it doesn't extend beyond
	       * minuend
	       */
	      r2++;
	    }
	}
      else if (r2->x1 < r1->x2)
	{
	  /*
	   * Left part of subtrahend covers part of minuend: add uncovered
	   * part of minuend to region and skip to next subtrahend.
	   */
1395
	  g_assert(x1<r2->x1);
1396 1397 1398 1399 1400 1401 1402 1403
	  MEMCHECK(pReg, pNextRect, pReg->rects);
	  pNextRect->x1 = x1;
	  pNextRect->y1 = y1;
	  pNextRect->x2 = r2->x1;
	  pNextRect->y2 = y2;
	  pReg->numRects += 1;
	  pNextRect++;

1404
	  g_assert(pReg->numRects<=pReg->size);
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

	  x1 = r2->x2;
	  if (x1 >= r1->x2)
	    {
	      /*
	       * Minuend used up: advance to new...
	       */
	      r1++;
	      if (r1 != r1End)
		x1 = r1->x1;
	    }
	  else
	    {
	      /*
	       * Subtrahend used up
	       */
	      r2++;
	    }
	}
      else
	{
	  /*
	   * Minuend used up: add any remaining piece before advancing.
	   */
	  if (r1->x2 > x1)
	    {
	      MEMCHECK(pReg, pNextRect, pReg->rects);
	      pNextRect->x1 = x1;
	      pNextRect->y1 = y1;
	      pNextRect->x2 = r1->x2;
	      pNextRect->y2 = y2;
	      pReg->numRects += 1;
	      pNextRect++;
1438
	      g_assert(pReg->numRects<=pReg->size);
1439 1440
	    }
	  r1++;
Owen Taylor's avatar
Owen Taylor committed
1441 1442
	  if (r1 != r1End)
	    x1 = r1->x1;
1443 1444 1445 1446 1447 1448 1449 1450
	}
    }

  /*
     * Add remaining minuend rectangles to region.
     */
  while (r1 != r1End)
    {
1451
      g_assert(x1<r1->x2);
1452 1453 1454 1455 1456 1457 1458 1459
      MEMCHECK(pReg, pNextRect, pReg->rects);
      pNextRect->x1 = x1;
      pNextRect->y1 = y1;
      pNextRect->x2 = r1->x2;
      pNextRect->y2 = y2;
      pReg->numRects += 1;
      pNextRect++;

1460
      g_assert(pReg->numRects<=pReg->size);
1461 1462 1463 1464 1465 1466 1467 1468 1469

      r1++;
      if (r1 != r1End)
	{
	  x1 = r1->x1;
	}
    }
}

Havoc Pennington's avatar
Havoc Pennington committed
1470 1471 1472 1473 1474
/**
 * gdk_region_subtract:
 * @source1: a #GdkRegion
 * @source2: another #GdkRegion
 *
1475 1476
 * Subtracts the area of @source2 from the area @source1. The resulting
 * area is the set of pixels contained in @source1 but not in @source2.
Havoc Pennington's avatar
Havoc Pennington committed
1477
 **/
1478
void
1479 1480
gdk_region_subtract (GdkRegion       *source1,
		     const GdkRegion *source2)
1481
{
1482 1483
  g_return_if_fail (source1 != NULL);
  g_return_if_fail (source2 != NULL);
1484
  
1485
  /* check for trivial reject */
1486 1487
  if ((!(source1->numRects)) || (!(source2->numRects)) ||
      (!EXTENTCHECK(&source1->extents, &source2->extents)))
1488 1489
    return;
 
1490
  miRegionOp (source1, source1, source2, miSubtractO,
1491 1492 1493
	      miSubtractNonO1, (nonOverlapFunc) NULL);

  /*
1494
   * Can't alter source1's extents before we call miRegionOp because miRegionOp
1495 1496 1497 1498
   * depends on the extents of those regions being the unaltered. Besides, this
   * way there's no checking against rectangles that will be nuked
   * due to coalescing, so we have to examine fewer rectangles.
   */
1499
  miSetExtents (source1);
1500 1501
}

Havoc Pennington's avatar
Havoc Pennington committed
1502 1503 1504 1505 1506
/**
 * gdk_region_xor:
 * @source1: a #GdkRegion
 * @source2: another #GdkRegion
 *
1507 1508 1509
 * Sets the area of @source1 to the exclusive-OR of the areas of @source1
 * and @source2. The resulting area is the set of pixels contained in one
 * or the other of the two sources but not in both.
Havoc Pennington's avatar
Havoc Pennington committed
1510
 **/
1511
void
1512 1513
gdk_region_xor (GdkRegion       *source1,
		const GdkRegion *source2)
1514 1515 1516
{
  GdkRegion *trb;

1517 1518
  g_return_if_fail (source1 != NULL);
  g_return_if_fail (source2 != NULL);
1519

1520
  trb = gdk_region_copy (source2);