src/video/SDL_blit_A.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:50:19 +0000
changeset 1 cf2af46e9e2a
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Changes since SDL 1.2.0 release
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdio.h>
slouken@0
    29
slouken@0
    30
#include "SDL_types.h"
slouken@0
    31
#include "SDL_video.h"
slouken@0
    32
#include "SDL_blit.h"
slouken@0
    33
slouken@0
    34
/* Functions to perform alpha blended blitting */
slouken@0
    35
slouken@0
    36
/* N->1 blending with per-surface alpha */
slouken@0
    37
static void BlitNto1SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
    38
{
slouken@0
    39
	int width = info->d_width;
slouken@0
    40
	int height = info->d_height;
slouken@0
    41
	Uint8 *src = info->s_pixels;
slouken@0
    42
	int srcskip = info->s_skip;
slouken@0
    43
	Uint8 *dst = info->d_pixels;
slouken@0
    44
	int dstskip = info->d_skip;
slouken@0
    45
	Uint8 *palmap = info->table;
slouken@0
    46
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
    47
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
    48
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    49
slouken@0
    50
	const unsigned A = srcfmt->alpha;
slouken@0
    51
slouken@0
    52
	while ( height-- ) {
slouken@0
    53
	    DUFFS_LOOP4(
slouken@0
    54
	    {
slouken@0
    55
		Uint32 pixel;
slouken@0
    56
		unsigned sR;
slouken@0
    57
		unsigned sG;
slouken@0
    58
		unsigned sB;
slouken@0
    59
		unsigned dR;
slouken@0
    60
		unsigned dG;
slouken@0
    61
		unsigned dB;
slouken@0
    62
		DISEMBLE_RGB(src, srcbpp, srcfmt, pixel, sR, sG, sB);
slouken@0
    63
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
    64
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
    65
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
    66
		ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
    67
		dR &= 0xff;
slouken@0
    68
		dG &= 0xff;
slouken@0
    69
		dB &= 0xff;
slouken@0
    70
		/* Pack RGB into 8bit pixel */
slouken@0
    71
		if ( palmap == NULL ) {
slouken@0
    72
		    *dst =((dR>>5)<<(3+2))|
slouken@0
    73
			  ((dG>>5)<<(2))|
slouken@0
    74
			  ((dB>>6)<<(0));
slouken@0
    75
		} else {
slouken@0
    76
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
    77
				  ((dG>>5)<<(2))  |
slouken@0
    78
				  ((dB>>6)<<(0))];
slouken@0
    79
		}
slouken@0
    80
		dst++;
slouken@0
    81
		src += srcbpp;
slouken@0
    82
	    },
slouken@0
    83
	    width);
slouken@0
    84
	    src += srcskip;
slouken@0
    85
	    dst += dstskip;
slouken@0
    86
	}
slouken@0
    87
}
slouken@0
    88
slouken@0
    89
/* N->1 blending with pixel alpha */
slouken@0
    90
static void BlitNto1PixelAlpha(SDL_BlitInfo *info)
slouken@0
    91
{
slouken@0
    92
	int width = info->d_width;
slouken@0
    93
	int height = info->d_height;
slouken@0
    94
	Uint8 *src = info->s_pixels;
slouken@0
    95
	int srcskip = info->s_skip;
slouken@0
    96
	Uint8 *dst = info->d_pixels;
slouken@0
    97
	int dstskip = info->d_skip;
slouken@0
    98
	Uint8 *palmap = info->table;
slouken@0
    99
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   100
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   101
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   102
slouken@0
   103
	/* FIXME: fix alpha bit field expansion here too? */
slouken@0
   104
	while ( height-- ) {
slouken@0
   105
	    DUFFS_LOOP4(
slouken@0
   106
	    {
slouken@0
   107
		Uint32 pixel;
slouken@0
   108
		unsigned sR;
slouken@0
   109
		unsigned sG;
slouken@0
   110
		unsigned sB;
slouken@0
   111
		unsigned sA;
slouken@0
   112
		unsigned dR;
slouken@0
   113
		unsigned dG;
slouken@0
   114
		unsigned dB;
slouken@0
   115
		DISEMBLE_RGBA(src,srcbpp,srcfmt,pixel,sR,sG,sB,sA);
slouken@0
   116
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
   117
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
   118
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
   119
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   120
		dR &= 0xff;
slouken@0
   121
		dG &= 0xff;
slouken@0
   122
		dB &= 0xff;
slouken@0
   123
		/* Pack RGB into 8bit pixel */
slouken@0
   124
		if ( palmap == NULL ) {
slouken@0
   125
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   126
			  ((dG>>5)<<(2))|
slouken@0
   127
			  ((dB>>6)<<(0));
slouken@0
   128
		} else {
slouken@0
   129
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   130
				  ((dG>>5)<<(2))  |
slouken@0
   131
				  ((dB>>6)<<(0))  ];
slouken@0
   132
		}
slouken@0
   133
		dst++;
slouken@0
   134
		src += srcbpp;
slouken@0
   135
	    },
slouken@0
   136
	    width);
slouken@0
   137
	    src += srcskip;
slouken@0
   138
	    dst += dstskip;
slouken@0
   139
	}
