src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 14:17:52 -0700
changeset 7191 75360622e65f
parent 6885 700f1b25f77f
child 7364 e21bd6987b86
permissions -rw-r--r--
File style cleanup for the SDL 2.0 release
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@7191
   121
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)             \
slouken@7191
   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@7191
   127
#define RGB_FROM_RGB565(Pixel, r, g, b)                 \
slouken@7191
   128
{                                   \
slouken@7191
   129
    r = SDL_expand_byte[3][((Pixel&0xF800)>>11)];                   \
slouken@7191
   130
    g = SDL_expand_byte[2][((Pixel&0x07E0)>>5)];                    \
slouken@7191
   131
    b = SDL_expand_byte[3][(Pixel&0x001F)];                     \
slouken@0
   132
}
slouken@7191
   133
#define RGB_FROM_RGB555(Pixel, r, g, b)                 \
slouken@7191
   134
{                                   \
slouken@7191
   135
    r = SDL_expand_byte[3][((Pixel&0x7C00)>>10)];                   \
slouken@7191
   136
    g = SDL_expand_byte[3][((Pixel&0x03E0)>>5)];                    \
slouken@7191
   137
    b = SDL_expand_byte[3][(Pixel&0x001F)];                     \
slouken@0
   138
}
slouken@7191
   139
#define RGB_FROM_RGB888(Pixel, r, g, b)                 \
slouken@7191
   140
{                                   \
slouken@7191
   141
    r = ((Pixel&0xFF0000)>>16);                 \
slouken@7191
   142
    g = ((Pixel&0xFF00)>>8);                    \
slouken@7191
   143
    b = (Pixel&0xFF);                       \
slouken@0
   144
}
slouken@7191
   145
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)                \
slouken@7191
   146
do {                                       \
slouken@7191
   147
    switch (bpp) {                             \
slouken@7191
   148
        case 2:                            \
slouken@7191
   149
            Pixel = *((Uint16 *)(buf));            \
slouken@7191
   150
        break;                             \
slouken@7191
   151
                                       \
slouken@7191
   152
        case 3: {                          \
slouken@7191
   153
                Uint8 *B = (Uint8 *)(buf);             \
slouken@7191
   154
            if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {         \
slouken@7191
   155
                    Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@7191
   156
            } else {                       \
slouken@7191
   157
                    Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@7191
   158
            }                          \
slouken@7191
   159
        }                              \
slouken@7191
   160
        break;                             \
slouken@7191
   161
                                       \
slouken@7191
   162
        case 4:                            \
slouken@7191
   163
            Pixel = *((Uint32 *)(buf));            \
slouken@7191
   164
        break;                             \
slouken@7191
   165
                                       \
slouken@7191
   166
        default:                           \
slouken@7191
   167
                Pixel = 0; /* stop gcc complaints */           \
slouken@7191
   168
        break;                             \
slouken@7191
   169
    }                                  \
slouken@2824
   170
} while (0)
slouken@0
   171
slouken@7191
   172
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)            \
slouken@7191
   173
do {                                       \
slouken@7191
   174
    switch (bpp) {                             \
slouken@7191
   175
        case 2:                            \
slouken@7191
   176
            Pixel = *((Uint16 *)(buf));            \
slouken@7191
   177
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);           \
slouken@7191
   178
        break;                             \
slouken@7191
   179
                                       \
slouken@7191
   180
        case 3: {                          \
slouken@5426
   181
            Pixel = 0;                  \
slouken@7191
   182
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {         \
slouken@7191
   183
                    r = *((buf)+fmt->Rshift/8);        \
slouken@7191
   184
                g = *((buf)+fmt->Gshift/8);        \
slouken@7191
   185
                b = *((buf)+fmt->Bshift/8);        \
slouken@7191
   186
            } else {                       \
slouken@7191
   187
                    r = *((buf)+2-fmt->Rshift/8);          \
slouken@7191
   188
                g = *((buf)+2-fmt->Gshift/8);          \
slouken@7191
   189
                b = *((buf)+2-fmt->Bshift/8);          \
slouken@7191
   190
            }                          \
slouken@7191
   191
        }                              \
slouken@7191
   192
        break;                             \
slouken@7191
   193
                                       \
slouken@7191
   194
        case 4:                            \
slouken@7191
   195
            Pixel = *((Uint32 *)(buf));            \
slouken@7191
   196
            RGB_FROM_PIXEL(Pixel, fmt, r, g, b);           \
slouken@7191
   197
        break;                             \
slouken@7191
   198
                                       \
slouken@7191
   199
        default:                           \
slouken@7191
   200
                /* stop gcc complaints */          \
slouken@7191
   201
                Pixel = 0;              \
slouken@5426
   202
                r = g = b = 0;          \
slouken@7191
   203
        break;                             \
slouken@7191
   204
    }                                  \
slouken@2824
   205
} while (0)
slouken@0
   206
slouken@0
   207
/* Assemble R-G-B values into a specified pixel format and store them */
slouken@7191
   208
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)             \
slouken@7191
   209
{                                   \
slouken@7191
   210
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|             \
slouken@7191
   211
        ((g>>fmt->Gloss)<<fmt->Gshift)|             \
slouken@7191
   212
        ((b>>fmt->Bloss)<<fmt->Bshift)| \
slouken@7191
   213
        fmt->Amask;             \
slouken@0
   214
}
slouken@7191
   215
#define RGB565_FROM_RGB(Pixel, r, g, b)                 \
slouken@7191
   216
{                                   \
slouken@7191
   217
    Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);            \
slouken@0
   218
}
slouken@7191
   219
#define RGB555_FROM_RGB(Pixel, r, g, b)                 \
slouken@7191
   220
{                                   \
slouken@7191
   221
    Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);            \
slouken@0
   222
}
slouken@7191
   223
#define RGB888_FROM_RGB(Pixel, r, g, b)                 \
slouken@7191
   224
{                                   \
slouken@7191
   225
    Pixel = (r<<16)|(g<<8)|b;                   \
slouken@0
   226
}
slouken@7191
   227
#define ARGB8888_FROM_RGBA(Pixel, r, g, b, a)               \
slouken@7191
   228
{                                   \
slouken@7191
   229
    Pixel = (a<<24)|(r<<16)|(g<<8)|b;               \
slouken@2899
   230
}
slouken@7191
   231
#define RGBA8888_FROM_RGBA(Pixel, r, g, b, a)               \
slouken@7191
   232
{                                   \
slouken@7191
   233
    Pixel = (r<<24)|(g<<16)|(b<<8)|a;               \
slouken@3054
   234
}
slouken@7191
   235
#define ABGR8888_FROM_RGBA(Pixel, r, g, b, a)               \
slouken@7191
   236
{                                   \
slouken@7191
   237
    Pixel = (a<<24)|(b<<16)|(g<<8)|r;               \
slouken@3054
   238
}
slouken@7191
   239
#define BGRA8888_FROM_RGBA(Pixel, r, g, b, a)               \
slouken@7191
   240
{                                   \
slouken@7191
   241
    Pixel = (b<<24)|(g<<16)|(r<<8)|a;               \
slouken@3054
   242
}
slouken@7191
   243
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b)                \
slouken@7191
   244
{                                   \
slouken@7191
   245
    switch (bpp) {                          \
slouken@7191
   246
        case 2: {                       \
slouken@7191
   247
            Uint16 Pixel;                   \
slouken@7191
   248
                                    \
slouken@7191
   249
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);        \
slouken@7191
   250
            *((Uint16 *)(buf)) = Pixel;     \
slouken@7191
   251
        }                           \
slouken@7191
   252
        break;                          \
slouken@7191
   253
                                    \
slouken@7191
   254
        case 3: {                       \
slouken@7191
   255
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {      \
slouken@7191
   256
                    *((buf)+fmt->Rshift/8) = r;     \
slouken@7191
   257
                *((buf)+fmt->Gshift/8) = g;     \
slouken@7191
   258
                *((buf)+fmt->Bshift/8) = b;     \
slouken@7191
   259
            } else {                    \
slouken@7191
   260
                    *((buf)+2-fmt->Rshift/8) = r;       \
slouken@7191
   261
                *((buf)+2-fmt->Gshift/8) = g;       \
slouken@7191
   262
                *((buf)+2-fmt->Bshift/8) = b;       \
slouken@7191
   263
            }                       \
slouken@7191
   264
        }                           \
slouken@7191
   265
        break;                          \
slouken@7191
   266
                                    \
slouken@7191
   267
        case 4: {                       \
slouken@7191
   268
            Uint32 Pixel;                   \
slouken@7191
   269
                                    \
slouken@7191
   270
            PIXEL_FROM_RGB(Pixel, fmt, r, g, b);        \
slouken@7191
   271
            *((Uint32 *)(buf)) = Pixel;         \
slouken@7191
   272
        }                           \
slouken@7191
   273
        break;                          \
slouken@7191
   274
    }                               \
