2 #include "flat_input.h"
3 #include "gtest/gtest.h"
10 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height, MovitPixelFormat pixel_format, ColorSpace color_space, GammaCurve gamma_curve)
11 : chain(width, height), width(width), height(height)
13 add_input(data, pixel_format, color_space, gamma_curve);
15 glGenTextures(1, &texnum);
17 glBindTexture(GL_TEXTURE_2D, texnum);
19 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
22 glGenFramebuffers(1, &fbo);
24 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
26 glFramebufferTexture2D(
33 glBindFramebuffer(GL_FRAMEBUFFER, 0);
37 EffectChainTester::~EffectChainTester()
39 glDeleteFramebuffers(1, &fbo);
41 glDeleteTextures(1, &texnum);
45 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, ColorSpace color_space, GammaCurve gamma_curve)
48 format.color_space = color_space;
49 format.gamma_curve = gamma_curve;
51 FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
52 input->set_pixel_data(data);
53 chain.add_input(input);
57 void EffectChainTester::run(float *out_data, GLenum format, ColorSpace color_space, GammaCurve gamma_curve)
59 ImageFormat image_format;
60 image_format.color_space = color_space;
61 image_format.gamma_curve = gamma_curve;
62 chain.add_output(image_format);
65 chain.render_to_fbo(fbo, width, height);
67 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
68 glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
70 if (format == GL_RGBA) {
74 // Flip upside-down to compensate for different origin.
75 for (unsigned y = 0; y < height / 2; ++y) {
76 unsigned flip_y = height - y - 1;
77 for (unsigned x = 0; x < width; ++x) {
78 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
83 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
85 float largest_difference = -1.0f;
86 float squared_difference = 0.0f;
87 int largest_diff_x = -1, largest_diff_y = -1;
89 for (unsigned y = 0; y < height; ++y) {
90 for (unsigned x = 0; x < width; ++x) {
91 float diff = ref[y * width + x] - result[y * width + x];
92 if (fabs(diff) > largest_difference) {
93 largest_difference = fabs(diff);
97 squared_difference += diff * diff;
101 EXPECT_LT(largest_difference, largest_difference_limit)
102 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
103 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
104 << "Result: " << result[largest_diff_y * width + largest_diff_x];
106 float rms = sqrt(squared_difference) / (width * height);
107 EXPECT_LT(rms, rms_limit);
109 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
110 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
112 fprintf(stderr, "Reference:\n");
113 for (unsigned y = 0; y < height; ++y) {
114 for (unsigned x = 0; x < width; ++x) {
115 fprintf(stderr, "%7.4f ", ref[y * width + x]);
117 fprintf(stderr, "\n");
120 fprintf(stderr, "\nResult:\n");
121 for (unsigned y = 0; y < height; ++y) {
122 for (unsigned x = 0; x < width; ++x) {
123 fprintf(stderr, "%7.4f ", result[y * width + x]);
125 fprintf(stderr, "\n");