src/video/SDL_RLEaccel.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jun 2009 04:37:27 +0000
changeset 3162 dc1eb82ffdaa
parent 3035 ff602fdfdedc
child 3697 f7b03b6838cb
permissions -rw-r--r--
Von: Thomas Zimmermann
Betreff: [SDL] [PATCH] Make static variables const
Datum: Tue, 19 May 2009 19:45:37 +0200

Hi,

this is a set of simple changes which make some of SDL's internal static
arrays constant. The purpose is to shrink the number of write-able
static bytes and thus increase the number of memory pages shared between
SDL applications.

The patch set is against trunk@4513. Each of the attached patch files is
specific to a sub-system. The set is completed by a second mail, because
of the list's 40 KiB limit.

The files readelf-r4513.txt and readelf-const-patch.txt where made by
calling 'readelf -S libSDL.so'. They show the difference in ELF sections
without and with the patch. Some numbers measured on my x86-64:

Before

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000008170 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 00000000003045e0 001045e0
00000000000023d0 0000000000000000 WA 0 0 32
[23] .data PROGBITS 00000000003076e0 001076e0
0000000000004988 0000000000000000 WA 0 0 32

After

[13] .rodata PROGBITS 00000000000eaaa0 000eaaa0
0000000000009a50 0000000000000000 A 0 0 32
[19] .data.rel.ro PROGBITS 0000000000306040 00106040
0000000000002608 0000000000000000 WA 0 0 32
[23] .data PROGBITS 0000000000309360 00109360
0000000000002e88 0000000000000000 WA 0 0 32

The size of the write-able data section decreased considerably. Some
entries became const-after-relocation, while most of its content went
straight into the read-only data section.

Best regards, Thomas
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1895
   613
    Uint8 Rloss;
slouken@1895
   614
    Uint8 Gloss;
slouken@1895
   615
    Uint8 Bloss;
slouken@1895
   616
    Uint8 Rshift;
slouken@1895
   617
    Uint8 Gshift;
slouken@1895
   618
    Uint8 Bshift;
slouken@1895
   619
    Uint8 Ashift;
slouken@1895
   620
    Uint32 Rmask;
slouken@1895
   621
    Uint32 Gmask;
slouken@1895
   622
    Uint32 Bmask;
slouken@1895
   623
    Uint32 Amask;
slouken@0
   624
} RLEDestFormat;
slouken@0
   625
slouken@0
   626
/* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
slouken@1895
   627
static void
slouken@1895
   628
RLEAlphaClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
slouken@1895
   629
                 Uint8 * dstbuf, SDL_Rect * srcrect)
slouken@0
   630
{
slouken@0
   631
    SDL_PixelFormat *df = dst->format;
slouken@0
   632
    /*
slouken@0
   633
     * clipped blitter: Ptype is the destination pixel type,
slouken@0
   634
     * Ctype the translucent count type, and do_blend the macro
slouken@0
   635
     * to blend one pixel.
slouken@0
   636
     */
slouken@0
   637
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend)			  \
slouken@0
   638
    do {								  \
slouken@0
   639
	int linecount = srcrect->h;					  \
slouken@0
   640
	int left = srcrect->x;						  \
slouken@0
   641
	int right = left + srcrect->w;					  \
slouken@0
   642
	dstbuf -= left * sizeof(Ptype);					  \
slouken@0
   643
	do {								  \
slouken@0
   644
	    int ofs = 0;						  \
slouken@0
   645
	    /* blit opaque pixels on one line */			  \
slouken@0
   646
	    do {							  \
slouken@0
   647
		unsigned run;						  \
slouken@0
   648
		ofs += ((Ctype *)srcbuf)[0];				  \
slouken@0
   649
		run = ((Ctype *)srcbuf)[1];				  \
slouken@0
   650
		srcbuf += 2 * sizeof(Ctype);				  \
slouken@0
   651
		if(run) {						  \
slouken@0
   652
		    /* clip to left and right borders */		  \
slouken@0
   653
		    int cofs = ofs;					  \
slouken@0
   654
		    int crun = run;					  \
slouken@0
   655
		    if(left - cofs > 0) {				  \
slouken@0
   656
			crun -= left - cofs;				  \
slouken@0
   657
			cofs = left;					  \
slouken@0
   658
		    }							  \
slouken@0
   659
		    if(crun > right - cofs)				  \
slouken@0
   660
			crun = right - cofs;				  \
slouken@0
   661
		    if(crun > 0)					  \
slouken@1
   662
			PIXEL_COPY(dstbuf + cofs * sizeof(Ptype),	  \
slouken@0
   663
				   srcbuf + (cofs - ofs) * sizeof(Ptype), \
slouken@1
   664
				   (unsigned)crun, sizeof(Ptype));	  \
slouken@0
   665
		    srcbuf += run * sizeof(Ptype);			  \
slouken@0
   666
		    ofs += run;						  \
slouken@0
   667
		} else if(!ofs)						  \
slouken@0
   668
		    return;						  \
slouken@0
   669
	    } while(ofs < w);						  \
slouken@0
   670
	    /* skip padding if necessary */				  \
slouken@0
   671
	    if(sizeof(Ptype) == 2)					  \
slouken@1456
   672
		srcbuf += (uintptr_t)srcbuf & 2;			  \
slouken@0
   673
	    /* blit translucent pixels on the same line */		  \
slouken@0
   674
	    ofs = 0;							  \
slouken@0
   675
	    do {							  \
slouken@0
   676
		unsigned run;						  \
slouken@0
   677
		ofs += ((Uint16 *)srcbuf)[0];				  \
slouken@0
   678
		run = ((Uint16 *)srcbuf)[1];				  \
slouken@0
   679
		srcbuf += 4;						  \
slouken@0
   680
		if(run) {						  \
slouken@0
   681
		    /* clip to left and right borders */		  \
slouken@0
   682
		    int cofs = ofs;					  \
slouken@0
   683
		    int crun = run;					  \
slouken@0
   684
		    if(left - cofs > 0) {				  \
slouken@0
   685
			crun -= left - cofs;				  \
slouken@0
   686
			cofs = left;					  \
slouken@0
   687
		    }							  \
slouken@0
   688
		    if(crun > right - cofs)				  \
slouken@0
   689
			crun = right - cofs;				  \
slouken@0
   690
		    if(crun > 0) {					  \
slouken@0
   691
			Ptype *dst = (Ptype *)dstbuf + cofs;		  \
slouken@0
   692
			Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs);	  \
slouken@0
   693
			int i;						  \
slouken@0
   694
			for(i = 0; i < crun; i++)			  \
slouken@0
   695
			    do_blend(src[i], dst[i]);			  \
slouken@0
   696
		    }							  \
slouken@0
   697
		    srcbuf += run * 4;					  \
slouken@0
   698
		    ofs += run;						  \
slouken@0
   699
		}							  \
slouken@0
   700
	    } while(ofs < w);						  \
slouken@0
   701
	    dstbuf += dst->pitch;					  \
slouken@0
   702
	} while(--linecount);						  \
