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