📄 region.c
字号:
/* $Xorg: Region.c,v 1.6 2001/02/09 02:03:35 xorgcvs Exp $ *//************************************************************************Copyright 1987, 1988, 1998 The Open GroupPermission to use, copy, modify, distribute, and sell this software and itsdocumentation for any purpose is hereby granted without fee, provided thatthe above copyright notice appear in all copies and that both thatcopyright notice and this permission notice appear in supportingdocumentation.The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION 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 beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from The Open Group.Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. All Rights ReservedPermission 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 thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of Digital not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY 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 THISSOFTWARE.************************************************************************//* $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 "Xlibint.h"#include "Xregion.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)#endiftypedef void (*voidProcp)();static void miRegionOp();/* Create a new empty region */XRegionXCreateRegion(){ XRegion temp; if (! (temp = ( XRegion )Xmalloc( (unsigned) sizeof( REGION )))) return (XRegion) NULL; if (! (temp->rects = ( BOX * )Xmalloc( (unsigned) sizeof( BOX )))) { Xfree((char *) temp); return (XRegion) 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 );}intXClipBox( r, rect ) XRegion 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;}intXUnionRectWithRegion(rect, source, dest) register XRectangle *rect; XRegion 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 voidmiSetExtents (pReg) XRegion 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();intXDestroyRegion( r ) XRegion r;{ Xfree( (char *) r->rects ); Xfree( (char *) r ); return 1;}/* TranslateRegion(pRegion, x, y) translates in place added by raymond*/intXOffsetRegion(pRegion, x, y) register XRegion 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 voidCompress(r, s, t, dx, xdir, grow) XRegion 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 ZCopyRegionintXShrinkRegion(r, dx, dy) XRegion r; int dx, dy;{ XRegion 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 intmiIntersectO (pReg, r1, r1End, r2, r2End, y1, y2) register XRegion 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) { assert(y1<y2); MEMCHECK(pReg, pNextRect, pReg->rects); pNextRect->x1 = x1; pNextRect->y1 = y1; pNextRect->x2 = x2; pNextRect->y2 = y2; pReg->numRects += 1; pNextRect++; assert(pReg->numRects <= pReg->size); } /* * 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++; } } return 0; /* lint */}intXIntersectRegion(reg1, reg2, newReg) XRegion reg1; XRegion reg2; /* source regions */ register XRegion newReg; /* destination Region */{ /* check for trivial reject */ if ( (!(reg1->numRects)) || (!(reg2->numRects)) || (!EXTENTCHECK(®1->extents, ®2->extents))) newReg->numRects = 0; else miRegionOp (newReg, reg1, reg2, (voidProcp) miIntersectO, (voidProcp) NULL, (voidProcp) NULL); /* * Can't alter newReg's extents before we call miRegionOp because * it might be one of the source regions and miRegionOp depends * on the extents of those regions being the same. Besides, this * way there's no checking against rectangles that will be nuked * due to coalescing, so we have to examine fewer rectangles. */ miSetExtents(newReg); return 1;}static voidmiRegionCopy(dstrgn, rgn) register XRegion dstrgn; register XRegion rgn;{ if (dstrgn != rgn) /* don't want to copy to itself */ { if (dstrgn->size < rgn->numRects) { if (dstrgn->rects) { BOX *prevRects = dstrgn->rects; if (! (dstrgn->rects = (BOX *) Xrealloc((char *) dstrgn->rects, (unsigned) rgn->numRects * (sizeof(BOX))))) { Xfree(prevRects); return; } } dstrgn->size = rgn->numRects; } dstrgn->numRects = rgn->numRects; dstrgn->extents.x1 = rgn->extents.x1; dstrgn->extents.y1 = rgn->extents.y1; dstrgn->extents.x2 = rgn->extents.x2; dstrgn->extents.y2 = rgn->extents.y2; memcpy((char *) dstrgn->rects, (char *) rgn->rects, (int) (rgn->numRects * sizeof(BOX))); }}#ifdef notdef/* * combinRegs(newReg, reg1, reg2) * if one region is above or below the other.*/ static voidcombineRegs(newReg, reg1, reg2) register XRegion newReg; XRegion reg1; XRegion reg2;{ register XRegion tempReg; register BOX *rects; register BOX *rects1; register BOX *rects2; register int total; rects1 = reg1->rects; rects2 = reg2->rects; total = reg1->numRects + reg2->numRects; if (! (tempReg = XCreateRegion())) return; tempReg->size = total; /* region 1 is below region 2 */ if (reg1->extents.y1 > reg2->extents.y1) { miRegionCopy(tempReg, reg2); rects = &tempReg->rects[tempReg->numRects]; total -= tempReg->numRects; while (total--) *rects++ = *rects1++; } else { miRegionCopy(tempReg, reg1); rects = &tempReg->rects[tempReg->numRects]; total -= tempReg->numRects; while (total--) *rects++ = *rects2++; } tempReg->extents = reg1->extents; tempReg->numRects = reg1->numRects + reg2->numRects; EXTENTS(®2->extents, tempReg); miRegionCopy(newReg, tempReg); Xfree((char *)tempReg);}/* * QuickCheck checks to see if it does not have to go through all the * the ugly code for the region call. It returns 1 if it did all * the work for Union, otherwise 0 - still work to be done.*/ static intQuickCheck(newReg, reg1, reg2) XRegion newReg, reg1, reg2;{ /* if unioning with itself or no rects to union with */ if ( (reg1 == reg2) || (!(reg1->numRects)) ) { miRegionCopy(newReg, reg2); return TRUE; } /* if nothing to union */ if (!(reg2->numRects))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -