colormap.c 69.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/***********************************************************

Copyright 1987, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION 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 be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.


Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission 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 that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY 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 THIS
SOFTWARE.

******************************************************************/


49 50 51 52
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

53
#include <X11/X.h>
54
#define NEED_EVENTS
55
#include <X11/Xproto.h>
56 57 58
#include <stdio.h>
#include <string.h>
#include <strings.h>
59
#include "misc.h"
60 61
#include "dix.h"
#include "colormapst.h"
62
#include "os.h"
63 64 65
#include "scrnintstr.h"
#include "resource.h"
#include "windowstr.h"
66
#include "privates.h"
67
#include "xace.h"
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

extern XID clientErrorValue;

static Pixel FindBestPixel(
    EntryPtr /*pentFirst*/,
    int /*size*/,
    xrgb * /*prgb*/,
    int /*channel*/
);

static int AllComp(
    EntryPtr /*pent*/,
    xrgb * /*prgb*/
);

static int RedComp(
    EntryPtr /*pent*/,
    xrgb * /*prgb*/
);

static int GreenComp(
    EntryPtr /*pent*/,
    xrgb * /*prgb*/
);

static int BlueComp(
    EntryPtr /*pent*/,
    xrgb * /*prgb*/
);

static void FreePixels(
99 100
    ColormapPtr /*pmap*/,
    int /*client*/
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
);

static void CopyFree(
    int /*channel*/,
    int /*client*/,
    ColormapPtr /*pmapSrc*/,
    ColormapPtr /*pmapDst*/
);

static void FreeCell(
    ColormapPtr /*pmap*/,
    Pixel /*i*/,
    int /*channel*/
);

static void UpdateColors(
    ColormapPtr /*pmap*/
);

static int AllocDirect(
    int /*client*/,
    ColormapPtr /*pmap*/,
    int /*c*/,
    int /*r*/,
    int /*g*/,
    int /*b*/,
    Bool /*contig*/,
    Pixel * /*pixels*/,
    Pixel * /*prmask*/,
    Pixel * /*pgmask*/,
    Pixel * /*pbmask*/
);

static int AllocPseudo(
    int /*client*/,
    ColormapPtr /*pmap*/,
    int /*c*/,
    int /*r*/,
    Bool /*contig*/,
    Pixel * /*pixels*/,
    Pixel * /*pmask*/,
    Pixel ** /*pppixFirst*/
);

static Bool AllocCP(
    ColormapPtr /*pmap*/,
    EntryPtr /*pentFirst*/,
    int /*count*/,
    int /*planes*/,
    Bool /*contig*/,
    Pixel * /*pixels*/,
    Pixel * /*pMask*/
);

static Bool AllocShared(
    ColormapPtr /*pmap*/,
    Pixel * /*ppix*/,
    int /*c*/,
    int /*r*/,
    int /*g*/,
    int /*b*/,
    Pixel /*rmask*/,
    Pixel /*gmask*/,
    Pixel /*bmask*/,
    Pixel * /*ppixFirst*/
);

static int FreeCo(
    ColormapPtr /*pmap*/,
    int /*client*/,
    int /*color*/,
    int /*npixIn*/,
    Pixel * /*ppixIn*/,
    Pixel /*mask*/
);

static int   TellNoMap(
    WindowPtr	/*pwin*/,
    Colormap 	* /*pmid*/
);

static void FindColorInRootCmap (
    ColormapPtr	/* pmap */,
    EntryPtr	/* pentFirst */,
    int		/* size */,
    xrgb*	/* prgb */,
    Pixel*	/* pPixel */,
    int		/* channel */,
    ColorCompareProcPtr /* comp */
);

#define NUMRED(vis) ((vis->redMask >> vis->offsetRed) + 1)
#define NUMGREEN(vis) ((vis->greenMask >> vis->offsetGreen) + 1)
#define NUMBLUE(vis) ((vis->blueMask >> vis->offsetBlue) + 1)
195 196 197
#if COMPOSITE
#define ALPHAMASK(vis)	((vis)->nplanes < 32 ? 0 : \
			 (CARD32) ~((vis)->redMask|(vis)->greenMask|(vis)->blueMask))
198 199 200 201 202
#else
#define ALPHAMASK(vis)	0
#endif

#define RGBMASK(vis) (vis->redMask | vis->greenMask | vis->blueMask | ALPHAMASK(vis))
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

/* 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	0

typedef 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
 */


Roland Mainz's avatar
Roland Mainz committed
250 251 252 253 254 255
/** 
 * Create and initialize the color map 
 * 
 * \param mid    resource to use for this colormap
 * \param alloc  1 iff all entries are allocated writable
 */
256
_X_EXPORT int 
Roland Mainz's avatar
Roland Mainz committed
257 258
CreateColormap (Colormap mid, ScreenPtr pScreen, VisualPtr pVisual, 
                ColormapPtr *ppcmap, int alloc, int client)
259 260 261 262
{
    int		class, size;
    unsigned long sizebytes;
    ColormapPtr	pmap;
263
    EntryPtr	pent;
264
    int		i;
265
    Pixel	*ppix, **pptr;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    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);
281 282 283 284 285 286 287
#if defined(_XSERVER64)
    pmap->pad0 = 0;
    pmap->pad1 = 0;
#if (X_BYTE_ORDER == X_LITTLE_ENDIAN)
    pmap->pad2 = 0;
#endif
#endif
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    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;
	}
    }
390
    pmap->devPrivates = NULL;
