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  *		XcmsCCC.c - Color Conversion Context Routines
28  *
29  *	DESCRIPTION
30  *		Routines to create, access, and free Color Conversion
31  *		Context structures.
32  *
33  *
34  */
35 
36 /*
37 
38 Copyright 1994, 1998  The Open Group
39 
40 Permission to use, copy, modify, distribute, and sell this software and its
41 documentation for any purpose is hereby granted without fee, provided that
42 the above copyright notice appear in all copies and that both that
43 copyright notice and this permission notice appear in supporting
44 documentation.
45 
46 The above copyright notice and this permission notice shall be included
47 in all copies or substantial portions of the Software.
48 
49 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
50 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
53 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
54 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
55 OTHER DEALINGS IN THE SOFTWARE.
56 
57 Except as contained in this notice, the name of The Open Group shall
58 not be used in advertising or otherwise to promote the sale, use or
59 other dealings in this Software without prior written authorization
60 from The Open Group.
61 
62 */
63 
64 #ifdef HAVE_CONFIG_H
65 #include <config.h>
66 #endif
67 #include <stdio.h>
68 #include "Xlibint.h"
69 #include "Xcmsint.h"
70 #include "Cv.h"
71 
72 
73 
74 /************************************************************************
75  *									*
76  *			PUBLIC INTERFACES				*
77  *									*
78  ************************************************************************/
79 
80 /*
81  *	NAME
82  *		XcmsCreateCCC
83  *
84  *	SYNOPSIS
85  */
86 
87 XcmsCCC
XcmsCreateCCC(Display * dpy,int screenNumber,Visual * visual,XcmsColor * clientWhitePt,XcmsCompressionProc gamutCompProc,XPointer gamutCompClientData,XcmsWhiteAdjustProc whitePtAdjProc,XPointer whitePtAdjClientData)88 XcmsCreateCCC(
89     Display *dpy,
90     int screenNumber,
91     Visual *visual,
92     XcmsColor *clientWhitePt,
93     XcmsCompressionProc gamutCompProc,
94     XPointer gamutCompClientData,
95     XcmsWhiteAdjustProc whitePtAdjProc,
96     XPointer whitePtAdjClientData)
97 /*
98  *	DESCRIPTION
99  *		Given a Display, Screen, Visual, etc., this routine creates
100  *		an appropriate Color Conversion Context.
101  *
102  *	RETURNS
103  *		Returns NULL if failed; otherwise address of the newly
104  *		created XcmsCCC.
105  *
106  */
107 {
108     XcmsCCC pDefaultCCC = XcmsDefaultCCC(dpy, screenNumber);
109     XcmsCCC newccc;
110     XcmsIntensityMap *pIMap;
111     XcmsPerScrnInfo *pNewScrnInfo;
112 
113     if (pDefaultCCC == NULL ||
114 	    !(newccc = Xcalloc(1, sizeof(XcmsCCCRec)))) {
115 	return(NULL);
116     }
117 
118     /*
119      * Should inherit the following as result of a memmove():
120      *		dpy
121      *		screenNumber
122      *		pPerScrnInfo
123      */
124     memcpy((char *)newccc, (char *)pDefaultCCC, sizeof(XcmsCCCRec));
125     if (clientWhitePt) {
126 	memcpy((char *)&newccc->clientWhitePt, (char *)clientWhitePt,
127 		sizeof(XcmsColor));
128     }
129     if (gamutCompProc) {
130 	newccc->gamutCompProc = gamutCompProc;
131     }
132     if (gamutCompClientData) {
133 	newccc->gamutCompClientData = gamutCompClientData;
134     }
135     if (whitePtAdjProc) {
136 	newccc->whitePtAdjProc = whitePtAdjProc;
137     }
138     if (whitePtAdjClientData) {
139 	newccc->whitePtAdjClientData = whitePtAdjClientData;
140     }
141 
142     /*
143      * Now check our list of per-Visual Intensity tables.
144      * If one exists replace the pPerScrnInfo.
145      */
146     if ((pIMap = _XcmsGetIntensityMap(dpy, visual)) != NULL) {
147 	if (!(pNewScrnInfo = Xcalloc(1, sizeof(XcmsPerScrnInfo)))) {
148 	    Xfree(newccc);
149 	    return(NULL);
150 	}
151 	memcpy((char *)pNewScrnInfo, (char *)newccc->pPerScrnInfo,
152 		sizeof(XcmsPerScrnInfo));
153 	pNewScrnInfo->screenData = pIMap->screenData;
154 	newccc->pPerScrnInfo = pNewScrnInfo;
155     }
156 
157     /*
158      * Set visual component
159      */
160     newccc->visual = visual;
161 
162     return(newccc);
163 }
164 
165 
166 /*
167  *	NAME
168  *		XcmsDefaultCCC
169  *
170  *	SYNOPSIS
171  */
172 XcmsCCC
XcmsDefaultCCC(Display * dpy,int screenNumber)173 XcmsDefaultCCC(
174     Display *dpy,
175     int screenNumber)
176 /*
177  *	DESCRIPTION
178  *		Given a Display and Screen, this routine creates
179  *		returns the Screen's default Color Conversion Context.
180  *		Note that a Screen's default CCC is built with the
181  *		screen default visual.
182  *
183  *	RETURNS
184  *		Returns NULL if failed; otherwise address of the
185  *		XcmsCCC for the Screen's default CCC.
186  *
187  */
188 {
189     XcmsCCC ccc;
190 
191 
192     if ((screenNumber < 0) || (screenNumber >= ScreenCount(dpy))) {
193 	return((XcmsCCC)NULL);
194     }
195 
196     /*
197      * Check if the XcmsCCC's for each screen has been created
198      */
199     if ((XcmsCCC)dpy->cms.defaultCCCs == NULL) {
200 	if (!_XcmsInitDefaultCCCs(dpy)) {
201 	    return((XcmsCCC)NULL);
202 	}
203     }
204 
205     ccc = (XcmsCCC)dpy->cms.defaultCCCs + screenNumber;
206 
207     if (!ccc->pPerScrnInfo) {
208 	/*
209 	 * Need to create the XcmsPerScrnInfo structure.  The
210 	 * _XcmsInitScrnInfo routine will create the XcmsPerScrnInfo
211 	 * structure as well as initialize its functionSet and pScreenData
212 	 * components.
213 	 */
214 	if (!_XcmsInitScrnInfo(dpy, screenNumber)) {
215 	    return((XcmsCCC)NULL);
216 	}
217 	return(ccc);
218     } else {
219 	/*
220 	 * If ccc->pPerScrnInfo->state == XcmsInitSuccess,
221 	 *    then the pPerScrnInfo component has already been initialized
222 	 *    therefore, just return ccc.
223 	 * If ccc->pPerScrnInfo->state == XcmsInitFailure,
224 	 *    then this means that we already attempted to initialize
225 	 *    the pPerScrnInfo component but failed therefore stuffing
226 	 *    the pPerScrnInfo component with defaults.  Just return ccc.
227 	 * If ccc->pPerScrnInfo->state == XcmsInitNone,
228 	 *    then attempt to initialize the pPerScrnInfo component.
229 	 */
230 	switch (ccc->pPerScrnInfo->state) {
231 	   case XcmsInitFailure :
232 	    /* fall through */
233 	   case XcmsInitSuccess :
234 	    return(ccc);
235 	   case XcmsInitNone :
236 	    /* XcmsPerScreenInfo has not been initialized */
237 	    if (!_XcmsInitScrnInfo(dpy, screenNumber)) {
238 		return((XcmsCCC)NULL);
239 	    }
240 	    return(ccc);
241 	   default :
242 	    return((XcmsCCC)NULL);
243 	}
244     }
245 }
246 
247 
248 /*
249  *	NAME
250  *		XcmsFreeCCC
251  *
252  *	SYNOPSIS
253  */
254 void
XcmsFreeCCC(XcmsCCC ccc)255 XcmsFreeCCC(XcmsCCC ccc)
256 /*
257  *	DESCRIPTION
258  *		Frees memory associated with a Color Conversion Context
259  *		that was created with XcmsCreateCCC().
260  *
261  *	RETURNS
262  *		void
263  *
264  */
265 {
266     if (ccc->dpy->cms.defaultCCCs &&
267 	ccc == ((XcmsCCC)ccc->dpy->cms.defaultCCCs) + ccc->screenNumber) {
268 	/* do not allow clients to free DefaultCCC's */
269 	return;
270     }
271 
272     /*
273      * Note that XcmsPerScrnInfo sub-structures are freed here only if
274      * they are for visuals that have per-Visual intensity tables.
275      * Otherwise the XcmsPerScrnInfo structure is being shared!
276      * For the latter, there is only one allocated per Screen and it just
277      * so happens * that we place its initial reference is placed in the
278      * 	default CCC.  The routine _XcmsFreeDefaultCCCs frees them.
279      */
280     if (_XcmsGetIntensityMap(ccc->dpy, ccc->visual) != NULL) {
281 	Xfree(ccc->pPerScrnInfo);
282     }
283 
284     Xfree(ccc);
285 }
286