Coverage Report

Created: 2024-02-25 06:47

/src/mupdf/thirdparty/lcms2/src/cmssamp.c
Line
Count
Source (jump to first uncovered line)
1
//---------------------------------------------------------------------------------
2
//
3
//  Little Color Management System
4
//  Copyright (c) 1998-2022 Marti Maria Saguer
5
//
6
// Permission is hereby granted, free of charge, to any person obtaining
7
// a copy of this software and associated documentation files (the "Software"),
8
// to deal in the Software without restriction, including without limitation
9
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
// and/or sell copies of the Software, and to permit persons to whom the Software
11
// is furnished to do so, subject to the following conditions:
12
//
13
// The above copyright notice and this permission notice shall be included in
14
// all copies or substantial portions of the Software.
15
//
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
//
24
//---------------------------------------------------------------------------------
25
//
26
27
#include "lcms2_internal.h"
28
29
30
37.8k
#define cmsmin(a, b) (((a) < (b)) ? (a) : (b))
31
34
#define cmsmax(a, b) (((a) > (b)) ? (a) : (b))
32
33
// This file contains routines for resampling and LUT optimization, black point detection
34
// and black preservation.
35
36
// Black point detection -------------------------------------------------------------------------
37
38
39
// PCS -> PCS round trip transform, always uses relative intent on the device -> pcs
40
static
41
cmsHTRANSFORM CreateRoundtripXForm(cmsContext ContextID, cmsHPROFILE hProfile, cmsUInt32Number nIntent)
42
927
{
43
927
    cmsHPROFILE hLab = cmsCreateLab4Profile(ContextID, NULL);
44
927
    cmsHTRANSFORM xform;
45
927
    cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
46
927
    cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
47
927
    cmsHPROFILE hProfiles[4];
48
927
    cmsUInt32Number Intents[4];
49
50
927
    hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
51
927
    Intents[0]   = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
52
53
927
    xform =  cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents,
54
927
        States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
55
56
927
    cmsCloseProfile(ContextID, hLab);
57
927
    return xform;
58
927
}
59
60
// Use darker colorants to obtain black point. This works in the relative colorimetric intent and
61
// assumes more ink results in darker colors. No ink limit is assumed.
62
static
63
cmsBool  BlackPointAsDarkerColorant(cmsContext ContextID,
64
                                    cmsHPROFILE hInput,
65
                                    cmsUInt32Number Intent,
66
                                    cmsCIEXYZ* BlackPoint,
67
                                    cmsUInt32Number dwFlags)
68
15.9k
{
69
15.9k
    cmsUInt16Number *Black;
70
15.9k
    cmsHTRANSFORM xform;
71
15.9k
    cmsColorSpaceSignature Space;
72
15.9k
    cmsUInt32Number nChannels;
73
15.9k
    cmsUInt32Number dwFormat;
74
15.9k
    cmsHPROFILE hLab;
75
15.9k
    cmsCIELab  Lab;
76
15.9k
    cmsCIEXYZ  BlackXYZ;
77
78
    // If the profile does not support input direction, assume Black point 0
79
15.9k
    if (!cmsIsIntentSupported(ContextID, hInput, Intent, LCMS_USED_AS_INPUT)) {
80
81
43
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
82
43
        return FALSE;
83
43
    }
84
85
    // Create a formatter which has n channels and no floating point
86
15.8k
    dwFormat = cmsFormatterForColorspaceOfProfile(ContextID, hInput, 2, FALSE);
87
88
    // Try to get black by using black colorant
89
15.8k
    Space = cmsGetColorSpace(ContextID, hInput);
90
91
    // This function returns darker colorant in 16 bits for several spaces
92
15.8k
    if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) {
93
94
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
95
0
        return FALSE;
96
0
    }
97
98
15.8k
    if (nChannels != T_CHANNELS(dwFormat)) {
99
0
       BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
100
0
       return FALSE;
101
0
    }
102
103
    // Lab will be used as the output space, but lab2 will avoid recursion
104
15.8k
    hLab = cmsCreateLab2Profile(ContextID, NULL);
105
15.8k
    if (hLab == NULL) {
106
0
       BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
107
0
       return FALSE;
108
0
    }
