src/video/SDL_RLEaccel.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
/*
slouken@0
    29
 * RLE encoding for software colorkey and alpha-channel acceleration
slouken@0
    30
 *
slouken@0
    31
 * Original version by Sam Lantinga
slouken@0
    32
 *
slouken@0
    33
 * Mattias Engdegård (Yorick): Rewrite. New encoding format, encoder and
slouken@0
    34
 * decoder. Added per-surface alpha blitter. Added per-pixel alpha
slouken@0
    35
 * format, encoder and blitter.
slouken@0
    36
 *
slouken@0
    37
 * Many thanks to Xark and johns for hints, benchmarks and useful comments
slouken@0
    38
 * leading to this code.
slouken@0
    39
 *
slouken@0
    40
 * Welcome to Macro Mayhem.
slouken@0
    41
 */
slouken@0
    42
slouken@0
    43
/*
slouken@0
    44
 * The encoding translates the image data to a stream of segments of the form
slouken@0
    45
 *
slouken@0
    46
 * <skip> <run> <data>
slouken@0
    47
 *
slouken@0
    48
 * where <skip> is the number of transparent pixels to skip,
slouken@0
    49
 *       <run>  is the number of opaque pixels to blit,
slouken@0
    50
 * and   <data> are the pixels themselves.
slouken@0
    51
 *
slouken@0
    52
 * This basic structure is used both for colorkeyed surfaces, used for simple
slouken@0
    53
 * binary transparency and for per-surface alpha blending, and for surfaces
slouken@0
    54
 * with per-pixel alpha. The details differ, however:
slouken@0
    55
 *
slouken@0
    56
 * Encoding of colorkeyed surfaces:
slouken@0
    57
 *
slouken@0
    58
 *   Encoded pixels always have the same format as the target surface.
slouken@0
    59
 *   <skip> and <run> are unsigned 8 bit integers, except for 32 bit depth
slouken@0
    60
 *   where they are 16 bit. This makes the pixel data aligned at all times.
slouken@0
    61
 *   Segments never wrap around from one scan line to the next.
slouken@0
    62
 *
slouken@0
    63
 *   The end of the sequence is marked by a zero <skip>,<run> pair at the *
slouken@0
    64
 *   beginning of a line.
slouken@0
    65
 *
slouken@0
    66
 * Encoding of surfaces with per-pixel alpha:
slouken@0
    67
 *
slouken@0
    68
 *   The sequence begins with a struct RLEDestFormat describing the target
slouken@0
    69
 *   pixel format, to provide reliable un-encoding.
slouken@0
    70
 *
slouken@0
    71
 *   Each scan line is encoded twice: First all completely opaque pixels,
slouken@0
    72
 *   encoded in the target format as described above, and then all
slouken@0
    73
 *   partially transparent (translucent) pixels (where 1 <= alpha <= 254),
slouken@0
    74
 *   in the following 32-bit format:
slouken@0
    75
 *
slouken@0
    76
 *   For 32-bit targets, each pixel has the target RGB format but with
slouken@0
    77
 *   the alpha value occupying the highest 8 bits. The <skip> and <run>
slouken@0
    78
 *   counts are 16 bit.
slouken@0
    79
 * 
slouken@0
    80
 *   For 16-bit targets, each pixel has the target RGB format, but with
slouken@0
    81
 *   the middle component (usually green) shifted 16 steps to the left,
slouken@0
    82
 *   and the hole filled with the 5 most significant bits of the alpha value.
slouken@0
    83
 *   i.e. if the target has the format         rrrrrggggggbbbbb,
slouken@0
    84
 *   the encoded pixel will be 00000gggggg00000rrrrr0aaaaabbbbb.
slouken@0
    85
 *   The <skip> and <run> counts are 8 bit for the opaque lines, 16 bit
slouken@0
    86
 *   for the translucent lines. Two padding bytes may be inserted
slouken@0
    87
 *   before each translucent line to keep them 32-bit aligned.
slouken@0
    88
 *
slouken@0
    89
 *   The end of the sequence is marked by a zero <skip>,<run> pair at the
slouken@0
    90
 *   beginning of an opaque line.
slouken@0
    91
 */
slouken@0
    92
slouken@0
    93
#include <stdio.h>
slouken@0
    94
#include <stdlib.h>
slouken@0
    95
#include <string.h>
slouken@0
    96
slouken@0
    97
#include "SDL_types.h"
slouken@0
    98
#include "SDL_video.h"
slouken@0
    99
#include "SDL_error.h"
slouken@0
   100
#include "SDL_sysvideo.h"
slouken@0
   101
#include "SDL_blit.h"
slouken@0
   102
#include "SDL_memops.h"
slouken@0
   103
#include "SDL_RLEaccel_c.h"
slouken@0
   104
slouken@0
   105
#ifndef MAX
slouken@0
   106
#define MAX(a, b) ((a) > (b) ? (a) : (b))
slouken@0
   107
#endif
slouken@0
   108
#ifndef MIN
slouken@0
   109
#define MIN(a, b) ((a) < (b) ? (a) : (b))
slouken@0
   110
#endif
slouken@0
   111
slouken@1
   112
#define PIXEL_COPY(to, from, len, bpp)			\
slouken@1
   113
do {							\
slouken@1
   114
    if(bpp == 4) {					\
slouken@1
   115
	SDL_memcpy4(to, from, (unsigned)(len));		\
slouken@1
   116
    } else {						\
slouken@1
   117
	SDL_memcpy(to, from, (unsigned)(len) * (bpp));	\
slouken@1
   118
    }							\
slouken@1
   119
} while(0)
slouken@1
   120
slouken@0
   121
/*
slouken@0
   122
 * Various colorkey blit methods, for opaque and per-surface alpha
slouken@0
   123
 */
slouken@0
   124
slouken@0
   125
#define OPAQUE_BLIT(to, from, length, bpp, alpha)	\
slouken@1
   126
    PIXEL_COPY(to, from, length, bpp)
slouken@0
   127
slouken@0
   128
/*
slouken@0
   129
 * For 32bpp pixels on the form 0x00rrggbb:
slouken@0
   130
 * If we treat the middle component separately, we can process the two
slouken@0
   131
 * remaining in parallel. This is safe to do because of the gap to the left
slouken@0
   132
 * of each component, so the bits from the multiplication don't collide.
slouken@0
   133
 * This can be used for any RGB permutation of course.
slouken@0
   134
 */
slouken@0
   135
#define ALPHA_BLIT32_888(to, from, length, bpp, alpha)		\
slouken@0
   136
    do {							\
slouken@0
   137
        int i;							\
slouken@0
   138
	Uint32 *src = (Uint32 *)(from);				\
slouken@0
   139
	Uint32 *dst = (Uint32 *)(to);				\
slouken@0
   140
	for(i = 0; i < (int)(length); i++) {			\
slouken@0
   141
	    Uint32 s = *src++;					\
slouken@0
   142
	    Uint32 d = *dst;					\
slouken@0
   143
	    Uint32 s1 = s & 0xff00ff;				\
slouken@0
   144
	    Uint32 d1 = d & 0xff00ff;				\
slouken@0
   145
	    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;	\
slouken@0
   146
	    s &= 0xff00;					\
slouken@0
   147
	    d &= 0xff00;					\
slouken@0
   148
	    d = (d + ((s - d) * alpha >> 8)) & 0xff00;		\
slouken@0
   149
	    *dst++ = d1 | d;					\
slouken@0
   150
	}							\
slouken@0
   151
    } while(0)
slouken@0
   152
slouken@0
   153
/*
slouken@0
   154
 * For 16bpp pixels we can go a step further: put the middle component
slouken@0
   155
 * in the high 16 bits of a 32 bit word, and process all three RGB
slouken@0
   156
 * components at the same time. Since the smallest gap is here just
slouken@0
   157
 * 5 bits, we have to scale alpha down to 5 bits as well.
slouken@0
   158
 */
slouken@0
   159
#define ALPHA_BLIT16_565(to, from, length, bpp, alpha)	\
slouken@0
   160
    do {						\
slouken@0
   161
        int i;						\
slouken@0
   162
	Uint16 *src = (Uint16 *)(from);			\
slouken@0
   163
	Uint16 *dst = (Uint16 *)(to);			\
slouken@0
   164
	for(i = 0; i < (int)(length); i++) {		\
slouken@0
   165
	    Uint32 s = *src++;				\
slouken@0
   166
	    Uint32 d = *dst;				\
slouken@0
   167
	    s = (s | s << 16) & 0x07e0f81f;		\
slouken@0
   168
	    d = (d | d << 16) & 0x07e0f81f;		\
slouken@0
   169
	    d += (s - d) * alpha >> 5;			\
slouken@0
   170
	    d &= 0x07e0f81f;				\
slouken@0
   171
	    *dst++ = d | d >> 16;			\
slouken@0
   172
	}						\
slouken@0
   173
    } while(0)
slouken@0
   174
slouken@0
   175
#define ALPHA_BLIT16_555(to, from, length, bpp, alpha)	\
slouken@0
   176
    do {						\
slouken@0
   177
        int i;						\
slouken@0
   178
	Uint16 *src = (Uint16 *)(from);			\
slouken@0
   179
	Uint16 *dst = (Uint16 *)(to);			\
slouken@0
   180
	for(i = 0; i < (int)(length); i++) {		\
slouken@0
   181
	    Uint32 s = *src++;				\
slouken@0
   182
	    Uint32 d = *dst;				\
slouken@0
   183
	    s = (s | s << 16) & 0x03e07c1f;		\
slouken@0
   184
	    d = (d | d << 16) & 0x03e07c1f;		\
slouken@0
   185
	    d += (s - d) * alpha >> 5;			\
slouken@0
   186
	    d &= 0x03e07c1f;				\
slouken@0
   187
	    *dst++ = d | d >> 16;			\
slouken@0
   188
	}						\
slouken@0
   189
    } while(0)
slouken@0
   190
slouken@0
   191
/*
slouken@0
   192
 * The general slow catch-all function, for remaining depths and formats
slouken@0
   193
 */
slouken@0
   194
