src/video/SDL_RLEaccel.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 14:17:52 -0700
changeset 7191 75360622e65f
parent 7128 e10c5a9cc858
child 7351 668a3dc28361
permissions -rw-r--r--
File style cleanup for the SDL 2.0 release
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@7191
    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@7191
   100
#define PIXEL_COPY(to, from, len, bpp)          \
slouken@7191
   101
do {                            \
slouken@7191
   102
    if(bpp == 4) {                  \
slouken@7191
   103
    SDL_memcpy4(to, from, (size_t)(len));       \
slouken@7191
   104
    } else {                        \
slouken@7191
   105
    SDL_memcpy(to, from, (size_t)(len) * (bpp));    \
slouken@7191
   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@7191
   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@7191
   123
#define ALPHA_BLIT32_888(to, from, length, bpp, alpha)      \
slouken@7191
   124
    do {                            \
slouken@7191
   125
        int i;                          \
slouken@7191
   126
    Uint32 *src = (Uint32 *)(from);             \
slouken@7191
   127
    Uint32 *dst = (Uint32 *)(to);               \
slouken@7191
   128
    for(i = 0; i < (int)(length); i++) {            \
slouken@7191
   129
        Uint32 s = *src++;                  \
slouken@7191
   130
        Uint32 d = *dst;                    \
slouken@7191
   131
        Uint32 s1 = s & 0xff00ff;               \
slouken@7191
   132
        Uint32 d1 = d & 0xff00ff;               \
slouken@7191
   133
        d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;    \
slouken@7191
   134
        s &= 0xff00;                    \
slouken@7191
   135
        d &= 0xff00;                    \
slouken@7191
   136
        d = (d + ((s - d) * alpha >> 8)) & 0xff00;      \
slouken@7191
   137
        *dst++ = d1 | d;                    \
slouken@7191
   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@7191
   147
#define ALPHA_BLIT16_565(to, from, length, bpp, alpha)  \
slouken@7191
   148
    do {                        \
slouken@7191
   149
        int i;                      \
slouken@7191
   150
    Uint16 *src = (Uint16 *)(from);         \
slouken@7191
   151
    Uint16 *dst = (Uint16 *)(to);           \
slouken@7191
   152
    Uint32 ALPHA = alpha >> 3;          \
slouken@7191
   153
    for(i = 0; i < (int)(length); i++) {        \
slouken@7191
   154
        Uint32 s = *src++;              \
slouken@7191
   155
        Uint32 d = *dst;                \
slouken@7191
   156
        s = (s | s << 16) & 0x07e0f81f;     \
slouken@7191
   157
        d = (d | d << 16) & 0x07e0f81f;     \
slouken@7191
   158
        d += (s - d) * ALPHA >> 5;          \
slouken@7191
   159
        d &= 0x07e0f81f;                \
slouken@7191
   160
        *dst++ = (Uint16)(d | d >> 16);         \
slouken@7191
   161
    }                       \
slouken@0
   162
    } while(0)
slouken@0
   163
slouken@7191
   164
#define ALPHA_BLIT16_555(to, from, length, bpp, alpha)  \
slouken@7191
   165
    do {                        \
slouken@7191
   166
        int i;                      \
slouken@7191
   167
    Uint16 *src = (Uint16 *)(from);         \
slouken@7191
   168
    Uint16 *dst = (Uint16 *)(to);           \
slouken@7191
   169
    Uint32 ALPHA = alpha >> 3;          \
slouken@7191
   170
    for(i = 0; i < (int)(length); i++) {        \
slouken@7191
   171
        Uint32 s = *src++;              \
slouken@7191
   172
        Uint32 d = *dst;                \
slouken@7191
   173
        s = (s | s << 16) & 0x03e07c1f;     \
slouken@7191
   174
        d = (d | d << 16) & 0x03e07c1f;     \
slouken@7191
   175
        d += (s - d) * ALPHA >> 5;          \
slouken@7191
   176
        d &= 0x03e07c1f;                \
slouken@7191
   177
        *dst++ = (Uint16)(d | d >> 16);         \
slouken@7191
   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@7191
   184
#define ALPHA_BLIT_ANY(to, from, length, bpp, alpha)            \
slouken@7191
   185
    do {                                \
slouken@7191
   186
        int i;                              \
slouken@7191
   187
    Uint8 *src = from;                      \
slouken@7191
   188
    Uint8 *dst = to;                        \
slouken@7191
   189
    for(i = 0; i < (int)(length); i++) {                \
slouken@7191
   190
        Uint32 s, d;                        \
slouken@7191
   191
        unsigned rs, gs, bs, rd, gd, bd;                \
slouken@7191
   192
        switch(bpp) {                       \
slouken@7191
   193
        case 2:                         \
slouken@7191
   194
        s = *(Uint16 *)src;                 \
slouken@7191
   195
        d = *(Uint16 *)dst;                 \
slouken@7191
   196
        break;                          \
slouken@7191
   197
        case 3:                         \
slouken@7191
   198
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {           \
slouken@7191
   199
            s = (src[0] << 16) | (src[1] << 8) | src[2];    \
slouken@7191
   200
            d = (dst[0] << 16) | (dst[1] << 8) | dst[2];    \
slouken@7191
   201
        } else {                        \
slouken@7191
   202
            s = (src[2] << 16) | (src[1] << 8) | src[0];    \
slouken@7191
   203
            d = (dst[2] << 16) | (dst[1] << 8) | dst[0];    \
slouken@7191
   204
        }                           \
slouken@7191
   205
        break;                          \
slouken@7191
   206
        case 4:                         \
slouken@7191
   207
        s = *(Uint32 *)src;                 \
slouken@7191
   208
        d = *(Uint32 *)dst;                 \
slouken@7191
   209
        break;                          \
slouken@7191
   210
        }                               \
slouken@7191
   211
        RGB_FROM_PIXEL(s, fmt, rs, gs, bs);             \
slouken@7191
   212
        RGB_FROM_PIXEL(d, fmt, rd, gd, bd);             \
slouken@7191
   213
        rd += (rs - rd) * alpha >> 8;               \
slouken@7191
   214
        gd += (gs - gd) * alpha >> 8;               \
slouken@7191
   215
        bd += (bs - bd) * alpha >> 8;               \
slouken@7191
   216
        PIXEL_FROM_RGB(d, fmt, rd, gd, bd);             \
slouken@7191
   217
        switch(bpp) {                       \
slouken@7191
   218
        case 2:                         \
slouken@7191
   219
        *(Uint16 *)dst = (Uint16)d;                 \
slouken@7191
   220
        break;                          \
slouken@7191
   221
        case 3:                         \
slouken@7191
   222
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {           \
slouken@7191
   223
            dst[0] = (Uint8)(d >> 16);                  \
slouken@7191
   224
            dst[1] = (Uint8)(d >> 8);                   \
slouken@7191
   225
            dst[2] = (Uint8)(d);                        \
slouken@7191
   226
        } else {                        \
slouken@7191
   227
            dst[0] = (Uint8)d;                      \
slouken@7191
   228
            dst[1] = (Uint8)(d >> 8);                   \
slouken@7191
   229
            dst[2] = (Uint8)(d >> 16);                  \
slouken@7191
   230
        }                           \
slouken@7191
   231
        break;                          \
slouken@7191
   232
        case 4:                         \
slouken@7191
   233
        *(Uint32 *)dst = d;                 \
slouken@7191
   234
        break;                          \
slouken@7191
   235
        }                               \
slouken@7191
   236
        src += bpp;                         \
slouken@7191
   237
        dst += bpp;                         \
slouken@7191
   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@7191
   249
#define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha)       \
slouken@7191
   250
    do {                                \
slouken@7191
   251
        int i;                              \
slouken@7191
   252
    Uint32 *src = (Uint32 *)(from);                 \
slouken@7191
   253
    Uint32 *dst = (Uint32 *)(to);                   \
slouken@7191
   254
    for(i = 0; i < (int)(length); i++) {                \
slouken@7191
   255
        Uint32 s = *src++;                      \
slouken@7191
   256
        Uint32 d = *dst;                        \
slouken@7191
   257
        *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)   \
slouken@7191
   258
             + (s & d & 0x00010101);                \
slouken@7191
   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@7191
   268
#define BLEND16_50(dst, src, mask)          \
slouken@7191
   269
    do {                        \
slouken@7191
   270
    Uint32 s = *src++;              \
slouken@7191
   271
    Uint32 d = *dst;                \
slouken@7191
   272
    *dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) +    \
slouken@7191
   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@7191
   277
#define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask)     \
slouken@7191
   278
    do {                                \
slouken@7191
   279
    unsigned n = (length);                      \
slouken@7191
   280
    Uint16 *src = (Uint16 *)(from);                 \
slouken@7191
   281
    Uint16 *dst = (Uint16 *)(to);                   \
slouken@7191
   282
    if(((uintptr_t)src ^ (uintptr_t)dst) & 3) {         \
slouken@7191
   283
        /* source and destination not in phase, blit one by one */  \
slouken@7191
   284
        while(n--)                          \
slouken@7191
   285
        BLEND16_50(dst, src, mask);             \
slouken@7191
   286
    } else {                            \
slouken@7191
   287
        if((uintptr_t)src & 3) {                    \
slouken@7191
   288
        /* first odd pixel */                   \
slouken@7191
   289
        BLEND16_50(dst, src, mask);             \
slouken@7191
   290
        n--;                            \
slouken@7191
   291
        }                               \
slouken@7191
   292
        for(; n > 1; n -= 2) {                  \
slouken@7191
   293
        Uint32 s = *(Uint32 *)src;              \
slouken@7191
   294
        Uint32 d = *(Uint32 *)dst;              \
slouken@7191
   295
        *(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1)   \
slouken@7191
   296
                       + ((d & (mask | mask << 16)) >> 1)   \
slouken@7191
   297
                       + (s & d & (~(mask | mask << 16)));  \
slouken@7191
   298
        src += 2;                       \
slouken@7191
   299
        dst += 2;                       \
slouken@7191
   300
        }                               \
slouken@7191
   301
        if(n)                           \
slouken@7191
   302
        BLEND16_50(dst, src, mask); /* last odd pixel */    \
slouken@7191
   303
    }                               \
slouken@0
   304
    } while(0)
slouken@0
   305
slouken@7191
   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@7191
   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@7191
   312
#define CHOOSE_BLIT(blitter, alpha, fmt)                \
slouken@7191
   313
    do {                                \
slouken@7191
   314
        if(alpha == 255) {                      \
slouken@7191
   315
        switch(fmt->BytesPerPixel) {                \
slouken@7191
   316
        case 1: blitter(1, Uint8, OPAQUE_BLIT); break;      \
slouken@7191
   317
        case 2: blitter(2, Uint8, OPAQUE_BLIT); break;      \
slouken@7191
   318
        case 3: blitter(3, Uint8, OPAQUE_BLIT); break;      \
slouken@7191
   319
        case 4: blitter(4, Uint16, OPAQUE_BLIT); break;     \
slouken@7191
   320
        }                               \
slouken@7191
   321
    } else {                            \
slouken@7191
   322
        switch(fmt->BytesPerPixel) {                \
slouken@7191
   323
        case 1:                         \
slouken@7191
   324
        /* No 8bpp alpha blitting */                \
slouken@7191
   325
        break;                          \
slouken@7191
   326
                                    \
slouken@7191
   327
        case 2:                         \
slouken@7191
   328
        switch(fmt->Rmask | fmt->Gmask | fmt->Bmask) {      \
slouken@7191
   329
        case 0xffff:                        \
slouken@7191
   330
            if(fmt->Gmask == 0x07e0             \
slouken@7191
   331
               || fmt->Rmask == 0x07e0              \
slouken@7191
   332
               || fmt->Bmask == 0x07e0) {           \
slouken@7191
   333
            if(alpha == 128)                \
slouken@7191
   334
                blitter(2, Uint8, ALPHA_BLIT16_565_50); \
slouken@7191
   335
            else {                      \
slouken@7191
   336
                blitter(2, Uint8, ALPHA_BLIT16_565);    \
slouken@7191
   337
            }                       \
slouken@7191
   338
            } else                      \
slouken@7191
   339
            goto general16;                 \
slouken@7191
   340
            break;                      \
slouken@7191
   341
                                    \
slouken@7191
   342
        case 0x7fff:                        \
slouken@7191
   343
            if(fmt->Gmask == 0x03e0             \
slouken@7191
   344
               || fmt->Rmask == 0x03e0              \
slouken@7191
   345
               || fmt->Bmask == 0x03e0) {           \
slouken@7191
   346
            if(alpha == 128)                \
slouken@7191
   347
                blitter(2, Uint8, ALPHA_BLIT16_555_50); \
slouken@7191
   348
            else {                      \
slouken@7191
   349
                blitter(2, Uint8, ALPHA_BLIT16_555);    \
slouken@7191
   350
            }                       \
slouken@7191
   351
            break;                      \
slouken@7191
   352
            }                           \
slouken@7191
   353
            /* fallthrough */                   \
slouken@7191
   354
                                    \
slouken@7191
   355
        default:                        \
slouken@7191
   356
        general16:                      \
slouken@7191
   357
            blitter(2, Uint8, ALPHA_BLIT_ANY);          \
slouken@7191
   358
        }                           \
slouken@7191
   359
        break;                          \
slouken@7191
   360
                                    \
slouken@7191
   361
        case 3:                         \
slouken@7191
   362
        blitter(3, Uint8, ALPHA_BLIT_ANY);          \
slouken@7191
   363
        break;                          \
slouken@7191
   364
                                    \
slouken@7191
   365
        case 4:                         \
slouken@7191
   366
        if((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff \
slouken@7191
   367
           && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 \
slouken@7191
   368
               || fmt->Bmask == 0xff00)) {          \
slouken@7191
   369
            if(alpha == 128)                    \
slouken@7191
   370
            blitter(4, Uint16, ALPHA_BLIT32_888_50);    \
slouken@7191
   371
            else                        \
slouken@7191
   372
            blitter(4, Uint16, ALPHA_BLIT32_888);       \
slouken@7191
   373
        } else                          \
slouken@7191
   374
            blitter(4, Uint16, ALPHA_BLIT_ANY);         \
slouken@7191
   375
        break;                          \
slouken@7191
   376
        }                               \
slouken@7191
   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@7191
   390
#define RLECLIPBLIT(bpp, Type, do_blit)                    \
slouken@7191
   391
    do {                                   \
slouken@7191
   392
    int linecount = srcrect->h;                    \
slouken@7191
   393
    int ofs = 0;                               \
slouken@7191
   394
    int left = srcrect->x;                         \
slouken@7191
   395
    int right = left + srcrect->w;                     \
slouken@7191
   396
    dstbuf -= left * bpp;                          \
slouken@7191
   397
    for(;;) {                              \
slouken@7191
   398
        int run;                               \
slouken@7191
   399
        ofs += *(Type *)srcbuf;                    \
slouken@7191
   400
        run = ((Type *)srcbuf)[1];                     \
slouken@7191
   401
        srcbuf += 2 * sizeof(Type);                    \
slouken@7191
   402
        if(run) {                              \
slouken@7191
   403
        /* clip to left and right borders */               \
slouken@7191
   404
        if(ofs < right) {                      \
slouken@7191
   405
            int start = 0;                     \
slouken@7191
   406
            int len = run;                     \
slouken@7191
   407
            int startcol;                      \
slouken@7191
   408
            if(left - ofs > 0) {                   \
slouken@7191
   409
            start = left - ofs;                \
slouken@7191
   410
            len -= start;                      \
slouken@7191
   411
            if(len <= 0)                       \
slouken@7191
   412
                goto nocopy ## bpp ## do_blit;         \
slouken@7191
   413
            }                              \
slouken@7191
   414
            startcol = ofs + start;                \
slouken@7191
   415
            if(len > right - startcol)                 \
slouken@7191
   416
            len = right - startcol;                \
slouken@7191
   417
            do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
slouken@7191
   418
                len, bpp, alpha);                  \
slouken@7191
   419
        }                              \
slouken@7191
   420
        nocopy ## bpp ## do_blit:                      \
slouken@7191
   421
        srcbuf += run * bpp;                       \
slouken@7191
   422
        ofs += run;                        \
slouken@7191
   423
        } else if(!ofs)                        \
slouken@7191
   424
        break;                             \
slouken@7191
   425
        if(ofs == w) {                         \
slouken@7191
   426
        ofs = 0;                           \
slouken@7191
   427
        dstbuf += dst->pitch;                      \
slouken@7191
   428
        if(!--linecount)                       \
slouken@7191
   429
            break;                         \
slouken@7191
   430
        }                                  \
slouken@7191
   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
    {
philipp@7128
   467
        /* skip lines at the top if necessary */
slouken@1895
   468
        int vskip = srcrect->y;
slouken@1895
   469
        int ofs = 0;
slouken@1895
   470
        if (vskip) {
slouken@0
   471
slouken@7191
   472
#define RLESKIP(bpp, Type)          \
slouken@7191
   473
        for(;;) {           \
slouken@7191
   474
            int run;            \
slouken@7191
   475
            ofs += *(Type *)srcbuf; \
slouken@7191
   476
            run = ((Type *)srcbuf)[1];  \
slouken@7191
   477
            srcbuf += sizeof(Type) * 2; \
slouken@7191
   478
            if(run) {           \
slouken@7191
   479
            srcbuf += run * bpp;    \
slouken@7191
   480
            ofs += run;     \
slouken@7191
   481
            } else if(!ofs)     \
slouken@7191
   482
            goto done;      \
slouken@7191
   483
            if(ofs == w) {      \
slouken@7191
   484
            ofs = 0;        \
slouken@7191
   485
            if(!--vskip)        \
slouken@7191
   486
                break;      \
slouken@7191
   487
            }               \
slouken@7191
   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@7191
   517
#define RLEBLIT(bpp, Type, do_blit)                       \
slouken@7191
   518
        do {                                  \
slouken@7191
   519
        int linecount = srcrect->h;                   \
slouken@7191
   520
        int ofs = 0;                              \
slouken@7191
   521
        for(;;) {                             \
slouken@7191
   522
            unsigned run;                         \
slouken@7191
   523
            ofs += *(Type *)srcbuf;                   \
slouken@7191
   524
            run = ((Type *)srcbuf)[1];                    \
slouken@7191
   525
            srcbuf += 2 * sizeof(Type);                   \
slouken@7191
   526
            if(run) {                             \
slouken@7191
   527
            do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
slouken@7191
   528
            srcbuf += run * bpp;                      \
slouken@7191
   529
            ofs += run;                       \
slouken@7191
   530
            } else if(!ofs)                       \
slouken@7191
   531
            break;                            \
slouken@7191
   532
            if(ofs == w) {                        \
slouken@7191
   533
            ofs = 0;                          \
slouken@7191
   534
            dstbuf += dst->pitch;                     \
slouken@7191
   535
            if(!--linecount)                      \
slouken@7191
   536
                break;                        \
slouken@7191
   537
            }                                 \
slouken@7191
   538
        }                                 \
slouken@7191
   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@7191
   565
#define BLIT_TRANSL_888(src, dst)               \
slouken@7191
   566
    do {                            \
slouken@7191
   567
        Uint32 s = src;                     \
slouken@7191
   568
    Uint32 d = dst;                     \
slouken@7191
   569
    unsigned alpha = s >> 24;               \
slouken@7191
   570
    Uint32 s1 = s & 0xff00ff;               \
slouken@7191
   571
    Uint32 d1 = d & 0xff00ff;               \
slouken@7191
   572
    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;    \
slouken@7191
   573
    s &= 0xff00;                        \
slouken@7191
   574
    d &= 0xff00;                        \
slouken@7191
   575
    d = (d + ((s - d) * alpha >> 8)) & 0xff00;      \
slouken@7191
   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@7191
   583
#define BLIT_TRANSL_565(src, dst)       \
slouken@7191
   584
    do {                    \
slouken@7191
   585
    Uint32 s = src;             \
slouken@7191
   586
    Uint32 d = dst;             \
slouken@7191
   587
    unsigned alpha = (s & 0x3e0) >> 5;  \
slouken@7191
   588
    s &= 0x07e0f81f;            \
slouken@7191
   589
    d = (d | d << 16) & 0x07e0f81f;     \
slouken@7191
   590
    d += (s - d) * alpha >> 5;      \
slouken@7191
   591
    d &= 0x07e0f81f;            \
slouken@7191
   592
    dst = (Uint16)(d | d >> 16);            \
slouken@0
   593
    } while(0)
slouken@0
   594
slouken@7191
   595
#define BLIT_TRANSL_555(src, dst)       \
slouken@7191
   596
    do {                    \
slouken@7191
   597
    Uint32 s = src;             \
slouken@7191
   598
    Uint32 d = dst;             \
slouken@7191
   599
    unsigned alpha = (s & 0x3e0) >> 5;  \
slouken@7191
   600
    s &= 0x03e07c1f;            \
slouken@7191
   601
    d = (d | d << 16) & 0x03e07c1f;     \
slouken@7191
   602
    d += (s - d) * alpha >> 5;      \
slouken@7191
   603
    d &= 0x03e07c1f;            \
slouken@7191
   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@7191
   638
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend)              \
slouken@7191
   639
    do {                                  \
slouken@7191
   640
    int linecount = srcrect->h;                   \
slouken@7191
   641
    int left = srcrect->x;                        \
slouken@7191
   642
    int right = left + srcrect->w;                    \
slouken@7191
   643
    dstbuf -= left * sizeof(Ptype);                   \
slouken@7191
   644
    do {                                  \
slouken@7191
   645
        int ofs = 0;                          \
slouken@7191
   646
        /* blit opaque pixels on one line */              \
slouken@7191
   647
        do {                              \
slouken@7191
   648
        unsigned run;                         \
slouken@7191
   649
        ofs += ((Ctype *)srcbuf)[0];                  \
slouken@7191
   650
        run = ((Ctype *)srcbuf)[1];               \
slouken@7191
   651
        srcbuf += 2 * sizeof(Ctype);                  \
slouken@7191
   652
        if(run) {                         \
slouken@7191
   653
            /* clip to left and right borders */          \
slouken@7191
   654
            int cofs = ofs;                   \
slouken@7191
   655
            int crun = run;                   \
slouken@7191
   656
            if(left - cofs > 0) {                 \
slouken@7191
   657
            crun -= left - cofs;                  \
slouken@7191
   658
            cofs = left;                      \
slouken@7191
   659
            }                             \
slouken@7191
   660
            if(crun > right - cofs)               \
slouken@7191
   661
            crun = right - cofs;                  \
slouken@7191
   662
            if(crun > 0)                      \
slouken@7191
   663
            PIXEL_COPY(dstbuf + cofs * sizeof(Ptype),     \
slouken@7191
   664
                   srcbuf + (cofs - ofs) * sizeof(Ptype), \
slouken@7191
   665
                   (unsigned)crun, sizeof(Ptype));    \
slouken@7191
   666
            srcbuf += run * sizeof(Ptype);            \
slouken@7191
   667
            ofs += run;                       \
slouken@7191
   668
        } else if(!ofs)                       \
slouken@7191
   669
            return;                       \
slouken@7191
   670
        } while(ofs < w);                         \
slouken@7191
   671
        /* skip padding if necessary */               \
slouken@7191
   672
        if(sizeof(Ptype) == 2)                    \
slouken@7191
   673
        srcbuf += (uintptr_t)srcbuf & 2;              \
slouken@7191
   674
        /* blit translucent pixels on the same line */        \
slouken@7191
   675
        ofs = 0;                              \
slouken@7191
   676
        do {                              \
slouken@7191
   677
        unsigned run;                         \
slouken@7191
   678
        ofs += ((Uint16 *)srcbuf)[0];                 \
slouken@7191
   679
        run = ((Uint16 *)srcbuf)[1];                  \
slouken@7191
   680
        srcbuf += 4;                          \
slouken@7191
   681
        if(run) {                         \
slouken@7191
   682
            /* clip to left and right borders */          \
slouken@7191
   683
            int cofs = ofs;                   \
slouken@7191
   684
            int crun = run;                   \
slouken@7191
   685
            if(left - cofs > 0) {                 \
slouken@7191
   686
            crun -= left - cofs;                  \
slouken@7191
   687
            cofs = left;                      \
slouken@7191
   688
            }                             \
slouken@7191
   689
            if(crun > right - cofs)               \
slouken@7191
   690
            crun = right - cofs;                  \
slouken@7191
   691
            if(crun > 0) {                    \
slouken@7191
   692
            Ptype *dst = (Ptype *)dstbuf + cofs;          \
slouken@7191
   693
            Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs);    \
slouken@7191
   694
            int i;                        \
slouken@7191
   695
            for(i = 0; i < crun; i++)             \
slouken@7191
   696
                do_blend(src[i], dst[i]);             \
slouken@7191
   697
            }                             \
slouken@7191
   698
            srcbuf += run * 4;                    \
slouken@7191
   699
            ofs += run;                       \
slouken@7191
   700
        }                             \
slouken@7191
   701
        } while(ofs < w);                         \
slouken@7191
   702
        dstbuf += dst->pitch;                     \
slouken@7191
   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@7191
   807
#define RLEALPHABLIT(Ptype, Ctype, do_blend)                 \
slouken@7191
   808
    do {                                 \
slouken@7191
   809
        int linecount = srcrect->h;                  \
slouken@7191
   810
        do {                             \
slouken@7191
   811
        int ofs = 0;                         \
slouken@7191
   812
        /* blit opaque pixels on one line */             \
slouken@7191
   813
        do {                             \
slouken@7191
   814
            unsigned run;                    \
slouken@7191
   815
            ofs += ((Ctype *)srcbuf)[0];             \
slouken@7191
   816
            run = ((Ctype *)srcbuf)[1];              \
slouken@7191
   817
            srcbuf += 2 * sizeof(Ctype);             \
slouken@7191
   818
            if(run) {                        \
slouken@7191
   819
            PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
slouken@7191
   820
                   run, sizeof(Ptype));          \
slouken@7191
   821
            srcbuf += run * sizeof(Ptype);           \
slouken@7191
   822
            ofs += run;                  \
slouken@7191
   823
            } else if(!ofs)                  \
slouken@7191
   824
            goto done;                   \
slouken@7191
   825
        } while(ofs < w);                    \
slouken@7191
   826
        /* skip padding if necessary */              \
slouken@7191
   827
        if(sizeof(Ptype) == 2)                   \
slouken@7191
   828
            srcbuf += (uintptr_t)srcbuf & 2;             \
slouken@7191
   829
        /* blit translucent pixels on the same line */       \
slouken@7191
   830
        ofs = 0;                         \
slouken@7191
   831
        do {                             \
slouken@7191
   832
            unsigned run;                    \
slouken@7191
   833
            ofs += ((Uint16 *)srcbuf)[0];            \
slouken@7191
   834
            run = ((Uint16 *)srcbuf)[1];             \
slouken@7191
   835
            srcbuf += 4;                     \
slouken@7191
   836
            if(run) {                        \
slouken@7191
   837
            Ptype *dst = (Ptype *)dstbuf + ofs;      \
slouken@7191
   838
            unsigned i;                  \
slouken@7191
   839
            for(i = 0; i < run; i++) {           \
slouken@7191
   840
                Uint32 src = *(Uint32 *)srcbuf;      \
slouken@7191
   841
                do_blend(src, *dst);             \
slouken@7191
   842
                srcbuf += 4;                 \
slouken@7191
   843
                dst++;                   \
slouken@7191
   844
            }                        \
slouken@7191
   845
            ofs += run;                  \
slouken@7191
   846
            }                            \
slouken@7191
   847
        } while(ofs < w);                    \
slouken@7191
   848
        dstbuf += dst->pitch;                    \
slouken@7191
   849
        } while(--linecount);                    \
slouken@7191
   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@7191
  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) {
icculus@7037
  1090
        return SDL_OutOfMemory();
slouken@0
  1091
    }
slouken@0
  1092
    {
slouken@1895
  1093
        /* save the destination format so we can undo the encoding later */
slouken@1895
  1094
        RLEDestFormat *r = (RLEDestFormat *) rlebuf;
slouken@1895
  1095
        r->BytesPerPixel = df->BytesPerPixel;
slouken@5435
  1096
        r->Rmask = df->Rmask;
slouken@5435
  1097
        r->Gmask = df->Gmask;
slouken@5435
  1098
        r->Bmask = df->Bmask;
slouken@5435
  1099
        r->Amask = df->Amask;
slouken@1895
  1100
        r->Rloss = df->Rloss;
slouken@1895
  1101
        r->Gloss = df->Gloss;
slouken@1895
  1102
        r->Bloss = df->Bloss;
slouken@5435
  1103
        r->Aloss = df->Aloss;
slouken@1895
  1104
        r->Rshift = df->Rshift;
slouken@1895
  1105
        r->Gshift = df->Gshift;
slouken@1895
  1106
        r->Bshift = df->Bshift;
slouken@1895
  1107
        r->Ashift = df->Ashift;
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@7191
  1120
#define ADD_OPAQUE_COUNTS(n, m)         \
slouken@7191
  1121
    if(df->BytesPerPixel == 4) {        \
slouken@7191
  1122
        ((Uint16 *)dst)[0] = n;     \
slouken@7191
  1123
        ((Uint16 *)dst)[1] = m;     \
slouken@7191
  1124
        dst += 4;               \
slouken@7191
  1125
    } else {                \
slouken@7191
  1126
        dst[0] = n;             \
slouken@7191
  1127
        dst[1] = m;             \
slouken@7191
  1128
        dst += 2;               \
slouken@7191
  1129
    }
slouken@0
  1130
slouken@1895
  1131
        /* translucent counts are always 16 bit */
slouken@7191
  1132
#define ADD_TRANSL_COUNTS(n, m)     \
slouken@7191
  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;
icculus@6389
  1272
    Uint8 *srcbuf, *lastline;
slouken@1895
  1273
    int maxsize = 0;
slouken@1895
  1274
    int bpp = surface->format->BytesPerPixel;
slouken@1895
  1275
    getpix_func getpix;
slouken@1895
  1276
    Uint32 ckey, rgbmask;
slouken@1895
  1277
    int w, h;
slouken@0
  1278
slouken@1895
  1279
    /* calculate the worst case size for the compressed surface */
slouken@1895
  1280
    switch (bpp) {
slouken@1895
  1281
    case 1:
slouken@1895
  1282
        /* worst case is alternating opaque and transparent pixels,
slouken@1895
  1283
           starting with an opaque pixel */
slouken@1895
  1284
        maxsize = surface->h * 3 * (surface->w / 2 + 1) + 2;
slouken@1895
  1285
        break;
slouken@1895
  1286
    case 2:
slouken@1895
  1287
    case 3:
slouken@1895
  1288
        /* worst case is solid runs, at most 255 pixels wide */
slouken@1895
  1289
        maxsize = surface->h * (2 * (surface->w / 255 + 1)
slouken@1895
  1290
                                + surface->w * bpp) + 2;
slouken@1895
  1291
        break;
slouken@1895
  1292
    case 4:
slouken@1895
  1293
        /* worst case is solid runs, at most 65535 pixels wide */
slouken@1895
  1294
        maxsize = surface->h * (4 * (surface->w / 65535 + 1)
slouken@1895
  1295
                                + surface->w * 4) + 4;
slouken@1895
  1296
        break;
slouken@1895
  1297
    }
slouken@0
  1298
slouken@1895
  1299
    rlebuf = (Uint8 *) SDL_malloc(maxsize);
slouken@1895
  1300
    if (rlebuf == NULL) {
icculus@7037
  1301
        return SDL_OutOfMemory();
slouken@1895
  1302
    }
slouken@0
  1303
slouken@1895
  1304
    /* Set up the conversion */
slouken@1895
  1305
    srcbuf = (Uint8 *) surface->pixels;
slouken@1895
  1306
    maxn = bpp == 4 ? 65535 : 255;
slouken@1895
  1307
    dst = rlebuf;
slouken@1895
  1308
    rgbmask = ~surface->format->Amask;
slouken@2262
  1309
    ckey = surface->map->info.colorkey & rgbmask;
slouken@1895
  1310
    lastline = dst;
slouken@1895
  1311
    getpix = getpixes[bpp - 1];
slouken@1895
  1312
    w = surface->w;
slouken@1895
  1313
    h = surface->h;
slouken@0
  1314
slouken@7191
  1315
#define ADD_COUNTS(n, m)            \
slouken@7191
  1316
    if(bpp == 4) {              \
slouken@7191
  1317
        ((Uint16 *)dst)[0] = n;     \
slouken@7191
  1318
        ((Uint16 *)dst)[1] = m;     \
slouken@7191
  1319
        dst += 4;               \
slouken@7191
  1320
    } else {                \
slouken@7191
  1321
        dst[0] = n;             \
slouken@7191
  1322
        dst[1] = m;             \
slouken@7191
  1323
        dst += 2;               \
slouken@7191
  1324
    }
slouken@0
  1325
slouken@1895
  1326
    for (y = 0; y < h; y++) {
slouken@1895
  1327
        int x = 0;
slouken@1895
  1328
        int blankline = 0;
slouken@1895
  1329
        do {
slouken@1895
  1330
            int run, skip, len;
slouken@1895
  1331
            int runstart;
slouken@1895
  1332
            int skipstart = x;
slouken@0
  1333
slouken@1895
  1334
            /* find run of transparent, then opaque pixels */
slouken@1895
  1335
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) == ckey)
slouken@1895
  1336
                x++;
slouken@1895
  1337
            runstart = x;
slouken@1895
  1338
            while (x < w && (getpix(srcbuf + x * bpp) & rgbmask) != ckey)
slouken@1895
  1339
                x++;
slouken@1895
  1340
            skip = runstart - skipstart;
slouken@1895
  1341
            if (skip == w)
slouken@1895
  1342
                blankline = 1;
slouken@1895
  1343
            run = x - runstart;
slouken@0
  1344
slouken@1895
  1345
            /* encode segment */
slouken@1895
  1346
            while (skip > maxn) {
slouken@1895
  1347
                ADD_COUNTS(maxn, 0);
slouken@1895
  1348
                skip -= maxn;
slouken@1895
  1349
            }
slouken@1895
  1350
            len = MIN(run, maxn);
slouken@1895
  1351
            ADD_COUNTS(skip, len);
slouken@1895
  1352
            SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1353
            dst += len * bpp;
slouken@1895
  1354
            run -= len;
slouken@1895
  1355
            runstart += len;
slouken@1895
  1356
            while (run) {
slouken@1895
  1357
                len = MIN(run, maxn);
slouken@1895
  1358
                ADD_COUNTS(0, len);
slouken@1895
  1359
                SDL_memcpy(dst, srcbuf + runstart * bpp, len * bpp);
slouken@1895
  1360
                dst += len * bpp;
slouken@1895
  1361
                runstart += len;
slouken@1895
  1362
                run -= len;
slouken@1895
  1363
            }
slouken@1895
  1364
            if (!blankline)
slouken@1895
  1365
                lastline = dst;
slouken@2735
  1366
        } while (x < w);
slouken@0
  1367
slouken@1895
  1368
        srcbuf += surface->pitch;
slouken@1895
  1369
    }
slouken@1895
  1370
    dst = lastline;             /* back up bast trailing blank lines */
slouken@1895
  1371
    ADD_COUNTS(0, 0);
slouken@0
  1372
slouken@0
  1373
#undef ADD_COUNTS
slouken@0
  1374
slouken@1895
  1375
    /* Now that we have it encoded, release the original pixels */
slouken@2222
  1376
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1377
        SDL_free(surface->pixels);
slouken@1895
  1378
        surface->pixels = NULL;
slouken@1895
  1379
    }
slouken@0
  1380
slouken@1895
  1381
    /* realloc the buffer to release unused memory */
slouken@1895
  1382
    {
slouken@1895
  1383
        /* If realloc returns NULL, the original block is left intact */
slouken@1895
  1384
        Uint8 *p = SDL_realloc(rlebuf, dst - rlebuf);
slouken@1895
  1385
        if (!p)
slouken@1895
  1386
            p = rlebuf;
slouken@2257
  1387
        surface->map->data = p;
slouken@1895
  1388
    }
slouken@0
  1389
slouken@1895
  1390
    return (0);
slouken@0
  1391
}
slouken@0
  1392
slouken@1895
  1393
int
slouken@1895
  1394
SDL_RLESurface(SDL_Surface * surface)
slouken@0
  1395
{
slouken@2266
  1396
    int flags;
slouken@0
  1397
slouken@1895
  1398
    /* Clear any previous RLE conversion */
slouken@1895
  1399
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
  1400
        SDL_UnRLESurface(surface, 1);
slouken@1895
  1401
    }
slouken@0
  1402
slouken@1895
  1403
    /* We don't support RLE encoding of bitmaps */
slouken@1895
  1404
    if (surface->format->BitsPerPixel < 8) {
slouken@2266
  1405
        return -1;
slouken@1895
  1406
    }
slouken@0
  1407
slouken@2266
  1408
    /* Make sure the pixels are available */
slouken@2266
  1409
    if (!surface->pixels) {
slouken@2266
  1410
        return -1;
slouken@1895
  1411
    }
slouken@0
  1412
slouken@2266
  1413
    /* If we don't have colorkey or blending, nothing to do... */
slouken@2266
  1414
    flags = surface->map->info.flags;
slouken@2267
  1415
    if (!(flags & (SDL_COPY_COLORKEY | SDL_COPY_BLEND))) {
slouken@2266
  1416
        return -1;
slouken@1895
  1417
    }
slouken@0
  1418
slouken@2266
  1419
    /* Pass on combinations not supported */
slouken@2266
  1420
    if ((flags & SDL_COPY_MODULATE_COLOR) ||
slouken@2824
  1421
        ((flags & SDL_COPY_MODULATE_ALPHA) && surface->format->Amask) ||
slouken@5184
  1422
        (flags & (SDL_COPY_ADD | SDL_COPY_MOD)) ||
slouken@2266
  1423
        (flags & SDL_COPY_NEAREST)) {
slouken@2266
  1424
        return -1;
slouken@1895
  1425
    }
slouken@0
  1426
slouken@2266
  1427
    /* Encode and set up the blit */
slouken@2266
  1428
    if (!surface->format->Amask || !(flags & SDL_COPY_BLEND)) {
slouken@2266
  1429
        if (!surface->map->identity) {
slouken@2266
  1430
            return -1;
slouken@2266
  1431
        }
slouken@2266
  1432
        if (RLEColorkeySurface(surface) < 0) {
slouken@2266
  1433
            return -1;
slouken@2266
  1434
        }
slouken@2266
  1435
        surface->map->blit = SDL_RLEBlit;
slouken@2266
  1436
        surface->map->info.flags |= SDL_COPY_RLE_COLORKEY;
slouken@2266
  1437
    } else {
slouken@2266
  1438
        if (RLEAlphaSurface(surface) < 0) {
slouken@2266
  1439
            return -1;
slouken@2266
  1440
        }
slouken@2266
  1441
        surface->map->blit = SDL_RLEAlphaBlit;
slouken@2266
  1442
        surface->map->info.flags |= SDL_COPY_RLE_ALPHAKEY;
slouken@2266
  1443
    }
slouken@0
  1444
slouken@1895
  1445
    /* The surface is now accelerated */
slouken@1895
  1446
    surface->flags |= SDL_RLEACCEL;
slouken@0
  1447
slouken@1895
  1448
    return (0);
slouken@0
  1449
}
slouken@0
  1450
slouken@0
  1451
/*
slouken@0
  1452
 * Un-RLE a surface with pixel alpha
slouken@0
  1453
 * This may not give back exactly the image before RLE-encoding; all
philipp@7128
  1454
 * completely transparent pixels will be lost, and color and alpha depth
slouken@0
  1455
 * may have been reduced (when encoding for 16bpp targets).
slouken@0
  1456
 */
slouken@1895
  1457
static SDL_bool
slouken@1895
  1458
UnRLEAlpha(SDL_Surface * surface)
slouken@0
  1459
{
slouken@0
  1460
    Uint8 *srcbuf;
slouken@0
  1461
    Uint32 *dst;
slouken@0
  1462
    SDL_PixelFormat *sf = surface->format;
slouken@2257
  1463
    RLEDestFormat *df = surface->map->data;
slouken@1895
  1464
    int (*uncopy_opaque) (Uint32 *, void *, int,
slouken@1895
  1465
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@1895
  1466
    int (*uncopy_transl) (Uint32 *, void *, int,
slouken@1895
  1467
                          RLEDestFormat *, SDL_PixelFormat *);
slouken@0
  1468
    int w = surface->w;
slouken@0
  1469
    int bpp = df->BytesPerPixel;
slouken@0
  1470
slouken@1895
  1471
    if (bpp == 2) {
slouken@1895
  1472
        uncopy_opaque = uncopy_opaque_16;
slouken@1895
  1473
        uncopy_transl = uncopy_transl_16;
slouken@0
  1474
    } else {
slouken@1895
  1475
        uncopy_opaque = uncopy_transl = uncopy_32;
slouken@0
  1476
    }
slouken@0
  1477
slouken@1336
  1478
    surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1479
    if (!surface->pixels) {
slouken@1895
  1480
        return (SDL_FALSE);
slouken@944
  1481
    }
slouken@0
  1482
    /* fill background with transparent pixels */
slouken@1336
  1483
    SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@0
  1484
slouken@0
  1485
    dst = surface->pixels;
slouken@1895
  1486
    srcbuf = (Uint8 *) (df + 1);
slouken@1895
  1487
    for (;;) {
slouken@1895
  1488
        /* copy opaque pixels */
slouken@1895
  1489
        int ofs = 0;
slouken@1895
  1490
        do {
slouken@1895
  1491
            unsigned run;
slouken@1895
  1492
            if (bpp == 2) {
slouken@1895
  1493
                ofs += srcbuf[0];
slouken@1895
  1494
                run = srcbuf[1];
slouken@1895
  1495
                srcbuf += 2;
slouken@1895
  1496
            } else {
slouken@1895
  1497
                ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1498
                run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1499
                srcbuf += 4;
slouken@1895
  1500
            }
slouken@1895
  1501
            if (run) {
slouken@1895
  1502
                srcbuf += uncopy_opaque(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1503
                ofs += run;
slouken@1895
  1504
            } else if (!ofs)
slouken@1895
  1505
                return (SDL_TRUE);
slouken@2735
  1506
        } while (ofs < w);
slouken@0
  1507
slouken@1895
  1508
        /* skip padding if needed */
slouken@1895
  1509
        if (bpp == 2)
slouken@1895
  1510
            srcbuf += (uintptr_t) srcbuf & 2;
slouken@1895
  1511
slouken@1895
  1512
        /* copy translucent pixels */
slouken@1895
  1513
        ofs = 0;
slouken@1895
  1514
        do {
slouken@1895
  1515
            unsigned run;
slouken@1895
  1516
            ofs += ((Uint16 *) srcbuf)[0];
slouken@1895
  1517
            run = ((Uint16 *) srcbuf)[1];
slouken@1895
  1518
            srcbuf += 4;
slouken@1895
  1519
            if (run) {
slouken@1895
  1520
                srcbuf += uncopy_transl(dst + ofs, srcbuf, run, df, sf);
slouken@1895
  1521
                ofs += run;
slouken@1895
  1522
            }
slouken@2735
  1523
        } while (ofs < w);
slouken@1895
  1524
        dst += surface->pitch >> 2;
slouken@0
  1525
    }
slouken@944
  1526
    /* Make the compiler happy */
slouken@1895
  1527
    return (SDL_TRUE);
slouken@0
  1528
}
slouken@0
  1529
slouken@1895
  1530
void
slouken@1895
  1531
SDL_UnRLESurface(SDL_Surface * surface, int recode)
slouken@0
  1532
{
slouken@2266
  1533
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1534
        surface->flags &= ~SDL_RLEACCEL;
slouken@0
  1535
slouken@2222
  1536
        if (recode && !(surface->flags & SDL_PREALLOC)) {
slouken@2266
  1537
            if (surface->map->info.flags & SDL_COPY_RLE_COLORKEY) {
slouken@1895
  1538
                SDL_Rect full;
slouken@0
  1539
slouken@1895
  1540
                /* re-create the original surface */
slouken@1895
  1541
                surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
  1542
                if (!surface->pixels) {
slouken@1895
  1543
                    /* Oh crap... */
slouken@1895
  1544
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1545
                    return;
slouken@1895
  1546
                }
slouken@0
  1547
philipp@7128
  1548
                /* fill it with the background color */
slouken@2262
  1549
                SDL_FillRect(surface, NULL, surface->map->info.colorkey);
slouken@0
  1550
slouken@1895
  1551
                /* now render the encoded surface */
slouken@1895
  1552
                full.x = full.y = 0;
slouken@1895
  1553
                full.w = surface->w;
slouken@1895
  1554
                full.h = surface->h;
slouken@1895
  1555
                SDL_RLEBlit(surface, &full, surface, &full);
slouken@1895
  1556
            } else {
slouken@1895
  1557
                if (!UnRLEAlpha(surface)) {
slouken@1895
  1558
                    /* Oh crap... */
slouken@1895
  1559
                    surface->flags |= SDL_RLEACCEL;
slouken@1895
  1560
                    return;
slouken@1895
  1561
                }
slouken@1895
  1562
            }
slouken@1895
  1563
        }
slouken@2267
  1564
        surface->map->info.flags &=
slouken@2798
  1565
            ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY);
slouken@0
  1566
slouken@2266
  1567
        if (surface->map->data) {
slouken@2257
  1568
            SDL_free(surface->map->data);
slouken@2257
  1569
            surface->map->data = NULL;
slouken@1895
  1570
        }
slouken@0
  1571
    }
slouken@0
  1572
}
slouken@0
  1573
slouken@1895
  1574
/* vi: set ts=4 sw=4 expandtab: */