src/video/SDL_pixels.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 06 Mar 2011 21:47:48 -0800
changeset 5432 b3913347f1d7
parent 5431 5369d8f32f08
child 5433 1b6eee840e26
permissions -rw-r--r--
Fixed garbage trying to create a FourCC pixel format.
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@5432
    86
    /* This function doesn't work with FourCC pixel formats */
slouken@5432
    87
    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
slouken@5432
    88
        SDL_SetError("Unknown pixel format");
slouken@5432
    89
        return SDL_FALSE;
slouken@5432
    90
    }
slouken@5432
    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@5350
   245
        if (Rmask == 0) {
slouken@1965
   246
            return SDL_PIXELFORMAT_INDEX8;
slouken@5350
   247
        }
slouken@5350
   248
        if (Rmask == 0xE0 &&
slouken@5350
   249
            Gmask == 0x1C &&
slouken@5350
   250
            Bmask == 0x03 &&
slouken@5350
   251
            Amask == 0x00) {
slouken@5352
   252
            return SDL_PIXELFORMAT_RGB332;
slouken@1895
   253
        }
slouken@1895
   254
        break;
slouken@1895
   255
    case 12:
slouken@5350
   256
        if (Rmask == 0) {
slouken@5352
   257
            return SDL_PIXELFORMAT_RGB444;
slouken@5350
   258
        }
slouken@5350
   259
        if (Rmask == 0x0F00 &&
slouken@5350
   260
            Gmask == 0x00F0 &&
slouken@5350
   261
            Bmask == 0x000F &&
slouken@5350
   262
            Amask == 0x0000) {
slouken@5352
   263
            return SDL_PIXELFORMAT_RGB444;
slouken@1895
   264
        }
slouken@1895
   265
        break;
slouken@1895
   266
    case 15:
slouken@5288
   267
        if (Rmask == 0) {
slouken@5288
   268
            return SDL_PIXELFORMAT_RGB555;
slouken@5288
   269
        }
slouken@5350
   270
        /* Fall through to 16-bit checks */
slouken@1895
   271
    case 16:
slouken@5350
   272
        if (Rmask == 0) {
slouken@5288
   273
            return SDL_PIXELFORMAT_RGB565;
slouken@5350
   274
        }
slouken@5350
   275
        if (Rmask == 0x7C00 &&
slouken@5350
   276
            Gmask == 0x03E0 &&
slouken@5350
   277
            Bmask == 0x001F &&
slouken@5350
   278
            Amask == 0x0000) {
slouken@5352
   279
            return SDL_PIXELFORMAT_RGB555;
slouken@5350
   280
        }
slouken@5350
   281
        if (Rmask == 0x001F &&
slouken@5350
   282
            Gmask == 0x03E0 &&
slouken@5350
   283
            Bmask == 0x7C00 &&
slouken@5350
   284
            Amask == 0x0000) {
slouken@5352
   285
            return SDL_PIXELFORMAT_BGR555;
slouken@5350
   286
        }
slouken@5350
   287
        if (Rmask == 0x0F00 &&
slouken@5350
   288
            Gmask == 0x00F0 &&
slouken@5350
   289
            Bmask == 0x000F &&
slouken@5350
   290
            Amask == 0xF000) {
slouken@5352
   291
            return SDL_PIXELFORMAT_ARGB4444;
slouken@5350
   292
        }
slouken@5350
   293
        if (Rmask == 0xF000 &&
slouken@5350
   294
            Gmask == 0x0F00 &&
slouken@5350
   295
            Bmask == 0x00F0 &&
slouken@5350
   296
            Amask == 0x000F) {
slouken@5352
   297
            return SDL_PIXELFORMAT_RGBA4444;
slouken@5350
   298
        }
slouken@5350
   299
        if (Rmask == 0x000F &&
slouken@5350
   300
            Gmask == 0x00F0 &&
slouken@5350
   301
            Bmask == 0x0F00 &&
slouken@5350
   302
            Amask == 0xF000) {
slouken@5352
   303
            return SDL_PIXELFORMAT_ABGR4444;
slouken@5350
   304
        }
slouken@5350
   305
        if (Rmask == 0x00F0 &&
slouken@5350
   306
            Gmask == 0x0F00 &&
slouken@5350
   307
            Bmask == 0xF000 &&
slouken@5350
   308
            Amask == 0x000F) {
slouken@5352
   309
            return SDL_PIXELFORMAT_BGRA4444;
slouken@5350
   310
        }
slouken@5350
   311
        if (Rmask == 0x7C00 &&
slouken@5350
   312
            Gmask == 0x03E0 &&
slouken@5350
   313
            Bmask == 0x001F &&
slouken@5350
   314
            Amask == 0x8000) {
slouken@5352
   315
            return SDL_PIXELFORMAT_ARGB1555;
slouken@5350
   316
        }
