Coverage Report

Created: 2025-08-26 06:14

/src/lcms/src/cmsopt.c
Line
Count
Source (jump to first uncovered line)
1
//---------------------------------------------------------------------------------
2
//
3
//  Little Color Management System
4
//  Copyright (c) 1998-2024 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
//----------------------------------------------------------------------------------
31
32
// Optimization for 8 bits, Shaper-CLUT (3 inputs only)
33
typedef struct {
34
35
    cmsContext ContextID;
36
37
    const cmsInterpParams* p;   // Tetrahedrical interpolation parameters. This is a not-owned pointer.
38
39
    cmsUInt16Number rx[256], ry[256], rz[256];
40
    cmsUInt32Number X0[256], Y0[256], Z0[256];  // Precomputed nodes and offsets for 8-bit input data
41
42
43
} Prelin8Data;
44
45
46
// Generic optimization for 16 bits Shaper-CLUT-Shaper (any inputs)
47
typedef struct {
48
49
    cmsContext ContextID;
50
51
    // Number of channels
52
    cmsUInt32Number nInputs;
53
    cmsUInt32Number nOutputs;
54
55
    _cmsInterpFn16 EvalCurveIn16[MAX_INPUT_DIMENSIONS];       // The maximum number of input channels is known in advance
56
    cmsInterpParams*  ParamsCurveIn16[MAX_INPUT_DIMENSIONS];
57
58
    _cmsInterpFn16 EvalCLUT;            // The evaluator for 3D grid
59
    const cmsInterpParams* CLUTparams;  // (not-owned pointer)
60
61
62
    _cmsInterpFn16* EvalCurveOut16;       // Points to an array of curve evaluators in 16 bits (not-owned pointer)
63
    cmsInterpParams**  ParamsCurveOut16;  // Points to an array of references to interpolation params (not-owned pointer)
64
65
66
} Prelin16Data;
67
68
69
// Optimization for matrix-shaper in 8 bits. Numbers are operated in n.14 signed, tables are stored in 1.14 fixed
70
71
typedef cmsInt32Number cmsS1Fixed14Number;   // Note that this may hold more than 16 bits!
72
73
19.6k
#define DOUBLE_TO_1FIXED14(x) ((cmsS1Fixed14Number) floor((x) * 16384.0 + 0.5))
74
75
typedef struct {
76
77
    cmsContext ContextID;
78
79
    cmsS1Fixed14Number Shaper1R[256];  // from 0..255 to 1.14  (0.0...1.0)
80
    cmsS1Fixed14Number Shaper1G[256];
81
    cmsS1Fixed14Number Shaper1B[256];
82
83
    cmsS1Fixed14Number Mat[3][3];     // n.14 to n.14 (needs a saturation after that)
84
    cmsS1Fixed14Number Off[3];
85
86
    cmsUInt16Number Shaper2R[16385];    // 1.14 to 0..255
87
    cmsUInt16Number Shaper2G[16385];
88
    cmsUInt16Number Shaper2B[16385];
89
90
} MatShaper8Data;
91
92
// Curves, optimization is shared between 8 and 16 bits
93
typedef struct {
94
95
    cmsContext ContextID;
96
97
    cmsUInt32Number nCurves;      // Number of curves
98
    cmsUInt32Number nElements;    // Elements in curves
99
    cmsUInt16Number** Curves;     // Points to a dynamically  allocated array
100
101
} Curves16Data;
102
103
104
// Simple optimizations ----------------------------------------------------------------------------------------------------------
105
106
107
// Remove an element in linked chain
108
static
109
void _RemoveElement(cmsStage** head)
110
3.10k
{
111
3.10k
    cmsStage* mpe = *head;
112
3.10k
    cmsStage* next = mpe ->Next;
113
3.10k
    *head = next;
114
3.10k
    cmsStageFree(mpe);
115
3.10k
}
116
117
// Remove all identities in chain. Note that pt actually is a double pointer to the element that holds the pointer.
118
static
119
cmsBool _Remove1Op(cmsPipeline* Lut, cmsStageSignature UnaryOp)
120
3.79k
{
121
3.79k
    cmsStage** pt = &Lut ->Elements;
122
3.79k
    cmsBool AnyOpt = FALSE;
123
124
21.7k
    while (*pt != NULL) {
125
126
18.0k
        if ((*pt) ->Implements == UnaryOp) {
127
1.01k
            _RemoveElement(pt);
128
1.01k
            AnyOpt = TRUE;
129
1.01k
        }
130
16.9k
        else
131
16.9k
            pt = &((*pt) -> Next);
132
18.0k
    }
133
134
3.79k
    return AnyOpt;
135
3.79k
}
136
137
// Same, but only if two adjacent elements are found
138
static
139
cmsBool _Remove2Op(cmsPipeline* Lut, cmsStageSignature Op1, cmsStageSignature Op2)
140
22.7k
{
141
22.7k
    cmsStage** pt1;
142
22.7k
    cmsStage** pt2;
143
22.7k
    cmsBool AnyOpt = FALSE;
144
145
22.7k
    pt1 = &Lut ->Elements;
146
22.7k
    if (*pt1 == NULL) return AnyOpt;
147
148
95.7k
    while (*pt1 != NULL) {
149
150
94.7k
        pt2 = &((*pt1) -> Next);
151
94.7k
        if (*pt2 == NULL) return AnyOpt;
152
153
73.0k
        if ((*pt1) ->Implements == Op1 && (*pt2) ->Implements == Op2) {
154
1.02k
            _RemoveElement(pt2);
155
1.02k
            _RemoveElement(pt1);
156
1.02k
            AnyOpt = TRUE;
157
1.02k
        }
158
72.0k
        else
159
72.0k
            pt1 = &((*pt1) -> Next);
160
73.0k
    }
161
162
1.01k
    return AnyOpt;
163
22.7k
}
164
165
166
static
167
cmsBool CloseEnoughFloat(cmsFloat64Number a, cmsFloat64Number b)
168
27
{
169
27
       return fabs(b - a) < 0.00001f;
170
27
}
171
172
static
173
cmsBool  isFloatMatrixIdentity(const cmsMAT3* a)
174
21
{
175
21
       cmsMAT3 Identity;
176
21
       int i, j;
177
178
21
       _cmsMAT3identity(&Identity);
179
180
22
       for (i = 0; i < 3; i++)
181
28
              for (j = 0; j < 3; j++)
182
27
                     if (!CloseEnoughFloat(a->v[i].n[j], Identity.v[i].n[j])) return FALSE;
183
184
0
       return TRUE;
185
21
}
186
187
// if two adjacent matrices are found, multiply them. 
188
static
189
cmsBool _MultiplyMatrix(cmsPipeline* Lut)
190
3.79k
{
191
3.79k
       cmsStage** pt1;
192
3.79k
       cmsStage** pt2;
193
3.79k
       cmsStage*  chain;
194
3.79k
       cmsBool AnyOpt = FALSE;
195
196
3.79k
       pt1 = &Lut->Elements;
197
3.79k
       if (*pt1 == NULL) return AnyOpt;
198
199
13.5k
       while (*pt1 != NULL) {
200
201
13.5k
              pt2 = &((*pt1)->Next);
202
13.5k
              if (*pt2 == NULL) return AnyOpt;
203
204
10.2k
              if ((*pt1)->Implements == cmsSigMatrixElemType && (*pt2)->Implements == cmsSigMatrixElemType) {
205
206
                     // Get both matrices
207
546
                     _cmsStageMatrixData* m1 = (_cmsStageMatrixData*) cmsStageData(*pt1);
208
546
                     _cmsStageMatrixData* m2 = (_cmsStageMatrixData*) cmsStageData(*pt2);
209
546
                     cmsMAT3 res;
210
                     
211
                     // Input offset and output offset should be zero to use this optimization
212
546
                     if (m1->Offset != NULL || m2 ->Offset != NULL || 
213
546
                            cmsStageInputChannels(*pt1) != 3 || cmsStageOutputChannels(*pt1) != 3 ||                            
214
546
                            cmsStageInputChannels(*pt2) != 3 || cmsStageOutputChannels(*pt2) != 3)
215
525
                            return FALSE;
216
217
                     // Multiply both matrices to get the result
218
21
                     _cmsMAT3per(&res, (cmsMAT3*)m2->Double, (cmsMAT3*)m1->Double);
219
220
                     // Get the next in chain after the matrices
221
21
                     chain = (*pt2)->Next;
222
223
                     // Remove both matrices
224
21
                     _RemoveElement(pt2);
225
21
                     _RemoveElement(pt1);
226
227
                     // Now what if the result is a plain identity?                     
228
21
                     if (!isFloatMatrixIdentity(&res)) {
229
230
                            // We can not get rid of full matrix                            
231
21
                            cmsStage* Multmat = cmsStageAllocMatrix(Lut->ContextID, 3, 3, (const cmsFloat64Number*) &res, NULL);
232
21
                            if (Multmat == NULL) return FALSE;  // Should never happen
233
234
                            // Recover the chain
235
21
                            Multmat->Next = chain;
236
21
                            *pt1 = Multmat;
237
21
                     }
238
239
21
                     AnyOpt = TRUE;
240
21
              }
241
9.74k
              else
242
9.74k
                     pt1 = &((*pt1)->Next);
243
10.2k
       }
244
245
0
       return AnyOpt;
246
3.78k
}
247
248
249
// Preoptimize just gets rif of no-ops coming paired. Conversion from v2 to v4 followed
250
// by a v4 to v2 and vice-versa. The elements are then discarded.
251
static
252
cmsBool PreOptimize(cmsPipeline* Lut)
253
2.74k
{
254
2.74k
    cmsBool AnyOpt = FALSE, Opt;
255
256
3.79k
    do {
257
258
3.79k
        Opt = FALSE;
259
260
        // Remove all identities
261
3.79k
        Opt |= _Remove1Op(Lut, cmsSigIdentityElemType);
262
263
        // Remove XYZ2Lab followed by Lab2XYZ
264
3.79k
        Opt |= _Remove2Op(Lut, cmsSigXYZ2LabElemType, cmsSigLab2XYZElemType);
265
266
        // Remove Lab2XYZ followed by XYZ2Lab
267
3.79k
        Opt |= _Remove2Op(Lut, cmsSigLab2XYZElemType, cmsSigXYZ2LabElemType);
268
269
        // Remove V4 to V2 followed by V2 to V4
270
3.79k
        Opt |= _Remove2Op(Lut, cmsSigLabV4toV2, cmsSigLabV2toV4);
271
272
        // Remove V2 to V4 followed by V4 to V2
273
3.79k
        Opt |= _Remove2Op(Lut, cmsSigLabV2toV4, cmsSigLabV4toV2);
274
275
        // Remove float pcs Lab conversions
276
3.79k
        Opt |= _Remove2Op(Lut, cmsSigLab2FloatPCS, cmsSigFloatPCS2Lab);
277
278
        // Remove float pcs Lab conversions
279
3.79k
        Opt |= _Remove2Op(Lut, cmsSigXYZ2FloatPCS, cmsSigFloatPCS2XYZ);
280
281
        // Simplify matrix. 
282
3.79k
        Opt |= _MultiplyMatrix(Lut);
283
284
3.79k
        if (Opt) AnyOpt = TRUE;
285
286
3.79k
    } while (Opt);
287
288
2.74k
    return AnyOpt;
289
2.74k
}
290
291
static
292
void Eval16nop1D(CMSREGISTER const cmsUInt16Number Input[],
293
                 CMSREGISTER cmsUInt16Number Output[],
294
                 CMSREGISTER const struct _cms_interp_struc* p)
295
156
{
296
156
    Output[0] = Input[0];
297
298
156
    cmsUNUSED_PARAMETER(p);
299
156
}
300
301
static
302
void PrelinEval16(CMSREGISTER const cmsUInt16Number Input[],
303
                  CMSREGISTER cmsUInt16Number Output[],
304
                  CMSREGISTER const void* D)
305
59
{
306
59
    Prelin16Data* p16 = (Prelin16Data*) D;
307
59
    cmsUInt16Number  StageABC[MAX_INPUT_DIMENSIONS];
308
59
    cmsUInt16Number  StageDEF[cmsMAXCHANNELS];
309
59
    cmsUInt32Number i;
310
311
215
    for (i=0; i < p16 ->nInputs; i++) {
312
313
156
        p16 ->EvalCurveIn16[i](&Input[i], &StageABC[i], p16 ->ParamsCurveIn16[i]);
314
156
    }
315
316
59
    p16 ->EvalCLUT(StageABC, StageDEF, p16 ->CLUTparams);
317
318
236
    for (i=0; i < p16 ->nOutputs; i++) {
319
320
177
        p16 ->EvalCurveOut16[i](&StageDEF[i], &Output[i], p16 ->ParamsCurveOut16[i]);
321
177
    }
322
59
}
323
324
325
static
326
void PrelinOpt16free(cmsContext ContextID, void* ptr)
327
309
{
328
309
    Prelin16Data* p16 = (Prelin16Data*) ptr;
329
330
309
    _cmsFree(ContextID, p16 ->EvalCurveOut16);
331
309
    _cmsFree(ContextID, p16 ->ParamsCurveOut16);
332
333
309
    _cmsFree(ContextID, p16);
334
309
}
335
336
static
337
void* Prelin16dup(cmsContext ContextID, const void* ptr)
338
116
{
339
116
    Prelin16Data* p16 = (Prelin16Data*) ptr;
340
116
    Prelin16Data* Duped = (Prelin16Data*) _cmsDupMem(ContextID, p16, sizeof(Prelin16Data));
341
342
116
    if (Duped == NULL) return NULL;
343
344
116
    Duped->EvalCurveOut16 = (_cmsInterpFn16*) _cmsDupMem(ContextID, p16->EvalCurveOut16, p16->nOutputs * sizeof(_cmsInterpFn16));
345
116
    Duped->ParamsCurveOut16 = (cmsInterpParams**)_cmsDupMem(ContextID, p16->ParamsCurveOut16, p16->nOutputs * sizeof(cmsInterpParams*));
346
347
116
    return Duped;
348
116
}
349
350
351
static
352
Prelin16Data* PrelinOpt16alloc(cmsContext ContextID,
353
                               const cmsInterpParams* ColorMap,
354
                               cmsUInt32Number nInputs, cmsToneCurve** In,
355
                               cmsUInt32Number nOutputs, cmsToneCurve** Out )
356
193
{
357
193
    cmsUInt32Number i;
358
193
    Prelin16Data* p16 = (Prelin16Data*)_cmsMallocZero(ContextID, sizeof(Prelin16Data));
359
193
    if (p16 == NULL) return NULL;
360
361
193
    p16 ->nInputs = nInputs;
362
193
    p16 ->nOutputs = nOutputs;
363
364
365
1.12k
    for (i=0; i < nInputs; i++) {
366
367
935
        if (In == NULL) {
368
935
            p16 -> ParamsCurveIn16[i] = NULL;
369
935
            p16 -> EvalCurveIn16[i] = Eval16nop1D;
370
371
935
        }
372
0
        else {
373
0
            p16 -> ParamsCurveIn16[i] = In[i] ->InterpParams;
374
0
            p16 -> EvalCurveIn16[i] = p16 ->ParamsCurveIn16[i]->Interpolation.Lerp16;
375
0
        }
376
935
    }
377
378
193
    p16 ->CLUTparams = ColorMap;
379
193
    p16 ->EvalCLUT   = ColorMap ->Interpolation.Lerp16;
380
381
382
193
    p16 -> EvalCurveOut16 = (_cmsInterpFn16*) _cmsCalloc(ContextID, nOutputs, sizeof(_cmsInterpFn16));
383
193
    if (p16->EvalCurveOut16 == NULL)
384
0
    {
385
0
        _cmsFree(ContextID, p16);
386
0
        return NULL;
387
0
    }
388
389
193
    p16 -> ParamsCurveOut16 = (cmsInterpParams**) _cmsCalloc(ContextID, nOutputs, sizeof(cmsInterpParams* ));
390
193
    if (p16->ParamsCurveOut16 == NULL)
391
0
    {
392
393
0
        _cmsFree(ContextID, p16->EvalCurveOut16);
394
0
        _cmsFree(ContextID, p16);
395
0
        return NULL;
396
0
    }
397
398
772
    for (i=0; i < nOutputs; i++) {
399
400
579
        if (Out == NULL) {
401
0
            p16 ->ParamsCurveOut16[i] = NULL;
402
0
            p16 -> EvalCurveOut16[i] = Eval16nop1D;
403
0
        }
404
579
        else {
405
406
579
            p16 ->ParamsCurveOut16[i] = Out[i] ->InterpParams;
407
579
            p16 -> EvalCurveOut16[i] = p16 ->ParamsCurveOut16[i]->Interpolation.Lerp16;
408
579
        }
409
579
    }
410
411
193
    return p16;
412
193
}
413
414
415
416
// Resampling ---------------------------------------------------------------------------------
417
418
0
#define PRELINEARIZATION_POINTS 4096
419
420
// Sampler implemented by another LUT. This is a clean way to precalculate the devicelink 3D CLUT for
421
// almost any transform. We use floating point precision and then convert from floating point to 16 bits.
422
static
423
cmsInt32Number XFormSampler16(CMSREGISTER const cmsUInt16Number In[], 
424
                              CMSREGISTER cmsUInt16Number Out[], 
425
                              CMSREGISTER void* Cargo)
426
177M
{
427
177M
    cmsPipeline* Lut = (cmsPipeline*) Cargo;
428
177M
    cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
429
177M
    cmsUInt32Number i;
430
431
177M
    _cmsAssert(Lut -> InputChannels < cmsMAXCHANNELS);
432
177M
    _cmsAssert(Lut -> OutputChannels < cmsMAXCHANNELS);
433
434
    // From 16 bit to floating point
435
763M
    for (i=0; i < Lut ->InputChannels; i++)
436
585M
        InFloat[i] = (cmsFloat32Number) (In[i] / 65535.0);
437
438
    // Evaluate in floating point
439
177M
    cmsPipelineEvalFloat(InFloat, OutFloat, Lut);
440
441
    // Back to 16 bits representation
442
711M
    for (i=0; i < Lut ->OutputChannels; i++)
443
533M
        Out[i] = _cmsQuickSaturateWord(OutFloat[i] * 65535.0);
444
445
    // Always succeed
446
177M
    return TRUE;
447
177M
}
448
449
// Try to see if the curves of a given MPE are linear
450
static
451
cmsBool AllCurvesAreLinear(cmsStage* mpe)
452
193
{
453
193
    cmsToneCurve** Curves;
454
193
    cmsUInt32Number i, n;
455
456
193
    Curves = _cmsStageGetPtrToCurveSet(mpe);
457
193
    if (Curves == NULL) return FALSE;
458
459
193
    n = cmsStageOutputChannels(mpe);
460
461
193
    for (i=0; i < n; i++) {
462
193
        if (!cmsIsToneCurveLinear(Curves[i])) return FALSE;
463
193
    }
464
465
0
    return TRUE;
466
193
}
467
468
// This function replaces a specific node placed in "At" by the "Value" numbers. Its purpose
469
// is to fix scum dot on broken profiles/transforms. Works on 1, 3 and 4 channels
470
static
471
cmsBool  PatchLUT(cmsStage* CLUT, cmsUInt16Number At[], cmsUInt16Number Value[],
472
                  cmsUInt32Number nChannelsOut, cmsUInt32Number nChannelsIn)
473
75
{
474
75
    _cmsStageCLutData* Grid = (_cmsStageCLutData*) CLUT ->Data;
475
75
    cmsInterpParams* p16  = Grid ->Params;
476
75
    cmsFloat64Number px, py, pz, pw;
477
75
    int        x0, y0, z0, w0;
478
75
    int        i, index;
479
480
75
    if (CLUT -> Type != cmsSigCLutElemType) {
481
0
        cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) Attempt to PatchLUT on non-lut stage");
482
0
        return FALSE;
483
0
    }
484
485
75
    if (nChannelsIn == 4) {
486
487
27
        px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
488
27
        py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
489
27
        pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
490
27
        pw = ((cmsFloat64Number) At[3] * (p16->Domain[3])) / 65535.0;
491
492
27
        x0 = (int) floor(px);
493
27
        y0 = (int) floor(py);
494
27
        z0 = (int) floor(pz);
495
27
        w0 = (int) floor(pw);
496
497
27
        if (((px - x0) != 0) ||
498
27
            ((py - y0) != 0) ||
499
27
            ((pz - z0) != 0) ||
500
27
            ((pw - w0) != 0)) return FALSE; // Not on exact node
501
502
27
        index = (int) p16 -> opta[3] * x0 +
503
27
                (int) p16 -> opta[2] * y0 +
504
27
                (int) p16 -> opta[1] * z0 +
505
27
                (int) p16 -> opta[0] * w0;
506
27
    }
507
48
    else
508
48
        if (nChannelsIn == 3) {
509
510
42
            px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
511
42
            py = ((cmsFloat64Number) At[1] * (p16->Domain[1])) / 65535.0;
512
42
            pz = ((cmsFloat64Number) At[2] * (p16->Domain[2])) / 65535.0;
513
           
514
42
            x0 = (int) floor(px);
515
42
            y0 = (int) floor(py);
516
42
            z0 = (int) floor(pz);
517
           
518
42
            if (((px - x0) != 0) ||
519
42
                ((py - y0) != 0) ||
520
42
                ((pz - z0) != 0)) return FALSE;  // Not on exact node
521
522
29
            index = (int) p16 -> opta[2] * x0 +
523
29
                    (int) p16 -> opta[1] * y0 +
524
29
                    (int) p16 -> opta[0] * z0;
525
29
        }
526
6
        else
527
6
            if (nChannelsIn == 1) {
528
529
6
                px = ((cmsFloat64Number) At[0] * (p16->Domain[0])) / 65535.0;
530
                
531
6
                x0 = (int) floor(px);
532
                
533
6
                if (((px - x0) != 0)) return FALSE; // Not on exact node
534
535
6
                index = (int) p16 -> opta[0] * x0;
536
6
            }
537
0
            else {
538
0
                cmsSignalError(CLUT->ContextID, cmsERROR_INTERNAL, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
539
0
                return FALSE;
540
0
            }
541
542
248
    for (i = 0; i < (int) nChannelsOut; i++)
543
186
        Grid->Tab.T[index + i] = Value[i];
544
545
62
    return TRUE;
546
75
}
547
548
// Auxiliary, to see if two values are equal or very different
549
static
550
cmsBool WhitesAreEqual(cmsUInt32Number n, cmsUInt16Number White1[], cmsUInt16Number White2[] )
551
355
{
552
355
    cmsUInt32Number i;
553
554
600
    for (i=0; i < n; i++) {
555
556
537
        if (abs(White1[i] - White2[i]) > 0xf000) return TRUE;  // Values are so extremely different that the fixup should be avoided
557
320
        if (White1[i] != White2[i]) return FALSE;
558
320
    }
559
63
    return TRUE;
560
355
}
561
562
563
// Locate the node for the white point and fix it to pure white in order to avoid scum dot.
564
static
565
cmsBool FixWhiteMisalignment(cmsPipeline* Lut, cmsColorSpaceSignature EntryColorSpace, cmsColorSpaceSignature ExitColorSpace)
566
681
{
567
681
    cmsUInt16Number *WhitePointIn, *WhitePointOut;
568
681
    cmsUInt16Number  WhiteIn[cmsMAXCHANNELS], WhiteOut[cmsMAXCHANNELS], ObtainedOut[cmsMAXCHANNELS];
569
681
    cmsUInt32Number i, nOuts, nIns;
570
681
    cmsStage *PreLin = NULL, *CLUT = NULL, *PostLin = NULL;
571
572
681
    if (!_cmsEndPointsBySpace(EntryColorSpace,
573
681
        &WhitePointIn, NULL, &nIns)) return FALSE;
574
575
355
    if (!_cmsEndPointsBySpace(ExitColorSpace,
576
355
        &WhitePointOut, NULL, &nOuts)) return FALSE;
577
578
    // It needs to be fixed?
579
355
    if (Lut ->InputChannels != nIns) return FALSE;
580
355
    if (Lut ->OutputChannels != nOuts) return FALSE;
581
582
355
    cmsPipelineEval16(WhitePointIn, ObtainedOut, Lut);
583
584
355
    if (WhitesAreEqual(nOuts, WhitePointOut, ObtainedOut)) return TRUE; // whites already match
585
586
    // Check if the LUT comes as Prelin, CLUT or Postlin. We allow all combinations
587
75
    if (!cmsPipelineCheckAndRetreiveStages(Lut, 3, cmsSigCurveSetElemType, cmsSigCLutElemType, cmsSigCurveSetElemType, &PreLin, &CLUT, &PostLin))
588
75
        if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCurveSetElemType, cmsSigCLutElemType, &PreLin, &CLUT))
589
75
            if (!cmsPipelineCheckAndRetreiveStages(Lut, 2, cmsSigCLutElemType, cmsSigCurveSetElemType, &CLUT, &PostLin))
590
56
                if (!cmsPipelineCheckAndRetreiveStages(Lut, 1, cmsSigCLutElemType, &CLUT))
591
0
                    return FALSE;
592
593
    // We need to interpolate white points of both, pre and post curves
594
75
    if (PreLin) {
595
596
0
        cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PreLin);
597
598
0
        for (i=0; i < nIns; i++) {
599
0
            WhiteIn[i] = cmsEvalToneCurve16(Curves[i], WhitePointIn[i]);
600
0
        }
601
0
    }
602
75
    else {
603
315
        for (i=0; i < nIns; i++)
604
240
            WhiteIn[i] = WhitePointIn[i];
605
75
    }
606
607
    // If any post-linearization, we need to find how is represented white before the curve, do
608
    // a reverse interpolation in this case.
609
75
    if (PostLin) {
610
611
19
        cmsToneCurve** Curves = _cmsStageGetPtrToCurveSet(PostLin);
612
613
76
        for (i=0; i < nOuts; i++) {
614
615
57
            cmsToneCurve* InversePostLin = cmsReverseToneCurve(Curves[i]);
616
57
            if (InversePostLin == NULL) {
617
0
                WhiteOut[i] = WhitePointOut[i];    
618
619
57
            } else {
620
621
57
                WhiteOut[i] = cmsEvalToneCurve16(InversePostLin, WhitePointOut[i]);
622
57
                cmsFreeToneCurve(InversePostLin);
623
57
            }
624
57
        }
625
19
    }
626
56
    else {
627
224
        for (i=0; i < nOuts; i++)
628
168
            WhiteOut[i] = WhitePointOut[i];
629
56
    }
630
631
    // Ok, proceed with patching. May fail and we don't care if it fails
632
75
    PatchLUT(CLUT, WhiteIn, WhiteOut, nOuts, nIns);
633
634
75
    return TRUE;
635
75
}
636
637
// -----------------------------------------------------------------------------------------------------------------------------------------------
638
// This function creates simple LUT from complex ones. The generated LUT has an optional set of
639
// prelinearization curves, a CLUT of nGridPoints and optional postlinearization tables.
640
// These curves have to exist in the original LUT in order to be used in the simplified output.
641
// Caller may also use the flags to allow this feature.
642
// LUTS with all curves will be simplified to a single curve. Parametric curves are lost.
643
// This function should be used on 16-bits LUTS only, as floating point losses precision when simplified
644
// -----------------------------------------------------------------------------------------------------------------------------------------------
645
646
static
647
cmsBool OptimizeByResampling(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
648
1.33k
{
649
1.33k
    cmsPipeline* Src = NULL;
650
1.33k
    cmsPipeline* Dest = NULL;
651
1.33k
    cmsStage* CLUT;
652
1.33k
    cmsStage *KeepPreLin = NULL, *KeepPostLin = NULL;
653
1.33k
    cmsUInt32Number nGridPoints;
654
1.33k
    cmsColorSpaceSignature ColorSpace, OutputColorSpace;
655
1.33k
    cmsStage *NewPreLin = NULL;
656
1.33k
    cmsStage *NewPostLin = NULL;
657
1.33k
    _cmsStageCLutData* DataCLUT;
658
1.33k
    cmsToneCurve** DataSetIn;
659
1.33k
    cmsToneCurve** DataSetOut;
660
1.33k
    Prelin16Data* p16;
661
662
    // This is a lossy optimization! does not apply in floating-point cases
663
1.33k
    if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
664
665
1.33k
    ColorSpace       = _cmsICCcolorSpace((int) T_COLORSPACE(*InputFormat));
666
1.33k
    OutputColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*OutputFormat));
667
668
    // Color space must be specified
669
1.33k
    if (ColorSpace == (cmsColorSpaceSignature)0 ||
670
1.33k
        OutputColorSpace == (cmsColorSpaceSignature)0) return FALSE;
671
       
672
    // For empty LUTs, 2 points are enough
673
922
    if (cmsPipelineStageCount(*Lut) == 0)
674
0
        nGridPoints = 2;
675
922
    else
676
922
    {
677
922
        nGridPoints = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
678
679
        // Lab16 as input cannot be optimized by a CLUT due to centering issues, thanks to Mike Chaney for discovering this.
680
922
        if (!(*dwFlags & cmsFLAGS_FORCE_CLUT) && (ColorSpace == cmsSigLabData) && (T_BYTES(*InputFormat) == 2)) return FALSE;
681
922
    }
682
683
907
    Src = *Lut;
684
685
    // Allocate an empty LUT
686
907
    Dest =  cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
687
907
    if (!Dest) return FALSE;
688
689
    // Prelinearization tables are kept unless indicated by flags
690
907
    if (*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION) {
691
692
        // Get a pointer to the prelinearization element
693
0
        cmsStage* PreLin = cmsPipelineGetPtrToFirstStage(Src);
694
695
        // Check if suitable
696
0
        if (PreLin && PreLin ->Type == cmsSigCurveSetElemType) {
697
698
            // Maybe this is a linear tram, so we can avoid the whole stuff
699
0
            if (!AllCurvesAreLinear(PreLin)) {
700
701
                // All seems ok, proceed.
702
0
                NewPreLin = cmsStageDup(PreLin);
703
0
                if(!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, NewPreLin))
704
0
                    goto Error;
705
706
                // Remove prelinearization. Since we have duplicated the curve
707
                // in destination LUT, the sampling should be applied after this stage.
708
0
                cmsPipelineUnlinkStage(Src, cmsAT_BEGIN, &KeepPreLin);
709
0
            }
710
0
        }
711
0
    }
712
713
    // Allocate the CLUT
714
907
    CLUT = cmsStageAllocCLut16bit(Src ->ContextID, nGridPoints, Src ->InputChannels, Src->OutputChannels, NULL);
715
907
    if (CLUT == NULL) goto Error;
716
717
    // Add the CLUT to the destination LUT
718
770
    if (!cmsPipelineInsertStage(Dest, cmsAT_END, CLUT)) {
719
0
        goto Error;
720
0
    }
721
722
    // Postlinearization tables are kept unless indicated by flags
723
770
    if (*dwFlags & cmsFLAGS_CLUT_POST_LINEARIZATION) {
724
725
        // Get a pointer to the postlinearization if present
726
231
        cmsStage* PostLin = cmsPipelineGetPtrToLastStage(Src);
727
728
        // Check if suitable
729
231
        if (PostLin && cmsStageType(PostLin) == cmsSigCurveSetElemType) {
730
731
            // Maybe this is a linear tram, so we can avoid the whole stuff
732
193
            if (!AllCurvesAreLinear(PostLin)) {
733
734
                // All seems ok, proceed.
735
193
                NewPostLin = cmsStageDup(PostLin);
736
193
                if (!cmsPipelineInsertStage(Dest, cmsAT_END, NewPostLin))
737
0
                    goto Error;
738
739
                // In destination LUT, the sampling should be applied after this stage.
740
193
                cmsPipelineUnlinkStage(Src, cmsAT_END, &KeepPostLin);
741
193
            }
742
193
        }
743
231
    }
744
745
    // Now its time to do the sampling. We have to ignore pre/post linearization
746
    // The source LUT without pre/post curves is passed as parameter.
747
770
    if (!cmsStageSampleCLut16bit(CLUT, XFormSampler16, (void*) Src, 0)) {
748
137
Error:
749
        // Ops, something went wrong, Restore stages
750
137
        if (KeepPreLin != NULL) {
751
0
            if (!cmsPipelineInsertStage(Src, cmsAT_BEGIN, KeepPreLin)) {
752
0
                _cmsAssert(0); // This never happens
753
0
            }
754
0
        }
755
137
        if (KeepPostLin != NULL) {
756
0
            if (!cmsPipelineInsertStage(Src, cmsAT_END,   KeepPostLin)) {
757
0
                _cmsAssert(0); // This never happens
758
0
            }
759
0
        }
760
137
        cmsPipelineFree(Dest);
761
137
        return FALSE;
762
137
    }
763
764
    // Done.
765
766
770
    if (KeepPreLin != NULL) cmsStageFree(KeepPreLin);
767
770
    if (KeepPostLin != NULL) cmsStageFree(KeepPostLin);
768
770
    cmsPipelineFree(Src);
769
770
770
    DataCLUT = (_cmsStageCLutData*) CLUT ->Data;
771
772
770
    if (NewPreLin == NULL) DataSetIn = NULL;
