cmsgmt.c 20.8 KB
Newer Older
D
duke 已提交
1 2 3 4 5
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
6
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
7
 * particular file as subject to the "Classpath" exception as provided
8
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
9 10 11 12 13 14 15 16 17 18 19
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
23 24 25 26 27 28 29
 */

// This file is available under and governed by the GNU General Public
// License version 2 only, as published by the Free Software Foundation.
// However, the following notice accompanied the original version of this
// file:
//
30
//---------------------------------------------------------------------------------
D
duke 已提交
31
//
32
//  Little Color Management System
B
bae 已提交
33
//  Copyright (c) 1998-2012 Marti Maria Saguer
D
duke 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 53 54
//
//---------------------------------------------------------------------------------
//
D
duke 已提交
55

56
#include "lcms2_internal.h"
D
duke 已提交
57 58


59 60 61 62 63 64 65 66 67 68 69
// Auxiliar: append a Lab identity after the given sequence of profiles
// and return the transform. Lab profile is closed, rest of profiles are kept open.
cmsHTRANSFORM _cmsChain2Lab(cmsContext            ContextID,
                            cmsUInt32Number        nProfiles,
                            cmsUInt32Number        InputFormat,
                            cmsUInt32Number        OutputFormat,
                            const cmsUInt32Number  Intents[],
                            const cmsHPROFILE      hProfiles[],
                            const cmsBool          BPC[],
                            const cmsFloat64Number AdaptationStates[],
                            cmsUInt32Number        dwFlags)
D
duke 已提交
70
{
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    cmsHTRANSFORM xform;
    cmsHPROFILE   hLab;
    cmsHPROFILE   ProfileList[256];
    cmsBool       BPCList[256];
    cmsFloat64Number AdaptationList[256];
    cmsUInt32Number IntentList[256];
    cmsUInt32Number i;

    // This is a rather big number and there is no need of dynamic memory
    // since we are adding a profile, 254 + 1 = 255 and this is the limit
    if (nProfiles > 254) return NULL;

    // The output space
    hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
    if (hLab == NULL) return NULL;

    // Create a copy of parameters
    for (i=0; i < nProfiles; i++) {

        ProfileList[i]    = hProfiles[i];
        BPCList[i]        = BPC[i];
        AdaptationList[i] = AdaptationStates[i];
        IntentList[i]     = Intents[i];
    }
D
duke 已提交
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    // Place Lab identity at chain's end.
    ProfileList[nProfiles]    = hLab;
    BPCList[nProfiles]        = 0;
    AdaptationList[nProfiles] = 1.0;
    IntentList[nProfiles]     = INTENT_RELATIVE_COLORIMETRIC;

    // Create the transform
    xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList,
                                       BPCList,
                                       IntentList,
                                       AdaptationList,
                                       NULL, 0,
                                       InputFormat,
                                       OutputFormat,
                                       dwFlags);
D
duke 已提交
111

112
    cmsCloseProfile(hLab);
D
duke 已提交
113

114
    return xform;
D
duke 已提交
115 116 117
}


118 119
// Compute K -> L* relationship. Flags may include black point compensation. In this case,
// the relationship is assumed from the profile with BPC to a black point zero.
D
duke 已提交
120
static
121 122 123 124 125 126 127 128
cmsToneCurve* ComputeKToLstar(cmsContext            ContextID,
                               cmsUInt32Number       nPoints,
                               cmsUInt32Number       nProfiles,
                               const cmsUInt32Number Intents[],
                               const cmsHPROFILE     hProfiles[],
                               const cmsBool         BPC[],
                               const cmsFloat64Number AdaptationStates[],
                               cmsUInt32Number dwFlags)
D
duke 已提交
129
{
130 131 132 133 134 135
    cmsToneCurve* out = NULL;
    cmsUInt32Number i;
    cmsHTRANSFORM xform;
    cmsCIELab Lab;
    cmsFloat32Number cmyk[4];
    cmsFloat32Number* SampledPoints;
D
duke 已提交
136

137 138
    xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
    if (xform == NULL) return NULL;
D
duke 已提交
139

140 141
    SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number));
    if (SampledPoints  == NULL) goto Error;
