src/video/SDL_pixels.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Feb 2011 13:46:10 -0800
changeset 5288 d4381f3b0d1e
parent 5262 b530ef003506
child 5295 b88d05603e4b
permissions -rw-r--r--
A few fixes:
Fixed creating render texture framebuffer.
Removed the need for palette watch, added surface format caching.
Added an SDL_DONTFREE flag so you can't free the window and 1.2 shadow surfaces.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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
/* General (mostly internal) pixel/color manipulation routines for SDL */
slouken@0
    25
slouken@0
    26
#include "SDL_endian.h"
slouken@0
    27
#include "SDL_video.h"
slouken@0
    28
#include "SDL_sysvideo.h"
slouken@0
    29
#include "SDL_blit.h"
slouken@0
    30
#include "SDL_pixels_c.h"
slouken@0
    31
#include "SDL_RLEaccel_c.h"
slouken@0
    32
slouken@3555
    33
slouken@0
    34
/* Helper functions */
slouken@1895
    35
slouken@4982
    36
const char*
slouken@4982
    37
SDL_GetPixelFormatName(Uint32 format)
slouken@4982
    38
{
slouken@4982
    39
    switch (format) {
slouken@4982
    40
#define CASE(X) case X: return #X;
slouken@4982
    41
    CASE(SDL_PIXELFORMAT_INDEX1LSB)
slouken@4982
    42
    CASE(SDL_PIXELFORMAT_INDEX1MSB)
slouken@4982
    43
    CASE(SDL_PIXELFORMAT_INDEX4LSB)
slouken@4982
    44
    CASE(SDL_PIXELFORMAT_INDEX4MSB)
slouken@4982
    45
    CASE(SDL_PIXELFORMAT_INDEX8)
slouken@4982
    46
    CASE(SDL_PIXELFORMAT_RGB332)
slouken@4982
    47
    CASE(SDL_PIXELFORMAT_RGB444)
slouken@4982
    48
    CASE(SDL_PIXELFORMAT_RGB555)
slouken@4982
    49
    CASE(SDL_PIXELFORMAT_BGR555)
slouken@4982
    50
    CASE(SDL_PIXELFORMAT_ARGB4444)
slouken@4982
    51
    CASE(SDL_PIXELFORMAT_RGBA4444)
slouken@4982
    52
    CASE(SDL_PIXELFORMAT_ABGR4444)
slouken@4982
    53
    CASE(SDL_PIXELFORMAT_BGRA4444)
slouken@4982
    54
    CASE(SDL_PIXELFORMAT_ARGB1555)
slouken@4982
    55
    CASE(SDL_PIXELFORMAT_RGBA5551)
slouken@4982
    56
    CASE(SDL_PIXELFORMAT_ABGR1555)
slouken@4982
    57
    CASE(SDL_PIXELFORMAT_BGRA5551)
slouken@4982
    58
    CASE(SDL_PIXELFORMAT_RGB565)
slouken@4982
    59
    CASE(SDL_PIXELFORMAT_BGR565)
slouken@4982
    60
    CASE(SDL_PIXELFORMAT_RGB24)
slouken@4982
    61
    CASE(SDL_PIXELFORMAT_BGR24)
slouken@4982
    62
    CASE(SDL_PIXELFORMAT_RGB888)
slouken@4982
    63
    CASE(SDL_PIXELFORMAT_BGR888)
slouken@4982
    64
    CASE(SDL_PIXELFORMAT_ARGB8888)
slouken@4982
    65
    CASE(SDL_PIXELFORMAT_RGBA8888)
slouken@4982
    66
    CASE(SDL_PIXELFORMAT_ABGR8888)
slouken@4982
    67
    CASE(SDL_PIXELFORMAT_BGRA8888)
slouken@4982
    68
    CASE(SDL_PIXELFORMAT_ARGB2101010)
slouken@4982
    69
    CASE(SDL_PIXELFORMAT_YV12)
slouken@4982
    70
    CASE(SDL_PIXELFORMAT_IYUV)
slouken@4982
    71
    CASE(SDL_PIXELFORMAT_YUY2)
slouken@4982
    72
    CASE(SDL_PIXELFORMAT_UYVY)
slouken@4982
    73
    CASE(SDL_PIXELFORMAT_YVYU)
slouken@4982
    74
#undef CASE
slouken@4982
    75
    default:
slouken@4982
    76
        return "SDL_PIXELFORMAT_UNKNOWN";
slouken@4982
    77
    }
slouken@4982
    78
}
slouken@4982
    79
slouken@1895
    80
SDL_bool
slouken@1895
    81
SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask,
slouken@1895
    82
                           Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