773
0
    else DataSetIn = ((_cmsStageToneCurvesData*) NewPreLin ->Data) ->TheCurves;
774
775
770
    if (NewPostLin == NULL) DataSetOut = NULL;
776
193
    else  DataSetOut = ((_cmsStageToneCurvesData*) NewPostLin ->Data) ->TheCurves;
777
778
779
770
    if (DataSetIn == NULL && DataSetOut == NULL) {
780
781
577
        _cmsPipelineSetOptimizationParameters(Dest, (_cmsPipelineEval16Fn) DataCLUT->Params->Interpolation.Lerp16, DataCLUT->Params, NULL, NULL);
782
577
    }
783
193
    else {
784
785
193
        p16 = PrelinOpt16alloc(Dest ->ContextID,
786
193
            DataCLUT ->Params,
787
193
            Dest ->InputChannels,
788
193
            DataSetIn,
789
193
            Dest ->OutputChannels,
790
193
            DataSetOut);
791
792
193
        _cmsPipelineSetOptimizationParameters(Dest, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
793
193
    }
794
795
796
    // Don't fix white on absolute colorimetric
797
770
    if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
798
28
        *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
799
800
770
    if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
801
802
681
        FixWhiteMisalignment(Dest, ColorSpace, OutputColorSpace);
803
681
    }
804
805
770
    *Lut = Dest;
806
770
    return TRUE;
807
808
0
    cmsUNUSED_PARAMETER(Intent);
809
0
}
810
811
812
// -----------------------------------------------------------------------------------------------------------------------------------------------
813
// Fixes the gamma balancing of transform. This is described in my paper "Prelinearization Stages on
814
// Color-Management Application-Specific Integrated Circuits (ASICs)" presented at NIP24. It only works
815
// for RGB transforms. See the paper for more details
816
// -----------------------------------------------------------------------------------------------------------------------------------------------
817
818
819
// Normalize endpoints by slope limiting max and min. This assures endpoints as well.
820
// Descending curves are handled as well.
821
static
822
void SlopeLimiting(cmsToneCurve* g)
823
0
{
824
0
    int BeginVal, EndVal;
825
0
    int AtBegin = (int) floor((cmsFloat64Number) g ->nEntries * 0.02 + 0.5);   // Cutoff at 2%
826
0
    int AtEnd   = (int) g ->nEntries - AtBegin - 1;                                  // And 98%
827
0
    cmsFloat64Number Val, Slope, beta;
828
0
    int i;
829
830
0
    if (cmsIsToneCurveDescending(g)) {
831
0
        BeginVal = 0xffff; EndVal = 0;
832
0
    }
833
0
    else {
834
0
        BeginVal = 0; EndVal = 0xffff;
835
0
    }
836
837
    // Compute slope and offset for begin of curve
838
0
    Val   = g ->Table16[AtBegin];
839
0
    Slope = (Val - BeginVal) / AtBegin;
840
0
    beta  = Val - Slope * AtBegin;
841
842
0
    for (i=0; i < AtBegin; i++)
843
0
        g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
844
845
    // Compute slope and offset for the end
846
0
    Val   = g ->Table16[AtEnd];
847
0
    Slope = (EndVal - Val) / AtBegin;   // AtBegin holds the X interval, which is same in both cases
848
0
    beta  = Val - Slope * AtEnd;
849
850
0
    for (i = AtEnd; i < (int) g ->nEntries; i++)
851
0
        g ->Table16[i] = _cmsQuickSaturateWord(i * Slope + beta);
852
0
}
853
854
855
// Precomputes tables for 8-bit on input devicelink.
856
static
857
Prelin8Data* PrelinOpt8alloc(cmsContext ContextID, const cmsInterpParams* p, cmsToneCurve* G[3])
858
0
{
859
0
    int i;
860
0
    cmsUInt16Number Input[3];
861
0
    cmsS15Fixed16Number v1, v2, v3;
862
0
    Prelin8Data* p8;
863
864
0
    p8 = (Prelin8Data*)_cmsMallocZero(ContextID, sizeof(Prelin8Data));
865
0
    if (p8 == NULL) return NULL;
866
867
    // Since this only works for 8 bit input, values comes always as x * 257,
868
    // we can safely take msb byte (x << 8 + x)
869
870
0
    for (i=0; i < 256; i++) {
871
872
0
        if (G != NULL) {
873
874
            // Get 16-bit representation
875
0
            Input[0] = cmsEvalToneCurve16(G[0], FROM_8_TO_16(i));
876
0
            Input[1] = cmsEvalToneCurve16(G[1], FROM_8_TO_16(i));
877
0
            Input[2] = cmsEvalToneCurve16(G[2], FROM_8_TO_16(i));
878
0
        }
879
0
        else {
880
0
            Input[0] = FROM_8_TO_16(i);
881
0
            Input[1] = FROM_8_TO_16(i);
882
0
            Input[2] = FROM_8_TO_16(i);
883
0
        }
884
885
886
        // Move to 0..1.0 in fixed domain
887
0
        v1 = _cmsToFixedDomain((int) (Input[0] * p -> Domain[0]));
888
0
        v2 = _cmsToFixedDomain((int) (Input[1] * p -> Domain[1]));
889
0
        v3 = _cmsToFixedDomain((int) (Input[2] * p -> Domain[2]));
890
891
        // Store the precalculated table of nodes
892
0
        p8 ->X0[i] = (p->opta[2] * FIXED_TO_INT(v1));
893
0
        p8 ->Y0[i] = (p->opta[1] * FIXED_TO_INT(v2));
894
0
        p8 ->Z0[i] = (p->opta[0] * FIXED_TO_INT(v3));
895
896
        // Store the precalculated table of offsets
897
0
        p8 ->rx[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v1);
898
0
        p8 ->ry[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v2);
899
0
        p8 ->rz[i] = (cmsUInt16Number) FIXED_REST_TO_INT(v3);
900
0
    }
901
902
0
    p8 ->ContextID = ContextID;
903
0
    p8 ->p = p;
904
905
0
    return p8;
906
0
}
907
908
static
909
void Prelin8free(cmsContext ContextID, void* ptr)
910
0
{
911
0
    _cmsFree(ContextID, ptr);
912
0
}
913
914
static
915
void* Prelin8dup(cmsContext ContextID, const void* ptr)
916
0
{
917
0
    return _cmsDupMem(ContextID, ptr, sizeof(Prelin8Data));
918
0
}
919
920
921
922
// A optimized interpolation for 8-bit input.
923
0
#define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan])
924
static CMS_NO_SANITIZE
925
void PrelinEval8(CMSREGISTER const cmsUInt16Number Input[],
926
                 CMSREGISTER cmsUInt16Number Output[],
927
                 CMSREGISTER const void* D)