slouken@0
   140
}
slouken@0
   141
slouken@0
   142
/* colorkeyed N->1 blending with per-surface alpha */
slouken@0
   143
static void BlitNto1SurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
   144
{
slouken@0
   145
	int width = info->d_width;
slouken@0
   146
	int height = info->d_height;
slouken@0
   147
	Uint8 *src = info->s_pixels;
slouken@0
   148
	int srcskip = info->s_skip;
slouken@0
   149
	Uint8 *dst = info->d_pixels;
slouken@0
   150
	int dstskip = info->d_skip;
slouken@0
   151
	Uint8 *palmap = info->table;
slouken@0
   152
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   153
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   154
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   155
	Uint32 ckey = srcfmt->colorkey;
slouken@0
   156
slouken@0
   157
	const int A = srcfmt->alpha;
slouken@0
   158
slouken@0
   159
	while ( height-- ) {
slouken@0
   160
	    DUFFS_LOOP(
slouken@0
   161
	    {
slouken@0
   162
		Uint32 pixel;
slouken@0
   163
		unsigned sR;
slouken@0
   164
		unsigned sG;
slouken@0
   165
		unsigned sB;
slouken@0
   166
		unsigned dR;
slouken@0
   167
		unsigned dG;
slouken@0
   168
		unsigned dB;
slouken@0
   169
		DISEMBLE_RGB(src, srcbpp, srcfmt, pixel, sR, sG, sB);
slouken@0
   170
		if ( pixel != ckey ) {
slouken@0
   171
		    dR = dstfmt->palette->colors[*dst].r;
slouken@0
   172
		    dG = dstfmt->palette->colors[*dst].g;
slouken@0
   173
		    dB = dstfmt->palette->colors[*dst].b;
slouken@0
   174
		    ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   175
		    dR &= 0xff;
slouken@0
   176
		    dG &= 0xff;
slouken@0
   177
		    dB &= 0xff;
slouken@0
   178
		    /* Pack RGB into 8bit pixel */
slouken@0
   179
		    if ( palmap == NULL ) {
slouken@0
   180
			*dst =((dR>>5)<<(3+2))|
slouken@0
   181
			      ((dG>>5)<<(2)) |
slouken@0
   182
			      ((dB>>6)<<(0));
slouken@0
   183
		    } else {
slouken@0
   184
			*dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   185
				      ((dG>>5)<<(2))  |
slouken@0
   186
				      ((dB>>6)<<(0))  ];
slouken@0
   187
		    }
slouken@0
   188
		}
slouken@0
   189
		dst++;
slouken@0
   190
		src += srcbpp;
slouken@0
   191
	    },
slouken@0
   192
	    width);
slouken@0
   193
	    src += srcskip;
slouken@0
   194
	    dst += dstskip;
slouken@0
   195
	}
slouken@0
   196
}
slouken@0
   197
slouken@1
   198
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1
   199
static void BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo *info)
slouken@0
   200
{
slouken@0
   201
	int width = info->d_width;
slouken@0
   202
	int height = info->d_height;
slouken@0
   203
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   204
	int srcskip = info->s_skip >> 2;
slouken@0
   205
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
   206
	int dstskip = info->d_skip >> 2;
slouken@0
   207
slouken@0
   208
	while(height--) {
slouken@0
   209
	    DUFFS_LOOP4({
slouken@1
   210
		    Uint32 s = *srcp++;
slouken@1
   211
		    Uint32 d = *dstp;
slouken@1
   212
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
   213
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
   214
	    }, width);
slouken@0
   215
	    srcp += srcskip;
slouken@0
   216
	    dstp += dstskip;
slouken@0
   217
	}
slouken@0
   218
}
slouken@0
   219
slouken@1
   220
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1
   221
static void BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo *info)
slouken@1
   222
{
slouken@1
   223
	unsigned alpha = info->src->alpha;
slouken@1
   224
	if(alpha == 128) {
slouken@1
   225
		BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1
   226
	} else {
slouken@1
   227
		int width = info->d_width;
slouken@1
   228
		int height = info->d_height;
slouken@1
   229
		Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1
   230
		int srcskip = info->s_skip >> 2;
slouken@1
   231
		Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1
   232
		int dstskip = info->d_skip >> 2;
slouken@1
   233
slouken@1
   234
		while(height--) {
slouken@1
   235
			DUFFS_LOOP4({
slouken@1
   236
				Uint32 s;
slouken@1
   237
				Uint32 d;
slouken@1
   238
				Uint32 s1;
slouken@1
   239
				Uint32 d1;
slouken@1
   240
				s = *srcp;
slouken@1
   241
				d = *dstp;
slouken@1
   242
				s1 = s & 0xff00ff;
slouken@1
   243
				d1 = d & 0xff00ff;
slouken@1
   244
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
   245
				     & 0xff00ff;
slouken@1
   246
				s &= 0xff00;
slouken@1
   247
				d &= 0xff00;
slouken@1
   248
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
   249
				*dstp = d1 | d | 0xff000000;
slouken@1
   250
				++srcp;
slouken@1
   251
				++dstp;
slouken@1
   252
			}, width);
slouken@1
   253
			srcp += srcskip;
slouken@1
   254
			dstp += dstskip;
slouken@1
   255
		}
slouken@1
   256
	}
slouken@1
   257
}
slouken@1
   258
slouken@0
   259
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@0
   260
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
slouken@0
   261
{
slouken@0
   262
	int width = info->d_width;
slouken@0
   263
	int height = info->d_height;
slouken@0
   264
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   265
	int srcskip = info->s_skip >> 2;
slouken@0
   266
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
   267
	int dstskip = info->d_skip >> 2;
slouken@0
   268
slouken@0
   269
	while(height--) {
slouken@0
   270
	    DUFFS_LOOP4({
slouken@0
   271
		Uint32 dalpha;
slouken@0
   272
		Uint32 d;
slouken@0
   273
		Uint32 s1;
slouken@0
   274
		Uint32 d1;
slouken@0
   275
		Uint32 s = *srcp;
slouken@0
   276
		Uint32 alpha = s >> 24;
slouken@0
   277
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   278
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   279
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   280
		   Benchmark this! */
slouken@0
   281
		if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
   282
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@0
   283
		} else {
slouken@0
   284
		    /*
slouken@0
   285
		     * take out the middle component (green), and process
slouken@0
   286
		     * the other two in parallel. One multiply less.
slouken@0
   287
		     */
slouken@0
   288
		    d = *dstp;
slouken@0
   289
		    dalpha = d & 0xff000000;
slouken@0
   290
		    s1 = s & 0xff00ff;
slouken@0
   291
		    d1 = d & 0xff00ff;
slouken@0
   292
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
   293
		    s &= 0xff00;
slouken@0
   294
		    d &= 0xff00;
slouken@0
   295
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
   296
		    *dstp = d1 | d | dalpha;
slouken@0
   297
		}
slouken@0
   298
		++srcp;
slouken@0
   299
		++dstp;
slouken@0
   300
	    }, width);
slouken@0
   301
	    srcp += srcskip;
slouken@0
   302
	    dstp += dstskip;
slouken@0
   303
	}
slouken@0
   304
}
slouken@0
   305
slouken@1
   306
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
   307
slouken@1
   308
/* blend a single 16 bit pixel at 50% */
slouken@1
   309
#define BLEND16_50(d, s, mask)						\
slouken@1
   310
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
   311
slouken@1
   312
/* blend two 16 bit pixels at 50% */
slouken@1
   313
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
   314
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
   315
	 + (s & d & (~(mask | mask << 16))))
