SDL_image save image to file

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.

MasonFrom: Daniel Gibson
To: sdl at lists.libsdl.org
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: 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
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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?On Tue, Jan 26, 2016 at 5:28 AM, Mason Wheeler wrote:

./lxnt

You can try the untested indexed PNG save/load here:

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