src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 30 Jan 2018 18:08:34 -0800
changeset 11839 971881e55d61
parent 11811 5d94cb6b24d3
child 12273 c522b61334f1
permissions -rw-r--r--
Fixed ISO C99 compatibility
SDL now builds with gcc 7.2 with the following command line options:
-Wall -pedantic-errors -Wno-deprecated-declarations -Wno-overlength-strings --std=c99
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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@10638
    23
#ifndef SDL_blit_h_
slouken@10638
    24
#define SDL_blit_h_
slouken@0
    25
slouken@2250
    26
#include "SDL_cpuinfo.h"
slouken@0
    27
#include "SDL_endian.h"
slouken@5163
    28
#include "SDL_surface.h"
slouken@0
    29
slouken@5439
    30
/* Table to do pixel byte expansion */
slouken@5439
    31
extern Uint8* SDL_expand_byte[9];
slouken@5439
    32
slouken@2262
    33
/* SDL blit copy flags */
slouken@2266
    34
#define SDL_COPY_MODULATE_COLOR     0x00000001
slouken@2266
    35
#define SDL_COPY_MODULATE_ALPHA     0x00000002
slouken@5140
    36
#define SDL_COPY_BLEND              0x00000010
slouken@5140
    37
#define SDL_COPY_ADD                0x00000020
slouken@5184
    38
#define SDL_COPY_MOD                0x00000040
slouken@2266
    39
#define SDL_COPY_COLORKEY           0x00000100
slouken@2266
    40
#define SDL_COPY_NEAREST            0x00000200
slouken@2266
    41
#define SDL_COPY_RLE_DESIRED        0x00001000
slouken@2266
    42
#define SDL_COPY_RLE_COLORKEY       0x00002000
slouken@2266
    43
#define SDL_COPY_RLE_ALPHAKEY       0x00004000
slouken@2853
    44
#define SDL_COPY_RLE_MASK           (SDL_COPY_RLE_DESIRED|SDL_COPY_RLE_COLORKEY|SDL_COPY_RLE_ALPHAKEY)
slouken@2262
    45
slouken@2262
    46
/* SDL blit CPU flags */
slouken@2266
    47
#define SDL_CPU_ANY                 0x00000000
slouken@2266
    48
#define SDL_CPU_MMX                 0x00000001
slouken@5389
    49
#define SDL_CPU_3DNOW               0x00000002
slouken@2266
    50
#define SDL_CPU_SSE                 0x00000004
slouken@2266
    51
#define SDL_CPU_SSE2                0x00000008
slouken@5389
    52
#define SDL_CPU_ALTIVEC_PREFETCH    0x00000010
slouken@5389
    53
#define SDL_CPU_ALTIVEC_NOPREFETCH  0x00000020
slouken@2262
    54
slouken@2267
    55
typedef struct
slouken@2267
    56
{
slouken@2262
    57
    Uint8 *src;
slouken@2262
    58
    int src_w, src_h;
slouken@2262
    59
    int src_pitch;
slouken@2267
    60
    int src_skip;
slouken@2262
    61
    Uint8 *dst;
slouken@2262
    62
    int dst_w, dst_h;
slouken@2262
    63
    int dst_pitch;
slouken@2267
    64
    int dst_skip;
slouken@2262
    65
    SDL_PixelFormat *src_fmt;
slouken@2262
    66
    SDL_PixelFormat *dst_fmt;
slouken@1895
    67
    Uint8 *table;
slouken@2262
    68
    int flags;
slouken@2262
    69
    Uint32 colorkey;
slouken@2262
    70
    Uint8 r, g, b, a;
slouken@0
    71
} SDL_BlitInfo;
slouken@0
    72
slouken@11273
    73
typedef void (*SDL_BlitFunc) (SDL_BlitInfo *info);
slouken@11273
    74
slouken@2262
    75
slouken@2267
    76
typedef struct
slouken@2267
    77
{
slouken@2262
    78
    Uint32 src_format;
slouken@2262
    79
    Uint32 dst_format;
slouken@2262
    80
    int flags;
slouken@2262
    81
    int cpu;
slouken@2262
    82
    SDL_BlitFunc func;
slouken@2262
    83
} SDL_BlitFuncEntry;
slouken@0
    84
