src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Oct 2013 21:56:15 -0700
changeset 7860 2b0bcdea3a79
parent 7562 5287c82340e3
child 8093 b43765095a6f
permissions -rw-r--r--
Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings

Andreas Ertelt

The problem in question is caused by changeset 7771 (http://hg.libsdl.org/SDL/rev/4434498bf4b9 / https://bugzilla.libsdl.org/show_bug.cgi?id=2121)

The redefinition of __inline__ (introduced by the addition of begin_code.h:128's "|| __STRICT_ANSI__") results in mingw's gcc throwing multiple

warning: always_inline function might not be inlinable [-Wattributes]

as well as a whole bunch of redefinitions of mingw internals which break linking of projects including the SDL2 headers.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
#ifndef _SDL_blit_h
slouken@0
    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@2267
    73
typedef void (SDLCALL * SDL_BlitFunc) (SDL_BlitInfo * info);
slouken@2262
    74
slouken@2267
    75
typedef struct
slouken@2267
    76
{
slouken@2262
    77
    Uint32 src_format;
slouken@2262
    78
    Uint32 dst_format;
slouken@2262
    79
    int flags;
slouken@2262
    80
    int cpu;
slouken@2262
    81
    SDL_BlitFunc func;
slouken@2262
    82
} SDL_BlitFuncEntry;
slouken@0
    83
slouken@0
    84
/* Blit mapping definition */
slouken@1895
    85
typedef struct SDL_BlitMap
slouken@1895
    86
{
slouken@1895
    87
    SDL_Surface *dst;
slouken@1895
    88
    int identity;
slouken@2257
    89
    SDL_blit blit;
slouken@2257
    90
    void *data;
slouken@2262
    91
    SDL_BlitInfo info;
slouken@0
    92
slouken@1895
    93
    /* the version count matches the destination; mismatch indicates
slouken@1895
    94
       an invalid mapping */
slouken@6166
    95
    Uint32 dst_palette_version;
slouken@6166
    96
    Uint32 src_palette_version;
slouken@0
    97
} SDL_BlitMap;
slouken@0
    98
slouken@0
    99
/* Functions found in SDL_blit.c */
slouken@1895
   100
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   101
slouken@2267
   102
/* Functions found in SDL_blit_*.c */
slouken@2267
   103
extern SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface * surface);
slouken@2267
   104
extern SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface * surface);
slouken@2267
   105
extern SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface * surface);
slouken@2267
   106
extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
slouken@2263
   107
slouken@0
   108
/*
slouken@0
   109
 * Useful macros for blitting routines
slouken@0
   110
 */
slouken@0
   111
slouken@2249
   112
#if defined(__GNUC__)
slouken@2249
   113
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   114
#elif defined(_MSC_VER)
slouken@2251
   115
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   116
#else
slouken@2249
   117
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   118
#endif
slouken@2249
   119
slouken@0
   120
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@7364
   121
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)                             \
slouken@7364
   122
{                                                                       \
slouken@7191
   123
    r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@7191
   124
    g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@7191
   125
    b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@0
   126
}
slouken@7364
   127
#define RGB_FROM_RGB565(Pixel, r, g, b)                                 \
slouken@7364
   128
    {                                                                   \
slouken@7364
   129
    r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];                       \
slouken@7364
   130
    g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)];                        \
slouken@7364
   131
    b = SDL_expand_byte[3][(Pixel&0x001F)];                             \
slouken@0
   132
}
slouken@7364
   133
#define RGB_FROM_RGB555(Pixel, r, g, b)                                 \
slouken@7364
   134
{                                                                       \
slouken@7364
   135
    r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];                       \
slouken@7364
   136
    g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)];                        \
slouken@7364
   137
    b = SDL_expand_byte[3][(Pixel&0x001F)];                             \
slouken@0
   138
}
slouken@7364
   139
