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