mirror of
https://github.com/SFML/SFML.git
synced 2025-02-08 09:28:02 +08:00
Ensure sf::Image
remains unchanged after an unsuccessful load
This commit is contained in:
parent
355df11d09
commit
0a2e9ac340
@ -210,9 +210,6 @@ bool Image::loadFromFile(const std::filesystem::path& filename)
|
||||
|
||||
#endif
|
||||
|
||||
// Clear the array (just in case)
|
||||
m_pixels.clear();
|
||||
|
||||
// Set up the stb_image callbacks for the std::ifstream
|
||||
const auto readStdIfStream = [](void* user, char* data, int size)
|
||||
{
|
||||
@ -267,9 +264,6 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
||||
// Check input parameters
|
||||
if (data && size)
|
||||
{
|
||||
// Clear the array (just in case)
|
||||
m_pixels.clear();
|
||||
|
||||
// Load the image and get a pointer to the pixels in memory
|
||||
Vector2i imageSize;
|
||||
int channels = 0;
|
||||
@ -295,9 +289,6 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
||||
////////////////////////////////////////////////////////////
|
||||
bool Image::loadFromStream(InputStream& stream)
|
||||
{
|
||||
// Clear the array (just in case)
|
||||
m_pixels.clear();
|
||||
|
||||
// Make sure that the stream's reading position is at the beginning
|
||||
if (!stream.seek(0).has_value())
|
||||
{
|
||||
|
@ -321,6 +321,17 @@ TEST_CASE("[Graphics] sf::Image")
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
}
|
||||
|
||||
SECTION("Successful then unsuccessful load")
|
||||
{
|
||||
REQUIRE(image.loadFromFile("Graphics/sfml-logo-big.jpg"));
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
|
||||
REQUIRE(!image.loadFromFile("does-not-exist.jpg"));
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("loadFromMemory()")
|
||||
@ -355,8 +366,6 @@ TEST_CASE("[Graphics] sf::Image")
|
||||
CHECK(!image.loadFromMemory(memory.data(), memory.size()));
|
||||
}
|
||||
|
||||
SECTION("Successful load")
|
||||
{
|
||||
const auto memory = []()
|
||||
{
|
||||
sf::Image savedImage;
|
||||
@ -364,12 +373,25 @@ TEST_CASE("[Graphics] sf::Image")
|
||||
return savedImage.saveToMemory("png").value();
|
||||
}();
|
||||
|
||||
SECTION("Successful load")
|
||||
{
|
||||
CHECK(image.loadFromMemory(memory.data(), memory.size()));
|
||||
CHECK(image.getSize() == sf::Vector2u(24, 24));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
CHECK(image.getPixel({0, 0}) == sf::Color::Green);
|
||||
CHECK(image.getPixel({23, 23}) == sf::Color::Green);
|
||||
}
|
||||
|
||||
SECTION("Successful then unsuccessful load")
|
||||
{
|
||||
REQUIRE(image.loadFromMemory(memory.data(), memory.size()));
|
||||
CHECK(image.getSize() == sf::Vector2u(24, 24));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
|
||||
REQUIRE(!image.loadFromMemory(memory.data(), 1));
|
||||
CHECK(image.getSize() == sf::Vector2u(24, 24));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("loadFromStream()")
|
||||
@ -382,15 +404,28 @@ TEST_CASE("[Graphics] sf::Image")
|
||||
CHECK(!image.loadFromStream(stream));
|
||||
}
|
||||
|
||||
REQUIRE(stream.open("Graphics/sfml-logo-big.png"));
|
||||
|
||||
SECTION("Successful load")
|
||||
{
|
||||
CHECK(stream.open("Graphics/sfml-logo-big.png"));
|
||||
REQUIRE(image.loadFromStream(stream));
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0));
|
||||
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
|
||||
}
|
||||
|
||||
SECTION("Successful then unsuccessful load")
|
||||
{
|
||||
REQUIRE(image.loadFromStream(stream));
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
|
||||
sf::FileInputStream emptyStream;
|
||||
REQUIRE(!image.loadFromStream(emptyStream));
|
||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||
CHECK(image.getPixelsPtr() != nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("saveToFile()")
|
||||
|
Loading…
x
Reference in New Issue
Block a user