src/video/SDL_blit_0.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Feb 2006 08:46:50 +0000
changeset 1402 d910939febfa
parent 1358 c71e05b4dc2e
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
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
#include "SDL_video.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
slouken@0
    27
/* Functions to blit from bitmaps to other surfaces */
slouken@0
    28
slouken@0
    29
static void BlitBto1(SDL_BlitInfo *info)
slouken@0
    30
{
slouken@0
    31
	int c;
slouken@0
    32
	int width, height;
slouken@0
    33
	Uint8 *src, *map, *dst;
slouken@0
    34
	int srcskip, dstskip;
slouken@0
    35
slouken@0
    36
	/* Set up some basic variables */
slouken@0
    37
	width = info->d_width;
slouken@0
    38
	height = info->d_height;
slouken@0
    39
	src = info->s_pixels;
slouken@0
    40
	srcskip = info->s_skip;
slouken@0
    41
	dst = info->d_pixels;
slouken@0
    42
	dstskip = info->d_skip;
slouken@0
    43
	map = info->table;
slouken@0
    44
	srcskip += width-(width+7)/8;
slouken@0
    45
slouken@0
    46
	if ( map ) {
slouken@0
    47
		while ( height-- ) {
slouken@0
    48
		        Uint8 byte = 0, bit;
slouken@0
    49
	    		for ( c=0; c<width; ++c ) {
slouken@91
    50
				if ( (c&7) == 0 ) {
slouken@0
    51
					byte = *src++;
slouken@0
    52
				}
slouken@0
    53
				bit = (byte&0x80)>>7;
slouken@0
    54
				if ( 1 ) {
slouken@0
    55
				  *dst = map[bit];
slouken@0
    56
				}
slouken@0
    57
				dst++;
slouken@0
    58
				byte <<= 1;
slouken@0
    59
			}
slouken@0
    60
			src += srcskip;
slouken@0
    61
			dst += dstskip;
slouken@0
    62
		}
slouken@0
    63
	} else {
slouken@0
    64
		while ( height-- ) {
slouken@0
    65
		        Uint8 byte = 0, bit;
slouken@0
    66
	    		for ( c=0; c<width; ++c ) {
slouken@91
    67
				if ( (c&7) == 0 ) {
slouken@0
    68
					byte = *src++;
slouken@0
    69
				}
slouken@0
    70
				bit = (byte&0x80)>>7;
slouken@0
    71
				if ( 1 ) {
slouken@0
    72
				  *dst = bit;
slouken@0
    73
				}
slouken@0
    74
				dst++;
slouken@0
    75
				byte <<= 1;
slouken@0
    76
			}
slouken@0
    77
			src += srcskip;
slouken@0
    78
			dst += dstskip;
slouken@0
    79
		}
slouken@0
    80
	}
slouken@0
    81
}
slouken@0
    82
static void BlitBto2(SDL_BlitInfo *info)
slouken@0
    83
{
slouken@0
    84
	int c;
slouken@0
    85
	int width, height;
slouken@0
    86
	Uint8 *src;
slouken@0
    87
	Uint16 *map, *dst;
slouken@0
    88
	int srcskip, dstskip;
slouken@0
    89
slouken@0
    90
	/* Set up some basic variables */
slouken@0
    91
	width = info->d_width;
slouken@0
    92
	height = info->d_height;
slouken@0
    93
	src = info->s_pixels;
slouken@0
    94
	srcskip = info->s_skip;
slouken@0
    95
	dst = (Uint16 *)info->d_pixels;
slouken@0
    96
	dstskip = info->d_skip/2;
slouken@0
    97
	map = (Uint16 *)info->table;
slouken@0
    98
	srcskip += width-(width+7)/8;
slouken@0
    99
slouken@0
   100
	while ( height-- ) {
slouken@0
   101
	        Uint8 byte = 0, bit;
slouken@0
   102
	    	for ( c=0; c<width; ++c ) {
slouken@91
   103
			if ( (c&7) == 0 ) {
slouken@0
   104
				byte = *src++;
slouken@0
   105
			}
slouken@0
   106
			bit = (byte&0x80)>>7;
slouken@0
   107
			if ( 1 ) {
slouken@0
   108
				*dst = map[bit];
slouken@0
   109
			}
slouken@0
   110
			byte <<= 1;
slouken@0
   111
			dst++;
slouken@0
   112
		}
slouken@0
   113
		src += srcskip;
slouken@0
   114
		dst += dstskip;
slouken@0
   115
	}
slouken@0
   116
}
slouken@0
   117
