# SDL_Image Colorkeying

## 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 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 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 on other sites
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.

##### Share on other sites
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)?

##### 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 KylotanOn 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 ZadrraSSo, 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 formatSDL_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 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 on other sites
Quote:
 Original post by AntiGuyAm 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 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 on other sites

[quote]
// convert the image into the current display formatSDL_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 formatSDL_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 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

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627678
• Total Posts
2978605

• 12
• 12
• 10
• 12
• 22