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