src/video/SDL_blit_1.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_1.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,530 @@
     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 +
    1.33 +#include "SDL_types.h"
    1.34 +#include "SDL_video.h"
    1.35 +#include "SDL_blit.h"
    1.36 +#include "SDL_sysvideo.h"
    1.37 +#include "SDL_endian.h"
    1.38 +
    1.39 +/* Functions to blit from 8-bit surfaces to other surfaces */
    1.40 +
    1.41 +static void Blit1to1(SDL_BlitInfo *info)
    1.42 +{
    1.43 +#ifndef USE_DUFFS_LOOP
    1.44 +	int c;
    1.45 +#endif
    1.46 +	int width, height;
    1.47 +	Uint8 *src, *map, *dst;
    1.48 +	int srcskip, dstskip;
    1.49 +
    1.50 +	/* Set up some basic variables */
    1.51 +	width = info->d_width;
    1.52 +	height = info->d_height;
    1.53 +	src = info->s_pixels;
    1.54 +	srcskip = info->s_skip;
    1.55 +	dst = info->d_pixels;
    1.56 +	dstskip = info->d_skip;
    1.57 +	map = info->table;
    1.58 +
    1.59 +	while ( height-- ) {
    1.60 +#ifdef USE_DUFFS_LOOP
    1.61 +		DUFFS_LOOP(
    1.62 +			{
    1.63 +			  *dst = map[*src];
    1.64 +			}
    1.65 +			dst++;
    1.66 +			src++;
    1.67 +		, width);
    1.68 +#else
    1.69 +		for ( c=width; c; --c ) {
    1.70 +		        *dst = map[*src];
    1.71 +			dst++;
    1.72 +			src++;
    1.73 +		}
    1.74 +#endif
    1.75 +		src += srcskip;
    1.76 +		dst += dstskip;
    1.77 +	}
    1.78 +}
    1.79 +/* This is now endian dependent */
    1.80 +#if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
    1.81 +#define HI	1
    1.82 +#define LO	0
    1.83 +#else /* ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) */
    1.84 +#define HI	0
    1.85 +#define LO	1
    1.86 +#endif
    1.87 +static void Blit1to2(SDL_BlitInfo *info)
    1.88 +{
    1.89 +#ifndef USE_DUFFS_LOOP
    1.90 +	int c;
    1.91 +#endif
    1.92 +	int width, height;
    1.93 +	Uint8 *src, *dst;
    1.94 +	Uint16 *map;
    1.95 +	int srcskip, dstskip;
    1.96 +
    1.97 +	/* Set up some basic variables */
    1.98 +	width = info->d_width;
    1.99 +	height = info->d_height;
   1.100 +	src = info->s_pixels;
   1.101 +	srcskip = info->s_skip;
   1.102 +	dst = info->d_pixels;
   1.103 +	dstskip = info->d_skip;
   1.104 +	map = (Uint16 *)info->table;
   1.105 +
   1.106 +#ifdef USE_DUFFS_LOOP
   1.107 +	while ( height-- ) {
   1.108 +		DUFFS_LOOP(
   1.109 +		{
   1.110 +			*(Uint16 *)dst = map[*src++];
   1.111 +			dst += 2;
   1.112 +		},
   1.113 +		width);
   1.114 +		src += srcskip;
   1.115 +		dst += dstskip;
   1.116 +	}
   1.117 +#else
   1.118 +	/* Memory align at 4-byte boundary, if necessary */
   1.119 +	if ( (long)dst & 0x03 ) {
   1.120 +		/* Don't do anything if width is 0 */
   1.121 +		if ( width == 0 ) {
   1.122 +			return;
   1.123 +		}
   1.124 +		--width;
   1.125 +
   1.126 +		while ( height-- ) {
   1.127 +			/* Perform copy alignment */
   1.128 +			*(Uint16 *)dst = map[*src++];
   1.129 +			dst += 2;
   1.130 +
   1.131 +			/* Copy in 4 pixel chunks */
   1.132 +			for ( c=width/4; c; --c ) {
   1.133 +				*(Uint32 *)dst =
   1.134 +					(map[src[HI]]<<16)|(map[src[LO]]);
   1.135 +				src += 2;
   1.136 +				dst += 4;
   1.137 +				*(Uint32 *)dst =
   1.138 +					(map[src[HI]]<<16)|(map[src[LO]]);
   1.139 +				src += 2;
   1.140 +				dst += 4;
   1.141 +			}
   1.142 +			/* Get any leftovers */
   1.143 +			switch (width % 4) {
   1.144 +				case 3:
   1.145 +					*(Uint16 *)dst = map[*src++];
   1.146 +					dst += 2;
   1.147 +				case 2:
   1.148 +					*(Uint32 *)dst =
   1.149 +					  (map[src[HI]]<<16)|(map[src[LO]]);
   1.150 +					src += 2;
   1.151 +					dst += 4;
   1.152 +					break;
   1.153 +				case 1:
   1.154 +					*(Uint16 *)dst = map[*src++];
   1.155 +					dst += 2;
   1.156 +					break;
   1.157 +			}
   1.158 +			src += srcskip;
   1.159 +			dst += dstskip;
   1.160 +		}
   1.161 +	} else { 
   1.162 +		while ( height-- ) {
   1.163 +			/* Copy in 4 pixel chunks */
   1.164 +			for ( c=width/4; c; --c ) {
   1.165 +				*(Uint32 *)dst =
   1.166 +					(map[src[HI]]<<16)|(map[src[LO]]);
   1.167 +				src += 2;
   1.168 +				dst += 4;
   1.169 +				*(Uint32 *)dst =
   1.170 +					(map[src[HI]]<<16)|(map[src[LO]]);
   1.171 +				src += 2;
   1.172 +				dst += 4;
   1.173 +			}
   1.174 +			/* Get any leftovers */
   1.175 +			switch (width % 4) {
   1.176 +				case 3:
   1.177 +					*(Uint16 *)dst = map[*src++];
   1.178 +					dst += 2;
   1.179 +				case 2:
   1.180 +					*(Uint32 *)dst =
   1.181 +					  (map[src[HI]]<<16)|(map[src[LO]]);
   1.182 +					src += 2;
   1.183 +					dst += 4;
   1.184 +					break;
   1.185 +				case 1:
   1.186 +					*(Uint16 *)dst = map[*src++];
   1.187 +					dst += 2;
   1.188 +					break;
   1.189 +			}
   1.190 +			src += srcskip;
   1.191 +			dst += dstskip;
   1.192 +		}
   1.193 +	}
   1.194 +#endif /* USE_DUFFS_LOOP */
   1.195 +}
   1.196 +static void Blit1to3(SDL_BlitInfo *info)
   1.197 +{
   1.198 +#ifndef USE_DUFFS_LOOP
   1.199 +	int c;
   1.200 +#endif
   1.201 +	int o;
   1.202 +	int width, height;
   1.203 +	Uint8 *src, *map, *dst;
   1.204 +	int srcskip, dstskip;
   1.205 +
   1.206 +	/* Set up some basic variables */
   1.207 +	width = info->d_width;
   1.208 +	height = info->d_height;
   1.209 +	src = info->s_pixels;
   1.210 +	srcskip = info->s_skip;
   1.211 +	dst = info->d_pixels;
   1.212 +	dstskip = info->d_skip;
   1.213 +	map = info->table;
   1.214 +
   1.215 +	while ( height-- ) {
   1.216 +#ifdef USE_DUFFS_LOOP
   1.217 +		DUFFS_LOOP(
   1.218 +			{
   1.219 +				o = *src * 4;
   1.220 +				dst[0] = map[o++];
   1.221 +				dst[1] = map[o++];
   1.222 +				dst[2] = map[o++];
   1.223 +			}
   1.224 +			src++;
   1.225 +			dst += 3;
   1.226 +		, width);
   1.227 +#else
   1.228 +		for ( c=width; c; --c ) {
   1.229 +			o = *src * 4;
   1.230 +			dst[0] = map[o++];
   1.231 +			dst[1] = map[o++];
   1.232 +			dst[2] = map[o++];
   1.233 +			src++;
   1.234 +			dst += 3;
   1.235 +		}
   1.236 +#endif /* USE_DUFFS_LOOP */
   1.237 +		src += srcskip;
   1.238 +		dst += dstskip;
   1.239 +	}
   1.240 +}
   1.241 +static void Blit1to4(SDL_BlitInfo *info)
   1.242 +{
   1.243 +#ifndef USE_DUFFS_LOOP
   1.244 +	int c;
   1.245 +#endif
   1.246 +	int width, height;
   1.247 +	Uint8 *src;
   1.248 +	Uint32 *map, *dst;
   1.249 +	int srcskip, dstskip;
   1.250 +
   1.251 +	/* Set up some basic variables */
   1.252 +	width = info->d_width;
   1.253 +	height = info->d_height;
   1.254 +	src = info->s_pixels;
   1.255 +	srcskip = info->s_skip;
   1.256 +	dst = (Uint32 *)info->d_pixels;
   1.257 +	dstskip = info->d_skip/4;
   1.258 +	map = (Uint32 *)info->table;
   1.259 +
   1.260 +	while ( height-- ) {
   1.261 +#ifdef USE_DUFFS_LOOP
   1.262 +		DUFFS_LOOP(
   1.263 +			*dst++ = map[*src++];
   1.264 +		, width);
   1.265 +#else
   1.266 +		for ( c=width/4; c; --c ) {
   1.267 +			*dst++ = map[*src++];
   1.268 +			*dst++ = map[*src++];
   1.269 +			*dst++ = map[*src++];
   1.270 +			*dst++ = map[*src++];
   1.271 +		}
   1.272 +		switch ( width % 4 ) {
   1.273 +			case 3:
   1.274 +				*dst++ = map[*src++];
   1.275 +			case 2:
   1.276 +				*dst++ = map[*src++];
   1.277 +			case 1:
   1.278 +				*dst++ = map[*src++];
   1.279 +		}
   1.280 +#endif /* USE_DUFFS_LOOP */
   1.281 +		src += srcskip;
   1.282 +		dst += dstskip;
   1.283 +	}
   1.284 +}
   1.285 +
   1.286 +static void Blit1to1Key(SDL_BlitInfo *info)
   1.287 +{
   1.288 +	int width = info->d_width;
   1.289 +	int height = info->d_height;
   1.290 +	Uint8 *src = info->s_pixels;
   1.291 +	int srcskip = info->s_skip;
   1.292 +	Uint8 *dst = info->d_pixels;
   1.293 +	int dstskip = info->d_skip;
   1.294 +	Uint8 *palmap = info->table;
   1.295 +	Uint32 ckey = info->src->colorkey;
   1.296 +        
   1.297 +	if ( palmap ) {
   1.298 +		while ( height-- ) {
   1.299 +			DUFFS_LOOP(
   1.300 +			{
   1.301 +				if ( *src != ckey ) {
   1.302 +				  *dst = palmap[*src];
   1.303 +				}
   1.304 +				dst++;
   1.305 +				src++;
   1.306 +			},
   1.307 +			width);
   1.308 +			src += srcskip;
   1.309 +			dst += dstskip;
   1.310 +		}
   1.311 +	} else {
   1.312 +		while ( height-- ) {
   1.313 +			DUFFS_LOOP(
   1.314 +			{
   1.315 +				if ( *src != ckey ) {
   1.316 +				  *dst = *src;
   1.317 +				}
   1.318 +				dst++;
   1.319 +				src++;
   1.320 +			},
   1.321 +			width);
   1.322 +			src += srcskip;
   1.323 +			dst += dstskip;
   1.324 +		}
   1.325 +	}
   1.326 +}
   1.327 +
   1.328 +static void Blit1to2Key(SDL_BlitInfo *info)
   1.329 +{
   1.330 +	int width = info->d_width;
   1.331 +	int height = info->d_height;
   1.332 +	Uint8 *src = info->s_pixels;
   1.333 +	int srcskip = info->s_skip;
   1.334 +	Uint16 *dstp = (Uint16 *)info->d_pixels;
   1.335 +	int dstskip = info->d_skip;
   1.336 +	Uint16 *palmap = (Uint16 *)info->table;
   1.337 +	Uint32 ckey = info->src->colorkey;
   1.338 +
   1.339 +	/* Set up some basic variables */
   1.340 +	dstskip /= 2;
   1.341 +
   1.342 +	while ( height-- ) {
   1.343 +		DUFFS_LOOP(
   1.344 +		{
   1.345 +			if ( *src != ckey ) {
   1.346 +				*dstp=palmap[*src];
   1.347 +			}
   1.348 +			src++;
   1.349 +			dstp++;
   1.350 +		},
   1.351 +		width);
   1.352 +		src += srcskip;
   1.353 +		dstp += dstskip;
   1.354 +	}
   1.355 +}
   1.356 +
   1.357 +static void Blit1to3Key(SDL_BlitInfo *info)
   1.358 +{
   1.359 +	int width = info->d_width;
   1.360 +	int height = info->d_height;
   1.361 +	Uint8 *src = info->s_pixels;
   1.362 +	int srcskip = info->s_skip;
   1.363 +	Uint8 *dst = info->d_pixels;
   1.364 +	int dstskip = info->d_skip;
   1.365 +	Uint8 *palmap = info->table;
   1.366 +	Uint32 ckey = info->src->colorkey;
   1.367 +	int o;
   1.368 +
   1.369 +	while ( height-- ) {
   1.370 +		DUFFS_LOOP(
   1.371 +		{
   1.372 +			if ( *src != ckey ) {
   1.373 +				o = *src * 4;
   1.374 +				dst[0] = palmap[o++];
   1.375 +				dst[1] = palmap[o++];
   1.376 +				dst[2] = palmap[o++];
   1.377 +			}
   1.378 +			src++;
   1.379 +			dst += 3;
   1.380 +		},
   1.381 +		width);
   1.382 +		src += srcskip;
   1.383 +		dst += dstskip;
   1.384 +	}
   1.385 +}
   1.386 +
   1.387 +static void Blit1to4Key(SDL_BlitInfo *info)
   1.388 +{
   1.389 +	int width = info->d_width;
   1.390 +	int height = info->d_height;
   1.391 +	Uint8 *src = info->s_pixels;
   1.392 +	int srcskip = info->s_skip;
   1.393 +	Uint32 *dstp = (Uint32 *)info->d_pixels;
   1.394 +	int dstskip = info->d_skip;
   1.395 +	Uint32 *palmap = (Uint32 *)info->table;
   1.396 +	Uint32 ckey = info->src->colorkey;
   1.397 +
   1.398 +	/* Set up some basic variables */
   1.399 +	dstskip /= 4;
   1.400 +
   1.401 +	while ( height-- ) {
   1.402 +		DUFFS_LOOP(
   1.403 +		{
   1.404 +			if ( *src != ckey ) {
   1.405 +				*dstp = palmap[*src];
   1.406 +			}
   1.407 +			src++;
   1.408 +			dstp++;
   1.409 +		},
   1.410 +		width);
   1.411 +		src += srcskip;
   1.412 +		dstp += dstskip;
   1.413 +	}
   1.414 +}
   1.415 +
   1.416 +static void Blit1toNAlpha(SDL_BlitInfo *info)
   1.417 +{
   1.418 +	int width = info->d_width;
   1.419 +	int height = info->d_height;
   1.420 +	Uint8 *src = info->s_pixels;
   1.421 +	int srcskip = info->s_skip;
   1.422 +	Uint8 *dst = info->d_pixels;
   1.423 +	int dstskip = info->d_skip;
   1.424 +	SDL_PixelFormat *dstfmt = info->dst;
   1.425 +	const SDL_Color *srcpal	= info->src->palette->colors;
   1.426 +	int dstbpp;
   1.427 +	const int A = info->src->alpha;
   1.428 +
   1.429 +	/* Set up some basic variables */
   1.430 +	dstbpp = dstfmt->BytesPerPixel;
   1.431 +
   1.432 +	while ( height-- ) {
   1.433 +	        int sR, sG, sB;
   1.434 +		int dR, dG, dB;
   1.435 +	    	DUFFS_LOOP4(
   1.436 +			{
   1.437 +			        Uint32 pixel;
   1.438 +				sR = srcpal[*src].r;
   1.439 +				sG = srcpal[*src].g;
   1.440 +				sB = srcpal[*src].b;
   1.441 +				DISEMBLE_RGB(dst, dstbpp, dstfmt,
   1.442 +					     pixel, dR, dG, dB);
   1.443 +				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
   1.444 +			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
   1.445 +				src++;
   1.446 +				dst += dstbpp;
   1.447 +			},
   1.448 +			width);
   1.449 +		src += srcskip;
   1.450 +		dst += dstskip;
   1.451 +	}
   1.452 +}
   1.453 +
   1.454 +static void Blit1toNAlphaKey(SDL_BlitInfo *info)
   1.455 +{
   1.456 +	int width = info->d_width;
   1.457 +	int height = info->d_height;
   1.458 +	Uint8 *src = info->s_pixels;
   1.459 +	int srcskip = info->s_skip;
   1.460 +	Uint8 *dst = info->d_pixels;
   1.461 +	int dstskip = info->d_skip;
   1.462 +	SDL_PixelFormat *srcfmt = info->src;
   1.463 +	SDL_PixelFormat *dstfmt = info->dst;
   1.464 +	const SDL_Color *srcpal	= info->src->palette->colors;
   1.465 +	Uint32 ckey = srcfmt->colorkey;
   1.466 +	int dstbpp;
   1.467 +	const int A = srcfmt->alpha;
   1.468 +
   1.469 +	/* Set up some basic variables */
   1.470 +	dstbpp = dstfmt->BytesPerPixel;
   1.471 +
   1.472 +	while ( height-- ) {
   1.473 +	        int sR, sG, sB;
   1.474 +		int dR, dG, dB;
   1.475 +		DUFFS_LOOP(
   1.476 +		{
   1.477 +			if ( *src != ckey ) {
   1.478 +			        Uint32 pixel;
   1.479 +				sR = srcpal[*src].r;
   1.480 +				sG = srcpal[*src].g;
   1.481 +				sB = srcpal[*src].b;
   1.482 +				DISEMBLE_RGB(dst, dstbpp, dstfmt,
   1.483 +							pixel, dR, dG, dB);
   1.484 +				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
   1.485 +			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
   1.486 +			}
   1.487 +			src++;
   1.488 +			dst += dstbpp;
   1.489 +		},
   1.490 +		width);
   1.491 +		src += srcskip;
   1.492 +		dst += dstskip;
   1.493 +	}
   1.494 +}
   1.495 +
   1.496 +static SDL_loblit one_blit[] = {
   1.497 +	NULL, Blit1to1, Blit1to2, Blit1to3, Blit1to4
   1.498 +};
   1.499 +
   1.500 +static SDL_loblit one_blitkey[] = {
   1.501 +        NULL, Blit1to1Key, Blit1to2Key, Blit1to3Key, Blit1to4Key
   1.502 +};
   1.503 +
   1.504 +SDL_loblit SDL_CalculateBlit1(SDL_Surface *surface, int blit_index)
   1.505 +{
   1.506 +	int which;
   1.507 +	SDL_PixelFormat *dstfmt;
   1.508 +
   1.509 +	dstfmt = surface->map->dst->format;
   1.510 +	if ( dstfmt->BitsPerPixel < 8 ) {
   1.511 +		which = 0;
   1.512 +	} else {
   1.513 +		which = dstfmt->BytesPerPixel;
   1.514 +	}
   1.515 +	switch(blit_index) {
   1.516 +	case 0:			/* copy */
   1.517 +	    return one_blit[which];
   1.518 +
   1.519 +	case 1:			/* colorkey */
   1.520 +	    return one_blitkey[which];
   1.521 +
   1.522 +	case 2:			/* alpha */
   1.523 +	    /* Supporting 8bpp->8bpp alpha is doable but requires lots of
   1.524 +	       tables which consume space and takes time to precompute,
   1.525 +	       so is better left to the user */
   1.526 +	    return which >= 2 ? Blit1toNAlpha : NULL;
   1.527 +
   1.528 +	case 3:			/* alpha + colorkey */
   1.529 +	    return which >= 2 ? Blit1toNAlphaKey : NULL;
   1.530 +
   1.531 +	}
   1.532 +	return NULL;
   1.533 +}