src/video/SDL_blit_0.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Aug 2007 06:21:58 +0000
changeset 2262 bee005ace1bf
parent 2257 340942cfda48
child 2267 c785543d1843
permissions -rw-r--r--
Work in progress: merging new texture features into SDL blit system
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
slouken@0
    27
/* Functions to blit from bitmaps to other surfaces */
slouken@0
    28
slouken@1895
    29
static void
slouken@1895
    30
BlitBto1(SDL_BlitInfo * info)
slouken@0
    31
{
slouken@1895
    32
    int c;
slouken@1895
    33
    int width, height;
slouken@1895
    34
    Uint8 *src, *map, *dst;
slouken@1895
    35
    int srcskip, dstskip;
slouken@0
    36
slouken@1895
    37
    /* Set up some basic variables */
slouken@2262
    38
    width = info->dst_w;
slouken@2262
    39
    height = info->dst_h;
slouken@2262
    40
    src = info->src;
slouken@1895
    41
    srcskip = info->s_skip;
slouken@2262
    42
    dst = info->dst;
slouken@2262
    43
    dstskip = info->dst_pitch;
slouken@1895
    44
    map = info->table;
slouken@1895
    45
    srcskip += width - (width + 7) / 8;
slouken@0
    46
slouken@1895
    47
    if (map) {
slouken@1895
    48
        while (height--) {
slouken@1895
    49
            Uint8 byte = 0, bit;
slouken@1895
    50
            for (c = 0; c < width; ++c) {
slouken@1895
    51
                if ((c & 7) == 0) {
slouken@1895
    52
                    byte = *src++;
slouken@1895
    53
                }
slouken@1895
    54
                bit = (byte & 0x80) >> 7;
slouken@1895
    55
                if (1) {
slouken@1895
    56
                    *dst = map[bit];
slouken@1895
    57
                }
slouken@1895
    58
                dst++;
slouken@1895
    59
                byte <<= 1;
slouken@1895
    60
            }
slouken@1895
    61
            src += srcskip;
slouken@1895
    62
            dst += dstskip;
slouken@1895
    63
        }
slouken@1895
    64
    } else {
slouken@1895
    65
        while (height--) {
slouken@1895
    66
            Uint8 byte = 0, bit;
slouken@1895
    67
            for (c = 0; c < width; ++c) {
slouken@1895
    68
                if ((c & 7) == 0) {
slouken@1895
    69
                    byte = *src++;
slouken@1895
    70
                }
slouken@1895
    71
                bit = (byte & 0x80) >> 7;
slouken@1895
    72
                if (1) {
slouken@1895
    73
                    *dst = bit;
slouken@1895
    74
                }
slouken@1895
    75
                dst++;
slouken@1895
    76
                byte <<= 1;
slouken@1895
    77
            }
slouken@1895
    78
            src += srcskip;
slouken@1895
    79
            dst += dstskip;
slouken@1895
    80
        }
slouken@1895
    81
    }
slouken@0
    82
}
slouken@1895
    83
static void
slouken@1895
    84