#define ALPHA_BLIT_ANY(to, from, length, bpp, alpha)			\
slouken@0
   195
    do {								\
slouken@0
   196
        int i;								\
slouken@0
   197
	Uint8 *src = from;						\
slouken@0
   198
	Uint8 *dst = to;						\
slouken@0
   199
	for(i = 0; i < (int)(length); i++) {				\
slouken@0
   200
	    Uint32 s, d;						\
slouken@0
   201
	    unsigned rs, gs, bs, rd, gd, bd;				\
slouken@0
   202
	    switch(bpp) {						\
slouken@0
   203
	    case 2:							\
slouken@0
   204
		s = *(Uint16 *)src;					\
slouken@0
   205
		d = *(Uint16 *)dst;					\
slouken@0
   206
		break;							\
slouken@0
   207
	    case 3:							\
slouken@0
   208
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {			\
slouken@0
   209
		    s = (src[0] << 16) | (src[1] << 8) | src[2];	\
slouken@0
   210
		    d = (dst[0] << 16) | (dst[1] << 8) | dst[2];	\
slouken@0
   211
		} else {						\
slouken@0
   212
		    s = (src[2] << 16) | (src[1] << 8) | src[0];	\
slouken@0
   213
		    d = (dst[2] << 16) | (dst[1] << 8) | dst[0];	\
slouken@0
   214
		}							\
slouken@0
   215
		break;							\
slouken@0
   216
	    case 4:							\
slouken@0
   217
		s = *(Uint32 *)src;					\
slouken@0
   218
		d = *(Uint32 *)dst;					\
slouken@0
   219
		break;							\
slouken@0
   220
	    }								\
slouken@0
   221
	    RGB_FROM_PIXEL(s, fmt, rs, gs, bs);				\
slouken@0
   222
	    RGB_FROM_PIXEL(d, fmt, rd, gd, bd);				\
slouken@0
   223
	    rd += (rs - rd) * alpha >> 8;				\
slouken@0
   224
	    gd += (gs - gd) * alpha >> 8;				\
slouken@0
   225
	    bd += (bs - bd) * alpha >> 8;				\
slouken@0
   226
	    PIXEL_FROM_RGB(d, fmt, rd, gd, bd);				\
slouken@0
   227
	    switch(bpp) {						\
slouken@0
   228
	    case 2:							\
slouken@0
   229
		*(Uint16 *)dst = d;					\
slouken@0
   230
		break;							\
slouken@0
   231
	    case 3:							\
slouken@0
   232
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {			\
slouken@0
   233
		    dst[0] = d >> 16;					\
slouken@0
   234
		    dst[1] = d >> 8;					\
slouken@0
   235
		    dst[2] = d;						\
slouken@0
   236
		} else {						\
slouken@0
   237
		    dst[0] = d;						\
slouken@0
   238
		    dst[1] = d >> 8;					\
slouken@0
   239
		    dst[2] = d >> 16;					\
slouken@0
   240
		}							\
slouken@0
   241
		break;							\
slouken@0
   242
	    case 4:							\
slouken@0
   243
		*(Uint32 *)dst = d;					\
slouken@0
   244
		break;							\
slouken@0
   245
	    }								\
slouken@0
   246
	    src += bpp;							\
slouken@0
   247
	    dst += bpp;							\
slouken@0
   248
	}								\
slouken@0
   249
    } while(0)
slouken@0
   250
slouken@0
   251
slouken@0
   252
/*
slouken@0
   253
 * Special case: 50% alpha (alpha=128)
slouken@0
   254
 * This is treated specially because it can be optimized very well, and
slouken@0
   255
 * since it is good for many cases of semi-translucency.
slouken@0
   256
 * The theory is to do all three components at the same time:
slouken@0
   257
 * First zero the lowest bit of each component, which gives us room to
slouken@0
   258
 * add them. Then shift right and add the sum of the lowest bits.
slouken@0
   259
 */
slouken@0
   260
#define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha)		\
slouken@0
   261
    do {								\
slouken@0
   262
        int i;								\
slouken@0
   263
	Uint32 *src = (Uint32 *)(from);					\
slouken@0
   264
	Uint32 *dst = (Uint32 *)(to);					\
slouken@0
   265
	for(i = 0; i < (int)(length); i++) {				\
slouken@0
   266
	    Uint32 s = *src++;						\
slouken@0
   267
	    Uint32 d = *dst;						\
slouken@0
   268
	    *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)	\
slouken@0
   269
		     + (s & d & 0x00010101);				\
slouken@0
   270
	}								\
slouken@0
   271
    } while(0)
slouken@0
   272
slouken@0
   273
/*
slouken@0
   274
 * For 16bpp, we can actually blend two pixels in parallel, if we take
slouken@0
   275
 * care to shift before we add, not after.
slouken@0
   276
 */
slouken@0
   277
slouken@0
   278
/* helper: blend a single 16 bit pixel at 50% */
slouken@0
   279
#define BLEND16_50(dst, src, mask)			\
slouken@0
   280
    do {						\
slouken@0
   281
        Uint32 s = *src++;				\
slouken@0
   282
	Uint32 d = *dst;				\
slouken@0
   283
	*dst++ = (((s & mask) + (d & mask)) >> 1)	\
slouken@0
   284
	         + (s & d & (~mask & 0xffff));		\
slouken@0
   285
    } while(0)
slouken@0
   286
slouken@0
   287
/* basic 16bpp blender. mask is the pixels to keep when adding. */
slouken@0
   288
#define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask)		\
slouken@0
   289
    do {								\
slouken@0
   290
	unsigned n = (length);						\
slouken@0
   291
	Uint16 *src = (Uint16 *)(from);					\
slouken@0
   292
	Uint16 *dst = (Uint16 *)(to);					\
slouken@0
   293
	if(((unsigned long)src ^ (unsigned long)dst) & 3) {		\
slouken@0
   294
	    /* source and destination not in phase, blit one by one */	\
slouken@0
   295
	    while(n--)							\
slouken@0
   296
		BLEND16_50(dst, src, mask);				\
slouken@0
   297
	} else {							\
slouken@0
   298
	    if((unsigned long)src & 3) {				\
slouken@0
   299
		/* first odd pixel */					\
slouken@0
   300
		BLEND16_50(dst, src, mask);				\
slouken@0
   301
		n--;							\
slouken@0
   302
	    }								\
slouken@0
   303
	    for(; n > 1; n -= 2) {					\
slouken@0
   304
		Uint32 s = *(Uint32 *)src;				\
slouken@0
   305
		Uint32 d = *(Uint32 *)dst;				\
slouken@0
   306
		*(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1)	\
slouken@0
   307
		               + ((d & (mask | mask << 16)) >> 1)	\
slouken@0
   308
		               + (s & d & (~(mask | mask << 16)));	\
slouken@0
   309
		src += 2;						\
slouken@0
   310
		dst += 2;						\
slouken@0
   311
	    }								\
slouken@0
   312
	    if(n)							\
slouken@0
   313
		BLEND16_50(dst, src, mask); /* last odd pixel */	\
slouken@0
   314
	}								\
slouken@0
   315
    } while(0)
slouken@0
   316
slouken@0
   317
#define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha)	\
slouken@0
   318
    ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7de)
slouken@0
   319
slouken@0
   320
#define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha)	\
slouken@0
   321
    ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbde)
slouken@0
   322
slouken@0
   323
slouken@0
   324
#define CHOOSE_BLIT(blitter, alpha, fmt)				\
slouken@0
   325
    do {								\
slouken@0
   326
        if(alpha == 255) {						\
slouken@0
   327
	    switch(fmt->BytesPerPixel) {				\
slouken@0
   328
	    case 1: blitter(1, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   329
	    case 2: blitter(2, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   330
	    case 3: blitter(3, Uint8, OPAQUE_BLIT); break;		\
slouken@0
   331
	    case 4: blitter(4, Uint16, OPAQUE_BLIT); break;		\
slouken@0
   332
	    }								\
slouken@0
   333
	} else {							\
slouken@0
   334
	    switch(fmt->BytesPerPixel) {				\
slouken@0
   335
	    case 1:							\
slouken@0
   336
		/* No 8bpp alpha blitting */				\
slouken@0
   337
		break;							\
slouken@0
   338
									\
slouken@0
   339
	    case 2:							\
slouken@0
   340
		switch(fmt->Rmask | fmt->Gmask | fmt->Bmask) {		\
slouken@0
   341
		case 0xffff:						\
slouken@0
   342
		    if(fmt->Gmask == 0x07e0				\
slouken@0
   343
		       || fmt->Rmask == 0x07e0				\
slouken@0
   344
		       || fmt->Bmask == 0x07e0) {			\
slouken@0
   345
			if(alpha == 128)				\
slouken@0
   346
			    blitter(2, Uint8, ALPHA_BLIT16_565_50);	\
slouken@0
   347
			else {						\
slouken@0
   348
			    alpha >>= 3; /* use 5 bit alpha */		\
slouken@0
   349
			    blitter(2, Uint8, ALPHA_BLIT16_565);	\
slouken@0
   350
			}						\
slouken@0
   351
		    } else						\
slouken@0
   352
			goto general16;					\
slouken@0
   353
		    break;						\
slouken@0
   354
									\
slouken@0
   355
		case 0x7fff:						\
slouken@0
   356
		    if(fmt->Gmask == 0x03e0				\
slouken@0
   357
		       || fmt->Rmask == 0x03e0				\
slouken@0
   358
		       || fmt->Bmask == 0x03e0) {			\
slouken@0
   359
			if(alpha == 128)				\
slouken@0
   360
			    blitter(2, Uint8, ALPHA_BLIT16_555_50);	\
slouken@0
   361
			else {						\
slouken@0
   362
			    alpha >>= 3; /* use 5 bit alpha */		\
slouken@0
   363
			    blitter(2, Uint8, ALPHA_BLIT16_555);	\
slouken@0
   364
			}						\
slouken@0
   365
			break;						\
slouken@0
   366
		    }							\
slouken@0
   367
		    /* fallthrough */					\
slouken@0
   368
									\
slouken@0
   369
		default:						\
slouken@0
   370
		general16:						\
slouken@0
   371
		    blitter(2, Uint8, ALPHA_BLIT_ANY);			\
slouken@0
   372
		}							\
slouken@0
   373
		break;							\
slouken@0
   374
									\
slouken@0
   375
	    case 3:							\
slouken@0
   376
		blitter(3, Uint8, ALPHA_BLIT_ANY);			\
slouken@0
   377
		break;							\
slouken@0
   378
									\
slouken@0
   379
	    case 4:							\
slouken@0
   380
		if((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff	\
slouken@0
   381
		   && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00	\
slouken@0
   382
		       || fmt->Bmask == 0xff00)) {			\
slouken@0
   383
		    if(alpha == 128)					\
slouken@0
   384
			blitter(4, Uint16, ALPHA_BLIT32_888_50);	\
slouken@0
   385
		    else						\
slouken@0
   386
			blitter(4, Uint16, ALPHA_BLIT32_888);		\
slouken@0
   387
		} else							\
slouken@0
   388
		    blitter(4, Uint16, ALPHA_BLIT_ANY);			\
slouken@0
   389
		break;							\
slouken@0
   390
	    }								\
slouken@0
   391
	}								\
slouken@0
   392
    } while(0)
slouken@0
   393
slouken@0
   394
slouken@0
   395
/*
slouken@0
   396
 * This takes care of the case when the surface is clipped on the left and/or
slouken@0
   397
 * right. Top clipping has already been taken care of.
slouken@0
   398
 */
slouken@0
   399
static void RLEClipBlit(int w, Uint8 *srcbuf, SDL_Surface *dst,
slouken@0
   400
			Uint8 *dstbuf, SDL_Rect *srcrect, unsigned alpha)
slouken@0
   401
{
slouken@0
   402
    SDL_PixelFormat *fmt = dst->format;
slouken@0
   403
slouken@0
   404
#define RLECLIPBLIT(bpp, Type, do_blit)					   \
slouken@0
   405
    do {								   \
slouken@0
   406
	int linecount = srcrect->h;					   \
slouken@0
   407
	int ofs = 0;							   \
slouken@0
   408
	int left = srcrect->x;						   \
slouken@0
   409
	int right = left + srcrect->w;					   \
slouken@0
   410
	dstbuf -= left * bpp;						   \
slouken@0
   411
	for(;;) {							   \
slouken@0
   412
	    int run;							   \
slouken@0
   413
	    ofs += *(Type *)srcbuf;					   \
slouken@0
   414
	    run = ((Type *)srcbuf)[1];					   \
slouken@0
   415
	    srcbuf += 2 * sizeof(Type);					   \
slouken@0
   416
	    if(run) {							   \
slouken@0
   417
		/* clip to left and right borders */			   \
slouken@0
   418
		if(ofs < right) {					   \
slouken@0
   419
		    int start = 0;					   \
slouken@0
   420
		    int len = run;					   \
slouken@0
   421
		    int startcol;					   \
slouken@0
   422
		    if(left - ofs > 0) {				   \
slouken@0
   423
			start = left - ofs;				   \
slouken@0
   424
			len -= start;					   \
slouken@0
   425
			if(len <= 0)					   \
slouken@0
   426
			    goto nocopy ## bpp ## do_blit;		   \
slouken@0
   427
		    }							   \
slouken@0
   428
		    startcol = ofs + start;				   \
slouken@0
   429
		    if(len > right - startcol)				   \
slouken@0
   430
			len = right - startcol;				   \
slouken@0
   431
		    do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
slouken@0
   432
			    len, bpp, alpha);				   \
slouken@0
   433
		}							   \
slouken@0
   434
	    nocopy ## bpp ## do_blit:					   \
slouken@0
   435
		srcbuf += run * bpp;					   \
slouken@0
   436
		ofs += run;						   \
slouken@0
   437
	    } else if(!ofs)						   \
slouken@0
   438
		break;							   \
slouken@0
   439
	    if(ofs == w) {						   \
slouken@0
   440
		ofs = 0;						   \
slouken@0
   441
		dstbuf += dst->pitch;					   \
slouken@0
   442
		if(!--linecount)					   \
slouken@0
   443
		    break;						   \
slouken@0
   444
	    }								   \
slouken@0
   445
	}								   \
slouken@0
   446
    } while(0)
slouken@0
   447
slouken@0
   448
    CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
slouken@0
   449
slouken@0
   450
#undef RLECLIPBLIT
slouken@0
   451
slouken@0
   452
}
slouken@0
   453
slouken@0
   454
slouken@0
   455
/* blit a colorkeyed RLE surface */
slouken@0
   456
int SDL_RLEBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   457
		SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   458
{
slouken@0
   459
	Uint8 *dstbuf;
slouken@0
   460
	Uint8 *srcbuf;
slouken@0
   461
	int x, y;
slouken@0
   462
	int w = src->w;
slouken@0
   463
	unsigned alpha;
slouken@0
   464
slouken@0
   465
	/* Lock the destination if necessary */
slouken@0
   466
	if ( dst->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
   467
		SDL_VideoDevice *video = current_video;
slouken@0
   468
		SDL_VideoDevice *this  = current_video;
slouken@0
   469
		if ( video->LockHWSurface(this, dst) < 0 ) {
slouken@0
   470
			return(-1);
slouken@0
   471
		}
slouken@0
   472
	}
slouken@0
   473
slouken@0
   474
	/* Set up the source and destination pointers */
slouken@0
   475
	x = dstrect->x;
slouken@0
   476
	y = dstrect->y;
slouken@0
   477
	dstbuf = (Uint8 *)dst->pixels + dst->offset
slouken@0
   478
	         + y * dst->pitch + x * src->format->BytesPerPixel;
slouken@0
   479
	srcbuf = (Uint8 *)src->map->sw_data->aux_data;
slouken@0
   480
slouken@0
   481
	{
slouken@0
   482
	    /* skip lines at the top if neccessary */
slouken@0
   483
	    int vskip = srcrect->y;
slouken@0
   484
	    int ofs = 0;
slouken@0
   485
	    if(vskip) {
slouken@0
   486
slouken@0
   487
#define RLESKIP(bpp, Type)			\
slouken@0
   488
		for(;;) {			\
slouken@0
   489
		    int run;			\
slouken@0
   490
		    ofs += *(Type *)srcbuf;	\
slouken@0
   491
		    run = ((Type *)srcbuf)[1];	\
slouken@0
   492
		    srcbuf += sizeof(Type) * 2;	\
slouken@0
   493
		    if(run) {			\
slouken@0
   494
			srcbuf += run * bpp;	\
slouken@0
   495
			ofs += run;		\
slouken@0
   496
		    } else if(!ofs)		\
slouken@0
   497
			goto done;		\
slouken@0
   498
		    if(ofs == w) {		\
slouken@0
   499
			ofs = 0;		\
slouken@0
   500
			if(!--vskip)		\
slouken@0
   501
			    break;		\
slouken@0
   502
		    }				\
slouken@0
   503
		}
slouken@0
   504
slouken@0
   505
		switch(src->format->BytesPerPixel) {
slouken@0
   506
		case 1: RLESKIP(1, Uint8); break;
slouken@0
   507
		case 2: RLESKIP(2, Uint8); break;
slouken@0
   508
		case 3: RLESKIP(3, Uint8); break;
slouken@0
   509
		case 4: RLESKIP(4, Uint16); break;
slouken@0
   510
		}
slouken@0
   511
slouken@0
   512
#undef RLESKIP
slouken@0
   513
slouken@0
   514
	    }
slouken@0
   515
	}
slouken@0
   516
slouken@0
   517
	alpha = (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA
slouken@0
   518
	        ? src->format->alpha : 255;
slouken@0
   519
	/* if left or right edge clipping needed, call clip blit */
slouken@0
   520
	if ( srcrect->x || srcrect->w != src->w ) {
slouken@0
   521
	    RLEClipBlit(w, srcbuf, dst, dstbuf, srcrect, alpha);
slouken@0
   522
	} else {
slouken@0
   523
	    SDL_PixelFormat *fmt = src->format;
slouken@0
   524
slouken@0
   525
#define RLEBLIT(bpp, Type, do_blit)					      \
slouken@0
   526
	    do {							      \
slouken@0
   527
		int linecount = srcrect->h;				      \
slouken@0
   528
		int ofs = 0;						      \
slouken@0
   529
		for(;;) {						      \
slouken@0
   530
		    unsigned run;					      \
slouken@0
   531
		    ofs += *(Type *)srcbuf;				      \
slouken@0
   532
		    run = ((Type *)srcbuf)[1];				      \
slouken@0
   533
		    srcbuf += 2 * sizeof(Type);				      \
slouken@0
   534
		    if(run) {						      \
slouken@0
   535
			do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
slouken@0
   536
			srcbuf += run * bpp;				      \
slouken@0
   537
			ofs += run;					      \
slouken@0
   538
		    } else if(!ofs)					      \
slouken@0
   539
			break;						      \
slouken@0
   540
		    if(ofs == w) {					      \
slouken@0
   541
			ofs = 0;					      \
slouken@0
   542
			dstbuf += dst->pitch;				      \
slouken@0
   543
			if(!--linecount)				      \
slouken@0
   544
			    break;					      \
slouken@0
   545
		    }							      \
slouken@0
   546
		}							      \
slouken@0
   547
	    } while(0)
slouken@0
   548
slouken@0
   549
	    CHOOSE_BLIT(RLEBLIT, alpha, fmt);
slouken@0
   550
slouken@0
   551
#undef RLEBLIT
slouken@0
   552
	}
slouken@0
   553
slouken@0
   554
done:
slouken@0
   555
	/* Unlock the destination if necessary */
slouken@0
   556
	if ( dst->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
   557
		SDL_VideoDevice *video = current_video;
slouken@0
   558
		SDL_VideoDevice *this  = current_video;
slouken@0
   559
		video->UnlockHWSurface(this, dst);
slouken@0
   560
	}
slouken@0
   561
	return(0);
slouken@0
   562
}
slouken@0
   563
slouken@0
   564
#undef OPAQUE_BLIT
slouken@0
   565
slouken@0
   566
/*
slouken@0
   567
 * Per-pixel blitting macros for translucent pixels:
slouken@0
   568
 * These use the same techniques as the per-surface blitting macros
slouken@0
   569
 */
slouken@0
   570
slouken@0
   571
/*
slouken@0
   572
 * For 32bpp pixels, we have made sure the alpha is stored in the top
slouken@0
   573
 * 8 bits, so proceed as usual
slouken@0
   574
 */
slouken@0
   575
#define BLIT_TRANSL_888(src, dst)				\
slouken@0
   576
    do {							\
slouken@0
   577
        Uint32 s = src;						\
slouken@0
   578
	Uint32 d = dst;						\
slouken@0
   579
	unsigned alpha = s >> 24;				\
slouken@0
   580
	Uint32 s1 = s & 0xff00ff;				\
slouken@0
   581
	Uint32 d1 = d & 0xff00ff;				\
slouken@0
   582
	d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;	\
slouken@0
   583
	s &= 0xff00;						\
slouken@0
   584
	d &= 0xff00;						\
slouken@0
   585
	d = (d + ((s - d) * alpha >> 8)) & 0xff00;		\
slouken@0
   586
	dst = d1 | d;						\
slouken@0
   587
    } while(0)
slouken@0
   588
slouken@0
   589
/*
slouken@0
   590
 * For 16bpp pixels, we have stored the 5 most significant alpha bits in
slouken@0
   591
 * bits 5-10. As before, we can process all 3 RGB components at the same time.
slouken@0
   592
 */
slouken@0
   593
#define BLIT_TRANSL_565(src, dst)		\
slouken@0
   594
    do {					\
slouken@0
   595
        Uint32 s = src;				\
slouken@0
   596
	Uint32 d = dst;				\
slouken@0
   597
	unsigned alpha = (s & 0x3e0) >> 5;	\
slouken@0
   598
	s &= 0x07e0f81f;			\
slouken@0
   599
	d = (d | d << 16) & 0x07e0f81f;		\
slouken@0
   600
	d += (s - d) * alpha >> 5;		\
slouken@0
   601
	d &= 0x07e0f81f;			\
slouken@0
   602
	dst = d | d >> 16;			\
slouken@0
   603
    } while(0)
slouken@0
   604
slouken@0
   605
#define BLIT_TRANSL_555(src, dst)		\
slouken@0
   606
    do {					\
slouken@0
   607
        Uint32 s = src;				\
slouken@0
   608
	Uint32 d = dst;				\
slouken@0
   609
	unsigned alpha = (s & 0x3e0) >> 5;	\
slouken@0
   610
	s &= 0x03e07c1f;			\
slouken@0
   611
	d = (d | d << 16) & 0x03e07c1f;		\
slouken@0
   612
	d += (s - d) * alpha >> 5;		\
slouken@0
   613
	d &= 0x03e07c1f;			\
slouken@0
   614
	dst = d | d >> 16;			\
slouken@0
   615
    } while(0)
slouken@0
   616
slouken@0
   617
/* used to save the destination format in the encoding. Designed to be
slouken@0
   618
   macro-compatible with SDL_PixelFormat but without the unneeded fields */
slouken@0
   619
typedef struct {
slouken@0
   620
    	Uint8  BytesPerPixel;
slouken@0
   621
	Uint8  Rloss;
slouken@0
   622
	Uint8  Gloss;
slouken@0
   623
	Uint8  Bloss;
slouken@0
   624
	Uint8  Rshift;
slouken@0
   625
	Uint8  Gshift;
slouken@0
   626
	Uint8  Bshift;
slouken@0
   627
	Uint8  Ashift;
slouken@0
   628
	Uint32 Rmask;
slouken@0
   629
	Uint32 Gmask;
slouken@0
   630
	Uint32 Bmask;
slouken@0
   631
	Uint32 Amask;
slouken@0
   632
} RLEDestFormat;
slouken@0
   633
slouken@0
   634
/* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
slouken@0
   635
static void RLEAlphaClipBlit(int w, Uint8 *srcbuf, SDL_Surface *dst,
slouken@0
   636
			     Uint8 *dstbuf, SDL_Rect *srcrect)
slouken@0
   637
{
slouken@0
   638
    SDL_PixelFormat *df = dst->format;
slouken@0
   639
    /*
slouken@0
   640
     * clipped blitter: Ptype is the destination pixel type,
slouken@0
   641
     * Ctype the translucent count type, and do_blend the macro
slouken@0
   642
     * to blend one pixel.
slouken@0
   643
     */
slouken@0
   644
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend)			  \
slouken@0
   645
    do {								  \
slouken@0
   646
	int linecount = srcrect->h;					  \
slouken@0
   647
	int left = srcrect->x;						  \
slouken@0
   648
	int right = left + srcrect->w;					  \
slouken@0
   649
	dstbuf -= left * sizeof(Ptype);					  \
slouken@0
   650
	do {								  \
slouken@0
   651
	    int ofs = 0;						  \
slouken@0
   652
	    /* blit opaque pixels on one line */			  \
slouken@0
   653
	    do {							  \
slouken@0
   654
		unsigned run;						  \
slouken@0
   655
		ofs += ((Ctype *)srcbuf)[0];				  \
slouken@0
   656
		run = ((Ctype *)srcbuf)[1];				  \
slouken@0
   657
		srcbuf += 2 * sizeof(Ctype);				  \
slouken@0
   658
		if(run) {						  \
slouken@0
   659
		    /* clip to left and right borders */		  \
slouken@0
   660
		    int cofs = ofs;					  \
slouken@0
   661
		    int crun = run;					  \
slouken@0
   662
		    if(left - cofs > 0) {				  \
slouken@0
   663
			crun -= left - cofs;				  \
slouken@0
   664
			cofs = left;					  \
slouken@0
   665
		    }							  \
slouken@0
   666
		    if(crun > right - cofs)				  \
slouken@0
   667
			crun = right - cofs;				  \
slouken@0
   668
		    if(crun > 0)					  \
slouken@1
   669
			PIXEL_COPY(dstbuf + cofs * sizeof(Ptype),	  \
slouken@0
   670
				   srcbuf + (cofs - ofs) * sizeof(Ptype), \
slouken@1
   671
				   (unsigned)crun, sizeof(Ptype));	  \
slouken@0
   672
		    srcbuf += run * sizeof(Ptype);			  \
slouken@0
   673
		    ofs += run;						  \
slouken@0
   674
		} else if(!ofs)						  \
slouken@0
   675
		    return;						  \
slouken@0
   676
	    } while(ofs < w);						  \
slouken@0
   677
	    /* skip padding if necessary */				  \
slouken@0
   678
	    if(sizeof(Ptype) == 2)					  \
slouken@0
   679
		srcbuf += (unsigned long)srcbuf & 2;			  \
slouken@0
   680
	    /* blit translucent pixels on the same line */		  \
slouken@0
   681
	    ofs = 0;							  \
slouken@0
   682
	    do {							  \
slouken@0
   683
		unsigned run;						  \
slouken@0
   684
		ofs += ((Uint16 *)srcbuf)[0];				  \
slouken@0
   685
		run = ((Uint16 *)srcbuf)[1];				  \
slouken@0
   686
		srcbuf += 4;						  \
slouken@0
   687
		if(run) {						  \
slouken@0
   688
		    /* clip to left and right borders */		  \
slouken@0
   689
		    int cofs = ofs;					  \
slouken@0
   690
		    int crun = run;					  \
slouken@0
   691
		    if(left - cofs > 0) {				  \
slouken@0
   692
			crun -= left - cofs;				  \
slouken@0
   693
			cofs = left;					  \
slouken@0
   694
		    }							  \
slouken@0
   695
		    if(crun > right - cofs)				  \
slouken@0
   696
			crun = right - cofs;				  \
slouken@0
   697
		    if(crun > 0) {					  \
slouken@0
   698
			Ptype *dst = (Ptype *)dstbuf + cofs;		  \
slouken@0
   699
			Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs);	  \
slouken@0
   700
			int i;						  \
slouken@0
   701
			for(i = 0; i < crun; i++)			  \
slouken@0
   702
			    do_blend(src[i], dst[i]);			  \
slouken@0
   703
		    }							  \
slouken@0
   704
		    srcbuf += run * 4;					  \
slouken@0
   705
		    ofs += run;						  \
slouken@0
   706
		}							  \
slouken@0
   707
	    } while(ofs < w);						  \
slouken@0
   708
	    dstbuf += dst->pitch;					  \
slouken@0
   709
	} while(--linecount);						  \
slouken@0
   710
    } while(0)
