SFML/test/Graphics/Image.test.cpp

305 lines
9.8 KiB
C++
Raw Normal View History

2022-06-18 01:03:21 +08:00
#include <SFML/Graphics/Image.hpp>
2023-01-18 12:51:08 +08:00
#include <catch2/catch_test_macros.hpp>
2022-06-18 01:03:21 +08:00
2022-07-05 00:20:58 +08:00
#include <GraphicsUtil.hpp>
#include <array>
#include <type_traits>
TEST_CASE("[Graphics] sf::Image")
2022-06-18 01:03:21 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Type traits")
{
STATIC_CHECK(std::is_copy_constructible_v<sf::Image>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Image>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Image>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Image>);
}
SECTION("Default constructor")
2022-06-18 01:03:21 +08:00
{
const sf::Image image;
CHECK(image.getSize() == sf::Vector2u());
CHECK(image.getPixelsPtr() == nullptr);
}
2023-01-18 12:51:08 +08:00
SECTION("Create")
2022-06-18 01:03:21 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("create(Vector2)")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10));
CHECK(image.getSize() == sf::Vector2u(10, 10));
CHECK(image.getPixelsPtr() != nullptr);
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
2023-03-20 03:43:04 +08:00
CHECK(image.getPixel(sf::Vector2u(i, j)) == sf::Color::Black);
2022-06-18 01:03:21 +08:00
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("create(Vector2, Color)")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Red);
CHECK(image.getSize() == sf::Vector2u(10, 10));
CHECK(image.getPixelsPtr() != nullptr);
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image.getPixel(sf::Vector2u(i, j)) == sf::Color::Red);
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("create(Vector2, std::uint8_t*)")
2022-06-18 01:03:21 +08:00
{
// 10 x 10, with 4 colour channels array
std::array<std::uint8_t, 400> pixels;
2022-06-18 01:03:21 +08:00
for (std::size_t i = 0; i < pixels.size(); i += 4)
{
2022-07-05 00:20:58 +08:00
pixels[i] = 255; // r
2022-06-18 01:03:21 +08:00
pixels[i + 1] = 0; // g
pixels[i + 2] = 0; // b
pixels[i + 3] = 255; // a
}
sf::Image image;
image.create(sf::Vector2u(10, 10), pixels.data());
CHECK(image.getSize() == sf::Vector2u(10, 10));
CHECK(image.getPixelsPtr() != nullptr);
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image.getPixel(sf::Vector2u(i, j)) == sf::Color::Red);
}
}
}
}
SECTION("loadFromFile()")
{
sf::Image image;
SECTION("bmp")
{
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.bmp"));
CHECK(image.getPixel({0, 0}) == sf::Color::White);
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
}
SECTION("png")
{
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.png"));
CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0));
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
}
SECTION("jpg")
{
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.jpg"));
CHECK(image.getPixel({0, 0}) == sf::Color::White);
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
}
SECTION("gif")
{
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.gif"));
CHECK(image.getPixel({0, 0}) == sf::Color::White);
CHECK(image.getPixel({200, 150}) == sf::Color(146, 210, 62));
}
SECTION("psd")
{
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.psd"));
CHECK(image.getPixel({0, 0}) == sf::Color::White);
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
}
CHECK(image.getSize() == sf::Vector2u(1001, 304));
CHECK(image.getPixelsPtr() != nullptr);
}
2023-01-18 12:51:08 +08:00
SECTION("Set/get pixel")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Green);
CHECK(image.getPixel(sf::Vector2u(2, 2)) == sf::Color::Green);
image.setPixel(sf::Vector2u(2, 2), sf::Color::Blue);
CHECK(image.getPixel(sf::Vector2u(2, 2)) == sf::Color::Blue);
}
2023-01-18 12:51:08 +08:00
SECTION("Copy from Image")
2022-06-18 01:03:21 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("Copy (Image, Vector2u)")
2022-06-18 01:03:21 +08:00
{
sf::Image image1;
image1.create(sf::Vector2u(10, 10), sf::Color::Blue);
sf::Image image2;
image2.create(sf::Vector2u(10, 10));
CHECK(image2.copy(image1, sf::Vector2u(0, 0)));
2022-06-18 01:03:21 +08:00
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image1.getPixel(sf::Vector2u(i, j)) == image2.getPixel(sf::Vector2u(i, j)));
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Copy (Image, Vector2u, IntRect)")
2022-06-18 01:03:21 +08:00
{
sf::Image image1;
image1.create(sf::Vector2u(5, 5), sf::Color::Blue);
sf::Image image2;
image2.create(sf::Vector2u(10, 10));
CHECK(image2.copy(image1, sf::Vector2u(0, 0), sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(5, 5))));
2022-06-18 01:03:21 +08:00
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
if (i <= 4 && j <= 4)
CHECK(image2.getPixel(sf::Vector2u(i, j)) == sf::Color::Blue);
else
2023-03-20 03:43:04 +08:00
CHECK(image2.getPixel(sf::Vector2u(i, j)) == sf::Color::Black);
2022-06-18 01:03:21 +08:00
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Copy (Image, Vector2u, IntRect, bool)")
2022-06-18 01:03:21 +08:00
{
const sf::Color dest(255, 0, 0, 255);
const sf::Color source(5, 255, 78, 232);
// Create the composited colour for via the alpha composite over operation
const auto a = static_cast<std::uint8_t>(source.a + (dest.a * (255 - source.a)) / 255);
const auto r = static_cast<std::uint8_t>(
2022-07-05 00:20:58 +08:00
((source.r * source.a) + (((dest.r * dest.a) * (255 - source.a))) / 255) / a);
const auto g = static_cast<std::uint8_t>(
2022-07-05 00:20:58 +08:00
((source.g * source.a) + (((dest.g * dest.a) * (255 - source.a))) / 255) / a);
const auto b = static_cast<std::uint8_t>(
2022-07-05 00:20:58 +08:00
((source.b * source.a) + (((dest.b * dest.a) * (255 - source.a))) / 255) / a);
2022-06-18 01:03:21 +08:00
const sf::Color composite(r, g, b, a);
sf::Image image1;
image1.create(sf::Vector2u(10, 10), dest);
sf::Image image2;
image2.create(sf::Vector2u(10, 10), source);
CHECK(image1.copy(image2, sf::Vector2u(0, 0), sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(10, 10)), true));
2022-06-18 01:03:21 +08:00
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image1.getPixel(sf::Vector2u(i, j)) == composite);
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Copy (Empty image)")
2022-06-18 01:03:21 +08:00
{
const sf::Image image1;
sf::Image image2;
2022-06-18 01:03:21 +08:00
image2.create(sf::Vector2u(10, 10), sf::Color::Red);
CHECK(!image2.copy(image1, sf::Vector2u(0, 0), sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(9, 9))));
2022-06-18 01:03:21 +08:00
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image2.getPixel(sf::Vector2u(i, j)) == sf::Color::Red);
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Copy (Out of bounds sourceRect)")
2022-06-18 01:03:21 +08:00
{
sf::Image image1;
image1.create(sf::Vector2u(5, 5), sf::Color::Blue);
sf::Image image2;
image2.create(sf::Vector2u(10, 10), sf::Color::Red);
CHECK(!image2.copy(image1, sf::Vector2u(0, 0), sf::IntRect(sf::Vector2i(5, 5), sf::Vector2i(9, 9))));
2022-06-18 01:03:21 +08:00
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image2.getPixel(sf::Vector2u(i, j)) == sf::Color::Red);
}
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Create mask from color")
2022-06-18 01:03:21 +08:00
{
2023-01-18 12:51:08 +08:00
SECTION("createMaskFromColor(Color)")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Blue);
image.createMaskFromColor(sf::Color::Blue);
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image.getPixel(sf::Vector2u(i, j)) == sf::Color(0, 0, 255, 0));
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("createMaskFromColor(Color, std::uint8_t)")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Blue);
image.createMaskFromColor(sf::Color::Blue, 100);
for (std::uint32_t i = 0; i < 10; ++i)
2022-06-18 01:03:21 +08:00
{
for (std::uint32_t j = 0; j < 10; ++j)
2022-06-18 01:03:21 +08:00
{
CHECK(image.getPixel(sf::Vector2u(i, j)) == sf::Color(0, 0, 255, 100));
}
}
}
}
2023-01-18 12:51:08 +08:00
SECTION("Flip horizontally")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Red);
image.setPixel(sf::Vector2u(0, 0), sf::Color::Green);
image.flipHorizontally();
CHECK(image.getPixel(sf::Vector2u(9, 0)) == sf::Color::Green);
}
2023-01-18 12:51:08 +08:00
SECTION("Flip vertically")
2022-06-18 01:03:21 +08:00
{
sf::Image image;
image.create(sf::Vector2u(10, 10), sf::Color::Red);
image.setPixel(sf::Vector2u(0, 0), sf::Color::Green);
image.flipVertically();
CHECK(image.getPixel(sf::Vector2u(0, 9)) == sf::Color::Green);
}
}