109
110
    // Create the transform
111
15.8k
    xform = cmsCreateTransform(ContextID, hInput, dwFormat,
112
15.8k
                                hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
113
15.8k
    cmsCloseProfile(ContextID, hLab);
114
115
15.8k
    if (xform == NULL) {
116
117
        // Something went wrong. Get rid of open resources and return zero as black
118
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
119
0
        return FALSE;
120
0
    }
121
122
    // Convert black to Lab
123
15.8k
    cmsDoTransform(ContextID, xform, Black, &Lab, 1);
124
125
    // Force it to be neutral, clip to max. L* of 50
126
15.8k
    Lab.a = Lab.b = 0;
127
15.8k
    if (Lab.L > 50) Lab.L = 50;
128
15.8k
    if (Lab.L < 0) Lab.L = 0;
129
130
    // Free the resources
131
15.8k
    cmsDeleteTransform(ContextID, xform);
132
133
    // Convert from Lab (which is now clipped) to XYZ.
134
15.8k
    cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &Lab);
135
136
15.8k
    if (BlackPoint != NULL)
137
15.8k
        *BlackPoint = BlackXYZ;
138
139
15.8k
    return TRUE;
140
141
0
    cmsUNUSED_PARAMETER(dwFlags);
142
0
}
143
144
// Get a black point of output CMYK profile, discounting any ink-limiting embedded
145
// in the profile. For doing that, we use perceptual intent in input direction:
146
// Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
147
static
148
cmsBool BlackPointUsingPerceptualBlack(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
149
890
{
150
890
    cmsHTRANSFORM hRoundTrip;
151
890
    cmsCIELab LabIn, LabOut;
152
890
    cmsCIEXYZ  BlackXYZ;
153
154
     // Is the intent supported by the profile?
155
890
    if (!cmsIsIntentSupported(ContextID, hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
156
157
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
158
0
        return TRUE;
159
0
    }
160
161
890
    hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, INTENT_PERCEPTUAL);
162
890
    if (hRoundTrip == NULL) {
163
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
164
0
        return FALSE;
165
0
    }
166
167
890
    LabIn.L = LabIn.a = LabIn.b = 0;
168
890
    cmsDoTransform(ContextID, hRoundTrip, &LabIn, &LabOut, 1);
169
170
    // Clip Lab to reasonable limits
171
890
    if (LabOut.L > 50) LabOut.L = 50;
172
890
    LabOut.a = LabOut.b = 0;
173
174
890
    cmsDeleteTransform(ContextID, hRoundTrip);
175
176
    // Convert it to XYZ
177
890
    cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &LabOut);
178
179
890
    if (BlackPoint != NULL)
180
890
        *BlackPoint = BlackXYZ;
181
182
890
    return TRUE;
183
890
}
184
185
// This function shouldn't exist at all -- there is such quantity of broken
186
// profiles on black point tag, that we must somehow fix chromaticity to
187
// avoid huge tint when doing Black point compensation. This function does
188
// just that. There is a special flag for using black point tag, but turned
189
// off by default because it is bogus on most profiles. The detection algorithm
190
// involves to turn BP to neutral and to use only L component.
191
cmsBool CMSEXPORT cmsDetectBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
192
16.8k
{
193
16.8k
    cmsProfileClassSignature devClass;
194
195
    // Make sure the device class is adequate
196
16.8k
    devClass = cmsGetDeviceClass(ContextID, hProfile);
197
16.8k
    if (devClass == cmsSigLinkClass ||
198
16.8k
        devClass == cmsSigAbstractClass ||
199
16.8k
        devClass == cmsSigNamedColorClass) {
200
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
201
0
            return FALSE;
202
0
    }
203
204
    // Make sure intent is adequate
205
16.8k
    if (Intent != INTENT_PERCEPTUAL &&
206
16.8k
        Intent != INTENT_RELATIVE_COLORIMETRIC &&
207
16.8k
        Intent != INTENT_SATURATION) {
208
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
209
0
            return FALSE;
210
0
    }
211
212
    // v4 + perceptual & saturation intents does have its own black point, and it is
213
    // well specified enough to use it. Black point tag is deprecated in V4.
214
16.8k
    if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) &&
