]> git.sesse.net Git - movit/blob - test_util.cpp
Split test utilities out into its own file.
[movit] / test_util.cpp
1 #include "test_util.h"
2 #include "flat_input.h"
3 #include "gtest/gtest.h"
4
5 #include <stdio.h>
6 #include <math.h>
7
8 #include <algorithm>
9
10 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height, ColorSpace color_space, GammaCurve gamma_curve)
11         : chain(width, height), width(width), height(height)
12 {
13         ImageFormat format;
14         format.color_space = color_space;
15         format.gamma_curve = gamma_curve;
16
17         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
18         input->set_pixel_data(data);
19         chain.add_input(input);
20 }
21
22 void EffectChainTester::run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
23 {
24         ImageFormat format;
25         format.color_space = color_space;
26         format.gamma_curve = gamma_curve;
27         chain.add_output(format);
28         chain.finalize();
29
30         glViewport(0, 0, width, height);
31         chain.render_to_screen();
32
33         glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
34
35         // Flip upside-down to compensate for different origin.
36         for (unsigned y = 0; y < height / 2; ++y) {
37                 unsigned flip_y = height - y - 1;
38                 for (unsigned x = 0; x < width; ++x) {
39                         std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
40                 }
41         }
42 }
43
44 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
45 {
46         float largest_difference = -1.0f;
47         float squared_difference = 0.0f;
48
49         for (unsigned y = 0; y < height; ++y) {
50                 for (unsigned x = 0; x < width; ++x) {
51                         float diff = ref[y * width + x] - result[y * width + x];
52                         largest_difference = std::max(largest_difference, fabsf(diff));
53                         squared_difference += diff * diff;
54                 }
55         }
56
57         const float largest_difference_limit = 1.5 / 255.0;
58         const float rms_limit = 0.5 / 255.0;
59
60         EXPECT_LT(largest_difference, largest_difference_limit);
61
62         float rms = sqrt(squared_difference) / (width * height);
63         EXPECT_LT(rms, rms_limit);
64
65         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
66                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
67
68                 fprintf(stderr, "Reference:\n");
69                 for (unsigned y = 0; y < height; ++y) {
70                         for (unsigned x = 0; x < width; ++x) {
71                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
72                         }
73                         fprintf(stderr, "\n");
74                 }
75
76                 fprintf(stderr, "\nResult:\n");
77                 for (unsigned y = 0; y < height; ++y) {
78                         for (unsigned x = 0; x < width; ++x) {
79                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
80                         }
81                         fprintf(stderr, "\n");
82                 }
83         }
84 }