2 #include <gtest/gtest-message.h>
7 #include "flat_input.h"
8 #include "gtest/gtest.h"
10 #include "resource_pool.h"
11 #include "test_util.h"
20 // Not thread-safe, but this isn't a big problem for testing.
21 ResourcePool *get_static_pool()
23 static ResourcePool *resource_pool = NULL;
25 resource_pool = new ResourcePool();
30 // Flip upside-down to compensate for different origin.
32 void vertical_flip(T *data, unsigned width, unsigned height)
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]);
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)
49 init_movit(".", MOVIT_DEBUG_OFF);
52 add_input(data, pixel_format, color_space, gamma_curve);
55 glGenTextures(1, &texnum);
57 glBindTexture(GL_TEXTURE_2D, texnum);
59 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
62 glGenFramebuffers(1, &fbo);
64 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
66 glFramebufferTexture2D(
73 glBindFramebuffer(GL_FRAMEBUFFER, 0);
77 EffectChainTester::~EffectChainTester()
79 glDeleteFramebuffers(1, &fbo);
81 glDeleteTextures(1, &texnum);
85 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
88 format.color_space = color_space;
89 format.gamma_curve = gamma_curve;
91 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
92 input->set_pixel_data(data);
93 chain.add_input(input);
97 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
100 format.color_space = color_space;
101 format.gamma_curve = gamma_curve;
103 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
104 input->set_pixel_data(data);
105 chain.add_input(input);
109 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
112 finalize_chain(color_space, gamma_curve, alpha_format);
115 chain.render_to_fbo(fbo, width, height);
117 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
118 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
120 if (format == GL_RGBA) {
124 vertical_flip(out_data, width, height);
127 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
130 finalize_chain(color_space, gamma_curve, alpha_format);
133 chain.render_to_fbo(fbo, width, height);
135 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
136 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
138 if (format == GL_RGBA) {
142 vertical_flip(out_data, width, height);
145 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
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);
156 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
158 float largest_difference = -1.0f;
159 float squared_difference = 0.0f;
160 int largest_diff_x = -1, largest_diff_y = -1;
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);
170 squared_difference += diff * diff;
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];
179 float rms = sqrt(squared_difference) / (width * height);
180 EXPECT_LT(rms, rms_limit);
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");
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]);
190 fprintf(stderr, "\n");
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]);
198 fprintf(stderr, "\n");
203 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
208 float *ref_float = new float[width * height];
209 float *result_float = new float[width * height];
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];
218 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
221 delete[] result_float;
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)
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);
232 if (expected[i] > 0.0) {
233 double relative_error = fabs(absolute_error / expected[i]);
235 EXPECT_LT(relative_error, relative_error_limit);
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);
243 double rms = sqrt(squared_difference) / num_values;
244 EXPECT_LT(rms, rms_limit);