src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6166 0bc1f6da9c74
child 7191 75360622e65f
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6166
    95
    Uint32 dst_palette_version;
slouken@6166
    96
    Uint32 src_palette_version;
slouken@0
    97
} SDL_BlitMap;
slouken@0
    98
slouken@0
    99
/* Functions found in SDL_blit.c */
slouken@1895
   100
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   101
slouken@2267
   102
/* Functions found in SDL_blit_*.c */
slouken@2267
   103
extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
slouken@2267
   104
extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
slouken@2267
   105
extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
slouken@2267
   106
extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
slouken@2263
   107
slouken@0
   108
/*
slouken@0
   109
 * Useful macros for blitting routines
slouken@0
   110
 */
slouken@0
   111
slouken@2249
   112
#if defined(__GNUC__)
slouken@2249
   113
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   114
#elif defined(_MSC_VER)
slouken@2251
   115
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   116
#else
slouken@2249
   117
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   118
#endif
slouken@2249
   119
slouken@0
   120
/* Load pixel of the specified format from a buffer and get its R-G-B values */
icculus@1162
   121
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   122
{									\
slouken@5439
   123
	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@5439
   124
	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@5439
   125
	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@0
   126
}
icculus@1162
   127
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   128
{									\
slouken@5439
   129
	r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];		 			\
slouken@5439
   130
	g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)]; 					\
slouken@5439
   131
	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
slouken@0
   132
}
icculus@1162
   133
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   134
{									\
slouken@5439
   135
	r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];		 			\
slouken@5439
   136
	g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)]; 					\
slouken@5439
   137
	b = SDL_expand_byte[3][(Pixel&0x001F)]; 					\
slouken@0
   138
}
icculus@1162
   139
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   140
{									\
icculus@1162
   141
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   142
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   143
	b = (Pixel&0xFF);			 			\
slouken@0
   144
}
icculus@1162
   145
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   146
do {									   \
slouken@0
   147
	switch (bpp) {							   \
slouken@0
   148
		case 2:							   \
icculus@1162
   149
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   150
		break;							   \
slouken@0
   151
									   \
slouken@0
   152
		case 3: {						   \
slouken@0
   153
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@2824
   154
			if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   155
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   156
			} else {					   \
icculus@1162
   157
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   158
			}						   \
slouken@0
   159
		}							   \
slouken@0
   160
		break;							   \
slouken@0
   161
									   \
slouken@0
   162
		case 4:							   \
icculus@1162
   163
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   164
		break;							   \
slouken@0
   165
									   \
slouken@0
   166
		default:						   \
slouken@5426
   167
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   168
		break;							   \
slouken@0
   169
	}								   \
slouken@2824
   170
} while (0)
slouken@0
   171
icculus@1162
   172
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   173
do {									   \
slouken@0
   174
	switch (bpp) {							   \
slouken@0
   175
		case 2:							   \
icculus@1162
   176
			Pixel = *((Uint16 *)(buf));			   \
slouken@2824
   177
			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
slouken@0
   178
		break;							   \
slouken@0
   179
									   \
slouken@2824
   180
		case 3:	{						   \
slouken@5426
   181
            Pixel = 0;                  \
slouken@2824
   182
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
slouken@2824
   183
			        r = *((buf)+fmt->Rshift/8);		   \
slouken@2824
   184
				g = *((buf)+fmt->Gshift/8);		   \
slouken@2824
   185
				b = *((buf)+fmt->Bshift/8);		   \
slouken@0
   186
			} else {					   \
slouken@2824
   187
			        r = *((buf)+2-fmt->Rshift/8);		   \
slouken@2824
   188
				g = *((buf)+2-fmt->Gshift/8);		   \
slouken@2824
   189
				b = *((buf)+2-fmt->Bshift/8);		   \
slouken@0
   190
			}						   \
slouken@0
   191
		}							   \
slouken@0
   192
		break;							   \
slouken@0
   193
									   \
slouken@0
   194
		case 4:							   \
icculus@1162
   195
			Pixel = *((Uint32 *)(buf));			   \
slouken@2824
   196
			RGB_FROM_PIXEL(Pixel, fmt, r, g, b);		   \
slouken@0
   197
		break;							   \
slouken@0
   198
									   \
slouken@2824
   199
		default:						   \
slouken@5426
   200
                /* stop gcc complaints */		   \
slouken@5426
   201
		        Pixel = 0;              \
slouken@5426
   202
                r = g = b = 0;          \
slouken@0
   203
		break;							   \
slouken@0
   204
	}								   \
