Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/lcms2/src/cmsgamma.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 //--------------------------------------------------------------------------------- | |
| 2 // | |
| 3 // Little Color Management System | |
| 4 // Copyright (c) 1998-2023 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 #include "lcms2_internal.h" | |
| 27 | |
| 28 // Tone curves are powerful constructs that can contain curves specified in diverse ways. | |
| 29 // The curve is stored in segments, where each segment can be sampled or specified by parameters. | |
| 30 // a 16.bit simplification of the *whole* curve is kept for optimization purposes. For float operation, | |
| 31 // each segment is evaluated separately. Plug-ins may be used to define new parametric schemes, | |
| 32 // each plug-in may define up to MAX_TYPES_IN_LCMS_PLUGIN functions types. For defining a function, | |
| 33 // the plug-in should provide the type id, how many parameters each type has, and a pointer to | |
| 34 // a procedure that evaluates the function. In the case of reverse evaluation, the evaluator will | |
| 35 // be called with the type id as a negative value, and a sampled version of the reversed curve | |
| 36 // will be built. | |
| 37 | |
| 38 // ----------------------------------------------------------------- Implementation | |
| 39 // Maxim number of nodes | |
| 40 #define MAX_NODES_IN_CURVE 4097 | |
| 41 #define MINUS_INF (-1E22F) | |
| 42 #define PLUS_INF (+1E22F) | |
| 43 | |
| 44 // The list of supported parametric curves | |
| 45 typedef struct _cmsParametricCurvesCollection_st { | |
| 46 | |
| 47 cmsUInt32Number nFunctions; // Number of supported functions in this chunk | |
| 48 cmsInt32Number FunctionTypes[MAX_TYPES_IN_LCMS_PLUGIN]; // The identification types | |
| 49 cmsUInt32Number ParameterCount[MAX_TYPES_IN_LCMS_PLUGIN]; // Number of parameters for each function | |
| 50 | |
| 51 cmsParametricCurveEvaluator Evaluator; // The evaluator | |
| 52 | |
| 53 struct _cmsParametricCurvesCollection_st* Next; // Next in list | |
| 54 | |
| 55 } _cmsParametricCurvesCollection; | |
| 56 | |
| 57 // This is the default (built-in) evaluator | |
| 58 static cmsFloat64Number DefaultEvalParametricFn(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R); | |
| 59 | |
| 60 // The built-in list | |
| 61 static _cmsParametricCurvesCollection DefaultCurves = { | |
| 62 10, // # of curve types | |
| 63 { 1, 2, 3, 4, 5, 6, 7, 8, 108, 109 }, // Parametric curve ID | |
| 64 { 1, 3, 4, 5, 7, 4, 5, 5, 1, 1 }, // Parameters by type | |
| 65 DefaultEvalParametricFn, // Evaluator | |
| 66 NULL // Next in chain | |
| 67 }; | |
| 68 | |
| 69 // Duplicates the zone of memory used by the plug-in in the new context | |
| 70 static | |
| 71 void DupPluginCurvesList(struct _cmsContext_struct* ctx, | |
| 72 const struct _cmsContext_struct* src) | |
| 73 { | |
| 74 _cmsCurvesPluginChunkType newHead = { NULL }; | |
| 75 _cmsParametricCurvesCollection* entry; | |
| 76 _cmsParametricCurvesCollection* Anterior = NULL; | |
| 77 _cmsCurvesPluginChunkType* head = (_cmsCurvesPluginChunkType*) src->chunks[CurvesPlugin]; | |
| 78 | |
| 79 _cmsAssert(head != NULL); | |
| 80 | |
| 81 // Walk the list copying all nodes | |
| 82 for (entry = head->ParametricCurves; | |
| 83 entry != NULL; | |
| 84 entry = entry ->Next) { | |
| 85 | |
| 86 _cmsParametricCurvesCollection *newEntry = ( _cmsParametricCurvesCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsParametricCurvesCollection)); | |
| 87 | |
| 88 if (newEntry == NULL) | |
| 89 return; | |
| 90 | |
| 91 // We want to keep the linked list order, so this is a little bit tricky | |
| 92 newEntry -> Next = NULL; | |
| 93 if (Anterior) | |
| 94 Anterior -> Next = newEntry; | |
| 95 | |
| 96 Anterior = newEntry; | |
| 97 | |
| 98 if (newHead.ParametricCurves == NULL) | |
| 99 newHead.ParametricCurves = newEntry; | |
| 100 } | |
| 101 | |
| 102 ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsCurvesPluginChunkType)); | |
| 103 } | |
| 104 | |
| 105 // The allocator have to follow the chain | |
| 106 void _cmsAllocCurvesPluginChunk(struct _cmsContext_struct* ctx, | |
| 107 const struct _cmsContext_struct* src) | |
| 108 { | |
| 109 _cmsAssert(ctx != NULL); | |
| 110 | |
| 111 if (src != NULL) { | |
| 112 | |
| 113 // Copy all linked list | |
| 114 DupPluginCurvesList(ctx, src); | |
| 115 } | |
| 116 else { | |
| 117 static _cmsCurvesPluginChunkType CurvesPluginChunk = { NULL }; | |
| 118 ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx ->MemPool, &CurvesPluginChunk, sizeof(_cmsCurvesPluginChunkType)); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 | |
| 123 // The linked list head | |
| 124 _cmsCurvesPluginChunkType _cmsCurvesPluginChunk = { NULL }; | |
| 125 | |
| 126 // As a way to install new parametric curves | |
| 127 cmsBool _cmsRegisterParametricCurvesPlugin(cmsContext ContextID, cmsPluginBase* Data) | |
| 128 { | |
| 129 _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin); | |
| 130 cmsPluginParametricCurves* Plugin = (cmsPluginParametricCurves*) Data; | |
| 131 _cmsParametricCurvesCollection* fl; | |
| 132 | |
| 133 if (Data == NULL) { | |
| 134 | |
| 135 ctx -> ParametricCurves = NULL; | |
| 136 return TRUE; | |
| 137 } | |
| 138 | |
| 139 fl = (_cmsParametricCurvesCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsParametricCurvesCollection)); | |
| 140 if (fl == NULL) return FALSE; | |
| 141 | |
| 142 // Copy the parameters | |
| 143 fl ->Evaluator = Plugin ->Evaluator; | |
| 144 fl ->nFunctions = Plugin ->nFunctions; | |
| 145 | |
| 146 // Make sure no mem overwrites | |
| 147 if (fl ->nFunctions > MAX_TYPES_IN_LCMS_PLUGIN) | |
| 148 fl ->nFunctions = MAX_TYPES_IN_LCMS_PLUGIN; | |
| 149 | |
| 150 // Copy the data | |
| 151 memmove(fl->FunctionTypes, Plugin ->FunctionTypes, fl->nFunctions * sizeof(cmsUInt32Number)); | |
| 152 memmove(fl->ParameterCount, Plugin ->ParameterCount, fl->nFunctions * sizeof(cmsUInt32Number)); | |
| 153 | |
| 154 // Keep linked list | |
| 155 fl ->Next = ctx->ParametricCurves; | |
| 156 ctx->ParametricCurves = fl; | |
| 157 | |
| 158 // All is ok | |
| 159 return TRUE; | |
| 160 } | |
| 161 | |
| 162 | |
| 163 // Search in type list, return position or -1 if not found | |
| 164 static | |
| 165 int IsInSet(int Type, _cmsParametricCurvesCollection* c) | |
| 166 { | |
| 167 int i; | |
| 168 | |
| 169 for (i=0; i < (int) c ->nFunctions; i++) | |
| 170 if (abs(Type) == c ->FunctionTypes[i]) return i; | |
| 171 | |
| 172 return -1; | |
| 173 } | |
| 174 | |
| 175 | |
| 176 // Search for the collection which contains a specific type | |
| 177 static | |
| 178 _cmsParametricCurvesCollection *GetParametricCurveByType(cmsContext ContextID, int Type, int* index) | |
| 179 { | |
| 180 _cmsParametricCurvesCollection* c; | |
| 181 int Position; | |
| 182 _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin); | |
| 183 | |
| 184 for (c = ctx->ParametricCurves; c != NULL; c = c ->Next) { | |
| 185 | |
| 186 Position = IsInSet(Type, c); | |
| 187 | |
| 188 if (Position != -1) { | |
| 189 if (index != NULL) | |
| 190 *index = Position; | |
| 191 return c; | |
| 192 } | |
| 193 } | |
| 194 // If none found, revert for defaults | |
| 195 for (c = &DefaultCurves; c != NULL; c = c ->Next) { | |
| 196 | |
| 197 Position = IsInSet(Type, c); | |
| 198 | |
| 199 if (Position != -1) { | |
| 200 if (index != NULL) | |
| 201 *index = Position; | |
| 202 return c; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 return NULL; | |
| 207 } | |
| 208 | |
| 209 // Low level allocate, which takes care of memory details. nEntries may be zero, and in this case | |
| 210 // no optimization curve is computed. nSegments may also be zero in the inverse case, where only the | |
| 211 // optimization curve is given. Both features simultaneously is an error | |
| 212 static | |
| 213 cmsToneCurve* AllocateToneCurveStruct(cmsContext ContextID, cmsUInt32Number nEntries, | |
| 214 cmsUInt32Number nSegments, const cmsCurveSegment* Segments, | |
| 215 const cmsUInt16Number* Values) | |
| 216 { | |
| 217 cmsToneCurve* p; | |
| 218 cmsUInt32Number i; | |
| 219 | |
| 220 // We allow huge tables, which are then restricted for smoothing operations | |
| 221 if (nEntries > 65530) { | |
| 222 cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't create tone curve of more than 65530 entries"); | |
| 223 return NULL; | |
| 224 } | |
| 225 | |
| 226 if (nEntries == 0 && nSegments == 0) { | |
| 227 cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't create tone curve with zero segments and no table"); | |
| 228 return NULL; | |
| 229 } | |
| 230 | |
| 231 // Allocate all required pointers, etc. | |
| 232 p = (cmsToneCurve*) _cmsMallocZero(ContextID, sizeof(cmsToneCurve)); | |
| 233 if (!p) return NULL; | |
| 234 | |
| 235 // In this case, there are no segments | |
| 236 if (nSegments == 0) { | |
| 237 p ->Segments = NULL; | |
| 238 p ->Evals = NULL; | |
| 239 } | |
| 240 else { | |
| 241 p ->Segments = (cmsCurveSegment*) _cmsCalloc(ContextID, nSegments, sizeof(cmsCurveSegment)); | |
| 242 if (p ->Segments == NULL) goto Error; | |
| 243 | |
| 244 p ->Evals = (cmsParametricCurveEvaluator*) _cmsCalloc(ContextID, nSegments, sizeof(cmsParametricCurveEvaluator)); | |
| 245 if (p ->Evals == NULL) goto Error; | |
| 246 } | |
| 247 | |
| 248 p -> nSegments = nSegments; | |
| 249 | |
| 250 // This 16-bit table contains a limited precision representation of the whole curve and is kept for | |
| 251 // increasing xput on certain operations. | |
| 252 if (nEntries == 0) { | |
| 253 p ->Table16 = NULL; | |
| 254 } | |
| 255 else { | |
| 256 p ->Table16 = (cmsUInt16Number*) _cmsCalloc(ContextID, nEntries, sizeof(cmsUInt16Number)); | |
| 257 if (p ->Table16 == NULL) goto Error; | |
| 258 } | |
| 259 | |
| 260 p -> nEntries = nEntries; | |
| 261 | |
| 262 // Initialize members if requested | |
| 263 if (Values != NULL && (nEntries > 0)) { | |
| 264 | |
| 265 for (i=0; i < nEntries; i++) | |
| 266 p ->Table16[i] = Values[i]; | |
| 267 } | |
| 268 | |
| 269 // Initialize the segments stuff. The evaluator for each segment is located and a pointer to it | |
| 270 // is placed in advance to maximize performance. | |
| 271 if (Segments != NULL && (nSegments > 0)) { | |
| 272 | |
| 273 _cmsParametricCurvesCollection *c; | |
| 274 | |
| 275 p ->SegInterp = (cmsInterpParams**) _cmsCalloc(ContextID, nSegments, sizeof(cmsInterpParams*)); | |
| 276 if (p ->SegInterp == NULL) goto Error; | |
| 277 | |
| 278 for (i=0; i < nSegments; i++) { | |
| 279 | |
| 280 // Type 0 is a special marker for table-based curves | |
| 281 if (Segments[i].Type == 0) | |
| 282 p ->SegInterp[i] = _cmsComputeInterpParams(ContextID, Segments[i].nGridPoints, 1, 1, NULL, CMS_LERP_FLAGS_FLOAT); | |
| 283 | |
| 284 memmove(&p ->Segments[i], &Segments[i], sizeof(cmsCurveSegment)); | |
| 285 | |
| 286 if (Segments[i].Type == 0 && Segments[i].SampledPoints != NULL) | |
| 287 p ->Segments[i].SampledPoints = (cmsFloat32Number*) _cmsDupMem(ContextID, Segments[i].SampledPoints, sizeof(cmsFloat32Number) * Segments[i].nGridPoints); | |
| 288 else | |
| 289 p ->Segments[i].SampledPoints = NULL; | |
| 290 | |
| 291 | |
| 292 c = GetParametricCurveByType(ContextID, Segments[i].Type, NULL); | |
| 293 if (c != NULL) | |
| 294 p ->Evals[i] = c ->Evaluator; | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 p ->InterpParams = _cmsComputeInterpParams(ContextID, p ->nEntries, 1, 1, p->Table16, CMS_LERP_FLAGS_16BITS); | |
| 299 if (p->InterpParams != NULL) | |
| 300 return p; | |
| 301 | |
| 302 Error: | |
| 303 for (i=0; i < nSegments; i++) { | |
| 304 if (p ->Segments && p ->Segments[i].SampledPoints) _cmsFree(ContextID, p ->Segments[i].SampledPoints); | |
| 305 if (p ->SegInterp && p ->SegInterp[i]) _cmsFree(ContextID, p ->SegInterp[i]); | |
| 306 } | |
| 307 if (p -> SegInterp) _cmsFree(ContextID, p -> SegInterp); | |
| 308 if (p -> Segments) _cmsFree(ContextID, p -> Segments); | |
| 309 if (p -> Evals) _cmsFree(ContextID, p -> Evals); | |
| 310 if (p ->Table16) _cmsFree(ContextID, p ->Table16); | |
| 311 _cmsFree(ContextID, p); | |
| 312 return NULL; | |
| 313 } | |
| 314 | |
| 315 | |
| 316 // Generates a sigmoidal function with desired steepness. | |
| 317 cmsINLINE double sigmoid_base(double k, double t) | |
| 318 { | |
| 319 return (1.0 / (1.0 + exp(-k * t))) - 0.5; | |
| 320 } | |
| 321 | |
| 322 cmsINLINE double inverted_sigmoid_base(double k, double t) | |
| 323 { | |
| 324 return -log((1.0 / (t + 0.5)) - 1.0) / k; | |
| 325 } | |
| 326 | |
| 327 cmsINLINE double sigmoid_factory(double k, double t) | |
| 328 { | |
| 329 double correction = 0.5 / sigmoid_base(k, 1); | |
| 330 | |
| 331 return correction * sigmoid_base(k, 2.0 * t - 1.0) + 0.5; | |
| 332 } | |
| 333 | |
| 334 cmsINLINE double inverse_sigmoid_factory(double k, double t) | |
| 335 { | |
| 336 double correction = 0.5 / sigmoid_base(k, 1); | |
| 337 | |
| 338 return (inverted_sigmoid_base(k, (t - 0.5) / correction) + 1.0) / 2.0; | |
| 339 } | |
| 340 | |
| 341 | |
| 342 // Parametric Fn using floating point | |
| 343 static | |
| 344 cmsFloat64Number DefaultEvalParametricFn(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R) | |
| 345 { | |
| 346 cmsFloat64Number e, Val, disc; | |
| 347 cmsUNUSED_PARAMETER(ContextID); | |
| 348 | |
| 349 switch (Type) { | |
| 350 | |
| 351 // X = Y ^ Gamma | |
| 352 case 1: | |
| 353 if (R < 0) { | |
| 354 | |
| 355 if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE) | |
| 356 Val = R; | |
| 357 else | |
| 358 Val = 0; | |
| 359 } | |
| 360 else | |
| 361 Val = pow(R, Params[0]); | |
| 362 break; | |
| 363 | |
| 364 // Type 1 Reversed: X = Y ^1/gamma | |
| 365 case -1: | |
| 366 if (R < 0) { | |
| 367 | |
| 368 if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE) | |
| 369 Val = R; | |
| 370 else | |
| 371 Val = 0; | |
| 372 } | |
| 373 else | |
| 374 { | |
| 375 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE) | |
| 376 Val = PLUS_INF; | |
| 377 else | |
| 378 Val = pow(R, 1 / Params[0]); | |
| 379 } | |
| 380 break; | |
| 381 | |
| 382 // CIE 122-1966 | |
| 383 // Y = (aX + b)^Gamma | X >= -b/a | |
| 384 // Y = 0 | else | |
| 385 case 2: | |
| 386 { | |
| 387 | |
| 388 if (fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 389 { | |
| 390 Val = 0; | |
| 391 } | |
| 392 else | |
| 393 { | |
| 394 disc = -Params[2] / Params[1]; | |
| 395 | |
| 396 if (R >= disc) { | |
| 397 | |
| 398 e = Params[1] * R + Params[2]; | |
| 399 | |
| 400 if (e > 0) | |
| 401 Val = pow(e, Params[0]); | |
| 402 else | |
| 403 Val = 0; | |
| 404 } | |
| 405 else | |
| 406 Val = 0; | |
| 407 } | |
| 408 } | |
| 409 break; | |
| 410 | |
| 411 // Type 2 Reversed | |
| 412 // X = (Y ^1/g - b) / a | |
| 413 case -2: | |
| 414 { | |
| 415 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 416 fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 417 { | |
| 418 Val = 0; | |
| 419 } | |
| 420 else | |
| 421 { | |
| 422 if (R < 0) | |
| 423 Val = 0; | |
| 424 else | |
| 425 Val = (pow(R, 1.0 / Params[0]) - Params[2]) / Params[1]; | |
| 426 | |
| 427 if (Val < 0) | |
| 428 Val = 0; | |
| 429 } | |
| 430 } | |
| 431 break; | |
| 432 | |
| 433 | |
| 434 // IEC 61966-3 | |
| 435 // Y = (aX + b)^Gamma + c | X <= -b/a | |
| 436 // Y = c | else | |
| 437 case 3: | |
| 438 { | |
| 439 if (fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 440 { | |
| 441 Val = 0; | |
| 442 } | |
| 443 else | |
| 444 { | |
| 445 disc = -Params[2] / Params[1]; | |
| 446 if (disc < 0) | |
| 447 disc = 0; | |
| 448 | |
| 449 if (R >= disc) { | |
| 450 | |
| 451 e = Params[1] * R + Params[2]; | |
| 452 | |
| 453 if (e > 0) | |
| 454 Val = pow(e, Params[0]) + Params[3]; | |
| 455 else | |
| 456 Val = 0; | |
| 457 } | |
| 458 else | |
| 459 Val = Params[3]; | |
| 460 } | |
| 461 } | |
| 462 break; | |
| 463 | |
| 464 | |
| 465 // Type 3 reversed | |
| 466 // X=((Y-c)^1/g - b)/a | (Y>=c) | |
| 467 // X=-b/a | (Y<c) | |
| 468 case -3: | |
| 469 { | |
| 470 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 471 fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 472 { | |
| 473 Val = 0; | |
| 474 } | |
| 475 else | |
| 476 { | |
| 477 if (R >= Params[3]) { | |
| 478 | |
| 479 e = R - Params[3]; | |
| 480 | |
| 481 if (e > 0) | |
| 482 Val = (pow(e, 1 / Params[0]) - Params[2]) / Params[1]; | |
| 483 else | |
| 484 Val = 0; | |
| 485 } | |
| 486 else { | |
| 487 Val = -Params[2] / Params[1]; | |
| 488 } | |
| 489 } | |
| 490 } | |
| 491 break; | |
| 492 | |
| 493 | |
| 494 // IEC 61966-2.1 (sRGB) | |
| 495 // Y = (aX + b)^Gamma | X >= d | |
| 496 // Y = cX | X < d | |
| 497 case 4: | |
| 498 if (R >= Params[4]) { | |
| 499 | |
| 500 e = Params[1]*R + Params[2]; | |
| 501 | |
| 502 if (e > 0) | |
| 503 Val = pow(e, Params[0]); | |
| 504 else | |
| 505 Val = 0; | |
| 506 } | |
| 507 else | |
| 508 Val = R * Params[3]; | |
| 509 break; | |
| 510 | |
| 511 // Type 4 reversed | |
| 512 // X=((Y^1/g-b)/a) | Y >= (ad+b)^g | |
| 513 // X=Y/c | Y< (ad+b)^g | |
| 514 case -4: | |
| 515 { | |
| 516 | |
| 517 e = Params[1] * Params[4] + Params[2]; | |
| 518 if (e < 0) | |
| 519 disc = 0; | |
| 520 else | |
| 521 disc = pow(e, Params[0]); | |
| 522 | |
| 523 if (R >= disc) { | |
| 524 | |
| 525 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 526 fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 527 | |
| 528 Val = 0; | |
| 529 | |
| 530 else | |
| 531 Val = (pow(R, 1.0 / Params[0]) - Params[2]) / Params[1]; | |
| 532 } | |
| 533 else { | |
| 534 | |
| 535 if (fabs(Params[3]) < MATRIX_DET_TOLERANCE) | |
| 536 Val = 0; | |
| 537 else | |
| 538 Val = R / Params[3]; | |
| 539 } | |
| 540 | |
| 541 } | |
| 542 break; | |
| 543 | |
| 544 | |
| 545 // Y = (aX + b)^Gamma + e | X >= d | |
| 546 // Y = cX + f | X < d | |
| 547 case 5: | |
| 548 if (R >= Params[4]) { | |
| 549 | |
| 550 e = Params[1]*R + Params[2]; | |
| 551 | |
| 552 if (e > 0) | |
| 553 Val = pow(e, Params[0]) + Params[5]; | |
| 554 else | |
| 555 Val = Params[5]; | |
| 556 } | |
| 557 else | |
| 558 Val = R*Params[3] + Params[6]; | |
| 559 break; | |
| 560 | |
| 561 | |
| 562 // Reversed type 5 | |
| 563 // X=((Y-e)1/g-b)/a | Y >=(ad+b)^g+e), cd+f | |
| 564 // X=(Y-f)/c | else | |
| 565 case -5: | |
| 566 { | |
| 567 disc = Params[3] * Params[4] + Params[6]; | |
| 568 if (R >= disc) { | |
| 569 | |
| 570 e = R - Params[5]; | |
| 571 if (e < 0) | |
| 572 Val = 0; | |
| 573 else | |
| 574 { | |
| 575 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 576 fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 577 | |
| 578 Val = 0; | |
| 579 else | |
| 580 Val = (pow(e, 1.0 / Params[0]) - Params[2]) / Params[1]; | |
| 581 } | |
| 582 } | |
| 583 else { | |
| 584 if (fabs(Params[3]) < MATRIX_DET_TOLERANCE) | |
| 585 Val = 0; | |
| 586 else | |
| 587 Val = (R - Params[6]) / Params[3]; | |
| 588 } | |
| 589 | |
| 590 } | |
| 591 break; | |
| 592 | |
| 593 | |
| 594 // Types 6,7,8 comes from segmented curves as described in ICCSpecRevision_02_11_06_Float.pdf | |
| 595 // Type 6 is basically identical to type 5 without d | |
| 596 | |
| 597 // Y = (a * X + b) ^ Gamma + c | |
| 598 case 6: | |
| 599 e = Params[1]*R + Params[2]; | |
| 600 | |
| 601 // On gamma 1.0, don't clamp | |
| 602 if (Params[0] == 1.0) { | |
| 603 Val = e + Params[3]; | |
| 604 } | |
| 605 else { | |
| 606 if (e < 0) | |
| 607 Val = Params[3]; | |
| 608 else | |
| 609 Val = pow(e, Params[0]) + Params[3]; | |
| 610 } | |
| 611 break; | |
| 612 | |
| 613 // ((Y - c) ^1/Gamma - b) / a | |
| 614 case -6: | |
| 615 { | |
| 616 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 617 fabs(Params[1]) < MATRIX_DET_TOLERANCE) | |
| 618 { | |
| 619 Val = 0; | |
| 620 } | |
| 621 else | |
| 622 { | |
| 623 e = R - Params[3]; | |
| 624 if (e < 0) | |
| 625 Val = 0; | |
| 626 else | |
| 627 Val = (pow(e, 1.0 / Params[0]) - Params[2]) / Params[1]; | |
| 628 } | |
| 629 } | |
| 630 break; | |
| 631 | |
| 632 | |
| 633 // Y = a * log (b * X^Gamma + c) + d | |
| 634 case 7: | |
| 635 | |
| 636 e = Params[2] * pow(R, Params[0]) + Params[3]; | |
| 637 if (e <= 0) | |
| 638 Val = Params[4]; | |
| 639 else | |
| 640 Val = Params[1]*log10(e) + Params[4]; | |
| 641 break; | |
| 642 | |
| 643 // (Y - d) / a = log(b * X ^Gamma + c) | |
| 644 // pow(10, (Y-d) / a) = b * X ^Gamma + c | |
| 645 // pow((pow(10, (Y-d) / a) - c) / b, 1/g) = X | |
| 646 case -7: | |
| 647 { | |
| 648 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 649 fabs(Params[1]) < MATRIX_DET_TOLERANCE || | |
| 650 fabs(Params[2]) < MATRIX_DET_TOLERANCE) | |
| 651 { | |
| 652 Val = 0; | |
| 653 } | |
| 654 else | |
| 655 { | |
| 656 Val = pow((pow(10.0, (R - Params[4]) / Params[1]) - Params[3]) / Params[2], 1.0 / Params[0]); | |
| 657 } | |
| 658 } | |
| 659 break; | |
| 660 | |
| 661 | |
| 662 //Y = a * b^(c*X+d) + e | |
| 663 case 8: | |
| 664 Val = (Params[0] * pow(Params[1], Params[2] * R + Params[3]) + Params[4]); | |
| 665 break; | |
| 666 | |
| 667 | |
| 668 // Y = (log((y-e) / a) / log(b) - d ) / c | |
| 669 // a=0, b=1, c=2, d=3, e=4, | |
| 670 case -8: | |
| 671 | |
| 672 disc = R - Params[4]; | |
| 673 if (disc < 0) Val = 0; | |
| 674 else | |
| 675 { | |
| 676 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE || | |
| 677 fabs(Params[2]) < MATRIX_DET_TOLERANCE) | |
| 678 { | |
| 679 Val = 0; | |
| 680 } | |
| 681 else | |
| 682 { | |
| 683 Val = (log(disc / Params[0]) / log(Params[1]) - Params[3]) / Params[2]; | |
| 684 } | |
| 685 } | |
| 686 break; | |
| 687 | |
| 688 | |
| 689 // S-Shaped: (1 - (1-x)^1/g)^1/g | |
| 690 case 108: | |
| 691 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE) | |
| 692 Val = 0; | |
| 693 else | |
| 694 Val = pow(1.0 - pow(1 - R, 1/Params[0]), 1/Params[0]); | |
| 695 break; | |
| 696 | |
| 697 // y = (1 - (1-x)^1/g)^1/g | |
| 698 // y^g = (1 - (1-x)^1/g) | |
| 699 // 1 - y^g = (1-x)^1/g | |
| 700 // (1 - y^g)^g = 1 - x | |
| 701 // 1 - (1 - y^g)^g | |
| 702 case -108: | |
| 703 Val = 1 - pow(1 - pow(R, Params[0]), Params[0]); | |
| 704 break; | |
| 705 | |
| 706 // Sigmoidals | |
| 707 case 109: | |
| 708 Val = sigmoid_factory(Params[0], R); | |
| 709 break; | |
| 710 | |
| 711 case -109: | |
| 712 Val = inverse_sigmoid_factory(Params[0], R); | |
| 713 break; | |
| 714 | |
| 715 default: | |
| 716 // Unsupported parametric curve. Should never reach here | |
| 717 return 0; | |
| 718 } | |
| 719 | |
| 720 return Val; | |
| 721 } | |
| 722 | |
| 723 // Evaluate a segmented function for a single value. Return -Inf if no valid segment found . | |
| 724 // If fn type is 0, perform an interpolation on the table | |
| 725 static | |
| 726 cmsFloat64Number EvalSegmentedFn(cmsContext ContextID, const cmsToneCurve *g, cmsFloat64Number R) | |
| 727 { | |
| 728 int i; | |
| 729 cmsFloat32Number Out32; | |
| 730 cmsFloat64Number Out; | |
| 731 | |
| 732 for (i = (int) g->nSegments - 1; i >= 0; --i) { | |
| 733 | |
| 734 // Check for domain | |
| 735 if ((R > g->Segments[i].x0) && (R <= g->Segments[i].x1)) { | |
| 736 | |
| 737 // Type == 0 means segment is sampled | |
| 738 if (g->Segments[i].Type == 0) { | |
| 739 | |
| 740 cmsFloat32Number R1 = (cmsFloat32Number)(R - g->Segments[i].x0) / (g->Segments[i].x1 - g->Segments[i].x0); | |
| 741 | |
| 742 // Setup the table (TODO: clean that) | |
| 743 g->SegInterp[i]->Table = g->Segments[i].SampledPoints; | |
| 744 | |
| 745 g->SegInterp[i]->Interpolation.LerpFloat(ContextID, &R1, &Out32, g->SegInterp[i]); | |
| 746 Out = (cmsFloat64Number) Out32; | |
| 747 | |
| 748 } | |
| 749 else { | |
| 750 Out = g->Evals[i](ContextID, g->Segments[i].Type, g->Segments[i].Params, R); | |
| 751 } | |
| 752 | |
| 753 if (isinf(Out)) | |
| 754 return PLUS_INF; | |
| 755 else | |
| 756 { | |
| 757 if (isinf(-Out)) | |
| 758 return MINUS_INF; | |
| 759 } | |
| 760 | |
| 761 return Out; | |
| 762 } | |
| 763 } | |
| 764 | |
| 765 return MINUS_INF; | |
| 766 } | |
| 767 | |
| 768 // Access to estimated low-res table | |
| 769 cmsUInt32Number CMSEXPORT cmsGetToneCurveEstimatedTableEntries(cmsContext ContextID, const cmsToneCurve* t) | |
| 770 { | |
| 771 cmsUNUSED_PARAMETER(ContextID); | |
| 772 _cmsAssert(t != NULL); | |
| 773 return t ->nEntries; | |
| 774 } | |
| 775 | |
| 776 const cmsUInt16Number* CMSEXPORT cmsGetToneCurveEstimatedTable(cmsContext ContextID, const cmsToneCurve* t) | |
| 777 { | |
| 778 cmsUNUSED_PARAMETER(ContextID); | |
| 779 _cmsAssert(t != NULL); | |
| 780 return t ->Table16; | |
| 781 } | |
| 782 | |
| 783 | |
| 784 // Create an empty gamma curve, by using tables. This specifies only the limited-precision part, and leaves the | |
| 785 // floating point description empty. | |
| 786 cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurve16(cmsContext ContextID, cmsUInt32Number nEntries, const cmsUInt16Number Values[]) | |
| 787 { | |
| 788 return AllocateToneCurveStruct(ContextID, nEntries, 0, NULL, Values); | |
| 789 } | |
| 790 | |
| 791 static | |
| 792 cmsUInt32Number EntriesByGamma(cmsFloat64Number Gamma) | |
| 793 { | |
| 794 if (fabs(Gamma - 1.0) < 0.001) return 2; | |
| 795 return 4096; | |
| 796 } | |
| 797 | |
| 798 | |
| 799 // Create a segmented gamma, fill the table | |
| 800 cmsToneCurve* CMSEXPORT cmsBuildSegmentedToneCurve(cmsContext ContextID, | |
| 801 cmsUInt32Number nSegments, const cmsCurveSegment Segments[]) | |
| 802 { | |
| 803 cmsUInt32Number i; | |
| 804 cmsFloat64Number R, Val; | |
| 805 cmsToneCurve* g; | |
| 806 cmsUInt32Number nGridPoints = 4096; | |
| 807 | |
| 808 _cmsAssert(Segments != NULL); | |
| 809 | |
| 810 // Optimizatin for identity curves. | |
| 811 if (nSegments == 1 && Segments[0].Type == 1) { | |
| 812 | |
| 813 nGridPoints = EntriesByGamma(Segments[0].Params[0]); | |
| 814 } | |
| 815 | |
| 816 g = AllocateToneCurveStruct(ContextID, nGridPoints, nSegments, Segments, NULL); | |
| 817 if (g == NULL) return NULL; | |
| 818 | |
| 819 // Once we have the floating point version, we can approximate a 16 bit table of 4096 entries | |
| 820 // for performance reasons. This table would normally not be used except on 8/16 bits transforms. | |
| 821 for (i = 0; i < nGridPoints; i++) { | |
| 822 | |
| 823 R = (cmsFloat64Number) i / (nGridPoints-1); | |
| 824 | |
| 825 Val = EvalSegmentedFn(ContextID, g, R); | |
| 826 | |
| 827 // Round and saturate | |
| 828 g ->Table16[i] = _cmsQuickSaturateWord(Val * 65535.0); | |
| 829 } | |
| 830 | |
| 831 return g; | |
| 832 } | |
| 833 | |
| 834 // Use a segmented curve to store the floating point table | |
| 835 cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurveFloat(cmsContext ContextID, cmsUInt32Number nEntries, const cmsFloat32Number values[]) | |
| 836 { | |
| 837 cmsCurveSegment Seg[3]; | |
| 838 | |
| 839 // Do some housekeeping | |
| 840 if (nEntries == 0 || values == NULL) | |
| 841 return NULL; | |
| 842 | |
| 843 // A segmented tone curve should have function segments in the first and last positions | |
| 844 // Initialize segmented curve part up to 0 to constant value = samples[0] | |
| 845 Seg[0].x0 = MINUS_INF; | |
| 846 Seg[0].x1 = 0; | |
| 847 Seg[0].Type = 6; | |
| 848 | |
| 849 Seg[0].Params[0] = 1; | |
| 850 Seg[0].Params[1] = 0; | |
| 851 Seg[0].Params[2] = 0; | |
| 852 Seg[0].Params[3] = values[0]; | |
| 853 Seg[0].Params[4] = 0; | |
| 854 | |
| 855 // From zero to 1 | |
| 856 Seg[1].x0 = 0; | |
| 857 Seg[1].x1 = 1.0; | |
| 858 Seg[1].Type = 0; | |
| 859 | |
| 860 Seg[1].nGridPoints = nEntries; | |
| 861 Seg[1].SampledPoints = (cmsFloat32Number*) values; | |
| 862 | |
| 863 // Final segment is constant = lastsample | |
| 864 Seg[2].x0 = 1.0; | |
| 865 Seg[2].x1 = PLUS_INF; | |
| 866 Seg[2].Type = 6; | |
| 867 | |
| 868 Seg[2].Params[0] = 1; | |
| 869 Seg[2].Params[1] = 0; | |
| 870 Seg[2].Params[2] = 0; | |
| 871 Seg[2].Params[3] = values[nEntries-1]; | |
| 872 Seg[2].Params[4] = 0; | |
| 873 | |
| 874 | |
| 875 return cmsBuildSegmentedToneCurve(ContextID, 3, Seg); | |
| 876 } | |
| 877 | |
| 878 // Parametric curves | |
| 879 // | |
| 880 // Parameters goes as: Curve, a, b, c, d, e, f | |
| 881 // Type is the ICC type +1 | |
| 882 // if type is negative, then the curve is analytically inverted | |
| 883 cmsToneCurve* CMSEXPORT cmsBuildParametricToneCurve(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[]) | |
| 884 { | |
| 885 cmsCurveSegment Seg0; | |
| 886 int Pos = 0; | |
| 887 cmsUInt32Number size; | |
| 888 _cmsParametricCurvesCollection* c = GetParametricCurveByType(ContextID, Type, &Pos); | |
| 889 | |
| 890 _cmsAssert(Params != NULL); | |
| 891 | |
| 892 if (c == NULL) { | |
| 893 cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Invalid parametric curve type %d", Type); | |
| 894 return NULL; | |
| 895 } | |
| 896 | |
| 897 memset(&Seg0, 0, sizeof(Seg0)); | |
| 898 | |
| 899 Seg0.x0 = MINUS_INF; | |
| 900 Seg0.x1 = PLUS_INF; | |
| 901 Seg0.Type = Type; | |
| 902 | |
| 903 size = c->ParameterCount[Pos] * sizeof(cmsFloat64Number); | |
| 904 memmove(Seg0.Params, Params, size); | |
| 905 | |
| 906 return cmsBuildSegmentedToneCurve(ContextID, 1, &Seg0); | |
| 907 } | |
| 908 | |
| 909 | |
| 910 | |
| 911 // Build a gamma table based on gamma constant | |
| 912 cmsToneCurve* CMSEXPORT cmsBuildGamma(cmsContext ContextID, cmsFloat64Number Gamma) | |
| 913 { | |
| 914 return cmsBuildParametricToneCurve(ContextID, 1, &Gamma); | |
| 915 } | |
| 916 | |
| 917 | |
| 918 // Free all memory taken by the gamma curve | |
| 919 void CMSEXPORT cmsFreeToneCurve(cmsContext ContextID, cmsToneCurve* Curve) | |
| 920 { | |
| 921 if (Curve == NULL) return; | |
| 922 | |
| 923 _cmsFreeInterpParams(ContextID, Curve ->InterpParams); | |
| 924 | |
| 925 if (Curve -> Table16) | |
| 926 _cmsFree(ContextID, Curve ->Table16); | |
| 927 | |
| 928 if (Curve ->Segments) { | |
| 929 | |
| 930 cmsUInt32Number i; | |
| 931 | |
| 932 for (i=0; i < Curve ->nSegments; i++) { | |
| 933 | |
| 934 if (Curve ->Segments[i].SampledPoints) { | |
| 935 _cmsFree(ContextID, Curve ->Segments[i].SampledPoints); | |
| 936 } | |
| 937 | |
| 938 if (Curve ->SegInterp[i] != 0) | |
| 939 _cmsFreeInterpParams(ContextID, Curve->SegInterp[i]); | |
| 940 } | |
| 941 | |
| 942 _cmsFree(ContextID, Curve ->Segments); | |
| 943 _cmsFree(ContextID, Curve ->SegInterp); | |
| 944 } | |
| 945 | |
| 946 if (Curve -> Evals) | |
| 947 _cmsFree(ContextID, Curve -> Evals); | |
| 948 | |
| 949 _cmsFree(ContextID, Curve); | |
| 950 } | |
| 951 | |
| 952 // Utility function, free 3 gamma tables | |
| 953 void CMSEXPORT cmsFreeToneCurveTriple(cmsContext ContextID, cmsToneCurve* Curve[3]) | |
| 954 { | |
| 955 | |
| 956 _cmsAssert(Curve != NULL); | |
| 957 | |
| 958 if (Curve[0] != NULL) cmsFreeToneCurve(ContextID, Curve[0]); | |
| 959 if (Curve[1] != NULL) cmsFreeToneCurve(ContextID, Curve[1]); | |
| 960 if (Curve[2] != NULL) cmsFreeToneCurve(ContextID, Curve[2]); | |
| 961 | |
| 962 Curve[0] = Curve[1] = Curve[2] = NULL; | |
| 963 } | |
| 964 | |
| 965 | |
| 966 // Duplicate a gamma table | |
| 967 cmsToneCurve* CMSEXPORT cmsDupToneCurve(cmsContext ContextID, const cmsToneCurve* In) | |
| 968 { | |
| 969 if (In == NULL) return NULL; | |
| 970 | |
| 971 return AllocateToneCurveStruct(ContextID, In ->nEntries, In ->nSegments, In ->Segments, In ->Table16); | |
| 972 } | |
| 973 | |
| 974 // Joins two curves for X and Y. Curves should be monotonic. | |
| 975 // We want to get | |
| 976 // | |
| 977 // y = Y^-1(X(t)) | |
| 978 // | |
| 979 cmsToneCurve* CMSEXPORT cmsJoinToneCurve(cmsContext ContextID, | |
| 980 const cmsToneCurve* X, | |
| 981 const cmsToneCurve* Y, cmsUInt32Number nResultingPoints) | |
| 982 { | |
| 983 cmsToneCurve* out = NULL; | |
| 984 cmsToneCurve* Yreversed = NULL; | |
| 985 cmsFloat32Number t, x; | |
| 986 cmsFloat32Number* Res = NULL; | |
| 987 cmsUInt32Number i; | |
| 988 | |
| 989 | |
| 990 _cmsAssert(X != NULL); | |
| 991 _cmsAssert(Y != NULL); | |
| 992 | |
| 993 Yreversed = cmsReverseToneCurveEx(ContextID, nResultingPoints, Y); | |
| 994 if (Yreversed == NULL) goto Error; | |
| 995 | |
| 996 Res = (cmsFloat32Number*) _cmsCalloc(ContextID, nResultingPoints, sizeof(cmsFloat32Number)); | |
| 997 if (Res == NULL) goto Error; | |
| 998 | |
| 999 //Iterate | |
| 1000 for (i=0; i < nResultingPoints; i++) { | |
| 1001 | |
| 1002 t = (cmsFloat32Number) i / (cmsFloat32Number)(nResultingPoints-1); | |
| 1003 x = cmsEvalToneCurveFloat(ContextID, X, t); | |
| 1004 Res[i] = cmsEvalToneCurveFloat(ContextID, Yreversed, x); | |
| 1005 } | |
| 1006 | |
| 1007 // Allocate space for output | |
| 1008 out = cmsBuildTabulatedToneCurveFloat(ContextID, nResultingPoints, Res); | |
| 1009 | |
| 1010 Error: | |
| 1011 | |
| 1012 if (Res != NULL) _cmsFree(ContextID, Res); | |
| 1013 if (Yreversed != NULL) cmsFreeToneCurve(ContextID, Yreversed); | |
| 1014 | |
| 1015 return out; | |
| 1016 } | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 // Get the surrounding nodes. This is tricky on non-monotonic tables | |
| 1021 static | |
| 1022 int GetInterval(cmsFloat64Number In, const cmsUInt16Number LutTable[], const struct _cms_interp_struc* p) | |
| 1023 { | |
| 1024 int i; | |
| 1025 int y0, y1; | |
| 1026 | |
| 1027 // A 1 point table is not allowed | |
| 1028 if (p -> Domain[0] < 1) return -1; | |
| 1029 | |
| 1030 // Let's see if ascending or descending. | |
| 1031 if (LutTable[0] < LutTable[p ->Domain[0]]) { | |
| 1032 | |
| 1033 // Table is overall ascending | |
| 1034 for (i = (int) p->Domain[0] - 1; i >= 0; --i) { | |
| 1035 | |
| 1036 y0 = LutTable[i]; | |
| 1037 y1 = LutTable[i+1]; | |
| 1038 | |
| 1039 if (y0 <= y1) { // Increasing | |
| 1040 if (In >= y0 && In <= y1) return i; | |
| 1041 } | |
| 1042 else | |
| 1043 if (y1 < y0) { // Decreasing | |
| 1044 if (In >= y1 && In <= y0) return i; | |
| 1045 } | |
| 1046 } | |
| 1047 } | |
| 1048 else { | |
| 1049 // Table is overall descending | |
| 1050 for (i=0; i < (int) p -> Domain[0]; i++) { | |
| 1051 | |
| 1052 y0 = LutTable[i]; | |
| 1053 y1 = LutTable[i+1]; | |
| 1054 | |
| 1055 if (y0 <= y1) { // Increasing | |
| 1056 if (In >= y0 && In <= y1) return i; | |
| 1057 } | |
| 1058 else | |
| 1059 if (y1 < y0) { // Decreasing | |
| 1060 if (In >= y1 && In <= y0) return i; | |
| 1061 } | |
| 1062 } | |
| 1063 } | |
| 1064 | |
| 1065 return -1; | |
| 1066 } | |
| 1067 | |
| 1068 // Reverse a gamma table | |
| 1069 cmsToneCurve* CMSEXPORT cmsReverseToneCurveEx(cmsContext ContextID, cmsUInt32Number nResultSamples, const cmsToneCurve* InCurve) | |
| 1070 { | |
| 1071 cmsToneCurve *out; | |
| 1072 cmsFloat64Number a = 0, b = 0, y, x1, y1, x2, y2; | |
| 1073 int i, j; | |
| 1074 int Ascending; | |
| 1075 | |
| 1076 _cmsAssert(InCurve != NULL); | |
| 1077 | |
| 1078 // Try to reverse it analytically whatever possible | |
| 1079 | |
| 1080 if (InCurve ->nSegments == 1 && InCurve ->Segments[0].Type > 0 && | |
| 1081 /* InCurve -> Segments[0].Type <= 5 */ | |
| 1082 GetParametricCurveByType(ContextID, InCurve ->Segments[0].Type, NULL) != NULL) { | |
| 1083 | |
| 1084 return cmsBuildParametricToneCurve(ContextID, | |
| 1085 -(InCurve -> Segments[0].Type), | |
| 1086 InCurve -> Segments[0].Params); | |
| 1087 } | |
| 1088 | |
| 1089 // Nope, reverse the table. | |
| 1090 out = cmsBuildTabulatedToneCurve16(ContextID, nResultSamples, NULL); | |
| 1091 if (out == NULL) | |
| 1092 return NULL; | |
| 1093 | |
| 1094 // We want to know if this is an ascending or descending table | |
| 1095 Ascending = !cmsIsToneCurveDescending(ContextID, InCurve); | |
| 1096 | |
| 1097 // Iterate across Y axis | |
| 1098 for (i=0; i < (int) nResultSamples; i++) { | |
| 1099 | |
| 1100 y = (cmsFloat64Number) i * 65535.0 / (nResultSamples - 1); | |
| 1101 | |
| 1102 // Find interval in which y is within. | |
| 1103 j = GetInterval(y, InCurve->Table16, InCurve->InterpParams); | |
| 1104 if (j >= 0) { | |
| 1105 | |
| 1106 | |
| 1107 // Get limits of interval | |
| 1108 x1 = InCurve ->Table16[j]; | |
| 1109 x2 = InCurve ->Table16[j+1]; | |
| 1110 | |
| 1111 y1 = (cmsFloat64Number) (j * 65535.0) / (InCurve ->nEntries - 1); | |
| 1112 y2 = (cmsFloat64Number) ((j+1) * 65535.0 ) / (InCurve ->nEntries - 1); | |
| 1113 | |
| 1114 // If collapsed, then use any | |
| 1115 if (x1 == x2) { | |
| 1116 | |
| 1117 out ->Table16[i] = _cmsQuickSaturateWord(Ascending ? y2 : y1); | |
| 1118 continue; | |
| 1119 | |
| 1120 } else { | |
| 1121 | |
| 1122 // Interpolate | |
| 1123 a = (y2 - y1) / (x2 - x1); | |
| 1124 b = y2 - a * x2; | |
| 1125 } | |
| 1126 } | |
| 1127 | |
| 1128 out ->Table16[i] = _cmsQuickSaturateWord(a* y + b); | |
| 1129 } | |
| 1130 | |
| 1131 | |
| 1132 return out; | |
| 1133 } | |
| 1134 | |
| 1135 // Reverse a gamma table | |
| 1136 cmsToneCurve* CMSEXPORT cmsReverseToneCurve(cmsContext ContextID, const cmsToneCurve* InGamma) | |
| 1137 { | |
| 1138 _cmsAssert(InGamma != NULL); | |
| 1139 | |
| 1140 return cmsReverseToneCurveEx(ContextID, 4096, InGamma); | |
| 1141 } | |
| 1142 | |
| 1143 // From: Eilers, P.H.C. (1994) Smoothing and interpolation with finite | |
| 1144 // differences. in: Graphic Gems IV, Heckbert, P.S. (ed.), Academic press. | |
| 1145 // | |
| 1146 // Smoothing and interpolation with second differences. | |
| 1147 // | |
| 1148 // Input: weights (w), data (y): vector from 1 to m. | |
| 1149 // Input: smoothing parameter (lambda), length (m). | |
| 1150 // Output: smoothed vector (z): vector from 1 to m. | |
| 1151 | |
| 1152 static | |
| 1153 cmsBool smooth2(cmsContext ContextID, cmsFloat32Number w[], cmsFloat32Number y[], | |
| 1154 cmsFloat32Number z[], cmsFloat32Number lambda, int m) | |
| 1155 { | |
| 1156 int i, i1, i2; | |
| 1157 cmsFloat32Number *c, *d, *e; | |
| 1158 cmsBool st; | |
| 1159 | |
| 1160 | |
| 1161 c = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number)); | |
| 1162 d = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number)); | |
| 1163 e = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number)); | |
| 1164 | |
| 1165 if (c != NULL && d != NULL && e != NULL) { | |
| 1166 | |
| 1167 | |
| 1168 d[1] = w[1] + lambda; | |
| 1169 c[1] = -2 * lambda / d[1]; | |
| 1170 e[1] = lambda /d[1]; | |
| 1171 z[1] = w[1] * y[1]; | |
| 1172 d[2] = w[2] + 5 * lambda - d[1] * c[1] * c[1]; | |
| 1173 c[2] = (-4 * lambda - d[1] * c[1] * e[1]) / d[2]; | |
| 1174 e[2] = lambda / d[2]; | |
| 1175 z[2] = w[2] * y[2] - c[1] * z[1]; | |
| 1176 | |
| 1177 for (i = 3; i < m - 1; i++) { | |
| 1178 i1 = i - 1; i2 = i - 2; | |
| 1179 d[i]= w[i] + 6 * lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2]; | |
| 1180 c[i] = (-4 * lambda -d[i1] * c[i1] * e[i1])/ d[i]; | |
| 1181 e[i] = lambda / d[i]; | |
| 1182 z[i] = w[i] * y[i] - c[i1] * z[i1] - e[i2] * z[i2]; | |
| 1183 } | |
| 1184 | |
| 1185 i1 = m - 2; i2 = m - 3; | |
| 1186 | |
| 1187 d[m - 1] = w[m - 1] + 5 * lambda -c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2]; | |
| 1188 c[m - 1] = (-2 * lambda - d[i1] * c[i1] * e[i1]) / d[m - 1]; | |
| 1189 z[m - 1] = w[m - 1] * y[m - 1] - c[i1] * z[i1] - e[i2] * z[i2]; | |
| 1190 i1 = m - 1; i2 = m - 2; | |
| 1191 | |
| 1192 d[m] = w[m] + lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2]; | |
| 1193 z[m] = (w[m] * y[m] - c[i1] * z[i1] - e[i2] * z[i2]) / d[m]; | |
| 1194 z[m - 1] = z[m - 1] / d[m - 1] - c[m - 1] * z[m]; | |
| 1195 | |
| 1196 for (i = m - 2; 1<= i; i--) | |
| 1197 z[i] = z[i] / d[i] - c[i] * z[i + 1] - e[i] * z[i + 2]; | |
| 1198 | |
| 1199 st = TRUE; | |
| 1200 } | |
| 1201 else st = FALSE; | |
| 1202 | |
| 1203 if (c != NULL) _cmsFree(ContextID, c); | |
| 1204 if (d != NULL) _cmsFree(ContextID, d); | |
| 1205 if (e != NULL) _cmsFree(ContextID, e); | |
| 1206 | |
| 1207 return st; | |
| 1208 } | |
| 1209 | |
| 1210 // Smooths a curve sampled at regular intervals. | |
| 1211 cmsBool CMSEXPORT cmsSmoothToneCurve(cmsContext ContextID, cmsToneCurve* Tab, cmsFloat64Number lambda) | |
| 1212 { | |
| 1213 cmsBool SuccessStatus = TRUE; | |
| 1214 cmsFloat32Number *w, *y, *z; | |
| 1215 cmsUInt32Number i, nItems, Zeros, Poles; | |
| 1216 cmsBool notCheck = FALSE; | |
| 1217 | |
| 1218 if (Tab != NULL && Tab->InterpParams != NULL) | |
| 1219 { | |
| 1220 if (!cmsIsToneCurveLinear(ContextID, Tab)) // Only non-linear curves need smoothing | |
| 1221 { | |
| 1222 nItems = Tab->nEntries; | |
| 1223 if (nItems < MAX_NODES_IN_CURVE) | |
| 1224 { | |
| 1225 // Allocate one more item than needed | |
| 1226 w = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number)); | |
| 1227 y = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number)); | |
| 1228 z = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number)); | |
| 1229 | |
| 1230 if (w != NULL && y != NULL && z != NULL) // Ensure no memory allocation failure | |
| 1231 { | |
| 1232 memset(w, 0, (nItems + 1) * sizeof(cmsFloat32Number)); | |
| 1233 memset(y, 0, (nItems + 1) * sizeof(cmsFloat32Number)); | |
| 1234 memset(z, 0, (nItems + 1) * sizeof(cmsFloat32Number)); | |
| 1235 | |
| 1236 for (i = 0; i < nItems; i++) | |
| 1237 { | |
| 1238 y[i + 1] = (cmsFloat32Number)Tab->Table16[i]; | |
| 1239 w[i + 1] = 1.0; | |
| 1240 } | |
| 1241 | |
| 1242 if (lambda < 0) | |
| 1243 { | |
| 1244 notCheck = TRUE; | |
| 1245 lambda = -lambda; | |
| 1246 } | |
| 1247 | |
| 1248 if (smooth2(ContextID, w, y, z, (cmsFloat32Number)lambda, (int)nItems)) | |
| 1249 { | |
| 1250 // Do some reality - checking... | |
| 1251 | |
| 1252 Zeros = Poles = 0; | |
| 1253 for (i = nItems; i > 1; --i) | |
| 1254 { | |
| 1255 if (z[i] == 0.) Zeros++; | |
| 1256 if (z[i] >= 65535.) Poles++; | |
| 1257 if (z[i] < z[i - 1]) | |
| 1258 { | |
| 1259 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Non-Monotonic."); | |
| 1260 SuccessStatus = notCheck; | |
| 1261 break; | |
| 1262 } | |
| 1263 } | |
| 1264 | |
| 1265 if (SuccessStatus && Zeros > (nItems / 3)) | |
| 1266 { | |
| 1267 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly zeros."); | |
| 1268 SuccessStatus = notCheck; | |
| 1269 } | |
| 1270 | |
| 1271 if (SuccessStatus && Poles > (nItems / 3)) | |
| 1272 { | |
| 1273 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly poles."); | |
| 1274 SuccessStatus = notCheck; | |
| 1275 } | |
| 1276 | |
| 1277 if (SuccessStatus) // Seems ok | |
| 1278 { | |
| 1279 for (i = 0; i < nItems; i++) | |
| 1280 { | |
| 1281 // Clamp to cmsUInt16Number | |
| 1282 Tab->Table16[i] = _cmsQuickSaturateWord(z[i + 1]); | |
| 1283 } | |
| 1284 } | |
| 1285 } | |
| 1286 else // Could not smooth | |
| 1287 { | |
| 1288 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Function smooth2 failed."); | |
| 1289 SuccessStatus = FALSE; | |
| 1290 } | |
| 1291 } | |
| 1292 else // One or more buffers could not be allocated | |
| 1293 { | |
| 1294 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Could not allocate memory."); | |
| 1295 SuccessStatus = FALSE; | |
| 1296 } | |
| 1297 | |
| 1298 if (z != NULL) | |
| 1299 _cmsFree(ContextID, z); | |
| 1300 | |
| 1301 if (y != NULL) | |
| 1302 _cmsFree(ContextID, y); | |
| 1303 | |
| 1304 if (w != NULL) | |
| 1305 _cmsFree(ContextID, w); | |
| 1306 } | |
| 1307 else // too many items in the table | |
| 1308 { | |
| 1309 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Too many points."); | |
| 1310 SuccessStatus = FALSE; | |
| 1311 } | |
| 1312 } | |
| 1313 } | |
| 1314 else // Tab parameter or Tab->InterpParams is NULL | |
| 1315 { | |
| 1316 // Can't signal an error here since the ContextID is not known at this point | |
| 1317 SuccessStatus = FALSE; | |
| 1318 } | |
| 1319 | |
| 1320 return SuccessStatus; | |
| 1321 } | |
| 1322 | |
| 1323 // Is a table linear? Do not use parametric since we cannot guarantee some weird parameters resulting | |
| 1324 // in a linear table. This way assures it is linear in 12 bits, which should be enough in most cases. | |
| 1325 cmsBool CMSEXPORT cmsIsToneCurveLinear(cmsContext ContextID, const cmsToneCurve* Curve) | |
| 1326 { | |
| 1327 int i; | |
| 1328 int diff; | |
| 1329 cmsUNUSED_PARAMETER(ContextID); | |
| 1330 | |
| 1331 _cmsAssert(Curve != NULL); | |
| 1332 | |
| 1333 for (i=0; i < (int) Curve ->nEntries; i++) { | |
| 1334 | |
| 1335 diff = abs((int) Curve->Table16[i] - (int) _cmsQuantizeVal(i, Curve ->nEntries)); | |
| 1336 if (diff > 0x0f) | |
| 1337 return FALSE; | |
| 1338 } | |
| 1339 | |
| 1340 return TRUE; | |
| 1341 } | |
| 1342 | |
| 1343 // Same, but for monotonicity | |
| 1344 cmsBool CMSEXPORT cmsIsToneCurveMonotonic(cmsContext ContextID, const cmsToneCurve* t) | |
| 1345 { | |
| 1346 cmsUInt32Number n; | |
| 1347 int i, last; | |
| 1348 cmsBool lDescending; | |
| 1349 | |
| 1350 _cmsAssert(t != NULL); | |
| 1351 | |
| 1352 // Degenerated curves are monotonic? Ok, let's pass them | |
| 1353 n = t ->nEntries; | |
| 1354 if (n < 2) return TRUE; | |
| 1355 | |
| 1356 // Curve direction | |
| 1357 lDescending = cmsIsToneCurveDescending(ContextID, t); | |
| 1358 | |
| 1359 if (lDescending) { | |
| 1360 | |
| 1361 last = t ->Table16[0]; | |
| 1362 | |
| 1363 for (i = 1; i < (int) n; i++) { | |
| 1364 | |
| 1365 if (t ->Table16[i] - last > 2) // We allow some ripple | |
| 1366 return FALSE; | |
| 1367 else | |
| 1368 last = t ->Table16[i]; | |
| 1369 | |
| 1370 } | |
| 1371 } | |
| 1372 else { | |
| 1373 | |
| 1374 last = t ->Table16[n-1]; | |
| 1375 | |
| 1376 for (i = (int) n - 2; i >= 0; --i) { | |
| 1377 | |
| 1378 if (t ->Table16[i] - last > 2) | |
| 1379 return FALSE; | |
| 1380 else | |
| 1381 last = t ->Table16[i]; | |
| 1382 | |
| 1383 } | |
| 1384 } | |
| 1385 | |
| 1386 return TRUE; | |
| 1387 } | |
| 1388 | |
| 1389 // Same, but for descending tables | |
| 1390 cmsBool CMSEXPORT cmsIsToneCurveDescending(cmsContext ContextID, const cmsToneCurve* t) | |
| 1391 { | |
| 1392 _cmsAssert(t != NULL); | |
| 1393 cmsUNUSED_PARAMETER(ContextID); | |
| 1394 | |
| 1395 return t ->Table16[0] > t ->Table16[t ->nEntries-1]; | |
| 1396 } | |
| 1397 | |
| 1398 | |
| 1399 // Another info fn: is out gamma table multisegment? | |
| 1400 cmsBool CMSEXPORT cmsIsToneCurveMultisegment(cmsContext ContextID, const cmsToneCurve* t) | |
| 1401 { | |
| 1402 _cmsAssert(t != NULL); | |
| 1403 cmsUNUSED_PARAMETER(ContextID); | |
| 1404 | |
| 1405 return t -> nSegments > 1; | |
| 1406 } | |
| 1407 | |
| 1408 cmsInt32Number CMSEXPORT cmsGetToneCurveParametricType(cmsContext ContextID, const cmsToneCurve* t) | |
| 1409 { | |
| 1410 _cmsAssert(t != NULL); | |
| 1411 cmsUNUSED_PARAMETER(ContextID); | |
| 1412 | |
| 1413 if (t -> nSegments != 1) return 0; | |
| 1414 return t ->Segments[0].Type; | |
| 1415 } | |
| 1416 | |
| 1417 // We need accuracy this time | |
| 1418 cmsFloat32Number CMSEXPORT cmsEvalToneCurveFloat(cmsContext ContextID, const cmsToneCurve* Curve, cmsFloat32Number v) | |
| 1419 { | |
| 1420 _cmsAssert(Curve != NULL); | |
| 1421 | |
| 1422 // Check for 16 bits table. If so, this is a limited-precision tone curve | |
| 1423 if (Curve ->nSegments == 0) { | |
| 1424 | |
| 1425 cmsUInt16Number In, Out; | |
| 1426 | |
| 1427 In = (cmsUInt16Number) _cmsQuickSaturateWord(v * 65535.0); | |
| 1428 Out = cmsEvalToneCurve16(ContextID, Curve, In); | |
| 1429 | |
| 1430 return (cmsFloat32Number) (Out / 65535.0); | |
| 1431 } | |
| 1432 | |
| 1433 return (cmsFloat32Number) EvalSegmentedFn(ContextID, Curve, v); | |
| 1434 } | |
| 1435 | |
| 1436 // We need xput over here | |
| 1437 cmsUInt16Number CMSEXPORT cmsEvalToneCurve16(cmsContext ContextID, const cmsToneCurve* Curve, cmsUInt16Number v) | |
| 1438 { | |
| 1439 cmsUInt16Number out; | |
| 1440 | |
| 1441 _cmsAssert(Curve != NULL); | |
| 1442 | |
| 1443 Curve ->InterpParams ->Interpolation.Lerp16(ContextID, &v, &out, Curve ->InterpParams); | |
| 1444 return out; | |
| 1445 } | |
| 1446 | |
| 1447 | |
| 1448 // Least squares fitting. | |
| 1449 // A mathematical procedure for finding the best-fitting curve to a given set of points by | |
| 1450 // minimizing the sum of the squares of the offsets ("the residuals") of the points from the curve. | |
| 1451 // The sum of the squares of the offsets is used instead of the offset absolute values because | |
| 1452 // this allows the residuals to be treated as a continuous differentiable quantity. | |
| 1453 // | |
| 1454 // y = f(x) = x ^ g | |
| 1455 // | |
| 1456 // R = (yi - (xi^g)) | |
| 1457 // R2 = (yi - (xi^g))2 | |
| 1458 // SUM R2 = SUM (yi - (xi^g))2 | |
| 1459 // | |
| 1460 // dR2/dg = -2 SUM x^g log(x)(y - x^g) | |
| 1461 // solving for dR2/dg = 0 | |
| 1462 // | |
| 1463 // g = 1/n * SUM(log(y) / log(x)) | |
| 1464 | |
| 1465 cmsFloat64Number CMSEXPORT cmsEstimateGamma(cmsContext ContextID, const cmsToneCurve* t, cmsFloat64Number Precision) | |
| 1466 { | |
| 1467 cmsFloat64Number gamma, sum, sum2; | |
| 1468 cmsFloat64Number n, x, y, Std; | |
| 1469 cmsUInt32Number i; | |
| 1470 | |
| 1471 _cmsAssert(t != NULL); | |
| 1472 | |
| 1473 sum = sum2 = n = 0; | |
| 1474 | |
| 1475 // Excluding endpoints | |
| 1476 for (i=1; i < (MAX_NODES_IN_CURVE-1); i++) { | |
| 1477 | |
| 1478 x = (cmsFloat64Number) i / (MAX_NODES_IN_CURVE-1); | |
| 1479 y = (cmsFloat64Number) cmsEvalToneCurveFloat(ContextID, t, (cmsFloat32Number) x); | |
| 1480 | |
| 1481 // Avoid 7% on lower part to prevent | |
| 1482 // artifacts due to linear ramps | |
| 1483 | |
| 1484 if (y > 0. && y < 1. && x > 0.07) { | |
| 1485 | |
| 1486 gamma = log(y) / log(x); | |
| 1487 sum += gamma; | |
| 1488 sum2 += gamma * gamma; | |
| 1489 n++; | |
| 1490 } | |
| 1491 } | |
| 1492 | |
| 1493 // We need enough valid samples | |
| 1494 if (n <= 1) return -1.0; | |
| 1495 | |
| 1496 // Take a look on SD to see if gamma isn't exponential at all | |
| 1497 Std = sqrt((n * sum2 - sum * sum) / (n*(n-1))); | |
| 1498 | |
| 1499 if (Std > Precision) | |
| 1500 return -1.0; | |
| 1501 | |
| 1502 return (sum / n); // The mean | |
| 1503 } | |
| 1504 | |
| 1505 // Retrieve segments on tone curves | |
| 1506 | |
| 1507 const cmsCurveSegment* CMSEXPORT cmsGetToneCurveSegment(cmsContext contextID, cmsInt32Number n, const cmsToneCurve* t) | |
| 1508 { | |
| 1509 _cmsAssert(t != NULL); | |
| 1510 | |
| 1511 if (n < 0 || n >= (cmsInt32Number) t->nSegments) return NULL; | |
| 1512 return t->Segments + n; | |
| 1513 } | |
| 1514 |