slouken@1895
    83
{
slouken@1895
    84
    Uint32 masks[4];
slouken@1895
    85
slouken@1895
    86
    /* Initialize the values here */
slouken@3284
    87
    if (SDL_BYTESPERPIXEL(format) <= 2) {
slouken@3284
    88
        *bpp = SDL_BITSPERPIXEL(format);
slouken@3284
    89
    } else {
slouken@1895
    90
        *bpp = SDL_BYTESPERPIXEL(format) * 8;
slouken@1895
    91
    }
slouken@1895
    92
    *Rmask = *Gmask = *Bmask = *Amask = 0;
slouken@1895
    93
slouken@3557
    94
    if (format == SDL_PIXELFORMAT_RGB24) {
slouken@3557
    95
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3557
    96
        *Rmask = 0x00FF0000;
slouken@3557
    97
        *Gmask = 0x0000FF00;
slouken@3557
    98
        *Bmask = 0x000000FF;
slouken@3557
    99
#else
slouken@3557
   100
        *Rmask = 0x000000FF;
slouken@3557
   101
        *Gmask = 0x0000FF00;
slouken@3557
   102
        *Bmask = 0x00FF0000;
slouken@3557
   103
#endif
slouken@3557
   104
        return SDL_TRUE;
slouken@3557
   105
    }
slouken@3557
   106
slouken@3557
   107
    if (format == SDL_PIXELFORMAT_BGR24) {
slouken@3557
   108
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3557
   109
        *Rmask = 0x000000FF;
slouken@3557
   110
        *Gmask = 0x0000FF00;
slouken@3557
   111
        *Bmask = 0x00FF0000;
slouken@3557
   112
#else
slouken@3557
   113
        *Rmask = 0x00FF0000;
slouken@3557
   114
        *Gmask = 0x0000FF00;
slouken@3557
   115
        *Bmask = 0x000000FF;
slouken@3557
   116
#endif
slouken@3557
   117
        return SDL_TRUE;
slouken@3557
   118
    }
slouken@3557
   119
slouken@1965
   120
    if (SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED8 &&
slouken@1965
   121
        SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED16 &&
slouken@1965
   122
        SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED32) {
slouken@1895
   123
        /* Not a format that uses masks */
slouken@1895
   124
        return SDL_TRUE;
slouken@1895
   125
    }
slouken@1895
   126
slouken@1895
   127
    switch (SDL_PIXELLAYOUT(format)) {
slouken@1965
   128
    case SDL_PACKEDLAYOUT_332:
slouken@1895
   129
        masks[0] = 0x00000000;
slouken@1895
   130
        masks[1] = 0x000000E0;
slouken@1895
   131
        masks[2] = 0x0000001C;
slouken@1895
   132
        masks[3] = 0x00000003;
slouken@1895
   133
        break;
slouken@1965
   134
    case SDL_PACKEDLAYOUT_4444:
slouken@1895
   135
        masks[0] = 0x0000F000;
slouken@1895
   136
        masks[1] = 0x00000F00;
slouken@1895
   137
        masks[2] = 0x000000F0;
slouken@1895
   138
        masks[3] = 0x0000000F;
slouken@1895
   139
        break;
slouken@1965
   140
    case SDL_PACKEDLAYOUT_1555:
slouken@1895
   141
        masks[0] = 0x00008000;
slouken@1895
   142
        masks[1] = 0x00007C00;
slouken@1895
   143
        masks[2] = 0x000003E0;
slouken@1895
   144
        masks[3] = 0x0000001F;
slouken@1895
   145
        break;
lestat@3163
   146
    case SDL_PACKEDLAYOUT_5551:
lestat@3163
   147
        masks[0] = 0x0000F800;
lestat@3163
   148
        masks[1] = 0x000007C0;
lestat@3163
   149
        masks[2] = 0x0000003E;
lestat@3163
   150
        masks[3] = 0x00000001;
lestat@3163
   151
        break;
slouken@1965
   152
    case SDL_PACKEDLAYOUT_565:
slouken@1895
   153
        masks[0] = 0x00000000;
slouken@1895
   154
        masks[1] = 0x0000F800;
slouken@1895
   155
        masks[2] = 0x000007E0;
slouken@1895
   156
        masks[3] = 0x0000001F;
slouken@1895
   157
        break;
slouken@1965
   158
    case SDL_PACKEDLAYOUT_8888:
slouken@1895
   159
        masks[0] = 0xFF000000;
slouken@1895
   160
        masks[1] = 0x00FF0000;
slouken@1895
   161
        masks[2] = 0x0000FF00;
slouken@1895
   162
        masks[3] = 0x000000FF;
slouken@1895
   163
        break;
slouken@1965
   164
    case SDL_PACKEDLAYOUT_2101010:
slouken@1895
   165
        masks[0] = 0xC0000000;
slouken@1895
   166
        masks[1] = 0x3FF00000;
slouken@1895
   167
        masks[2] = 0x000FFC00;
slouken@1895
   168
        masks[3] = 0x000003FF;
slouken@1895
   169
        break;
lestat@3163
   170
    case SDL_PACKEDLAYOUT_1010102:
lestat@3163
   171
        masks[0] = 0xFFC00000;
lestat@3163
   172
        masks[1] = 0x003FF000;
lestat@3163
   173
        masks[2] = 0x00000FFC;
lestat@3163
   174
        masks[3] = 0x00000003;
lestat@3163
   175
        break;
slouken@1895
   176
    default:
slouken@3484
   177
        SDL_SetError("Unknown pixel format");
slouken@1895
   178
        return SDL_FALSE;
slouken@1895
   179
    }
slouken@1895
   180
slouken@1895
   181
    switch (SDL_PIXELORDER(format)) {
slouken@1965
   182
    case SDL_PACKEDORDER_XRGB:
slouken@1895
   183
        *Rmask = masks[1];
slouken@1895
   184
        *Gmask = masks[2];
slouken@1895
   185
        *Bmask = masks[3];
slouken@1895
   186
        break;
slouken@1965
   187
    case SDL_PACKEDORDER_RGBX:
slouken@1895
   188
        *Rmask = masks[0];
slouken@1895
   189
        *Gmask = masks[1];
slouken@1895
   190
        *Bmask = masks[2];
slouken@1895
   191
        break;
slouken@1965
   192
    case SDL_PACKEDORDER_ARGB:
slouken@1895
   193
        *Amask = masks[0];
slouken@1895
   194
        *Rmask = masks[1];
slouken@1895
   195
        *Gmask = masks[2];
slouken@1895
   196
        *Bmask = masks[3];
slouken@1895
   197
        break;
slouken@1965
   198
    case SDL_PACKEDORDER_RGBA:
slouken@1895
   199
        *Rmask = masks[0];
slouken@1895
   200
        *Gmask = masks[1];
slouken@1895
   201
        *Bmask = masks[2];
slouken@1895
   202
        *Amask = masks[3];
slouken@1895
   203
        break;
slouken@1965
   204
    case SDL_PACKEDORDER_XBGR:
slouken@1895
   205
        *Bmask = masks[1];
slouken@1895
   206
        *Gmask = masks[2];
slouken@1895
   207
        *Rmask = masks[3];
slouken@1895
   208
        break;
slouken@1965
   209
    case SDL_PACKEDORDER_BGRX:
slouken@1895
   210
        *Bmask = masks[0];
slouken@1895
   211
        *Gmask = masks[1];
slouken@1895
   212
        *Rmask = masks[2];
slouken@1895
   213
        break;
slouken@1965
   214
    case SDL_PACKEDORDER_BGRA:
slouken@1895
   215
        *Bmask = masks[0];
slouken@1895
   216
        *Gmask = masks[1];
slouken@1895
   217
        *Rmask = masks[2];
slouken@1895
   218
        *Amask = masks[3];
slouken@1895
   219
        break;
slouken@1965
   220
    case SDL_PACKEDORDER_ABGR:
slouken@1895
   221
        *Amask = masks[0];
slouken@1895
   222
        *Bmask = masks[1];
slouken@1895
   223
        *Gmask = masks[2];
slouken@1895
   224
        *Rmask = masks[3];
slouken@1895
   225
        break;
slouken@1895
   226
    default:
slouken@3484
   227
        SDL_SetError("Unknown pixel format");
slouken@1895
   228
        return SDL_FALSE;
slouken@1895
   229
    }
slouken@1895
   230
    return SDL_TRUE;
slouken@1895
   231
}
slouken@1895
   232
