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 * CIEuvy.c 281ab64890Smrg * 291ab64890Smrg * DESCRIPTION 301ab64890Smrg * This file contains routines that support the CIE u'v'Y 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 381ab64890Smrg#ifdef HAVE_CONFIG_H 391ab64890Smrg#include <config.h> 401ab64890Smrg#endif 411ab64890Smrg#include <X11/Xos.h> 421ab64890Smrg#include "Xlibint.h" 431ab64890Smrg#include "Xcmsint.h" 441ab64890Smrg#include "Cv.h" 451ab64890Smrg 461ab64890Smrg#include <stdio.h> 471ab64890Smrg 481ab64890Smrg/* 491ab64890Smrg * FORWARD DECLARATIONS 501ab64890Smrg */ 511ab64890Smrgstatic int CIEuvY_ParseString(register char *spec, XcmsColor *pColor); 521ab64890Smrg 531ab64890Smrg/* 541ab64890Smrg * DEFINES 551ab64890Smrg * Internal definitions that need NOT be exported to any package 561ab64890Smrg * or program using this package. 571ab64890Smrg */ 581ab64890Smrg#ifdef DBL_EPSILON 591ab64890Smrg# define XMY_DBL_EPSILON DBL_EPSILON 601ab64890Smrg#else 611ab64890Smrg# define XMY_DBL_EPSILON 0.00001 621ab64890Smrg#endif 631ab64890Smrg 641ab64890Smrg 651ab64890Smrg/* 661ab64890Smrg * LOCAL VARIABLES 671ab64890Smrg */ 681ab64890Smrg 691ab64890Smrg /* 701ab64890Smrg * NULL terminated list of functions applied to get from CIEuvY to CIEXYZ 711ab64890Smrg */ 721ab64890Smrgstatic XcmsConversionProc Fl_CIEuvY_to_CIEXYZ[] = { 731ab64890Smrg XcmsCIEuvYToCIEXYZ, 741ab64890Smrg NULL 751ab64890Smrg}; 761ab64890Smrg 771ab64890Smrg /* 781ab64890Smrg * NULL terminated list of functions applied to get from CIEXYZ to CIEuvY 791ab64890Smrg */ 801ab64890Smrgstatic XcmsConversionProc Fl_CIEXYZ_to_CIEuvY[] = { 811ab64890Smrg XcmsCIEXYZToCIEuvY, 821ab64890Smrg NULL 831ab64890Smrg}; 841ab64890Smrg 851ab64890Smrg 861ab64890Smrg/* 871ab64890Smrg * GLOBALS 881ab64890Smrg */ 891ab64890Smrg 901ab64890Smrg /* 911ab64890Smrg * CIE uvY Color Space 921ab64890Smrg */ 931ab64890SmrgXcmsColorSpace XcmsCIEuvYColorSpace = 941ab64890Smrg { 951ab64890Smrg _XcmsCIEuvY_prefix, /* prefix */ 961ab64890Smrg XcmsCIEuvYFormat, /* id */ 971ab64890Smrg CIEuvY_ParseString, /* parseString */ 981ab64890Smrg Fl_CIEuvY_to_CIEXYZ, /* to_CIEXYZ */ 991ab64890Smrg Fl_CIEXYZ_to_CIEuvY, /* from_CIEXYZ */ 1001ab64890Smrg 1 1011ab64890Smrg }; 1021ab64890Smrg 1031ab64890Smrg 1041ab64890Smrg 1051ab64890Smrg/************************************************************************ 1061ab64890Smrg * * 1071ab64890Smrg * PRIVATE ROUTINES * 1081ab64890Smrg * * 1091ab64890Smrg ************************************************************************/ 1101ab64890Smrg 1111ab64890Smrg/* 1121ab64890Smrg * NAME 1131ab64890Smrg * CIEuvY_ParseString 1141ab64890Smrg * 1151ab64890Smrg * SYNOPSIS 1161ab64890Smrg */ 1171ab64890Smrgstatic int 1181ab64890SmrgCIEuvY_ParseString( 1191ab64890Smrg register char *spec, 1201ab64890Smrg XcmsColor *pColor) 1211ab64890Smrg/* 1221ab64890Smrg * DESCRIPTION 1231ab64890Smrg * This routines takes a string and attempts to convert 1241ab64890Smrg * it into a XcmsColor structure with XcmsCIEuvYFormat. 1251ab64890Smrg * The assumed CIEuvY string syntax is: 1261ab64890Smrg * CIEuvY:<u>/<v>/<Y> 1271ab64890Smrg * Where u, v, and Y are in string input format for floats 1281ab64890Smrg * consisting of: 1291ab64890Smrg * a. an optional sign 1301ab64890Smrg * b. a string of numbers possibly containing a decimal point, 1311ab64890Smrg * c. an optional exponent field containing an 'E' or 'e' 1321ab64890Smrg * followed by a possibly signed integer string. 1331ab64890Smrg * 1341ab64890Smrg * RETURNS 1351ab64890Smrg * 0 if failed, non-zero otherwise. 1361ab64890Smrg */ 1371ab64890Smrg{ 1382d67cb4fSmrg size_t n; 1391ab64890Smrg char *pchar; 1401ab64890Smrg 1411ab64890Smrg if ((pchar = strchr(spec, ':')) == NULL) { 1421ab64890Smrg return(XcmsFailure); 1431ab64890Smrg } 1442d67cb4fSmrg n = (size_t)(pchar - spec); 1451ab64890Smrg 1461ab64890Smrg /* 1471ab64890Smrg * Check for proper prefix. 1481ab64890Smrg */ 1491ab64890Smrg if (strncmp(spec, _XcmsCIEuvY_prefix, n) != 0) { 1501ab64890Smrg return(XcmsFailure); 1511ab64890Smrg } 1521ab64890Smrg 1531ab64890Smrg /* 1541ab64890Smrg * Attempt to parse the value portion. 1551ab64890Smrg */ 1561ab64890Smrg if (sscanf(spec + n + 1, "%lf/%lf/%lf", 1571ab64890Smrg &pColor->spec.CIEuvY.u_prime, 1581ab64890Smrg &pColor->spec.CIEuvY.v_prime, 1591ab64890Smrg &pColor->spec.CIEuvY.Y) != 3) { 1601ab64890Smrg char *s; /* Maybe failed due to locale */ 1611ab64890Smrg int f; 1621ab64890Smrg if ((s = strdup(spec))) { 1631ab64890Smrg for (f = 0; s[f]; ++f) 1641ab64890Smrg if (s[f] == '.') 1651ab64890Smrg s[f] = ','; 1661ab64890Smrg else if (s[f] == ',') 1671ab64890Smrg s[f] = '.'; 1681ab64890Smrg if (sscanf(s + n + 1, "%lf/%lf/%lf", 1691ab64890Smrg &pColor->spec.CIEuvY.u_prime, 1701ab64890Smrg &pColor->spec.CIEuvY.v_prime, 1711ab64890Smrg &pColor->spec.CIEuvY.Y) != 3) { 1721ab64890Smrg free(s); 1731ab64890Smrg return(XcmsFailure); 1741ab64890Smrg } 1751ab64890Smrg free(s); 1761ab64890Smrg } else 1771ab64890Smrg return(XcmsFailure); 1781ab64890Smrg } 1791ab64890Smrg pColor->format = XcmsCIEuvYFormat; 1801ab64890Smrg pColor->pixel = 0; 1811ab64890Smrg return(_XcmsCIEuvY_ValidSpec(pColor)); 1821ab64890Smrg} 1831ab64890Smrg 1841ab64890Smrg 1851ab64890Smrg/************************************************************************ 1861ab64890Smrg * * 1871ab64890Smrg * PUBLIC ROUTINES * 1881ab64890Smrg * * 1891ab64890Smrg ************************************************************************/ 1901ab64890Smrg 1911ab64890Smrg/* 1921ab64890Smrg * NAME 1931ab64890Smrg * XcmsCIEuvY_ValidSpec 1941ab64890Smrg * 1951ab64890Smrg * SYNOPSIS 1961ab64890Smrg */ 1971ab64890SmrgStatus 1981ab64890Smrg_XcmsCIEuvY_ValidSpec( 1991ab64890Smrg XcmsColor *pColor) 2001ab64890Smrg/* 2011ab64890Smrg * DESCRIPTION 2021ab64890Smrg * Checks if color specification valid for CIE u'v'Y. 2031ab64890Smrg * 2041ab64890Smrg * RETURNS 2051ab64890Smrg * XcmsFailure if invalid, 2061ab64890Smrg * XcmsSuccess if valid. 2071ab64890Smrg * 2081ab64890Smrg */ 2091ab64890Smrg{ 2101ab64890Smrg if (pColor->format != XcmsCIEuvYFormat 2111ab64890Smrg || 2121ab64890Smrg (pColor->spec.CIEuvY.Y < 0.0 - XMY_DBL_EPSILON) 2131ab64890Smrg || 2141ab64890Smrg (pColor->spec.CIEuvY.Y > 1.0 + XMY_DBL_EPSILON)) { 2151ab64890Smrg return(XcmsFailure); 2161ab64890Smrg } 2171ab64890Smrg return(XcmsSuccess); 2181ab64890Smrg} 2191ab64890Smrg 2201ab64890Smrg 2211ab64890Smrg/* 2221ab64890Smrg * NAME 2231ab64890Smrg * XcmsCIEuvYToCIEXYZ - convert CIEuvY to CIEXYZ 2241ab64890Smrg * 2251ab64890Smrg * SYNOPSIS 2261ab64890Smrg */ 2271ab64890SmrgStatus 2281ab64890SmrgXcmsCIEuvYToCIEXYZ( 2291ab64890Smrg XcmsCCC ccc, 2301ab64890Smrg XcmsColor *puvY_WhitePt, 2311ab64890Smrg XcmsColor *pColors_in_out, 2321ab64890Smrg unsigned int nColors) 2331ab64890Smrg/* 2341ab64890Smrg * DESCRIPTION 2351ab64890Smrg * Converts color specifications in an array of XcmsColor 2361ab64890Smrg * structures from CIEuvY format to CIEXYZ format. 2371ab64890Smrg * 2381ab64890Smrg * RETURNS 2391ab64890Smrg * XcmsFailure if failed, 2401ab64890Smrg * XcmsSuccess if succeeded. 2411ab64890Smrg * 2421ab64890Smrg */ 2431ab64890Smrg{ 2441ab64890Smrg XcmsCIEXYZ XYZ_return; 2451ab64890Smrg XcmsColor whitePt; 2462d67cb4fSmrg unsigned int i; 2471ab64890Smrg XcmsColor *pColor = pColors_in_out; 2481ab64890Smrg XcmsFloat div, x, y, z, Y; 2491ab64890Smrg 2501ab64890Smrg /* 2511ab64890Smrg * Check arguments 2521ab64890Smrg * Postpone checking puvY_WhitePt until it is actually needed 2531ab64890Smrg * otherwise converting between XYZ and uvY will fail. 2541ab64890Smrg */ 2551ab64890Smrg if (pColors_in_out == NULL) { 2561ab64890Smrg return(XcmsFailure); 2571ab64890Smrg } 2581ab64890Smrg 2591ab64890Smrg 2601ab64890Smrg /* 2611ab64890Smrg * Now convert each XcmsColor structure to CIEXYZ form 2621ab64890Smrg */ 2631ab64890Smrg for (i = 0; i < nColors; i++, pColor++) { 2641ab64890Smrg 2651ab64890Smrg /* Make sure original format is CIEuvY */ 2661ab64890Smrg if (!_XcmsCIEuvY_ValidSpec(pColor)) { 2671ab64890Smrg return(XcmsFailure); 2681ab64890Smrg } 2691ab64890Smrg 2701ab64890Smrg /* 2711ab64890Smrg * Convert to CIEXYZ 2721ab64890Smrg */ 2731ab64890Smrg 2741ab64890Smrg Y = pColor->spec.CIEuvY.Y; 2751ab64890Smrg 2761ab64890Smrg /* Convert color u'v' to xyz space */ 2771ab64890Smrg div = (6.0 * pColor->spec.CIEuvY.u_prime) - (16.0 * pColor->spec.CIEuvY.v_prime) + 12.0; 2781ab64890Smrg if (div == 0.0) { 2791ab64890Smrg /* use white point since div == 0 */ 2801ab64890Smrg if (puvY_WhitePt == NULL ) { 2811ab64890Smrg return(XcmsFailure); 2821ab64890Smrg } 2831ab64890Smrg /* 2841ab64890Smrg * Make sure white point is in CIEuvY form 2851ab64890Smrg */ 2861ab64890Smrg if (puvY_WhitePt->format != XcmsCIEuvYFormat) { 2871ab64890Smrg /* Make copy of the white point because we're going to modify it */ 2881ab64890Smrg memcpy((char *)&whitePt, (char *)puvY_WhitePt, sizeof(XcmsColor)); 2891ab64890Smrg if (!_XcmsDIConvertColors(ccc, &whitePt, (XcmsColor *)NULL, 1, 2901ab64890Smrg XcmsCIEuvYFormat)) { 2911ab64890Smrg return(XcmsFailure); 2921ab64890Smrg } 2931ab64890Smrg puvY_WhitePt = &whitePt; 2941ab64890Smrg } 2951ab64890Smrg /* Make sure it is a white point, i.e., Y == 1.0 */ 2961ab64890Smrg if (puvY_WhitePt->spec.CIEuvY.Y != 1.0) { 2971ab64890Smrg return(XcmsFailure); 2981ab64890Smrg } 2991ab64890Smrg div = (6.0 * puvY_WhitePt->spec.CIEuvY.u_prime) - 3001ab64890Smrg (16.0 * puvY_WhitePt->spec.CIEuvY.v_prime) + 12.0; 3011ab64890Smrg if (div == 0) { 3021ab64890Smrg /* internal error */ 3031ab64890Smrg return(XcmsFailure); 3041ab64890Smrg } 3051ab64890Smrg x = 9.0 * puvY_WhitePt->spec.CIEuvY.u_prime / div; 3061ab64890Smrg y = 4.0 * puvY_WhitePt->spec.CIEuvY.v_prime / div; 3071ab64890Smrg } else { 3081ab64890Smrg x = 9.0 * pColor->spec.CIEuvY.u_prime / div; 3091ab64890Smrg y = 4.0 * pColor->spec.CIEuvY.v_prime / div; 3101ab64890Smrg } 3111ab64890Smrg z = 1.0 - x - y; 3121ab64890Smrg 3131ab64890Smrg /* Convert from xyz to XYZ */ 3141ab64890Smrg /* Conversion uses color normalized lightness based on Y */ 3151ab64890Smrg if (y != 0.0) { 3161ab64890Smrg XYZ_return.X = x * Y / y; 3171ab64890Smrg } else { 3181ab64890Smrg XYZ_return.X = x; 3191ab64890Smrg } 3201ab64890Smrg XYZ_return.Y = Y; 3211ab64890Smrg if (y != 0.0) { 3221ab64890Smrg XYZ_return.Z = z * Y / y; 3231ab64890Smrg } else { 3241ab64890Smrg XYZ_return.Z = z; 3251ab64890Smrg } 3261ab64890Smrg 3271ab64890Smrg memcpy((char *)&pColor->spec.CIEXYZ, (char *)&XYZ_return, sizeof(XcmsCIEXYZ)); 3281ab64890Smrg /* Identify that format is now CIEXYZ */ 3291ab64890Smrg pColor->format = XcmsCIEXYZFormat; 3301ab64890Smrg } 3311ab64890Smrg 3321ab64890Smrg return(XcmsSuccess); 3331ab64890Smrg} 3341ab64890Smrg 3351ab64890Smrg 3361ab64890Smrg/* 3371ab64890Smrg * NAME 3381ab64890Smrg * XcmsCIEXYZToCIEuvY - convert CIEXYZ to CIEuvY 3391ab64890Smrg * 3401ab64890Smrg * SYNOPSIS 3411ab64890Smrg */ 3421ab64890SmrgStatus 3431ab64890SmrgXcmsCIEXYZToCIEuvY( 3441ab64890Smrg XcmsCCC ccc, 3451ab64890Smrg XcmsColor *puvY_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 CIEuvY format. 3521ab64890Smrg * 3531ab64890Smrg * RETURNS 3541ab64890Smrg * XcmsFailure if failed, 3551ab64890Smrg * XcmsSuccess if succeeded. 3561ab64890Smrg * 3571ab64890Smrg */ 3581ab64890Smrg{ 3591ab64890Smrg XcmsCIEuvY uvY_return; 3601ab64890Smrg XcmsColor whitePt; 3612d67cb4fSmrg unsigned int i; 3621ab64890Smrg XcmsColor *pColor = pColors_in_out; 3631ab64890Smrg XcmsFloat div; 3641ab64890Smrg 3651ab64890Smrg /* 3661ab64890Smrg * Check arguments 3671ab64890Smrg * Postpone checking puvY_WhitePt until it is actually needed 3681ab64890Smrg * otherwise converting between XYZ and uvY will fail. 3691ab64890Smrg */ 3701ab64890Smrg if (pColors_in_out == NULL) { 3711ab64890Smrg return(XcmsFailure); 3721ab64890Smrg } 3731ab64890Smrg 3741ab64890Smrg /* 3751ab64890Smrg * Now convert each XcmsColor structure to CIEuvY form 3761ab64890Smrg */ 3771ab64890Smrg for (i = 0; i < nColors; i++, pColor++) { 3781ab64890Smrg 3791ab64890Smrg /* Make sure original format is CIEXYZ */ 3801ab64890Smrg if (!_XcmsCIEXYZ_ValidSpec(pColor)) { 3811ab64890Smrg return(XcmsFailure); 3821ab64890Smrg } 3831ab64890Smrg 3841ab64890Smrg /* Convert to CIEuvY */ 3851ab64890Smrg div = pColor->spec.CIEXYZ.X + (15.0 * pColor->spec.CIEXYZ.Y) + 3861ab64890Smrg (3.0 * pColor->spec.CIEXYZ.Z); 3871ab64890Smrg if (div == 0.0) { 3881ab64890Smrg /* Use white point since div == 0.0 */ 3891ab64890Smrg if (puvY_WhitePt == NULL ) { 3901ab64890Smrg return(XcmsFailure); 3911ab64890Smrg } 3921ab64890Smrg /* 3931ab64890Smrg * Make sure white point is in CIEuvY form 3941ab64890Smrg */ 3951ab64890Smrg if (puvY_WhitePt->format != XcmsCIEuvYFormat) { 3961ab64890Smrg /* Make copy of the white point because we're going to modify it */ 3971ab64890Smrg memcpy((char *)&whitePt, (char *)puvY_WhitePt, sizeof(XcmsColor)); 3981ab64890Smrg if (!_XcmsDIConvertColors(ccc, &whitePt, (XcmsColor *)NULL, 1, 3991ab64890Smrg XcmsCIEuvYFormat)) { 4001ab64890Smrg return(XcmsFailure); 4011ab64890Smrg } 4021ab64890Smrg puvY_WhitePt = &whitePt; 4031ab64890Smrg } 4041ab64890Smrg /* Make sure it is a white point, i.e., Y == 1.0 */ 4051ab64890Smrg if (puvY_WhitePt->spec.CIEuvY.Y != 1.0) { 4061ab64890Smrg return(XcmsFailure); 4071ab64890Smrg } 4081ab64890Smrg uvY_return.Y = pColor->spec.CIEXYZ.Y; 4091ab64890Smrg uvY_return.u_prime = puvY_WhitePt->spec.CIEuvY.u_prime; 4101ab64890Smrg uvY_return.v_prime = puvY_WhitePt->spec.CIEuvY.v_prime; 4111ab64890Smrg } else { 4121ab64890Smrg uvY_return.u_prime = 4.0 * pColor->spec.CIEXYZ.X / div; 4131ab64890Smrg uvY_return.v_prime = 9.0 * pColor->spec.CIEXYZ.Y / div; 4141ab64890Smrg uvY_return.Y = pColor->spec.CIEXYZ.Y; 4151ab64890Smrg } 4161ab64890Smrg 4171ab64890Smrg memcpy((char *)&pColor->spec.CIEuvY, (char *)&uvY_return, sizeof(XcmsCIEuvY)); 4181ab64890Smrg /* Identify that format is now CIEuvY */ 4191ab64890Smrg pColor->format = XcmsCIEuvYFormat; 4201ab64890Smrg } 4211ab64890Smrg 4221ab64890Smrg return(XcmsSuccess); 4231ab64890Smrg} 424