2 #include <gtest/gtest-message.h>
8 #include "flat_input.h"
9 #include "gtest/gtest.h"
11 #include "test_util.h"
18 // Flip upside-down to compensate for different origin.
20 void vertical_flip(T *data, unsigned width, unsigned height)
22 for (unsigned y = 0; y < height / 2; ++y) {
23 unsigned flip_y = height - y - 1;
24 for (unsigned x = 0; x < width; ++x) {
25 std::swap(data[y * width + x], data[flip_y * width + x]);
32 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
33 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
34 GLenum framebuffer_format)
35 : chain(width, height), width(width), height(height), finalized(false)
37 init_movit(".", MOVIT_DEBUG_ON);
40 add_input(data, pixel_format, color_space, gamma_curve);
43 glGenTextures(1, &texnum);
45 glBindTexture(GL_TEXTURE_2D, texnum);
47 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
50 glGenFramebuffers(1, &fbo);
52 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
54 glFramebufferTexture2D(
61 glBindFramebuffer(GL_FRAMEBUFFER, 0);
65 EffectChainTester::~EffectChainTester()
67 glDeleteFramebuffers(1, &fbo);
69 glDeleteTextures(1, &texnum);
73 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
76 format.color_space = color_space;
77 format.gamma_curve = gamma_curve;
79 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
80 input->set_pixel_data(data);
81 chain.add_input(input);
85 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
88 format.color_space = color_space;
89 format.gamma_curve = gamma_curve;
91 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
92 input->set_pixel_data(data);
93 chain.add_input(input);
97 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
100 finalize_chain(color_space, gamma_curve, alpha_format);
103 chain.render_to_fbo(fbo, width, height);
105 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
106 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
108 if (format == GL_RGBA) {
112 vertical_flip(out_data, width, height);
115 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
118 finalize_chain(color_space, gamma_curve, alpha_format);
121 chain.render_to_fbo(fbo, width, height);
123 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
124 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
126 if (format == GL_RGBA) {
130 vertical_flip(out_data, width, height);
133 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
136 ImageFormat image_format;
137 image_format.color_space = color_space;
138 image_format.gamma_curve = gamma_curve;
139 chain.add_output(image_format, alpha_format);
144 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
146 float largest_difference = -1.0f;
147 float squared_difference = 0.0f;
148 int largest_diff_x = -1, largest_diff_y = -1;
150 for (unsigned y = 0; y < height; ++y) {
151 for (unsigned x = 0; x < width; ++x) {
152 float diff = ref[y * width + x] - result[y * width + x];
153 if (fabs(diff) > largest_difference) {
154 largest_difference = fabs(diff);
158 squared_difference += diff * diff;
162 EXPECT_LT(largest_difference, largest_difference_limit)
163 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
164 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
165 << "Result: " << result[largest_diff_y * width + largest_diff_x];
167 float rms = sqrt(squared_difference) / (width * height);
168 EXPECT_LT(rms, rms_limit);
170 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
171 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
173 fprintf(stderr, "Reference:\n");
174 for (unsigned y = 0; y < height; ++y) {
175 for (unsigned x = 0; x < width; ++x) {
176 fprintf(stderr, "%7.4f ", ref[y * width + x]);
178 fprintf(stderr, "\n");
181 fprintf(stderr, "\nResult:\n");
182 for (unsigned y = 0; y < height; ++y) {
183 for (unsigned x = 0; x < width; ++x) {
184 fprintf(stderr, "%7.4f ", result[y * width + x]);
186 fprintf(stderr, "\n");
191 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
196 float *ref_float = new float[width * height];
197 float *result_float = new float[width * height];
199 for (unsigned y = 0; y < height; ++y) {
200 for (unsigned x = 0; x < width; ++x) {
201 ref_float[y * width + x] = ref[y * width + x];
202 result_float[y * width + x] = result[y * width + x];
206 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
209 delete[] result_float;