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