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 *	NAME
261ab64890Smrg *		CIExyY.c
271ab64890Smrg *
281ab64890Smrg *	DESCRIPTION
291ab64890Smrg *		This file contains routines that support the CIE xyY
301ab64890Smrg *		color space to include conversions to and from the CIE
311ab64890Smrg *		XYZ space.
321ab64890Smrg *
331ab64890Smrg *	DOCUMENTATION
341ab64890Smrg *		"TekColor Color Management System, System Implementor's Manual"
351ab64890Smrg */
361ab64890Smrg
371ab64890Smrg#ifdef HAVE_CONFIG_H
381ab64890Smrg#include <config.h>
391ab64890Smrg#endif
401ab64890Smrg#include <stdio.h>
411ab64890Smrg#include <X11/Xos.h>
421ab64890Smrg#include "Xlibint.h"
431ab64890Smrg#include "Xcmsint.h"
441ab64890Smrg#include "Cv.h"
451ab64890Smrg
461ab64890Smrg/*
471ab64890Smrg *	DEFINES
481ab64890Smrg */
491ab64890Smrg#define EPS 0.00001	/* some extremely small number */
501ab64890Smrg#ifdef DBL_EPSILON
511ab64890Smrg#  define XMY_DBL_EPSILON DBL_EPSILON
521ab64890Smrg#else
531ab64890Smrg#  define XMY_DBL_EPSILON 0.00001
541ab64890Smrg#endif
551ab64890Smrg
561ab64890Smrg/*
571ab64890Smrg *	FORWARD DECLARATIONS
581ab64890Smrg */
591ab64890Smrg
601ab64890Smrgstatic int CIExyY_ParseString(register char *spec, XcmsColor *pColor);
611ab64890Smrgstatic Status XcmsCIExyY_ValidSpec(XcmsColor *pColor);
621ab64890Smrg
631ab64890Smrg
641ab64890Smrg/*
651ab64890Smrg *	LOCAL VARIABLES
661ab64890Smrg */
671ab64890Smrg
681ab64890Smrg    /*
691ab64890Smrg     * NULL terminated list of functions applied to get from CIExyY to CIEXYZ
701ab64890Smrg     */
711ab64890Smrgstatic XcmsConversionProc Fl_CIExyY_to_CIEXYZ[] = {
721ab64890Smrg    XcmsCIExyYToCIEXYZ,
731ab64890Smrg    NULL
741ab64890Smrg};
751ab64890Smrg
761ab64890Smrg    /*
771ab64890Smrg     * NULL terminated list of functions applied to get from CIEXYZ to CIExyY
781ab64890Smrg     */
791ab64890Smrgstatic XcmsConversionProc Fl_CIEXYZ_to_CIExyY[] = {
801ab64890Smrg    XcmsCIEXYZToCIExyY,
811ab64890Smrg    NULL
821ab64890Smrg};
831ab64890Smrg
841ab64890Smrg
851ab64890Smrg/*
861ab64890Smrg *	GLOBALS
871ab64890Smrg */
881ab64890Smrg
891ab64890Smrg    /*
901ab64890Smrg     * CIE xyY Color Space
911ab64890Smrg     */
921ab64890SmrgXcmsColorSpace	XcmsCIExyYColorSpace =
931ab64890Smrg    {
941ab64890Smrg	_XcmsCIExyY_prefix,	/* prefix */
951ab64890Smrg	XcmsCIExyYFormat,		/* id */
961ab64890Smrg	CIExyY_ParseString,	/* parseString */
971ab64890Smrg	Fl_CIExyY_to_CIEXYZ,	/* to_CIEXYZ */
981ab64890Smrg	Fl_CIEXYZ_to_CIExyY,	/* from_CIEXYZ */
991ab64890Smrg	1
1001ab64890Smrg    };
1011ab64890Smrg
1021ab64890Smrg
1031ab64890Smrg
1041ab64890Smrg/************************************************************************
1051ab64890Smrg *									*
1061ab64890Smrg *			 PRIVATE ROUTINES				*
1071ab64890Smrg *									*
1081ab64890Smrg ************************************************************************/
1091ab64890Smrg
1101ab64890Smrg/*
1111ab64890Smrg *	NAME
1121ab64890Smrg *		CIExyY_ParseString
1131ab64890Smrg *
1141ab64890Smrg *	SYNOPSIS
1151ab64890Smrg */
1161ab64890Smrgstatic int
1171ab64890SmrgCIExyY_ParseString(
1181ab64890Smrg    register char *spec,
1191ab64890Smrg    XcmsColor *pColor)
1201ab64890Smrg/*
1211ab64890Smrg *	DESCRIPTION
1221ab64890Smrg *		This routines takes a string and attempts to convert
1231ab64890Smrg *		it into a XcmsColor structure with XcmsCIExyYFormat.
1241ab64890Smrg *		The assumed CIExyY string syntax is:
1251ab64890Smrg *		    CIExyY:<x>/<y>/<Y>
1261ab64890Smrg *		Where x, y, and Y are in string input format for floats
1271ab64890Smrg *		consisting of:
1281ab64890Smrg *		    a. an optional sign
1291ab64890Smrg *		    b. a string of numbers possibly containing a decimal point,
1301ab64890Smrg *		    c. an optional exponent field containing an 'E' or 'e'
1311ab64890Smrg *			followed by a possibly signed integer string.
1321ab64890Smrg *
1331ab64890Smrg *	RETURNS
1341ab64890Smrg *		0 if failed, non-zero otherwise.
1351ab64890Smrg */
1361ab64890Smrg{
1371ab64890Smrg    int n;
1381ab64890Smrg    char *pchar;
1391ab64890Smrg
1401ab64890Smrg    if ((pchar = strchr(spec, ':')) == NULL) {
1411ab64890Smrg	return(XcmsFailure);
1421ab64890Smrg    }
1431ab64890Smrg    n = (int)(pchar - spec);
1441ab64890Smrg
1451ab64890Smrg    /*
1461ab64890Smrg     * Check for proper prefix.
1471ab64890Smrg     */
1489c019ec5Smaya    if (strncmp(spec, _XcmsCIExyY_prefix, (size_t)n) != 0) {
1491ab64890Smrg	return(XcmsFailure);
1501ab64890Smrg    }
1511ab64890Smrg
1521ab64890Smrg    /*
1531ab64890Smrg     * Attempt to parse the value portion.
1541ab64890Smrg     */
1551ab64890Smrg    if (sscanf(spec + n + 1, "%lf/%lf/%lf",
1561ab64890Smrg	    &pColor->spec.CIExyY.x,
1571ab64890Smrg	    &pColor->spec.CIExyY.y,
1581ab64890Smrg	    &pColor->spec.CIExyY.Y) != 3) {
1591ab64890Smrg        char *s; /* Maybe failed due to locale */
1601ab64890Smrg        int f;
1611ab64890Smrg        if ((s = strdup(spec))) {
1621ab64890Smrg            for (f = 0; s[f]; ++f)
1631ab64890Smrg                if (s[f] == '.')
1641ab64890Smrg                    s[f] = ',';
1651ab64890Smrg                else if (s[f] == ',')
1661ab64890Smrg                    s[f] = '.';
1671ab64890Smrg	    if (sscanf(s + n + 1, "%lf/%lf/%lf",
1681ab64890Smrg		       &pColor->spec.CIExyY.x,
1691ab64890Smrg		       &pColor->spec.CIExyY.y,
1701ab64890Smrg		       &pColor->spec.CIExyY.Y) != 3) {
1711ab64890Smrg                free(s);
1721ab64890Smrg                return(XcmsFailure);
1731ab64890Smrg            }
1741ab64890Smrg            free(s);
1751ab64890Smrg        } else
1761ab64890Smrg	    return(XcmsFailure);
1771ab64890Smrg    }
1781ab64890Smrg    pColor->format = XcmsCIExyYFormat;
1791ab64890Smrg    pColor->pixel = 0;
1801ab64890Smrg    return(XcmsCIExyY_ValidSpec(pColor));
1811ab64890Smrg}
1821ab64890Smrg
1831ab64890Smrg
1841ab64890Smrg
1851ab64890Smrg/************************************************************************
1861ab64890Smrg *									*
1871ab64890Smrg *			 PUBLIC ROUTINES				*
1881ab64890Smrg *									*
1891ab64890Smrg ************************************************************************/
1901ab64890Smrg
1911ab64890Smrg/*
1921ab64890Smrg *	NAME
1931ab64890Smrg *		CIExyY_ValidSpec()
1941ab64890Smrg *
1951ab64890Smrg *	SYNOPSIS
1961ab64890Smrg */
1971ab64890Smrgstatic Status
1981ab64890SmrgXcmsCIExyY_ValidSpec(
1991ab64890Smrg    XcmsColor *pColor)
2001ab64890Smrg/*
2011ab64890Smrg *	DESCRIPTION
2021ab64890Smrg *		Checks a valid CIExyY color specification.
2031ab64890Smrg *
2041ab64890Smrg *	RETURNS
2051ab64890Smrg *		XcmsFailure if invalid.
2061ab64890Smrg *		XcmsSuccess if valid.
2071ab64890Smrg *
2081ab64890Smrg */
2091ab64890Smrg{
2101ab64890Smrg    if (pColor->format != XcmsCIExyYFormat
2111ab64890Smrg	    ||
2121ab64890Smrg	    (pColor->spec.CIExyY.x < 0.0 - XMY_DBL_EPSILON)
2131ab64890Smrg	    ||
2141ab64890Smrg	    (pColor->spec.CIExyY.x > 1.0 + XMY_DBL_EPSILON)
2151ab64890Smrg	    ||
2161ab64890Smrg	    (pColor->spec.CIExyY.y < 0.0 - XMY_DBL_EPSILON)
2171ab64890Smrg	    ||
2181ab64890Smrg	    (pColor->spec.CIExyY.y > 1.0 + XMY_DBL_EPSILON)
2191ab64890Smrg	    ||
2201ab64890Smrg	    (pColor->spec.CIExyY.Y < 0.0 - XMY_DBL_EPSILON)
2211ab64890Smrg	    ||
2221ab64890Smrg	    (pColor->spec.CIExyY.Y > 1.0 + XMY_DBL_EPSILON)) {
2231ab64890Smrg	return(XcmsFailure);
2241ab64890Smrg    }
2251ab64890Smrg    return(XcmsSuccess);
2261ab64890Smrg}
2271ab64890Smrg
2281ab64890Smrg
2291ab64890Smrg/*
2301ab64890Smrg *	NAME
2311ab64890Smrg *		XcmsCIExyYToCIEXYZ - convert CIExyY to CIEXYZ
2321ab64890Smrg *
2331ab64890Smrg *	SYNOPSIS
2341ab64890Smrg */
2351ab64890SmrgStatus
2361ab64890SmrgXcmsCIExyYToCIEXYZ(
2371ab64890Smrg    XcmsCCC ccc,
2381ab64890Smrg    XcmsColor *pxyY_WhitePt,
2391ab64890Smrg    XcmsColor *pColors_in_out,
2401ab64890Smrg    unsigned int nColors)
2411ab64890Smrg/*
2421ab64890Smrg *	DESCRIPTION
2431ab64890Smrg *		Converts color specifications in an array of XcmsColor
2441ab64890Smrg *		structures from CIExyY format to CIEXYZ format.
2451ab64890Smrg *
2461ab64890Smrg *	RETURNS
2471ab64890Smrg *		XcmsFailure if failed,
2481ab64890Smrg *		XcmsSuccess if succeeded.
2491ab64890Smrg */
2501ab64890Smrg{
2511ab64890Smrg    XcmsColor	*pColor = pColors_in_out;
2521ab64890Smrg    XcmsColor	whitePt;
2531ab64890Smrg    XcmsCIEXYZ	XYZ_return;
2541ab64890Smrg    XcmsFloat	div;		/* temporary storage in case divisor is zero */
2551ab64890Smrg    XcmsFloat	u, v, x, y, z;	/* temporary storage */
2562d67cb4fSmrg    unsigned int i;
2571ab64890Smrg
2581ab64890Smrg    /*
2591ab64890Smrg     * Check arguments
2601ab64890Smrg     */
2611ab64890Smrg    if (pxyY_WhitePt == NULL || pColors_in_out == NULL) {
2621ab64890Smrg	return(XcmsFailure);
2631ab64890Smrg    }
2641ab64890Smrg
2651ab64890Smrg
2661ab64890Smrg    /*
2671ab64890Smrg     * Now convert each XcmsColor structure to CIEXYZ form
2681ab64890Smrg     */
2691ab64890Smrg    for (i = 0; i < nColors; i++, pColor++) {
2701ab64890Smrg	/* Make sure original format is CIExyY and valid */
2711ab64890Smrg	if (!XcmsCIExyY_ValidSpec(pColor)) {
2721ab64890Smrg	    return(XcmsFailure);
2731ab64890Smrg	}
2741ab64890Smrg
2751ab64890Smrg	if ((div = (-2 * pColor->spec.CIExyY.x) + (12 * pColor->spec.CIExyY.y) + 3) == 0.0) {
2761ab64890Smrg	    /* Note that the divisor is zero */
2771ab64890Smrg	    /* This return is abitrary. */
2781ab64890Smrg	    XYZ_return.X = 0;
2791ab64890Smrg	    XYZ_return.Y = 0;
2801ab64890Smrg	    XYZ_return.Z = 0;
2811ab64890Smrg	} else {
2821ab64890Smrg	    /*
2831ab64890Smrg	     * Make sure white point is in CIEXYZ form
2841ab64890Smrg	     */
2851ab64890Smrg	    if (pxyY_WhitePt->format != XcmsCIEXYZFormat) {
2861ab64890Smrg		/* Make copy of the white point because we're going to modify it */
2871ab64890Smrg		memcpy((char *)&whitePt, (char *)pxyY_WhitePt, sizeof(XcmsColor));
2881ab64890Smrg		if (!_XcmsDIConvertColors(ccc, &whitePt, (XcmsColor *)NULL, 1,
2891ab64890Smrg			XcmsCIEXYZFormat)) {
2901ab64890Smrg		    return(XcmsFailure);
2911ab64890Smrg		}
2921ab64890Smrg		pxyY_WhitePt = &whitePt;
2931ab64890Smrg	    }
2941ab64890Smrg
2951ab64890Smrg	    /* Make sure it is a white point, i.e., Y == 1.0 */
2961ab64890Smrg	    if (pxyY_WhitePt->spec.CIEXYZ.Y != 1.0) {
2971ab64890Smrg		return(XcmsFailure);
2981ab64890Smrg	    }
2991ab64890Smrg
3001ab64890Smrg	    /* Convert from xyY to uvY to XYZ */
3011ab64890Smrg	    u = (4 * pColor->spec.CIExyY.x) / div;
3021ab64890Smrg	    v = (9 * pColor->spec.CIExyY.y) / div;
3031ab64890Smrg	    div = (6.0 * u) - (16.0 * v) + 12.0;
3041ab64890Smrg	    if (div == 0.0) {
3051ab64890Smrg		/* Note that the divisor is zero */
3061ab64890Smrg		/* This return is abitrary. */
3071ab64890Smrg		if ((div = (6.0 * whitePt.spec.CIEuvY.u_prime) -
3081ab64890Smrg		           (16.0 * whitePt.spec.CIEuvY.v_prime) + 12.0) == 0.0) {
3091ab64890Smrg		    div = EPS;
3101ab64890Smrg		}
3111ab64890Smrg		x = 9.0 * whitePt.spec.CIEuvY.u_prime / div;
3121ab64890Smrg		y = 4.0 * whitePt.spec.CIEuvY.u_prime / div;
3131ab64890Smrg	    } else {
3141ab64890Smrg		/* convert u, v to small xyz */
3151ab64890Smrg		x = 9.0 * u / div;
3161ab64890Smrg		y = 4.0 * v / div;
3171ab64890Smrg	    }
3181ab64890Smrg	    z = 1.0 - x - y;
3191ab64890Smrg	    if (y == 0.0) y = EPS;	/* Have to worry about divide by 0 */
3201ab64890Smrg	    XYZ_return.Y = pColor->spec.CIExyY.Y;
3211ab64890Smrg	    XYZ_return.X = x * XYZ_return.Y / y;
3221ab64890Smrg	    XYZ_return.Z = z * XYZ_return.Y / y;
3231ab64890Smrg	}
3241ab64890Smrg
3251ab64890Smrg	/* Copy result to pColor */
3261ab64890Smrg	memcpy ((char *)&pColor->spec, (char *)&XYZ_return, sizeof(XcmsCIEXYZ));
3271ab64890Smrg
3281ab64890Smrg	/* Identify that the format is now CIEXYZ */
3291ab64890Smrg	pColor->format = XcmsCIEXYZFormat;
3301ab64890Smrg    }
3311ab64890Smrg    return(XcmsSuccess);
3321ab64890Smrg}
3331ab64890Smrg
3341ab64890Smrg
3351ab64890Smrg/*
3361ab64890Smrg *	NAME
3371ab64890Smrg *		XcmsCIEXYZToCIExyY - convert CIEXYZ to CIExyY
3381ab64890Smrg *
3391ab64890Smrg *	SYNOPSIS
3401ab64890Smrg */
3411ab64890Smrg/* ARGSUSED */
3421ab64890SmrgStatus
3431ab64890SmrgXcmsCIEXYZToCIExyY(
3441ab64890Smrg    XcmsCCC ccc,
3451ab64890Smrg    XcmsColor *pxyY_WhitePt,
3461ab64890Smrg    XcmsColor *pColors_in_out,
3471ab64890Smrg    unsigned int nColors)
3481ab64890Smrg/*
3491ab64890Smrg *	DESCRIPTION
3501ab64890Smrg *		Converts color specifications in an array of XcmsColor
3511ab64890Smrg *		structures from CIEXYZ format to CIExyY format.
3521ab64890Smrg *
3531ab64890Smrg *	RETURNS
3541ab64890Smrg *		XcmsFailure if failed,
3551ab64890Smrg *		XcmsSuccess if succeeded.
3561ab64890Smrg *
3571ab64890Smrg */
3581ab64890Smrg{
3591ab64890Smrg    XcmsColor	*pColor = pColors_in_out;
3601ab64890Smrg    XcmsCIExyY	xyY_return;
3611ab64890Smrg    XcmsFloat	div;		/* temporary storage in case divisor is zero */
3622d67cb4fSmrg    unsigned int i;
3631ab64890Smrg
3641ab64890Smrg    /*
3651ab64890Smrg     * Check arguments
3661ab64890Smrg     * 		pxyY_WhitePt ignored
3671ab64890Smrg     */
3681ab64890Smrg    if (pColors_in_out == NULL) {
3691ab64890Smrg	return(XcmsFailure);
3701ab64890Smrg    }
3711ab64890Smrg
3721ab64890Smrg    /*
3731ab64890Smrg     * Now convert each XcmsColor structure to CIEXYZ form
3741ab64890Smrg     */
3751ab64890Smrg    for (i = 0; i < nColors; i++, pColor++) {
3761ab64890Smrg
3771ab64890Smrg	if (!_XcmsCIEXYZ_ValidSpec(pColor)) {
3781ab64890Smrg	    return(XcmsFailure);
3791ab64890Smrg	}
3801ab64890Smrg	/* Now convert for XYZ to xyY */
3811ab64890Smrg	if ((div = pColor->spec.CIEXYZ.X + pColor->spec.CIEXYZ.Y + pColor->spec.CIEXYZ.Z) == 0.0) {
3821ab64890Smrg	    div = EPS;
38361b2299dSmrg	}
3841ab64890Smrg	xyY_return.x = pColor->spec.CIEXYZ.X / div;
3851ab64890Smrg	xyY_return.y = pColor->spec.CIEXYZ.Y / div;
3861ab64890Smrg	xyY_return.Y = pColor->spec.CIEXYZ.Y;
3871ab64890Smrg
3881ab64890Smrg	/* Copy result to pColor */
3891ab64890Smrg	memcpy ((char *)&pColor->spec, (char *)&xyY_return, sizeof(XcmsCIExyY));
3901ab64890Smrg
3911ab64890Smrg	/* Identify that the format is now CIEXYZ */
3921ab64890Smrg	pColor->format = XcmsCIExyYFormat;
3931ab64890Smrg    }
3941ab64890Smrg    return(XcmsSuccess);
3951ab64890Smrg}
396