Remove the finalize() member function from Input.
[movit] / test_util.cpp
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <algorithm>
5
6 #include "flat_input.h"
7 #include "glew.h"
8 #include "gtest/gtest.h"
9 #include "gtest/gtest-message.h"
10 #include "init.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 using namespace std;
16
17 class Input;
18
19 namespace {
20
21 // Not thread-safe, but this isn't a big problem for testing.
22 ResourcePool *get_static_pool()
23 {
24         static ResourcePool *resource_pool = NULL;
25         if (!resource_pool) {
26                 resource_pool = new ResourcePool();
27         }
28         return resource_pool;
29 }
30
31 // Flip upside-down to compensate for different origin.
32 template<class T>
33 void vertical_flip(T *data, unsigned width, unsigned height)
34 {
35         for (unsigned y = 0; y < height / 2; ++y) {
36                 unsigned flip_y = height - y - 1;
37                 for (unsigned x = 0; x < width; ++x) {
38                         swap(data[y * width + x], data[flip_y * width + x]);
39                 }
40         }
41 }
42
43 }  // namespace
44
45 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
46                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
47                                      GLenum framebuffer_format)
48         : chain(width, height, get_static_pool()), width(width), height(height), finalized(false)
49 {
50         init_movit(".", MOVIT_DEBUG_OFF);
51
52         if (data != NULL) {
53                 add_input(data, pixel_format, color_space, gamma_curve);
54         }
55
56         glGenTextures(1, &texnum);
57         check_error();
58         glBindTexture(GL_TEXTURE_2D, texnum);
59         check_error();
60         glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
61         check_error();
62
63         glGenFramebuffers(1, &fbo);
64         check_error();
65         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
66         check_error();
67         glFramebufferTexture2D(
68                 GL_FRAMEBUFFER,
69                 GL_COLOR_ATTACHMENT0,
70                 GL_TEXTURE_2D,
71                 texnum,
72                 0);
73         check_error();
74         glBindFramebuffer(GL_FRAMEBUFFER, 0);
75         check_error();
76 }
77
78 EffectChainTester::~EffectChainTester()
79 {
80         glDeleteFramebuffers(1, &fbo);
81         check_error();
82         glDeleteTextures(1, &texnum);
83         check_error();
84 }
85
86 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
87 {
88         ImageFormat format;
89         format.color_space = color_space;
90         format.gamma_curve = gamma_curve;
91
92         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
93         input->set_pixel_data(data);
94         chain.add_input(input);
95         return input;
96 }
97
98 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
99 {
100         ImageFormat format;
101         format.color_space = color_space;
102         format.gamma_curve = gamma_curve;
103
104         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
105         input->set_pixel_data(data);
106         chain.add_input(input);
107         return input;
108 }
109
110 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
111 {
112         if (!finalized) {
113                 finalize_chain(color_space, gamma_curve, alpha_format);
114         }
115
116         chain.render_to_fbo(fbo, width, height);
117
118         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
119         glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
120
121         if (format == GL_RGBA) {
122                 width *= 4;
123         }
124
125         vertical_flip(out_data, width, height);
126 }
127
128 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
129 {
130         if (!finalized) {
131                 finalize_chain(color_space, gamma_curve, alpha_format);
132         }
133
134         chain.render_to_fbo(fbo, width, height);
135
136         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
137         glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
138
139         if (format == GL_RGBA) {
140                 width *= 4;
141         }
142
143         vertical_flip(out_data, width, height);
144 }
145
146 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
147 {
148         assert(!finalized);
149         ImageFormat image_format;
150         image_format.color_space = color_space;
151         image_format.gamma_curve = gamma_curve;
152         chain.add_output(image_format, alpha_format);
153         chain.finalize();
154         finalized = true;
155 }
156
157 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
158 {
159         float largest_difference = -1.0f;
160         float squared_difference = 0.0f;
161         int largest_diff_x = -1, largest_diff_y = -1;
162
163         for (unsigned y = 0; y < height; ++y) {
164                 for (unsigned x = 0; x < width; ++x) {
165                         float diff = ref[y * width + x] - result[y * width + x];
166                         if (fabs(diff) > largest_difference) {
167                                 largest_difference = fabs(diff);
168                                 largest_diff_x = x;
169                                 largest_diff_y = y;
170                         }
171                         squared_difference += diff * diff;
172                 }
173         }
174
175         EXPECT_LT(largest_difference, largest_difference_limit)
176                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
177                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
178                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
179
180         float rms = sqrt(squared_difference) / (width * height);
181         EXPECT_LT(rms, rms_limit);
182
183         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
184                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
185
186                 fprintf(stderr, "Reference:\n");
187                 for (unsigned y = 0; y < height; ++y) {
188                         for (unsigned x = 0; x < width; ++x) {
189                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
190                         }
191                         fprintf(stderr, "\n");
192                 }
193
194                 fprintf(stderr, "\nResult:\n");
195                 for (unsigned y = 0; y < height; ++y) {
196                         for (unsigned x = 0; x < width; ++x) {
197                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
198                         }
199                         fprintf(stderr, "\n");
200                 }
201         }
202 }
203
204 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
205 {
206         assert(width > 0);
207         assert(height > 0);
208
209         float *ref_float = new float[width * height];
210         float *result_float = new float[width * height];
211
212         for (unsigned y = 0; y < height; ++y) {
213                 for (unsigned x = 0; x < width; ++x) {
214                         ref_float[y * width + x] = ref[y * width + x];
215                         result_float[y * width + x] = result[y * width + x];
216                 }
217         }
218
219         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
220
221         delete[] ref_float;
222         delete[] result_float;
223 }
224
225 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)
226 {
227         double squared_difference = 0.0;
228         for (unsigned i = 0; i < num_values; ++i) {
229                 double absolute_error = fabs(expected[i] - result[i]);
230                 squared_difference += absolute_error * absolute_error;
231                 EXPECT_LT(absolute_error, absolute_error_limit);
232
233                 if (expected[i] > 0.0) {
234                         double relative_error = fabs(absolute_error / expected[i]);
235
236                         EXPECT_LT(relative_error, relative_error_limit);
237                 }
238                 if (i < num_values - 1) {
239                         double delta = expected[i + 1] - expected[i];
240                         double local_relative_error = fabs(absolute_error / delta);
241                         EXPECT_LT(local_relative_error, local_relative_error_limit);
242                 }
243         }
244         double rms = sqrt(squared_difference) / num_values;
245         EXPECT_LT(rms, rms_limit);
246 }