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