static void BlitBto3(SDL_BlitInfo *info)
slouken@0
   118
{
slouken@0
   119
	int c, o;
slouken@0
   120
	int width, height;
slouken@0
   121
	Uint8 *src, *map, *dst;
slouken@0
   122
	int srcskip, dstskip;
slouken@0
   123
slouken@0
   124
	/* Set up some basic variables */
slouken@0
   125
	width = info->d_width;
slouken@0
   126
	height = info->d_height;
slouken@0
   127
	src = info->s_pixels;
slouken@0
   128
	srcskip = info->s_skip;
slouken@0
   129
	dst = info->d_pixels;
slouken@0
   130
	dstskip = info->d_skip;
slouken@0
   131
	map = info->table;
slouken@0
   132
	srcskip += width-(width+7)/8;
slouken@0
   133
slouken@0
   134
	while ( height-- ) {
slouken@0
   135
	        Uint8 byte = 0, bit;
slouken@0
   136
	    	for ( c=0; c<width; ++c ) {
slouken@91
   137
			if ( (c&7) == 0 ) {
slouken@0
   138
				byte = *src++;
slouken@0
   139
			}
slouken@0
   140
			bit = (byte&0x80)>>7;
slouken@0
   141
			if ( 1 ) {
slouken@0
   142
				o = bit * 4;
slouken@0
   143
				dst[0] = map[o++];
slouken@0
   144
				dst[1] = map[o++];
slouken@0
   145
				dst[2] = map[o++];
slouken@0
   146
			}
slouken@0
   147
			byte <<= 1;
slouken@0
   148
			dst += 3;
slouken@0
   149
		}
slouken@0
   150
		src += srcskip;
slouken@0
   151
		dst += dstskip;
slouken@0
   152
	}
slouken@0
   153
}
slouken@0
   154
static void BlitBto4(SDL_BlitInfo *info)
slouken@0
   155
{
slouken@0
   156
	int width, height;
slouken@0
   157
	Uint8 *src;
slouken@0
   158
	Uint32 *map, *dst;
slouken@0
   159
	int srcskip, dstskip;
slouken@0
   160
	int c;
slouken@0
   161
slouken@0
   162
	/* Set up some basic variables */
slouken@0
   163
	width = info->d_width;
slouken@0
   164
	height = info->d_height;
slouken@0
   165
	src = info->s_pixels;
slouken@0
   166
	srcskip = info->s_skip;
slouken@0
   167
	dst = (Uint32 *)info->d_pixels;
slouken@0
   168
	dstskip = info->d_skip/4;
slouken@0
   169
	map = (Uint32 *)info->table;
slouken@0
   170
	srcskip += width-(width+7)/8;
slouken@0
   171
slouken@0
   172
	while ( height-- ) {
slouken@0
   173
	        Uint8 byte = 0, bit;
slouken@0
   174
	    	for ( c=0; c<width; ++c ) {
slouken@91
   175
			if ( (c&7) == 0 ) {
slouken@0
   176
				byte = *src++;
slouken@0
   177
			}
slouken@0
   178
			bit = (byte&0x80)>>7;
slouken@0
   179
			if ( 1 ) {
slouken@0
   180
				*dst = map[bit];
slouken@0
   181
			}
slouken@0
   182
			byte <<= 1;
slouken@0
   183
			dst++;
slouken@0
   184
		}
slouken@0
   185
		src += srcskip;
slouken@0
   186
		dst += dstskip;
slouken@0
   187
	}
slouken@0
   188
}
slouken@0
   189
slouken@0
   190
