]> git.sesse.net Git - movit/blob - test_util.cpp
Remove dead function set_uniform_float_array().
[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), finalized(false)
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         if (!finalized) {
75                 ImageFormat image_format;
76                 image_format.color_space = color_space;
77                 image_format.gamma_curve = gamma_curve;
78                 chain.add_output(image_format);
79                 chain.finalize();
80                 finalized = true;
81         }
82
83         chain.render_to_fbo(fbo, width, height);
84
85         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
86         glReadPixels(0, 0, width, height, format, GL_FLOAT, out_data);
87
88         if (format == GL_RGBA) {
89                 width *= 4;
90         }
91
92         // Flip upside-down to compensate for different origin.
93         for (unsigned y = 0; y < height / 2; ++y) {
94                 unsigned flip_y = height - y - 1;
95                 for (unsigned x = 0; x < width; ++x) {
96                         std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
97                 }
98         }
99 }
100
101 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit, float rms_limit)
102 {
103         float largest_difference = -1.0f;
104         float squared_difference = 0.0f;
105         int largest_diff_x = -1, largest_diff_y = -1;
106
107         for (unsigned y = 0; y < height; ++y) {
108                 for (unsigned x = 0; x < width; ++x) {
109                         float diff = ref[y * width + x] - result[y * width + x];
110                         if (fabs(diff) > largest_difference) {
111                                 largest_difference = fabs(diff);
112                                 largest_diff_x = x;
113                                 largest_diff_y = y;
114                         }
115                         squared_difference += diff * diff;
116                 }
117         }
118
119         EXPECT_LT(largest_difference, largest_difference_limit)
120                 << "Largest difference is in x=" << largest_diff_x << ", y=" << largest_diff_y << ":\n"
121                 << "Reference: " << ref[largest_diff_y * width + largest_diff_x] << "\n"
122                 << "Result:    " << result[largest_diff_y * width + largest_diff_x];
123
124         float rms = sqrt(squared_difference) / (width * height);
125         EXPECT_LT(rms, rms_limit);
126
127         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
128                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
129
130                 fprintf(stderr, "Reference:\n");
131                 for (unsigned y = 0; y < height; ++y) {
132                         for (unsigned x = 0; x < width; ++x) {
133                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
134                         }
135                         fprintf(stderr, "\n");
136                 }
137
138                 fprintf(stderr, "\nResult:\n");
139                 for (unsigned y = 0; y < height; ++y) {
140                         for (unsigned x = 0; x < width; ++x) {
141                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
142                         }
143                         fprintf(stderr, "\n");
144                 }
145         }
146 }