src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1312 c9b51268668f
child 1662 782fd950bd46
child 1895 c121d94672cb
child 3976 8582c6a5ca16
permissions -rw-r--r--
Use consistent identifiers for the various platforms we support.
Make sure every source file includes SDL_config.h, so the proper system
headers are chosen.
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@0
    27
#include "SDL_endian.h"
slouken@0
    28
slouken@0
    29
/* The structure passed to the low level blit functions */
slouken@0
    30
typedef struct {
slouken@0
    31
	Uint8 *s_pixels;
slouken@0
    32
	int s_width;
slouken@0
    33
	int s_height;
slouken@0
    34
	int s_skip;
slouken@0
    35
	Uint8 *d_pixels;
slouken@0
    36
	int d_width;
slouken@0
    37
	int d_height;
slouken@0
    38
	int d_skip;
slouken@0
    39
	void *aux_data;
slouken@0
    40
	SDL_PixelFormat *src;
slouken@0
    41
	Uint8 *table;
slouken@0
    42
	SDL_PixelFormat *dst;
slouken@0
    43
} SDL_BlitInfo;
slouken@0
    44
slouken@0
    45
/* The type definition for the low level blit functions */
slouken@0
    46
typedef void (*SDL_loblit)(SDL_BlitInfo *info);
slouken@0
    47
slouken@0
    48
/* This is the private info structure for software accelerated blits */
slouken@0
    49
struct private_swaccel {
slouken@0
    50
	SDL_loblit blit;
slouken@0
    51
	void *aux_data;
slouken@0
    52
};
slouken@0
    53
slouken@0
    54
/* Blit mapping definition */
slouken@0
    55
typedef struct SDL_BlitMap {
slouken@0
    56
	SDL_Surface *dst;
slouken@0
    57
	int identity;
slouken@0
    58
	Uint8 *table;
slouken@0
    59
	SDL_blit hw_blit;
slouken@0
    60
	SDL_blit sw_blit;
slouken@0
    61
	struct private_hwaccel *hw_data;
slouken@0
    62
	struct private_swaccel *sw_data;
slouken@0
    63
slouken@0
    64
	/* the version count matches the destination; mismatch indicates
slouken@0
    65
	   an invalid mapping */
slouken@0
    66
        unsigned int format_version;
slouken@0
    67
} SDL_BlitMap;
slouken@0
    68
slouken@0
    69
slouken@0
    70
/* Functions found in SDL_blit.c */
slouken@0
    71
extern int SDL_CalculateBlit(SDL_Surface *surface);
slouken@0
    72
slouken@0
    73
/* Functions found in SDL_blit_{0,1,N,A}.c */
slouken@0
    74
extern SDL_loblit SDL_CalculateBlit0(SDL_Surface *surface, int complex);
slouken@0
    75
extern SDL_loblit SDL_CalculateBlit1(SDL_Surface *surface, int complex);
slouken@0
    76
extern SDL_loblit SDL_CalculateBlitN(SDL_Surface *surface, int complex);
slouken@0
    77
extern SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int complex);
slouken@0
    78
slouken@0
    79
/*
slouken@0
    80
 * Useful macros for blitting routines
slouken@0
    81
 */
slouken@0
    82
slouken@0
    83
#define FORMAT_EQUAL(A, B)						\
slouken@0
    84
    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
slouken@0
    85
     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
slouken@0
    86
slouken@0
    87
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@0
    88
