src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5440 810b064d41fe
child 6138 4c64952a58fb
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
#ifndef _SDL_blit_h
slouken@0
    24
#define _SDL_blit_h
slouken@0
    25
slouken@2250
    26
#include "SDL_cpuinfo.h"
slouken@0
    27
#include "SDL_endian.h"
slouken@5163
    28
#include "SDL_surface.h"
slouken@0
    29
slouken@5439
    30
/* Table to do pixel byte expansion */
slouken@5439
    31
extern Uint8* SDL_expand_byte[9];
slouken@5439
    32
slouken@2262
    33
/* SDL blit copy flags */
slouken@2266
    34
#define SDL_COPY_MODULATE_COLOR     0x00000001
slouken@2266
    35
#define SDL_COPY_MODULATE_ALPHA     0x00000002
slouken@5140
    36
#define SDL_COPY_BLEND              0x00000010
slouken@5140
    37
#define SDL_COPY_ADD                0x00000020
slouken@5184
    38
#define SDL_COPY_MOD                0x00000040
slouken@2266
    39
#define SDL_COPY_COLORKEY           0x00000100
slouken@2266
    40
#define SDL_COPY_NEAREST            0x00000200
slouken@2266
    41
#define SDL_COPY_RLE_DESIRED        0x00001000
slouken@2266
    42
#define SDL_COPY_RLE_COLORKEY       0x00002000
slouken@2266
    43
#define SDL_COPY_RLE_ALPHAKEY       0x00004000
slouken@2853
    44
#define SDL_COPY_RLE_MASK           (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY)
slouken@2262
    45
slouken@2262
    46
/* SDL blit CPU flags */
slouken@2266
    47
#define SDL_CPU_ANY                 0x00000000
slouken@2266
    48
#define SDL_CPU_MMX                 0x00000001
slouken@5389
    49
#define SDL_CPU_3DNOW               0x00000002
slouken@2266
    50
#define SDL_CPU_SSE                 0x00000004
slouken@2266
    51
#define SDL_CPU_SSE2                0x00000008
slouken@5389
    52
#define SDL_CPU_ALTIVEC_PREFETCH    0x00000010
slouken@5389
    53
#define SDL_CPU_ALTIVEC_NOPREFETCH  0x00000020
slouken@2262
    54
slouken@2267
    55
typedef struct
slouken@2267
    56
{
slouken@2262
    57
    Uint8 *src;
slouken@2262
    58
    int src_w, src_h;
slouken@2262
    59
    int src_pitch;
slouken@2267
    60
    int src_skip;
slouken@2262
    61
    Uint8 *dst;
slouken@2262
    62
    int dst_w, dst_h;
slouken@2262
    63
    int dst_pitch;
slouken@2267
    64
    int dst_skip;
slouken@2262
    65
    SDL_PixelFormat *src_fmt;
slouken@2262
    66
    SDL_PixelFormat *dst_fmt;
slouken@1895
    67
    Uint8 *table;
slouken@2262
    68
    int flags;
slouken@2262
    69
    Uint32 colorkey;
slouken@2262
    70
    Uint8 r, g, b, a;
slouken@0
    71
} SDL_BlitInfo;
slouken@0
    72
slouken@2267
    73
typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
slouken@2262
    74
slouken@2267
    75
typedef struct
slouken@2267
    76
{
slouken@2262
    77
    Uint32 src_format;
slouken@2262
    78
    Uint32 dst_format;
slouken@2262
    79
    int flags;
slouken@2262
    80
    int cpu;
slouken@2262
    81
    SDL_BlitFunc func;
slouken@2262
    82
} SDL_BlitFuncEntry;
slouken@0
    83
slouken@0
    84
/* Blit mapping definition */
slouken@1895
    85
