Skip to content

Instantly share code, notes, and snippets.

@begla
Last active March 1, 2023 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save begla/00c39241b529dd7b3969be96aa354abd to your computer and use it in GitHub Desktop.
Save begla/00c39241b529dd7b3969be96aa354abd to your computer and use it in GitHub Desktop.
Custom Log Transfer Function
// https://colab.research.google.com/drive/1NwjaD0NzNMzQeNQqZECj33PdcYGkeBM4#scrollTo=d61Knj0O9Lwf
namespace CustomLog
{
static struct Params
{
Params(float w0, float w1, float o0, float p0, float p1)
: w0(w0), w1(w1), o0(o0), p0(p0), p1(p1)
{
computeConstants();
}
void computeConstants()
{
s0 = (w1 - p1) / glm::log((w0 - o0) / (p0 - o0));
k = s0 / (p0 - o0);
o1 = w1 - s0 * glm::log(w0 - o0);
y0 = p1 - k * p0;
}
float w0, w1, o0, p0, p1;
float s0, k, o1, y0;
} params_ = {200.000000f, 1.000000f, -0.0066f, 0.022051f, 0.08f};
inline glm::vec3 fromLinear(const glm::vec3& x)
{
static const auto map = [](float x) {
if (x < params_.p0)
return params_.k * x + params_.y0;
return params_.s0 * glm::log(x - params_.o0) + params_.o1;
};
return glm::vec3(map(x.x), map(x.y), map(x.z));
}
inline glm::vec3 toLinear(const glm::vec3& x)
{
static const auto map = [](float x) {
if (x < params_.p1)
return (x - params_.y0) / params_.k;
return glm::exp((x - params_.o1) / params_.s0) + params_.o0;
};
return glm::vec3(map(x.x), map(x.y), map(x.z));
}
void computeNonNegativeFit()
{
params_.p0 = params_.p1 / params_.k;
params_.computeConstants();
}
} // namespace CustomLog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment