1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2017 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 // Allocates an empty multi profile element
_cmsStageAllocPlaceholder(cmsContext ContextID,cmsStageSignature Type,cmsUInt32Number InputChannels,cmsUInt32Number OutputChannels,_cmsStageEvalFn EvalPtr,_cmsStageDupElemFn DupElemPtr,_cmsStageFreeElemFn FreePtr,void * Data)31 cmsStage* CMSEXPORT _cmsStageAllocPlaceholder(cmsContext ContextID,
32 cmsStageSignature Type,
33 cmsUInt32Number InputChannels,
34 cmsUInt32Number OutputChannels,
35 _cmsStageEvalFn EvalPtr,
36 _cmsStageDupElemFn DupElemPtr,
37 _cmsStageFreeElemFn FreePtr,
38 void* Data)
39 {
40 cmsStage* ph = (cmsStage*) _cmsMallocZero(ContextID, sizeof(cmsStage));
41
42 if (ph == NULL) return NULL;
43
44
45 ph ->ContextID = ContextID;
46
47 ph ->Type = Type;
48 ph ->Implements = Type; // By default, no clue on what is implementing
49
50 ph ->InputChannels = InputChannels;
51 ph ->OutputChannels = OutputChannels;
52 ph ->EvalPtr = EvalPtr;
53 ph ->DupElemPtr = DupElemPtr;
54 ph ->FreePtr = FreePtr;
55 ph ->Data = Data;
56
57 return ph;
58 }
59
60
61 static
EvaluateIdentity(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)62 void EvaluateIdentity(const cmsFloat32Number In[],
63 cmsFloat32Number Out[],
64 const cmsStage *mpe)
65 {
66 memmove(Out, In, mpe ->InputChannels * sizeof(cmsFloat32Number));
67 }
68
69
cmsStageAllocIdentity(cmsContext ContextID,cmsUInt32Number nChannels)70 cmsStage* CMSEXPORT cmsStageAllocIdentity(cmsContext ContextID, cmsUInt32Number nChannels)
71 {
72 return _cmsStageAllocPlaceholder(ContextID,
73 cmsSigIdentityElemType,
74 nChannels, nChannels,
75 EvaluateIdentity,
76 NULL,
77 NULL,
78 NULL);
79 }
80
81 // Conversion functions. From floating point to 16 bits
82 static
FromFloatTo16(const cmsFloat32Number In[],cmsUInt16Number Out[],cmsUInt32Number n)83 void FromFloatTo16(const cmsFloat32Number In[], cmsUInt16Number Out[], cmsUInt32Number n)
84 {
85 cmsUInt32Number i;
86
87 for (i=0; i < n; i++) {
88 Out[i] = _cmsQuickSaturateWord(In[i] * 65535.0);
89 }
90 }
91
92 // From 16 bits to floating point
93 static
From16ToFloat(const cmsUInt16Number In[],cmsFloat32Number Out[],cmsUInt32Number n)94 void From16ToFloat(const cmsUInt16Number In[], cmsFloat32Number Out[], cmsUInt32Number n)
95 {
96 cmsUInt32Number i;
97
98 for (i=0; i < n; i++) {
99 Out[i] = (cmsFloat32Number) In[i] / 65535.0F;
100 }
101 }
102
103
104 // This function is quite useful to analyze the structure of a LUT and retrieve the MPE elements
105 // that conform the LUT. It should be called with the LUT, the number of expected elements and
106 // then a list of expected types followed with a list of cmsFloat64Number pointers to MPE elements. If
107 // the function founds a match with current pipeline, it fills the pointers and returns TRUE
108 // if not, returns FALSE without touching anything. Setting pointers to NULL does bypass
109 // the storage process.
cmsPipelineCheckAndRetreiveStages(const cmsPipeline * Lut,cmsUInt32Number n,...)110 cmsBool CMSEXPORT cmsPipelineCheckAndRetreiveStages(const cmsPipeline* Lut, cmsUInt32Number n, ...)
111 {
112 va_list args;
113 cmsUInt32Number i;
114 cmsStage* mpe;
115 cmsStageSignature Type;
116 void** ElemPtr;
117
118 // Make sure same number of elements
119 if (cmsPipelineStageCount(Lut) != n) return FALSE;
120
121 va_start(args, n);
122
123 // Iterate across asked types
124 mpe = Lut ->Elements;
125 for (i=0; i < n; i++) {
126
127 // Get asked type. cmsStageSignature is promoted to int by compiler
128 Type = (cmsStageSignature)va_arg(args, int);
129 if (mpe ->Type != Type) {
130
131 va_end(args); // Mismatch. We are done.
132 return FALSE;
133 }
134 mpe = mpe ->Next;
135 }
136
137 // Found a combination, fill pointers if not NULL
138 mpe = Lut ->Elements;
139 for (i=0; i < n; i++) {
140
141 ElemPtr = va_arg(args, void**);
142 if (ElemPtr != NULL)
143 *ElemPtr = mpe;
144
145 mpe = mpe ->Next;
146 }
147
148 va_end(args);
149 return TRUE;
150 }
151
152 // Below there are implementations for several types of elements. Each type may be implemented by a
153 // evaluation function, a duplication function, a function to free resources and a constructor.
154
155 // *************************************************************************************************
156 // Type cmsSigCurveSetElemType (curves)
157 // *************************************************************************************************
158
_cmsStageGetPtrToCurveSet(const cmsStage * mpe)159 cmsToneCurve** _cmsStageGetPtrToCurveSet(const cmsStage* mpe)
160 {
161 _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data;
162
163 return Data ->TheCurves;
164 }
165
166 static
EvaluateCurves(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)167 void EvaluateCurves(const cmsFloat32Number In[],
168 cmsFloat32Number Out[],
169 const cmsStage *mpe)
170 {
171 _cmsStageToneCurvesData* Data;
172 cmsUInt32Number i;
173
174 _cmsAssert(mpe != NULL);
175
176 Data = (_cmsStageToneCurvesData*) mpe ->Data;
177 if (Data == NULL) return;
178
179 if (Data ->TheCurves == NULL) return;
180
181 for (i=0; i < Data ->nCurves; i++) {
182 Out[i] = cmsEvalToneCurveFloat(Data ->TheCurves[i], In[i]);
183 }
184 }
185
186 static
CurveSetElemTypeFree(cmsStage * mpe)187 void CurveSetElemTypeFree(cmsStage* mpe)
188 {
189 _cmsStageToneCurvesData* Data;
190 cmsUInt32Number i;
191
192 _cmsAssert(mpe != NULL);
193
194 Data = (_cmsStageToneCurvesData*) mpe ->Data;
195 if (Data == NULL) return;
196
197 if (Data ->TheCurves != NULL) {
198 for (i=0; i < Data ->nCurves; i++) {
199 if (Data ->TheCurves[i] != NULL)
200 cmsFreeToneCurve(Data ->TheCurves[i]);
201 }
202 }
203 _cmsFree(mpe ->ContextID, Data ->TheCurves);
204 _cmsFree(mpe ->ContextID, Data);
205 }
206
207
208 static
CurveSetDup(cmsStage * mpe)209 void* CurveSetDup(cmsStage* mpe)
210 {
211 _cmsStageToneCurvesData* Data = (_cmsStageToneCurvesData*) mpe ->Data;
212 _cmsStageToneCurvesData* NewElem;
213 cmsUInt32Number i;
214
215 NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageToneCurvesData));
216 if (NewElem == NULL) return NULL;
217
218 NewElem ->nCurves = Data ->nCurves;
219 NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(mpe ->ContextID, NewElem ->nCurves, sizeof(cmsToneCurve*));
220
221 if (NewElem ->TheCurves == NULL) goto Error;
222
223 for (i=0; i < NewElem ->nCurves; i++) {
224
225 // Duplicate each curve. It may fail.
226 NewElem ->TheCurves[i] = cmsDupToneCurve(Data ->TheCurves[i]);
227 if (NewElem ->TheCurves[i] == NULL) goto Error;
228
229
230 }
231 return (void*) NewElem;
232
233 Error:
234
235 if (NewElem ->TheCurves != NULL) {
236 for (i=0; i < NewElem ->nCurves; i++) {
237 if (NewElem ->TheCurves[i])
238 cmsFreeToneCurve(NewElem ->TheCurves[i]);
239 }
240 }
241 _cmsFree(mpe ->ContextID, NewElem ->TheCurves);
242 _cmsFree(mpe ->ContextID, NewElem);
243 return NULL;
244 }
245
246
247 // Curves == NULL forces identity curves
cmsStageAllocToneCurves(cmsContext ContextID,cmsUInt32Number nChannels,cmsToneCurve * const Curves[])248 cmsStage* CMSEXPORT cmsStageAllocToneCurves(cmsContext ContextID, cmsUInt32Number nChannels, cmsToneCurve* const Curves[])
249 {
250 cmsUInt32Number i;
251 _cmsStageToneCurvesData* NewElem;
252 cmsStage* NewMPE;
253
254
255 NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCurveSetElemType, nChannels, nChannels,
256 EvaluateCurves, CurveSetDup, CurveSetElemTypeFree, NULL );
257 if (NewMPE == NULL) return NULL;
258
259 NewElem = (_cmsStageToneCurvesData*) _cmsMallocZero(ContextID, sizeof(_cmsStageToneCurvesData));
260 if (NewElem == NULL) {
261 cmsStageFree(NewMPE);
262 return NULL;
263 }
264
265 NewMPE ->Data = (void*) NewElem;
266
267 NewElem ->nCurves = nChannels;
268 NewElem ->TheCurves = (cmsToneCurve**) _cmsCalloc(ContextID, nChannels, sizeof(cmsToneCurve*));
269 if (NewElem ->TheCurves == NULL) {
270 cmsStageFree(NewMPE);
271 return NULL;
272 }
273
274 for (i=0; i < nChannels; i++) {
275
276 if (Curves == NULL) {
277 NewElem ->TheCurves[i] = cmsBuildGamma(ContextID, 1.0);
278 }
279 else {
280 NewElem ->TheCurves[i] = cmsDupToneCurve(Curves[i]);
281 }
282
283 if (NewElem ->TheCurves[i] == NULL) {
284 cmsStageFree(NewMPE);
285 return NULL;
286 }
287
288 }
289
290 return NewMPE;
291 }
292
293
294 // Create a bunch of identity curves
_cmsStageAllocIdentityCurves(cmsContext ContextID,cmsUInt32Number nChannels)295 cmsStage* _cmsStageAllocIdentityCurves(cmsContext ContextID, cmsUInt32Number nChannels)
296 {
297 cmsStage* mpe = cmsStageAllocToneCurves(ContextID, nChannels, NULL);
298
299 if (mpe == NULL) return NULL;
300 mpe ->Implements = cmsSigIdentityElemType;
301 return mpe;
302 }
303
304
305 // *************************************************************************************************
306 // Type cmsSigMatrixElemType (Matrices)
307 // *************************************************************************************************
308
309
310 // Special care should be taken here because precision loss. A temporary cmsFloat64Number buffer is being used
311 static
EvaluateMatrix(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)312 void EvaluateMatrix(const cmsFloat32Number In[],
313 cmsFloat32Number Out[],
314 const cmsStage *mpe)
315 {
316 cmsUInt32Number i, j;
317 _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
318 cmsFloat64Number Tmp;
319
320 // Input is already in 0..1.0 notation
321 for (i=0; i < mpe ->OutputChannels; i++) {
322
323 Tmp = 0;
324 for (j=0; j < mpe->InputChannels; j++) {
325 Tmp += In[j] * Data->Double[i*mpe->InputChannels + j];
326 }
327
328 if (Data ->Offset != NULL)
329 Tmp += Data->Offset[i];
330
331 Out[i] = (cmsFloat32Number) Tmp;
332 }
333
334
335 // Output in 0..1.0 domain
336 }
337
338
339 // Duplicate a yet-existing matrix element
340 static
MatrixElemDup(cmsStage * mpe)341 void* MatrixElemDup(cmsStage* mpe)
342 {
343 _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
344 _cmsStageMatrixData* NewElem;
345 cmsUInt32Number sz;
346
347 NewElem = (_cmsStageMatrixData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageMatrixData));
348 if (NewElem == NULL) return NULL;
349
350 sz = mpe ->InputChannels * mpe ->OutputChannels;
351
352 NewElem ->Double = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID, Data ->Double, sz * sizeof(cmsFloat64Number)) ;
353
354 if (Data ->Offset)
355 NewElem ->Offset = (cmsFloat64Number*) _cmsDupMem(mpe ->ContextID,
356 Data ->Offset, mpe -> OutputChannels * sizeof(cmsFloat64Number)) ;
357
358 return (void*) NewElem;
359 }
360
361
362 static
MatrixElemTypeFree(cmsStage * mpe)363 void MatrixElemTypeFree(cmsStage* mpe)
364 {
365 _cmsStageMatrixData* Data = (_cmsStageMatrixData*) mpe ->Data;
366 if (Data == NULL)
367 return;
368 if (Data ->Double)
369 _cmsFree(mpe ->ContextID, Data ->Double);
370
371 if (Data ->Offset)
372 _cmsFree(mpe ->ContextID, Data ->Offset);
373
374 _cmsFree(mpe ->ContextID, mpe ->Data);
375 }
376
377
378
cmsStageAllocMatrix(cmsContext ContextID,cmsUInt32Number Rows,cmsUInt32Number Cols,const cmsFloat64Number * Matrix,const cmsFloat64Number * Offset)379 cmsStage* CMSEXPORT cmsStageAllocMatrix(cmsContext ContextID, cmsUInt32Number Rows, cmsUInt32Number Cols,
380 const cmsFloat64Number* Matrix, const cmsFloat64Number* Offset)
381 {
382 cmsUInt32Number i, n;
383 _cmsStageMatrixData* NewElem;
384 cmsStage* NewMPE;
385
386 n = Rows * Cols;
387
388 // Check for overflow
389 if (n == 0) return NULL;
390 if (n >= UINT_MAX / Cols) return NULL;
391 if (n >= UINT_MAX / Rows) return NULL;
392 if (n < Rows || n < Cols) return NULL;
393
394 NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigMatrixElemType, Cols, Rows,
395 EvaluateMatrix, MatrixElemDup, MatrixElemTypeFree, NULL );
396 if (NewMPE == NULL) return NULL;
397
398
399 NewElem = (_cmsStageMatrixData*) _cmsMallocZero(ContextID, sizeof(_cmsStageMatrixData));
400 if (NewElem == NULL) return NULL;
401
402
403 NewElem ->Double = (cmsFloat64Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat64Number));
404
405 if (NewElem->Double == NULL) {
406 MatrixElemTypeFree(NewMPE);
407 return NULL;
408 }
409
410 for (i=0; i < n; i++) {
411 NewElem ->Double[i] = Matrix[i];
412 }
413
414
415 if (Offset != NULL) {
416
417 NewElem ->Offset = (cmsFloat64Number*) _cmsCalloc(ContextID, Rows, sizeof(cmsFloat64Number));
418 if (NewElem->Offset == NULL) {
419 MatrixElemTypeFree(NewMPE);
420 return NULL;
421 }
422
423 for (i=0; i < Rows; i++) {
424 NewElem ->Offset[i] = Offset[i];
425 }
426
427 }
428
429 NewMPE ->Data = (void*) NewElem;
430 return NewMPE;
431 }
432
433
434 // *************************************************************************************************
435 // Type cmsSigCLutElemType
436 // *************************************************************************************************
437
438
439 // Evaluate in true floating point
440 static
EvaluateCLUTfloat(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)441 void EvaluateCLUTfloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
442 {
443 _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
444
445 Data -> Params ->Interpolation.LerpFloat(In, Out, Data->Params);
446 }
447
448
449 // Convert to 16 bits, evaluate, and back to floating point
450 static
EvaluateCLUTfloatIn16(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)451 void EvaluateCLUTfloatIn16(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
452 {
453 _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
454 cmsUInt16Number In16[MAX_STAGE_CHANNELS], Out16[MAX_STAGE_CHANNELS];
455
456 _cmsAssert(mpe ->InputChannels <= MAX_STAGE_CHANNELS);
457 _cmsAssert(mpe ->OutputChannels <= MAX_STAGE_CHANNELS);
458
459 FromFloatTo16(In, In16, mpe ->InputChannels);
460 Data -> Params ->Interpolation.Lerp16(In16, Out16, Data->Params);
461 From16ToFloat(Out16, Out, mpe ->OutputChannels);
462 }
463
464
465 // Given an hypercube of b dimensions, with Dims[] number of nodes by dimension, calculate the total amount of nodes
466 static
CubeSize(const cmsUInt32Number Dims[],cmsUInt32Number b)467 cmsUInt32Number CubeSize(const cmsUInt32Number Dims[], cmsUInt32Number b)
468 {
469 cmsUInt32Number rv, dim;
470
471 _cmsAssert(Dims != NULL);
472
473 for (rv = 1; b > 0; b--) {
474
475 dim = Dims[b-1];
476 if (dim == 0) return 0; // Error
477
478 rv *= dim;
479
480 // Check for overflow
481 if (rv > UINT_MAX / dim) return 0;
482 }
483
484 return rv;
485 }
486
487 static
CLUTElemDup(cmsStage * mpe)488 void* CLUTElemDup(cmsStage* mpe)
489 {
490 _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
491 _cmsStageCLutData* NewElem;
492
493
494 NewElem = (_cmsStageCLutData*) _cmsMallocZero(mpe ->ContextID, sizeof(_cmsStageCLutData));
495 if (NewElem == NULL) return NULL;
496
497 NewElem ->nEntries = Data ->nEntries;
498 NewElem ->HasFloatValues = Data ->HasFloatValues;
499
500 if (Data ->Tab.T) {
501
502 if (Data ->HasFloatValues) {
503 NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.TFloat, Data ->nEntries * sizeof (cmsFloat32Number));
504 if (NewElem ->Tab.TFloat == NULL)
505 goto Error;
506 } else {
507 NewElem ->Tab.T = (cmsUInt16Number*) _cmsDupMem(mpe ->ContextID, Data ->Tab.T, Data ->nEntries * sizeof (cmsUInt16Number));
508 if (NewElem ->Tab.T == NULL)
509 goto Error;
510 }
511 }
512
513 NewElem ->Params = _cmsComputeInterpParamsEx(mpe ->ContextID,
514 Data ->Params ->nSamples,
515 Data ->Params ->nInputs,
516 Data ->Params ->nOutputs,
517 NewElem ->Tab.T,
518 Data ->Params ->dwFlags);
519 if (NewElem->Params != NULL)
520 return (void*) NewElem;
521 Error:
522 if (NewElem->Tab.T)
523 // This works for both types
524 _cmsFree(mpe ->ContextID, NewElem -> Tab.T);
525 _cmsFree(mpe ->ContextID, NewElem);
526 return NULL;
527 }
528
529
530 static
CLutElemTypeFree(cmsStage * mpe)531 void CLutElemTypeFree(cmsStage* mpe)
532 {
533
534 _cmsStageCLutData* Data = (_cmsStageCLutData*) mpe ->Data;
535
536 // Already empty
537 if (Data == NULL) return;
538
539 // This works for both types
540 if (Data -> Tab.T)
541 _cmsFree(mpe ->ContextID, Data -> Tab.T);
542
543 _cmsFreeInterpParams(Data ->Params);
544 _cmsFree(mpe ->ContextID, mpe ->Data);
545 }
546
547
548 // Allocates a 16-bit multidimensional CLUT. This is evaluated at 16-bit precision. Table may have different
549 // granularity on each dimension.
cmsStageAllocCLut16bitGranular(cmsContext ContextID,const cmsUInt32Number clutPoints[],cmsUInt32Number inputChan,cmsUInt32Number outputChan,const cmsUInt16Number * Table)550 cmsStage* CMSEXPORT cmsStageAllocCLut16bitGranular(cmsContext ContextID,
551 const cmsUInt32Number clutPoints[],
552 cmsUInt32Number inputChan,
553 cmsUInt32Number outputChan,
554 const cmsUInt16Number* Table)
555 {
556 cmsUInt32Number i, n;
557 _cmsStageCLutData* NewElem;
558 cmsStage* NewMPE;
559
560 _cmsAssert(clutPoints != NULL);
561
562 if (inputChan > MAX_INPUT_DIMENSIONS) {
563 cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS);
564 return NULL;
565 }
566
567 NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan,
568 EvaluateCLUTfloatIn16, CLUTElemDup, CLutElemTypeFree, NULL );
569
570 if (NewMPE == NULL) return NULL;
571
572 NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData));
573 if (NewElem == NULL) {
574 cmsStageFree(NewMPE);
575 return NULL;
576 }
577
578 NewMPE ->Data = (void*) NewElem;
579
580 NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan);
581 NewElem -> HasFloatValues = FALSE;
582
583 if (n == 0) {
584 cmsStageFree(NewMPE);
585 return NULL;
586 }
587
588
589 NewElem ->Tab.T = (cmsUInt16Number*) _cmsCalloc(ContextID, n, sizeof(cmsUInt16Number));
590 if (NewElem ->Tab.T == NULL) {
591 cmsStageFree(NewMPE);
592 return NULL;
593 }
594
595 if (Table != NULL) {
596 for (i=0; i < n; i++) {
597 NewElem ->Tab.T[i] = Table[i];
598 }
599 }
600
601 NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.T, CMS_LERP_FLAGS_16BITS);
602 if (NewElem ->Params == NULL) {
603 cmsStageFree(NewMPE);
604 return NULL;
605 }
606
607 return NewMPE;
608 }
609
cmsStageAllocCLut16bit(cmsContext ContextID,cmsUInt32Number nGridPoints,cmsUInt32Number inputChan,cmsUInt32Number outputChan,const cmsUInt16Number * Table)610 cmsStage* CMSEXPORT cmsStageAllocCLut16bit(cmsContext ContextID,
611 cmsUInt32Number nGridPoints,
612 cmsUInt32Number inputChan,
613 cmsUInt32Number outputChan,
614 const cmsUInt16Number* Table)
615 {
616 cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
617 int i;
618
619 // Our resulting LUT would be same gridpoints on all dimensions
620 for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
621 Dimensions[i] = nGridPoints;
622
623 return cmsStageAllocCLut16bitGranular(ContextID, Dimensions, inputChan, outputChan, Table);
624 }
625
626
cmsStageAllocCLutFloat(cmsContext ContextID,cmsUInt32Number nGridPoints,cmsUInt32Number inputChan,cmsUInt32Number outputChan,const cmsFloat32Number * Table)627 cmsStage* CMSEXPORT cmsStageAllocCLutFloat(cmsContext ContextID,
628 cmsUInt32Number nGridPoints,
629 cmsUInt32Number inputChan,
630 cmsUInt32Number outputChan,
631 const cmsFloat32Number* Table)
632 {
633 cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
634 int i;
635
636 // Our resulting LUT would be same gridpoints on all dimensions
637 for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
638 Dimensions[i] = nGridPoints;
639
640 return cmsStageAllocCLutFloatGranular(ContextID, Dimensions, inputChan, outputChan, Table);
641 }
642
643
644
cmsStageAllocCLutFloatGranular(cmsContext ContextID,const cmsUInt32Number clutPoints[],cmsUInt32Number inputChan,cmsUInt32Number outputChan,const cmsFloat32Number * Table)645 cmsStage* CMSEXPORT cmsStageAllocCLutFloatGranular(cmsContext ContextID, const cmsUInt32Number clutPoints[], cmsUInt32Number inputChan, cmsUInt32Number outputChan, const cmsFloat32Number* Table)
646 {
647 cmsUInt32Number i, n;
648 _cmsStageCLutData* NewElem;
649 cmsStage* NewMPE;
650
651 _cmsAssert(clutPoints != NULL);
652
653 if (inputChan > MAX_INPUT_DIMENSIONS) {
654 cmsSignalError(ContextID, cmsERROR_RANGE, "Too many input channels (%d channels, max=%d)", inputChan, MAX_INPUT_DIMENSIONS);
655 return NULL;
656 }
657
658 NewMPE = _cmsStageAllocPlaceholder(ContextID, cmsSigCLutElemType, inputChan, outputChan,
659 EvaluateCLUTfloat, CLUTElemDup, CLutElemTypeFree, NULL);
660 if (NewMPE == NULL) return NULL;
661
662
663 NewElem = (_cmsStageCLutData*) _cmsMallocZero(ContextID, sizeof(_cmsStageCLutData));
664 if (NewElem == NULL) {
665 cmsStageFree(NewMPE);
666 return NULL;
667 }
668
669 NewMPE ->Data = (void*) NewElem;
670
671 // There is a potential integer overflow on conputing n and nEntries.
672 NewElem -> nEntries = n = outputChan * CubeSize(clutPoints, inputChan);
673 NewElem -> HasFloatValues = TRUE;
674
675 if (n == 0) {
676 cmsStageFree(NewMPE);
677 return NULL;
678 }
679
680 NewElem ->Tab.TFloat = (cmsFloat32Number*) _cmsCalloc(ContextID, n, sizeof(cmsFloat32Number));
681 if (NewElem ->Tab.TFloat == NULL) {
682 cmsStageFree(NewMPE);
683 return NULL;
684 }
685
686 if (Table != NULL) {
687 for (i=0; i < n; i++) {
688 NewElem ->Tab.TFloat[i] = Table[i];
689 }
690 }
691
692 NewElem ->Params = _cmsComputeInterpParamsEx(ContextID, clutPoints, inputChan, outputChan, NewElem ->Tab.TFloat, CMS_LERP_FLAGS_FLOAT);
693 if (NewElem ->Params == NULL) {
694 cmsStageFree(NewMPE);
695 return NULL;
696 }
697
698 return NewMPE;
699 }
700
701
702 static
IdentitySampler(register const cmsUInt16Number In[],register cmsUInt16Number Out[],register void * Cargo)703 int IdentitySampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void * Cargo)
704 {
705 int nChan = *(int*) Cargo;
706 int i;
707
708 for (i=0; i < nChan; i++)
709 Out[i] = In[i];
710
711 return 1;
712 }
713
714 // Creates an MPE that just copies input to output
_cmsStageAllocIdentityCLut(cmsContext ContextID,cmsUInt32Number nChan)715 cmsStage* _cmsStageAllocIdentityCLut(cmsContext ContextID, cmsUInt32Number nChan)
716 {
717 cmsUInt32Number Dimensions[MAX_INPUT_DIMENSIONS];
718 cmsStage* mpe ;
719 int i;
720
721 for (i=0; i < MAX_INPUT_DIMENSIONS; i++)
722 Dimensions[i] = 2;
723
724 mpe = cmsStageAllocCLut16bitGranular(ContextID, Dimensions, nChan, nChan, NULL);
725 if (mpe == NULL) return NULL;
726
727 if (!cmsStageSampleCLut16bit(mpe, IdentitySampler, &nChan, 0)) {
728 cmsStageFree(mpe);
729 return NULL;
730 }
731
732 mpe ->Implements = cmsSigIdentityElemType;
733 return mpe;
734 }
735
736
737
738 // Quantize a value 0 <= i < MaxSamples to 0..0xffff
_cmsQuantizeVal(cmsFloat64Number i,cmsUInt32Number MaxSamples)739 cmsUInt16Number _cmsQuantizeVal(cmsFloat64Number i, cmsUInt32Number MaxSamples)
740 {
741 cmsFloat64Number x;
742
743 x = ((cmsFloat64Number) i * 65535.) / (cmsFloat64Number) (MaxSamples - 1);
744 return _cmsQuickSaturateWord(x);
745 }
746
747
748 // This routine does a sweep on whole input space, and calls its callback
749 // function on knots. returns TRUE if all ok, FALSE otherwise.
cmsStageSampleCLut16bit(cmsStage * mpe,cmsSAMPLER16 Sampler,void * Cargo,cmsUInt32Number dwFlags)750 cmsBool CMSEXPORT cmsStageSampleCLut16bit(cmsStage* mpe, cmsSAMPLER16 Sampler, void * Cargo, cmsUInt32Number dwFlags)
751 {
752 int i, t, index, rest;
753 cmsUInt32Number nTotalPoints;
754 cmsUInt32Number nInputs, nOutputs;
755 cmsUInt32Number* nSamples;
756 cmsUInt16Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS];
757 _cmsStageCLutData* clut;
758
759 if (mpe == NULL) return FALSE;
760
761 clut = (_cmsStageCLutData*) mpe->Data;
762
763 if (clut == NULL) return FALSE;
764
765 nSamples = clut->Params ->nSamples;
766 nInputs = clut->Params ->nInputs;
767 nOutputs = clut->Params ->nOutputs;
768
769 if (nInputs <= 0) return FALSE;
770 if (nOutputs <= 0) return FALSE;
771 if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE;
772 if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE;
773
774 memset(In, 0, sizeof(In));
775 memset(Out, 0, sizeof(Out));
776
777 nTotalPoints = CubeSize(nSamples, nInputs);
778 if (nTotalPoints == 0) return FALSE;
779
780 index = 0;
781 for (i = 0; i < (int) nTotalPoints; i++) {
782
783 rest = i;
784 for (t = (int)nInputs - 1; t >= 0; --t) {
785
786 cmsUInt32Number Colorant = rest % nSamples[t];
787
788 rest /= nSamples[t];
789
790 In[t] = _cmsQuantizeVal(Colorant, nSamples[t]);
791 }
792
793 if (clut ->Tab.T != NULL) {
794 for (t = 0; t < (int)nOutputs; t++)
795 Out[t] = clut->Tab.T[index + t];
796 }
797
798 if (!Sampler(In, Out, Cargo))
799 return FALSE;
800
801 if (!(dwFlags & SAMPLER_INSPECT)) {
802
803 if (clut ->Tab.T != NULL) {
804 for (t=0; t < (int) nOutputs; t++)
805 clut->Tab.T[index + t] = Out[t];
806 }
807 }
808
809 index += nOutputs;
810 }
811
812 return TRUE;
813 }
814
815 // Same as anterior, but for floating point
cmsStageSampleCLutFloat(cmsStage * mpe,cmsSAMPLERFLOAT Sampler,void * Cargo,cmsUInt32Number dwFlags)816 cmsBool CMSEXPORT cmsStageSampleCLutFloat(cmsStage* mpe, cmsSAMPLERFLOAT Sampler, void * Cargo, cmsUInt32Number dwFlags)
817 {
818 int i, t, index, rest;
819 cmsUInt32Number nTotalPoints;
820 cmsUInt32Number nInputs, nOutputs;
821 cmsUInt32Number* nSamples;
822 cmsFloat32Number In[MAX_INPUT_DIMENSIONS+1], Out[MAX_STAGE_CHANNELS];
823 _cmsStageCLutData* clut = (_cmsStageCLutData*) mpe->Data;
824
825 nSamples = clut->Params ->nSamples;
826 nInputs = clut->Params ->nInputs;
827 nOutputs = clut->Params ->nOutputs;
828
829 if (nInputs <= 0) return FALSE;
830 if (nOutputs <= 0) return FALSE;
831 if (nInputs > MAX_INPUT_DIMENSIONS) return FALSE;
832 if (nOutputs >= MAX_STAGE_CHANNELS) return FALSE;
833
834 nTotalPoints = CubeSize(nSamples, nInputs);
835 if (nTotalPoints == 0) return FALSE;
836
837 index = 0;
838 for (i = 0; i < (int)nTotalPoints; i++) {
839
840 rest = i;
841 for (t = (int) nInputs-1; t >=0; --t) {
842
843 cmsUInt32Number Colorant = rest % nSamples[t];
844
845 rest /= nSamples[t];
846
847 In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, nSamples[t]) / 65535.0);
848 }
849
850 if (clut ->Tab.TFloat != NULL) {
851 for (t=0; t < (int) nOutputs; t++)
852 Out[t] = clut->Tab.TFloat[index + t];
853 }
854
855 if (!Sampler(In, Out, Cargo))
856 return FALSE;
857
858 if (!(dwFlags & SAMPLER_INSPECT)) {
859
860 if (clut ->Tab.TFloat != NULL) {
861 for (t=0; t < (int) nOutputs; t++)
862 clut->Tab.TFloat[index + t] = Out[t];
863 }
864 }
865
866 index += nOutputs;
867 }
868
869 return TRUE;
870 }
871
872
873
874 // This routine does a sweep on whole input space, and calls its callback
875 // function on knots. returns TRUE if all ok, FALSE otherwise.
cmsSliceSpace16(cmsUInt32Number nInputs,const cmsUInt32Number clutPoints[],cmsSAMPLER16 Sampler,void * Cargo)876 cmsBool CMSEXPORT cmsSliceSpace16(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
877 cmsSAMPLER16 Sampler, void * Cargo)
878 {
879 int i, t, rest;
880 cmsUInt32Number nTotalPoints;
881 cmsUInt16Number In[cmsMAXCHANNELS];
882
883 if (nInputs >= cmsMAXCHANNELS) return FALSE;
884
885 nTotalPoints = CubeSize(clutPoints, nInputs);
886 if (nTotalPoints == 0) return FALSE;
887
888 for (i = 0; i < (int) nTotalPoints; i++) {
889
890 rest = i;
891 for (t = (int) nInputs-1; t >=0; --t) {
892
893 cmsUInt32Number Colorant = rest % clutPoints[t];
894
895 rest /= clutPoints[t];
896 In[t] = _cmsQuantizeVal(Colorant, clutPoints[t]);
897
898 }
899
900 if (!Sampler(In, NULL, Cargo))
901 return FALSE;
902 }
903
904 return TRUE;
905 }
906
cmsSliceSpaceFloat(cmsUInt32Number nInputs,const cmsUInt32Number clutPoints[],cmsSAMPLERFLOAT Sampler,void * Cargo)907 cmsInt32Number CMSEXPORT cmsSliceSpaceFloat(cmsUInt32Number nInputs, const cmsUInt32Number clutPoints[],
908 cmsSAMPLERFLOAT Sampler, void * Cargo)
909 {
910 int i, t, rest;
911 cmsUInt32Number nTotalPoints;
912 cmsFloat32Number In[cmsMAXCHANNELS];
913
914 if (nInputs >= cmsMAXCHANNELS) return FALSE;
915
916 nTotalPoints = CubeSize(clutPoints, nInputs);
917 if (nTotalPoints == 0) return FALSE;
918
919 for (i = 0; i < (int) nTotalPoints; i++) {
920
921 rest = i;
922 for (t = (int) nInputs-1; t >=0; --t) {
923
924 cmsUInt32Number Colorant = rest % clutPoints[t];
925
926 rest /= clutPoints[t];
927 In[t] = (cmsFloat32Number) (_cmsQuantizeVal(Colorant, clutPoints[t]) / 65535.0);
928
929 }
930
931 if (!Sampler(In, NULL, Cargo))
932 return FALSE;
933 }
934
935 return TRUE;
936 }
937
938 // ********************************************************************************
939 // Type cmsSigLab2XYZElemType
940 // ********************************************************************************
941
942
943 static
EvaluateLab2XYZ(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)944 void EvaluateLab2XYZ(const cmsFloat32Number In[],
945 cmsFloat32Number Out[],
946 const cmsStage *mpe)
947 {
948 cmsCIELab Lab;
949 cmsCIEXYZ XYZ;
950 const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ;
951
952 // V4 rules
953 Lab.L = In[0] * 100.0;
954 Lab.a = In[1] * 255.0 - 128.0;
955 Lab.b = In[2] * 255.0 - 128.0;
956
957 cmsLab2XYZ(NULL, &XYZ, &Lab);
958
959 // From XYZ, range 0..19997 to 0..1.0, note that 1.99997 comes from 0xffff
960 // encoded as 1.15 fixed point, so 1 + (32767.0 / 32768.0)
961
962 Out[0] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.X / XYZadj);
963 Out[1] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Y / XYZadj);
964 Out[2] = (cmsFloat32Number) ((cmsFloat64Number) XYZ.Z / XYZadj);
965 return;
966
967 cmsUNUSED_PARAMETER(mpe);
968 }
969
970
971 // No dup or free routines needed, as the structure has no pointers in it.
_cmsStageAllocLab2XYZ(cmsContext ContextID)972 cmsStage* _cmsStageAllocLab2XYZ(cmsContext ContextID)
973 {
974 return _cmsStageAllocPlaceholder(ContextID, cmsSigLab2XYZElemType, 3, 3, EvaluateLab2XYZ, NULL, NULL, NULL);
975 }
976
977 // ********************************************************************************
978
979 // v2 L=100 is supposed to be placed on 0xFF00. There is no reasonable
980 // number of gridpoints that would make exact match. However, a prelinearization
981 // of 258 entries, would map 0xFF00 exactly on entry 257, and this is good to avoid scum dot.
982 // Almost all what we need but unfortunately, the rest of entries should be scaled by
983 // (255*257/256) and this is not exact.
984
_cmsStageAllocLabV2ToV4curves(cmsContext ContextID)985 cmsStage* _cmsStageAllocLabV2ToV4curves(cmsContext ContextID)
986 {
987 cmsStage* mpe;
988 cmsToneCurve* LabTable[3];
989 int i, j;
990
991 LabTable[0] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
992 LabTable[1] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
993 LabTable[2] = cmsBuildTabulatedToneCurve16(ContextID, 258, NULL);
994
995 for (j=0; j < 3; j++) {
996
997 if (LabTable[j] == NULL) {
998 cmsFreeToneCurveTriple(LabTable);
999 return NULL;
1000 }
1001
1002 // We need to map * (0xffff / 0xff00), that's same as (257 / 256)
1003 // So we can use 258-entry tables to do the trick (i / 257) * (255 * 257) * (257 / 256);
1004 for (i=0; i < 257; i++) {
1005
1006 LabTable[j]->Table16[i] = (cmsUInt16Number) ((i * 0xffff + 0x80) >> 8);
1007 }
1008
1009 LabTable[j] ->Table16[257] = 0xffff;
1010 }
1011
1012 mpe = cmsStageAllocToneCurves(ContextID, 3, LabTable);
1013 cmsFreeToneCurveTriple(LabTable);
1014
1015 if (mpe == NULL) return NULL;
1016 mpe ->Implements = cmsSigLabV2toV4;
1017 return mpe;
1018 }
1019
1020 // ********************************************************************************
1021
1022 // Matrix-based conversion, which is more accurate, but slower and cannot properly be saved in devicelink profiles
_cmsStageAllocLabV2ToV4(cmsContext ContextID)1023 cmsStage* _cmsStageAllocLabV2ToV4(cmsContext ContextID)
1024 {
1025 static const cmsFloat64Number V2ToV4[] = { 65535.0/65280.0, 0, 0,
1026 0, 65535.0/65280.0, 0,
1027 0, 0, 65535.0/65280.0
1028 };
1029
1030 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V2ToV4, NULL);
1031
1032 if (mpe == NULL) return mpe;
1033 mpe ->Implements = cmsSigLabV2toV4;
1034 return mpe;
1035 }
1036
1037
1038 // Reverse direction
_cmsStageAllocLabV4ToV2(cmsContext ContextID)1039 cmsStage* _cmsStageAllocLabV4ToV2(cmsContext ContextID)
1040 {
1041 static const cmsFloat64Number V4ToV2[] = { 65280.0/65535.0, 0, 0,
1042 0, 65280.0/65535.0, 0,
1043 0, 0, 65280.0/65535.0
1044 };
1045
1046 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, V4ToV2, NULL);
1047
1048 if (mpe == NULL) return mpe;
1049 mpe ->Implements = cmsSigLabV4toV2;
1050 return mpe;
1051 }
1052
1053
1054 // To Lab to float. Note that the MPE gives numbers in normal Lab range
1055 // and we need 0..1.0 range for the formatters
1056 // L* : 0...100 => 0...1.0 (L* / 100)
1057 // ab* : -128..+127 to 0..1 ((ab* + 128) / 255)
1058
_cmsStageNormalizeFromLabFloat(cmsContext ContextID)1059 cmsStage* _cmsStageNormalizeFromLabFloat(cmsContext ContextID)
1060 {
1061 static const cmsFloat64Number a1[] = {
1062 1.0/100.0, 0, 0,
1063 0, 1.0/255.0, 0,
1064 0, 0, 1.0/255.0
1065 };
1066
1067 static const cmsFloat64Number o1[] = {
1068 0,
1069 128.0/255.0,
1070 128.0/255.0
1071 };
1072
1073 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1);
1074
1075 if (mpe == NULL) return mpe;
1076 mpe ->Implements = cmsSigLab2FloatPCS;
1077 return mpe;
1078 }
1079
1080 // Fom XYZ to floating point PCS
_cmsStageNormalizeFromXyzFloat(cmsContext ContextID)1081 cmsStage* _cmsStageNormalizeFromXyzFloat(cmsContext ContextID)
1082 {
1083 #define n (32768.0/65535.0)
1084 static const cmsFloat64Number a1[] = {
1085 n, 0, 0,
1086 0, n, 0,
1087 0, 0, n
1088 };
1089 #undef n
1090
1091 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL);
1092
1093 if (mpe == NULL) return mpe;
1094 mpe ->Implements = cmsSigXYZ2FloatPCS;
1095 return mpe;
1096 }
1097
_cmsStageNormalizeToLabFloat(cmsContext ContextID)1098 cmsStage* _cmsStageNormalizeToLabFloat(cmsContext ContextID)
1099 {
1100 static const cmsFloat64Number a1[] = {
1101 100.0, 0, 0,
1102 0, 255.0, 0,
1103 0, 0, 255.0
1104 };
1105
1106 static const cmsFloat64Number o1[] = {
1107 0,
1108 -128.0,
1109 -128.0
1110 };
1111
1112 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, o1);
1113 if (mpe == NULL) return mpe;
1114 mpe ->Implements = cmsSigFloatPCS2Lab;
1115 return mpe;
1116 }
1117
_cmsStageNormalizeToXyzFloat(cmsContext ContextID)1118 cmsStage* _cmsStageNormalizeToXyzFloat(cmsContext ContextID)
1119 {
1120 #define n (65535.0/32768.0)
1121
1122 static const cmsFloat64Number a1[] = {
1123 n, 0, 0,
1124 0, n, 0,
1125 0, 0, n
1126 };
1127 #undef n
1128
1129 cmsStage *mpe = cmsStageAllocMatrix(ContextID, 3, 3, a1, NULL);
1130 if (mpe == NULL) return mpe;
1131 mpe ->Implements = cmsSigFloatPCS2XYZ;
1132 return mpe;
1133 }
1134
1135 // Clips values smaller than zero
1136 static
Clipper(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)1137 void Clipper(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
1138 {
1139 cmsUInt32Number i;
1140 for (i = 0; i < mpe->InputChannels; i++) {
1141
1142 cmsFloat32Number n = In[i];
1143 Out[i] = n < 0 ? 0 : n;
1144 }
1145 }
1146
_cmsStageClipNegatives(cmsContext ContextID,cmsUInt32Number nChannels)1147 cmsStage* _cmsStageClipNegatives(cmsContext ContextID, cmsUInt32Number nChannels)
1148 {
1149 return _cmsStageAllocPlaceholder(ContextID, cmsSigClipNegativesElemType,
1150 nChannels, nChannels, Clipper, NULL, NULL, NULL);
1151 }
1152
1153 // ********************************************************************************
1154 // Type cmsSigXYZ2LabElemType
1155 // ********************************************************************************
1156
1157 static
EvaluateXYZ2Lab(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsStage * mpe)1158 void EvaluateXYZ2Lab(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsStage *mpe)
1159 {
1160 cmsCIELab Lab;
1161 cmsCIEXYZ XYZ;
1162 const cmsFloat64Number XYZadj = MAX_ENCODEABLE_XYZ;
1163
1164 // From 0..1.0 to XYZ
1165
1166 XYZ.X = In[0] * XYZadj;
1167 XYZ.Y = In[1] * XYZadj;
1168 XYZ.Z = In[2] * XYZadj;
1169
1170 cmsXYZ2Lab(NULL, &Lab, &XYZ);
1171
1172 // From V4 Lab to 0..1.0
1173
1174 Out[0] = (cmsFloat32Number) (Lab.L / 100.0);
1175 Out[1] = (cmsFloat32Number) ((Lab.a + 128.0) / 255.0);
1176 Out[2] = (cmsFloat32Number) ((Lab.b + 128.0) / 255.0);
1177 return;
1178
1179 cmsUNUSED_PARAMETER(mpe);
1180 }
1181
_cmsStageAllocXYZ2Lab(cmsContext ContextID)1182 cmsStage* _cmsStageAllocXYZ2Lab(cmsContext ContextID)
1183 {
1184 return _cmsStageAllocPlaceholder(ContextID, cmsSigXYZ2LabElemType, 3, 3, EvaluateXYZ2Lab, NULL, NULL, NULL);
1185
1186 }
1187
1188 // ********************************************************************************
1189
1190 // For v4, S-Shaped curves are placed in a/b axis to increase resolution near gray
1191
_cmsStageAllocLabPrelin(cmsContext ContextID)1192 cmsStage* _cmsStageAllocLabPrelin(cmsContext ContextID)
1193 {
1194 cmsToneCurve* LabTable[3];
1195 cmsFloat64Number Params[1] = {2.4} ;
1196
1197 LabTable[0] = cmsBuildGamma(ContextID, 1.0);
1198 LabTable[1] = cmsBuildParametricToneCurve(ContextID, 108, Params);
1199 LabTable[2] = cmsBuildParametricToneCurve(ContextID, 108, Params);
1200
1201 return cmsStageAllocToneCurves(ContextID, 3, LabTable);
1202 }
1203
1204
1205 // Free a single MPE
cmsStageFree(cmsStage * mpe)1206 void CMSEXPORT cmsStageFree(cmsStage* mpe)
1207 {
1208 if (mpe ->FreePtr)
1209 mpe ->FreePtr(mpe);
1210
1211 _cmsFree(mpe ->ContextID, mpe);
1212 }
1213
1214
cmsStageInputChannels(const cmsStage * mpe)1215 cmsUInt32Number CMSEXPORT cmsStageInputChannels(const cmsStage* mpe)
1216 {
1217 return mpe ->InputChannels;
1218 }
1219
cmsStageOutputChannels(const cmsStage * mpe)1220 cmsUInt32Number CMSEXPORT cmsStageOutputChannels(const cmsStage* mpe)
1221 {
1222 return mpe ->OutputChannels;
1223 }
1224
cmsStageType(const cmsStage * mpe)1225 cmsStageSignature CMSEXPORT cmsStageType(const cmsStage* mpe)
1226 {
1227 return mpe -> Type;
1228 }
1229
cmsStageData(const cmsStage * mpe)1230 void* CMSEXPORT cmsStageData(const cmsStage* mpe)
1231 {
1232 return mpe -> Data;
1233 }
1234
cmsStageNext(const cmsStage * mpe)1235 cmsStage* CMSEXPORT cmsStageNext(const cmsStage* mpe)
1236 {
1237 return mpe -> Next;
1238 }
1239
1240
1241 // Duplicates an MPE
cmsStageDup(cmsStage * mpe)1242 cmsStage* CMSEXPORT cmsStageDup(cmsStage* mpe)
1243 {
1244 cmsStage* NewMPE;
1245
1246 if (mpe == NULL) return NULL;
1247 NewMPE = _cmsStageAllocPlaceholder(mpe ->ContextID,
1248 mpe ->Type,
1249 mpe ->InputChannels,
1250 mpe ->OutputChannels,
1251 mpe ->EvalPtr,
1252 mpe ->DupElemPtr,
1253 mpe ->FreePtr,
1254 NULL);
1255 if (NewMPE == NULL) return NULL;
1256
1257 NewMPE ->Implements = mpe ->Implements;
1258
1259 if (mpe ->DupElemPtr) {
1260
1261 NewMPE ->Data = mpe ->DupElemPtr(mpe);
1262
1263 if (NewMPE->Data == NULL) {
1264
1265 cmsStageFree(NewMPE);
1266 return NULL;
1267 }
1268
1269 } else {
1270
1271 NewMPE ->Data = NULL;
1272 }
1273
1274 return NewMPE;
1275 }
1276
1277
1278 // ***********************************************************************************************************
1279
1280 // This function sets up the channel count
1281 static
BlessLUT(cmsPipeline * lut)1282 cmsBool BlessLUT(cmsPipeline* lut)
1283 {
1284 // We can set the input/output channels only if we have elements.
1285 if (lut ->Elements != NULL) {
1286
1287 cmsStage* prev;
1288 cmsStage* next;
1289 cmsStage* First;
1290 cmsStage* Last;
1291
1292 First = cmsPipelineGetPtrToFirstStage(lut);
1293 Last = cmsPipelineGetPtrToLastStage(lut);
1294
1295 if (First == NULL || Last == NULL) return FALSE;
1296
1297 lut->InputChannels = First->InputChannels;
1298 lut->OutputChannels = Last->OutputChannels;
1299
1300 // Check chain consistency
1301 prev = First;
1302 next = prev->Next;
1303
1304 while (next != NULL)
1305 {
1306 if (next->InputChannels != prev->OutputChannels)
1307 return FALSE;
1308
1309 next = next->Next;
1310 prev = prev->Next;
1311 }
1312 }
1313
1314 return TRUE;
1315 }
1316
1317
1318 // Default to evaluate the LUT on 16 bit-basis. Precision is retained.
1319 static
_LUTeval16(register const cmsUInt16Number In[],register cmsUInt16Number Out[],register const void * D)1320 void _LUTeval16(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register const void* D)
1321 {
1322 cmsPipeline* lut = (cmsPipeline*) D;
1323 cmsStage *mpe;
1324 cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS] = {0.0f};
1325 int Phase = 0, NextPhase;
1326
1327 From16ToFloat(In, &Storage[Phase][0], lut ->InputChannels);
1328
1329 for (mpe = lut ->Elements;
1330 mpe != NULL;
1331 mpe = mpe ->Next) {
1332
1333 NextPhase = Phase ^ 1;
1334 mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1335 Phase = NextPhase;
1336 }
1337
1338
1339 FromFloatTo16(&Storage[Phase][0], Out, lut ->OutputChannels);
1340 }
1341
1342
1343
1344 // Does evaluate the LUT on cmsFloat32Number-basis.
1345 static
_LUTevalFloat(register const cmsFloat32Number In[],register cmsFloat32Number Out[],const void * D)1346 void _LUTevalFloat(register const cmsFloat32Number In[], register cmsFloat32Number Out[], const void* D)
1347 {
1348 cmsPipeline* lut = (cmsPipeline*) D;
1349 cmsStage *mpe;
1350 cmsFloat32Number Storage[2][MAX_STAGE_CHANNELS] = {0.0f};
1351 int Phase = 0, NextPhase;
1352
1353 memmove(&Storage[Phase][0], In, lut ->InputChannels * sizeof(cmsFloat32Number));
1354
1355 for (mpe = lut ->Elements;
1356 mpe != NULL;
1357 mpe = mpe ->Next) {
1358
1359 NextPhase = Phase ^ 1;
1360 mpe ->EvalPtr(&Storage[Phase][0], &Storage[NextPhase][0], mpe);
1361 Phase = NextPhase;
1362 }
1363
1364 memmove(Out, &Storage[Phase][0], lut ->OutputChannels * sizeof(cmsFloat32Number));
1365 }
1366
1367
1368 // LUT Creation & Destruction
cmsPipelineAlloc(cmsContext ContextID,cmsUInt32Number InputChannels,cmsUInt32Number OutputChannels)1369 cmsPipeline* CMSEXPORT cmsPipelineAlloc(cmsContext ContextID, cmsUInt32Number InputChannels, cmsUInt32Number OutputChannels)
1370 {
1371 cmsPipeline* NewLUT;
1372
1373 // A value of zero in channels is allowed as placeholder
1374 if (InputChannels >= cmsMAXCHANNELS ||
1375 OutputChannels >= cmsMAXCHANNELS) return NULL;
1376
1377 NewLUT = (cmsPipeline*) _cmsMallocZero(ContextID, sizeof(cmsPipeline));
1378 if (NewLUT == NULL) return NULL;
1379
1380 NewLUT -> InputChannels = InputChannels;
1381 NewLUT -> OutputChannels = OutputChannels;
1382
1383 NewLUT ->Eval16Fn = _LUTeval16;
1384 NewLUT ->EvalFloatFn = _LUTevalFloat;
1385 NewLUT ->DupDataFn = NULL;
1386 NewLUT ->FreeDataFn = NULL;
1387 NewLUT ->Data = NewLUT;
1388 NewLUT ->ContextID = ContextID;
1389
1390 if (!BlessLUT(NewLUT))
1391 {
1392 _cmsFree(ContextID, NewLUT);
1393 return NULL;
1394 }
1395
1396 return NewLUT;
1397 }
1398
cmsGetPipelineContextID(const cmsPipeline * lut)1399 cmsContext CMSEXPORT cmsGetPipelineContextID(const cmsPipeline* lut)
1400 {
1401 _cmsAssert(lut != NULL);
1402 return lut ->ContextID;
1403 }
1404
cmsPipelineInputChannels(const cmsPipeline * lut)1405 cmsUInt32Number CMSEXPORT cmsPipelineInputChannels(const cmsPipeline* lut)
1406 {
1407 _cmsAssert(lut != NULL);
1408 return lut ->InputChannels;
1409 }
1410
cmsPipelineOutputChannels(const cmsPipeline * lut)1411 cmsUInt32Number CMSEXPORT cmsPipelineOutputChannels(const cmsPipeline* lut)
1412 {
1413 _cmsAssert(lut != NULL);
1414 return lut ->OutputChannels;
1415 }
1416
1417 // Free a profile elements LUT
cmsPipelineFree(cmsPipeline * lut)1418 void CMSEXPORT cmsPipelineFree(cmsPipeline* lut)
1419 {
1420 cmsStage *mpe, *Next;
1421
1422 if (lut == NULL) return;
1423
1424 for (mpe = lut ->Elements;
1425 mpe != NULL;
1426 mpe = Next) {
1427
1428 Next = mpe ->Next;
1429 cmsStageFree(mpe);
1430 }
1431
1432 if (lut ->FreeDataFn) lut ->FreeDataFn(lut ->ContextID, lut ->Data);
1433
1434 _cmsFree(lut ->ContextID, lut);
1435 }
1436
1437
1438 // Default to evaluate the LUT on 16 bit-basis.
cmsPipelineEval16(const cmsUInt16Number In[],cmsUInt16Number Out[],const cmsPipeline * lut)1439 void CMSEXPORT cmsPipelineEval16(const cmsUInt16Number In[], cmsUInt16Number Out[], const cmsPipeline* lut)
1440 {
1441 _cmsAssert(lut != NULL);
1442 lut ->Eval16Fn(In, Out, lut->Data);
1443 }
1444
1445
1446 // Does evaluate the LUT on cmsFloat32Number-basis.
cmsPipelineEvalFloat(const cmsFloat32Number In[],cmsFloat32Number Out[],const cmsPipeline * lut)1447 void CMSEXPORT cmsPipelineEvalFloat(const cmsFloat32Number In[], cmsFloat32Number Out[], const cmsPipeline* lut)
1448 {
1449 _cmsAssert(lut != NULL);
1450 lut ->EvalFloatFn(In, Out, lut);
1451 }
1452
1453
1454
1455 // Duplicates a LUT
cmsPipelineDup(const cmsPipeline * lut)1456 cmsPipeline* CMSEXPORT cmsPipelineDup(const cmsPipeline* lut)
1457 {
1458 cmsPipeline* NewLUT;
1459 cmsStage *NewMPE, *Anterior = NULL, *mpe;
1460 cmsBool First = TRUE;
1461
1462 if (lut == NULL) return NULL;
1463
1464 NewLUT = cmsPipelineAlloc(lut ->ContextID, lut ->InputChannels, lut ->OutputChannels);
1465 if (NewLUT == NULL) return NULL;
1466
1467 for (mpe = lut ->Elements;
1468 mpe != NULL;
1469 mpe = mpe ->Next) {
1470
1471 NewMPE = cmsStageDup(mpe);
1472
1473 if (NewMPE == NULL) {
1474 cmsPipelineFree(NewLUT);
1475 return NULL;
1476 }
1477
1478 if (First) {
1479 NewLUT ->Elements = NewMPE;
1480 First = FALSE;
1481 }
1482 else {
1483 if (Anterior != NULL)
1484 Anterior ->Next = NewMPE;
1485 }
1486
1487 Anterior = NewMPE;
1488 }
1489
1490 NewLUT ->Eval16Fn = lut ->Eval16Fn;
1491 NewLUT ->EvalFloatFn = lut ->EvalFloatFn;
1492 NewLUT ->DupDataFn = lut ->DupDataFn;
1493 NewLUT ->FreeDataFn = lut ->FreeDataFn;
1494
1495 if (NewLUT ->DupDataFn != NULL)
1496 NewLUT ->Data = NewLUT ->DupDataFn(lut ->ContextID, lut->Data);
1497
1498
1499 NewLUT ->SaveAs8Bits = lut ->SaveAs8Bits;
1500
1501 if (!BlessLUT(NewLUT))
1502 {
1503 _cmsFree(lut->ContextID, NewLUT);
1504 return NULL;
1505 }
1506
1507 return NewLUT;
1508 }
1509
1510
cmsPipelineInsertStage(cmsPipeline * lut,cmsStageLoc loc,cmsStage * mpe)1511 int CMSEXPORT cmsPipelineInsertStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage* mpe)
1512 {
1513 cmsStage* Anterior = NULL, *pt;
1514
1515 if (lut == NULL || mpe == NULL)
1516 return FALSE;
1517
1518 switch (loc) {
1519
1520 case cmsAT_BEGIN:
1521 mpe ->Next = lut ->Elements;
1522 lut ->Elements = mpe;
1523 break;
1524
1525 case cmsAT_END:
1526
1527 if (lut ->Elements == NULL)
1528 lut ->Elements = mpe;
1529 else {
1530
1531 for (pt = lut ->Elements;
1532 pt != NULL;
1533 pt = pt -> Next) Anterior = pt;
1534
1535 Anterior ->Next = mpe;
1536 mpe ->Next = NULL;
1537 }
1538 break;
1539 default:;
1540 return FALSE;
1541 }
1542
1543 return BlessLUT(lut);
1544 }
1545
1546 // Unlink an element and return the pointer to it
cmsPipelineUnlinkStage(cmsPipeline * lut,cmsStageLoc loc,cmsStage ** mpe)1547 void CMSEXPORT cmsPipelineUnlinkStage(cmsPipeline* lut, cmsStageLoc loc, cmsStage** mpe)
1548 {
1549 cmsStage *Anterior, *pt, *Last;
1550 cmsStage *Unlinked = NULL;
1551
1552
1553 // If empty LUT, there is nothing to remove
1554 if (lut ->Elements == NULL) {
1555 if (mpe) *mpe = NULL;
1556 return;
1557 }
1558
1559 // On depending on the strategy...
1560 switch (loc) {
1561
1562 case cmsAT_BEGIN:
1563 {
1564 cmsStage* elem = lut ->Elements;
1565
1566 lut ->Elements = elem -> Next;
1567 elem ->Next = NULL;
1568 Unlinked = elem;
1569
1570 }
1571 break;
1572
1573 case cmsAT_END:
1574 Anterior = Last = NULL;
1575 for (pt = lut ->Elements;
1576 pt != NULL;
1577 pt = pt -> Next) {
1578 Anterior = Last;
1579 Last = pt;
1580 }
1581
1582 Unlinked = Last; // Next already points to NULL
1583
1584 // Truncate the chain
1585 if (Anterior)
1586 Anterior ->Next = NULL;
1587 else
1588 lut ->Elements = NULL;
1589 break;
1590 default:;
1591 }
1592
1593 if (mpe)
1594 *mpe = Unlinked;
1595 else
1596 cmsStageFree(Unlinked);
1597
1598 // May fail, but we ignore it
1599 BlessLUT(lut);
1600 }
1601
1602
1603 // Concatenate two LUT into a new single one
cmsPipelineCat(cmsPipeline * l1,const cmsPipeline * l2)1604 cmsBool CMSEXPORT cmsPipelineCat(cmsPipeline* l1, const cmsPipeline* l2)
1605 {
1606 cmsStage* mpe;
1607
1608 // If both LUTS does not have elements, we need to inherit
1609 // the number of channels
1610 if (l1 ->Elements == NULL && l2 ->Elements == NULL) {
1611 l1 ->InputChannels = l2 ->InputChannels;
1612 l1 ->OutputChannels = l2 ->OutputChannels;
1613 }
1614
1615 // Cat second
1616 for (mpe = l2 ->Elements;
1617 mpe != NULL;
1618 mpe = mpe ->Next) {
1619
1620 // We have to dup each element
1621 if (!cmsPipelineInsertStage(l1, cmsAT_END, cmsStageDup(mpe)))
1622 return FALSE;
1623 }
1624
1625 return BlessLUT(l1);
1626 }
1627
1628
cmsPipelineSetSaveAs8bitsFlag(cmsPipeline * lut,cmsBool On)1629 cmsBool CMSEXPORT cmsPipelineSetSaveAs8bitsFlag(cmsPipeline* lut, cmsBool On)
1630 {
1631 cmsBool Anterior = lut ->SaveAs8Bits;
1632
1633 lut ->SaveAs8Bits = On;
1634 return Anterior;
1635 }
1636
1637
cmsPipelineGetPtrToFirstStage(const cmsPipeline * lut)1638 cmsStage* CMSEXPORT cmsPipelineGetPtrToFirstStage(const cmsPipeline* lut)
1639 {
1640 return lut ->Elements;
1641 }
1642
cmsPipelineGetPtrToLastStage(const cmsPipeline * lut)1643 cmsStage* CMSEXPORT cmsPipelineGetPtrToLastStage(const cmsPipeline* lut)
1644 {
1645 cmsStage *mpe, *Anterior = NULL;
1646
1647 for (mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next)
1648 Anterior = mpe;
1649
1650 return Anterior;
1651 }
1652
cmsPipelineStageCount(const cmsPipeline * lut)1653 cmsUInt32Number CMSEXPORT cmsPipelineStageCount(const cmsPipeline* lut)
1654 {
1655 cmsStage *mpe;
1656 cmsUInt32Number n;
1657
1658 for (n=0, mpe = lut ->Elements; mpe != NULL; mpe = mpe ->Next)
1659 n++;
1660
1661 return n;
1662 }
1663
1664 // This function may be used to set the optional evaluator and a block of private data. If private data is being used, an optional
1665 // duplicator and free functions should also be specified in order to duplicate the LUT construct. Use NULL to inhibit such functionality.
_cmsPipelineSetOptimizationParameters(cmsPipeline * Lut,_cmsOPTeval16Fn Eval16,void * PrivateData,_cmsFreeUserDataFn FreePrivateDataFn,_cmsDupUserDataFn DupPrivateDataFn)1666 void CMSEXPORT _cmsPipelineSetOptimizationParameters(cmsPipeline* Lut,
1667 _cmsOPTeval16Fn Eval16,
1668 void* PrivateData,
1669 _cmsFreeUserDataFn FreePrivateDataFn,
1670 _cmsDupUserDataFn DupPrivateDataFn)
1671 {
1672
1673 Lut ->Eval16Fn = Eval16;
1674 Lut ->DupDataFn = DupPrivateDataFn;
1675 Lut ->FreeDataFn = FreePrivateDataFn;
1676 Lut ->Data = PrivateData;
1677 }
1678
1679
1680 // ----------------------------------------------------------- Reverse interpolation
1681 // Here's how it goes. The derivative Df(x) of the function f is the linear
1682 // transformation that best approximates f near the point x. It can be represented
1683 // by a matrix A whose entries are the partial derivatives of the components of f
1684 // with respect to all the coordinates. This is know as the Jacobian
1685 //
1686 // The best linear approximation to f is given by the matrix equation:
1687 //
1688 // y-y0 = A (x-x0)
1689 //
1690 // So, if x0 is a good "guess" for the zero of f, then solving for the zero of this
1691 // linear approximation will give a "better guess" for the zero of f. Thus let y=0,
1692 // and since y0=f(x0) one can solve the above equation for x. This leads to the
1693 // Newton's method formula:
1694 //
1695 // xn+1 = xn - A-1 f(xn)
1696 //
1697 // where xn+1 denotes the (n+1)-st guess, obtained from the n-th guess xn in the
1698 // fashion described above. Iterating this will give better and better approximations
1699 // if you have a "good enough" initial guess.
1700
1701
1702 #define JACOBIAN_EPSILON 0.001f
1703 #define INVERSION_MAX_ITERATIONS 30
1704
1705 // Increment with reflexion on boundary
1706 static
IncDelta(cmsFloat32Number * Val)1707 void IncDelta(cmsFloat32Number *Val)
1708 {
1709 if (*Val < (1.0 - JACOBIAN_EPSILON))
1710
1711 *Val += JACOBIAN_EPSILON;
1712
1713 else
1714 *Val -= JACOBIAN_EPSILON;
1715
1716 }
1717
1718
1719
1720 // Euclidean distance between two vectors of n elements each one
1721 static
EuclideanDistance(cmsFloat32Number a[],cmsFloat32Number b[],int n)1722 cmsFloat32Number EuclideanDistance(cmsFloat32Number a[], cmsFloat32Number b[], int n)
1723 {
1724 cmsFloat32Number sum = 0;
1725 int i;
1726
1727 for (i=0; i < n; i++) {
1728 cmsFloat32Number dif = b[i] - a[i];
1729 sum += dif * dif;
1730 }
1731
1732 return sqrtf(sum);
1733 }
1734
1735
1736 // Evaluate a LUT in reverse direction. It only searches on 3->3 LUT. Uses Newton method
1737 //
1738 // x1 <- x - [J(x)]^-1 * f(x)
1739 //
1740 // lut: The LUT on where to do the search
1741 // Target: LabK, 3 values of Lab plus destination K which is fixed
1742 // Result: The obtained CMYK
1743 // Hint: Location where begin the search
1744
cmsPipelineEvalReverseFloat(cmsFloat32Number Target[],cmsFloat32Number Result[],cmsFloat32Number Hint[],const cmsPipeline * lut)1745 cmsBool CMSEXPORT cmsPipelineEvalReverseFloat(cmsFloat32Number Target[],
1746 cmsFloat32Number Result[],
1747 cmsFloat32Number Hint[],
1748 const cmsPipeline* lut)
1749 {
1750 cmsUInt32Number i, j;
1751 cmsFloat64Number error, LastError = 1E20;
1752 cmsFloat32Number fx[4], x[4], xd[4], fxd[4];
1753 cmsVEC3 tmp, tmp2;
1754 cmsMAT3 Jacobian;
1755
1756 // Only 3->3 and 4->3 are supported
1757 if (lut ->InputChannels != 3 && lut ->InputChannels != 4) return FALSE;
1758 if (lut ->OutputChannels != 3) return FALSE;
1759
1760 // Take the hint as starting point if specified
1761 if (Hint == NULL) {
1762
1763 // Begin at any point, we choose 1/3 of CMY axis
1764 x[0] = x[1] = x[2] = 0.3f;
1765 }
1766 else {
1767
1768 // Only copy 3 channels from hint...
1769 for (j=0; j < 3; j++)
1770 x[j] = Hint[j];
1771 }
1772
1773 // If Lut is 4-dimensions, then grab target[3], which is fixed
1774 if (lut ->InputChannels == 4) {
1775 x[3] = Target[3];
1776 }
1777 else x[3] = 0; // To keep lint happy
1778
1779
1780 // Iterate
1781 for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) {
1782
1783 // Get beginning fx
1784 cmsPipelineEvalFloat(x, fx, lut);
1785
1786 // Compute error
1787 error = EuclideanDistance(fx, Target, 3);
1788
1789 // If not convergent, return last safe value
1790 if (error >= LastError)
1791 break;
1792
1793 // Keep latest values
1794 LastError = error;
1795 for (j=0; j < lut ->InputChannels; j++)
1796 Result[j] = x[j];
1797
1798 // Found an exact match?
1799 if (error <= 0)
1800 break;
1801
1802 // Obtain slope (the Jacobian)
1803 for (j = 0; j < 3; j++) {
1804
1805 xd[0] = x[0];
1806 xd[1] = x[1];
1807 xd[2] = x[2];
1808 xd[3] = x[3]; // Keep fixed channel
1809
1810 IncDelta(&xd[j]);
1811
1812 cmsPipelineEvalFloat(xd, fxd, lut);
1813
1814 Jacobian.v[0].n[j] = ((fxd[0] - fx[0]) / JACOBIAN_EPSILON);
1815 Jacobian.v[1].n[j] = ((fxd[1] - fx[1]) / JACOBIAN_EPSILON);
1816 Jacobian.v[2].n[j] = ((fxd[2] - fx[2]) / JACOBIAN_EPSILON);
1817 }
1818
1819 // Solve system
1820 tmp2.n[0] = fx[0] - Target[0];
1821 tmp2.n[1] = fx[1] - Target[1];
1822 tmp2.n[2] = fx[2] - Target[2];
1823
1824 if (!_cmsMAT3solve(&tmp, &Jacobian, &tmp2))
1825 return FALSE;
1826
1827 // Move our guess
1828 x[0] -= (cmsFloat32Number) tmp.n[0];
1829 x[1] -= (cmsFloat32Number) tmp.n[1];
1830 x[2] -= (cmsFloat32Number) tmp.n[2];
1831
1832 // Some clipping....
1833 for (j=0; j < 3; j++) {
1834 if (x[j] < 0) x[j] = 0;
1835 else
1836 if (x[j] > 1.0) x[j] = 1.0;
1837 }
1838 }
1839
1840 return TRUE;
1841 }
1842
1843
1844