3 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
6 * This file is a component of an X Window System-specific implementation
7 * of XCMS based on the TekColor Color Management System. Permission is
8 * hereby granted to use, copy, modify, sell, and otherwise distribute this
9 * software and its documentation for any purpose and without fee, provided
10 * that this copyright, permission, and disclaimer notice is reproduced in
11 * all copies of this software and in supporting documentation. TekColor
12 * is a trademark of Tektronix, Inc.
14 * Tektronix makes no representation about the suitability of this software
15 * for any purpose. It is provided "as is" and with all faults.
17 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
18 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE. IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
20 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
22 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
23 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
29 * Source for XcmsCIELuvClipLuv() gamut
30 * compression function.
43 * Internal defines that need NOT be exported to any package or
44 * program using this package.
46 #define MAXBISECTCOUNT 100
49 /************************************************************************
53 ************************************************************************/
57 * XcmsCIELuvClipLuv - Return the closest L* and chroma
65 XcmsColor *pColors_in_out,
71 * This routine will find the closest L* and chroma
72 * for a specific hue. The color input is converted to
73 * CIE L*u*v* format and returned as CIE XYZ format.
75 * Since this routine works with the L* within
76 * pColor_in_out intermediate results may be returned
77 * even though it may be invalid.
80 * XcmsFailure - Failure
81 * XcmsSuccess - Succeeded
89 XcmsFloat hue, chroma, maxChroma;
90 XcmsFloat Chroma, bestChroma, Lstar, maxLstar, saveLstar;
91 XcmsFloat bestLstar, bestustar, bestvstar;
92 XcmsFloat nT, saveDist, tmpDist;
94 int nCount, nMaxCount, nI, nILast;
97 memcpy ((char *)&myCCC, (char *)ccc, sizeof(XcmsCCCRec));
98 myCCC.clientWhitePt.format = XcmsUndefinedFormat;/* inherit screen white */
99 myCCC.gamutCompProc = (XcmsCompressionProc)NULL;/* no gamut compression func */
102 * Color specification passed as input can be assumed to:
103 * 1. Be in XcmsCIEXYZFormat
104 * 2. Already be white point adjusted for the Screen White Point.
105 * This means that the white point now associated with this
106 * color spec is the Screen White Point (even if the
107 * ccc->clientWhitePt differs).
110 pColor = pColors_in_out + i;
112 if (ccc->visual->class < StaticColor) {
116 _XcmsDIConvertColors(ccc, pColor, ScreenWhitePointOfCCC(ccc),
117 1, XcmsCIELuvFormat);
118 _XcmsDIConvertColors(ccc, pColor, ScreenWhitePointOfCCC(ccc),
119 1, XcmsCIEXYZFormat);
121 *(pCompressed + i) = True;
126 /* Convert from CIEXYZ to CIELuv format */
127 if (_XcmsDIConvertColors(&myCCC, pColor,
128 ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIELuvFormat)
133 /* Step 1: compute the maximum L* and chroma for this hue. */
134 /* This copy may be overkill but it preserves the pixel etc. */
135 saveLstar = pColor->spec.CIELuv.L_star;
136 hue = XCMS_CIELUV_PMETRIC_HUE(pColor->spec.CIELuv.u_star,
137 pColor->spec.CIELuv.v_star);
138 chroma = XCMS_CIELUV_PMETRIC_CHROMA(pColor->spec.CIELuv.u_star,
139 pColor->spec.CIELuv.v_star);
140 memcpy((char *)&Luv_max, (char *)pColor, sizeof(XcmsColor));
141 if (_XcmsCIELuvQueryMaxLCRGB (&myCCC, hue, &Luv_max, &rgb_max)
143 return (XcmsFailure);
145 maxLstar = Luv_max.spec.CIELuv.L_star;
147 /* Now check and return the appropriate L* */
148 if (saveLstar == maxLstar) {
149 /* When the L* input is equal to the maximum L* */
150 /* merely return the maximum Luv point. */
151 memcpy((char *)pColor, (char *)&Luv_max, sizeof(XcmsColor));
152 retval = _XcmsDIConvertColors(&myCCC, pColor,
153 ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIEXYZFormat);
155 /* return the closest point on the hue leaf. */
156 /* must do a bisection here to compute the delta e. */
157 maxChroma = XCMS_CIELUV_PMETRIC_CHROMA(Luv_max.spec.CIELuv.u_star,
158 Luv_max.spec.CIELuv.v_star);
159 nMaxCount = MAXBISECTCOUNT;
161 bestLstar = Lstar = pColor->spec.CIELuv.L_star;
162 bestustar = pColor->spec.CIELuv.u_star;
163 bestvstar = pColor->spec.CIELuv.v_star;
164 bestChroma = Chroma = chroma;
165 saveDist = XCMS_SQRT(((Chroma - maxChroma) * (Chroma - maxChroma)) +
166 ((Lstar - maxLstar) * (Lstar - maxLstar)));
167 for (nCount = 0; nCount < nMaxCount; nCount++) {
168 nT = (XcmsFloat) nI / (XcmsFloat) nMaxCount;
169 if (saveLstar > maxLstar) {
170 pColor->spec.RGBi.red = rgb_max.red * (1.0 - nT) + nT;
171 pColor->spec.RGBi.green = rgb_max.green * (1.0 - nT) + nT;
172 pColor->spec.RGBi.blue = rgb_max.blue * (1.0 - nT) + nT;
174 pColor->spec.RGBi.red = rgb_max.red - (rgb_max.red * nT);
175 pColor->spec.RGBi.green = rgb_max.green - (rgb_max.green * nT);
176 pColor->spec.RGBi.blue = rgb_max.blue - (rgb_max.blue * nT);
178 pColor->format = XcmsRGBiFormat;
180 /* Convert from RGBi to CIE Luv */
181 if (_XcmsConvertColorsWithWhitePt(&myCCC, pColor,
182 ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIELuvFormat,
183 (Bool *) NULL) == XcmsFailure) {
184 return (XcmsFailure);
186 chroma = XCMS_CIELUV_PMETRIC_CHROMA(pColor->spec.CIELuv.u_star,
187 pColor->spec.CIELuv.v_star);
188 tmpDist = XCMS_SQRT(((Chroma - chroma) * (Chroma - chroma)) +
189 ((Lstar - pColor->spec.CIELuv.L_star) *
190 (Lstar - pColor->spec.CIELuv.L_star)));
192 if (tmpDist > saveDist) {
195 nI = (nMaxCount + nI) / 2;
197 bestLstar = pColor->spec.CIELuv.L_star;
198 bestustar = pColor->spec.CIELuv.u_star;
199 bestvstar = pColor->spec.CIELuv.v_star;
202 if (nI == nILast || nI == 0) {
206 if (bestChroma >= maxChroma) {
207 pColor->spec.CIELuv.L_star = maxLstar;
208 pColor->spec.CIELuv.u_star = Luv_max.spec.CIELuv.u_star;
209 pColor->spec.CIELuv.v_star = Luv_max.spec.CIELuv.v_star;
211 pColor->spec.CIELuv.L_star = bestLstar;
212 pColor->spec.CIELuv.u_star = bestustar;
213 pColor->spec.CIELuv.v_star = bestvstar;
215 retval = _XcmsDIConvertColors(&myCCC, pColor,
216 ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIEXYZFormat);
218 if (retval != XcmsFailure && pCompressed != NULL) {
219 *(pCompressed + i) = True;