slouken@2824
   205
} while (0)
slouken@0
   206
slouken@0
   207
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   208
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   209
{									\
icculus@1162
   210
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   211
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@5440
   212
		((b>>fmt->Bloss)<<fmt->Bshift)| \
slouken@5439
   213
        fmt->Amask;				\
slouken@0
   214
}
icculus@1162
   215
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   216
{									\
icculus@1162
   217
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   218
}
icculus@1162
   219
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   220
{									\
icculus@1162
   221
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   222
}
icculus@1162
   223
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   224
{									\
icculus@1162
   225
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   226
}
slouken@2899
   227
#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@2899
   228
{									\
slouken@2899
   229
	Pixel = (a<<24)|(r<<16)|(g<<8)|b;				\
slouken@2899
   230
}
slouken@3054
   231
#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   232
{									\
slouken@3054
   233
	Pixel = (r<<24)|(g<<16)|(b<<8)|a;				\
slouken@3054
   234
}
slouken@3054
   235
#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   236
{									\
slouken@3054
   237
	Pixel = (a<<24)|(b<<16)|(g<<8)|r;				\
slouken@3054
   238
}
slouken@3054
   239
#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)				\
slouken@3054
   240
{									\
slouken@3054
   241
	Pixel = (b<<24)|(g<<16)|(r<<8)|a;				\
slouken@3054
   242
}
slouken@0
   243
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   244
{									\
slouken@0
   245
	switch (bpp) {							\
slouken@0
   246
		case 2: {						\
icculus@1162
   247
			Uint16 Pixel;					\
slouken@0
   248
									\
icculus@1162
   249
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
slouken@5439
   250
			*((Uint16 *)(buf)) = Pixel;		\
slouken@0
   251
		}							\
slouken@0
   252
		break;							\
slouken@0
   253
									\
slouken@0
   254
		case 3: {						\
slouken@2824
   255
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   256
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   257
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   258
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   259
			} else {					\
slouken@0
   260
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   261
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   262
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   263
			}						\
slouken@0
   264
		}							\
slouken@0
   265
		break;							\
slouken@0
   266
									\
slouken@0
   267
		case 4: {						\
icculus@1162
   268
			Uint32 Pixel;					\
slouken@0
   269
									\
icculus@1162
   270
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   271
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   272
		}							\
slouken@0
   273
		break;							\
slouken@0
   274
	}								\
slouken@0
   275
}
slouken@0
   276
slouken@0
   277
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   278
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   279
{									\
slouken@5439
   280
	r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@5439
   281
	g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@5439
   282
	b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@5439
   283
	a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
slouken@0
   284
}
icculus@1162
   285
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   286
{						\
icculus@1162
   287
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   288
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   289
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   290
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   291
}
icculus@1162
   292
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   293
{									\
icculus@1162
   294
	r = (Pixel>>24);						\
icculus@1162
   295
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   296
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   297
	a = (Pixel&0xFF);						\
slouken@0
   298
}
icculus@1162
   299
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   300
{									\
icculus@1162
   301
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   302
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   303
	b = (Pixel&0xFF);						\
icculus@1162
   304
	a = (Pixel>>24);						\
slouken@0
   305
}
icculus@1162
   306
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   307
{									\
icculus@1162
   308
	r = (Pixel&0xFF);						\
icculus@1162
   309
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   310
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   311
	a = (Pixel>>24);						\
slouken@0
   312
}
slouken@3054
   313
