3 #include "flat_input.h"
4 #include "gtest/gtest.h"
11 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
12 MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
13 : chain(width, height), width(width), height(height), finalized(false)
18 add_input(data, pixel_format, color_space, gamma_curve);
21 glGenTextures(1, &texnum);
23 glBindTexture(GL_TEXTURE_2D, texnum);
25 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
28 glGenFramebuffers(1, &fbo);
30 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
32 glFramebufferTexture2D(
39 glBindFramebuffer(GL_FRAMEBUFFER, 0);
43 EffectChainTester::~EffectChainTester()
45 glDeleteFramebuffers(1, &fbo);
47 glDeleteTextures(1, &texnum);
51 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
54 format.color_space = color_space;
55 format.gamma_curve = gamma_curve;
57 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
58 input->set_pixel_data(data);
59 chain.add_input(input);
63 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
66 format.color_space = color_space;
67 format.gamma_curve = gamma_curve;
69 FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
70 input->set_pixel_data(data);
71 chain.add_input(input);
75 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
78 ImageFormat image_format;
79 image_format.color_space = color_space;
80 image_format.gamma_curve = gamma_curve;
81 chain.add_output(image_format);
86 chain.render_to_fbo(fbo, width, height);
88 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
89 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
91 if (format == GL_RGBA) {
95 // Flip upside-down to compensate for different origin.
96 for (unsigned y = 0; y < height / 2; ++y) {
97 unsigned flip_y = height - y - 1;
98 for (unsigned x = 0; x < width; ++x) {
99 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
104 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
106 float largest_difference = -1.0f;
107 float squared_difference = 0.0f;
108 int largest_diff_x = -1, largest_diff_y = -1;
110 for (unsigned y = 0; y < height; ++y) {
111 for (unsigned x = 0; x < width; ++x) {
112 float diff = ref[y * width + x] - result[y * width + x];
113 if (fabs(diff) > largest_difference) {
114 largest_difference = fabs(diff);
118 squared_difference += diff * diff;
122 EXPECT_LT(largest_difference, largest_difference_limit)
123 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
124 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
125 << "Result: " << result[largest_diff_y * width + largest_diff_x];
127 float rms = sqrt(squared_difference) / (width * height);
128 EXPECT_LT(rms, rms_limit);
130 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
131 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
133 fprintf(stderr, "Reference:\n");
134 for (unsigned y = 0; y < height; ++y) {
135 for (unsigned x = 0; x < width; ++x) {
136 fprintf(stderr, "%7.4f ", ref[y * width + x]);
138 fprintf(stderr, "\n");
141 fprintf(stderr, "\nResult:\n");
142 for (unsigned y = 0; y < height; ++y) {
143 for (unsigned x = 0; x < width; ++x) {
144 fprintf(stderr, "%7.4f ", result[y * width + x]);
146 fprintf(stderr, "\n");