From 3bea8a9c40f8cf5dbb2fa0a2bf33b47faac79d6a Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 16 May 2005 05:34:58 +0000 Subject: [PATCH] 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. --- src/video/SDL_blit_0.c | 4 ++ src/video/SDL_pixels.c | 109 +++++++++++++++++++++++++++++++++-------- 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/src/video/SDL_blit_0.c b/src/video/SDL_blit_0.c index d46d745e3..85e0ee15a 100644 --- a/src/video/SDL_blit_0.c +++ b/src/video/SDL_blit_0.c @@ -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 { diff --git a/src/video/SDL_pixels.c b/src/video/SDL_pixels.c index 1eca1dbc9..0294de474 100644 --- a/src/video/SDL_pixels.c +++ b/src/video/SDL_pixels.c @@ -112,11 +112,26 @@ SDL_PixelFormat *SDL_AllocFormat(int bpp, format->Rmask = ((0xFF>>format->Rloss)<Rshift); format->Gmask = ((0xFF>>format->Gloss)<Gshift); format->Bmask = ((0xFF>>format->Bloss)<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<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<Gloss; + for(i=format->Gloss;i>0;i-=Gw) + Gm|=1<Bloss; + for(i=format->Bloss;i>0;i-=Bw) + Bm|=1<Aloss; + for(i=format->Aloss;i>0;i-=Aw) + Am|=1<>format->Rshift; + r=(r<Rloss)|((r*Rm)>>Rw); + format->palette->colors[i].r=r; + + g=(i&Gmask)>>format->Gshift; + g=(g<Gloss)|((g*Gm)>>Gw); + format->palette->colors[i].g=g; + + b=(i&Bmask)>>format->Bshift; + b=(b<Bloss)|((b*Bm)>>Bw); + format->palette->colors[i].b=b; + +#ifdef ENABLE_PALETTE_ALPHA + a=(i&Amask)>>format->Ashift; + a=(a<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); }