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"
18 // Not thread-safe, but this isn't a big problem for testing.
19 ResourcePool *get_static_pool()
21 static ResourcePool *resource_pool = NULL;
23 resource_pool = new ResourcePool();
28 // Flip upside-down to compensate for different origin.
30 void vertical_flip(T *data, unsigned width, unsigned height)
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]);
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)
47 init_movit(".", MOVIT_DEBUG_ON);
50 add_input(data, pixel_format, color_space, gamma_curve);
53 glGenTextures(1, &texnum);
55 glBindTexture(GL_TEXTURE_2D, texnum);
57 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
60 glGenFramebuffers(1, &fbo);
62 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
64 glFramebufferTexture2D(
71 glBindFramebuffer(GL_FRAMEBUFFER, 0);
75 EffectChainTester::~EffectChainTester()
77 glDeleteFramebuffers(1, &fbo);
79 glDeleteTextures(1, &texnum);
83 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
86 format.color_space = color_space;
87 format.gamma_curve = gamma_curve;
89 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
90 input->set_pixel_data(data);
91 chain.add_input(input);
95 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
98 format.color_space = color_space;
99 format.gamma_curve = gamma_curve;
101 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
102 input->set_pixel_data(data);
103 chain.add_input(input);
107 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
110 finalize_chain(color_space, gamma_curve, alpha_format);
113 chain.render_to_fbo(fbo, width, height);
115 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
116 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
118 if (format == GL_RGBA) {
122 vertical_flip(out_data, width, height);
125 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
128 finalize_chain(color_space, gamma_curve, alpha_format);
131 chain.render_to_fbo(fbo, width, height);
133 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
134 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
136 if (format == GL_RGBA) {
140 vertical_flip(out_data, width, height);
143 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
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);
154 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
156 float largest_difference = -1.0f;
157 float squared_difference = 0.0f;
158 int largest_diff_x = -1, largest_diff_y = -1;
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);
168 squared_difference += diff * diff;
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];
177 float rms = sqrt(squared_difference) / (width * height);
178 EXPECT_LT(rms, rms_limit);
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");
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]);
188 fprintf(stderr, "\n");
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]);
196 fprintf(stderr, "\n");
201 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
206 float *ref_float = new float[width * height];
207 float *result_float = new float[width * height];
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];
216 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
219 delete[] result_float;
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)
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);
230 if (expected[i] > 0.0) {
231 double relative_error = fabs(absolute_error / expected[i]);
233 EXPECT_LT(relative_error, relative_error_limit);
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);
241 double rms = sqrt(squared_difference) / num_values;
242 EXPECT_LT(rms, rms_limit);