928
0
{
929
930
0
    cmsUInt8Number         r, g, b;
931
0
    cmsS15Fixed16Number    rx, ry, rz;
932
0
    cmsS15Fixed16Number    c0, c1, c2, c3, Rest;
933
0
    int                    OutChan;
934
0
    CMSREGISTER cmsS15Fixed16Number X0, X1, Y0, Y1, Z0, Z1;
935
0
    Prelin8Data* p8 = (Prelin8Data*) D;
936
0
    CMSREGISTER const cmsInterpParams* p = p8 ->p;
937
0
    int                    TotalOut = (int) p -> nOutputs;
938
0
    const cmsUInt16Number* LutTable = (const cmsUInt16Number*) p->Table;
939
940
0
    r = (cmsUInt8Number) (Input[0] >> 8);
941
0
    g = (cmsUInt8Number) (Input[1] >> 8);
942
0
    b = (cmsUInt8Number) (Input[2] >> 8);
943
944
0
    X0 = (cmsS15Fixed16Number) p8->X0[r];
945
0
    Y0 = (cmsS15Fixed16Number) p8->Y0[g];
946
0
    Z0 = (cmsS15Fixed16Number) p8->Z0[b];
947
948
0
    rx = p8 ->rx[r];
949
0
    ry = p8 ->ry[g];
950
0
    rz = p8 ->rz[b];
951
952
0
    X1 = X0 + (cmsS15Fixed16Number)((rx == 0) ? 0 :  p ->opta[2]);
953
0
    Y1 = Y0 + (cmsS15Fixed16Number)((ry == 0) ? 0 :  p ->opta[1]);
954
0
    Z1 = Z0 + (cmsS15Fixed16Number)((rz == 0) ? 0 :  p ->opta[0]);
955
956
957
    // These are the 6 Tetrahedral
958
0
    for (OutChan=0; OutChan < TotalOut; OutChan++) {
959
960
0
        c0 = DENS(X0, Y0, Z0);
961
962
0
        if (rx >= ry && ry >= rz)
963
0
        {
964
0
            c1 = DENS(X1, Y0, Z0) - c0;
965
0
            c2 = DENS(X1, Y1, Z0) - DENS(X1, Y0, Z0);
966
0
            c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
967
0
        }
968
0
        else
969
0
            if (rx >= rz && rz >= ry)
970
0
            {
971
0
                c1 = DENS(X1, Y0, Z0) - c0;
972
0
                c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
973
0
                c3 = DENS(X1, Y0, Z1) - DENS(X1, Y0, Z0);
974
0
            }
975
0
            else
976
0
                if (rz >= rx && rx >= ry)
977
0
                {
978
0
                    c1 = DENS(X1, Y0, Z1) - DENS(X0, Y0, Z1);
979
0
                    c2 = DENS(X1, Y1, Z1) - DENS(X1, Y0, Z1);
980
0
                    c3 = DENS(X0, Y0, Z1) - c0;
981
0
                }
982
0
                else
983
0
                    if (ry >= rx && rx >= rz)
984
0
                    {
985
0
                        c1 = DENS(X1, Y1, Z0) - DENS(X0, Y1, Z0);
986
0
                        c2 = DENS(X0, Y1, Z0) - c0;
987
0
                        c3 = DENS(X1, Y1, Z1) - DENS(X1, Y1, Z0);
988
0
                    }
989
0
                    else
990
0
                        if (ry >= rz && rz >= rx)
991
0
                        {
992
0
                            c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
993
0
                            c2 = DENS(X0, Y1, Z0) - c0;
994
0
                            c3 = DENS(X0, Y1, Z1) - DENS(X0, Y1, Z0);
995
0
                        }
996
0
                        else
997
0
                            if (rz >= ry && ry >= rx)
998
0
                            {
999
0
                                c1 = DENS(X1, Y1, Z1) - DENS(X0, Y1, Z1);
1000
0
                                c2 = DENS(X0, Y1, Z1) - DENS(X0, Y0, Z1);
1001
0
                                c3 = DENS(X0, Y0, Z1) - c0;
1002
0
                            }
1003
0
                            else  {
1004
0
                                c1 = c2 = c3 = 0;
1005
0
                            }
1006
1007
0
        Rest = c1 * rx + c2 * ry + c3 * rz + 0x8001;
1008
0
        Output[OutChan] = (cmsUInt16Number) (c0 + ((Rest + (Rest >> 16)) >> 16));
1009
1010
0
    }
1011
0
}
1012
1013
#undef DENS
1014
1015
1016
// Curves that contain wide empty areas are not optimizeable
1017
static
1018
cmsBool IsDegenerated(const cmsToneCurve* g)
1019
0
{
1020
0
    cmsUInt32Number i, Zeros = 0, Poles = 0;
1021
0
    cmsUInt32Number nEntries = g ->nEntries;
1022
1023
0
    for (i=0; i < nEntries; i++) {
1024
1025
0
        if (g ->Table16[i] == 0x0000) Zeros++;
1026
0
        if (g ->Table16[i] == 0xffff) Poles++;
1027
0
    }
1028
1029
0
    if (Zeros == 1 && Poles == 1) return FALSE;  // For linear tables
1030
0
    if (Zeros > (nEntries / 20)) return TRUE;  // Degenerated, many zeros
1031
0
    if (Poles > (nEntries / 20)) return TRUE;  // Degenerated, many poles
1032
1033
0
    return FALSE;
1034
0
}
1035
1036
// --------------------------------------------------------------------------------------------------------------
1037
// We need xput over here
1038
1039
static
1040
cmsBool OptimizeByComputingLinearization(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1041
649
{
1042
649
    cmsPipeline* OriginalLut;
1043
649
    cmsUInt32Number nGridPoints;
1044
649
    cmsToneCurve *Trans[cmsMAXCHANNELS], *TransReverse[cmsMAXCHANNELS];
1045
649
    cmsUInt32Number t, i;
1046
649
    cmsFloat32Number v, In[cmsMAXCHANNELS], Out[cmsMAXCHANNELS];
1047
649
    cmsBool lIsSuitable, lIsLinear;
1048
649
    cmsPipeline* OptimizedLUT = NULL, *LutPlusCurves = NULL;
1049
649
    cmsStage* OptimizedCLUTmpe;
1050
649
    cmsColorSpaceSignature ColorSpace, OutputColorSpace;
1051
649
    cmsStage* OptimizedPrelinMpe;
1052
649
    cmsToneCurve** OptimizedPrelinCurves;
1053
649
    _cmsStageCLutData* OptimizedPrelinCLUT;
1054
1055
1056
    // This is a lossy optimization! does not apply in floating-point cases
1057
649
    if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
1058
1059
    // Only on chunky RGB
1060
649
    if (T_COLORSPACE(*InputFormat)  != PT_RGB) return FALSE;
1061
64
    if (T_PLANAR(*InputFormat)) return FALSE;
1062
1063
64
    if (T_COLORSPACE(*OutputFormat) != PT_RGB) return FALSE;
1064
64
    if (T_PLANAR(*OutputFormat)) return FALSE;
1065
1066
    // On 16 bits, user has to specify the feature
1067
64
    if (!_cmsFormatterIs8bit(*InputFormat)) {
1068
64
        if (!(*dwFlags & cmsFLAGS_CLUT_PRE_LINEARIZATION)) return FALSE;
1069
64
    }
1070
1071
0
    OriginalLut = *Lut;
1072
   
1073
0
    ColorSpace       = _cmsICCcolorSpace((int) T_COLORSPACE(*InputFormat));
1074
0
    OutputColorSpace = _cmsICCcolorSpace((int) T_COLORSPACE(*OutputFormat));
1075
1076
    // Color space must be specified
1077
0
    if (ColorSpace == (cmsColorSpaceSignature)0 ||
1078
0
        OutputColorSpace == (cmsColorSpaceSignature)0) return FALSE;
1079
1080
0
    nGridPoints      = _cmsReasonableGridpointsByColorspace(ColorSpace, *dwFlags);
1081
1082
    // Empty gamma containers
1083
0
    memset(Trans, 0, sizeof(Trans));
1084
0
    memset(TransReverse, 0, sizeof(TransReverse));
1085
1086
    // If the last stage of the original lut are curves, and those curves are
1087
    // degenerated, it is likely the transform is squeezing and clipping
1088
    // the output from previous CLUT. We cannot optimize this case     
1089
0
    {
1090
0
        cmsStage* last = cmsPipelineGetPtrToLastStage(OriginalLut);
1091
1092
0
        if (last == NULL) goto Error;
1093
0
        if (cmsStageType(last) == cmsSigCurveSetElemType) {
1094
1095
0
            _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*)cmsStageData(last);
1096
0
            for (i = 0; i < Data->nCurves; i++) {
1097
0
                if (IsDegenerated(Data->TheCurves[i]))
1098
0
                    goto Error;
1099
0
            }
1100
0
        }
1101
0
    }
1102
1103
0
    for (t = 0; t < OriginalLut ->InputChannels; t++) {
1104
0
        Trans[t] = cmsBuildTabulatedToneCurve16(OriginalLut ->ContextID, PRELINEARIZATION_POINTS, NULL);
1105
0
        if (Trans[t] == NULL) goto Error;
1106
0
    }
1107
1108
    // Populate the curves
1109
0
    for (i=0; i < PRELINEARIZATION_POINTS; i++) {
1110
1111
0
        v = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
1112
1113
        // Feed input with a gray ramp
1114
0
        for (t=0; t < OriginalLut ->InputChannels; t++)
1115
0
            In[t] = v;
1116
1117
        // Evaluate the gray value
1118
0
        cmsPipelineEvalFloat(In, Out, OriginalLut);
1119
1120
        // Store result in curve
1121
0
        for (t=0; t < OriginalLut ->InputChannels; t++)
1122
0
        {
1123
0
            if (Trans[t]->Table16 != NULL)
1124
0
                Trans[t] ->Table16[i] = _cmsQuickSaturateWord(Out[t] * 65535.0);
1125
0
        }
1126
0
    }
1127
1128
    // Slope-limit the obtained curves
1129
0
    for (t = 0; t < OriginalLut ->InputChannels; t++)
1130
0
        SlopeLimiting(Trans[t]);
1131
1132
    // Check for validity. lIsLinear is here for debug purposes
1133
0
    lIsSuitable = TRUE;
1134
0
    lIsLinear   = TRUE;
1135
0
    for (t=0; (lIsSuitable && (t < OriginalLut ->InputChannels)); t++) {
1136
1137
        // Exclude if already linear
1138
0
        if (!cmsIsToneCurveLinear(Trans[t]))
1139
0
            lIsLinear = FALSE;
1140
1141
        // Exclude if non-monotonic
1142
0
        if (!cmsIsToneCurveMonotonic(Trans[t]))
1143
0
            lIsSuitable = FALSE;
1144
1145
0
        if (IsDegenerated(Trans[t]))
1146
0
            lIsSuitable = FALSE;
1147
0
    }
1148
1149
    // If it is not suitable, just quit
1150
0
    if (!lIsSuitable) goto Error;
1151
1152
    // Invert curves if possible
1153
0
    for (t = 0; t < OriginalLut ->InputChannels; t++) {
1154
0
        TransReverse[t] = cmsReverseToneCurveEx(PRELINEARIZATION_POINTS, Trans[t]);
1155
0
        if (TransReverse[t] == NULL) goto Error;
1156
0
    }
1157
1158
    // Now inset the reversed curves at the begin of transform
1159
0
    LutPlusCurves = cmsPipelineDup(OriginalLut);
1160
0
    if (LutPlusCurves == NULL) goto Error;
1161
1162
0
    if (!cmsPipelineInsertStage(LutPlusCurves, cmsAT_BEGIN, cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, TransReverse)))
1163
0
        goto Error;
1164
1165
    // Create the result LUT
1166
0
    OptimizedLUT = cmsPipelineAlloc(OriginalLut ->ContextID, OriginalLut ->InputChannels, OriginalLut ->OutputChannels);
1167
0
    if (OptimizedLUT == NULL) goto Error;
1168
1169
0
    OptimizedPrelinMpe = cmsStageAllocToneCurves(OriginalLut ->ContextID, OriginalLut ->InputChannels, Trans);
1170
1171
    // Create and insert the curves at the beginning
1172
0
    if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_BEGIN, OptimizedPrelinMpe))
1173
0
        goto Error;
1174
1175
    // Allocate the CLUT for result
1176
0
    OptimizedCLUTmpe = cmsStageAllocCLut16bit(OriginalLut ->ContextID, nGridPoints, OriginalLut ->InputChannels, OriginalLut ->OutputChannels, NULL);
1177
1178
    // Add the CLUT to the destination LUT
1179
0
    if (!cmsPipelineInsertStage(OptimizedLUT, cmsAT_END, OptimizedCLUTmpe))
1180
0
        goto Error;
1181
1182
    // Resample the LUT
1183
0
    if (!cmsStageSampleCLut16bit(OptimizedCLUTmpe, XFormSampler16, (void*) LutPlusCurves, 0)) goto Error;
1184
1185
    // Free resources
1186
0
    for (t = 0; t < OriginalLut ->InputChannels; t++) {
1187
1188
0
        if (Trans[t]) cmsFreeToneCurve(Trans[t]);
1189
0
        if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
1190
0
    }
1191
1192
0
    cmsPipelineFree(LutPlusCurves);
1193
1194
1195
0
    OptimizedPrelinCurves = _cmsStageGetPtrToCurveSet(OptimizedPrelinMpe);
1196
0
    OptimizedPrelinCLUT   = (_cmsStageCLutData*) OptimizedCLUTmpe ->Data;
1197
1198
    // Set the evaluator if 8-bit
1199
0
    if (_cmsFormatterIs8bit(*InputFormat)) {
1200
1201
0
        Prelin8Data* p8 = PrelinOpt8alloc(OptimizedLUT ->ContextID,
1202
0
                                                OptimizedPrelinCLUT ->Params,
1203
0
                                                OptimizedPrelinCurves);
1204
0
        if (p8 == NULL) return FALSE;
1205
1206
0
        _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval8, (void*) p8, Prelin8free, Prelin8dup);
1207
1208
0
    }
1209
0
    else
1210
0
    {
1211
0
        Prelin16Data* p16 = PrelinOpt16alloc(OptimizedLUT ->ContextID,
1212
0
            OptimizedPrelinCLUT ->Params,
1213
0
            3, OptimizedPrelinCurves, 3, NULL);
1214
0
        if (p16 == NULL) return FALSE;
1215
1216
0
        _cmsPipelineSetOptimizationParameters(OptimizedLUT, PrelinEval16, (void*) p16, PrelinOpt16free, Prelin16dup);
1217
1218
0
    }
1219
1220
    // Don't fix white on absolute colorimetric
1221
0
    if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
1222
0
        *dwFlags |= cmsFLAGS_NOWHITEONWHITEFIXUP;
1223
1224
0
    if (!(*dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP)) {
1225
1226
0
        if (!FixWhiteMisalignment(OptimizedLUT, ColorSpace, OutputColorSpace)) {
1227
1228
0
            return FALSE;
1229
0
        }
1230
0
    }
1231
1232
    // And return the obtained LUT
1233
1234
0
    cmsPipelineFree(OriginalLut);
1235
0
    *Lut = OptimizedLUT;
1236
0
    return TRUE;
1237
1238
0
Error:
1239
1240
0
    for (t = 0; t < OriginalLut ->InputChannels; t++) {
1241
1242
0
        if (Trans[t]) cmsFreeToneCurve(Trans[t]);
1243
0
        if (TransReverse[t]) cmsFreeToneCurve(TransReverse[t]);
1244
0
    }
1245
1246
0
    if (LutPlusCurves != NULL) cmsPipelineFree(LutPlusCurves);
