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