src/video/SDL_blit_A.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 1 cf2af46e9e2a
permissions -rw-r--r--
Initial revision
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@0
   198
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@0
   199
static void BlitRGBtoRGBSurfaceAlpha(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
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   208
	unsigned alpha = srcfmt->alpha;
slouken@0
   209
slouken@0
   210
	while(height--) {
slouken@0
   211
	    DUFFS_LOOP4({
slouken@0
   212
		Uint32 s;
slouken@0
   213
		Uint32 d;
slouken@0
   214
		Uint32 s1;
slouken@0
   215
		Uint32 d1;
slouken@0
   216
		s = *srcp;
slouken@0
   217
		d = *dstp;
slouken@0
   218
		s1 = s & 0xff00ff;
slouken@0
   219
		d1 = d & 0xff00ff;
slouken@0
   220
		d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
   221
		s &= 0xff00;
slouken@0
   222
		d &= 0xff00;
slouken@0
   223
		d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
   224
		*dstp = d1 | d | 0xff000000;
slouken@0
   225
		++srcp;
slouken@0
   226
		++dstp;
slouken@0
   227
	    }, width);
slouken@0
   228
	    srcp += srcskip;
slouken@0
   229
	    dstp += dstskip;
slouken@0
   230
	}
slouken@0
   231
}
slouken@0
   232
slouken@0
   233
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@0
   234
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
slouken@0
   235
{
slouken@0
   236
	int width = info->d_width;
slouken@0
   237
	int height = info->d_height;
slouken@0
   238
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   239
	int srcskip = info->s_skip >> 2;
slouken@0
   240
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
   241
	int dstskip = info->d_skip >> 2;
slouken@0
   242
slouken@0
   243
	while(height--) {
slouken@0
   244
	    DUFFS_LOOP4({
slouken@0
   245
		Uint32 dalpha;
slouken@0
   246
		Uint32 d;
slouken@0
   247
		Uint32 s1;
slouken@0
   248
		Uint32 d1;
slouken@0
   249
		Uint32 s = *srcp;
slouken@0
   250
		Uint32 alpha = s >> 24;
slouken@0
   251
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   252
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   253
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   254
		   Benchmark this! */
slouken@0
   255
		if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
   256
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@0
   257
		} else {
slouken@0
   258
		    /*
slouken@0
   259
		     * take out the middle component (green), and process
slouken@0
   260
		     * the other two in parallel. One multiply less.
slouken@0
   261
		     */
slouken@0
   262
		    d = *dstp;
slouken@0
   263
		    dalpha = d & 0xff000000;
slouken@0
   264
		    s1 = s & 0xff00ff;
slouken@0
   265
		    d1 = d & 0xff00ff;
slouken@0
   266
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
   267
		    s &= 0xff00;
slouken@0
   268
		    d &= 0xff00;
slouken@0
   269
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
   270
		    *dstp = d1 | d | dalpha;
slouken@0
   271
		}
slouken@0
   272
		++srcp;
slouken@0
   273
		++dstp;
slouken@0
   274
	    }, width);
slouken@0
   275
	    srcp += srcskip;
slouken@0
   276
	    dstp += dstskip;
slouken@0
   277
	}
slouken@0
   278
}
slouken@0
   279
slouken@0
   280
/* fast RGB565->RGB565 blending with surface alpha */
slouken@0
   281