BlitBto2(SDL_BlitInfo * info)
slouken@0
    85
{
slouken@1895
    86
    int c;
slouken@1895
    87
    int width, height;
slouken@1895
    88
    Uint8 *src;
slouken@1895
    89
    Uint16 *map, *dst;
slouken@1895
    90
    int srcskip, dstskip;
slouken@0
    91
slouken@1895
    92
    /* Set up some basic variables */
slouken@2262
    93
    width = info->dst_w;
slouken@2262
    94
    height = info->dst_h;
slouken@2262
    95
    src = info->src;
slouken@1895
    96
    srcskip = info->s_skip;
slouken@2262
    97
    dst = (Uint16 *) info->dst;
slouken@2262
    98
    dstskip = info->dst_pitch / 2;
slouken@1895
    99
    map = (Uint16 *) info->table;
slouken@1895
   100
    srcskip += width - (width + 7) / 8;
slouken@0
   101
slouken@1895
   102
    while (height--) {
slouken@1895
   103
        Uint8 byte = 0, bit;
slouken@1895
   104
        for (c = 0; c < width; ++c) {
slouken@1895
   105
            if ((c & 7) == 0) {
slouken@1895
   106
                byte = *src++;
slouken@1895
   107
            }
slouken@1895
   108
            bit = (byte & 0x80) >> 7;
slouken@1895
   109
            if (1) {
slouken@1895
   110
                *dst = map[bit];
slouken@1895
   111
            }
slouken@1895
   112
            byte <<= 1;
slouken@1895
   113
            dst++;
slouken@1895
   114
        }
slouken@1895
   115
        src += srcskip;
slouken@1895
   116
        dst += dstskip;
slouken@1895
   117
    }
slouken@0
   118
}
slouken@1895
   119
static void
slouken@1895
   120
BlitBto3(SDL_BlitInfo * info)
slouken@0
   121
{
slouken@1895
   122
    int c, o;
slouken@1895
   123
    int width, height;
slouken@1895
   124
    Uint8 *src, *map, *dst;
slouken@1895
   125
    int srcskip, dstskip;
slouken@0
   126
slouken@1895
   127
    /* Set up some basic variables */
slouken@2262
   128
    width = info->dst_w;
slouken@2262
   129
    height = info->dst_h;
slouken@2262
   130
    src = info->src;
slouken@1895
   131
    srcskip = info->s_skip;
slouken@2262
   132
    dst = info->dst;
slouken@2262
   133
    dstskip = info->dst_pitch;
slouken@1895
   134
    map = info->table;
slouken@1895
   135
    srcskip += width - (width + 7) / 8;
slouken@0
   136
slouken@1895
   137
    while (height--) {
slouken@1895
   138
        Uint8 byte = 0, bit;
slouken@1895
   139
        for (c = 0; c < width; ++c) {
slouken@1895
   140
            if ((c & 7) == 0) {
slouken@1895
   141
                byte = *src++;
slouken@1895
   142
            }
slouken@1895
   143
            bit = (byte & 0x80) >> 7;
slouken@1895
   144
            if (1) {
slouken@1895
   145
                o = bit * 4;
slouken@1895
   146
                dst[0] = map[o++];
slouken@1895
   147
                dst[1] = map[o++];
slouken@1895
   148
                dst[2] = map[o++];
slouken@1895
   149
            }
slouken@1895
   150
            byte <<= 1;
slouken@1895
   151
            dst += 3;
slouken@1895
   152
        }
slouken@1895
   153
        src += srcskip;
slouken@1895
   154
        dst += dstskip;
slouken@1895
   155
    }
slouken@0
   156
}
slouken@1895
   157
static void
slouken@1895
   158
BlitBto4(SDL_BlitInfo * info)
slouken@0
   159
{
slouken@1895
   160
    int width, height;
slouken@1895
   161
    Uint8 *src;
slouken@1895
   162
    Uint32 *map, *dst;
slouken@1895
   163
    int srcskip, dstskip;
slouken@1895
   164
    int c;
slouken@0
   165
slouken@1895
   166
    /* Set up some basic variables */
slouken@2262
   167
    width = info->dst_w;
slouken@2262
   168
    height = info->dst_h;
slouken@2262
   169
    src = info->src;
slouken@1895
   170
    srcskip = info->s_skip;
slouken@2262
   171
    dst = (Uint32 *) info->dst;
slouken@2262
   172
    dstskip = info->dst_pitch / 4;
slouken@1895
   173
    map = (Uint32 *) info->table;
slouken@1895
   174
    srcskip += width - (width + 7) / 8;
slouken@0
   175
slouken@1895
   176
    while (height--) {
slouken@1895
   177
        Uint8 byte = 0, bit;
slouken@1895
   178
        for (c = 0; c < width; ++c) {
slouken@1895
   179
            if ((c & 7) == 0) {
slouken@1895
   180
                byte = *src++;
slouken@1895
   181
            }
slouken@1895
   182
            bit = (byte & 0x80) >> 7;
slouken@1895
   183
            if (1) {
slouken@1895
   184
                *dst = map[bit];
slouken@1895
   185
            }
slouken@1895
   186
            byte <<= 1;
slouken@1895
   187
            dst++;
slouken@1895
   188
        }
slouken@1895
   189
        src += srcskip;
slouken@1895
   190
        dst += dstskip;
slouken@1895
   191
    }
slouken@0
   192
}
slouken@0
   193
