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