📄 region.c
字号:
/* $Xorg: Region.c,v 1.6 2001/02/09 02:03:35 xorgcvs Exp $ */
/************************************************************************
Copyright 1987, 1988, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
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.8 2001/12/14 19:54:05 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...
*/
#include "Xregion.h"
//#include "Xlibint.h"
//#include "Xutil.h"
#include "region.h"
//#include "poly.h"
#ifdef DEBUG
#include <stdio.h>
#define assert(expr) {if (!(expr)) fprintf(stderr,\
"Assertion failed file %s, line %d: expr\n", __FILE__, __LINE__); }
#else
#define assert(expr)
#endif
typedef void (*voidProcp)();
static void miRegionOp();
/* Create a new empty region */
Region
XCreateRegion()
{
Region temp;
if (! (temp = ( Region )Xmalloc( (unsigned) sizeof( REGION ))))
return (Region) NULL;
if (! (temp->rects = ( BOX * )Xmalloc( (unsigned) sizeof( BOX )))) {
Xfree((char *) temp);
return (Region) NULL;
}
temp->numRects = 0;
temp->extents.x1 = 0;
temp->extents.y1 = 0;
temp->extents.x2 = 0;
temp->extents.y2 = 0;
temp->size = 1;
return( temp );
}
int
XClipBox( r, rect )
Region r;
XRectangle *rect;
{
rect->x = r->extents.x1;
rect->y = r->extents.y1;
rect->width = r->extents.x2 - r->extents.x1;
rect->height = r->extents.y2 - r->extents.y1;
return 1;
}
int
XUnionRectWithRegion(rect, source, dest)
register XRectangle *rect;
Region source, dest;
{
REGION region;
if (!rect->width || !rect->height)
return 0;
region.rects = ®ion.extents;
region.numRects = 1;
region.extents.x1 = rect->x;
region.extents.y1 = rect->y;
region.extents.x2 = rect->x + rect->width;
region.extents.y2 = rect->y + rect->height;
region.size = 1;
return XUnionRegion(®ion, source, dest);
}
/*-
*-----------------------------------------------------------------------
* 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 (pReg)
Region pReg;
{
register BoxPtr pBox,
pBoxEnd,
pExtents;
if (pReg->numRects == 0)
{
pReg->extents.x1 = 0;
pReg->extents.y1 = 0;
pReg->extents.x2 = 0;
pReg->extents.y2 = 0;
return;
}
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;
assert(pExtents->y1 < pExtents->y2);
while (pBox <= pBoxEnd)
{
if (pBox->x1 < pExtents->x1)
{
pExtents->x1 = pBox->x1;
}
if (pBox->x2 > pExtents->x2)
{
pExtents->x2 = pBox->x2;
}
pBox++;
}
assert(pExtents->x1 < pExtents->x2);
}
extern void _XSetClipRectangles();
#if 0
int
XSetRegion( dpy, gc, r )
Display *dpy;
GC gc;
register Region r;
{
register int i;
register XRectangle *xr, *pr;
register BOX *pb;
unsigned long total;
LockDisplay (dpy);
total = r->numRects * sizeof (XRectangle);
if ((xr = (XRectangle *) _XAllocTemp(dpy, total))) {
for (pr = xr, pb = r->rects, i = r->numRects; --i >= 0; pr++, pb++) {
pr->x = pb->x1;
pr->y = pb->y1;
pr->width = pb->x2 - pb->x1;
pr->height = pb->y2 - pb->y1;
}
}
if (xr || !r->numRects)
_XSetClipRectangles(dpy, gc, 0, 0, xr, r->numRects, YXBanded);
if (xr)
_XFreeTemp(dpy, (char *)xr, total);
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
#endif
int
XDestroyRegion( r )
Region r;
{
Xfree( (char *) r->rects );
Xfree( (char *) r );
return 1;
}
/* TranslateRegion(pRegion, x, y)
translates in place
added by raymond
*/
int
XOffsetRegion(pRegion, x, y)
register Region pRegion;
register int x;
register int y;
{
register int nbox;
register BOX *pbox;
pbox = pRegion->rects;
nbox = pRegion->numRects;
while(nbox--)
{
pbox->x1 += x;
pbox->x2 += x;
pbox->y1 += y;
pbox->y2 += y;
pbox++;
}
pRegion->extents.x1 += x;
pRegion->extents.x2 += x;
pRegion->extents.y1 += y;
pRegion->extents.y2 += y;
return 1;
}
/*
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,c) if (grow) XUnionRegion(a,b,c); \
else XIntersectRegion(a,b,c)
#define ZShiftRegion(a,b) if (xdir) XOffsetRegion(a,b,0); \
else XOffsetRegion(a,0,b)
#define ZCopyRegion(a,b) XUnionRegion(a,a,b)
static void
Compress(r, s, t, dx, xdir, grow)
Region r, s, t;
register unsigned dx;
register int xdir, grow;
{
register unsigned shift = 1;
ZCopyRegion(r, s);
while (dx) {
if (dx & shift) {
ZShiftRegion(r, -(int)shift);
ZOpRegion(r, s, r);
dx -= shift;
if (!dx) break;
}
ZCopyRegion(s, t);
ZShiftRegion(s, -(int)shift);
ZOpRegion(s, t, s);
shift <<= 1;
}
}
#undef ZOpRegion
#undef ZShiftRegion
#undef ZCopyRegion
int
XShrinkRegion(r, dx, dy)
Region r;
int dx, dy;
{
Region s, t;
int grow;
if (!dx && !dy) return 0;
if ((! (s = XCreateRegion())) || (! (t = XCreateRegion()))) return 0;
if ((grow = (dx < 0))) dx = -dx;
if (dx) Compress(r, s, t, (unsigned) 2*dx, TRUE, grow);
if ((grow = (dy < 0))) dy = -dy;
if (dy) Compress(r, s, t, (unsigned) 2*dy, FALSE, grow);
XOffsetRegion(r, dx, dy);
XDestroyRegion(s);
XDestroyRegion(t);
return 0;
}
#ifdef notdef
/***********************************************************
* Bop down the array of rects until we have passed
* scanline y. numRects is the size of the array.
***********************************************************/
static BOX
*IndexRects(rects, numRects, y)
register BOX *rects;
register int numRects;
register int y;
{
while ((numRects--) && (rects->y2 <= y))
rects++;
return(rects);
}
#endif
/*======================================================================
* Region Intersection
*====================================================================*/
/*-
*-----------------------------------------------------------------------
* miIntersectO --
* Handle an overlapping band for miIntersect.
*
* Results:
* None.
*
* Side Effects:
* Rectangles may be added to the region.
*
*-----------------------------------------------------------------------
*/
/* static void*/
static int
miIntersectO (pReg, r1, r1End, r2, r2End, y1, y2)
register Region pReg;
register BoxPtr r1;
BoxPtr r1End;
register BoxPtr r2;
BoxPtr r2End;
short y1;
short y2;
{
register short x1;
register short x2;
register BoxPtr 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)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -