]> git.sesse.net Git - movit/blob - test_util.cpp
Add a unit test for GammaCompressionEffect.
[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, ColorSpace color_space, GammaCurve gamma_curve)
11         : chain(width, height), width(width), height(height)
12 {
13         add_input(data, color_space, gamma_curve);
14
15         glGenTextures(1, &texnum);
16         check_error();
17         glBindTexture(GL_TEXTURE_2D, texnum);
18         check_error();
19         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
20         check_error();
21
22         glGenFramebuffers(1, &fbo);
23         check_error();
24         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
25         check_error();
26         glFramebufferTexture2D(
27                 GL_FRAMEBUFFER,
28                 GL_COLOR_ATTACHMENT0,
29                 GL_TEXTURE_2D,
30                 texnum,
31                 0);
32         check_error();
33         glBindFramebuffer(GL_FRAMEBUFFER, 0);
34         check_error();
35 }
36
37 Input *EffectChainTester::add_input(const float *data, ColorSpace color_space, GammaCurve gamma_curve)
38 {
39         ImageFormat format;
40         format.color_space = color_space;
41         format.gamma_curve = gamma_curve;
42
43         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
44         input->set_pixel_data(data);
45         chain.add_input(input);
46         return input;
47 }
48
49 void EffectChainTester::run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
50 {
51         ImageFormat format;
52         format.color_space = color_space;
53         format.gamma_curve = gamma_curve;
54         chain.add_output(format);
55         chain.finalize();
56
57         chain.render_to_fbo(fbo, width, height);
58
59         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
60         glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
61
62         // Flip upside-down to compensate for different origin.
63         for (unsigned y = 0; y < height / 2; ++y) {
64                 unsigned flip_y = height - y - 1;
65                 for (unsigned x = 0; x < width; ++x) {
66                         std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
67                 }
68         }
69 }
70
71 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
72 {
73         float largest_difference = -1.0f;
74         float squared_difference = 0.0f;
75
76         for (unsigned y = 0; y < height; ++y) {
77                 for (unsigned x = 0; x < width; ++x) {
78                         float diff = ref[y * width + x] - result[y * width + x];
79                         largest_difference = std::max(largest_difference, fabsf(diff));
80                         squared_difference += diff * diff;
81                 }
82         }
83
84         const float largest_difference_limit = 1.5 / 255.0;
85         const float rms_limit = 0.5 / 255.0;
86
87         EXPECT_LT(largest_difference, largest_difference_limit);
88
89         float rms = sqrt(squared_difference) / (width * height);
90         EXPECT_LT(rms, rms_limit);
91
92         if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
93                 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
94
95                 fprintf(stderr, "Reference:\n");
96                 for (unsigned y = 0; y < height; ++y) {
97                         for (unsigned x = 0; x < width; ++x) {
98                                 fprintf(stderr, "%7.4f ", ref[y * width + x]);
99                         }
100                         fprintf(stderr, "\n");
101                 }
102
103                 fprintf(stderr, "\nResult:\n");
104                 for (unsigned y = 0; y < height; ++y) {
105                         for (unsigned x = 0; x < width; ++x) {
106                                 fprintf(stderr, "%7.4f ", result[y * width + x]);
107                         }
108                         fprintf(stderr, "\n");
109                 }
110         }
111 }