D
duke 已提交
142

143
    for (i=0; i < nPoints; i++) {
D
duke 已提交
144

145 146 147 148
        cmyk[0] = 0;
        cmyk[1] = 0;
        cmyk[2] = 0;
        cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1));
D
duke 已提交
149

150 151 152
        cmsDoTransform(xform, cmyk, &Lab, 1);
        SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation
    }
D
duke 已提交
153

154
    out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints);
D
duke 已提交
155

156
Error:
D
duke 已提交
157

158 159
    cmsDeleteTransform(xform);
    if (SampledPoints) _cmsFree(ContextID, SampledPoints);
D
duke 已提交
160

161
    return out;
D
duke 已提交
162 163 164
}


165 166 167 168 169 170 171 172 173 174 175
// Compute Black tone curve on a CMYK -> CMYK transform. This is done by
// using the proof direction on both profiles to find K->L* relationship
// then joining both curves. dwFlags may include black point compensation.
cmsToneCurve* _cmsBuildKToneCurve(cmsContext        ContextID,
                                   cmsUInt32Number   nPoints,
                                   cmsUInt32Number   nProfiles,
                                   const cmsUInt32Number Intents[],
                                   const cmsHPROFILE hProfiles[],
                                   const cmsBool     BPC[],
                                   const cmsFloat64Number AdaptationStates[],
                                   cmsUInt32Number   dwFlags)
D
duke 已提交
176
{
177
    cmsToneCurve *in, *out, *KTone;
D
duke 已提交
178

179 180 181
    // Make sure CMYK -> CMYK
    if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
        cmsGetColorSpace(hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL;
D
duke 已提交
182 183


184 185
    // Make sure last is an output profile
    if (cmsGetDeviceClass(hProfiles[nProfiles - 1]) != cmsSigOutputClass) return NULL;
D
duke 已提交
186

187 188 189 190 191 192 193
    // Create individual curves. BPC works also as each K to L* is
    // computed as a BPC to zero black point in case of L*
    in  = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
    if (in == NULL) return NULL;

    out = ComputeKToLstar(ContextID, nPoints, 1,
                            Intents + (nProfiles - 1),
P
prr 已提交
194
                            &hProfiles [nProfiles - 1],
195 196 197 198 199 200
                            BPC + (nProfiles - 1),
                            AdaptationStates + (nProfiles - 1),
                            dwFlags);
    if (out == NULL) {
        cmsFreeToneCurve(in);
        return NULL;
D
duke 已提交
201 202
    }

203 204 205
    // Build the relationship. This effectively limits the maximum accuracy to 16 bits, but
    // since this is used on black-preserving LUTs, we are not loosing  accuracy in any case
    KTone = cmsJoinToneCurve(ContextID, in, out, nPoints);
D
duke 已提交
206

207 208
    // Get rid of components
    cmsFreeToneCurve(in); cmsFreeToneCurve(out);
D
duke 已提交
209

210 211
    // Something went wrong...
    if (KTone == NULL) return NULL;
D
duke 已提交
212

213 214 215 216
    // Make sure it is monotonic
    if (!cmsIsToneCurveMonotonic(KTone)) {
        cmsFreeToneCurve(KTone);
        return NULL;
D
duke 已提交
217 218
    }

219
    return KTone;
D
duke 已提交
220 221 222
}


223
// Gamut LUT Creation -----------------------------------------------------------------------------------------
D
duke 已提交
224 225 226 227 228

// Used by gamut & softproofing

typedef struct {

229
    cmsHTRANSFORM hInput;               // From whatever input color space. 16 bits to DBL
D
duke 已提交
230
    cmsHTRANSFORM hForward, hReverse;   // Transforms going from Lab to colorant and back
231
    cmsFloat64Number Thereshold;        // The thereshold after which is considered out of gamut
D
duke 已提交
232

233
    } GAMUTCHAIN;
D
duke 已提交
234 235 236 237 238 239 240 241 242

// This sampler does compute gamut boundaries by comparing original
// values with a transform going back and forth. Values above ERR_THERESHOLD
// of maximum are considered out of gamut.

#define ERR_THERESHOLD      5


static
243
int GamutSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
D
duke 已提交
244
{
245
    GAMUTCHAIN*  t = (GAMUTCHAIN* ) Cargo;
D
duke 已提交
246 247
    cmsCIELab LabIn1, LabOut1;
    cmsCIELab LabIn2, LabOut2;
B
bae 已提交
248
    cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS];
249
    cmsFloat64Number dE1, dE2, ErrorRatio;
D
duke 已提交
250 251 252 253

    // Assume in-gamut by default.
    ErrorRatio = 1.0;

254
    // Convert input to Lab
P
prr 已提交
255
    cmsDoTransform(t -> hInput, In, &LabIn1, 1);
D
duke 已提交
256 257 258

    // converts from PCS to colorant. This always
    // does return in-gamut values,
259
    cmsDoTransform(t -> hForward, &LabIn1, Proof, 1);
D
duke 已提交
260 261

    // Now, do the inverse, from colorant to PCS.
262
    cmsDoTransform(t -> hReverse, Proof, &LabOut1, 1);
D
duke 已提交
263

264
    memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab));