slouken@0
    85
/* Blit mapping definition */
slouken@1895
    86
typedef struct SDL_BlitMap
slouken@1895
    87
{
slouken@1895
    88
    SDL_Surface *dst;
slouken@1895
    89
    int identity;
slouken@2257
    90
    SDL_blit blit;
slouken@2257
    91
    void *data;
slouken@2262
    92
    SDL_BlitInfo info;
slouken@0
    93
slouken@1895
    94
    /* the version count matches the destination; mismatch indicates
slouken@1895
    95
       an invalid mapping */
slouken@6166
    96
    Uint32 dst_palette_version;
slouken@6166
    97
    Uint32 src_palette_version;
slouken@0
    98
} SDL_BlitMap;
slouken@0
    99
slouken@0
   100
/* Functions found in SDL_blit.c */
slouken@1895
   101
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   102
slouken@2267
   103
/* Functions found in SDL_blit_*.c */
slouken@2267
   104
extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
slouken@2267
   105
extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
slouken@2267
   106
extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
slouken@2267
   107
extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
slouken@2263
   108
slouken@0
   109
/*
slouken@0
   110
 * Useful macros for blitting routines
slouken@0
   111
 */
slouken@0
   112
slouken@2249
   113
#if defined(__GNUC__)
slouken@2249
   114
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   115
#elif defined(_MSC_VER)
slouken@2251
   116
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   117
#else
slouken@2249
   118
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   119
#endif
slouken@2249
   120
slouken@0
   121
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@7364
   122
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)                             \
slouken@7364
   123
{                                                                       \
slouken@7191
   124
    r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@7191
   125
    g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@7191
   126
    b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@0
   127
}
slouken@7364
   128
#define RGB_FROM_RGB565(Pixel, r, g, b)                                 \
slouken@7364
   129
    {                                                                   \
slouken@7364
   130
    r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];                       \
slouken@7364
   131
    g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)];                        \
slouken@7364
   132
    b = SDL_expand_byte[3][(Pixel&0x001F)];                             \
slouken@0
   133
}
slouken@7364
   134
#define RGB_FROM_RGB555(Pixel, r, g, b)                                 \
slouken@7364
   135
{                                                                       \
slouken@7364
   136
    r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];                       \
slouken@7364
   137
    g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)];                        \
slouken@7364
   138
    b = SDL_expand_byte[3][(Pixel&0x001F)];                             \
slouken@0
   139
}
slouken@7364
   140
#define RGB_FROM_RGB888(Pixel, r, g, b)                                 \
slouken@7364
   141
{                                                                       \
slouken@7364
   142
    r = ((Pixel&0xFF0000)>>16);                                         \
slouken@7364
   143
    g = ((Pixel&0xFF00)>>8);                                            \
slouken@7364
   144
    b = (Pixel&0xFF);                                                   \
slouken@0
   145
}
slouken@7364
   146
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)                             \
slouken@7364
   147
do {                                                                    \
slouken@7364
   148
    switch (bpp) {                                                      \
slouken@7364
   149
        case 1:                                                         \
slouken@7364
   150
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   151
        break;                                                          \
slouken@7364
   152
                                                                        \
slouken@7364
   153
        case 2:                                                         \
slouken@7364
   154
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   155
        break;                                                          \
slouken@7364
   156
                                                                        \
slouken@7364
   157
        case 3: {                                                       \
slouken@7364
   158
            Uint8 *B = (Uint8 *)(buf);                                  \
slouken@7364
   159
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   160
                Pixel = B[0] + (B[1] << 8) + (B[2] << 16);              \
slouken@7364
   161
            } else {                                                    \
slouken@7364
   162
                Pixel = (B[0] << 16) + (B[1] << 8) + B[2];              \
slouken@7364
   163
            }                                                           \
slouken@7364
   164
        }                                                               \
slouken@7364
   165
        break;                                                          \
slouken@7364
   166
                                                                        \
slouken@7364
   167
        case 4:                                                         \
slouken@7364
   168
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   169
        break;                                                          \
slouken@7364
   170
                                                                        \
slouken@7364
   171
        default:                                                        \
slouken@7364
   172
                Pixel = 0; /* stop gcc complaints */                    \
slouken@7364
   173
        break;                                                          \
slouken@7364
   174
    }                                                                   \
