summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/gamma_correct_test.cc
blob: 131ec4fa83d50bbb1ee95ecef3538907743cf2de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <stdlib.h>

#include <algorithm>

#include "lib/jxl/enc_gamma_correct.h"
#include "lib/jxl/testing.h"

namespace jxl {
namespace {

TEST(GammaCorrectTest, TestLinearToSrgbEdgeCases) {
  EXPECT_EQ(0, LinearToSrgb8Direct(0.0));
  EXPECT_NEAR(0, LinearToSrgb8Direct(1E-6f), 2E-5);
  EXPECT_EQ(0, LinearToSrgb8Direct(-1E-6f));
  EXPECT_EQ(0, LinearToSrgb8Direct(-1E6));
  EXPECT_NEAR(1, LinearToSrgb8Direct(1 - 1E-6f), 1E-5);
  EXPECT_EQ(1, LinearToSrgb8Direct(1 + 1E-6f));
  EXPECT_EQ(1, LinearToSrgb8Direct(1E6));
}

TEST(GammaCorrectTest, TestRoundTrip) {
  // NOLINTNEXTLINE(clang-analyzer-security.FloatLoopCounter)
  for (double linear = 0.0; linear <= 1.0; linear += 1E-7) {
    const double srgb = LinearToSrgb8Direct(linear);
    const double linear2 = Srgb8ToLinearDirect(srgb);
    ASSERT_LT(std::abs(linear - linear2), 2E-13)
        << "linear = " << linear << ", linear2 = " << linear2;
  }
}

}  // namespace
}  // namespace jxl