/* FIXME: rescale values to 0..255 here? */
icculus@1162
    89
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
    90
{									\
icculus@1162
    91
	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
icculus@1162
    92
	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
icculus@1162
    93
	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
slouken@0
    94
}
icculus@1162
    95
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
    96
{									\
icculus@1162
    97
	r = (((Pixel&0xF800)>>11)<<3);		 			\
icculus@1162
    98
	g = (((Pixel&0x07E0)>>5)<<2); 					\
icculus@1162
    99
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   100
}
icculus@1162
   101
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   102
{									\
icculus@1162
   103
	r = (((Pixel&0x7C00)>>10)<<3);		 			\
icculus@1162
   104
	g = (((Pixel&0x03E0)>>5)<<3); 					\
icculus@1162
   105
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   106
}
icculus@1162
   107
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   108
{									\
icculus@1162
   109
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   110
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   111
	b = (Pixel&0xFF);			 			\
slouken@0
   112
}
icculus@1162
   113
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   114
do {									   \
slouken@0
   115
	switch (bpp) {							   \
slouken@0
   116
		case 2:							   \
icculus@1162
   117
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   118
		break;							   \
slouken@0
   119
									   \
slouken@0
   120
		case 3: {						   \
slouken@0
   121
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@0
   122
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   123
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   124
			} else {					   \
icculus@1162
   125
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   126
			}						   \
slouken@0
   127
		}							   \
slouken@0
   128
		break;							   \
slouken@0
   129
									   \
slouken@0
   130
		case 4:							   \
icculus@1162
   131
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   132
		break;							   \
slouken@0
   133
									   \
slouken@0
   134
		default:						   \
icculus@1162
   135
			Pixel = 0; /* appease gcc */			   \
slouken@0
   136
		break;							   \
slouken@0
   137
	}								   \
slouken@0
   138
} while(0)
slouken@0
   139
icculus@1162
   140
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   141
do {									   \
slouken@0
   142
	switch (bpp) {							   \
slouken@0
   143
		case 2:							   \
icculus@1162
   144
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   145
		break;							   \
slouken@0
   146
									   \
slouken@0
   147
		case 3: {						   \
slouken@0
   148
		        Uint8 *B = (Uint8 *)buf;			   \
slouken@0
   149
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   150
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   151
			} else {					   \
icculus@1162
   152
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   153
			}						   \
slouken@0
   154
		}							   \
slouken@0
   155
		break;							   \
slouken@0
   156
									   \
slouken@0
   157
		case 4:							   \
icculus@1162
   158
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   159
		break;							   \
slouken@0
   160
									   \
slouken@0
   161
	        default:						   \
icculus@1162
   162
		        Pixel = 0;	/* prevent gcc from complaining */ \
slouken@0
   163
		break;							   \
slouken@0
   164
	}								   \
icculus@1162
   165
	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
slouken@0
   166
} while(0)
slouken@0
   167
slouken@0
   168
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   169
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   170
{									\
icculus@1162
   171
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   172
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   173
		((b>>fmt->Bloss)<<fmt->Bshift);				\
slouken@0
   174
}
icculus@1162
   175
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   176
{									\
icculus@1162
   177
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   178
}
icculus@1162
   179
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   180
{									\
icculus@1162
   181
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   182
}
icculus@1162
   183
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   184
{									\
icculus@1162
   185
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   186
}
slouken@0
   187
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   188
{									\
slouken@0
   189
	switch (bpp) {							\
slouken@0
   190
		case 2: {						\
icculus@1162
   191
			Uint16 Pixel;					\
slouken@0
   192
									\
icculus@1162
   193
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   194
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   195
		}							\
slouken@0
   196
		break;							\
slouken@0
   197
									\
slouken@0
   198
		case 3: {						\
slouken@0
   199
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   200
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   201
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   202
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   203
			} else {					\
slouken@0
   204
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   205
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   206
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   207
			}						\
slouken@0
   208
		}							\
slouken@0
   209
		break;							\
slouken@0
   210
									\
slouken@0
   211
		case 4: {						\
icculus@1162
   212
			Uint32 Pixel;					\
slouken@0
   213
									\
icculus@1162
   214
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   215
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   216
		}							\
slouken@0
   217
		break;							\
slouken@0
   218
	}								\
slouken@0
   219
}
slouken@0
   220
#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
slouken@0
   221
{									\
slouken@0
   222
	switch (bpp) {							\
slouken@0
   223
		case 2: {						\
slouken@0
   224
			Uint16 *bufp;					\
icculus@1162
   225
			Uint16 Pixel;					\
slouken@0
   226
									\
slouken@0
   227
			bufp = (Uint16 *)buf;				\
icculus@1162
   228
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   229
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   230
		}							\
slouken@0
   231
		break;							\
slouken@0
   232
									\
slouken@0
   233
		case 3: {						\
slouken@0
   234
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   235
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   236
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   237
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   238
			} else {					\
slouken@0
   239
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   240
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   241
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   242
			}						\
slouken@0
   243
		}							\
slouken@0
   244
		break;							\
slouken@0
   245
									\
slouken@0
   246
		case 4: {						\
slouken@0
   247
			Uint32 *bufp;					\
icculus@1162
   248
			Uint32 Pixel;					\
slouken@0
   249
									\
slouken@0
   250
			bufp = (Uint32 *)buf;				\
icculus@1162
   251
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   252
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   253
		}							\
slouken@0
   254
		break;							\
slouken@0
   255
	}								\
slouken@0
   256
}
slouken@0
   257
slouken@0
   258
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   259
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   260
{									\
icculus@1162
   261
	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
icculus@1162
   262
	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
icculus@1162
   263
	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
icculus@1162
   264
	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
slouken@0
   265
}
icculus@1162
   266
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   267
{						\
icculus@1162
   268
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   269
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   270
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   271
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   272
}
icculus@1162
   273
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   274
{									\
icculus@1162
   275
	r = (Pixel>>24);						\
icculus@1162
   276
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   277
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   278
	a = (Pixel&0xFF);						\
slouken@0
   279
}
icculus@1162
   280
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   281
{									\
icculus@1162
   282
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   283
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   284
	b = (Pixel&0xFF);						\
icculus@1162
   285
	a = (Pixel>>24);						\
slouken@0
   286
}
icculus@1162
   287
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   288
{									\
icculus@1162
   289
	r = (Pixel&0xFF);						\
icculus@1162
   290
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   291
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   292
	a = (Pixel>>24);						\
slouken@0
   293
}
icculus@1162
   294
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   295
do {									   \
slouken@0
   296
	switch (bpp) {							   \
slouken@0
   297
		case 2:							   \
icculus@1162
   298
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   299
		break;							   \
slouken@0
   300
									   \
slouken@0
   301
		case 3:	{/* FIXME: broken code (no alpha) */		   \
slouken@0
   302
		        Uint8 *b = (Uint8 *)buf;			   \
slouken@0
   303
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   304
			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
slouken@0
   305
			} else {					   \
icculus@1162
   306
			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
slouken@0
   307
			}						   \
slouken@0
   308
		}							   \
slouken@0
   309
		break;							   \
slouken@0
   310
									   \
slouken@0
   311
		case 4:							   \
icculus@1162
   312
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   313
		break;							   \
slouken@0
   314
									   \
slouken@0
   315
		default:						   \
icculus@1162
   316
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   317
		break;							   \
slouken@0
   318
	}								   \
icculus@1162
   319
	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
icculus@1162
   320
	Pixel &= ~fmt->Amask;						   \
slouken@0
   321
} while(0)
slouken@0
   322
slouken@0
   323
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   324
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   325
{									\
icculus@1162
   326
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   327
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   328
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   329
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   330
}
slouken@0
   331
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   332
{									\
slouken@0
   333
	switch (bpp) {							\
slouken@0
   334
		case 2: {						\
icculus@1162
   335
			Uint16 Pixel;					\
slouken@0
   336
									\
icculus@1162
   337
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   338
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   339
		}							\
slouken@0
   340
		break;							\
slouken@0
   341
									\
slouken@0
   342
		case 3: { /* FIXME: broken code (no alpha) */		\
slouken@0
   343
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   344
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   345
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   346
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   347
			} else {					\
slouken@0
   348
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   349
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   350
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   351
			}						\
slouken@0
   352
		}							\
slouken@0
   353
		break;							\
slouken@0
   354
									\
slouken@0
   355
		case 4: {						\
icculus@1162
   356
			Uint32 Pixel;					\
slouken@0
   357
									\
icculus@1162
   358
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   359
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   360
		}							\
slouken@0
   361
		break;							\
slouken@0
   362
	}								\