391 392
    pmap->flags |= BeingCreated;

393 394
    if (!AddResource(mid, RT_COLORMAP, (pointer)pmap))
	return (BadAlloc);
395

396 397 398 399 400 401 402 403 404 405
    /*  
     * Security creation/labeling check
     */
    i = XaceHook(XACE_RESOURCE_ACCESS, clients[client], mid, RT_COLORMAP,
		 DixCreateAccess, pmap);
    if (i != Success) {
	FreeResource(mid, RT_NONE);
	return i;
    }

406 407 408
    /* 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 */
409 410 411 412 413 414 415 416 417 418
    if (!(*pScreen->CreateColormap)(pmap))
    {
	FreeResource (mid, RT_NONE);
	return BadAlloc;
    }
    pmap->flags &= ~BeingCreated;
    *ppcmap = pmap;
    return (Success);
}

Roland Mainz's avatar
Roland Mainz committed
419 420 421 422
/**
 *
 * \param value  must conform to DeleteType
 */
423
int
Roland Mainz's avatar
Roland Mainz committed
424
FreeColormap (pointer value, XID mid)
425
{
426 427
    int	i;
    EntryPtr pent;
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    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]);
        }
    }

472
    dixFreePrivates(pmap->devPrivates);
473 474 475 476 477 478
    xfree(pmap);
    return(Success);
}

/* Tell window that pmid has disappeared */
static int
Roland Mainz's avatar
Roland Mainz committed
479
TellNoMap (WindowPtr pwin, Colormap *pmid)
480 481
{
    xEvent 	xE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
482

483 484 485 486 487 488 489 490
    if (wColormap(pwin) == *pmid)
    {
	/* This should be call to DeliverEvent */
	xE.u.u.type = ColormapNotify;
	xE.u.colormap.window = pwin->drawable.id;
	xE.u.colormap.colormap = None;
	xE.u.colormap.new = TRUE;
	xE.u.colormap.state = ColormapUninstalled;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
491 492 493 494
#ifdef PANORAMIX
        if(noPanoramiXExtension || !pwin->drawable.pScreen->myNum)
#endif
	   DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
495 496 497 498 499 500 501 502 503 504
	if (pwin->optional) {
	    pwin->optional->colormap = None;
	    CheckWindowOptionalNeed (pwin);
	}
    }

    return (WT_WALKCHILDREN);
}

/* Tell window that pmid got uninstalled */
505
_X_EXPORT int
Roland Mainz's avatar
Roland Mainz committed
506
TellLostMap (WindowPtr pwin, pointer value)
507 508 509
{
    Colormap 	*pmid = (Colormap *)value;
    xEvent 	xE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
510 511 512 513 514

#ifdef PANORAMIX
    if(!noPanoramiXExtension && pwin->drawable.pScreen->myNum)
	return WT_STOPWALKING;
#endif
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    if (wColormap(pwin) == *pmid)
    {
	/* This should be call to DeliverEvent */
	xE.u.u.type = ColormapNotify;
	xE.u.colormap.window = pwin->drawable.id;
	xE.u.colormap.colormap = *pmid;
	xE.u.colormap.new = FALSE;
	xE.u.colormap.state = ColormapUninstalled;
	DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
    }

    return (WT_WALKCHILDREN);
}

/* Tell window that pmid got installed */
530
_X_EXPORT int
Roland Mainz's avatar
Roland Mainz committed
531
TellGainedMap (WindowPtr pwin, pointer value)
532 533 534
{
    Colormap 	*pmid = (Colormap *)value;
    xEvent 	xE;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
535 536 537 538 539

#ifdef PANORAMIX
    if(!noPanoramiXExtension && pwin->drawable.pScreen->myNum)
	return WT_STOPWALKING;
#endif
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    if (wColormap (pwin) == *pmid)
    {
	/* This should be call to DeliverEvent */
	xE.u.u.type = ColormapNotify;
	xE.u.colormap.window = pwin->drawable.id;
	xE.u.colormap.colormap = *pmid;
	xE.u.colormap.new = FALSE;
	xE.u.colormap.state = ColormapInstalled;
	DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
    }

    return (WT_WALKCHILDREN);
}

  
int
Roland Mainz's avatar
Roland Mainz committed
556
CopyColormapAndFree (Colormap mid, ColormapPtr pSrc, int client)
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
{
    ColormapPtr	pmap = (ColormapPtr) NULL;
    int		result, alloc, size;
    Colormap	midSrc;
    ScreenPtr	pScreen;
    VisualPtr	pVisual;

    pScreen = pSrc->pScreen;
    pVisual = pSrc->pVisual;
    midSrc = pSrc->mid;
    alloc = ((pSrc->flags & AllAllocated) && CLIENT_ID(midSrc) == client) ?
            AllocAll : AllocNone;
    size = pVisual->ColormapEntries;

    /* If the create returns non-0, it failed */
    result = CreateColormap (mid, pScreen, pVisual, &pmap, alloc, client);
    if(result != Success)
        return(result);
    if(alloc == AllocAll)
    {
	memmove((char *)pmap->red, (char *)pSrc->red, size * sizeof(Entry));
	if((pmap->class | DynamicClass) == DirectColor)
	{
	    memmove((char *)pmap->green, (char *)pSrc->green, size * sizeof(Entry));
	    memmove((char *)pmap->blue, (char *)pSrc->blue, size * sizeof(Entry));
	}
	pSrc->flags &= ~AllAllocated;
	FreePixels(pSrc, client);
	UpdateColors(pmap);
	return(Success);
    }

    CopyFree(REDMAP, client, pSrc, pmap);
    if ((pmap->class | DynamicClass) == DirectColor)
    {
	CopyFree(GREENMAP, client, pSrc, pmap);
	CopyFree(BLUEMAP, client, pSrc, pmap);
    }
    if (pmap->class & DynamicClass)
	UpdateColors(pmap);
    /* XXX should worry about removing any RT_CMAPENTRY resource */
    return(Success);
}