1247
0
    if (OptimizedLUT != NULL) cmsPipelineFree(OptimizedLUT);
1248
1249
0
    return FALSE;
1250
1251
0
    cmsUNUSED_PARAMETER(Intent);
1252
0
    cmsUNUSED_PARAMETER(lIsLinear);
1253
0
}
1254
1255
1256
// Curves optimizer ------------------------------------------------------------------------------------------------------------------
1257
1258
static
1259
void CurvesFree(cmsContext ContextID, void* ptr)
1260
0
{
1261
0
     Curves16Data* Data = (Curves16Data*) ptr;
1262
0
     cmsUInt32Number i;
1263
1264
0
     for (i=0; i < Data -> nCurves; i++) {
1265
1266
0
         _cmsFree(ContextID, Data ->Curves[i]);
1267
0
     }
1268
1269
0
     _cmsFree(ContextID, Data ->Curves);
1270
0
     _cmsFree(ContextID, ptr);
1271
0
}
1272
1273
static
1274
void* CurvesDup(cmsContext ContextID, const void* ptr)
1275
0
{
1276
0
    Curves16Data* Data = (Curves16Data*)_cmsDupMem(ContextID, ptr, sizeof(Curves16Data));
1277
0
    cmsUInt32Number i;
1278
1279
0
    if (Data == NULL) return NULL;
1280
1281
0
    Data->Curves = (cmsUInt16Number**) _cmsDupMem(ContextID, Data->Curves, Data->nCurves * sizeof(cmsUInt16Number*));
1282
1283
0
    for (i=0; i < Data -> nCurves; i++) {
1284
0
        Data->Curves[i] = (cmsUInt16Number*) _cmsDupMem(ContextID, Data->Curves[i], Data->nElements * sizeof(cmsUInt16Number));
1285
0
    }
1286
1287
0
    return (void*) Data;
1288
0
}
1289
1290
// Precomputes tables for 8-bit on input devicelink.
1291
static
1292
Curves16Data* CurvesAlloc(cmsContext ContextID, cmsUInt32Number nCurves, cmsUInt32Number nElements, cmsToneCurve** G)
1293
0
{
1294
0
    cmsUInt32Number i, j;
1295
0
    Curves16Data* c16;
1296
1297
0
    c16 = (Curves16Data*)_cmsMallocZero(ContextID, sizeof(Curves16Data));
1298
0
    if (c16 == NULL) return NULL;
1299
1300
0
    c16 ->nCurves = nCurves;
1301
0
    c16 ->nElements = nElements;
1302
1303
0
    c16->Curves = (cmsUInt16Number**) _cmsCalloc(ContextID, nCurves, sizeof(cmsUInt16Number*));
1304
0
    if (c16->Curves == NULL) {
1305
0
        _cmsFree(ContextID, c16);
1306
0
        return NULL;
1307
0
    }
1308
1309
0
    for (i=0; i < nCurves; i++) {
1310
1311
0
        c16->Curves[i] = (cmsUInt16Number*) _cmsCalloc(ContextID, nElements, sizeof(cmsUInt16Number));
1312
1313
0
        if (c16->Curves[i] == NULL) {
1314
1315
0
            for (j=0; j < i; j++) {
1316
0
                _cmsFree(ContextID, c16->Curves[j]);
1317
0
            }
1318
0
            _cmsFree(ContextID, c16->Curves);
1319
0
            _cmsFree(ContextID, c16);
1320
0
            return NULL;
1321
0
        }
1322
1323
0
        if (nElements == 256U) {
1324
1325
0
            for (j=0; j < nElements; j++) {
1326
1327
0
                c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], FROM_8_TO_16(j));
1328
0
            }
1329
0
        }
1330
0
        else {
1331
1332
0
            for (j=0; j < nElements; j++) {
1333
0
                c16 ->Curves[i][j] = cmsEvalToneCurve16(G[i], (cmsUInt16Number) j);
1334
0
            }
1335
0
        }
1336
0
    }
1337
1338
0
    return c16;
1339
0
}
1340
1341
static
1342
void FastEvaluateCurves8(CMSREGISTER const cmsUInt16Number In[],
1343
                         CMSREGISTER cmsUInt16Number Out[],
1344
                         CMSREGISTER const void* D)
1345
0
{
1346
0
    Curves16Data* Data = (Curves16Data*) D;
1347
0
    int x;
1348
0
    cmsUInt32Number i;
1349
1350
0
    for (i=0; i < Data ->nCurves; i++) {
1351
1352
0
         x = (In[i] >> 8);
1353
0
         Out[i] = Data -> Curves[i][x];
1354
0
    }
1355
0
}
1356
1357
1358
static
1359
void FastEvaluateCurves16(CMSREGISTER const cmsUInt16Number In[],
1360
                          CMSREGISTER cmsUInt16Number Out[],
1361
                          CMSREGISTER const void* D)
1362
0
{
1363
0
    Curves16Data* Data = (Curves16Data*) D;
1364
0
    cmsUInt32Number i;
1365
1366
0
    for (i=0; i < Data ->nCurves; i++) {
1367
0
         Out[i] = Data -> Curves[i][In[i]];
1368
0
    }
1369
0
}
1370
1371
1372
static
1373
void FastIdentity16(CMSREGISTER const cmsUInt16Number In[],
1374
                    CMSREGISTER cmsUInt16Number Out[],
1375
                    CMSREGISTER const void* D)
1376
0
{
1377
0
    cmsPipeline* Lut = (cmsPipeline*) D;
1378
0
    cmsUInt32Number i;
1379
1380
0
    for (i=0; i < Lut ->InputChannels; i++) {
1381
0
         Out[i] = In[i];
1382
0
    }
1383
0
}
1384
1385
1386
// If the target LUT holds only curves, the optimization procedure is to join all those
1387
// curves together. That only works on curves and does not work on matrices.
1388
static
1389
cmsBool OptimizeByJoiningCurves(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1390
685
{
1391
685
    cmsToneCurve** GammaTables = NULL;
1392
685
    cmsFloat32Number InFloat[cmsMAXCHANNELS], OutFloat[cmsMAXCHANNELS];
1393
685
    cmsUInt32Number i, j;
1394
685
    cmsPipeline* Src = *Lut;
1395
685
    cmsPipeline* Dest = NULL;
1396
685
    cmsStage* mpe;
1397
685
    cmsStage* ObtainedCurves = NULL;
1398
1399
1400
    // This is a lossy optimization! does not apply in floating-point cases
1401
685
    if (_cmsFormatterIsFloat(*InputFormat) || _cmsFormatterIsFloat(*OutputFormat)) return FALSE;
1402
1403
    //  Only curves in this LUT?
1404
685
    for (mpe = cmsPipelineGetPtrToFirstStage(Src);
1405
1.13k
         mpe != NULL;
1406
1.13k
         mpe = cmsStageNext(mpe)) {
1407
1.13k
            if (cmsStageType(mpe) != cmsSigCurveSetElemType) return FALSE;
1408
1.13k
    }
1409
1410
    // Allocate an empty LUT
1411
0
    Dest =  cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
1412
0
    if (Dest == NULL) return FALSE;
1413
1414
    // Create target curves
1415
0
    GammaTables = (cmsToneCurve**) _cmsCalloc(Src ->ContextID, Src ->InputChannels, sizeof(cmsToneCurve*));
1416
0
    if (GammaTables == NULL) goto Error;
1417
1418
0
    for (i=0; i < Src ->InputChannels; i++) {
1419
0
        GammaTables[i] = cmsBuildTabulatedToneCurve16(Src ->ContextID, PRELINEARIZATION_POINTS, NULL);
1420
0
        if (GammaTables[i] == NULL) goto Error;
1421
0
    }
1422
1423
    // Compute 16 bit result by using floating point
1424
0
    for (i=0; i < PRELINEARIZATION_POINTS; i++) {
1425
1426
0
        for (j=0; j < Src ->InputChannels; j++)
1427
0
            InFloat[j] = (cmsFloat32Number) ((cmsFloat64Number) i / (PRELINEARIZATION_POINTS - 1));
1428
1429
0
        cmsPipelineEvalFloat(InFloat, OutFloat, Src);
1430
1431
0
        for (j=0; j < Src ->InputChannels; j++)
1432
0
            GammaTables[j] -> Table16[i] = _cmsQuickSaturateWord(OutFloat[j] * 65535.0);
1433
0
    }
1434
1435
0
    ObtainedCurves = cmsStageAllocToneCurves(Src ->ContextID, Src ->InputChannels, GammaTables);
1436
0
    if (ObtainedCurves == NULL) goto Error;
1437
1438
0
    for (i=0; i < Src ->InputChannels; i++) {
1439
0
        cmsFreeToneCurve(GammaTables[i]);
1440
0
        GammaTables[i] = NULL;
1441
0
    }
1442
1443
0
    if (GammaTables != NULL) {
1444
0
        _cmsFree(Src->ContextID, GammaTables);
1445
0
        GammaTables = NULL;
1446
0
    }
1447
1448
    // Maybe the curves are linear at the end
1449
0
    if (!AllCurvesAreLinear(ObtainedCurves)) {
1450
0
       _cmsStageToneCurvesData* Data;
1451
1452
0
        if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, ObtainedCurves))
1453
0
            goto Error;
1454
0
        Data = (_cmsStageToneCurvesData*) cmsStageData(ObtainedCurves);
1455
0
        ObtainedCurves = NULL;
1456
1457
        // If the curves are to be applied in 8 bits, we can save memory
1458
0
        if (_cmsFormatterIs8bit(*InputFormat)) {
1459
0
             Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 256, Data ->TheCurves);
1460
1461
0
             if (c16 == NULL) goto Error;
1462
0
             *dwFlags |= cmsFLAGS_NOCACHE;
1463
0
            _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves8, c16, CurvesFree, CurvesDup);
1464
1465
0
        }
1466
0
        else {
1467
0
             Curves16Data* c16 = CurvesAlloc(Dest ->ContextID, Data ->nCurves, 65536, Data ->TheCurves);
1468
1469
0
             if (c16 == NULL) goto Error;
1470
0
             *dwFlags |= cmsFLAGS_NOCACHE;
1471
0
            _cmsPipelineSetOptimizationParameters(Dest, FastEvaluateCurves16, c16, CurvesFree, CurvesDup);
1472
0
        }
1473
0
    }
1474
0
    else {
1475
1476
        // LUT optimizes to nothing. Set the identity LUT
1477
0
        cmsStageFree(ObtainedCurves);
1478
0
        ObtainedCurves = NULL;
1479
1480
0
        if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageAllocIdentity(Dest ->ContextID, Src ->InputChannels)))
1481
0
            goto Error;
1482
1483
0
        *dwFlags |= cmsFLAGS_NOCACHE;
1484
0
        _cmsPipelineSetOptimizationParameters(Dest, FastIdentity16, (void*) Dest, NULL, NULL);
1485
0
    }
1486
1487
    // We are done.
1488
0
    cmsPipelineFree(Src);
1489
0
    *Lut = Dest;
1490
0
    return TRUE;
1491
1492
0
Error:
1493
1494
0
    if (ObtainedCurves != NULL) cmsStageFree(ObtainedCurves);
