Coverage Report

Created: 2026-08-08 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/lcms2mt/src/cmssamp.c
Line
Count
Source
1
//---------------------------------------------------------------------------------
2
//
3
//  Little Color Management System
4
//  Copyright (c) 1998-2026 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
4.74M
#define cmsmin(a, b) (((a) < (b)) ? (a) : (b))
31
400
#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
18.4k
{
43
18.4k
    cmsHPROFILE hLab = cmsCreateLab4Profile(ContextID, NULL);
44
18.4k
    cmsHTRANSFORM xform;
45
18.4k
    cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
46
18.4k
    cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
47
18.4k
    cmsHPROFILE hProfiles[4];
48
18.4k
    cmsUInt32Number Intents[4];
49
50
18.4k
    hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
51
18.4k
    Intents[0]   = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
52
53
18.4k
    xform =  cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents,
54
18.4k
        States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
55
56
18.4k
    cmsCloseProfile(ContextID, hLab);
57
18.4k
    return xform;
58
18.4k
}
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
957k
{
69
957k
    cmsUInt16Number *Black;
70
957k
    cmsHTRANSFORM xform;
71
957k
    cmsColorSpaceSignature Space;
72
957k
    cmsUInt32Number nChannels;
73
957k
    cmsUInt32Number dwFormat;
74
957k
    cmsHPROFILE hLab;
75
957k
    cmsCIELab  Lab;
76
957k
    cmsCIEXYZ  BlackXYZ;
77
78
    // If the profile does not support input direction, assume Black point 0
79
957k
    if (!cmsIsIntentSupported(ContextID, hInput, Intent, LCMS_USED_AS_INPUT)) {
80
81
819
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
82
819
        return FALSE;
83
819
    }
84
85
    // Create a formatter which has n channels and no floating point
86
956k
    dwFormat = cmsFormatterForColorspaceOfProfile(ContextID, hInput, 2, FALSE);
87
88
    // Try to get black by using black colorant
89
956k
    Space = cmsGetColorSpace(ContextID, hInput);
90
91
    // This function returns darker colorant in 16 bits for several spaces
92
956k
    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
956k
    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
956k
    hLab = cmsCreateLab2Profile(ContextID, NULL);
105
956k
    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
956k
    xform = cmsCreateTransform(ContextID, hInput, dwFormat,
112
956k
                                hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
113
956k
    cmsCloseProfile(ContextID, hLab);
114
115
956k
    if (xform == NULL) {
116
117
        // Something went wrong. Get rid of open resources and return zero as black
118
13
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
119
13
        return FALSE;
120
13
    }
121
122
    // Convert black to Lab
123
956k
    cmsDoTransform(ContextID, xform, Black, &Lab, 1);
124
125
    // Force it to be neutral, check for inconsistencies
126
956k
    Lab.a = Lab.b = 0;
127
128
956k
    if (Lab.L > 95)
129
22
        Lab.L = 0;  // for synthetical negative profiles
130
956k
    else if (Lab.L < 0)
131
7
        Lab.L = 0;
132
956k
    else if (Lab.L > 50)
133
59
        Lab.L = 50;
134
135
    // Free the resources
136
956k
    cmsDeleteTransform(ContextID, xform);
137
138
    // Convert from Lab (which is now clipped) to XYZ.
139
956k
    cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &Lab);
140
141
956k
    if (BlackPoint != NULL)
142
956k
        *BlackPoint = BlackXYZ;
143
144
956k
    return TRUE;
145
146
0
    cmsUNUSED_PARAMETER(dwFlags);
147
0
}
148
149
// Get a black point of output CMYK profile, discounting any ink-limiting embedded
150
// in the profile. For doing that, we use perceptual intent in input direction:
151
// Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
152
static
153
cmsBool BlackPointUsingPerceptualBlack(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
154
13.8k
{
155
13.8k
    cmsHTRANSFORM hRoundTrip;
156
13.8k
    cmsCIELab LabIn, LabOut;
157
13.8k
    cmsCIEXYZ  BlackXYZ;
158
159
     // Is the intent supported by the profile?
160
13.8k
    if (!cmsIsIntentSupported(ContextID, hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
161
162
1
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
163
1
        return TRUE;
164
1
    }
165
166
13.8k
    hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, INTENT_PERCEPTUAL);
167
13.8k
    if (hRoundTrip == NULL) {
168
54
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
169
54
        return FALSE;
170
54
    }
171
172
13.7k
    LabIn.L = LabIn.a = LabIn.b = 0;
173
13.7k
    cmsDoTransform(ContextID, hRoundTrip, &LabIn, &LabOut, 1);
174
175
    // Clip Lab to reasonable limits
176
13.7k
    if (LabOut.L > 50) LabOut.L = 50;
177
13.7k
    LabOut.a = LabOut.b = 0;
178
179
13.7k
    cmsDeleteTransform(ContextID, hRoundTrip);
180
181
    // Convert it to XYZ
182
13.7k
    cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &LabOut);
183
184
13.7k
    if (BlackPoint != NULL)
185
13.7k
        *BlackPoint = BlackXYZ;
186
187
13.7k
    return TRUE;
188
13.8k
}
189
190
191
static
192
cmsBool isInkColorspace(cmsColorSpaceSignature c)
193
18.4k
{
194
18.4k
    switch(c)
195
18.4k
    {
196
18.4k
    case cmsSigCmykData:
197
18.4k
    case cmsSigCmyData:
198
18.4k
    case cmsSigMCH1Data:
199
18.4k
    case cmsSigMCH2Data:
200
18.4k
    case cmsSigMCH3Data:
201
18.4k
    case cmsSigMCH4Data:
202
18.4k
    case cmsSigMCH5Data:
203
18.4k
    case cmsSigMCH6Data:
204
18.4k
    case cmsSigMCH7Data:
205
18.4k
    case cmsSigMCH8Data:
206
18.4k
    case cmsSigMCH9Data:
207
18.4k
    case cmsSigMCHAData:
208
18.4k
    case cmsSigMCHBData:
209
18.4k
    case cmsSigMCHCData:
210
18.4k
    case cmsSigMCHDData:
211
18.4k
    case cmsSigMCHEData:
212
18.4k
    case cmsSigMCHFData:
213
18.4k
    case cmsSig1colorData:
214
18.4k
    case cmsSig2colorData:
215
18.4k
    case cmsSig3colorData:
216
18.4k
    case cmsSig4colorData:
217
18.4k
    case cmsSig5colorData:
218
18.4k
    case cmsSig6colorData:
219
18.4k
    case cmsSig7colorData:
220
18.4k
    case cmsSig8colorData:
221
18.4k
    case cmsSig9colorData:
222
18.4k
    case cmsSig10colorData:
223
18.4k
    case cmsSig11colorData:
224
18.4k
    case cmsSig12colorData:
225
18.4k
    case cmsSig13colorData:
226
18.4k
    case cmsSig14colorData:
227
18.4k
    case cmsSig15colorData:
228
18.4k
            return TRUE;
229
0
    default:
230
0
        return FALSE;
231
18.4k
    }
232
18.4k
}
233
234
// This function shouldn't exist at all -- there is such quantity of broken
235
// profiles on black point tag, that we must somehow fix chromaticity to
236
// avoid huge tint when doing Black point compensation. This function does
237
// just that. There is a special flag for using black point tag, but turned
238
// off by default because it is bogus on most profiles. The detection algorithm
239
// involves to turn BP to neutral and to use only L component.
240
cmsBool CMSEXPORT cmsDetectBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
241
970k
{
242
970k
    cmsProfileClassSignature devClass;
243
244
    // Make sure the device class is adequate
245
970k
    devClass = cmsGetDeviceClass(ContextID, hProfile);
246
970k
    if (devClass == cmsSigLinkClass ||
247
970k
        devClass == cmsSigAbstractClass ||
248
970k
        devClass == cmsSigNamedColorClass) {
249
54
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
250
54
            return FALSE;
251
54
    }
252
253
    // Make sure intent is adequate
254
970k
    if (Intent != INTENT_PERCEPTUAL &&
255
952k
        Intent != INTENT_RELATIVE_COLORIMETRIC &&
256
232
        Intent != INTENT_SATURATION) {
257
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
258
0
            return FALSE;
259
0
    }
260
261
    // v4 + perceptual & saturation intents does have its own black point, and it is
262
    // well specified enough to use it. Black point tag is deprecated in V4.
263
970k
    if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) &&
264
9.23k
        (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
265
266
            // Matrix shaper share MRC & perceptual intents
267
1.80k
            if (cmsIsMatrixShaper(ContextID, hProfile))
268
1.74k
                return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
269
270
            // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
271
56
            BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
272
56
            BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
273
56
            BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
274
275
56
            return TRUE;
276
1.80k
    }
277
278
279
#ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
280
281
    // v2, v4 rel/abs colorimetric
282
    if (cmsIsTag(ContextID, hProfile, cmsSigMediaBlackPointTag) &&
283
        Intent == INTENT_RELATIVE_COLORIMETRIC) {
284
285
            cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
286
            cmsCIELab Lab;
287
288
            // If black point is specified, then use it,
289
290
            BlackPtr = cmsReadTag(ContextID, hProfile, cmsSigMediaBlackPointTag);
291
            if (BlackPtr != NULL) {
292
293
                BlackXYZ = *BlackPtr;
294
                _cmsReadMediaWhitePoint(ContextID, &MediaWhite, hProfile);
295
296
                // Black point is absolute XYZ, so adapt to D50 to get PCS value
297
                cmsAdaptToIlluminant(ContextID, &UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(ContextID), &BlackXYZ);
298
299
                // Force a=b=0 to get rid of any chroma
300
                cmsXYZ2Lab(ContextID, NULL, &Lab, &UntrustedBlackPoint);
301
                Lab.a = Lab.b = 0;
302
                if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
303
                cmsLab2XYZ(ContextID, NULL, &TrustedBlackPoint, &Lab);
304
305
                if (BlackPoint != NULL)
306
                    *BlackPoint = TrustedBlackPoint;
307
308
                return TRUE;
309
            }
310
    }