slouken@0
   703
    } while(0)
slouken@0
   704
slouken@1895
   705
    switch (df->BytesPerPixel) {
slouken@0
   706
    case 2:
slouken@1895
   707
        if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0)
slouken@1895
   708
            RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@1895
   709
        else
slouken@1895
   710
            RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@1895
   711
        break;
slouken@0
   712
    case 4:
slouken@1895
   713
        RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@1895
   714
        break;
slouken@0
   715
    }
slouken@0
   716
}
slouken@0
   717
slouken@0
   718
/* blit a pixel-alpha RLE surface */
slouken@1895
   719
int
slouken@1895
   720
SDL_RLEAlphaBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   721
                 SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   722
{
slouken@0
   723
    int x, y;
slouken@0
   724
    int w = src->w;
slouken@0
   725
    Uint8 *srcbuf, *dstbuf;
slouken@0
   726
    SDL_PixelFormat *df = dst->format;
slouken@0
   727
slouken@0
   728
    /* Lock the destination if necessary */
slouken@1895
   729
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
   730
        if (SDL_LockSurface(dst) < 0) {
slouken@1895
   731
            return -1;
slouken@1895
   732
        }
slouken@0
   733
    }
slouken@0
   734
slouken@0
   735
    x = dstrect->x;
slouken@0
   736
    y = dstrect->y;
slouken@1895
   737
    dstbuf = (Uint8 *) dst->pixels + y * dst->pitch + x * df->BytesPerPixel;
slouken@2257
   738
    srcbuf = (Uint8 *) src->map->data + sizeof(RLEDestFormat);
slouken@0
   739
slouken@0
   740
    {
slouken@1895
   741
        /* skip lines at the top if necessary */
slouken@1895
   742
        int vskip = srcrect->y;
slouken@1895
   743
        if (vskip) {
slouken@1895
   744
            int ofs;
slouken@1895
   745
            if (df->BytesPerPixel == 2) {
slouken@1895
   746
                /* the 16/32 interleaved format */
slouken@1895
   747
                do {
slouken@1895
   748
                    /* skip opaque line */
slouken@1895
   749
                    ofs = 0;
slouken@1895
   750
                    do {
slouken@1895
   751
                        int run;
slouken@1895
   752
                        ofs += srcbuf[0];
slouken@1895
   753
                        run = srcbuf[1];
slouken@1895
   754
                        srcbuf += 2;
slouken@1895
   755
                        if (run) {
slouken@1895
   756
                            srcbuf += 2 * run;
slouken@1895
   757
                            ofs += run;
slouken@1895
   758
                        } else if (!ofs)
slouken@1895
   759
                            goto done;
slouken@2735
   760
                    } while (ofs < w);
slouken@0
   761
slouken@1895
   762
                    /* skip padding */
slouken@1895
   763
                    srcbuf += (uintptr_t) srcbuf & 2;
slouken@0
   764
slouken@1895
   765
                    /* skip translucent line */
slouken@1895
   766
                    ofs = 0;
slouken@1895
   767
                    do {
slouken@1895
   768
                        int run;
slouken@1895
   769
                        ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
   770
                        run = ((Uint16 *) srcbuf)[1];
slouken@1895
   771
                        srcbuf += 4 * (run + 1);
slouken@1895
   772
                        ofs += run;
slouken@2735
   773
                    } while (ofs < w);
slouken@2735
   774
                } while (--vskip);
slouken@1895
   775
            } else {
slouken@1895
   776
                /* the 32/32 interleaved format */
slouken@1895
   777
                vskip <<= 1;    /* opaque and translucent have same format */
slouken@1895
   778
                do {
slouken@1895
   779
                    ofs = 0;
slouken@1895
   780
                    do {
slouken@1895
   781
                        int run;
slouken@1895
   782
                        ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
   783
                        run = ((Uint16 *) srcbuf)[1];
slouken@1895
   784
                        srcbuf += 4;
slouken@1895
   785
                        if (run) {
slouken@1895
   786
                            srcbuf += 4 * run;
slouken@1895
   787
                            ofs += run;
slouken@1895
   788
                        } else if (!ofs)
slouken@1895
   789
                            goto done;
slouken@2735
   790
                    } while (ofs < w);
slouken@2735
   791
                } while (--vskip);
slouken@1895
   792
            }
slouken@1895
   793
        }
slouken@0
   794
    }
