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