311
#endif
312
313
    // That is about v2 profiles.
314
315
    // If output profile, discount ink-limiting and that's all
316
968k
    if (Intent == INTENT_RELATIVE_COLORIMETRIC &&
317
952k
        (cmsGetDeviceClass(ContextID, hProfile) == cmsSigOutputClass) &&
318
13.8k
        (isInkColorspace(cmsGetColorSpace(ContextID, hProfile))))
319
13.8k
        return BlackPointUsingPerceptualBlack(ContextID, BlackPoint, hProfile);
320
321
    // Nope, compute BP using current intent.
322
954k
    return BlackPointAsDarkerColorant(ContextID, hProfile, Intent, BlackPoint, dwFlags);
323
968k
}
324
325
326
327
// ---------------------------------------------------------------------------------------------------------
328
329
// Least Squares Fit of a Quadratic Curve to Data
330
// http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html
331
332
static
333
cmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(cmsContext ContextID, int n, cmsFloat64Number x[], cmsFloat64Number y[])
334
200
{
335
200
    double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0;
336
200
    double sum_y = 0, sum_yx = 0, sum_yx2 = 0;
337
200
    double d, a, b, c;
338
200
    int i;
339
200
    cmsMAT3 m;
340
200
    cmsVEC3 v, res;
341
342
200
    if (n < 4) return 0;
343
344
9.60k
    for (i=0; i < n; i++) {
345
346
9.40k
        double xn = x[i];
347
9.40k
        double yn = y[i];
348
349
9.40k
        sum_x  += xn;
350
9.40k
        sum_x2 += xn*xn;
351
9.40k
        sum_x3 += xn*xn*xn;
352
9.40k
        sum_x4 += xn*xn*xn*xn;
353
354
9.40k
        sum_y += yn;
355
9.40k
        sum_yx += yn*xn;
356
9.40k
        sum_yx2 += yn*xn*xn;
357
9.40k
    }
358
359
200
    _cmsVEC3init(ContextID, &m.v[0], n,      sum_x,  sum_x2);
360
200
    _cmsVEC3init(ContextID, &m.v[1], sum_x,  sum_x2, sum_x3);
361
200
    _cmsVEC3init(ContextID, &m.v[2], sum_x2, sum_x3, sum_x4);
362
363
200
    _cmsVEC3init(ContextID, &v, sum_y, sum_yx, sum_yx2);
364
365
200
    if (!_cmsMAT3solve(ContextID, &res, &m, &v)) return 0;
366
367
368
200
    a = res.n[2];
369
200
    b = res.n[1];
370
200
    c = res.n[0];
371
372
200
    if (fabs(a) < 1.0E-10) {
373
374
0
        if (fabs(b) < 1.0E-10) return 0;
375
0
        return cmsmax(0, cmsmin(50, -c/b ));
376
0
    }
377
200
    else {
378
379
200
         d = b*b - 4.0 * a * c;
380
200
         if (d <= 0) {
381
0
             return 0;
382
0
         }
383
200
         else {
384
385
200
             double rt;
386
387
200
             if (fabs(a) < 1.0E-10) return 0;
388
389
200
             rt = (-b + sqrt(d)) / (2.0 * a);
390
391
200
             return cmsmax(0, cmsmin(50, rt));
392
200
         }
393
200
   }
394
395
200
}
396
397
398
399
// Calculates the black point of a destination profile.
400
// This algorithm comes from the Adobe paper disclosing its black point compensation method.
401
cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
402
485k
{
403
485k
    cmsColorSpaceSignature ColorSpace;
404
485k
    cmsHTRANSFORM hRoundTrip = NULL;
405
485k
    cmsCIELab InitialLab, destLab, Lab;
406
485k
    cmsFloat64Number inRamp[256], outRamp[256];
407
485k
    cmsFloat64Number MinL, MaxL;
408
485k
    cmsBool NearlyStraightMidrange = TRUE;
409
485k
    cmsFloat64Number yRamp[256];
410
485k
    cmsFloat64Number x[256], y[256];
411
485k
    cmsFloat64Number lo, hi;
412
485k
    int n, l;
413
485k
    cmsProfileClassSignature devClass;
414
415
    // Make sure the device class is adequate
416
485k
    devClass = cmsGetDeviceClass(ContextID, hProfile);
417
485k
    if (devClass == cmsSigLinkClass ||
418
485k
        devClass == cmsSigAbstractClass ||
419
485k
        devClass == cmsSigNamedColorClass) {
420
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
421
0
            return FALSE;
422
0
    }
423
424
    // Make sure intent is adequate
425
485k
    if (Intent != INTENT_PERCEPTUAL &&
426
476k
        Intent != INTENT_RELATIVE_COLORIMETRIC &&
427
116
        Intent != INTENT_SATURATION) {
428
0
            BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
429
0
            return FALSE;
430
0
    }
431
432
433
    // v4 + perceptual & saturation intents does have its own black point, and it is
434
    // well specified enough to use it. Black point tag is deprecated in V4.
435
485k
    if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) &&
