1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// Unit tests for DitherEffect.
//
// Note: Dithering of multiple outputs is tested (somewhat weakly)
// in YCbCrConversionEffectTest.
#include <epoxy/gl.h>
#include <math.h>
#include "effect_chain.h"
#include "gtest/gtest.h"
#include "image_format.h"
#include "test_util.h"
#include "util.h"
namespace movit {
TEST(DitherEffectTest, NoDitherOnExactValues) {
const int size = 4;
float data[size * size] = {
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 1.0, 0.0,
0.0, 0.2, 1.0, 0.2,
0.0, 0.0, 0.0, 0.0,
};
unsigned char expected_data[size * size] = {
0, 255, 0, 255,
0, 255, 255, 0,
0, 51, 255, 51,
0, 0, 0, 0,
};
unsigned char out_data[size * size];
EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
check_error();
tester.get_chain()->set_dither_bits(8);
check_error();
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
check_error();
expect_equal(expected_data, out_data, size, size);
}
TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) {
const float frequency = 0.3f * M_PI;
const unsigned size = 2048;
const float amplitude = 0.25f / 255.0f; // 6 dB below what can be represented without dithering.
float data[size];
for (unsigned i = 0; i < size; ++i) {
data[i] = 0.2 + amplitude * sin(i * frequency);
}
unsigned char out_data[size];
EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
tester.get_chain()->set_dither_bits(8);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
// Measure how strong the given sinusoid is in the output.
float sum = 0.0f;
for (unsigned i = 0; i < size; ++i) {
sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
}
EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
}
} // namespace movit
|