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.  TekColor is a
81ab64890Smrg * trademark of Tektronix, Inc.  The term "TekHVC" designates a particular
91ab64890Smrg * color space that is the subject of U.S. Patent No. 4,985,853 (equivalent
101ab64890Smrg * foreign patents pending).  Permission is hereby granted to use, copy,
111ab64890Smrg * modify, sell, and otherwise distribute this software and its
121ab64890Smrg * documentation for any purpose and without fee, provided that:
1361b2299dSmrg *
141ab64890Smrg * 1. This copyright, permission, and disclaimer notice is reproduced in
151ab64890Smrg *    all copies of this software and any modification thereof and in
1661b2299dSmrg *    supporting documentation;
171ab64890Smrg * 2. Any color-handling application which displays TekHVC color
181ab64890Smrg *    cooordinates identifies these as TekHVC color coordinates in any
191ab64890Smrg *    interface that displays these coordinates and in any associated
201ab64890Smrg *    documentation;
211ab64890Smrg * 3. The term "TekHVC" is always used, and is only used, in association
221ab64890Smrg *    with the mathematical derivations of the TekHVC Color Space,
231ab64890Smrg *    including those provided in this file and any equivalent pathways and
241ab64890Smrg *    mathematical derivations, regardless of digital (e.g., floating point
251ab64890Smrg *    or integer) representation.
2661b2299dSmrg *
271ab64890Smrg * Tektronix makes no representation about the suitability of this software
281ab64890Smrg * for any purpose.  It is provided "as is" and with all faults.
2961b2299dSmrg *
301ab64890Smrg * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
311ab64890Smrg * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
321ab64890Smrg * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
331ab64890Smrg * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
341ab64890Smrg * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
351ab64890Smrg * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
361ab64890Smrg * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
371ab64890Smrg *
381ab64890Smrg *	NAME
391ab64890Smrg *		TekHVCGcV.c
401ab64890Smrg *
411ab64890Smrg *	DESCRIPTION
421ab64890Smrg *		Source for XcmsTekHVCClipV() gamut compression routine.
431ab64890Smrg *
441ab64890Smrg */
451ab64890Smrg
461ab64890Smrg#ifdef HAVE_CONFIG_H
471ab64890Smrg#include <config.h>
481ab64890Smrg#endif
491ab64890Smrg#include "Xlibint.h"
501ab64890Smrg#include "Xcmsint.h"
511ab64890Smrg#include "Cv.h"
521ab64890Smrg
531ab64890Smrg
541ab64890Smrg/************************************************************************
551ab64890Smrg *									*
561ab64890Smrg *			 PUBLIC ROUTINES				*
571ab64890Smrg *									*
581ab64890Smrg ************************************************************************/
591ab64890Smrg
601ab64890Smrg/*
611ab64890Smrg *	NAME
621ab64890Smrg *		XcmsTekHVCClipV - Return the closest value
631ab64890Smrg *
641ab64890Smrg *	SYNOPSIS
651ab64890Smrg */
661ab64890Smrg/* ARGSUSED */
671ab64890SmrgStatus
681ab64890SmrgXcmsTekHVCClipV (
6961b2299dSmrg    XcmsCCC ccc,
7061b2299dSmrg    XcmsColor *pColors_in_out,
711ab64890Smrg    unsigned int nColors,
721ab64890Smrg    unsigned int i,
731ab64890Smrg    Bool *pCompressed)
741ab64890Smrg/*
751ab64890Smrg *	DESCRIPTION
761ab64890Smrg *		Return the closest value for a specific hue and chroma.
7761b2299dSmrg *		This routine takes any color as input and outputs
781ab64890Smrg *		a CIE XYZ color.
791ab64890Smrg *
801ab64890Smrg *		Since this routine works with the value within
811ab64890Smrg *		pColor_in_out intermediate results may be returned
821ab64890Smrg *		even though it may be invalid.
831ab64890Smrg *
841ab64890Smrg *	RETURNS
851ab64890Smrg *		XcmsFailure - Failure
861ab64890Smrg *              XcmsSuccess - Succeeded
871ab64890Smrg *
881ab64890Smrg */
891ab64890Smrg{
901ab64890Smrg    XcmsColor  *pColor;
911ab64890Smrg    XcmsColor  hvc_max;
921ab64890Smrg    XcmsCCCRec myCCC;
931ab64890Smrg    Status retval;
941ab64890Smrg
951ab64890Smrg    /*
961ab64890Smrg     * Insure TekHVC installed
971ab64890Smrg     */
981ab64890Smrg    if (XcmsAddColorSpace(&XcmsTekHVCColorSpace) == XcmsFailure) {
991ab64890Smrg	return(XcmsFailure);
1001ab64890Smrg    }
1011ab64890Smrg
1021ab64890Smrg    /* Use my own CCC */
1031ab64890Smrg    memcpy ((char *)&myCCC, (char *)ccc, sizeof(XcmsCCCRec));
1041ab64890Smrg    myCCC.clientWhitePt.format = XcmsUndefinedFormat;/* Inherit Screen WP */
1051ab64890Smrg    myCCC.gamutCompProc = (XcmsCompressionProc)NULL;/* no gamut compression */
1061ab64890Smrg
1071ab64890Smrg    /*
1081ab64890Smrg     * Color specification passed as input can be assumed to:
1091ab64890Smrg     *	1. Be in XcmsCIEXYZFormat
1101ab64890Smrg     *	2. Already be white point adjusted for the Screen White Point.
1111ab64890Smrg     *	    This means that the white point now associated with this
1121ab64890Smrg     *	    color spec is the Screen White Point (even if the
1131ab64890Smrg     *	    ccc->clientWhitePt differs).
1141ab64890Smrg     */
1151ab64890Smrg
1161ab64890Smrg    pColor = pColors_in_out + i;
11761b2299dSmrg
1181ab64890Smrg    if (ccc->visual->class < StaticColor &&
1191ab64890Smrg	    FunctionSetOfCCC(ccc) != (XPointer) &XcmsLinearRGBFunctionSet) {
1201ab64890Smrg	/*
1211ab64890Smrg	 * GRAY !
1221ab64890Smrg	 */
1231ab64890Smrg	return(XcmsFailure);
1241ab64890Smrg    } else {
1251ab64890Smrg	/* Convert from CIEXYZ to TekHVC format */
1261ab64890Smrg	if (_XcmsDIConvertColors(&myCCC, pColor,
1271ab64890Smrg		&myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsTekHVCFormat)
1281ab64890Smrg		== XcmsFailure) {
1291ab64890Smrg	    return(XcmsFailure);
1301ab64890Smrg	}
1311ab64890Smrg
1321ab64890Smrg	/* check to make sure we have a valid TekHVC number */
1331ab64890Smrg	if (!_XcmsTekHVC_CheckModify (pColor)) {
1341ab64890Smrg	    return (XcmsFailure);
1351ab64890Smrg	}
1361ab64890Smrg
1371ab64890Smrg	/* Step 1: compute the maximum value and chroma for this hue. */
1381ab64890Smrg	/*         This copy may be overkill but it preserves the pixel etc. */
1391ab64890Smrg	memcpy((char *)&hvc_max, (char *)pColor, sizeof(XcmsColor));
1401ab64890Smrg	if (_XcmsTekHVCQueryMaxVCRGB (&myCCC, hvc_max.spec.TekHVC.H, &hvc_max,
1411ab64890Smrg		(XcmsRGBi *)NULL) == XcmsFailure) {
1421ab64890Smrg	    return (XcmsFailure);
1431ab64890Smrg	}
1441ab64890Smrg
1451ab64890Smrg	/* Now check and return the appropriate value */
1461ab64890Smrg	if (pColor->spec.TekHVC.C == hvc_max.spec.TekHVC.C) {
1471ab64890Smrg	    /* When the chroma input is equal to the maximum chroma */
1481ab64890Smrg	    /* merely return the value for that chroma. */
1491ab64890Smrg	    pColor->spec.TekHVC.V = hvc_max.spec.TekHVC.V;
1501ab64890Smrg	    if (!_XcmsTekHVC_CheckModify (pColor)) {
1511ab64890Smrg		return (XcmsFailure);
1521ab64890Smrg	    }
1531ab64890Smrg	    retval = _XcmsDIConvertColors(&myCCC, pColor,
1541ab64890Smrg		    &myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsCIEXYZFormat);
1551ab64890Smrg	} else if (pColor->spec.TekHVC.C > hvc_max.spec.TekHVC.C) {
1561ab64890Smrg	    /* When the chroma input is greater than the maximum chroma */
1571ab64890Smrg	    /* merely return the value and chroma for the given hue. */
1581ab64890Smrg	    pColor->spec.TekHVC.C = hvc_max.spec.TekHVC.C;
1591ab64890Smrg	    pColor->spec.TekHVC.V = hvc_max.spec.TekHVC.V;
1601ab64890Smrg	    return (XcmsFailure);
1611ab64890Smrg	} else if (pColor->spec.TekHVC.V < hvc_max.spec.TekHVC.V) {
1621ab64890Smrg	    /* When the value input is less than the maximum value point */
1631ab64890Smrg	    /* compute the intersection of the line from 0,0 to max_V, max_C */
1641ab64890Smrg	    /* using the chroma input. */
16561b2299dSmrg	    pColor->spec.TekHVC.V = pColor->spec.TekHVC.C *
1661ab64890Smrg				    hvc_max.spec.TekHVC.V / hvc_max.spec.TekHVC.C;
1671ab64890Smrg	    if (pColor->spec.TekHVC.V >= hvc_max.spec.TekHVC.V) {
1681ab64890Smrg		pColor->spec.TekHVC.C = hvc_max.spec.TekHVC.C;
1691ab64890Smrg		pColor->spec.TekHVC.V = hvc_max.spec.TekHVC.V;
1701ab64890Smrg	    }
1711ab64890Smrg	    if (!_XcmsTekHVC_CheckModify (pColor)) {
1721ab64890Smrg		return (XcmsFailure);
1731ab64890Smrg	    }
1741ab64890Smrg	    retval = _XcmsDIConvertColors(&myCCC, pColor,
1751ab64890Smrg		    &myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsCIEXYZFormat);
1761ab64890Smrg	} else {
1771ab64890Smrg	    /* When the value input is greater than the maximum value point */
1781ab64890Smrg	    /* use HvcMaxValue to find the maximum value for the given chroma. */
1791ab64890Smrg	    if (pColor->format != XcmsTekHVCFormat) {
1801ab64890Smrg		if (_XcmsDIConvertColors(ccc, pColor,
1811ab64890Smrg			&ccc->pPerScrnInfo->screenWhitePt, 1, XcmsCIEXYZFormat)
1821ab64890Smrg			== XcmsFailure) {
1831ab64890Smrg		    return(XcmsFailure);
1841ab64890Smrg		}
1851ab64890Smrg	    }
1861ab64890Smrg	    if (XcmsTekHVCQueryMaxV(&myCCC,
1871ab64890Smrg		    pColor->spec.TekHVC.H,
1881ab64890Smrg		    pColor->spec.TekHVC.C,
1891ab64890Smrg		    pColor)
1901ab64890Smrg		    == XcmsFailure) {
1911ab64890Smrg		return (XcmsFailure);
1921ab64890Smrg	    }
1931ab64890Smrg	    retval = _XcmsDIConvertColors(&myCCC, pColor,
1941ab64890Smrg		    &myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsCIEXYZFormat);
1951ab64890Smrg	}
1961ab64890Smrg	if (retval != XcmsFailure && pCompressed != NULL) {
1971ab64890Smrg	    *(pCompressed + i) = True;
1981ab64890Smrg	}
1991ab64890Smrg	return(retval);
2001ab64890Smrg    }
2011ab64890Smrg}
202