#define RGB_FROM_RGB888(Pixel, r, g, b)                                 \
slouken@7364
   140
{                                                                       \
slouken@7364
   141
    r = ((Pixel&0xFF0000)>>16);                                         \
slouken@7364
   142
    g = ((Pixel&0xFF00)>>8);                                            \
slouken@7364
   143
    b = (Pixel&0xFF);                                                   \
slouken@0
   144
}
slouken@7364
   145
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)                             \
slouken@7364
   146
do {                                                                    \
slouken@7364
   147
    switch (bpp) {                                                      \
slouken@7364
   148
        case 1:                                                         \
slouken@7364
   149
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   150
        break;                                                          \
slouken@7364
   151
                                                                        \
slouken@7364
   152
        case 2:                                                         \
slouken@7364
   153
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   154
        break;                                                          \
slouken@7364
   155
                                                                        \
slouken@7364
   156
        case 3: {                                                       \
slouken@7364
   157
            Uint8 *B = (Uint8 *)(buf);                                  \
slouken@7364
   158
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   159
                Pixel = B[0] + (B[1] << 8) + (B[2] << 16);              \
slouken@7364
   160
            } else {                                                    \
slouken@7364
   161
                Pixel = (B[0] << 16) + (B[1] << 8) + B[2];              \
slouken@7364
   162
            }                                                           \
slouken@7364
   163
        }                                                               \
slouken@7364
   164
        break;                                                          \
slouken@7364
   165
                                                                        \
slouken@7364
   166
        case 4:                                                         \
slouken@7364
   167
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   168
        break;                                                          \
slouken@7364
   169
                                                                        \
slouken@7364
   170
        default:                                                        \
slouken@7364
   171
                Pixel = 0; /* stop gcc complaints */                    \
slouken@7364
   172
        break;                                                          \
slouken@7364
   173
    }                                                                   \
slouken@2824
   174
} while (0)
slouken@0
   175
slouken@7364
   176
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)                     \
slouken@7364
   177
do {                                                                    \
slouken@7364
   178
    switch (bpp) {                                                      \
slouken@7364
   179
        case 1:                                                         \
slouken@7364
   180
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   181
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   182
        break;                                                          \
slouken@7364
   183
                                                                        \
slouken@7364
   184
        case 2:                                                         \
slouken@7364
   185
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   186
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   187
        break;                                                          \
slouken@7364
   188
                                                                        \
slouken@7364
   189
        case 3: {                                                       \
slouken@7364
   190
            Pixel = 0;                                                  \
slouken@7364
   191
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   192
                r = *((buf)+fmt->Rshift/8);                             \
slouken@7364
   193
                g = *((buf)+fmt->Gshift/8);                             \
slouken@7364
   194
                b = *((buf)+fmt->Bshift/8);                             \
slouken@7364
   195
            } else {                                                    \
slouken@7364
   196
                r = *((buf)+2-fmt->Rshift/8);                           \
slouken@7364
   197
                g = *((buf)+2-fmt->Gshift/8);                           \
slouken@7364
   198
                b = *((buf)+2-fmt->Bshift/8);                           \
slouken@7364
   199
            }                                                           \
slouken@7364
   200
        }                                                               \
slouken@7364
   201
        break;                                                          \
slouken@7364
   202
                                                                        \
slouken@7364
   203
        case 4:                                                         \
slouken@7364
   204
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   205
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);                        \
slouken@7364
   206
        break;                                                          \
slouken@7364
   207
                                                                        \
slouken@7364
   208
        default:                                                        \
slouken@7364
   209
                /* stop gcc complaints */                               \
slouken@7364
   210
                Pixel = 0;                                              \
slouken@7364
   211
                r = g = b = 0;                                          \
slouken@7364
   212
        break;                                                          \
slouken@7364
   213
    }                                                                   \
slouken@2824
   214
} while (0)
slouken@0
   215
slouken@0
   216