static void Blit565to565SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
   282
{
slouken@0
   283
	int width = info->d_width;
slouken@0
   284
	int height = info->d_height;
slouken@0
   285
	Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@0
   286
	int srcskip = info->s_skip >> 1;
slouken@0
   287
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   288
	int dstskip = info->d_skip >> 1;
slouken@0
   289
	unsigned alpha = info->src->alpha >> 3; /* downscale alpha to 5 bits */
slouken@0
   290
slouken@0
   291
	while(height--) {
slouken@0
   292
	    DUFFS_LOOP4({
slouken@0
   293
		Uint32 s = *srcp++;
slouken@0
   294
		Uint32 d = *dstp;
slouken@0
   295
		/*
slouken@0
   296
		 * shift out the middle component (green) to the high 16
slouken@0
   297
		 * bits, and process all three RGB components at the same
slouken@0
   298
		 * time.
slouken@0
   299
		 */
slouken@0
   300
		s = (s | s << 16) & 0x07e0f81f;
slouken@0
   301
		d = (d | d << 16) & 0x07e0f81f;
slouken@0
   302
		d += (s - d) * alpha >> 5;
slouken@0
   303
		d &= 0x07e0f81f;
slouken@0
   304
		*dstp++ = d | d >> 16;
slouken@0
   305
	    }, width);
slouken@0
   306
	    srcp += srcskip;
slouken@0
   307
	    dstp += dstskip;
slouken@0
   308
	}
slouken@0
   309
}
slouken@0
   310
slouken@0
   311
/* fast RGB555->RGB555 blending with surface alpha */
slouken@0
   312
static void Blit555to555SurfaceAlpha(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
	Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@0
   317
	int srcskip = info->s_skip >> 1;
slouken@0
   318
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   319
	int dstskip = info->d_skip >> 1;
slouken@0
   320
	unsigned alpha = info->src->alpha >> 3; /* downscale alpha to 5 bits */
slouken@0
   321
slouken@0
   322
	while(height--) {
slouken@0
   323
	    DUFFS_LOOP4({
slouken@0
   324
		Uint32 s = *srcp++;
slouken@0
   325
		Uint32 d = *dstp;
slouken@0
   326
		/*
slouken@0
   327
		 * shift out the middle component (green) to the high 16
slouken@0
   328
		 * bits, and process all three RGB components at the same
slouken@0
   329
		 * time.
slouken@0
   330
		 */
slouken@0
   331
		s = (s | s << 16) & 0x03e07c1f;
slouken@0
   332
		d = (d | d << 16) & 0x03e07c1f;
slouken@0
   333
		d += (s - d) * alpha >> 5;
slouken@0
   334
		d &= 0x03e07c1f;
slouken@0
   335
		*dstp++ = d | d >> 16;
slouken@0
   336
	    }, width);
slouken@0
   337
	    srcp += srcskip;
slouken@0
   338
	    dstp += dstskip;
slouken@0
   339
	}
slouken@0
   340
}
slouken@0
   341
slouken@0
   342
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@0
   343
static void BlitARGBto565PixelAlpha(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
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   348
	int srcskip = info->s_skip >> 2;
slouken@0
   349
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   350
	int dstskip = info->d_skip >> 1;
slouken@0
   351
slouken@0
   352
	while(height--) {
slouken@0
   353
	    DUFFS_LOOP4({
slouken@0
   354
		Uint32 s = *srcp;
slouken@0
   355
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
   356
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   357
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   358
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   359
		   Benchmark this! */
slouken@0
   360
		if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@0
   361
		    *dstp = (s >> 8 & 0xf800) + (s >> 5 & 0x7e0)
slouken@0
   362
			  + (s >> 3  & 0x1f);
slouken@0
   363
		} else {
slouken@0
   364
		    Uint32 d = *dstp;
slouken@0
   365
		    /*
slouken@0
   366
		     * convert source and destination to G0RAB65565
slouken@0
   367
		     * and blend all components at the same time
slouken@0
   368
		     */
slouken@0
   369
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
   370
		      + (s >> 3 & 0x1f);
slouken@0
   371
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
   372
		    d += (s - d) * alpha >> 5;
slouken@0
   373
		    d &= 0x07e0f81f;
slouken@0
   374
		    *dstp = d | d >> 16;
slouken@0
   375
		}
slouken@0
   376
		srcp++;
slouken@0
   377
		dstp++;
slouken@0
   378
	    }, width);
slouken@0
   379
	    srcp += srcskip;
slouken@0
   380
	    dstp += dstskip;
slouken@0
   381
	}
slouken@0
   382
}
slouken@0
   383
slouken@0
   384
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@0
   385
static void BlitARGBto555PixelAlpha(SDL_BlitInfo *info)
slouken@0
   386
{
slouken@0
   387
	int width = info->d_width;
slouken@0
   388
	int height = info->d_height;
slouken@0
   389
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
   390
	int srcskip = info->s_skip >> 2;
slouken@0
   391
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
   392
	int dstskip = info->d_skip >> 1;
slouken@0
   393
slouken@0
   394
	while(height--) {
slouken@0
   395
	    DUFFS_LOOP4({
slouken@0
   396
		unsigned alpha;
slouken@0
   397
		Uint32 s = *srcp;
slouken@0
   398
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
   399
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
   400
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
   401
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
   402
		   Benchmark this! */
slouken@0
   403
		if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@0
   404
		    *dstp = (s >> 9 & 0x7c00) + (s >> 6 & 0x3e0)
slouken@0
   405
			  + (s >> 3  & 0x1f);
slouken@0
   406
		} else {
slouken@0
   407
		    Uint32 d = *dstp;
slouken@0
   408
		    /*
slouken@0
   409
		     * convert source and destination to G0RAB65565
slouken@0
   410
		     * and blend all components at the same time
slouken@0
   411
		     */
slouken@0
   412
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
   413
		      + (s >> 3 & 0x1f);
slouken@0
   414
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
   415
		    d += (s - d) * alpha >> 5;
slouken@0
   416
		    d &= 0x03e07c1f;
slouken@0
   417
		    *dstp = d | d >> 16;
slouken@0
   418
		}
slouken@0
   419
		srcp++;
slouken@0
   420
		dstp++;
slouken@0
   421
	    }, width);
slouken@0
   422
	    srcp += srcskip;
slouken@0
   423
	    dstp += dstskip;
slouken@0
   424
	}
slouken@0
   425
}
slouken@0
   426
slouken@0
   427
/* General (slow) N->N blending with per-surface alpha */
slouken@0
   428
static void BlitNtoNSurfaceAlpha(SDL_BlitInfo *info)
slouken@0
   429
{
slouken@0
   430
	int width = info->d_width;
slouken@0
   431
	int height = info->d_height;
slouken@0
   432
	Uint8 *src = info->s_pixels;
slouken@0
   433
	int srcskip = info->s_skip;
slouken@0
   434
	Uint8 *dst = info->d_pixels;
slouken@0
   435
	int dstskip = info->d_skip;
slouken@0
   436
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   437
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   438
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   439
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
   440
	unsigned sA = srcfmt->alpha;
slouken@0
   441
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
   442
slouken@0
   443
	while ( height-- ) {
slouken@0
   444
	    DUFFS_LOOP4(
slouken@0
   445
	    {
slouken@0
   446
		Uint32 pixel;
slouken@0
   447
		unsigned sR;
slouken@0
   448
		unsigned sG;
slouken@0
   449
		unsigned sB;
slouken@0
   450
		unsigned dR;
slouken@0
   451
		unsigned dG;
slouken@0
   452
		unsigned dB;
slouken@0
   453
		DISEMBLE_RGB(src, srcbpp, srcfmt, pixel, sR, sG, sB);
slouken@0
   454
		DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@0
   455
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   456
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   457
		src += srcbpp;
slouken@0
   458
		dst += dstbpp;
slouken@0
   459
	    },
slouken@0
   460
	    width);
slouken@0
   461
	    src += srcskip;
slouken@0
   462
	    dst += dstskip;
slouken@0
   463
	}
slouken@0
   464
}
slouken@0
   465
slouken@0
   466
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@0
   467
