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 *		TekHVCMxV.c
401ab64890Smrg *
411ab64890Smrg *	DESCRIPTION
421ab64890Smrg *		Source for the XcmsTekHVCQueryMaxV() gamut boundary
431ab64890Smrg *		querying routine.
441ab64890Smrg *
451ab64890Smrg */
461ab64890Smrg
471ab64890Smrg#ifdef HAVE_CONFIG_H
481ab64890Smrg#include <config.h>
491ab64890Smrg#endif
501ab64890Smrg#include "Xlibint.h"
511ab64890Smrg#include "Xcmsint.h"
521ab64890Smrg#include <math.h>
531ab64890Smrg#include "Cv.h"
541ab64890Smrg
551ab64890Smrg/*
561ab64890Smrg *	DEFINES
571ab64890Smrg */
581ab64890Smrg#define MAXBISECTCOUNT	100
591ab64890Smrg#define EPS	    0.001
601ab64890Smrg
611ab64890Smrg
621ab64890Smrg/************************************************************************
631ab64890Smrg *									*
641ab64890Smrg *			 PUBLIC ROUTINES				*
651ab64890Smrg *									*
661ab64890Smrg ************************************************************************/
671ab64890Smrg
681ab64890Smrg/*
691ab64890Smrg *	NAME
701ab64890Smrg *		XcmsTekHVCQueryMaxV - Compute maximum value for a hue and chroma
711ab64890Smrg *
721ab64890Smrg *	SYNOPSIS
731ab64890Smrg */
741ab64890SmrgStatus
751ab64890SmrgXcmsTekHVCQueryMaxV(
761ab64890Smrg    XcmsCCC ccc,
771ab64890Smrg    XcmsFloat hue,
781ab64890Smrg    XcmsFloat chroma,
791ab64890Smrg    XcmsColor *pColor_return)
801ab64890Smrg/*
811ab64890Smrg *	DESCRIPTION
821ab64890Smrg *		Return the maximum value for a specified hue and chroma.
831ab64890Smrg *
841ab64890Smrg *	ASSUMPTIONS
851ab64890Smrg *		This routine assumes that the white point associated with
861ab64890Smrg *		the color specification is the Screen White Point.  The
871ab64890Smrg *		Screen White Point will also be associated with the
881ab64890Smrg *		returned color specification.
891ab64890Smrg *
901ab64890Smrg *	RETURNS
911ab64890Smrg *		XcmsFailure - Failure
921ab64890Smrg *              XcmsSuccess - Succeeded with no modifications
931ab64890Smrg *
941ab64890Smrg */
951ab64890Smrg{
961ab64890Smrg    XcmsCCCRec myCCC;
971ab64890Smrg    XcmsColor   tmp;
981ab64890Smrg    XcmsColor   max_vc;
991ab64890Smrg    XcmsRGBi    rgb_saved;
1001ab64890Smrg    int         nCount, nMaxCount;
1011ab64890Smrg    XcmsFloat   nT, nChroma, savedChroma, lastValue, lastChroma, prevChroma;
1021ab64890Smrg    XcmsFloat   rFactor;
1031ab64890Smrg    XcmsFloat	ftmp1, ftmp2;
1041ab64890Smrg
1051ab64890Smrg    /*
1061ab64890Smrg     * Check Arguments
1071ab64890Smrg     */
1081ab64890Smrg    if (ccc == NULL || pColor_return == NULL) {
1091ab64890Smrg	return(XcmsFailure);
1101ab64890Smrg    }
1111ab64890Smrg
1121ab64890Smrg    /*
1131ab64890Smrg     * Insure TekHVC installed
1141ab64890Smrg     */
1151ab64890Smrg    if (XcmsAddColorSpace(&XcmsTekHVCColorSpace) == XcmsFailure) {
1161ab64890Smrg	return(XcmsFailure);
1171ab64890Smrg    }
1181ab64890Smrg
1191ab64890Smrg    /* setup the CCC to use for the conversions. */
1201ab64890Smrg    memcpy ((char *) &myCCC, (char *) ccc, sizeof(XcmsCCCRec));
1211ab64890Smrg    myCCC.clientWhitePt.format = XcmsUndefinedFormat;
1221ab64890Smrg    myCCC.gamutCompProc = (XcmsCompressionProc) NULL;
1231ab64890Smrg
1241ab64890Smrg    tmp.spec.TekHVC.H = hue;
1251ab64890Smrg    tmp.spec.TekHVC.V = 0.0;
1261ab64890Smrg    tmp.spec.TekHVC.C = chroma;
1271ab64890Smrg    tmp.pixel = pColor_return->pixel;
1281ab64890Smrg    tmp.format = XcmsTekHVCFormat;
1291ab64890Smrg
1301ab64890Smrg    if (!_XcmsTekHVC_CheckModify (&tmp)) {
1311ab64890Smrg	return(XcmsFailure);
1321ab64890Smrg    }
1331ab64890Smrg
1341ab64890Smrg    /* Step 1: compute the maximum value and chroma for this hue. */
1351ab64890Smrg    /*         This copy may be overkill but it preserves the pixel etc. */
1361ab64890Smrg    memcpy((char *)&max_vc, (char *)&tmp, sizeof(XcmsColor));
1371ab64890Smrg    hue = max_vc.spec.TekHVC.H;
1381ab64890Smrg    if (_XcmsTekHVCQueryMaxVCRGB(&myCCC, max_vc.spec.TekHVC.H, &max_vc, &rgb_saved)
1391ab64890Smrg	    == XcmsFailure) {
1401ab64890Smrg	return(XcmsFailure);
1411ab64890Smrg    }
1421ab64890Smrg
1431ab64890Smrg    if (max_vc.spec.TekHVC.C < tmp.spec.TekHVC.C) {
1441ab64890Smrg	/*
14561b2299dSmrg	 *  If the chroma is greater than the chroma for the
1461ab64890Smrg	 *  maximum value/chroma point then the value is the
1471ab64890Smrg	 *  the value for the maximum value, chroma point.
1481ab64890Smrg	 *  This is an error but it I return the best approximation I can.
1491ab64890Smrg         *  Thus the inconsistency.
1501ab64890Smrg	 */
1511ab64890Smrg	tmp.spec.TekHVC.C = max_vc.spec.TekHVC.C;
1521ab64890Smrg	tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
1531ab64890Smrg	memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
1541ab64890Smrg	return(XcmsSuccess);
1551ab64890Smrg    } else if (max_vc.spec.TekHVC.C == tmp.spec.TekHVC.C) {
1561ab64890Smrg	/*
15761b2299dSmrg	 *  If the chroma is equal to the chroma for the
1581ab64890Smrg	 *  maximum value/chroma point then the value is the
1591ab64890Smrg	 *  the value for the maximum value, chroma point.
1601ab64890Smrg	 */
1611ab64890Smrg	tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
1621ab64890Smrg	memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
1631ab64890Smrg	return(XcmsSuccess);
1641ab64890Smrg    } else {
1651ab64890Smrg	/* must do a bisection here to compute the maximum value */
1661ab64890Smrg	/* save the structure input so that any elements that */
1671ab64890Smrg	/* are not touched are recopied later in the routine. */
1681ab64890Smrg	nChroma = savedChroma = tmp.spec.TekHVC.C;
1691ab64890Smrg	tmp.spec.TekHVC.C = max_vc.spec.TekHVC.C;
1701ab64890Smrg	tmp.spec.TekHVC.V = max_vc.spec.TekHVC.V;
1711ab64890Smrg	lastChroma = -1.0;
1721ab64890Smrg	lastValue = -1.0;
1731ab64890Smrg	nMaxCount = MAXBISECTCOUNT;
1741ab64890Smrg	rFactor = 1.0;
1751ab64890Smrg
1761ab64890Smrg	for (nCount = 0; nCount < nMaxCount; nCount++) {
1771ab64890Smrg	    prevChroma = lastChroma;
1781ab64890Smrg	    lastValue =  tmp.spec.TekHVC.V;
1791ab64890Smrg	    lastChroma = tmp.spec.TekHVC.C;
1801ab64890Smrg	    nT = (1.0 - (nChroma / max_vc.spec.TekHVC.C)) * rFactor;
1811ab64890Smrg	    tmp.spec.RGBi.red   = rgb_saved.red * (1.0 - nT) + nT;
1821ab64890Smrg	    tmp.spec.RGBi.green = rgb_saved.green * (1.0 - nT) + nT;
1831ab64890Smrg	    tmp.spec.RGBi.blue  = rgb_saved.blue * (1.0 - nT) + nT;
1841ab64890Smrg	    tmp.format = XcmsRGBiFormat;
1851ab64890Smrg
1861ab64890Smrg	    /* convert from RGB to HVC */
1871ab64890Smrg	    if (_XcmsConvertColorsWithWhitePt(&myCCC, &tmp,
1881ab64890Smrg		    &myCCC.pPerScrnInfo->screenWhitePt, 1, XcmsTekHVCFormat,
1891ab64890Smrg		    (Bool *) NULL) == XcmsFailure) {
1901ab64890Smrg		return(XcmsFailure);
1911ab64890Smrg	    }
1921ab64890Smrg
1931ab64890Smrg	    /* Now check the return against what is expected */
1941ab64890Smrg	    if (tmp.spec.TekHVC.C <= savedChroma + EPS &&
1951ab64890Smrg		tmp.spec.TekHVC.C >= savedChroma - EPS) {
1961ab64890Smrg		tmp.spec.TekHVC.H = hue;  /* use the saved hue */
1971ab64890Smrg		memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
1981ab64890Smrg		return(XcmsSuccess);
19961b2299dSmrg	    }
2001ab64890Smrg	    nChroma += savedChroma - tmp.spec.TekHVC.C;
2011ab64890Smrg	    if (nChroma > max_vc.spec.TekHVC.C) {
2021ab64890Smrg		nChroma = max_vc.spec.TekHVC.C;
2031ab64890Smrg		rFactor *= 0.5;  /* selective relaxation employed */
2041ab64890Smrg	    } else if (nChroma < 0.0) {
2051ab64890Smrg		/* avoid using fabs */
2061ab64890Smrg		ftmp1 = lastChroma - savedChroma;
2071ab64890Smrg		if (ftmp1 < 0.0)
2081ab64890Smrg		    ftmp1 = -ftmp1;
2091ab64890Smrg		ftmp2 = tmp.spec.TekHVC.C - savedChroma;
2101ab64890Smrg		if (ftmp2 < 0.0)
2111ab64890Smrg		    ftmp2 = -ftmp2;
2121ab64890Smrg		if (ftmp1 < ftmp2) {
2131ab64890Smrg		    tmp.spec.TekHVC.V = lastValue;
2141ab64890Smrg		    tmp.spec.TekHVC.C = lastChroma;
2151ab64890Smrg		}
2161ab64890Smrg		/* make sure to return the input hue */
2171ab64890Smrg		tmp.spec.TekHVC.H = hue;
2181ab64890Smrg		if (!_XcmsTekHVC_CheckModify(&tmp)) {
2191ab64890Smrg		    return(XcmsFailure);
2201ab64890Smrg		}
2211ab64890Smrg		memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
2221ab64890Smrg		return(XcmsSuccess);
2231ab64890Smrg	    } else if (tmp.spec.TekHVC.C <= prevChroma + EPS &&
2241ab64890Smrg		       tmp.spec.TekHVC.C >= prevChroma - EPS) {
2251ab64890Smrg		rFactor *= 0.5;  /* selective relaxation employed */
2261ab64890Smrg	    }
2271ab64890Smrg	}
2281ab64890Smrg	if (nCount >= nMaxCount) {
2291ab64890Smrg	    /* avoid using fabs */
2301ab64890Smrg	    ftmp1 = lastChroma - savedChroma;
2311ab64890Smrg	    if (ftmp1 < 0.0)
2321ab64890Smrg		ftmp1 = -ftmp1;
2331ab64890Smrg	    ftmp2 = tmp.spec.TekHVC.C - savedChroma;
2341ab64890Smrg	    if (ftmp2 < 0.0)
2351ab64890Smrg		ftmp2 = -ftmp2;
2361ab64890Smrg	    if (ftmp1 < ftmp2) {
2371ab64890Smrg		    tmp.spec.TekHVC.V = lastValue;
2381ab64890Smrg		    tmp.spec.TekHVC.C = lastChroma;
2391ab64890Smrg	    }
2401ab64890Smrg	}
2411ab64890Smrg    }
2421ab64890Smrg
2431ab64890Smrg    /* make sure to return the input hue */
2441ab64890Smrg    tmp.spec.TekHVC.H = hue;
2451ab64890Smrg    memcpy ((char *) pColor_return, (char *) &tmp, sizeof (XcmsColor));
2461ab64890Smrg    return(XcmsSuccess);
2471ab64890Smrg}
248