slouken@5350
   317
        if (Rmask == 0xF800 &&
slouken@5350
   318
            Gmask == 0x07C0 &&
slouken@5350
   319
            Bmask == 0x003E &&
slouken@5350
   320
            Amask == 0x0001) {
slouken@5352
   321
            return SDL_PIXELFORMAT_RGBA5551;
slouken@5350
   322
        }
slouken@5350
   323
        if (Rmask == 0x001F &&
slouken@5350
   324
            Gmask == 0x03E0 &&
slouken@5350
   325
            Bmask == 0x7C00 &&
slouken@5350
   326
            Amask == 0x8000) {
slouken@5352
   327
            return SDL_PIXELFORMAT_ABGR1555;
slouken@5350
   328
        }
slouken@5350
   329
        if (Rmask == 0x003E &&
slouken@5350
   330
            Gmask == 0x07C0 &&
slouken@5350
   331
            Bmask == 0xF800 &&
slouken@5350
   332
            Amask == 0x0001) {
slouken@5352
   333
            return SDL_PIXELFORMAT_BGRA5551;
slouken@5350
   334
        }
slouken@5350
   335
        if (Rmask == 0xF800 &&
slouken@5350
   336
            Gmask == 0x07E0 &&
slouken@5350
   337
            Bmask == 0x001F &&
slouken@5350
   338
            Amask == 0x0000) {
slouken@5352
   339
            return SDL_PIXELFORMAT_RGB565;
slouken@5350
   340
        }
slouken@5350
   341
        if (Rmask == 0x001F &&
slouken@5350
   342
            Gmask == 0x07E0 &&
slouken@5350
   343
            Bmask == 0xF800 &&
slouken@5350
   344
            Amask == 0x0000) {
slouken@5352
   345
            return SDL_PIXELFORMAT_BGR565;
slouken@1895
   346
        }
slouken@1895
   347
        break;
lestat@3182
   348
    case 24:
lestat@3182
   349
        switch (Rmask) {
slouken@5288
   350
        case 0:
lestat@3182
   351
        case 0x00FF0000:
slouken@3597
   352
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3597
   353
            return SDL_PIXELFORMAT_RGB24;
slouken@3597
   354
#else
slouken@3597
   355
            return SDL_PIXELFORMAT_BGR24;
slouken@3597
   356
#endif
lestat@3182
   357
        case 0x000000FF:
slouken@3597
   358
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@3597
   359
            return SDL_PIXELFORMAT_BGR24;
slouken@3597
   360
#else
slouken@3597
   361
            return SDL_PIXELFORMAT_RGB24;
slouken@3597
   362
#endif
lestat@3182
   363
        }
slouken@1895
   364
    case 32:
slouken@5350
   365
        if (Rmask == 0) {
slouken@5350
   366
            return SDL_PIXELFORMAT_RGB888;
slouken@5350
   367
        }
slouken@5350
   368
        if (Rmask == 0x00FF0000 &&
slouken@5350
   369
            Gmask == 0x0000FF00 &&
slouken@5350
   370
            Bmask == 0x000000FF &&
slouken@5350
   371
            Amask == 0x00000000) {
slouken@5352
   372
            return SDL_PIXELFORMAT_RGB888;
slouken@5350
   373
        }
slouken@5350
   374
        if (Rmask == 0x000000FF &&
slouken@5350
   375
            Gmask == 0x0000FF00 &&
slouken@5350
   376
            Bmask == 0x00FF0000 &&
slouken@5350
   377
            Amask == 0x00000000) {
slouken@5352
   378
            return SDL_PIXELFORMAT_BGR888;
slouken@5350
   379
        }
slouken@5350
   380
        if (Rmask == 0x00FF0000 &&
slouken@5350
   381
            Gmask == 0x0000FF00 &&
slouken@5350
   382
            Bmask == 0x000000FF &&
slouken@5350
   383
            Amask == 0xFF000000) {
slouken@5352
   384
            return SDL_PIXELFORMAT_ARGB8888;
slouken@5350
   385
        }
slouken@5350
   386
        if (Rmask == 0xFF000000 &&
slouken@5350
   387
            Gmask == 0x00FF0000 &&
slouken@5350
   388
            Bmask == 0x0000FF00 &&
slouken@5350
   389
            Amask == 0x000000FF) {
slouken@5352
   390
            return SDL_PIXELFORMAT_RGBA8888;
slouken@5350
   391
        }
slouken@5350
   392
        if (Rmask == 0x000000FF &&
slouken@5350
   393
            Gmask == 0x0000FF00 &&
slouken@5350
   394
            Bmask == 0x00FF0000 &&
slouken@5350
   395
            Amask == 0xFF000000) {
slouken@5352
   396
            return SDL_PIXELFORMAT_ABGR8888;
slouken@5350
   397
        }
