?? region.c
字號:
/*** $Id: region.c,v 1.17 2003/09/04 06:02:53 weiym Exp $**** region.c: Device-independent multi-rectangle clipping routines.** ** Copyright (C) 2003 Feynman Software** Copyright (C) 2001 ~ 2002 Wei Yongming.**** GDI region objects. Shamelessly ripped out from the X11 distribution** Thanks for the nice licence.**** Copyright 1993, 1994, 1995 Alexandre Julliard** Modifications and additions: Copyright 1998 Huw Davies**** Current maintainer: Wei Yongming.**** Create date: 2001/10/17**** Derived from old file: ../kernel/cliprect.c*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//************************************************************************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... *//* * Modify records: * * Who When Where For What Status *----------------------------------------------------------------------------- * * TODO: */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include "common.h"#include "gdi.h"#include "cliprect.h"#include "gal.h"#include "blockheap.h"typedef void (*voidProcp1)(CLIPRGN *region, const CLIPRECT *r1, const CLIPRECT *r1End, const CLIPRECT *r2, const CLIPRECT *r2End, int top, int bottom);typedef void (*voidProcp2)(CLIPRGN *region, const CLIPRECT *r, const CLIPRECT *rEnd, int top, int bottom);/* 1 if two RECTs overlap. * 0 if two RECTs do not overlap. */#define EXTENTCHECK(r1, r2) \ ((r1)->right > (r2)->left && \ (r1)->left < (r2)->right && \ (r1)->bottom > (r2)->top && \ (r1)->top < (r2)->bottom)/* * Allocate a new clipping rect and add it to the region. */#define NEWCLIPRECT(region, rect) \ {\ rect = ClipRectAlloc(region->heap);\ rect->next = NULL;\ rect->prev = region->tail;\ if (region->tail)\ region->tail->next = rect;\ region->tail = rect;\ if (region->head == NULL)\ region->head = rect;\ }#define REGION_NOT_EMPTY(region) region->head#define INRECT(r, x, y) \ ( ( ((r).right > x)) && \ ( ((r).left <= x)) && \ ( ((r).bottom > y)) && \ ( ((r).top <= y)) )/* return TRUE if point is in region*/BOOL GUIAPI PtInRegion (PCLIPRGN region, int x, int y){ int top; PCLIPRECT cliprect = region->head; /* check with bounding rect of clipping region */ if (y >= region->tail->rc.bottom || y < region->head->rc.top) return FALSE; /* find the ban in which this point lies */ cliprect = region->head; while (cliprect && y >= cliprect->rc.bottom) { cliprect = cliprect->next; } if (!cliprect) return FALSE; /* check in this ban */ top = cliprect->rc.top; while (cliprect && cliprect->rc.top == top) { if (INRECT (cliprect->rc, x, y)) return TRUE; cliprect = cliprect->next; } return FALSE;}/* Returns TRUE if rect is at least partly inside region */BOOL GUIAPI RectInRegion (PCLIPRGN region, const RECT* rect){ PCLIPRECT cliprect = region->head; BOOL ret = FALSE; if (cliprect && EXTENTCHECK (®ion->rcBound, rect)) { while (cliprect) { if (cliprect->rc.bottom <= rect->top) { cliprect = cliprect->next; continue; /* not far enough down yet */ } if (cliprect->rc.top >= rect->bottom) { ret = FALSE; /* too far down */ break; } if (cliprect->rc.right <= rect->left) { cliprect = cliprect->next; continue; /* not far enough over yet */ } if (cliprect->rc.left >= rect->right) { cliprect = cliprect->next; continue; } ret = TRUE; break; } } return ret;}/* Init a region */void GUIAPI InitClipRgn (PCLIPRGN pRgn, PBLOCKHEAP heap){ pRgn->type = NULLREGION; SetRectEmpty (&pRgn->rcBound); pRgn->head = NULL; pRgn->tail = NULL; pRgn->heap = heap; // read-only field.}/* return bound rect of a region */void GUIAPI GetClipRgnBoundRect (PCLIPRGN pRgn, PRECT pRect){ *pRect = pRgn->rcBound;}BOOL GUIAPI IsEmptyClipRgn (const CLIPRGN* pRgn){ if (pRgn->head == NULL) return TRUE; return FALSE;}void GUIAPI EmptyClipRgn (PCLIPRGN pRgn){ PCLIPRECT pCRect, pTemp; pCRect = pRgn->head; while (pCRect) { pTemp = pCRect->next; FreeClipRect (pRgn->heap, pCRect); pCRect = pTemp; } pRgn->type = NULLREGION; SetRectEmpty (&pRgn->rcBound); pRgn->head = NULL; pRgn->tail = NULL;}/* Reset a region */BOOL GUIAPI SetClipRgn (PCLIPRGN pRgn, const RECT* pRect){ PCLIPRECT pClipRect; if (IsRectEmpty (pRect)) return FALSE; // empty rgn first EmptyClipRgn (pRgn); // get a new clip rect from free list pClipRect = ClipRectAlloc (pRgn->heap); if (pClipRect == NULL) return FALSE; // set clip rect pClipRect->rc = *pRect; pClipRect->next = NULL; pClipRect->prev = NULL; pRgn->type = SIMPLEREGION; pRgn->head = pRgn->tail = pClipRect; pRgn->rcBound = *pRect; return TRUE;} BOOL GUIAPI ClipRgnCopy (PCLIPRGN pDstRgn, const CLIPRGN* pSrcRgn){ PCLIPRECT pcr; PCLIPRECT pnewcr, prev; // return false if the destination region is not an empty one. if (pDstRgn == pSrcRgn) return FALSE; EmptyClipRgn (pDstRgn); if (!(pcr = pSrcRgn->head)) return TRUE; pnewcr = ClipRectAlloc (pDstRgn->heap); pDstRgn->head = pnewcr; pnewcr->rc = pcr->rc; prev = NULL; while (pcr->next) { pnewcr->next = ClipRectAlloc (pDstRgn->heap); pnewcr->prev = prev; prev = pnewcr; pcr = pcr->next; pnewcr = pnewcr->next; pnewcr->rc = pcr->rc; } pnewcr->prev = prev; pnewcr->next = NULL; pDstRgn->tail = pnewcr; pDstRgn->type = pSrcRgn->type; pDstRgn->rcBound = pSrcRgn->rcBound; return TRUE;}/* Re-calculate the rcBound of a region */static void REGION_SetExtents (CLIPRGN *region){ PCLIPRECT cliprect; RECT *pExtents; if (region->head == NULL) { region->rcBound.left = 0; region->rcBound.top = 0; region->rcBound.right = 0; region->rcBound.bottom = 0; return; } pExtents = ®ion->rcBound; /* * Since head is the first rectangle in the region, it must have the * smallest top and since tail is the last rectangle in the region, * it must have the largest bottom, because of banding. Initialize left and * right from head and tail, resp., as good things to initialize them * to... */ pExtents->left = region->head->rc.left; pExtents->top = region->head->rc.top; pExtents->right = region->tail->rc.right; pExtents->bottom = region->tail->rc.bottom; cliprect = region->head; while (cliprect) { if (cliprect->rc.left < pExtents->left) pExtents->left = cliprect->rc.left; if (cliprect->rc.right > pExtents->right) pExtents->right = cliprect->rc.right; cliprect = cliprect->next; }}#ifdef _REGION_DEBUGvoid dumpRegion (CLIPRGN* region){ CLIPRECT *cliprect; if (!(cliprect = region->head)) { fprintf (stderr, "region: %p is a null region.\n", region); } else { fprintf (stderr, "start of region: %p.\n", region); fprintf (stderr, "head of region: %p.\n", region->head); fprintf (stderr, "tail of region: %p.\n", region->tail); fprintf (stderr, "Bound of region: (%d, %d, %d, %d)\n", region->rcBound.left, region->rcBound.top, region->rcBound.right, region->rcBound.bottom); while (cliprect) { fprintf (stderr, "cliprect %p: (%d, %d, %d, %d)\n", cliprect, cliprect->rc.left, cliprect->rc.top, cliprect->rc.right, cliprect->rc.bottom); cliprect = cliprect->next; } fprintf (stderr, "end of region: %p.\n", region); }}#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -