Blitting of 8 bit surfaces and palettes

Hi!

I have a question about what happens to the color palettes when blitting
an 8 bit surface. When I make some changes an the palette right after
loading the image, before the first blit, the changes work as expected.
But after blitting the surface onto an other, the changes have no effect
on further blits anymore.

Example:

SDL_Surface* suface = loadImage("…");
setColor( surface, 0, 255, 0, 0);
SDL_BlitSurface( surface, NULL, output, NULL); //output looks as expected.

setColor( surface, 0, 0, 255, 0 );
SDL_BlitSurface( surface, NULL, output, NULL); //here output looks
exactly like after first blit. The palette change has no effect.

setColor is defined like this:
void setColor( SDL_Surface* surface, unsigned char nr, unsigned char r,
unsigned char g, unsigned char b )
{
surface->format->palette->colors[nr].r = r;
surface->format->palette->colors[nr].g = g;
surface->format->palette->colors[nr].b = b;
}

Has anyone an idea who to change this behavior?

Thanks in advance!
Christian

Example:

SDL_Surface* suface = loadImage("…");
setColor( surface, 0, 255, 0, 0);
SDL_BlitSurface( surface, NULL, output, NULL); //output looks as expected.

setColor( surface, 0, 0, 255, 0 );
SDL_BlitSurface( surface, NULL, output, NULL); //here output looks exactly
like after first blit. The palette change has no effect.

setColor is defined like this:
void setColor( SDL_Surface* surface, unsigned char nr, unsigned char r,
unsigned char g, unsigned char b )
{
surface->format->palette->colors[nr].r = r;
surface->format->palette->colors[nr].g = g;
surface->format->palette->colors[nr].b = b;
}

Has anyone an idea who to change this behavior?

I have never used a 256 color palette with SDL. However, your example
changes the rgb values of the source image prior to blitting to the display,
which has its own palette (I am guessing), and that would be the one you
need to change. You see, every pixel value acts as an index into the
palette, and if you are not changing the display’s palette, the color-index
remains the same.

Hope this helps.

Cheers,
PeterFrom: tvgenial@web.de (Christian Meyer)

SDL creates a mapping on the first blit involving paletted surface, in
order to match the colors. That mapping is updated if you setup the
palette by using SDL_SetPalette as intended, and not by hacking into
the struct (have look at SDL_video.h and notice that the "format"
member of SDL_Surface is read-only).

StephaneOn Thu, Aug 21, 2008 at 6:07 PM, Christian Meyer wrote:

Hi!

I have a question about what happens to the color palettes when blitting an
8 bit surface. When I make some changes an the palette right after loading
the image, before the first blit, the changes work as expected. But after
blitting the surface onto an other, the changes have no effect on further
blits anymore.

Example:

SDL_Surface* suface = loadImage("…");
setColor( surface, 0, 255, 0, 0);
SDL_BlitSurface( surface, NULL, output, NULL); //output looks as expected.

setColor( surface, 0, 0, 255, 0 );
SDL_BlitSurface( surface, NULL, output, NULL); //here output looks exactly
like after first blit. The palette change has no effect.

setColor is defined like this:
void setColor( SDL_Surface* surface, unsigned char nr, unsigned char r,
unsigned char g, unsigned char b )
{
surface->format->palette->colors[nr].r = r;
surface->format->palette->colors[nr].g = g;
surface->format->palette->colors[nr].b = b;
}

Has anyone an idea who to change this behavior?

Stephane Marchesin schrieb:

SDL creates a mapping on the first blit involving paletted surface, in
order to match the colors. That mapping is updated if you setup the
palette by using SDL_SetPalette as intended, and not by hacking into
the struct (have look at SDL_video.h and notice that the "format"
member of SDL_Surface is read-only).

Stephane

Yes, of course, thats the solution. Thanks a lot!

Christian