#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)				\
slouken@3054
   314
{									\
slouken@3054
   315
	r = ((Pixel>>8)&0xFF);						\
slouken@3054
   316
	g = ((Pixel>>16)&0xFF);						\
slouken@3054
   317
	b = (Pixel>>24);						\
slouken@3054
   318
	a = (Pixel&0xFF);						\
slouken@3054
   319
}
icculus@1162
   320
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   321
do {									   \
slouken@0
   322
	switch (bpp) {							   \
slouken@0
   323
		case 2:							   \
icculus@1162
   324
			Pixel = *((Uint16 *)(buf));			   \
slouken@2824
   325
			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
slouken@0
   326
		break;							   \
slouken@0
   327
									   \
slouken@2824
   328
		case 3:	{						   \
slouken@5426
   329
            Pixel = 0; \
slouken@2824
   330
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
slouken@2824
   331
			        r = *((buf)+fmt->Rshift/8);		   \
slouken@2824
   332
				g = *((buf)+fmt->Gshift/8);		   \
slouken@2824
   333
				b = *((buf)+fmt->Bshift/8);		   \
slouken@0
   334
			} else {					   \
slouken@2824
   335
			        r = *((buf)+2-fmt->Rshift/8);		   \
slouken@2824
   336
				g = *((buf)+2-fmt->Gshift/8);		   \
slouken@2824
   337
				b = *((buf)+2-fmt->Bshift/8);		   \
slouken@0
   338
			}						   \
slouken@2824
   339
			a = 0xFF;					   \
slouken@0
   340
		}							   \
slouken@0
   341
		break;							   \
slouken@0
   342
									   \
slouken@0
   343
		case 4:							   \
icculus@1162
   344
			Pixel = *((Uint32 *)(buf));			   \
slouken@2824
   345
			RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);	   \
slouken@0
   346
		break;							   \
slouken@0
   347
									   \
slouken@0
   348
		default:						   \
slouken@5426
   349
                /* stop gcc complaints */		   \
slouken@5426
   350
		        Pixel = 0;              \
slouken@5426
   351
                r = g = b = a = 0;      \
slouken@0
   352
		break;							   \
slouken@0
   353
	}								   \
slouken@2824
   354
} while (0)
slouken@0
   355
slouken@0
   356
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   357
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   358
{									\
icculus@1162
   359
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   360
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   361
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   362
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   363
}
slouken@0
   364
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   365
{									\
slouken@0
   366
	switch (bpp) {							\
slouken@0
   367
		case 2: {						\
icculus@1162
   368
			Uint16 Pixel;					\
slouken@0
   369
									\
icculus@1162
   370
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
slouken@5439
   371
			*((Uint16 *)(buf)) = Pixel;		\
slouken@0
   372
		}							\
slouken@0
   373
		break;							\
slouken@0
   374
									\
slouken@2824
   375
		case 3: {						\
slouken@2824
   376
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   377
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   378
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   379
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   380
			} else {					\
slouken@0
   381
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   382
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   383
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   384
			}						\
slouken@0
   385
		}							\
slouken@0
   386
		break;							\
slouken@0
   387
									\
slouken@0
   388
		case 4: {						\
icculus@1162
   389
			Uint32 Pixel;					\
slouken@0
   390
									\
icculus@1162
   391
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   392
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   393
		}							\
slouken@0
   394
		break;							\
slouken@0
   395
	}								\
slouken@0
   396
}
slouken@0
   397
icculus@1162
   398
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   399
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   400
do {						\
icculus@3631
   401
	dR = ((((int)(sR-dR)*(int)A)/255)+dR);	\
icculus@3631
   402
	dG = ((((int)(sG-dG)*(int)A)/255)+dG);	\
icculus@3631
   403
	dB = ((((int)(sB-dB)*(int)A)/255)+dB);	\
icculus@1047
   404
} while(0)
icculus@1047
   405
icculus@1047
   406
slouken@0
   407
/* This is a very useful loop for optimizing blitters */
slouken@553
   408
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   409
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   410
#else
slouken@0
   411
#define USE_DUFFS_LOOP
slouken@553
   412
#endif
slouken@0
   413
