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