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