📄 colormap.c
字号:
/***********************************************************Copyright (c) 1987 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 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.******************************************************************//* $XConsortium: colormap.c /main/71 1996/06/17 11:01:33 mor $ *//* $XFree86: xc/programs/Xserver/dix/colormap.c,v 3.1 1996/12/23 06:29:34 dawes Exp $ */#include "X.h"#define NEED_EVENTS#include "Xproto.h"#include "misc.h"#include "dix.h"#include "colormapst.h"#include "os.h"#include "scrnintstr.h"#include "resource.h"#include "windowstr.h"extern XID clientErrorValue;static Pixel FindBestPixel(#if NeedFunctionPrototypes EntryPtr /*pentFirst*/, int /*size*/, xrgb * /*prgb*/, int /*channel*/#endif);static int AllComp(#if NeedFunctionPrototypes EntryPtr /*pent*/, xrgb * /*prgb*/#endif);static int RedComp(#if NeedFunctionPrototypes EntryPtr /*pent*/, xrgb * /*prgb*/#endif);static int GreenComp(#if NeedFunctionPrototypes EntryPtr /*pent*/, xrgb * /*prgb*/#endif);static int BlueComp(#if NeedFunctionPrototypes EntryPtr /*pent*/, xrgb * /*prgb*/#endif);static void FreePixels(#if NeedFunctionPrototypes register ColormapPtr /*pmap*/, register int /*client*/#endif);static void CopyFree(#if NeedFunctionPrototypes int /*channel*/, int /*client*/, ColormapPtr /*pmapSrc*/, ColormapPtr /*pmapDst*/#endif);static void FreeCell(#if NeedFunctionPrototypes ColormapPtr /*pmap*/, Pixel /*i*/, int /*channel*/#endif);static void UpdateColors(#if NeedFunctionPrototypes ColormapPtr /*pmap*/#endif);static int AllocDirect(#if NeedFunctionPrototypes int /*client*/, ColormapPtr /*pmap*/, int /*c*/, int /*r*/, int /*g*/, int /*b*/, Bool /*contig*/, Pixel * /*pixels*/, Pixel * /*prmask*/, Pixel * /*pgmask*/, Pixel * /*pbmask*/#endif);static int AllocPseudo(#if NeedFunctionPrototypes int /*client*/, ColormapPtr /*pmap*/, int /*c*/, int /*r*/, Bool /*contig*/, Pixel * /*pixels*/, Pixel * /*pmask*/, Pixel ** /*pppixFirst*/#endif);static Bool AllocCP(#if NeedFunctionPrototypes ColormapPtr /*pmap*/, EntryPtr /*pentFirst*/, int /*count*/, int /*planes*/, Bool /*contig*/, Pixel * /*pixels*/, Pixel * /*pMask*/#endif);static Bool AllocShared(#if NeedFunctionPrototypes ColormapPtr /*pmap*/, Pixel * /*ppix*/, int /*c*/, int /*r*/, int /*g*/, int /*b*/, Pixel /*rmask*/, Pixel /*gmask*/, Pixel /*bmask*/, Pixel * /*ppixFirst*/#endif);static int FreeCo(#if NeedFunctionPrototypes ColormapPtr /*pmap*/, int /*client*/, int /*color*/, int /*npixIn*/, Pixel * /*ppixIn*/, Pixel /*mask*/#endif);static int TellNoMap(#if NeedFunctionPrototypes WindowPtr /*pwin*/, Colormap * /*pmid*/#endif);#define NUMRED(vis) ((vis->redMask >> vis->offsetRed) + 1)#define NUMGREEN(vis) ((vis->greenMask >> vis->offsetGreen) + 1)#define NUMBLUE(vis) ((vis->blueMask >> vis->offsetBlue) + 1)#define RGBMASK(vis) (vis->redMask | vis->greenMask | vis->blueMask)/* GetNextBitsOrBreak(bits, mask, base) -- * (Suggestion: First read the macro, then read this explanation. * * Either generate the next value to OR in to a pixel or break out of this * while loop * * This macro is used when we're trying to generate all 2^n combinations of * bits in mask. What we're doing here is counting in binary, except that * the bits we use to count may not be contiguous. This macro will be * called 2^n times, returning a different value in bits each time. Then * it will cause us to break out of a surrounding loop. (It will always be * called from within a while loop.) * On call: mask is the value we want to find all the combinations for * base has 1 bit set where the least significant bit of mask is set * * For example,if mask is 01010, base should be 0010 and we count like this: * 00010 (see this isn't so hard), * then we add base to bits and get 0100. (bits & ~mask) is (0100 & 0100) so * we add that to bits getting (0100 + 0100) = * 01000 for our next value. * then we add 0010 to get * 01010 and we're done (easy as 1, 2, 3) */#define GetNextBitsOrBreak(bits, mask, base) \ if((bits) == (mask)) \ break; \ (bits) += (base); \ while((bits) & ~(mask)) \ (bits) += ((bits) & ~(mask)); /* ID of server as client */#define SERVER_ID 0typedef struct _colorResource{ Colormap mid; int client;} colorResource;/* Invariants: * refcnt == 0 means entry is empty * refcnt > 0 means entry is useable by many clients, so it can't be changed * refcnt == AllocPrivate means entry owned by one client only * fShared should only be set if refcnt == AllocPrivate, and only in red map *//* Create and initialize the color map */int CreateColormap (mid, pScreen, pVisual, ppcmap, alloc, client) Colormap mid; /* resource to use for this colormap */ ScreenPtr pScreen; VisualPtr pVisual; ColormapPtr *ppcmap; int alloc; /* 1 iff all entries are allocated writeable */ int client;{ int class, size; unsigned long sizebytes; ColormapPtr pmap; register EntryPtr pent; int i; register Pixel *ppix, **pptr; extern int colormapPrivateCount; class = pVisual->class; if(!(class & DynamicClass) && (alloc != AllocNone) && (client != SERVER_ID)) return (BadMatch); size = pVisual->ColormapEntries; sizebytes = (size * sizeof(Entry)) + (MAXCLIENTS * sizeof(Pixel *)) + (MAXCLIENTS * sizeof(int)); if ((class | DynamicClass) == DirectColor) sizebytes *= 3; sizebytes += sizeof(ColormapRec); pmap = (ColormapPtr) xalloc(sizebytes); if (!pmap) return (BadAlloc); pmap->red = (EntryPtr)((char *)pmap + sizeof(ColormapRec)); sizebytes = size * sizeof(Entry); pmap->clientPixelsRed = (Pixel **)((char *)pmap->red + sizebytes); pmap->numPixelsRed = (int *)((char *)pmap->clientPixelsRed + (MAXCLIENTS * sizeof(Pixel *))); pmap->mid = mid; pmap->flags = 0; /* start out with all flags clear */ if(mid == pScreen->defColormap) pmap->flags |= IsDefault; pmap->pScreen = pScreen; pmap->pVisual = pVisual; pmap->class = class; if ((class | DynamicClass) == DirectColor) size = NUMRED(pVisual); pmap->freeRed = size; bzero ((char *) pmap->red, (int)sizebytes); bzero((char *) pmap->numPixelsRed, MAXCLIENTS * sizeof(int)); for (pptr = &pmap->clientPixelsRed[MAXCLIENTS]; --pptr >= pmap->clientPixelsRed; ) *pptr = (Pixel *)NULL; if (alloc == AllocAll) { if (class & DynamicClass) pmap->flags |= AllAllocated; for (pent = &pmap->red[size - 1]; pent >= pmap->red; pent--) pent->refcnt = AllocPrivate; pmap->freeRed = 0; ppix = (Pixel *)xalloc(size * sizeof(Pixel)); if (!ppix) { xfree(pmap); return (BadAlloc); } pmap->clientPixelsRed[client] = ppix; for(i = 0; i < size; i++) ppix[i] = i; pmap->numPixelsRed[client] = size; } if ((class | DynamicClass) == DirectColor) { pmap->freeGreen = NUMGREEN(pVisual); pmap->green = (EntryPtr)((char *)pmap->numPixelsRed + (MAXCLIENTS * sizeof(int))); pmap->clientPixelsGreen = (Pixel **)((char *)pmap->green + sizebytes); pmap->numPixelsGreen = (int *)((char *)pmap->clientPixelsGreen + (MAXCLIENTS * sizeof(Pixel *))); pmap->freeBlue = NUMBLUE(pVisual); pmap->blue = (EntryPtr)((char *)pmap->numPixelsGreen + (MAXCLIENTS * sizeof(int))); pmap->clientPixelsBlue = (Pixel **)((char *)pmap->blue + sizebytes); pmap->numPixelsBlue = (int *)((char *)pmap->clientPixelsBlue + (MAXCLIENTS * sizeof(Pixel *))); bzero ((char *) pmap->green, (int)sizebytes); bzero ((char *) pmap->blue, (int)sizebytes); memmove((char *) pmap->clientPixelsGreen, (char *) pmap->clientPixelsRed, MAXCLIENTS * sizeof(Pixel *)); memmove((char *) pmap->clientPixelsBlue, (char *) pmap->clientPixelsRed, MAXCLIENTS * sizeof(Pixel *)); bzero((char *) pmap->numPixelsGreen, MAXCLIENTS * sizeof(int)); bzero((char *) pmap->numPixelsBlue, MAXCLIENTS * sizeof(int)); /* If every cell is allocated, mark its refcnt */ if (alloc == AllocAll) { size = pmap->freeGreen; for(pent = &pmap->green[size-1]; pent >= pmap->green; pent--) pent->refcnt = AllocPrivate; pmap->freeGreen = 0; ppix = (Pixel *) xalloc(size * sizeof(Pixel)); if (!ppix) { xfree(pmap->clientPixelsRed[client]); xfree(pmap); return(BadAlloc); } pmap->clientPixelsGreen[client] = ppix; for(i = 0; i < size; i++) ppix[i] = i; pmap->numPixelsGreen[client] = size; size = pmap->freeBlue; for(pent = &pmap->blue[size-1]; pent >= pmap->blue; pent--) pent->refcnt = AllocPrivate; pmap->freeBlue = 0; ppix = (Pixel *) xalloc(size * sizeof(Pixel)); if (!ppix) { xfree(pmap->clientPixelsGreen[client]); xfree(pmap->clientPixelsRed[client]); xfree(pmap); return(BadAlloc); } pmap->clientPixelsBlue[client] = ppix; for(i = 0; i < size; i++) ppix[i] = i; pmap->numPixelsBlue[client] = size; } } if (!AddResource(mid, RT_COLORMAP, (pointer)pmap)) return (BadAlloc); /* If the device wants a chance to initialize the colormap in any way, * this is it. In specific, if this is a Static colormap, this is the * time to fill in the colormap's values */ pmap->flags |= BeingCreated; /* * Allocate the array of devPrivate's for this colormap. */ if (colormapPrivateCount == 0) pmap->devPrivates = NULL; else { pmap->devPrivates = (DevUnion *) xalloc ( colormapPrivateCount * sizeof(DevUnion)); if (!pmap->devPrivates) { FreeResource (mid, RT_NONE); return BadAlloc; } } if (!(*pScreen->CreateColormap)(pmap)) { FreeResource (mid, RT_NONE); return BadAlloc; } pmap->flags &= ~BeingCreated; *ppcmap = pmap; return (Success);}intFreeColormap (value, mid) pointer value; /* must conform to DeleteType */ XID mid;{ int i; register EntryPtr pent; ColormapPtr pmap = (ColormapPtr)value; if(CLIENT_ID(mid) != SERVER_ID) { (*pmap->pScreen->UninstallColormap) (pmap); WalkTree(pmap->pScreen, (VisitWindowProcPtr)TellNoMap, (pointer) &mid); } /* This is the device's chance to undo anything it needs to, especially * to free any storage it allocated */ (*pmap->pScreen->DestroyColormap)(pmap); if(pmap->clientPixelsRed) { for(i = 0; i < MAXCLIENTS; i++) xfree(pmap->clientPixelsRed[i]); } if ((pmap->class == PseudoColor) || (pmap->class == GrayScale)) { for(pent = &pmap->red[pmap->pVisual->ColormapEntries - 1]; pent >= pmap->red; pent--) { if(pent->fShared) { if (--pent->co.shco.red->refcnt == 0) xfree(pent->co.shco.red); if (--pent->co.shco.green->refcnt == 0) xfree(pent->co.shco.green); if (--pent->co.shco.blue->refcnt == 0) xfree(pent->co.shco.blue); } } } if((pmap->class | DynamicClass) == DirectColor) { for(i = 0; i < MAXCLIENTS; i++) { xfree(pmap->clientPixelsGreen[i]); xfree(pmap->clientPixelsBlue[i]); } } if (pmap->devPrivates) xfree(pmap->devPrivates); xfree(pmap); return(Success);}/* Tell window that pmid has disappeared */static intTellNoMap (pwin, pmid) WindowPtr pwin; Colormap *pmid;{ xEvent xE; if (wColormap(pwin) == *pmid) { /* This should be call to DeliverEvent */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -