1
2/*
3 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
4 * 	All Rights Reserved
5 *
6 * This file is a component of an X Window System-specific implementation
7 * of Xcms based on the TekColor Color Management System.  Permission is
8 * hereby granted to use, copy, modify, sell, and otherwise distribute this
9 * software and its documentation for any purpose and without fee, provided
10 * that this copyright, permission, and disclaimer notice is reproduced in
11 * all copies of this software and in supporting documentation.  TekColor
12 * is a trademark of Tektronix, Inc.
13 *
14 * Tektronix makes no representation about the suitability of this software
15 * for any purpose.  It is provided "as is" and with all faults.
16 *
17 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
18 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
24 *
25 *
26 *	NAME
27 *		XcmsAddDIC.c
28 *
29 *	DESCRIPTION
30 *		Source for XcmsAddColorSpace
31 *
32 *
33 */
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38#include "Xlibint.h"
39#include "Xcmsint.h"
40#include "Cv.h"
41
42
43/*
44 *      DEFINES
45 */
46#define NextUnregDiCsID(lastid) \
47	    (XCMS_UNREG_ID(lastid) ? ++lastid : XCMS_FIRST_UNREG_DI_ID)
48#define MAX(x,y) ((x) < (y) ? (y) : (x))
49
50
51/*
52 *	NAME
53 *		XcmsAddColorSpace - Add a Device-Independent Color Space
54 *
55 *	SYNOPSIS
56 */
57Status
58XcmsAddColorSpace(XcmsColorSpace *pCS)
59/*
60 *	DESCRIPTION
61 *		DI Color Spaces are managed on a global basis.
62 *		This means that with exception of the provided DI color spaces:
63 *			CIEXYZ, CIExyY, CIELab, CIEuvY, CIELuv, and TekHVC
64 *		DI color spaces may have different XcmsColorFormat IDs between
65 *		clients.  So, you must be careful when using XcmsColor
66 *		structures between clients!  Use the routines XcmsFormatOfPrefix()
67 *		and XcmsPrefixOfFormat() appropriately.
68 *
69 *	RETURNS
70 *		XcmsSuccess if succeeded, otherwise XcmsFailure
71 */
72{
73    XcmsColorSpace **papColorSpaces;
74    XcmsColorSpace *ptmpCS;
75    XcmsColorFormat lastID = 0;
76
77    if ((pCS->id = _XcmsRegFormatOfPrefix(pCS->prefix)) != 0) {
78	if (XCMS_DD_ID(pCS->id)) {
79	    /* This is a Device-Dependent Color Space */
80	    return(XcmsFailure);
81	}
82	/*
83	 * REGISTERED DI Color Space
84	 *    then see if the color space has already been added to the
85	 *    system:
86	 *	    a. If the same ID/prefix and same XcmsColorSpec is found,
87	 *		then its a duplicate, so return success.
88	 *	    b. If same ID/prefix but different XcmsColorSpec is
89	 *		found, then add the color space to the front of the
90	 *		list using the same ID.  This allows one to override
91	 *		an existing DI Color Space.
92	 *	    c. Otherwise none found so just add the color space.
93	 */
94	if ((papColorSpaces = _XcmsDIColorSpaces) != NULL) {
95	    while ((ptmpCS = *papColorSpaces++) != NULL) {
96		if (pCS->id == ptmpCS->id) {
97		    if (pCS == ptmpCS) {
98			/* a. duplicate*/
99			return(XcmsSuccess);
100		    }
101		    /* b. same ID/prefix but different XcmsColorSpace */
102		    break;
103		}
104	    }
105	}
106	/* c. None found */
107    } else {
108	/*
109	 * UNREGISTERED DI Color Space
110	 *    then see if the color space has already been added to the
111	 *    system:
112	 *	    a. If same prefix and XcmsColorSpec, then
113	 *		its a duplicate ... return success.
114	 *	    b. If same prefix but different XcmsColorSpec, then
115	 *		add the color space to the front of the list using
116	 *		the same ID.  This allows one to override an existing
117	 *		DI Color Space.
118	 *	    c. Otherwise none found so, add the color space using the
119	 *		next unregistered ID for the connection.
120	 */
121	if ((papColorSpaces = _XcmsDIColorSpaces) != NULL) {
122	    while ((ptmpCS = *papColorSpaces++) != NULL) {
123		lastID = MAX(lastID, ptmpCS->id);
124		if (strcmp(pCS->prefix, ptmpCS->prefix) == 0) {
125		    if (pCS == ptmpCS) {
126			/* a. duplicate */
127			return(XcmsSuccess);
128		    }
129		    /* b. same prefix but different XcmsColorSpec */
130		    pCS->id = ptmpCS->id;
131		    goto AddColorSpace;
132		}
133	    }
134	}
135	/* c. None found */
136	pCS->id = NextUnregDiCsID(lastID);
137    }
138
139
140AddColorSpace:
141    if ((papColorSpaces = (XcmsColorSpace **)
142	    _XcmsPushPointerArray((XPointer *)_XcmsDIColorSpaces,
143	    (XPointer)pCS,
144	    (XPointer *)_XcmsDIColorSpacesInit)) == NULL) {
145	return(XcmsFailure);
146    }
147    _XcmsDIColorSpaces = papColorSpaces;
148    return(XcmsSuccess);
149}
150