slouken@5350
   398
        if (Rmask == 0x0000FF00 &&
slouken@5350
   399
            Gmask == 0x00FF0000 &&
slouken@5350
   400
            Bmask == 0xFF000000 &&
slouken@5350
   401
            Amask == 0x000000FF) {
slouken@5352
   402
            return SDL_PIXELFORMAT_BGRA8888;
slouken@5350
   403
        }
slouken@5350
   404
        if (Rmask == 0x3FF00000 &&
slouken@5350
   405
            Gmask == 0x000FFC00 &&
slouken@5350
   406
            Bmask == 0x000003FF &&
slouken@5350
   407
            Amask == 0xC0000000) {
slouken@5352
   408
            return SDL_PIXELFORMAT_ARGB2101010;
slouken@1895
   409
        }
slouken@1895
   410
    }
slouken@1965
   411
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@1895
   412
}
slouken@1895
   413
slouken@5288
   414
static SDL_PixelFormat *formats;
slouken@1895
   415
slouken@5288
   416
SDL_PixelFormat *
slouken@5288
   417
SDL_AllocFormat(Uint32 pixel_format)
slouken@5288
   418
{
slouken@5288
   419
    SDL_PixelFormat *format;
slouken@5288
   420
slouken@5288
   421
    if (SDL_ISPIXELFORMAT_FOURCC(pixel_format)) {
slouken@5288
   422
        SDL_SetError("FOURCC pixel formats are not supported");
slouken@1895
   423
        return NULL;
slouken@1895
   424
    }
slouken@1895
   425
slouken@5288
   426
    /* Look it up in our list of previously allocated formats */
slouken@5288
   427
    for (format = formats; format; format = format->next) {
slouken@5288
   428
        if (pixel_format == format->format) {
slouken@5288
   429
            ++format->refcount;
slouken@5288
   430
            return format;
slouken@1895
   431
        }
slouken@1895
   432
    }
slouken@1895
   433
slouken@5288
   434
    /* Allocate an empty pixel format structure, and initialize it */
slouken@2967
   435
    format = SDL_malloc(sizeof(*format));
slouken@1895
   436
    if (format == NULL) {
slouken@1895
   437
        SDL_OutOfMemory();
slouken@1895
   438
        return (NULL);
slouken@1895
   439
    }
slouken@5288
   440
    SDL_InitFormat(format, pixel_format);
slouken@0
   441
slouken@5288
   442
    if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
   443
        /* Cache the RGB formats */
slouken@5288
   444
        format->next = formats;
slouken@5288
   445
        formats = format;
slouken@5288
   446
    }
slouken@5288
   447
    return format;
slouken@2967
   448
}
slouken@2967
   449
slouken@5288
   450
int
slouken@5288
   451
SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format)
slouken@2967
   452
{
slouken@5432
   453
    int bpp;
slouken@5432
   454
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@5432
   455
    Uint32 mask;
slouken@5432
   456
slouken@5432
   457
    if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
slouken@5432
   458
                                    &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@5432
   459
        SDL_SetError("Unknown pixel format");
slouken@5432
   460
        return -1;
slouken@5432
   461
    }
slouken@5432
   462
slouken@2967
   463
    /* Set up the format */
slouken@2967
   464
    SDL_zerop(format);
slouken@5288
   465
    format->format = pixel_format;
slouken@5432
   466
    format->BitsPerPixel = bpp;
slouken@5432
   467
    format->BytesPerPixel = (bpp + 7) / 8;
slouken@5431
   468
slouken@5432
   469
    format->Rmask = Rmask;
slouken@5432
   470
    format->Rshift = 0;
slouken@5432
   471
    format->Rloss = 8;
slouken@5432
   472
    if (Rmask) {
slouken@5432
   473
        for (mask = Rmask; !(mask & 0x01); mask >>= 1)
slouken@5432
   474
            ++format->Rshift;
slouken@5432
   475
        for (; (mask & 0x01); mask >>= 1)
slouken@5432
   476
            --format->Rloss;
slouken@5432
   477
    }
slouken@5431
   478
slouken@5432
   479
    format->Gmask = Gmask;
slouken@5432
   480
    format->Gshift = 0;
slouken@5432
   481
    format->Gloss = 8;
slouken@5432
   482
    if (Gmask) {
slouken@5432
   483
        for (mask = Gmask; !(mask & 0x01); mask >>= 1)
slouken@5432
   484
            ++format->Gshift;
slouken@5432
   485
        for (; (mask & 0x01); mask >>= 1)
slouken@5432
   486
            --format->Gloss;
slouken@1895
   487
    }
slouken@5432
   488
slouken@5432
   489
    format->Bmask = Bmask;
slouken@5432
   490
    format->Bshift = 0;
slouken@5432
   491
    format->Bloss = 8;
slouken@5432
   492
    if (Bmask) {
slouken@5432
   493
        for (mask = Bmask; !(mask & 0x01); mask >>= 1)
slouken@5432
   494
            ++format->Bshift;
slouken@5432
   495
        for (; (mask & 0x01); mask >>= 1)
slouken@5432
   496
            --format->Bloss;
slouken@5432
   497
    }
slouken@5432
   498
slouken@5432
   499
    format->Amask = Amask;
slouken@5432
   500
    format->Ashift = 0;
slouken@5432
   501
    format->Aloss = 8;
slouken@5432
   502
    if (Amask) {
slouken@5432
   503
        for (mask = Amask; !(mask & 0x01); mask >>= 1)
slouken@5432
   504
            ++format->Ashift;
slouken@5432
   505
        for (; (mask & 0x01); mask >>= 1)
slouken@5432
   506
            --format->Aloss;
slouken@5432
   507
    }
slouken@5432
   508
slouken@1895
   509
    format->palette = NULL;
slouken@5288
   510
    format->refcount = 1;
slouken@5288
   511
    format->next = NULL;
slouken@1057
   512
slouken@5288
   513
    return 0;
slouken@0
   514
}
slouken@0
   515
slouken@1895
   516
void
slouken@5288
   517
SDL_FreeFormat(SDL_PixelFormat *format)
slouken@0
   518
{
slouken@5288
   519
    SDL_PixelFormat *prev;
slouken@1895
   520
slouken@1895
   521
    if (!format) {
slouken@1895
   522
        return;
slouken@1895
   523
    }
slouken@5288
   524
    if (--format->refcount > 0) {
slouken@5288
   525
        return;
slouken@5288
   526
    }
slouken@5288
   527
slouken@5288
   528
    /* Remove this format from our list */
slouken@5288
   529
    if (format == formats) {
slouken@5288
   530
        formats = format->next;
slouken@5288
   531
    } else if (formats) {
slouken@5288
   532
        for (prev = formats; prev->next; prev = prev->next) {
slouken@5288
   533
            if (prev->next == format) {
slouken@5288
   534
                prev->next = format->next;
slouken@5288
   535
                break;
slouken@5288
   536
            }
slouken@5288
   537
        }
slouken@5288
   538
    }
slouken@5288
   539
slouken@5288
   540
    if (format->palette) {
slouken@5288
   541
        SDL_FreePalette(format->palette);
slouken@5288
   542
    }
slouken@1895
   543
    SDL_free(format);
slouken@0
   544
}
slouken@1895
   545
slouken@5288
   546
SDL_Palette *
slouken@5288
   547
SDL_AllocPalette(int ncolors)
slouken@5288
   548
{
slouken@5288
   549
    SDL_Palette *palette;
slouken@5288
   550
slouken@5288
   551
    palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
slouken@5288
   552
    if (!palette) {
slouken@5288
   553
        SDL_OutOfMemory();
slouken@5288
   554
        return NULL;
slouken@5288
   555
    }
slouken@5288
   556
    palette->colors =
slouken@5288
   557
        (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
slouken@5288
   558
    if (!palette->colors) {
slouken@5288
   559
        SDL_free(palette);
slouken@5288
   560
        return NULL;
slouken@5288
   561
    }
slouken@5288
   562
    palette->ncolors = ncolors;
slouken@5288
   563
    palette->version = 1;
slouken@5288
   564
    palette->refcount = 1;
slouken@5288
   565
slouken@5288
   566
    SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
slouken@5288
   567
slouken@5288
   568
    return palette;
slouken@5288
   569
}
slouken@5288
   570
slouken@5288
   571
int
slouken@5288
   572
SDL_SetPixelFormatPalette(SDL_PixelFormat * format, SDL_Palette *palette)
slouken@5288
   573
{
slouken@5288
   574
    if (!format) {
slouken@5288
   575
        SDL_SetError("SDL_SetPixelFormatPalette() passed NULL format");
slouken@5288
   576
        return -1;
slouken@5288
   577
    }
slouken@5288
   578
slouken@5288
   579
    if (palette && palette->ncolors != (1 << format->BitsPerPixel)) {
slouken@5288
   580
        SDL_SetError("SDL_SetPixelFormatPalette() passed a palette that doesn't match the format");
slouken@5288
   581
        return -1;
slouken@5288
   582
    }
slouken@5288
   583
slouken@5288
   584
    if (format->palette == palette) {
slouken@5288
   585
        return 0;
slouken@5288
   586
    }
slouken@5288
   587
slouken@5288
   588
    if (format->palette) {
slouken@5288
   589
        SDL_FreePalette(format->palette);
slouken@5288
   590
    }
slouken@5288
   591
slouken@5288
   592
    format->palette = palette;
slouken@5288
   593
slouken@5288
   594
    if (format->palette) {
slouken@5288
   595
        ++format->palette->refcount;
slouken@5288
   596
    }
slouken@5295
   597
slouken@5295
   598
    return 0;
slouken@5288
   599
}
slouken@5288
   600
slouken@5288
   601
int
slouken@5288
   602
SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
slouken@5288
   603
                     int firstcolor, int ncolors)
slouken@5288
   604
{
slouken@5288
   605
    int status = 0;
slouken@5288
   606
slouken@5288
   607
    /* Verify the parameters */
slouken@5288
   608
    if (!palette) {
slouken@5288
   609
        return -1;
slouken@5288
   610
    }
slouken@5288
   611
    if (ncolors > (palette->ncolors - firstcolor)) {
slouken@5288
   612
        ncolors = (palette->ncolors - firstcolor);
slouken@5288
   613
        status = -1;
slouken@5288
   614
    }
slouken@5288
   615
slouken@5288
   616
    if (colors != (palette->colors + firstcolor)) {
slouken@5288
   617
        SDL_memcpy(palette->colors + firstcolor, colors,
slouken@5288
   618
                   ncolors * sizeof(*colors));
slouken@5288
   619
    }
slouken@5288
   620
    ++palette->version;
slouken@5288
   621
    if (!palette->version) {
slouken@5288
   622
        palette->version = 1;
slouken@5288
   623
    }
slouken@5288
   624
slouken@5288
   625
    return status;
slouken@5288
   626
}
slouken@5288
   627
slouken@5288
   628
void
slouken@5288
   629
SDL_FreePalette(SDL_Palette * palette)
slouken@5288
   630
{
slouken@5288
   631
    if (!palette) {
slouken@5288
   632
        return;
slouken@5288
   633
    }
slouken@5288
   634
    if (--palette->refcount > 0) {
slouken@5288
   635
        return;
slouken@5288
   636
    }
slouken@5288
   637
    if (palette->colors) {
slouken@5288
   638
        SDL_free(palette->colors);
slouken@5288
   639
    }
slouken@5288
   640
    SDL_free(palette);
slouken@5288
   641
}
slouken@5288
   642
slouken@0
   643
/*
slouken@0
   644
 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
slouken@0
   645
 */
slouken@1895
   646
void
slouken@1895
   647
SDL_DitherColors(SDL_Color * colors, int bpp)
slouken@0
   648
{
slouken@1895
   649
    int i;
slouken@1895
   650
    if (bpp != 8)
slouken@1895
   651
        return;                 /* only 8bpp supported right now */
slouken@0
   652
slouken@1895
   653
    for (i = 0; i < 256; i++) {
slouken@1895
   654
        int r, g, b;
slouken@1895
   655
        /* map each bit field to the full [0, 255] interval,
slouken@1895
   656
           so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
slouken@1895
   657
        r = i & 0xe0;
slouken@1895
   658
        r |= r >> 3 | r >> 6;
slouken@1895
   659
        colors[i].r = r;
slouken@1895
   660
        g = (i << 3) & 0xe0;
slouken@1895
   661
        g |= g >> 3 | g >> 6;
slouken@1895
   662
        colors[i].g = g;
slouken@1895
   663
        b = i & 0x3;
slouken@1895
   664
        b |= b << 2;
slouken@1895
   665
        b |= b << 4;
slouken@1895
   666
        colors[i].b = b;
slouken@1895
   667
        colors[i].unused = SDL_ALPHA_OPAQUE;
slouken@1895
   668
    }
slouken@0
   669
}
slouken@1895
   670
slouken@0
   671
/* 
slouken@0
   672
 * Calculate the pad-aligned scanline width of a surface
slouken@0
   673
 */
slouken@1895
   674
int
slouken@1895
   675
SDL_CalculatePitch(SDL_Surface * surface)
slouken@0
   676
{
slouken@1895
   677
    int pitch;
slouken@0
   678
slouken@1895
   679
    /* Surface should be 4-byte aligned for speed */
slouken@1895
   680
    pitch = surface->w * surface->format->BytesPerPixel;
slouken@1895
   681
    switch (surface->format->BitsPerPixel) {
slouken@1895
   682
    case 1:
slouken@1895
   683
        pitch = (pitch + 7) / 8;
slouken@1895
   684
        break;
slouken@1895
   685
    case 4:
slouken@1895
   686
        pitch = (pitch + 1) / 2;
slouken@1895
   687
        break;
slouken@1895
   688
    default:
slouken@1895
   689
        break;
slouken@1895
   690
    }
slouken@1895
   691
    pitch = (pitch + 3) & ~3;   /* 4-byte aligning */
slouken@1895
   692
    return (pitch);
slouken@0
   693
}
slouken@1895
   694
slouken@0
   695
/*
slouken@0
   696
 * Match an RGB value to a particular palette index
slouken@0
   697
 */
slouken@1895
   698
Uint8
slouken@1895
   699
SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   700
{
slouken@1895
   701
    /* Do colorspace distance matching */
slouken@1895
   702
    unsigned int smallest;
slouken@1895
   703
    unsigned int distance;
slouken@1895
   704
    int rd, gd, bd;
slouken@1895
   705
    int i;
slouken@1895
   706
    Uint8 pixel = 0;
slouken@1895
   707
slouken@1895
   708
    smallest = ~0;
slouken@1895
   709
    for (i = 0; i < pal->ncolors; ++i) {
slouken@1895
   710
        rd = pal->colors[i].r - r;
slouken@1895
   711
        gd = pal->colors[i].g - g;
slouken@1895
   712
        bd = pal->colors[i].b - b;
slouken@1895
   713
        distance = (rd * rd) + (gd * gd) + (bd * bd);
slouken@1895
   714
        if (distance < smallest) {
slouken@1895
   715
            pixel = i;
slouken@1895
   716
            if (distance == 0) {        /* Perfect match! */
slouken@1895
   717
                break;
slouken@1895
   718
            }
slouken@1895
   719
            smallest = distance;
slouken@1895
   720
        }
slouken@1895
   721
    }
slouken@1895
   722
    return (pixel);
slouken@0
   723
}
slouken@0
   724
slouken@0
   725
/* Find the opaque pixel value corresponding to an RGB triple */
slouken@1895
   726
Uint32
slouken@2275
   727
SDL_MapRGB(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   728
{
slouken@1895
   729
    if (format->palette == NULL) {
slouken@1895
   730
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   731
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   732
            | (b >> format->Bloss) << format->Bshift | format->Amask;
slouken@1895
   733
    } else {
slouken@1895
   734
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   735
    }
slouken@0
   736
}
slouken@0
   737
slouken@0
   738
/* Find the pixel value corresponding to an RGBA quadruple */
slouken@1895
   739
Uint32
slouken@2275
   740
SDL_MapRGBA(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b,
slouken@2275
   741
            Uint8 a)
slouken@0
   742
{
slouken@1895
   743
    if (format->palette == NULL) {
slouken@1895
   744
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   745
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   746
            | (b >> format->Bloss) << format->Bshift
slouken@1895
   747
            | ((a >> format->Aloss) << format->Ashift & format->Amask);
slouken@1895
   748
    } else {
slouken@1895
   749
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   750
    }
slouken@0
   751
}
slouken@0
   752
slouken@1895
   753
void
slouken@2275
   754
SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
slouken@2275
   755
           Uint8 * b)
slouken@2275
   756
{
slouken@2275
   757
    if (format->palette == NULL) {
slouken@2275
   758
        /*
slouken@2275
   759
         * This makes sure that the result is mapped to the
slouken@2275
   760
         * interval [0..255], and the maximum value for each
slouken@2275
   761
         * component is 255. This is important to make sure
slouken@2275
   762
         * that white is indeed reported as (255, 255, 255).
slouken@2275
   763
         * This only works for RGB bit fields at least 4 bit
slouken@2275
   764
         * wide, which is almost always the case.
slouken@2275
   765
         */
slouken@2275
   766
        unsigned v;
slouken@2275
   767
        v = (pixel & format->Rmask) >> format->Rshift;
slouken@2275
   768
        *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
slouken@2275
   769
        v = (pixel & format->Gmask) >> format->Gshift;
slouken@2275
   770
        *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
slouken@2275
   771
        v = (pixel & format->Bmask) >> format->Bshift;
slouken@2275
   772
        *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
slouken@2275
   773
    } else {
slouken@2275
   774
        *r = format->palette->colors[pixel].r;
slouken@2275
   775
        *g = format->palette->colors[pixel].g;
slouken@2275
   776
        *b = format->palette->colors[pixel].b;
slouken@2275
   777
    }
slouken@2275
   778
}
slouken@2275
   779
slouken@2275
   780
void
slouken@2275
   781
SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
slouken@1895
   782
            Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@0
   783
{
slouken@2275
   784
    if (format->palette == NULL) {
slouken@1895
   785
        /*
slouken@1895
   786
         * This makes sure that the result is mapped to the
slouken@1895
   787
         * interval [0..255], and the maximum value for each
slouken@1895
   788
         * component is 255. This is important to make sure
slouken@1895
   789
         * that white is indeed reported as (255, 255, 255),
slouken@1895
   790
         * and that opaque alpha is 255.
slouken@1895
   791
         * This only works for RGB bit fields at least 4 bit
slouken@1895
   792
         * wide, which is almost always the case.
slouken@1895
   793
         */
slouken@1895
   794
        unsigned v;
slouken@2275
   795
        v = (pixel & format->Rmask) >> format->Rshift;
slouken@2275
   796
        *r = (v << format->Rloss) + (v >> (8 - (format->Rloss << 1)));
slouken@2275
   797
        v = (pixel & format->Gmask) >> format->Gshift;
slouken@2275
   798
        *g = (v << format->Gloss) + (v >> (8 - (format->Gloss << 1)));
slouken@2275
   799
        v = (pixel & format->Bmask) >> format->Bshift;
slouken@2275
   800
        *b = (v << format->Bloss) + (v >> (8 - (format->Bloss << 1)));
slouken@2275
   801
        if (format->Amask) {
slouken@2275
   802
            v = (pixel & format->Amask) >> format->Ashift;
slouken@2275
   803
            *a = (v << format->Aloss) + (v >> (8 - (format->Aloss << 1)));
slouken@1895
   804
        } else {
slouken@1895
   805
            *a = SDL_ALPHA_OPAQUE;
slouken@1895
   806
        }
slouken@1895
   807
    } else {
slouken@2275
   808
        *r = format->palette->colors[pixel].r;
slouken@2275
   809
        *g = format->palette->colors[pixel].g;
slouken@2275
   810
        *b = format->palette->colors[pixel].b;
slouken@1895
   811
        *a = SDL_ALPHA_OPAQUE;
slouken@1895
   812
    }
slouken@0
   813
}
slouken@0
   814
slouken@0
   815
/* Map from Palette to Palette */
slouken@1895
   816
static Uint8 *
slouken@1895
   817
Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
slouken@0
   818
{
slouken@1895
   819
    Uint8 *map;
slouken@1895
   820
    int i;
slouken@0
   821
slouken@1895
   822
    if (identical) {
slouken@1895
   823
        if (src->ncolors <= dst->ncolors) {
slouken@1895
   824
            /* If an identical palette, no need to map */
slouken@1895
   825
            if (src == dst
slouken@1895
   826
                ||
slouken@1895
   827
                (SDL_memcmp
slouken@1895
   828
                 (src->colors, dst->colors,
slouken@1895
   829
                  src->ncolors * sizeof(SDL_Color)) == 0)) {
slouken@1895
   830
                *identical = 1;
slouken@1895
   831
                return (NULL);
slouken@1895
   832
            }
slouken@1895
   833
        }
slouken@1895
   834
        *identical = 0;
slouken@1895
   835
    }
slouken@1895
   836
    map = (Uint8 *) SDL_malloc(src->ncolors);
slouken@1895
   837
    if (map == NULL) {
slouken@1895
   838
        SDL_OutOfMemory();
slouken@1895
   839
        return (NULL);
slouken@1895
   840
    }
slouken@1895
   841
    for (i = 0; i < src->ncolors; ++i) {
slouken@1895
   842
        map[i] = SDL_FindColor(dst,
slouken@1895
   843
                               src->colors[i].r, src->colors[i].g,
slouken@1895
   844
                               src->colors[i].b);
slouken@1895
   845
    }
slouken@1895
   846
    return (map);
slouken@0
   847
}
slouken@1895
   848
slouken@0
   849
/* Map from Palette to BitField */
slouken@1895
   850
static Uint8 *
slouken@2267
   851
Map1toN(SDL_PixelFormat * src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod,
slouken@2267
   852
        SDL_PixelFormat * dst)
slouken@0
   853
{
slouken@1895
   854
    Uint8 *map;
slouken@1895
   855
    int i;
slouken@1895
   856
    int bpp;
slouken@1895
   857
    SDL_Palette *pal = src->palette;
slouken@0
   858
slouken@1895
   859
    bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
slouken@1895
   860
    map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
slouken@1895
   861
    if (map == NULL) {
slouken@1895
   862
        SDL_OutOfMemory();
slouken@1895
   863
        return (NULL);
slouken@1895
   864
    }
slouken@0
   865
slouken@1895
   866
    /* We memory copy to the pixel map so the endianness is preserved */
slouken@1895
   867
    for (i = 0; i < pal->ncolors; ++i) {
slouken@2257
   868
        Uint8 A = Amod;
slouken@2267
   869
        Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
slouken@2267
   870
        Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
slouken@2267
   871
        Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
slouken@2257
   872
        ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
slouken@1895
   873
    }
slouken@1895
   874
    return (map);
slouken@0
   875
}
slouken@1895
   876
slouken@0
   877
/* Map from BitField to Dithered-Palette to Palette */
slouken@1895
   878
static Uint8 *
slouken@1895
   879
MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical)
slouken@0
   880
{
slouken@1895
   881
    /* Generate a 256 color dither palette */
slouken@1895
   882
    SDL_Palette dithered;
slouken@1895
   883
    SDL_Color colors[256];
slouken@1895
   884
    SDL_Palette *pal = dst->palette;
slouken@0
   885
slouken@1895
   886
    dithered.ncolors = 256;
slouken@1895
   887
    SDL_DitherColors(colors, 8);
slouken@1895
   888
    dithered.colors = colors;
slouken@1895
   889
    return (Map1to1(&dithered, pal, identical));
slouken@0
   890
}
slouken@0
   891
slouken@1895
   892
SDL_BlitMap *
slouken@1895
   893
SDL_AllocBlitMap(void)
slouken@0
   894
{
slouken@1895
   895
    SDL_BlitMap *map;
slouken@0
   896
slouken@1895
   897
    /* Allocate the empty map */
slouken@1920
   898
    map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
slouken@1895
   899
    if (map == NULL) {
slouken@1895
   900
        SDL_OutOfMemory();
slouken@1895
   901
        return (NULL);
slouken@1895
   902
    }
slouken@2267
   903
    map->info.r = 0xFF;
slouken@2267
   904
    map->info.g = 0xFF;
slouken@2267
   905
    map->info.b = 0xFF;
slouken@2267
   906
    map->info.a = 0xFF;
slouken@0
   907
slouken@1895
   908
    /* It's ready to go */
slouken@1895
   909
    return (map);
slouken@0
   910
}
slouken@1895
   911
slouken@1895
   912
void
slouken@1895
   913
SDL_InvalidateMap(SDL_BlitMap * map)
slouken@0
   914
{
slouken@1895
   915
    if (!map) {
slouken@1895
   916
        return;
slouken@1895
   917
    }
slouken@1895
   918
    map->dst = NULL;
slouken@5288
   919
    map->palette_version = 0;
slouken@2267
   920
    if (map->info.table) {
slouken@2267
   921
        SDL_free(map->info.table);
slouken@2267
   922
        map->info.table = NULL;
slouken@1895
   923
    }
slouken@0
   924
}
slouken@2735
   925
slouken@1895
   926
int
slouken@1895
   927
SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)
slouken@0
   928
{
slouken@1895
   929
    SDL_PixelFormat *srcfmt;
slouken@1895
   930
    SDL_PixelFormat *dstfmt;
slouken@1895
   931
    SDL_BlitMap *map;
slouken@0
   932
slouken@1895
   933
    /* Clear out any previous mapping */
slouken@1895
   934
    map = src->map;
slouken@1895
   935
    if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   936
        SDL_UnRLESurface(src, 1);
slouken@1895
   937
    }
slouken@1895
   938
    SDL_InvalidateMap(map);
slouken@0
   939
slouken@1895
   940
    /* Figure out what kind of mapping we're doing */
slouken@1895
   941
    map->identity = 0;
slouken@1895
   942
    srcfmt = src->format;
slouken@1895
   943
    dstfmt = dst->format;
slouken@5288
   944
    if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
slouken@5288
   945
        if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
slouken@1895
   946
            /* Palette --> Palette */
slouken@2267
   947
            map->info.table =
slouken@1895
   948
                Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
slouken@1895
   949
            if (!map->identity) {
slouken@2267
   950
                if (map->info.table == NULL) {
slouken@1895
   951
                    return (-1);
slouken@1895
   952
                }
slouken@1895
   953
            }
slouken@1895
   954
            if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
slouken@1895
   955
                map->identity = 0;
slouken@5288
   956
        } else {
slouken@1895
   957
            /* Palette --> BitField */
slouken@2267
   958
            map->info.table =
slouken@2267
   959
                Map1toN(srcfmt, src->map->info.r, src->map->info.g,
slouken@2267
   960
                        src->map->info.b, src->map->info.a, dstfmt);
slouken@2267
   961
            if (map->info.table == NULL) {
slouken@1895
   962
                return (-1);
slouken@1895
   963
            }
slouken@1895
   964
        }
slouken@5288
   965
    } else {
slouken@5288
   966
        if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
slouken@1895
   967
            /* BitField --> Palette */
slouken@2267
   968
            map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
slouken@1895
   969
            if (!map->identity) {
slouken@2267
   970
                if (map->info.table == NULL) {
slouken@1895
   971
                    return (-1);
slouken@1895
   972
                }
slouken@1895
   973
            }
slouken@1895
   974
            map->identity = 0;  /* Don't optimize to copy */
slouken@5288
   975
        } else {
slouken@1895
   976
            /* BitField --> BitField */
slouken@5288
   977
            if (srcfmt == dstfmt) {
slouken@1895
   978
                map->identity = 1;
slouken@5288
   979
            }
slouken@1895
   980
        }
slouken@1895
   981
    }
slouken@0
   982
slouken@1895
   983
    map->dst = dst;
slouken@5288
   984
slouken@5288
   985
    if (dstfmt->palette) {
slouken@5288
   986
        map->palette_version = dstfmt->palette->version;
slouken@5288
   987
    } else {
slouken@5288
   988
        map->palette_version = 0;
slouken@5288
   989
    }
slouken@0
   990
slouken@1895
   991
    /* Choose your blitters wisely */
slouken@1895
   992
    return (SDL_CalculateBlit(src));
slouken@0
   993
}
slouken@1895
   994
slouken@1895
   995
void
slouken@1895
   996
SDL_FreeBlitMap(SDL_BlitMap * map)
slouken@0
   997
{
slouken@1895
   998
    if (map) {
slouken@1895
   999
        SDL_InvalidateMap(map);
slouken@1895
  1000
        SDL_free(map);
slouken@1895
  1001
    }
slouken@0
  1002
}
slouken@1895
  1003
slouken@1895
  1004
/* vi: set ts=4 sw=4 expandtab: */