Sign in to follow this  

SDL_Image Colorkeying

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Color Keying works fine with BMP files but when I change to any other file type, it doesn't work. However, it does work with black and white. Am I doing something wrong or does SDL_image not support Color Keying?

Share this post


Link to post
Share on other sites
do you mean it doesn't work at all or it doesn't work as well as BMP? BMP files store data for every single pixel in the image. JPG and GIF files, however, store only general pixel information and then extrapolate that to create the image (it's compressed, in other words). Because of this, if you see a black background in a JPG, not every single pixel is going to equal (0,0,0). SO you'll end up with spotty transparency. If you want transparent images with small file sizes, use PNGs since they have an alpha layer

Share this post


Link to post
Share on other sites
What are these other file types? Depending on how these types are stored, the colors in the file may not be the colors you used to draw them originally. JPEG is a prime example of that.

(Gaiiden: GIF compression isn't lossy, so it won't actually cause this problem...)

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
(Gaiiden: GIF compression isn't lossy, so it won't actually cause this problem...)

Whoops :P
Quote:
Original post by Kylotan
On the other hand the GIF file doesn't store colours but palette entries, so again the value you pass it may not correspond to the exact value in the surface data.

Aha. I knew something was afoot.
Quote:
Original post by ZadrraS
So, with .png, do i have to set transperency and save the image or do the .png's work the same as .bmp's(and i should set their transperency in game)?

You should set their tranparency in the image. When you load images into SDL, use the following code

// convert the image into the current display format
SDL_Surface* m_pImage = IMG_Load(cImageFile);
if (m_pImage->format->Amask)
m_pImage = SDL_DisplayFormatAlpha(m_pImage);
else
m_pImage = SDL_DisplayFormat(m_pImage);


This way if you load a PNG, it will detect the alpha layer and format to an alpha surface so you don't lose that transparency value

Share this post


Link to post
Share on other sites
Sorry about the delay, I forgot about the post. Anyway, I mean't it won't work at all. No matter what color (other than black and white) I set the key to, transparency won't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by AntiGuy
Am I doing something wrong or does SDL_image not support Color Keying?

SDL_Image has nothing to do with color keying. You have to set the color key for the surface through SDL_SetColorKey(). Lets see some code.

Share this post


Link to post
Share on other sites
Once you get into MIP mapping and filtering (which will happen for rotation of sprites), then your color key may bleed through at the fringe, either as the key color, or as the replacement (typically black).

Instead, author your images using a large fringe of the edge colors, and real alpha, in a format such as TGA or DDS (DXT5) that supports full alpha.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

[quote]

// convert the image into the current display format
SDL_Surface* m_pImage = IMG_Load(cImageFile);
if (m_pImage->format->Amask)
m_pImage = SDL_DisplayFormatAlpha(m_pImage);
else
m_pImage = SDL_DisplayFormat(m_pImage);

[/quote]

btw i believe thats a memory leak...use this one as with the original he loaded a pointer to a surface and then re-assigned it without freeing it first.

// convert the image into the current display format
SDL_Surface* m_pImage;
SDL_Surface* temp = IMG_Load(cImageFile);
if (m_pImage->format->Amask)
m_pImage = SDL_DisplayFormatAlpha(temp);
else
m_pImage = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);



Share this post


Link to post
Share on other sites
Ooohhh... that is a leak! I forgot that function creates and returns a new surface. I've posted this code a few times and no one else has noticed that. Either that or they haven't cared to tell me. Punks. Too bad you're an Anon, and I can't rate you up :P

Share this post


Link to post
Share on other sites

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this