📄 colormap.c
字号:
item->pixel = 0; if (FindColor(pmap, pmap->red, entries, &rgb, &temp, PSEUDOMAP, -1, AllComp) == Success) { item->pixel = temp; break; } /* fall through ... */ case StaticColor: case StaticGray: item->pixel = FindBestPixel(pmap->red, entries, &rgb, PSEUDOMAP); break; case DirectColor: /* Look up each component in its own map, then OR them together */ pixR = (item->pixel & pVisual->redMask) >> pVisual->offsetRed; pixG = (item->pixel & pVisual->greenMask) >> pVisual->offsetGreen; pixB = (item->pixel & pVisual->blueMask) >> pVisual->offsetBlue; if (FindColor(pmap, pmap->red, NUMRED(pVisual), &rgb, &pixR, REDMAP, -1, RedComp) != Success) pixR = FindBestPixel(pmap->red, NUMRED(pVisual), &rgb, REDMAP) << pVisual->offsetRed; if (FindColor(pmap, pmap->green, NUMGREEN(pVisual), &rgb, &pixG, GREENMAP, -1, GreenComp) != Success) pixG = FindBestPixel(pmap->green, NUMGREEN(pVisual), &rgb, GREENMAP) << pVisual->offsetGreen; if (FindColor(pmap, pmap->blue, NUMBLUE(pVisual), &rgb, &pixB, BLUEMAP, -1, BlueComp) != Success) pixB = FindBestPixel(pmap->blue, NUMBLUE(pVisual), &rgb, BLUEMAP) << pVisual->offsetBlue; item->pixel = pixR | pixG | pixB; break; case TrueColor: /* Look up each component in its own map, then OR them together */ pixR = FindBestPixel(pmap->red, NUMRED(pVisual), &rgb, REDMAP); pixG = FindBestPixel(pmap->green, NUMGREEN(pVisual), &rgb, GREENMAP); pixB = FindBestPixel(pmap->blue, NUMBLUE(pVisual), &rgb, BLUEMAP); item->pixel = (pixR << pVisual->offsetRed) | (pixG << pVisual->offsetGreen) | (pixB << pVisual->offsetBlue); break; }}/* free a pixel value obtained from FakeAllocColor */voidFakeFreeColor(pmap, pixel) register ColormapPtr pmap; Pixel pixel;{ register VisualPtr pVisual; Pixel pixR, pixG, pixB; switch (pmap->class) { case GrayScale: case PseudoColor: if (pmap->red[pixel].refcnt == AllocTemporary) pmap->red[pixel].refcnt = 0; break; case DirectColor: pVisual = pmap->pVisual; pixR = (pixel & pVisual->redMask) >> pVisual->offsetRed; pixG = (pixel & pVisual->greenMask) >> pVisual->offsetGreen; pixB = (pixel & pVisual->blueMask) >> pVisual->offsetBlue; if (pmap->red[pixR].refcnt == AllocTemporary) pmap->red[pixR].refcnt = 0; if (pmap->green[pixG].refcnt == AllocTemporary) pmap->green[pixG].refcnt = 0; if (pmap->blue[pixB].refcnt == AllocTemporary) pmap->blue[pixB].refcnt = 0; break; }}typedef unsigned short BigNumUpper;typedef unsigned long BigNumLower;#define BIGNUMLOWERBITS 24#define BIGNUMUPPERBITS 16#define BIGNUMLOWER (1 << BIGNUMLOWERBITS)#define BIGNUMUPPER (1 << BIGNUMUPPERBITS)#define UPPERPART(i) ((i) >> BIGNUMLOWERBITS)#define LOWERPART(i) ((i) & (BIGNUMLOWER - 1))typedef struct _bignum { BigNumUpper upper; BigNumLower lower;} BigNumRec, *BigNumPtr;#define BigNumGreater(x,y) (((x)->upper > (y)->upper) ||\ ((x)->upper == (y)->upper && (x)->lower > (y)->lower))#define UnsignedToBigNum(u,r) (((r)->upper = UPPERPART(u)), \ ((r)->lower = LOWERPART(u)))#define MaxBigNum(r) (((r)->upper = BIGNUMUPPER-1), \ ((r)->lower = BIGNUMLOWER-1))static void#if NeedFunctionPrototypesBigNumAdd (BigNumPtr x, BigNumPtr y, BigNumPtr r)#elseBigNumAdd (x, y, r) BigNumPtr x, y, r;#endif{ BigNumLower lower, carry = 0; lower = x->lower + y->lower; if (lower >= BIGNUMLOWER) { lower -= BIGNUMLOWER; carry = 1; } r->lower = lower; r->upper = x->upper + y->upper + carry;}static PixelFindBestPixel(pentFirst, size, prgb, channel) EntryPtr pentFirst; int size; xrgb *prgb; int channel;{ EntryPtr pent; Pixel pixel, final; long dr, dg, db; unsigned long sq; BigNumRec minval, sum, temp; final = 0; MaxBigNum(&minval); /* look for the minimal difference */ for (pent = pentFirst, pixel = 0; pixel < size; pent++, pixel++) { dr = dg = db = 0; switch(channel) { case PSEUDOMAP: dg = pent->co.local.green - prgb->green; db = pent->co.local.blue - prgb->blue; case REDMAP: dr = pent->co.local.red - prgb->red; break; case GREENMAP: dg = pent->co.local.green - prgb->green; break; case BLUEMAP: db = pent->co.local.blue - prgb->blue; break; } sq = dr * dr; UnsignedToBigNum (sq, &sum); sq = dg * dg; UnsignedToBigNum (sq, &temp); BigNumAdd (&sum, &temp, &sum); sq = db * db; UnsignedToBigNum (sq, &temp); BigNumAdd (&sum, &temp, &sum); if (BigNumGreater (&minval, &sum)) { final = pixel; minval = sum; } } return(final);}/* Tries to find a color in pmap that exactly matches the one requested in prgb * if it can't it allocates one. * Starts looking at pentFirst + *pPixel, so if you want a specific pixel, * load *pPixel with that value, otherwise set it to 0 */intFindColor (pmap, pentFirst, size, prgb, pPixel, channel, client, comp) ColormapPtr pmap; EntryPtr pentFirst; int size; xrgb *prgb; Pixel *pPixel; int channel; int client; ColorCompareProcPtr comp;{ EntryPtr pent; Bool foundFree; Pixel pixel, Free; int npix, count, *nump; Pixel **pixp, *ppix; xColorItem def; foundFree = FALSE; if((pixel = *pPixel) >= size) pixel = 0; /* see if there is a match, and also look for a free entry */ for (pent = pentFirst + pixel, count = size; --count >= 0; ) { if (pent->refcnt > 0) { if ((*comp) (pent, prgb)) { if (client >= 0) pent->refcnt++; *pPixel = pixel; switch(channel) { case REDMAP: *pPixel <<= pmap->pVisual->offsetRed; case PSEUDOMAP: break; case GREENMAP: *pPixel <<= pmap->pVisual->offsetGreen; break; case BLUEMAP: *pPixel <<= pmap->pVisual->offsetBlue; break; } goto gotit; } } else if (!foundFree && pent->refcnt == 0) { Free = pixel; foundFree = TRUE; /* If we're initializing the colormap, then we are looking for * the first free cell we can find, not to minimize the number * of entries we use. So don't look any further. */ if(pmap->flags & BeingCreated) break; } pixel++; if(pixel >= size) { pent = pentFirst; pixel = 0; } else pent++; } /* If we got here, we didn't find a match. If we also didn't find * a free entry, we're out of luck. Otherwise, we'll usurp a free * entry and fill it in */ if (!foundFree) return (BadAlloc); pent = pentFirst + Free; pent->fShared = FALSE; pent->refcnt = (client >= 0) ? 1 : AllocTemporary; switch (channel) { case PSEUDOMAP: pent->co.local.red = prgb->red; pent->co.local.green = prgb->green; pent->co.local.blue = prgb->blue; def.red = prgb->red; def.green = prgb->green; def.blue = prgb->blue; def.flags = (DoRed|DoGreen|DoBlue); if (client >= 0) pmap->freeRed--; def.pixel = Free; break; case REDMAP: pent->co.local.red = prgb->red; def.red = prgb->red; def.green = pmap->green[0].co.local.green; def.blue = pmap->blue[0].co.local.blue; def.flags = DoRed; if (client >= 0) pmap->freeRed--; def.pixel = Free << pmap->pVisual->offsetRed; break; case GREENMAP: pent->co.local.green = prgb->green; def.red = pmap->red[0].co.local.red; def.green = prgb->green; def.blue = pmap->blue[0].co.local.blue; def.flags = DoGreen; if (client >= 0) pmap->freeGreen--; def.pixel = Free << pmap->pVisual->offsetGreen; break; case BLUEMAP: pent->co.local.blue = prgb->blue; def.red = pmap->red[0].co.local.red; def.green = pmap->green[0].co.local.green; def.blue = prgb->blue; def.flags = DoBlue; if (client >= 0) pmap->freeBlue--; def.pixel = Free << pmap->pVisual->offsetBlue; break; } (*pmap->pScreen->StoreColors) (pmap, 1, &def); pixel = Free; *pPixel = def.pixel;gotit: if (pmap->flags & BeingCreated || client == -1) return(Success); /* Now remember the pixel, for freeing later */ switch (channel) { case PSEUDOMAP: case REDMAP: nump = pmap->numPixelsRed; pixp = pmap->clientPixelsRed; break; case GREENMAP: nump = pmap->numPixelsGreen; pixp = pmap->clientPixelsGreen; break; case BLUEMAP: nump = pmap->numPixelsBlue; pixp = pmap->clientPixelsBlue; break; } npix = nump[client]; ppix = (Pixel *) xrealloc (pixp[client], (npix + 1) * sizeof(Pixel)); if (!ppix) { pent->refcnt--; if (!pent->fShared) switch (channel) { case PSEUDOMAP: case REDMAP: pmap->freeRed++; break; case GREENMAP: pmap->freeGreen++; break; case BLUEMAP: pmap->freeBlue++; break; } return(BadAlloc); } ppix[npix] = pixel; pixp[client] = ppix; nump[client]++; return(Success);}/* Comparison functions -- passed to FindColor to determine if an * entry is already the color we're looking for or not */static intAllComp (pent, prgb) EntryPtr pent; xrgb *prgb;{ if((pent->co.local.red == prgb->red) && (pent->co.local.green == prgb->green) && (pent->co.local.blue == prgb->blue) ) return (1); return (0);}static intRedComp (pent, prgb) EntryPtr pent; xrgb *prgb;{ if (pent->co.local.red == prgb->red) return (1); return (0);}static intGreenComp (pent, prgb) EntryPtr pent; xrgb *prgb;{ if (pent->co.local.green == prgb->green) return (1); return (0);}static intBlueComp (pent, prgb) EntryPtr pent; xrgb *prgb;{ if (pent->co.local.blue == prgb->blue) return (1); return (0);}/* Read the color value of a cell */intQueryColors (pmap, count, ppixIn, prgbList) ColormapPtr pmap; int count; Pixel *ppixIn; xrgb *prgbList;{ Pixel *ppix, pixel; xrgb *prgb; VisualPtr pVisual; EntryPtr pent; Pixel i; int errVal = Success; pVisual = pmap->pVisual; if ((pmap->class | DynamicClass) == DirectColor) { int numred, numgreen, numblue; Pixel rgbbad; numred = NUMRED(pVisual); numgreen = NUMGREEN(pVisual); numblue = NUMBLUE(pVisual); rgbbad = ~RGBMASK(pVisual); for( ppix = ppixIn, prgb = prgbList; --count >= 0; ppix++, prgb++) { pixel = *ppix; if (pixel & rgbbad) { clientErrorValue = pixel; errVal = BadValue; continue; } i = (pixel & pVisual->redMask) >> pVisual->offsetRed; if (i >= numred) { clientErrorValue = pixel; errVal = BadValue; continue; } prgb->red = pmap->red[i].co.local.red; i = (pixel & pVisual->greenMask) >> pVisual->offsetGreen; if (i >= numgreen) { clientErrorValue = pixel; errVal = BadValue; continue; } prgb->green = pmap->green[i].co.local.green; i = (pixel & pVisual->blueMask) >> pVisual->offsetBlue; if (i >= numblue) { clientErrorValue = pixel; errVal = BadValue; continue; } prgb->blue = pmap->blue[i].co.local.blue; } } else { for( ppix = ppixIn, prgb = prgbList; --count >= 0; ppix++, prgb++) { pixel = *ppix; if (pixel >= pVisual->ColormapEntries) { clientErrorValue = pixel; errVal = BadValue; } else { pent = (EntryPtr)&pmap->red[pixel]; if (pent->fShared) { prgb->red = pent->co.shco.red->color; prgb->green = pent->co.shco.green->color; prgb->blue = pent->co.shco.blue->color; } else { prgb->red = pent->co.local.red; prgb->green = pent->co.local.green; prgb->blue = pent->co.local.blue; } } } } return (errVal);}static voidFreePixels(pmap, client) register ColormapPtr pmap; register int client;{ register Pixel *ppix, *ppixStart;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -