SDL_SetColorkey on a surface made from a PNG?

I’m trying to get SDL_SetColorkey to work on a surface I made from a .png using
SDL_image.

My code looks like this:

SDL_Surface *flower;
flower=IMG_Load(“flower.png”)
SDL_SetColorKey(flower, SDL_SRCCOLORKEY, SDL_MapRGB(flower->format, 255, 0, 255));

When I do this, my sruface is still shown with the magenta visible. If,
however, I change the image to a bmp, it works fine. This code works without
problems:

SDL_Surface *flower;
flower=SDL_LoadBMP(“flower.bmp”)
SDL_SetColorKey(flower, SDL_SRCCOLORKEY, SDL_MapRGB(flower->format, 255, 0, 255));

What can I do to get it working on my .png?

Thanks for the help.

Does your PNG have an alpha channel? If so, you should probably use
SDL_MapRGBA() instead. (Though you should get the same result with
SDL_MapRGB(), unless your colorkey alpha value is not all ones.)

If there is an alpha channel, is it clean? Some applications (old
versions of GIMP and PhotoShop, among others) have rounding errors
that result in many operations (among other things) “rounding” 255 to
254, which ruins colorkey and pollutes the alpha channel.

If your application screws up and darkens or lightens an image when
you convert it back and forth between indexed color and RGB modes, it
has the kind of bug(s) that would cause this problem.

That said, if both files are generated by the same application, it’s
weird that it works with BMP… Are there any additional steps when
saving as PNG or BMP?

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Sunday 05 December 2004 16.39, James O’Meara wrote:

I’m trying to get SDL_SetColorkey to work on a surface I made from
a .png using SDL_image.

My code looks like this:

SDL_Surface *flower;
flower=IMG_Load(“flower.png”)
SDL_SetColorKey(flower, SDL_SRCCOLORKEY,
SDL_MapRGB(flower->format, 255, 0, 255));

When I do this, my sruface is still shown with the magenta
visible. If, however, I change the image to a bmp, it works fine.
This code works without problems:

SDL_Surface *flower;
flower=SDL_LoadBMP(“flower.bmp”)
SDL_SetColorKey(flower, SDL_SRCCOLORKEY,
SDL_MapRGB(flower->format, 255, 0, 255));

What can I do to get it working on my .png?

David Olofson <david olofson.net> writes:

Does your PNG have an alpha channel? If so, you should probably use
SDL_MapRGBA() instead. (Though you should get the same result with
SDL_MapRGB(), unless your colorkey alpha value is not all ones.)

If there is an alpha channel, is it clean? Some applications (old
versions of GIMP and PhotoShop, among others) have rounding errors
that result in many operations (among other things) “rounding” 255 to
254, which ruins colorkey and pollutes the alpha channel.

If your application screws up and darkens or lightens an image when
you convert it back and forth between indexed color and RGB modes, it
has the kind of bug(s) that would cause this problem.

That said, if both files are generated by the same application, it’s
weird that it works with BMP… Are there any additional steps when
saving as PNG or BMP?

I made both the png and the bmp using Photoshop CS. However, I’m not exactly
sure how to tell if there’s an alpha channel, or if it’s clean. As for the
aditional steps, all I did when making either picture was save the .psd as one
format or another.