slouken@0
   795
slouken@0
   796
    /* if left or right edge clipping needed, call clip blit */
slouken@1895
   797
    if (srcrect->x || srcrect->w != src->w) {
slouken@1895
   798
        RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect);
slouken@0
   799
    } else {
slouken@0
   800
slouken@1895
   801
        /*
slouken@1895
   802
         * non-clipped blitter. Ptype is the destination pixel type,
slouken@1895
   803
         * Ctype the translucent count type, and do_blend the
slouken@1895
   804
         * macro to blend one pixel.
slouken@1895
   805
         */
slouken@0
   806
#define RLEALPHABLIT(Ptype, Ctype, do_blend)				 \
slouken@0
   807
	do {								 \
slouken@0
   808
	    int linecount = srcrect->h;					 \
slouken@0
   809
	    do {							 \
slouken@0
   810
		int ofs = 0;						 \
slouken@0
   811
		/* blit opaque pixels on one line */			 \
slouken@0
   812
		do {							 \
slouken@0
   813
		    unsigned run;					 \
slouken@0
   814
		    ofs += ((Ctype *)srcbuf)[0];			 \
slouken@0
   815
		    run = ((Ctype *)srcbuf)[1];				 \
slouken@0
   816
		    srcbuf += 2 * sizeof(Ctype);			 \
slouken@0
   817
		    if(run) {						 \
slouken@1
   818
			PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
slouken@1
   819
				   run, sizeof(Ptype));			 \
slouken@0
   820
			srcbuf += run * sizeof(Ptype);			 \
slouken@0
   821
			ofs += run;					 \
slouken@0
   822
		    } else if(!ofs)					 \
slouken@0
   823
			goto done;					 \
slouken@0
   824
		} while(ofs < w);					 \
slouken@0
   825
		/* skip padding if necessary */				 \
slouken@0
   826
		if(sizeof(Ptype) == 2)					 \
slouken@1456
   827
		    srcbuf += (uintptr_t)srcbuf & 2;		 	 \
slouken@0
   828
		/* blit translucent pixels on the same line */		 \
slouken@0
   829
		ofs = 0;						 \
slouken@0
   830
		do {							 \
slouken@0
   831
		    unsigned run;					 \
slouken@0
   832
		    ofs += ((Uint16 *)srcbuf)[0];			 \
slouken@0
   833
		    run = ((Uint16 *)srcbuf)[1];			 \
slouken@0
   834
		    srcbuf += 4;					 \
slouken@0
   835
		    if(run) {						 \
slouken@0
   836
			Ptype *dst = (Ptype *)dstbuf + ofs;		 \
slouken@0
   837
			unsigned i;					 \
slouken@0
   838
			for(i = 0; i < run; i++) {			 \
slouken@0
   839
			    Uint32 src = *(Uint32 *)srcbuf;		 \
slouken@0
   840
			    do_blend(src, *dst);			 \
slouken@0
   841
			    srcbuf += 4;				 \
slouken@0
   842
			    dst++;					 \
slouken@0
   843
			}						 \
slouken@0
   844
			ofs += run;					 \
slouken@0
   845
		    }							 \
slouken@0
   846
		} while(ofs < w);					 \
slouken@0
   847
		dstbuf += dst->pitch;					 \
slouken@0
   848
	    } while(--linecount);					 \
slouken@0
   849
	} while(0)
slouken@0
   850
slouken@1895
   851
        switch (df->BytesPerPixel) {
slouken@1895
   852
        case 2:
slouken@1895
   853
            if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0
slouken@1895
   854
                || df->Bmask == 0x07e0)
slouken@1895
   855
                RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
slouken@1895
   856
            else
slouken@1895
   857
                RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
slouken@1895
   858
            break;
slouken@1895
   859
        case 4:
slouken@1895
   860
            RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
slouken@1895
   861
            break;
slouken@1895
   862
        }
slouken@0
   863
    }
slouken@0
   864
slouken@1895
   865
  done:
slouken@0
   866
    /* Unlock the destination if necessary */
slouken@1895
   867
    if (SDL_MUSTLOCK(dst)) {
slouken@1895
   868
        SDL_UnlockSurface(dst);
slouken@0
   869
    }
slouken@0
   870
    return 0;
slouken@0
   871
}
slouken@0
   872
slouken@0
   873
/*
slouken@0
   874
 * Auxiliary functions:
slouken@0
   875
 * The encoding functions take 32bpp rgb + a, and
slouken@0
   876
 * return the number of bytes copied to the destination.
slouken@0
   877
 * The decoding functions copy to 32bpp rgb + a, and
slouken@0
   878
 * return the number of bytes copied from the source.
slouken@0
   879
 * These are only used in the encoder and un-RLE code and are therefore not
slouken@0
   880
 * highly optimised.
slouken@0
   881
 */
slouken@0
   882
slouken@0
   883
/* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
slouken@1895
   884
static int
slouken@1895
   885
copy_opaque_16(void *dst, Uint32 * src, int n,
slouken@1895
   886
               SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   887
{
slouken@0
   888
    int i;
slouken@0
   889
    Uint16 *d = dst;
slouken@1895
   890
    for (i = 0; i < n; i++) {
slouken@1895
   891
        unsigned r, g, b;
slouken@1895
   892
        RGB_FROM_PIXEL(*src, sfmt, r, g, b);
slouken@1895
   893
        PIXEL_FROM_RGB(*d, dfmt, r, g, b);
slouken@1895
   894
        src++;
slouken@1895
   895
        d++;
slouken@0
   896
    }
slouken@0
   897
    return n * 2;
slouken@0
   898
}
slouken@0
   899
slouken@0
   900
/* decode opaque pixels from 16bpp to 32bpp rgb + a */
slouken@1895
   901
