Date: Mon, 02 May 2005 04:23:16 -0500

From: Jonathan Atkins
Subject: Re: [PATCH] *CRITICAL* 8bit direct RGB palette not being created

I think that SDL_AllocFormat should create the palette for all 8bit
surfaces.  And when the RGBAmasks match the normal 3:3:2:0 we need to
apply the old behavior.  If the mask doesn't match that, then we need
to make the right palette assuming the masks are valid (I don't think
we validate any masks for high color surfaces...so we wouldn't here)
Then there's always a palette available for the 8bit surfaces.
This restores the normal behavior and allows for masks to create
palettes automatically for odd masks even, which would be a neato
thing to have in there, as SDL never did this before.
This commit is contained in:
Sam Lantinga 2005-05-16 05:34:58 +00:00
parent aac57c52e5
commit 8a68142a92
2 changed files with 93 additions and 20 deletions

View File

@ -452,6 +452,10 @@ SDL_loblit SDL_CalculateBlit0(SDL_Surface *surface, int blit_index)
{
int which;
if ( surface->format->BitsPerPixel > 1 ) {
/* We don't support sub 8-bit packed pixel modes */
return NULL;
}
if ( surface->map->dst->format->BitsPerPixel < 8 ) {
which = 0;
} else {

View File

@ -112,11 +112,26 @@ SDL_PixelFormat *SDL_AllocFormat(int bpp,
format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
} else { /* Palettized mode */
int i, ncolors = 1;
for ( i = 0; i < bpp; ++i ) {
ncolors *= 2;
}
} else {
/* Palettized formats have no mask info */
format->Rloss = 8;
format->Gloss = 8;
format->Bloss = 8;
format->Aloss = 8;
format->Rshift = 0;
format->Gshift = 0;
format->Bshift = 0;
format->Ashift = 0;
format->Rmask = 0;
format->Gmask = 0;
format->Bmask = 0;
format->Amask = 0;
}
if ( bpp <= 8 ) { /* Palettized mode */
int ncolors = 1<<bpp;
#ifdef DEBUG_PALETTE
fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors);
#endif
format->palette = (SDL_Palette *)malloc(sizeof(SDL_Palette));
if ( format->palette == NULL ) {
SDL_FreeFormat(format);
@ -131,7 +146,75 @@ SDL_PixelFormat *SDL_AllocFormat(int bpp,
SDL_OutOfMemory();
return(NULL);
}
if ( ncolors == 2 ) {
if ( Rmask || Bmask || Gmask ) {
/* create palette according to masks */
int i;
int Rm=0,Gm=0,Bm=0;
int Rw=0,Gw=0,Bw=0;
#ifdef ENABLE_PALETTE_ALPHA
int Am=0,Aw=0;
#endif
if(Rmask)
{
Rw=8-format->Rloss;
for(i=format->Rloss;i>0;i-=Rw)
Rm|=1<<i;
}
#ifdef DEBUG_PALETTE
fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm);
#endif
if(Gmask)
{
Gw=8-format->Gloss;
for(i=format->Gloss;i>0;i-=Gw)
Gm|=1<<i;
}
#ifdef DEBUG_PALETTE
fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm);
#endif
if(Bmask)
{
Bw=8-format->Bloss;
for(i=format->Bloss;i>0;i-=Bw)
Bm|=1<<i;
}
#ifdef DEBUG_PALETTE
fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm);
#endif
#ifdef ENABLE_PALETTE_ALPHA
if(Amask)
{
Aw=8-format->Aloss;
for(i=format->Aloss;i>0;i-=Aw)
Am|=1<<i;
}
# ifdef DEBUG_PALETTE
fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am);
# endif
#endif
for(i=0; i < ncolors; ++i) {
int r,g,b;
r=(i&Rmask)>>format->Rshift;
r=(r<<format->Rloss)|((r*Rm)>>Rw);
format->palette->colors[i].r=r;
g=(i&Gmask)>>format->Gshift;
g=(g<<format->Gloss)|((g*Gm)>>Gw);
format->palette->colors[i].g=g;
b=(i&Bmask)>>format->Bshift;
b=(b<<format->Bloss)|((b*Bm)>>Bw);
format->palette->colors[i].b=b;
#ifdef ENABLE_PALETTE_ALPHA
a=(i&Amask)>>format->Ashift;
a=(a<<format->Aloss)|((a*Am)>>Aw);
format->palette->colors[i].unused=a;
#else
format->palette->colors[i].unused=0;
#endif
}
} else if ( ncolors == 2 ) {
/* Create a black and white bitmap palette */
format->palette->colors[0].r = 0xFF;
format->palette->colors[0].g = 0xFF;
@ -144,20 +227,6 @@ SDL_PixelFormat *SDL_AllocFormat(int bpp,
memset((format->palette)->colors, 0,
(format->palette)->ncolors*sizeof(SDL_Color));
}
/* Palettized formats have no mask info */
format->Rloss = 8;
format->Gloss = 8;
format->Bloss = 8;
format->Aloss = 8;
format->Rshift = 0;
format->Gshift = 0;
format->Bshift = 0;
format->Ashift = 0;
format->Rmask = 0;
format->Gmask = 0;
format->Bmask = 0;
format->Amask = 0;
}
return(format);
}