slouken@1
   316
slouken@1
   317
static void Blit16to16SurfaceAlpha128(SDL_BlitInfo *info, Uint16 mask)
slouken@0
   318
{
slouken@0
   319
	int width = info->d_width;
slouken@0
   320
	int height = info->d_height;
slouken@0
   321
	Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@0
   322
	int srcskip = info->s_skip >> 1;
slouken@0
   323
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   324
	int dstskip = info->d_skip >> 1;
slouken@0
   325
slouken@0
   326
	while(height--) {
slouken@1
   327
		if(((unsigned long)srcp ^ (unsigned long)dstp) & 2) {
slouken@1
   328
			/*
slouken@1
   329
			 * Source and destination not aligned, pipeline it.
slouken@1
   330
			 * This is mostly a win for big blits but no loss for
slouken@1
   331
			 * small ones
slouken@1
   332
			 */
slouken@1
   333
			Uint32 prev_sw;
slouken@1
   334
			int w = width;
slouken@1
   335
slouken@1
   336
			/* handle odd destination */
slouken@1
   337
			if((unsigned long)dstp & 2) {
slouken@1
   338
				Uint16 d = *dstp, s = *srcp;
slouken@1
   339
				*dstp = BLEND16_50(d, s, mask);
slouken@1
   340
				dstp++;
slouken@1
   341
				srcp++;
slouken@1
   342
				w--;
slouken@1
   343
			}
slouken@1
   344
			srcp++;	/* srcp is now 32-bit aligned */
slouken@1
   345
slouken@1
   346
			/* bootstrap pipeline with first halfword */
slouken@1
   347
			prev_sw = ((Uint32 *)srcp)[-1];
slouken@1
   348
slouken@1
   349
			while(w > 1) {
slouken@1
   350
				Uint32 sw, dw, s;
slouken@1
   351
				sw = *(Uint32 *)srcp;
slouken@1
   352
				dw = *(Uint32 *)dstp;
slouken@1
   353
				if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@1
   354
					s = (prev_sw << 16) + (sw >> 16);
slouken@1
   355
				else
slouken@1
   356
					s = (prev_sw >> 16) + (sw << 16);
slouken@1
   357
				prev_sw = sw;
slouken@1
   358
				*(Uint32 *)dstp = BLEND2x16_50(dw, s, mask);
slouken@1
   359
				dstp += 2;
slouken@1
   360
				srcp += 2;
slouken@1
   361
				w -= 2;
slouken@1
   362
			}
slouken@1
   363
slouken@1
   364
			/* final pixel if any */
slouken@1
   365
			if(w) {
slouken@1
   366
				Uint16 d = *dstp, s;
slouken@1
   367
				if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@1
   368
					s = prev_sw;
slouken@1
   369
				else
slouken@1
   370
					s = prev_sw >> 16;
slouken@1
   371
				*dstp = BLEND16_50(d, s, mask);
slouken@1
   372
				srcp++;
slouken@1
   373
				dstp++;
slouken@1
   374
			}
slouken@1
   375
			srcp += srcskip - 1;
slouken@1
   376
			dstp += dstskip;
slouken@1
   377
		} else {
slouken@1
   378
			/* source and destination are aligned */
slouken@1
   379
			int w = width;
slouken@1
   380
slouken@1
   381
			/* first odd pixel? */
slouken@1
   382
			if((unsigned long)srcp & 2) {
slouken@1
   383
				Uint16 d = *dstp, s = *srcp;
slouken@1
   384
				*dstp = BLEND16_50(d, s, mask);
slouken@1
   385
				srcp++;
slouken@1
   386
				dstp++;
slouken@1
   387
				w--;
slouken@1
   388
			}
slouken@1
   389
			/* srcp and dstp are now 32-bit aligned */
slouken@1
   390
slouken@1
   391
			while(w > 1) {
slouken@1
   392
				Uint32 sw = *(Uint32 *)srcp;
slouken@1
   393
				Uint32 dw = *(Uint32 *)dstp;
slouken@1
   394
				*(Uint32 *)dstp = BLEND2x16_50(dw, sw, mask);
slouken@1
   395
				srcp += 2;
slouken@1
   396
				dstp += 2;
slouken@1
   397
				w -= 2;
slouken@1
   398
			}
slouken@1
   399
slouken@1
   400
			/* last odd pixel? */
slouken@1
   401
			if(w) {
slouken@1
   402
				Uint16 d = *dstp, s = *srcp;
slouken@1
   403
				*dstp = BLEND16_50(d, s, mask);
slouken@1
   404
				srcp++;
slouken@1
   405
				dstp++;
slouken@1
   406
			}
slouken@1
   407
			srcp += srcskip;
slouken@1
   408
			dstp += dstskip;
slouken@1
   409
		}
slouken@1
   410
	}
slouken@1
   411
}
slouken@1
   412