215
16.8k
        (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
216
217
            // Matrix shaper share MRC & perceptual intents
218
0
            if (cmsIsMatrixShaper(ContextID, hProfile))
219
0
                return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
220
221
            // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
222
0
            BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
223
0
            BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
224
0
            BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
225
226
0
            return TRUE;
227
0
    }
228
229
230
#ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
231
232
    // v2, v4 rel/abs colorimetric
233
    if (cmsIsTag(ContextID, hProfile, cmsSigMediaBlackPointTag) &&
234
        Intent == INTENT_RELATIVE_COLORIMETRIC) {
235
236
            cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
237
            cmsCIELab Lab;
238
239
            // If black point is specified, then use it,
240
241
            BlackPtr = cmsReadTag(ContextID, hProfile, cmsSigMediaBlackPointTag);
242
            if (BlackPtr != NULL) {
243
244
                BlackXYZ = *BlackPtr;
245
                _cmsReadMediaWhitePoint(ContextID, &MediaWhite, hProfile);
246
247
                // Black point is absolute XYZ, so adapt to D50 to get PCS value
248
                cmsAdaptToIlluminant(ContextID, &UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(ContextID), &BlackXYZ);
249
250
                // Force a=b=0 to get rid of any chroma
251
                cmsXYZ2Lab(ContextID, NULL, &Lab, &UntrustedBlackPoint);
252
                Lab.a = Lab.b = 0;
253
                if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
254
                cmsLab2XYZ(ContextID, NULL, &TrustedBlackPoint, &Lab);
255
256
                if (BlackPoint != NULL)
257
                    *BlackPoint = TrustedBlackPoint;
258
259
                return TRUE;
260
            }
261
    }
262
#endif
263
264
    // That is about v2 profiles.
265
266
    // If output profile, discount ink-limiting and that's all
267
16.8k
    if (Intent == INTENT_RELATIVE_COLORIMETRIC &&
268
16.8k
        (cmsGetDeviceClass(ContextID, hProfile) == cmsSigOutputClass) &&
269
16.8k
        (cmsGetColorSpace(ContextID, hProfile)  == cmsSigCmykData))
270
890
        return BlackPointUsingPerceptualBlack(ContextID, BlackPoint, hProfile);
271
272
    // Nope, compute BP using current intent.
273
15.9k
    return BlackPointAsDarkerColorant(ContextID, hProfile, Intent, BlackPoint, dwFlags);