static int
slouken@1895
   902
uncopy_opaque_16(Uint32 * dst, void *src, int n,
slouken@1895
   903
                 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   904
{
slouken@0
   905
    int i;
slouken@0
   906
    Uint16 *s = src;
slouken@0
   907
    unsigned alpha = dfmt->Amask ? 255 : 0;
slouken@1895
   908
    for (i = 0; i < n; i++) {
slouken@1895
   909
        unsigned r, g, b;
slouken@1895
   910
        RGB_FROM_PIXEL(*s, sfmt, r, g, b);
slouken@1895
   911
        PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
slouken@1895
   912
        s++;
slouken@1895
   913
        dst++;
slouken@0
   914
    }
slouken@0
   915
    return n * 2;
slouken@0
   916
}
slouken@0
   917
slouken@0
   918
slouken@0
   919
slouken@0
   920
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
slouken@1895
   921
static int
slouken@1895
   922
copy_transl_565(void *dst, Uint32 * src, int n,
slouken@1895
   923
                SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   924
{
slouken@0
   925
    int i;
slouken@0
   926
    Uint32 *d = dst;
slouken@1895
   927
    for (i = 0; i < n; i++) {
slouken@1895
   928
        unsigned r, g, b, a;
slouken@1895
   929
        Uint16 pix;
slouken@1895
   930
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
   931
        PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@1895
   932
        *d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
slouken@1895
   933
        src++;
slouken@1895
   934
        d++;
slouken@0
   935
    }
slouken@0
   936
    return n * 4;
slouken@0
   937
}
slouken@0
   938
slouken@0
   939
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
slouken@1895
   940
static int
slouken@1895
   941
copy_transl_555(void *dst, Uint32 * src, int n,
slouken@1895
   942
                SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   943
{
slouken@0
   944
    int i;
slouken@0
   945
    Uint32 *d = dst;
slouken@1895
   946
    for (i = 0; i < n; i++) {
slouken@1895
   947
        unsigned r, g, b, a;
slouken@1895
   948
        Uint16 pix;
slouken@1895
   949
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
   950
        PIXEL_FROM_RGB(pix, dfmt, r, g, b);
slouken@1895
   951
        *d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
slouken@1895
   952
        src++;
slouken@1895
   953
        d++;
slouken@0
   954
    }
slouken@0
   955
    return n * 4;
slouken@0
   956
}
slouken@0
   957
slouken@0
   958
/* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
slouken@1895
   959
static int
slouken@1895
   960
uncopy_transl_16(Uint32 * dst, void *src, int n,
slouken@1895
   961
                 RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   962
{
slouken@0
   963
    int i;
slouken@0
   964
    Uint32 *s = src;
slouken@1895
   965
    for (i = 0; i < n; i++) {
slouken@1895
   966
        unsigned r, g, b, a;
slouken@1895
   967
        Uint32 pix = *s++;
slouken@1895
   968
        a = (pix & 0x3e0) >> 2;
slouken@1895
   969
        pix = (pix & ~0x3e0) | pix >> 16;
slouken@1895
   970
        RGB_FROM_PIXEL(pix, sfmt, r, g, b);
slouken@1895
   971
        PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
slouken@1895
   972
        dst++;
slouken@0
   973
    }
slouken@0
   974
    return n * 4;
slouken@0
   975
}
slouken@0
   976
slouken@0
   977
/* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
slouken@1895
   978
static int
slouken@1895
   979
copy_32(void *dst, Uint32 * src, int n,
slouken@1895
   980
        SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
slouken@0
   981
{
slouken@0
   982
    int i;
slouken@0
   983
    Uint32 *d = dst;
slouken@1895
   984
    for (i = 0; i < n; i++) {
slouken@1895
   985
        unsigned r, g, b, a;
slouken@1895
   986
        Uint32 pixel;
slouken@1895
   987
        RGBA_FROM_8888(*src, sfmt, r, g, b, a);
slouken@1895
   988
        PIXEL_FROM_RGB(pixel, dfmt, r, g, b);
slouken@1895
   989
        *d++ = pixel | a << 24;
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@1895
  1097
        r->Rloss = df->Rloss;
slouken@1895
  1098
        r->Gloss = df->Gloss;
slouken@1895
  1099
        r->Bloss = df->Bloss;
slouken@1895
  1100
        r->Rshift = df->Rshift;
slouken@1895
  1101
        r->Gshift = df->Gshift;
slouken@1895
  1102
        r->Bshift = df->Bshift;
slouken@1895
  1103
        r->Ashift = df->Ashift;
slouken@1895
  1104
        r->Rmask = df->Rmask;
slouken@1895
  1105
        r->Gmask = df->Gmask;
slouken@1895
  1106
        r->Bmask = df->Bmask;
slouken@1895
  1107
        r->Amask = df->Amask;
slouken@0
  1108
    }
slouken@0
  1109
    dst = rlebuf + sizeof(RLEDestFormat);
slouken@0
  1110
slouken@0
  1111
    /* Do the actual encoding */
slouken@0
  1112
    {
slouken@1895
  1113
        int x, y;
slouken@1895
  1114
        int h = surface->h, w = surface->w;
slouken@1895
  1115
        SDL_PixelFormat *sf = surface->format;
slouken@1895
  1116
        Uint32 *src = (Uint32 *) surface->pixels;
slouken@1895
  1117
        Uint8 *lastline = dst;  /* end of last non-blank line */
slouken@0
  1118
slouken@1895
  1119
        /* opaque counts are 8 or 16 bits, depending on target depth */
slouken@0
  1120
#define ADD_OPAQUE_COUNTS(n, m)			\
slouken@0
  1121
	if(df->BytesPerPixel == 4) {		\
slouken@0
  1122
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1123
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1124
	    dst += 4;				\
slouken@0
  1125
	} else {				\
slouken@0
  1126
	    dst[0] = n;				\
slouken@0
  1127
	    dst[1] = m;				\
slouken@0
  1128
	    dst += 2;				\
slouken@0
  1129
	}
slouken@0
  1130
slouken@1895
  1131
        /* translucent counts are always 16 bit */
slouken@0
  1132
#define ADD_TRANSL_COUNTS(n, m)		\
slouken@0
  1133
	(((Uint16 *)dst)[0] = n, ((Uint16 *)dst)[1] = m, dst += 4)
slouken@0
  1134
slouken@1895
  1135
        for (y = 0; y < h; y++) {
slouken@1895
  1136
            int runstart, skipstart;
slouken@1895
  1137
            int blankline = 0;
slouken@1895
  1138
            /* First encode all opaque pixels of a scan line */
slouken@1895
  1139
            x = 0;
slouken@1895
  1140
            do {
slouken@1895
  1141
                int run, skip, len;
slouken@1895
  1142
                skipstart = x;
slouken@1895
  1143
                while (x < w && !ISOPAQUE(src[x], sf))
slouken@1895
  1144
                    x++;
slouken@1895
  1145
                runstart = x;
slouken@1895
  1146
                while (x < w && ISOPAQUE(src[x], sf))
slouken@1895
  1147
                    x++;
slouken@1895
  1148
                skip = runstart - skipstart;
slouken@1895
  1149
                if (skip == w)
slouken@1895
  1150
                    blankline = 1;
slouken@1895
  1151
                run = x - runstart;
slouken@1895
  1152
                while (skip > max_opaque_run) {
slouken@1895
  1153
                    ADD_OPAQUE_COUNTS(max_opaque_run, 0);
slouken@1895
  1154
                    skip -= max_opaque_run;
slouken@1895
  1155
                }
slouken@1895
  1156
                len = MIN(run, max_opaque_run);
slouken@1895
  1157
                ADD_OPAQUE_COUNTS(skip, len);
slouken@1895
  1158
                dst += copy_opaque(dst, src + runstart, len, sf, df);
slouken@1895
  1159
                runstart += len;
slouken@1895
  1160
                run -= len;
slouken@1895
  1161
                while (run) {
slouken@1895
  1162
                    len = MIN(run, max_opaque_run);
slouken@1895
  1163
                    ADD_OPAQUE_COUNTS(0, 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
                }
slouken@2735
  1168
            } while (x < w);
slouken@0
  1169
slouken@1895
  1170
            /* Make sure the next output address is 32-bit aligned */
slouken@1895
  1171
            dst += (uintptr_t) dst & 2;
slouken@0
  1172
slouken@1895
  1173
            /* Next, encode all translucent pixels of the same scan line */
slouken@1895
  1174
            x = 0;
slouken@1895
  1175
            do {
slouken@1895
  1176
                int run, skip, len;
slouken@1895
  1177
                skipstart = x;
slouken@1895
  1178
                while (x < w && !ISTRANSL(src[x], sf))
slouken@1895
  1179
                    x++;
slouken@1895
  1180
                runstart = x;
slouken@1895
  1181
                while (x < w && ISTRANSL(src[x], sf))
slouken@1895
  1182
                    x++;
slouken@1895
  1183
                skip = runstart - skipstart;
slouken@1895
  1184
                blankline &= (skip == w);
slouken@1895
  1185
                run = x - runstart;
slouken@1895
  1186
                while (skip > max_transl_run) {
slouken@1895
  1187
                    ADD_TRANSL_COUNTS(max_transl_run, 0);
slouken@1895
  1188
                    skip -= max_transl_run;
slouken@1895
  1189
                }
slouken@1895
  1190
                len = MIN(run, max_transl_run);
slouken@1895
  1191
                ADD_TRANSL_COUNTS(skip, len);
slouken@1895
  1192
                dst += copy_transl(dst, src + runstart, len, sf, df);
slouken@1895
  1193
                runstart += len;
slouken@1895
  1194
                run -= len;
slouken@1895
  1195
                while (run) {
slouken@1895
  1196
                    len = MIN(run, max_transl_run);
slouken@1895
  1197
                    ADD_TRANSL_COUNTS(0, 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
                }
slouken@1895
  1202
                if (!blankline)
slouken@1895
  1203
                    lastline = dst;
slouken@2735
  1204
            } while (x < w);
slouken@0
  1205
slouken@1895
  1206
            src += surface->pitch >> 2;
slouken@1895
  1207
        }
slouken@1895
  1208
        dst = lastline;         /* back up past trailing blank lines */
slouken@1895
  1209
        ADD_OPAQUE_COUNTS(0, 0);
slouken@0
  1210
    }
slouken@0
  1211
slouken@0
  1212
#undef ADD_OPAQUE_COUNTS
slouken@0
  1213
#undef ADD_TRANSL_COUNTS
slouken@0
  1214
slouken@0
  1215
    /* Now that we have it encoded, release the original pixels */
slouken@2222
  1216
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1217
        SDL_free(surface->pixels);
slouken@1895
  1218
        surface->pixels = NULL;
slouken@0
  1219
    }
slouken@0
  1220
slouken@0
  1221
    /* realloc the buffer to release unused memory */
slouken@0
  1222
    {
slouken@1895
  1223
        Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
slouken@1895
  1224
        if (!p)
slouken@1895
  1225
            p = rlebuf;
slouken@2257
  1226
        surface->map->data = p;
slouken@0
  1227
    }
slouken@0
  1228
slouken@0
  1229
    return 0;
slouken@0
  1230
}
slouken@0
  1231
slouken@1895
  1232
static Uint32
slouken@1895
  1233
getpix_8(Uint8 * srcbuf)
slouken@0
  1234
{
slouken@0
  1235
    return *srcbuf;
slouken@0
  1236
}
slouken@0
  1237
slouken@1895
  1238
static Uint32
slouken@1895
  1239
getpix_16(Uint8 * srcbuf)
slouken@0
  1240
{
slouken@1895
  1241
    return *(Uint16 *) srcbuf;
slouken@0
  1242
}
slouken@0
  1243
slouken@1895
  1244
static Uint32
slouken@1895
  1245
getpix_24(Uint8 * srcbuf)
slouken@0
  1246
{
icculus@1155
  1247
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
icculus@1155
  1248
    return srcbuf[0] + (srcbuf[1] << 8) + (srcbuf[2] << 16);
icculus@1155
  1249
#else
icculus@1155
  1250
    return (srcbuf[0] << 16) + (srcbuf[1] << 8) + srcbuf[2];
icculus@1155
  1251
#endif
slouken@0
  1252
}
slouken@0
  1253
slouken@1895
  1254
static Uint32
slouken@1895
  1255
getpix_32(Uint8 * srcbuf)
slouken@0
  1256
{
slouken@1895
  1257
    return *(Uint32 *) srcbuf;
slouken@0
  1258
}
slouken@0
  1259
slouken@1895
  1260
typedef Uint32(*getpix_func) (Uint8 *);
slouken@0
  1261
slouken@3162
  1262
static const getpix_func getpixes[4] = {
slouken@0
  1263
    getpix_8, getpix_16, getpix_24, getpix_32
slouken@0
  1264
};
slouken@0
  1265
slouken@1895
  1266
static int
slouken@1895
  1267
RLEColorkeySurface(SDL_Surface * surface)
slouken@0
  1268
{
slouken@1895
  1269
    Uint8 *rlebuf, *dst;
slouken@1895
  1270
    int maxn;
slouken@1895
  1271
    int y;
slouken@1895
  1272
    Uint8 *srcbuf, *curbuf, *lastline;
slouken@1895
  1273
    int maxsize = 0;
slouken@1895
  1274
    int skip, run;
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
    curbuf = srcbuf;
slouken@1895
  1309
    maxn = bpp == 4 ? 65535 : 255;
slouken@1895
  1310
    skip = run = 0;
slouken@1895
  1311
    dst = rlebuf;
slouken@1895
  1312
    rgbmask = ~surface->format->Amask;
slouken@2262
  1313
    ckey = surface->map->info.colorkey & rgbmask;
slouken@1895
  1314
    lastline = dst;
slouken@1895
  1315
    getpix = getpixes[bpp - 1];
slouken@1895
  1316
    w = surface->w;
slouken@1895
  1317
    h = surface->h;
slouken@0
  1318
slouken@0
  1319
#define ADD_COUNTS(n, m)			\
slouken@0
  1320
	if(bpp == 4) {				\
slouken@0
  1321
	    ((Uint16 *)dst)[0] = n;		\
slouken@0
  1322
	    ((Uint16 *)dst)[1] = m;		\
slouken@0
  1323
	    dst += 4;				\
slouken@0
  1324
	} else {				\
slouken@0
  1325
	    dst[0] = n;				\
slouken@0
  1326
	    dst[1] = m;				\
slouken@0
  1327
	    dst += 2;				\
slouken@0
  1328
	}
slouken@0
  1329
slouken@1895
  1330
    for (y = 0; y < h; y++) {
slouken@1895
  1331
        int x = 0;
slouken@1895
  1332
        int blankline = 0;
slouken@1895
  1333
        do {
slouken@1895
  1334
            int run, skip, len;
slouken@1895
  1335
            int runstart;
slouken@1895
  1336
            int skipstart = x;
slouken@0
  1337
slouken@1895
  1338
            /* find run of transparent, then opaque pixels */
slouken@1895
  1339
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey)
slouken@1895
  1340
                x++;
slouken@1895
  1341
            runstart = x;
slouken@1895
  1342
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey)
slouken@1895
  1343
                x++;
slouken@1895
  1344
            skip = runstart - skipstart;
slouken@1895
  1345
            if (skip == w)
slouken@1895
  1346
                blankline = 1;
slouken@1895
  1347
            run = x - runstart;
slouken@0
  1348
slouken@1895
  1349
            /* encode segment */
slouken@1895
  1350
            while (skip > maxn) {
slouken@1895
  1351
                ADD_COUNTS(maxn, 0);
slouken@1895
  1352
                skip -= maxn;
slouken@1895
  1353
            }
slouken@1895
  1354
            len = MIN(run, maxn);
slouken@1895
  1355
            ADD_COUNTS(skip, len);
slouken@1895
  1356
            SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1357
            dst += len * bpp;
slouken@1895
  1358
            run -= len;
slouken@1895
  1359
            runstart += len;
slouken@1895
  1360
            while (run) {
slouken@1895
  1361
                len = MIN(run, maxn);
slouken@1895
  1362
                ADD_COUNTS(0, len);
slouken@1895
  1363
                SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1364
                dst += len * bpp;
slouken@1895
  1365
                runstart += len;
slouken@1895
  1366
                run -= len;
slouken@1895
  1367
            }
slouken@1895
  1368
            if (!blankline)
slouken@1895
  1369
                lastline = dst;
slouken@2735
  1370
        } while (x < w);
slouken@0
  1371
slouken@1895
  1372
        srcbuf += surface->pitch;
slouken@1895
  1373
    }
slouken@1895
  1374
    dst = lastline;             /* back up bast trailing blank lines */
slouken@1895
  1375
    ADD_COUNTS(0, 0);
slouken@0
  1376
slouken@0
  1377
#undef ADD_COUNTS
slouken@0
  1378
slouken@1895
  1379
    /* Now that we have it encoded, release the original pixels */
slouken@2222
  1380
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1381
        SDL_free(surface->pixels);
slouken@1895
  1382
        surface->pixels = NULL;
slouken@1895
  1383
    }
slouken@0
  1384
slouken@1895
  1385
    /* realloc the buffer to release unused memory */
slouken@1895
  1386
    {
slouken@1895
  1387
        /* If realloc returns NULL, the original block is left intact */
slouken@1895
  1388
        Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
slouken@1895
  1389
        if (!p)
slouken@1895
  1390
            p = rlebuf;
slouken@2257
  1391
        surface->map->data = p;
slouken@1895
  1392
    }
slouken@0
  1393
slouken@1895
  1394
    return (0);
slouken@0
  1395
}
slouken@0
  1396