/* Helper routine for freeing large numbers of cells from a map */
static void
Roland Mainz's avatar
Roland Mainz committed
603
CopyFree (int channel, int client, ColormapPtr pmapSrc, ColormapPtr pmapDst)
604
{
605
    int		z, npix;
606 607 608 609 610 611 612
    EntryPtr	pentSrcFirst, pentDstFirst;
    EntryPtr	pentSrc, pentDst;
    Pixel	*ppix;
    int		nalloc;

    switch(channel)
    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
613
      default:	/* so compiler can see that everything gets initialized */
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
      case REDMAP:
	ppix = (pmapSrc->clientPixelsRed)[client];
	npix = (pmapSrc->numPixelsRed)[client];
	pentSrcFirst = pmapSrc->red;
	pentDstFirst = pmapDst->red;
	break;
      case GREENMAP:
	ppix = (pmapSrc->clientPixelsGreen)[client];
	npix = (pmapSrc->numPixelsGreen)[client];
	pentSrcFirst = pmapSrc->green;
	pentDstFirst = pmapDst->green;
	break;
      case BLUEMAP:
	ppix = (pmapSrc->clientPixelsBlue)[client];
	npix = (pmapSrc->numPixelsBlue)[client];
	pentSrcFirst = pmapSrc->blue;
	pentDstFirst = pmapDst->blue;
	break;
    }
    nalloc = 0;
    if (pmapSrc->class & DynamicClass)
    {
	for(z = npix; --z >= 0; ppix++)
	{
	    /* Copy entries */
	    pentSrc = pentSrcFirst + *ppix;
	    pentDst = pentDstFirst + *ppix;
	    if (pentDst->refcnt > 0)
	    {
		pentDst->refcnt++;
	    }
	    else
	    {
		*pentDst = *pentSrc;
		nalloc++;
		if (pentSrc->refcnt > 0)
		    pentDst->refcnt = 1;
		else
		    pentSrc->fShared = FALSE;
	    }
	    FreeCell(pmapSrc, *ppix, channel);
	}
    }

    /* Note that FreeCell has already fixed pmapSrc->free{Color} */
    switch(channel)
    {
      case REDMAP:
        pmapDst->freeRed -= nalloc;
        (pmapDst->clientPixelsRed)[client] =
	    (pmapSrc->clientPixelsRed)[client];
        (pmapSrc->clientPixelsRed)[client] = (Pixel *) NULL;
        (pmapDst->numPixelsRed)[client] = (pmapSrc->numPixelsRed)[client];
        (pmapSrc->numPixelsRed)[client] = 0;
	break;
      case GREENMAP:
        pmapDst->freeGreen -= nalloc;
        (pmapDst->clientPixelsGreen)[client] =
	    (pmapSrc->clientPixelsGreen)[client];
        (pmapSrc->clientPixelsGreen)[client] = (Pixel *) NULL;
        (pmapDst->numPixelsGreen)[client] = (pmapSrc->numPixelsGreen)[client];
        (pmapSrc->numPixelsGreen)[client] = 0;
	break;
      case BLUEMAP:
        pmapDst->freeBlue -= nalloc;
        pmapDst->clientPixelsBlue[client] = pmapSrc->clientPixelsBlue[client];
        pmapSrc->clientPixelsBlue[client] = (Pixel *) NULL;
        pmapDst->numPixelsBlue[client] = pmapSrc->numPixelsBlue[client];
        pmapSrc->numPixelsBlue[client] = 0;
	break;
    }
}

/* Free the ith entry in a color map.  Must handle freeing of
 * colors allocated through AllocColorPlanes */
static void
Roland Mainz's avatar
Roland Mainz committed
690
FreeCell (ColormapPtr pmap, Pixel i, int channel)
691 692 693 694 695 696 697
{
    EntryPtr pent;
    int	*pCount;


    switch (channel)
    {
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
698
      default:	/* so compiler can see that everything gets initialized */
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
      case PSEUDOMAP:
      case REDMAP:
          pent = (EntryPtr) &pmap->red[i];
	  pCount = &pmap->freeRed;
	  break;
      case GREENMAP:
          pent = (EntryPtr) &pmap->green[i];
	  pCount = &pmap->freeGreen;
	  break;
      case BLUEMAP:
          pent = (EntryPtr) &pmap->blue[i];
	  pCount = &pmap->freeBlue;
	  break;
    }
    /* If it's not privately allocated and it's not time to free it, just
     * decrement the count */
    if (pent->refcnt > 1)
	pent->refcnt--;
    else
    {
        /* If the color type is shared, find the sharedcolor. If decremented
         * refcnt is 0, free the shared cell. */
        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);
	    pent->fShared = FALSE;
	}
	pent->refcnt = 0;
	*pCount += 1;
    }
}

