src/video/SDL_blit_0.c
changeset 0 74212992fb08
child 91 e85e03f195b4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_blit_0.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,475 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include <stdio.h>
    1.32 +#include <string.h>
    1.33 +
    1.34 +#include "SDL_types.h"
    1.35 +#include "SDL_video.h"
    1.36 +#include "SDL_blit.h"
    1.37 +
    1.38 +/* Functions to blit from bitmaps to other surfaces */
    1.39 +
    1.40 +static void BlitBto1(SDL_BlitInfo *info)
    1.41 +{
    1.42 +	int c;
    1.43 +	int width, height;
    1.44 +	Uint8 *src, *map, *dst;
    1.45 +	int srcskip, dstskip;
    1.46 +
    1.47 +	/* Set up some basic variables */
    1.48 +	width = info->d_width;
    1.49 +	height = info->d_height;
    1.50 +	src = info->s_pixels;
    1.51 +	srcskip = info->s_skip;
    1.52 +	dst = info->d_pixels;
    1.53 +	dstskip = info->d_skip;
    1.54 +	map = info->table;
    1.55 +	srcskip += width-(width+7)/8;
    1.56 +
    1.57 +	if ( map ) {
    1.58 +		while ( height-- ) {
    1.59 +		        Uint8 byte = 0, bit;
    1.60 +	    		for ( c=0; c<width; ++c ) {
    1.61 +				if ( (c%8) == 0 ) {
    1.62 +					byte = *src++;
    1.63 +				}
    1.64 +				bit = (byte&0x80)>>7;
    1.65 +				if ( 1 ) {
    1.66 +				  *dst = map[bit];
    1.67 +				}
    1.68 +				dst++;
    1.69 +				byte <<= 1;
    1.70 +			}
    1.71 +			src += srcskip;
    1.72 +			dst += dstskip;
    1.73 +		}
    1.74 +	} else {
    1.75 +		while ( height-- ) {
    1.76 +		        Uint8 byte = 0, bit;
    1.77 +	    		for ( c=0; c<width; ++c ) {
    1.78 +				if ( (c%8) == 0 ) {
    1.79 +					byte = *src++;
    1.80 +				}
    1.81 +				bit = (byte&0x80)>>7;
    1.82 +				if ( 1 ) {
    1.83 +				  *dst = bit;
    1.84 +				}
    1.85 +				dst++;
    1.86 +				byte <<= 1;
    1.87 +			}
    1.88 +			src += srcskip;
    1.89 +			dst += dstskip;
    1.90 +		}
    1.91 +	}
    1.92 +}
    1.93 +static void BlitBto2(SDL_BlitInfo *info)
    1.94 +{
    1.95 +	int c;
    1.96 +	int width, height;
    1.97 +	Uint8 *src;
    1.98 +	Uint16 *map, *dst;
    1.99 +	int srcskip, dstskip;
   1.100 +
   1.101 +	/* Set up some basic variables */
   1.102 +	width = info->d_width;
   1.103 +	height = info->d_height;
   1.104 +	src = info->s_pixels;
   1.105 +	srcskip = info->s_skip;
   1.106 +	dst = (Uint16 *)info->d_pixels;
   1.107 +	dstskip = info->d_skip/2;
   1.108 +	map = (Uint16 *)info->table;
   1.109 +	srcskip += width-(width+7)/8;
   1.110 +
   1.111 +	while ( height-- ) {
   1.112 +	        Uint8 byte = 0, bit;
   1.113 +	    	for ( c=0; c<width; ++c ) {
   1.114 +			if ( (c%8) == 0 ) {
   1.115 +				byte = *src++;
   1.116 +			}
   1.117 +			bit = (byte&0x80)>>7;
   1.118 +			if ( 1 ) {
   1.119 +				*dst = map[bit];
   1.120 +			}
   1.121 +			byte <<= 1;
   1.122 +			dst++;
   1.123 +		}
   1.124 +		src += srcskip;
   1.125 +		dst += dstskip;
   1.126 +	}
   1.127 +}
   1.128 +static void BlitBto3(SDL_BlitInfo *info)
   1.129 +{
   1.130 +	int c, o;
   1.131 +	int width, height;
   1.132 +	Uint8 *src, *map, *dst;
   1.133 +	int srcskip, dstskip;
   1.134 +
   1.135 +	/* Set up some basic variables */
   1.136 +	width = info->d_width;
   1.137 +	height = info->d_height;
   1.138 +	src = info->s_pixels;
   1.139 +	srcskip = info->s_skip;
   1.140 +	dst = info->d_pixels;
   1.141 +	dstskip = info->d_skip;
   1.142 +	map = info->table;
   1.143 +	srcskip += width-(width+7)/8;
   1.144 +
   1.145 +	while ( height-- ) {
   1.146 +	        Uint8 byte = 0, bit;
   1.147 +	    	for ( c=0; c<width; ++c ) {
   1.148 +			if ( (c%8) == 0 ) {
   1.149 +				byte = *src++;
   1.150 +			}
   1.151 +			bit = (byte&0x80)>>7;
   1.152 +			if ( 1 ) {
   1.153 +				o = bit * 4;
   1.154 +				dst[0] = map[o++];
   1.155 +				dst[1] = map[o++];
   1.156 +				dst[2] = map[o++];
   1.157 +			}
   1.158 +			byte <<= 1;
   1.159 +			dst += 3;
   1.160 +		}
   1.161 +		src += srcskip;
   1.162 +		dst += dstskip;
   1.163 +	}
   1.164 +}
   1.165 +static void BlitBto4(SDL_BlitInfo *info)
   1.166 +{
   1.167 +	int width, height;
   1.168 +	Uint8 *src;
   1.169 +	Uint32 *map, *dst;
   1.170 +	int srcskip, dstskip;
   1.171 +	int c;
   1.172 +
   1.173 +	/* Set up some basic variables */
   1.174 +	width = info->d_width;
   1.175 +	height = info->d_height;
   1.176 +	src = info->s_pixels;
   1.177 +	srcskip = info->s_skip;
   1.178 +	dst = (Uint32 *)info->d_pixels;
   1.179 +	dstskip = info->d_skip/4;
   1.180 +	map = (Uint32 *)info->table;
   1.181 +	srcskip += width-(width+7)/8;
   1.182 +
   1.183 +	while ( height-- ) {
   1.184 +	        Uint8 byte = 0, bit;
   1.185 +	    	for ( c=0; c<width; ++c ) {
   1.186 +			if ( (c%8) == 0 ) {
   1.187 +				byte = *src++;
   1.188 +			}
   1.189 +			bit = (byte&0x80)>>7;
   1.190 +			if ( 1 ) {
   1.191 +				*dst = map[bit];
   1.192 +			}
   1.193 +			byte <<= 1;
   1.194 +			dst++;
   1.195 +		}
   1.196 +		src += srcskip;
   1.197 +		dst += dstskip;
   1.198 +	}
   1.199 +}
   1.200 +
   1.201 +static void BlitBto1Key(SDL_BlitInfo *info)
   1.202 +{
   1.203 +        int width = info->d_width;
   1.204 +	int height = info->d_height;
   1.205 +	Uint8 *src = info->s_pixels;
   1.206 +	Uint8 *dst = info->d_pixels;
   1.207 +	int srcskip = info->s_skip;
   1.208 +	int dstskip = info->d_skip;
   1.209 +	Uint32 ckey = info->src->colorkey;
   1.210 +	Uint8 *palmap = info->table;
   1.211 +	int c;
   1.212 +
   1.213 +	/* Set up some basic variables */
   1.214 +	srcskip += width-(width+7)/8;
   1.215 +
   1.216 +	if ( palmap ) {
   1.217 +		while ( height-- ) {
   1.218 +		        Uint8  byte = 0, bit;
   1.219 +	    		for ( c=0; c<width; ++c ) {
   1.220 +				if ( (c%8) == 0 ) {
   1.221 +					byte = *src++;
   1.222 +				}
   1.223 +				bit = (byte&0x80)>>7;
   1.224 +				if ( bit != ckey ) {
   1.225 +				  *dst = palmap[bit];
   1.226 +				}
   1.227 +				dst++;
   1.228 +				byte <<= 1;
   1.229 +			}
   1.230 +			src += srcskip;
   1.231 +			dst += dstskip;
   1.232 +		}
   1.233 +	} else {
   1.234 +		while ( height-- ) {
   1.235 +		        Uint8  byte = 0, bit;
   1.236 +	    		for ( c=0; c<width; ++c ) {
   1.237 +				if ( (c%8) == 0 ) {
   1.238 +					byte = *src++;
   1.239 +				}
   1.240 +				bit = (byte&0x80)>>7;
   1.241 +				if ( bit != ckey ) {
   1.242 +				  *dst = bit;
   1.243 +				}
   1.244 +				dst++;
   1.245 +				byte <<= 1;
   1.246 +			}
   1.247 +			src += srcskip;
   1.248 +			dst += dstskip;
   1.249 +		}
   1.250 +	}
   1.251 +}
   1.252 +
   1.253 +static void BlitBto2Key(SDL_BlitInfo *info)
   1.254 +{
   1.255 +        int width = info->d_width;
   1.256 +	int height = info->d_height;
   1.257 +	Uint8 *src = info->s_pixels;
   1.258 +	Uint16 *dstp = (Uint16 *)info->d_pixels;
   1.259 +	int srcskip = info->s_skip;
   1.260 +	int dstskip = info->d_skip;
   1.261 +	Uint32 ckey = info->src->colorkey;
   1.262 +	Uint8 *palmap = info->table;
   1.263 +	int c;
   1.264 +
   1.265 +	/* Set up some basic variables */
   1.266 +	srcskip += width-(width+7)/8;
   1.267 +	dstskip /= 2;
   1.268 +
   1.269 +	while ( height-- ) {
   1.270 +	        Uint8 byte = 0, bit;
   1.271 +	    	for ( c=0; c<width; ++c ) {
   1.272 +			if ( (c%8) == 0 ) {
   1.273 +				byte = *src++;
   1.274 +			}
   1.275 +			bit = (byte&0x80)>>7;
   1.276 +			if ( bit != ckey ) {
   1.277 +				*dstp=((Uint16 *)palmap)[bit];
   1.278 +			}
   1.279 +			byte <<= 1;
   1.280 +			dstp++;
   1.281 +		}
   1.282 +		src += srcskip;
   1.283 +		dstp += dstskip;
   1.284 +	}
   1.285 +}
   1.286 +
   1.287 +static void BlitBto3Key(SDL_BlitInfo *info)
   1.288 +{
   1.289 +        int width = info->d_width;
   1.290 +	int height = info->d_height;
   1.291 +	Uint8 *src = info->s_pixels;
   1.292 +	Uint8 *dst = info->d_pixels;
   1.293 +	int srcskip = info->s_skip;
   1.294 +	int dstskip = info->d_skip;
   1.295 +	Uint32 ckey = info->src->colorkey;
   1.296 +	Uint8 *palmap = info->table;
   1.297 +	int c;
   1.298 +
   1.299 +	/* Set up some basic variables */
   1.300 +	srcskip += width-(width+7)/8;
   1.301 +
   1.302 +	while ( height-- ) {
   1.303 +	        Uint8  byte = 0, bit;
   1.304 +	    	for ( c=0; c<width; ++c ) {
   1.305 +			if ( (c%8) == 0 ) {
   1.306 +				byte = *src++;
   1.307 +			}
   1.308 +			bit = (byte&0x80)>>7;
   1.309 +			if ( bit != ckey ) {
   1.310 +				memcpy(dst, &palmap[bit*4], 3);
   1.311 +			}
   1.312 +			byte <<= 1;
   1.313 +			dst += 3;
   1.314 +		}
   1.315 +		src += srcskip;
   1.316 +		dst += dstskip;
   1.317 +	}
   1.318 +}
   1.319 +
   1.320 +static void BlitBto4Key(SDL_BlitInfo *info)
   1.321 +{
   1.322 +        int width = info->d_width;
   1.323 +	int height = info->d_height;
   1.324 +	Uint8 *src = info->s_pixels;
   1.325 +	Uint32 *dstp = (Uint32 *)info->d_pixels;
   1.326 +	int srcskip = info->s_skip;
   1.327 +	int dstskip = info->d_skip;
   1.328 +	Uint32 ckey = info->src->colorkey;
   1.329 +	Uint8 *palmap = info->table;
   1.330 +	int c;
   1.331 +
   1.332 +	/* Set up some basic variables */
   1.333 +	srcskip += width-(width+7)/8;
   1.334 +	dstskip /= 4;
   1.335 +
   1.336 +	while ( height-- ) {
   1.337 +	        Uint8 byte = 0, bit;
   1.338 +	    	for ( c=0; c<width; ++c ) {
   1.339 +			if ( (c%8) == 0 ) {
   1.340 +				byte = *src++;
   1.341 +			}
   1.342 +			bit = (byte&0x80)>>7;
   1.343 +			if ( bit != ckey ) {
   1.344 +				*dstp=((Uint32 *)palmap)[bit];
   1.345 +			}
   1.346 +			byte <<= 1;
   1.347 +			dstp++;
   1.348 +		}
   1.349 +		src += srcskip;
   1.350 +		dstp += dstskip;
   1.351 +	}
   1.352 +}
   1.353 +
   1.354 +static void BlitBtoNAlpha(SDL_BlitInfo *info)
   1.355 +{
   1.356 +        int width = info->d_width;
   1.357 +	int height = info->d_height;
   1.358 +	Uint8 *src = info->s_pixels;
   1.359 +	Uint8 *dst = info->d_pixels;
   1.360 +	int srcskip = info->s_skip;
   1.361 +	int dstskip = info->d_skip;
   1.362 +	const SDL_Color *srcpal	= info->src->palette->colors;
   1.363 +	SDL_PixelFormat *dstfmt = info->dst;
   1.364 +	int  dstbpp;
   1.365 +	int c;
   1.366 +	const int A = info->src->alpha;
   1.367 +
   1.368 +	/* Set up some basic variables */
   1.369 +	dstbpp = dstfmt->BytesPerPixel;
   1.370 +	srcskip += width-(width+7)/8;
   1.371 +
   1.372 +	while ( height-- ) {
   1.373 +	        Uint8 byte = 0, bit;
   1.374 +	    	for ( c=0; c<width; ++c ) {
   1.375 +			if ( (c%8) == 0 ) {
   1.376 +				byte = *src++;
   1.377 +			}
   1.378 +			bit = (byte&0x80)>>7;
   1.379 +			if ( 1 ) {
   1.380 +			        Uint32 pixel;
   1.381 +			        unsigned sR, sG, sB;
   1.382 +				unsigned dR, dG, dB;
   1.383 +				sR = srcpal[bit].r;
   1.384 +				sG = srcpal[bit].g;
   1.385 +				sB = srcpal[bit].b;
   1.386 +				DISEMBLE_RGB(dst, dstbpp, dstfmt,
   1.387 +							pixel, dR, dG, dB);
   1.388 +				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
   1.389 +			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
   1.390 +			}
   1.391 +			byte <<= 1;
   1.392 +			dst += dstbpp;
   1.393 +		}
   1.394 +		src += srcskip;
   1.395 +		dst += dstskip;
   1.396 +	}
   1.397 +}
   1.398 +
   1.399 +static void BlitBtoNAlphaKey(SDL_BlitInfo *info)
   1.400 +{
   1.401 +        int width = info->d_width;
   1.402 +	int height = info->d_height;
   1.403 +	Uint8 *src = info->s_pixels;
   1.404 +	Uint8 *dst = info->d_pixels;
   1.405 +	int srcskip = info->s_skip;
   1.406 +	int dstskip = info->d_skip;
   1.407 +	SDL_PixelFormat *srcfmt = info->src;
   1.408 +	SDL_PixelFormat *dstfmt = info->dst;
   1.409 +	const SDL_Color *srcpal	= srcfmt->palette->colors;
   1.410 +	int dstbpp;
   1.411 +	int c;
   1.412 +	const int A = srcfmt->alpha;
   1.413 +	Uint32 ckey = srcfmt->colorkey;
   1.414 +
   1.415 +	/* Set up some basic variables */
   1.416 +	dstbpp = dstfmt->BytesPerPixel;
   1.417 +	srcskip += width-(width+7)/8;
   1.418 +
   1.419 +	while ( height-- ) {
   1.420 +	        Uint8  byte = 0, bit;
   1.421 +	    	for ( c=0; c<width; ++c ) {
   1.422 +			if ( (c%8) == 0 ) {
   1.423 +				byte = *src++;
   1.424 +			}
   1.425 +			bit = (byte&0x80)>>7;
   1.426 +			if ( bit != ckey ) {
   1.427 +			        int sR, sG, sB;
   1.428 +				int dR, dG, dB;
   1.429 +				Uint32 pixel;
   1.430 +				sR = srcpal[bit].r;
   1.431 +				sG = srcpal[bit].g;
   1.432 +				sB = srcpal[bit].b;
   1.433 +				DISEMBLE_RGB(dst, dstbpp, dstfmt,
   1.434 +							pixel, dR, dG, dB);
   1.435 +				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
   1.436 +			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
   1.437 +			}
   1.438 +			byte <<= 1;
   1.439 +			dst += dstbpp;
   1.440 +		}
   1.441 +		src += srcskip;
   1.442 +		dst += dstskip;
   1.443 +	}
   1.444 +}
   1.445 +
   1.446 +static SDL_loblit bitmap_blit[] = {
   1.447 +	NULL, BlitBto1, BlitBto2, BlitBto3, BlitBto4
   1.448 +};
   1.449 +
   1.450 +static SDL_loblit colorkey_blit[] = {
   1.451 +    NULL, BlitBto1Key, BlitBto2Key, BlitBto3Key, BlitBto4Key
   1.452 +};
   1.453 +
   1.454 +SDL_loblit SDL_CalculateBlit0(SDL_Surface *surface, int blit_index)
   1.455 +{
   1.456 +	int which;
   1.457 +
   1.458 +	if ( surface->map->dst->format->BitsPerPixel < 8 ) {
   1.459 +		which = 0;
   1.460 +	} else {
   1.461 +		which = surface->map->dst->format->BytesPerPixel;
   1.462 +	}
   1.463 +	switch(blit_index) {
   1.464 +	case 0:			/* copy */
   1.465 +	    return bitmap_blit[which];
   1.466 +
   1.467 +	case 1:			/* colorkey */
   1.468 +	    return colorkey_blit[which];
   1.469 +
   1.470 +	case 2:			/* alpha */
   1.471 +	    return which >= 2 ? BlitBtoNAlpha : NULL;
   1.472 +
   1.473 +	case 4:			/* alpha + colorkey */
   1.474 +	    return which >= 2 ? BlitBtoNAlphaKey : NULL;
   1.475 +	}
   1.476 +	return NULL;
   1.477 +}
   1.478 +