~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/lib/X11/LuvGcLC.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: LuvGcLC.c,v 1.3 2000/08/17 19:44:41 cpqbld Exp $ */
 
2
 
 
3
/*
 
4
 * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
 
5
 *      All Rights Reserved
 
6
 * 
 
7
 * This file is a component of an X Window System-specific implementation
 
8
 * of XCMS based on the TekColor Color Management System.  Permission is
 
9
 * hereby granted to use, copy, modify, sell, and otherwise distribute this
 
10
 * software and its documentation for any purpose and without fee, provided
 
11
 * that this copyright, permission, and disclaimer notice is reproduced in
 
12
 * all copies of this software and in supporting documentation.  TekColor
 
13
 * is a trademark of Tektronix, Inc.
 
14
 * 
 
15
 * Tektronix makes no representation about the suitability of this software
 
16
 * for any purpose.  It is provided "as is" and with all faults.
 
17
 * 
 
18
 * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
 
19
 * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
20
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
 
21
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
22
 * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
 
23
 * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
24
 * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
 
25
 *
 
26
 *      NAME
 
27
 *              CIELuvGcLC.c
 
28
 *
 
29
 *      DESCRIPTION
 
30
 *              Source for XcmsCIELuvClipLuv() gamut
 
31
 *              compression function.
 
32
 */
 
33
/* $XFree86: xc/lib/X11/LuvGcLC.c,v 1.3 2001/01/17 19:41:39 dawes Exp $ */
 
34
 
 
35
#include "Xlibint.h"
 
36
#include "Xcmsint.h"
 
37
#include <math.h>
 
38
 
 
39
/*
 
40
 *      INTERNALS
 
41
 *              Internal defines that need NOT be exported to any package or
 
42
 *              program using this package.
 
43
 */
 
44
#define MAXBISECTCOUNT  100
 
45
 
 
46
/*
 
47
 *      EXTERNS
 
48
 */
 
49
extern Status _XcmsCIELuvQueryMaxLCRGB();
 
50
 
 
51
/* CvCols.c */
 
52
extern Status _XcmsDIConvertColors();
 
53
 
 
54
/* CvColW.c */
 
55
extern Status _XcmsConvertColorsWithWhitePt();
 
56
 
 
57
 
 
58
/************************************************************************
 
59
 *                                                                      *
 
60
 *                       PUBLIC ROUTINES                                *
 
61
 *                                                                      *
 
62
 ************************************************************************/
 
63
 
 
64
/*
 
65
 *      NAME
 
66
 *              XcmsCIELuvClipLuv - Return the closest L* and chroma
 
67
 *
 
68
 *      SYNOPSIS
 
69
 */
 
70
/* ARGSUSED */
 
71
Status
 
72
XcmsCIELuvClipLuv (ccc, pColors_in_out, nColors, i, pCompressed)
 
73
    XcmsCCC ccc;
 
74
    XcmsColor *pColors_in_out;
 
75
    unsigned int nColors;
 
76
    unsigned int i;
 
77
    Bool *pCompressed;
 
78
/*
 
79
 *      DESCRIPTION
 
80
 *              This routine will find the closest L* and chroma 
 
81
 *              for a specific hue.  The color input is converted to
 
82
 *              CIE L*u*v* format and returned as CIE XYZ format.
 
83
 *
 
84
 *              Since this routine works with the L* within
 
85
 *              pColor_in_out intermediate results may be returned
 
86
 *              even though it may be invalid.
 
87
 *
 
88
 *      RETURNS
 
89
 *              XcmsFailure - Failure
 
90
 *              XcmsSuccess - Succeeded
 
91
 *
 
92
 */
 
