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)
13 add_input(data, color_space, gamma_curve);
16 Input *EffectChainTester::add_input(const float *data, ColorSpace color_space, GammaCurve gamma_curve)
19 format.color_space = color_space;
20 format.gamma_curve = gamma_curve;
22 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
23 input->set_pixel_data(data);
24 chain.add_input(input);
28 void EffectChainTester::run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
31 format.color_space = color_space;
32 format.gamma_curve = gamma_curve;
33 chain.add_output(format);
36 glViewport(0, 0, width, height);
37 chain.render_to_screen();
39 glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
41 // Flip upside-down to compensate for different origin.
42 for (unsigned y = 0; y < height / 2; ++y) {
43 unsigned flip_y = height - y - 1;
44 for (unsigned x = 0; x < width; ++x) {
45 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
50 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
52 float largest_difference = -1.0f;
53 float squared_difference = 0.0f;
55 for (unsigned y = 0; y < height; ++y) {
56 for (unsigned x = 0; x < width; ++x) {
57 float diff = ref[y * width + x] - result[y * width + x];
58 largest_difference = std::max(largest_difference, fabsf(diff));
59 squared_difference += diff * diff;
63 const float largest_difference_limit = 1.5 / 255.0;
64 const float rms_limit = 0.5 / 255.0;
66 EXPECT_LT(largest_difference, largest_difference_limit);
68 float rms = sqrt(squared_difference) / (width * height);
69 EXPECT_LT(rms, rms_limit);
71 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
72 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
74 fprintf(stderr, "Reference:\n");
75 for (unsigned y = 0; y < height; ++y) {
76 for (unsigned x = 0; x < width; ++x) {
77 fprintf(stderr, "%7.4f ", ref[y * width + x]);
79 fprintf(stderr, "\n");
82 fprintf(stderr, "\nResult:\n");
83 for (unsigned y = 0; y < height; ++y) {
84 for (unsigned x = 0; x < width; ++x) {
85 fprintf(stderr, "%7.4f ", result[y * width + x]);
87 fprintf(stderr, "\n");