slouken@1895
   233
Uint32
slouken@1895
   234
SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   235
                           Uint32 Amask)
slouken@1895
   236
{
slouken@1895
   237
    switch (bpp) {
slouken@1895
   238
    case 8:
slouken@1895
   239
        switch (Rmask) {
slouken@1895
   240
        case 0:
slouken@1965
   241
            return SDL_PIXELFORMAT_INDEX8;
slouken@1895
   242
        case 0xE0:
slouken@1965
   243
            return SDL_PIXELFORMAT_RGB332;
slouken@1895
   244
        }
slouken@1895
   245
        break;
slouken@1895
   246
    case 12:
slouken@1895
   247
        switch (Rmask) {
slouken@1895
   248
        case 0x0F00:
slouken@1965
   249
            return SDL_PIXELFORMAT_RGB444;
slouken@1895
   250
        }
slouken@1895
   251
        break;
slouken@1895
   252
    case 15:
slouken@5288
   253
        if (Rmask == 0) {
slouken@5288
   254
            return SDL_PIXELFORMAT_RGB555;
slouken@5288
   255
        }
slouken@5181
   256
        if (Rmask == 0x7C00 && Bmask == 0x001F) {
slouken@5181
   257
            return SDL_PIXELFORMAT_RGB555;
slouken@5181
   258
        }
slouken@5181
   259
        if (Rmask == 0x001F && Bmask == 0x7C00) {
slouken@2735
   260
            return SDL_PIXELFORMAT_BGR555;
slouken@1895
   261
        }
slouken@1895
   262
        break;
slouken@1895
   263
    case 16:
slouken@1895
   264
        switch (Rmask) {
slouken@5288
   265
        case 0:
slouken@5288
   266
            return SDL_PIXELFORMAT_RGB565;
slouken@4983
   267
        case 0xF000:
slouken@4983
   268
            return SDL_PIXELFORMAT_RGBA4444;
slouken@4983
   269
        case 0x0F00:
slouken@4983
   270
            return SDL_PIXELFORMAT_ARGB4444;
slouken@4983
   271
        case 0x00F0:
slouken@4983
   272
            return SDL_PIXELFORMAT_BGRA4444;
lestat@3163
   273
        case 0x000F:
lestat@3163
   274
            return SDL_PIXELFORMAT_ABGR4444;
slouken@5181
   275
        case 0xF800:
slouken@5181
   276
            if (Bmask == 0x001F) {
slouken@5181
   277
                return SDL_PIXELFORMAT_RGB565;
slouken@5181
   278
            }
slouken@5181
   279
            break;
slouken@5181
   280
        case 0x7C00:
slouken@5181
   281
            if (Bmask == 0x001F) {
slouken@5181
   282
                return SDL_PIXELFORMAT_RGB555;
slouken@5181
   283
            }
slouken@5181
   284
            break;
slouken@2735
   285
        case 0x001F:
slouken@5181
   286
            if (Bmask == 0xF800) {
lestat@3163
   287
                return SDL_PIXELFORMAT_BGR565;
lestat@3163
   288
            }
slouken@5181
   289
            if (Bmask == 0x7C00) {
slouken@5181
   290
                return SDL_PIXELFORMAT_BGR555;
slouken@4983
   291
            }
slouken@5181
   292
            break;
slouken@1895
   293
        }
slouken@1895
   294
        break;
lestat@3182
   295
    case 24:
lestat@3182
   296
        switch (Rmask) {
slouken@5288
   297
        case 0:
lestat@3182
   298
        case 0x00FF0000:
slouken@3597
   299
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3597
   300
            return SDL_PIXELFORMAT_RGB24;
slouken@3597
   301
#else
slouken@3597
   302
            return SDL_PIXELFORMAT_BGR24;
slouken@3597
   303
#endif
lestat@3182
   304
        case 0x000000FF:
slouken@3597
   305
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3597
   306
            return SDL_PIXELFORMAT_BGR24;
slouken@3597
   307
#else
slouken@3597
   308
            return SDL_PIXELFORMAT_RGB24;
slouken@3597
   309
#endif
lestat@3182
   310
        }
slouken@1895
   311
    case 32:
slouken@1895
   312
        switch (Rmask) {
slouken@1895
   313
        case 0xFF000000:
slouken@1895
   314
            if (Amask == 0x000000FF) {
slouken@1965
   315
                return SDL_PIXELFORMAT_RGBA8888;
slouken@1895
   316
            }
slouken@1895
   317
            break;
slouken@5288
   318
        case 0:
slouken@1895
   319
        case 0x00FF0000:
slouken@1895
   320
            if (Amask == 0xFF000000) {
slouken@1965
   321
                return SDL_PIXELFORMAT_ARGB8888;
slouken@1895
   322
            } else {
slouken@1965
   323
                return SDL_PIXELFORMAT_RGB888;
slouken@1895
   324
            }
slouken@1895
   325
            break;
slouken@1895
   326
        case 0x0000FF00:
slouken@1895
   327
            if (Amask == 0x000000FF) {
slouken@1965
   328
                return SDL_PIXELFORMAT_BGRA8888;
slouken@1895
   329
            }
slouken@1895
   330
            break;
slouken@1895
   331
        case 0x000000FF:
slouken@1895
   332
            if (Amask == 0xFF000000) {
slouken@1965
   333
                return SDL_PIXELFORMAT_ABGR8888;
slouken@1895
   334
            } else {
slouken@1965
   335
                return SDL_PIXELFORMAT_BGR888;
slouken@1895
   336
            }
slouken@1895
   337
            break;
slouken@1895
   338
        case 0x3FF00000:
slouken@1965
   339
            return SDL_PIXELFORMAT_ARGB2101010;
slouken@1895
   340
        }
slouken@1895
   341
    }
slouken@1965
   342
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@1895
   343
}
slouken@1895
   344