static void
Roland Mainz's avatar
Roland Mainz committed
737
UpdateColors (ColormapPtr pmap)
738 739
{
    xColorItem		*defs;
740 741 742
    xColorItem *pdef;
    EntryPtr 	pent;
    VisualPtr	pVisual;
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
    int			i, n, size;

    pVisual = pmap->pVisual;
    size = pVisual->ColormapEntries;
    defs = (xColorItem *)ALLOCATE_LOCAL(size * sizeof(xColorItem));
    if (!defs)
	return;
    n = 0;
    pdef = defs;
    if (pmap->class == DirectColor)
    {
        for (i = 0; i < size; i++)
	{
	    if (!pmap->red[i].refcnt &&
		!pmap->green[i].refcnt &&
		!pmap->blue[i].refcnt)
		continue;
	    pdef->pixel = ((Pixel)i << pVisual->offsetRed) |
			  ((Pixel)i << pVisual->offsetGreen) |
			  ((Pixel)i << pVisual->offsetBlue);
	    pdef->red = pmap->red[i].co.local.red;
	    pdef->green = pmap->green[i].co.local.green;
	    pdef->blue = pmap->blue[i].co.local.blue;
	    pdef->flags = DoRed|DoGreen|DoBlue;
	    pdef++;
	    n++;
	}
    }
    else
    {
        for (i = 0, pent = pmap->red; i < size; i++, pent++)
	{
	    if (!pent->refcnt)
		continue;
	    pdef->pixel = i;
	    if(pent->fShared)
	    {
		pdef->red = pent->co.shco.red->color;
		pdef->green = pent->co.shco.green->color;
		pdef->blue = pent->co.shco.blue->color;
	    }
	    else
	    {
		pdef->red = pent->co.local.red;
		pdef->green = pent->co.local.green;
		pdef->blue = pent->co.local.blue;
	    }
	    pdef->flags = DoRed|DoGreen|DoBlue;
	    pdef++;
	    n++;
	}
    }
    if (n)
	(*pmap->pScreen->StoreColors)(pmap, n, defs);
    DEALLOCATE_LOCAL(defs);
}

/* Get a read-only color from a ColorMap (probably slow for large maps)
 * Returns by changing the value in pred, pgreen, pblue and pPix
 */
803
_X_EXPORT int
Roland Mainz's avatar
Roland Mainz committed
804 805 806
AllocColor (ColormapPtr pmap, 
            unsigned short *pred, unsigned short *pgreen, unsigned short *pblue, 
            Pixel *pPix, int client)
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
{
    Pixel	pixR, pixG, pixB;
    int		entries;
    xrgb	rgb;
    int		class;
    VisualPtr	pVisual;
    int		npix;
    Pixel	*ppix;

    pVisual = pmap->pVisual;
    (*pmap->pScreen->ResolveColor) (pred, pgreen, pblue, pVisual);
    rgb.red = *pred;
    rgb.green = *pgreen;
    rgb.blue = *pblue;
    class = pmap->class;
    entries = pVisual->ColormapEntries;

    /* If the colormap is being created, then we want to be able to change
     * the colormap, even if it's a static type. Otherwise, we'd never be
     * able to initialize static colormaps
     */
    if(pmap->flags & BeingCreated)
	class |= DynamicClass;

    /* If this is one of the static storage classes, and we're not initializing
     * it, the best we can do is to find the closest color entry to the
     * requested one and return that.
     */
    switch (class) {
    case StaticColor:
    case StaticGray:
	/* Look up all three components in the same pmap */
	*pPix = pixR = FindBestPixel(pmap->red, entries, &rgb, PSEUDOMAP);
	*pred = pmap->red[pixR].co.local.red;
	*pgreen = pmap->red[pixR].co.local.green;
	*pblue = pmap->red[pixR].co.local.blue;
	npix = pmap->numPixelsRed[client];
	ppix = (Pixel *) xrealloc(pmap->clientPixelsRed[client],
				  (npix + 1) * sizeof(Pixel));
	if (!ppix)
	    return (BadAlloc);
	ppix[npix] = pixR;
	pmap->clientPixelsRed[client] = ppix;
	pmap->numPixelsRed[client]++;
	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);
	*pPix = (pixR << pVisual->offsetRed) |
		(pixG << pVisual->offsetGreen) |
860 861
		(pixB << pVisual->offsetBlue) |
		ALPHAMASK(pVisual);
862
	
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
	*pred = pmap->red[pixR].co.local.red;
	*pgreen = pmap->green[pixG].co.local.green;
	*pblue = pmap->blue[pixB].co.local.blue;
	npix = pmap->numPixelsRed[client];
	ppix = (Pixel *) xrealloc(pmap->clientPixelsRed[client],
				  (npix + 1) * sizeof(Pixel));
	if (!ppix)
	    return (BadAlloc);
	ppix[npix] = pixR;
	pmap->clientPixelsRed[client] = ppix;
	npix = pmap->numPixelsGreen[client];
	ppix = (Pixel *) xrealloc(pmap->clientPixelsGreen[client],
				  (npix + 1) * sizeof(Pixel));
	if (!ppix)
	    return (BadAlloc);
	ppix[npix] = pixG;
	pmap->clientPixelsGreen[client] = ppix;
	npix = pmap->numPixelsBlue[client];
	ppix = (Pixel *) xrealloc(pmap->clientPixelsBlue[client],
				  (npix + 1) * sizeof(Pixel));
	if (!ppix)
	    return (BadAlloc);
	ppix[npix] = pixB;
	pmap->clientPixelsBlue[client] = ppix;
	pmap->numPixelsRed[client]++;
	pmap->numPixelsGreen[client]++;
	pmap->numPixelsBlue[client]++;
	break;

    case GrayScale:
    case PseudoColor:
	if (pmap->mid != pmap->pScreen->defColormap &&
	    pmap->pVisual->vid == pmap->pScreen->rootVisual)
	{
	    ColormapPtr prootmap = (ColormapPtr)
		SecurityLookupIDByType (clients[client], pmap->pScreen->defColormap,
899
					 RT_COLORMAP, DixReadAccess);
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

	    if (pmap->class == prootmap->class)
		FindColorInRootCmap (prootmap, prootmap->red, entries, &rgb, 
			pPix, PSEUDOMAP, AllComp);
	}
	if (FindColor(pmap, pmap->red, entries, &rgb, pPix, PSEUDOMAP,
		      client, AllComp) != Success)
	    return (BadAlloc);
        break;

    case DirectColor:
	if (pmap->mid != pmap->pScreen->defColormap &&
	    pmap->pVisual->vid == pmap->pScreen->rootVisual)
	{
	    ColormapPtr prootmap = (ColormapPtr)
		SecurityLookupIDByType (clients[client], pmap->pScreen->defColormap,
916
					 RT_COLORMAP, DixReadAccess);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

	    if (pmap->class == prootmap->class)
	    {
		pixR = (*pPix & pVisual->redMask) >> pVisual->offsetRed; 
		FindColorInRootCmap (prootmap, prootmap->red, entries, &rgb, 
			&pixR, REDMAP, RedComp);
		pixG = (*pPix & pVisual->greenMask) >> pVisual->offsetGreen; 
		FindColorInRootCmap (prootmap, prootmap->green, entries, &rgb, 
			&pixG, GREENMAP, GreenComp);
		pixB = (*pPix & pVisual->blueMask) >> pVisual->offsetBlue; 
		FindColorInRootCmap (prootmap, prootmap->blue, entries, &rgb, 
			&pixB, BLUEMAP, BlueComp);
		*pPix = pixR | pixG | pixB;
	    }
	}

	pixR = (*pPix & pVisual->redMask) >> pVisual->offsetRed; 
	if (FindColor(pmap, pmap->red, NUMRED(pVisual), &rgb, &pixR, REDMAP,
		      client, RedComp) != Success)
	    return (BadAlloc);
	pixG = (*pPix & pVisual->greenMask) >> pVisual->offsetGreen; 
	if (FindColor(pmap, pmap->green, NUMGREEN(pVisual), &rgb, &pixG,
		      GREENMAP, client, GreenComp) != Success)
	{
	    (void)FreeCo(pmap, client, REDMAP, 1, &pixR, (Pixel)0);
	    return (BadAlloc);
	}
	pixB = (*pPix & pVisual->blueMask) >> pVisual->offsetBlue; 
	if (FindColor(pmap, pmap->blue, NUMBLUE(pVisual), &rgb, &pixB, BLUEMAP,
		      client, BlueComp) != Success)
	{
	    (void)FreeCo(pmap, client, GREENMAP, 1, &pixG, (Pixel)0);
	    (void)FreeCo(pmap, client, REDMAP, 1, &pixR, (Pixel)0);
	    return (BadAlloc);
	}
952
	*pPix = pixR | pixG | pixB | ALPHAMASK(pVisual);
953

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	break;
    }

    /* if this is the client's first pixel in this colormap, tell the
     * resource manager that the client has pixels in this colormap which
     * should be freed when the client dies */
    if ((pmap->numPixelsRed[client] == 1) &&
	(CLIENT_ID(pmap->mid) != client) &&
	!(pmap->flags & BeingCreated))
    {
	colorResource	*pcr;

	pcr = (colorResource *) xalloc(sizeof(colorResource));
	if (!pcr)
	{
	    (void)FreeColors(pmap, client, 1, pPix, (Pixel)0);
	    return (BadAlloc);
	}
	pcr->mid = pmap->mid;
	pcr->client = client;
	if (!AddResource(FakeClientID(client), RT_CMAPENTRY, (pointer)pcr))
	    return (BadAlloc);
    }
    return (Success);
}

/*
 * FakeAllocColor -- fake an AllocColor request by
 * returning a free pixel if availible, otherwise returning
 * the closest matching pixel.  This is used by the mi
 * software sprite code to recolor cursors.  A nice side-effect
 * is that this routine will never return failure.
 */