D
duke 已提交
265 266

    // Try again, but this time taking Check as input
P
prr 已提交
267
    cmsDoTransform(t -> hForward, &LabOut1, Proof2, 1);
268
    cmsDoTransform(t -> hReverse, Proof2, &LabOut2, 1);
D
duke 已提交
269

270 271
    // Take difference of direct value
    dE1 = cmsDeltaE(&LabIn1, &LabOut1);
D
duke 已提交
272

273 274
    // Take difference of converted value
    dE2 = cmsDeltaE(&LabIn2, &LabOut2);
D
duke 已提交
275 276


277 278 279
    // if dE1 is small and dE2 is small, value is likely to be in gamut
    if (dE1 < t->Thereshold && dE2 < t->Thereshold)
        Out[0] = 0;
D
duke 已提交
280 281
    else {

282 283
        // if dE1 is small and dE2 is big, undefined. Assume in gamut
        if (dE1 < t->Thereshold && dE2 > t->Thereshold)
D
duke 已提交
284 285
            Out[0] = 0;
        else
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
            // dE1 is big and dE2 is small, clearly out of gamut
            if (dE1 > t->Thereshold && dE2 < t->Thereshold)
                Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Thereshold) + .5);
            else  {

                // dE1 is big and dE2 is also big, could be due to perceptual mapping
                // so take error ratio
                if (dE2 == 0.0)
                    ErrorRatio = dE1;
                else
                    ErrorRatio = dE1 / dE2;

                if (ErrorRatio > t->Thereshold)
                    Out[0] = (cmsUInt16Number)  _cmsQuickFloor((ErrorRatio - t->Thereshold) + .5);
                else
                    Out[0] = 0;
            }
D
duke 已提交
303 304
    }

305

D
duke 已提交
306 307 308
    return TRUE;
}

309 310 311
// Does compute a gamut LUT going back and forth across pcs -> relativ. colorimetric intent -> pcs
// the dE obtained is then annotated on the LUT. Values truely out of gamut are clipped to dE = 0xFFFE
// and values changed are supposed to be handled by any gamut remapping, so, are out of gamut as well.
D
duke 已提交
312
//
313 314 315 316 317 318 319 320 321 322
// **WARNING: This algorithm does assume that gamut remapping algorithms does NOT move in-gamut colors,
// of course, many perceptual and saturation intents does not work in such way, but relativ. ones should.

cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID,
                                          cmsHPROFILE hProfiles[],
                                          cmsBool  BPC[],
                                          cmsUInt32Number Intents[],
                                          cmsFloat64Number AdaptationStates[],
                                          cmsUInt32Number nGamutPCSposition,
                                          cmsHPROFILE hGamut)
