From 4793b9e49be0131fff1fee2f57c15dd5d1c9d71f Mon Sep 17 00:00:00 2001
From: Paul Miller <>
Date: Tue, 12 Mar 2013 11:37:28 +0000
Subject: [PATCH 1/2] cmsBuildTabulatedToneCurveFloat fix

cmsBuildTabulatedToneCurveFloat should build a curve which evaluates to
value[0] for x = 0
also added additional segment for 1.0 to plus infinity for compliance
with ICC 4.3 spec which requires first and last segments to be
functions.  I'm using constant sections for the 2 end segments which
means the curve is not invertible - linear extrapolation of the sample
data may be a better solution for some applications...
---
 src/cmsgamma.c |   27 ++++++++++++++++++++-------
 1 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/cmsgamma.c b/src/cmsgamma.c
index 9fdf79e..5676f27 100644
--- a/src/cmsgamma.c
+++ b/src/cmsgamma.c
@@ -600,28 +600,41 @@ cmsToneCurve* CMSEXPORT cmsBuildSegmentedToneCurve(cmsContext ContextID,
 // Use a segmented curve to store the floating point table
 cmsToneCurve* CMSEXPORT cmsBuildTabulatedToneCurveFloat(cmsContext ContextID, cmsUInt32Number nEntries, const cmsFloat32Number values[])
 {
-    cmsCurveSegment Seg[2];
+    cmsCurveSegment Seg[3];
 
-    // Initialize segmented curve part up to 0
-    Seg[0].x0 = -1;
+	// A segmented tone curve should have function segments in the first and last positions
+    // Initialize segmented curve part up to 0 to constant value = samples[0]
+    Seg[0].x0 = MINUS_INF;
     Seg[0].x1 = 0;
     Seg[0].Type = 6;
 
     Seg[0].Params[0] = 1;
     Seg[0].Params[1] = 0;
     Seg[0].Params[2] = 0;
-    Seg[0].Params[3] = 0;
+    Seg[0].Params[3] = values[0];
     Seg[0].Params[4] = 0;
 
-    // From zero to any
+    // From zero to 1
     Seg[1].x0 = 0;
     Seg[1].x1 = 1.0;
     Seg[1].Type = 0;
 
     Seg[1].nGridPoints = nEntries;
     Seg[1].SampledPoints = (cmsFloat32Number*) values;
-
-    return cmsBuildSegmentedToneCurve(ContextID, 2, Seg);
+	
+	// Final segment is constant = lastsample
+	Seg[2].x0 = 1.0;
+	Seg[2].x1 = PLUS_INF;
+	Seg[2].Type = 6;
+	
+    Seg[2].Params[0] = 1;
+    Seg[2].Params[1] = 0;
+    Seg[2].Params[2] = 0;
+    Seg[2].Params[3] = values[nEntries-1];
+    Seg[2].Params[4] = 0;
+	
+
+    return cmsBuildSegmentedToneCurve(ContextID, 3, Seg);
 }
 
 // Parametric curves
-- 
1.7.7.5 (Apple Git-26)

