2 #include <gtest/gtest-message.h>
7 #include "flat_input.h"
8 #include "gtest/gtest.h"
10 #include "test_util.h"
17 // Flip upside-down to compensate for different origin.
19 void vertical_flip(T *data, unsigned width, unsigned height)
21 for (unsigned y = 0; y < height / 2; ++y) {
22 unsigned flip_y = height - y - 1;
23 for (unsigned x = 0; x < width; ++x) {
24 std::swap(data[y * width + x], data[flip_y * width + x]);
31 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
32 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve,
33 GLenum framebuffer_format)
34 : chain(width, height), width(width), height(height), finalized(false)
36 init_movit(".", MOVIT_DEBUG_ON);
39 add_input(data, pixel_format, color_space, gamma_curve);
42 glGenTextures(1, &texnum);
44 glBindTexture(GL_TEXTURE_2D, texnum);
46 glTexImage2D(GL_TEXTURE_2D, 0, framebuffer_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
49 glGenFramebuffers(1, &fbo);
51 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
53 glFramebufferTexture2D(
60 glBindFramebuffer(GL_FRAMEBUFFER, 0);
64 EffectChainTester::~EffectChainTester()
66 glDeleteFramebuffers(1, &fbo);
68 glDeleteTextures(1, &texnum);
72 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
75 format.color_space = color_space;
76 format.gamma_curve = gamma_curve;
78 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
79 input->set_pixel_data(data);
80 chain.add_input(input);
84 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
87 format.color_space = color_space;
88 format.gamma_curve = gamma_curve;
90 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
91 input->set_pixel_data(data);
92 chain.add_input(input);
96 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
99 finalize_chain(color_space, gamma_curve, alpha_format);
102 chain.render_to_fbo(fbo, width, height);
104 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
105 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
107 if (format == GL_RGBA) {
111 vertical_flip(out_data, width, height);
114 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
117 finalize_chain(color_space, gamma_curve, alpha_format);
120 chain.render_to_fbo(fbo, width, height);
122 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
123 glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
125 if (format == GL_RGBA) {
129 vertical_flip(out_data, width, height);
132 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve, OutputAlphaFormat alpha_format)
135 ImageFormat image_format;
136 image_format.color_space = color_space;
137 image_format.gamma_curve = gamma_curve;
138 chain.add_output(image_format, alpha_format);
143 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
145 float largest_difference = -1.0f;
146 float squared_difference = 0.0f;
147 int largest_diff_x = -1, largest_diff_y = -1;
149 for (unsigned y = 0; y < height; ++y) {
150 for (unsigned x = 0; x < width; ++x) {
151 float diff = ref[y * width + x] - result[y * width + x];
152 if (fabs(diff) > largest_difference) {
153 largest_difference = fabs(diff);
157 squared_difference += diff * diff;
161 EXPECT_LT(largest_difference, largest_difference_limit)
162 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
163 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
164 << "Result: " << result[largest_diff_y * width + largest_diff_x];
166 float rms = sqrt(squared_difference) / (width * height);
167 EXPECT_LT(rms, rms_limit);
169 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
170 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
172 fprintf(stderr, "Reference:\n");
173 for (unsigned y = 0; y < height; ++y) {
174 for (unsigned x = 0; x < width; ++x) {
175 fprintf(stderr, "%7.4f ", ref[y * width + x]);
177 fprintf(stderr, "\n");
180 fprintf(stderr, "\nResult:\n");
181 for (unsigned y = 0; y < height; ++y) {
182 for (unsigned x = 0; x < width; ++x) {
183 fprintf(stderr, "%7.4f ", result[y * width + x]);
185 fprintf(stderr, "\n");
190 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
195 float *ref_float = new float[width * height];
196 float *result_float = new float[width * height];
198 for (unsigned y = 0; y < height; ++y) {
199 for (unsigned x = 0; x < width; ++x) {
200 ref_float[y * width + x] = ref[y * width + x];
201 result_float[y * width + x] = result[y * width + x];
205 expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
208 delete[] result_float;