D
duke 已提交
323 324
{
    cmsHPROFILE hLab;
325 326 327
    cmsPipeline* Gamut;
    cmsStage* CLUT;
    cmsUInt32Number dwFormat;
D
duke 已提交
328
    GAMUTCHAIN Chain;
329 330 331 332 333 334 335
    int nChannels, nGridpoints;
    cmsColorSpaceSignature ColorSpace;
    cmsUInt32Number i;
    cmsHPROFILE ProfileList[256];
    cmsBool     BPCList[256];
    cmsFloat64Number AdaptationList[256];
    cmsUInt32Number IntentList[256];
D
duke 已提交
336

337
    memset(&Chain, 0, sizeof(GAMUTCHAIN));
D
duke 已提交
338 339


340 341 342 343 344 345 346
    if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) {
        cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition);
        return NULL;
    }

    hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
    if (hLab == NULL) return NULL;
D
duke 已提交
347 348 349 350 351 352


    // The figure of merit. On matrix-shaper profiles, should be almost zero as
    // the conversion is pretty exact. On LUT based profiles, different resolutions
    // of input and output CLUT may result in differences.

353
    if (cmsIsMatrixShaper(hGamut)) {
D
duke 已提交
354 355

        Chain.Thereshold = 1.0;
356 357
    }
    else {
D
duke 已提交
358
        Chain.Thereshold = ERR_THERESHOLD;
359
    }
D
duke 已提交
360 361


362 363 364 365 366 367 368
    // Create a copy of parameters
    for (i=0; i < nGamutPCSposition; i++) {
        ProfileList[i]    = hProfiles[i];
        BPCList[i]        = BPC[i];
        AdaptationList[i] = AdaptationStates[i];
        IntentList[i]     = Intents[i];
    }
D
duke 已提交
369

370 371 372 373
    // Fill Lab identity
    ProfileList[nGamutPCSposition] = hLab;
    BPCList[nGamutPCSposition] = 0;
    AdaptationList[nGamutPCSposition] = 1.0;
P
prr 已提交
374
    IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC;
D
duke 已提交
375 376


377
    ColorSpace  = cmsGetColorSpace(hGamut);
D
duke 已提交
378

379 380 381 382 383 384
    nChannels   = cmsChannelsOf(ColorSpace);
    nGridpoints = _cmsReasonableGridpointsByColorspace(ColorSpace, cmsFLAGS_HIGHRESPRECALC);
    dwFormat    = (CHANNELS_SH(nChannels)|BYTES_SH(2));

    // 16 bits to Lab double
    Chain.hInput = cmsCreateExtendedTransform(ContextID,
P
prr 已提交
385 386 387 388 389 390 391 392
        nGamutPCSposition + 1,
        ProfileList,
        BPCList,
        IntentList,
        AdaptationList,
        NULL, 0,
        dwFormat, TYPE_Lab_DBL,
        cmsFLAGS_NOCACHE);
D
duke 已提交
393 394


B
bae 已提交
395 396
    // Does create the forward step. Lab double to device
    dwFormat    = (CHANNELS_SH(nChannels)|BYTES_SH(2));
397
    Chain.hForward = cmsCreateTransformTHR(ContextID,
P
prr 已提交
398 399 400 401
        hLab, TYPE_Lab_DBL,
        hGamut, dwFormat,
        INTENT_RELATIVE_COLORIMETRIC,
        cmsFLAGS_NOCACHE);
402 403 404

    // Does create the backwards step
    Chain.hReverse = cmsCreateTransformTHR(ContextID, hGamut, dwFormat,
P
prr 已提交
405 406 407
        hLab, TYPE_Lab_DBL,
        INTENT_RELATIVE_COLORIMETRIC,
        cmsFLAGS_NOCACHE);
D
duke 已提交
408 409 410


    // All ok?
