src/video/SDL_blit.h
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.h	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,437 @@
     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 +#ifndef _SDL_blit_h
    1.32 +#define _SDL_blit_h
    1.33 +
    1.34 +#include "SDL_endian.h"
    1.35 +
    1.36 +/* The structure passed to the low level blit functions */
    1.37 +typedef struct {
    1.38 +	Uint8 *s_pixels;
    1.39 +	int s_width;
    1.40 +	int s_height;
    1.41 +	int s_skip;
    1.42 +	Uint8 *d_pixels;
    1.43 +	int d_width;
    1.44 +	int d_height;
    1.45 +	int d_skip;
    1.46 +	void *aux_data;
    1.47 +	SDL_PixelFormat *src;
    1.48 +	Uint8 *table;
    1.49 +	SDL_PixelFormat *dst;
    1.50 +} SDL_BlitInfo;
    1.51 +
    1.52 +/* The type definition for the low level blit functions */
    1.53 +typedef void (*SDL_loblit)(SDL_BlitInfo *info);
    1.54 +
    1.55 +/* This is the private info structure for software accelerated blits */
    1.56 +struct private_swaccel {
    1.57 +	SDL_loblit blit;
    1.58 +	void *aux_data;
    1.59 +};
    1.60 +
    1.61 +/* Blit mapping definition */
    1.62 +typedef struct SDL_BlitMap {
    1.63 +	SDL_Surface *dst;
    1.64 +	int identity;
    1.65 +	Uint8 *table;
    1.66 +	SDL_blit hw_blit;
    1.67 +	SDL_blit sw_blit;
    1.68 +	struct private_hwaccel *hw_data;
    1.69 +	struct private_swaccel *sw_data;
    1.70 +
    1.71 +	/* the version count matches the destination; mismatch indicates
    1.72 +	   an invalid mapping */
    1.73 +        unsigned int format_version;
    1.74 +} SDL_BlitMap;
    1.75 +
    1.76 +
    1.77 +/* Definitions for special global blit functions */
    1.78 +#include "SDL_blit_A.h"
    1.79 +
    1.80 +/* Functions found in SDL_blit.c */
    1.81 +extern int SDL_CalculateBlit(SDL_Surface *surface);
    1.82 +
    1.83 +/* Functions found in SDL_blit_{0,1,N,A}.c */
    1.84 +extern SDL_loblit SDL_CalculateBlit0(SDL_Surface *surface, int complex);
    1.85 +extern SDL_loblit SDL_CalculateBlit1(SDL_Surface *surface, int complex);
    1.86 +extern SDL_loblit SDL_CalculateBlitN(SDL_Surface *surface, int complex);
    1.87 +extern SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int complex);
    1.88 +
    1.89 +/*
    1.90 + * Useful macros for blitting routines
    1.91 + */
    1.92 +
    1.93 +#define FORMAT_EQUAL(A, B)						\
    1.94 +    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
    1.95 +     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
    1.96 +
    1.97 +/* Load pixel of the specified format from a buffer and get its R-G-B values */
    1.98 +/* FIXME: rescale values to 0..255 here? */
    1.99 +#define RGB_FROM_PIXEL(pixel, fmt, r, g, b)				\
   1.100 +{									\
   1.101 +	r = (((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
   1.102 +	g = (((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
   1.103 +	b = (((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
   1.104 +}
   1.105 +#define RGB_FROM_RGB565(pixel, r, g, b)					\
   1.106 +{									\
   1.107 +	r = (((pixel&0xF800)>>11)<<3);		 			\
   1.108 +	g = (((pixel&0x07E0)>>5)<<2); 					\
   1.109 +	b = ((pixel&0x001F)<<3); 					\
   1.110 +}
   1.111 +#define RGB_FROM_RGB555(pixel, r, g, b)					\
   1.112 +{									\
   1.113 +	r = (((pixel&0x7C00)>>10)<<3);		 			\
   1.114 +	g = (((pixel&0x03E0)>>5)<<3); 					\
   1.115 +	b = ((pixel&0x001F)<<3); 					\
   1.116 +}
   1.117 +#define RGB_FROM_RGB888(pixel, r, g, b)					\
   1.118 +{									\
   1.119 +	r = ((pixel&0xFF0000)>>16);		 			\
   1.120 +	g = ((pixel&0xFF00)>>8);		 			\
   1.121 +	b = (pixel&0xFF);			 			\
   1.122 +}
   1.123 +#define RETRIEVE_RGB_PIXEL(buf, bpp, pixel)				   \
   1.124 +do {									   \
   1.125 +	switch (bpp) {							   \
   1.126 +		case 2:							   \
   1.127 +			pixel = *((Uint16 *)(buf));			   \
   1.128 +		break;							   \
   1.129 +									   \
   1.130 +		case 3: {						   \
   1.131 +		        Uint8 *B = (Uint8 *)(buf);			   \
   1.132 +			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   1.133 +			        pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   1.134 +			} else {					   \
   1.135 +			        pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   1.136 +			}						   \
   1.137 +		}							   \
   1.138 +		break;							   \
   1.139 +									   \
   1.140 +		case 4:							   \
   1.141 +			pixel = *((Uint32 *)(buf));			   \
   1.142 +		break;							   \
   1.143 +									   \
   1.144 +		default:						   \
   1.145 +			pixel = 0; /* appease gcc */			   \
   1.146 +		break;							   \
   1.147 +	}								   \
   1.148 +} while(0)
   1.149 +
   1.150 +#define DISEMBLE_RGB(buf, bpp, fmt, pixel, r, g, b)			   \
   1.151 +do {									   \
   1.152 +	switch (bpp) {							   \
   1.153 +		case 2:							   \
   1.154 +			pixel = *((Uint16 *)(buf));			   \
   1.155 +		break;							   \
   1.156 +									   \
   1.157 +		case 3: {						   \
   1.158 +		        Uint8 *B = (Uint8 *)buf;			   \
   1.159 +			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   1.160 +			        pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
   1.161 +			} else {					   \
   1.162 +			        pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
   1.163 +			}						   \
   1.164 +		}							   \
   1.165 +		break;							   \
   1.166 +									   \
   1.167 +		case 4:							   \
   1.168 +			pixel = *((Uint32 *)(buf));			   \
   1.169 +		break;							   \
   1.170 +									   \
   1.171 +	        default:						   \
   1.172 +		        pixel = 0;	/* prevent gcc from complaining */ \
   1.173 +		break;							   \
   1.174 +	}								   \
   1.175 +	RGB_FROM_PIXEL(pixel, fmt, r, g, b);				   \
   1.176 +} while(0)
   1.177 +
   1.178 +/* Assemble R-G-B values into a specified pixel format and store them */
   1.179 +#define PIXEL_FROM_RGB(pixel, fmt, r, g, b)				\
   1.180 +{									\
   1.181 +	pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   1.182 +		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   1.183 +		((b>>fmt->Bloss)<<fmt->Bshift);				\
   1.184 +}
   1.185 +#define RGB565_FROM_RGB(pixel, r, g, b)					\
   1.186 +{									\
   1.187 +	pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
   1.188 +}
   1.189 +#define RGB555_FROM_RGB(pixel, r, g, b)					\
   1.190 +{									\
   1.191 +	pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
   1.192 +}
   1.193 +#define RGB888_FROM_RGB(pixel, r, g, b)					\
   1.194 +{									\
   1.195 +	pixel = (r<<16)|(g<<8)|b;					\
   1.196 +}
   1.197 +#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
   1.198 +{									\
   1.199 +	switch (bpp) {							\
   1.200 +		case 2: {						\
   1.201 +			Uint16 pixel;					\
   1.202 +									\
   1.203 +			PIXEL_FROM_RGB(pixel, fmt, r, g, b);		\
   1.204 +			*((Uint16 *)(buf)) = pixel;			\
   1.205 +		}							\
   1.206 +		break;							\
   1.207 +									\
   1.208 +		case 3: {						\
   1.209 +                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   1.210 +			        *((buf)+fmt->Rshift/8) = r;		\
   1.211 +				*((buf)+fmt->Gshift/8) = g;		\
   1.212 +				*((buf)+fmt->Bshift/8) = b;		\
   1.213 +			} else {					\
   1.214 +			        *((buf)+2-fmt->Rshift/8) = r;		\
   1.215 +				*((buf)+2-fmt->Gshift/8) = g;		\
   1.216 +				*((buf)+2-fmt->Bshift/8) = b;		\
   1.217 +			}						\
   1.218 +		}							\
   1.219 +		break;							\
   1.220 +									\
   1.221 +		case 4: {						\
   1.222 +			Uint32 pixel;					\
   1.223 +									\
   1.224 +			PIXEL_FROM_RGB(pixel, fmt, r, g, b);		\
   1.225 +			*((Uint32 *)(buf)) = pixel;			\
   1.226 +		}							\
   1.227 +		break;							\
   1.228 +	}								\
   1.229 +}
   1.230 +#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
   1.231 +{									\
   1.232 +	switch (bpp) {							\
   1.233 +		case 2: {						\
   1.234 +			Uint16 *bufp;					\
   1.235 +			Uint16 pixel;					\
   1.236 +									\
   1.237 +			bufp = (Uint16 *)buf;				\
   1.238 +			PIXEL_FROM_RGB(pixel, fmt, r, g, b);		\
   1.239 +			*bufp = pixel | (*bufp & Amask);		\
   1.240 +		}							\
   1.241 +		break;							\
   1.242 +									\
   1.243 +		case 3: {						\
   1.244 +                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   1.245 +			        *((buf)+fmt->Rshift/8) = r;		\
   1.246 +				*((buf)+fmt->Gshift/8) = g;		\
   1.247 +				*((buf)+fmt->Bshift/8) = b;		\
   1.248 +			} else {					\
   1.249 +			        *((buf)+2-fmt->Rshift/8) = r;		\
   1.250 +				*((buf)+2-fmt->Gshift/8) = g;		\
   1.251 +				*((buf)+2-fmt->Bshift/8) = b;		\
   1.252 +			}						\
   1.253 +		}							\
   1.254 +		break;							\
   1.255 +									\
   1.256 +		case 4: {						\
   1.257 +			Uint32 *bufp;					\
   1.258 +			Uint32 pixel;					\
   1.259 +									\
   1.260 +			bufp = (Uint32 *)buf;				\
   1.261 +			PIXEL_FROM_RGB(pixel, fmt, r, g, b);		\
   1.262 +			*bufp = pixel | (*bufp & Amask);		\
   1.263 +		}							\
   1.264 +		break;							\
   1.265 +	}								\
   1.266 +}
   1.267 +
   1.268 +/* FIXME: Should we rescale alpha into 0..255 here? */
   1.269 +#define RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a)				\
   1.270 +{									\
   1.271 +	r = ((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
   1.272 +	g = ((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
   1.273 +	b = ((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
   1.274 +	a = ((pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
   1.275 +}
   1.276 +#define RGBA_FROM_8888(pixel, fmt, r, g, b, a)	\
   1.277 +{						\
   1.278 +	r = (pixel&fmt->Rmask)>>fmt->Rshift;	\
   1.279 +	g = (pixel&fmt->Gmask)>>fmt->Gshift;	\
   1.280 +	b = (pixel&fmt->Bmask)>>fmt->Bshift;	\
   1.281 +	a = (pixel&fmt->Amask)>>fmt->Ashift;	\
   1.282 +}
   1.283 +#define RGBA_FROM_RGBA8888(pixel, r, g, b, a)				\
   1.284 +{									\
   1.285 +	r = (pixel>>24);						\
   1.286 +	g = ((pixel>>16)&0xFF);						\
   1.287 +	b = ((pixel>>8)&0xFF);						\
   1.288 +	a = (pixel&0xFF);						\
   1.289 +}
   1.290 +#define RGBA_FROM_ARGB8888(pixel, r, g, b, a)				\
   1.291 +{									\
   1.292 +	r = ((pixel>>16)&0xFF);						\
   1.293 +	g = ((pixel>>8)&0xFF);						\
   1.294 +	b = (pixel&0xFF);						\
   1.295 +	a = (pixel>>24);						\
   1.296 +}
   1.297 +#define RGBA_FROM_ABGR8888(pixel, r, g, b, a)				\
   1.298 +{									\
   1.299 +	r = (pixel&0xFF);						\
   1.300 +	g = ((pixel>>8)&0xFF);						\
   1.301 +	b = ((pixel>>16)&0xFF);						\
   1.302 +	a = (pixel>>24);						\
   1.303 +}
   1.304 +#define DISEMBLE_RGBA(buf, bpp, fmt, pixel, r, g, b, a)			   \
   1.305 +do {									   \
   1.306 +	switch (bpp) {							   \
   1.307 +		case 2:							   \
   1.308 +			pixel = *((Uint16 *)(buf));			   \
   1.309 +		break;							   \
   1.310 +									   \
   1.311 +		case 3:	{/* FIXME: broken code (no alpha) */		   \
   1.312 +		        Uint8 *b = (Uint8 *)buf;			   \
   1.313 +			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
   1.314 +			        pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
   1.315 +			} else {					   \
   1.316 +			        pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
   1.317 +			}						   \
   1.318 +		}							   \
   1.319 +		break;							   \
   1.320 +									   \
   1.321 +		case 4:							   \
   1.322 +			pixel = *((Uint32 *)(buf));			   \
   1.323 +		break;							   \
   1.324 +									   \
   1.325 +		default:						   \
   1.326 +		        pixel = 0; /* stop gcc complaints */		   \
   1.327 +		break;							   \
   1.328 +	}								   \
   1.329 +	RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a);			   \
   1.330 +	pixel &= ~fmt->Amask;						   \
   1.331 +} while(0)
   1.332 +
   1.333 +/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
   1.334 +#define PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a)				\
   1.335 +{									\
   1.336 +	pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
   1.337 +		((g>>fmt->Gloss)<<fmt->Gshift)|				\
   1.338 +		((b>>fmt->Bloss)<<fmt->Bshift)|				\
   1.339 +		((a<<fmt->Aloss)<<fmt->Ashift);				\
   1.340 +}
   1.341 +#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
   1.342 +{									\
   1.343 +	switch (bpp) {							\
   1.344 +		case 2: {						\
   1.345 +			Uint16 pixel;					\
   1.346 +									\
   1.347 +			PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a);	\
   1.348 +			*((Uint16 *)(buf)) = pixel;			\
   1.349 +		}							\
   1.350 +		break;							\
   1.351 +									\
   1.352 +		case 3: { /* FIXME: broken code (no alpha) */		\
   1.353 +                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
   1.354 +			        *((buf)+fmt->Rshift/8) = r;		\
   1.355 +				*((buf)+fmt->Gshift/8) = g;		\
   1.356 +				*((buf)+fmt->Bshift/8) = b;		\
   1.357 +			} else {					\
   1.358 +			        *((buf)+2-fmt->Rshift/8) = r;		\
   1.359 +				*((buf)+2-fmt->Gshift/8) = g;		\
   1.360 +				*((buf)+2-fmt->Bshift/8) = b;		\
   1.361 +			}						\
   1.362 +		}							\
   1.363 +		break;							\
   1.364 +									\
   1.365 +		case 4: {						\
   1.366 +			Uint32 pixel;					\
   1.367 +									\
   1.368 +			PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a);	\
   1.369 +			*((Uint32 *)(buf)) = pixel;			\
   1.370 +		}							\
   1.371 +		break;							\
   1.372 +	}								\
   1.373 +}
   1.374 +
   1.375 +/* Blend the RGB values of two pixels based on a source alpha value */
   1.376 +#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
   1.377 +do {						\
   1.378 +	dR = (((sR-dR)*(A))>>8)+dR;		\
   1.379 +	dG = (((sG-dG)*(A))>>8)+dG;		\
   1.380 +	dB = (((sB-dB)*(A))>>8)+dB;		\
   1.381 +} while(0)
   1.382 +
   1.383 +/* This is a very useful loop for optimizing blitters */
   1.384 +#define USE_DUFFS_LOOP
   1.385 +#ifdef USE_DUFFS_LOOP
   1.386 +
   1.387 +/* 8-times unrolled loop */
   1.388 +#define DUFFS_LOOP8(pixel_copy_increment, width)			\
   1.389 +{ int n = (width+7)/8;							\
   1.390 +	switch (width % 8) {						\
   1.391 +	case 0: do {	pixel_copy_increment;				\
   1.392 +	case 7:		pixel_copy_increment;				\
   1.393 +	case 6:		pixel_copy_increment;				\
   1.394 +	case 5:		pixel_copy_increment;				\
   1.395 +	case 4:		pixel_copy_increment;				\
   1.396 +	case 3:		pixel_copy_increment;				\
   1.397 +	case 2:		pixel_copy_increment;				\
   1.398 +	case 1:		pixel_copy_increment;				\
   1.399 +		} while ( --n > 0 );					\
   1.400 +	}								\
   1.401 +}
   1.402 +
   1.403 +/* 4-times unrolled loop */
   1.404 +#define DUFFS_LOOP4(pixel_copy_increment, width)			\
   1.405 +{ int n = (width+3)/4;							\
   1.406 +	switch (width % 4) {						\
   1.407 +	case 0: do {	pixel_copy_increment;				\
   1.408 +	case 3:		pixel_copy_increment;				\
   1.409 +	case 2:		pixel_copy_increment;				\
   1.410 +	case 1:		pixel_copy_increment;				\
   1.411 +		} while ( --n > 0 );					\
   1.412 +	}								\
   1.413 +}
   1.414 +
   1.415 +/* Use the 8-times version of the loop by default */
   1.416 +#define DUFFS_LOOP(pixel_copy_increment, width)				\
   1.417 +	DUFFS_LOOP8(pixel_copy_increment, width)
   1.418 +
   1.419 +#else
   1.420 +
   1.421 +/* Don't use Duff's device to unroll loops */
   1.422 +#define DUFFS_LOOP(pixel_copy_increment, width)				\
   1.423 +{ int n;								\
   1.424 +	for ( n=width; n > 0; --n ) {					\
   1.425 +		pixel_copy_increment;					\
   1.426 +	}								\
   1.427 +}
   1.428 +#define DUFFS_LOOP8(pixel_copy_increment, width)			\
   1.429 +	DUFFS_LOOP(pixel_copy_increment, width)
   1.430 +#define DUFFS_LOOP4(pixel_copy_increment, width)			\
   1.431 +	DUFFS_LOOP(pixel_copy_increment, width)
   1.432 +
   1.433 +#endif /* USE_DUFFS_LOOP */
   1.434 +
   1.435 +/* Prevent Visual C++ 6.0 from printing out stupid warnings */
   1.436 +#if defined(_MSC_VER) && (_MSC_VER >= 600)
   1.437 +#pragma warning(disable: 4550)
   1.438 +#endif
   1.439 +
   1.440 +#endif /* _SDL_blit_h */