93
{
 
94
    Status retval;
 
95
    XcmsCCCRec  myCCC;
 
96
    XcmsColor   *pColor;
 
97
    XcmsColor   Luv_max;
 
98
    XcmsFloat   hue, chroma, maxChroma;
 
99
    XcmsFloat   Chroma, bestChroma, Lstar, maxLstar, saveLstar;
 
100
    XcmsFloat   bestLstar, bestustar, bestvstar;
 
101
    XcmsFloat   nT, saveDist, tmpDist;
 
102
    XcmsRGBi    rgb_max;
 
103
    int         nCount, nMaxCount, nI, nILast;
 
104
 
 
105
    /* Use my own CCC */
 
106
    memcpy ((char *)&myCCC, (char *)ccc, sizeof(XcmsCCCRec));
 
107
    myCCC.clientWhitePt.format = XcmsUndefinedFormat;/* inherit screen white */
 
108
    myCCC.gamutCompProc = (XcmsCompressionProc)NULL;/* no gamut compression func */
 
109
 
 
110
    /*
 
111
     * Color specification passed as input can be assumed to:
 
112
     *  1. Be in XcmsCIEXYZFormat
 
113
     *  2. Already be white point adjusted for the Screen White Point.
 
114
     *      This means that the white point now associated with this
 
115
     *      color spec is the Screen White Point (even if the
 
116
     *      ccc->clientWhitePt differs).
 
117
     */
 
118
 
 
119
    pColor = pColors_in_out + i;
 
120
 
 
121
    if (ccc->visual->class < StaticColor) {
 
122
        /*
 
123
         * GRAY !
 
124
         */
 
125
        _XcmsDIConvertColors(ccc, pColor, ScreenWhitePointOfCCC(ccc),
 
126
                1, XcmsCIELuvFormat);
 
127
        _XcmsDIConvertColors(ccc, pColor, ScreenWhitePointOfCCC(ccc),
 
128
                1, XcmsCIEXYZFormat);
 
129
        if (pCompressed) {
 
130
            *(pCompressed + i) = True;
 
131
        }
 
132
        return(XcmsSuccess);
 
133
    }
 
134
 
 
135
    /* Convert from CIEXYZ to CIELuv format */
 
136
    if (_XcmsDIConvertColors(&myCCC, pColor,
 
137
                            ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIELuvFormat)
 
138
                == XcmsFailure) {
 
139
        return(XcmsFailure);
 
140
    }
 
141
 
 
142
    /* Step 1: compute the maximum L* and chroma for this hue. */
 
143
    /*         This copy may be overkill but it preserves the pixel etc. */
 
144
    saveLstar = pColor->spec.CIELuv.L_star;
 
145
    hue = XCMS_CIELUV_PMETRIC_HUE(pColor->spec.CIELuv.u_star,
 
146
                                  pColor->spec.CIELuv.v_star);
 
147
    chroma = XCMS_CIELUV_PMETRIC_CHROMA(pColor->spec.CIELuv.u_star,
 
148
                                        pColor->spec.CIELuv.v_star);
 
149
    memcpy((char *)&Luv_max, (char *)pColor, sizeof(XcmsColor));
 
150
    if (_XcmsCIELuvQueryMaxLCRGB (&myCCC, hue, &Luv_max, &rgb_max) 
 
151
            == XcmsFailure) {
 
152
        return (XcmsFailure);
 
153
    }
 
154
    maxLstar = Luv_max.spec.CIELuv.L_star;
 
155
        
 
156
    /* Now check and return the appropriate L* */
 
157
    if (saveLstar == maxLstar) {
 
158
        /* When the L* input is equal to the maximum L* */
 
159
        /* merely return the maximum Luv point. */
 
160
        memcpy((char *)pColor, (char *)&Luv_max, sizeof(XcmsColor));
 
161
        retval = _XcmsDIConvertColors(&myCCC, pColor,
 
162
                           ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIEXYZFormat);
 
163
    } else {
 
164
        /* return the closest point on the hue leaf. */
 
165
        /* must do a bisection here to compute the delta e. */
 
166
        maxChroma = XCMS_CIELUV_PMETRIC_CHROMA(Luv_max.spec.CIELuv.u_star,
 
167
                                               Luv_max.spec.CIELuv.v_star);
 
168
        nMaxCount = MAXBISECTCOUNT;
 
169
        nI = nMaxCount / 2;
 
170
        bestLstar = Lstar = pColor->spec.CIELuv.L_star;
 
171
        bestustar = pColor->spec.CIELuv.u_star;
 
172
        bestvstar = pColor->spec.CIELuv.v_star;
 
173
        bestChroma = Chroma = chroma;
 
174
        saveDist = XCMS_SQRT(((Chroma - maxChroma) * (Chroma - maxChroma)) +
 
175
                             ((Lstar - maxLstar) * (Lstar - maxLstar)));
 
176
        for (nCount = 0; nCount < nMaxCount; nCount++) {
 
177
            nT = (XcmsFloat) nI / (XcmsFloat) nMaxCount;
 
178
            if (saveLstar > maxLstar) {
 
179
                pColor->spec.RGBi.red   = rgb_max.red * (1.0 - nT) + nT;
 
180
                pColor->spec.RGBi.green = rgb_max.green * (1.0 - nT) + nT;
 
181
                pColor->spec.RGBi.blue  = rgb_max.blue * (1.0 - nT) + nT;
 
182
            } else {
 
183
                pColor->spec.RGBi.red   = rgb_max.red - (rgb_max.red * nT);
 
184
                pColor->spec.RGBi.green = rgb_max.green - (rgb_max.green * nT);
 
185
                pColor->spec.RGBi.blue  = rgb_max.blue - (rgb_max.blue * nT);
 
186
            }
 
187
            pColor->format = XcmsRGBiFormat;
 
188
 
 
189
            /* Convert from RGBi to CIE Luv */
 
190
            if (_XcmsConvertColorsWithWhitePt(&myCCC, pColor,
 
191
                            ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIELuvFormat,
 
192
                            (Bool *) NULL) == XcmsFailure) {
 
193
                return (XcmsFailure);
 
194
            }
 
195
            chroma = XCMS_CIELUV_PMETRIC_CHROMA(pColor->spec.CIELuv.u_star,
 
196
                                                pColor->spec.CIELuv.v_star);
 
197
            tmpDist = XCMS_SQRT(((Chroma - chroma) * (Chroma - chroma)) +
 
198
                                ((Lstar - pColor->spec.CIELuv.L_star) *
 
199
                                 (Lstar - pColor->spec.CIELuv.L_star)));
 
200
            nILast = nI;
 
201
            if (tmpDist > saveDist) {
 
202
                nI /= 2;
 
203
            } else {
 
204
                nI = (nMaxCount + nI) / 2;
 
205
                saveDist = tmpDist;
 
206
                bestLstar = pColor->spec.CIELuv.L_star;
 
207
                bestustar = pColor->spec.CIELuv.u_star;
 
208
                bestvstar = pColor->spec.CIELuv.v_star;
 
209
                bestChroma = chroma;        
 
210
            }
 
211
            if (nI == nILast || nI == 0) {
 
212
                break;
 
213
            }
 
214
        }
 
215
        if (bestChroma >= maxChroma) {
 
216
            pColor->spec.CIELuv.L_star = maxLstar;
 
217
            pColor->spec.CIELuv.u_star = Luv_max.spec.CIELuv.u_star;
 
218
            pColor->spec.CIELuv.v_star = Luv_max.spec.CIELuv.v_star;
 
219
        } else {
 
220
            pColor->spec.CIELuv.L_star = bestLstar;
 
221
            pColor->spec.CIELuv.u_star = bestustar;
 
222
            pColor->spec.CIELuv.v_star = bestvstar;
 
223
        }
 
224
        retval = _XcmsDIConvertColors(&myCCC, pColor,
 
225
                           ScreenWhitePointOfCCC(&myCCC), 1, XcmsCIEXYZFormat);
 
226
 
 
227
        if (retval != XcmsFailure && pCompressed != NULL) {
 
228
            *(pCompressed + i) = True;
 
229
        }
 
230
    }
 
231
    return(retval);
 
232
}