Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/src/gainmapmath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ float srgbInvOetfLUT(float e_gamma) {
}

Color srgbInvOetfLUT(Color e_gamma) {
return {{{srgbInvOetfLUT(e_gamma.r), srgbInvOetfLUT(e_gamma.g), srgbInvOetfLUT(e_gamma.b)}}};
static LookUpTable kSrgbLut(kSrgbInvOETFNumEntries, static_cast<float (*)(float)>(srgbInvOetfLUT));
const std::vector<float>& lut = kSrgbLut.getTable();
const auto index = [](float value) {
int32_t lut_index = static_cast<int32_t>(value * (kSrgbInvOETFNumEntries - 1) + 0.5);
return CLIP3(lut_index, 0, kSrgbInvOETFNumEntries - 1);
};
return {{{lut[index(e_gamma.r)], lut[index(e_gamma.g)], lut[index(e_gamma.b)]}}};
}

// See IEC 61966-2-1/Amd 1:2003, Equations F.10 and F.11.
Expand Down
42 changes: 42 additions & 0 deletions tests/gainmapmath_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <array>
#include <cmath>
#include <vector>

#include "ultrahdr/gainmapmath.h"
Expand Down Expand Up @@ -1383,6 +1385,46 @@ TEST_F(GainMapMathTest, srgbInvOetfLUT) {
}
}

TEST_F(GainMapMathTest, SrgbInvOetfLutRgbPreservesScalarIndexing) {
constexpr int32_t kLast = kSrgbInvOETFNumEntries - 1;
const auto expected = [=](float value) {
int32_t index = static_cast<int32_t>(value * kLast + 0.5);
index = CLIP3(index, 0, kLast);
return srgbInvOetf(static_cast<float>(index) / static_cast<float>(kLast));
};
const std::array<std::array<int, 3>, 6> permutations = {{
{{0, 1, 2}}, {{0, 2, 1}}, {{1, 0, 2}},
{{1, 2, 0}}, {{2, 0, 1}}, {{2, 1, 0}},
}};
const auto check = [&](float value) {
const std::array<float, 3> source = {value, 0.123f, 0.876f};
for (const auto& permutation : permutations) {
const std::array<float, 3> input = {
source[permutation[0]], source[permutation[1]], source[permutation[2]]};
const Color actual = srgbInvOetfLUT({{{input[0], input[1], input[2]}}});
const std::array<float, 3> output = {actual.r, actual.g, actual.b};
for (size_t channel = 0; channel < output.size(); ++channel) {
const float scalar = srgbInvOetfLUT(input[channel]);
EXPECT_EQ(output[channel], scalar);
EXPECT_EQ(scalar, expected(input[channel]));
}
}
};

for (int32_t index = 0; index <= kLast; ++index) {
check(static_cast<float>(index) / static_cast<float>(kLast));
}
for (int32_t index = 0; index < kLast; ++index) {
const float boundary = static_cast<float>((static_cast<double>(index) + 0.5) / kLast);
check(std::nextafter(boundary, -1.0f));
check(boundary);
check(std::nextafter(boundary, 2.0f));
}
for (const float value : {-1000.0f, -1.0f, -0.0f, 0.0f, 1.0f, 2.0f, 1000.0f}) {
check(value);
}
}

TEST_F(GainMapMathTest, applyGainLUT) {
for (float boost = 1.5; boost <= 12; boost++) {
uhdr_gainmap_metadata_ext_t metadata(kJpegrVersion);
Expand Down
Loading