988
_X_EXPORT void
989
FakeAllocColor (ColormapPtr pmap, xColorItem *item)
990
{
991 992 993 994 995 996
    Pixel pixR, pixG, pixB;
    Pixel temp;
    int	entries;
    xrgb rgb;
    int	class;
    VisualPtr pVisual;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

    pVisual = pmap->pVisual;
    rgb.red = item->red;
    rgb.green = item->green;
    rgb.blue = item->blue;
    (*pmap->pScreen->ResolveColor) (&rgb.red, &rgb.green, &rgb.blue, pVisual);
    class = pmap->class;
    entries = pVisual->ColormapEntries;

    switch (class) {
    case GrayScale:
    case PseudoColor:
	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 */
1054
_X_EXPORT void
1055
FakeFreeColor(ColormapPtr pmap, Pixel pixel)
1056
{
1057
    VisualPtr pVisual;
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
    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;

Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1096 1097
#define BigNumGreater(x,y) (((x)->upper > (y)->upper) ||\
			    ((x)->upper == (y)->upper && (x)->lower > (y)->lower))
1098 1099 1100 1101 1102 1103 1104 1105

#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
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1106
BigNumAdd (BigNumPtr x, BigNumPtr y, BigNumPtr r)
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
{
    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 Pixel
Roland Mainz's avatar
Roland Mainz committed
1120
FindBestPixel(EntryPtr pentFirst, int size, xrgb *prgb, int channel)
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
{
    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 = (long) pent->co.local.green - prgb->green;
	      db = (long) pent->co.local.blue - prgb->blue;
	  case REDMAP:
	      dr = (long) pent->co.local.red - prgb->red;
	      break;
	  case GREENMAP:
	      dg = (long) pent->co.local.green - prgb->green;
	      break;
	  case BLUEMAP:
	      db = (long) 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);
}

static void
Roland Mainz's avatar
Roland Mainz committed
1167 1168 1169
FindColorInRootCmap (ColormapPtr pmap, EntryPtr pentFirst, int size, 
                     xrgb *prgb, Pixel *pPixel, int channel, 
                     ColorCompareProcPtr comp)
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
{
    EntryPtr    pent;
    Pixel	pixel;
    int         count;

    if ((pixel = *pPixel) >= size)
	pixel = 0;
    for (pent = pentFirst + pixel, count = size; --count >= 0; pent++, pixel++)
    {
	if (pent->refcnt > 0 && (*comp) (pent, prgb))
	{
	    switch (channel)
	    {
	    case REDMAP:
		pixel <<= pmap->pVisual->offsetRed;
		break;
	    case GREENMAP:
		pixel <<= pmap->pVisual->offsetGreen;
		break;
	    case BLUEMAP:
		pixel <<= pmap->pVisual->offsetBlue;
		break;
	    default: /* PSEUDOMAP */
		break;
	    }
	    *pPixel = pixel;
	}
    }
}

/* 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
 */
int
Roland Mainz's avatar
Roland Mainz committed
1206 1207 1208
FindColor (ColormapPtr pmap, EntryPtr pentFirst, int size, xrgb *prgb, 
           Pixel *pPixel, int channel, int client, 
           ColorCompareProcPtr comp)
1209 1210 1211
{
    EntryPtr	pent;
    Bool	foundFree;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1212 1213 1214
    Pixel	pixel, Free = 0;
    int		npix, count, *nump = NULL;
    Pixel	**pixp = NULL, *ppix;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
    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 int
Roland Mainz's avatar
Roland Mainz committed
1381
AllComp (EntryPtr pent, xrgb *prgb)
1382 1383 1384 1385 1386 1387 1388 1389 1390
{
    if((pent->co.local.red == prgb->red) &&
       (pent->co.local.green == prgb->green) &&
       (pent->co.local.blue == prgb->blue) )
       return (1);
    return (0);
}

static int
Roland Mainz's avatar
Roland Mainz committed
1391
RedComp (EntryPtr pent, xrgb *prgb)
1392 1393 1394 1395 1396 1397 1398
{
    if (pent->co.local.red == prgb->red) 
	return (1);
    return (0);
}

static int
Roland Mainz's avatar
Roland Mainz committed
1399
GreenComp (EntryPtr pent, xrgb *prgb)
1400 1401 1402 1403 1404 1405 1406
{
    if (pent->co.local.green == prgb->green) 
	return (1);
    return (0);
}

static int
Roland Mainz's avatar
Roland Mainz committed
1407
BlueComp (EntryPtr pent, xrgb *prgb)
1408 1409 1410 1411 1412 1413 1414 1415 1416
{
    if (pent->co.local.blue == prgb->blue) 
	return (1);
    return (0);
}


/* Read the color value of a cell */

1417
_X_EXPORT int
Roland Mainz's avatar
Roland Mainz committed
1418
QueryColors (ColormapPtr pmap, int count, Pixel *ppixIn, xrgb *prgbList)
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
{
    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 void
1503
FreePixels(ColormapPtr pmap, int client)
1504
{
1505 1506 1507
    Pixel *ppix, *ppixStart;
    int n;
    int	class;
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

    class = pmap->class;
    ppixStart = pmap->clientPixelsRed[client];
    if (class & DynamicClass)
    {
	n = pmap->numPixelsRed[client];
	for (ppix = ppixStart; --n >= 0; )
	{
	    FreeCell(pmap, *ppix, REDMAP);
	    ppix++;
	}
    }

    xfree(ppixStart);
    pmap->clientPixelsRed[client] = (Pixel *) NULL;
    pmap->numPixelsRed[client] = 0;
    if ((class | DynamicClass) == DirectColor) 
    {
        ppixStart = pmap->clientPixelsGreen[client];
	if (class & DynamicClass)
	    for (ppix = ppixStart, n = pmap->numPixelsGreen[client]; --n >= 0;)
		FreeCell(pmap, *ppix++, GREENMAP);
	xfree(ppixStart);
	pmap->clientPixelsGreen[client] = (Pixel *) NULL;
	pmap->numPixelsGreen[client] = 0;

        ppixStart = pmap->clientPixelsBlue[client];
	if (class & DynamicClass)
	    for (ppix = ppixStart, n = pmap->numPixelsBlue[client]; --n >= 0; )
		FreeCell(pmap, *ppix++, BLUEMAP);
	xfree(ppixStart);
	pmap->clientPixelsBlue[client] = (Pixel *) NULL;
	pmap->numPixelsBlue[client] = 0;
    }
}

Roland Mainz's avatar
Roland Mainz committed
1544 1545 1546 1547 1548 1549
/** 
 * Frees all of a client's colors and cells.
 *
 *  \param value  must conform to DeleteType
 *  \unused fakeid
 */
1550
int
Roland Mainz's avatar
Roland Mainz committed
1551
FreeClientPixels (pointer value, XID fakeid)
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
{
    ColormapPtr pmap;
    colorResource *pcr = (colorResource *)value;

    pmap = (ColormapPtr) LookupIDByType(pcr->mid, RT_COLORMAP);
    if (pmap)
	FreePixels(pmap, pcr->client);
    xfree(pcr);
    return Success;
}

int
Roland Mainz's avatar
Roland Mainz committed
1564 1565
AllocColorCells (int client, ColormapPtr pmap, int colors, int planes, 
                 Bool contig, Pixel *ppix, Pixel *masks)
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
{
    Pixel	rmask, gmask, bmask, *ppixFirst, r, g, b;
    int		n, class;
    int		ok;
    int		oldcount;
    colorResource *pcr = (colorResource *)NULL;

    class = pmap->class;
    if (!(class & DynamicClass))
	return (BadAlloc); /* Shouldn't try on this type */
    oldcount = pmap->numPixelsRed[client];
    if (pmap->class == DirectColor)
	oldcount += pmap->numPixelsGreen[client] + pmap->numPixelsBlue[client];
    if (!oldcount && (CLIENT_ID(pmap->mid) != client))
    {
	pcr = (colorResource *) xalloc(sizeof(colorResource));
	if (!pcr)
	    return (BadAlloc);
    }

    if (pmap->class == DirectColor)
    {
        ok = AllocDirect (client, pmap, colors, planes, planes, planes,
			  contig, ppix, &rmask, &gmask, &bmask);
	if(ok == Success)
	{
	    for (r = g = b = 1, n = planes; --n >= 0; r += r, g += g, b += b)
	    {
		while(!(rmask & r))
		    r += r;
		while(!(gmask & g))
		    g += g;
		while(!(bmask & b))
		    b += b;
		*masks++ = r | g | b;
	    }
	}
    }
    else
    {
        ok = AllocPseudo (client, pmap, colors, planes, contig, ppix, &rmask,
			  &ppixFirst);
	if(ok == Success)
	{
	    for (r = 1, n = planes; --n >= 0; r += r)
	    {
		while(!(rmask & r))
		    r += r;
		*masks++ = r;
	    }
	}
    }

    /* if this is the client's first pixels in this colormap, tell the
     * resource manager that the client has pixels in this colormap which
     * should be freed when the client dies */
    if ((ok == Success) && pcr)
    {
	pcr->mid = pmap->mid;
	pcr->client = client;
	if (!AddResource(FakeClientID(client), RT_CMAPENTRY, (pointer)pcr))
	    ok = BadAlloc;
    } else if (pcr)
	xfree(pcr);

    return (ok);
}


int
Roland Mainz's avatar
Roland Mainz committed
1636 1637 1638
AllocColorPlanes (int client, ColormapPtr pmap, int colors, 
                  int r, int g, int b, Bool contig, Pixel *pixels, 
                  Pixel *prmask, Pixel *pgmask, Pixel *pbmask)
1639 1640 1641
{
    int		ok;
    Pixel	mask, *ppixFirst;
1642 1643
    Pixel shift;
    int i;
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    int		class;
    int		oldcount;
    colorResource *pcr = (colorResource *)NULL;

    class = pmap->class;
    if (!(class & DynamicClass))
	return (BadAlloc); /* Shouldn't try on this type */
    oldcount = pmap->numPixelsRed[client];
    if (class == DirectColor)
	oldcount += pmap->numPixelsGreen[client] + pmap->numPixelsBlue[client];
    if (!oldcount && (CLIENT_ID(pmap->mid) != client))
    {
	pcr = (colorResource *) xalloc(sizeof(colorResource));
	if (!pcr)
	    return (BadAlloc);
    }

    if (class == DirectColor)
    {
        ok = AllocDirect (client, pmap, colors, r, g, b, contig, pixels,
			  prmask, pgmask, pbmask);
    }
    else
    {
	/* Allocate the proper pixels */
	/* XXX This is sort of bad, because of contig is set, we force all
	 * r + g + b bits to be contiguous.  Should only force contiguity
	 * per mask 
	 */
        ok = AllocPseudo (client, pmap, colors, r + g + b, contig, pixels,
			  &mask, &ppixFirst);

	if(ok == Success)
	{
	    /* now split that mask into three */
	    *prmask = *pgmask = *pbmask = 0;
	    shift = 1;
	    for (i = r; --i >= 0; shift += shift)
	    {
		while (!(mask & shift))
		    shift += shift;
		*prmask |= shift;
	    }
	    for (i = g; --i >= 0; shift += shift)
	    {
		while (!(mask & shift))
		    shift += shift;
		*pgmask |= shift;
	    }
	    for (i = b; --i >= 0; shift += shift)
	    {
		while (!(mask & shift))
		    shift += shift;
		*pbmask |= shift;
	    }

	    /* set up the shared color cells */
	    if (!AllocShared(pmap, pixels, colors, r, g, b,
			     *prmask, *pgmask, *pbmask, ppixFirst))
	    {
		(void)FreeColors(pmap, client, colors, pixels, mask);
		ok = BadAlloc;
	    }
	}
    }

    /* if this is the client's first pixels in this colormap, tell the
     * resource manager that the client has pixels in this colormap which
     * should be freed when the client dies */
    if ((ok == Success) && pcr)
    {
	pcr->mid = pmap->mid;
	pcr->client = client;
	if (!AddResource(FakeClientID(client), RT_CMAPENTRY, (pointer)pcr))
	    ok = BadAlloc;
    } else if (pcr)
	xfree(pcr);

    return (ok);
}

static int
Roland Mainz's avatar
Roland Mainz committed
1726 1727
AllocDirect (int client, ColormapPtr pmap, int c, int r, int g, int b, Bool contig, 
             Pixel *pixels, Pixel *prmask, Pixel *pgmask, Pixel *pbmask)
1728 1729 1730 1731 1732
{
    Pixel	*ppixRed, *ppixGreen, *ppixBlue;
    Pixel	*ppix, *pDst, *p;
    int		npix, npixR, npixG, npixB;
    Bool	okR, okG, okB;
Kaleb Keithley Keithley's avatar
Kaleb Keithley Keithley committed
1733
    Pixel	*rpix = 0, *gpix = 0, *bpix = 0;
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831

    npixR = c << r;
    npixG = c << g;
    npixB = c << b;
    if ((r >= 32) || (g >= 32) || (b >= 32) ||
	(npixR > pmap->freeRed) || (npixR < c) ||
	(npixG > pmap->freeGreen) || (npixG < c) ||
	(npixB > pmap->freeBlue) || (npixB < c))
	return BadAlloc;

    /* start out with empty pixels */
    for(p = pixels; p < pixels + c; p++)
	*p = 0;

    ppixRed = (Pixel *)ALLOCATE_LOCAL(npixR * sizeof(Pixel));
    ppixGreen = (Pixel *)ALLOCATE_LOCAL(npixG * sizeof(Pixel));
    ppixBlue = (Pixel *)ALLOCATE_LOCAL(npixB * sizeof(Pixel));
    if (!ppixRed || !ppixGreen || !ppixBlue)
    {
	if (ppixBlue) DEALLOCATE_LOCAL(ppixBlue);
	if (ppixGreen) DEALLOCATE_LOCAL(ppixGreen);
	if (ppixRed) DEALLOCATE_LOCAL(ppixRed);
	return(BadAlloc);
    }

    okR = AllocCP(pmap, pmap->red, c, r, contig, ppixRed, prmask);
    okG = AllocCP(pmap, pmap->green, c, g, contig, ppixGreen, pgmask);
    okB = AllocCP(pmap, pmap->blue, c, b, contig, ppixBlue, pbmask);

    if (okR && okG && okB)
    {
	rpix = (Pixel *) xrealloc(pmap->clientPixelsRed[client],
				  (pmap->numPixelsRed[client] + (c << r)) *
				  sizeof(Pixel));
	if (rpix)
	    pmap->clientPixelsRed[client] = rpix;
	gpix = (Pixel *) xrealloc(pmap->clientPixelsGreen[client],
				  (pmap->numPixelsGreen[client] + (c << g)) *
				  sizeof(Pixel));
	if (gpix)
	    pmap->clientPixelsGreen[client] = gpix;
	bpix = (Pixel *) xrealloc(pmap->clientPixelsBlue[client],
				  (pmap->numPixelsBlue[client] + (c << b)) *
				  sizeof(Pixel));
	if (bpix)
	    pmap->clientPixelsBlue[client] = bpix;
    }

    if (!okR || !okG || !okB || !rpix || !gpix || !bpix)
    {
	if (okR)
	    for(ppix = ppixRed, npix = npixR; --npix >= 0; ppix++)
		pmap->red[*ppix].refcnt = 0;
	if (okG)
	    for(ppix = ppixGreen, npix = npixG; --npix >= 0; ppix++)
		pmap->green[*ppix].refcnt = 0;
	if (okB)
	    for(ppix = ppixBlue, npix = npixB; --npix >= 0; ppix++)
		pmap->blue[*ppix].refcnt = 0;
	DEALLOCATE_LOCAL(ppixBlue);
	DEALLOCATE_LOCAL(ppixGreen);
	DEALLOCATE_LOCAL(ppixRed);
	return(BadAlloc);
    }

    *prmask <<= pmap->pVisual->offsetRed;
    *pgmask <<= pmap->pVisual->offsetGreen;
    *pbmask <<= pmap->pVisual->offsetBlue;

    ppix = rpix + pmap->numPixelsRed[client];
    for (pDst = pixels, p = ppixRed; p < ppixRed + npixR; p++)
    {
	*ppix++ = *p;
	if(p < ppixRed + c)
	    *pDst++ |= *p << pmap->pVisual->offsetRed;
    }
    pmap->numPixelsRed[client] += npixR;
    pmap->freeRed -= npixR;

    ppix = gpix + pmap->numPixelsGreen[client];
    for (pDst = pixels, p = ppixGreen; p < ppixGreen + npixG; p++)
    {
	*ppix++ = *p;
	if(p < ppixGreen + c)
	    *pDst++ |= *p << pmap->pVisual->offsetGreen;
    }
    pmap->numPixelsGreen[client] += npixG;
    pmap->freeGreen -= npixG;

    ppix = bpix + pmap->numPixelsBlue[client];
    for (pDst = pixels, p = ppixBlue; p < ppixBlue + npixB; p++)
    {
	*ppix++ = *p;
	if(p < ppixBlue + c)
	    *pDst++ |= *p << pmap->pVisual->offsetBlue;
    }
    pmap->numPixelsBlue[client] += npixB;
    pmap->freeBlue -= npixB;
1832 1833


1834
    for (pDst = pixels; pDst < pixels + c; pDst++)
Keith Packard's avatar