src/video/SDL_pixels.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Aug 2006 17:09:42 +0000
changeset 1965 a788656ca29a
parent 1920 8a162bfdc838
child 2102 0975e24a3670
permissions -rw-r--r--
SDL constants are all uppercase.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* 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@0
    33
/* Helper functions */
slouken@1895
    34
slouken@1895
    35
SDL_bool
slouken@1895
    36
SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask,
slouken@1895
    37
                           Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
slouken@1895
    38
{
slouken@1895
    39
    Uint32 masks[4];
slouken@1895
    40
slouken@1895
    41
    /* Initialize the values here */
slouken@1895
    42
    if (SDL_BITSPERPIXEL(format) == 24) {
slouken@1895
    43
        *bpp = SDL_BYTESPERPIXEL(format) * 8;
slouken@1895
    44
    } else {
slouken@1895
    45
        *bpp = SDL_BITSPERPIXEL(format);
slouken@1895
    46
    }
slouken@1895
    47
    *Rmask = *Gmask = *Bmask = *Amask = 0;
slouken@1895
    48
slouken@1965
    49
    if (SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED8 &&
slouken@1965
    50
        SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED16 &&
slouken@1965
    51
        SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED32) {
slouken@1895
    52
        /* Not a format that uses masks */
slouken@1895
    53
        return SDL_TRUE;
slouken@1895
    54
    }
slouken@1895
    55
slouken@1895
    56
    switch (SDL_PIXELLAYOUT(format)) {
slouken@1965
    57
    case SDL_PACKEDLAYOUT_332:
slouken@1895
    58
        masks[0] = 0x00000000;
slouken@1895
    59
        masks[1] = 0x000000E0;
slouken@1895
    60
        masks[2] = 0x0000001C;
slouken@1895
    61
        masks[3] = 0x00000003;
slouken@1895
    62
        break;
slouken@1965
    63
    case SDL_PACKEDLAYOUT_4444:
slouken@1895
    64
        masks[0] = 0x0000F000;
slouken@1895
    65
        masks[1] = 0x00000F00;
slouken@1895
    66
        masks[2] = 0x000000F0;
slouken@1895
    67
        masks[3] = 0x0000000F;
slouken@1895
    68
        break;
slouken@1965
    69
    case SDL_PACKEDLAYOUT_1555:
slouken@1895
    70
        masks[0] = 0x00008000;
slouken@1895
    71
        masks[1] = 0x00007C00;
slouken@1895
    72
        masks[2] = 0x000003E0;
slouken@1895
    73
        masks[3] = 0x0000001F;
slouken@1895
    74
        break;
slouken@1965
    75
    case SDL_PACKEDLAYOUT_565:
slouken@1895
    76
        masks[0] = 0x00000000;
slouken@1895
    77
        masks[1] = 0x0000F800;
slouken@1895
    78
        masks[2] = 0x000007E0;
slouken@1895
    79
        masks[3] = 0x0000001F;
slouken@1895
    80
        break;
slouken@1965
    81
    case SDL_PACKEDLAYOUT_8888:
slouken@1895
    82
        masks[0] = 0xFF000000;
slouken@1895
    83
        masks[1] = 0x00FF0000;
slouken@1895
    84
        masks[2] = 0x0000FF00;
slouken@1895
    85
        masks[3] = 0x000000FF;
slouken@1895
    86
        break;
slouken@1965
    87
    case SDL_PACKEDLAYOUT_2101010:
slouken@1895
    88
        masks[0] = 0xC0000000;
slouken@1895
    89
        masks[1] = 0x3FF00000;
slouken@1895
    90
        masks[2] = 0x000FFC00;
slouken@1895
    91
        masks[3] = 0x000003FF;
slouken@1895
    92
        break;
slouken@1895
    93
    default:
slouken@1895
    94
        /* Unknown layout */
slouken@1895
    95
        return SDL_FALSE;
slouken@1895
    96
    }
slouken@1895
    97
slouken@1895
    98
    switch (SDL_PIXELORDER(format)) {
slouken@1965
    99
    case SDL_PACKEDORDER_XRGB:
slouken@1895
   100
        *Rmask = masks[1];
slouken@1895
   101
        *Gmask = masks[2];
slouken@1895
   102
        *Bmask = masks[3];
slouken@1895
   103
        break;
slouken@1965
   104
    case SDL_PACKEDORDER_RGBX:
slouken@1895
   105
        *Rmask = masks[0];
slouken@1895
   106
        *Gmask = masks[1];
slouken@1895
   107
        *Bmask = masks[2];
slouken@1895
   108
        break;
slouken@1965
   109
    case SDL_PACKEDORDER_ARGB:
slouken@1895
   110
        *Amask = masks[0];
slouken@1895
   111
        *Rmask = masks[1];
slouken@1895
   112
        *Gmask = masks[2];
slouken@1895
   113
        *Bmask = masks[3];
slouken@1895
   114
        break;
slouken@1965
   115
    case SDL_PACKEDORDER_RGBA:
slouken@1895
   116
        *Rmask = masks[0];
slouken@1895
   117
        *Gmask = masks[1];
slouken@1895
   118
        *Bmask = masks[2];
slouken@1895
   119
        *Amask = masks[3];
slouken@1895
   120
        break;
slouken@1965
   121
    case SDL_PACKEDORDER_XBGR:
slouken@1895
   122
        *Bmask = masks[1];
slouken@1895
   123
        *Gmask = masks[2];
slouken@1895
   124
        *Rmask = masks[3];
slouken@1895
   125
        break;
slouken@1965
   126
    case SDL_PACKEDORDER_BGRX:
slouken@1895
   127
        *Bmask = masks[0];
slouken@1895
   128
        *Gmask = masks[1];
slouken@1895
   129
        *Rmask = masks[2];
slouken@1895
   130
        break;
slouken@1965
   131
    case SDL_PACKEDORDER_BGRA:
slouken@1895
   132
        *Bmask = masks[0];
slouken@1895
   133
        *Gmask = masks[1];
slouken@1895
   134
        *Rmask = masks[2];
slouken@1895
   135
        *Amask = masks[3];
slouken@1895
   136
        break;
slouken@1965
   137
    case SDL_PACKEDORDER_ABGR:
slouken@1895
   138
        *Amask = masks[0];
slouken@1895
   139
        *Bmask = masks[1];
slouken@1895
   140
        *Gmask = masks[2];
slouken@1895
   141
        *Rmask = masks[3];
slouken@1895
   142
        break;
slouken@1895
   143
    default:
slouken@1895
   144
        /* Unknown order */
slouken@1895
   145
        return SDL_FALSE;
slouken@1895
   146
    }
slouken@1895
   147
    return SDL_TRUE;
slouken@1895
   148
}
slouken@1895
   149