slouken@1
   413
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1
   414
static void Blit565to565SurfaceAlpha(SDL_BlitInfo *info)
slouken@1
   415
{
slouken@1
   416
	unsigned alpha = info->src->alpha;
slouken@1
   417
	if(alpha == 128) {
slouken@1
   418
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1
   419
	} else {
slouken@1
   420
		int width = info->d_width;
slouken@1
   421
		int height = info->d_height;
slouken@1
   422
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
   423
		int srcskip = info->s_skip >> 1;
slouken@1
   424
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
   425
		int dstskip = info->d_skip >> 1;
slouken@1
   426
		alpha >>= 3;	/* downscale alpha to 5 bits */
slouken@1
   427
slouken@1
   428
		while(height--) {
slouken@1
   429
			DUFFS_LOOP4({
slouken@1
   430
				Uint32 s = *srcp++;
slouken@1
   431
				Uint32 d = *dstp;
slouken@1
   432
				/*
slouken@1
   433
				 * shift out the middle component (green) to
slouken@1
   434
				 * the high 16 bits, and process all three RGB
slouken@1
   435
				 * components at the same time.
slouken@1
   436
				 */
slouken@1
   437
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
   438
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
   439
				d += (s - d) * alpha >> 5;
slouken@1
   440
				d &= 0x07e0f81f;
slouken@1
   441
				*dstp++ = d | d >> 16;
slouken@1
   442
			}, width);
slouken@1
   443
			srcp += srcskip;
slouken@1
   444
			dstp += dstskip;
slouken@1
   445
		}
slouken@0
   446
	}
slouken@0
   447
}
slouken@0
   448
slouken@0
   449
/* fast RGB555->RGB555 blending with surface alpha */
slouken@0
   450
static void Blit555to555SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
   451
{
slouken@1
   452
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@1
   453
	if(alpha == 128) {
slouken@1
   454
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1
   455
	} else {
slouken@1
   456
		int width = info->d_width;
slouken@1
   457
		int height = info->d_height;
slouken@1
   458
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
   459
		int srcskip = info->s_skip >> 1;
slouken@1
   460
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
   461
		int dstskip = info->d_skip >> 1;
slouken@1
   462
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@0
   463
slouken@1
   464
		while(height--) {
slouken@1
   465
			DUFFS_LOOP4({
slouken@1
   466
				Uint32 s = *srcp++;
slouken@1
   467
				Uint32 d = *dstp;
slouken@1
   468
				/*
slouken@1
   469
				 * shift out the middle component (green) to
slouken@1
   470
				 * the high 16 bits, and process all three RGB
slouken@1
   471
				 * components at the same time.
slouken@1
   472
				 */
slouken@1
   473
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
   474
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
   475
				d += (s - d) * alpha >> 5;
slouken@1
   476
				d &= 0x03e07c1f;
slouken@1
   477
				*dstp++ = d | d >> 16;
slouken@1
   478
			}, width);
slouken@1
   479
			srcp += srcskip;
slouken@1
   480
			dstp += dstskip;
slouken@1
   481
		}
slouken@0
   482
	}
slouken@0
   483
}
slouken@0
   484
slouken@0
   485
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@0
   486
