[SDL] Converting an SDL_Surface to BMP format in memory

E. Wing ewmailing at gmail.com
Sat Jun 10 08:31:42 PDT 2006


I just picked up Sam's old clipboard demo "scrap". For copying images
to the clipboard, it seems that SDL_surfaces are converted to BMPs and
then are serialized into char* buffers so they can be passed through
the API.

The demo code does this by calling SDL_SaveBMP on the SDL_Surface and
writes a temporary file. Then the file is read back in directly into a
char* buffer using fread().

I was wondering how to accomplish this without writing to the file
system. I was thinking of using RWops for this like so:

// Contents of the image_surface are converted to BMP and placed in scrap_buffer
SDL_SaveBMP_RW(image_surface, SDL_RWFromMem(scrap_buffer, scraplen), 1);

But I think I have a problem with this because I think I need to make
sure the buffer is large enough to hold the BMP. But I don't know the
size should be.

Assuming this is the right approach, is there a safe way I can
determine the size the buffer needs to be?

I'm thinking along the lines of:

scraplen = image->h * image->pitch * image->format->BytesPerPixel +
max_header_bytes

Does this look reasonable? Can somebody tell me what the magic number
for max_header_bytes should be for SDL_SaveBMP_RW?

Thanks,
Eric




More information about the SDL mailing list