slouken@1895
  1397
int
slouken@1895
  1398
SDL_RLESurface(SDL_Surface * surface)
slouken@0
  1399
{
slouken@2266
  1400
    int flags;
slouken@0
  1401
slouken@1895
  1402
    /* Clear any previous RLE conversion */
slouken@1895
  1403
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
  1404
        SDL_UnRLESurface(surface, 1);
slouken@1895
  1405
    }
slouken@0
  1406
slouken@1895
  1407
    /* We don't support RLE encoding of bitmaps */
slouken@1895
  1408
    if (surface->format->BitsPerPixel < 8) {
slouken@2266
  1409
        return -1;
slouken@1895
  1410
    }
slouken@0
  1411
slouken@2266
  1412
    /* Make sure the pixels are available */
slouken@2266
  1413
    if (!surface->pixels) {
slouken@2266
  1414
        return -1;
slouken@1895
  1415
    }
slouken@0
  1416
slouken@2266
  1417
    /* If we don't have colorkey or blending, nothing to do... */
slouken@2266
  1418
    flags = surface->map->info.flags;
slouken@2267
  1419
    if (!(flags & (SDL_COPY_COLORKEY | SDL_COPY_BLEND))) {
slouken@2266
  1420
        return -1;
slouken@1895
  1421
    }
