Coverage Report

Created: 2026-07-20 07:19

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