File: gamma_compression_effect_test.cpp

package info (click to toggle)
movit 1.6.3-5
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,080 kB
  • sloc: cpp: 16,502; sh: 3,254; makefile: 164
file content (212 lines) | stat: -rw-r--r-- 7,246 bytes parent folder | download | duplicates (4)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Unit tests for GammaCompressionEffect.
//
// Pretty much the inverse of the GammaExpansionEffect tests;
// EffectChainTest tests that they are actually inverses.
// However, the accuracy tests are somewhat simpler, since we
// only need to care about absolute errors and not relative.

#include <epoxy/gl.h>
#include <math.h>

#include "gtest/gtest.h"
#include "gtest/gtest-message.h"
#include "image_format.h"
#include "test_util.h"

namespace movit {

TEST(GammaCompressionEffectTest, sRGB_KeyValues) {
	float data[] = {
		0.0f, 1.0f,
		0.00309f, 0.00317f,   // On either side of the discontinuity.
		-0.5f, 1.5f,          // To check clamping.
	};
	float expected_data[] = {
		0.0f, 1.0f,
		0.040f, 0.041f,
		0.0f, 1.0f,
	};
	float out_data[6];
	EffectChainTester tester(data, 2, 3, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);

	expect_equal(expected_data, out_data, 2, 3);
}

TEST(GammaCompressionEffectTest, sRGB_RampAlwaysIncreases) {
	float data[256], out_data[256];
	for (unsigned i = 0; i < 256; ++i) {
		data[i] = i / 255.0f;
	}
	EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);

	for (unsigned i = 1; i < 256; ++i) {
		EXPECT_GT(out_data[i], out_data[i - 1])
		   << "No increase between " << i-1 << " and " << i;
	}
}

TEST(GammaCompressionEffectTest, sRGB_Accuracy) {
	float data[256], expected_data[256], out_data[256];

	for (int i = 0; i < 256; ++i) {
		double x = i / 255.0;

		expected_data[i] = x;
		data[i] = srgb_to_linear(x);
	}

	EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);

	// Maximum absolute error is 25% of one pixel level. For comparison,
	// a straightforward ALU solution (using a branch and pow()), used as a
	// “high anchor” to indicate limitations of float arithmetic etc.,
	// reaches maximum absolute error of 3.7% of one pixel level
	// and rms of 3.2e-6.
	expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-4);
}

TEST(GammaCompressionEffectTest, Rec709_KeyValues) {
	float data[] = {
		0.0f, 1.0f,
		0.017778f, 0.018167f,  // On either side of the discontinuity.
	};
	float expected_data[] = {
		0.0f, 1.0f,
		0.080f, 0.082f,
	};
	float out_data[4];
	EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);

	expect_equal(expected_data, out_data, 2, 2);
}

TEST(GammaCompressionEffectTest, Rec709_RampAlwaysIncreases) {
	float data[256], out_data[256];
	for (unsigned i = 0; i < 256; ++i) {
		data[i] = i / 255.0f;
	}
	EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);

	for (unsigned i = 1; i < 256; ++i) {
		EXPECT_GT(out_data[i], out_data[i - 1])
		   << "No increase between " << i-1 << " and " << i;
	}
}

TEST(GammaCompressionEffectTest, Rec709_Accuracy) {
	float data[256], expected_data[256], out_data[256];

	for (int i = 0; i < 256; ++i) {
		double x = i / 255.0;

		expected_data[i] = x;

		// Rec. 2020, page 3.
		if (x < 0.018 * 4.5) {
			data[i] = x / 4.5;
		} else {
			data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
		}
	}

	EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);

	// Maximum absolute error is 25% of one pixel level. For comparison,
	// a straightforward ALU solution (using a branch and pow()), used as a
	// “high anchor” to indicate limitations of float arithmetic etc.,
	// reaches maximum absolute error of 3.7% of one pixel level
	// and rms of 3.5e-6.
	expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-5);
}

// This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
// input range and somewhat looser error bounds. (One could claim that this is
// already on the limit of what we can reasonably do with fp16 input, if you
// look at the local relative error.)
TEST(GammaCompressionEffectTest, Rec2020_10Bit_Accuracy) {
	float data[1024], expected_data[1024], out_data[1024];

	for (int i = 0; i < 1024; ++i) {
		double x = i / 1023.0;

		expected_data[i] = x;

		// Rec. 2020, page 3.
		if (x < 0.018 * 4.5) {
			data[i] = x / 4.5;
		} else {
			data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
		}
	}

	EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT);

	// Maximum absolute error is 30% of one pixel level. For comparison,
	// a straightforward ALU solution (using a branch and pow()), used as a
	// “high anchor” to indicate limitations of float arithmetic etc.,
	// reaches maximum absolute error of 25.2% of one pixel level
	// and rms of 1.8e-6, so this is probably mostly related to input precision.
	expect_equal(expected_data, out_data, 1024, 1, 0.30 / 1023.0, 1e-5);
}

TEST(GammaCompressionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
	float data[4096];
	for (unsigned i = 0; i < 4096; ++i) {
		data[i] = i / 4095.0f;
	}
	float out_data_709[4096];
	float out_data_2020[4096];

	EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
	EffectChainTester tester2(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);

	double sqdiff = 0.0;
	for (unsigned i = 0; i < 4096; ++i) {
		EXPECT_NEAR(out_data_709[i], out_data_2020[i], 0.001);
		sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
	}
	EXPECT_GT(sqdiff, 1e-6);
}

// The fp16 _input_ provided by FlatInput is not enough to distinguish between
// all of the possible 12-bit input values (every other level translates to the
// same value). Thus, this test has extremely loose bounds; if we ever decide
// to start supporting fp32, we should re-run this and tighten them a lot.
TEST(GammaCompressionEffectTest, Rec2020_12Bit_Inaccuracy) {
	float data[4096], expected_data[4096], out_data[4096];

	for (int i = 0; i < 4096; ++i) {
		double x = i / 4095.0;

		expected_data[i] = x;

		// Rec. 2020, page 3.
		if (x < 0.0181 * 4.5) {
			data[i] = x / 4.5;
		} else {
			data[i] = pow((x + 0.0993) / 1.0993, 1.0 / 0.45);
		}
	}

	EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);

	// Maximum absolute error is 120% of one pixel level. For comparison,
	// a straightforward ALU solution (using a branch and pow()), used as a
	// “high anchor” to indicate limitations of float arithmetic etc.,
	// reaches maximum absolute error of 71.1% of one pixel level
	// and rms of 0.9e-6, so this is probably a combination of input
	// precision and inaccuracies in the polynomial approximation.
	expect_equal(expected_data, out_data, 4096, 1, 1.2 / 4095.0, 1e-5);
}

}  // namespace movit