Converting an SDL_Surface to BMP format in memory

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

Hi Erik,

in my Windows programs I use the follwing code:

If the Image is 8 Bit Black and White:

if( bitmapWidth%4)
padding_bytes=4- (bitmapWidth%4); // the width bytes must aways fit
DWORD units
else
padding_bytes=0;

filesize = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD) +
(bitmapWidth+padding_bytes)*bitmapHeight;

If RGB:

if( (bitmapWidth*3)%4)
padding_bytes=4- (bitmapWidth%4); // the width bytes must aways fit
DWORD units
else
padding_bytes=0;

filesize = sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER) +
bitmapHeight*(bitmapWidth*3 + padding_bytes);

Hope this helps.

Chris> ----- Original Message -----

From: ewmailing@gmail.com (Eric Wing)
Newsgroups: gmane.comp.lib.sdl
Sent: Saturday, June 10, 2006 5:31 PM
Subject: Converting an SDL_Surface to BMP format in memory

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