slouken@1895
   150
Uint32
slouken@1895
   151
SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   152
                           Uint32 Amask)
slouken@1895
   153
{
slouken@1895
   154
    switch (bpp) {
slouken@1895
   155
    case 8:
slouken@1895
   156
        switch (Rmask) {
slouken@1895
   157
        case 0:
slouken@1965
   158
            return SDL_PIXELFORMAT_INDEX8;
slouken@1895
   159
        case 0xE0:
slouken@1965
   160
            return SDL_PIXELFORMAT_RGB332;
slouken@1895
   161
        }
slouken@1895
   162
        break;
slouken@1895
   163
    case 12:
slouken@1895
   164
        switch (Rmask) {
slouken@1895
   165
        case 0x0F00:
slouken@1965
   166
            return SDL_PIXELFORMAT_RGB444;
slouken@1895
   167
        }
slouken@1895
   168
        break;
slouken@1895
   169
    case 15:
slouken@1895
   170
        switch (Rmask) {
slouken@1895
   171
        case 0x7C00:
slouken@1965
   172
            return SDL_PIXELFORMAT_RGB555;
slouken@1895
   173
        }
slouken@1895
   174
        break;
slouken@1895
   175
    case 16:
slouken@1895
   176
        switch (Rmask) {
slouken@1895
   177
        case 0x0F00:
slouken@1965
   178
            return SDL_PIXELFORMAT_ARGB4444;
slouken@1895
   179
        case 0x7C00:
slouken@1965
   180
            return SDL_PIXELFORMAT_ARGB1555;
slouken@1895
   181
        case 0xF800:
slouken@1965
   182
            return SDL_PIXELFORMAT_RGB565;
slouken@1895
   183
        }
slouken@1895
   184
        break;
slouken@1895
   185
    case 32:
slouken@1895
   186
        switch (Rmask) {
slouken@1895
   187
        case 0xFF000000:
slouken@1895
   188
            if (Amask == 0x000000FF) {
slouken@1965
   189
                return SDL_PIXELFORMAT_RGBA8888;
slouken@1895
   190
            }
slouken@1895
   191
            break;
slouken@1895
   192
        case 0x00FF0000:
slouken@1895
   193
            if (Amask == 0xFF000000) {
slouken@1965
   194
                return SDL_PIXELFORMAT_ARGB8888;
slouken@1895
   195
            } else {
slouken@1965
   196
                return SDL_PIXELFORMAT_RGB888;
slouken@1895
   197
            }
slouken@1895
   198
            break;
slouken@1895
   199
        case 0x0000FF00:
slouken@1895
   200
            if (Amask == 0x000000FF) {
slouken@1965
   201
                return SDL_PIXELFORMAT_BGRA8888;
slouken@1895
   202
            }
slouken@1895
   203
            break;
slouken@1895
   204
        case 0x000000FF:
slouken@1895
   205
            if (Amask == 0xFF000000) {
slouken@1965
   206
                return SDL_PIXELFORMAT_ABGR8888;
slouken@1895
   207
            } else {
slouken@1965
   208
                return SDL_PIXELFORMAT_BGR888;
slouken@1895
   209
            }
slouken@1895
   210
            break;
slouken@1895
   211
        case 0x3FF00000:
slouken@1965
   212
            return SDL_PIXELFORMAT_ARGB2101010;
slouken@1895
   213
        }
slouken@1895
   214
    }
slouken@1965
   215
    return SDL_PIXELFORMAT_UNKNOWN;
slouken@1895
   216
}
slouken@1895
   217
slouken@1895
   218
