[Sdl] Converting a 24 bit RGB surface with color key to 32 bit RGBA surface

Hi, I’m trying to change a 24 bit surface with a color key to a 32 bit
surface with alpha channel. Because there is the color key, I want all
pixels in the new surface with the color matching the color key has an
alpha channel of 0 (or transparent). I’ve tried making a new 32 bit
surface with alpha channel 0 and blit the 24 bit surface to it but it
didn’t work. In fact all the colors are wrong in the new surface. How do I
do that? Is there anything wrong with my code? Thanks in advance.

BTW, this is the code:

if ( (surface = SDL_LoadBMP(“test.bmp”)) )
{
char color=surface->flags&SDL_SRCCOLORKEY;

SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format,

0, 0, 0));

/* From SDL Documentation.
Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,
as expected by OpenGL for textures */

SDL_Surface *tempSur;
Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */

#if not SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

tempSur = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h,

32,rmask, gmask, bmask, amask);

if(tempSur == NULL) {
fprintf(stderr, "CreateRGBSurface failed: %s\n",   SDL_GetError());
exit(1);
}


SDL_SetAlpha(tempSur, SDL_SRCALPHA, 0);
SDL_BlitSurface(surface,NULL,tempSur,NULL);

SDL_FreeSurface( surface );

surface=tempSur;

}
else {
fprintf(stderr, “Cannot load BMP: %s\n”, SDL_GetError());
exit(1);
}

Fare thee well,
Bawenang R. P. P.----------------
ERROR: Brain not found. Please insert a new brain!

?Do nothing which is of no use.? - Miyamoto Musashi.

“I live for my dream. And my dream is to live my life to the fullest.”

Try using other color masks - in particular, the ones of the surface
returned by SDL_SetVideoMode. For example, if your original surface was
called MainSurface, it might look like this:

rmask = MainSurface->format->Rmask;
gmask = MainSurface->format->Gmask;
bmask = MainSurface->format->Bmask;
amask = MainSurface->format->Amask;

Of course, that’s provided that you called SDL_SetVideoMode with 32-bit
depth (which I’m assuming you did, since your comments said you’re using
OpenGL). If you’re calling SDL_SetVideoMode with 24 bits (or less), maybe
take a look at the SDL_ConvertSurface method?On 7/23/07, benang at cs.its.ac.id wrote:

Hi, I’m trying to change a 24 bit surface with a color key to a 32 bit
surface with alpha channel. Because there is the color key, I want all
pixels in the new surface with the color matching the color key has an
alpha channel of 0 (or transparent). I’ve tried making a new 32 bit
surface with alpha channel 0 and blit the 24 bit surface to it but it
didn’t work. In fact all the colors are wrong in the new surface. How do I
do that? Is there anything wrong with my code? Thanks in advance.

BTW, this is the code:

if ( (surface = SDL_LoadBMP(“test.bmp”)) )
{
char color=surface->flags&SDL_SRCCOLORKEY;

SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format,

0, 0, 0));

/* From SDL Documentation.
Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,
as expected by OpenGL for textures */

SDL_Surface *tempSur;
Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */

#if not SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

tempSur = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h,

32,rmask, gmask, bmask, amask);

if(tempSur == NULL) {
    fprintf(stderr, "CreateRGBSurface failed: %s\n",

SDL_GetError());
exit(1);
}

SDL_SetAlpha(tempSur, SDL_SRCALPHA, 0);
SDL_BlitSurface(surface,NULL,tempSur,NULL);

SDL_FreeSurface( surface );

surface=tempSur;

}
else {
fprintf(stderr, “Cannot load BMP: %s\n”, SDL_GetError());
exit(1);
}

Fare thee well,
Bawenang R. P. P.


ERROR: Brain not found. Please insert a new brain!

“Do nothing which is of no use.” - Miyamoto Musashi.

“I live for my dream. And my dream is to live my life to the fullest.”


Sdl mailing list
Sdl at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


“If you see somebody who you don’t know getting into a crop duster that
doesn’t belong to you, report them.” – President George W. Bush

Sorry it took a while. I’m currently using the easy way out. I’m now using
a PNG with alpha channel of 0 for the transparent pixel rather than
colorkeying the black color of a BMP. And I use SDL_DiplayFormatAlpha() to
convert it to 32 bit RGBA. Thanks a lot though.

James Buchwald said:

Try using other color masks - in particular, the ones of the surface
returned by SDL_SetVideoMode. For example, if your original surface was
called MainSurface, it might look like this:

rmask = MainSurface->format->Rmask;
gmask = MainSurface->format->Gmask;
bmask = MainSurface->format->Bmask;
amask = MainSurface->format->Amask;

Of course, that’s provided that you called SDL_SetVideoMode with 32-bit
depth (which I’m assuming you did, since your comments said you’re using
OpenGL). If you’re calling SDL_SetVideoMode with 24 bits (or less), maybe
take a look at the SDL_ConvertSurface method?

Hi, I’m trying to change a 24 bit surface with a color key to a 32 bit
surface with alpha channel. Because there is the color key, I want all
pixels in the new surface with the color matching the color key has an
alpha channel of 0 (or transparent). I’ve tried making a new 32 bit
surface with alpha channel 0 and blit the 24 bit surface to it but it
didn’t work. In fact all the colors are wrong in the new surface. How do
I
do that? Is there anything wrong with my code? Thanks in advance.

BTW, this is the code:

if ( (surface = SDL_LoadBMP(“test.bmp”)) )
{
char color=surface->flags&SDL_SRCCOLORKEY;

SDL_SetColorKey(surface, SDL_SRCCOLORKEY,

SDL_MapRGB(surface->format,
0, 0, 0));

/* From SDL Documentation.
Create a 32-bit surface with the bytes of each pixel in R,G,B,A
order,
as expected by OpenGL for textures */

SDL_Surface *tempSur;
Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must
depend
on the endianness (byte order) of the machine */

#if not SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

tempSur = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w,

surface->h,
32,rmask, gmask, bmask, amask);

if(tempSur == NULL) {
    fprintf(stderr, "CreateRGBSurface failed: %s\n",

SDL_GetError());
exit(1);
}

SDL_SetAlpha(tempSur, SDL_SRCALPHA, 0);
SDL_BlitSurface(surface,NULL,tempSur,NULL);

SDL_FreeSurface( surface );

surface=tempSur;

}
else {
fprintf(stderr, “Cannot load BMP: %s\n”, SDL_GetError());
exit(1);
}

Fare thee well,
Bawenang R. P. P.


ERROR: Brain not found. Please insert a new brain!

“Do nothing which is of no use.” - Miyamoto Musashi.

“I live for my dream. And my dream is to live my life to the fullest.”


Sdl mailing list
Sdl at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


“If you see somebody who you don’t know getting into a crop duster that
doesn’t belong to you, report them.” – President George W. Bush


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Fare thee well,
Bawenang R. P. P.> On 7/23/07, @benang_at_cs.its.ac <@benang_at_cs.its.ac> wrote:


ERROR: Brain not found. Please insert a new brain!

?Do nothing which is of no use.? - Miyamoto Musashi.

“I live for my dream. And my dream is to live my life to the fullest.”