Creating a png-file out of a SDL surface

Feb 26, 2009 at 8:50pm
How to create a png-file out of a SDL surface? Searching this site and google didn't gave me an answer.
Feb 26, 2009 at 9:22pm
SDL has a function that prepares an in-memory BMP (there's also a macro that calls this function and saves the BMP). You can pass this to any of the image libraries available. There's libpng, ImageMagick, FreeImage...
Feb 26, 2009 at 9:40pm
Thanks for the reply.

So I'll need an image library. Do you recommend one? And what is this function? If you could show how it is done with a sample-code, or post a link where it's explained, that would be great.
Feb 26, 2009 at 9:56pm
SDL_SaveBMP(screen,filename);
That's an example of the macro. The macro is declared like this:
1
2
#define SDL_SaveBMP(surface, file) \
		SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1) 

In case you don't know, an SDL_RWops abstracts some type of storage. It can linked to a file, a portion of memory, and not-sure-what-else.

I've used FreeImage with good results. It supports many formats and includes some useful algorithms (Lanczos is one of my favorites). It can be a bit difficult to get the hang of at first, but if you follow the examples you'll get it.
Feb 28, 2009 at 1:04pm
Thanks helios.

This more complicated as I hoped it would be :). I thought the file would just contain a serie of numbers (the RGB-values or something like that), and then it would be quit easy to store those values into a file.
I don't know SDL quit wel yet, so I'll concentrate on that for now. I'll come back to this later, and learn about image libraries etc. Thanks for your help!
Feb 28, 2009 at 4:50pm
What you said is true for BMP, but not for PNG. PNG images are compressed using a lossless algorithm (unlike JPEG which are compressed using a lossy algorithm).

Unless you have a very specific requirement for space, I'd recommend you stick you BMPs and SDL_SaveBMP().
Mar 1, 2009 at 4:18pm
Oke, thanks! In that case, I'll stick to BMP for now. I need this to create files I'll use in other SDL applications, and because I'm always using png-images, I asked how to do. But this will work fine for me to.
Topic archived. No new replies allowed.