msdasql: Check rowset pointer before assignment in ICommandText::Execute.
[wine.git] / libs / lcms2 / src / cmsgamma.c
blob1031fc15e0efb86b85d330ca021606ddc7bd03cc
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
24 //---------------------------------------------------------------------------------
26 #include "lcms2_internal.h"
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.
38 // ----------------------------------------------------------------- Implementation
39 // Maxim number of nodes
40 #define MAX_NODES_IN_CURVE 4097
41 #define MINUS_INF (-1E22F)
42 #define PLUS_INF (+1E22F)
44 // The list of supported parametric curves
45 typedef struct _cmsParametricCurvesCollection_st {
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
51 cmsParametricCurveEvaluator Evaluator; // The evaluator
53 struct _cmsParametricCurvesCollection_st* Next; // Next in list
55 } _cmsParametricCurvesCollection;
57 // This is the default (built-in) evaluator
58 static cmsFloat64Number DefaultEvalParametricFn(cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R);
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
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)
74 _cmsCurvesPluginChunkType newHead = { NULL };
75 _cmsParametricCurvesCollection* entry;
76 _cmsParametricCurvesCollection* Anterior = NULL;
77 _cmsCurvesPluginChunkType* head = (_cmsCurvesPluginChunkType*) src->chunks[CurvesPlugin];
79 _cmsAssert(head != NULL);
81 // Walk the list copying all nodes
82 for (entry = head->ParametricCurves;
83 entry != NULL;
84 entry = entry ->Next) {
86 _cmsParametricCurvesCollection *newEntry = ( _cmsParametricCurvesCollection *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(_cmsParametricCurvesCollection));
88 if (newEntry == NULL)
89 return;
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;
96 Anterior = newEntry;
98 if (newHead.ParametricCurves == NULL)
99 newHead.ParametricCurves = newEntry;
102 ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsCurvesPluginChunkType));
105 // The allocator have to follow the chain
106 void _cmsAllocCurvesPluginChunk(struct _cmsContext_struct* ctx,
107 const struct _cmsContext_struct* src)
109 _cmsAssert(ctx != NULL);
111 if (src != NULL) {
113 // Copy all linked list
114 DupPluginCurvesList(ctx, src);
116 else {
117 static _cmsCurvesPluginChunkType CurvesPluginChunk = { NULL };
118 ctx ->chunks[CurvesPlugin] = _cmsSubAllocDup(ctx ->MemPool, &CurvesPluginChunk, sizeof(_cmsCurvesPluginChunkType));
123 // The linked list head
124 _cmsCurvesPluginChunkType _cmsCurvesPluginChunk = { NULL };
126 // As a way to install new parametric curves
127 cmsBool _cmsRegisterParametricCurvesPlugin(cmsContext ContextID, cmsPluginBase* Data)
129 _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin);
130 cmsPluginParametricCurves* Plugin = (cmsPluginParametricCurves*) Data;
131 _cmsParametricCurvesCollection* fl;
133 if (Data == NULL) {
135 ctx -> ParametricCurves = NULL;
136 return TRUE;
139 fl = (_cmsParametricCurvesCollection*) _cmsPluginMalloc(ContextID, sizeof(_cmsParametricCurvesCollection));
140 if (fl == NULL) return FALSE;
142 // Copy the parameters
143 fl ->Evaluator = Plugin ->Evaluator;
144 fl ->nFunctions = Plugin ->nFunctions;
146 // Make sure no mem overwrites
147 if (fl ->nFunctions > MAX_TYPES_IN_LCMS_PLUGIN)
148 fl ->nFunctions = MAX_TYPES_IN_LCMS_PLUGIN;
150 // Copy the data
151 memmove(fl->FunctionTypes, Plugin ->FunctionTypes, fl->nFunctions * sizeof(cmsUInt32Number));
152 memmove(fl->ParameterCount, Plugin ->ParameterCount, fl->nFunctions * sizeof(cmsUInt32Number));
154 // Keep linked list
155 fl ->Next = ctx->ParametricCurves;
156 ctx->ParametricCurves = fl;
158 // All is ok
159 return TRUE;
163 // Search in type list, return position or -1 if not found
164 static
165 int IsInSet(int Type, _cmsParametricCurvesCollection* c)
167 int i;
169 for (i=0; i < (int) c ->nFunctions; i++)
170 if (abs(Type) == c ->FunctionTypes[i]) return i;
172 return -1;
176 // Search for the collection which contains a specific type
177 static
178 _cmsParametricCurvesCollection *GetParametricCurveByType(cmsContext ContextID, int Type, int* index)
180 _cmsParametricCurvesCollection* c;
181 int Position;
182 _cmsCurvesPluginChunkType* ctx = ( _cmsCurvesPluginChunkType*) _cmsContextGetClientChunk(ContextID, CurvesPlugin);
184 for (c = ctx->ParametricCurves; c != NULL; c = c ->Next) {
186 Position = IsInSet(Type, c);
188 if (Position != -1) {
189 if (index != NULL)
190 *index = Position;
191 return c;
194 // If none found, revert for defaults
195 for (c = &DefaultCurves; c != NULL; c = c ->Next) {
197 Position = IsInSet(Type, c);
199 if (Position != -1) {
200 if (index != NULL)
201 *index = Position;
202 return c;
206 return NULL;
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)
217 cmsToneCurve* p;
218 cmsUInt32Number i;
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;
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;
231 // Allocate all required pointers, etc.
232 p = (cmsToneCurve*) _cmsMallocZero(ContextID, sizeof(cmsToneCurve));
233 if (!p) return NULL;
235 // In this case, there are no segments
236 if (nSegments == 0) {
237 p ->Segments = NULL;
238 p ->Evals = NULL;
240 else {
241 p ->Segments = (cmsCurveSegment*) _cmsCalloc(ContextID, nSegments, sizeof(cmsCurveSegment));
242 if (p ->Segments == NULL) goto Error;
244 p ->Evals = (cmsParametricCurveEvaluator*) _cmsCalloc(ContextID, nSegments, sizeof(cmsParametricCurveEvaluator));
245 if (p ->Evals == NULL) goto Error;
248 p -> nSegments = nSegments;
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;
255 else {
256 p ->Table16 = (cmsUInt16Number*) _cmsCalloc(ContextID, nEntries, sizeof(cmsUInt16Number));
257 if (p ->Table16 == NULL) goto Error;
260 p -> nEntries = nEntries;
262 // Initialize members if requested
263 if (Values != NULL && (nEntries > 0)) {
265 for (i=0; i < nEntries; i++)
266 p ->Table16[i] = Values[i];
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)) {
273 _cmsParametricCurvesCollection *c;
275 p ->SegInterp = (cmsInterpParams**) _cmsCalloc(ContextID, nSegments, sizeof(cmsInterpParams*));
276 if (p ->SegInterp == NULL) goto Error;
278 for (i=0; i < nSegments; i++) {
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);
284 memmove(&p ->Segments[i], &Segments[i], sizeof(cmsCurveSegment));
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;
292 c = GetParametricCurveByType(ContextID, Segments[i].Type, NULL);
293 if (c != NULL)
294 p ->Evals[i] = c ->Evaluator;
298 p ->InterpParams = _cmsComputeInterpParams(ContextID, p ->nEntries, 1, 1, p->Table16, CMS_LERP_FLAGS_16BITS);
299 if (p->InterpParams != NULL)
300 return p;
302 Error:
303 if (p -> SegInterp) _cmsFree(ContextID, p -> SegInterp);
304 if (p -> Segments) _cmsFree(ContextID, p -> Segments);
305 if (p -> Evals) _cmsFree(ContextID, p -> Evals);
306 if (p ->Table16) _cmsFree(ContextID, p ->Table16);
307 _cmsFree(ContextID, p);
308 return NULL;
312 // Generates a sigmoidal function with desired steepness.
313 cmsINLINE double sigmoid_base(double k, double t)
315 return (1.0 / (1.0 + exp(-k * t))) - 0.5;
318 cmsINLINE double inverted_sigmoid_base(double k, double t)
320 return -log((1.0 / (t + 0.5)) - 1.0) / k;
323 cmsINLINE double sigmoid_factory(double k, double t)
325 double correction = 0.5 / sigmoid_base(k, 1);
327 return correction * sigmoid_base(k, 2.0 * t - 1.0) + 0.5;
330 cmsINLINE double inverse_sigmoid_factory(double k, double t)
332 double correction = 0.5 / sigmoid_base(k, 1);
334 return (inverted_sigmoid_base(k, (t - 0.5) / correction) + 1.0) / 2.0;
338 // Parametric Fn using floating point
339 static
340 cmsFloat64Number DefaultEvalParametricFn(cmsInt32Number Type, const cmsFloat64Number Params[], cmsFloat64Number R)
342 cmsFloat64Number e, Val, disc;
344 switch (Type) {
346 // X = Y ^ Gamma
347 case 1:
348 if (R < 0) {
350 if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE)
351 Val = R;
352 else
353 Val = 0;
355 else
356 Val = pow(R, Params[0]);
357 break;
359 // Type 1 Reversed: X = Y ^1/gamma
360 case -1:
361 if (R < 0) {
363 if (fabs(Params[0] - 1.0) < MATRIX_DET_TOLERANCE)
364 Val = R;
365 else
366 Val = 0;
368 else
370 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE)
371 Val = PLUS_INF;
372 else
373 Val = pow(R, 1 / Params[0]);
375 break;
377 // CIE 122-1966
378 // Y = (aX + b)^Gamma | X >= -b/a
379 // Y = 0 | else
380 case 2:
383 if (fabs(Params[1]) < MATRIX_DET_TOLERANCE)
385 Val = 0;
387 else
389 disc = -Params[2] / Params[1];
391 if (R >= disc) {
393 e = Params[1] * R + Params[2];
395 if (e > 0)
396 Val = pow(e, Params[0]);
397 else
398 Val = 0;
400 else
401 Val = 0;
404 break;
406 // Type 2 Reversed
407 // X = (Y ^1/g - b) / a
408 case -2:
410 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
411 fabs(Params[1]) < MATRIX_DET_TOLERANCE)
413 Val = 0;
415 else
417 if (R < 0)
418 Val = 0;
419 else
420 Val = (pow(R, 1.0 / Params[0]) - Params[2]) / Params[1];
422 if (Val < 0)
423 Val = 0;
426 break;
429 // IEC 61966-3
430 // Y = (aX + b)^Gamma + c | X <= -b/a
431 // Y = c | else
432 case 3:
434 if (fabs(Params[1]) < MATRIX_DET_TOLERANCE)
436 Val = 0;
438 else
440 disc = -Params[2] / Params[1];
441 if (disc < 0)
442 disc = 0;
444 if (R >= disc) {
446 e = Params[1] * R + Params[2];
448 if (e > 0)
449 Val = pow(e, Params[0]) + Params[3];
450 else
451 Val = 0;
453 else
454 Val = Params[3];
457 break;
460 // Type 3 reversed
461 // X=((Y-c)^1/g - b)/a | (Y>=c)
462 // X=-b/a | (Y<c)
463 case -3:
465 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
466 fabs(Params[1]) < MATRIX_DET_TOLERANCE)
468 Val = 0;
470 else
472 if (R >= Params[3]) {
474 e = R - Params[3];
476 if (e > 0)
477 Val = (pow(e, 1 / Params[0]) - Params[2]) / Params[1];
478 else
479 Val = 0;
481 else {
482 Val = -Params[2] / Params[1];
486 break;
489 // IEC 61966-2.1 (sRGB)
490 // Y = (aX + b)^Gamma | X >= d
491 // Y = cX | X < d
492 case 4:
493 if (R >= Params[4]) {
495 e = Params[1]*R + Params[2];
497 if (e > 0)
498 Val = pow(e, Params[0]);
499 else
500 Val = 0;
502 else
503 Val = R * Params[3];
504 break;
506 // Type 4 reversed
507 // X=((Y^1/g-b)/a) | Y >= (ad+b)^g
508 // X=Y/c | Y< (ad+b)^g
509 case -4:
512 e = Params[1] * Params[4] + Params[2];
513 if (e < 0)
514 disc = 0;
515 else
516 disc = pow(e, Params[0]);
518 if (R >= disc) {
520 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
521 fabs(Params[1]) < MATRIX_DET_TOLERANCE)
523 Val = 0;
525 else
526 Val = (pow(R, 1.0 / Params[0]) - Params[2]) / Params[1];
528 else {
530 if (fabs(Params[3]) < MATRIX_DET_TOLERANCE)
531 Val = 0;
532 else
533 Val = R / Params[3];
537 break;
540 // Y = (aX + b)^Gamma + e | X >= d
541 // Y = cX + f | X < d
542 case 5:
543 if (R >= Params[4]) {
545 e = Params[1]*R + Params[2];
547 if (e > 0)
548 Val = pow(e, Params[0]) + Params[5];
549 else
550 Val = Params[5];
552 else
553 Val = R*Params[3] + Params[6];
554 break;
557 // Reversed type 5
558 // X=((Y-e)1/g-b)/a | Y >=(ad+b)^g+e), cd+f
559 // X=(Y-f)/c | else
560 case -5:
562 disc = Params[3] * Params[4] + Params[6];
563 if (R >= disc) {
565 e = R - Params[5];
566 if (e < 0)
567 Val = 0;
568 else
570 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
571 fabs(Params[1]) < MATRIX_DET_TOLERANCE)
573 Val = 0;
574 else
575 Val = (pow(e, 1.0 / Params[0]) - Params[2]) / Params[1];
578 else {
579 if (fabs(Params[3]) < MATRIX_DET_TOLERANCE)
580 Val = 0;
581 else
582 Val = (R - Params[6]) / Params[3];
586 break;
589 // Types 6,7,8 comes from segmented curves as described in ICCSpecRevision_02_11_06_Float.pdf
590 // Type 6 is basically identical to type 5 without d
592 // Y = (a * X + b) ^ Gamma + c
593 case 6:
594 e = Params[1]*R + Params[2];
596 if (e < 0)
597 Val = Params[3];
598 else
599 Val = pow(e, Params[0]) + Params[3];
600 break;
602 // ((Y - c) ^1/Gamma - b) / a
603 case -6:
605 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
606 fabs(Params[1]) < MATRIX_DET_TOLERANCE)
608 Val = 0;
610 else
612 e = R - Params[3];
613 if (e < 0)
614 Val = 0;
615 else
616 Val = (pow(e, 1.0 / Params[0]) - Params[2]) / Params[1];
619 break;
622 // Y = a * log (b * X^Gamma + c) + d
623 case 7:
625 e = Params[2] * pow(R, Params[0]) + Params[3];
626 if (e <= 0)
627 Val = Params[4];
628 else
629 Val = Params[1]*log10(e) + Params[4];
630 break;
632 // (Y - d) / a = log(b * X ^Gamma + c)
633 // pow(10, (Y-d) / a) = b * X ^Gamma + c
634 // pow((pow(10, (Y-d) / a) - c) / b, 1/g) = X
635 case -7:
637 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
638 fabs(Params[1]) < MATRIX_DET_TOLERANCE ||
639 fabs(Params[2]) < MATRIX_DET_TOLERANCE)
641 Val = 0;
643 else
645 Val = pow((pow(10.0, (R - Params[4]) / Params[1]) - Params[3]) / Params[2], 1.0 / Params[0]);
648 break;
651 //Y = a * b^(c*X+d) + e
652 case 8:
653 Val = (Params[0] * pow(Params[1], Params[2] * R + Params[3]) + Params[4]);
654 break;
657 // Y = (log((y-e) / a) / log(b) - d ) / c
658 // a=0, b=1, c=2, d=3, e=4,
659 case -8:
661 disc = R - Params[4];
662 if (disc < 0) Val = 0;
663 else
665 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE ||
666 fabs(Params[2]) < MATRIX_DET_TOLERANCE)
668 Val = 0;
670 else
672 Val = (log(disc / Params[0]) / log(Params[1]) - Params[3]) / Params[2];
675 break;
678 // S-Shaped: (1 - (1-x)^1/g)^1/g
679 case 108:
680 if (fabs(Params[0]) < MATRIX_DET_TOLERANCE)
681 Val = 0;
682 else
683 Val = pow(1.0 - pow(1 - R, 1/Params[0]), 1/Params[0]);
684 break;
686 // y = (1 - (1-x)^1/g)^1/g
687 // y^g = (1 - (1-x)^1/g)
688 // 1 - y^g = (1-x)^1/g
689 // (1 - y^g)^g = 1 - x
690 // 1 - (1 - y^g)^g
691 case -108:
692 Val = 1 - pow(1 - pow(R, Params[0]), Params[0]);
693 break;
695 // Sigmoidals
696 case 109:
697 Val = sigmoid_factory(Params[0], R);
698 break;
700 case -109:
701 Val = inverse_sigmoid_factory(Params[0], R);
702 break;
704 default:
705 // Unsupported parametric curve. Should never reach here
706 return 0;
709 return Val;
712 // Evaluate a segmented function for a single value. Return -Inf if no valid segment found .
713 // If fn type is 0, perform an interpolation on the table
714 static
715 cmsFloat64Number EvalSegmentedFn(const cmsToneCurve *g, cmsFloat64Number R)
717 int i;
718 cmsFloat32Number Out32;
719 cmsFloat64Number Out;
721 for (i = (int) g->nSegments - 1; i >= 0; --i) {
723 // Check for domain
724 if ((R > g->Segments[i].x0) && (R <= g->Segments[i].x1)) {
726 // Type == 0 means segment is sampled
727 if (g->Segments[i].Type == 0) {
729 cmsFloat32Number R1 = (cmsFloat32Number)(R - g->Segments[i].x0) / (g->Segments[i].x1 - g->Segments[i].x0);
731 // Setup the table (TODO: clean that)
732 g->SegInterp[i]->Table = g->Segments[i].SampledPoints;
734 g->SegInterp[i]->Interpolation.LerpFloat(&R1, &Out32, g->SegInterp[i]);
735 Out = (cmsFloat64Number) Out32;
738 else {
739 Out = g->Evals[i](g->Segments[i].Type, g->Segments[i].Params, R);
742 if (isinf(Out))
743 return PLUS_INF;
744 else
746 if (isinf(-Out))
747 return MINUS_INF;
750 return Out;
754 return MINUS_INF;
757 // Access to estimated low-res table
758 cmsUInt32Number CMSEXPORT cmsGetToneCurveEstimatedTableEntries(const cmsToneCurve* t)
760 _cmsAssert(t != NULL);
761 return t ->nEntries;
764 const cmsUInt16Number* CMSEXPORT cmsGetToneCurveEstimatedTable(const cmsToneCurve* t)
766 _cmsAssert(t != NULL);
767 return t ->Table16;
771 // Create an empty gamma curve, by using tables. This specifies only the limited-precision part, and leaves the
772 // floating point description empty.
773 cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurve16(cmsContext ContextID, cmsUInt32Number nEntries, const cmsUInt16Number Values[])
775 return AllocateToneCurveStruct(ContextID, nEntries, 0, NULL, Values);
778 static
779 cmsUInt32Number EntriesByGamma(cmsFloat64Number Gamma)
781 if (fabs(Gamma - 1.0) < 0.001) return 2;
782 return 4096;
786 // Create a segmented gamma, fill the table
787 cmsToneCurve* CMSEXPORT cmsBuildSegmentedToneCurve(cmsContext ContextID,
788 cmsUInt32Number nSegments, const cmsCurveSegment Segments[])
790 cmsUInt32Number i;
791 cmsFloat64Number R, Val;
792 cmsToneCurve* g;
793 cmsUInt32Number nGridPoints = 4096;
795 _cmsAssert(Segments != NULL);
797 // Optimizatin for identity curves.
798 if (nSegments == 1 && Segments[0].Type == 1) {
800 nGridPoints = EntriesByGamma(Segments[0].Params[0]);
803 g = AllocateToneCurveStruct(ContextID, nGridPoints, nSegments, Segments, NULL);
804 if (g == NULL) return NULL;
806 // Once we have the floating point version, we can approximate a 16 bit table of 4096 entries
807 // for performance reasons. This table would normally not be used except on 8/16 bits transforms.
808 for (i = 0; i < nGridPoints; i++) {
810 R = (cmsFloat64Number) i / (nGridPoints-1);
812 Val = EvalSegmentedFn(g, R);
814 // Round and saturate
815 g ->Table16[i] = _cmsQuickSaturateWord(Val * 65535.0);
818 return g;
821 // Use a segmented curve to store the floating point table
822 cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurveFloat(cmsContext ContextID, cmsUInt32Number nEntries, const cmsFloat32Number values[])
824 cmsCurveSegment Seg[3];
826 // Do some housekeeping
827 if (nEntries == 0 || values == NULL)
828 return NULL;
830 // A segmented tone curve should have function segments in the first and last positions
831 // Initialize segmented curve part up to 0 to constant value = samples[0]
832 Seg[0].x0 = MINUS_INF;
833 Seg[0].x1 = 0;
834 Seg[0].Type = 6;
836 Seg[0].Params[0] = 1;
837 Seg[0].Params[1] = 0;
838 Seg[0].Params[2] = 0;
839 Seg[0].Params[3] = values[0];
840 Seg[0].Params[4] = 0;
842 // From zero to 1
843 Seg[1].x0 = 0;
844 Seg[1].x1 = 1.0;
845 Seg[1].Type = 0;
847 Seg[1].nGridPoints = nEntries;
848 Seg[1].SampledPoints = (cmsFloat32Number*) values;
850 // Final segment is constant = lastsample
851 Seg[2].x0 = 1.0;
852 Seg[2].x1 = PLUS_INF;
853 Seg[2].Type = 6;
855 Seg[2].Params[0] = 1;
856 Seg[2].Params[1] = 0;
857 Seg[2].Params[2] = 0;
858 Seg[2].Params[3] = values[nEntries-1];
859 Seg[2].Params[4] = 0;
862 return cmsBuildSegmentedToneCurve(ContextID, 3, Seg);
865 // Parametric curves
867 // Parameters goes as: Curve, a, b, c, d, e, f
868 // Type is the ICC type +1
869 // if type is negative, then the curve is analytically inverted
870 cmsToneCurve* CMSEXPORT cmsBuildParametricToneCurve(cmsContext ContextID, cmsInt32Number Type, const cmsFloat64Number Params[])
872 cmsCurveSegment Seg0;
873 int Pos = 0;
874 cmsUInt32Number size;
875 _cmsParametricCurvesCollection* c = GetParametricCurveByType(ContextID, Type, &Pos);
877 _cmsAssert(Params != NULL);
879 if (c == NULL) {
880 cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Invalid parametric curve type %d", Type);
881 return NULL;
884 memset(&Seg0, 0, sizeof(Seg0));
886 Seg0.x0 = MINUS_INF;
887 Seg0.x1 = PLUS_INF;
888 Seg0.Type = Type;
890 size = c->ParameterCount[Pos] * sizeof(cmsFloat64Number);
891 memmove(Seg0.Params, Params, size);
893 return cmsBuildSegmentedToneCurve(ContextID, 1, &Seg0);
898 // Build a gamma table based on gamma constant
899 cmsToneCurve* CMSEXPORT cmsBuildGamma(cmsContext ContextID, cmsFloat64Number Gamma)
901 return cmsBuildParametricToneCurve(ContextID, 1, &Gamma);
905 // Free all memory taken by the gamma curve
906 void CMSEXPORT cmsFreeToneCurve(cmsToneCurve* Curve)
908 cmsContext ContextID;
910 if (Curve == NULL) return;
912 ContextID = Curve ->InterpParams->ContextID;
914 _cmsFreeInterpParams(Curve ->InterpParams);
916 if (Curve -> Table16)
917 _cmsFree(ContextID, Curve ->Table16);
919 if (Curve ->Segments) {
921 cmsUInt32Number i;
923 for (i=0; i < Curve ->nSegments; i++) {
925 if (Curve ->Segments[i].SampledPoints) {
926 _cmsFree(ContextID, Curve ->Segments[i].SampledPoints);
929 if (Curve ->SegInterp[i] != 0)
930 _cmsFreeInterpParams(Curve->SegInterp[i]);
933 _cmsFree(ContextID, Curve ->Segments);
934 _cmsFree(ContextID, Curve ->SegInterp);
937 if (Curve -> Evals)
938 _cmsFree(ContextID, Curve -> Evals);
940 _cmsFree(ContextID, Curve);
943 // Utility function, free 3 gamma tables
944 void CMSEXPORT cmsFreeToneCurveTriple(cmsToneCurve* Curve[3])
947 _cmsAssert(Curve != NULL);
949 if (Curve[0] != NULL) cmsFreeToneCurve(Curve[0]);
950 if (Curve[1] != NULL) cmsFreeToneCurve(Curve[1]);
951 if (Curve[2] != NULL) cmsFreeToneCurve(Curve[2]);
953 Curve[0] = Curve[1] = Curve[2] = NULL;
957 // Duplicate a gamma table
958 cmsToneCurve* CMSEXPORT cmsDupToneCurve(const cmsToneCurve* In)
960 if (In == NULL) return NULL;
962 return AllocateToneCurveStruct(In ->InterpParams ->ContextID, In ->nEntries, In ->nSegments, In ->Segments, In ->Table16);
965 // Joins two curves for X and Y. Curves should be monotonic.
966 // We want to get
968 // y = Y^-1(X(t))
970 cmsToneCurve* CMSEXPORT cmsJoinToneCurve(cmsContext ContextID,
971 const cmsToneCurve* X,
972 const cmsToneCurve* Y, cmsUInt32Number nResultingPoints)
974 cmsToneCurve* out = NULL;
975 cmsToneCurve* Yreversed = NULL;
976 cmsFloat32Number t, x;
977 cmsFloat32Number* Res = NULL;
978 cmsUInt32Number i;
981 _cmsAssert(X != NULL);
982 _cmsAssert(Y != NULL);
984 Yreversed = cmsReverseToneCurveEx(nResultingPoints, Y);
985 if (Yreversed == NULL) goto Error;
987 Res = (cmsFloat32Number*) _cmsCalloc(ContextID, nResultingPoints, sizeof(cmsFloat32Number));
988 if (Res == NULL) goto Error;
990 //Iterate
991 for (i=0; i < nResultingPoints; i++) {
993 t = (cmsFloat32Number) i / (cmsFloat32Number)(nResultingPoints-1);
994 x = cmsEvalToneCurveFloat(X, t);
995 Res[i] = cmsEvalToneCurveFloat(Yreversed, x);
998 // Allocate space for output
999 out = cmsBuildTabulatedToneCurveFloat(ContextID, nResultingPoints, Res);
1001 Error:
1003 if (Res != NULL) _cmsFree(ContextID, Res);
1004 if (Yreversed != NULL) cmsFreeToneCurve(Yreversed);
1006 return out;
1011 // Get the surrounding nodes. This is tricky on non-monotonic tables
1012 static
1013 int GetInterval(cmsFloat64Number In, const cmsUInt16Number LutTable[], const struct _cms_interp_struc* p)
1015 int i;
1016 int y0, y1;
1018 // A 1 point table is not allowed
1019 if (p -> Domain[0] < 1) return -1;
1021 // Let's see if ascending or descending.
1022 if (LutTable[0] < LutTable[p ->Domain[0]]) {
1024 // Table is overall ascending
1025 for (i = (int) p->Domain[0] - 1; i >= 0; --i) {
1027 y0 = LutTable[i];
1028 y1 = LutTable[i+1];
1030 if (y0 <= y1) { // Increasing
1031 if (In >= y0 && In <= y1) return i;
1033 else
1034 if (y1 < y0) { // Decreasing
1035 if (In >= y1 && In <= y0) return i;
1039 else {
1040 // Table is overall descending
1041 for (i=0; i < (int) p -> Domain[0]; i++) {
1043 y0 = LutTable[i];
1044 y1 = LutTable[i+1];
1046 if (y0 <= y1) { // Increasing
1047 if (In >= y0 && In <= y1) return i;
1049 else
1050 if (y1 < y0) { // Decreasing
1051 if (In >= y1 && In <= y0) return i;
1056 return -1;
1059 // Reverse a gamma table
1060 cmsToneCurve* CMSEXPORT cmsReverseToneCurveEx(cmsUInt32Number nResultSamples, const cmsToneCurve* InCurve)
1062 cmsToneCurve *out;
1063 cmsFloat64Number a = 0, b = 0, y, x1, y1, x2, y2;
1064 int i, j;
1065 int Ascending;
1067 _cmsAssert(InCurve != NULL);
1069 // Try to reverse it analytically whatever possible
1071 if (InCurve ->nSegments == 1 && InCurve ->Segments[0].Type > 0 &&
1072 /* InCurve -> Segments[0].Type <= 5 */
1073 GetParametricCurveByType(InCurve ->InterpParams->ContextID, InCurve ->Segments[0].Type, NULL) != NULL) {
1075 return cmsBuildParametricToneCurve(InCurve ->InterpParams->ContextID,
1076 -(InCurve -> Segments[0].Type),
1077 InCurve -> Segments[0].Params);
1080 // Nope, reverse the table.
1081 out = cmsBuildTabulatedToneCurve16(InCurve ->InterpParams->ContextID, nResultSamples, NULL);
1082 if (out == NULL)
1083 return NULL;
1085 // We want to know if this is an ascending or descending table
1086 Ascending = !cmsIsToneCurveDescending(InCurve);
1088 // Iterate across Y axis
1089 for (i=0; i < (int) nResultSamples; i++) {
1091 y = (cmsFloat64Number) i * 65535.0 / (nResultSamples - 1);
1093 // Find interval in which y is within.
1094 j = GetInterval(y, InCurve->Table16, InCurve->InterpParams);
1095 if (j >= 0) {
1098 // Get limits of interval
1099 x1 = InCurve ->Table16[j];
1100 x2 = InCurve ->Table16[j+1];
1102 y1 = (cmsFloat64Number) (j * 65535.0) / (InCurve ->nEntries - 1);
1103 y2 = (cmsFloat64Number) ((j+1) * 65535.0 ) / (InCurve ->nEntries - 1);
1105 // If collapsed, then use any
1106 if (x1 == x2) {
1108 out ->Table16[i] = _cmsQuickSaturateWord(Ascending ? y2 : y1);
1109 continue;
1111 } else {
1113 // Interpolate
1114 a = (y2 - y1) / (x2 - x1);
1115 b = y2 - a * x2;
1119 out ->Table16[i] = _cmsQuickSaturateWord(a* y + b);
1123 return out;
1126 // Reverse a gamma table
1127 cmsToneCurve* CMSEXPORT cmsReverseToneCurve(const cmsToneCurve* InGamma)
1129 _cmsAssert(InGamma != NULL);
1131 return cmsReverseToneCurveEx(4096, InGamma);
1134 // From: Eilers, P.H.C. (1994) Smoothing and interpolation with finite
1135 // differences. in: Graphic Gems IV, Heckbert, P.S. (ed.), Academic press.
1137 // Smoothing and interpolation with second differences.
1139 // Input: weights (w), data (y): vector from 1 to m.
1140 // Input: smoothing parameter (lambda), length (m).
1141 // Output: smoothed vector (z): vector from 1 to m.
1143 static
1144 cmsBool smooth2(cmsContext ContextID, cmsFloat32Number w[], cmsFloat32Number y[],
1145 cmsFloat32Number z[], cmsFloat32Number lambda, int m)
1147 int i, i1, i2;
1148 cmsFloat32Number *c, *d, *e;
1149 cmsBool st;
1152 c = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
1153 d = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
1154 e = (cmsFloat32Number*) _cmsCalloc(ContextID, MAX_NODES_IN_CURVE, sizeof(cmsFloat32Number));
1156 if (c != NULL && d != NULL && e != NULL) {
1159 d[1] = w[1] + lambda;
1160 c[1] = -2 * lambda / d[1];
1161 e[1] = lambda /d[1];
1162 z[1] = w[1] * y[1];
1163 d[2] = w[2] + 5 * lambda - d[1] * c[1] * c[1];
1164 c[2] = (-4 * lambda - d[1] * c[1] * e[1]) / d[2];
1165 e[2] = lambda / d[2];
1166 z[2] = w[2] * y[2] - c[1] * z[1];
1168 for (i = 3; i < m - 1; i++) {
1169 i1 = i - 1; i2 = i - 2;
1170 d[i]= w[i] + 6 * lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
1171 c[i] = (-4 * lambda -d[i1] * c[i1] * e[i1])/ d[i];
1172 e[i] = lambda / d[i];
1173 z[i] = w[i] * y[i] - c[i1] * z[i1] - e[i2] * z[i2];
1176 i1 = m - 2; i2 = m - 3;
1178 d[m - 1] = w[m - 1] + 5 * lambda -c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
1179 c[m - 1] = (-2 * lambda - d[i1] * c[i1] * e[i1]) / d[m - 1];
1180 z[m - 1] = w[m - 1] * y[m - 1] - c[i1] * z[i1] - e[i2] * z[i2];
1181 i1 = m - 1; i2 = m - 2;
1183 d[m] = w[m] + lambda - c[i1] * c[i1] * d[i1] - e[i2] * e[i2] * d[i2];
1184 z[m] = (w[m] * y[m] - c[i1] * z[i1] - e[i2] * z[i2]) / d[m];
1185 z[m - 1] = z[m - 1] / d[m - 1] - c[m - 1] * z[m];
1187 for (i = m - 2; 1<= i; i--)
1188 z[i] = z[i] / d[i] - c[i] * z[i + 1] - e[i] * z[i + 2];
1190 st = TRUE;
1192 else st = FALSE;
1194 if (c != NULL) _cmsFree(ContextID, c);
1195 if (d != NULL) _cmsFree(ContextID, d);
1196 if (e != NULL) _cmsFree(ContextID, e);
1198 return st;
1201 // Smooths a curve sampled at regular intervals.
1202 cmsBool CMSEXPORT cmsSmoothToneCurve(cmsToneCurve* Tab, cmsFloat64Number lambda)
1204 cmsBool SuccessStatus = TRUE;
1205 cmsFloat32Number *w, *y, *z;
1206 cmsUInt32Number i, nItems, Zeros, Poles;
1207 cmsBool notCheck = FALSE;
1209 if (Tab != NULL && Tab->InterpParams != NULL)
1211 cmsContext ContextID = Tab->InterpParams->ContextID;
1213 if (!cmsIsToneCurveLinear(Tab)) // Only non-linear curves need smoothing
1215 nItems = Tab->nEntries;
1216 if (nItems < MAX_NODES_IN_CURVE)
1218 // Allocate one more item than needed
1219 w = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number));
1220 y = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number));
1221 z = (cmsFloat32Number *)_cmsCalloc(ContextID, nItems + 1, sizeof(cmsFloat32Number));
1223 if (w != NULL && y != NULL && z != NULL) // Ensure no memory allocation failure
1225 memset(w, 0, (nItems + 1) * sizeof(cmsFloat32Number));
1226 memset(y, 0, (nItems + 1) * sizeof(cmsFloat32Number));
1227 memset(z, 0, (nItems + 1) * sizeof(cmsFloat32Number));
1229 for (i = 0; i < nItems; i++)
1231 y[i + 1] = (cmsFloat32Number)Tab->Table16[i];
1232 w[i + 1] = 1.0;
1235 if (lambda < 0)
1237 notCheck = TRUE;
1238 lambda = -lambda;
1241 if (smooth2(ContextID, w, y, z, (cmsFloat32Number)lambda, (int)nItems))
1243 // Do some reality - checking...
1245 Zeros = Poles = 0;
1246 for (i = nItems; i > 1; --i)
1248 if (z[i] == 0.) Zeros++;
1249 if (z[i] >= 65535.) Poles++;
1250 if (z[i] < z[i - 1])
1252 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Non-Monotonic.");
1253 SuccessStatus = notCheck;
1254 break;
1258 if (SuccessStatus && Zeros > (nItems / 3))
1260 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly zeros.");
1261 SuccessStatus = notCheck;
1264 if (SuccessStatus && Poles > (nItems / 3))
1266 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Degenerated, mostly poles.");
1267 SuccessStatus = notCheck;
1270 if (SuccessStatus) // Seems ok
1272 for (i = 0; i < nItems; i++)
1274 // Clamp to cmsUInt16Number
1275 Tab->Table16[i] = _cmsQuickSaturateWord(z[i + 1]);
1279 else // Could not smooth
1281 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Function smooth2 failed.");
1282 SuccessStatus = FALSE;
1285 else // One or more buffers could not be allocated
1287 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Could not allocate memory.");
1288 SuccessStatus = FALSE;
1291 if (z != NULL)
1292 _cmsFree(ContextID, z);
1294 if (y != NULL)
1295 _cmsFree(ContextID, y);
1297 if (w != NULL)
1298 _cmsFree(ContextID, w);
1300 else // too many items in the table
1302 cmsSignalError(ContextID, cmsERROR_RANGE, "cmsSmoothToneCurve: Too many points.");
1303 SuccessStatus = FALSE;
1307 else // Tab parameter or Tab->InterpParams is NULL
1309 // Can't signal an error here since the ContextID is not known at this point
1310 SuccessStatus = FALSE;
1313 return SuccessStatus;
1316 // Is a table linear? Do not use parametric since we cannot guarantee some weird parameters resulting
1317 // in a linear table. This way assures it is linear in 12 bits, which should be enough in most cases.
1318 cmsBool CMSEXPORT cmsIsToneCurveLinear(const cmsToneCurve* Curve)
1320 int i;
1321 int diff;
1323 _cmsAssert(Curve != NULL);
1325 for (i=0; i < (int) Curve ->nEntries; i++) {
1327 diff = abs((int) Curve->Table16[i] - (int) _cmsQuantizeVal(i, Curve ->nEntries));
1328 if (diff > 0x0f)
1329 return FALSE;
1332 return TRUE;
1335 // Same, but for monotonicity
1336 cmsBool CMSEXPORT cmsIsToneCurveMonotonic(const cmsToneCurve* t)
1338 cmsUInt32Number n;
1339 int i, last;
1340 cmsBool lDescending;
1342 _cmsAssert(t != NULL);
1344 // Degenerated curves are monotonic? Ok, let's pass them
1345 n = t ->nEntries;
1346 if (n < 2) return TRUE;
1348 // Curve direction
1349 lDescending = cmsIsToneCurveDescending(t);
1351 if (lDescending) {
1353 last = t ->Table16[0];
1355 for (i = 1; i < (int) n; i++) {
1357 if (t ->Table16[i] - last > 2) // We allow some ripple
1358 return FALSE;
1359 else
1360 last = t ->Table16[i];
1364 else {
1366 last = t ->Table16[n-1];
1368 for (i = (int) n - 2; i >= 0; --i) {
1370 if (t ->Table16[i] - last > 2)
1371 return FALSE;
1372 else
1373 last = t ->Table16[i];
1378 return TRUE;
1381 // Same, but for descending tables
1382 cmsBool CMSEXPORT cmsIsToneCurveDescending(const cmsToneCurve* t)
1384 _cmsAssert(t != NULL);
1386 return t ->Table16[0] > t ->Table16[t ->nEntries-1];
1390 // Another info fn: is out gamma table multisegment?
1391 cmsBool CMSEXPORT cmsIsToneCurveMultisegment(const cmsToneCurve* t)
1393 _cmsAssert(t != NULL);
1395 return t -> nSegments > 1;
1398 cmsInt32Number CMSEXPORT cmsGetToneCurveParametricType(const cmsToneCurve* t)
1400 _cmsAssert(t != NULL);
1402 if (t -> nSegments != 1) return 0;
1403 return t ->Segments[0].Type;
1406 // We need accuracy this time
1407 cmsFloat32Number CMSEXPORT cmsEvalToneCurveFloat(const cmsToneCurve* Curve, cmsFloat32Number v)
1409 _cmsAssert(Curve != NULL);
1411 // Check for 16 bits table. If so, this is a limited-precision tone curve
1412 if (Curve ->nSegments == 0) {
1414 cmsUInt16Number In, Out;
1416 In = (cmsUInt16Number) _cmsQuickSaturateWord(v * 65535.0);
1417 Out = cmsEvalToneCurve16(Curve, In);
1419 return (cmsFloat32Number) (Out / 65535.0);
1422 return (cmsFloat32Number) EvalSegmentedFn(Curve, v);
1425 // We need xput over here
1426 cmsUInt16Number CMSEXPORT cmsEvalToneCurve16(const cmsToneCurve* Curve, cmsUInt16Number v)
1428 cmsUInt16Number out;
1430 _cmsAssert(Curve != NULL);
1432 Curve ->InterpParams ->Interpolation.Lerp16(&v, &out, Curve ->InterpParams);
1433 return out;
1437 // Least squares fitting.
1438 // A mathematical procedure for finding the best-fitting curve to a given set of points by
1439 // minimizing the sum of the squares of the offsets ("the residuals") of the points from the curve.
1440 // The sum of the squares of the offsets is used instead of the offset absolute values because
1441 // this allows the residuals to be treated as a continuous differentiable quantity.
1443 // y = f(x) = x ^ g
1445 // R = (yi - (xi^g))
1446 // R2 = (yi - (xi^g))2
1447 // SUM R2 = SUM (yi - (xi^g))2
1449 // dR2/dg = -2 SUM x^g log(x)(y - x^g)
1450 // solving for dR2/dg = 0
1452 // g = 1/n * SUM(log(y) / log(x))
1454 cmsFloat64Number CMSEXPORT cmsEstimateGamma(const cmsToneCurve* t, cmsFloat64Number Precision)
1456 cmsFloat64Number gamma, sum, sum2;
1457 cmsFloat64Number n, x, y, Std;
1458 cmsUInt32Number i;
1460 _cmsAssert(t != NULL);
1462 sum = sum2 = n = 0;
1464 // Excluding endpoints
1465 for (i=1; i < (MAX_NODES_IN_CURVE-1); i++) {
1467 x = (cmsFloat64Number) i / (MAX_NODES_IN_CURVE-1);
1468 y = (cmsFloat64Number) cmsEvalToneCurveFloat(t, (cmsFloat32Number) x);
1470 // Avoid 7% on lower part to prevent
1471 // artifacts due to linear ramps
1473 if (y > 0. && y < 1. && x > 0.07) {
1475 gamma = log(y) / log(x);
1476 sum += gamma;
1477 sum2 += gamma * gamma;
1478 n++;
1482 // We need enough valid samples
1483 if (n <= 1) return -1.0;
1485 // Take a look on SD to see if gamma isn't exponential at all
1486 Std = sqrt((n * sum2 - sum * sum) / (n*(n-1)));
1488 if (Std > Precision)
1489 return -1.0;
1491 return (sum / n); // The mean
1495 // Retrieve parameters on one-segment tone curves
1497 cmsFloat64Number* CMSEXPORT cmsGetToneCurveParams(const cmsToneCurve* t)
1499 _cmsAssert(t != NULL);
1501 if (t->nSegments != 1) return NULL;
1502 return t->Segments[0].Params;