1 // Unit tests for EffectChain.
3 #include "effect_chain.h"
4 #include "flat_input.h"
5 #include "sandbox_effect.h"
7 #include "gtest/gtest.h"
14 class EffectChainTester {
16 EffectChainTester(const float *data, unsigned width, unsigned height, ColorSpace color_space, GammaCurve gamma_curve)
17 : chain(width, height), width(width), height(height)
20 format.color_space = color_space;
21 format.gamma_curve = gamma_curve;
23 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
24 input->set_pixel_data(data);
25 chain.add_input(input);
28 EffectChain *get_chain() { return &chain; }
30 void run(float *out_data, ColorSpace color_space, GammaCurve gamma_curve)
33 format.color_space = color_space;
34 format.gamma_curve = gamma_curve;
35 chain.add_output(format);
38 glViewport(0, 0, width, height);
39 chain.render_to_screen();
41 glReadPixels(0, 0, width, height, GL_RED, GL_FLOAT, out_data);
43 // Flip upside-down to compensate for different origin.
44 for (unsigned y = 0; y < height / 2; ++y) {
45 unsigned flip_y = height - y - 1;
46 for (unsigned x = 0; x < width; ++x) {
47 std::swap(out_data[y * width + x], out_data[flip_y * width + x]);
54 unsigned width, height;
57 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height)
59 float largest_difference = -1.0f;
60 float squared_difference = 0.0f;
62 for (unsigned y = 0; y < height; ++y) {
63 for (unsigned x = 0; x < width; ++x) {
64 float diff = ref[y * width + x] - result[y * width + x];
65 largest_difference = std::max(largest_difference, fabsf(diff));
66 squared_difference += diff * diff;
70 const float largest_difference_limit = 1.5 / 255.0;
71 const float rms_limit = 0.5 / 255.0;
73 EXPECT_LT(largest_difference, largest_difference_limit);
75 float rms = sqrt(squared_difference) / (width * height);
76 EXPECT_LT(rms, rms_limit);
78 if (largest_difference >= largest_difference_limit || rms >= rms_limit) {
79 fprintf(stderr, "Dumping matrices for easier debugging, since at least one test failed.\n");
81 fprintf(stderr, "Reference:\n");
82 for (unsigned y = 0; y < height; ++y) {
83 for (unsigned x = 0; x < width; ++x) {
84 fprintf(stderr, "%7.4f ", ref[y * width + x]);
86 fprintf(stderr, "\n");
89 fprintf(stderr, "\nResult:\n");
90 for (unsigned y = 0; y < height; ++y) {
91 for (unsigned x = 0; x < width; ++x) {
92 fprintf(stderr, "%7.4f ", result[y * width + x]);
94 fprintf(stderr, "\n");
99 TEST(EffectChainTest, Identity) {
105 EffectChainTester tester(data, 3, 2, COLORSPACE_sRGB, GAMMA_LINEAR);
106 tester.run(out_data, COLORSPACE_sRGB, GAMMA_LINEAR);
108 expect_equal(data, out_data, 3, 2);