1495
0
    if (GammaTables != NULL) {
1496
0
        for (i=0; i < Src ->InputChannels; i++) {
1497
0
            if (GammaTables[i] != NULL) cmsFreeToneCurve(GammaTables[i]);
1498
0
        }
1499
1500
0
        _cmsFree(Src ->ContextID, GammaTables);
1501
0
    }
1502
1503
0
    if (Dest != NULL) cmsPipelineFree(Dest);
1504
0
    return FALSE;
1505
1506
0
    cmsUNUSED_PARAMETER(Intent);
1507
0
    cmsUNUSED_PARAMETER(InputFormat);
1508
0
    cmsUNUSED_PARAMETER(OutputFormat);
1509
0
    cmsUNUSED_PARAMETER(dwFlags);
1510
0
}
1511
1512
// -------------------------------------------------------------------------------------------------------------------------------------
1513
// LUT is Shaper - Matrix - Matrix - Shaper, which is very frequent when combining two matrix-shaper profiles
1514
1515
1516
static
1517
void  FreeMatShaper(cmsContext ContextID, void* Data)
1518
82
{
1519
82
    if (Data != NULL) _cmsFree(ContextID, Data);
1520
82
}
1521
1522
static
1523
void* DupMatShaper(cmsContext ContextID, const void* Data)
1524
46
{
1525
46
    return _cmsDupMem(ContextID, Data, sizeof(MatShaper8Data));
1526
46
}
1527
1528
1529
// A fast matrix-shaper evaluator for 8 bits. This is a bit tricky since I'm using 1.14 signed fixed point
1530
// to accomplish some performance. Actually it takes 256x3 16 bits tables and 16385 x 3 tables of 8 bits,
1531
// in total about 50K, and the performance boost is huge!
1532
static CMS_NO_SANITIZE
1533
void MatShaperEval16(CMSREGISTER const cmsUInt16Number In[],
1534
                     CMSREGISTER cmsUInt16Number Out[],
1535
                     CMSREGISTER const void* D)
1536
0
{
1537
0
    MatShaper8Data* p = (MatShaper8Data*) D;
1538
0
    cmsS1Fixed14Number l1, l2, l3, r, g, b;
1539
0
    cmsUInt32Number ri, gi, bi;
1540
1541
    // In this case (and only in this case!) we can use this simplification since
1542
    // In[] is assured to come from a 8 bit number. (a << 8 | a)
1543
0
    ri = In[0] & 0xFFU;
1544
0
    gi = In[1] & 0xFFU;
1545
0
    bi = In[2] & 0xFFU;
1546
1547
    // Across first shaper, which also converts to 1.14 fixed point
1548
0
    r = p->Shaper1R[ri];
1549
0
    g = p->Shaper1G[gi];
1550
0
    b = p->Shaper1B[bi];
1551
1552
    // Evaluate the matrix in 1.14 fixed point
1553
0
    l1 =  (p->Mat[0][0] * r + p->Mat[0][1] * g + p->Mat[0][2] * b + p->Off[0] + 0x2000) >> 14;
1554
0
    l2 =  (p->Mat[1][0] * r + p->Mat[1][1] * g + p->Mat[1][2] * b + p->Off[1] + 0x2000) >> 14;
1555
0
    l3 =  (p->Mat[2][0] * r + p->Mat[2][1] * g + p->Mat[2][2] * b + p->Off[2] + 0x2000) >> 14;
1556
1557
    // Now we have to clip to 0..1.0 range
1558
0
    ri = (l1 < 0) ? 0 : ((l1 > 16384) ? 16384U : (cmsUInt32Number) l1);
1559
0
    gi = (l2 < 0) ? 0 : ((l2 > 16384) ? 16384U : (cmsUInt32Number) l2);
1560
0
    bi = (l3 < 0) ? 0 : ((l3 > 16384) ? 16384U : (cmsUInt32Number) l3);
1561
1562
    // And across second shaper,
1563
0
    Out[0] = p->Shaper2R[ri];
1564
0
    Out[1] = p->Shaper2G[gi];
1565
0
    Out[2] = p->Shaper2B[bi];
1566
1567
0
}
1568
1569
// This table converts from 8 bits to 1.14 after applying the curve
1570
static
1571
void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
1572
108
{
1573
108
    int i;
1574
108
    cmsFloat32Number R, y;
1575
1576
27.7k
    for (i=0; i < 256; i++) {
1577
1578
27.6k
        R   = (cmsFloat32Number) (i / 255.0);
1579
27.6k
        y   = cmsEvalToneCurveFloat(Curve, R);
1580
1581
27.6k
        if (y < 131072.0)
1582
19.3k
            Table[i] = DOUBLE_TO_1FIXED14(y);
1583
8.28k
        else
1584
8.28k
            Table[i] = 0x7fffffff;
1585
27.6k
    }
1586
108
}
1587
1588
// This table converts form 1.14 (being 0x4000 the last entry) to 8 bits after applying the curve
1589
static
1590
void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
1591
108
{
1592
108
    int i;
1593
108
    cmsFloat32Number R, Val;
1594
1595
1.76M
    for (i=0; i < 16385; i++) {
1596
1597
1.76M
        R   = (cmsFloat32Number) (i / 16384.0);
1598
1.76M
        Val = cmsEvalToneCurveFloat(Curve, R);    // Val comes 0..1.0
1599
1600
1.76M
        if (Val < 0)
1601
0
            Val = 0;
1602
1603
1.76M
        if (Val > 1.0)
1604
0
            Val = 1.0;
1605
1606
1.76M
        if (Is8BitsOutput) {
1607
1608
            // If 8 bits output, we can optimize further by computing the / 257 part.
1609
            // first we compute the resulting byte and then we store the byte times
1610
            // 257. This quantization allows to round very quick by doing a >> 8, but
1611
            // since the low byte is always equal to msb, we can do a & 0xff and this works!
1612
1.76M
            cmsUInt16Number w = _cmsQuickSaturateWord(Val * 65535.0);
1613
1.76M
            cmsUInt8Number  b = FROM_16_TO_8(w);
1614
1615
1.76M
            Table[i] = FROM_8_TO_16(b);
1616
1.76M
        }
1617
0
        else Table[i]  = _cmsQuickSaturateWord(Val * 65535.0);
1618
1.76M
    }
1619
108
}
1620
1621
// Compute the matrix-shaper structure
1622
static
1623
cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, cmsVEC3* Off, cmsToneCurve* Curve2[3], cmsUInt32Number* OutputFormat)
1624
36
{
1625
36
    MatShaper8Data* p;
1626
36
    int i, j;
1627
36
    cmsBool Is8Bits = _cmsFormatterIs8bit(*OutputFormat);
1628
1629
    // Allocate a big chuck of memory to store precomputed tables
1630
36
    p = (MatShaper8Data*) _cmsMalloc(Dest ->ContextID, sizeof(MatShaper8Data));
1631
36
    if (p == NULL) return FALSE;
1632
1633
36
    p -> ContextID = Dest -> ContextID;
1634
1635
    // Precompute tables
1636
36
    FillFirstShaper(p ->Shaper1R, Curve1[0]);
1637
36
    FillFirstShaper(p ->Shaper1G, Curve1[1]);
1638
36
    FillFirstShaper(p ->Shaper1B, Curve1[2]);
1639
1640
36
    FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits);
1641
36
    FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits);
1642
36
    FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits);
1643
1644
    // Convert matrix to nFixed14. Note that those values may take more than 16 bits 
1645
144
    for (i=0; i < 3; i++) {
1646
432
        for (j=0; j < 3; j++) {
1647
324
            p ->Mat[i][j] = DOUBLE_TO_1FIXED14(Mat->v[i].n[j]);
1648
324
        }
1649
108
    }
1650
1651
144
    for (i=0; i < 3; i++) {
1652
1653
108
        if (Off == NULL) {
1654
108
            p ->Off[i] = 0;
1655
108
        }
1656
0
        else {
1657
0
            p ->Off[i] = DOUBLE_TO_1FIXED14(Off->n[i]);
1658
0
        }
1659
108
    }
1660
1661
    // Mark as optimized for faster formatter
1662
36
    if (Is8Bits)
1663
36
        *OutputFormat |= OPTIMIZED_SH(1);
1664
1665
    // Fill function pointers
1666
36
    _cmsPipelineSetOptimizationParameters(Dest, MatShaperEval16, (void*) p, FreeMatShaper, DupMatShaper);
1667
36
    return TRUE;
1668
36
}
1669
1670
//  8 bits on input allows matrix-shaper boot up to 25 Mpixels per second on RGB. That's fast!
1671
static
1672
cmsBool OptimizeMatrixShaper(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt32Number* InputFormat, cmsUInt32Number* OutputFormat, cmsUInt32Number* dwFlags)
1673
685
{
1674
685
       cmsStage* Curve1, *Curve2;
1675
685
       cmsStage* Matrix1, *Matrix2;
1676
685
       cmsMAT3 res;
1677
685
       cmsBool IdentityMat;
1678
685
       cmsPipeline* Dest, *Src;
1679
685
       cmsFloat64Number* Offset;
1680
1681
       // Only works on RGB to RGB
1682
685
       if (T_CHANNELS(*InputFormat) != 3 || T_CHANNELS(*OutputFormat) != 3) return FALSE;
1683
1684
       // Only works on 8 bit input
1685
290
       if (!_cmsFormatterIs8bit(*InputFormat)) return FALSE;
1686
1687
       // Seems suitable, proceed
1688
146
       Src = *Lut;
1689
1690
       // Check for:
1691
       // 
1692
       //    shaper-matrix-matrix-shaper 
1693
       //    shaper-matrix-shaper
1694
       // 
1695
       // Both of those constructs are possible (first because abs. colorimetric). 
1696
       // additionally, In the first case, the input matrix offset should be zero.
1697
1698
146
       IdentityMat = FALSE;
1699
146
       if (cmsPipelineCheckAndRetreiveStages(Src, 4,
1700
146
              cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
1701
146
              &Curve1, &Matrix1, &Matrix2, &Curve2)) {
1702
1703
              // Get both matrices
1704
5
              _cmsStageMatrixData* Data1 = (_cmsStageMatrixData*)cmsStageData(Matrix1);
1705
5
              _cmsStageMatrixData* Data2 = (_cmsStageMatrixData*)cmsStageData(Matrix2);
1706
1707
              // Only RGB to RGB
1708
5
              if (Matrix1->InputChannels != 3 || Matrix1->OutputChannels != 3 ||
1709
5
                  Matrix2->InputChannels != 3 || Matrix2->OutputChannels != 3) return FALSE;
1710
1711
              // Input offset should be zero
1712
5
              if (Data1->Offset != NULL) return FALSE;
1713
1714
              // Multiply both matrices to get the result
1715
0
              _cmsMAT3per(&res, (cmsMAT3*)Data2->Double, (cmsMAT3*)Data1->Double);
1716
1717
              // Only 2nd matrix has offset, or it is zero 
1718
0
              Offset = Data2->Offset;
1719
1720
              // Now the result is in res + Data2 -> Offset. Maybe is a plain identity?
1721
0
              if (_cmsMAT3isIdentity(&res) && Offset == NULL) {
1722
1723
                     // We can get rid of full matrix
1724
0
                     IdentityMat = TRUE;
1725
0
              }
1726
1727
0
       }
1728
141
       else {
1729
1730
141
              if (cmsPipelineCheckAndRetreiveStages(Src, 3,
1731
141
                     cmsSigCurveSetElemType, cmsSigMatrixElemType, cmsSigCurveSetElemType,
1732
141
                     &Curve1, &Matrix1, &Curve2)) {
1733
1734
36
                     _cmsStageMatrixData* Data = (_cmsStageMatrixData*)cmsStageData(Matrix1);
1735
1736
36
                     if (Matrix1->InputChannels != 3 || Matrix1->OutputChannels != 3) return FALSE;
1737
1738
                     // Copy the matrix to our result
1739
36
                     memcpy(&res, Data->Double, sizeof(res));
1740
1741
                     // Preserve the Odffset (may be NULL as a zero offset)
1742
36
                     Offset = Data->Offset;
1743
1744
36
                     if (_cmsMAT3isIdentity(&res) && Offset == NULL) {
1745
1746
                            // We can get rid of full matrix
1747
0
                            IdentityMat = TRUE;
1748
0
                     }
1749
36
              }
1750
105
              else
1751
105
                     return FALSE; // Not optimizeable this time
1752
1753
141
       }
1754
1755
      // Allocate an empty LUT
1756
36
    Dest =  cmsPipelineAlloc(Src ->ContextID, Src ->InputChannels, Src ->OutputChannels);
1757
36
    if (!Dest) return FALSE;
1758
1759
    // Assamble the new LUT
1760
36
    if (!cmsPipelineInsertStage(Dest, cmsAT_BEGIN, cmsStageDup(Curve1)))
1761
0
        goto Error;
1762
1763
36
    if (!IdentityMat) {
1764
1765
36
           if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageAllocMatrix(Dest->ContextID, 3, 3, (const cmsFloat64Number*)&res, Offset)))
