]> git.sesse.net Git - movit/blob - test_util.cpp
cc0280119aaab6c2c7984f91cb8cdf169aad4b99
[movit] / test_util.cpp
1 #include "init.h"
2 #include "test_util.h"
3 #include "flat_input.h"
4 #include "gtest/gtest.h"
5
6 #include <stdio.h>
7 #include <math.h>
8
9 #include <algorithm>
10
11 namespace {
12
13 // Flip upside-down to compensate for different origin.
14 template<class T>
15 void vertical_flip(T *data, unsigned width, unsigned height)
16 {
17         for (unsigned y = 0; y < height / 2; ++y) {
18                 unsigned flip_y = height - y - 1;
19                 for (unsigned x = 0; x < width; ++x) {
20                         std::swap(data[y * width + x], data[flip_y * width + x]);
21                 }
22         }
23 }
24
25 }  // namespace
26
27 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
28                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
29         : chain(width, height), width(width), height(height), finalized(false)
30 {
31         init_movit();
32
33         if (data != NULL) {
34                 add_input(data, pixel_format, color_space, gamma_curve);
35         }
36
37         glGenTextures(1, &texnum);
38         check_error();
39         glBindTexture(GL_TEXTURE_2D, texnum);
40         check_error();
41         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
42         check_error();
43
44         glGenFramebuffers(1, &fbo);
45         check_error();
46         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
47         check_error();
48         glFramebufferTexture2D(
49                 GL_FRAMEBUFFER,
50                 GL_COLOR_ATTACHMENT0,
51                 GL_TEXTURE_2D,
52                 texnum,
53                 0);
54         check_error();
55         glBindFramebuffer(GL_FRAMEBUFFER, 0);
56         check_error();
57 }
58
59 EffectChainTester::~EffectChainTester()
60 {
61         glDeleteFramebuffers(1, &fbo);
62         check_error();
63         glDeleteTextures(1, &texnum);
64         check_error();
65 }
66
67 Input *EffectChainTester::add_input(const float *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
68 {
69         ImageFormat format;
70         format.color_space = color_space;
71         format.gamma_curve = gamma_curve;
72
73         FlatInput *input = new FlatInput(format, pixel_format, GL_FLOAT, width, height);
74         input->set_pixel_data(data);
75         chain.add_input(input);
76         return input;
77 }
78
79 Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
80 {
81         ImageFormat format;
82         format.color_space = color_space;
83         format.gamma_curve = gamma_curve;
84
85         FlatInput *input = new FlatInput(format, pixel_format, GL_UNSIGNED_BYTE, width, height);
86         input->set_pixel_data(data);
87         chain.add_input(input);
88         return input;
89 }
90
91 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
92 {
93         if (!finalized) {
94                 finalize_chain(color_space, gamma_curve);
95         }
96
97         chain.render_to_fbo(fbo, width, height);
98
99         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
100         glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
101
102         if (format == GL_RGBA) {
103                 width *= 4;
104         }
105
106         vertical_flip(out_data, width, height);
107 }
108
109 void EffectChainTester::run(unsigned char *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
110 {
111         if (!finalized) {
112                 finalize_chain(color_space, gamma_curve);
113         }
114
115         chain.render_to_fbo(fbo, width, height);
116
117         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
118         glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, out_data);
119
120         if (format == GL_RGBA) {
121                 width *= 4;
122         }
123
124         vertical_flip(out_data, width, height);
125 }
126
127 void EffectChainTester::finalize_chain(Colorspace color_space, GammaCurve gamma_curve)
128 {
129         assert(!finalized);
130         ImageFormat image_format;
131         image_format.color_space = color_space;
132         image_format.gamma_curve = gamma_curve;
133         chain.add_output(image_format);
134         chain.finalize();
135         finalized = true;
136 }
137
138 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
139 {
140         float largest_difference = -1.0f;
141         float squared_difference = 0.0f;
142         int largest_diff_x = -1, largest_diff_y = -1;
143
144         for (unsigned y = 0; y < height; ++y) {
145                 for (unsigned x = 0; x < width; ++x) {
146                         float diff = ref[y * width + x] - result[y * width + x];
147                         if (fabs(diff) > largest_difference) {
148                                 largest_difference = fabs(diff);
149                                 largest_diff_x = x;
150                                 largest_diff_y = y;
151                         }
152                         squared_difference += diff * diff;
153                 }
154         }
155
156         EXPECT_LT(largest_difference, largest_difference_limit)
157                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
158                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
159                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
160
161         float rms = sqrt(squared_difference) / (width * height);
162         EXPECT_LT(rms, rms_limit);
163
164         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
165                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
166
167                 fprintf(stderr, "Reference:\n");
168                 for (unsigned y = 0; y < height; ++y) {
169                         for (unsigned x = 0; x < width; ++x) {
170                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
171                         }
172                         fprintf(stderr, "\n");
173                 }
174
175                 fprintf(stderr, "\nResult:\n");
176                 for (unsigned y = 0; y < height; ++y) {
177                         for (unsigned x = 0; x < width; ++x) {
178                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
179                         }
180                         fprintf(stderr, "\n");
181                 }
182         }
183 }
184
185 void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit, float rms_limit)
186 {
187         float *ref_float = new float[width * height];
188         float *result_float = new float[width * height];
189
190         for (unsigned y = 0; y < height; ++y) {
191                 for (unsigned x = 0; x < width; ++x) {
192                         ref_float[y * width + x] = ref[y * width + x];
193                         result_float[y * width + x] = result[y * width + x];
194                 }
195         }
196
197         expect_equal(ref_float, result_float, width, height, largest_difference_limit, rms_limit);
198 }