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