mirror of
https://github.com/SFML/SFML.git
synced 2025-01-31 13:45:13 +08:00
Reduce code duplication
This commit is contained in:
parent
9132655a3d
commit
7b0d72966b
@ -244,17 +244,12 @@ bool Image::loadFromFile(const std::filesystem::path& filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load the image and get a pointer to the pixels in memory
|
// Load the image and get a pointer to the pixels in memory
|
||||||
int width = 0;
|
sf::Vector2i imageSize;
|
||||||
int height = 0;
|
int channels = 0;
|
||||||
int channels = 0;
|
if (const auto ptr = StbPtr(
|
||||||
if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &file, &width, &height, &channels, STBI_rgb_alpha)))
|
stbi_load_from_callbacks(&callbacks, &file, &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha)))
|
||||||
{
|
{
|
||||||
// Assign the image properties
|
resize(Vector2u(imageSize), ptr.get());
|
||||||
m_size = Vector2u(Vector2i(width, height));
|
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
|
||||||
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,19 +271,13 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
|
|||||||
m_pixels.clear();
|
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
|
||||||
int width = 0;
|
Vector2i imageSize;
|
||||||
int height = 0;
|
|
||||||
int channels = 0;
|
int channels = 0;
|
||||||
const auto* buffer = static_cast<const unsigned char*>(data);
|
const auto* buffer = static_cast<const unsigned char*>(data);
|
||||||
if (const auto ptr = StbPtr(
|
if (const auto ptr = StbPtr(
|
||||||
stbi_load_from_memory(buffer, static_cast<int>(size), &width, &height, &channels, STBI_rgb_alpha)))
|
stbi_load_from_memory(buffer, static_cast<int>(size), &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha)))
|
||||||
{
|
{
|
||||||
// Assign the image properties
|
resize(Vector2u(imageSize), ptr.get());
|
||||||
m_size = Vector2u(Vector2i(width, height));
|
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
|
||||||
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -323,17 +312,12 @@ bool Image::loadFromStream(InputStream& stream)
|
|||||||
callbacks.eof = eof;
|
callbacks.eof = eof;
|
||||||
|
|
||||||
// Load the image and get a pointer to the pixels in memory
|
// Load the image and get a pointer to the pixels in memory
|
||||||
int width = 0;
|
Vector2i imageSize;
|
||||||
int height = 0;
|
int channels = 0;
|
||||||
int channels = 0;
|
if (const auto ptr = StbPtr(
|
||||||
if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha)))
|
stbi_load_from_callbacks(&callbacks, &stream, &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha)))
|
||||||
{
|
{
|
||||||
// Assign the image properties
|
resize(Vector2u(imageSize), ptr.get());
|
||||||
m_size = Vector2u(Vector2i(width, height));
|
|
||||||
|
|
||||||
// Copy the loaded pixels to the pixel buffer
|
|
||||||
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user