Clarified the code in the pixel format allocation
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2005 19:38:24 +0000
changeset 1027c69697a85412
parent 1026 0f3aa6ab3341
child 1028 5ba65305c954
Clarified the code in the pixel format allocation
Added support for direct color 8-bpp modes.
src/video/SDL_pixels.c
     1.1 --- a/src/video/SDL_pixels.c	Wed Jan 12 06:04:48 2005 +0000
     1.2 +++ b/src/video/SDL_pixels.c	Wed Jan 12 19:38:24 2005 +0000
     1.3 @@ -61,160 +61,45 @@
     1.4  	/* Set up the format */
     1.5  	format->BitsPerPixel = bpp;
     1.6  	format->BytesPerPixel = (bpp+7)/8;
     1.7 -	switch (bpp) {
     1.8 -		case 1:
     1.9 -			/* Create the 2 color black-white palette */
    1.10 -			format->palette = (SDL_Palette *)malloc(
    1.11 -							sizeof(SDL_Palette));
    1.12 -			if ( format->palette == NULL ) {
    1.13 -				SDL_FreeFormat(format);
    1.14 -				SDL_OutOfMemory();
    1.15 -				return(NULL);
    1.16 -			}
    1.17 -			(format->palette)->ncolors = 2;
    1.18 -			(format->palette)->colors = (SDL_Color *)malloc(
    1.19 -				(format->palette)->ncolors*sizeof(SDL_Color));
    1.20 -			if ( (format->palette)->colors == NULL ) {
    1.21 -				SDL_FreeFormat(format);
    1.22 -				SDL_OutOfMemory();
    1.23 -				return(NULL);
    1.24 -			}
    1.25 -			format->palette->colors[0].r = 0xFF;
    1.26 -			format->palette->colors[0].g = 0xFF;
    1.27 -			format->palette->colors[0].b = 0xFF;
    1.28 -			format->palette->colors[1].r = 0x00;
    1.29 -			format->palette->colors[1].g = 0x00;
    1.30 -			format->palette->colors[1].b = 0x00;
    1.31 -			format->Rloss = 8;
    1.32 -			format->Gloss = 8;
    1.33 -			format->Bloss = 8;
    1.34 -			format->Aloss = 8;
    1.35 -			format->Rshift = 0;
    1.36 -			format->Gshift = 0;
    1.37 -			format->Bshift = 0;
    1.38 -			format->Ashift = 0;
    1.39 -			format->Rmask = 0;
    1.40 -			format->Gmask = 0;
    1.41 -			format->Bmask = 0;
    1.42 -			format->Amask = 0;
    1.43 -			break;
    1.44 -
    1.45 -		case 4:
    1.46 -			/* Create the 16 color VGA palette */
    1.47 -			format->palette = (SDL_Palette *)malloc(
    1.48 -							sizeof(SDL_Palette));
    1.49 -			if ( format->palette == NULL ) {
    1.50 -				SDL_FreeFormat(format);
    1.51 -				SDL_OutOfMemory();
    1.52 -				return(NULL);
    1.53 -			}
    1.54 -			(format->palette)->ncolors = 16;
    1.55 -			(format->palette)->colors = (SDL_Color *)malloc(
    1.56 -				(format->palette)->ncolors*sizeof(SDL_Color));
    1.57 -			if ( (format->palette)->colors == NULL ) {
    1.58 -				SDL_FreeFormat(format);
    1.59 -				SDL_OutOfMemory();
    1.60 -				return(NULL);
    1.61 -			}
    1.62 -			/* Punt for now, will this ever be used? */
    1.63 -			memset((format->palette)->colors, 0,
    1.64 -				(format->palette)->ncolors*sizeof(SDL_Color));
    1.65 -
    1.66 -			/* Palettized formats have no mask info */
    1.67 -			format->Rloss = 8;
    1.68 -			format->Gloss = 8;
    1.69 -			format->Bloss = 8;
    1.70 -			format->Aloss = 8;
    1.71 -			format->Rshift = 0;
    1.72 -			format->Gshift = 0;
    1.73 -			format->Bshift = 0;
    1.74 -			format->Ashift = 0;
    1.75 -			format->Rmask = 0;
    1.76 -			format->Gmask = 0;
    1.77 -			format->Bmask = 0;
    1.78 -			format->Amask = 0;
    1.79 -			break;
    1.80 -
    1.81 -		case 8:
    1.82 -			/* Create an empty 256 color palette */
    1.83 -			format->palette = (SDL_Palette *)malloc(
    1.84 -							sizeof(SDL_Palette));
    1.85 -			if ( format->palette == NULL ) {
    1.86 -				SDL_FreeFormat(format);
    1.87 -				SDL_OutOfMemory();
    1.88 -				return(NULL);
    1.89 -			}
    1.90 -			(format->palette)->ncolors = 256;
    1.91 -			(format->palette)->colors = (SDL_Color *)malloc(
    1.92 -				(format->palette)->ncolors*sizeof(SDL_Color));
    1.93 -			if ( (format->palette)->colors == NULL ) {
    1.94 -				SDL_FreeFormat(format);
    1.95 -				SDL_OutOfMemory();
    1.96 -				return(NULL);
    1.97 -			}
    1.98 -			memset((format->palette)->colors, 0,
    1.99 -				(format->palette)->ncolors*sizeof(SDL_Color));
   1.100 -
   1.101 -			/* Palettized formats have no mask info */
   1.102 -			format->Rloss = 8;
   1.103 -			format->Gloss = 8;
   1.104 -			format->Bloss = 8;
   1.105 -			format->Aloss = 8;
   1.106 -			format->Rshift = 0;
   1.107 -			format->Gshift = 0;
   1.108 -			format->Bshift = 0;
   1.109 -			format->Ashift = 0;
   1.110 -			format->Rmask = 0;
   1.111 -			format->Gmask = 0;
   1.112 -			format->Bmask = 0;
   1.113 -			format->Amask = 0;
   1.114 -			break;
   1.115 -
   1.116 -		default:
   1.117 -			/* No palette, just packed pixel info */
   1.118 -			format->palette = NULL;
   1.119 -			format->Rshift = 0;
   1.120 -			format->Rloss = 8;
   1.121 -			if ( Rmask ) {
   1.122 -				for ( mask = Rmask; !(mask&0x01); mask >>= 1 )
   1.123 -					++format->Rshift;
   1.124 -				for ( ; (mask&0x01); mask >>= 1 )
   1.125 -					--format->Rloss;
   1.126 -			}
   1.127 -			format->Gshift = 0;
   1.128 -			format->Gloss = 8;
   1.129 -			if ( Gmask ) {
   1.130 -				for ( mask = Gmask; !(mask&0x01); mask >>= 1 )
   1.131 -					++format->Gshift;
   1.132 -				for ( ; (mask&0x01); mask >>= 1 )
   1.133 -					--format->Gloss;
   1.134 -			}
   1.135 -			format->Bshift = 0;
   1.136 -			format->Bloss = 8;
   1.137 -			if ( Bmask ) {
   1.138 -				for ( mask = Bmask; !(mask&0x01); mask >>= 1 )
   1.139 -					++format->Bshift;
   1.140 -				for ( ; (mask&0x01); mask >>= 1 )
   1.141 -					--format->Bloss;
   1.142 -			}
   1.143 -			format->Ashift = 0;
   1.144 -			format->Aloss = 8;
   1.145 -			if ( Amask ) {
   1.146 -				for ( mask = Amask; !(mask&0x01); mask >>= 1 )
   1.147 -					++format->Ashift;
   1.148 -				for ( ; (mask&0x01); mask >>= 1 )
   1.149 -					--format->Aloss;
   1.150 -			}
   1.151 -			format->Rmask = Rmask;
   1.152 -			format->Gmask = Gmask;
   1.153 -			format->Bmask = Bmask;
   1.154 -			format->Amask = Amask;
   1.155 -			break;
   1.156 -	}
   1.157 -	/* Calculate some standard bitmasks, if necessary 
   1.158 -	 * Note:  This could conflict with an alpha mask, if given.
   1.159 -	 */
   1.160 -	if ( (bpp > 8) && !format->Rmask && !format->Gmask && !format->Bmask ) {
   1.161 +	if ( Rmask || Bmask || Gmask ) { /* Packed pixels with custom mask */
   1.162 +		format->palette = NULL;
   1.163 +		format->Rshift = 0;
   1.164 +		format->Rloss = 8;
   1.165 +		if ( Rmask ) {
   1.166 +			for ( mask = Rmask; !(mask&0x01); mask >>= 1 )
   1.167 +				++format->Rshift;
   1.168 +			for ( ; (mask&0x01); mask >>= 1 )
   1.169 +				--format->Rloss;
   1.170 +		}
   1.171 +		format->Gshift = 0;
   1.172 +		format->Gloss = 8;
   1.173 +		if ( Gmask ) {
   1.174 +			for ( mask = Gmask; !(mask&0x01); mask >>= 1 )
   1.175 +				++format->Gshift;
   1.176 +			for ( ; (mask&0x01); mask >>= 1 )
   1.177 +				--format->Gloss;
   1.178 +		}
   1.179 +		format->Bshift = 0;
   1.180 +		format->Bloss = 8;
   1.181 +		if ( Bmask ) {
   1.182 +			for ( mask = Bmask; !(mask&0x01); mask >>= 1 )
   1.183 +				++format->Bshift;
   1.184 +			for ( ; (mask&0x01); mask >>= 1 )
   1.185 +				--format->Bloss;
   1.186 +		}
   1.187 +		format->Ashift = 0;
   1.188 +		format->Aloss = 8;
   1.189 +		if ( Amask ) {
   1.190 +			for ( mask = Amask; !(mask&0x01); mask >>= 1 )
   1.191 +				++format->Ashift;
   1.192 +			for ( ; (mask&0x01); mask >>= 1 )
   1.193 +				--format->Aloss;
   1.194 +		}
   1.195 +		format->Rmask = Rmask;
   1.196 +		format->Gmask = Gmask;
   1.197 +		format->Bmask = Bmask;
   1.198 +		format->Amask = Amask;
   1.199 +	} else if ( bpp > 8 ) {		/* Packed pixels with standard mask */
   1.200  		/* R-G-B */
   1.201  		if ( bpp > 24 )
   1.202  			bpp = 24;
   1.203 @@ -227,6 +112,52 @@
   1.204  		format->Rmask = ((0xFF>>format->Rloss)<<format->Rshift);
   1.205  		format->Gmask = ((0xFF>>format->Gloss)<<format->Gshift);
   1.206  		format->Bmask = ((0xFF>>format->Bloss)<<format->Bshift);
   1.207 +	} else {			/* Palettized mode */
   1.208 +		int i, ncolors = 1;
   1.209 +		for ( i = 0; i < bpp; ++i ) {
   1.210 +			ncolors *= 2;
   1.211 +		}
   1.212 +		format->palette = (SDL_Palette *)malloc(sizeof(SDL_Palette));
   1.213 +		if ( format->palette == NULL ) {
   1.214 +			SDL_FreeFormat(format);
   1.215 +			SDL_OutOfMemory();
   1.216 +			return(NULL);
   1.217 +		}
   1.218 +		(format->palette)->ncolors = ncolors;
   1.219 +		(format->palette)->colors = (SDL_Color *)malloc(
   1.220 +				(format->palette)->ncolors*sizeof(SDL_Color));
   1.221 +		if ( (format->palette)->colors == NULL ) {
   1.222 +			SDL_FreeFormat(format);
   1.223 +			SDL_OutOfMemory();
   1.224 +			return(NULL);
   1.225 +		}
   1.226 +		if ( ncolors == 2 ) {
   1.227 +			/* Create a black and white bitmap palette */
   1.228 +			format->palette->colors[0].r = 0xFF;
   1.229 +			format->palette->colors[0].g = 0xFF;
   1.230 +			format->palette->colors[0].b = 0xFF;
   1.231 +			format->palette->colors[1].r = 0x00;
   1.232 +			format->palette->colors[1].g = 0x00;
   1.233 +			format->palette->colors[1].b = 0x00;
   1.234 +		} else {
   1.235 +			/* Create an empty palette */
   1.236 +			memset((format->palette)->colors, 0,
   1.237 +				(format->palette)->ncolors*sizeof(SDL_Color));
   1.238 +		}
   1.239 +
   1.240 +		/* Palettized formats have no mask info */
   1.241 +		format->Rloss = 8;
   1.242 +		format->Gloss = 8;
   1.243 +		format->Bloss = 8;
   1.244 +		format->Aloss = 8;
   1.245 +		format->Rshift = 0;
   1.246 +		format->Gshift = 0;
   1.247 +		format->Bshift = 0;
   1.248 +		format->Ashift = 0;
   1.249 +		format->Rmask = 0;
   1.250 +		format->Gmask = 0;
   1.251 +		format->Bmask = 0;
   1.252 +		format->Amask = 0;
   1.253  	}
   1.254  	return(format);
   1.255  }