diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 053b1f3c2..eb4a261bc 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -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; - int channels = 0; - if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &file, &width, &height, &channels, STBI_rgb_alpha))) + sf::Vector2i imageSize; + int channels = 0; + 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(data); if (const auto ptr = StbPtr( - stbi_load_from_memory(buffer, static_cast(size), &width, &height, &channels, STBI_rgb_alpha))) + stbi_load_from_memory(buffer, static_cast(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; - int channels = 0; - if (const auto ptr = StbPtr(stbi_load_from_callbacks(&callbacks, &stream, &width, &height, &channels, STBI_rgb_alpha))) + Vector2i imageSize; + int channels = 0; + 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; }