436
3.54k
        (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
437
438
            // Matrix shaper share MRC & perceptual intents
439
1.25k
            if (cmsIsMatrixShaper(ContextID, hProfile))
440
1.10k
                return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
441
442
            // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
443
148
            BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
444
148
            BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
445
148
            BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
446
148
            return TRUE;
447
1.25k
    }
448
449
450
    // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document)
451
484k
    ColorSpace = cmsGetColorSpace(ContextID, hProfile);
452
484k
    if (!cmsIsCLUT(ContextID, hProfile, Intent, LCMS_USED_AS_OUTPUT ) ||
453
4.63k
        (ColorSpace != cmsSigGrayData &&
454
4.63k
         ColorSpace != cmsSigRgbData  &&
455
480k
         !isInkColorspace(ColorSpace))) {
456
457
        // In this case, handle as input case
458
480k
        return cmsDetectBlackPoint(ContextID, BlackPoint, hProfile, Intent, dwFlags);
459
480k
    }
460
461
    // It is one of the valid cases!, use Adobe algorithm
462
463
464
    // Set a first guess, that should work on good profiles.
465
4.63k
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
466
467
4.43k
        cmsCIEXYZ IniXYZ;
468
469
        // calculate initial Lab as source black point
470
4.43k
        if (!cmsDetectBlackPoint(ContextID, &IniXYZ, hProfile, Intent, dwFlags)) {
471
0
            return FALSE;
472
0
        }
473
474
        // convert the XYZ to lab
475
4.43k
        cmsXYZ2Lab(ContextID, NULL, &InitialLab, &IniXYZ);