static void BlitNtoNSurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
   468
{
slouken@0
   469
	int width = info->d_width;
slouken@0
   470
	int height = info->d_height;
slouken@0
   471
	Uint8 *src = info->s_pixels;
slouken@0
   472
	int srcskip = info->s_skip;
slouken@0
   473
	Uint8 *dst = info->d_pixels;
slouken@0
   474
	int dstskip = info->d_skip;
slouken@0
   475
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   476
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   477
	Uint32 ckey = srcfmt->colorkey;
slouken@0
   478
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   479
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
   480
	unsigned sA = srcfmt->alpha;
slouken@0
   481
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
   482
slouken@0
   483
	while ( height-- ) {
slouken@0
   484
	    DUFFS_LOOP4(
slouken@0
   485
	    {
slouken@0
   486
		Uint32 pixel;
slouken@0
   487
		unsigned sR;
slouken@0
   488
		unsigned sG;
slouken@0
   489
		unsigned sB;
slouken@0
   490
		unsigned dR;
slouken@0
   491
		unsigned dG;
slouken@0
   492
		unsigned dB;
slouken@0
   493
		RETRIEVE_RGB_PIXEL(src, srcbpp, pixel);
slouken@0
   494
		if(pixel != ckey) {
slouken@0
   495
		    RGB_FROM_PIXEL(pixel, srcfmt, sR, sG, sB);
slouken@0
   496
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@0
   497
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   498
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   499
		}
slouken@0
   500
		src += srcbpp;
slouken@0
   501
		dst += dstbpp;
slouken@0
   502
	    },
slouken@0
   503
	    width);
slouken@0
   504
	    src += srcskip;
slouken@0
   505
	    dst += dstskip;
slouken@0
   506
	}
slouken@0
   507
}
slouken@0
   508
slouken@0
   509
/* General (slow) N->N blending with pixel alpha */
slouken@0
   510
static void BlitNtoNPixelAlpha(SDL_BlitInfo *info)
slouken@0
   511
{
slouken@0
   512
	int width = info->d_width;
slouken@0
   513
	int height = info->d_height;
slouken@0
   514
	Uint8 *src = info->s_pixels;
slouken@0
   515
	int srcskip = info->s_skip;
slouken@0
   516
	Uint8 *dst = info->d_pixels;
slouken@0
   517
	int dstskip = info->d_skip;
slouken@0
   518
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   519
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   520
slouken@0
   521
	int  srcbpp;
slouken@0
   522
	int  dstbpp;
slouken@0
   523
slouken@0
   524
	/* Set up some basic variables */
slouken@0
   525
	srcbpp = srcfmt->BytesPerPixel;
slouken@0
   526
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
   527
slouken@0
   528
	/* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@0
   529
	   quite right. for <8bpp source alpha, it gets them very wrong
slouken@0
   530
	   (check all macros!)
slouken@0
   531
	   It is unclear whether there is a good general solution that doesn't
slouken@0
   532
	   need a branch (or a divide). */
slouken@0
   533
	while ( height-- ) {
slouken@0
   534
	    DUFFS_LOOP4(
slouken@0
   535
	    {
slouken@0
   536
		Uint32 pixel;
slouken@0
   537
		unsigned sR;
slouken@0
   538
		unsigned sG;
slouken@0
   539
		unsigned sB;
slouken@0
   540
		unsigned dR;
slouken@0
   541
		unsigned dG;
slouken@0
   542
		unsigned dB;
slouken@0
   543
		unsigned sA;
slouken@0
   544
		unsigned dA;
slouken@0
   545
		DISEMBLE_RGBA(src, srcbpp, srcfmt, pixel, sR, sG, sB, sA);
slouken@0
   546
		DISEMBLE_RGBA(dst, dstbpp, dstfmt, pixel, dR, dG, dB, dA);
slouken@0
   547
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   548
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
   549
		src += srcbpp;
slouken@0
   550
		dst += dstbpp;
slouken@0
   551
	    },
slouken@0
   552
	    width);
slouken@0
   553
	    src += srcskip;
slouken@0
   554
	    dst += dstskip;
slouken@0
   555
	}
slouken@0
   556
}
slouken@0
   557