slouken@0
   275
}
slouken@0
   276
slouken@0
   277
/* FIXME: Should we rescale alpha into 0..255 here? */
slouken@7191
   278
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)             \
slouken@7191
   279
{                                   \
slouken@7191
   280
    r = SDL_expand_byte[fmt->Rloss][((Pixel&fmt->Rmask)>>fmt->Rshift)]; \
slouken@7191
   281
    g = SDL_expand_byte[fmt->Gloss][((Pixel&fmt->Gmask)>>fmt->Gshift)]; \
slouken@7191
   282
    b = SDL_expand_byte[fmt->Bloss][((Pixel&fmt->Bmask)>>fmt->Bshift)]; \
slouken@7191
   283
    a = SDL_expand_byte[fmt->Aloss][((Pixel&fmt->Amask)>>fmt->Ashift)]; \
slouken@0
   284
}
slouken@7191
   285
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)  \
slouken@7191
   286
{                       \
slouken@7191
   287
    r = (Pixel&fmt->Rmask)>>fmt->Rshift;    \
slouken@7191
   288
    g = (Pixel&fmt->Gmask)>>fmt->Gshift;    \
slouken@7191
   289
    b = (Pixel&fmt->Bmask)>>fmt->Bshift;    \
slouken@7191
   290
    a = (Pixel&fmt->Amask)>>fmt->Ashift;    \
slouken@0
   291
}
slouken@7191
   292
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)               \
slouken@7191
   293
{                                   \
slouken@7191
   294
    r = (Pixel>>24);                        \
slouken@7191
   295
    g = ((Pixel>>16)&0xFF);                     \
slouken@7191
   296
    b = ((Pixel>>8)&0xFF);                      \
slouken@7191
   297
    a = (Pixel&0xFF);                       \
slouken@0
   298
}
slouken@7191
   299
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)               \
slouken@7191
   300
{                                   \
slouken@7191
   301
    r = ((Pixel>>16)&0xFF);                     \
slouken@7191
   302
    g = ((Pixel>>8)&0xFF);                      \
slouken@7191
   303
    b = (Pixel&0xFF);                       \
slouken@7191
   304
    a = (Pixel>>24);                        \
slouken@0
   305
}
slouken@7191
   306
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)               \
slouken@7191
   307
{                                   \
slouken@7191
   308
    r = (Pixel&0xFF);                       \
slouken@7191
   309
    g = ((Pixel>>8)&0xFF);                      \
slouken@7191
   310
    b = ((Pixel>>16)&0xFF);                     \
slouken@7191
   311
    a = (Pixel>>24);                        \
slouken@0
   312
}
slouken@7191
   313
#define RGBA_FROM_BGRA8888(Pixel, r, g, b, a)               \
slouken@7191
   314
{                                   \
slouken@7191
   315
    r = ((Pixel>>8)&0xFF);                      \
slouken@7191
   316
    g = ((Pixel>>16)&0xFF);                     \
slouken@7191
   317
    b = (Pixel>>24);                        \
slouken@7191
   318
    a = (Pixel&0xFF);                       \
slouken@3054
   319
}
slouken@7191
   320
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)            \
slouken@7191
   321
do {                                       \
slouken@7191
   322
    switch (bpp) {                             \
slouken@7191
   323
        case 2:                            \
slouken@7191
   324
            Pixel = *((Uint16 *)(buf));            \
slouken@7191
   325
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);       \
slouken@7191
   326
        break;                             \
slouken@7191
   327
                                       \
slouken@7191
   328
        case 3: {                          \
slouken@5426
   329
            Pixel = 0; \
slouken@7191
   330
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {         \
slouken@7191
   331
                    r = *((buf)+fmt->Rshift/8);        \
slouken@7191
   332
                g = *((buf)+fmt->Gshift/8);        \
slouken@7191
   333
                b = *((buf)+fmt->Bshift/8);        \
slouken@7191
   334
            } else {                       \
slouken@7191
   335
                    r = *((buf)+2-fmt->Rshift/8);          \
slouken@7191
   336
                g = *((buf)+2-fmt->Gshift/8);          \
slouken@7191
   337
                b = *((buf)+2-fmt->Bshift/8);          \
slouken@7191
   338
            }                          \
slouken@7191
   339
            a = 0xFF;                      \
slouken@7191
   340
        }                              \
slouken@7191
   341
        break;                             \
slouken@7191
   342
                                       \
slouken@7191
   343
        case 4:                            \
slouken@7191
   344
            Pixel = *((Uint32 *)(buf));            \
slouken@7191
   345
            RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);       \