1766
0
                  goto Error;
1767
36
    }
1768
1769
36
    if (!cmsPipelineInsertStage(Dest, cmsAT_END, cmsStageDup(Curve2)))
1770
0
        goto Error;
1771
1772
    // If identity on matrix, we can further optimize the curves, so call the join curves routine
1773
36
    if (IdentityMat) {
1774
1775
0
        OptimizeByJoiningCurves(&Dest, Intent, InputFormat, OutputFormat, dwFlags);
1776
0
    }
1777
36
    else {
1778
36
        _cmsStageToneCurvesData* mpeC1 = (_cmsStageToneCurvesData*) cmsStageData(Curve1);
1779
36
        _cmsStageToneCurvesData* mpeC2 = (_cmsStageToneCurvesData*) cmsStageData(Curve2);
1780
1781
        // In this particular optimization, cache does not help as it takes more time to deal with
1782
        // the cache than with the pixel handling
1783
36
        *dwFlags |= cmsFLAGS_NOCACHE;
1784
1785
        // Setup the optimizarion routines
1786
36
        SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Offset, mpeC2->TheCurves, OutputFormat);
1787
36
    }
1788
1789
36
    cmsPipelineFree(Src);
1790
36
    *Lut = Dest;
1791
36
    return TRUE;
1792
0
Error:
1793
    // Leave Src unchanged
1794
0
    cmsPipelineFree(Dest);
1795
0
    return FALSE;
1796
36
}
1797
1798
1799
// -------------------------------------------------------------------------------------------------------------------------------------
1800
// Optimization plug-ins
1801
1802
// List of optimizations
1803
typedef struct _cmsOptimizationCollection_st {
1804
1805
    _cmsOPToptimizeFn  OptimizePtr;
1806
1807
    struct _cmsOptimizationCollection_st *Next;
1808
1809
} _cmsOptimizationCollection;
1810
1811
1812
// The built-in list. We currently implement 4 types of optimizations. Joining of curves, matrix-shaper, linearization and resampling
1813
static _cmsOptimizationCollection DefaultOptimization[] = {
1814
1815
    { OptimizeByJoiningCurves,            &DefaultOptimization[1] },
1816
    { OptimizeMatrixShaper,               &DefaultOptimization[2] },
1817
    { OptimizeByComputingLinearization,   &DefaultOptimization[3] },
1818
    { OptimizeByResampling,               NULL }
1819
};
1820
1821
// The linked list head
1822
_cmsOptimizationPluginChunkType _cmsOptimizationPluginChunk = { NULL };
1823
1824
1825
// Duplicates the zone of memory used by the plug-in in the new context
1826
static
1827
void DupPluginOptimizationList(struct _cmsContext_struct* ctx, 
1828
                               const struct _cmsContext_struct* src)
1829
0
{
1830
0
   _cmsOptimizationPluginChunkType newHead = { NULL };
1831
0
   _cmsOptimizationCollection*  entry;
1832
0
   _cmsOptimizationCollection*  Anterior = NULL;
1833
0
   _cmsOptimizationPluginChunkType* head = (_cmsOptimizationPluginChunkType*) src->chunks[OptimizationPlugin];
1834
1835
0
    _cmsAssert(ctx != NULL);
1836
0
    _cmsAssert(head != NULL);
1837
1838
    // Walk the list copying all nodes
1839
0
   for (entry = head->OptimizationCollection;
1840
0
        entry != NULL;
1841
0
        entry = entry ->Next) {
1842
1843
0
            _cmsOptimizationCollection *newEntry = ( _cmsOptimizationCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsOptimizationCollection));
1844
   
1845
0
            if (newEntry == NULL) 
1846
0
                return;
1847
1848
            // We want to keep the linked list order, so this is a little bit tricky
1849
0
            newEntry -> Next = NULL;
1850
0
            if (Anterior)
1851
0
                Anterior -> Next = newEntry;
1852
     
1853
0
            Anterior = newEntry;
1854
1855
0
            if (newHead.OptimizationCollection == NULL)
1856
0
                newHead.OptimizationCollection = newEntry;
1857
0
    }
1858
1859
0
  ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsOptimizationPluginChunkType));
1860
0
}
1861
1862
void  _cmsAllocOptimizationPluginChunk(struct _cmsContext_struct* ctx, 
1863
                                         const struct _cmsContext_struct* src)
1864
0
{
1865
0
  if (src != NULL) {
1866
1867
        // Copy all linked list
1868
0
       DupPluginOptimizationList(ctx, src);
1869
0
    }
1870
0
    else {
1871
0
        static _cmsOptimizationPluginChunkType OptimizationPluginChunkType = { NULL };
1872
0
        ctx ->chunks[OptimizationPlugin] = _cmsSubAllocDup(ctx ->MemPool, &OptimizationPluginChunkType, sizeof(_cmsOptimizationPluginChunkType));
1873
0
    }
1874
0
}
1875
1876
1877
// Register new ways to optimize
1878
cmsBool  _cmsRegisterOptimizationPlugin(cmsContext ContextID, cmsPluginBase* Data)
1879
0
{
1880
0
    cmsPluginOptimization* Plugin = (cmsPluginOptimization*) Data;
1881
0
    _cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
1882
0
    _cmsOptimizationCollection* fl;
1883
1884
0
    if (Data == NULL) {
1885
1886
0
        ctx->OptimizationCollection = NULL;
1887
0
        return TRUE;
1888
0
    }
1889
1890
    // Optimizer callback is required
1891
0
    if (Plugin ->OptimizePtr == NULL) return FALSE;
1892
1893
0
    fl = (_cmsOptimizationCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsOptimizationCollection));
1894
0
    if (fl == NULL) return FALSE;
1895
1896
    // Copy the parameters
1897
0
    fl ->OptimizePtr = Plugin ->OptimizePtr;
1898
1899
    // Keep linked list
1900
0
    fl ->Next = ctx->OptimizationCollection;
1901
1902
    // Set the head
1903
0
    ctx ->OptimizationCollection = fl;
1904
1905
    // All is ok
1906
0
    return TRUE;
1907
0
}
1908
1909
// The entry point for LUT optimization
1910
cmsBool CMSEXPORT _cmsOptimizePipeline(cmsContext ContextID,
1911
                             cmsPipeline**    PtrLut,
1912
                             cmsUInt32Number  Intent,
1913
                             cmsUInt32Number* InputFormat,
1914
                             cmsUInt32Number* OutputFormat,
1915
                             cmsUInt32Number* dwFlags)
1916
2.76k
{
1917
2.76k
    _cmsOptimizationPluginChunkType* ctx = ( _cmsOptimizationPluginChunkType*) _cmsContextGetClientChunk(ContextID, OptimizationPlugin);
1918
2.76k
    _cmsOptimizationCollection* Opts;
1919
2.76k
    cmsBool AnySuccess = FALSE;
1920
2.76k
    cmsStage* mpe;
1921
1922
    // A CLUT is being asked, so force this specific optimization
1923
2.76k
    if (*dwFlags & cmsFLAGS_FORCE_CLUT) {
1924
1925
683
        PreOptimize(*PtrLut);
1926
683
        return OptimizeByResampling(PtrLut, Intent, InputFormat, OutputFormat, dwFlags);
1927
683
    }
1928
1929
    // Anything to optimize?
1930
2.08k
    if ((*PtrLut) ->Elements == NULL) {
1931
0
        _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
1932
0
        return TRUE;
1933
0
    }
1934
1935
    // Named color pipelines cannot be optimized 
1936
2.08k
    for (mpe = cmsPipelineGetPtrToFirstStage(*PtrLut);
1937
14.3k
        mpe != NULL;
1938
12.2k
        mpe = cmsStageNext(mpe)) {        
1939
12.2k
            if (cmsStageType(mpe) == cmsSigNamedColorElemType) return FALSE;
1940
12.2k
    }
1941
1942
    // Try to get rid of identities and trivial conversions.
1943
2.06k
    AnySuccess = PreOptimize(*PtrLut);
1944
1945
    // After removal do we end with an identity?
1946
2.06k
    if ((*PtrLut) ->Elements == NULL) {
1947
1
        _cmsPipelineSetOptimizationParameters(*PtrLut, FastIdentity16, (void*) *PtrLut, NULL, NULL);
1948
1
        return TRUE;
1949
1
    }
1950
1951
    // Do not optimize, keep all precision
1952
2.06k
    if (*dwFlags & cmsFLAGS_NOOPTIMIZE)
1953
1.38k
        return FALSE;
1954
1955
    // Try plug-in optimizations 
1956
685
    for (Opts = ctx->OptimizationCollection;
1957
685
         Opts != NULL;
1958
685
         Opts = Opts ->Next) {
1959
1960
            // If one schema succeeded, we are done
1961
0
            if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
1962
1963
0
                return TRUE;    // Optimized!
1964
0
            }
1965
0
    }
1966
1967
   // Try built-in optimizations 
1968
685
    for (Opts = DefaultOptimization;
1969
3.04k
         Opts != NULL;
1970
2.66k
         Opts = Opts ->Next) {
1971
1972
2.66k
            if (Opts ->OptimizePtr(PtrLut, Intent, InputFormat, OutputFormat, dwFlags)) {
1973
1974
305
                return TRUE;  
1975
305
            }
1976
2.66k
    }
1977
1978
    // Only simple optimizations succeeded
1979
380
    return AnySuccess;
1980
685
}
1981
1982
1983