AddDIC.c revision 1ab64890
11ab64890Smrg/* $Xorg: AddDIC.c,v 1.3 2000/08/17 19:44:29 cpqbld Exp $ */
21ab64890Smrg
31ab64890Smrg/*
41ab64890Smrg * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
51ab64890Smrg * 	All Rights Reserved
61ab64890Smrg *
71ab64890Smrg * This file is a component of an X Window System-specific implementation
81ab64890Smrg * of Xcms based on the TekColor Color Management System.  Permission is
91ab64890Smrg * hereby granted to use, copy, modify, sell, and otherwise distribute this
101ab64890Smrg * software and its documentation for any purpose and without fee, provided
111ab64890Smrg * that this copyright, permission, and disclaimer notice is reproduced in
121ab64890Smrg * all copies of this software and in supporting documentation.  TekColor
131ab64890Smrg * is a trademark of Tektronix, Inc.
141ab64890Smrg *
151ab64890Smrg * Tektronix makes no representation about the suitability of this software
161ab64890Smrg * for any purpose.  It is provided "as is" and with all faults.
171ab64890Smrg *
181ab64890Smrg * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
191ab64890Smrg * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
201ab64890Smrg * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
211ab64890Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
221ab64890Smrg * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
231ab64890Smrg * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
241ab64890Smrg * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
251ab64890Smrg *
261ab64890Smrg *
271ab64890Smrg *	NAME
281ab64890Smrg *		XcmsAddDIC.c
291ab64890Smrg *
301ab64890Smrg *	DESCRIPTION
311ab64890Smrg *		Source for XcmsAddColorSpace
321ab64890Smrg *
331ab64890Smrg *
341ab64890Smrg */
351ab64890Smrg/* $XFree86$ */
361ab64890Smrg
371ab64890Smrg#ifdef HAVE_CONFIG_H
381ab64890Smrg#include <config.h>
391ab64890Smrg#endif
401ab64890Smrg#include "Xlibint.h"
411ab64890Smrg#include "Xcmsint.h"
421ab64890Smrg#include "Cv.h"
431ab64890Smrg
441ab64890Smrg
451ab64890Smrg/*
461ab64890Smrg *      DEFINES
471ab64890Smrg */
481ab64890Smrg#define NextUnregDiCsID(lastid) \
491ab64890Smrg	    (XCMS_UNREG_ID(lastid) ? ++lastid : XCMS_FIRST_UNREG_DI_ID)
501ab64890Smrg#define MAX(x,y) ((x) < (y) ? (y) : (x))
511ab64890Smrg
521ab64890Smrg
531ab64890Smrg/*
541ab64890Smrg *	NAME
551ab64890Smrg *		XcmsAddColorSpace - Add a Device-Independent Color Space
561ab64890Smrg *
571ab64890Smrg *	SYNOPSIS
581ab64890Smrg */
591ab64890SmrgStatus
601ab64890SmrgXcmsAddColorSpace(XcmsColorSpace *pCS)
611ab64890Smrg/*
621ab64890Smrg *	DESCRIPTION
631ab64890Smrg *		DI Color Spaces are managed on a global basis.
641ab64890Smrg *		This means that with exception of the provided DI color spaces:
651ab64890Smrg *			CIEXYZ, CIExyY, CIELab, CIEuvY, CIELuv, and TekHVC
661ab64890Smrg *		DI color spaces may have different XcmsColorFormat IDs between
671ab64890Smrg *		clients.  So, you must be careful when using XcmsColor
681ab64890Smrg *		structures between clients!  Use the routines XcmsFormatOfPrefix()
691ab64890Smrg *		and XcmsPrefixOfFormat() appropriately.
701ab64890Smrg *
711ab64890Smrg *	RETURNS
721ab64890Smrg *		XcmsSuccess if succeeded, otherwise XcmsFailure
731ab64890Smrg */
741ab64890Smrg{
751ab64890Smrg    XcmsColorSpace **papColorSpaces;
761ab64890Smrg    XcmsColorSpace *ptmpCS;
771ab64890Smrg    XcmsColorFormat lastID = 0;
781ab64890Smrg
791ab64890Smrg    if ((pCS->id = _XcmsRegFormatOfPrefix(pCS->prefix)) != 0) {
801ab64890Smrg	if (XCMS_DD_ID(pCS->id)) {
811ab64890Smrg	    /* This is a Device-Dependent Color Space */
821ab64890Smrg	    return(XcmsFailure);
831ab64890Smrg	}
841ab64890Smrg	/*
851ab64890Smrg	 * REGISTERED DI Color Space
861ab64890Smrg	 *    then see if the color space has already been added to the
871ab64890Smrg	 *    system:
881ab64890Smrg	 *	    a. If the same ID/prefix and same XcmsColorSpec is found,
891ab64890Smrg	 *		then its a duplicate, so return success.
901ab64890Smrg	 *	    b. If same ID/prefix but different XcmsColorSpec is
911ab64890Smrg	 *		found, then add the color space to the front of the
921ab64890Smrg	 *		list using the same ID.  This allows one to override
931ab64890Smrg	 *		an existing DI Color Space.
941ab64890Smrg	 *	    c. Otherwise none found so just add the color space.
951ab64890Smrg	 */
961ab64890Smrg	if ((papColorSpaces = _XcmsDIColorSpaces) != NULL) {
971ab64890Smrg	    while ((ptmpCS = *papColorSpaces++) != NULL) {
981ab64890Smrg		if (pCS->id == ptmpCS->id) {
991ab64890Smrg		    if (pCS == ptmpCS) {
1001ab64890Smrg			/* a. duplicate*/
1011ab64890Smrg			return(XcmsSuccess);
1021ab64890Smrg		    }
1031ab64890Smrg		    /* b. same ID/prefix but different XcmsColorSpace */
1041ab64890Smrg		    break;
1051ab64890Smrg		}
1061ab64890Smrg	    }
1071ab64890Smrg	}
1081ab64890Smrg	/* c. None found */
1091ab64890Smrg    } else {
1101ab64890Smrg	/*
1111ab64890Smrg	 * UNREGISTERED DI Color Space
1121ab64890Smrg	 *    then see if the color space has already been added to the
1131ab64890Smrg	 *    system:
1141ab64890Smrg	 *	    a. If same prefix and XcmsColorSpec, then
1151ab64890Smrg	 *		its a duplicate ... return success.
1161ab64890Smrg	 *	    b. If same prefix but different XcmsColorSpec, then
1171ab64890Smrg	 *		add the color space to the front of the list using
1181ab64890Smrg	 *		the same ID.  This allows one to override an existing
1191ab64890Smrg	 *		DI Color Space.
1201ab64890Smrg	 *	    c. Otherwise none found so, add the color space using the
1211ab64890Smrg	 *		next unregistered ID for the connection.
1221ab64890Smrg	 */
1231ab64890Smrg	if ((papColorSpaces = _XcmsDIColorSpaces) != NULL) {
1241ab64890Smrg	    while ((ptmpCS = *papColorSpaces++) != NULL) {
1251ab64890Smrg		lastID = MAX(lastID, ptmpCS->id);
1261ab64890Smrg		if (strcmp(pCS->prefix, ptmpCS->prefix) == 0) {
1271ab64890Smrg		    if (pCS == ptmpCS) {
1281ab64890Smrg			/* a. duplicate */
1291ab64890Smrg			return(XcmsSuccess);
1301ab64890Smrg		    }
1311ab64890Smrg		    /* b. same prefix but different XcmsColorSpec */
1321ab64890Smrg		    pCS->id = ptmpCS->id;
1331ab64890Smrg		    goto AddColorSpace;
1341ab64890Smrg		}
1351ab64890Smrg	    }
1361ab64890Smrg	}
1371ab64890Smrg	/* c. None found */
1381ab64890Smrg	pCS->id = NextUnregDiCsID(lastID);
1391ab64890Smrg    }
1401ab64890Smrg
1411ab64890Smrg
1421ab64890SmrgAddColorSpace:
1431ab64890Smrg    if ((papColorSpaces = (XcmsColorSpace **)
1441ab64890Smrg	    _XcmsPushPointerArray((XPointer *)_XcmsDIColorSpaces,
1451ab64890Smrg	    (XPointer)pCS,
1461ab64890Smrg	    (XPointer *)_XcmsDIColorSpacesInit)) == NULL) {
1471ab64890Smrg	return(XcmsFailure);
1481ab64890Smrg    }
1491ab64890Smrg    _XcmsDIColorSpaces = papColorSpaces;
1501ab64890Smrg    return(XcmsSuccess);
1511ab64890Smrg}
152