Add a unit test for FlatInput.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Oct 2012 13:00:24 +0000 (15:00 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Oct 2012 13:00:24 +0000 (15:00 +0200)
.gitignore
Makefile
flat_input_test.cpp [new file with mode: 0644]
test_util.cpp
test_util.h

index e119ad8..c1a6dd7 100644 (file)
@@ -17,4 +17,5 @@ saturation_effect_test
 deconvolution_sharpen_effect_test
 blur_effect_test
 unsharp_mask_effect_test
+flat_input_test
 chain-*.frag
index 3747597..27bca34 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ TESTS += deconvolution_sharpen_effect_test
 TESTS += blur_effect_test
 TESTS += unsharp_mask_effect_test
 TESTS += diffusion_effect_test
+TESTS += flat_input_test
 
 # Core.
 LIB_OBJS=util.o widgets.o effect.o effect_chain.o
@@ -82,6 +83,8 @@ unsharp_mask_effect_test: unsharp_mask_effect_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
 diffusion_effect_test: diffusion_effect_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
+flat_input_test: flat_input_test.o $(TEST_OBJS) libmovit.a
+       $(CXX) -o $@ $^ $(LDFLAGS)
 
 OBJS=$(DEMO_OBJS) $(LIB_OBJS) $(TEST_OBJS) $(TESTS:=.o)
 
diff --git a/flat_input_test.cpp b/flat_input_test.cpp
new file mode 100644 (file)
index 0000000..17ee9ce
--- /dev/null
@@ -0,0 +1,194 @@
+// Unit tests for FlatInput.
+
+#include "test_util.h"
+#include "gtest/gtest.h"
+#include "flat_input.h"
+
+TEST(FlatInput, SimpleGrayscale) {
+       const int size = 4;
+
+       float data[size] = {
+               0.0,
+               0.5,
+               0.7,
+               1.0,
+       };
+       float expected_data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.5, 0.5, 0.5, 1.0,
+               0.7, 0.7, 0.7, 1.0,
+               1.0, 1.0, 1.0, 1.0,
+       };
+       float out_data[4 * size];
+
+       EffectChainTester tester(data, 1, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(FlatInput, RGB) {
+       const int size = 5;
+
+       float data[3 * size] = {
+               0.0, 0.0, 0.0,
+               0.5, 0.0, 0.0,
+               0.0, 0.5, 0.0,
+               0.0, 0.0, 0.7,
+               0.0, 0.3, 0.7,
+       };
+       float expected_data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.5, 0.0, 0.0, 1.0,
+               0.0, 0.5, 0.0, 1.0,
+               0.0, 0.0, 0.7, 1.0,
+               0.0, 0.3, 0.7, 1.0,
+       };
+       float out_data[4 * size];
+
+       EffectChainTester tester(data, 1, size, FORMAT_RGB, COLORSPACE_sRGB, GAMMA_LINEAR);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(FlatInput, RGBA) {
+       const int size = 5;
+
+       float data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.5, 0.0, 0.0, 0.3,
+               0.0, 0.5, 0.0, 0.7,
+               0.0, 0.0, 0.7, 1.0,
+               0.0, 0.3, 0.7, 0.2,
+       };
+       float expected_data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.5, 0.0, 0.0, 0.3,
+               0.0, 0.5, 0.0, 0.7,
+               0.0, 0.0, 0.7, 1.0,
+               0.0, 0.3, 0.7, 0.2,
+       };
+       float out_data[4 * size];
+
+       EffectChainTester tester(data, 1, size, FORMAT_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(FlatInput, BGR) {
+       const int size = 5;
+
+       float data[3 * size] = {
+               0.0, 0.0, 0.0,
+               0.5, 0.0, 0.0,
+               0.0, 0.5, 0.0,
+               0.0, 0.0, 0.7,
+               0.0, 0.3, 0.7,
+       };
+       float expected_data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.0, 0.0, 0.5, 1.0,
+               0.0, 0.5, 0.0, 1.0,
+               0.7, 0.0, 0.0, 1.0,
+               0.7, 0.3, 0.0, 1.0,
+       };
+       float out_data[4 * size];
+
+       EffectChainTester tester(data, 1, size, FORMAT_BGR, COLORSPACE_sRGB, GAMMA_LINEAR);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(FlatInput, BGRA) {
+       const int size = 5;
+
+       float data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.5, 0.0, 0.0, 0.3,
+               0.0, 0.5, 0.0, 0.7,
+               0.0, 0.0, 0.7, 1.0,
+               0.0, 0.3, 0.7, 0.2,
+       };
+       float expected_data[4 * size] = {
+               0.0, 0.0, 0.0, 1.0,
+               0.0, 0.0, 0.5, 0.3,
+               0.0, 0.5, 0.0, 0.7,
+               0.7, 0.0, 0.0, 1.0,
+               0.7, 0.3, 0.0, 0.2,
+       };
+       float out_data[4 * size];
+
+       EffectChainTester tester(data, 1, size, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(FlatInput, Pitch) {
+       const int pitch = 3;
+       const int width = 2;
+       const int height = 4;
+
+       float data[pitch * height] = {
+               0.0, 1.0, 999.0f,
+               0.5, 0.5, 999.0f,
+               0.7, 0.2, 999.0f,
+               1.0, 0.6, 999.0f,
+       };
+       float expected_data[4 * width * height] = {
+               0.0, 0.0, 0.0, 1.0,  1.0, 1.0, 1.0, 1.0,
+               0.5, 0.5, 0.5, 1.0,  0.5, 0.5, 0.5, 1.0,
+               0.7, 0.7, 0.7, 1.0,  0.2, 0.2, 0.2, 1.0,
+               1.0, 1.0, 1.0, 1.0,  0.6, 0.6, 0.6, 1.0,
+       };
+       float out_data[4 * width * height];
+
+       EffectChainTester tester(NULL, width, height);
+
+       ImageFormat format;
+       format.color_space = COLORSPACE_sRGB;
+       format.gamma_curve = GAMMA_LINEAR;
+
+       FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
+       input->set_pitch(pitch);
+       input->set_pixel_data(data);
+       tester.get_chain()->add_input(input);
+
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       expect_equal(expected_data, out_data, 4 * width, height);
+}
+
+TEST(FlatInput, UpdatedData) {
+       const int width = 2;
+       const int height = 4;
+
+       float data[width * height] = {
+               0.0, 1.0,
+               0.5, 0.5,
+               0.7, 0.2,
+               1.0, 0.6,
+       };
+       float out_data[width * height];
+
+       EffectChainTester tester(NULL, width, height);
+
+       ImageFormat format;
+       format.color_space = COLORSPACE_sRGB;
+       format.gamma_curve = GAMMA_LINEAR;
+
+       FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
+       input->set_pixel_data(data);
+       tester.get_chain()->add_input(input);
+
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+       expect_equal(data, out_data, width, height);
+
+       data[6] = 0.3;
+       input->invalidate_pixel_data();
+
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+       expect_equal(data, out_data, width, height);
+}
index 1730907..6beaba1 100644 (file)
@@ -9,7 +9,7 @@
 
 EffectChainTester::EffectChainTester(const float *data, unsigned width, unsigned height,
                                      MovitPixelFormat pixel_format, Colorspace color_space, GammaCurve gamma_curve)
-       : chain(width, height), width(width), height(height)
+       : chain(width, height), width(width), height(height), finalized(false)
 {
        if (data != NULL) {
                add_input(data, pixel_format, color_space, gamma_curve);
@@ -71,11 +71,14 @@ Input *EffectChainTester::add_input(const unsigned char *data, MovitPixelFormat
 
 void EffectChainTester::run(float *out_data, GLenum format, Colorspace color_space, GammaCurve gamma_curve)
 {
-       ImageFormat image_format;
-       image_format.color_space = color_space;
-       image_format.gamma_curve = gamma_curve;
-       chain.add_output(image_format);
-       chain.finalize();
+       if (!finalized) {
+               ImageFormat image_format;
+               image_format.color_space = color_space;
+               image_format.gamma_curve = gamma_curve;
+               chain.add_output(image_format);
+               chain.finalize();
+               finalized = true;
+       }
 
        chain.render_to_fbo(fbo, width, height);
 
index 2430b8f..cc7318b 100644 (file)
@@ -20,6 +20,7 @@ private:
        EffectChain chain;
        GLuint fbo, texnum;
        unsigned width, height;
+       bool finalized;
 };
 
 void expect_equal(const float *ref, const float *result, unsigned width, unsigned height, float largest_difference_limit = 1.5 / 255.0, float rms_limit = 0.2 / 255.0);