6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
9 #include "flat_input.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
26 static ResourcePool *resource_pool = NULL;
28 resource_pool = new ResourcePool();
33 // Flip upside-down to compensate for different origin.
35 void vertical_flip(T *data, unsigned width, unsigned height)
37 for (unsigned y = 0; y < height / 2; ++y) {
38 unsigned flip_y = height - y - 1;
39 for (unsigned x = 0; x < width; ++x) {
40 swap(data[y * width + x], data[flip_y * width + x]);
47 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
48 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
49 GLenum framebuffer_format)
50 : chain(width, height, get_static_pool()),
53 framebuffer_format(framebuffer_format),
57 CHECK(init_movit(".", MOVIT_DEBUG_OFF));
60 add_input(data, pixel_format, color_space, gamma_curve);
64 EffectChainTester::~EffectChainTester()
68 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
71 format.color_space = color_space;
72 format.gamma_curve = gamma_curve;
74 if (input_width == -1) {
77 if (input_height == -1) {
78 input_height = height;
81 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
82 input->set_pixel_data(data);
83 chain.add_input(input);
87 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
90 format.color_space = color_space;
91 format.gamma_curve = gamma_curve;
93 if (input_width == -1) {
96 if (input_height == -1) {
97 input_height = height;
100 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
101 input->set_pixel_data(data);
102 chain.add_input(input);
106 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
108 internal_run<float>(out_data, NULL, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
111 void EffectChainTester::run(float *out_data, float *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
113 internal_run<float>(out_data, out_data2, NULL, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
116 void EffectChainTester::run(float *out_data, float *out_data2, float *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
118 internal_run(out_data, out_data2, out_data3, GL_FLOAT, format, color_space, gamma_curve, alpha_format);
121 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
123 internal_run<unsigned char>(out_data, NULL, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
126 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
128 internal_run<unsigned char>(out_data, out_data2, NULL, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
131 void EffectChainTester::run(unsigned char *out_data, unsigned char *out_data2, unsigned char *out_data3, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
133 internal_run(out_data, out_data2, out_data3, GL_UNSIGNED_BYTE, format, color_space, gamma_curve, alpha_format);
137 void EffectChainTester::internal_run(T *out_data, T *out_data2, T *out_data3, GLenum internal_format, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
140 finalize_chain(color_space, gamma_curve, alpha_format);
144 if (framebuffer_format == GL_RGBA8) {
145 type = GL_UNSIGNED_BYTE;
146 } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
149 // Add more here as needed.
153 unsigned num_outputs;
154 if (out_data3 != NULL) {
156 } else if (out_data2 != NULL) {
162 GLuint fbo, texnum[3];
164 glGenTextures(num_outputs, texnum);
166 for (unsigned i = 0; i < num_outputs; ++i) {
167 glBindTexture(GL_TEXTURE_2D, texnum[i]);
169 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
173 glGenFramebuffers(1, &fbo);
175 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
177 for (unsigned i = 0; i < num_outputs; ++i) {
178 glFramebufferTexture2D(
180 GL_COLOR_ATTACHMENT0 + i,
187 GLenum bufs[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
188 glDrawBuffers(num_outputs, bufs);
190 chain.render_to_fbo(fbo, width, height);
192 T *data[3] = { out_data, out_data2, out_data3 };
194 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
196 for (unsigned i = 0; i < num_outputs; ++i) {
198 glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
199 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
200 // GLES will only read GL_RGBA.
201 T *temp = new T[width * height * 4];
202 glReadPixels(0, 0, width, height, GL_RGBA, internal_format, temp);
204 if (format == GL_ALPHA) {
205 for (unsigned i = 0; i < width * height; ++i) {
206 ptr[i] = temp[i * 4 + 3];
208 } else if (format == GL_BLUE) {
209 for (unsigned i = 0; i < width * height; ++i) {
210 ptr[i] = temp[i * 4 + 2];
213 for (unsigned i = 0; i < width * height; ++i) {
214 ptr[i] = temp[i * 4];
219 glReadPixels(0, 0, width, height, format, internal_format, ptr);
223 if (format == GL_RGBA) {
224 vertical_flip(ptr, width * 4, height);
226 vertical_flip(ptr, width, height);
230 glDeleteFramebuffers(1, &fbo);
232 glDeleteTextures(num_outputs, texnum);
236 void EffectChainTester::add_output(const ImageFormat &format, OutputAlphaFormat alpha_format)
238 chain.add_output(format, alpha_format);
242 void EffectChainTester::add_ycbcr_output(const ImageFormat &format, OutputAlphaFormat alpha_format, const YCbCrFormat &ycbcr_format, YCbCrOutputSplitting output_splitting)
244 chain.add_ycbcr_output(format, alpha_format, ycbcr_format, output_splitting);
248 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
252 ImageFormat image_format;
253 image_format.color_space = color_space;
254 image_format.gamma_curve = gamma_curve;
255 chain.add_output(image_format, alpha_format);
262 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
264 float largest_difference = -1.0f;
265 float squared_difference = 0.0f;
266 int largest_diff_x = -1, largest_diff_y = -1;
268 for (unsigned y = 0; y < height; ++y) {
269 for (unsigned x = 0; x < width; ++x) {
270 float diff = ref[y * width + x] - result[y * width + x];
271 if (fabs(diff) > largest_difference) {
272 largest_difference = fabs(diff);
276 squared_difference += diff * diff;
280 EXPECT_LT(largest_difference, largest_difference_limit)
281 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
282 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
283 << "Result: " << result[largest_diff_y * width + largest_diff_x];
285 float rms = sqrt(squared_difference) / (width * height);
286 EXPECT_LT(rms, rms_limit);
288 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
289 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
291 fprintf(stderr, "Reference:\n");
292 for (unsigned y = 0; y < height; ++y) {
293 for (unsigned x = 0; x < width; ++x) {
294 fprintf(stderr, "%7.4f ", ref[y * width + x]);
296 fprintf(stderr, "\n");
299 fprintf(stderr, "\nResult:\n");
300 for (unsigned y = 0; y < height; ++y) {
301 for (unsigned x = 0; x < width; ++x) {
302 fprintf(stderr, "%7.4f ", result[y * width + x]);
304 fprintf(stderr, "\n");
309 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
314 float *ref_float = new float[width * height];
315 float *result_float = new float[width * height];
317 for (unsigned y = 0; y < height; ++y) {
318 for (unsigned x = 0; x < width; ++x) {
319 ref_float[y * width + x] = ref[y * width + x];
320 result_float[y * width + x] = result[y * width + x];
324 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
327 delete[] result_float;
330 void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit)
332 double squared_difference = 0.0;
333 for (unsigned i = 0; i < num_values; ++i) {
334 double absolute_error = fabs(expected[i] - result[i]);
335 squared_difference += absolute_error * absolute_error;
336 EXPECT_LT(absolute_error, absolute_error_limit);
338 if (expected[i] > 0.0) {
339 double relative_error = fabs(absolute_error / expected[i]);
341 EXPECT_LT(relative_error, relative_error_limit);
343 if (i < num_values - 1) {
344 double delta = expected[i + 1] - expected[i];
345 double local_relative_error = fabs(absolute_error / delta);
346 EXPECT_LT(local_relative_error, local_relative_error_limit);
349 double rms = sqrt(squared_difference) / num_values;
350 EXPECT_LT(rms, rms_limit);
353 double srgb_to_linear(double x)
355 // From the Wikipedia article on sRGB.
359 return pow((x + 0.055) / 1.055, 2.4);
363 double linear_to_srgb(double x)
365 // From the Wikipedia article on sRGB.
369 return 1.055 * pow(x, 1.0 / 2.4) - 0.055;