SetGetCols.c revision 818534a1
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 *		XcmsSetGet.c
28 *
29 *	DESCRIPTION
30 *		Source for _XcmsSetGetColors()
31 *
32 *
33 */
34
35/*
36 *      EXTERNAL INCLUDES
37 *              Include files that must be exported to any package or
38 *              program using this package.
39 */
40#ifdef HAVE_CONFIG_H
41#include <config.h>
42#endif
43#include "Xlibint.h"
44#include "Xcmsint.h"
45#include "Cv.h"
46
47
48/************************************************************************
49 *									*
50 *			API PRIVATE ROUTINES				*
51 *									*
52 ************************************************************************/
53
54/*
55 *	NAME
56 *		XcmsSetColor -
57 *
58 *	SYNOPSIS
59 */
60Status
61_XcmsSetGetColor(
62    Status (*xColorProc)(
63        Display*            /* display */,
64        Colormap            /* colormap */,
65        XColor*             /* screen_in_out */),
66    Display *dpy,
67    Colormap cmap,
68    XcmsColor *pColors_in_out,
69    XcmsColorFormat result_format,
70    Bool *pCompressed)
71/*
72 *	DESCRIPTION
73 *		Routine containing code common to:
74 *			XcmsAllocColor
75 *			XcmsQueryColor
76 *			XcmsStoreColor
77 *
78 *	RETURNS
79 *		XcmsFailure if failed;
80 *		XcmsSuccess if it succeeded without gamut compression;
81 *		XcmsSuccessWithCompression if it succeeded with gamut
82 *			compression;
83 */
84{
85    XcmsCCC ccc;
86    XColor XColors_in_out;
87    Status retval = XcmsSuccess;
88
89    /*
90     * Argument Checking
91     *	1. Assume xColorProc is correct
92     *	2. Insure ccc not NULL
93     *	3. Assume cmap correct (should be checked by Server)
94     *	4. Insure pColors_in_out valid
95     *	5. Assume method_in is valid (should be checked by Server)
96     */
97
98    if (dpy == NULL) {
99	return(XcmsFailure);
100    }
101
102    if (result_format == XcmsUndefinedFormat) {
103	return(XcmsFailure);
104    }
105
106    if ( !((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor)
107	    || (*xColorProc == XQueryColor)) ) {
108	return(XcmsFailure);
109    }
110
111    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
112	return(XcmsFailure);
113    }
114
115    if (*xColorProc == XQueryColor) {
116	goto Query;
117    }
118
119    /*
120     * Convert to RGB, adjusting for white point differences if necessary.
121     */
122    if ((retval = XcmsConvertColors(ccc, pColors_in_out, 1, XcmsRGBFormat,
123	    pCompressed)) == XcmsFailure) {
124	return(XcmsFailure);
125    }
126
127Query:
128    /*
129     * Convert XcmsColor to XColor structures
130     */
131    _XcmsRGB_to_XColor(pColors_in_out, &XColors_in_out, 1);
132
133    /*
134     * Now make appropriate X Call
135     */
136    if (*xColorProc == XAllocColor) {
137	if ((*xColorProc)(ccc->dpy, cmap, &XColors_in_out) == 0) {
138	    return(XcmsFailure);
139	}
140    } else if ((*xColorProc == XQueryColor) || (*xColorProc == XStoreColor)) {
141	/* Note: XQueryColor and XStoreColor do not return any Status */
142	(*xColorProc)(ccc->dpy, cmap, &XColors_in_out);
143    } else {
144	return(XcmsFailure);
145    }
146
147    if ((*xColorProc == XStoreColor)) {
148	return(retval);
149    }
150
151    /*
152     * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures
153     */
154    _XColor_to_XcmsRGB(ccc, &XColors_in_out, pColors_in_out, 1);
155
156    /*
157     * Then, convert XcmsColor structures to the original specification
158     *    format.  Note that we must use NULL instead of passing
159     *    pCompressed.
160     */
161
162    if (result_format != XcmsRGBFormat) {
163	if (XcmsConvertColors(ccc, pColors_in_out, 1, result_format,
164		(Bool *) NULL) == XcmsFailure) {
165	    return(XcmsFailure);
166	}
167    }
168    return(retval);
169}
170
171/*
172 *	NAME
173 *		XcmsSetColors -
174 *
175 *	SYNOPSIS
176 */
177Status
178_XcmsSetGetColors(
179    Status (*xColorProc)(
180        Display*            /* display */,
181        Colormap            /* colormap */,
182        XColor*             /* screen_in_out */,
183        int                 /* nColors */),
184    Display *dpy,
185    Colormap cmap,
186    XcmsColor *pColors_in_out,
187    int nColors,
188    XcmsColorFormat result_format,
189    Bool *pCompressed)
190/*
191 *	DESCRIPTION
192 *		Routine containing code common to:
193 *			XcmsQueryColors
194 *			XcmsStoreColors
195 *
196 *	RETURNS
197 *		XcmsFailure if failed;
198 *		XcmsSuccess if it succeeded without gamut compression;
199 *		XcmsSuccessWithCompression if it succeeded with gamut
200 *			compression;
201 */
202{
203    XcmsCCC ccc;
204    XColor *pXColors_in_out;
205    Status retval = XcmsSuccess;
206
207    /*
208     * Argument Checking
209     *	1. Assume xColorProc is correct
210     *	2. Insure ccc not NULL
211     *	3. Assume cmap correct (should be checked by Server)
212     *	4. Insure pColors_in_out valid
213     *	5. Assume method_in is valid (should be checked by Server)
214     *	6. Insure nColors > 0
215     */
216
217    if (dpy == NULL) {
218	return(XcmsFailure);
219    }
220
221    if (nColors == 0) {
222	return(XcmsSuccess);
223    }
224
225    if (result_format == XcmsUndefinedFormat) {
226	return(XcmsFailure);
227    }
228
229    if ( !((*xColorProc == XStoreColors) || (*xColorProc == XQueryColors)) ) {
230	return(XcmsFailure);
231    }
232
233    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
234	return(XcmsFailure);
235    }
236
237    /*
238     * Allocate space for XColors
239     */
240    if ((pXColors_in_out = Xcalloc(nColors, sizeof(XColor))) ==
241	    NULL) {
242	return(XcmsFailure);
243    }
244
245    if (*xColorProc == XQueryColors) {
246	goto Query;
247    }
248
249    /*
250     * Convert to RGB, adjusting for white point differences if necessary.
251     */
252    if ((retval = XcmsConvertColors(ccc, pColors_in_out, nColors, XcmsRGBFormat,
253	    pCompressed)) == XcmsFailure) {
254        Xfree(pXColors_in_out);
255	return(XcmsFailure);
256    }
257
258Query:
259    /*
260     * Convert XcmsColor to XColor structures
261     */
262    _XcmsRGB_to_XColor(pColors_in_out, pXColors_in_out, nColors);
263
264    /*
265     * Now make appropriate X Call
266     */
267    if ((*xColorProc == XQueryColors) || (*xColorProc == XStoreColors)){
268	/* Note: XQueryColors and XStoreColors do not return any Status */
269	(*xColorProc)(ccc->dpy, cmap, pXColors_in_out, nColors);
270    } else {
271	Xfree(pXColors_in_out);
272	return(XcmsFailure);
273    }
274
275    if (*xColorProc == XStoreColors) {
276	Xfree(pXColors_in_out);
277	return(retval);
278    }
279
280    /*
281     * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures
282     */
283    _XColor_to_XcmsRGB(ccc, pXColors_in_out, pColors_in_out, nColors);
284    Xfree(pXColors_in_out);
285
286    /*
287     * Then, convert XcmsColor structures to the original specification
288     *    format.  Note that we must use NULL instead of passing
289     *    pCompressed.
290     */
291    if (result_format != XcmsRGBFormat) {
292	if (XcmsConvertColors(ccc, pColors_in_out, nColors, result_format,
293		(Bool *) NULL) == XcmsFailure) {
294	    return(XcmsFailure);
295	}
296    }
297
298    return(retval);
299}
300
301/* ### EOF ### */
302