typedef struct SDL_BlitMap
slouken@1895
    86
{
slouken@1895
    87
    SDL_Surface *dst;
slouken@1895
    88
    int identity;
slouken@2257
    89
    SDL_blit blit;
slouken@2257
    90
    void *data;
slouken@2262
    91
    SDL_BlitInfo info;
slouken@0
    92
slouken@1895
    93
    /* the version count matches the destination; mismatch indicates
slouken@1895
    94
       an invalid mapping */
slouken@5288
    95
    Uint32 palette_version;
slouken@0
    96
} SDL_BlitMap;
slouken@0
    97
slouken@0
    98
/* Functions found in SDL_blit.c */
slouken@1895
    99
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   100
slouken@2267
   101
/* Functions found in SDL_blit_*.c */
slouken@2267
   102
extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
slouken@2267
   103
extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
slouken@2267
   104
extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
slouken@2267
   105
extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
slouken@2263
   106
slouken@0
   107
/*
slouken@0
   108
 * Useful macros for blitting routines
slouken@0
   109
 */
slouken@0
   110
slouken@2249
   111
#if defined(__GNUC__)
slouken@2249
   112
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   113
#elif defined(_MSC_VER)
slouken@2251
   114
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   115
#else
slouken@2249
   116
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   117
#endif
slouken@2249
   118
slouken@0
   119
/* Load pixel of the specified format from a buffer and get its R-G-B values */
icculus@1162
   120
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   121
{									\
slouken@5439
   122
	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@5439
   123
	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@5439
   124
	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@0
   125
}
icculus@1162
   126
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   127
{									\
slouken@5439
   128
	r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];		 			\
slouken@5439
   129
	g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; 					\
slouken@5439
   130
	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
slouken@0
   131
}
icculus@1162
   132
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   133
{									\
slouken@5439
   134
	r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];		 			\
slouken@5439
   135
	g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; 					\
slouken@5439
   136
	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
slouken@0
   137
}
icculus@1162
   138
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   139
{									\
icculus@1162
   140
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   141
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   142
	b = (Pixel&0xFF);			 			\
slouken@0
   143
}
icculus@1162
   144
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   145
do {									   \
slouken@0
   146
	switch (bpp) {							   \
slouken@0
   147
		case 2:							   \
icculus@1162
   148
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   149
		break;							   \
slouken@0
   150
									   \
slouken@0
   151
		case 3: {						   \
slouken@0
   152
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@2824
   153
			if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   154
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   155
			} else {					   \
icculus@1162
   156
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   157
			}						   \
slouken@0
   158
		}							   \
slouken@0
   159
		break;							   \
slouken@0
   160
									   \
slouken@0
   161
		case 4:							   \
icculus@1162
   162
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   163
		break;							   \
slouken@0
   164
									   \
slouken@0
   165
		default:						   \
slouken@5426
   166
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   167
		break;							   \
slouken@0
   168
	}								   \
slouken@2824
   169
} while (0)
slouken@0
   170
icculus@1162
   171
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   172
do {									   \
slouken@0
   173
	switch (bpp) {							   \
slouken@0
   174
		case 2:							   \
icculus@1162
   175
			Pixel = *((Uint16 *)(buf));			   \
slouken@2824
   176
			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
slouken@0
   177
		break;							   \
slouken@0
   178
									   \
slouken@2824
   179
		case 3:	{						   \
slouken@5426
   180
            Pixel = 0;                  \
slouken@2824
   181
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
slouken@2824
   182
			        r = *((buf)+fmt->Rshift/8);		   \
slouken@2824
   183
				g = *((buf)+fmt->Gshift/8);		   \
slouken@2824
   184
				b = *((buf)+fmt->Bshift/8);		   \
slouken@0
   185
			} else {					   \
slouken@2824
   186
			        r = *((buf)+2-fmt->Rshift/8);		   \
slouken@2824
   187
				g = *((buf)+2-fmt->Gshift/8);		   \
slouken@2824
   188
				b = *((buf)+2-fmt->Bshift/8);		   \
slouken@0
   189
			}						   \
slouken@0
   190
		}							   \
slouken@0
   191
		break;							   \
slouken@0
   192
									   \
slouken@0
   193
		case 4:							   \
icculus@1162
   194
			Pixel = *((Uint32 *)(buf));			   \
slouken@2824
   195
			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
slouken@0
   196
		break;							   \
slouken@0
   197
									   \
slouken@2824
   198
		default:						   \
slouken@5426
   199
                /* stop gcc complaints */		   \
slouken@5426
   200
		        Pixel = 0;              \
slouken@5426
   201
                r = g = b = 0;          \
slouken@0
   202
		break;							   \
slouken@0
   203
	}								   \