slouken@2824
   175
} while (0)
slouken@0
   176
slouken@7364
   177
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)                     \
slouken@7364
   178
do {                                                                    \
slouken@7364
   179
    switch (bpp) {                                                      \
slouken@7364
   180
        case 1:                                                         \
slouken@7364
   181
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   182
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   183
        break;                                                          \
slouken@7364
   184
                                                                        \
slouken@7364
   185
        case 2:                                                         \
slouken@7364
   186
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   187
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   188
        break;                                                          \
slouken@7364
   189
                                                                        \
slouken@7364
   190
        case 3: {                                                       \
slouken@7364
   191
            Pixel = 0;                                                  \
slouken@7364
   192
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   193
                r = *((buf)+fmt->Rshift/8);                             \
slouken@7364
   194
                g = *((buf)+fmt->Gshift/8);                             \
slouken@7364
   195
                b = *((buf)+fmt->Bshift/8);                             \
slouken@7364
   196
            } else {                                                    \
slouken@7364
   197
                r = *((buf)+2-fmt->Rshift/8);                           \
slouken@7364
   198
                g = *((buf)+2-fmt->Gshift/8);                           \
slouken@7364
   199
                b = *((buf)+2-fmt->Bshift/8);                           \
slouken@7364
   200
            }                                                           \
slouken@7364
   201
        }                                                               \
slouken@7364
   202
        break;                                                          \
slouken@7364
   203
                                                                        \
slouken@7364
   204
        case 4:                                                         \
slouken@7364
   205
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   206
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   207
        break;                                                          \
slouken@7364
   208
                                                                        \
slouken@7364
   209
        default:                                                        \
slouken@7364
   210
                /* stop gcc complaints */                               \
slouken@7364
   211
                Pixel = 0;                                              \
slouken@7364
   212
                r = g = b = 0;                                          \
slouken@7364
   213
        break;                                                          \
slouken@7364
   214
    }                                                                   \
slouken@2824
   215
} while (0)
slouken@0
   216
slouken@0
   217
/* Assemble R-G-B values into a specified pixel format and store them */
slouken@7364
   218
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)                             \
slouken@7364
   219
{                                                                       \
slouken@7364
   220
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                             \
slouken@7364
   221
        ((g>>fmt->Gloss)<<fmt->Gshift)|                                 \
slouken@7364
   222
        ((b>>fmt->Bloss)<<fmt->Bshift)|                                 \
slouken@7364
   223
        fmt->Amask;                                                     \
slouken@0
   224
}
slouken@7364
   225
#define RGB565_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   226
{                                                                       \
slouken@7364
   227
    Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);                            \
slouken@0
   228
}
slouken@7364
   229
#define RGB555_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   230
{                                                                       \
slouken@7364
   231
    Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);                            \
slouken@0
   232
}
slouken@7364
   233
#define RGB888_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   234
{                                                                       \
slouken@7364
   235
    Pixel = (r<<16)|(g<<8)|b;                                           \
slouken@0
   236
}
slouken@7364
   237
#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   238
{                                                                       \
slouken@7364
   239
    Pixel = (a<<24)|(r<<16)|(g<<8)|b;                                   \
slouken@2899
   240
}
slouken@7364
   241
#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   242
{                                                                       \
slouken@7364
   243
    Pixel = (r<<24)|(g<<16)|(b<<8)|a;                                   \
slouken@3054
   244
}
slouken@7364
   245
#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   246
{                                                                       \
slouken@7364
   247
    Pixel = (a<<24)|(b<<16)|(g<<8)|r;                                   \
slouken@3054
   248
}
slouken@7364
   249
#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   250
{                                                                       \
slouken@7364
   251
    Pixel = (b<<24)|(g<<16)|(r<<8)|a;                                   \
slouken@3054
   252
}
slouken@7373
   253