static void BlitARGBto565PixelAlpha(SDL_BlitInfo *info)
slouken@0
   487
{
slouken@0
   488
	int width = info->d_width;
slouken@0
   489
	int height = info->d_height;
slouken@0
   490
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   491
	int srcskip = info->s_skip >> 2;
slouken@0
   492
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   493
	int dstskip = info->d_skip >> 1;
slouken@0
   494
slouken@0
   495
	while(height--) {
slouken@0
   496
	    DUFFS_LOOP4({
slouken@0
   497
		Uint32 s = *srcp;
slouken@0
   498
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
   499
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   500
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   501
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   502
		   Benchmark this! */
slouken@0
   503
		if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@0
   504
		    *dstp = (s >> 8 & 0xf800) + (s >> 5 & 0x7e0)
slouken@0
   505
			  + (s >> 3  & 0x1f);
slouken@0
   506
		} else {
slouken@0
   507
		    Uint32 d = *dstp;
slouken@0
   508
		    /*
slouken@0
   509
		     * convert source and destination to G0RAB65565
slouken@0
   510
		     * and blend all components at the same time
slouken@0
   511
		     */
slouken@0
   512
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
   513
		      + (s >> 3 & 0x1f);
slouken@0
   514
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
   515
		    d += (s - d) * alpha >> 5;
slouken@0
   516
		    d &= 0x07e0f81f;
slouken@0
   517
		    *dstp = d | d >> 16;
slouken@0
   518
		}
slouken@0
   519
		srcp++;
slouken@0
   520
		dstp++;
slouken@0
   521
	    }, width);
slouken@0
   522
	    srcp += srcskip;
slouken@0
   523
	    dstp += dstskip;
slouken@0
   524
	}
slouken@0
   525
}
slouken@0
   526
slouken@0
   527
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@0
   528
static void BlitARGBto555PixelAlpha(SDL_BlitInfo *info)
slouken@0
   529
{
slouken@0
   530
	int width = info->d_width;
slouken@0
   531
	int height = info->d_height;
slouken@0
   532
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   533
	int srcskip = info->s_skip >> 2;
slouken@0
   534
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   535
	int dstskip = info->d_skip >> 1;
slouken@0
   536
slouken@0
   537
	while(height--) {
slouken@0
   538
	    DUFFS_LOOP4({
slouken@0
   539
		unsigned alpha;
slouken@0
   540
		Uint32 s = *srcp;
slouken@0
   541
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
   542
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   543
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   544
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   545
		   Benchmark this! */
slouken@0
   546
		if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@0
   547
		    *dstp = (s >> 9 & 0x7c00) + (s >> 6 & 0x3e0)
slouken@0
   548
			  + (s >> 3  & 0x1f);
slouken@0
   549
		} else {
slouken@0
   550
		    Uint32 d = *dstp;
slouken@0
   551
		    /*
slouken@0
   552
		     * convert source and destination to G0RAB65565
slouken@0
   553
		     * and blend all components at the same time
slouken@0
   554
		     */
slouken@0
   555
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
   556
		      + (s >> 3 & 0x1f);
slouken@0
   557
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
   558
		    d += (s - d) * alpha >> 5;
slouken@0
   559
		    d &= 0x03e07c1f;
slouken@0
   560
		    *dstp = d | d >> 16;
slouken@0
   561
		}
slouken@0
   562
		srcp++;
slouken@0
   563
		dstp++;
slouken@0
   564
	    }, width);
slouken@0
   565
	    srcp += srcskip;
slouken@0
   566
	    dstp += dstskip;
slouken@0
   567
	}
slouken@0
   568
}
slouken@0
   569
slouken@0
   570
/* General (slow) N->N blending with per-surface alpha */
slouken@0
   571
static void BlitNtoNSurfaceAlpha(SDL_BlitInfo *info)
slouken@0
   572
{
slouken@0
   573
	int width = info->d_width;
slouken@0
   574
	int height = info->d_height;
slouken@0
   575
	Uint8 *src = info->s_pixels;
slouken@0
   576
	int srcskip = info->s_skip;
slouken@0
   577
	Uint8 *dst = info->d_pixels;
slouken@0
   578
	int dstskip = info->d_skip;
slouken@0
   579
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   580
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   581
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   582
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
   583
	unsigned sA = srcfmt->alpha;
slouken@0
   584
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
   585
slouken@0
   586
	while ( height-- ) {
slouken@0
   587
	    DUFFS_LOOP4(
slouken@0
   588
	    {
slouken@0
   589
		Uint32 pixel;
slouken@0
   590
		unsigned sR;
slouken@0
   591
		unsigned sG;
slouken@0
   592
		unsigned sB;
slouken@0
   593
		unsigned dR;
slouken@0
   594
		unsigned dG;
slouken@0
   595
		unsigned dB;
slouken@0
   596
		DISEMBLE_RGB(src, srcbpp, srcfmt, pixel, sR, sG, sB);
slouken@0
   597
		DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@0
   598
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   599
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   600
		src += srcbpp;
slouken@0
   601
		dst += dstbpp;
slouken@0
   602
	    },
slouken@0
   603
	    width);
slouken@0
   604
	    src += srcskip;
slouken@0
   605
	    dst += dstskip;
slouken@0
   606
	}
slouken@0
   607
}
slouken@0
   608
