Reduce code duplication

This commit is contained in:
Chris Thrasher 2025-01-26 23:12:05 -07:00
parent 9132655a3d
commit 7b0d72966b

View File

@ -244,17 +244,12 @@ bool Image::loadFromFile(const std::filesystem::path& filename)
}
// Load the image and get a pointer to the pixels in memory
int width = 0;
int height = 0;
sf::Vector2i imageSize;
int channels = 0;
if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &file, &width, &height, &channels, STBI_rgb_alpha)))
if (const auto ptr = StbPtr(
stbi_load_from_callbacks(&callbacks, &file, &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha)))
{
// Assign the image properties
m_size = Vector2u(Vector2i(width, height));
// Copy the loaded pixels to the pixel buffer
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
resize(Vector2u(imageSize), ptr.get());
return true;
}
@ -276,19 +271,13 @@ bool Image::loadFromMemory(const void* data, std::size_t size)
m_pixels.clear();
// Load the image and get a pointer to the pixels in memory
int width = 0;
int height = 0;
Vector2i imageSize;
int channels = 0;
const auto* buffer = static_cast<const unsigned char*>(data);
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
m_size = Vector2u(Vector2i(width, height));
// Copy the loaded pixels to the pixel buffer
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
resize(Vector2u(imageSize), ptr.get());
return true;
}
@ -323,17 +312,12 @@ bool Image::loadFromStream(InputStream& stream)
callbacks.eof = eof;
// Load the image and get a pointer to the pixels in memory
int width = 0;
int height = 0;
Vector2i imageSize;
int channels = 0;
if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha)))
if (const auto ptr = StbPtr(
stbi_load_from_callbacks(&callbacks, &stream, &imageSize.x, &imageSize.y, &channels, STBI_rgb_alpha)))
{
// Assign the image properties
m_size = Vector2u(Vector2i(width, height));
// Copy the loaded pixels to the pixel buffer
m_pixels.assign(ptr.get(), ptr.get() + width * height * 4);
resize(Vector2u(imageSize), ptr.get());
return true;
}