274
16.8k
}
275
276
277
278
// ---------------------------------------------------------------------------------------------------------
279
280
// Least Squares Fit of a Quadratic Curve to Data
281
// http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html
282
283
static
284
cmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(cmsContext ContextID, int n, cmsFloat64Number x[], cmsFloat64Number y[])
285
17
{
286
17
    double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0;
287
17
    double sum_y = 0, sum_yx = 0, sum_yx2 = 0;
288
17
    double d, a, b, c;
289
17
    int i;
290
17
    cmsMAT3 m;
291
17
    cmsVEC3 v, res;
292
293
17
    if (n < 4) return 0;
294
295
816
    for (i=0; i < n; i++) {
296
297
799
        double xn = x[i];
298
799
        double yn = y[i];
299
300
799
        sum_x  += xn;
301
799
        sum_x2 += xn*xn;
302
799
        sum_x3 += xn*xn*xn;
303
799
        sum_x4 += xn*xn*xn*xn;
304
305
799
        sum_y += yn;
306
799
        sum_yx += yn*xn;
307
799
        sum_yx2 += yn*xn*xn;
308
799
    }
309
310
17
    _cmsVEC3init(ContextID, &m.v[0], n,      sum_x,  sum_x2);
311
17
    _cmsVEC3init(ContextID, &m.v[1], sum_x,  sum_x2, sum_x3);
312
17
    _cmsVEC3init(ContextID, &m.v[2], sum_x2, sum_x3, sum_x4);
313
314
17
    _cmsVEC3init(ContextID, &v, sum_y, sum_yx, sum_yx2);
315
316
17
    if (!_cmsMAT3solve(ContextID, &res, &m, &v)) return 0;
317
318
319
17
    a = res.n[2];
320
17
    b = res.n[1];
321
17
    c = res.n[0];
322
323
17
    if (fabs(a) < 1.0E-10) {
324
325
0
        if (fabs(b) < 1.0E-10) return 0;
326
0
        return cmsmin(0, cmsmax(50, -c/b ));
327
0
    }
328
17
    else {
329
330
17
         d = b*b - 4.0 * a * c;
331
17
         if (d <= 0) {
332
0
             return 0;
333
0
         }
334
17
         else {
335
336
17
             double rt;
337
338
17
             if (fabs(a) < 1.0E-10) return 0;
339
340
17
             rt = (-b + sqrt(d)) / (2.0 * a);
341
342
17
             return cmsmax(0, cmsmin(50, rt));
343
17
         }
344
17
   }
345
346
17
}
347
348
349
350
// Calculates the black point of a destination profile.
351
// This algorithm comes from the Adobe paper disclosing its black point compensation method.
352
cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
353
8.41k
{
354
8.41k
    cmsColorSpaceSignature ColorSpace;
355
8.41k
    cmsHTRANSFORM hRoundTrip = NULL;
356
8.41k
    cmsCIELab InitialLab, destLab, Lab;
357
8.41k
    cmsFloat64Number inRamp[256], outRamp[256];
358
8.41k
    cmsFloat64Number MinL, MaxL;
359
8.41k
    cmsBool NearlyStraightMidrange = TRUE;
360
8.41k
    cmsFloat64Number yRamp[256];
361
8.41k
    cmsFloat64Number x[256], y[256];
362
8.41k
    cmsFloat64Number lo, hi;
363
8.41k
    int n, l;
364
8.41k
    cmsProfileClassSignature devClass;
365
366
    // Make sure the device class is adequate
367
8.41k
    devClass = cmsGetDeviceClass(ContextID, hProfile);
368
8.41k
    if (devClass == cmsSigLinkClass ||
369
8.41k
        devClass == cmsSigAbstractClass ||
370
8.41k
        devClass == cmsSigNamedColorClass) {
371
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
372
0
            return FALSE;
373
0
    }
374
375
    // Make sure intent is adequate
376
8.41k
    if (Intent != INTENT_PERCEPTUAL &&
377
8.41k
        Intent != INTENT_RELATIVE_COLORIMETRIC &&
378
8.41k
        Intent != INTENT_SATURATION) {
379
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
380
0
            return FALSE;
381
0
    }
382
383
384
    // v4 + perceptual & saturation intents does have its own black point, and it is
385
    // well specified enough to use it. Black point tag is deprecated in V4.
386
8.41k
    if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) &&
387
8.41k
        (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
388
389
            // Matrix shaper share MRC & perceptual intents
390
0
            if (cmsIsMatrixShaper(ContextID, hProfile))
391
0
                return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
392
393
            // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
394
0
            BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
395
0
            BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
396
0
            BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
397
0
            return TRUE;
398
0
    }
399
400
401
    // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document)
402
8.41k
    ColorSpace = cmsGetColorSpace(ContextID, hProfile);
403
8.41k
    if (!cmsIsCLUT(ContextID, hProfile, Intent, LCMS_USED_AS_OUTPUT ) ||
404
8.41k
        (ColorSpace != cmsSigGrayData &&
405
37
         ColorSpace != cmsSigRgbData  &&
406
8.38k
         ColorSpace != cmsSigCmykData)) {
407
408
        // In this case, handle as input case
409
8.38k
        return cmsDetectBlackPoint(ContextID, BlackPoint, hProfile, Intent, dwFlags);
410
8.38k
    }
411
412
    // It is one of the valid cases!, use Adobe algorithm
413
414
415
    // Set a first guess, that should work on good profiles.
416
37
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
417
418
20
        cmsCIEXYZ IniXYZ;
419
420
        // calculate initial Lab as source black point
421
20
        if (!cmsDetectBlackPoint(ContextID, &IniXYZ, hProfile, Intent, dwFlags)) {
422
0
            return FALSE;
423
0
        }
424
425
        // convert the XYZ to lab
426
20
        cmsXYZ2Lab(ContextID, NULL, &InitialLab, &IniXYZ);
