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