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