427
428
20
    } else {
429
430
        // set the initial Lab to zero, that should be the black point for perceptual and saturation
431
17
        InitialLab.L = 0;
432
17
        InitialLab.a = 0;
433
17
        InitialLab.b = 0;
434
17
    }
435
436
437
    // Step 2
438
    // ======
439
440
    // Create a roundtrip. Define a Transform BT for all x in L*a*b*
441
37
    hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, Intent);
442
37
    if (hRoundTrip == NULL)  return FALSE;
443
444
    // Compute ramps
445
446
9.50k
    for (l=0; l < 256; l++) {
447
448
9.47k
        Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0;
449
9.47k
        Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a));
450
9.47k
        Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b));
451
452
9.47k
        cmsDoTransform(ContextID, hRoundTrip, &Lab, &destLab, 1);
453
454
9.47k
        inRamp[l]  = Lab.L;
455
9.47k
        outRamp[l] = destLab.L;
456
9.47k
    }
457
458
    // Make monotonic
459
9.43k
    for (l = 254; l > 0; --l) {
460
9.39k
        outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]);
461
9.39k
    }
462
463
    // Check
464
37
    if (! (outRamp[0] < outRamp[255])) {
465
466
0
        cmsDeleteTransform(ContextID, hRoundTrip);
467
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
468
0
        return FALSE;
469
0
    }
470
471
472
    // Test for mid range straight (only on relative colorimetric)
473
37
    NearlyStraightMidrange = TRUE;
474
37
    MinL = outRamp[0]; MaxL = outRamp[255];
475
37
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
476
477
5.14k
        for (l=0; l < 256; l++) {
478
479
5.12k
            if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) ||
480
5.12k
                (fabs(inRamp[l] - outRamp[l]) < 4.0 )))
481
0
                NearlyStraightMidrange = FALSE;
482
5.12k
        }
483
484
        // If the mid range is straight (as determined above) then the
485
        // DestinationBlackPoint shall be the same as initialLab.
486
        // Otherwise, the DestinationBlackPoint shall be determined
487
        // using curve fitting.
488
20
        if (NearlyStraightMidrange) {
489
490
20
            cmsLab2XYZ(ContextID, NULL, BlackPoint, &InitialLab);
491
20
            cmsDeleteTransform(ContextID, hRoundTrip);
492
20
            return TRUE;
493
20
        }
494
20
    }
495
496
497
    // curve fitting: The round-trip curve normally looks like a nearly constant section at the black point,
498
    // with a corner and a nearly straight line to the white point.
499
4.36k
    for (l=0; l < 256; l++) {
500
501
4.35k
        yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL);
502
4.35k
    }
503
504
    // find the black point using the least squares error quadratic curve fitting
505
17
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
506
0
        lo = 0.1;
507
0
        hi = 0.5;
508
0
    }
509
17
    else {
510
511
        // Perceptual and saturation
512
17
        lo = 0.03;
513
17
        hi = 0.25;
514
17
    }
515
516
    // Capture shadow points for the fitting.
517
17
    n = 0;
518
4.36k
    for (l=0; l < 256; l++) {
519
520
4.35k
        cmsFloat64Number ff = yRamp[l];
521
522
4.35k
        if (ff >= lo && ff < hi) {
523
799
            x[n] = inRamp[l];
524
799
            y[n] = yRamp[l];
525
799
            n++;
526
799
        }
527
4.35k
    }
528
529
530
    // No suitable points
531
17
    if (n < 3 ) {
532
0
        cmsDeleteTransform(ContextID, hRoundTrip);
533
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
534
0
        return FALSE;
535
0
    }
536
537
538
    // fit and get the vertex of quadratic curve
539
17
    Lab.L = RootOfLeastSquaresFitQuadraticCurve(ContextID, n, x, y);
540
541
17
    if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative
542
0
        Lab.L = 0;
543
0
    }
544
545
17
    Lab.a = InitialLab.a;
546
17
    Lab.b = InitialLab.b;
547
548
17
    cmsLab2XYZ(ContextID, NULL, BlackPoint, &Lab);
549
550
17
    cmsDeleteTransform(ContextID, hRoundTrip);
551
17
    return TRUE;
552
17
}