slouken@0
   609
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@0
   610
static void BlitNtoNSurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
   611
{
slouken@0
   612
	int width = info->d_width;
slouken@0
   613
	int height = info->d_height;
slouken@0
   614
	Uint8 *src = info->s_pixels;
slouken@0
   615
	int srcskip = info->s_skip;
slouken@0
   616
	Uint8 *dst = info->d_pixels;
slouken@0
   617
	int dstskip = info->d_skip;
slouken@0
   618
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   619
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   620
	Uint32 ckey = srcfmt->colorkey;
slouken@0
   621
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   622
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
   623
	unsigned sA = srcfmt->alpha;
slouken@0
   624
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
   625
slouken@0
   626
	while ( height-- ) {
slouken@0
   627
	    DUFFS_LOOP4(
slouken@0
   628
	    {
slouken@0
   629
		Uint32 pixel;
slouken@0
   630
		unsigned sR;
slouken@0
   631
		unsigned sG;
slouken@0
   632
		unsigned sB;
slouken@0
   633
		unsigned dR;
slouken@0
   634
		unsigned dG;
slouken@0
   635
		unsigned dB;
slouken@0
   636
		RETRIEVE_RGB_PIXEL(src, srcbpp, pixel);
slouken@0
   637
		if(pixel != ckey) {
slouken@0
   638
		    RGB_FROM_PIXEL(pixel, srcfmt, sR, sG, sB);
slouken@0
   639
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@0
   640
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   641
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   642
		}
slouken@0
   643
		src += srcbpp;
slouken@0
   644
		dst += dstbpp;
slouken@0
   645
	    },
slouken@0
   646
	    width);
slouken@0
   647
	    src += srcskip;
slouken@0
   648
	    dst += dstskip;
slouken@0
   649
	}
slouken@0
   650
}
slouken@0
   651
slouken@0
   652
/* General (slow) N->N blending with pixel alpha */
slouken@0
   653
static void BlitNtoNPixelAlpha(SDL_BlitInfo *info)
slouken@0
   654
{
slouken@0
   655
	int width = info->d_width;
slouken@0
   656
	int height = info->d_height;
slouken@0
   657
	Uint8 *src = info->s_pixels;
slouken@0
   658
	int srcskip = info->s_skip;
slouken@0
   659
	Uint8 *dst = info->d_pixels;
slouken@0
   660
	int dstskip = info->d_skip;
slouken@0
   661
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   662
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   663
slouken@0
   664
	int  srcbpp;
slouken@0
   665
	int  dstbpp;
slouken@0
   666
slouken@0
   667
	/* Set up some basic variables */
slouken@0
   668
	srcbpp = srcfmt->BytesPerPixel;
slouken@0
   669
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
   670
slouken@0
   671
	/* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@0
   672
	   quite right. for <8bpp source alpha, it gets them very wrong
slouken@0
   673
	   (check all macros!)
slouken@0
   674
	   It is unclear whether there is a good general solution that doesn't
slouken@0
   675
	   need a branch (or a divide). */
slouken@0
   676
	while ( height-- ) {
slouken@0
   677
	    DUFFS_LOOP4(
slouken@0
   678
	    {
slouken@0
   679
		Uint32 pixel;
slouken@0
   680
		unsigned sR;
slouken@0
   681
		unsigned sG;
slouken@0
   682
		unsigned sB;
slouken@0
   683
		unsigned dR;
slouken@0
   684
		unsigned dG;
slouken@0
   685
		unsigned dB;
slouken@0
   686
		unsigned sA;
slouken@0
   687
		unsigned dA;
slouken@0
   688
		DISEMBLE_RGBA(src, srcbpp, srcfmt, pixel, sR, sG, sB, sA);
slouken@0
   689
		DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
slouken@0
   690
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   691
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   692
		src += srcbpp;
slouken@0
   693
		dst += dstbpp;
slouken@0
   694
	    },
slouken@0
   695
	    width);
slouken@0
   696
	    src += srcskip;
slouken@0
   697
	    dst += dstskip;
slouken@0
   698
	}
slouken@0
   699
}
slouken@0
   700
