Date: Mon, 02 May 2005 04:23:16 -0500
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 May 2005 05:34:58 +0000
changeset 1057e9d23bb80140
parent 1056 d33c095f7001
child 1058 e6c91fd1911e
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
src/video/SDL_pixels.c
     1.1 --- a/src/video/SDL_blit_0.c	Mon May 16 04:45:07 2005 +0000
     1.2 +++ b/src/video/SDL_blit_0.c	Mon May 16 05:34:58 2005 +0000
     1.3 @@ -452,6 +452,10 @@
     1.4  {
     1.5  	int which;
     1.6  
     1.7 +	if ( surface->format->BitsPerPixel > 1 ) {
     1.8 +		/* We don't support sub 8-bit packed pixel modes */
     1.9 +		return NULL;
    1.10 +	}
    1.11  	if ( surface->map->dst->format->BitsPerPixel < 8 ) {
    1.12  		which = 0;
    1.13  	} else {
     2.1 --- a/src/video/SDL_pixels.c	Mon May 16 04:45:07 2005 +0000
     2.2 +++ b/src/video/SDL_pixels.c	Mon May 16 05:34:58 2005 +0000
     2.3 @@ -112,11 +112,26 @@
     2.4  		format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
     2.5  		format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
     2.6  		format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
     2.7 -	} else {			/* Palettized mode */
     2.8 -		int i, ncolors = 1;
     2.9 -		for ( i = 0; i < bpp; ++i ) {
    2.10 -			ncolors *= 2;
    2.11 -		}
    2.12 +	} else {
    2.13 +		/* Palettized formats have no mask info */
    2.14 +		format->Rloss = 8;
    2.15 +		format->Gloss = 8;
    2.16 +		format->Bloss = 8;
    2.17 +		format->Aloss = 8;
    2.18 +		format->Rshift = 0;
    2.19 +		format->Gshift = 0;
    2.20 +		format->Bshift = 0;
    2.21 +		format->Ashift = 0;
    2.22 +		format->Rmask = 0;
    2.23 +		format->Gmask = 0;
    2.24 +		format->Bmask = 0;
    2.25 +		format->Amask = 0;
    2.26 +	}
    2.27 +	if ( bpp <= 8 ) {			/* Palettized mode */
    2.28 +		int ncolors = 1<<bpp;
    2.29 +#ifdef DEBUG_PALETTE
    2.30 +		fprintf(stderr,"bpp=%d ncolors=%d\n",bpp,ncolors);
    2.31 +#endif
    2.32  		format->palette = (SDL_Palette *)malloc(sizeof(SDL_Palette));
    2.33  		if ( format->palette == NULL ) {
    2.34  			SDL_FreeFormat(format);
    2.35 @@ -131,7 +146,75 @@
    2.36  			SDL_OutOfMemory();
    2.37  			return(NULL);
    2.38  		}
    2.39 -		if ( ncolors == 2 ) {
    2.40 +		if ( Rmask || Bmask || Gmask ) {
    2.41 +			/* create palette according to masks */
    2.42 +			int i;
    2.43 +			int Rm=0,Gm=0,Bm=0;
    2.44 +			int Rw=0,Gw=0,Bw=0;
    2.45 +#ifdef ENABLE_PALETTE_ALPHA
    2.46 +			int Am=0,Aw=0;
    2.47 +#endif
    2.48 +			if(Rmask)
    2.49 +			{
    2.50 +				Rw=8-format->Rloss;
    2.51 +				for(i=format->Rloss;i>0;i-=Rw)
    2.52 +					Rm|=1<<i;
    2.53 +			}
    2.54 +#ifdef DEBUG_PALETTE
    2.55 +			fprintf(stderr,"Rw=%d Rm=0x%02X\n",Rw,Rm);
    2.56 +#endif
    2.57 +			if(Gmask)
    2.58 +			{
    2.59 +				Gw=8-format->Gloss;
    2.60 +				for(i=format->Gloss;i>0;i-=Gw)
    2.61 +					Gm|=1<<i;
    2.62 +			}
    2.63 +#ifdef DEBUG_PALETTE
    2.64 +			fprintf(stderr,"Gw=%d Gm=0x%02X\n",Gw,Gm);
    2.65 +#endif
    2.66 +			if(Bmask)
    2.67 +			{
    2.68 +				Bw=8-format->Bloss;
    2.69 +				for(i=format->Bloss;i>0;i-=Bw)
    2.70 +					Bm|=1<<i;
    2.71 +			}
    2.72 +#ifdef DEBUG_PALETTE
    2.73 +			fprintf(stderr,"Bw=%d Bm=0x%02X\n",Bw,Bm);
    2.74 +#endif
    2.75 +#ifdef ENABLE_PALETTE_ALPHA
    2.76 +			if(Amask)
    2.77 +			{
    2.78 +				Aw=8-format->Aloss;
    2.79 +				for(i=format->Aloss;i>0;i-=Aw)
    2.80 +					Am|=1<<i;
    2.81 +			}
    2.82 +# ifdef DEBUG_PALETTE
    2.83 +			fprintf(stderr,"Aw=%d Am=0x%02X\n",Aw,Am);
    2.84 +# endif
    2.85 +#endif
    2.86 +			for(i=0; i < ncolors; ++i) {
    2.87 +				int r,g,b;
    2.88 +				r=(i&Rmask)>>format->Rshift;
    2.89 +				r=(r<<format->Rloss)|((r*Rm)>>Rw);
    2.90 +				format->palette->colors[i].r=r;
    2.91 +
    2.92 +				g=(i&Gmask)>>format->Gshift;
    2.93 +				g=(g<<format->Gloss)|((g*Gm)>>Gw);
    2.94 +				format->palette->colors[i].g=g;
    2.95 +
    2.96 +				b=(i&Bmask)>>format->Bshift;
    2.97 +				b=(b<<format->Bloss)|((b*Bm)>>Bw);
    2.98 +				format->palette->colors[i].b=b;
    2.99 +
   2.100 +#ifdef ENABLE_PALETTE_ALPHA
   2.101 +				a=(i&Amask)>>format->Ashift;
   2.102 +				a=(a<<format->Aloss)|((a*Am)>>Aw);
   2.103 +				format->palette->colors[i].unused=a;
   2.104 +#else
   2.105 +				format->palette->colors[i].unused=0;
   2.106 +#endif
   2.107 +			}
   2.108 +		} else if ( ncolors == 2 ) {
   2.109  			/* Create a black and white bitmap palette */
   2.110  			format->palette->colors[0].r = 0xFF;
   2.111  			format->palette->colors[0].g = 0xFF;
   2.112 @@ -144,20 +227,6 @@
   2.113  			memset((format->palette)->colors, 0,
   2.114  				(format->palette)->ncolors*sizeof(SDL_Color));
   2.115  		}
   2.116 -
   2.117 -		/* Palettized formats have no mask info */
   2.118 -		format->Rloss = 8;
   2.119 -		format->Gloss = 8;
   2.120 -		format->Bloss = 8;
   2.121 -		format->Aloss = 8;
   2.122 -		format->Rshift = 0;
   2.123 -		format->Gshift = 0;
   2.124 -		format->Bshift = 0;
   2.125 -		format->Ashift = 0;
   2.126 -		format->Rmask = 0;
   2.127 -		format->Gmask = 0;
   2.128 -		format->Bmask = 0;
   2.129 -		format->Amask = 0;
   2.130  	}
   2.131  	return(format);
   2.132  }