slouken@0
   558
slouken@0
   559
SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int blit_index)
slouken@0
   560
{
slouken@0
   561
    SDL_PixelFormat *sf = surface->format;
slouken@0
   562
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
   563
slouken@0
   564
    if(sf->Amask == 0) {
slouken@0
   565
	if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
   566
	    if(df->BytesPerPixel == 1)
slouken@0
   567
		return BlitNto1SurfaceAlphaKey;
slouken@0
   568
	    else
slouken@0
   569
		return BlitNtoNSurfaceAlphaKey;
slouken@0
   570
	} else {
slouken@0
   571
	    /* Per-surface alpha blits */
slouken@0
   572
	    switch(df->BytesPerPixel) {
slouken@0
   573
	    case 1:
slouken@0
   574
		return BlitNto1SurfaceAlpha;
slouken@0
   575
slouken@0
   576
	    case 2:
slouken@0
   577
		if(surface->map->identity) {
slouken@0
   578
		    if(df->Gmask == 0x7e0)
slouken@0
   579
			return Blit565to565SurfaceAlpha;
slouken@0
   580
		    else if(df->Gmask == 0x3e0)
slouken@0
   581
			return Blit555to555SurfaceAlpha;
slouken@0
   582
		}
slouken@0
   583
		return BlitNtoNSurfaceAlpha;
slouken@0
   584
slouken@0
   585
	    case 4:
slouken@0
   586
		if(sf->Rmask == df->Rmask
slouken@0
   587
		   && sf->Gmask == df->Gmask
slouken@0
   588
		   && sf->Bmask == df->Bmask
slouken@0
   589
		   && (sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff
slouken@0
   590
		   && sf->BytesPerPixel == 4)
slouken@0
   591
		    return BlitRGBtoRGBSurfaceAlpha;
slouken@0
   592
		else
slouken@0
   593
		    return BlitNtoNSurfaceAlpha;
slouken@0
   594
slouken@0
   595
	    case 3:
slouken@0
   596
	    default:
slouken@0
   597
		return BlitNtoNSurfaceAlpha;
slouken@0
   598
	    }
slouken@0
   599
	}
slouken@0
   600
    } else {
slouken@0
   601
	/* Per-pixel alpha blits */
slouken@0
   602
	switch(df->BytesPerPixel) {
slouken@0
   603
	case 1:
slouken@0
   604
	    return BlitNto1PixelAlpha;
slouken@0
   605
slouken@0
   606
	case 2:
slouken@0
   607
	    if(sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@0
   608
	       && sf->Gmask == 0xff00
slouken@0
   609
	       && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@0
   610
		   || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@0
   611
		if(df->Gmask == 0x7e0)
slouken@0
   612
		    return BlitARGBto565PixelAlpha;
slouken@0
   613
		else if(df->Gmask == 0x3e0)
slouken@0
   614
		    return BlitARGBto555PixelAlpha;
slouken@0
   615
	    }
slouken@0
   616
	    return BlitNtoNPixelAlpha;
slouken@0
   617
slouken@0
   618
	case 4:
slouken@0
   619
	    if(sf->Amask == 0xff000000
slouken@0
   620
	       && sf->Rmask == df->Rmask
slouken@0
   621
	       && sf->Gmask == df->Gmask
slouken@0
   622
	       && sf->Bmask == df->Bmask
slouken@0
   623
	       && sf->BytesPerPixel == 4)
slouken@0
   624
		return BlitRGBtoRGBPixelAlpha;
slouken@0
   625
	    return BlitNtoNPixelAlpha;
slouken@0
   626
slouken@0
   627
	case 3:
slouken@0
   628
	default:
slouken@0
   629
	    return BlitNtoNPixelAlpha;
slouken@0
   630
	}
slouken@0
   631
    }
slouken@0
   632
}
slouken@0
   633