slouken@0
  1422
slouken@2266
  1423
    /* Pass on combinations not supported */
slouken@2266
  1424
    if ((flags & SDL_COPY_MODULATE_COLOR) ||
slouken@2824
  1425
        ((flags & SDL_COPY_MODULATE_ALPHA) && surface->format->Amask) ||
slouken@2267
  1426
        (flags & (SDL_COPY_ADD | SDL_COPY_MOD)) ||
slouken@2266
  1427
        (flags & SDL_COPY_NEAREST)) {
slouken@2266
  1428
        return -1;
slouken@1895
  1429
    }
slouken@0
  1430
slouken@2266
  1431
    /* Encode and set up the blit */
slouken@2266
  1432
    if (!surface->format->Amask || !(flags & SDL_COPY_BLEND)) {
slouken@2266
  1433
        if (!surface->map->identity) {
slouken@2266
  1434
            return -1;
slouken@2266
  1435
        }
slouken@2266
  1436
        if (RLEColorkeySurface(surface) < 0) {
slouken@2266
  1437
            return -1;
slouken@2266
  1438
        }
slouken@2266
  1439
        surface->map->blit = SDL_RLEBlit;
slouken@2266
  1440
        surface->map->info.flags |= SDL_COPY_RLE_COLORKEY;
slouken@2266
  1441
    } else {
slouken@2266
  1442
        if (RLEAlphaSurface(surface) < 0) {
slouken@2266
  1443
            return -1;
slouken@2266
  1444
        }
slouken@2266
  1445
        surface->map->blit = SDL_RLEAlphaBlit;
slouken@2266
  1446
        surface->map->info.flags |= SDL_COPY_RLE_ALPHAKEY;
slouken@2266
  1447
    }
