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