mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:07:45 +00:00
LibGfx/ICC: Extract a lerp_1d() function
Pure code move, no behavior change.
This commit is contained in:
parent
4c4daf4a62
commit
9372f6d726
1 changed files with 16 additions and 6 deletions
|
@ -20,6 +20,21 @@
|
||||||
|
|
||||||
namespace Gfx::ICC {
|
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<class T>
|
||||||
|
float lerp_1d(ReadonlySpan<T> values, float x)
|
||||||
|
{
|
||||||
|
size_t n = values.size() - 1;
|
||||||
|
size_t i = static_cast<size_t>(x * n);
|
||||||
|
if (i == values.size() - 1)
|
||||||
|
--i;
|
||||||
|
|
||||||
|
return mix(static_cast<float>(values[i]), static_cast<float>(values[i + 1]), x * n - i);
|
||||||
|
}
|
||||||
|
|
||||||
// Does multi-dimensional linear interpolation over a lookup table.
|
// Does multi-dimensional linear interpolation over a lookup table.
|
||||||
// `size(i)` should returns the number of samples in the i'th dimension.
|
// `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.
|
// `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)
|
if (values().size() == 1)
|
||||||
return powf(x, values()[0] / (float)0x100);
|
return powf(x, values()[0] / (float)0x100);
|
||||||
|
|
||||||
size_t n = values().size() - 1;
|
return lerp_1d(values().span(), x) / 65535.0f;
|
||||||
size_t i = static_cast<size_t>(x * n);
|
|
||||||
if (i == values().size() - 1)
|
|
||||||
--i;
|
|
||||||
|
|
||||||
return mix(values()[i] / 65535.f, values()[i + 1] / 65535.f, x * n - i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// y must be in [0..1].
|
// y must be in [0..1].
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue