src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 Aug 2007 01:44:21 +0000
changeset 2266 e61ad15a205f
parent 2263 900c35d8e8fd
child 2267 c785543d1843
permissions -rw-r--r--
More work in progress integrating SDL_Surface and the new SDL_Texture API
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#ifndef _SDL_blit_h
slouken@0
    25
#define _SDL_blit_h
slouken@0
    26
slouken@2249
    27
#ifdef __MMX__
slouken@2249
    28
#include <mmintrin.h>
slouken@2249
    29
#endif
slouken@2261
    30
#ifdef __3dNOW__
slouken@2261
    31
#include <mm3dnow.h>
slouken@2261
    32
#endif
slouken@2249
    33
#ifdef __SSE__
slouken@2249
    34
#include <xmmintrin.h>
slouken@2249
    35
#endif
slouken@2262
    36
#ifdef __SSE2__
slouken@2262
    37
#include <emmintrin.h>
slouken@2262
    38
#endif
slouken@2249
    39
slouken@2250
    40
#include "SDL_cpuinfo.h"
slouken@0
    41
#include "SDL_endian.h"
slouken@0
    42
slouken@2262
    43
/* SDL blit copy flags */
slouken@2266
    44
#define SDL_COPY_MODULATE_COLOR     0x00000001
slouken@2266
    45
#define SDL_COPY_MODULATE_ALPHA     0x00000002
slouken@2266
    46
#define SDL_COPY_MASK               0x00000010
slouken@2266
    47
#define SDL_COPY_BLEND              0x00000020
slouken@2266
    48
#define SDL_COPY_ADD                0x00000040
slouken@2266
    49
#define SDL_COPY_MOD                0x00000080
slouken@2266
    50
#define SDL_COPY_COLORKEY           0x00000100
slouken@2266
    51
#define SDL_COPY_NEAREST            0x00000200
slouken@2266
    52
#define SDL_COPY_RLE_DESIRED        0x00001000
slouken@2266
    53
#define SDL_COPY_RLE_COLORKEY       0x00002000
slouken@2266
    54
#define SDL_COPY_RLE_ALPHAKEY       0x00004000
slouken@2262
    55
slouken@2262
    56
/* SDL blit CPU flags */
slouken@2266
    57
#define SDL_CPU_ANY                 0x00000000
slouken@2266
    58
#define SDL_CPU_MMX                 0x00000001
slouken@2266
    59
#define SDL_CPU_3DNOW               0x00000002
slouken@2266
    60
#define SDL_CPU_SSE                 0x00000004
slouken@2266
    61
#define SDL_CPU_SSE2                0x00000008
slouken@2266
    62
#define SDL_CPU_ALTIVEC_PREFETCH    0x00000010
slouken@2266
    63
#define SDL_CPU_ALTIVEC_NOPREFETCH  0x00000020
slouken@2262
    64
slouken@2262
    65
typedef struct {
slouken@2262
    66
    Uint8 *src;
slouken@2262
    67
    int src_w, src_h;
slouken@2262
    68
    int src_pitch;
slouken@2262
    69
    Uint8 *dst;
slouken@2262
    70
    int dst_w, dst_h;
slouken@2262
    71
    int dst_pitch;
slouken@2262
    72
    SDL_PixelFormat *src_fmt;
slouken@2262
    73
    SDL_PixelFormat *dst_fmt;
slouken@1895
    74
    Uint8 *table;
slouken@2262
    75
    int flags;
slouken@2262
    76
    Uint32 colorkey;
slouken@2262
    77
    Uint8 r, g, b, a;
slouken@0
    78
} SDL_BlitInfo;
slouken@0
    79
slouken@2262
    80
typedef void (SDLCALL * SDL_BlitFunc)(SDL_BlitInfo *info);
slouken@2262
    81
slouken@2262
    82
typedef struct {
slouken@2262
    83
    Uint32 src_format;
slouken@2262
    84
    Uint32 dst_format;
slouken@2262
    85
    int flags;
slouken@2262
    86
    int cpu;
slouken@2262
    87
    SDL_BlitFunc func;
slouken@2262
    88
} SDL_BlitFuncEntry;
slouken@0
    89
slouken@0
    90
/* Blit mapping definition */
slouken@1895
    91
typedef struct SDL_BlitMap
slouken@1895
    92
{
slouken@1895
    93
    SDL_Surface *dst;
slouken@1895
    94
    int identity;
slouken@2257
    95
    SDL_blit blit;
slouken@2257
    96
    void *data;
slouken@2262
    97
    SDL_BlitInfo info;
slouken@0
    98
slouken@1895
    99
    /* the version count matches the destination; mismatch indicates
slouken@1895
   100
       an invalid mapping */
slouken@1895
   101
    unsigned int format_version;
slouken@0
   102
} SDL_BlitMap;
slouken@0
   103
slouken@0
   104
/* Functions found in SDL_blit.c */
slouken@1895
   105
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   106
slouken@2263
   107
/* Blit function tables in SDL_blit_*.c */
slouken@2263
   108
extern SDL_BlitFuncEntry SDL_BlitFuncTable0[];
slouken@2263
   109
extern SDL_BlitFuncEntry SDL_BlitFuncTable1[];
slouken@2263
   110
extern SDL_BlitFuncEntry SDL_BlitFuncTableN[];
slouken@2263
   111
slouken@0
   112
/*
slouken@0
   113
 * Useful macros for blitting routines
slouken@0
   114
 */
slouken@0
   115
slouken@2249
   116
#if defined(__GNUC__)
slouken@2249
   117
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   118
#elif defined(_MSC_VER)
slouken@2251
   119
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   120
#else
slouken@2249
   121
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   122
#endif
slouken@2249
   123
slouken@0
   124
#define FORMAT_EQUAL(A, B)						\
slouken@0
   125
    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
slouken@0
   126
     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
slouken@0
   127
slouken@0
   128
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@0
   129
/* FIXME: rescale values to 0..255 here? */
icculus@1162
   130
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   131
{									\
icculus@1162
   132
	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
icculus@1162
   133
	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
icculus@1162
   134
	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
slouken@0
   135
}
icculus@1162
   136
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   137
{									\
icculus@1162
   138
	r = (((Pixel&0xF800)>>11)<<3);		 			\
icculus@1162
   139
	g = (((Pixel&0x07E0)>>5)<<2); 					\
icculus@1162
   140
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   141
}
icculus@1162
   142
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   143
{									\
icculus@1162
   144
	r = (((Pixel&0x7C00)>>10)<<3);		 			\
icculus@1162
   145
	g = (((Pixel&0x03E0)>>5)<<3); 					\
icculus@1162
   146
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   147
}
icculus@1162
   148
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   149
{									\
icculus@1162
   150
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   151
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   152
	b = (Pixel&0xFF);			 			\
slouken@0
   153
}
icculus@1162
   154
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   155
do {									   \
slouken@0
   156
	switch (bpp) {							   \
slouken@0
   157
		case 2:							   \
icculus@1162
   158
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   159
		break;							   \
slouken@0
   160
									   \
slouken@0
   161
		case 3: {						   \
slouken@0
   162
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@0
   163
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   164
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   165
			} else {					   \
icculus@1162
   166
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   167
			}						   \
slouken@0
   168
		}							   \
slouken@0
   169
		break;							   \
slouken@0
   170
									   \
slouken@0
   171
		case 4:							   \
icculus@1162
   172
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   173
		break;							   \
slouken@0
   174
									   \
slouken@0
   175
		default:						   \
icculus@1162
   176
			Pixel = 0; /* appease gcc */			   \
slouken@0
   177
		break;							   \
slouken@0
   178
	}								   \
slouken@0
   179
} while(0)
slouken@0
   180
icculus@1162
   181
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   182
do {									   \
slouken@0
   183
	switch (bpp) {							   \
slouken@0
   184
		case 2:							   \
icculus@1162
   185
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   186
		break;							   \
slouken@0
   187
									   \
slouken@0
   188
		case 3: {						   \
slouken@0
   189
		        Uint8 *B = (Uint8 *)buf;			   \
slouken@0
   190
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   191
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   192
			} else {					   \
icculus@1162
   193
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   194
			}						   \
slouken@0
   195
		}							   \
slouken@0
   196
		break;							   \
slouken@0
   197
									   \
slouken@0
   198
		case 4:							   \
icculus@1162
   199
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   200
		break;							   \
slouken@0
   201
									   \
slouken@0
   202
	        default:						   \
icculus@1162
   203
		        Pixel = 0;	/* prevent gcc from complaining */ \
slouken@0
   204
		break;							   \
slouken@0
   205
	}								   \
icculus@1162
   206
	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
slouken@0
   207
} while(0)
slouken@0
   208
slouken@0
   209
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   210
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   211
{									\
icculus@1162
   212
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   213
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   214
		((b>>fmt->Bloss)<<fmt->Bshift);				\
slouken@0
   215
}
icculus@1162
   216
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   217
{									\
icculus@1162
   218
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   219
}
icculus@1162
   220
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   221
{									\
icculus@1162
   222
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   223
}
icculus@1162
   224
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   225
{									\
icculus@1162
   226
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   227
}
slouken@0
   228
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   229
{									\
slouken@0
   230
	switch (bpp) {							\
slouken@0
   231
		case 2: {						\
icculus@1162
   232
			Uint16 Pixel;					\
slouken@0
   233
									\
icculus@1162
   234
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   235
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   236
		}							\
slouken@0
   237
		break;							\
slouken@0
   238
									\
slouken@0
   239
		case 3: {						\
slouken@0
   240
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   241
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   242
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   243
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   244
			} else {					\
slouken@0
   245
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   246
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   247
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   248
			}						\
slouken@0
   249
		}							\
slouken@0
   250
		break;							\
slouken@0
   251
									\
slouken@0
   252
		case 4: {						\
icculus@1162
   253
			Uint32 Pixel;					\
slouken@0
   254
									\
icculus@1162
   255
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   256
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   257
		}							\
slouken@0
   258
		break;							\
slouken@0
   259
	}								\
slouken@0
   260
}
slouken@0
   261
#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
slouken@0
   262
{									\
slouken@0
   263
	switch (bpp) {							\
slouken@0
   264
		case 2: {						\
slouken@0
   265
			Uint16 *bufp;					\
icculus@1162
   266
			Uint16 Pixel;					\
slouken@0
   267
									\
slouken@0
   268
			bufp = (Uint16 *)buf;				\
icculus@1162
   269
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   270
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   271
		}							\
slouken@0
   272
		break;							\
slouken@0
   273
									\
slouken@0
   274
		case 3: {						\
slouken@0
   275
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   276
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   277
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   278
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   279
			} else {					\
slouken@0
   280
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   281
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   282
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   283
			}						\
slouken@0
   284
		}							\
slouken@0
   285
		break;							\
slouken@0
   286
									\
slouken@0
   287
		case 4: {						\
slouken@0
   288
			Uint32 *bufp;					\
icculus@1162
   289
			Uint32 Pixel;					\
slouken@0
   290
									\
slouken@0
   291
			bufp = (Uint32 *)buf;				\
icculus@1162
   292
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   293
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   294
		}							\
slouken@0
   295
		break;							\
slouken@0
   296
	}								\
slouken@0
   297
}
slouken@0
   298
slouken@0
   299
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   300
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   301
{									\
icculus@1162
   302
	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
icculus@1162
   303
	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
icculus@1162
   304
	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
icculus@1162
   305
	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
slouken@0
   306
}
icculus@1162
   307
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   308
{						\
icculus@1162
   309
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   310
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   311
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   312
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   313
}
icculus@1162
   314
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   315
{									\
icculus@1162
   316
	r = (Pixel>>24);						\
icculus@1162
   317
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   318
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   319
	a = (Pixel&0xFF);						\
slouken@0
   320
}
icculus@1162
   321
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   322
{									\
icculus@1162
   323
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   324
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   325
	b = (Pixel&0xFF);						\
icculus@1162
   326
	a = (Pixel>>24);						\
slouken@0
   327
}
icculus@1162
   328
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   329
{									\
icculus@1162
   330
	r = (Pixel&0xFF);						\
icculus@1162
   331
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   332
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   333
	a = (Pixel>>24);						\
slouken@0
   334
}
icculus@1162
   335
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   336
do {									   \
slouken@0
   337
	switch (bpp) {							   \
slouken@0
   338
		case 2:							   \
icculus@1162
   339
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   340
		break;							   \
slouken@0
   341
									   \
slouken@0
   342
		case 3:	{/* FIXME: broken code (no alpha) */		   \
slouken@0
   343
		        Uint8 *b = (Uint8 *)buf;			   \
slouken@0
   344
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   345
			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
slouken@0
   346
			} else {					   \
icculus@1162
   347
			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
slouken@0
   348
			}						   \
slouken@0
   349
		}							   \
slouken@0
   350
		break;							   \
slouken@0
   351
									   \
slouken@0
   352
		case 4:							   \
icculus@1162
   353
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   354
		break;							   \
slouken@0
   355
									   \
slouken@0
   356
		default:						   \
icculus@1162
   357
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   358
		break;							   \
slouken@0
   359
	}								   \
icculus@1162
   360
	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
icculus@1162
   361
	Pixel &= ~fmt->Amask;						   \
slouken@0
   362
} while(0)
slouken@0
   363
slouken@0
   364
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   365
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   366
{									\
icculus@1162
   367
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   368
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   369
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   370
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   371
}
slouken@0
   372
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   373
{									\
slouken@0
   374
	switch (bpp) {							\
slouken@0
   375
		case 2: {						\
icculus@1162
   376
			Uint16 Pixel;					\
slouken@0
   377
									\
icculus@1162
   378
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   379
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   380
		}							\
slouken@0
   381
		break;							\
slouken@0
   382
									\
slouken@0
   383
		case 3: { /* FIXME: broken code (no alpha) */		\
slouken@0
   384
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   385
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   386
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   387
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   388
			} else {					\
slouken@0
   389
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   390
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   391
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   392
			}						\
slouken@0
   393
		}							\
slouken@0
   394
		break;							\
slouken@0
   395
									\
slouken@0
   396
		case 4: {						\
icculus@1162
   397
			Uint32 Pixel;					\
slouken@0
   398
									\
icculus@1162
   399
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   400
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   401
		}							\
slouken@0
   402
		break;							\
slouken@0
   403
	}								\
slouken@0
   404
}
slouken@0
   405
icculus@1162
   406
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   407
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   408
do {						\
slouken@0
   409
	dR = (((sR-dR)*(A))>>8)+dR;		\
slouken@0
   410
	dG = (((sG-dG)*(A))>>8)+dG;		\
slouken@0
   411
	dB = (((sB-dB)*(A))>>8)+dB;		\
slouken@0
   412
} while(0)
slouken@0
   413
icculus@1162
   414
/* Blend the RGB values of two Pixels based on a source alpha value */
icculus@1047
   415
#define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
icculus@1047
   416
do {						\
icculus@1047
   417
    unsigned tR, tG, tB, tA; \
icculus@1047
   418
    tA = 255 - sA; \
icculus@1047
   419
    tR = 1 + (sR * sA) + (dR * tA); \
icculus@1047
   420
    dR = (tR + (tR >> 8)) >> 8; \
icculus@1047
   421
    tG = 1 + (sG * sA) + (dG * tA); \
icculus@1047
   422
    dG = (tG + (tG >> 8)) >> 8; \
icculus@1047
   423
    tB = 1 + (sB * sA) + (dB * tA); \
icculus@1047
   424
    dB = (tB + (tB >> 8)) >> 8; \
icculus@1047
   425
} while(0)
icculus@1047
   426
icculus@1047
   427
slouken@0
   428
/* This is a very useful loop for optimizing blitters */
slouken@553
   429
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   430
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   431
#else
slouken@0
   432
#define USE_DUFFS_LOOP
slouken@553
   433
#endif
slouken@0
   434
#ifdef USE_DUFFS_LOOP
slouken@0
   435
slouken@0
   436
/* 8-times unrolled loop */
slouken@0
   437
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   438
{ int n = (width+7)/8;							\
slouken@91
   439
	switch (width & 7) {						\
slouken@0
   440
	case 0: do {	pixel_copy_increment;				\
slouken@0
   441
	case 7:		pixel_copy_increment;				\
slouken@0
   442
	case 6:		pixel_copy_increment;				\
slouken@0
   443
	case 5:		pixel_copy_increment;				\
slouken@0
   444
	case 4:		pixel_copy_increment;				\
slouken@0
   445
	case 3:		pixel_copy_increment;				\
slouken@0
   446
	case 2:		pixel_copy_increment;				\
slouken@0
   447
	case 1:		pixel_copy_increment;				\
slouken@0
   448
		} while ( --n > 0 );					\
slouken@0
   449
	}								\
slouken@0
   450
}
slouken@0
   451
slouken@0
   452
/* 4-times unrolled loop */
slouken@0
   453
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   454
{ int n = (width+3)/4;							\
slouken@91
   455
	switch (width & 3) {						\
slouken@0
   456
	case 0: do {	pixel_copy_increment;				\
slouken@0
   457
	case 3:		pixel_copy_increment;				\
slouken@0
   458
	case 2:		pixel_copy_increment;				\
slouken@0
   459
	case 1:		pixel_copy_increment;				\
slouken@0
   460
		} while ( --n > 0 );					\
slouken@0
   461
	}								\
slouken@0
   462
}
slouken@0
   463
slouken@689
   464
/* 2 - times unrolled loop */
slouken@689
   465
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   466
				double_pixel_copy_increment, width)	\