/* Assemble R-G-B values into a specified pixel format and store them */
slouken@7364
   217
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)                             \
slouken@7364
   218
{                                                                       \
slouken@7364
   219
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                             \
slouken@7364
   220
        ((g>>fmt->Gloss)<<fmt->Gshift)|                                 \
slouken@7364
   221
        ((b>>fmt->Bloss)<<fmt->Bshift)|                                 \
slouken@7364
   222
        fmt->Amask;                                                     \
slouken@0
   223
}
slouken@7364
   224
#define RGB565_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   225
{                                                                       \
slouken@7364
   226
    Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);                            \
slouken@0
   227
}
slouken@7364
   228
#define RGB555_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   229
{                                                                       \
slouken@7364
   230
    Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);                            \
slouken@0
   231
}
slouken@7364
   232
#define RGB888_FROM_RGB(Pixel, r, g, b)                                 \
slouken@7364
   233
{                                                                       \
slouken@7364
   234
    Pixel = (r<<16)|(g<<8)|b;                                           \
slouken@0
   235
}
slouken@7364
   236
#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   237
{                                                                       \
slouken@7364
   238
    Pixel = (a<<24)|(r<<16)|(g<<8)|b;                                   \
slouken@2899
   239
}
slouken@7364
   240
#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   241
{                                                                       \
slouken@7364
   242
    Pixel = (r<<24)|(g<<16)|(b<<8)|a;                                   \
slouken@3054
   243
}
slouken@7364
   244
#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   245
{                                                                       \
slouken@7364
   246
    Pixel = (a<<24)|(b<<16)|(g<<8)|r;                                   \
slouken@3054
   247
}
slouken@7364
   248
#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)                           \
slouken@7364
   249
{                                                                       \
slouken@7364
   250
    Pixel = (b<<24)|(g<<16)|(r<<8)|a;                                   \
slouken@3054
   251
}
slouken@7373
   252
#define ARGB2101010_FROM_RGBA(Pixel, r, g, b, a)                        \
slouken@7373
   253
{                                                                       \
slouken@7373
   254
    r = r ? ((r << 2) | 0x3) : 0;                                       \
slouken@7373
   255
    g = g ? ((g << 2) | 0x3) : 0;                                       \
slouken@7373
   256
    b = b ? ((b << 2) | 0x3) : 0;                                       \
slouken@7373
   257
    a = (a * 3) / 255;                                                  \
slouken@7373
   258
    Pixel = (a<<30)|(r<<20)|(g<<10)|b;                                  \
slouken@7373
   259
}
slouken@7364
   260
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b)                            \
slouken@7364
   261
{                                                                       \
slouken@7364
   262
    switch (bpp) {                                                      \
slouken@7364
   263
        case 1: {                                                       \
slouken@7364
   264
            Uint8 Pixel;                                                \
slouken@7364
   265
                                                                        \
slouken@7364
   266
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   267
            *((Uint8 *)(buf)) = Pixel;                                  \
slouken@7364
   268
        }                                                               \
slouken@7364
   269
        break;                                                          \
slouken@7364
   270
                                                                        \
slouken@7364
   271
        case 2: {                                                       \
slouken@7364
   272
            Uint16 Pixel;                                               \
slouken@7364
   273
                                                                        \
slouken@7364
   274
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   275
            *((Uint16 *)(buf)) = Pixel;                                 \
slouken@7364
   276
        }                                                               \
slouken@7364
   277
        break;                                                          \
slouken@7364
   278
                                                                        \
slouken@7364
   279
        case 3: {                                                       \
slouken@7364
   280
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   281
                *((buf)+fmt->Rshift/8) = r;                             \
slouken@7364
   282
                *((buf)+fmt->Gshift/8) = g;                             \
slouken@7364
   283
                *((buf)+fmt->Bshift/8) = b;                             \
slouken@7364
   284
            } else {                                                    \
slouken@7364
   285
                *((buf)+2-fmt->Rshift/8) = r;                           \
slouken@7364
   286
                *((buf)+2-fmt->Gshift/8) = g;                           \
slouken@7364
   287
                *((buf)+2-fmt->Bshift/8) = b;                           \
slouken@7364
   288
            }                                                           \
slouken@7364
   289
        }                                                               \
slouken@7364
   290
        break;                                                          \
slouken@7364
   291
                                                                        \
slouken@7364
   292
        case 4: {                                                       \
slouken@7364
   293
            Uint32 Pixel;                                               \
slouken@7364
   294
                                                                        \
slouken@7364
   295
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);                        \
slouken@7364
   296
            *((Uint32 *)(buf)) = Pixel;                                 \
slouken@7364
   297
        }                                                               \
