2 #include "flat_input.h"
3 #include "gtest/gtest.h"
10 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height, ColorSpace color_space, GammaCurve gamma_curve)
11 : chain(width, height), width(width), height(height)
14 format.color_space = color_space;
15 format.gamma_curve = gamma_curve;
17 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
18 input->set_pixel_data(data);
19 chain.add_input(input);
22 void EffectChainTester::run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
25 format.color_space = color_space;
26 format.gamma_curve = gamma_curve;
27 chain.add_output(format);
30 glViewport(0, 0, width, height);
31 chain.render_to_screen();
33 glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
35 // Flip upside-down to compensate for different origin.
36 for (unsigned y = 0; y < height / 2; ++y) {
37 unsigned flip_y = height - y - 1;
38 for (unsigned x = 0; x < width; ++x) {
39 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
44 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
46 float largest_difference = -1.0f;
47 float squared_difference = 0.0f;
49 for (unsigned y = 0; y < height; ++y) {
50 for (unsigned x = 0; x < width; ++x) {
51 float diff = ref[y * width + x] - result[y * width + x];
52 largest_difference = std::max(largest_difference, fabsf(diff));
53 squared_difference += diff * diff;
57 const float largest_difference_limit = 1.5 / 255.0;
58 const float rms_limit = 0.5 / 255.0;
60 EXPECT_LT(largest_difference, largest_difference_limit);
62 float rms = sqrt(squared_difference) / (width * height);
63 EXPECT_LT(rms, rms_limit);
65 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
66 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
68 fprintf(stderr, "Reference:\n");
69 for (unsigned y = 0; y < height; ++y) {
70 for (unsigned x = 0; x < width; ++x) {
71 fprintf(stderr, "%7.4f ", ref[y * width + x]);
73 fprintf(stderr, "\n");
76 fprintf(stderr, "\nResult:\n");
77 for (unsigned y = 0; y < height; ++y) {
78 for (unsigned x = 0; x < width; ++x) {
79 fprintf(stderr, "%7.4f ", result[y * width + x]);
81 fprintf(stderr, "\n");