#define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a)                        \
slouken@7373
   254
{                                                                       \
slouken@7373
   255
    r = r ? ((r << 2) | 0x3) : 0;                                       \
slouken@7373
   256
    g = g ? ((g << 2) | 0x3) : 0;                                       \
slouken@7373
   257
    b = b ? ((b << 2) | 0x3) : 0;                                       \
slouken@7373
   258
    a = (a * 3) / 255;                                                  \
slouken@7373
   259
    Pixel = (a<<30)|(r<<20)|(g<<10)|b;                                  \
slouken@7373
   260
}
slouken@7364
   261
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b)                            \
slouken@7364
   262
{                                                                       \
slouken@7364
   263
    switch (bpp) {                                                      \
slouken@7364
   264
        case 1: {                                                       \
slouken@7364
   265
            Uint8 Pixel;                                                \
slouken@7364
   266
                                                                        \
slouken@7364
   267
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   268
            *((Uint8 *)(buf)) = Pixel;                                  \
slouken@7364
   269
        }                                                               \
slouken@7364
   270
        break;                                                          \
slouken@7364
   271
                                                                        \
slouken@7364
   272
        case 2: {                                                       \
slouken@7364
   273
            Uint16 Pixel;                                               \
slouken@7364
   274
                                                                        \
slouken@7364
   275
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   276
            *((Uint16 *)(buf)) = Pixel;                                 \
slouken@7364
   277
        }                                                               \
slouken@7364
   278
        break;                                                          \
slouken@7364
   279
                                                                        \
slouken@7364
   280
        case 3: {                                                       \
slouken@7364
   281
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   282
                *((buf)+fmt->Rshift/8) = r;                             \
slouken@7364
   283
                *((buf)+fmt->Gshift/8) = g;                             \
slouken@7364
   284
                *((buf)+fmt->Bshift/8) = b;                             \
slouken@7364
   285
            } else {                                                    \
slouken@7364
   286
                *((buf)+2-fmt->Rshift/8) = r;                           \
slouken@7364
   287
                *((buf)+2-fmt->Gshift/8) = g;                           \
slouken@7364
   288
                *((buf)+2-fmt->Bshift/8) = b;                           \
slouken@7364
   289
            }                                                           \
slouken@7364
   290
        }                                                               \
slouken@7364
   291
        break;                                                          \
slouken@7364
   292
                                                                        \
slouken@7364
   293
        case 4: {                                                       \
slouken@7364
   294
            Uint32 Pixel;                                               \
slouken@7364
   295
                                                                        \
slouken@7364
   296
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   297
            *((Uint32 *)(buf)) = Pixel;                                 \
slouken@7364
   298
        }                                                               \
slouken@7364
   299
        break;                                                          \
slouken@7364
   300
    }                                                                   \
slouken@0
   301
}
slouken@0
   302
slouken@0
   303
/* FIXME: Should we rescale alpha into 0..255 here? */
slouken@7364
   304
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)                         \
slouken@7364
   305
{                                                                       \
slouken@7191
   306
    r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@7191
   307
    g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@7191
   308
    b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@7191
   309
    a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
slouken@0
   310
}
slouken@7364
   311
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)                          \
slouken@7364
   312
{                                                                       \
slouken@7364
   313
    r = (Pixel&fmt->Rmask)>>fmt->Rshift;                                \
slouken@7364
   314
    g = (Pixel&fmt->Gmask)>>fmt->Gshift;                                \
slouken@7364
   315
    b = (Pixel&fmt->Bmask)>>fmt->Bshift;                                \
slouken@7364
   316
    a = (Pixel&fmt->Amask)>>fmt->Ashift;                                \
slouken@0
   317
}
slouken@7364
   318
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)                           \
slouken@7364
   319
{                                                                       \
slouken@7364
   320
    r = (Pixel>>24);                                                    \
slouken@7364
   321
    g = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   322
    b = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   323
    a = (Pixel&0xFF);                                                   \
slouken@0
   324
}
slouken@7364
   325
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)                           \
slouken@7364
   326
{                                                                       \
slouken@7364
   327
    r = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   328
    g = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   329
    b = (Pixel&0xFF);                                                   \
slouken@7364
   330
    a = (Pixel>>24);                                                    \
slouken@0
   331
}
slouken@7364
   332
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)                           \
slouken@7364
   333
{                                                                       \
slouken@7364
   334
    r = (Pixel&0xFF);                                                   \
slouken@7364
   335
    g = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   336
    b = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   337
    a = (Pixel>>24);                                                    \
slouken@0
   338
}
slouken@7364
   339