slouken@1895
   219
SDL_Palette *
slouken@1895
   220
SDL_AllocPalette(int ncolors)
slouken@1895
   221
{
slouken@1895
   222
    SDL_Palette *palette;
slouken@1895
   223
slouken@1895
   224
    palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
slouken@1895
   225
    if (!palette) {
slouken@1895
   226
        SDL_OutOfMemory();
slouken@1895
   227
        return NULL;
slouken@1895
   228
    }
slouken@1895
   229
    palette->colors =
slouken@1895
   230
        (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
slouken@1895
   231
    if (!palette->colors) {
slouken@1895
   232
        SDL_free(palette);
slouken@1895
   233
        return NULL;
slouken@1895
   234
    }
slouken@1895
   235
    palette->ncolors = ncolors;
slouken@1895
   236
    palette->watch = NULL;
slouken@1895
   237
    palette->refcount = 1;
slouken@1895
   238
slouken@1895
   239
    SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
slouken@1895
   240
slouken@1895
   241
    return palette;
slouken@1895
   242
}
slouken@1895
   243
slouken@1895
   244
int
slouken@1895
   245
SDL_AddPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
slouken@1895
   246
                    void *userdata)
slouken@1895
   247
{
slouken@1895
   248
    SDL_PaletteWatch *watch;
slouken@1895
   249
slouken@1895
   250
    if (!palette) {
slouken@1895
   251
        return -1;
slouken@1895
   252
    }
slouken@1895
   253
slouken@1895
   254
    watch = (SDL_PaletteWatch *) SDL_malloc(sizeof(*watch));
slouken@1895
   255
    if (!watch) {
slouken@1895
   256
        SDL_OutOfMemory();
slouken@1895
   257
        return -1;
slouken@1895
   258
    }
slouken@1895
   259
slouken@1895
   260
    watch->callback = callback;
slouken@1895
   261
    watch->userdata = userdata;
slouken@1895
   262
    watch->next = palette->watch;
slouken@1895
   263
    palette->watch = watch;
slouken@1895
   264
    ++palette->refcount;
slouken@1895
   265
    return 0;
slouken@1895
   266
}
slouken@1895
   267
slouken@1895
   268
void
slouken@1895
   269
SDL_DelPaletteWatch(SDL_Palette * palette, SDL_PaletteChangedFunc callback,
slouken@1895
   270
                    void *userdata)
slouken@1895
   271
{
slouken@1895
   272
    SDL_PaletteWatch *prev, *watch;
slouken@1895
   273
slouken@1895
   274
    if (!palette) {
slouken@1895
   275
        return;
slouken@1895
   276
    }
slouken@1895
   277
slouken@1895
   278
    for (prev = NULL, watch = palette->watch; watch;
slouken@1895
   279
         prev = watch, watch = watch->next) {
slouken@1895
   280
        if (watch->callback == callback && watch->userdata == userdata) {
slouken@1895
   281
            if (prev) {
slouken@1895
   282
                prev->next = watch->next;
slouken@1895
   283
            } else {
slouken@1895
   284
                palette->watch = watch->next;
slouken@1895
   285
            }
slouken@1895
   286
            SDL_free(watch);
slouken@1895
   287
            SDL_FreePalette(palette);
slouken@1895
   288
            return;
slouken@1895
   289
        }
slouken@1895
   290
    }
slouken@1895
   291
}
slouken@1895
   292
slouken@1895
   293
int
slouken@1895
   294
SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
slouken@1895
   295
                     int firstcolor, int ncolors)
slouken@1895
   296
{
slouken@1895
   297
    SDL_PaletteWatch *watch;
slouken@1895
   298
    int status = 0;
slouken@1895
   299
slouken@1895
   300
    /* Verify the parameters */
slouken@1895
   301
    if (!palette) {
slouken@1895
   302
        return -1;
slouken@1895
   303
    }
slouken@1895
   304
    if (ncolors > (palette->ncolors - firstcolor)) {
slouken@1895
   305
        ncolors = (palette->ncolors - firstcolor);
slouken@1895
   306
        status = -1;
slouken@1895
   307
    }
slouken@1895
   308
slouken@1895
   309
    if (colors != (palette->colors + firstcolor)) {
slouken@1895
   310
        SDL_memcpy(palette->colors + firstcolor, colors,
slouken@1895
   311
                   ncolors * sizeof(*colors));
slouken@1895
   312
    }
slouken@1895
   313
slouken@1895
   314
    for (watch = palette->watch; watch; watch = watch->next) {
slouken@1895
   315
        if (watch->callback(watch->userdata, palette) < 0) {
slouken@1895
   316
            status = -1;
slouken@1895
   317
        }
slouken@1895
   318
    }
slouken@1895
   319
slouken@1895
   320
    return status;
slouken@1895
   321
}
slouken@1895
   322
slouken@1895
   323
void
slouken@1895
   324
SDL_FreePalette(SDL_Palette * palette)
slouken@1895
   325
{
slouken@1895
   326
    if (!palette) {
slouken@1895
   327
        return;
slouken@1895
   328
    }
slouken@1895
   329
    if (--palette->refcount > 0) {
slouken@1895
   330
        return;
slouken@1895
   331
    }
slouken@1895
   332
    if (palette->colors) {
slouken@1895
   333
        SDL_free(palette->colors);
slouken@1895
   334
    }
slouken@1895
   335
    SDL_free(palette);
slouken@1895
   336
}
slouken@1895
   337