slouken@0
  1448
slouken@1895
  1449
    /* The surface is now accelerated */
slouken@1895
  1450
    surface->flags |= SDL_RLEACCEL;
slouken@0
  1451
slouken@1895
  1452
    return (0);
slouken@0
  1453
}
slouken@0
  1454
slouken@0
  1455
/*
slouken@0
  1456
 * Un-RLE a surface with pixel alpha
slouken@0
  1457
 * This may not give back exactly the image before RLE-encoding; all
slouken@0
  1458
 * completely transparent pixels will be lost, and colour and alpha depth
slouken@0
  1459
 * may have been reduced (when encoding for 16bpp targets).
slouken@0
  1460
 */
slouken@1895
  1461
static SDL_bool
slouken@1895
  1462
UnRLEAlpha(SDL_Surface * surface)
slouken@0
  1463
{
slouken@0
  1464
    Uint8 *srcbuf;
slouken@0
  1465
    Uint32 *dst;
slouken@0
  1466
    SDL_PixelFormat *sf = surface->format;
slouken@2257
  1467
    RLEDestFormat *df = surface->map->data;
slouken@1895
  1468
    int (*uncopy_opaque) (Uint32 *, void *, int,
slouken@1895
  1469
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@1895
  1470
    int (*uncopy_transl) (Uint32 *, void *, int,
slouken@1895
  1471
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@0
  1472
    int w = surface->w;
slouken@0
  1473
    int bpp = df->BytesPerPixel;
slouken@0
  1474
slouken@1895
  1475
    if (bpp == 2) {
slouken@1895
  1476
        uncopy_opaque = uncopy_opaque_16;
slouken@1895
  1477
        uncopy_transl = uncopy_transl_16;
slouken@0
  1478
    } else {
slouken@1895
  1479
        uncopy_opaque = uncopy_transl = uncopy_32;
slouken@0
  1480
    }
slouken@0
  1481
slouken@1336
  1482
    surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1483
    if (!surface->pixels) {
slouken@1895
  1484
        return (SDL_FALSE);
slouken@944
  1485
    }
slouken@0
  1486
    /* fill background with transparent pixels */
slouken@1336
  1487
    SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@0
  1488
slouken@0
  1489
    dst = surface->pixels;
slouken@1895
  1490
    srcbuf = (Uint8 *) (df + 1);
slouken@1895
  1491
    for (;;) {
slouken@1895
  1492
        /* copy opaque pixels */
slouken@1895
  1493
        int ofs = 0;
slouken@1895
  1494
        do {
slouken@1895
  1495
            unsigned run;
slouken@1895
  1496
            if (bpp == 2) {
slouken@1895
  1497
                ofs += srcbuf[0];
slouken@1895
  1498
                run = srcbuf[1];
slouken@1895
  1499
                srcbuf += 2;
slouken@1895
  1500
            } else {
slouken@1895
  1501
                ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1502
                run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1503
                srcbuf += 4;
slouken@1895
  1504
            }
slouken@1895
  1505
            if (run) {
slouken@1895
  1506
                srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1507
                ofs += run;
slouken@1895
  1508
            } else if (!ofs)
slouken@1895
  1509
                return (SDL_TRUE);
slouken@2735
  1510
        } while (ofs < w);
slouken@0
  1511
slouken@1895
  1512
        /* skip padding if needed */
slouken@1895
  1513
        if (bpp == 2)
slouken@1895
  1514
            srcbuf += (uintptr_t) srcbuf & 2;
slouken@1895
  1515
slouken@1895
  1516
        /* copy translucent pixels */
slouken@1895
  1517
        ofs = 0;
slouken@1895
  1518
        do {
slouken@1895
  1519
            unsigned run;
slouken@1895
  1520
            ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1521
            run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1522
            srcbuf += 4;
slouken@1895
  1523
            if (run) {
slouken@1895
  1524
                srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1525
                ofs += run;
slouken@1895
  1526
            }
slouken@2735
  1527
        } while (ofs < w);
slouken@1895
  1528
        dst += surface->pitch >> 2;
slouken@0
  1529
    }
slouken@944
  1530
    /* Make the compiler happy */
slouken@1895
  1531
    return (SDL_TRUE);
slouken@0
  1532
}
slouken@0
  1533
slouken@1895
  1534
void
slouken@1895
  1535
SDL_UnRLESurface(SDL_Surface * surface, int recode)
slouken@0
  1536
{
slouken@2266
  1537
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1538
        surface->flags &= ~SDL_RLEACCEL;
slouken@0
  1539
slouken@2222
  1540
        if (recode && !(surface->flags & SDL_PREALLOC)) {
slouken@2266
  1541
            if (surface->map->info.flags & SDL_COPY_RLE_COLORKEY) {
slouken@1895
  1542
                SDL_Rect full;
slouken@0
  1543
slouken@1895
  1544
                /* re-create the original surface */
slouken@1895
  1545
                surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1546
                if (!surface->pixels) {
slouken@1895
  1547
                    /* Oh crap... */
slouken@1895
  1548
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1549
                    return;
slouken@1895
  1550
                }
slouken@0
  1551
slouken@1895
  1552
                /* fill it with the background colour */
slouken@2262
  1553
                SDL_FillRect(surface, NULL, surface->map->info.colorkey);
slouken@0
  1554
slouken@1895
  1555
                /* now render the encoded surface */
slouken@1895
  1556
                full.x = full.y = 0;
slouken@1895
  1557
                full.w = surface->w;
slouken@1895
  1558
                full.h = surface->h;
slouken@1895
  1559
                SDL_RLEBlit(surface, &full, surface, &full);
slouken@1895
  1560
            } else {
slouken@1895
  1561
                if (!UnRLEAlpha(surface)) {
slouken@1895
  1562
                    /* Oh crap... */
slouken@1895
  1563
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1564
                    return;
slouken@1895
  1565
                }
slouken@1895
  1566
            }
slouken@1895
  1567
        }
slouken@2267
  1568
        surface->map->info.flags &=
slouken@2798
  1569
            ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
slouken@0
  1570
slouken@2266
  1571
        if (surface->map->data) {
slouken@2257
  1572
            SDL_free(surface->map->data);
slouken@2257
  1573
            surface->map->data = NULL;
slouken@1895
  1574
        }
slouken@0
  1575
    }
slouken@0
  1576
}
slouken@0
  1577
slouken@1895
  1578
/* vi: set ts=4 sw=4 expandtab: */