static void BlitBto1Key(SDL_BlitInfo *info)
slouken@0
   191
{
slouken@0
   192
        int width = info->d_width;
slouken@0
   193
	int height = info->d_height;
slouken@0
   194
	Uint8 *src = info->s_pixels;
slouken@0
   195
	Uint8 *dst = info->d_pixels;
slouken@0
   196
	int srcskip = info->s_skip;
slouken@0
   197
	int dstskip = info->d_skip;
slouken@0
   198
	Uint32 ckey = info->src->colorkey;
slouken@0
   199
	Uint8 *palmap = info->table;
slouken@0
   200
	int c;
slouken@0
   201
slouken@0
   202
	/* Set up some basic variables */
slouken@0
   203
	srcskip += width-(width+7)/8;
slouken@0
   204
slouken@0
   205
	if ( palmap ) {
slouken@0
   206
		while ( height-- ) {
slouken@0
   207
		        Uint8  byte = 0, bit;
slouken@0
   208
	    		for ( c=0; c<width; ++c ) {
slouken@91
   209
				if ( (c&7) == 0 ) {
slouken@0
   210
					byte = *src++;
slouken@0
   211
				}
slouken@0
   212
				bit = (byte&0x80)>>7;
slouken@0
   213
				if ( bit != ckey ) {
slouken@0
   214
				  *dst = palmap[bit];
slouken@0
   215
				}
slouken@0
   216
				dst++;
slouken@0
   217
				byte <<= 1;
slouken@0
   218
			}
slouken@0
   219
			src += srcskip;
slouken@0
   220
			dst += dstskip;
slouken@0
   221
		}
slouken@0
   222
	} else {
slouken@0
   223
		while ( height-- ) {
slouken@0
   224
		        Uint8  byte = 0, bit;
slouken@0
   225
	    		for ( c=0; c<width; ++c ) {
slouken@91
   226
				if ( (c&7) == 0 ) {
slouken@0
   227
					byte = *src++;
slouken@0
   228
				}
slouken@0
   229
				bit = (byte&0x80)>>7;
slouken@0
   230
				if ( bit != ckey ) {
slouken@0
   231
				  *dst = bit;
slouken@0
   232
				}
slouken@0
   233
				dst++;
slouken@0
   234
				byte <<= 1;
slouken@0
   235
			}
slouken@0
   236
			src += srcskip;
slouken@0
   237
			dst += dstskip;
slouken@0
   238
		}
slouken@0
   239
	}
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
static void BlitBto2Key(SDL_BlitInfo *info)
slouken@0
   243
{
slouken@0
   244
        int width = info->d_width;
slouken@0
   245
	int height = info->d_height;
slouken@0
   246
	Uint8 *src = info->s_pixels;
slouken@0
   247
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   248
	int srcskip = info->s_skip;
slouken@0
   249
	int dstskip = info->d_skip;
slouken@0
   250
	Uint32 ckey = info->src->colorkey;
slouken@0
   251
	Uint8 *palmap = info->table;
slouken@0
   252
	int c;
slouken@0
   253
slouken@0
   254
	/* Set up some basic variables */
slouken@0
   255
	srcskip += width-(width+7)/8;
slouken@0
   256
	dstskip /= 2;
slouken@0
   257
slouken@0
   258
	while ( height-- ) {
slouken@0
   259
	        Uint8 byte = 0, bit;
slouken@0
   260
	    	for ( c=0; c<width; ++c ) {
slouken@91
   261
			if ( (c&7) == 0 ) {
slouken@0
   262
				byte = *src++;
slouken@0
   263
			}
slouken@0
   264
			bit = (byte&0x80)>>7;
slouken@0
   265
			if ( bit != ckey ) {
slouken@0
   266
				*dstp=((Uint16 *)palmap)[bit];
slouken@0
   267
			}
slouken@0
   268
			byte <<= 1;
slouken@0
   269
			dstp++;
slouken@0
   270
		}
slouken@0
   271
		src += srcskip;
slouken@0
   272
		dstp += dstskip;
slouken@0
   273
	}
slouken@0
   274
}
slouken@0
   275
slouken@0
   276
static void BlitBto3Key(SDL_BlitInfo *info)
slouken@0
   277
{
slouken@0
   278
        int width = info->d_width;
slouken@0
   279
	int height = info->d_height;
slouken@0
   280
	Uint8 *src = info->s_pixels;
slouken@0
   281
	Uint8 *dst = info->d_pixels;
slouken@0
   282
	int srcskip = info->s_skip;
slouken@0
   283
	int dstskip = info->d_skip;
slouken@0
   284
	Uint32 ckey = info->src->colorkey;
slouken@0
   285
	Uint8 *palmap = info->table;
slouken@0
   286
	int c;
slouken@0
   287
slouken@0
   288
	/* Set up some basic variables */
slouken@0
   289
	srcskip += width-(width+7)/8;
slouken@0
   290
slouken@0
   291
	while ( height-- ) {
slouken@0
   292
	        Uint8  byte = 0, bit;
slouken@0
   293
	    	for ( c=0; c<width; ++c ) {
slouken@91
   294
			if ( (c&7) == 0 ) {
slouken@0
   295
				byte = *src++;
slouken@0
   296
			}
slouken@0
   297
			bit = (byte&0x80)>>7;
slouken@0
   298
			if ( bit != ckey ) {
slouken@1336
   299
				SDL_memcpy(dst, &palmap[bit*4], 3);
slouken@0
   300
			}
slouken@0
   301
			byte <<= 1;
slouken@0
   302
			dst += 3;
slouken@0
   303
		}
slouken@0
   304
		src += srcskip;
slouken@0
   305
		dst += dstskip;
slouken@0
   306
	}
slouken@0
   307
}
slouken@0
   308
slouken@0
   309
static void BlitBto4Key(SDL_BlitInfo *info)
slouken@0
   310
{
slouken@0
   311
        int width = info->d_width;
slouken@0
   312
	int height = info->d_height;
slouken@0
   313
	Uint8 *src = info->s_pixels;
slouken@0
   314
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
   315
	int srcskip = info->s_skip;
slouken@0
   316
	int dstskip = info->d_skip;
slouken@0
   317
	Uint32 ckey = info->src->colorkey;
slouken@0
   318
	Uint8 *palmap = info->table;
slouken@0
   319
	int c;
slouken@0
   320
slouken@0
   321
	/* Set up some basic variables */
slouken@0
   322
	srcskip += width-(width+7)/8;
slouken@0
   323
	dstskip /= 4;
slouken@0
   324
slouken@0
   325
	while ( height-- ) {
slouken@0
   326
	        Uint8 byte = 0, bit;
slouken@0
   327
	    	for ( c=0; c<width; ++c ) {
slouken@91
   328
			if ( (c&7) == 0 ) {
slouken@0
   329
				byte = *src++;
slouken@0
   330
			}
slouken@0
   331
			bit = (byte&0x80)>>7;
slouken@0
   332
			if ( bit != ckey ) {
slouken@0
   333
				*dstp=((Uint32 *)palmap)[bit];
slouken@0
   334
			}
slouken@0
   335
			byte <<= 1;
slouken@0
   336
			dstp++;
slouken@0
   337
		}
slouken@0
   338
		src += srcskip;
slouken@0
   339
		dstp += dstskip;
slouken@0
   340
	}
slouken@0
   341
}
slouken@0
   342
slouken@0
   343
static void BlitBtoNAlpha(SDL_BlitInfo *info)
slouken@0
   344
{
slouken@0
   345
        int width = info->d_width;
slouken@0
   346
	int height = info->d_height;
slouken@0
   347
	Uint8 *src = info->s_pixels;
slouken@0
   348
	Uint8 *dst = info->d_pixels;
slouken@0
   349
	int srcskip = info->s_skip;
slouken@0
   350
	int dstskip = info->d_skip;
slouken@0
   351
	const SDL_Color *srcpal	= info->src->palette->colors;
slouken@0
   352
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   353
	int  dstbpp;
slouken@0
   354
	int c;
slouken@0
   355
	const int A = info->src->alpha;
slouken@0
   356
slouken@0
   357
	/* Set up some basic variables */
slouken@0
   358
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
   359
	srcskip += width-(width+7)/8;
slouken@0
   360
slouken@0
   361
	while ( height-- ) {
slouken@0
   362
	        Uint8 byte = 0, bit;
slouken@0
   363
	    	for ( c=0; c<width; ++c ) {
slouken@91
   364
			if ( (c&7) == 0 ) {
slouken@0
   365
				byte = *src++;
slouken@0
   366
			}
slouken@0
   367
			bit = (byte&0x80)>>7;
slouken@0
   368
			if ( 1 ) {
slouken@0
   369
			        Uint32 pixel;
slouken@0
   370
			        unsigned sR, sG, sB;
slouken@0
   371
				unsigned dR, dG, dB;
slouken@0
   372
				sR = srcpal[bit].r;
slouken@0
   373
				sG = srcpal[bit].g;
slouken@0
   374
				sB = srcpal[bit].b;
slouken@0
   375
				DISEMBLE_RGB(dst, dstbpp, dstfmt,
slouken@0
   376
							pixel, dR, dG, dB);
slouken@0
   377
				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   378
			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
slouken@0
   379
			}
slouken@0
   380
			byte <<= 1;
slouken@0
   381
			dst += dstbpp;
slouken@0
   382
		}
slouken@0
   383
		src += srcskip;
slouken@0
   384
		dst += dstskip;
slouken@0
   385
	}
slouken@0
   386
}
slouken@0
   387
slouken@0
   388
static void BlitBtoNAlphaKey(SDL_BlitInfo *info)
slouken@0
   389
{
slouken@0
   390
        int width = info->d_width;
slouken@0
   391
	int height = info->d_height;
slouken@0
   392
	Uint8 *src = info->s_pixels;
slouken@0
   393
	Uint8 *dst = info->d_pixels;
slouken@0
   394
	int srcskip = info->s_skip;
slouken@0
   395
	int dstskip = info->d_skip;
slouken@0
   396
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   397
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   398
	const SDL_Color *srcpal	= srcfmt->palette->colors;
slouken@0
   399
	int dstbpp;
slouken@0
   400
	int c;
slouken@0
   401
	const int A = srcfmt->alpha;
slouken@0
   402
	Uint32 ckey = srcfmt->colorkey;
slouken@0
   403
slouken@0
   404
	/* Set up some basic variables */
slouken@0
   405
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
   406
	srcskip += width-(width+7)/8;
slouken@0
   407
slouken@0
   408
	while ( height-- ) {
slouken@0
   409
	        Uint8  byte = 0, bit;
slouken@0
   410
	    	for ( c=0; c<width; ++c ) {
slouken@91
   411
			if ( (c&7) == 0 ) {
slouken@0
   412
				byte = *src++;
slouken@0
   413
			}
slouken@0
   414
			bit = (byte&0x80)>>7;
slouken@0
   415
			if ( bit != ckey ) {
slouken@0
   416
			        int sR, sG, sB;
slouken@0
   417
				int dR, dG, dB;
slouken@0
   418
				Uint32 pixel;
slouken@0
   419
				sR = srcpal[bit].r;
slouken@0
   420
				sG = srcpal[bit].g;
slouken@0
   421
				sB = srcpal[bit].b;
slouken@0
   422
				DISEMBLE_RGB(dst, dstbpp, dstfmt,
slouken@0
   423
							pixel, dR, dG, dB);
slouken@0
   424
				ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   425
			  	ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
slouken@0
   426
			}
slouken@0
   427
			byte <<= 1;
slouken@0
   428
			dst += dstbpp;
slouken@0
   429
		}
slouken@0
   430
		src += srcskip;
slouken@0
   431
		dst += dstskip;
slouken@0
   432
	}
slouken@0
   433
}
slouken@0
   434
slouken@0
   435
static SDL_loblit bitmap_blit[] = {
slouken@0
   436
	NULL, BlitBto1, BlitBto2, BlitBto3, BlitBto4
slouken@0
   437
};
slouken@0
   438
slouken@0
   439
static SDL_loblit colorkey_blit[] = {
slouken@0
   440
    NULL, BlitBto1Key, BlitBto2Key, BlitBto3Key, BlitBto4Key
slouken@0
   441
};
slouken@0
   442
slouken@0
   443
SDL_loblit SDL_CalculateBlit0(SDL_Surface *surface, int blit_index)
slouken@0
   444
{
slouken@0
   445
	int which;
slouken@0
   446
slouken@1058
   447
	if ( surface->format->BitsPerPixel != 1 ) {
slouken@1057
   448
		/* We don't support sub 8-bit packed pixel modes */
slouken@1057
   449
		return NULL;
slouken@1057
   450
	}
slouken@0
   451
	if ( surface->map->dst->format->BitsPerPixel < 8 ) {
slouken@0
   452
		which = 0;
slouken@0
   453
	} else {
slouken@0
   454
		which = surface->map->dst->format->BytesPerPixel;
slouken@0
   455
	}
slouken@0
   456
	switch(blit_index) {
slouken@0
   457
	case 0:			/* copy */
slouken@0
   458
	    return bitmap_blit[which];
slouken@0
   459
slouken@0
   460
	case 1:			/* colorkey */
slouken@0
   461
	    return colorkey_blit[which];
slouken@0
   462
slouken@0
   463
	case 2:			/* alpha */
slouken@0
   464
	    return which >= 2 ? BlitBtoNAlpha : NULL;
slouken@0
   465
slouken@0
   466
	case 4:			/* alpha + colorkey */
slouken@0
   467
	    return which >= 2 ? BlitBtoNAlphaKey : NULL;
slouken@0
   468
	}
slouken@0
   469
	return NULL;
slouken@0
   470
}
slouken@0
   471