slouken@1895
   194
static void
slouken@1895
   195
BlitBto1Key(SDL_BlitInfo * info)
slouken@0
   196
{
slouken@2262
   197
    int width = info->dst_w;
slouken@2262
   198
    int height = info->dst_h;
slouken@2262
   199
    Uint8 *src = info->src;
slouken@2262
   200
    Uint8 *dst = info->dst;
slouken@1895
   201
    int srcskip = info->s_skip;
slouken@2262
   202
    int dstskip = info->dst_pitch;
slouken@2257
   203
    Uint32 ckey = info->ckey;
slouken@1895
   204
    Uint8 *palmap = info->table;
slouken@1895
   205
    int c;
slouken@0
   206
slouken@1895
   207
    /* Set up some basic variables */
slouken@1895
   208
    srcskip += width - (width + 7) / 8;
slouken@0
   209
slouken@1895
   210
    if (palmap) {
slouken@1895
   211
        while (height--) {
slouken@1895
   212
            Uint8 byte = 0, bit;
slouken@1895
   213
            for (c = 0; c < width; ++c) {
slouken@1895
   214
                if ((c & 7) == 0) {
slouken@1895
   215
                    byte = *src++;
slouken@1895
   216
                }
slouken@1895
   217
                bit = (byte & 0x80) >> 7;
slouken@1895
   218
                if (bit != ckey) {
slouken@1895
   219
                    *dst = palmap[bit];
slouken@1895
   220
                }
slouken@1895
   221
                dst++;
slouken@1895
   222
                byte <<= 1;
slouken@1895
   223
            }
slouken@1895
   224
            src += srcskip;
slouken@1895
   225
            dst += dstskip;
slouken@1895
   226
        }
slouken@1895
   227
    } else {
slouken@1895
   228
        while (height--) {
slouken@1895
   229
            Uint8 byte = 0, bit;
slouken@1895
   230
            for (c = 0; c < width; ++c) {
slouken@1895
   231
                if ((c & 7) == 0) {
slouken@1895
   232
                    byte = *src++;
slouken@1895
   233
                }
slouken@1895
   234
                bit = (byte & 0x80) >> 7;
slouken@1895
   235
                if (bit != ckey) {
slouken@1895
   236
                    *dst = bit;
slouken@1895
   237
                }
slouken@1895
   238
                dst++;
slouken@1895
   239
                byte <<= 1;
slouken@1895
   240
            }
slouken@1895
   241
            src += srcskip;
slouken@1895
   242
            dst += dstskip;
slouken@1895
   243
        }
slouken@1895
   244
    }
slouken@0
   245
}
slouken@0
   246
slouken@1895
   247
static void
slouken@1895
   248
