diff --git a/third_party/skcms/src/Curve.c b/third_party/skcms/src/Curve.c index f52eb983fe..2b5a042500 100644 --- a/third_party/skcms/src/Curve.c +++ b/third_party/skcms/src/Curve.c @@ -10,25 +10,36 @@ #include "TransferFunction.h" #include +static float minus_1_ulp(float x) { + int32_t bits; + memcpy(&bits, &x, sizeof(bits)); + bits = bits - 1; + memcpy(&x, &bits, sizeof(bits)); + return x; +} + float skcms_eval_curve(const skcms_Curve* curve, float x) { if (curve->table_entries == 0) { return skcms_TransferFunction_eval(&curve->parametric, x); } - // TODO: today we should always hit an entry exactly, but if that changes, lerp? - // (We add half to account for slight int -> float -> int round tripping issues.) - float fx = x*(curve->table_entries - 1); - int ix = (int)( fx + 0.5f ); - - assert ( fabsf_(fx - (float)ix) < 0.0005 ); + float ix = fmaxf_(0, fminf_(x, 1)) * (curve->table_entries - 1); + int lo = (int) ix, + hi = (int)minus_1_ulp(ix + 1.0f); + float t = ix - (float)lo; + float l, h; if (curve->table_8) { - return curve->table_8[ix] * (1/255.0f); + l = curve->table_8[lo] * (1/255.0f); + h = curve->table_8[hi] * (1/255.0f); } else { - uint16_t be; - memcpy(&be, curve->table_16 + 2*ix, 2); - - uint16_t le = ((be << 8) | (be >> 8)) & 0xffff; - return le * (1/65535.0f); + uint16_t be_l, be_h; + memcpy(&be_l, curve->table_16 + 2*lo, 2); + memcpy(&be_h, curve->table_16 + 2*hi, 2); + uint16_t le_l = ((be_l << 8) | (be_l >> 8)) & 0xffff; + uint16_t le_h = ((be_h << 8) | (be_h >> 8)) & 0xffff; + l = le_l * (1/65535.0f); + h = le_h * (1/65535.0f); } + return l + (h-l)*t; } diff --git a/third_party/skcms/version.sha1 b/third_party/skcms/version.sha1 index 14bc5efe03..a2c36d6635 100755 --- a/third_party/skcms/version.sha1 +++ b/third_party/skcms/version.sha1 @@ -1 +1 @@ -5cc905db968da1cad33f473f2d7700972bb959a1 \ No newline at end of file +e4c0e54a9c1998286f0d4760a71f42ae23679541 \ No newline at end of file