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