BlitBto2Key(SDL_BlitInfo * info)
slouken@0
   249
{
slouken@2262
   250
    int width = info->dst_w;
slouken@2262
   251
    int height = info->dst_h;
slouken@2262
   252
    Uint8 *src = info->src;
slouken@2262
   253
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@1895
   254
    int srcskip = info->s_skip;
slouken@2262
   255
    int dstskip = info->dst_pitch;
slouken@2257
   256
    Uint32 ckey = info->ckey;
slouken@1895
   257
    Uint8 *palmap = info->table;
slouken@1895
   258
    int c;
slouken@0
   259
slouken@1895
   260
    /* Set up some basic variables */
slouken@1895
   261
    srcskip += width - (width + 7) / 8;
slouken@1895
   262
    dstskip /= 2;
slouken@0
   263
slouken@1895
   264
    while (height--) {
slouken@1895
   265
        Uint8 byte = 0, bit;
slouken@1895
   266
        for (c = 0; c < width; ++c) {
slouken@1895
   267
            if ((c & 7) == 0) {
slouken@1895
   268
                byte = *src++;
slouken@1895
   269
            }
slouken@1895
   270
            bit = (byte & 0x80) >> 7;
slouken@1895
   271
            if (bit != ckey) {
slouken@1895
   272
                *dstp = ((Uint16 *) palmap)[bit];
slouken@1895
   273
            }
slouken@1895
   274
            byte <<= 1;
slouken@1895
   275
            dstp++;
slouken@1895
   276
        }
slouken@1895
   277
        src += srcskip;
slouken@1895
   278
        dstp += dstskip;
slouken@1895
   279
    }
slouken@0
   280
}
slouken@0
   281
slouken@1895
   282
static void
slouken@1895
   283
BlitBto3Key(SDL_BlitInfo * info)
slouken@0
   284
{
slouken@2262
   285
    int width = info->dst_w;
slouken@2262
   286
    int height = info->dst_h;
slouken@2262
   287
    Uint8 *src = info->src;
slouken@2262
   288
    Uint8 *dst = info->dst;
slouken@1895
   289
    int srcskip = info->s_skip;
slouken@2262
   290
    int dstskip = info->dst_pitch;
slouken@2257
   291
    Uint32 ckey = info->ckey;
slouken@1895
   292
    Uint8 *palmap = info->table;
slouken@1895
   293
    int c;
slouken@0
   294
slouken@1895
   295
    /* Set up some basic variables */
slouken@1895
   296
    srcskip += width - (width + 7) / 8;
slouken@0
   297
slouken@1895
   298
    while (height--) {
slouken@1895
   299
        Uint8 byte = 0, bit;
slouken@1895
   300
        for (c = 0; c < width; ++c) {
slouken@1895
   301
            if ((c & 7) == 0) {
slouken@1895
   302
                byte = *src++;
slouken@1895
   303
            }
slouken@1895
   304
            bit = (byte & 0x80) >> 7;
slouken@1895
   305
            if (bit != ckey) {
slouken@1895
   306
                SDL_memcpy(dst, &palmap[bit * 4], 3);
slouken@1895
   307
            }
slouken@1895
   308
            byte <<= 1;
slouken@1895
   309
            dst += 3;
slouken@1895
   310
        }
slouken@1895
   311
        src += srcskip;
slouken@1895
   312
        dst += dstskip;
slouken@1895
   313
    }
slouken@0
   314
}
slouken@0
   315
slouken@1895
   316
static void
slouken@1895
   317