slouken@0
   363
}
slouken@0
   364
icculus@1162
   365
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   366
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   367
do {						\
slouken@0
   368
	dR = (((sR-dR)*(A))>>8)+dR;		\
slouken@0
   369
	dG = (((sG-dG)*(A))>>8)+dG;		\
slouken@0
   370
	dB = (((sB-dB)*(A))>>8)+dB;		\
slouken@0
   371
} while(0)
slouken@0
   372
icculus@1162
   373
/* Blend the RGB values of two Pixels based on a source alpha value */
icculus@1047
   374
#define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
icculus@1047
   375
do {						\
icculus@1047
   376
    unsigned tR, tG, tB, tA; \
icculus@1047
   377
    tA = 255 - sA; \
icculus@1047
   378
    tR = 1 + (sR * sA) + (dR * tA); \
icculus@1047
   379
    dR = (tR + (tR >> 8)) >> 8; \
icculus@1047
   380
    tG = 1 + (sG * sA) + (dG * tA); \
icculus@1047
   381
    dG = (tG + (tG >> 8)) >> 8; \
icculus@1047
   382
    tB = 1 + (sB * sA) + (dB * tA); \
icculus@1047
   383
    dB = (tB + (tB >> 8)) >> 8; \
icculus@1047
   384
} while(0)
icculus@1047
   385
icculus@1047
   386
slouken@0
   387
/* This is a very useful loop for optimizing blitters */
slouken@553
   388
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   389
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   390
#else
slouken@0
   391
#define USE_DUFFS_LOOP
slouken@553
   392
#endif
slouken@0
   393
#ifdef USE_DUFFS_LOOP
slouken@0
   394
slouken@0
   395
/* 8-times unrolled loop */
slouken@0
   396
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   397
{ int n = (width+7)/8;							\
slouken@91
   398
	switch (width & 7) {						\
slouken@0
   399
	case 0: do {	pixel_copy_increment;				\
slouken@0
   400
	case 7:		pixel_copy_increment;				\
slouken@0
   401
	case 6:		pixel_copy_increment;				\
slouken@0
   402
	case 5:		pixel_copy_increment;				\
slouken@0
   403
	case 4:		pixel_copy_increment;				\
slouken@0
   404
	case 3:		pixel_copy_increment;				\
slouken@0
   405
	case 2:		pixel_copy_increment;				\
slouken@0
   406
	case 1:		pixel_copy_increment;				\
slouken@0
   407
		} while ( --n > 0 );					\
slouken@0
   408
	}								\
slouken@0
   409
}
slouken@0
   410
slouken@0
   411
/* 4-times unrolled loop */
slouken@0
   412
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   413
{ int n = (width+3)/4;							\
slouken@91
   414
	switch (width & 3) {						\
slouken@0
   415
	case 0: do {	pixel_copy_increment;				\
slouken@0
   416
	case 3:		pixel_copy_increment;				\
slouken@0
   417
	case 2:		pixel_copy_increment;				\
slouken@0
   418
	case 1:		pixel_copy_increment;				\
slouken@0
   419
		} while ( --n > 0 );					\
slouken@0
   420
	}								\
slouken@0
   421
}
slouken@0
   422
slouken@689
   423
/* 2 - times unrolled loop */
slouken@689
   424
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   425
				double_pixel_copy_increment, width)	\
slouken@689
   426
{ int n, w = width;							\
slouken@689
   427
	if( w & 1 ) {							\
slouken@689
   428
	    pixel_copy_increment;					\
slouken@689
   429
	    w--;							\
slouken@689
   430
	}								\
slouken@689
   431
	if ( w > 0 )	{						\
slouken@689
   432
	    n = ( w + 2) / 4;						\
slouken@689
   433
	    switch( w & 2 ) {						\
slouken@689
   434
	    case 0: do {	double_pixel_copy_increment;		\
slouken@689
   435
	    case 2:		double_pixel_copy_increment;		\
slouken@689
   436
		    } while ( --n > 0 );					\
slouken@689
   437
	    }								\
slouken@689
   438
	}								\
slouken@689
   439
}
slouken@689
   440
