Since saving to memory is not the typical game use case, it is probably occurring mostly when interfacing with a different rendering engine or image processing systems. Those usually have a rather low-level API based on char*/size_t pairs. Even if std::vector supports this format, for any scenario where ownership is transferred to a third-party system, the std::vector leads to a useless temporary copy.
Usually, these other systems have methods to load RAW RGBA data, which makes
getPixelsPtr and
width * height * 4 perfect for this use case. The whole idea of
saveToMemory is to encode this data in a specified format. Otherwise we could just copy the internal pixel data.
std::vector is convenient, but I would add it only once we gather some knowledge about common use cases (e.g. which element type).
Two particular use cases I've ran into were the following:
1-) Saving a project file in json. One of the data types stored were base64 encoded images. Encoding directly from
getPixelsPtr was particularly easy, however the final size transformed a 78KB JPEG into 5,6MB RAW RGBA. That's where
saveToMemory comes in, encoding the image without accessing the filesystem (yet). By using
std::vector, there is a minimal code difference here.
2-) A server application which modifies files and returns them through the network. Without adding additional libraries,
saveToMemory could remove the need to write to the filesystem as a middleman, reducing SSD wear and API response times.
That said, a
saveToStream really does sound like the better option, so we could propose something like:
bool sf::Image::saveToStream(sf::OutputStream& stream, const std::string& format = "png");