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