slouken@0
   711
slouken@0
   712
    switch(df->BytesPerPixel) {
slouken@0
   713
    case 2:
slouken@0
   714
	if(df->Gmask == 0x07e0 || df->Rmask == 0x07e0
slouken@0
   715
	   || df->Bmask == 0x07e0)
slouken@0
   716
	    RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@0
   717
	else
slouken@0
   718
	    RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@0
   719
	break;
slouken@0
   720
    case 4:
slouken@0
   721
	RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@0
   722
	break;
slouken@0
   723
    }
slouken@0
   724
}
slouken@0
   725
slouken@0
   726
/* blit a pixel-alpha RLE surface */
slouken@0
   727
int SDL_RLEAlphaBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   728
		     SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   729
{
slouken@0
   730
    int x, y;
slouken@0
   731
    int w = src->w;
slouken@0
   732
    Uint8 *srcbuf, *dstbuf;
slouken@0
   733
    SDL_PixelFormat *df = dst->format;
slouken@0
   734
slouken@0
   735
    /* Lock the destination if necessary */
slouken@0
   736
    if(dst->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT)) {
slouken@0
   737
	SDL_VideoDevice *video = current_video;
slouken@0
   738
	SDL_VideoDevice *this  = current_video;
slouken@0
   739
	if(video->LockHWSurface(this, dst) < 0) {
slouken@0
   740
	    return -1;
slouken@0
   741
	}
slouken@0
   742
    }
slouken@0
   743
slouken@0
   744
    x = dstrect->x;
slouken@0
   745
    y = dstrect->y;
slouken@0
   746
    dstbuf = (Uint8 *)dst->pixels + dst->offset
slouken@0
   747
	     + y * dst->pitch + x * df->BytesPerPixel;
slouken@0
   748
    srcbuf = (Uint8 *)src->map->sw_data->aux_data + sizeof(RLEDestFormat);
slouken@0
   749
