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