slouken@7191
   346
        break;                             \
slouken@7191
   347
                                       \
slouken@7191
   348
        default:                           \
slouken@7191
   349
                /* stop gcc complaints */          \
slouken@7191
   350
                Pixel = 0;              \
slouken@5426
   351
                r = g = b = a = 0;      \
slouken@7191
   352
        break;                             \
slouken@7191
   353
    }                                  \
slouken@2824
   354
} while (0)
slouken@0
   355
slouken@0
   356
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
slouken@7191
   357
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)             \
slouken@7191
   358
{                                   \
slouken@7191
   359
    Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|             \
slouken@7191
   360
        ((g>>fmt->Gloss)<<fmt->Gshift)|             \
slouken@7191
   361
        ((b>>fmt->Bloss)<<fmt->Bshift)|             \
slouken@7191
   362
        ((a>>fmt->Aloss)<<fmt->Ashift);             \
slouken@0
   363
}
slouken@7191
   364
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)            \
slouken@7191
   365
{                                   \
slouken@7191
   366
    switch (bpp) {                          \
slouken@7191
   367
        case 2: {                       \
slouken@7191
   368
            Uint16 Pixel;                   \
slouken@7191
   369
                                    \
slouken@7191
   370
            PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);    \
slouken@7191
   371
            *((Uint16 *)(buf)) = Pixel;     \
slouken@7191
   372
        }                           \
slouken@7191
   373
        break;                          \
slouken@7191
   374
                                    \
slouken@7191
   375
        case 3: {                       \
slouken@7191
   376
                        if (SDL_BYTEORDER == SDL_LIL_ENDIAN) {      \
slouken@7191
   377
                    *((buf)+fmt->Rshift/8) = r;     \
slouken@7191
   378
                *((buf)+fmt->Gshift/8) = g;     \
slouken@7191
   379
                *((buf)+fmt->Bshift/8) = b;     \
slouken@7191
   380
            } else {                    \
slouken@7191
   381
                    *((buf)+2-fmt->Rshift/8) = r;       \
slouken@7191
   382
                *((buf)+2-fmt->Gshift/8) = g;       \
slouken@7191
   383
                *((buf)+2-fmt->Bshift/8) = b;       \
slouken@7191
   384
            }                       \
slouken@7191
   385
        }                           \
slouken@7191
   386
        break;                          \
slouken@7191
   387
                                    \
slouken@7191
   388
        case 4: {                       \
slouken@7191
   389
            Uint32 Pixel;                   \
slouken@7191
   390
                                    \
slouken@7191
   391
            PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);    \
slouken@7191
   392
            *((Uint32 *)(buf)) = Pixel;         \
slouken@7191
   393
        }                           \
slouken@7191
   394
        break;                          \
slouken@7191
   395
    }                               \
slouken@0
   396
}
slouken@0
   397
icculus@1162
   398
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@7191
   399
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)  \
slouken@7191
   400
do {                        \
slouken@7191
   401
    dR = ((((int)(sR-dR)*(int)A)/255)+dR);  \
slouken@7191
   402
    dG = ((((int)(sG-dG)*(int)A)/255)+dG);  \
slouken@7191
   403
    dB = ((((int)(sB-dB)*(int)A)/255)+dB);  \
icculus@1047
   404
} while(0)
icculus@1047
   405
icculus@1047
   406
slouken@0
   407
/* This is a very useful loop for optimizing blitters */
slouken@553
   408
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   409
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   410
#else
slouken@0
   411
#define USE_DUFFS_LOOP
slouken@553
   412
#endif
slouken@0
   413
#ifdef USE_DUFFS_LOOP
slouken@0
   414
slouken@0
   415
/* 8-times unrolled loop */
slouken@7191
   416
