3 #include "flat_input.h"
4 #include "gtest/gtest.h"
13 // Flip upside-down to compensate for different origin.
15 void vertical_flip(T *data, unsigned width, unsigned height)
17 for (unsigned y = 0; y < height / 2; ++y) {
18 unsigned flip_y = height - y - 1;
19 for (unsigned x = 0; x < width; ++x) {
20 std::swap(data[y * width + x], data[flip_y * width + x]);
27 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
28 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
29 : chain(width, height), width(width), height(height), finalized(false)
34 add_input(data, pixel_format, color_space, gamma_curve);
37 glGenTextures(1, &texnum);
39 glBindTexture(GL_TEXTURE_2D, texnum);
41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
44 glGenFramebuffers(1, &fbo);
46 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
48 glFramebufferTexture2D(
55 glBindFramebuffer(GL_FRAMEBUFFER, 0);
59 EffectChainTester::~EffectChainTester()
61 glDeleteFramebuffers(1, &fbo);
63 glDeleteTextures(1, &texnum);
67 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
70 format.color_space = color_space;
71 format.gamma_curve = gamma_curve;
73 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
74 input->set_pixel_data(data);
75 chain.add_input(input);
79 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
82 format.color_space = color_space;
83 format.gamma_curve = gamma_curve;
85 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
86 input->set_pixel_data(data);
87 chain.add_input(input);
91 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
94 finalize_chain(color_space, gamma_curve);
97 chain.render_to_fbo(fbo, width, height);
99 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
100 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
102 if (format == GL_RGBA) {
106 vertical_flip(out_data, width, height);
109 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
112 finalize_chain(color_space, gamma_curve);
115 chain.render_to_fbo(fbo, width, height);
117 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
118 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
120 if (format == GL_RGBA) {
124 vertical_flip(out_data, width, height);
127 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve)
130 ImageFormat image_format;
131 image_format.color_space = color_space;
132 image_format.gamma_curve = gamma_curve;
133 chain.add_output(image_format);
138 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
140 float largest_difference = -1.0f;
141 float squared_difference = 0.0f;
142 int largest_diff_x = -1, largest_diff_y = -1;
144 for (unsigned y = 0; y < height; ++y) {
145 for (unsigned x = 0; x < width; ++x) {
146 float diff = ref[y * width + x] - result[y * width + x];
147 if (fabs(diff) > largest_difference) {
148 largest_difference = fabs(diff);
152 squared_difference += diff * diff;
156 EXPECT_LT(largest_difference, largest_difference_limit)
157 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
158 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
159 << "Result: " << result[largest_diff_y * width + largest_diff_x];
161 float rms = sqrt(squared_difference) / (width * height);
162 EXPECT_LT(rms, rms_limit);
164 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
165 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
167 fprintf(stderr, "Reference:\n");
168 for (unsigned y = 0; y < height; ++y) {
169 for (unsigned x = 0; x < width; ++x) {
170 fprintf(stderr, "%7.4f ", ref[y * width + x]);
172 fprintf(stderr, "\n");
175 fprintf(stderr, "\nResult:\n");
176 for (unsigned y = 0; y < height; ++y) {
177 for (unsigned x = 0; x < width; ++x) {
178 fprintf(stderr, "%7.4f ", result[y * width + x]);
180 fprintf(stderr, "\n");
185 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
187 float *ref_float = new float[width * height];
188 float *result_float = new float[width * height];
190 for (unsigned y = 0; y < height; ++y) {
191 for (unsigned x = 0; x < width; ++x) {
192 ref_float[y * width + x] = ref[y * width + x];
193 result_float[y * width + x] = result[y * width + x];
197 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);