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 *		XcmsSetGet.c
281ab64890Smrg *
291ab64890Smrg *	DESCRIPTION
301ab64890Smrg *		Source for _XcmsSetGetColors()
311ab64890Smrg *
321ab64890Smrg *
331ab64890Smrg */
341ab64890Smrg
351ab64890Smrg/*
361ab64890Smrg *      EXTERNAL INCLUDES
371ab64890Smrg *              Include files that must be exported to any package or
381ab64890Smrg *              program using this package.
391ab64890Smrg */
401ab64890Smrg#ifdef HAVE_CONFIG_H
411ab64890Smrg#include <config.h>
421ab64890Smrg#endif
431ab64890Smrg#include "Xlibint.h"
441ab64890Smrg#include "Xcmsint.h"
451ab64890Smrg#include "Cv.h"
461ab64890Smrg
471ab64890Smrg
481ab64890Smrg/************************************************************************
491ab64890Smrg *									*
501ab64890Smrg *			API PRIVATE ROUTINES				*
511ab64890Smrg *									*
521ab64890Smrg ************************************************************************/
531ab64890Smrg
541ab64890Smrg/*
551ab64890Smrg *	NAME
5661b2299dSmrg *		XcmsSetColor -
571ab64890Smrg *
581ab64890Smrg *	SYNOPSIS
591ab64890Smrg */
601ab64890SmrgStatus
611ab64890Smrg_XcmsSetGetColor(
621ab64890Smrg    Status (*xColorProc)(
631ab64890Smrg        Display*            /* display */,
641ab64890Smrg        Colormap            /* colormap */,
651ab64890Smrg        XColor*             /* screen_in_out */),
661ab64890Smrg    Display *dpy,
671ab64890Smrg    Colormap cmap,
681ab64890Smrg    XcmsColor *pColors_in_out,
691ab64890Smrg    XcmsColorFormat result_format,
701ab64890Smrg    Bool *pCompressed)
711ab64890Smrg/*
721ab64890Smrg *	DESCRIPTION
731ab64890Smrg *		Routine containing code common to:
741ab64890Smrg *			XcmsAllocColor
751ab64890Smrg *			XcmsQueryColor
761ab64890Smrg *			XcmsStoreColor
771ab64890Smrg *
781ab64890Smrg *	RETURNS
791ab64890Smrg *		XcmsFailure if failed;
801ab64890Smrg *		XcmsSuccess if it succeeded without gamut compression;
811ab64890Smrg *		XcmsSuccessWithCompression if it succeeded with gamut
821ab64890Smrg *			compression;
831ab64890Smrg */
841ab64890Smrg{
851ab64890Smrg    XcmsCCC ccc;
861ab64890Smrg    XColor XColors_in_out;
871ab64890Smrg    Status retval = XcmsSuccess;
881ab64890Smrg
891ab64890Smrg    /*
901ab64890Smrg     * Argument Checking
911ab64890Smrg     *	1. Assume xColorProc is correct
921ab64890Smrg     *	2. Insure ccc not NULL
931ab64890Smrg     *	3. Assume cmap correct (should be checked by Server)
941ab64890Smrg     *	4. Insure pColors_in_out valid
951ab64890Smrg     *	5. Assume method_in is valid (should be checked by Server)
961ab64890Smrg     */
971ab64890Smrg
981ab64890Smrg    if (dpy == NULL) {
991ab64890Smrg	return(XcmsFailure);
1001ab64890Smrg    }
1011ab64890Smrg
1021ab64890Smrg    if (result_format == XcmsUndefinedFormat) {
1031ab64890Smrg	return(XcmsFailure);
1041ab64890Smrg    }
1051ab64890Smrg
1061ab64890Smrg    if ( !((*xColorProc == XAllocColor) || (*xColorProc == XStoreColor)
1071ab64890Smrg	    || (*xColorProc == XQueryColor)) ) {
1081ab64890Smrg	return(XcmsFailure);
1091ab64890Smrg    }
1101ab64890Smrg
1111ab64890Smrg    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
1121ab64890Smrg	return(XcmsFailure);
1131ab64890Smrg    }
1141ab64890Smrg
1151ab64890Smrg    if (*xColorProc == XQueryColor) {
1161ab64890Smrg	goto Query;
1171ab64890Smrg    }
1181ab64890Smrg
1191ab64890Smrg    /*
1201ab64890Smrg     * Convert to RGB, adjusting for white point differences if necessary.
1211ab64890Smrg     */
1221ab64890Smrg    if ((retval = XcmsConvertColors(ccc, pColors_in_out, 1, XcmsRGBFormat,
1231ab64890Smrg	    pCompressed)) == XcmsFailure) {
1241ab64890Smrg	return(XcmsFailure);
1251ab64890Smrg    }
1261ab64890Smrg
1271ab64890SmrgQuery:
1281ab64890Smrg    /*
1291ab64890Smrg     * Convert XcmsColor to XColor structures
1301ab64890Smrg     */
1311ab64890Smrg    _XcmsRGB_to_XColor(pColors_in_out, &XColors_in_out, 1);
1321ab64890Smrg
1331ab64890Smrg    /*
1341ab64890Smrg     * Now make appropriate X Call
1351ab64890Smrg     */
1361ab64890Smrg    if (*xColorProc == XAllocColor) {
1371ab64890Smrg	if ((*xColorProc)(ccc->dpy, cmap, &XColors_in_out) == 0) {
1381ab64890Smrg	    return(XcmsFailure);
1391ab64890Smrg	}
1401ab64890Smrg    } else if ((*xColorProc == XQueryColor) || (*xColorProc == XStoreColor)) {
1411ab64890Smrg	/* Note: XQueryColor and XStoreColor do not return any Status */
1421ab64890Smrg	(*xColorProc)(ccc->dpy, cmap, &XColors_in_out);
1431ab64890Smrg    } else {
1441ab64890Smrg	return(XcmsFailure);
1451ab64890Smrg    }
1461ab64890Smrg
1471ab64890Smrg    if ((*xColorProc == XStoreColor)) {
1481ab64890Smrg	return(retval);
1491ab64890Smrg    }
1501ab64890Smrg
1511ab64890Smrg    /*
1521ab64890Smrg     * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures
1531ab64890Smrg     */
1541ab64890Smrg    _XColor_to_XcmsRGB(ccc, &XColors_in_out, pColors_in_out, 1);
1551ab64890Smrg
1561ab64890Smrg    /*
1571ab64890Smrg     * Then, convert XcmsColor structures to the original specification
1581ab64890Smrg     *    format.  Note that we must use NULL instead of passing
1591ab64890Smrg     *    pCompressed.
1601ab64890Smrg     */
1611ab64890Smrg
1621ab64890Smrg    if (result_format != XcmsRGBFormat) {
1631ab64890Smrg	if (XcmsConvertColors(ccc, pColors_in_out, 1, result_format,
1641ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
1651ab64890Smrg	    return(XcmsFailure);
1661ab64890Smrg	}
1671ab64890Smrg    }
1681ab64890Smrg    return(retval);
1691ab64890Smrg}
1701ab64890Smrg
1711ab64890Smrg/*
1721ab64890Smrg *	NAME
17361b2299dSmrg *		XcmsSetColors -
1741ab64890Smrg *
1751ab64890Smrg *	SYNOPSIS
1761ab64890Smrg */
1771ab64890SmrgStatus
1781ab64890Smrg_XcmsSetGetColors(
1791ab64890Smrg    Status (*xColorProc)(
1801ab64890Smrg        Display*            /* display */,
1811ab64890Smrg        Colormap            /* colormap */,
1821ab64890Smrg        XColor*             /* screen_in_out */,
1831ab64890Smrg        int                 /* nColors */),
1841ab64890Smrg    Display *dpy,
1851ab64890Smrg    Colormap cmap,
1861ab64890Smrg    XcmsColor *pColors_in_out,
1871ab64890Smrg    int nColors,
1881ab64890Smrg    XcmsColorFormat result_format,
1891ab64890Smrg    Bool *pCompressed)
1901ab64890Smrg/*
1911ab64890Smrg *	DESCRIPTION
1921ab64890Smrg *		Routine containing code common to:
1931ab64890Smrg *			XcmsQueryColors
1941ab64890Smrg *			XcmsStoreColors
1951ab64890Smrg *
1961ab64890Smrg *	RETURNS
1971ab64890Smrg *		XcmsFailure if failed;
1981ab64890Smrg *		XcmsSuccess if it succeeded without gamut compression;
1991ab64890Smrg *		XcmsSuccessWithCompression if it succeeded with gamut
2001ab64890Smrg *			compression;
2011ab64890Smrg */
2021ab64890Smrg{
2031ab64890Smrg    XcmsCCC ccc;
2041ab64890Smrg    XColor *pXColors_in_out;
2051ab64890Smrg    Status retval = XcmsSuccess;
2061ab64890Smrg
2071ab64890Smrg    /*
2081ab64890Smrg     * Argument Checking
2091ab64890Smrg     *	1. Assume xColorProc is correct
2101ab64890Smrg     *	2. Insure ccc not NULL
2111ab64890Smrg     *	3. Assume cmap correct (should be checked by Server)
2121ab64890Smrg     *	4. Insure pColors_in_out valid
2131ab64890Smrg     *	5. Assume method_in is valid (should be checked by Server)
2141ab64890Smrg     *	6. Insure nColors > 0
2151ab64890Smrg     */
2161ab64890Smrg
2171ab64890Smrg    if (dpy == NULL) {
2181ab64890Smrg	return(XcmsFailure);
2191ab64890Smrg    }
2201ab64890Smrg
2211ab64890Smrg    if (nColors == 0) {
2221ab64890Smrg	return(XcmsSuccess);
2231ab64890Smrg    }
2241ab64890Smrg
2251ab64890Smrg    if (result_format == XcmsUndefinedFormat) {
2261ab64890Smrg	return(XcmsFailure);
2271ab64890Smrg    }
2281ab64890Smrg
2291ab64890Smrg    if ( !((*xColorProc == XStoreColors) || (*xColorProc == XQueryColors)) ) {
2301ab64890Smrg	return(XcmsFailure);
2311ab64890Smrg    }
2321ab64890Smrg
2331ab64890Smrg    if ((ccc = XcmsCCCOfColormap(dpy, cmap)) == (XcmsCCC)NULL) {
2341ab64890Smrg	return(XcmsFailure);
2351ab64890Smrg    }
2361ab64890Smrg
2371ab64890Smrg    /*
2381ab64890Smrg     * Allocate space for XColors
2391ab64890Smrg     */
240818534a1Smrg    if ((pXColors_in_out = Xcalloc(nColors, sizeof(XColor))) ==
2411ab64890Smrg	    NULL) {
2421ab64890Smrg	return(XcmsFailure);
2431ab64890Smrg    }
2441ab64890Smrg
2451ab64890Smrg    if (*xColorProc == XQueryColors) {
2461ab64890Smrg	goto Query;
2471ab64890Smrg    }
2481ab64890Smrg
2491ab64890Smrg    /*
2501ab64890Smrg     * Convert to RGB, adjusting for white point differences if necessary.
2511ab64890Smrg     */
2521ab64890Smrg    if ((retval = XcmsConvertColors(ccc, pColors_in_out, nColors, XcmsRGBFormat,
2531ab64890Smrg	    pCompressed)) == XcmsFailure) {
254818534a1Smrg        Xfree(pXColors_in_out);
2551ab64890Smrg	return(XcmsFailure);
2561ab64890Smrg    }
2571ab64890Smrg
2581ab64890SmrgQuery:
2591ab64890Smrg    /*
2601ab64890Smrg     * Convert XcmsColor to XColor structures
2611ab64890Smrg     */
2621ab64890Smrg    _XcmsRGB_to_XColor(pColors_in_out, pXColors_in_out, nColors);
2631ab64890Smrg
2641ab64890Smrg    /*
2651ab64890Smrg     * Now make appropriate X Call
2661ab64890Smrg     */
2671ab64890Smrg    if ((*xColorProc == XQueryColors) || (*xColorProc == XStoreColors)){
2681ab64890Smrg	/* Note: XQueryColors and XStoreColors do not return any Status */
2691ab64890Smrg	(*xColorProc)(ccc->dpy, cmap, pXColors_in_out, nColors);
2701ab64890Smrg    } else {
271818534a1Smrg	Xfree(pXColors_in_out);
2721ab64890Smrg	return(XcmsFailure);
2731ab64890Smrg    }
2741ab64890Smrg
2751ab64890Smrg    if (*xColorProc == XStoreColors) {
276818534a1Smrg	Xfree(pXColors_in_out);
2771ab64890Smrg	return(retval);
2781ab64890Smrg    }
2791ab64890Smrg
2801ab64890Smrg    /*
2811ab64890Smrg     * Now, convert the returned XColor (i.e., rgb) to XcmsColor structures
2821ab64890Smrg     */
2831ab64890Smrg    _XColor_to_XcmsRGB(ccc, pXColors_in_out, pColors_in_out, nColors);
284818534a1Smrg    Xfree(pXColors_in_out);
2851ab64890Smrg
2861ab64890Smrg    /*
2871ab64890Smrg     * Then, convert XcmsColor structures to the original specification
2881ab64890Smrg     *    format.  Note that we must use NULL instead of passing
2891ab64890Smrg     *    pCompressed.
2901ab64890Smrg     */
2911ab64890Smrg    if (result_format != XcmsRGBFormat) {
2921ab64890Smrg	if (XcmsConvertColors(ccc, pColors_in_out, nColors, result_format,
2931ab64890Smrg		(Bool *) NULL) == XcmsFailure) {
2941ab64890Smrg	    return(XcmsFailure);
2951ab64890Smrg	}
2961ab64890Smrg    }
2971ab64890Smrg
2981ab64890Smrg    return(retval);
2991ab64890Smrg}
3001ab64890Smrg
3011ab64890Smrg/* ### EOF ### */
302