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