LuvMxLC.c revision 61b2299d
11ab64890Smrg/* $Xorg: LuvMxLC.c,v 1.3 2000/08/17 19:44:45 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 *
271ab64890Smrg *
281ab64890Smrg *	NAME
291ab64890Smrg *		CIELuvMxVC.c
301ab64890Smrg *
311ab64890Smrg *	DESCRIPTION
321ab64890Smrg *		Source for the XcmsCIELuvQueryMaxLC() gamut boundary
331ab64890Smrg *		querying routine.
341ab64890Smrg *
351ab64890Smrg *	DOCUMENTATION
361ab64890Smrg *		"TekColor Color Management System, System Implementor's Manual"
371ab64890Smrg *		and
381ab64890Smrg *		Fred W. Billmeyer & Max Saltzman, "Principles of Color
391ab64890Smrg *		Technology", John Wily & Sons, Inc, 1981.
401ab64890Smrg */
411ab64890Smrg/* $XFree86$ */
421ab64890Smrg
431ab64890Smrg#ifdef HAVE_CONFIG_H
441ab64890Smrg#include <config.h>
451ab64890Smrg#endif
461ab64890Smrg#include "Xlibint.h"
471ab64890Smrg#include "Xcmsint.h"
481ab64890Smrg#include "Cv.h"
491ab64890Smrg
501ab64890Smrg/*
511ab64890Smrg *	DEFINES
521ab64890Smrg */
531ab64890Smrg#define MIN(x,y)	((x) > (y) ? (y) : (x))
541ab64890Smrg#define MIN3(x,y,z)	((x) > (MIN((y), (z))) ? (MIN((y), (z))) : (x))
551ab64890Smrg#define MAX(x,y)	((x) > (y) ? (x) : (y))
561ab64890Smrg#define MAX3(x,y,z)	((x) > (MAX((y), (z))) ? (x) : (MAX((y), (z))))
571ab64890Smrg#define START_LSTAR	(XcmsFloat)40.0
581ab64890Smrg#define START_CHROMA	(XcmsFloat)2.2
591ab64890Smrg
601ab64890Smrg
611ab64890Smrg/************************************************************************
621ab64890Smrg *									*
631ab64890Smrg *			 API PRIVATE ROUTINES				*
641ab64890Smrg *									*
651ab64890Smrg ************************************************************************/
661ab64890Smrg
671ab64890Smrg/*
681ab64890Smrg *	NAME
691ab64890Smrg *		_XcmsCIELuvQueryMaxLCRGB - Compute maximum L* and chroma.
701ab64890Smrg *
711ab64890Smrg *	SYNOPSIS
721ab64890Smrg */
731ab64890SmrgStatus
741ab64890Smrg_XcmsCIELuvQueryMaxLCRGB(
751ab64890Smrg    XcmsCCC	ccc,
761ab64890Smrg    XcmsFloat	hue,		/* hue in radians */
771ab64890Smrg    XcmsColor   *pColor_return,
781ab64890Smrg    XcmsRGBi    *pRGB_return)
791ab64890Smrg/*
801ab64890Smrg *	DESCRIPTION
811ab64890Smrg *		Return the maximum psychometric chroma for a specified
821ab64890Smrg *		hue angle(radians), and the corresponding L*.  This is computed
831ab64890Smrg *		by a binary search of all possible chromas.  An assumption
841ab64890Smrg *		is made that there are no local maxima.  Use the unrounded
851ab64890Smrg *		Max psychometric chroma because the difference check can be
861ab64890Smrg *		small.
871ab64890Smrg *
881ab64890Smrg *		NOTE:  No local CCC is used because this is a private
891ab64890Smrg *		       routine and all routines that call it are expected
901ab64890Smrg *		       to behave properly, i.e. send a local CCC with
911ab64890Smrg *		       no white adjust function and no gamut compression
921ab64890Smrg *		       function.
931ab64890Smrg *
941ab64890Smrg *		This routine only accepts hue as input and outputs
951ab64890Smrg *		Luv and RGBi.
961ab64890Smrg *
971ab64890Smrg *	RETURNS
981ab64890Smrg *		XcmsFailure - Failure
991ab64890Smrg *		XcmsSuccess - Succeeded
1001ab64890Smrg *
10161b2299dSmrg */
1021ab64890Smrg{
1031ab64890Smrg    XcmsFloat nSmall, nLarge;
1041ab64890Smrg    XcmsColor tmp;
1051ab64890Smrg
1061ab64890Smrg    tmp.format = XcmsCIELuvFormat;
1071ab64890Smrg    /*  Use some unreachable color on the given hue angle */
1081ab64890Smrg    tmp.spec.CIELuv.L_star = START_LSTAR;
1091ab64890Smrg    tmp.spec.CIELuv.u_star = XCMS_CIEUSTAROFHUE(hue, START_CHROMA);
1101ab64890Smrg    tmp.spec.CIELuv.v_star = XCMS_CIEVSTAROFHUE(hue, START_CHROMA);
1111ab64890Smrg    /*
1121ab64890Smrg     * Convert from Luv to RGB
1131ab64890Smrg     *
1141ab64890Smrg     * Note that the CIEXYZ to RGBi conversion routine must stuff the
1151ab64890Smrg     * out of bounds RGBi values in tmp when the ccc->gamutCompProc
1161ab64890Smrg     * is NULL.
1171ab64890Smrg     */
11861b2299dSmrg    if ((_XcmsConvertColorsWithWhitePt(ccc, &tmp, ScreenWhitePointOfCCC(ccc),
11961b2299dSmrg		               (unsigned int)1, XcmsRGBiFormat, (Bool *) NULL)
1201ab64890Smrg	    == XcmsFailure) && tmp.format != XcmsRGBiFormat) {
1211ab64890Smrg	return (XcmsFailure);
1221ab64890Smrg    }
1231ab64890Smrg
1241ab64890Smrg    /* Now pick the smallest RGB */
12561b2299dSmrg    nSmall = MIN3(tmp.spec.RGBi.red,
12661b2299dSmrg		  tmp.spec.RGBi.green,
1271ab64890Smrg		  tmp.spec.RGBi.blue);
1281ab64890Smrg    /* Make the smallest RGB equal to zero */
1291ab64890Smrg    tmp.spec.RGBi.red   -= nSmall;
1301ab64890Smrg    tmp.spec.RGBi.green -= nSmall;
1311ab64890Smrg    tmp.spec.RGBi.blue  -= nSmall;
1321ab64890Smrg
1331ab64890Smrg    /* Now pick the largest RGB */
13461b2299dSmrg    nLarge = MAX3(tmp.spec.RGBi.red,
13561b2299dSmrg		  tmp.spec.RGBi.green,
1361ab64890Smrg		  tmp.spec.RGBi.blue);
1371ab64890Smrg    /* Scale the RGB values based on the largest one */
1381ab64890Smrg    tmp.spec.RGBi.red   /= nLarge;
1391ab64890Smrg    tmp.spec.RGBi.green /= nLarge;
1401ab64890Smrg    tmp.spec.RGBi.blue  /= nLarge;
1411ab64890Smrg    tmp.format = XcmsRGBiFormat;
1421ab64890Smrg
1431ab64890Smrg    /* If the calling routine wants RGB value give them the ones used. */
1441ab64890Smrg    if (pRGB_return) {
1451ab64890Smrg	pRGB_return->red   = tmp.spec.RGBi.red;
1461ab64890Smrg	pRGB_return->green = tmp.spec.RGBi.green;
1471ab64890Smrg	pRGB_return->blue  = tmp.spec.RGBi.blue;
1481ab64890Smrg    }
1491ab64890Smrg
1501ab64890Smrg    /* Convert from RGBi to Luv */
1511ab64890Smrg    if (_XcmsConvertColorsWithWhitePt(ccc, &tmp,
15261b2299dSmrg	       ScreenWhitePointOfCCC(ccc), 1, XcmsCIELuvFormat, (Bool *) NULL)
1531ab64890Smrg	    == XcmsFailure) {
1541ab64890Smrg	return (XcmsFailure);
1551ab64890Smrg    }
1561ab64890Smrg
1571ab64890Smrg    memcpy((char *)pColor_return, (char *)&tmp, sizeof(XcmsColor));
15861b2299dSmrg    return (XcmsSuccess);
1591ab64890Smrg}
1601ab64890Smrg
1611ab64890Smrg
1621ab64890Smrg/************************************************************************
1631ab64890Smrg *									*
1641ab64890Smrg *			 PUBLIC ROUTINES				*
1651ab64890Smrg *									*
1661ab64890Smrg ************************************************************************/
1671ab64890Smrg
1681ab64890Smrg/*
1691ab64890Smrg *	NAME
1701ab64890Smrg *		XcmsCIELuvQueryMaxLC - Compute maximum L* and chroma.
1711ab64890Smrg *
1721ab64890Smrg *	SYNOPSIS
1731ab64890Smrg */
1741ab64890SmrgStatus
1751ab64890SmrgXcmsCIELuvQueryMaxLC (
1761ab64890Smrg    XcmsCCC ccc,
1771ab64890Smrg    XcmsFloat hue_angle,	    /* hue angle in degrees */
1781ab64890Smrg    XcmsColor *pColor_return)
1791ab64890Smrg
1801ab64890Smrg/*
1811ab64890Smrg *	DESCRIPTION
1821ab64890Smrg *		Return the point of maximum chroma for the specified
1831ab64890Smrg *		hue angle.
1841ab64890Smrg *
1851ab64890Smrg *	ASSUMPTIONS
1861ab64890Smrg *		This routine assumes that the white point associated with
1871ab64890Smrg *		the color specification is the Screen White Point.  The
1881ab64890Smrg *		Screen White Point will also be associated with the
1891ab64890Smrg *		returned color specification.
1901ab64890Smrg *
1911ab64890Smrg *	RETURNS
1921ab64890Smrg *		XcmsFailure - Failure
1931ab64890Smrg *		XcmsSuccess - Succeeded
1941ab64890Smrg *
19561b2299dSmrg */
1961ab64890Smrg{
1971ab64890Smrg    XcmsCCCRec myCCC;
1981ab64890Smrg
1991ab64890Smrg    /*
2001ab64890Smrg     * Check Arguments
2011ab64890Smrg     */
2021ab64890Smrg    if (ccc == NULL || pColor_return == NULL) {
2031ab64890Smrg	return(XcmsFailure);
2041ab64890Smrg    }
20561b2299dSmrg
2061ab64890Smrg    /* Use my own CCC */
2071ab64890Smrg    memcpy ((char *)&myCCC, (char *)ccc, sizeof(XcmsCCCRec));
2081ab64890Smrg    myCCC.clientWhitePt.format = XcmsUndefinedFormat;
2091ab64890Smrg    myCCC.gamutCompProc = (XcmsCompressionProc)NULL;
2101ab64890Smrg
2111ab64890Smrg    while (hue_angle < 0.0) {
2121ab64890Smrg	hue_angle += 360.0;
2131ab64890Smrg    }
2141ab64890Smrg    while (hue_angle >= 360.0) {
2151ab64890Smrg	hue_angle -= 360.0;
21661b2299dSmrg    }
21761b2299dSmrg
2181ab64890Smrg    return(_XcmsCIELuvQueryMaxLCRGB (&myCCC, radians(hue_angle), pColor_return,
2191ab64890Smrg	    (XcmsRGBi *)NULL));
2201ab64890Smrg}
221