summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/butteraugli_test.cc
blob: 3fdec09725d7a4fe17b2cbb69de20421fa1684cd (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// 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 <jxl/butteraugli.h>
#include <jxl/butteraugli_cxx.h>

#include "lib/jxl/test_image.h"
#include "lib/jxl/test_utils.h"
#include "lib/jxl/testing.h"

TEST(ButteraugliTest, Lossless) {
  uint32_t xsize = 171;
  uint32_t ysize = 219;
  std::vector<uint8_t> pixels = jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};

  JxlButteraugliApiPtr api(JxlButteraugliApiCreate(nullptr));
  JxlButteraugliResultPtr result(JxlButteraugliCompute(
      api.get(), xsize, ysize, &pixel_format, pixels.data(), pixels.size(),
      &pixel_format, pixels.data(), pixels.size()));
  EXPECT_EQ(0.0, JxlButteraugliResultGetDistance(result.get(), 8.0));
}

TEST(ButteraugliTest, Distmap) {
  uint32_t xsize = 171;
  uint32_t ysize = 219;
  std::vector<uint8_t> pixels = jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};

  JxlButteraugliApiPtr api(JxlButteraugliApiCreate(nullptr));
  JxlButteraugliResultPtr result(JxlButteraugliCompute(
      api.get(), xsize, ysize, &pixel_format, pixels.data(), pixels.size(),
      &pixel_format, pixels.data(), pixels.size()));
  EXPECT_EQ(0.0, JxlButteraugliResultGetDistance(result.get(), 8.0));
  const float* distmap;
  uint32_t row_stride;
  JxlButteraugliResultGetDistmap(result.get(), &distmap, &row_stride);
  for (uint32_t y = 0; y < ysize; y++) {
    for (uint32_t x = 0; x < xsize; x++) {
      EXPECT_EQ(0.0, distmap[y * row_stride + x]);
    }
  }
}

TEST(ButteraugliTest, Distorted) {
  uint32_t xsize = 171;
  uint32_t ysize = 219;
  std::vector<uint8_t> orig_pixels =
      jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  std::vector<uint8_t> dist_pixels =
      jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  dist_pixels[0] += 128;

  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};

  JxlButteraugliApiPtr api(JxlButteraugliApiCreate(nullptr));
  JxlButteraugliResultPtr result(JxlButteraugliCompute(
      api.get(), xsize, ysize, &pixel_format, orig_pixels.data(),
      orig_pixels.size(), &pixel_format, dist_pixels.data(),
      dist_pixels.size()));
  EXPECT_NE(0.0, JxlButteraugliResultGetDistance(result.get(), 8.0));
}

TEST(ButteraugliTest, Api) {
  uint32_t xsize = 171;
  uint32_t ysize = 219;
  std::vector<uint8_t> orig_pixels =
      jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  std::vector<uint8_t> dist_pixels =
      jxl::test::GetSomeTestImage(xsize, ysize, 4, 0);
  dist_pixels[0] += 128;

  JxlPixelFormat pixel_format = {4, JXL_TYPE_UINT16, JXL_BIG_ENDIAN, 0};

  JxlButteraugliApiPtr api(JxlButteraugliApiCreate(nullptr));
  JxlButteraugliApiSetHFAsymmetry(api.get(), 1.0f);
  JxlButteraugliApiSetIntensityTarget(api.get(), 250.0f);
  JxlButteraugliResultPtr result(JxlButteraugliCompute(
      api.get(), xsize, ysize, &pixel_format, orig_pixels.data(),
      orig_pixels.size(), &pixel_format, dist_pixels.data(),
      dist_pixels.size()));
  double distance0 = JxlButteraugliResultGetDistance(result.get(), 8.0);

  JxlButteraugliApiSetHFAsymmetry(api.get(), 2.0f);
  result.reset(JxlButteraugliCompute(api.get(), xsize, ysize, &pixel_format,
                                     orig_pixels.data(), orig_pixels.size(),
                                     &pixel_format, dist_pixels.data(),
                                     dist_pixels.size()));
  double distance1 = JxlButteraugliResultGetDistance(result.get(), 8.0);

  EXPECT_NE(distance0, distance1);

  JxlButteraugliApiSetIntensityTarget(api.get(), 80.0f);
  result.reset(JxlButteraugliCompute(api.get(), xsize, ysize, &pixel_format,
                                     orig_pixels.data(), orig_pixels.size(),
                                     &pixel_format, dist_pixels.data(),
                                     dist_pixels.size()));
  double distance2 = JxlButteraugliResultGetDistance(result.get(), 8.0);

  EXPECT_NE(distance1, distance2);
}