#define DUFFS_LOOP8(pixel_copy_increment, width)            \
slouken@7191
   417
{ int n = (width+7)/8;                          \
slouken@7191
   418
    switch (width & 7) {                        \
slouken@7191
   419
    case 0: do {    pixel_copy_increment;               \
slouken@7191
   420
    case 7:     pixel_copy_increment;               \
slouken@7191
   421
    case 6:     pixel_copy_increment;               \
slouken@7191
   422
    case 5:     pixel_copy_increment;               \
slouken@7191
   423
    case 4:     pixel_copy_increment;               \
slouken@7191
   424
    case 3:     pixel_copy_increment;               \
slouken@7191
   425
    case 2:     pixel_copy_increment;               \
slouken@7191
   426
    case 1:     pixel_copy_increment;               \
slouken@7191
   427
        } while ( --n > 0 );                    \
slouken@7191
   428
    }                               \
slouken@0
   429
}
slouken@0
   430
slouken@0
   431
/* 4-times unrolled loop */
slouken@7191
   432
#define DUFFS_LOOP4(pixel_copy_increment, width)            \
slouken@7191
   433
{ int n = (width+3)/4;                          \
slouken@7191
   434
    switch (width & 3) {                        \
slouken@7191
   435
    case 0: do {    pixel_copy_increment;               \
slouken@7191
   436
    case 3:     pixel_copy_increment;               \
slouken@7191
   437
    case 2:     pixel_copy_increment;               \
slouken@7191
   438
    case 1:     pixel_copy_increment;               \
slouken@7191
   439
        } while (--n > 0);                  \
slouken@7191
   440
    }                               \
slouken@689
   441
}
slouken@689
   442
slouken@0
   443
/* Use the 8-times version of the loop by default */
slouken@7191
   444
#define DUFFS_LOOP(pixel_copy_increment, width)             \
slouken@7191
   445
    DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   446
slouken@3035
   447
/* Special version of Duff's device for even more optimization */
slouken@7191
   448
#define DUFFS_LOOP_124(pixel_copy_increment1,               \
slouken@7191
   449
                       pixel_copy_increment2,               \
slouken@7191
   450
                       pixel_copy_increment4, width)            \
slouken@7191
   451
{ int n = width;                            \
slouken@7191
   452
    if (n & 1) {                            \
slouken@7191
   453
        pixel_copy_increment1; n -= 1;              \
slouken@7191
   454
    }                               \
slouken@7191
   455
    if (n & 2) {                            \
slouken@7191
   456
        pixel_copy_increment2; n -= 2;              \
slouken@7191
   457
    }                               \
slouken@7191
   458
    if (n) {                            \
slouken@7191
   459
        n = (n+7)/ 8;                       \
slouken@7191
   460
        switch (n & 4) {                    \
slouken@7191
   461
        case 0: do {    pixel_copy_increment4;          \
slouken@7191
   462
        case 4:     pixel_copy_increment4;          \
slouken@7191
   463
            } while (--n > 0);              \
slouken@7191
   464
        }                           \
slouken@7191
   465
    }                               \
slouken@689
   466
}
slouken@689
   467
slouken@3035
   468
#else
slouken@689
   469
slouken@689
   470
/* Don't use Duff's device to unroll loops */
slouken@7191
   471
#define DUFFS_LOOP(pixel_copy_increment, width)             \
slouken@7191
   472
{ int n;                                \
slouken@7191
   473
    for ( n=width; n > 0; --n ) {                   \
slouken@7191
   474
        pixel_copy_increment;                   \
slouken@7191
   475
    }                               \
slouken@0
   476
}
slouken@7191
   477
#define DUFFS_LOOP8(pixel_copy_increment, width)            \
slouken@7191
   478
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7191
   479
#define DUFFS_LOOP4(pixel_copy_increment, width)            \
slouken@7191
   480
    DUFFS_LOOP(pixel_copy_increment, width)
slouken@7191
   481
#define DUFFS_LOOP_124(pixel_copy_increment1,               \
slouken@7191
   482
                       pixel_copy_increment2,               \
slouken@7191
   483
                       pixel_copy_increment4, width)            \
slouken@7191
   484
    DUFFS_LOOP(pixel_copy_increment1, width)
slouken@0
   485
slouken@0
   486
#endif /* USE_DUFFS_LOOP */
slouken@0
   487
slouken@0
   488
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   489
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   490
#pragma warning(disable: 4550)
slouken@0
   491
#endif
slouken@0
   492
slouken@0
   493
#endif /* _SDL_blit_h */
slouken@2898
   494
slouken@1895
   495
/* vi: set ts=4 sw=4 expandtab: */