476
477
4.43k
    } else {
478
479
        // set the initial Lab to zero, that should be the black point for perceptual and saturation
480
200
        InitialLab.L = 0;
481
200
        InitialLab.a = 0;
482
200
        InitialLab.b = 0;
483
200
    }
484
485
486
    // Step 2
487
    // ======
488
489
    // Create a roundtrip. Define a Transform BT for all x in L*a*b*
490
4.63k
    hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, Intent);
491
4.63k
    if (hRoundTrip == NULL)  return FALSE;
492
493
    // Compute ramps
494
495
1.19M
    for (l=0; l < 256; l++) {
496
497
1.18M
        Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0;
498
1.18M
        Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a));
499
1.18M
        Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b));
500
501
1.18M
        cmsDoTransform(ContextID, hRoundTrip, &Lab, &destLab, 1);
502
503
1.18M
        inRamp[l]  = Lab.L;
504
1.18M
        outRamp[l] = destLab.L;
505
1.18M
    }
506
507
    // Make monotonic
508
1.18M
    for (l = 254; l > 0; --l) {
509
1.17M
        outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]);
510
1.17M
    }
511
512
    // Check
513
4.63k
    if (! (outRamp[0] < outRamp[255])) {
514
515
0
        cmsDeleteTransform(ContextID, hRoundTrip);
516
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
517
0
        return FALSE;
518
0
    }
519
520
521
    // Test for mid range straight (only on relative colorimetric)
522
4.63k
    NearlyStraightMidrange = TRUE;
523
4.63k
    MinL = outRamp[0]; MaxL = outRamp[255];
524
4.63k
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
525
526
1.13M
        for (l=0; l < 256; l++) {
527
528
1.13M
            if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) ||
529
758k
                (fabs(inRamp[l] - outRamp[l]) < 4.0 )))
530
0
                NearlyStraightMidrange = FALSE;
531
1.13M
        }
532
533
        // If the mid range is straight (as determined above) then the
534
        // DestinationBlackPoint shall be the same as initialLab.
535
        // Otherwise, the DestinationBlackPoint shall be determined
536
        // using curve fitting.
537
4.43k
        if (NearlyStraightMidrange) {
538
539
4.43k
            cmsLab2XYZ(ContextID, NULL, BlackPoint, &InitialLab);
540
4.43k
            cmsDeleteTransform(ContextID, hRoundTrip);
541
4.43k
            return TRUE;
542
4.43k
        }
543
4.43k
    }
544
545
546
    // curve fitting: The round-trip curve normally looks like a nearly constant section at the black point,
547
    // with a corner and a nearly straight line to the white point.
548
51.4k
    for (l=0; l < 256; l++) {
549
550
51.2k
        yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL);
551
51.2k
    }
552
553
    // find the black point using the least squares error quadratic curve fitting
554
200
    if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
555
0
        lo = 0.1;
556
0
        hi = 0.5;
557
0
    }
558
200
    else {
559
560
        // Perceptual and saturation
561
200
        lo = 0.03;
562
200
        hi = 0.25;
563
200
    }
564
565
    // Capture shadow points for the fitting.
566
200
    n = 0;
567
51.4k
    for (l=0; l < 256; l++) {
568
569
51.2k
        cmsFloat64Number ff = yRamp[l];
570
571
51.2k
        if (ff >= lo && ff < hi) {
572
9.40k
            x[n] = inRamp[l];
573
9.40k
            y[n] = yRamp[l];
574
9.40k
            n++;
575
9.40k
        }
576
51.2k
    }
577
578
579
    // No suitable points
580
200
    if (n < 3 ) {
581
0
        cmsDeleteTransform(ContextID, hRoundTrip);
582
0
        BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
583
0
        return FALSE;
584
0
    }
585
586
587
    // fit and get the vertex of quadratic curve
588
200
    Lab.L = RootOfLeastSquaresFitQuadraticCurve(ContextID, n, x, y);
589
590
200
    if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative
591
0
        Lab.L = 0;
592
0
    }
593
594
200
    Lab.a = InitialLab.a;
595
200
    Lab.b = InitialLab.b;
596
597
200
    cmsLab2XYZ(ContextID, NULL, BlackPoint, &Lab);
598
599
200
    cmsDeleteTransform(ContextID, hRoundTrip);
600
200
    return TRUE;
601
200
}