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 GLenum framebuffer_format)
30 : chain(width, height), width(width), height(height), finalized(false)
32 init_movit(".", MOVIT_DEBUG_ON);
35 add_input(data, pixel_format, color_space, gamma_curve);
38 glGenTextures(1, &texnum);
40 glBindTexture(GL_TEXTURE_2D, texnum);
42 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
45 glGenFramebuffers(1, &fbo);
47 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
49 glFramebufferTexture2D(
56 glBindFramebuffer(GL_FRAMEBUFFER, 0);
60 EffectChainTester::~EffectChainTester()
62 glDeleteFramebuffers(1, &fbo);
64 glDeleteTextures(1, &texnum);
68 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
71 format.color_space = color_space;
72 format.gamma_curve = gamma_curve;
74 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
75 input->set_pixel_data(data);
76 chain.add_input(input);
80 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
83 format.color_space = color_space;
84 format.gamma_curve = gamma_curve;
86 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
87 input->set_pixel_data(data);
88 chain.add_input(input);
92 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
95 finalize_chain(color_space, gamma_curve, alpha_format);
98 chain.render_to_fbo(fbo, width, height);
100 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
101 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
103 if (format == GL_RGBA) {
107 vertical_flip(out_data, width, height);
110 void EffectChainTester::run(unsigned char *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_UNSIGNED_BYTE, out_data);
121 if (format == GL_RGBA) {
125 vertical_flip(out_data, width, height);
128 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
131 ImageFormat image_format;
132 image_format.color_space = color_space;
133 image_format.gamma_curve = gamma_curve;
134 chain.add_output(image_format, alpha_format);
139 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
141 float largest_difference = -1.0f;
142 float squared_difference = 0.0f;
143 int largest_diff_x = -1, largest_diff_y = -1;
145 for (unsigned y = 0; y < height; ++y) {
146 for (unsigned x = 0; x < width; ++x) {
147 float diff = ref[y * width + x] - result[y * width + x];
148 if (fabs(diff) > largest_difference) {
149 largest_difference = fabs(diff);
153 squared_difference += diff * diff;
157 EXPECT_LT(largest_difference, largest_difference_limit)
158 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
159 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
160 << "Result: " << result[largest_diff_y * width + largest_diff_x];
162 float rms = sqrt(squared_difference) / (width * height);
163 EXPECT_LT(rms, rms_limit);
165 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
166 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
168 fprintf(stderr, "Reference:\n");
169 for (unsigned y = 0; y < height; ++y) {
170 for (unsigned x = 0; x < width; ++x) {
171 fprintf(stderr, "%7.4f ", ref[y * width + x]);
173 fprintf(stderr, "\n");
176 fprintf(stderr, "\nResult:\n");
177 for (unsigned y = 0; y < height; ++y) {
178 for (unsigned x = 0; x < width; ++x) {
179 fprintf(stderr, "%7.4f ", result[y * width + x]);
181 fprintf(stderr, "\n");
186 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
191 float *ref_float = new float[width * height];
192 float *result_float = new float[width * height];
194 for (unsigned y = 0; y < height; ++y) {
195 for (unsigned x = 0; x < width; ++x) {
196 ref_float[y * width + x] = ref[y * width + x];
197 result_float[y * width + x] = result[y * width + x];
201 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
204 delete[] result_float;