slouken@7364
   298
        break;                                                          \
slouken@7364
   299
    }                                                                   \
slouken@0
   300
}
slouken@0
   301
slouken@0
   302
/* FIXME: Should we rescale alpha into 0..255 here? */
slouken@7364
   303
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)                         \
slouken@7364
   304
{                                                                       \
slouken@7191
   305
    r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@7191
   306
    g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@7191
   307
    b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@7191
   308
    a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
slouken@0
   309
}
slouken@7364
   310
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)                          \
slouken@7364
   311
{                                                                       \
slouken@7364
   312
    r = (Pixel&fmt->Rmask)>>fmt->Rshift;                                \
slouken@7364
   313
    g = (Pixel&fmt->Gmask)>>fmt->Gshift;                                \
slouken@7364
   314
    b = (Pixel&fmt->Bmask)>>fmt->Bshift;                                \
slouken@7364
   315
    a = (Pixel&fmt->Amask)>>fmt->Ashift;                                \
slouken@0
   316
}
slouken@7364
   317
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)                           \
slouken@7364
   318
{                                                                       \
slouken@7364
   319
    r = (Pixel>>24);                                                    \
slouken@7364
   320
    g = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   321
    b = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   322
    a = (Pixel&0xFF);                                                   \
slouken@0
   323
}
slouken@7364
   324
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)                           \
slouken@7364
   325
{                                                                       \
slouken@7364
   326
    r = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   327
    g = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   328
    b = (Pixel&0xFF);                                                   \
slouken@7364
   329
    a = (Pixel>>24);                                                    \
slouken@0
   330
}
slouken@7364
   331
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)                           \
slouken@7364
   332
{                                                                       \
slouken@7364
   333
    r = (Pixel&0xFF);                                                   \
slouken@7364
   334
    g = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   335
    b = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   336
    a = (Pixel>>24);                                                    \
slouken@0
   337
}
slouken@7364
   338
#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)                           \
slouken@7364
   339
{                                                                       \
slouken@7364
   340
    r = ((Pixel>>8)&0xFF);                                              \
slouken@7364
   341
    g = ((Pixel>>16)&0xFF);                                             \
slouken@7364
   342
    b = (Pixel>>24);                                                    \
slouken@7364
   343
    a = (Pixel&0xFF);                                                   \
slouken@3054
   344
}
slouken@7373
   345
#define RGBA_FROM_ARGB2101010(Pixel, r, g, b, a)                        \
slouken@7373
   346
{                                                                       \
slouken@7373
   347
    r = ((Pixel>>22)&0xFF);                                             \
slouken@7373
   348
    g = ((Pixel>>12)&0xFF);                                             \
slouken@7373
   349
    b = ((Pixel>>2)&0xFF);                                              \
slouken@7373
   350
    a = SDL_expand_byte[6][(Pixel>>30)];                                \
slouken@7373
   351
}
slouken@7364
   352
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)                 \
slouken@7364
   353