slouken@689
   441
/* 2 - times unrolled loop 4 pixels */
slouken@689
   442
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   443
				double_pixel_copy_increment,		\
slouken@689
   444
				quatro_pixel_copy_increment, width)	\
slouken@689
   445
{ int n, w = width;								\
slouken@689
   446
        if(w & 1) {							\
slouken@689
   447
	  pixel_copy_increment;						\
slouken@689
   448
	  w--;								\
slouken@689
   449
	}								\
slouken@689
   450
	if(w & 2) {							\
slouken@689
   451
	  double_pixel_copy_increment;					\
slouken@689
   452
	  w -= 2;							\
slouken@689
   453
	}								\
slouken@689
   454
	if ( w > 0 ) {							\
slouken@689
   455
	    n = ( w + 7 ) / 8;						\
slouken@689
   456
	    switch( w & 4 ) {						\
slouken@689
   457
	    case 0: do {	quatro_pixel_copy_increment;		\
slouken@689
   458
	    case 4:		quatro_pixel_copy_increment;		\
slouken@689
   459
		    } while ( --n > 0 );					\
slouken@689
   460
	    }								\
slouken@689
   461
	}								\
slouken@689
   462
}
slouken@689
   463
slouken@0
   464
/* Use the 8-times version of the loop by default */
slouken@0
   465
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   466
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   467
slouken@0
   468
#else
slouken@0
   469
slouken@0
   470
/* Don't use Duff's device to unroll loops */
slouken@689
   471
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   472
			 double_pixel_copy_increment, width)		\
slouken@689
   473
{ int n = width;								\
slouken@689
   474
    if( n & 1 ) {							\
slouken@689
   475
	pixel_copy_increment;						\
slouken@689
   476
	n--;								\
slouken@689
   477
    }									\
slouken@689
   478
    n=n>>1;								\
slouken@689
   479
    for(; n > 0; --n) {   						\
slouken@689
   480
	double_pixel_copy_increment;					\
slouken@689
   481
    }									\
slouken@689
   482
}
slouken@689
   483
slouken@689
   484
/* Don't use Duff's device to unroll loops */
slouken@689
   485
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   486
				double_pixel_copy_increment,		\
slouken@689
   487
				quatro_pixel_copy_increment, width)	\
slouken@689
   488
{ int n = width;								\
slouken@689
   489
        if(n & 1) {							\
slouken@689
   490
	  pixel_copy_increment;						\
slouken@689
   491
	  n--;								\
slouken@689
   492
	}								\
slouken@689
   493
	if(n & 2) {							\
slouken@689
   494
	  double_pixel_copy_increment;					\
slouken@689
   495
	  n -= 2;							\
slouken@689
   496
	}								\
slouken@689
   497
	n=n>>2;								\
slouken@689
   498
	for(; n > 0; --n) {   						\
slouken@689
   499
	  quatro_pixel_copy_increment;					\
slouken@689
   500
        }								\
slouken@689
   501
}
slouken@689
   502
slouken@689
   503
/* Don't use Duff's device to unroll loops */
slouken@0
   504
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   505
{ int n;								\
slouken@0
   506
	for ( n=width; n > 0; --n ) {					\
slouken@0
   507
		pixel_copy_increment;					\
slouken@0
   508
	}								\
slouken@0
   509
}
slouken@0
   510
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   511
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   512
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   513
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   514
slouken@0
   515
#endif /* USE_DUFFS_LOOP */
slouken@0
   516
slouken@0
   517
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   518
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   519
#pragma warning(disable: 4550)
slouken@0
   520
#endif
slouken@0
   521
slouken@0
   522
#endif /* _SDL_blit_h */