slouken@0
   750
    {
slouken@0
   751
	/* skip lines at the top if necessary */
slouken@0
   752
	int vskip = srcrect->y;
slouken@0
   753
	if(vskip) {
slouken@0
   754
	    int ofs;
slouken@0
   755
	    if(df->BytesPerPixel == 2) {
slouken@0
   756
		/* the 16/32 interleaved format */
slouken@0
   757
		do {
slouken@0
   758
		    /* skip opaque line */
slouken@0
   759
		    ofs = 0;
slouken@0
   760
		    do {
slouken@0
   761
			int run;
slouken@0
   762
			ofs += srcbuf[0];
slouken@0
   763
			run = srcbuf[1];
slouken@0
   764
			srcbuf += 2;
slouken@0
   765
			if(run) {
slouken@0
   766
			    srcbuf += 2 * run;
slouken@0
   767
			    ofs += run;
slouken@0
   768
			} else if(!ofs)
slouken@0
   769
			    goto done;
slouken@0
   770
		    } while(ofs < w);
slouken@0
   771
slouken@0
   772
		    /* skip padding */
slouken@0
   773
		    srcbuf += (unsigned long)srcbuf & 2;
slouken@0
   774
slouken@0
   775
		    /* skip translucent line */
slouken@0
   776
		    ofs = 0;
slouken@0
   777
		    do {
slouken@0
   778
			int run;
slouken@0
   779
			ofs += ((Uint16 *)srcbuf)[0];
slouken@0
   780
			run = ((Uint16 *)srcbuf)[1];
slouken@0
   781
			srcbuf += 4 * (run + 1);
slouken@0
   782
			ofs += run;
slouken@0
   783
		    } while(ofs < w);
slouken@0
   784
		} while(--vskip);
slouken@0
   785
	    } else {
slouken@0
   786
		/* the 32/32 interleaved format */
slouken@0
   787
		vskip <<= 1;	/* opaque and translucent have same format */
slouken@0
   788
		do {
slouken@0
   789
		    ofs = 0;
slouken@0
   790
		    do {
slouken@0
   791
			int run;
slouken@0
   792
			ofs += ((Uint16 *)srcbuf)[0];
slouken@0
   793
			run = ((Uint16 *)srcbuf)[1];
slouken@0
   794
			srcbuf += 4;
slouken@0
   795
			if(run) {
slouken@0
   796
			    srcbuf += 4 * run;
slouken@0
   797
			    ofs += run;
slouken@0
   798
			} else if(!ofs)
slouken@0
   799
			    goto done;
slouken@0
   800
		    } while(ofs < w);
slouken@0
   801
		} while(--vskip);
slouken@0
   802
	    }
slouken@0
   803
	}
slouken@0
   804
    }
slouken@0
   805
slouken@0
   806
    /* if left or right edge clipping needed, call clip blit */
slouken@0
   807
    if(srcrect->x || srcrect->w != src->w) {
slouken@0
   808
	RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect);
slouken@0
   809
    } else {
slouken@0
   810
slouken@0
   811
	/*
slouken@0
   812
	 * non-clipped blitter. Ptype is the destination pixel type,
slouken@0
   813
	 * Ctype the translucent count type, and do_blend the
slouken@0
   814
	 * macro to blend one pixel.
slouken@0
   815
	 */
slouken@0
   816
#define RLEALPHABLIT(Ptype, Ctype, do_blend)				 \
slouken@0
   817
	do {								 \
slouken@0
   818
	    int linecount = srcrect->h;					 \
slouken@0
   819
	    do {							 \
slouken@0
   820
		int ofs = 0;						 \
slouken@0
   821
		/* blit opaque pixels on one line */			 \
slouken@0
   822
		do {							 \
slouken@0
   823
		    unsigned run;					 \
slouken@0
   824
		    ofs += ((Ctype *)srcbuf)[0];			 \
slouken@0
   825
		    run = ((Ctype *)srcbuf)[1];				 \
slouken@0
   826
		    srcbuf += 2 * sizeof(Ctype);			 \
slouken@0
   827
		    if(run) {						 \
slouken@1
   828
			PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
slouken@1
   829
				   run, sizeof(Ptype));			 \
slouken@0
   830
			srcbuf += run * sizeof(Ptype);			 \
slouken@0
   831
			ofs += run;					 \
slouken@0
   832
		    } else if(!ofs)					 \
slouken@0
   833
			goto done;					 \
slouken@0
   834
		} while(ofs < w);					 \
slouken@0
   835
		/* skip padding if necessary */				 \
slouken@0
   836
		if(sizeof(Ptype) == 2)					 \
slouken@0
   837
		    srcbuf += (unsigned long)srcbuf & 2;		 \
slouken@0
   838
		/* blit translucent pixels on the same line */		 \
slouken@0
   839
		ofs = 0;						 \
slouken@0
   840
		do {							 \
slouken@0
   841
		    unsigned run;					 \
slouken@0
   842
		    ofs += ((Uint16 *)srcbuf)[0];			 \
slouken@0
   843
		    run = ((Uint16 *)srcbuf)[1];			 \
slouken@0
   844
		    srcbuf += 4;					 \
slouken@0
   845
		    if(run) {						 \
slouken@0
   846
			Ptype *dst = (Ptype *)dstbuf + ofs;		 \
slouken@0
   847
			unsigned i;					 \
slouken@0
   848
			for(i = 0; i < run; i++) {			 \
slouken@0
   849
			    Uint32 src = *(Uint32 *)srcbuf;		 \
slouken@0
   850
			    do_blend(src, *dst);			 \
slouken@0
   851
			    srcbuf += 4;				 \
slouken@0
   852
			    dst++;					 \
slouken@0
   853
			}						 \
slouken@0
   854
			ofs += run;					 \
slouken@0
   855
		    }							 \
slouken@0
   856
		} while(ofs < w);					 \
slouken@0
   857
		dstbuf += dst->pitch;					 \
slouken@0
   858
	    } while(--linecount);					 \
slouken@0
   859
	} while(0)
slouken@0
   860
slouken@0
   861
	switch(df->BytesPerPixel) {
slouken@0
   862
	case 2:
slouken@0
   863
	    if(df->Gmask == 0x07e0 || df->Rmask == 0x07e0
slouken@0
   864
	       || df->Bmask == 0x07e0)
slouken@0
   865
		RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@0
   866
	    else
slouken@0
   867
		RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@0
   868
	    break;
slouken@0
   869
	case 4:
slouken@0
   870
	    RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@0
   871
	    break;
slouken@0
   872
	}
slouken@0
   873
    }
slouken@0
   874
slouken@0
   875
 done:
slouken@0
   876
    /* Unlock the destination if necessary */
slouken@0
   877
    if(dst->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT)) {
slouken@0
   878
	SDL_VideoDevice *video = current_video;
slouken@0
   879
	SDL_VideoDevice *this  = current_video;
slouken@0
   880
	video->UnlockHWSurface(this, dst);
slouken@0
   881
    }
slouken@0
   882
    return 0;
slouken@0
   883
}
slouken@0
   884
slouken@0
   885
/*
slouken@0
   886
 * Auxiliary functions:
slouken@0
   887
 * The encoding functions take 32bpp rgb + a, and
slouken@0
   888
 * return the number of bytes copied to the destination.
slouken@0
   889
 * The decoding functions copy to 32bpp rgb + a, and
slouken@0
   890
 * return the number of bytes copied from the source.
slouken@0
   891
 * These are only used in the encoder and un-RLE code and are therefore not
slouken@0
   892
 * highly optimised.
slouken@0
   893
 */
slouken@0
   894
slouken@0
   895
/* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
slouken@0
   896
static int copy_opaque_16(void *dst, Uint32 *src, int n,
slouken@0
   897
			  SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   898
{
slouken@0
   899
    int i;
slouken@0
   900
    Uint16 *d = dst;
slouken@0
   901
    for(i = 0; i < n; i++) {
slouken@0
   902
	unsigned r, g, b;
slouken@0
   903
	RGB_FROM_PIXEL(*src, sfmt, r, g, b);
slouken@0
   904
	PIXEL_FROM_RGB(*d, dfmt, r, g, b);
slouken@0
   905
	src++;
slouken@0
   906
	d++;
slouken@0
   907
    }
slouken@0
   908
    return n * 2;
slouken@0
   909
}
slouken@0
   910
slouken@0
   911
/* decode opaque pixels from 16bpp to 32bpp rgb + a */
slouken@0
   912
static int uncopy_opaque_16(Uint32 *dst, void *src, int n,
slouken@0
   913
			    RLEDestFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   914
{
slouken@0
   915
    int i;
slouken@0
   916
    Uint16 *s = src;
slouken@0
   917
    unsigned alpha = dfmt->Amask ? 255 : 0;
slouken@0
   918
    for(i = 0; i < n; i++) {
slouken@0
   919
	unsigned r, g, b;
slouken@0
   920
	RGB_FROM_PIXEL(*s, sfmt, r, g, b);
slouken@0
   921
	PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
slouken@0
   922
	s++;
slouken@0
   923
	dst++;
slouken@0
   924
    }
slouken@0
   925
    return n * 2;
slouken@0
   926
}
slouken@0
   927
slouken@0
   928
slouken@0
   929
slouken@0
   930
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
slouken@0
   931
static int copy_transl_565(void *dst, Uint32 *src, int n,
slouken@0
   932
			   SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   933
{
slouken@0
   934
    int i;
slouken@0
   935
    Uint32 *d = dst;
slouken@0
   936
    for(i = 0; i < n; i++) {
slouken@0
   937
	unsigned r, g, b, a;
slouken@0
   938
	Uint16 pix;
slouken@0
   939
	RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@0
   940
	PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@0
   941
	*d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
slouken@0
   942
	src++;
slouken@0
   943
	d++;
slouken@0
   944
    }
slouken@0
   945
    return n * 4;
slouken@0
   946
}
slouken@0
   947
slouken@0
   948
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
slouken@0
   949
static int copy_transl_555(void *dst, Uint32 *src, int n,
slouken@0
   950
			   SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   951
{
slouken@0
   952
    int i;
slouken@0
   953
    Uint32 *d = dst;
slouken@0
   954
    for(i = 0; i < n; i++) {
slouken@0
   955
	unsigned r, g, b, a;
slouken@0
   956
	Uint16 pix;
slouken@0
   957
	RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@0
   958
	PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@0
   959
	*d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
slouken@0
   960
	src++;
slouken@0
   961
	d++;
slouken@0
   962
    }
slouken@0
   963
    return n * 4;
slouken@0
   964
}
slouken@0
   965
slouken@0
   966
/* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
slouken@0
   967
static int uncopy_transl_16(Uint32 *dst, void *src, int n,
slouken@0
   968
			    RLEDestFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   969
{
slouken@0
   970
    int i;
slouken@0
   971
    Uint32 *s = src;
slouken@0
   972
    for(i = 0; i < n; i++) {
slouken@0
   973
	unsigned r, g, b, a;
slouken@0
   974
	Uint32 pix = *s++;
slouken@0
   975
	a = (pix & 0x3e0) >> 2;
slouken@0
   976
	pix = (pix & ~0x3e0) | pix >> 16;
slouken@0
   977
	RGB_FROM_PIXEL(pix, sfmt, r, g, b);
slouken@0
   978
	PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
slouken@0
   979
	dst++;
slouken@0
   980
    }
slouken@0
   981
    return n * 4;
slouken@0
   982
}
slouken@0
   983
slouken@0
   984
/* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
slouken@0
   985
static int copy_32(void *dst, Uint32 *src, int n,
slouken@0
   986
		   SDL_PixelFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
   987
{
slouken@0
   988
    int i;
slouken@0
   989
    Uint32 *d = dst;
slouken@0
   990
    for(i = 0; i < n; i++) {
slouken@0
   991
	unsigned r, g, b, a;
slouken@0
   992
	Uint32 pixel;
slouken@0
   993
	RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@0
   994
	PIXEL_FROM_RGB(pixel, dfmt, r, g, b);
slouken@0
   995
	*d++ = pixel | a << 24;
slouken@0
   996
	src++;
slouken@0
   997
    }
slouken@0
   998
    return n * 4;
slouken@0
   999
}
slouken@0
  1000
slouken@0
  1001
/* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
slouken@0
  1002
static int uncopy_32(Uint32 *dst, void *src, int n,
slouken@0
  1003
		     RLEDestFormat *sfmt, SDL_PixelFormat *dfmt)
slouken@0
  1004
{
slouken@0
  1005
    int i;
slouken@0
  1006
    Uint32 *s = src;
slouken@0
  1007
    for(i = 0; i < n; i++) {
slouken@0
  1008
	unsigned r, g, b, a;
slouken@0
  1009
	Uint32 pixel = *s++;
slouken@0
  1010
	RGB_FROM_PIXEL(pixel, sfmt, r, g, b);
slouken@0
  1011
	a = pixel >> 24;
slouken@0
  1012
	PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
slouken@0
  1013
	dst++;
slouken@0
  1014
    }
slouken@0
  1015
    return n * 4;
slouken@0
  1016
}
slouken@0
  1017
slouken@0
  1018
#define ISOPAQUE(pixel, fmt) ((((pixel) & fmt->Amask) >> fmt->Ashift) == 255)
slouken@0
  1019
slouken@0
  1020
#define ISTRANSL(pixel, fmt)	\
slouken@0
  1021
    ((unsigned)((((pixel) & fmt->Amask) >> fmt->Ashift) - 1U) < 254U)
slouken@0
  1022
slouken@0
  1023
/* convert surface to be quickly alpha-blittable onto dest, if possible */
slouken@0
  1024
static int RLEAlphaSurface(SDL_Surface *surface)
slouken@0
  1025
{
slouken@0
  1026
    SDL_Surface *dest;
slouken@0
  1027
    SDL_PixelFormat *df;
slouken@0
  1028
    int maxsize = 0;
slouken@0
  1029
    int max_opaque_run;
slouken@0
  1030
    int max_transl_run = 65535;
slouken@0
  1031
    unsigned masksum;
slouken@0
  1032
    Uint8 *rlebuf, *dst;
slouken@0
  1033
    int (*copy_opaque)(void *, Uint32 *, int,
slouken@0
  1034
		       SDL_PixelFormat *, SDL_PixelFormat *);
slouken@0
  1035
    int (*copy_transl)(void *, Uint32 *, int,
slouken@0
  1036
		       SDL_PixelFormat *, SDL_PixelFormat *);
slouken@0
  1037
slouken@0
  1038
    dest = surface->map->dst;
slouken@0
  1039
    if(!dest)
slouken@0
  1040
	return -1;
slouken@0
  1041
    df = dest->format;
slouken@0
  1042
    if(surface->format->BitsPerPixel != 32)
slouken@0
  1043
	return -1;		/* only 32bpp source supported */
slouken@0
  1044
slouken@0
  1045
    /* find out whether the destination is one we support,
slouken@0
  1046
       and determine the max size of the encoded result */
slouken@0
  1047
    masksum = df->Rmask | df->Gmask | df->Bmask;
slouken@0
  1048
    switch(df->BytesPerPixel) {
slouken@0
  1049
    case 2:
slouken@0
  1050
	/* 16bpp: only support 565 and 555 formats */
slouken@0
  1051
	switch(masksum) {
slouken@0
  1052
	case 0xffff:
slouken@0
  1053
	    if(df->Gmask == 0x07e0
slouken@0
  1054
	       || df->Rmask == 0x07e0 || df->Bmask == 0x07e0) {
slouken@0
  1055
		copy_opaque = copy_opaque_16;
slouken@0
  1056
		copy_transl = copy_transl_565;
slouken@0
  1057
	    } else
slouken@0
  1058
		return -1;
slouken@0
  1059
	    break;
slouken@0
  1060
	case 0x7fff:
slouken@0
  1061
	    if(df->Gmask == 0x03e0
slouken@0
  1062
	       || df->Rmask == 0x03e0 || df->Bmask == 0x03e0) {
slouken@0
  1063
		copy_opaque = copy_opaque_16;
slouken@0
  1064
		copy_transl = copy_transl_555;
slouken@0
  1065
	    } else
slouken@0
  1066
		return -1;
slouken@0
  1067
	    break;
slouken@0
  1068
	default:
slouken@0
  1069
	    return -1;
slouken@0
  1070
	}
slouken@0
  1071
	max_opaque_run = 255;	/* runs stored as bytes */
slouken@0
  1072
slouken@0
  1073
	/* worst case is alternating opaque and translucent pixels,
slouken@0
  1074
	   with room for alignment padding between lines */
slouken@0
  1075
	maxsize = surface->h * (2 + (4 + 2) * (surface->w + 1)) + 2;
slouken@0
  1076
	break;
slouken@0
  1077
    case 4:
slouken@0
  1078
	if(masksum != 0x00ffffff)
slouken@0
  1079
	    return -1;		/* requires unused high byte */
slouken@0
  1080
	copy_opaque = copy_32;
slouken@0
  1081
	copy_transl = copy_32;
slouken@0
  1082
	max_opaque_run = 255;	/* runs stored as short ints */
slouken@0
  1083
slouken@0
  1084
	/* worst case is alternating opaque and translucent pixels */
slouken@0
  1085
	maxsize = surface->h * 2 * 4 * (surface->w + 1) + 4;
slouken@0
  1086
	break;
slouken@0
  1087
    default:
slouken@0
  1088
	return -1;		/* anything else unsupported right now */
slouken@0
  1089
    }
slouken@0
  1090
slouken@0
  1091
    maxsize += sizeof(RLEDestFormat);
slouken@0
  1092
    rlebuf = (Uint8 *)malloc(maxsize);
slouken@0
  1093
    if(!rlebuf) {
slouken@0
  1094
	SDL_OutOfMemory();
slouken@0
  1095
	return -1;
slouken@0
  1096
    }
slouken@0
  1097
    {
slouken@0
  1098
	/* save the destination format so we can undo the encoding later */
slouken@0
  1099
	RLEDestFormat *r = (RLEDestFormat *)rlebuf;
slouken@0
  1100
	r->BytesPerPixel = df->BytesPerPixel;
slouken@0
  1101
	r->Rloss = df->Rloss;
slouken@0
  1102
	r->Gloss = df->Gloss;
slouken@0
  1103
	r->Bloss = df->Bloss;
slouken@0
  1104
	r->Rshift = df->Rshift;
slouken@0
  1105
	r->Gshift = df->Gshift;
slouken@0
  1106
	r->Bshift = df->Bshift;
slouken@0
  1107
	r->Ashift = df->Ashift;
slouken@0
  1108
	r->Rmask = df->Rmask;
slouken@0
  1109
	r->Gmask = df->Gmask;
slouken@0
  1110
	r->Bmask = df->Bmask;
slouken@0
  1111
	r->Amask = df->Amask;
slouken@0
  1112
    }
slouken@0
  1113
    dst = rlebuf + sizeof(RLEDestFormat);
slouken@0
  1114
slouken@0
  1115
    /* Do the actual encoding */
slouken@0
  1116
    {
slouken@0
  1117
	int x, y;
slouken@0
  1118
	int h = surface->h, w = surface->w;
slouken@0
  1119
	SDL_PixelFormat *sf = surface->format;
slouken@0
  1120
	Uint32 *src = (Uint32 *)((Uint8 *)surface->pixels + surface->offset);
slouken@0
  1121
	Uint8 *lastline = dst;	/* end of last non-blank line */
slouken@0
  1122
slouken@0
  1123
	/* opaque counts are 8 or 16 bits, depending on target depth */
slouken@0
  1124
#define ADD_OPAQUE_COUNTS(n, m)			\
slouken@0
  1125
	if(df->BytesPerPixel == 4) {		\
slouken@0
  1126
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1127
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1128
	    dst += 4;				\
slouken@0
  1129
	} else {				\
slouken@0
  1130
	    dst[0] = n;				\
slouken@0
  1131
	    dst[1] = m;				\
slouken@0
  1132
	    dst += 2;				\
slouken@0
  1133
	}
slouken@0
  1134
slouken@0
  1135
	/* translucent counts are always 16 bit */
slouken@0
  1136
#define ADD_TRANSL_COUNTS(n, m)		\
slouken@0
  1137
	(((Uint16 *)dst)[0] = n, ((Uint16 *)dst)[1] = m, dst += 4)
slouken@0
  1138
slouken@0
  1139
	for(y = 0; y < h; y++) {
slouken@0
  1140
	    int runstart, skipstart;
slouken@0
  1141
	    int blankline = 0;
slouken@0
  1142
	    /* First encode all opaque pixels of a scan line */
slouken@0
  1143
	    x = 0;
slouken@0
  1144
	    do {
slouken@0
  1145
		int run, skip, len;
slouken@0
  1146
		skipstart = x;
slouken@0
  1147
		while(x < w && !ISOPAQUE(src[x], sf))
slouken@0
  1148
		    x++;
slouken@0
  1149
		runstart = x;
slouken@0
  1150
		while(x < w && ISOPAQUE(src[x], sf))
slouken@0
  1151
		    x++;
slouken@0
  1152
		skip = runstart - skipstart;
slouken@0
  1153
		if(skip == w)
slouken@0
  1154
		    blankline = 1;
slouken@0
  1155
		run = x - runstart;
slouken@0
  1156
		while(skip > max_opaque_run) {
slouken@0
  1157
		    ADD_OPAQUE_COUNTS(max_opaque_run, 0);
slouken@0
  1158
		    skip -= max_opaque_run;
slouken@0
  1159
		}
slouken@0
  1160
		len = MIN(run, max_opaque_run);
slouken@0
  1161
		ADD_OPAQUE_COUNTS(skip, len);
slouken@0
  1162
		dst += copy_opaque(dst, src + runstart, len, sf, df);
slouken@0
  1163
		runstart += len;
slouken@0
  1164
		run -= len;
slouken@0
  1165
		while(run) {
slouken@0
  1166
		    len = MIN(run, max_opaque_run);
slouken@0
  1167
		    ADD_OPAQUE_COUNTS(0, len);
slouken@0
  1168
		    dst += copy_opaque(dst, src + runstart, len, sf, df);
slouken@0
  1169
		    runstart += len;
slouken@0
  1170
		    run -= len;
slouken@0
  1171
		}
slouken@0
  1172
	    } while(x < w);
slouken@0
  1173
slouken@0
  1174
	    /* Make sure the next output address is 32-bit aligned */
slouken@0
  1175
	    dst += (unsigned long)dst & 2;
slouken@0
  1176
slouken@0
  1177
	    /* Next, encode all translucent pixels of the same scan line */
slouken@0
  1178
	    x = 0;
slouken@0
  1179
	    do {
slouken@0
  1180
		int run, skip, len;
slouken@0
  1181
		skipstart = x;
slouken@0
  1182
		while(x < w && !ISTRANSL(src[x], sf))
slouken@0
  1183
		    x++;
slouken@0
  1184
		runstart = x;
slouken@0
  1185
		while(x < w && ISTRANSL(src[x], sf))
slouken@0
  1186
		    x++;
slouken@0
  1187
		skip = runstart - skipstart;
slouken@0
  1188
		blankline &= (skip == w);
slouken@0
  1189
		run = x - runstart;
slouken@0
  1190
		while(skip > max_transl_run) {
slouken@0
  1191
		    ADD_TRANSL_COUNTS(max_transl_run, 0);
slouken@0
  1192
		    skip -= max_transl_run;
slouken@0
  1193
		}
slouken@0
  1194
		len = MIN(run, max_transl_run);
slouken@0
  1195
		ADD_TRANSL_COUNTS(skip, len);
slouken@0
  1196
		dst += copy_transl(dst, src + runstart, len, sf, df);
slouken@0
  1197
		runstart += len;
slouken@0
  1198
		run -= len;
slouken@0
  1199
		while(run) {
slouken@0
  1200
		    len = MIN(run, max_transl_run);
slouken@0
  1201
		    ADD_TRANSL_COUNTS(0, len);
slouken@0
  1202
		    dst += copy_transl(dst, src + runstart, len, sf, df);
slouken@0
  1203
		    runstart += len;
slouken@0
  1204
		    run -= len;
slouken@0
  1205
		}
slouken@0
  1206
		if(!blankline)
slouken@0
  1207
		    lastline = dst;
slouken@0
  1208
	    } while(x < w);
slouken@0
  1209
slouken@0
  1210
	    src += surface->pitch >> 2;
slouken@0
  1211
	}
slouken@0
  1212
	dst = lastline;		/* back up past trailing blank lines */
slouken@0
  1213
	ADD_OPAQUE_COUNTS(0, 0);
slouken@0
  1214
    }
slouken@0
  1215
slouken@0
  1216
#undef ADD_OPAQUE_COUNTS
slouken@0
  1217
#undef ADD_TRANSL_COUNTS
slouken@0
  1218
slouken@0
  1219
    /* Now that we have it encoded, release the original pixels */
slouken@0
  1220
    if((surface->flags & SDL_PREALLOC) != SDL_PREALLOC
slouken@0
  1221
       && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
slouken@0
  1222
	free( surface->pixels );
slouken@0
  1223
	surface->pixels = NULL;
slouken@0
  1224
    }
slouken@0
  1225
slouken@0
  1226
    /* realloc the buffer to release unused memory */
slouken@0
  1227
    {
slouken@0
  1228
	Uint8 *p = realloc(rlebuf, dst - rlebuf);
slouken@0
  1229
	if(!p)
slouken@0
  1230
	    p = rlebuf;
slouken@0
  1231
	surface->map->sw_data->aux_data = p;
slouken@0
  1232
    }
slouken@0
  1233
slouken@0
  1234
    return 0;
slouken@0
  1235
}
slouken@0
  1236
slouken@0
  1237
static Uint32 getpix_8(Uint8 *srcbuf)
slouken@0
  1238
{
slouken@0
  1239
    return *srcbuf;
slouken@0
  1240
}
slouken@0
  1241
slouken@0
  1242
static Uint32 getpix_16(Uint8 *srcbuf)
slouken@0
  1243
{
slouken@0
  1244
    return *(Uint16 *)srcbuf;
slouken@0
  1245
}
slouken@0
  1246
slouken@0
  1247
static Uint32 getpix_24(Uint8 *srcbuf)
slouken@0
  1248
{
slouken@0
  1249
    if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
slouken@0
  1250
	return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
slouken@0
  1251
    else
slouken@0
  1252
	return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
slouken@0
  1253
}
slouken@0
  1254
slouken@0
  1255
static Uint32 getpix_32(Uint8 *srcbuf)
slouken@0
  1256
{
slouken@0
  1257
    return *(Uint32 *)srcbuf;
slouken@0
  1258
}
slouken@0
  1259
slouken@0
  1260
typedef Uint32 (*getpix_func)(Uint8 *);
slouken@0
  1261
slouken@0
  1262
static getpix_func getpixes[4] = {
slouken@0
  1263
    getpix_8, getpix_16, getpix_24, getpix_32
slouken@0
  1264
};
slouken@0
  1265
slouken@0
  1266
static int RLEColorkeySurface(SDL_Surface *surface)
slouken@0
  1267
{
slouken@0
  1268
        Uint8 *rlebuf, *dst;
slouken@0
  1269
	int maxn;
slouken@0
  1270
	int y;
slouken@0
  1271
	Uint8 *srcbuf, *curbuf, *lastline;
slouken@0
  1272
	int maxsize = 0;
slouken@0
  1273
	int skip, run;
slouken@0
  1274
	int bpp = surface->format->BytesPerPixel;
slouken@0
  1275
	getpix_func getpix;
slouken@0
  1276
	Uint32 ckey, rgbmask;
slouken@0
  1277
	int w, h;
slouken@0
  1278
slouken@0
  1279
	/* calculate the worst case size for the compressed surface */
slouken@0
  1280
	switch(bpp) {
slouken@0
  1281
	case 1:
slouken@0
  1282
	    /* worst case is alternating opaque and transparent pixels,
slouken@0
  1283
	       starting with an opaque pixel */
slouken@0
  1284
	    maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
slouken@0
  1285
	    break;
slouken@0
  1286
	case 2:
slouken@0
  1287
	case 3:
slouken@0
  1288
	    /* worst case is solid runs, at most 255 pixels wide */
slouken@0
  1289
	    maxsize = surface->h * (2 * (surface->w / 255 + 1)
slouken@0
  1290
				    + surface->w * bpp) + 2;
slouken@0
  1291
	    break;
slouken@0
  1292
	case 4:
slouken@0
  1293
	    /* worst case is solid runs, at most 65535 pixels wide */
slouken@0
  1294
	    maxsize = surface->h * (4 * (surface->w / 65535 + 1)
slouken@0
  1295
				    + surface->w * 4) + 4;
slouken@0
  1296
	    break;
slouken@0
  1297
	}
slouken@0
  1298
slouken@0
  1299
	rlebuf = (Uint8 *)malloc(maxsize);
slouken@0
  1300
	if ( rlebuf == NULL ) {
slouken@0
  1301
		SDL_OutOfMemory();
slouken@0
  1302
		return(-1);
slouken@0
  1303
	}
slouken@0
  1304
slouken@0
  1305
	/* Set up the conversion */
slouken@0
  1306
	srcbuf = (Uint8 *)surface->pixels+surface->offset;
slouken@0
  1307
	curbuf = srcbuf;
slouken@0
  1308
	maxn = bpp == 4 ? 65535 : 255;
slouken@0
  1309
	skip = run = 0;
slouken@0
  1310
	dst = rlebuf;
slouken@0
  1311
	rgbmask = ~surface->format->Amask;
slouken@0
  1312
	ckey = surface->format->colorkey & rgbmask;
slouken@0
  1313
	lastline = dst;
slouken@0
  1314
	getpix = getpixes[bpp - 1];
slouken@0
  1315
	w = surface->w;
slouken@0
  1316
	h = surface->h;
slouken@0
  1317
slouken@0
  1318
#define ADD_COUNTS(n, m)			\
slouken@0
  1319
	if(bpp == 4) {				\
slouken@0
  1320
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1321
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1322
	    dst += 4;				\
slouken@0
  1323
	} else {				\
slouken@0
  1324
	    dst[0] = n;				\
slouken@0
  1325
	    dst[1] = m;				\
slouken@0
  1326
	    dst += 2;				\
slouken@0
  1327
	}
slouken@0
  1328
slouken@0
  1329
	for(y = 0; y < h; y++) {
slouken@0
  1330
	    int x = 0;
slouken@0
  1331
	    int blankline = 0;
slouken@0
  1332
	    do {
slouken@0
  1333
		int run, skip, len;
slouken@0
  1334
		int runstart;
slouken@0
  1335
		int skipstart = x;
slouken@0
  1336
slouken@0
  1337
		/* find run of transparent, then opaque pixels */
slouken@0
  1338
		while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey)
slouken@0
  1339
		    x++;
slouken@0
  1340
		runstart = x;
slouken@0
  1341
		while(x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey)
slouken@0
  1342
		    x++;
slouken@0
  1343
		skip = runstart - skipstart;
slouken@0
  1344
		if(skip == w)
slouken@0
  1345
		    blankline = 1;
slouken@0
  1346
		run = x - runstart;
slouken@0
  1347
slouken@0
  1348
		/* encode segment */
slouken@0
  1349
		while(skip > maxn) {
slouken@0
  1350
		    ADD_COUNTS(maxn, 0);
slouken@0
  1351
		    skip -= maxn;
slouken@0
  1352
		}
slouken@0
  1353
		len = MIN(run, maxn);
slouken@0
  1354
		ADD_COUNTS(skip, len);
slouken@0
  1355
		memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@0
  1356
		dst += len * bpp;
slouken@0
  1357
		run -= len;
slouken@0
  1358
		runstart += len;
slouken@0
  1359
		while(run) {
slouken@0
  1360
		    len = MIN(run, maxn);
slouken@0
  1361
		    ADD_COUNTS(0, len);
slouken@0
  1362
		    memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@0
  1363
		    dst += len * bpp;
slouken@0
  1364
		    runstart += len;
slouken@0
  1365
		    run -= len;
slouken@0
  1366
		}
slouken@0
  1367
		if(!blankline)
slouken@0
  1368
		    lastline = dst;
slouken@0
  1369
	    } while(x < w);
slouken@0
  1370
slouken@0
  1371
	    srcbuf += surface->pitch;
slouken@0
  1372
	}
slouken@0
  1373
	dst = lastline;		/* back up bast trailing blank lines */
slouken@0
  1374
	ADD_COUNTS(0, 0);
slouken@0
  1375
slouken@0
  1376
#undef ADD_COUNTS
slouken@0
  1377
slouken@0
  1378
	/* Now that we have it encoded, release the original pixels */
slouken@0
  1379
	if((surface->flags & SDL_PREALLOC) != SDL_PREALLOC
slouken@0
  1380
	   && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
slouken@0
  1381
	    free( surface->pixels );
slouken@0
  1382
	    surface->pixels = NULL;
slouken@0
  1383
	}
slouken@0
  1384
slouken@0
  1385
	/* realloc the buffer to release unused memory */
slouken@0
  1386
	{
slouken@0
  1387
	    /* If realloc returns NULL, the original block is left intact */
slouken@0
  1388
	    Uint8 *p = realloc(rlebuf, dst - rlebuf);
slouken@0
  1389
	    if(!p)
slouken@0
  1390
		p = rlebuf;
slouken@0
  1391
	    surface->map->sw_data->aux_data = p;
slouken@0
  1392
	}
slouken@0
  1393
slouken@0
  1394
	return(0);
slouken@0
  1395
}
slouken@0
  1396
slouken@0
  1397
int SDL_RLESurface(SDL_Surface *surface)
slouken@0
  1398
{
slouken@0
  1399
	int retcode;
slouken@0
  1400
slouken@0
  1401
	/* Clear any previous RLE conversion */
slouken@0
  1402
	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
  1403
		SDL_UnRLESurface(surface, 1);
slouken@0
  1404
	}
slouken@0
  1405
slouken@0
  1406
	/* We don't support RLE encoding of bitmaps */
slouken@0
  1407
	if ( surface->format->BitsPerPixel < 8 ) {
slouken@0
  1408
		return(-1);
slouken@0
  1409
	}
slouken@0
  1410
slouken@0
  1411
	/* Lock the surface if it's in hardware */
slouken@0
  1412
	if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
  1413
		SDL_VideoDevice *video = current_video;
slouken@0
  1414
		SDL_VideoDevice *this  = current_video;
slouken@0
  1415
		if ( video->LockHWSurface(this, surface) < 0 ) {
slouken@0
  1416
			return(-1);
slouken@0
  1417
		}
slouken@0
  1418
	}
slouken@0
  1419
slouken@0
  1420
	/* Encode */
slouken@0
  1421
	if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
  1422
	    retcode = RLEColorkeySurface(surface);
slouken@0
  1423
	} else {
slouken@0
  1424
	    if((surface->flags & SDL_SRCALPHA) == SDL_SRCALPHA
slouken@0
  1425
	       && surface->format->Amask != 0)
slouken@0
  1426
		retcode = RLEAlphaSurface(surface);
slouken@0
  1427
	    else
slouken@0
  1428
		retcode = -1;	/* no RLE for per-surface alpha sans ckey */
slouken@0
  1429
	}