#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)                           \
slouken@7364
   340
{                                                                       \
slouken@7364
   341
    r = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   342
    g = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   343
    b = (Pixel>>24);                                                    \
slouken@7364
   344
    a = (Pixel&0xFF);                                                   \
slouken@3054
   345
}
slouken@7373
   346
#define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a)                        \
slouken@7373
   347
{                                                                       \
slouken@7373
   348
    r = ((Pixel>>22)&0xFF);                                             \
slouken@7373
   349
    g = ((Pixel>>12)&0xFF);                                             \
slouken@7373
   350
    b = ((Pixel>>2)&0xFF);                                              \
slouken@7373
   351
    a = SDL_expand_byte[6][(Pixel>>30)];                                \
slouken@7373
   352
}
slouken@7364
   353
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)                 \
slouken@7364
   354
do {                                                                    \
slouken@7364
   355
    switch (bpp) {                                                      \
slouken@7364
   356
        case 1:                                                         \
slouken@7364
   357
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   358
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   359
        break;                                                          \
slouken@7364
   360
                                                                        \
slouken@7364
   361
        case 2:                                                         \
slouken@7364
   362
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   363
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   364
        break;                                                          \
slouken@7364
   365
                                                                        \
slouken@7364
   366
        case 3: {                                                       \
slouken@7364
   367
            Pixel = 0;                                                  \
slouken@7364
   368
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   369
                r = *((buf)+fmt->Rshift/8);                             \
slouken@7364
   370
                g = *((buf)+fmt->Gshift/8);                             \
slouken@7364
   371
                b = *((buf)+fmt->Bshift/8);                             \
slouken@7364
   372
            } else {                                                    \
slouken@7364
   373
                r = *((buf)+2-fmt->Rshift/8);                           \
slouken@7364
   374
                g = *((buf)+2-fmt->Gshift/8);                           \
slouken@7364
   375
                b = *((buf)+2-fmt->Bshift/8);                           \
slouken@7364
   376
            }                                                           \
slouken@7364
   377
            a = 0xFF;                                                   \
slouken@7364
   378
        }                                                               \
slouken@7364
   379
        break;                                                          \
slouken@7364
   380
                                                                        \
slouken@7364
   381
        case 4:                                                         \
slouken@7364
   382
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   383
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   384
        break;                                                          \
slouken@7364
   385
                                                                        \
slouken@7364
   386
        default:                                                        \
slouken@7364
   387
            /* stop gcc complaints */                                   \
slouken@7364
   388
            Pixel = 0;                                                  \
slouken@7364
   389
            r = g = b = a = 0;                                          \
slouken@7364
   390
        break;                                                          \
slouken@7364
   391
    }                                                                   \
slouken@2824
   392
} while (0)
slouken@0
   393
slouken@0
   394
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
slouken@7364
   395
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)                         \
slouken@7364
   396
{                                                                       \
slouken@7364
   397
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                             \
slouken@7364
   398
        ((g>>fmt->Gloss)<<fmt->Gshift)|                                 \
slouken@7364
   399
        ((b>>fmt->Bloss)<<fmt->Bshift)|                                 \
slouken@7364
   400
        ((a>>fmt->Aloss)<<fmt->Ashift);                                 \
slouken@0
   401
}
slouken@7364
   402
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)                        \
slouken@7364
   403
{                                                                       \
slouken@7364
   404
    switch (bpp) {                                                      \
slouken@7364
   405
        case 1: {                                                       \
slouken@8922
   406
            Uint8 _pixel;                                               \
slouken@7364
   407
                                                                        \
slouken@8922
   408
            PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a);                   \
slouken@8922
   409
            *((Uint8 *)(buf)) = _pixel;                                 \
slouken@7364
   410
        }                                                               \
slouken@7364
   411
        break;                                                          \
slouken@7364
   412
                                                                        \
slouken@7364
   413
        case 2: {                                                       \
slouken@8922
   414
            Uint16 _pixel;                                              \
slouken@7364
   415
                                                                        \
slouken@8922
   416
            PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a);                   \
slouken@8922
   417
            *((Uint16 *)(buf)) = _pixel;                                \
slouken@7364
   418
        }                                                               \
