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"
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);
58 glGenTextures(1, &texnum);
60 glBindTexture(GL_TEXTURE_2D, texnum);
62 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
65 glGenFramebuffers(1, &fbo);
67 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
69 glFramebufferTexture2D(
76 glBindFramebuffer(GL_FRAMEBUFFER, 0);
80 EffectChainTester::~EffectChainTester()
82 glDeleteFramebuffers(1, &fbo);
84 glDeleteTextures(1, &texnum);
88 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
91 format.color_space = color_space;
92 format.gamma_curve = gamma_curve;
94 if (input_width == -1) {
97 if (input_height == -1) {
98 input_height = height;
101 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, input_width, input_height);
102 input->set_pixel_data(data);
103 chain.add_input(input);
107 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve, int input_width, int input_height)
110 format.color_space = color_space;
111 format.gamma_curve = gamma_curve;
113 if (input_width == -1) {
116 if (input_height == -1) {
117 input_height = height;
120 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, input_width, input_height);
121 input->set_pixel_data(data);
122 chain.add_input(input);
126 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
129 finalize_chain(color_space, gamma_curve, alpha_format);
132 chain.render_to_fbo(fbo, width, height);
134 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
135 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
137 if (format == GL_RGBA) {
141 vertical_flip(out_data, width, height);
144 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
147 finalize_chain(color_space, gamma_curve, alpha_format);
150 chain.render_to_fbo(fbo, width, height);
152 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
153 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
155 if (format == GL_RGBA) {
159 vertical_flip(out_data, width, height);
162 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
165 ImageFormat image_format;
166 image_format.color_space = color_space;
167 image_format.gamma_curve = gamma_curve;
168 chain.add_output(image_format, alpha_format);
173 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
175 float largest_difference = -1.0f;
176 float squared_difference = 0.0f;
177 int largest_diff_x = -1, largest_diff_y = -1;
179 for (unsigned y = 0; y < height; ++y) {
180 for (unsigned x = 0; x < width; ++x) {
181 float diff = ref[y * width + x] - result[y * width + x];
182 if (fabs(diff) > largest_difference) {
183 largest_difference = fabs(diff);
187 squared_difference += diff * diff;
191 EXPECT_LT(largest_difference, largest_difference_limit)
192 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
193 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
194 << "Result: " << result[largest_diff_y * width + largest_diff_x];
196 float rms = sqrt(squared_difference) / (width * height);
197 EXPECT_LT(rms, rms_limit);
199 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
200 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
202 fprintf(stderr, "Reference:\n");
203 for (unsigned y = 0; y < height; ++y) {
204 for (unsigned x = 0; x < width; ++x) {
205 fprintf(stderr, "%7.4f ", ref[y * width + x]);
207 fprintf(stderr, "\n");
210 fprintf(stderr, "\nResult:\n");
211 for (unsigned y = 0; y < height; ++y) {
212 for (unsigned x = 0; x < width; ++x) {
213 fprintf(stderr, "%7.4f ", result[y * width + x]);
215 fprintf(stderr, "\n");
220 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
225 float *ref_float = new float[width * height];
226 float *result_float = new float[width * height];
228 for (unsigned y = 0; y < height; ++y) {
229 for (unsigned x = 0; x < width; ++x) {
230 ref_float[y * width + x] = ref[y * width + x];
231 result_float[y * width + x] = result[y * width + x];
235 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
238 delete[] result_float;
241 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)
243 double squared_difference = 0.0;
244 for (unsigned i = 0; i < num_values; ++i) {
245 double absolute_error = fabs(expected[i] - result[i]);
246 squared_difference += absolute_error * absolute_error;
247 EXPECT_LT(absolute_error, absolute_error_limit);
249 if (expected[i] > 0.0) {
250 double relative_error = fabs(absolute_error / expected[i]);
252 EXPECT_LT(relative_error, relative_error_limit);
254 if (i < num_values - 1) {
255 double delta = expected[i + 1] - expected[i];
256 double local_relative_error = fabs(absolute_error / delta);
257 EXPECT_LT(local_relative_error, local_relative_error_limit);
260 double rms = sqrt(squared_difference) / num_values;
261 EXPECT_LT(rms, rms_limit);