Debian Codegraph @

lib/jxl/cms/transfer_functions_test.cc

raw
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 #undef HWY_TARGET_INCLUDE
7 #define HWY_TARGET_INCLUDE "lib/jxl/cms/transfer_functions_test.cc"
8 #include "lib/jxl/cms/transfer_functions.h"
9
10 #include <cstdio>
11 #include <hwy/foreach_target.h>
12
13 #include "lib/jxl/base/random.h"
14 #include "lib/jxl/cms/transfer_functions-inl.h"
15 #include "lib/jxl/testing.h"
16
17 // Test utils
18 #include <hwy/highway.h>
19 #include <hwy/tests/hwy_gtest.h>
20 HWY_BEFORE_NAMESPACE();
21 namespace jxl {
22 namespace HWY_NAMESPACE {
23 namespace {
24
25 HWY_NOINLINE void TestPqEncodedFromDisplay() {
26 constexpr size_t kNumTrials = 1 << 23;
27 Rng rng(1);
28 float max_abs_err = 0;
29 HWY_FULL(float) d;
30 for (size_t i = 0; i < kNumTrials; i++) {
31 double intensity = 11000.0 + rng.UniformF(-150.0f, 150.0f);
32 TF_PQ tf_pq(intensity);
33 const float f = rng.UniformF(0.0f, 1.0f);
34 const float actual = GetLane(tf_pq.EncodedFromDisplay(d, Set(d, f)));
35 const float expected = TF_PQ_Base::EncodedFromDisplay(intensity, f);
36 const float abs_err = std::abs(expected - actual);
37 EXPECT_LT(abs_err, 6e-7) << "f = " << f;
38 max_abs_err = std::max(max_abs_err, abs_err);
39 }
40 printf("max abs err %e\n", static_cast<double>(max_abs_err));
41 }
42
43 HWY_NOINLINE void TestHlgEncodedFromDisplay() {
44 constexpr size_t kNumTrials = 1 << 23;
45 Rng rng(1);
46 float max_abs_err = 0;
47 HWY_FULL(float) d;
48 for (size_t i = 0; i < kNumTrials; i++) {
49 const float f = rng.UniformF(0.0f, 1.0f);
50 const float actual = GetLane(TF_HLG().EncodedFromDisplay(d, Set(d, f)));
51 const float expected = TF_HLG_Base::EncodedFromDisplay(f);
52 const float abs_err = std::abs(expected - actual);
53 EXPECT_LT(abs_err, 4e-7) << "f = " << f;
54 max_abs_err = std::max(max_abs_err, abs_err);
55 }
56 printf("max abs err %e\n", static_cast<double>(max_abs_err));
57 }
58
59 HWY_NOINLINE void TestPqDisplayFromEncoded() {
60 constexpr size_t kNumTrials = 1 << 23;
61 Rng rng(1);
62 float max_abs_err = 0;
63 HWY_FULL(float) d;
64 for (size_t i = 0; i < kNumTrials; i++) {
65 double intensity = 11000.0 + rng.UniformF(-150.0f, 150.0f);
66 TF_PQ tf_pq(intensity);
67 const float f = rng.UniformF(0.0f, 1.0f);
68 const float actual = GetLane(tf_pq.DisplayFromEncoded(d, Set(d, f)));
69 const float expected = TF_PQ_Base::DisplayFromEncoded(intensity, f);
70 const float abs_err = std::abs(expected - actual);
71 EXPECT_LT(abs_err, 3E-6) << "f = " << f;
72 max_abs_err = std::max(max_abs_err, abs_err);
73 }
74 printf("max abs err %e\n", static_cast<double>(max_abs_err));
75 }
76
77 } // namespace
78 // NOLINTNEXTLINE(google-readability-namespace-comments)
79 } // namespace HWY_NAMESPACE
80 } // namespace jxl
81 HWY_AFTER_NAMESPACE();
82
83 #if HWY_ONCE
84 namespace jxl {
85
86 class TransferFunctionsTargetTest : public hwy::TestWithParamTarget {};
87 HWY_TARGET_INSTANTIATE_TEST_SUITE_P(TransferFunctionsTargetTest);
88
89 HWY_EXPORT_AND_TEST_P(TransferFunctionsTargetTest, TestPqEncodedFromDisplay);
90 HWY_EXPORT_AND_TEST_P(TransferFunctionsTargetTest, TestHlgEncodedFromDisplay);
91 HWY_EXPORT_AND_TEST_P(TransferFunctionsTargetTest, TestPqDisplayFromEncoded);
92
93 } // namespace jxl
94 #endif // HWY_ONCE