#ifdef USE_DUFFS_LOOP
slouken@0
   414
slouken@0
   415
/* 8-times unrolled loop */
slouken@0
   416
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   417
{ int n = (width+7)/8;							\
slouken@91
   418
	switch (width & 7) {						\
slouken@0
   419
	case 0: do {	pixel_copy_increment;				\
slouken@0
   420
	case 7:		pixel_copy_increment;				\
slouken@0
   421
	case 6:		pixel_copy_increment;				\
slouken@0
   422
	case 5:		pixel_copy_increment;				\
slouken@0
   423
	case 4:		pixel_copy_increment;				\
slouken@0
   424
	case 3:		pixel_copy_increment;				\
slouken@0
   425
	case 2:		pixel_copy_increment;				\
slouken@0
   426
	case 1:		pixel_copy_increment;				\
slouken@0
   427
		} while ( --n > 0 );					\
slouken@0
   428
	}								\
slouken@0
   429
}
slouken@0
   430
slouken@0
   431
/* 4-times unrolled loop */
slouken@0
   432
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   433
{ int n = (width+3)/4;							\
slouken@91
   434
	switch (width & 3) {						\
slouken@0
   435
	case 0: do {	pixel_copy_increment;				\
slouken@0
   436
	case 3:		pixel_copy_increment;				\
slouken@0
   437
	case 2:		pixel_copy_increment;				\
slouken@0
   438
	case 1:		pixel_copy_increment;				\
slouken@3035
   439
		} while (--n > 0);					\
slouken@689
   440
	}								\
slouken@689
   441
}
slouken@689
   442
slouken@0
   443
/* Use the 8-times version of the loop by default */
slouken@0
   444
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   445
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   446
slouken@3035
   447
/* Special version of Duff's device for even more optimization */
slouken@3035
   448
#define DUFFS_LOOP_124(pixel_copy_increment1,				\
slouken@3035
   449
                       pixel_copy_increment2,				\
slouken@3035
   450
                       pixel_copy_increment4, width)			\
slouken@3035
   451
{ int n = width;							\
slouken@3035
   452
	if (n & 1) {							\
slouken@3035
   453
		pixel_copy_increment1; n -= 1;				\
slouken@3035
   454
	}								\
slouken@3035
   455
	if (n & 2) {							\
slouken@3035
   456
		pixel_copy_increment2; n -= 2;				\
slouken@3035
   457
	}								\
slouken@3035
   458
	if (n) {							\
slouken@3035
   459
		n = (n+7)/ 8;						\
slouken@3035
   460
		switch (n & 4) {					\
slouken@3035
   461
		case 0: do {	pixel_copy_increment4;			\
slouken@3035
   462
		case 4:		pixel_copy_increment4;			\
slouken@3035
   463
			} while (--n > 0);				\
slouken@3035
   464
		}							\
slouken@3035
   465
	}								\
slouken@689
   466
}
slouken@689
   467
slouken@3035
   468
#else
slouken@689
   469
slouken@689
   470
/* Don't use Duff's device to unroll loops */
slouken@0
   471
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   472
{ int n;								\
slouken@0
   473
	for ( n=width; n > 0; --n ) {					\
slouken@0
   474
		pixel_copy_increment;					\
slouken@0
   475
	}								\
slouken@0
   476
}
slouken@0
   477
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   478
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   479
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   480
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@3035
   481
#define DUFFS_LOOP_124(pixel_copy_increment1,				\
slouken@3035
   482
                       pixel_copy_increment2,				\
slouken@3035
   483
                       pixel_copy_increment4, width)			\
slouken@3035
   484
	DUFFS_LOOP(pixel_copy_increment1, width)
slouken@0
   485
slouken@0
   486
#endif /* USE_DUFFS_LOOP */
slouken@0
   487
slouken@0
   488
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   489
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   490
#pragma warning(disable: 4550)
slouken@0
   491
#endif
slouken@0
   492
slouken@0
   493
#endif /* _SDL_blit_h */
slouken@2898
   494
slouken@1895
   495
/* vi: set ts=4 sw=4 expandtab: */