📄 region.c
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: region.c,v 1.3.12.1 2004/07/09 01:59:28 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer and/or licensor of the Original Code and owns the * copyrights in the portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//************************************************************************Copyright (c) 1987, 1988 X ConsortiumPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: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 THEX CONSORTIUM 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 X Consortium shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from the X Consortium.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.************************************************************************//* * 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... *//* $XFree86: xc/lib/X11/_Region.c,v 1.1.1.2.2.2 1998/10/04 15:22:50 hohndel Exp $ */#include "region.h"#include "hlxclib/stdlib.h"#include "hlxclib/memory.h"#ifdef _DEBUG#include <hlxclib/stdio.h> /* for _DumpRegion() */#ifdef _WINDOWS#include "windows.h"#endif#endif#include <hlxclib/string.h>typedef void (*voidProcp)(); typedef int (*overlapFunc)( register _HXRegion pReg, register HXBoxPtr r1, HXBoxPtr r1End, register HXBoxPtr r2, HXBoxPtr r2End, register short y1, register short y2 );typedef int (*nonOverlapFunc)( register _HXRegion pReg, register HXBoxPtr r, HXBoxPtr rEnd, register short y1, register short y2 );static void miRegionOp( _HXRegion newReg, _HXRegion reg1, _HXRegion reg2, overlapFunc overlap, nonOverlapFunc nonoverlap, nonOverlapFunc nonoverlap2 );/* Create a new empty region */ _HXRegion HXCreateRegion(){ _HXRegion temp; if (! (temp = ( _HXRegion )malloc( (unsigned) sizeof( HXREGION )))) return (_HXRegion) NULL; if (! (temp->rects = ( HXBOX * )malloc( (unsigned) sizeof( HXBOX )))) { free((char *) temp); return (_HXRegion) 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 HXClipRBox( _HXRegion r, HXRECTANGLE* 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;}_HXRegion HXCreateRectRegion(int left, int top, int width, int height){ _HXRegion pRegion = HXCreateRegion(); /* too scared to put this code in. Will have to check to see if everything works with it later. pRegion->numRects++; temp->rects.x1 = pRegion->extents.x1 = left; temp->rects.y1 = pRegion->extents.y1 = top; temp->rects.x2 = pRegion->extents.x2 = right; temp->rects.y2 = pRegion->extents.y2 = bottom; */ HXRECTANGLE tempRECT; tempRECT.x = left; tempRECT.y = top; tempRECT.width = width; tempRECT.height = height; HXUnionRectWithRegion(&tempRECT, pRegion, pRegion); return pRegion;}//=======================================================================//=======================================================================// New methods to act on regions//=======================================================================//=======================================================================int HXCombineRgn( _HXRegion destRgn, _HXRegion srcRgn1, _HXRegion srcRgn2, HXRegionArithmetic rgnOperation){// HX_ASSERT( destRgn && srcRgn1 && srcRgn2); switch (rgnOperation) { case HX_RGN_DIFF: HXSubtractRegion( srcRgn1, srcRgn2, destRgn ); break; case HX_RGN_AND: HXIntersectRegion( srcRgn1, srcRgn2, destRgn ); break; case HX_RGN_OR: HXUnionRegion(srcRgn1, srcRgn2, destRgn ); break; case HX_RGN_XOR: HXXorRegion( srcRgn1, srcRgn2, destRgn ); break; default:// HX_ASSERT(!"Egads! Unknown HXCombineRgn Operation!"); break; } return 0;}//=======================================================================//=======================================================================// END: New methods to act on regions//=======================================================================//=======================================================================int HXUnionRectWithRegion(HXRECTANGLE* rect, _HXRegion source, _HXRegion dest){ HXREGION 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 HXUnionRegion(®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 (_HXRegion pReg){ register HXBoxPtr pHXBox, pHXBoxEnd, 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; pHXBox = pReg->rects; pHXBoxEnd = &pHXBox[pReg->numRects - 1]; /* * Since pHXBox is the first rectangle in the region, it must have the * smallest y1 and since pHXBoxEnd is the last rectangle in the region, * it must have the largest y2, because of banding. Initialize x1 and * x2 from pHXBox and pHXBoxEnd, resp., as good things to initialize them * to... */ pExtents->x1 = pHXBox->x1; pExtents->y1 = pHXBox->y1; pExtents->x2 = pHXBoxEnd->x2; pExtents->y2 = pHXBoxEnd->y2; /* assert(pExtents->y1 < pExtents->y2); */ while (pHXBox <= pHXBoxEnd) { if (pHXBox->x1 < pExtents->x1) { pExtents->x1 = pHXBox->x1; } if (pHXBox->x2 > pExtents->x2) { pExtents->x2 = pHXBox->x2; } pHXBox++; } /* assert(pExtents->x1 < pExtents->x2); */}int HXZeroOutRegion(_HXRegion r){ if (!r) { return 0; } if( r->rects ) free( (char *) r->rects ); if( !(r->rects = ( HXBOX * )malloc( (unsigned) sizeof( HXBOX ))) ) { return 0; } r->numRects = 0; r->extents.x1 = 0; r->extents.y1 = 0; r->extents.x2 = 0; r->extents.y2 = 0; r->size = 1; return 1;}int HXDestroyRegion(_HXRegion r){ if (!r) { return 0; } free( (char *) r->rects ); free( (char *) r ); return 1;}/* TranslateRegion(pRegion, x, y) translates in place added by raymond*/int HXOffsetRegion(_HXRegion pRegion, int x, int y){ register int nHXBox; register HXBOX *pHXBox; pHXBox = pRegion->rects; nHXBox = pRegion->numRects; while(nHXBox--) { pHXBox->x1 += x; pHXBox->x2 += x; pHXBox->y1 += y; pHXBox->y2 += y; pHXBox++; } 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) HXUnionRegion(a,b,c); \ else HXIntersectRegion(a,b,c)#define ZShiftRegion(a,b) if (xdir) HXOffsetRegion(a,b,0); \ else HXOffsetRegion(a,0,b)#define ZCopyRegion(a,b) HXUnionRegion(a,a,b)static voidCompress(_HXRegion r, _HXRegion s, _HXRegion t, unsigned dx, int xdir, int 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; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -