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