do {                                                                    \
slouken@7364
   354
    switch (bpp) {                                                      \
slouken@7364
   355
        case 1:                                                         \
slouken@7364
   356
            Pixel = *((Uint8 *)(buf));                                  \
slouken@7364
   357
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   358
        break;                                                          \
slouken@7364
   359
                                                                        \
slouken@7364
   360
        case 2:                                                         \
slouken@7364
   361
            Pixel = *((Uint16 *)(buf));                                 \
slouken@7364
   362
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   363
        break;                                                          \
slouken@7364
   364
                                                                        \
slouken@7364
   365
        case 3: {                                                       \
slouken@7364
   366
            Pixel = 0;                                                  \
slouken@7364
   367
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   368
                r = *((buf)+fmt->Rshift/8);                             \
slouken@7364
   369
                g = *((buf)+fmt->Gshift/8);                             \
slouken@7364
   370
                b = *((buf)+fmt->Bshift/8);                             \
slouken@7364
   371
            } else {                                                    \
slouken@7364
   372
                r = *((buf)+2-fmt->Rshift/8);                           \
slouken@7364
   373
                g = *((buf)+2-fmt->Gshift/8);                           \
slouken@7364
   374
                b = *((buf)+2-fmt->Bshift/8);                           \
slouken@7364
   375
            }                                                           \
slouken@7364
   376
            a = 0xFF;                                                   \
slouken@7364
   377
        }                                                               \
slouken@7364
   378
        break;                                                          \
slouken@7364
   379
                                                                        \
slouken@7364
   380
        case 4:                                                         \
slouken@7364
   381
            Pixel = *((Uint32 *)(buf));                                 \
slouken@7364
   382
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   383
        break;                                                          \
slouken@7364
   384
                                                                        \
slouken@7364
   385
        default:                                                        \
slouken@7364
   386
            /* stop gcc complaints */                                   \
slouken@7364
   387
            Pixel = 0;                                                  \
slouken@7364
   388
            r = g = b = a = 0;                                          \
slouken@7364
   389
        break;                                                          \
slouken@7364
   390
    }                                                                   \
slouken@2824
   391
} while (0)
slouken@0
   392
slouken@0
   393
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
slouken@7364
   394
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)                         \
slouken@7364
   395
{                                                                       \
slouken@7364
   396
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                             \
slouken@7364
   397
        ((g>>fmt->Gloss)<<fmt->Gshift)|                                 \
slouken@7364
   398
        ((b>>fmt->Bloss)<<fmt->Bshift)|                                 \
slouken@7364
   399
        ((a>>fmt->Aloss)<<fmt->Ashift);                                 \
slouken@0
   400
}
slouken@7364
   401
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)                        \
slouken@7364
   402
{                                                                       \
slouken@7364
   403
    switch (bpp) {                                                      \
slouken@7364
   404
        case 1: {                                                       \
slouken@7364
   405
            Uint8 Pixel;                                                \
slouken@7364
   406
                                                                        \
slouken@7364
   407
            PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   408
            *((Uint8 *)(buf)) = Pixel;                                  \
slouken@7364
   409
        }                                                               \
slouken@7364
   410
        break;                                                          \
slouken@7364
   411
                                                                        \
slouken@7364
   412
        case 2: {                                                       \
slouken@7364
   413
            Uint16 Pixel;                                               \
slouken@7364
   414
                                                                        \
slouken@7364
   415
            PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   416
            *((Uint16 *)(buf)) = Pixel;                                 \
slouken@7364
   417
        }                                                               \
slouken@7364
   418
        break;                                                          \
slouken@7364
   419
                                                                        \
slouken@7364
   420
        case 3: {                                                       \
slouken@7364
   421
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {                      \
slouken@7364
   422
                *((buf)+fmt->Rshift/8) = r;                             \
slouken@7364
   423
                *((buf)+fmt->Gshift/8) = g;                             \
slouken@7364
   424
                *((buf)+fmt->Bshift/8) = b;                             \
slouken@7364
   425
            } else {                                                    \
slouken@7364
   426
                *((buf)+2-fmt->Rshift/8) = r;                           \
slouken@7364
   427
                *((buf)+2-fmt->Gshift/8) = g;                           \
slouken@7364
   428
                *((buf)+2-fmt->Bshift/8) = b;                           \
slouken@7364
   429
            }                                                           \
slouken@7364
   430
        }                                                               \
slouken@7364
   431
        break;                                                          \
slouken@7364
   432
                                                                        \
slouken@7364
   433
        case 4: {                                                       \
slouken@7364
   434
            Uint32 Pixel;                                               \
slouken@7364
   435
                                                                        \
slouken@7364
   436
            PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);                    \
slouken@7364
   437
            *((Uint32 *)(buf)) = Pixel;                                 \
slouken@7364
   438
        }                                                               \
slouken@7364
   439
        break;                                                          \
slouken@7364
   440
    }                                                                   \