BlitBto4Key(SDL_BlitInfo * info)
slouken@0
   318
{
slouken@2262
   319
    int width = info->dst_w;
slouken@2262
   320
    int height = info->dst_h;
slouken@2262
   321
    Uint8 *src = info->src;
slouken@2262
   322
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@1895
   323
    int srcskip = info->s_skip;
slouken@2262
   324
    int dstskip = info->dst_pitch;
slouken@2257
   325
    Uint32 ckey = info->ckey;
slouken@1895
   326
    Uint8 *palmap = info->table;
slouken@1895
   327
    int c;
slouken@0
   328
slouken@1895
   329
    /* Set up some basic variables */
slouken@1895
   330
    srcskip += width - (width + 7) / 8;
slouken@1895
   331
    dstskip /= 4;
slouken@0
   332
slouken@1895
   333
    while (height--) {
slouken@1895
   334
        Uint8 byte = 0, bit;
slouken@1895
   335
        for (c = 0; c < width; ++c) {
slouken@1895
   336
            if ((c & 7) == 0) {
slouken@1895
   337
                byte = *src++;
slouken@1895
   338
            }
slouken@1895
   339
            bit = (byte & 0x80) >> 7;
slouken@1895
   340
            if (bit != ckey) {
slouken@1895
   341
                *dstp = ((Uint32 *) palmap)[bit];
slouken@1895
   342
            }
slouken@1895
   343
            byte <<= 1;
slouken@1895
   344
            dstp++;
slouken@1895
   345
        }
slouken@1895
   346
        src += srcskip;
slouken@1895
   347
        dstp += dstskip;
slouken@1895
   348
    }
slouken@0
   349
}
slouken@0
   350
slouken@1895
   351
static void
slouken@1895
   352
BlitBtoNAlpha(SDL_BlitInfo * info)
slouken@0
   353
{
slouken@2262
   354
    int width = info->dst_w;
slouken@2262
   355
    int height = info->dst_h;
slouken@2262
   356
    Uint8 *src = info->src;
slouken@2262
   357
    Uint8 *dst = info->dst;
slouken@1895
   358
    int srcskip = info->s_skip;
slouken@2262
   359
    int dstskip = info->dst_pitch;
slouken@1895
   360
    const SDL_Color *srcpal = info->src->palette->colors;
slouken@1895
   361
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
   362
    int dstbpp;
slouken@1895
   363
    int c;
slouken@2257
   364
    const int A = (info->cmod >> 24);
slouken@0
   365
slouken@1895
   366
    /* Set up some basic variables */
slouken@1895
   367
    dstbpp = dstfmt->BytesPerPixel;
slouken@1895
   368
    srcskip += width - (width + 7) / 8;
slouken@0
   369
slouken@1895
   370
    while (height--) {
slouken@1895
   371
        Uint8 byte = 0, bit;
slouken@1895
   372
        for (c = 0; c < width; ++c) {
slouken@1895
   373
            if ((c & 7) == 0) {
slouken@1895
   374
                byte = *src++;
slouken@1895
   375
            }
slouken@1895
   376
            bit = (byte & 0x80) >> 7;
slouken@1895
   377
            if (1) {
slouken@1895
   378
                Uint32 pixel;
slouken@1895
   379
                unsigned sR, sG, sB;
slouken@1895
   380
                unsigned dR, dG, dB;
slouken@1895
   381
                sR = srcpal[bit].r;
slouken@1895
   382
                sG = srcpal[bit].g;
slouken@1895
   383
                sB = srcpal[bit].b;
slouken@1895
   384
                DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@1895
   385
                ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@1895
   386
                ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
slouken@1895
   387
            }
slouken@1895
   388
            byte <<= 1;
slouken@1895
   389
            dst += dstbpp;
slouken@1895
   390
        }
slouken@1895
   391
        src += srcskip;
slouken@1895
   392
        dst += dstskip;
slouken@1895
   393
    }
slouken@0
   394
}
slouken@0
   395
slouken@1895
   396
static void
slouken@1895
   397
BlitBtoNAlphaKey(SDL_BlitInfo * info)
slouken@0
   398
{
slouken@2262
   399
    int width = info->dst_w;
slouken@2262
   400
    int height = info->dst_h;
slouken@2262
   401
    Uint8 *src = info->src;
slouken@2262
   402
    Uint8 *dst = info->dst;
slouken@1895
   403
    int srcskip = info->s_skip;
slouken@2262
   404
    int dstskip = info->dst_pitch;
slouken@1895
   405
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
   406
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
   407
    const SDL_Color *srcpal = srcfmt->palette->colors;
slouken@1895
   408
    int dstbpp;
slouken@1895
   409
    int c;
slouken@2257
   410
    const int A = (info->cmod >> 24);
slouken@2257
   411
    Uint32 ckey = info->ckey;
slouken@0
   412
slouken@1895
   413
    /* Set up some basic variables */
slouken@1895
   414
    dstbpp = dstfmt->BytesPerPixel;
slouken@1895
   415
    srcskip += width - (width + 7) / 8;
slouken@0
   416
slouken@1895
   417
    while (height--) {
slouken@1895
   418
        Uint8 byte = 0, bit;
slouken@1895
   419
        for (c = 0; c < width; ++c) {
slouken@1895
   420
            if ((c & 7) == 0) {
slouken@1895
   421
                byte = *src++;
slouken@1895
   422
            }
slouken@1895
   423
            bit = (byte & 0x80) >> 7;
slouken@1895
   424
            if (bit != ckey) {
slouken@1895
   425
                int sR, sG, sB;
slouken@1895
   426
                int dR, dG, dB;
slouken@1895
   427
                Uint32 pixel;
slouken@1895
   428
                sR = srcpal[bit].r;
slouken@1895
   429
                sG = srcpal[bit].g;
slouken@1895
   430
                sB = srcpal[bit].b;
slouken@1895
   431
                DISEMBLE_RGB(dst, dstbpp, dstfmt, pixel, dR, dG, dB);
slouken@1895
   432
                ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@1895
   433
                ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
slouken@1895
   434
            }
slouken@1895
   435
            byte <<= 1;
slouken@1895
   436
            dst += dstbpp;
slouken@1895
   437
        }
slouken@1895
   438
        src += srcskip;
slouken@1895
   439
        dst += dstskip;
slouken@1895
   440
    }
slouken@0
   441
}
slouken@0
   442
slouken@0
   443
static SDL_loblit bitmap_blit[] = {
slouken@1895
   444
    NULL, BlitBto1, BlitBto2, BlitBto3, BlitBto4
slouken@0
   445
};
slouken@0
   446
slouken@0
   447
static SDL_loblit colorkey_blit[] = {
slouken@0
   448
    NULL, BlitBto1Key, BlitBto2Key, BlitBto3Key, BlitBto4Key
slouken@0
   449
};
slouken@0
   450
slouken@1895
   451
SDL_loblit
slouken@1895
   452
SDL_CalculateBlit0(SDL_Surface * surface, int blit_index)
slouken@0
   453
{
slouken@1895
   454
    int which;
slouken@0
   455
slouken@1895
   456
    if (surface->format->BitsPerPixel != 1) {
slouken@1895
   457
        /* We don't support sub 8-bit packed pixel modes */
slouken@1895
   458
        return NULL;
slouken@1895
   459
    }
slouken@1895
   460
    if (surface->map->dst->format->BitsPerPixel < 8) {
slouken@1895
   461
        which = 0;
slouken@1895
   462
    } else {
slouken@1895
   463
        which = surface->map->dst->format->BytesPerPixel;
slouken@1895
   464
    }
slouken@1895
   465
    switch (blit_index) {
slouken@1895
   466
    case 0:                    /* copy */
slouken@1895
   467
        return bitmap_blit[which];
slouken@0
   468
slouken@1895
   469
    case 1:                    /* colorkey */
slouken@1895
   470
        return colorkey_blit[which];
slouken@0
   471
slouken@1895
   472
    case 2:                    /* alpha */
slouken@1895
   473
        return which >= 2 ? BlitBtoNAlpha : NULL;
slouken@0
   474
slouken@1895
   475
    case 4:                    /* alpha + colorkey */
slouken@1895
   476
        return which >= 2 ? BlitBtoNAlphaKey : NULL;
slouken@1895
   477
    }
slouken@1895
   478
    return NULL;
slouken@0
   479
}
slouken@0
   480
slouken@1895
   481
/* vi: set ts=4 sw=4 expandtab: */