slouken@2824
   204
} while (0)
slouken@0
   205
slouken@0
   206
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   207
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   208
{									\
icculus@1162
   209
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   210
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@5440
   211
		((b>>fmt->Bloss)<<fmt->Bshift)| \
slouken@5439
   212
        fmt->Amask;				\
slouken@0
   213
}
icculus@1162
   214
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   215
{									\
icculus@1162
   216
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   217
}
icculus@1162
   218
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   219
{									\
icculus@1162
   220
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   221
}
icculus@1162
   222
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   223
{									\
icculus@1162
   224
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   225
}
slouken@2899
   226
#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@2899
   227
{									\
slouken@2899
   228
	Pixel = (a<<24)|(r<<16)|(g<<8)|b;				\
slouken@2899
   229
}
slouken@3054
   230
#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   231
{									\
slouken@3054
   232
	Pixel = (r<<24)|(g<<16)|(b<<8)|a;				\
slouken@3054
   233
}
slouken@3054
   234
#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   235
{									\
slouken@3054
   236
	Pixel = (a<<24)|(b<<16)|(g<<8)|r;				\
slouken@3054
   237
}
slouken@3054
   238
#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   239
{									\
slouken@3054
   240
	Pixel = (b<<24)|(g<<16)|(r<<8)|a;				\
slouken@3054
   241
}
slouken@0
   242
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   243
{									\
slouken@0
   244
	switch (bpp) {							\
slouken@0
   245
		case 2: {						\
icculus@1162
   246
			Uint16 Pixel;					\
slouken@0
   247
									\
icculus@1162
   248
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
slouken@5439
   249
			*((Uint16 *)(buf)) = Pixel;		\
slouken@0
   250
		}							\
slouken@0
   251
		break;							\
slouken@0
   252
									\
slouken@0
   253
		case 3: {						\
slouken@2824
   254
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   255
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   256
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   257
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   258
			} else {					\
slouken@0
   259
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   260
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   261
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   262
			}						\
slouken@0
   263
		}							\
slouken@0
   264
		break;							\
slouken@0
   265
									\
slouken@0
   266
		case 4: {						\
icculus@1162
   267
			Uint32 Pixel;					\
slouken@0
   268
									\
icculus@1162
   269
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   270
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   271
		}							\
slouken@0
   272
		break;							\
slouken@0
   273
	}								\
slouken@0
   274
}
slouken@0
   275
slouken@0
   276
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   277
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   278
{									\
slouken@5439
   279
	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@5439
   280
	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@5439
   281
	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@5439
   282
	a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
slouken@0
   283
}
icculus@1162
   284
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   285
{						\
icculus@1162
   286
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   287
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   288
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   289
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   290
}
icculus@1162
   291
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   292
{									\
icculus@1162
   293
	r = (Pixel>>24);						\
icculus@1162
   294
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   295
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   296
	a = (Pixel&0xFF);						\
slouken@0
   297
}
icculus@1162
   298
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   299
{									\
icculus@1162
   300
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   301
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   302
	b = (Pixel&0xFF);						\
icculus@1162
   303
	a = (Pixel>>24);						\
slouken@0
   304
}
icculus@1162
   305
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   306
{									\
icculus@1162
   307
	r = (Pixel&0xFF);						\
icculus@1162
   308
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   309
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   310
	a = (Pixel>>24);						\
slouken@0
   311
}
slouken@3054
   312
#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)				\
slouken@3054
   313
{									\
slouken@3054
   314
	r = ((Pixel>>8)&0xFF);						\
slouken@3054
   315
	g = ((Pixel>>16)&0xFF);						\
slouken@3054
   316
	b = (Pixel>>24);						\
slouken@3054
   317
	a = (Pixel&0xFF);						\
slouken@3054
   318
}
icculus@1162
   319
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   320
do {									   \
slouken@0
   321
	switch (bpp) {							   \
slouken@0
   322
		case 2:							   \
icculus@1162
   323
			Pixel = *((Uint16 *)(buf));			   \
slouken@2824
   324
			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
slouken@0
   325
		break;							   \
slouken@0
   326
									   \
slouken@2824
   327
		case 3:	{						   \
slouken@5426
   328
            Pixel = 0; \
slouken@2824
   329
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
slouken@2824
   330
			        r = *((buf)+fmt->Rshift/8);		   \
slouken@2824
   331
				g = *((buf)+fmt->Gshift/8);		   \
slouken@2824
   332
				b = *((buf)+fmt->Bshift/8);		   \
slouken@0
   333
			} else {					   \
slouken@2824
   334
			        r = *((buf)+2-fmt->Rshift/8);		   \
slouken@2824
   335
				g = *((buf)+2-fmt->Gshift/8);		   \
slouken@2824
   336
				b = *((buf)+2-fmt->Bshift/8);		   \
slouken@0
   337
			}						   \
slouken@2824
   338
			a = 0xFF;					   \
slouken@0
   339
		}							   \
slouken@0
   340
		break;							   \
slouken@0
   341
									   \
slouken@0
   342
		case 4:							   \
icculus@1162
   343
			Pixel = *((Uint32 *)(buf));			   \
slouken@2824
   344
			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
slouken@0
   345
		break;							   \
slouken@0
   346
									   \
slouken@0
   347
		default:						   \
slouken@5426
   348
                /* stop gcc complaints */		   \
slouken@5426
   349
		        Pixel = 0;              \
slouken@5426
   350
                r = g = b = a = 0;      \
slouken@0
   351
		break;							   \
slouken@0
   352
	}								   \
slouken@2824
   353
} while (0)
slouken@0
   354
slouken@0
   355
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   356
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   357
{									\
icculus@1162
   358
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   359
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   360
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   361
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   362
}
slouken@0
   363
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   364
{									\
slouken@0
   365
	switch (bpp) {							\
slouken@0
   366
		case 2: {						\
icculus@1162
   367
			Uint16 Pixel;					\
slouken@0
   368
									\
icculus@1162
   369
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
slouken@5439
   370
			*((Uint16 *)(buf)) = Pixel;		\
slouken@0
   371
		}							\
slouken@0
   372
		break;							\
slouken@0
   373
									\
slouken@2824
   374
		case 3: {						\
slouken@2824
   375
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   376
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   377
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   378
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   379
			} else {					\
slouken@0
   380
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   381
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   382
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   383
			}						\
slouken@0
   384
		}							\
slouken@0
   385
		break;							\
slouken@0
   386
									\
slouken@0
   387
		case 4: {						\
icculus@1162
   388
			Uint32 Pixel;					\
slouken@0
   389
									\
icculus@1162
   390
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   391
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   392
		}							\
slouken@0
   393
		break;							\
slouken@0
   394
	}								\
slouken@0
   395
}
slouken@0
   396
icculus@1162
   397
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   398
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   399
do {						\
icculus@3631
   400
	dR = ((((int)(sR-dR)*(int)A)/255)+dR);	\
icculus@3631
   401
	dG = ((((int)(sG-dG)*(int)A)/255)+dG);	\
icculus@3631
   402
	dB = ((((int)(sB-dB)*(int)A)/255)+dB);	\
icculus@1047
   403
} while(0)
icculus@1047
   404
icculus@1047
   405
slouken@0
   406
/* This is a very useful loop for optimizing blitters */
slouken@553
   407
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   408
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   409
#else
slouken@0
   410
#define USE_DUFFS_LOOP
slouken@553
   411
#endif
slouken@0
   412
#ifdef USE_DUFFS_LOOP
slouken@0
   413
slouken@0
   414
/* 8-times unrolled loop */
slouken@0
   415
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   416
{ int n = (width+7)/8;							\
slouken@91
   417
	switch (width & 7) {						\
slouken@0
   418
	case 0: do {	pixel_copy_increment;				\
slouken@0
   419
	case 7:		pixel_copy_increment;				\
slouken@0
   420
	case 6:		pixel_copy_increment;				\
slouken@0
   421
	case 5:		pixel_copy_increment;				\
slouken@0
   422
	case 4:		pixel_copy_increment;				\
slouken@0
   423
	case 3:		pixel_copy_increment;				\
slouken@0
   424
	case 2:		pixel_copy_increment;				\
slouken@0
   425
	case 1:		pixel_copy_increment;				\
slouken@0
   426
		} while ( --n > 0 );					\
slouken@0
   427
	}								\
slouken@0
   428
}
slouken@0
   429
slouken@0
   430
/* 4-times unrolled loop */
slouken@0
   431
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   432
{ int n = (width+3)/4;							\
slouken@91
   433
	switch (width & 3) {						\
slouken@0
   434
	case 0: do {	pixel_copy_increment;				\
slouken@0
   435
	case 3:		pixel_copy_increment;				\
slouken@0
   436
	case 2:		pixel_copy_increment;				\
slouken@0
   437
	case 1:		pixel_copy_increment;				\
slouken@3035
   438
		} while (--n > 0);					\
slouken@689
   439
	}								\
slouken@689
   440
}
slouken@689
   441
slouken@0
   442
/* Use the 8-times version of the loop by default */
slouken@0
   443
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   444
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   445
slouken@3035
   446
/* Special version of Duff's device for even more optimization */
slouken@3035
   447
#define DUFFS_LOOP_124(pixel_copy_increment1,				\
slouken@3035
   448
                       pixel_copy_increment2,				\
slouken@3035
   449
                       pixel_copy_increment4, width)			\
slouken@3035
   450
{ int n = width;							\
slouken@3035
   451
	if (n & 1) {							\
slouken@3035
   452
		pixel_copy_increment1; n -= 1;				\
slouken@3035
   453
	}								\
slouken@3035
   454
	if (n & 2) {							\
slouken@3035
   455
		pixel_copy_increment2; n -= 2;				\
slouken@3035
   456
	}								\
slouken@3035
   457
	if (n) {							\
slouken@3035
   458
		n = (n+7)/ 8;						\
slouken@3035
   459
		switch (n & 4) {					\
slouken@3035
   460
		case 0: do {	pixel_copy_increment4;			\
slouken@3035
   461
		case 4:		pixel_copy_increment4;			\
slouken@3035
   462
			} while (--n > 0);				\
slouken@3035
   463
		}							\
slouken@3035
   464
	}								\
slouken@689
   465
}
slouken@689
   466
slouken@3035
   467
#else
slouken@689
   468
slouken@689
   469
/* Don't use Duff's device to unroll loops */
slouken@0
   470
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   471
{ int n;								\
slouken@0
   472
	for ( n=width; n > 0; --n ) {					\
slouken@0
   473
		pixel_copy_increment;					\
slouken@0
   474
	}								\
slouken@0
   475
}
slouken@0
   476
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   477
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   478
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   479
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@3035
   480
#define DUFFS_LOOP_124(pixel_copy_increment1,				\
slouken@3035
   481
                       pixel_copy_increment2,				\
slouken@3035
   482
                       pixel_copy_increment4, width)			\
slouken@3035
   483
	DUFFS_LOOP(pixel_copy_increment1, width)
slouken@0
   484
slouken@0
   485
#endif /* USE_DUFFS_LOOP */
slouken@0
   486
slouken@0
   487
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   488
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   489
#pragma warning(disable: 4550)
slouken@0
   490
#endif
slouken@0
   491
slouken@0
   492
#endif /* _SDL_blit_h */
slouken@2898
   493
slouken@1895
   494
/* vi: set ts=4 sw=4 expandtab: */