P
prr 已提交
411
    if (Chain.hInput && Chain.hForward && Chain.hReverse) {
D
duke 已提交
412

413 414
        // Go on, try to compute gamut LUT from PCS. This consist on a single channel containing
        // dE when doing a transform back and forth on the colorimetric intent.
D
duke 已提交
415

416 417
        Gamut = cmsPipelineAlloc(ContextID, 3, 1);
        if (Gamut != NULL) {
D
duke 已提交
418

P
prr 已提交
419 420 421 422 423 424 425 426
            CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL);
            if (!cmsPipelineInsertStage(Gamut, cmsAT_BEGIN, CLUT)) {
                cmsPipelineFree(Gamut);
                Gamut = NULL;
            }
            else {
                cmsStageSampleCLut16bit(CLUT, GamutSampler, (void*) &Chain, 0);
            }
427
        }
D
duke 已提交
428 429 430 431 432 433 434 435
    }
    else
        Gamut = NULL;   // Didn't work...

    // Free all needed stuff.
    if (Chain.hInput)   cmsDeleteTransform(Chain.hInput);
    if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
    if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
436
    if (hLab) cmsCloseProfile(hLab);
D
duke 已提交
437 438 439 440 441

    // And return computed hull
    return Gamut;
}

442
// Total Area Coverage estimation ----------------------------------------------------------------
D
duke 已提交
443

444 445 446 447 448
typedef struct {
    cmsUInt32Number  nOutputChans;
    cmsHTRANSFORM    hRoundTrip;
    cmsFloat32Number MaxTAC;
    cmsFloat32Number MaxInput[cmsMAXCHANNELS];
D
duke 已提交
449

450
} cmsTACestimator;
D
duke 已提交
451 452


453 454
// This callback just accounts the maximum ink dropped in the given node. It does not populate any
// memory, as the destination table is NULL. Its only purpose it to know the global maximum.
D
duke 已提交
455
static
456
int EstimateTAC(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
D
duke 已提交
457
{
458 459 460 461
    cmsTACestimator* bp = (cmsTACestimator*) Cargo;
    cmsFloat32Number RoundTrip[cmsMAXCHANNELS];
    cmsUInt32Number i;
    cmsFloat32Number Sum;
D
duke 已提交
462 463


464 465
    // Evaluate the xform
    cmsDoTransform(bp->hRoundTrip, In, RoundTrip, 1);
D
duke 已提交
466

467 468 469
    // All all amounts of ink
    for (Sum=0, i=0; i < bp ->nOutputChans; i++)
            Sum += RoundTrip[i];
D
duke 已提交
470

471 472
    // If above maximum, keep track of input values
    if (Sum > bp ->MaxTAC) {
D
duke 已提交
473

474
            bp ->MaxTAC = Sum;
D
duke 已提交
475

476 477 478
            for (i=0; i < bp ->nOutputChans; i++) {
                bp ->MaxInput[i] = In[i];
            }
D
duke 已提交
479 480 481 482
    }

    return TRUE;

483
    cmsUNUSED_PARAMETER(Out);
D
duke 已提交
484 485 486
}


487 488
// Detect Total area coverage of the profile
cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsHPROFILE hProfile)
D
duke 已提交
489
{
490 491 492 493 494
    cmsTACestimator bp;
    cmsUInt32Number dwFormatter;
    cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS];
    cmsHPROFILE hLab;
    cmsContext ContextID = cmsGetProfileContextID(hProfile);
D
duke 已提交
495

496 497 498
    // TAC only works on output profiles
    if (cmsGetDeviceClass(hProfile) != cmsSigOutputClass) {
        return 0;
D
duke 已提交
499 500
    }

501 502
    // Create a fake formatter for result
    dwFormatter = cmsFormatterForColorspaceOfProfile(hProfile, 4, TRUE);
D
duke 已提交
503

504 505
    bp.nOutputChans = T_CHANNELS(dwFormatter);
    bp.MaxTAC = 0;    // Initial TAC is 0
D
duke 已提交
506

507 508
    //  for safety
    if (bp.nOutputChans >= cmsMAXCHANNELS) return 0;
