diff --git a/Userland/Libraries/LibGfx/ICC/TagTypes.h b/Userland/Libraries/LibGfx/ICC/TagTypes.h index 6a82611047..761a3017b7 100644 --- a/Userland/Libraries/LibGfx/ICC/TagTypes.h +++ b/Userland/Libraries/LibGfx/ICC/TagTypes.h @@ -20,6 +20,21 @@ namespace Gfx::ICC { +// Does one-dimensional linear interpolation over a lookup table. +// Takes a span of values an x in [0.0, 1.0]. +// Finds the two values in the span closest to x (where x = 0.0 is the span's first element and x = 1.0 the span's last element), +// and linearly interpolates between those two values, assumes all values are the same amount apart. +template +float lerp_1d(ReadonlySpan values, float x) +{ + size_t n = values.size() - 1; + size_t i = static_cast(x * n); + if (i == values.size() - 1) + --i; + + return mix(static_cast(values[i]), static_cast(values[i + 1]), x * n - i); +} + // Does multi-dimensional linear interpolation over a lookup table. // `size(i)` should returns the number of samples in the i'th dimension. // `sample()` gets a vector where 0 <= i'th coordinate < size(i) and should return the value of the look-up table at that position. @@ -207,12 +222,7 @@ public: if (values().size() == 1) return powf(x, values()[0] / (float)0x100); - size_t n = values().size() - 1; - size_t i = static_cast(x * n); - if (i == values().size() - 1) - --i; - - return mix(values()[i] / 65535.f, values()[i + 1] / 65535.f, x * n - i); + return lerp_1d(values().span(), x) / 65535.0f; } // y must be in [0..1].