Factorize out some stuff from EffectChainTester::run().
[movit] / test_util.cpp
1 #include "init.h"
2 #include "test_util.h"
3 #include "flat_input.h"
4 #include "gtest/gtest.h"
5
6 #include <stdio.h>
7 #include <math.h>
8
9 #include <algorithm>
10
11 namespace {
12
13 // Flip upside-down to compensate for different origin.
14 template<class T>
15 void vertical_flip(T *data, unsigned width, unsigned height)
16 {
17         for (unsigned y = 0; y < height / 2; ++y) {
18                 unsigned flip_y = height - y - 1;
19                 for (unsigned x = 0; x < width; ++x) {
20                         std::swap(data[y * width + x], data[flip_y * width + x]);
21                 }
22         }
23 }
24
25 }  // namespace
26
27 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
28                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
29         : chain(width, height), width(width), height(height), finalized(false)
30 {
31         init_movit();
32
33         if (data != NULL) {
34                 add_input(data, pixel_format, color_space, gamma_curve);
35         }
36
37         glGenTextures(1, &texnum);
38         check_error();
39         glBindTexture(GL_TEXTURE_2D, texnum);
40         check_error();
41         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
42         check_error();
43
44         glGenFramebuffers(1, &fbo);
45         check_error();
46         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
47         check_error();
48         glFramebufferTexture2D(
49                 GL_FRAMEBUFFER,
50                 GL_COLOR_ATTACHMENT0,
51                 GL_TEXTURE_2D,
52                 texnum,
53                 0);
54         check_error();
55         glBindFramebuffer(GL_FRAMEBUFFER, 0);
56         check_error();
57 }
58
59 EffectChainTester::~EffectChainTester()
60 {
61         glDeleteFramebuffers(1, &fbo);
62         check_error();
63         glDeleteTextures(1, &texnum);
64         check_error();
65 }
66
67 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
68 {
69         ImageFormat format;
70         format.color_space = color_space;
71         format.gamma_curve = gamma_curve;
72
73         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
74         input->set_pixel_data(data);
75         chain.add_input(input);
76         return input;
77 }
78
79 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
80 {
81         ImageFormat format;
82         format.color_space = color_space;
83         format.gamma_curve = gamma_curve;
84
85         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
86         input->set_pixel_data(data);
87         chain.add_input(input);
88         return input;
89 }
90
91 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
92 {
93         if (!finalized) {
94                 finalize_chain(color_space, gamma_curve);
95         }
96
97         chain.render_to_fbo(fbo, width, height);
98
99         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
100         glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
101
102         if (format == GL_RGBA) {
103                 width *= 4;
104         }
105
106         vertical_flip(out_data, width, height);
107 }
108
109 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve)
110 {
111         assert(!finalized);
112         ImageFormat image_format;
113         image_format.color_space = color_space;
114         image_format.gamma_curve = gamma_curve;
115         chain.add_output(image_format);
116         chain.finalize();
117         finalized = true;
118 }
119
120 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
121 {
122         float largest_difference = -1.0f;
123         float squared_difference = 0.0f;
124         int largest_diff_x = -1, largest_diff_y = -1;
125
126         for (unsigned y = 0; y < height; ++y) {
127                 for (unsigned x = 0; x < width; ++x) {
128                         float diff = ref[y * width + x] - result[y * width + x];
129                         if (fabs(diff) > largest_difference) {
130                                 largest_difference = fabs(diff);
131                                 largest_diff_x = x;
132                                 largest_diff_y = y;
133                         }
134                         squared_difference += diff * diff;
135                 }
136         }
137
138         EXPECT_LT(largest_difference, largest_difference_limit)
139                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
140                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
141                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
142
143         float rms = sqrt(squared_difference) / (width * height);
144         EXPECT_LT(rms, rms_limit);
145
146         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
147                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
148
149                 fprintf(stderr, "Reference:\n");
150                 for (unsigned y = 0; y < height; ++y) {
151                         for (unsigned x = 0; x < width; ++x) {
152                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
153                         }
154                         fprintf(stderr, "\n");
155                 }
156
157                 fprintf(stderr, "\nResult:\n");
158                 for (unsigned y = 0; y < height; ++y) {
159                         for (unsigned x = 0; x < width; ++x) {
160                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
161                         }
162                         fprintf(stderr, "\n");
163                 }
164         }
165 }