slouken@0
  1430
slouken@0
  1431
	/* Unlock the surface if it's in hardware */
slouken@0
  1432
	if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
  1433
		SDL_VideoDevice *video = current_video;
slouken@0
  1434
		SDL_VideoDevice *this  = current_video;
slouken@0
  1435
		video->UnlockHWSurface(this, surface);
slouken@0
  1436
	}
slouken@0
  1437
slouken@0
  1438
	if(retcode < 0)
slouken@0
  1439
	    return -1;
slouken@0
  1440
slouken@0
  1441
	/* The surface is now accelerated */
slouken@0
  1442
	surface->flags |= SDL_RLEACCEL;
slouken@0
  1443
slouken@0
  1444
	return(0);
slouken@0
  1445
}
slouken@0
  1446
slouken@0
  1447
/*
slouken@0
  1448
 * Un-RLE a surface with pixel alpha
slouken@0
  1449
 * This may not give back exactly the image before RLE-encoding; all
slouken@0
  1450
 * completely transparent pixels will be lost, and colour and alpha depth
slouken@0
  1451
 * may have been reduced (when encoding for 16bpp targets).
slouken@0
  1452
 */
slouken@0
  1453
static void UnRLEAlpha(SDL_Surface *surface)
slouken@0
  1454
{
slouken@0
  1455
    Uint8 *srcbuf;
slouken@0
  1456
    Uint32 *dst;
slouken@0
  1457
    SDL_PixelFormat *sf = surface->format;
slouken@0
  1458
    RLEDestFormat *df = surface->map->sw_data->aux_data;
slouken@0
  1459
    int (*uncopy_opaque)(Uint32 *, void *, int,
slouken@0
  1460
			 RLEDestFormat *, SDL_PixelFormat *);
slouken@0
  1461
    int (*uncopy_transl)(Uint32 *, void *, int,
slouken@0
  1462
			 RLEDestFormat *, SDL_PixelFormat *);
slouken@0
  1463
    int w = surface->w;
slouken@0
  1464
    int bpp = df->BytesPerPixel;
slouken@0
  1465
slouken@0
  1466
    if(bpp == 2) {
slouken@0
  1467
	uncopy_opaque = uncopy_opaque_16;
slouken@0
  1468
	uncopy_transl = uncopy_transl_16;
slouken@0
  1469
    } else {
slouken@0
  1470
	uncopy_opaque = uncopy_transl = uncopy_32;
slouken@0
  1471
    }
slouken@0
  1472
slouken@0
  1473
    surface->pixels = malloc(surface->h * surface->pitch);
slouken@0
  1474
    /* fill background with transparent pixels */
slouken@0
  1475
    memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@0
  1476
slouken@0
  1477
    dst = surface->pixels;
slouken@0
  1478
    srcbuf = (Uint8 *)(df + 1);
slouken@0
  1479
    for(;;) {
slouken@0
  1480
	/* copy opaque pixels */
slouken@0
  1481
	int ofs = 0;
slouken@0
  1482
	do {
slouken@0
  1483
	    unsigned run;
slouken@0
  1484
	    if(bpp == 2) {
slouken@0
  1485
		ofs += srcbuf[0];
slouken@0
  1486
		run = srcbuf[1];
slouken@0
  1487
		srcbuf += 2;
slouken@0
  1488
	    } else {
slouken@0
  1489
		ofs += ((Uint16 *)srcbuf)[0];
slouken@0
  1490
		run = ((Uint16 *)srcbuf)[1];
slouken@0
  1491
		srcbuf += 4;
slouken@0
  1492
	    }
slouken@0
  1493
	    if(run) {
slouken@0
  1494
		srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
slouken@0
  1495
		ofs += run;
slouken@0
  1496
	    } else if(!ofs)
slouken@0
  1497
		return;
slouken@0
  1498
	} while(ofs < w);
slouken@0
  1499
slouken@0
  1500
	/* skip padding if needed */
slouken@0
  1501
	if(bpp == 2)
slouken@0
  1502
	    srcbuf += (unsigned long)srcbuf & 2;
slouken@0
  1503
	
slouken@0
  1504
	/* copy translucent pixels */
slouken@0
  1505
	ofs = 0;
slouken@0
  1506
	do {
slouken@0
  1507
	    unsigned run;
slouken@0
  1508
	    ofs += ((Uint16 *)srcbuf)[0];
slouken@0
  1509
	    run = ((Uint16 *)srcbuf)[1];
slouken@0
  1510
	    srcbuf += 4;
slouken@0
  1511
	    if(run) {
slouken@0
  1512
		srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
slouken@0
  1513
		ofs += run;
slouken@0
  1514
	    }
slouken@0
  1515
	} while(ofs < w);
slouken@0
  1516
	dst += surface->pitch >> 2;
slouken@0
  1517
    }
slouken@0
  1518
}
slouken@0
  1519