slouken@5288
   345
static SDL_PixelFormat *formats;
slouken@1895
   346
slouken@5288
   347
SDL_PixelFormat *
slouken@5288
   348
SDL_AllocFormat(Uint32 pixel_format)
slouken@1895
   349
{
slouken@5288
   350
    SDL_PixelFormat *format;
slouken@1895
   351
slouken@5288
   352
    if (SDL_ISPIXELFORMAT_FOURCC(pixel_format)) {
slouken@5288
   353
        SDL_SetError("FOURCC pixel formats are not supported");
slouken@1895
   354
        return NULL;
slouken@1895
   355
    }
slouken@1895
   356
slouken@5288
   357
    /* Look it up in our list of previously allocated formats */
slouken@5288
   358
    for (format = formats; format; format = format->next) {
slouken@5288
   359
        if (pixel_format == format->format) {
slouken@5288
   360
            ++format->refcount;
slouken@5288
   361
            return format;
slouken@1895
   362
        }
slouken@1895
   363
    }
slouken@1895
   364
slouken@5288
   365
    /* Allocate an empty pixel format structure, and initialize it */
slouken@2967
   366
    format = SDL_malloc(sizeof(*format));
slouken@1895
   367
    if (format == NULL) {
slouken@1895
   368
        SDL_OutOfMemory();
slouken@1895
   369
        return (NULL);
slouken@1895
   370
    }
slouken@5288
   371
    SDL_InitFormat(format, pixel_format);
slouken@0
   372
slouken@5288
   373
    if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
   374
        /* Cache the RGB formats */
slouken@5288
   375
        format->next = formats;
slouken@5288
   376
        formats = format;
slouken@5288
   377
    }
slouken@5288
   378
    return format;
slouken@2967
   379
}
slouken@2967
   380
slouken@5288
   381
int
slouken@5288
   382
SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format)
slouken@2967
   383
{
slouken@5288
   384
    int bpp;
slouken@5288
   385
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@2967
   386
    Uint32 mask;
slouken@2967
   387
slouken@5288
   388
    if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
slouken@5288
   389
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5288
   390
        SDL_SetError("Unknown pixel format");
slouken@5288
   391
        return -1;
slouken@5288
   392
    }
slouken@5288
   393
slouken@2967
   394
    /* Set up the format */
slouken@2967
   395
    SDL_zerop(format);
slouken@5288
   396
    format->format = pixel_format;
slouken@1895
   397
    format->BitsPerPixel = bpp;
slouken@1895
   398
    format->BytesPerPixel = (bpp + 7) / 8;
