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