slouken@689
   467
{ int n, w = width;							\
slouken@689
   468
	if( w & 1 ) {							\
slouken@689
   469
	    pixel_copy_increment;					\
slouken@689
   470
	    w--;							\
slouken@689
   471
	}								\
slouken@689
   472
	if ( w > 0 )	{						\
slouken@689
   473
	    n = ( w + 2) / 4;						\
slouken@689
   474
	    switch( w & 2 ) {						\
slouken@689
   475
	    case 0: do {	double_pixel_copy_increment;		\
slouken@689
   476
	    case 2:		double_pixel_copy_increment;		\
slouken@689
   477
		    } while ( --n > 0 );					\
slouken@689
   478
	    }								\
slouken@689
   479
	}								\
slouken@689
   480
}
slouken@689
   481
slouken@689
   482
/* 2 - times unrolled loop 4 pixels */
slouken@689
   483
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   484
				double_pixel_copy_increment,		\
slouken@689
   485
				quatro_pixel_copy_increment, width)	\
slouken@689
   486
{ int n, w = width;								\
slouken@689
   487
        if(w & 1) {							\
slouken@689
   488
	  pixel_copy_increment;						\
slouken@689
   489
	  w--;								\
slouken@689
   490
	}								\
slouken@689
   491
	if(w & 2) {							\
slouken@689
   492
	  double_pixel_copy_increment;					\
slouken@689
   493
	  w -= 2;							\
slouken@689
   494
	}								\
slouken@689
   495
	if ( w > 0 ) {							\
slouken@689
   496
	    n = ( w + 7 ) / 8;						\
slouken@689
   497
	    switch( w & 4 ) {						\
slouken@689
   498
	    case 0: do {	quatro_pixel_copy_increment;		\
slouken@689
   499
	    case 4:		quatro_pixel_copy_increment;		\
slouken@689
   500
		    } while ( --n > 0 );					\
slouken@689
   501
	    }								\
slouken@689
   502
	}								\
slouken@689
   503
}
slouken@689
   504
slouken@0
   505
/* Use the 8-times version of the loop by default */
slouken@0
   506
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   507
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   508
slouken@0
   509
#else
slouken@0
   510
slouken@0
   511
/* Don't use Duff's device to unroll loops */
slouken@689
   512
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   513
			 double_pixel_copy_increment, width)		\
slouken@689
   514
{ int n = width;								\
slouken@689
   515
    if( n & 1 ) {							\
slouken@689
   516
	pixel_copy_increment;						\
slouken@689
   517
	n--;								\
slouken@689
   518
    }									\
slouken@689
   519
    n=n>>1;								\
slouken@689
   520
    for(; n > 0; --n) {   						\
slouken@689
   521
	double_pixel_copy_increment;					\
slouken@689
   522
    }									\
slouken@689
   523
}
slouken@689
   524
slouken@689
   525
/* Don't use Duff's device to unroll loops */
slouken@689
   526
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   527
				double_pixel_copy_increment,		\
slouken@689
   528
				quatro_pixel_copy_increment, width)	\
slouken@689
   529
{ int n = width;								\
slouken@689
   530
        if(n & 1) {							\
slouken@689
   531
	  pixel_copy_increment;						\
slouken@689
   532
	  n--;								\
slouken@689
   533
	}								\
slouken@689
   534
	if(n & 2) {							\
slouken@689
   535
	  double_pixel_copy_increment;					\
slouken@689
   536
	  n -= 2;							\
slouken@689
   537
	}								\
slouken@689
   538
	n=n>>2;								\
slouken@689
   539
	for(; n > 0; --n) {   						\
slouken@689
   540
	  quatro_pixel_copy_increment;					\
slouken@689
   541
        }								\
slouken@689
   542
}
slouken@689
   543
slouken@689
   544
/* Don't use Duff's device to unroll loops */
slouken@0
   545
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   546
{ int n;								\
slouken@0
   547
	for ( n=width; n > 0; --n ) {					\
slouken@0
   548
		pixel_copy_increment;					\
slouken@0
   549
	}								\
slouken@0
   550
}
slouken@0
   551
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   552
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   553
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   554
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   555
slouken@0
   556
#endif /* USE_DUFFS_LOOP */
slouken@0
   557
slouken@0
   558
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   559
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   560
#pragma warning(disable: 4550)
slouken@0
   561
#endif
slouken@0
   562
slouken@0
   563
#endif /* _SDL_blit_h */
slouken@1895
   564
/* vi: set ts=4 sw=4 expandtab: */