]> git.sesse.net Git - movit/blob - test_util.cpp
Fix an issue where we could try to incorrectly gamma correct the colorspace conversio...
[movit] / test_util.cpp
1 #include "test_util.h"
2 #include "flat_input.h"
3 #include "gtest/gtest.h"
4
5 #include <stdio.h>
6 #include <math.h>
7
8 #include <algorithm>
9
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)
12 {
13         add_input(data, pixel_format, color_space, gamma_curve);
14
15         glGenTextures(1, &texnum);
16         check_error();
17         glBindTexture(GL_TEXTURE_2D, texnum);
18         check_error();
19         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
20         check_error();
21
22         glGenFramebuffers(1, &fbo);
23         check_error();
24         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
25         check_error();
26         glFramebufferTexture2D(
27                 GL_FRAMEBUFFER,
28                 GL_COLOR_ATTACHMENT0,
29                 GL_TEXTURE_2D,
30                 texnum,
31                 0);
32         check_error();
33         glBindFramebuffer(GL_FRAMEBUFFER, 0);
34         check_error();
35 }
36
37 EffectChainTester::~EffectChainTester()
38 {
39         glDeleteFramebuffers(1, &fbo);
40         check_error();
41         glDeleteTextures(1, &texnum);
42         check_error();
43 }
44
45 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, ColorSpace color_space, GammaCurve gamma_curve)
46 {
47         ImageFormat format;
48         format.color_space = color_space;
49         format.gamma_curve = gamma_curve;
50
51         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
52         input->set_pixel_data(data);
53         chain.add_input(input);
54         return input;
55 }
56
57 void EffectChainTester::run(float *out_data, GLenum format, ColorSpace color_space, GammaCurve gamma_curve)
58 {
59         ImageFormat image_format;
60         image_format.color_space = color_space;
61         image_format.gamma_curve = gamma_curve;
62         chain.add_output(image_format);
63         chain.finalize();
64
65         chain.render_to_fbo(fbo, width, height);
66
67         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
68         glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
69
70         // Flip upside-down to compensate for different origin.
71         for (unsigned y = 0; y < height / 2; ++y) {
72                 unsigned flip_y = height - y - 1;
73                 for (unsigned x = 0; x < width; ++x) {
74                         std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
75                 }
76         }
77 }
78
79 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
80 {
81         float largest_difference = -1.0f;
82         float squared_difference = 0.0f;
83         int largest_diff_x = -1, largest_diff_y = -1;
84
85         for (unsigned y = 0; y < height; ++y) {
86                 for (unsigned x = 0; x < width; ++x) {
87                         float diff = ref[y * width + x] - result[y * width + x];
88                         if (fabs(diff) > largest_difference) {
89                                 largest_difference = fabs(diff);
90                                 largest_diff_x = x;
91                                 largest_diff_y = y;
92                         }
93                         squared_difference += diff * diff;
94                 }
95         }
96
97         EXPECT_LT(largest_difference, largest_difference_limit)
98                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
99                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
100                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
101
102         float rms = sqrt(squared_difference) / (width * height);
103         EXPECT_LT(rms, rms_limit);
104
105         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
106                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
107
108                 fprintf(stderr, "Reference:\n");
109                 for (unsigned y = 0; y < height; ++y) {
110                         for (unsigned x = 0; x < width; ++x) {
111                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
112                         }
113                         fprintf(stderr, "\n");
114                 }
115
116                 fprintf(stderr, "\nResult:\n");
117                 for (unsigned y = 0; y < height; ++y) {
118                         for (unsigned x = 0; x < width; ++x) {
119                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
120                         }
121                         fprintf(stderr, "\n");
122                 }
123         }
124 }