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