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