The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_image save image to file
dandago


Joined: 30 Aug 2013
Posts: 34
Am I missing something, or does SDL_image not provide any facility to save an image to file?
AntTheAlchemist


Joined: 13 Feb 2015
Posts: 60
Location: UK
If you get stuck for another solution, LodePNG has an image save function. It's free and once you link the LodePNG source code into your project, it works across all the SDL platforms. It has a very small payload, too. Consider using it to load all your images, too (scrap SDL_image).
dandago


Joined: 30 Aug 2013
Posts: 34
Many thanks for the suggestion. I will check it out.
SDL_image save image to file
Daniel Gibson
Guest

On 01/13/2016 06:16 PM, dandago wrote:
Quote:
Am I missing something, or does SDL_image not provide any facility to
save an image to file?


No idea why this is not documented, but

/* Individual saving functions */
extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char
*file);
extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface,
SDL_RWops *dst, int freedst);

See: https://hg.libsdl.org/SDL_image/file/9a970a20248b/SDL_image.h#l132

(And in SDL itself there's SDL_SaveBMP())

Cheers,
Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL_image save image to file
Mason Wheeler
Guest

IMG_SavePNG is badly broken, unfortunately. It will always convert your surface to a 32-bit image if it's not one already, and if what you have is a paletted image with a colorkey, it doesn't handle the alpha correctly. Since 8-bit images are very common for PNGs, this is a very serious problem. Sadly, I raised the issue on here a few months ago and no one replied.



Mason



From: Daniel Gibson
To:
Sent: Thursday, January 14, 2016 12:44 PM
Subject: Re: [SDL] SDL_image save image to file


On 01/13/2016 06:16 PM, dandago wrote:> Am I missing something, or does SDL_image not provide any facility to> save an image to file?>No idea why this is not documented, but/* Individual saving functions */extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file);extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);See:http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL_image save image to file
Alexander Sabourenkov
Guest

On Tue, Jan 26, 2016 at 5:28 AM, Mason Wheeler wrote:
Quote:
IMG_SavePNG is badly broken, unfortunately.  It will always convert your surface to a 32-bit image if it's not one already, and if what you have is a paletted image with a colorkey, it doesn't handle the alpha correctly.  Since 8-bit images are very common for PNGs, this is a very serious problem.  Sadly, I raised the issue on here a few months ago and no one replied.





Well, you see, PNG format doesn't support colorkeyed palletted images. It only supports colorkey transparency for truecolor or grayscale images.


PNG supports optional transparency for palletted images in the form of an array of alpha values for each palette entry. As you can now see, while colorkeyed paletted SDL_Surface can be saved in that format, loading it presents a problem - if such PNG image contains something other than exacly one fully transparent entry in this transparency table, while all other entries are fully opaque, data loss will occur. That is why most PNG loaders upconvert palletted PNG images to RGBA8888 or something like it.


Are you only interested in saving images, or the requirement to get exactly same pixel format on load is in fact there, just not mentioned?




-- 


./lxnt
SDL_image save image to file
Alexander Sabourenkov
Guest

You can try the untested indexed PNG save/load here: https://github.com/lxnt/SDL_pnglite/tree/indexed-save


It should save colorkeyed paletted SDL_Surface as a paletted PNG with only that color set to transparent (all others opaque).

It should load such file to a paletted surface, and set the colorkey the right way.


Not tested in any way. Will appeciate test-cases, images, SDL_Surface dumps, etc. This lib is in dire need of tests.


Once it's tested and known to work I'm sure porting the support to SDL_image won't be a problem.


--


./lxnt