From a8eea6e9c1f32d5562594656c68dc4b9f985b790 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 14 Nov 2020 09:46:34 +0100 Subject: Add x_offset to drawable::line to be able to have drawImage render images outside the parent area. --- test/paintertest.cc | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'test') diff --git a/test/paintertest.cc b/test/paintertest.cc index f471313..fb17a91 100644 --- a/test/paintertest.cc +++ b/test/paintertest.cc @@ -31,6 +31,37 @@ #include "../plugingui/image.h" #include "../plugingui/font.h" +class TestColour +{ +public: + TestColour(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) + : colour(r, g, b, a) {} + TestColour(const GUI::Colour& colour) + : colour(colour) {} + + bool operator!=(const TestColour& other) const + { + return + colour.red() != other.colour.red() || + colour.green() != other.colour.green() || + colour.blue() != other.colour.blue() || + colour.alpha() != other.colour.alpha() + ; + } + + const GUI::Colour colour; +}; + +std::ostream& operator<<(std::ostream& stream, const TestColour& col) +{ + stream << "(" << + static_cast(col.colour.red()) << ", " << + static_cast(col.colour.green()) << ", " << + static_cast(col.colour.blue()) << ", " << + static_cast(col.colour.alpha()) << ")"; + return stream; +} + class TestableCanvas : public GUI::Canvas { @@ -48,6 +79,37 @@ private: GUI::PixelBufferAlpha pixbuf; }; +class TestImage + : public GUI::Image +{ +public: + TestImage(std::uint8_t width, std::uint8_t height, bool alpha) + : GUI::Image(":resources/logo.png") // just load some default image + { + _width = width; + _height = height; + has_alpha = alpha; + + image_data.resize(_width * _height); + image_data_raw.resize(_width * _height); + + // Store x and y coordinates as red and green colour components + for(std::uint8_t x = 0; x < _width; ++x) + { + for(std::uint8_t y = 0; y < _height; ++y) + { + image_data[x + _width * y] = GUI::Colour(x, y, 0, alpha ? 128 : 255); + image_data_raw[4 * (x + _width * y) + 0] = x; + image_data_raw[4 * (x + _width * y) + 1] = y; + image_data_raw[4 * (x + _width * y) + 2] = 0; + image_data_raw[4 * (x + _width * y) + 3] = alpha ? 128 : 255; + } + } + + valid = true; + } +}; + class PainterTest : public uUnit { @@ -56,6 +118,7 @@ public: { uUNIT_TEST(PainterTest::testDrawImage); uUNIT_TEST(PainterTest::testDrawText); + uUNIT_TEST(PainterTest::testClipping); } void testDrawImage() @@ -153,6 +216,35 @@ public: font, someText); } } + + // Test rendering images outside the container is being clipped correctly. + void testClipping() + { + TestableCanvas canvas(100, 100); + GUI::Painter painter(canvas); + + { // Without alpha + TestImage image(16, 16, false); + painter.clear(); + painter.drawImage(-10, -10, image); + auto& pixbuf = canvas.getPixelBuffer(); + + // Top left corner pixel should have the RGBA value (10, 10, 0, 255) + uUNIT_ASSERT_EQUAL(TestColour(10, 10, 0, 255), + TestColour(pixbuf.pixel(0, 0))); + } + + { // With alpha (different pipeline) + TestImage image(16, 16, true); + painter.clear(); + painter.drawImage(-10, -10, image); + auto& pixbuf = canvas.getPixelBuffer(); + + // Top left corner pixel should have the RGBA value (10, 10, 0, 128) + uUNIT_ASSERT_EQUAL(TestColour(10, 10, 0, 128), + TestColour(pixbuf.pixel(0, 0))); + } + } }; // Registers the fixture into the 'registry' -- cgit v1.2.3