SDL_Image Colorkeying

Started by
9 comments, last by Gaiiden 19 years, 6 months ago
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?
Advertisement
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

Drew Sikora
Executive Producer
GameDev.net

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...)
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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.
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)?
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 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

Drew Sikora
Executive Producer
GameDev.net

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.
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.

Drew Sikora
Executive Producer
GameDev.net

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.
enum Bool { True, False, FileNotFound };

// 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);


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);


This topic is closed to new replies.

Advertisement