slouken@0
   441
}
slouken@0
   442
slouken@7502
   443
/* Blend the RGB values of two pixels with an alpha value */
slouken@7502
   444
#define ALPHA_BLEND_RGB(sR, sG, sB, A, dR, dG, dB)                      \
slouken@7364
   445
do {                                                                    \
slouken@7502
   446
    dR = ((((unsigned)(sR-dR)*(unsigned)A)/255)+dR);                    \
slouken@7502
   447
    dG = ((((unsigned)(sG-dG)*(unsigned)A)/255)+dG);                    \
slouken@7502
   448
    dB = ((((unsigned)(sB-dB)*(unsigned)A)/255)+dB);                    \
slouken@7502
   449
} while(0)
slouken@7502
   450
slouken@7502
   451
slouken@7502
   452
/* Blend the RGBA values of two pixels */
slouken@7502
   453
#define ALPHA_BLEND_RGBA(sR, sG, sB, sA, dR, dG, dB, dA)                \
slouken@7502
   454
do {                                                                    \
slouken@7502
   455
    dR = ((((unsigned)(sR-dR)*(unsigned)sA)/255)+dR);                   \
slouken@7502
   456
    dG = ((((unsigned)(sG-dG)*(unsigned)sA)/255)+dG);                   \
slouken@7502
   457
    dB = ((((unsigned)(sB-dB)*(unsigned)sA)/255)+dB);                   \
slouken@7502
   458
    dA = ((unsigned)sA+(unsigned)dA-((unsigned)sA*dA)/255);             \
icculus@1047
   459
} while(0)
icculus@1047
   460
icculus@1047
   461
slouken@0
   462
/* This is a very useful loop for optimizing blitters */
slouken@553
   463
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   464
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   465
#else
slouken@0
   466
#define USE_DUFFS_LOOP
slouken@553
   467
#endif
slouken@0
   468
#ifdef USE_DUFFS_LOOP
slouken@0
   469
slouken@0
   470
/* 8-times unrolled loop */
slouken@7364
   471
#define DUFFS_LOOP8(pixel_copy_increment, width)                        \
slouken@7364
   472
{ int n = (width+7)/8;                                                  \
slouken@7364
   473
    switch (width & 7) {                                                \
slouken@7364
   474
    case 0: do {    pixel_copy_increment;                               \
slouken@7364
   475
    case 7:     pixel_copy_increment;                                   \
slouken@7364
   476
    case 6:     pixel_copy_increment;                                   \
slouken@7364
   477
    case 5:     pixel_copy_increment;                                   \
slouken@7364
   478
    case 4:     pixel_copy_increment;                                   \
slouken@7364
   479
    case 3:     pixel_copy_increment;                                   \
slouken@7364
   480
    case 2:     pixel_copy_increment;                                   \
slouken@7364
   481
    case 1:     pixel_copy_increment;                                   \
slouken@7364
   482
        } while ( --n > 0 );                                            \
slouken@7364
   483
    }                                                                   \
slouken@0
   484
}
slouken@0
   485
