src/video/SDL_RLEaccel.c
author Sylvain Becker <sylvain.becker@gmail.com>
Mon, 04 Feb 2019 09:11:07 +0100
changeset 12591 66cd8731c3b1
parent 12503 806492103856
permissions -rw-r--r--
Fixed bug 4484 - use SIMD aligned memory for SDL_Surface

Surfaces are allocated using SDL_SIMDAlloc()
They are marked with SDL_SIMD_ALIGNED flag to appropriatly free them with SDL_SIMDFree()
(Flag is cleared when pixels is free'd in RLE, in case user would hijack the pixels ptr)

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