slouken@0
   338
/*
slouken@0
   339
 * Allocate a pixel format structure and fill it according to the given info.
slouken@0
   340
 */
slouken@1895
   341
SDL_PixelFormat *
slouken@1895
   342
SDL_AllocFormat(int bpp,
slouken@1895
   343
                Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
   344
{
slouken@1895
   345
    SDL_PixelFormat *format;
slouken@1895
   346
    Uint32 mask;
slouken@0
   347
slouken@1895
   348
    /* Allocate an empty pixel format structure */
slouken@1920
   349
    format = SDL_calloc(1, sizeof(*format));
slouken@1895
   350
    if (format == NULL) {
slouken@1895
   351
        SDL_OutOfMemory();
slouken@1895
   352
        return (NULL);
slouken@1895
   353
    }
slouken@1895
   354
    format->alpha = SDL_ALPHA_OPAQUE;
slouken@0
   355
slouken@1895
   356
    /* Set up the format */
slouken@1895
   357
    format->BitsPerPixel = bpp;
slouken@1895
   358
    format->BytesPerPixel = (bpp + 7) / 8;
slouken@1895
   359
    if (Rmask || Bmask || Gmask) {      /* Packed pixels with custom mask */
slouken@1895
   360
        format->Rshift = 0;
slouken@1895
   361
        format->Rloss = 8;
slouken@1895
   362
        if (Rmask) {
slouken@1895
   363
            for (mask = Rmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   364
                ++format->Rshift;
slouken@1895
   365
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   366
                --format->Rloss;
slouken@1895
   367
        }
slouken@1895
   368
        format->Gshift = 0;
slouken@1895
   369
        format->Gloss = 8;
slouken@1895
   370
        if (Gmask) {
slouken@1895
   371
            for (mask = Gmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   372
                ++format->Gshift;
slouken@1895
   373
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   374
                --format->Gloss;
slouken@1895
   375
        }
slouken@1895
   376
        format->Bshift = 0;
slouken@1895
   377
        format->Bloss = 8;
slouken@1895
   378
        if (Bmask) {
slouken@1895
   379
            for (mask = Bmask; !(mask & 0x01); mask >>= 1)
slouken@1895
   380
                ++format->Bshift;
slouken@1895
   381
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   382
                --format->Bloss;
slouken@1895
   383
        }
slouken@1895
   384
        format->Ashift = 0;
slouken@1895
   385
        format->Aloss = 8;
slouken@1895
   386
        if (Amask) {
slouken@1895
   387
            for (mask = Amask; !(mask & 0x01); mask >>= 1)
slouken@1895
   388
                ++format->Ashift;
slouken@1895
   389
            for (; (mask & 0x01); mask >>= 1)
slouken@1895
   390
                --format->Aloss;
slouken@1895
   391
        }
slouken@1895
   392
        format->Rmask = Rmask;
slouken@1895
   393
        format->Gmask = Gmask;
slouken@1895
   394
        format->Bmask = Bmask;
slouken@1895
   395
        format->Amask = Amask;
slouken@1895
   396
    } else if (bpp > 8) {       /* Packed pixels with standard mask */
slouken@1895
   397
        /* R-G-B */
slouken@1895
   398
        if (bpp > 24)
slouken@1895
   399
            bpp = 24;
slouken@1895
   400
        format->Rloss = 8 - (bpp / 3);
slouken@1895
   401
        format->Gloss = 8 - (bpp / 3) - (bpp % 3);
slouken@1895
   402
        format->Bloss = 8 - (bpp / 3);
slouken@1895
   403
        format->Rshift = ((bpp / 3) + (bpp % 3)) + (bpp / 3);
slouken@1895
   404
        format->Gshift = (bpp / 3);
slouken@1895
   405
        format->Bshift = 0;
slouken@1895
   406
        format->Rmask = ((0xFF >> format->Rloss) << format->Rshift);
slouken@1895
   407
        format->Gmask = ((0xFF >> format->Gloss) << format->Gshift);
slouken@1895
   408
        format->Bmask = ((0xFF >> format->Bloss) << format->Bshift);
slouken@1895
   409
    } else {
slouken@1895
   410
        /* Palettized formats have no mask info */
slouken@1895
   411
        format->Rloss = 8;
slouken@1895
   412
        format->Gloss = 8;
slouken@1895
   413
        format->Bloss = 8;
slouken@1895
   414
        format->Aloss = 8;
slouken@1895
   415
        format->Rshift = 0;
slouken@1895
   416
        format->Gshift = 0;
slouken@1895
   417
        format->Bshift = 0;
slouken@1895
   418
        format->Ashift = 0;
slouken@1895
   419
        format->Rmask = 0;
slouken@1895
   420
        format->Gmask = 0;
slouken@1895
   421
        format->Bmask = 0;
slouken@1895
   422
        format->Amask = 0;
slouken@1895
   423
    }
slouken@1895
   424
    format->palette = NULL;
slouken@1057
   425
slouken@1895
   426
    return (format);
slouken@0
   427
}
slouken@0
   428
slouken@0
   429
/*
slouken@0
   430
 * Change any previous mappings from/to the new surface format
slouken@0
   431
 */
slouken@1895
   432
void
slouken@1895
   433
SDL_FormatChanged(SDL_Surface * surface)
slouken@0
   434
{
slouken@1895
   435
    static int format_version = 0;
slouken@1895
   436
    ++format_version;
slouken@1895
   437
    if (format_version < 0) {   /* It wrapped... */
slouken@1895
   438
        format_version = 1;
slouken@1895
   439
    }
slouken@1895
   440
    surface->format_version = format_version;
slouken@1895
   441
    SDL_InvalidateMap(surface->map);
slouken@0
   442
}
slouken@1895
   443
slouken@0
   444
/*
slouken@0
   445
 * Free a previously allocated format structure
slouken@0
   446
 */
slouken@1895
   447
void
slouken@1895
   448
SDL_FreeFormat(SDL_PixelFormat * format)
slouken@0
   449
{
slouken@1895
   450
    if (!format) {
slouken@1895
   451
        return;
slouken@1895
   452
    }
slouken@1895
   453
    SDL_free(format);
slouken@0
   454
}
slouken@1895
   455
slouken@0
   456
/*
slouken@0
   457
 * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
slouken@0
   458
 */
slouken@1895
   459
void
slouken@1895
   460
SDL_DitherColors(SDL_Color * colors, int bpp)
slouken@0
   461
{
slouken@1895
   462
    int i;
slouken@1895
   463
    if (bpp != 8)
slouken@1895
   464
        return;                 /* only 8bpp supported right now */
slouken@0
   465
slouken@1895
   466
    for (i = 0; i < 256; i++) {
slouken@1895
   467
        int r, g, b;
slouken@1895
   468
        /* map each bit field to the full [0, 255] interval,
slouken@1895
   469
           so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
slouken@1895
   470
        r = i & 0xe0;
slouken@1895
   471
        r |= r >> 3 | r >> 6;
slouken@1895
   472
        colors[i].r = r;
slouken@1895
   473
        g = (i << 3) & 0xe0;
slouken@1895
   474
        g |= g >> 3 | g >> 6;
slouken@1895
   475
        colors[i].g = g;
slouken@1895
   476
        b = i & 0x3;
slouken@1895
   477
        b |= b << 2;
slouken@1895
   478
        b |= b << 4;
slouken@1895
   479
        colors[i].b = b;
slouken@1895
   480
        colors[i].unused = SDL_ALPHA_OPAQUE;
slouken@1895
   481
    }
slouken@0
   482
}
slouken@1895
   483
slouken@0
   484
/* 
slouken@0
   485
 * Calculate the pad-aligned scanline width of a surface
slouken@0
   486
 */
slouken@1895
   487
int
slouken@1895
   488
SDL_CalculatePitch(SDL_Surface * surface)
slouken@0
   489
{
slouken@1895
   490
    int pitch;
slouken@0
   491
slouken@1895
   492
    /* Surface should be 4-byte aligned for speed */
slouken@1895
   493
    pitch = surface->w * surface->format->BytesPerPixel;
slouken@1895
   494
    switch (surface->format->BitsPerPixel) {
slouken@1895
   495
    case 1:
slouken@1895
   496
        pitch = (pitch + 7) / 8;
slouken@1895
   497
        break;
slouken@1895
   498
    case 4:
slouken@1895
   499
        pitch = (pitch + 1) / 2;
slouken@1895
   500
        break;
slouken@1895
   501
    default:
slouken@1895
   502
        break;
slouken@1895
   503
    }
slouken@1895
   504
    pitch = (pitch + 3) & ~3;   /* 4-byte aligning */
slouken@1895
   505
    return (pitch);
slouken@0
   506
}
slouken@1895
   507
slouken@0
   508
/*
slouken@0
   509
 * Match an RGB value to a particular palette index
slouken@0
   510
 */
slouken@1895
   511
Uint8
slouken@1895
   512
SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   513
{
slouken@1895
   514
    /* Do colorspace distance matching */
slouken@1895
   515
    unsigned int smallest;
slouken@1895
   516
    unsigned int distance;
slouken@1895
   517
    int rd, gd, bd;
slouken@1895
   518
    int i;
slouken@1895
   519
    Uint8 pixel = 0;
slouken@1895
   520
slouken@1895
   521
    smallest = ~0;
slouken@1895
   522
    for (i = 0; i < pal->ncolors; ++i) {
slouken@1895
   523
        rd = pal->colors[i].r - r;
slouken@1895
   524
        gd = pal->colors[i].g - g;
slouken@1895
   525
        bd = pal->colors[i].b - b;
slouken@1895
   526
        distance = (rd * rd) + (gd * gd) + (bd * bd);
slouken@1895
   527
        if (distance < smallest) {
slouken@1895
   528
            pixel = i;
slouken@1895
   529
            if (distance == 0) {        /* Perfect match! */
slouken@1895
   530
                break;
slouken@1895
   531
            }
slouken@1895
   532
            smallest = distance;
slouken@1895
   533
        }
slouken@1895
   534
    }
slouken@1895
   535
    return (pixel);
slouken@0
   536
}
slouken@0
   537
slouken@0
   538
/* Find the opaque pixel value corresponding to an RGB triple */
slouken@1895
   539
Uint32
slouken@1895
   540
SDL_MapRGB(SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b)
slouken@0
   541
{
slouken@1895
   542
    if (format->palette == NULL) {
slouken@1895
   543
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   544
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   545
            | (b >> format->Bloss) << format->Bshift | format->Amask;
slouken@1895
   546
    } else {
slouken@1895
   547
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   548
    }
slouken@0
   549
}
slouken@0
   550
slouken@0
   551
/* Find the pixel value corresponding to an RGBA quadruple */
slouken@1895
   552
Uint32
slouken@1895
   553
SDL_MapRGBA(SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@0
   554
{
slouken@1895
   555
    if (format->palette == NULL) {
slouken@1895
   556
        return (r >> format->Rloss) << format->Rshift
slouken@1895
   557
            | (g >> format->Gloss) << format->Gshift
slouken@1895
   558
            | (b >> format->Bloss) << format->Bshift
slouken@1895
   559
            | ((a >> format->Aloss) << format->Ashift & format->Amask);
slouken@1895
   560
    } else {
slouken@1895
   561
        return SDL_FindColor(format->palette, r, g, b);
slouken@1895
   562
    }
slouken@0
   563
}
slouken@0
   564
slouken@1895
   565
void
slouken@1895
   566
SDL_GetRGBA(Uint32 pixel, SDL_PixelFormat * fmt,
slouken@1895
   567
            Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
slouken@0
   568
{
slouken@1895
   569
    if (fmt->palette == NULL) {
slouken@1895
   570
        /*
slouken@1895
   571
         * This makes sure that the result is mapped to the
slouken@1895
   572
         * interval [0..255], and the maximum value for each
slouken@1895
   573
         * component is 255. This is important to make sure
slouken@1895
   574
         * that white is indeed reported as (255, 255, 255),
slouken@1895
   575
         * and that opaque alpha is 255.
slouken@1895
   576
         * This only works for RGB bit fields at least 4 bit
slouken@1895
   577
         * wide, which is almost always the case.
slouken@1895
   578
         */
slouken@1895
   579
        unsigned v;
slouken@1895
   580
        v = (pixel & fmt->Rmask) >> fmt->Rshift;
slouken@1895
   581
        *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
slouken@1895
   582
        v = (pixel & fmt->Gmask) >> fmt->Gshift;
slouken@1895
   583
        *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
slouken@1895
   584
        v = (pixel & fmt->Bmask) >> fmt->Bshift;
slouken@1895
   585
        *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
slouken@1895
   586
        if (fmt->Amask) {
slouken@1895
   587
            v = (pixel & fmt->Amask) >> fmt->Ashift;
slouken@1895
   588
            *a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
slouken@1895
   589
        } else {
slouken@1895
   590
            *a = SDL_ALPHA_OPAQUE;
slouken@1895
   591
        }
slouken@1895
   592
    } else {
slouken@1895
   593
        *r = fmt->palette->colors[pixel].r;
slouken@1895
   594
        *g = fmt->palette->colors[pixel].g;
slouken@1895
   595
        *b = fmt->palette->colors[pixel].b;
slouken@1895
   596
        *a = SDL_ALPHA_OPAQUE;
slouken@1895
   597
    }
slouken@0
   598
}
slouken@0
   599
slouken@1895
   600
void
slouken@1895
   601
SDL_GetRGB(Uint32 pixel, SDL_PixelFormat * fmt, Uint8 * r, Uint8 * g,
slouken@1895
   602
           Uint8 * b)
slouken@0
   603
{
slouken@1895
   604
    if (fmt->palette == NULL) {
slouken@1895
   605
        /* the note for SDL_GetRGBA above applies here too */
slouken@1895
   606
        unsigned v;
slouken@1895
   607
        v = (pixel & fmt->Rmask) >> fmt->Rshift;
slouken@1895
   608
        *r = (v << fmt->Rloss) + (v >> (8 - (fmt->Rloss << 1)));
slouken@1895
   609
        v = (pixel & fmt->Gmask) >> fmt->Gshift;
slouken@1895
   610
        *g = (v << fmt->Gloss) + (v >> (8 - (fmt->Gloss << 1)));
slouken@1895
   611
        v = (pixel & fmt->Bmask) >> fmt->Bshift;
slouken@1895
   612
        *b = (v << fmt->Bloss) + (v >> (8 - (fmt->Bloss << 1)));
slouken@1895
   613
    } else {
slouken@1895
   614
        *r = fmt->palette->colors[pixel].r;
slouken@1895
   615
        *g = fmt->palette->colors[pixel].g;
slouken@1895
   616
        *b = fmt->palette->colors[pixel].b;
slouken@1895
   617
    }
slouken@0
   618
}
slouken@0
   619
slouken@0
   620
/* Apply gamma to a set of colors - this is easy. :) */
slouken@1895
   621
void
slouken@1895
   622
SDL_ApplyGamma(Uint16 * gamma, SDL_Color * colors, SDL_Color * output,
slouken@1895
   623
               int ncolors)
slouken@0
   624
{
slouken@1895
   625
    int i;
slouken@0
   626
slouken@1895
   627
    for (i = 0; i < ncolors; ++i) {
slouken@1895
   628
        output[i].r = gamma[0 * 256 + colors[i].r] >> 8;
slouken@1895
   629
        output[i].g = gamma[1 * 256 + colors[i].g] >> 8;
slouken@1895
   630
        output[i].b = gamma[2 * 256 + colors[i].b] >> 8;
slouken@1895
   631
    }
slouken@0
   632
}
slouken@0
   633
slouken@0
   634
/* Map from Palette to Palette */
slouken@1895
   635
static Uint8 *
slouken@1895
   636
Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
slouken@0
   637
{
slouken@1895
   638
    Uint8 *map;
slouken@1895
   639
    int i;
slouken@0
   640
slouken@1895
   641
    if (identical) {
slouken@1895
   642
        if (src->ncolors <= dst->ncolors) {
slouken@1895
   643
            /* If an identical palette, no need to map */
slouken@1895
   644
            if (src == dst
slouken@1895
   645
                ||
slouken@1895
   646
                (SDL_memcmp
slouken@1895
   647
                 (src->colors, dst->colors,
slouken@1895
   648
                  src->ncolors * sizeof(SDL_Color)) == 0)) {
slouken@1895
   649
                *identical = 1;
slouken@1895
   650
                return (NULL);
slouken@1895
   651
            }
slouken@1895
   652
        }
slouken@1895
   653
        *identical = 0;
slouken@1895
   654
    }
slouken@1895
   655
    map = (Uint8 *) SDL_malloc(src->ncolors);
slouken@1895
   656
    if (map == NULL) {
slouken@1895
   657
        SDL_OutOfMemory();
slouken@1895
   658
        return (NULL);
slouken@1895
   659
    }
slouken@1895
   660
    for (i = 0; i < src->ncolors; ++i) {
slouken@1895
   661
        map[i] = SDL_FindColor(dst,
slouken@1895
   662
                               src->colors[i].r, src->colors[i].g,
slouken@1895
   663
                               src->colors[i].b);
slouken@1895
   664
    }
slouken@1895
   665
    return (map);
slouken@0
   666
}
slouken@1895
   667
slouken@0
   668
/* Map from Palette to BitField */
slouken@1895
   669
static Uint8 *
slouken@1895
   670
Map1toN(SDL_PixelFormat * src, SDL_PixelFormat * dst)
slouken@0
   671
{
slouken@1895
   672
    Uint8 *map;
slouken@1895
   673
    int i;
slouken@1895
   674
    int bpp;
slouken@1895
   675
    unsigned alpha;
slouken@1895
   676
    SDL_Palette *pal = src->palette;
slouken@0
   677
slouken@1895
   678
    bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
slouken@1895
   679
    map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
slouken@1895
   680
    if (map == NULL) {
slouken@1895
   681
        SDL_OutOfMemory();
slouken@1895
   682
        return (NULL);
slouken@1895
   683
    }
slouken@0
   684
slouken@1895
   685
    alpha = dst->Amask ? src->alpha : 0;
slouken@1895
   686
    /* We memory copy to the pixel map so the endianness is preserved */
slouken@1895
   687
    for (i = 0; i < pal->ncolors; ++i) {
slouken@1895
   688
        ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst,
slouken@1895
   689
                      pal->colors[i].r, pal->colors[i].g,
slouken@1895
   690
                      pal->colors[i].b, alpha);
slouken@1895
   691
    }
slouken@1895
   692
    return (map);
slouken@0
   693
}
slouken@1895
   694
slouken@0
   695
/* Map from BitField to Dithered-Palette to Palette */
slouken@1895
   696
static Uint8 *
slouken@1895
   697
MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical)
slouken@0
   698
{
slouken@1895
   699
    /* Generate a 256 color dither palette */
slouken@1895
   700
    SDL_Palette dithered;
slouken@1895
   701
    SDL_Color colors[256];
slouken@1895
   702
    SDL_Palette *pal = dst->palette;
slouken@0
   703
slouken@1895
   704
    dithered.ncolors = 256;
slouken@1895
   705
    SDL_DitherColors(colors, 8);
slouken@1895
   706
    dithered.colors = colors;
slouken@1895
   707
    return (Map1to1(&dithered, pal, identical));
slouken@0
   708
}
slouken@0
   709
slouken@1895
   710
SDL_BlitMap *
slouken@1895
   711
SDL_AllocBlitMap(void)
slouken@0
   712
{
slouken@1895
   713
    SDL_BlitMap *map;
slouken@0
   714
slouken@1895
   715
    /* Allocate the empty map */
slouken@1920
   716
    map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
slouken@1895
   717
    if (map == NULL) {
slouken@1895
   718
        SDL_OutOfMemory();
slouken@1895
   719
        return (NULL);
slouken@1895
   720
    }
slouken@0
   721
slouken@1895
   722
    /* Allocate the software blit data */
slouken@1895
   723
    map->sw_data =
slouken@1920
   724
        (struct private_swaccel *) SDL_calloc(1, sizeof(*map->sw_data));
slouken@1895
   725
    if (map->sw_data == NULL) {
slouken@1895
   726
        SDL_FreeBlitMap(map);
slouken@1895
   727
        SDL_OutOfMemory();
slouken@1895
   728
        return (NULL);
slouken@1895
   729
    }
slouken@0
   730
slouken@1895
   731
    /* It's ready to go */
slouken@1895
   732
    return (map);
slouken@0
   733
}
slouken@1895
   734
slouken@1895
   735
void
slouken@1895
   736
SDL_InvalidateMap(SDL_BlitMap * map)
slouken@0
   737
{
slouken@1895
   738
    if (!map) {
slouken@1895
   739
        return;
slouken@1895
   740
    }
slouken@1895
   741
    map->dst = NULL;
slouken@1895
   742
    map->format_version = (unsigned int) -1;
slouken@1895
   743
    if (map->table) {
slouken@1895
   744
        SDL_free(map->table);
slouken@1895
   745
        map->table = NULL;
slouken@1895
   746
    }
slouken@0
   747
}
slouken@1895
   748
int
slouken@1895
   749
SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)
slouken@0
   750
{
slouken@1895
   751
    SDL_PixelFormat *srcfmt;
slouken@1895
   752
    SDL_PixelFormat *dstfmt;
slouken@1895
   753
    SDL_BlitMap *map;
slouken@0
   754
slouken@1895
   755
    /* Clear out any previous mapping */
slouken@1895
   756
    map = src->map;
slouken@1895
   757
    if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   758
        SDL_UnRLESurface(src, 1);
slouken@1895
   759
    }
slouken@1895
   760
    SDL_InvalidateMap(map);
slouken@0
   761
slouken@1895
   762
    /* Figure out what kind of mapping we're doing */
slouken@1895
   763
    map->identity = 0;
slouken@1895
   764
    srcfmt = src->format;
slouken@1895
   765
    dstfmt = dst->format;
slouken@1895
   766
    switch (srcfmt->BytesPerPixel) {
slouken@1895
   767
    case 1:
slouken@1895
   768
        switch (dstfmt->BytesPerPixel) {
slouken@1895
   769
        case 1:
slouken@1895
   770
            /* Palette --> Palette */
slouken@1895
   771
            map->table =
slouken@1895
   772
                Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
slouken@1895
   773
            if (!map->identity) {
slouken@1895
   774
                if (map->table == NULL) {
slouken@1895
   775
                    return (-1);
slouken@1895
   776
                }
slouken@1895
   777
            }
slouken@1895
   778
            if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
slouken@1895
   779
                map->identity = 0;
slouken@1895
   780
            break;
slouken@0
   781
slouken@1895
   782
        default:
slouken@1895
   783
            /* Palette --> BitField */
slouken@1895
   784
            map->table = Map1toN(srcfmt, dstfmt);
slouken@1895
   785
            if (map->table == NULL) {
slouken@1895
   786
                return (-1);
slouken@1895
   787
            }
slouken@1895
   788
            break;
slouken@1895
   789
        }
slouken@1895
   790
        break;
slouken@1895
   791
    default:
slouken@1895
   792
        switch (dstfmt->BytesPerPixel) {
slouken@1895
   793
        case 1:
slouken@1895
   794
            /* BitField --> Palette */
slouken@1895
   795
            map->table = MapNto1(srcfmt, dstfmt, &map->identity);
slouken@1895
   796
            if (!map->identity) {
slouken@1895
   797
                if (map->table == NULL) {
slouken@1895
   798
                    return (-1);
slouken@1895
   799
                }
slouken@1895
   800
            }
slouken@1895
   801
            map->identity = 0;  /* Don't optimize to copy */
slouken@1895
   802
            break;
slouken@1895
   803
        default:
slouken@1895
   804
            /* BitField --> BitField */
slouken@1895
   805
            if (FORMAT_EQUAL(srcfmt, dstfmt))
slouken@1895
   806
                map->identity = 1;
slouken@1895
   807
            break;
slouken@1895
   808
        }
slouken@1895
   809
        break;
slouken@1895
   810
    }
slouken@0
   811
slouken@1895
   812
    map->dst = dst;
slouken@1895
   813
    map->format_version = dst->format_version;
slouken@0
   814
slouken@1895
   815
    /* Choose your blitters wisely */
slouken@1895
   816
    return (SDL_CalculateBlit(src));
slouken@0
   817
}
slouken@1895
   818
slouken@1895
   819
void
slouken@1895
   820
SDL_FreeBlitMap(SDL_BlitMap * map)
slouken@0
   821
{
slouken@1895
   822
    if (map) {
slouken@1895
   823
        SDL_InvalidateMap(map);
slouken@1895
   824
        if (map->sw_data != NULL) {
slouken@1895
   825
            SDL_free(map->sw_data);
slouken@1895
   826
        }
slouken@1895
   827
        SDL_free(map);
slouken@1895
   828
    }
slouken@0
   829
}
slouken@1895
   830
slouken@1895
   831
/* vi: set ts=4 sw=4 expandtab: */