6 #include "flat_input.h"
8 #include "gtest/gtest.h"
9 #include "gtest/gtest-message.h"
11 #include "resource_pool.h"
12 #include "test_util.h"
21 // Not thread-safe, but this isn't a big problem for testing.
22 ResourcePool *get_static_pool()
24 static ResourcePool *resource_pool = NULL;
26 resource_pool = new ResourcePool();
31 // Flip upside-down to compensate for different origin.
33 void vertical_flip(T *data, unsigned width, unsigned height)
35 for (unsigned y = 0; y < height / 2; ++y) {
36 unsigned flip_y = height - y - 1;
37 for (unsigned x = 0; x < width; ++x) {
38 swap(data[y * width + x], data[flip_y * width + x]);
45 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
46 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
47 GLenum framebuffer_format)
48 : chain(width, height, get_static_pool()), width(width), height(height), finalized(false)
50 init_movit(".", MOVIT_DEBUG_OFF);
53 add_input(data, pixel_format, color_space, gamma_curve);
56 glGenTextures(1, &texnum);
58 glBindTexture(GL_TEXTURE_2D, texnum);
60 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
63 glGenFramebuffers(1, &fbo);
65 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
67 glFramebufferTexture2D(
74 glBindFramebuffer(GL_FRAMEBUFFER, 0);
78 EffectChainTester::~EffectChainTester()
80 glDeleteFramebuffers(1, &fbo);
82 glDeleteTextures(1, &texnum);
86 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
89 format.color_space = color_space;
90 format.gamma_curve = gamma_curve;
92 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
93 input->set_pixel_data(data);
94 chain.add_input(input);
98 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
101 format.color_space = color_space;
102 format.gamma_curve = gamma_curve;
104 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
105 input->set_pixel_data(data);
106 chain.add_input(input);
110 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
113 finalize_chain(color_space, gamma_curve, alpha_format);
116 chain.render_to_fbo(fbo, width, height);
118 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
119 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
121 if (format == GL_RGBA) {
125 vertical_flip(out_data, width, height);
128 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
131 finalize_chain(color_space, gamma_curve, alpha_format);
134 chain.render_to_fbo(fbo, width, height);
136 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
137 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
139 if (format == GL_RGBA) {
143 vertical_flip(out_data, width, height);
146 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
149 ImageFormat image_format;
150 image_format.color_space = color_space;
151 image_format.gamma_curve = gamma_curve;
152 chain.add_output(image_format, alpha_format);
157 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
159 float largest_difference = -1.0f;
160 float squared_difference = 0.0f;
161 int largest_diff_x = -1, largest_diff_y = -1;
163 for (unsigned y = 0; y < height; ++y) {
164 for (unsigned x = 0; x < width; ++x) {
165 float diff = ref[y * width + x] - result[y * width + x];
166 if (fabs(diff) > largest_difference) {
167 largest_difference = fabs(diff);
171 squared_difference += diff * diff;
175 EXPECT_LT(largest_difference, largest_difference_limit)
176 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
177 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
178 << "Result: " << result[largest_diff_y * width + largest_diff_x];
180 float rms = sqrt(squared_difference) / (width * height);
181 EXPECT_LT(rms, rms_limit);
183 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
184 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
186 fprintf(stderr, "Reference:\n");
187 for (unsigned y = 0; y < height; ++y) {
188 for (unsigned x = 0; x < width; ++x) {
189 fprintf(stderr, "%7.4f ", ref[y * width + x]);
191 fprintf(stderr, "\n");
194 fprintf(stderr, "\nResult:\n");
195 for (unsigned y = 0; y < height; ++y) {
196 for (unsigned x = 0; x < width; ++x) {
197 fprintf(stderr, "%7.4f ", result[y * width + x]);
199 fprintf(stderr, "\n");
204 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
209 float *ref_float = new float[width * height];
210 float *result_float = new float[width * height];
212 for (unsigned y = 0; y < height; ++y) {
213 for (unsigned x = 0; x < width; ++x) {
214 ref_float[y * width + x] = ref[y * width + x];
215 result_float[y * width + x] = result[y * width + x];
219 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
222 delete[] result_float;
225 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)
227 double squared_difference = 0.0;
228 for (unsigned i = 0; i < num_values; ++i) {
229 double absolute_error = fabs(expected[i] - result[i]);
230 squared_difference += absolute_error * absolute_error;
231 EXPECT_LT(absolute_error, absolute_error_limit);
233 if (expected[i] > 0.0) {
234 double relative_error = fabs(absolute_error / expected[i]);
236 EXPECT_LT(relative_error, relative_error_limit);
238 if (i < num_values - 1) {
239 double delta = expected[i + 1] - expected[i];
240 double local_relative_error = fabs(absolute_error / delta);
241 EXPECT_LT(local_relative_error, local_relative_error_limit);
244 double rms = sqrt(squared_difference) / num_values;
245 EXPECT_LT(rms, rms_limit);