slouken@0
   486
/* 4-times unrolled loop */
slouken@7364
   487
#define DUFFS_LOOP4(pixel_copy_increment, width)                        \
slouken@7364
   488
{ int n = (width+3)/4;                                                  \
slouken@7364
   489
    switch (width & 3) {                                                \
slouken@7364
   490
    case 0: do {    pixel_copy_increment;                               \
slouken@7364
   491
    case 3:     pixel_copy_increment;                                   \
slouken@7364
   492
    case 2:     pixel_copy_increment;                                   \
slouken@7364
   493
    case 1:     pixel_copy_increment;                                   \
slouken@7364
   494
        } while (--n > 0);                                              \
slouken@7364
   495
    }                                                                   \
slouken@689
   496
}
slouken@689
   497
slouken@0
   498
/* Use the 8-times version of the loop by default */
slouken@7364
   499
#define DUFFS_LOOP(pixel_copy_increment, width)                         \
slouken@7191
   500
    DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   501
slouken@3035
   502
/* Special version of Duff's device for even more optimization */
slouken@7364
   503
#define DUFFS_LOOP_124(pixel_copy_increment1,                           \
slouken@7364
   504
                       pixel_copy_increment2,                           \
slouken@7364
   505
                       pixel_copy_increment4, width)                    \
slouken@7364
   506
{ int n = width;                                                        \
slouken@7364
   507
    if (n & 1) {                                                        \
slouken@7364
   508
        pixel_copy_increment1; n -= 1;                                  \
slouken@7364
   509
    }                                                                   \
slouken@7364
   510
    if (n & 2) {                                                        \
slouken@7364
   511
        pixel_copy_increment2; n -= 2;                                  \
slouken@7364
   512
    }                                                                   \
slouken@7562
   513
    if (n & 4) {                                                        \
slouken@7562
   514
        pixel_copy_increment4; n -= 4;                                  \
slouken@7562
   515
    }                                                                   \
slouken@7364
   516
    if (n) {                                                            \
slouken@7562
   517
        n /= 8;                                                         \
slouken@7562
   518
        do {                                                            \
slouken@7562
   519
            pixel_copy_increment4;                                      \
slouken@7562
   520
            pixel_copy_increment4;                                      \
slouken@7562
   521
        } while (--n > 0);                                              \
slouken@7364
   522
    }                                                                   \
slouken@689
   523
}
slouken@689
   524
slouken@3035
   525
#else
slouken@689
   526
slouken@689
   527
/* Don't use Duff's device to unroll loops */
slouken@7364
   528
#define DUFFS_LOOP(pixel_copy_increment, width)                         \
slouken@7364
   529
{ int n;                                                                \
slouken@7364
   530
    for ( n=width; n > 0; --n ) {                                       \
slouken@7364
   531
        pixel_copy_increment;                                           \
slouken@7364
   532
    }                                                                   \
slouken@0
   533
}
slouken@7364
   534
#define DUFFS_LOOP8(pixel_copy_increment, width)                        \
slouken@7191
   535
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7364
   536
#define DUFFS_LOOP4(pixel_copy_increment, width)                        \
slouken@7191
   537
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7364
   538
#define DUFFS_LOOP_124(pixel_copy_increment1,                           \
slouken@7364
   539
                       pixel_copy_increment2,                           \
slouken@7364
   540
                       pixel_copy_increment4, width)                    \
slouken@7191
   541
    DUFFS_LOOP(pixel_copy_increment1, width)
slouken@0
   542
slouken@0
   543
#endif /* USE_DUFFS_LOOP */
slouken@0
   544
slouken@0
   545
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   546
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   547
#pragma warning(disable: 4550)
slouken@0
   548
#endif
slouken@0
   549
slouken@0
   550
#endif /* _SDL_blit_h */
slouken@2898
   551
slouken@1895
   552
/* vi: set ts=4 sw=4 expandtab: */