slouken@7364
   419
        break;                                                          \
slouken@7364
   420
                                                                        \
slouken@7364
   421
        case 3: {                                                       \
slouken@7364
   422
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   423
                *((buf)+fmt->Rshift/8) = r;                             \
slouken@7364
   424
                *((buf)+fmt->Gshift/8) = g;                             \
slouken@7364
   425
                *((buf)+fmt->Bshift/8) = b;                             \
slouken@7364
   426
            } else {                                                    \
slouken@7364
   427
                *((buf)+2-fmt->Rshift/8) = r;                           \
slouken@7364
   428
                *((buf)+2-fmt->Gshift/8) = g;                           \
slouken@7364
   429
                *((buf)+2-fmt->Bshift/8) = b;                           \
slouken@7364
   430
            }                                                           \
slouken@7364
   431
        }                                                               \
slouken@7364
   432
        break;                                                          \
slouken@7364
   433
                                                                        \
slouken@7364
   434
        case 4: {                                                       \
slouken@8922
   435
            Uint32 _pixel;                                              \
slouken@7364
   436
                                                                        \
slouken@8922
   437
            PIXEL_FROM_RGBA(_pixel, fmt, r, g, b, a);                   \
slouken@8922
   438
            *((Uint32 *)(buf)) = _pixel;                                \
slouken@7364
   439
        }                                                               \
slouken@7364
   440
        break;                                                          \
slouken@7364
   441
    }                                                                   \
slouken@0
   442
}
slouken@0
   443
slouken@7502
   444
/* Blend the RGB values of two pixels with an alpha value */
slouken@7502
   445
#define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB)                      \
slouken@7364
   446
do {                                                                    \
slouken@10779
   447
    dR = (Uint8)((((int)(sR-dR)*(int)A)/255)+dR);                       \
slouken@10779
   448
    dG = (Uint8)((((int)(sG-dG)*(int)A)/255)+dG);                       \
slouken@10779
   449
    dB = (Uint8)((((int)(sB-dB)*(int)A)/255)+dB);                       \
slouken@7502
   450
} while(0)
slouken@7502
   451
slouken@7502
   452
slouken@7502
   453
/* Blend the RGBA values of two pixels */
slouken@7502
   454
#define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA)                \
slouken@7502
   455
do {                                                                    \
slouken@10779
   456
    dR = (Uint8)((((int)(sR-dR)*(int)sA)/255)+dR);                      \
slouken@10779
   457
    dG = (Uint8)((((int)(sG-dG)*(int)sA)/255)+dG);                      \
slouken@10779
   458
    dB = (Uint8)((((int)(sB-dB)*(int)sA)/255)+dB);                      \
slouken@10754
   459
    dA = (Uint8)((int)sA+dA-((int)sA*dA)/255);                          \
icculus@1047
   460
} while(0)
icculus@1047
   461
icculus@1047
   462
slouken@0
   463
/* This is a very useful loop for optimizing blitters */
slouken@553
   464
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   465
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   466
#else
slouken@0
   467
#define USE_DUFFS_LOOP
slouken@553
   468
#endif
slouken@0
   469
#ifdef USE_DUFFS_LOOP
slouken@0
   470
slouken@0
   471
/* 8-times unrolled loop */
slouken@7364
   472
#define DUFFS_LOOP8(pixel_copy_increment, width)                        \
slouken@7364
   473
{ int n = (width+7)/8;                                                  \
slouken@7364
   474
    switch (width & 7) {                                                \
slouken@11459
   475
    case 0: do {    pixel_copy_increment; /* fallthrough */             \
slouken@11459
   476
    case 7:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   477
    case 6:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   478
    case 5:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   479
    case 4:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   480
    case 3:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   481
    case 2:     pixel_copy_increment;     /* fallthrough */             \
slouken@11459
   482
    case 1:     pixel_copy_increment;     /* fallthrough */             \
slouken@7364
   483
        } while ( --n > 0 );                                            \
slouken@7364
   484
    }                                                                   \
slouken@0
   485
}
slouken@0
   486