slouken@0
  1520
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
slouken@0
  1521
{
slouken@0
  1522
    if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
  1523
	surface->flags &= ~SDL_RLEACCEL;
slouken@0
  1524
slouken@0
  1525
	if(recode && (surface->flags & SDL_PREALLOC) != SDL_PREALLOC
slouken@0
  1526
	   && (surface->flags & SDL_HWSURFACE) != SDL_HWSURFACE) {
slouken@0
  1527
	    if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
  1528
		SDL_Rect full;
slouken@0
  1529
		unsigned alpha_flag;
slouken@0
  1530
slouken@0
  1531
		/* re-create the original surface */
slouken@0
  1532
		surface->pixels = malloc(surface->h * surface->pitch);
slouken@0
  1533
slouken@0
  1534
		/* fill it with the background colour */
slouken@0
  1535
		SDL_FillRect(surface, NULL, surface->format->colorkey);
slouken@0
  1536
slouken@0
  1537
		/* now render the encoded surface */
slouken@0
  1538
		full.x = full.y = 0;
slouken@0
  1539
		full.w = surface->w;
slouken@0
  1540
		full.h = surface->h;
slouken@0
  1541
		alpha_flag = surface->flags & SDL_SRCALPHA;
slouken@0
  1542
		surface->flags &= ~SDL_SRCALPHA; /* opaque blit */
slouken@0
  1543
		SDL_RLEBlit(surface, &full, surface, &full);
slouken@0
  1544
		surface->flags |= alpha_flag;
slouken@0
  1545
	    } else
slouken@0
  1546
		UnRLEAlpha(surface);
slouken@0
  1547
	}
slouken@0
  1548
slouken@0
  1549
	if ( surface->map && surface->map->sw_data->aux_data ) {
slouken@0
  1550
	    free(surface->map->sw_data->aux_data);
slouken@0
  1551
	    surface->map->sw_data->aux_data = NULL;
slouken@0
  1552
	}
slouken@0
  1553
    }
slouken@0
  1554
}
slouken@0
  1555
slouken@0
  1556