D
duke 已提交
509

510 511 512 513 514
    hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
    if (hLab == NULL) return 0;
    // Setup a roundtrip on perceptual intent in output profile for TAC estimation
    bp.hRoundTrip = cmsCreateTransformTHR(ContextID, hLab, TYPE_Lab_16,
                                          hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
D
duke 已提交
515

516 517
    cmsCloseProfile(hLab);
    if (bp.hRoundTrip == NULL) return 0;
D
duke 已提交
518

519 520 521 522
    // For L* we only need black and white. For C* we need many points
    GridPoints[0] = 6;
    GridPoints[1] = 74;
    GridPoints[2] = 74;
D
duke 已提交
523 524


525 526
    if (!cmsSliceSpace16(3, GridPoints, EstimateTAC, &bp)) {
        bp.MaxTAC = 0;
D
duke 已提交
527 528
    }

529
    cmsDeleteTransform(bp.hRoundTrip);
D
duke 已提交
530

531 532 533
    // Results in %
    return bp.MaxTAC;
}
D
duke 已提交
534 535


536
// Carefully,  clamp on CIELab space.
537

538 539 540 541
cmsBool CMSEXPORT cmsDesaturateLab(cmsCIELab* Lab,
                                   double amax, double amin,
                                   double bmax, double bmin)
{
D
duke 已提交
542

543
    // Whole Luma surface to zero
D
duke 已提交
544

545
    if (Lab -> L < 0) {
D
duke 已提交
546

547 548
        Lab-> L = Lab->a = Lab-> b = 0.0;
        return FALSE;
D
duke 已提交
549 550
    }

551 552 553
    // Clamp white, DISCARD HIGHLIGHTS. This is done
    // in such way because icc spec doesn't allow the
    // use of L>100 as a highlight means.
D
duke 已提交
554

555 556
    if (Lab->L > 100)
        Lab -> L = 100;
D
duke 已提交
557

558
    // Check out gamut prism, on a, b faces
D
duke 已提交
559

560 561
    if (Lab -> a < amin || Lab->a > amax||
        Lab -> b < bmin || Lab->b > bmax) {
D
duke 已提交
562

563 564
            cmsCIELCh LCh;
            double h, slope;
D
duke 已提交
565

566 567
            // Falls outside a, b limits. Transports to LCh space,
            // and then do the clipping
D
duke 已提交
568 569


570
            if (Lab -> a == 0.0) { // Is hue exactly 90?
D
duke 已提交
571

572 573 574 575
                // atan will not work, so clamp here
                Lab -> b = Lab->b < 0 ? bmin : bmax;
                return TRUE;
            }
D
duke 已提交
576

577
            cmsLab2LCh(&LCh, Lab);
D
duke 已提交
578

579 580
            slope = Lab -> b / Lab -> a;
            h = LCh.h;
D
duke 已提交
581

582
            // There are 4 zones
D
duke 已提交
583

584 585
            if ((h >= 0. && h < 45.) ||
                (h >= 315 && h <= 360.)) {
D
duke 已提交
586

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
                    // clip by amax
                    Lab -> a = amax;
                    Lab -> b = amax * slope;
            }
            else
                if (h >= 45. && h < 135.)
                {
                    // clip by bmax
                    Lab -> b = bmax;
                    Lab -> a = bmax / slope;
                }
                else
                    if (h >= 135. && h < 225.) {
                        // clip by amin
                        Lab -> a = amin;
                        Lab -> b = amin * slope;
D
duke 已提交
603

604 605 606 607 608 609 610 611 612 613 614
                    }
                    else
                        if (h >= 225. && h < 315.) {
                            // clip by bmin
                            Lab -> b = bmin;
                            Lab -> a = bmin / slope;
                        }
                        else  {
                            cmsSignalError(0, cmsERROR_RANGE, "Invalid angle");
                            return FALSE;
                        }
D
duke 已提交
615 616 617

    }

618
    return TRUE;
D
duke 已提交
619
}