I want my game engine to be able to save screenshots in PNG format, but because I have an abstraction layer for handling files I want to create the image in memory before writing it to a file. I managed to do that by allocating a buffer, using SDL_RWFromMem to create an SDL_RWops and then passing that to IMG_SavePNG_RW to create the image.
The problem is that I don’t know beforehand how large the image will be, so I have to allocate the buffer using an arbitrary size and hope that it’s large enough (currently i’m using 4 * width * height bytes which should be enough because of the compression) .
Is there a better way to do this? Are there any assumptions I can make about the final image size so I don’t have to allocate an unnecessarily large buffer hoping that it won’t be too small?