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
|
#endif
|
||||||
|
|
||||||
// Clear the array (just in case)
|
|
||||||
m_pixels.clear();
|
|
||||||
|
|
||||||
// Set up the stb_image callbacks for the std::ifstream
|
// Set up the stb_image callbacks for the std::ifstream
|
||||||
const auto readStdIfStream = [](void* user, char* data, int size)
|
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
|
// Check input parameters
|
||||||
if (data && size)
|
if (data && size)
|
||||||
{
|
{
|
||||||
// Clear the array (just in case)
|
|
||||||
m_pixels.clear();
|
|
||||||
|
|
||||||
// Load the image and get a pointer to the pixels in memory
|
// Load the image and get a pointer to the pixels in memory
|
||||||
Vector2i imageSize;
|
Vector2i imageSize;
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
@ -295,9 +289,6 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool Image::loadFromStream(InputStream& stream)
|
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
|
// Make sure that the stream's reading position is at the beginning
|
||||||
if (!stream.seek(0).has_value())
|
if (!stream.seek(0).has_value())
|
||||||
{
|
{
|
||||||
|
@ -321,6 +321,17 @@ TEST_CASE("[Graphics] sf::Image")
|
|||||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||||
CHECK(image.getPixelsPtr() != nullptr);
|
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()")
|
SECTION("loadFromMemory()")
|
||||||
@ -355,21 +366,32 @@ TEST_CASE("[Graphics] sf::Image")
|
|||||||
CHECK(!image.loadFromMemory(memory.data(), memory.size()));
|
CHECK(!image.loadFromMemory(memory.data(), memory.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto memory = []()
|
||||||
|
{
|
||||||
|
sf::Image savedImage;
|
||||||
|
savedImage.resize({24, 24}, sf::Color::Green);
|
||||||
|
return savedImage.saveToMemory("png").value();
|
||||||
|
}();
|
||||||
|
|
||||||
SECTION("Successful load")
|
SECTION("Successful load")
|
||||||
{
|
{
|
||||||
const auto memory = []()
|
|
||||||
{
|
|
||||||
sf::Image savedImage;
|
|
||||||
savedImage.resize({24, 24}, sf::Color::Green);
|
|
||||||
return savedImage.saveToMemory("png").value();
|
|
||||||
}();
|
|
||||||
|
|
||||||
CHECK(image.loadFromMemory(memory.data(), memory.size()));
|
CHECK(image.loadFromMemory(memory.data(), memory.size()));
|
||||||
CHECK(image.getSize() == sf::Vector2u(24, 24));
|
CHECK(image.getSize() == sf::Vector2u(24, 24));
|
||||||
CHECK(image.getPixelsPtr() != nullptr);
|
CHECK(image.getPixelsPtr() != nullptr);
|
||||||
CHECK(image.getPixel({0, 0}) == sf::Color::Green);
|
CHECK(image.getPixel({0, 0}) == sf::Color::Green);
|
||||||
CHECK(image.getPixel({23, 23}) == 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()")
|
SECTION("loadFromStream()")
|
||||||
@ -382,15 +404,28 @@ TEST_CASE("[Graphics] sf::Image")
|
|||||||
CHECK(!image.loadFromStream(stream));
|
CHECK(!image.loadFromStream(stream));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
REQUIRE(stream.open("Graphics/sfml-logo-big.png"));
|
||||||
|
|
||||||
SECTION("Successful load")
|
SECTION("Successful load")
|
||||||
{
|
{
|
||||||
CHECK(stream.open("Graphics/sfml-logo-big.png"));
|
|
||||||
REQUIRE(image.loadFromStream(stream));
|
REQUIRE(image.loadFromStream(stream));
|
||||||
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
CHECK(image.getSize() == sf::Vector2u(1001, 304));
|
||||||
CHECK(image.getPixelsPtr() != nullptr);
|
CHECK(image.getPixelsPtr() != nullptr);
|
||||||
CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0));
|
CHECK(image.getPixel({0, 0}) == sf::Color(255, 255, 255, 0));
|
||||||
CHECK(image.getPixel({200, 150}) == sf::Color(144, 208, 62));
|
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()")
|
SECTION("saveToFile()")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user