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