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