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