slouken@0
   487
/* 4-times unrolled loop */
slouken@7364
   488
#define DUFFS_LOOP4(pixel_copy_increment, width)                        \
slouken@7364
   489
{ int n = (width+3)/4;                                                  \
slouken@7364
   490
    switch (width & 3) {                                                \
slouken@11492
   491
    case 0: do {    pixel_copy_increment;   /* fallthrough */           \
slouken@11492
   492
    case 3:     pixel_copy_increment;       /* fallthrough */           \
slouken@11492
   493
    case 2:     pixel_copy_increment;       /* fallthrough */           \
slouken@11492
   494
    case 1:     pixel_copy_increment;       /* fallthrough */           \
slouken@7364
   495
        } while (--n > 0);                                              \
slouken@7364
   496
    }                                                                   \
slouken@689
   497
}
slouken@689
   498
slouken@0
   499
/* Use the 8-times version of the loop by default */
slouken@7364
   500
#define DUFFS_LOOP(pixel_copy_increment, width)                         \
slouken@7191
   501
    DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   502
slouken@3035
   503
/* Special version of Duff's device for even more optimization */
slouken@7364
   504
#define DUFFS_LOOP_124(pixel_copy_increment1,                           \
slouken@7364
   505
                       pixel_copy_increment2,                           \
slouken@7364
   506
                       pixel_copy_increment4, width)                    \
slouken@7364
   507
{ int n = width;                                                        \
slouken@7364
   508
    if (n & 1) {                                                        \
slouken@7364
   509
        pixel_copy_increment1; n -= 1;                                  \
slouken@7364
   510
    }                                                                   \
slouken@7364
   511
    if (n & 2) {                                                        \
slouken@7364
   512
        pixel_copy_increment2; n -= 2;                                  \
slouken@7364
   513
    }                                                                   \
slouken@7562
   514
    if (n & 4) {                                                        \
slouken@7562
   515
        pixel_copy_increment4; n -= 4;                                  \
slouken@7562
   516
    }                                                                   \
slouken@7364
   517
    if (n) {                                                            \
slouken@7562
   518
        n /= 8;                                                         \
slouken@7562
   519
        do {                                                            \
slouken@7562
   520
            pixel_copy_increment4;                                      \
slouken@7562
   521
            pixel_copy_increment4;                                      \
slouken@7562
   522
        } while (--n > 0);                                              \
slouken@7364
   523
    }                                                                   \
slouken@689
   524
}
slouken@689
   525
slouken@3035
   526
#else
slouken@689
   527
slouken@689
   528
/* Don't use Duff's device to unroll loops */
slouken@7364
   529
#define DUFFS_LOOP(pixel_copy_increment, width)                         \
slouken@7364
   530
{ int n;                                                                \
slouken@7364
   531
    for ( n=width; n > 0; --n ) {                                       \
slouken@7364
   532
        pixel_copy_increment;                                           \
slouken@7364
   533
    }                                                                   \
slouken@0
   534
}
slouken@7364
   535
#define DUFFS_LOOP8(pixel_copy_increment, width)                        \
slouken@7191
   536
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7364
   537
#define DUFFS_LOOP4(pixel_copy_increment, width)                        \
slouken@7191
   538
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7364
   539
#define DUFFS_LOOP_124(pixel_copy_increment1,                           \
slouken@7364
   540
                       pixel_copy_increment2,                           \
slouken@7364
   541
                       pixel_copy_increment4, width)                    \
slouken@7191
   542
    DUFFS_LOOP(pixel_copy_increment1, width)
slouken@0
   543
slouken@0
   544
#endif /* USE_DUFFS_LOOP */
slouken@0
   545
slouken@0
   546
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   547
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   548
#pragma warning(disable: 4550)
slouken@0
   549
#endif
slouken@0
   550
slouken@10638
   551
#endif /* SDL_blit_h_ */
slouken@2898
   552
slouken@1895
   553
/* vi: set ts=4 sw=4 expandtab: */