From 725811baa91e48802f0a789975875f05cad1e2b1 Mon Sep 17 00:00:00 2001 From: kimci86 Date: Thu, 7 Sep 2023 21:50:03 +0200 Subject: [PATCH] Implement `sf::Image::saveToMemory` https://github.com/SFML/SFML/pull/1669 Co-authored-by: Chris Thrasher --- include/SFML/Graphics/Image.h | 22 ++++++++++++++++++++++ src/SFML/Graphics/Image.cpp | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/SFML/Graphics/Image.h b/include/SFML/Graphics/Image.h index 7a89f6ce..332d83eb 100644 --- a/include/SFML/Graphics/Image.h +++ b/include/SFML/Graphics/Image.h @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -156,9 +157,30 @@ CSFML_GRAPHICS_API void sfImage_destroy(sfImage* image); /// /// \return sfTrue if saving was successful /// +/// \see sfImage_saveToMemory +/// //////////////////////////////////////////////////////////// CSFML_GRAPHICS_API sfBool sfImage_saveToFile(const sfImage* image, const char* filename); +//////////////////////////////////////////////////////////// +/// \brief Save the image to a buffer in memory +/// +/// The format of the image must be specified. +/// The supported image formats are bmp, png, tga and jpg. +/// This function fails if the image is empty, or if +/// the format was invalid. +/// +/// \param image Image object +/// \param output Buffer to fill with encoded data +/// \param format Encoding format to use +/// +/// \return sfTrue if saving was successful +/// +/// \see sfImage_saveToFile +/// +//////////////////////////////////////////////////////////// +CSFML_GRAPHICS_API sfBool sfImage_saveToMemory(const sfImage* image, sfBuffer* output, const char* format); + //////////////////////////////////////////////////////////// /// \brief Return the size of an image /// diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index add3682c..490f94a7 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -27,9 +27,11 @@ //////////////////////////////////////////////////////////// #include #include +#include #include #include + //////////////////////////////////////////////////////////// sfImage* sfImage_create(unsigned int width, unsigned int height) { @@ -131,6 +133,14 @@ sfBool sfImage_saveToFile(const sfImage* image, const char* filename) } +//////////////////////////////////////////////////////////// +sfBool sfImage_saveToMemory(const sfImage* image, sfBuffer* output, const char* format) +{ + CSFML_CHECK_RETURN(output, sfFalse); + CSFML_CALL_RETURN(image, saveToMemory(output->buffer, format), sfFalse); +} + + //////////////////////////////////////////////////////////// void sfImage_createMaskFromColor(sfImage* image, sfColor colorKey, sfUint8 alpha) {