6 #include <gtest/gtest.h>
7 #include <gtest/gtest-message.h>
9 #include "flat_input.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
23 // Not thread-safe, but this isn't a big problem for testing.
24 ResourcePool *get_static_pool()
26 static ResourcePool *resource_pool = NULL;
28 resource_pool = new ResourcePool();
33 // Flip upside-down to compensate for different origin.
35 void vertical_flip(T *data, unsigned width, unsigned height)
37 for (unsigned y = 0; y < height / 2; ++y) {
38 unsigned flip_y = height - y - 1;
39 for (unsigned x = 0; x < width; ++x) {
40 swap(data[y * width + x], data[flip_y * width + x]);
47 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
48 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
49 GLenum framebuffer_format)
50 : chain(width, height, get_static_pool()), width(width), height(height), finalized(false)
52 CHECK(init_movit(".", MOVIT_DEBUG_OFF));
55 add_input(data, pixel_format, color_space, gamma_curve);
59 if (framebuffer_format == GL_RGBA8) {
60 type = GL_UNSIGNED_BYTE;
61 } else if (framebuffer_format == GL_RGBA16F || framebuffer_format == GL_RGBA32F) {
64 // Add more here as needed.
68 glGenTextures(1, &texnum);
70 glBindTexture(GL_TEXTURE_2D, texnum);
72 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, type, NULL);
75 glGenFramebuffers(1, &fbo);
77 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
79 glFramebufferTexture2D(
86 glBindFramebuffer(GL_FRAMEBUFFER, 0);
90 EffectChainTester::~EffectChainTester()
92 glDeleteFramebuffers(1, &fbo);
94 glDeleteTextures(1, &texnum);
98 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
101 format.color_space = color_space;
102 format.gamma_curve = gamma_curve;
104 if (input_width == -1) {
107 if (input_height == -1) {
108 input_height = height;
111 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
112 input->set_pixel_data(data);
113 chain.add_input(input);
117 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
120 format.color_space = color_space;
121 format.gamma_curve = gamma_curve;
123 if (input_width == -1) {
126 if (input_height == -1) {
127 input_height = height;
130 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
131 input->set_pixel_data(data);
132 chain.add_input(input);
136 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
139 finalize_chain(color_space, gamma_curve, alpha_format);
142 chain.render_to_fbo(fbo, width, height);
144 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
146 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
147 // GLES will only read GL_RGBA.
148 float *temp = new float[width * height * 4];
149 glReadPixels(0, 0, width, height, GL_RGBA, GL_FLOAT, temp);
151 if (format == GL_ALPHA) {
152 for (unsigned i = 0; i < width * height; ++i) {
153 out_data[i] = temp[i * 4 + 3];
155 } else if (format == GL_BLUE) {
156 for (unsigned i = 0; i < width * height; ++i) {
157 out_data[i] = temp[i * 4 + 2];
160 for (unsigned i = 0; i < width * height; ++i) {
161 out_data[i] = temp[i * 4];
166 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
170 if (format == GL_RGBA) {
174 vertical_flip(out_data, width, height);
177 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
180 finalize_chain(color_space, gamma_curve, alpha_format);
183 chain.render_to_fbo(fbo, width, height);
185 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
187 if (!epoxy_is_desktop_gl() && (format == GL_RED || format == GL_BLUE || format == GL_ALPHA)) {
188 // GLES will only read GL_RGBA.
189 unsigned char *temp = new unsigned char[width * height * 4];
190 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, temp);
192 if (format == GL_ALPHA) {
193 for (unsigned i = 0; i < width * height; ++i) {
194 out_data[i] = temp[i * 4 + 3];
196 } else if (format == GL_BLUE) {
197 for (unsigned i = 0; i < width * height; ++i) {
198 out_data[i] = temp[i * 4 + 2];
201 for (unsigned i = 0; i < width * height; ++i) {
202 out_data[i] = temp[i * 4];
207 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
211 if (format == GL_RGBA) {
215 vertical_flip(out_data, width, height);
218 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
221 ImageFormat image_format;
222 image_format.color_space = color_space;
223 image_format.gamma_curve = gamma_curve;
224 chain.add_output(image_format, alpha_format);
229 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
231 float largest_difference = -1.0f;
232 float squared_difference = 0.0f;
233 int largest_diff_x = -1, largest_diff_y = -1;
235 for (unsigned y = 0; y < height; ++y) {
236 for (unsigned x = 0; x < width; ++x) {
237 float diff = ref[y * width + x] - result[y * width + x];
238 if (fabs(diff) > largest_difference) {
239 largest_difference = fabs(diff);
243 squared_difference += diff * diff;
247 EXPECT_LT(largest_difference, largest_difference_limit)
248 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
249 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
250 << "Result: " << result[largest_diff_y * width + largest_diff_x];
252 float rms = sqrt(squared_difference) / (width * height);
253 EXPECT_LT(rms, rms_limit);
255 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
256 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
258 fprintf(stderr, "Reference:\n");
259 for (unsigned y = 0; y < height; ++y) {
260 for (unsigned x = 0; x < width; ++x) {
261 fprintf(stderr, "%7.4f ", ref[y * width + x]);
263 fprintf(stderr, "\n");
266 fprintf(stderr, "\nResult:\n");
267 for (unsigned y = 0; y < height; ++y) {
268 for (unsigned x = 0; x < width; ++x) {
269 fprintf(stderr, "%7.4f ", result[y * width + x]);
271 fprintf(stderr, "\n");
276 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
281 float *ref_float = new float[width * height];
282 float *result_float = new float[width * height];
284 for (unsigned y = 0; y < height; ++y) {
285 for (unsigned x = 0; x < width; ++x) {
286 ref_float[y * width + x] = ref[y * width + x];
287 result_float[y * width + x] = result[y * width + x];
291 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
294 delete[] result_float;
297 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)
299 double squared_difference = 0.0;
300 for (unsigned i = 0; i < num_values; ++i) {
301 double absolute_error = fabs(expected[i] - result[i]);
302 squared_difference += absolute_error * absolute_error;
303 EXPECT_LT(absolute_error, absolute_error_limit);
305 if (expected[i] > 0.0) {
306 double relative_error = fabs(absolute_error / expected[i]);
308 EXPECT_LT(relative_error, relative_error_limit);
310 if (i < num_values - 1) {
311 double delta = expected[i + 1] - expected[i];
312 double local_relative_error = fabs(absolute_error / delta);
313 EXPECT_LT(local_relative_error, local_relative_error_limit);
316 double rms = sqrt(squared_difference) / num_values;
317 EXPECT_LT(rms, rms_limit);