slouken@0
   701
slouken@0
   702
SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int blit_index)
slouken@0
   703
{
slouken@0
   704
    SDL_PixelFormat *sf = surface->format;
slouken@0
   705
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
   706
slouken@0
   707
    if(sf->Amask == 0) {
slouken@0
   708
	if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
   709
	    if(df->BytesPerPixel == 1)
slouken@0
   710
		return BlitNto1SurfaceAlphaKey;
slouken@0
   711
	    else
slouken@0
   712
		return BlitNtoNSurfaceAlphaKey;
slouken@0
   713
	} else {
slouken@0
   714
	    /* Per-surface alpha blits */
slouken@0
   715
	    switch(df->BytesPerPixel) {
slouken@0
   716
	    case 1:
slouken@0
   717
		return BlitNto1SurfaceAlpha;
slouken@0
   718
slouken@0
   719
	    case 2:
slouken@0
   720
		if(surface->map->identity) {
slouken@0
   721
		    if(df->Gmask == 0x7e0)
slouken@0
   722
			return Blit565to565SurfaceAlpha;
slouken@0
   723
		    else if(df->Gmask == 0x3e0)
slouken@0
   724
			return Blit555to555SurfaceAlpha;
slouken@0
   725
		}
slouken@0
   726
		return BlitNtoNSurfaceAlpha;
slouken@0
   727
slouken@0
   728
	    case 4:
slouken@0
   729
		if(sf->Rmask == df->Rmask
slouken@0
   730
		   && sf->Gmask == df->Gmask
slouken@0
   731
		   && sf->Bmask == df->Bmask
slouken@0
   732
		   && (sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff
slouken@0
   733
		   && sf->BytesPerPixel == 4)
slouken@0
   734
		    return BlitRGBtoRGBSurfaceAlpha;
slouken@0
   735
		else
slouken@0
   736
		    return BlitNtoNSurfaceAlpha;
slouken@0
   737
slouken@0
   738
	    case 3:
slouken@0
   739
	    default:
slouken@0
   740
		return BlitNtoNSurfaceAlpha;
slouken@0
   741
	    }
slouken@0
   742
	}
slouken@0
   743
    } else {
slouken@0
   744
	/* Per-pixel alpha blits */
slouken@0
   745
	switch(df->BytesPerPixel) {
slouken@0
   746
	case 1:
slouken@0
   747
	    return BlitNto1PixelAlpha;
slouken@0
   748
slouken@0
   749
	case 2:
slouken@0
   750
	    if(sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@0
   751
	       && sf->Gmask == 0xff00
slouken@0
   752
	       && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@0
   753
		   || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@0
   754
		if(df->Gmask == 0x7e0)
slouken@0
   755
		    return BlitARGBto565PixelAlpha;
slouken@0
   756
		else if(df->Gmask == 0x3e0)
slouken@0
   757
		    return BlitARGBto555PixelAlpha;
slouken@0
   758
	    }
slouken@0
   759
	    return BlitNtoNPixelAlpha;
slouken@0
   760
slouken@0
   761
	case 4:
slouken@0
   762
	    if(sf->Amask == 0xff000000
slouken@0
   763
	       && sf->Rmask == df->Rmask
slouken@0
   764
	       && sf->Gmask == df->Gmask
slouken@0
   765
	       && sf->Bmask == df->Bmask
slouken@0
   766
	       && sf->BytesPerPixel == 4)
slouken@0
   767
		return BlitRGBtoRGBPixelAlpha;
slouken@0
   768
	    return BlitNtoNPixelAlpha;
slouken@0
   769
slouken@0
   770
	case 3:
slouken@0
   771
	default:
slouken@0
   772
	    return BlitNtoNPixelAlpha;
slouken@0
   773
	}
slouken@0
   774
    }
slouken@0
   775
}
slouken@0
   776