slouken@1895
   399
    if (Rmask || Bmask || Gmask) {      /* Packed pixels with custom mask */
slouken@1895
   400
        format->Rshift = 0;
slouken@1895
   401
        format->Rloss = 8;
slouken@1895
   402
        if (Rmask) {
slouken@1895
   403
            for (mask = Rmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   404
                ++format->Rshift;
slouken@1895
   405
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   406
                --format->Rloss;
slouken@1895
   407
        }
slouken@1895
   408
        format->Gshift = 0;
slouken@1895
   409
        format->Gloss = 8;
slouken@1895
   410
        if (Gmask) {
slouken@1895
   411
            for (mask = Gmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   412
                ++format->Gshift;
slouken@1895
   413
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   414
                --format->Gloss;
slouken@1895
   415
        }
slouken@1895
   416
        format->Bshift = 0;
slouken@1895
   417
        format->Bloss = 8;
slouken@1895
   418
        if (Bmask) {
slouken@1895
   419
            for (mask = Bmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   420
                ++format->Bshift;
slouken@1895
   421
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   422
                --format->Bloss;
slouken@1895
   423
        }
slouken@1895
   424
        format->Ashift = 0;
slouken@1895
   425
        format->Aloss = 8;
slouken@1895
   426
        if (Amask) {
slouken@1895
   427
            for (mask = Amask; !(mask & 0x01); mask >>= 1)
slouken@1895
   428
                ++format->Ashift;
slouken@1895
   429
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   430
                --format->Aloss;
slouken@1895
   431
        }
slouken@1895
   432
        format->Rmask = Rmask;
slouken@1895
   433
        format->Gmask = Gmask;
slouken@1895
   434
        format->Bmask = Bmask;
slouken@1895
   435
        format->Amask = Amask;
slouken@1895
   436
    } else if (bpp > 8) {       /* Packed pixels with standard mask */
slouken@1895
   437
        /* R-G-B */
slouken@1895
   438
        if (bpp > 24)
slouken@1895
   439
            bpp = 24;
slouken@1895
   440
        format->Rloss = 8 - (bpp / 3);
slouken@1895
   441
        format->Gloss = 8 - (bpp / 3) - (bpp % 3);
slouken@1895
   442
        format->Bloss = 8 - (bpp / 3);
slouken@1895
   443
        format->Rshift = ((bpp / 3) + (bpp % 3)) + (bpp / 3);
slouken@1895
   444
        format->Gshift = (bpp / 3);
slouken@1895
   445
        format->Bshift = 0;
slouken@1895
   446
        format->Rmask = ((0xFF >> format->Rloss) << format->Rshift);
slouken@1895
   447
        format->Gmask = ((0xFF >> format->Gloss) << format->Gshift);
slouken@1895
   448
        format->Bmask = ((0xFF >> format->Bloss) << format->Bshift);
slouken@1895
   449
    } else {
slouken@1895
   450
        /* Palettized formats have no mask info */
slouken@1895
   451
        format->Rloss = 8;
slouken@1895
   452
        format->Gloss = 8;
slouken@1895
   453
        format->Bloss = 8;
slouken@1895
   454
        format->Aloss = 8;
slouken@1895
   455
        format->Rshift = 0;
slouken@1895
   456
        format->Gshift = 0;
slouken@1895
   457
        format->Bshift = 0;
slouken@1895
   458
        format->Ashift = 0;
slouken@1895
   459
        format->Rmask = 0;
slouken@1895
   460
        format->Gmask = 0;
slouken@1895
   461
        format->Bmask = 0;
slouken@1895
   462
        format->Amask = 0;
slouken@1895
   463
    }
slouken@1895
   464
    format->palette = NULL;
slouken@5288
   465
    format->refcount = 1;
slouken@5288
   466
    format->next = NULL;
slouken@1057
   467
slouken@5288
   468
    return 0;
slouken@0
   469
}
slouken@0
   470
slouken@1895
   471
void
slouken@5288
   472
SDL_FreeFormat(SDL_PixelFormat *format)
slouken@0
   473
{
slouken@5288
   474
    SDL_PixelFormat *prev;
slouken@1895
   475
slouken@1895
   476
    if (!format) {
slouken@1895
   477
        return;
slouken@1895
   478
    }
slouken@5288
   479
    if (--format->refcount > 0) {
slouken@5288
   480
        return;
slouken@5288
   481
    }
slouken@5288
   482
slouken@5288
   483
    /* Remove this format from our list */
slouken@5288
   484
    if (format == formats) {
slouken@5288
   485
        formats = format->next;
slouken@5288
   486
    } else if (formats) {
slouken@5288
   487
        for (prev = formats; prev->next; prev = prev->next) {
slouken@5288
   488
            if (prev->next == format) {
slouken@5288
   489
                prev->next = format->next;
slouken@5288
   490
                break;
slouken@5288
   491
            }
slouken@5288
   492
        }
slouken@5288
   493
    }
slouken@5288
   494
slouken@5288
   495
    if (format->palette) {
slouken@5288
   496
        SDL_FreePalette(format->palette);
slouken@5288
   497
    }
slouken@1895
   498
    SDL_free(format);
slouken@0
   499
}
slouken@1895
   500
slouken@5288
   501
SDL_Palette *
slouken@5288
   502
SDL_AllocPalette(int ncolors)
slouken@5288
   503
{
slouken@5288
   504
    SDL_Palette *palette;
slouken@5288
   505
slouken@5288
   506
    palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
slouken@5288
   507
    if (!palette) {
slouken@5288
   508
        SDL_OutOfMemory();
slouken@5288
   509
        return NULL;
slouken@5288
   510
    }
slouken@5288
   511
    palette->colors =
slouken@5288
   512
        (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
slouken@5288
   513
    if (!palette->colors) {
slouken@5288
   514
        SDL_free(palette);
slouken@5288
   515
        return NULL;
slouken@5288
   516
    }
slouken@5288
   517
    palette->ncolors = ncolors;
slouken@5288
   518
    palette->version = 1;
slouken@5288
   519
    palette->refcount = 1;
slouken@5288
   520
slouken@5288
   521
    SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
slouken@5288
   522
slouken@5288
   523
    return palette;
slouken@5288
   524
}
slouken@5288
   525
slouken@5288
   526
int
slouken@5288
   527
SDL_SetPixelFormatPalette(SDL_PixelFormat * format, SDL_Palette *palette)
slouken@5288
   528
{
slouken@5288
   529
    if (!format) {
slouken@5288
   530
        SDL_SetError("SDL_SetPixelFormatPalette() passed NULL format");
slouken@5288
   531
        return -1;
slouken@5288
   532
    }
slouken@5288
   533
slouken@5288
   534
    if (palette && palette->ncolors != (1 << format->BitsPerPixel)) {
slouken@5288
   535
        SDL_SetError("SDL_SetPixelFormatPalette() passed a palette that doesn't match the format");
slouken@5288
   536
        return -1;
slouken@5288
   537
    }
slouken@5288
   538
slouken@5288
   539
    if (format->palette == palette) {
slouken@5288
   540
        return 0;
slouken@5288
   541
    }
slouken@5288
   542
slouken@5288
   543
    if (format->palette) {
slouken@5288
   544
        SDL_FreePalette(format->palette);
slouken@5288
   545
    }
slouken@5288
   546
slouken@5288
   547
    format->palette = palette;
slouken@5288
   548
slouken@5288
   549
    if (format->palette) {
slouken@5288
   550
        ++format->palette->refcount;
slouken@5288
   551
    }
slouken@5288
   552
}
slouken@5288
   553
slouken@5288
   554
int
slouken@5288
   555
SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
slouken@5288
   556
                     int firstcolor, int ncolors)
slouken@5288
   557
{
slouken@5288
   558
    int status = 0;
slouken@5288
   559
slouken@5288
   560
    /* Verify the parameters */
slouken@5288
   561
    if (!palette) {
slouken@5288
   562
        return -1;
slouken@5288
   563
    }
slouken@5288
   564
    if (ncolors > (palette->ncolors - firstcolor)) {
slouken@5288
   565
        ncolors = (palette->ncolors - firstcolor);
slouken@5288
   566
        status = -1;
slouken@5288
   567
    }
slouken@5288
   568
slouken@5288
   569
    if (colors != (palette->colors + firstcolor)) {
slouken@5288
   570
        SDL_memcpy(palette->colors + firstcolor, colors,
slouken@5288
   571
                   ncolors * sizeof(*colors));
slouken@5288
   572
    }
slouken@5288
   573
    ++palette->version;
slouken@5288
   574
    if (!palette->version) {
slouken@5288
   575
        palette->version = 1;
slouken@5288
   576
    }
slouken@5288
   577
slouken@5288
   578
    return status;
slouken@5288
   579
}
slouken@5288
   580
slouken@5288
   581
void
slouken@5288
   582
SDL_FreePalette(SDL_Palette * palette)
slouken@5288
   583
{
slouken@5288
   584
    if (!palette) {
slouken@5288
   585
        return;
slouken@5288
   586
    }
slouken@5288
   587
    if (--palette->refcount > 0) {
slouken@5288
   588
        return;
slouken@5288
   589
    }
slouken@5288
   590
    if (palette->colors) {
slouken@5288
   591
        SDL_free(palette->colors);
slouken@5288
   592
    }
slouken@5288
   593
    SDL_free(palette);
slouken@5288
   594
}
slouken@5288
   595
slouken@0
   596
/*
slouken@0
   597
 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
slouken@0
   598
 */
slouken@1895
   599
void
slouken@1895
   600
SDL_DitherColors(SDL_Color * colors, int bpp)
slouken@0
   601
{
slouken@1895
   602
    int i;
slouken@1895
   603
    if (bpp != 8)
slouken@1895
   604
        return;                 /* only 8bpp supported right now */
slouken@0
   605
slouken@1895
   606
    for (i = 0; i < 256; i++) {
slouken@1895
   607
        int r, g, b;
slouken@1895
   608
        /* map each bit field to the full [0, 255] interval,
slouken@1895
   609
           so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
slouken@1895
   610
        r = i & 0xe0;
slouken@1895
   611
        r |= r >> 3 | r >> 6;
slouken@1895
   612
        colors[i].r = r;
slouken@1895
   613
        g = (i << 3) & 0xe0;
slouken@1895
   614
        g |= g >> 3 | g >> 6;
slouken@1895
   615
        colors[i].g = g;
slouken@1895
   616
        b = i & 0x3;
slouken@1895
   617
        b |= b << 2;
slouken@1895
   618
        b |= b << 4;
slouken@1895
   619
        colors[i].b = b;
slouken@1895
   620
        colors[i].unused = SDL_ALPHA_OPAQUE;
slouken@1895
   621
    }
slouken@0
   622
}
slouken@1895
   623
slouken@0
   624
/* 
slouken@0
   625
 * Calculate the pad-aligned scanline width of a surface
slouken@0
   626
 */
slouken@1895
   627
int
slouken@1895
   628
SDL_CalculatePitch(SDL_Surface * surface)
slouken@0
   629
{
slouken@1895
   630
    int pitch;
slouken@0
   631
slouken@1895
   632
    /* Surface should be 4-byte aligned for speed */
slouken@1895
   633
    pitch = surface->w * surface->format->BytesPerPixel;
slouken@1895
   634
    switch (surface->format->BitsPerPixel) {
slouken@1895
   635
    case 1:
slouken@1895
   636
        pitch = (pitch + 7) / 8;
slouken@1895
   637
        break;
slouken@1895
   638
    case 4:
slouken@1895
   639
        pitch = (pitch + 1) / 2;
slouken@1895
   640
        break;
slouken@1895
   641
    default:
slouken@1895
   642
        break;
slouken@1895
   643
    }
slouken@1895
   644
    pitch = (pitch + 3) & ~3;   /* 4-byte aligning */
slouken@1895
   645
    return (pitch);
slouken@0
   646
}
slouken@1895
   647
slouken@0
   648
/*
slouken@0
   649
 * Match an RGB value to a particular palette index
slouken@0
   650
 */
slouken@1895
   651
Uint8
slouken@1895
   652
SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   653
{
slouken@1895
   654
    /* Do colorspace distance matching */
slouken@1895
   655
    unsigned int smallest;
slouken@1895
   656
    unsigned int distance;
slouken@1895
   657
    int rd, gd, bd;
slouken@1895
   658
    int i;
slouken@1895
   659
    Uint8 pixel = 0;
slouken@1895
   660
slouken@1895
   661
    smallest = ~0;
slouken@1895
   662
    for (i = 0; i < pal->ncolors; ++i) {
slouken@1895
   663
        rd = pal->colors[i].r - r;
slouken@1895
   664
        gd = pal->colors[i].g - g;
slouken@1895
   665
        bd = pal->colors[i].b - b;
slouken@1895
   666
        distance = (rd * rd) + (gd * gd) + (bd * bd);
slouken@1895
   667
        if (distance < smallest) {
slouken@1895
   668
            pixel = i;
slouken@1895
   669
            if (distance == 0) {        /* Perfect match! */
slouken@1895
   670
                break;
slouken@1895
   671
            }
slouken@1895
   672
            smallest = distance;
slouken@1895
   673
        }
slouken@1895
   674
    }
slouken@1895
   675
    return (pixel);
slouken@0
   676
}
slouken@0
   677
slouken@0
   678
/* Find the opaque pixel value corresponding to an RGB triple */
slouken@1895
   679
Uint32
slouken@2275
   680
SDL_MapRGB(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   681
{
slouken@1895
   682
    if (format->palette == NULL) {
slouken@1895
   683
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   684
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   685
            | (b >> format->Bloss) << format->Bshift | format->Amask;
slouken@1895
   686
    } else {
slouken@1895
   687
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   688
    }
slouken@0
   689
}
slouken@0
   690
slouken@0
   691
/* Find the pixel value corresponding to an RGBA quadruple */
slouken@1895
   692
Uint32
slouken@2275
   693
SDL_MapRGBA(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b,
slouken@2275
   694
            Uint8 a)
slouken@0
   695
{
slouken@1895
   696
    if (format->palette == NULL) {
slouken@1895
   697
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   698
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   699
            | (b >> format->Bloss) << format->Bshift
slouken@1895
   700
            | ((a >> format->Aloss) << format->Ashift & format->Amask);
slouken@1895
   701
    } else {
slouken@1895
   702
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   703
    }
slouken@0
   704
}
slouken@0
   705
slouken@1895
   706
void
slouken@2275
   707
SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
slouken@2275
   708
           Uint8 * b)
slouken@2275
   709
{
slouken@2275
   710
    if (format->palette == NULL) {
slouken@2275
   711
        /*
slouken@2275
   712
         * This makes sure that the result is mapped to the
slouken@2275
   713
         * interval [0..255], and the maximum value for each
slouken@2275
   714
         * component is 255. This is important to make sure
slouken@2275
   715
         * that white is indeed reported as (255, 255, 255).
slouken@2275
   716
         * This only works for RGB bit fields at least 4 bit
slouken@2275
   717
         * wide, which is almost always the case.
slouken@2275
   718
         */
slouken@2275
   719
        unsigned v;
slouken@2275
   720
        v = (pixel & format->Rmask) >> format->Rshift;
slouken@2275
   721
        *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
slouken@2275
   722
        v = (pixel & format->Gmask) >> format->Gshift;
slouken@2275
   723
        *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
slouken@2275
   724
        v = (pixel & format->Bmask) >> format->Bshift;
slouken@2275
   725
        *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
slouken@2275
   726
    } else {
slouken@2275
   727
        *r = format->palette->colors[pixel].r;
slouken@2275
   728
        *g = format->palette->colors[pixel].g;
slouken@2275
   729
        *b = format->palette->colors[pixel].b;
slouken@2275
   730
    }
slouken@2275
   731
}
slouken@2275
   732
slouken@2275
   733
void
slouken@2275
   734
SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
slouken@1895
   735
            Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@0
   736
{
slouken@2275
   737
    if (format->palette == NULL) {
slouken@1895
   738
        /*
slouken@1895
   739
         * This makes sure that the result is mapped to the
slouken@1895
   740
         * interval [0..255], and the maximum value for each
slouken@1895
   741
         * component is 255. This is important to make sure
slouken@1895
   742
         * that white is indeed reported as (255, 255, 255),
slouken@1895
   743
         * and that opaque alpha is 255.
slouken@1895
   744
         * This only works for RGB bit fields at least 4 bit
slouken@1895
   745
         * wide, which is almost always the case.
slouken@1895
   746
         */
slouken@1895
   747
        unsigned v;
slouken@2275
   748
        v = (pixel & format->Rmask) >> format->Rshift;
slouken@2275
   749
        *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
slouken@2275
   750
        v = (pixel & format->Gmask) >> format->Gshift;
slouken@2275
   751
        *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
slouken@2275
   752
        v = (pixel & format->Bmask) >> format->Bshift;
slouken@2275
   753
        *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
slouken@2275
   754
        if (format->Amask) {
slouken@2275
   755
            v = (pixel & format->Amask) >> format->Ashift;
slouken@2275
   756
            *a = (v << format->Aloss) + (v >> (8 - (format->Aloss << 1)));
slouken@1895
   757
        } else {
slouken@1895
   758
            *a = SDL_ALPHA_OPAQUE;
slouken@1895
   759
        }
slouken@1895
   760
    } else {
slouken@2275
   761
        *r = format->palette->colors[pixel].r;
slouken@2275
   762
        *g = format->palette->colors[pixel].g;
slouken@2275
   763
        *b = format->palette->colors[pixel].b;
slouken@1895
   764
        *a = SDL_ALPHA_OPAQUE;
slouken@1895
   765
    }
slouken@0
   766
}
slouken@0
   767
slouken@0
   768
/* Map from Palette to Palette */
slouken@1895
   769
static Uint8 *
slouken@1895
   770
Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
slouken@0
   771
{
slouken@1895
   772
    Uint8 *map;
slouken@1895
   773
    int i;
slouken@0
   774
slouken@1895
   775
    if (identical) {
slouken@1895
   776
        if (src->ncolors <= dst->ncolors) {
slouken@1895
   777
            /* If an identical palette, no need to map */
slouken@1895
   778
            if (src == dst
slouken@1895
   779
                ||
slouken@1895
   780
                (SDL_memcmp
slouken@1895
   781
                 (src->colors, dst->colors,
slouken@1895
   782
                  src->ncolors * sizeof(SDL_Color)) == 0)) {
slouken@1895
   783
                *identical = 1;
slouken@1895
   784
                return (NULL);
slouken@1895
   785
            }
slouken@1895
   786
        }
slouken@1895
   787
        *identical = 0;
slouken@1895
   788
    }
slouken@1895
   789
    map = (Uint8 *) SDL_malloc(src->ncolors);
slouken@1895
   790
    if (map == NULL) {
slouken@1895
   791
        SDL_OutOfMemory();
slouken@1895
   792
        return (NULL);
slouken@1895
   793
    }
slouken@1895
   794
    for (i = 0; i < src->ncolors; ++i) {
slouken@1895
   795
        map[i] = SDL_FindColor(dst,
slouken@1895
   796
                               src->colors[i].r, src->colors[i].g,
slouken@1895
   797
                               src->colors[i].b);
slouken@1895
   798
    }
slouken@1895
   799
    return (map);
slouken@0
   800
}
slouken@0
   801
slouken@1895
   802
/* Map from Palette to BitField */
slouken@1895
   803
static Uint8 *
slouken@2267
   804
Map1toN(SDL_PixelFormat * src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod,
slouken@2267
   805
        SDL_PixelFormat * dst)
slouken@0
   806
{
slouken@1895
   807
    Uint8 *map;
slouken@1895
   808
    int i;
slouken@1895
   809
    int bpp;
slouken@1895
   810
    SDL_Palette *pal = src->palette;
slouken@0
   811
slouken@1895
   812
    bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
slouken@1895
   813
    map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
slouken@1895
   814
    if (map == NULL) {
slouken@1895
   815
        SDL_OutOfMemory();
slouken@1895
   816
        return (NULL);
slouken@1895
   817
    }
slouken@0
   818
slouken@1895
   819
    /* We memory copy to the pixel map so the endianness is preserved */
slouken@1895
   820
    for (i = 0; i < pal->ncolors; ++i) {
slouken@2257
   821
        Uint8 A = Amod;
slouken@2267
   822
        Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
slouken@2267
   823
        Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
slouken@2267
   824
        Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
slouken@2257
   825
        ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
slouken@1895
   826
    }
slouken@1895
   827
    return (map);
slouken@1895
   828
}
slouken@0
   829
slouken@1895
   830
/* Map from BitField to Dithered-Palette to Palette */
slouken@1895
   831
static Uint8 *
slouken@1895
   832
MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical)
slouken@1895
   833
{
slouken@1895
   834
    /* Generate a 256 color dither palette */
slouken@1895
   835
    SDL_Palette dithered;
slouken@1895
   836
    SDL_Color colors[256];
slouken@1895
   837
    SDL_Palette *pal = dst->palette;
slouken@1895
   838
slouken@1895
   839
    dithered.ncolors = 256;
slouken@1895
   840
    SDL_DitherColors(colors, 8);
slouken@1895
   841
    dithered.colors = colors;
slouken@1895
   842
    return (Map1to1(&dithered, pal, identical));
slouken@0
   843
}
slouken@1895
   844
slouken@1895
   845
SDL_BlitMap *
slouken@1895
   846
SDL_AllocBlitMap(void)
slouken@0
   847
{
slouken@1895
   848
    SDL_BlitMap *map;
slouken@1895
   849
slouken@1895
   850
    /* Allocate the empty map */
slouken@1920
   851
    map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
slouken@1895
   852
    if (map == NULL) {
slouken@1895
   853
        SDL_OutOfMemory();
slouken@1895
   854
        return (NULL);
slouken@1895
   855
    }
slouken@2267
   856
    map->info.r = 0xFF;
slouken@2267
   857
    map->info.g = 0xFF;
slouken@2267
   858
    map->info.b = 0xFF;
slouken@2267
   859
    map->info.a = 0xFF;
slouken@1895
   860
slouken@1895
   861
    /* It's ready to go */
slouken@1895
   862
    return (map);
slouken@0
   863
}
slouken@1895
   864
slouken@1895
   865
void
slouken@1895
   866
SDL_InvalidateMap(SDL_BlitMap * map)
slouken@0
   867
{
slouken@1895
   868
    if (!map) {
slouken@1895
   869
        return;
slouken@1895
   870
    }
slouken@1895
   871
    map->dst = NULL;
slouken@5288
   872
    map->palette_version = 0;
slouken@2267
   873
    if (map->info.table) {
slouken@2267
   874
        SDL_free(map->info.table);
slouken@2267
   875
        map->info.table = NULL;
slouken@1895
   876
    }
slouken@1895
   877
}
slouken@2735
   878
slouken@1895
   879
int
slouken@1895
   880
SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)
slouken@1895
   881
{
slouken@1895
   882
    SDL_PixelFormat *srcfmt;
slouken@1895
   883
    SDL_PixelFormat *dstfmt;
slouken@1895
   884
    SDL_BlitMap *map;
slouken@0
   885
slouken@1895
   886
    /* Clear out any previous mapping */
slouken@1895
   887
    map = src->map;
slouken@1895
   888
    if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   889
        SDL_UnRLESurface(src, 1);
slouken@1895
   890
    }
slouken@1895
   891
    SDL_InvalidateMap(map);
slouken@0
   892
slouken@1895
   893
    /* Figure out what kind of mapping we're doing */
slouken@1895
   894
    map->identity = 0;
slouken@1895
   895
    srcfmt = src->format;
slouken@1895
   896
    dstfmt = dst->format;
slouken@5288
   897
    if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
slouken@5288
   898
        if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
slouken@1895
   899
            /* Palette --> Palette */
slouken@2267
   900
            map->info.table =
slouken@1895
   901
                Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
slouken@1895
   902
            if (!map->identity) {
slouken@2267
   903
                if (map->info.table == NULL) {
slouken@1895
   904
                    return (-1);
slouken@1895
   905
                }
slouken@1895
   906
            }
slouken@1895
   907
            if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
slouken@1895
   908
                map->identity = 0;
slouken@5288
   909
        } else {
slouken@1895
   910
            /* Palette --> BitField */
slouken@2267
   911
            map->info.table =
slouken@2267
   912
                Map1toN(srcfmt, src->map->info.r, src->map->info.g,
slouken@2267
   913
                        src->map->info.b, src->map->info.a, dstfmt);
slouken@2267
   914
            if (map->info.table == NULL) {
slouken@1895
   915
                return (-1);
slouken@1895
   916
            }
slouken@1895
   917
        }
slouken@5288
   918
    } else {
slouken@5288
   919
        if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
slouken@1895
   920
            /* BitField --> Palette */
slouken@2267
   921
            map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
slouken@1895
   922
            if (!map->identity) {
slouken@2267
   923
                if (map->info.table == NULL) {
slouken@1895
   924
                    return (-1);
slouken@1895
   925
                }
slouken@1895
   926
            }
slouken@1895
   927
            map->identity = 0;  /* Don't optimize to copy */
slouken@5288
   928
        } else {
slouken@1895
   929
            /* BitField --> BitField */
slouken@5288
   930
            if (srcfmt == dstfmt) {
slouken@1895
   931
                map->identity = 1;
slouken@5288
   932
            }
slouken@1895
   933
        }
slouken@1895
   934
    }
slouken@0
   935
slouken@1895
   936
    map->dst = dst;
slouken@5288
   937
slouken@5288
   938
    if (dstfmt->palette) {
slouken@5288
   939
        map->palette_version = dstfmt->palette->version;
slouken@5288
   940
    } else {
slouken@5288
   941
        map->palette_version = 0;
slouken@5288
   942
    }
slouken@0
   943
slouken@1895
   944
    /* Choose your blitters wisely */
slouken@1895
   945
    return (SDL_CalculateBlit(src));
slouken@0
   946
}
slouken@1895
   947
slouken@1895
   948
void
slouken@1895
   949
SDL_FreeBlitMap(SDL_BlitMap * map)
slouken@0
   950
{
slouken@1895
   951
    if (map) {
slouken@1895
   952
        SDL_InvalidateMap(map);
slouken@1895
   953
        SDL_free(map);
slouken@1895
   954
    }
slouken@0
   955
}
slouken@1895
   956
slouken@1895
   957
/* vi: set ts=4 sw=4 expandtab: */