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