src/video/SDL_surface.c
author Sylvain Becker <sylvain.becker@gmail.com>
Tue, 25 Feb 2020 06:53:51 +0100
changeset 13552 2f75c19fd2f2
parent 13551 4efb3eb7a3b3
child 13643 b68bc68d5cce
permissions -rw-r--r--
Fix compilation C90 mixed declarations and code (Thanks Cameron Gutman !)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
#include "SDL_video.h"
slouken@0
    24
#include "SDL_sysvideo.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
#include "SDL_RLEaccel_c.h"
slouken@0
    27
#include "SDL_pixels_c.h"
slouken@11702
    28
#include "SDL_yuv_c.h"
slouken@0
    29
slouken@11574
    30
slouken@11628
    31
/* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow size_t */
slouken@11628
    32
SDL_COMPILE_TIME_ASSERT(surface_size_assumptions,
slouken@11628
    33
    sizeof(int) == sizeof(Sint32) && sizeof(size_t) >= sizeof(Sint32));
slouken@11628
    34
slouken@0
    35
/* Public routines */
slouken@10482
    36
slouken@0
    37
/*
slouken@11702
    38
 * Calculate the pad-aligned scanline width of a surface
slouken@11702
    39
 */
slouken@12242
    40
static int
slouken@11702
    41
SDL_CalculatePitch(Uint32 format, int width)
slouken@11702
    42
{
slouken@11702
    43
    int pitch;
slouken@11702
    44
slouken@13146
    45
    if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_BITSPERPIXEL(format) >= 8) {
slouken@13146
    46
        pitch = (width * SDL_BYTESPERPIXEL(format));
slouken@13146
    47
    } else {
slouken@13146
    48
        pitch = ((width * SDL_BITSPERPIXEL(format)) + 7) / 8;
slouken@13146
    49
    }
slouken@13131
    50
    pitch = (pitch + 3) & ~3;   /* 4-byte aligning for speed */
slouken@11702
    51
    return pitch;
slouken@11702
    52
}
slouken@11702
    53
slouken@11702
    54
/*
slouken@10482
    55
 * Create an empty RGB surface of the appropriate depth using the given
slouken@10482
    56
 * enum SDL_PIXELFORMAT_* format
slouken@0
    57
 */
slouken@1895
    58
SDL_Surface *
slouken@10482
    59
SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
slouken@10482
    60
                               Uint32 format)
slouken@0
    61
{
slouken@1895
    62
    SDL_Surface *surface;
slouken@0
    63
slouken@2807
    64
    /* The flags are no longer used, make the compiler happy */
slouken@4461
    65
    (void)flags;
slouken@2807
    66
slouken@1895
    67
    /* Allocate the surface */
slouken@1920
    68
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
slouken@1895
    69
    if (surface == NULL) {
slouken@1895
    70
        SDL_OutOfMemory();
slouken@1895
    71
        return NULL;
slouken@1895
    72
    }
slouken@940
    73
slouken@5288
    74
    surface->format = SDL_AllocFormat(format);
slouken@1895
    75
    if (!surface->format) {
slouken@1895
    76
        SDL_FreeSurface(surface);
slouken@1895
    77
        return NULL;
slouken@1895
    78
    }
slouken@1895
    79
    surface->w = width;
slouken@1895
    80
    surface->h = height;
slouken@11702
    81
    surface->pitch = SDL_CalculatePitch(format, width);
slouken@1895
    82
    SDL_SetClipRect(surface, NULL);
slouken@1895
    83
slouken@5288
    84
    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
slouken@1895
    85
        SDL_Palette *palette =
slouken@1895
    86
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
slouken@1895
    87
        if (!palette) {
slouken@1895
    88
            SDL_FreeSurface(surface);
slouken@1895
    89
            return NULL;
slouken@1895
    90
        }
slouken@5438
    91
        if (palette->ncolors == 2) {
slouken@1895
    92
            /* Create a black and white bitmap palette */
slouken@1895
    93
            palette->colors[0].r = 0xFF;
slouken@1895
    94
            palette->colors[0].g = 0xFF;
slouken@1895
    95
            palette->colors[0].b = 0xFF;
slouken@1895
    96
            palette->colors[1].r = 0x00;
slouken@1895
    97
            palette->colors[1].g = 0x00;
slouken@1895
    98
            palette->colors[1].b = 0x00;
slouken@1895
    99
        }
slouken@1895
   100
        SDL_SetSurfacePalette(surface, palette);
slouken@1895
   101
        SDL_FreePalette(palette);
slouken@1895
   102
    }
slouken@0
   103
slouken@1895
   104
    /* Get the pixels */
slouken@1895
   105
    if (surface->w && surface->h) {
slouken@11628
   106
        /* Assumptions checked in surface_size_assumptions assert above */
slouken@11628
   107
        Sint64 size = ((Sint64)surface->h * surface->pitch);
slouken@11628
   108
        if (size < 0 || size > SDL_MAX_SINT32) {
slouken@11572
   109
            /* Overflow... */
slouken@11572
   110
            SDL_FreeSurface(surface);
slouken@11572
   111
            SDL_OutOfMemory();
slouken@11572
   112
            return NULL;
slouken@11572
   113
        }
slouken@11572
   114
sylvain@12591
   115
        surface->pixels = SDL_SIMDAlloc((size_t)size);
slouken@1895
   116
        if (!surface->pixels) {
slouken@1895
   117
            SDL_FreeSurface(surface);
slouken@1895
   118
            SDL_OutOfMemory();
slouken@1895
   119
            return NULL;
slouken@1895
   120
        }
sylvain@12591
   121
        surface->flags |= SDL_SIMD_ALIGNED;
slouken@1895
   122
        /* This is important for bitmaps */
slouken@1895
   123
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1895
   124
    }
slouken@0
   125
slouken@1895
   126
    /* Allocate an empty mapping */
slouken@1895
   127
    surface->map = SDL_AllocBlitMap();
slouken@1895
   128
    if (!surface->map) {
slouken@1895
   129
        SDL_FreeSurface(surface);
slouken@1895
   130
        return NULL;
slouken@1895
   131
    }
slouken@1895
   132
slouken@2267
   133
    /* By default surface with an alpha mask are set up for blending */
slouken@10482
   134
    if (surface->format->Amask) {
slouken@2884
   135
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2267
   136
    }
slouken@2267
   137
slouken@1895
   138
    /* The surface is ready to go */
slouken@1895
   139
    surface->refcount = 1;
slouken@1895
   140
    return surface;
slouken@0
   141
}
slouken@1895
   142
slouken@0
   143
/*
slouken@10482
   144
 * Create an empty RGB surface of the appropriate depth
slouken@10482
   145
 */
slouken@10482
   146
SDL_Surface *
slouken@10482
   147
SDL_CreateRGBSurface(Uint32 flags,
slouken@10482
   148
                     int width, int height, int depth,
slouken@10482
   149
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@10482
   150
{
slouken@10482
   151
    Uint32 format;
slouken@10482
   152
slouken@10482
   153
    /* Get the pixel format */
slouken@10482
   154
    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
slouken@10482
   155
    if (format == SDL_PIXELFORMAT_UNKNOWN) {
slouken@10482
   156
        SDL_SetError("Unknown pixel format");
slouken@10482
   157
        return NULL;
slouken@10482
   158
    }
slouken@10482
   159
slouken@10482
   160
    return SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format);
slouken@10482
   161
}
slouken@10482
   162
slouken@10482
   163
/*
slouken@0
   164
 * Create an RGB surface from an existing memory buffer
slouken@0
   165
 */
slouken@1895
   166
SDL_Surface *
slouken@1895
   167
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   168
                         int width, int height, int depth, int pitch,
slouken@1895
   169
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   170
                         Uint32 Amask)
slouken@0
   171
{
slouken@1895
   172
    SDL_Surface *surface;
slouken@1895
   173
slouken@10482
   174
    surface = SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@10482
   175
    if (surface != NULL) {
slouken@10482
   176
        surface->flags |= SDL_PREALLOC;
slouken@10482
   177
        surface->pixels = pixels;
slouken@10482
   178
        surface->w = width;
slouken@10482
   179
        surface->h = height;
slouken@10482
   180
        surface->pitch = pitch;
slouken@10482
   181
        SDL_SetClipRect(surface, NULL);
slouken@10482
   182
    }
slouken@10482
   183
    return surface;
slouken@10482
   184
}
slouken@10482
   185
slouken@10482
   186
/*
slouken@10482
   187
 * Create an RGB surface from an existing memory buffer using the given given
slouken@10482
   188
 * enum SDL_PIXELFORMAT_* format
slouken@10482
   189
 */
slouken@10482
   190
SDL_Surface *
slouken@10482
   191
SDL_CreateRGBSurfaceWithFormatFrom(void *pixels,
slouken@10482
   192
                         int width, int height, int depth, int pitch,
slouken@10482
   193
                         Uint32 format)
slouken@10482
   194
{
slouken@10482
   195
    SDL_Surface *surface;
slouken@10482
   196
slouken@10482
   197
    surface = SDL_CreateRGBSurfaceWithFormat(0, 0, 0, depth, format);
slouken@1895
   198
    if (surface != NULL) {
slouken@1895
   199
        surface->flags |= SDL_PREALLOC;
slouken@1895
   200
        surface->pixels = pixels;
slouken@1895
   201
        surface->w = width;
slouken@1895
   202
        surface->h = height;
slouken@1895
   203
        surface->pitch = pitch;
slouken@1895
   204
        SDL_SetClipRect(surface, NULL);
slouken@1895
   205
    }
slouken@1895
   206
    return surface;
slouken@1895
   207
}
slouken@1895
   208
slouken@1895
   209
int
slouken@1895
   210
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   211
{
slouken@5288
   212
    if (!surface) {
icculus@7037
   213
        return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   214
    }
slouken@8990
   215
    if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
slouken@8990
   216
        return -1;
slouken@8990
   217
    }
slouken@8990
   218
    SDL_InvalidateMap(surface->map);
slouken@8990
   219
slouken@8990
   220
    return 0;
slouken@1895
   221
}
slouken@1895
   222
slouken@2267
   223
int
slouken@2267
   224
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
slouken@0
   225
{
slouken@2267
   226
    int flags;
slouken@0
   227
slouken@2266
   228
    if (!surface) {
slouken@2266
   229
        return -1;
slouken@1895
   230
    }
slouken@0
   231
slouken@2267
   232
    flags = surface->map->info.flags;
slouken@1895
   233
    if (flag) {
slouken@2267
   234
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@1895
   235
    } else {
slouken@2267
   236
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@1895
   237
    }
slouken@2267
   238
    if (surface->map->info.flags != flags) {
slouken@2266
   239
        SDL_InvalidateMap(surface->map);
slouken@2266
   240
    }
slouken@2266
   241
    return 0;
slouken@0
   242
}
slouken@1895
   243
slouken@2267
   244
int
slouken@3560
   245
SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
slouken@0
   246
{
slouken@2266
   247
    int flags;
slouken@0
   248
slouken@2266
   249
    if (!surface) {
slouken@7363
   250
        return SDL_InvalidParamError("surface");
slouken@7363
   251
    }
slouken@7363
   252
icculus@7482
   253
    if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
slouken@7363
   254
        return SDL_InvalidParamError("key");
slouken@2266
   255
    }
slouken@2266
   256
slouken@2266
   257
    if (flag & SDL_RLEACCEL) {
slouken@2266
   258
        SDL_SetSurfaceRLE(surface, 1);
slouken@1895
   259
    }
slouken@0
   260
slouken@2266
   261
    flags = surface->map->info.flags;
slouken@2266
   262
    if (flag) {
slouken@2266
   263
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   264
        surface->map->info.colorkey = key;
slouken@2266
   265
    } else {
slouken@2266
   266
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   267
    }
slouken@2266
   268
    if (surface->map->info.flags != flags) {
slouken@2266
   269
        SDL_InvalidateMap(surface->map);
slouken@2266
   270
    }
slouken@2267
   271
slouken@2266
   272
    return 0;
slouken@2266
   273
}
slouken@2266
   274
slouken@12204
   275
SDL_bool
slouken@12204
   276
SDL_HasColorKey(SDL_Surface * surface)
slouken@12204
   277
{
slouken@12204
   278
    if (!surface) {
slouken@12204
   279
        return SDL_FALSE;
slouken@12204
   280
    }
slouken@12204
   281
slouken@12204
   282
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@12204
   283
        return SDL_FALSE;
slouken@12204
   284
    }
slouken@12204
   285
sylvain@12587
   286
    return SDL_TRUE;
slouken@12204
   287
}
slouken@12204
   288
slouken@3103
   289
int
slouken@3103
   290
SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
slouken@3103
   291
{
slouken@3103
   292
    if (!surface) {
slouken@11772
   293
        return SDL_InvalidParamError("surface");
slouken@3103
   294
    }
slouken@3103
   295
slouken@3103
   296
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@11772
   297
        return SDL_SetError("Surface doesn't have a colorkey");
slouken@3103
   298
    }
slouken@3103
   299
slouken@3103
   300
    if (key) {
slouken@3103
   301
        *key = surface->map->info.colorkey;
slouken@3103
   302
    }
slouken@3103
   303
    return 0;
slouken@3103
   304
}
slouken@3103
   305
slouken@2785
   306
/* This is a fairly slow function to switch from colorkey to alpha */
slouken@2787
   307
static void
sylvain@12566
   308
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface, SDL_bool ignore_alpha)
slouken@2785
   309
{
slouken@2786
   310
    int x, y;
slouken@2785
   311
slouken@2786
   312
    if (!surface) {
slouken@2786
   313
        return;
slouken@2786
   314
    }
slouken@2785
   315
slouken@2786
   316
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
slouken@2786
   317
        !surface->format->Amask) {
slouken@2786
   318
        return;
slouken@2786
   319
    }
slouken@2785
   320
slouken@2786
   321
    SDL_LockSurface(surface);
slouken@2785
   322
slouken@2786
   323
    switch (surface->format->BytesPerPixel) {
slouken@2786
   324
    case 2:
slouken@2786
   325
        {
slouken@2786
   326
            Uint16 *row, *spot;
slouken@2786
   327
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
slouken@2786
   328
            Uint16 mask = (Uint16) (~surface->format->Amask);
slouken@2785
   329
sylvain@12566
   330
            /* Ignore, or not, alpha in colorkey comparison */
sylvain@12566
   331
            if (ignore_alpha) {
sylvain@12566
   332
                ckey &= mask;
sylvain@12566
   333
                row = (Uint16 *) surface->pixels;
sylvain@12566
   334
                for (y = surface->h; y--;) {
sylvain@12566
   335
                    spot = row;
sylvain@12566
   336
                    for (x = surface->w; x--;) {
sylvain@12566
   337
                        if ((*spot & mask) == ckey) {
sylvain@12566
   338
                            *spot &= mask;
sylvain@12566
   339
                        }
sylvain@12566
   340
                        ++spot;
slouken@2786
   341
                    }
sylvain@12566
   342
                    row += surface->pitch / 2;
slouken@2786
   343
                }
sylvain@12566
   344
            } else {
sylvain@12566
   345
                row = (Uint16 *) surface->pixels;
sylvain@12566
   346
                for (y = surface->h; y--;) {
sylvain@12566
   347
                    spot = row;
sylvain@12566
   348
                    for (x = surface->w; x--;) {
sylvain@12566
   349
                        if (*spot == ckey) {
sylvain@12566
   350
                            *spot &= mask;
sylvain@12566
   351
                        }
sylvain@12566
   352
                        ++spot;
sylvain@12566
   353
                    }
sylvain@12566
   354
                    row += surface->pitch / 2;
sylvain@12566
   355
                }
slouken@2786
   356
            }
slouken@2786
   357
        }
slouken@2786
   358
        break;
slouken@2786
   359
    case 3:
slouken@2786
   360
        /* FIXME */
slouken@2786
   361
        break;
slouken@2786
   362
    case 4:
slouken@2786
   363
        {
slouken@2786
   364
            Uint32 *row, *spot;
slouken@2786
   365
            Uint32 ckey = surface->map->info.colorkey;
slouken@2786
   366
            Uint32 mask = ~surface->format->Amask;
slouken@2785
   367
sylvain@12566
   368
            /* Ignore, or not, alpha in colorkey comparison */
sylvain@12566
   369
            if (ignore_alpha) {
sylvain@12566
   370
                ckey &= mask;
sylvain@12566
   371
                row = (Uint32 *) surface->pixels;
sylvain@12566
   372
                for (y = surface->h; y--;) {
sylvain@12566
   373
                    spot = row;
sylvain@12566
   374
                    for (x = surface->w; x--;) {
sylvain@12566
   375
                        if ((*spot & mask) == ckey) {
sylvain@12566
   376
                            *spot &= mask;
sylvain@12566
   377
                        }
sylvain@12566
   378
                        ++spot;
slouken@2786
   379
                    }
sylvain@12566
   380
                    row += surface->pitch / 4;
slouken@2786
   381
                }
sylvain@12566
   382
            } else {
sylvain@12566
   383
                row = (Uint32 *) surface->pixels;
sylvain@12566
   384
                for (y = surface->h; y--;) {
sylvain@12566
   385
                    spot = row;
sylvain@12566
   386
                    for (x = surface->w; x--;) {
sylvain@12566
   387
                        if (*spot == ckey) {
sylvain@12566
   388
                            *spot &= mask;
sylvain@12566
   389
                        }
sylvain@12566
   390
                        ++spot;
sylvain@12566
   391
                    }
sylvain@12566
   392
                    row += surface->pitch / 4;
sylvain@12566
   393
                }
slouken@2786
   394
            }
slouken@2786
   395
        }
slouken@2786
   396
        break;
slouken@2786
   397
    }
slouken@2785
   398
slouken@2786
   399
    SDL_UnlockSurface(surface);
slouken@2785
   400
slouken@2786
   401
    SDL_SetColorKey(surface, 0, 0);
slouken@2884
   402
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2785
   403
}
slouken@2785
   404
slouken@2267
   405
int
slouken@2267
   406
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   407
{
slouken@2266
   408
    int flags;
slouken@2266
   409
slouken@2266
   410
    if (!surface) {
slouken@2266
   411
        return -1;
slouken@1895
   412
    }
slouken@0
   413
slouken@2266
   414
    surface->map->info.r = r;
slouken@2266
   415
    surface->map->info.g = g;
slouken@2266
   416
    surface->map->info.b = b;
slouken@0
   417
slouken@2266
   418
    flags = surface->map->info.flags;
slouken@2266
   419
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   420
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@1895
   421
    } else {
slouken@2266
   422
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@1895
   423
    }
slouken@2266
   424
    if (surface->map->info.flags != flags) {
slouken@1895
   425
        SDL_InvalidateMap(surface->map);
slouken@1895
   426
    }
slouken@2266
   427
    return 0;
slouken@0
   428
}
slouken@1895
   429
slouken@2266
   430
slouken@2267
   431
int
slouken@2267
   432
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@431
   433
{
slouken@2266
   434
    if (!surface) {
slouken@1895
   435
        return -1;
slouken@1895
   436
    }
slouken@2266
   437
slouken@2266
   438
    if (r) {
slouken@2266
   439
        *r = surface->map->info.r;
slouken@2266
   440
    }
slouken@2266
   441
    if (g) {
slouken@2266
   442
        *g = surface->map->info.g;
slouken@2266
   443
    }
slouken@2266
   444
    if (b) {
slouken@2266
   445
        *b = surface->map->info.b;
slouken@1895
   446
    }
slouken@2266
   447
    return 0;
slouken@2266
   448
}
slouken@2266
   449
slouken@2267
   450
int
slouken@2267
   451
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
slouken@2266
   452
{
slouken@2266
   453
    int flags;
slouken@2266
   454
slouken@2266
   455
    if (!surface) {
slouken@2266
   456
        return -1;
slouken@1895
   457
    }
slouken@431
   458
slouken@2266
   459
    surface->map->info.a = alpha;
slouken@2266
   460
slouken@2266
   461
    flags = surface->map->info.flags;
slouken@2266
   462
    if (alpha != 0xFF) {
slouken@2266
   463
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   464
    } else {
slouken@2266
   465
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   466
    }
slouken@2266
   467
    if (surface->map->info.flags != flags) {
slouken@2266
   468
        SDL_InvalidateMap(surface->map);
slouken@1895
   469
    }
slouken@2266
   470
    return 0;
slouken@2266
   471
}
slouken@2266
   472
slouken@2267
   473
int
slouken@2267
   474
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
slouken@2266
   475
{
slouken@2266
   476
    if (!surface) {
slouken@2266
   477
        return -1;
slouken@1895
   478
    }
slouken@2266
   479
slouken@2266
   480
    if (alpha) {
slouken@2266
   481
        *alpha = surface->map->info.a;
slouken@1895
   482
    }
slouken@1895
   483
    return 0;
slouken@431
   484
}
slouken@0
   485
slouken@2267
   486
int
slouken@4929
   487
SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
slouken@2266
   488
{
slouken@2266
   489
    int flags, status;
slouken@2266
   490
slouken@2266
   491
    if (!surface) {
slouken@2266
   492
        return -1;
slouken@2266
   493
    }
slouken@2266
   494
slouken@2266
   495
    status = 0;
slouken@2266
   496
    flags = surface->map->info.flags;
slouken@5184
   497
    surface->map->info.flags &=
slouken@13418
   498
        ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL);
slouken@2266
   499
    switch (blendMode) {
slouken@2884
   500
    case SDL_BLENDMODE_NONE:
slouken@2266
   501
        break;
slouken@2884
   502
    case SDL_BLENDMODE_BLEND:
slouken@2266
   503
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   504
        break;
slouken@2884
   505
    case SDL_BLENDMODE_ADD:
slouken@2266
   506
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   507
        break;
slouken@5184
   508
    case SDL_BLENDMODE_MOD:
slouken@5184
   509
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@5184
   510
        break;
slouken@13418
   511
    case SDL_BLENDMODE_MUL:
slouken@13418
   512
        surface->map->info.flags |= SDL_COPY_MUL;
slouken@13418
   513
        break;
slouken@2266
   514
    default:
icculus@7037
   515
        status = SDL_Unsupported();
slouken@2266
   516
        break;
slouken@2266
   517
    }
slouken@2266
   518
slouken@2266
   519
    if (surface->map->info.flags != flags) {
slouken@2266
   520
        SDL_InvalidateMap(surface->map);
slouken@2266
   521
    }
slouken@2267
   522
slouken@2266
   523
    return status;
slouken@2266
   524
}
slouken@2266
   525
slouken@2267
   526
int
slouken@4929
   527
SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
slouken@2266
   528
{
slouken@2266
   529
    if (!surface) {
slouken@2266
   530
        return -1;
slouken@2266
   531
    }
slouken@2266
   532
slouken@2266
   533
    if (!blendMode) {
slouken@2266
   534
        return 0;
slouken@2266
   535
    }
slouken@2266
   536
slouken@5184
   537
    switch (surface->map->
slouken@13418
   538
            info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL)) {
slouken@2266
   539
    case SDL_COPY_BLEND:
slouken@2884
   540
        *blendMode = SDL_BLENDMODE_BLEND;
slouken@2266
   541
        break;
slouken@2266
   542
    case SDL_COPY_ADD:
slouken@2884
   543
        *blendMode = SDL_BLENDMODE_ADD;
slouken@2266
   544
        break;
slouken@5184
   545
    case SDL_COPY_MOD:
slouken@5184
   546
        *blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   547
        break;
slouken@13418
   548
    case SDL_COPY_MUL:
slouken@13418
   549
        *blendMode = SDL_BLENDMODE_MUL;
slouken@13418
   550
        break;
slouken@2266
   551
    default:
slouken@2884
   552
        *blendMode = SDL_BLENDMODE_NONE;
slouken@2266
   553
        break;
slouken@2266
   554
    }
slouken@2266
   555
    return 0;
slouken@2266
   556
}
slouken@2266
   557
slouken@1895
   558
SDL_bool
slouken@1895
   559
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   560
{
slouken@1895
   561
    SDL_Rect full_rect;
slouken@0
   562
slouken@1895
   563
    /* Don't do anything if there's no surface to act on */
slouken@1895
   564
    if (!surface) {
slouken@1895
   565
        return SDL_FALSE;
slouken@1895
   566
    }
slouken@1895
   567
slouken@1895
   568
    /* Set up the full surface rectangle */
slouken@1895
   569
    full_rect.x = 0;
slouken@1895
   570
    full_rect.y = 0;
slouken@1895
   571
    full_rect.w = surface->w;
slouken@1895
   572
    full_rect.h = surface->h;
slouken@0
   573
slouken@1895
   574
    /* Set the clipping rectangle */
slouken@1895
   575
    if (!rect) {
slouken@1895
   576
        surface->clip_rect = full_rect;
slouken@4966
   577
        return SDL_TRUE;
slouken@1895
   578
    }
slouken@1895
   579
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@1895
   580
}
slouken@0
   581
slouken@1895
   582
void
slouken@1895
   583
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@1895
   584
{
slouken@1895
   585
    if (surface && rect) {
slouken@1895
   586
        *rect = surface->clip_rect;
slouken@1895
   587
    }
slouken@0
   588
}
slouken@1895
   589
slouken@7191
   590
/*
slouken@0
   591
 * Set up a blit between two surfaces -- split into three parts:
slouken@7191
   592
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle
slouken@0
   593
 * verification.  The lower part is a pointer to a low level
slouken@0
   594
 * accelerated blitting function.
slouken@0
   595
 *
slouken@7191
   596
 * These parts are separated out and each used internally by this
slouken@0
   597
 * library in the optimimum places.  They are exported so that if
slouken@0
   598
 * you know exactly what you are doing, you can optimize your code
slouken@0
   599
 * by calling the one(s) you need.
slouken@0
   600
 */
slouken@1895
   601
int
slouken@1895
   602
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   603
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   604
{
slouken@1895
   605
    /* Check to make sure the blit mapping is valid */
slouken@1895
   606
    if ((src->map->dst != dst) ||
slouken@5288
   607
        (dst->format->palette &&
slouken@6166
   608
         src->map->dst_palette_version != dst->format->palette->version) ||
slouken@6166
   609
        (src->format->palette &&
slouken@6166
   610
         src->map->src_palette_version != src->format->palette->version)) {
slouken@1895
   611
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   612
            return (-1);
slouken@1895
   613
        }
bob@2328
   614
        /* just here for debugging */
bob@2329
   615
/*         printf */
bob@2329
   616
/*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
bob@2329
   617
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
bob@2329
   618
/*              dst->map->info.flags, src->map->blit); */
slouken@1895
   619
    }
slouken@2257
   620
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   621
}
slouken@0
   622
slouken@0
   623
slouken@1895
   624
int
slouken@4949
   625
SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
slouken@1895
   626
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   627
{
slouken@1895
   628
    SDL_Rect fulldst;
slouken@1895
   629
    int srcx, srcy, w, h;
slouken@0
   630
slouken@1895
   631
    /* Make sure the surfaces aren't locked */
slouken@1895
   632
    if (!src || !dst) {
icculus@7037
   633
        return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   634
    }
slouken@1895
   635
    if (src->locked || dst->locked) {
icculus@7037
   636
        return SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   637
    }
slouken@0
   638
slouken@1895
   639
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   640
    if (dstrect == NULL) {
slouken@1895
   641
        fulldst.x = fulldst.y = 0;
slouken@7511
   642
        fulldst.w = dst->w;
slouken@7511
   643
        fulldst.h = dst->h;
slouken@1895
   644
        dstrect = &fulldst;
slouken@1895
   645
    }
slouken@1895
   646
slouken@1895
   647
    /* clip the source rectangle to the source surface */
slouken@1895
   648
    if (srcrect) {
slouken@1895
   649
        int maxw, maxh;
slouken@0
   650
slouken@1895
   651
        srcx = srcrect->x;
slouken@1895
   652
        w = srcrect->w;
slouken@1895
   653
        if (srcx < 0) {
slouken@1895
   654
            w += srcx;
slouken@1895
   655
            dstrect->x -= srcx;
slouken@1895
   656
            srcx = 0;
slouken@1895
   657
        }
slouken@1895
   658
        maxw = src->w - srcx;
slouken@1895
   659
        if (maxw < w)
slouken@1895
   660
            w = maxw;
slouken@0
   661
slouken@1895
   662
        srcy = srcrect->y;
slouken@1895
   663
        h = srcrect->h;
slouken@1895
   664
        if (srcy < 0) {
slouken@1895
   665
            h += srcy;
slouken@1895
   666
            dstrect->y -= srcy;
slouken@1895
   667
            srcy = 0;
slouken@1895
   668
        }
slouken@1895
   669
        maxh = src->h - srcy;
slouken@1895
   670
        if (maxh < h)
slouken@1895
   671
            h = maxh;
slouken@0
   672
slouken@1895
   673
    } else {
slouken@1895
   674
        srcx = srcy = 0;
slouken@1895
   675
        w = src->w;
slouken@1895
   676
        h = src->h;
slouken@1895
   677
    }
slouken@1895
   678
slouken@1895
   679
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   680
    {
slouken@1895
   681
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   682
        int dx, dy;
slouken@0
   683
slouken@1895
   684
        dx = clip->x - dstrect->x;
slouken@1895
   685
        if (dx > 0) {
slouken@1895
   686
            w -= dx;
slouken@1895
   687
            dstrect->x += dx;
slouken@1895
   688
            srcx += dx;
slouken@1895
   689
        }
slouken@1895
   690
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   691
        if (dx > 0)
slouken@1895
   692
            w -= dx;
slouken@0
   693
slouken@1895
   694
        dy = clip->y - dstrect->y;
slouken@1895
   695
        if (dy > 0) {
slouken@1895
   696
            h -= dy;
slouken@1895
   697
            dstrect->y += dy;
slouken@1895
   698
            srcy += dy;
slouken@1895
   699
        }
slouken@1895
   700
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   701
        if (dy > 0)
slouken@1895
   702
            h -= dy;
slouken@1895
   703
    }
slouken@0
   704
slouken@7834
   705
    /* Switch back to a fast blit if we were previously stretching */
slouken@7834
   706
    if (src->map->info.flags & SDL_COPY_NEAREST) {
slouken@7834
   707
        src->map->info.flags &= ~SDL_COPY_NEAREST;
slouken@7834
   708
        SDL_InvalidateMap(src->map);
slouken@7834
   709
    }
slouken@7834
   710
slouken@1895
   711
    if (w > 0 && h > 0) {
slouken@1895
   712
        SDL_Rect sr;
slouken@1895
   713
        sr.x = srcx;
slouken@1895
   714
        sr.y = srcy;
slouken@1895
   715
        sr.w = dstrect->w = w;
slouken@1895
   716
        sr.h = dstrect->h = h;
slouken@1895
   717
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   718
    }
slouken@1895
   719
    dstrect->w = dstrect->h = 0;
slouken@1895
   720
    return 0;
slouken@0
   721
}
slouken@0
   722
ken@5296
   723
int
ken@5499
   724
SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
ken@5499
   725
              SDL_Surface * dst, SDL_Rect * dstrect)
ken@5499
   726
{
slouken@9076
   727
    double src_x0, src_y0, src_x1, src_y1;
slouken@9076
   728
    double dst_x0, dst_y0, dst_x1, dst_y1;
slouken@9076
   729
    SDL_Rect final_src, final_dst;
slouken@9076
   730
    double scaling_w, scaling_h;
slouken@9076
   731
    int src_w, src_h;
slouken@9076
   732
    int dst_w, dst_h;
ken@5499
   733
ken@5499
   734
    /* Make sure the surfaces aren't locked */
ken@5499
   735
    if (!src || !dst) {
icculus@7037
   736
        return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
ken@5499
   737
    }
ken@5499
   738
    if (src->locked || dst->locked) {
icculus@7037
   739
        return SDL_SetError("Surfaces must not be locked during blit");
ken@5499
   740
    }
ken@5499
   741
slouken@9076
   742
    if (NULL == srcrect) {
slouken@9076
   743
        src_w = src->w;
slouken@9076
   744
        src_h = src->h;
slouken@9076
   745
    } else {
slouken@9076
   746
        src_w = srcrect->w;
slouken@9076
   747
        src_h = srcrect->h;
slouken@9076
   748
    }
slouken@9076
   749
slouken@9076
   750
    if (NULL == dstrect) {
slouken@9076
   751
        dst_w = dst->w;
slouken@9076
   752
        dst_h = dst->h;
slouken@9076
   753
    } else {
slouken@9076
   754
        dst_w = dstrect->w;
slouken@9076
   755
        dst_h = dstrect->h;
slouken@9076
   756
    }
slouken@9076
   757
slouken@9076
   758
    if (dst_w == src_w && dst_h == src_h) {
slouken@9076
   759
        /* No scaling, defer to regular blit */
slouken@9076
   760
        return SDL_BlitSurface(src, srcrect, dst, dstrect);
slouken@9076
   761
    }
slouken@9076
   762
slouken@9076
   763
    scaling_w = (double)dst_w / src_w;
slouken@9076
   764
    scaling_h = (double)dst_h / src_h;
slouken@9076
   765
slouken@9076
   766
    if (NULL == dstrect) {
slouken@9076
   767
        dst_x0 = 0;
slouken@9076
   768
        dst_y0 = 0;
slouken@9076
   769
        dst_x1 = dst_w - 1;
slouken@9076
   770
        dst_y1 = dst_h - 1;
slouken@9076
   771
    } else {
slouken@9076
   772
        dst_x0 = dstrect->x;
slouken@9076
   773
        dst_y0 = dstrect->y;
slouken@9076
   774
        dst_x1 = dst_x0 + dst_w - 1;
slouken@9076
   775
        dst_y1 = dst_y0 + dst_h - 1;
ken@5499
   776
    }
ken@5499
   777
slouken@9076
   778
    if (NULL == srcrect) {
slouken@9076
   779
        src_x0 = 0;
slouken@9076
   780
        src_y0 = 0;
slouken@9076
   781
        src_x1 = src_w - 1;
slouken@9076
   782
        src_y1 = src_h - 1;
slouken@9076
   783
    } else {
slouken@9076
   784
        src_x0 = srcrect->x;
slouken@9076
   785
        src_y0 = srcrect->y;
slouken@9076
   786
        src_x1 = src_x0 + src_w - 1;
slouken@9076
   787
        src_y1 = src_y0 + src_h - 1;
ken@5499
   788
slouken@9076
   789
        /* Clip source rectangle to the source surface */
slouken@9076
   790
slouken@9076
   791
        if (src_x0 < 0) {
slouken@9076
   792
            dst_x0 -= src_x0 * scaling_w;
slouken@9076
   793
            src_x0 = 0;
ken@5499
   794
        }
slouken@9076
   795
slouken@9076
   796
        if (src_x1 >= src->w) {
slouken@9076
   797
            dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
slouken@9076
   798
            src_x1 = src->w - 1;
slouken@9076
   799
        }
ken@5499
   800
slouken@9076
   801
        if (src_y0 < 0) {
slouken@9076
   802
            dst_y0 -= src_y0 * scaling_h;
slouken@9076
   803
            src_y0 = 0;
slouken@9076
   804
        }
slouken@9076
   805
slouken@9076
   806
        if (src_y1 >= src->h) {
slouken@9076
   807
            dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
slouken@9076
   808
            src_y1 = src->h - 1;
ken@5499
   809
        }
slouken@9076
   810
    }
slouken@9076
   811
slouken@9076
   812
    /* Clip destination rectangle to the clip rectangle */
ken@5499
   813
slouken@9076
   814
    /* Translate to clip space for easier calculations */
slouken@9076
   815
    dst_x0 -= dst->clip_rect.x;
slouken@9076
   816
    dst_x1 -= dst->clip_rect.x;
slouken@9076
   817
    dst_y0 -= dst->clip_rect.y;
slouken@9076
   818
    dst_y1 -= dst->clip_rect.y;
slouken@9076
   819
slouken@9076
   820
    if (dst_x0 < 0) {
slouken@9076
   821
        src_x0 -= dst_x0 / scaling_w;
slouken@9076
   822
        dst_x0 = 0;
ken@5499
   823
    }
ken@5499
   824
slouken@9076
   825
    if (dst_x1 >= dst->clip_rect.w) {
slouken@9076
   826
        src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
slouken@9076
   827
        dst_x1 = dst->clip_rect.w - 1;
slouken@9076
   828
    }
ken@5499
   829
slouken@9076
   830
    if (dst_y0 < 0) {
slouken@9076
   831
        src_y0 -= dst_y0 / scaling_h;
slouken@9076
   832
        dst_y0 = 0;
slouken@9076
   833
    }
slouken@9076
   834
slouken@9076
   835
    if (dst_y1 >= dst->clip_rect.h) {
slouken@9076
   836
        src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
slouken@9076
   837
        dst_y1 = dst->clip_rect.h - 1;
ken@5499
   838
    }
ken@5499
   839
slouken@9076
   840
    /* Translate back to surface coordinates */
slouken@9076
   841
    dst_x0 += dst->clip_rect.x;
slouken@9076
   842
    dst_x1 += dst->clip_rect.x;
slouken@9076
   843
    dst_y0 += dst->clip_rect.y;
slouken@9076
   844
    dst_y1 += dst->clip_rect.y;
slouken@9076
   845
philipp@9267
   846
    final_src.x = (int)SDL_floor(src_x0 + 0.5);
philipp@9267
   847
    final_src.y = (int)SDL_floor(src_y0 + 0.5);
slouken@10668
   848
    final_src.w = (int)SDL_floor(src_x1 + 1 + 0.5) - (int)SDL_floor(src_x0 + 0.5);
slouken@10668
   849
    final_src.h = (int)SDL_floor(src_y1 + 1 + 0.5) - (int)SDL_floor(src_y0 + 0.5);
slouken@9076
   850
philipp@9267
   851
    final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
philipp@9267
   852
    final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
philipp@9267
   853
    final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
philipp@9267
   854
    final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
slouken@9076
   855
slouken@9076
   856
    if (final_dst.w < 0)
slouken@9076
   857
        final_dst.w = 0;
slouken@9076
   858
    if (final_dst.h < 0)
slouken@9076
   859
        final_dst.h = 0;
slouken@9076
   860
slouken@9076
   861
    if (dstrect)
slouken@9076
   862
        *dstrect = final_dst;
slouken@9076
   863
slouken@9076
   864
    if (final_dst.w == 0 || final_dst.h == 0 ||
slouken@9076
   865
        final_src.w <= 0 || final_src.h <= 0) {
slouken@9076
   866
        /* No-op. */
slouken@9076
   867
        return 0;
ken@5499
   868
    }
ken@5499
   869
slouken@9076
   870
    return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
ken@5499
   871
}
ken@5499
   872
ken@5499
   873
/**
ken@5499
   874
 *  This is a semi-private blit function and it performs low-level surface
ken@5499
   875
 *  scaled blitting only.
ken@5499
   876
 */
ken@5499
   877
int
ken@5499
   878
SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
ken@5499
   879
                SDL_Surface * dst, SDL_Rect * dstrect)
ken@5296
   880
{
slouken@6526
   881
    static const Uint32 complex_copy_flags = (
slouken@6526
   882
        SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
slouken@13418
   883
        SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD | SDL_COPY_MUL |
slouken@6526
   884
        SDL_COPY_COLORKEY
slouken@6526
   885
    );
slouken@6526
   886
slouken@7834
   887
    if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
slouken@7834
   888
        src->map->info.flags |= SDL_COPY_NEAREST;
slouken@7834
   889
        SDL_InvalidateMap(src->map);
slouken@7834
   890
    }
ken@5296
   891
slouken@6526
   892
    if ( !(src->map->info.flags & complex_copy_flags) &&
slouken@7191
   893
         src->format->format == dst->format->format &&
slouken@6526
   894
         !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
slouken@9076
   895
        return SDL_SoftStretch( src, srcrect, dst, dstrect );
ken@5296
   896
    } else {
slouken@9076
   897
        return SDL_LowerBlit( src, srcrect, dst, dstrect );
ken@5296
   898
    }
ken@5296
   899
}
ken@5296
   900
ken@5296
   901
/*
slouken@0
   902
 * Lock a surface to directly access the pixels
slouken@0
   903
 */
slouken@1895
   904
int
slouken@1895
   905
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   906
{
slouken@1895
   907
    if (!surface->locked) {
sylvain@13433
   908
#if SDL_HAVE_RLE
slouken@1895
   909
        /* Perform the lock */
slouken@1895
   910
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   911
            SDL_UnRLESurface(surface, 1);
slouken@1895
   912
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   913
        }
sylvain@13433
   914
#endif
slouken@1895
   915
    }
slouken@0
   916
slouken@1895
   917
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   918
    ++surface->locked;
slouken@0
   919
slouken@1895
   920
    /* Ready to go.. */
slouken@1895
   921
    return (0);
slouken@0
   922
}
slouken@1895
   923
slouken@0
   924
/*
slouken@0
   925
 * Unlock a previously locked surface
slouken@0
   926
 */
slouken@1895
   927
void
slouken@1895
   928
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   929
{
slouken@1895
   930
    /* Only perform an unlock if we are locked */
slouken@1895
   931
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   932
        return;
slouken@1895
   933
    }
slouken@0
   934
sylvain@13433
   935
#if SDL_HAVE_RLE
slouken@1895
   936
    /* Update RLE encoded surface with new data */
slouken@1895
   937
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   938
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   939
        SDL_RLESurface(surface);
slouken@1895
   940
    }
sylvain@13433
   941
#endif
slouken@0
   942
}
slouken@0
   943
slouken@7191
   944
/*
slouken@11289
   945
 * Creates a new surface identical to the existing surface
slouken@11289
   946
 */
slouken@11289
   947
SDL_Surface *
slouken@11289
   948
SDL_DuplicateSurface(SDL_Surface * surface)
slouken@11289
   949
{
slouken@11289
   950
    return SDL_ConvertSurface(surface, surface->format, surface->flags);
slouken@11289
   951
}
slouken@11289
   952
slouken@11289
   953
/*
slouken@0
   954
 * Convert a surface into the specified pixel format.
slouken@0
   955
 */
slouken@1895
   956
SDL_Surface *
icculus@7725
   957
SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
slouken@2807
   958
                   Uint32 flags)
slouken@0
   959
{
slouken@1895
   960
    SDL_Surface *convert;
slouken@2266
   961
    Uint32 copy_flags;
slouken@7601
   962
    SDL_Color copy_color;
slouken@1895
   963
    SDL_Rect bounds;
sylvain@12584
   964
    int ret;
sylvain@13548
   965
    SDL_bool palette_ck_transform = SDL_FALSE;
sylvain@13071
   966
    int palette_ck_value = 0;
sylvain@13548
   967
    SDL_bool palette_has_alpha = SDL_FALSE;
sylvain@13551
   968
    Uint8 *palette_saved_alpha = NULL;
slouken@0
   969
slouken@11289
   970
    if (!surface) {
slouken@11289
   971
        SDL_InvalidParamError("surface");
slouken@11289
   972
        return NULL;
slouken@11289
   973
    }
slouken@11289
   974
    if (!format) {
slouken@11289
   975
        SDL_InvalidParamError("format");
slouken@11289
   976
        return NULL;
slouken@11289
   977
    }
slouken@11289
   978
slouken@1895
   979
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   980
    if (format->palette != NULL) {
slouken@1895
   981
        int i;
slouken@1895
   982
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   983
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   984
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   985
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   986
                break;
slouken@1895
   987
        }
slouken@1895
   988
        if (i == format->palette->ncolors) {
slouken@1895
   989
            SDL_SetError("Empty destination palette");
slouken@1895
   990
            return (NULL);
slouken@1895
   991
        }
slouken@1895
   992
    }
slouken@264
   993
slouken@1895
   994
    /* Create a new surface with the desired format */
slouken@2807
   995
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
slouken@1895
   996
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   997
                                   format->Gmask, format->Bmask,
slouken@1895
   998
                                   format->Amask);
slouken@1895
   999
    if (convert == NULL) {
slouken@1895
  1000
        return (NULL);
slouken@1895
  1001
    }
slouken@0
  1002
slouken@1895
  1003
    /* Copy the palette if any */
slouken@1895
  1004
    if (format->palette && convert->format->palette) {
slouken@1895
  1005
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
  1006
                   format->palette->colors,
slouken@1895
  1007
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
  1008
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
  1009
    }
slouken@0
  1010
slouken@2266
  1011
    /* Save the original copy flags */
slouken@2266
  1012
    copy_flags = surface->map->info.flags;
slouken@7601
  1013
    copy_color.r = surface->map->info.r;
slouken@7601
  1014
    copy_color.g = surface->map->info.g;
slouken@7601
  1015
    copy_color.b = surface->map->info.b;
slouken@7601
  1016
    copy_color.a = surface->map->info.a;
slouken@7601
  1017
    surface->map->info.r = 0xFF;
slouken@7601
  1018
    surface->map->info.g = 0xFF;
slouken@7601
  1019
    surface->map->info.b = 0xFF;
slouken@7601
  1020
    surface->map->info.a = 0xFF;
slouken@2266
  1021
    surface->map->info.flags = 0;
slouken@7601
  1022
    SDL_InvalidateMap(surface->map);
slouken@0
  1023
slouken@1895
  1024
    /* Copy over the image data */
slouken@1895
  1025
    bounds.x = 0;
slouken@1895
  1026
    bounds.y = 0;
slouken@1895
  1027
    bounds.w = surface->w;
slouken@1895
  1028
    bounds.h = surface->h;
sylvain@13071
  1029
sylvain@13551
  1030
    /* Source surface has a palette with no real alpha (0 or OPAQUE).
sylvain@13551
  1031
     * Destination format has alpha.
sylvain@13551
  1032
     * -> set alpha channel to be opaque */
sylvain@13551
  1033
    if (surface->format->palette && format->Amask) {
sylvain@13551
  1034
        SDL_bool set_opaque = SDL_FALSE;
sylvain@13551
  1035
        {
sylvain@13551
  1036
            int i;
sylvain@13551
  1037
            for (i = 0; i < surface->format->palette->ncolors; i++) {
sylvain@13551
  1038
                Uint8 alpha_value = surface->format->palette->colors[i].a;
sylvain@13551
  1039
sylvain@13551
  1040
                if (alpha_value != 0 && alpha_value != SDL_ALPHA_OPAQUE) {
sylvain@13551
  1041
                    /* Palette has at least one alpha value. Don't do anything */
sylvain@13551
  1042
                    set_opaque = SDL_FALSE;
sylvain@13551
  1043
                    palette_has_alpha = SDL_TRUE;
sylvain@13551
  1044
                    break;
sylvain@13551
  1045
                }
sylvain@13551
  1046
sylvain@13551
  1047
                if (alpha_value == 0) {
sylvain@13551
  1048
                    set_opaque = SDL_TRUE;
sylvain@13551
  1049
                }
sylvain@13551
  1050
            }
sylvain@13551
  1051
        }
sylvain@13551
  1052
sylvain@13551
  1053
        /* Set opaque and backup palette alpha values */
sylvain@13551
  1054
        if (set_opaque) {
sylvain@13552
  1055
            int i;
sylvain@13551
  1056
            palette_saved_alpha = SDL_stack_alloc(Uint8, surface->format->palette->ncolors);
sylvain@13551
  1057
            for (i = 0; i < surface->format->palette->ncolors; i++) {
sylvain@13551
  1058
                palette_saved_alpha[i] = surface->format->palette->colors[i].a;
sylvain@13551
  1059
                surface->format->palette->colors[i].a = SDL_ALPHA_OPAQUE;
sylvain@13551
  1060
            }
sylvain@13551
  1061
        }
sylvain@13551
  1062
    }
sylvain@13551
  1063
sylvain@13071
  1064
    /* Transform colorkey to alpha. for cases where source palette has duplicate values, and colorkey is one of them */
sylvain@13071
  1065
    if (copy_flags & SDL_COPY_COLORKEY) {
sylvain@13071
  1066
        if (surface->format->palette && !format->palette) {
sylvain@13548
  1067
            palette_ck_transform = SDL_TRUE;
sylvain@13547
  1068
            palette_has_alpha = SDL_TRUE;
sylvain@13071
  1069
            palette_ck_value = surface->format->palette->colors[surface->map->info.colorkey].a;
sylvain@13071
  1070
            surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
sylvain@13071
  1071
        }
sylvain@13071
  1072
    }
sylvain@13071
  1073
sylvain@12584
  1074
    ret = SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
  1075
sylvain@13551
  1076
    /* Restore colorkey alpha value */
sylvain@13071
  1077
    if (palette_ck_transform) {
sylvain@13071
  1078
        surface->format->palette->colors[surface->map->info.colorkey].a = palette_ck_value;
sylvain@13071
  1079
    }
sylvain@13071
  1080
sylvain@13551
  1081
    /* Restore palette alpha values */
sylvain@13551
  1082
    if (palette_saved_alpha) {
sylvain@13551
  1083
        int i;
sylvain@13551
  1084
        for (i = 0; i < surface->format->palette->ncolors; i++) {
sylvain@13551
  1085
            surface->format->palette->colors[i].a = palette_saved_alpha[i];
sylvain@13551
  1086
        }
sylvain@13551
  1087
        SDL_stack_free(palette_saved_alpha);
sylvain@13551
  1088
    }
sylvain@13551
  1089
slouken@1895
  1090
    /* Clean up the original surface, and update converted surface */
slouken@7601
  1091
    convert->map->info.r = copy_color.r;
slouken@7601
  1092
    convert->map->info.g = copy_color.g;
slouken@7601
  1093
    convert->map->info.b = copy_color.b;
slouken@7601
  1094
    convert->map->info.a = copy_color.a;
slouken@2824
  1095
    convert->map->info.flags =
slouken@2824
  1096
        (copy_flags &
slouken@2824
  1097
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
slouken@2824
  1098
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
slouken@2824
  1099
           SDL_COPY_RLE_ALPHAKEY));
slouken@7601
  1100
    surface->map->info.r = copy_color.r;
slouken@7601
  1101
    surface->map->info.g = copy_color.g;
slouken@7601
  1102
    surface->map->info.b = copy_color.b;
slouken@7601
  1103
    surface->map->info.a = copy_color.a;
slouken@2824
  1104
    surface->map->info.flags = copy_flags;
slouken@7601
  1105
    SDL_InvalidateMap(surface->map);
sylvain@12584
  1106
sylvain@12584
  1107
    /* SDL_LowerBlit failed, and so the conversion */
sylvain@12584
  1108
    if (ret < 0) {
sylvain@12584
  1109
        SDL_FreeSurface(convert);
sylvain@12584
  1110
        return NULL;
sylvain@12584
  1111
    }
sylvain@12584
  1112
slouken@2266
  1113
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@7023
  1114
        SDL_bool set_colorkey_by_color = SDL_FALSE;
slouken@0
  1115
slouken@7023
  1116
        if (surface->format->palette) {
slouken@7191
  1117
            if (format->palette &&
slouken@7023
  1118
                surface->format->palette->ncolors <= format->palette->ncolors &&
slouken@7023
  1119
                (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
slouken@7023
  1120
                  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
slouken@7023
  1121
                /* The palette is identical, just set the same colorkey */
slouken@7023
  1122
                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
sylvain@13071
  1123
            } else if (!format->palette) {
sylvain@13071
  1124
                /* Was done by 'palette_ck_transform' */
slouken@7023
  1125
            } else {
slouken@7023
  1126
                set_colorkey_by_color = SDL_TRUE;
slouken@7023
  1127
            }
slouken@7023
  1128
        } else {
slouken@7023
  1129
            set_colorkey_by_color = SDL_TRUE;
slouken@7023
  1130
        }
slouken@7023
  1131
slouken@7023
  1132
        if (set_colorkey_by_color) {
slouken@11253
  1133
            SDL_Surface *tmp;
slouken@11253
  1134
            SDL_Surface *tmp2;
slouken@11253
  1135
            int converted_colorkey = 0;
slouken@11253
  1136
slouken@11253
  1137
            /* Create a dummy surface to get the colorkey converted */
slouken@11253
  1138
            tmp = SDL_CreateRGBSurface(0, 1, 1,
slouken@11253
  1139
                                   surface->format->BitsPerPixel, surface->format->Rmask,
slouken@11253
  1140
                                   surface->format->Gmask, surface->format->Bmask,
slouken@11253
  1141
                                   surface->format->Amask);
slouken@11253
  1142
icculus@11496
  1143
            /* Share the palette, if any */
icculus@11496
  1144
            if (surface->format->palette) {
icculus@11496
  1145
                SDL_SetSurfacePalette(tmp, surface->format->palette);
icculus@11496
  1146
            }
sylvain@12566
  1147
slouken@11253
  1148
            SDL_FillRect(tmp, NULL, surface->map->info.colorkey);
slouken@7023
  1149
slouken@11253
  1150
            tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@11253
  1151
slouken@11253
  1152
            /* Convertion of the colorkey */
slouken@11253
  1153
            tmp2 = SDL_ConvertSurface(tmp, format, 0);
slouken@11253
  1154
slouken@11253
  1155
            /* Get the converted colorkey */
slouken@11254
  1156
            SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->format->BytesPerPixel);
slouken@11253
  1157
slouken@11253
  1158
            SDL_FreeSurface(tmp);
slouken@11253
  1159
            SDL_FreeSurface(tmp2);
slouken@11253
  1160
slouken@11253
  1161
            /* Set the converted colorkey on the new surface */
slouken@11253
  1162
            SDL_SetColorKey(convert, 1, converted_colorkey);
slouken@11253
  1163
slouken@7023
  1164
            /* This is needed when converting for 3D texture upload */
sylvain@13547
  1165
            SDL_ConvertColorkeyToAlpha(convert, SDL_TRUE);
slouken@7023
  1166
        }
slouken@1895
  1167
    }
slouken@2824
  1168
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@2266
  1169
slouken@2266
  1170
    /* Enable alpha blending by default if the new surface has an
slouken@2266
  1171
     * alpha channel or alpha modulation */
slouken@2824
  1172
    if ((surface->format->Amask && format->Amask) ||
sylvain@13547
  1173
        (palette_has_alpha && format->Amask) ||
slouken@11246
  1174
        (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
slouken@2884
  1175
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
slouken@1895
  1176
    }
slouken@2824
  1177
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
slouken@2824
  1178
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
slouken@2824
  1179
    }
slouken@0
  1180
slouken@1895
  1181
    /* We're ready to go! */
slouken@1895
  1182
    return (convert);
slouken@0
  1183
}
slouken@0
  1184
slouken@5375
  1185
SDL_Surface *
slouken@5375
  1186
SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
slouken@5375
  1187
                         Uint32 flags)
slouken@5375
  1188
{
slouken@5375
  1189
    SDL_PixelFormat *fmt;
slouken@5515
  1190
    SDL_Surface *convert = NULL;
slouken@5375
  1191
slouken@5375
  1192
    fmt = SDL_AllocFormat(pixel_format);
slouken@5375
  1193
    if (fmt) {
slouken@5375
  1194
        convert = SDL_ConvertSurface(surface, fmt, flags);
slouken@5375
  1195
        SDL_FreeFormat(fmt);
slouken@5375
  1196
    }
slouken@5375
  1197
    return convert;
slouken@5375
  1198
}
slouken@5375
  1199
slouken@0
  1200
/*
slouken@3434
  1201
 * Create a surface on the stack for quick blit operations
slouken@3434
  1202
 */
slouken@7860
  1203
static SDL_INLINE SDL_bool
slouken@3434
  1204
SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
slouken@7191
  1205
                         void * pixels, int pitch, SDL_Surface * surface,
slouken@3434
  1206
                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
slouken@3434
  1207
{
slouken@5288
  1208
    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
  1209
        SDL_SetError("Indexed pixel formats not supported");
slouken@3434
  1210
        return SDL_FALSE;
slouken@3434
  1211
    }
slouken@5288
  1212
    if (SDL_InitFormat(format, pixel_format) < 0) {
slouken@3434
  1213
        return SDL_FALSE;
slouken@3434
  1214
    }
slouken@3434
  1215
slouken@3434
  1216
    SDL_zerop(surface);
slouken@3434
  1217
    surface->flags = SDL_PREALLOC;
slouken@5288
  1218
    surface->format = format;
slouken@3434
  1219
    surface->pixels = pixels;
slouken@3434
  1220
    surface->w = width;
slouken@3434
  1221
    surface->h = height;
slouken@3434
  1222
    surface->pitch = pitch;
slouken@3434
  1223
    /* We don't actually need to set up the clip rect for our purposes */
gabomdq@7678
  1224
    /* SDL_SetClipRect(surface, NULL); */
slouken@3434
  1225
slouken@3434
  1226
    /* Allocate an empty mapping */
slouken@3434
  1227
    SDL_zerop(blitmap);
slouken@3434
  1228
    blitmap->info.r = 0xFF;
slouken@3434
  1229
    blitmap->info.g = 0xFF;
slouken@3434
  1230
    blitmap->info.b = 0xFF;
slouken@3434
  1231
    blitmap->info.a = 0xFF;
slouken@3434
  1232
    surface->map = blitmap;
slouken@3434
  1233
slouken@3434
  1234
    /* The surface is ready to go */
slouken@3434
  1235
    surface->refcount = 1;
slouken@3434
  1236
    return SDL_TRUE;
slouken@3434
  1237
}
slouken@3434
  1238
slouken@3434
  1239
/*
slouken@3434
  1240
 * Copy a block of pixels of one format to another format
slouken@3434
  1241
 */
slouken@3434
  1242
int SDL_ConvertPixels(int width, int height,
slouken@3434
  1243
                      Uint32 src_format, const void * src, int src_pitch,
slouken@3434
  1244
                      Uint32 dst_format, void * dst, int dst_pitch)
slouken@3434
  1245
{
slouken@3434
  1246
    SDL_Surface src_surface, dst_surface;
slouken@3434
  1247
    SDL_PixelFormat src_fmt, dst_fmt;
slouken@3434
  1248
    SDL_BlitMap src_blitmap, dst_blitmap;
slouken@3434
  1249
    SDL_Rect rect;
icculus@6407
  1250
    void *nonconst_src = (void *) src;
slouken@3434
  1251
slouken@9046
  1252
    /* Check to make sure we are blitting somewhere, so we don't crash */
slouken@6920
  1253
    if (!dst) {
icculus@7037
  1254
        return SDL_InvalidParamError("dst");
slouken@6920
  1255
    }
slouken@6920
  1256
    if (!dst_pitch) {
icculus@7037
  1257
        return SDL_InvalidParamError("dst_pitch");
slouken@6920
  1258
    }
slouken@6920
  1259
sylvain@13433
  1260
#if SDL_HAVE_YUV
slouken@11702
  1261
    if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
slouken@11702
  1262
        return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
slouken@11702
  1263
    } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
slouken@11702
  1264
        return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
slouken@11702
  1265
    } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
slouken@11702
  1266
        return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
slouken@11702
  1267
    }
sylvain@13433
  1268
#else
sylvain@13433
  1269
    if (SDL_ISPIXELFORMAT_FOURCC(src_format) || SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
sylvain@13433
  1270
        SDL_SetError("SDL not built with YUV support");
sylvain@13433
  1271
        return -1;
sylvain@13433
  1272
    }
sylvain@13433
  1273
#endif
slouken@11702
  1274
slouken@3434
  1275
    /* Fast path for same format copy */
slouken@3434
  1276
    if (src_format == dst_format) {
slouken@11574
  1277
        int i;
slouken@11702
  1278
        const int bpp = SDL_BYTESPERPIXEL(src_format);
slouken@11702
  1279
        width *= bpp;
slouken@11702
  1280
        for (i = height; i--;) {
slouken@11702
  1281
            SDL_memcpy(dst, src, width);
slouken@11702
  1282
            src = (const Uint8*)src + src_pitch;
slouken@11702
  1283
            dst = (Uint8*)dst + dst_pitch;
slouken@9046
  1284
        }
slouken@6041
  1285
        return 0;
slouken@3434
  1286
    }
slouken@3434
  1287
icculus@6407
  1288
    if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
slouken@3434
  1289
                                  src_pitch,
slouken@3434
  1290
                                  &src_surface, &src_fmt, &src_blitmap)) {
slouken@3434
  1291
        return -1;
slouken@3434
  1292
    }
slouken@3434
  1293
    if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
slouken@3434
  1294
                                  &dst_surface, &dst_fmt, &dst_blitmap)) {
slouken@3434
  1295
        return -1;
slouken@3434
  1296
    }
slouken@3434
  1297
slouken@3434
  1298
    /* Set up the rect and go! */
slouken@3434
  1299
    rect.x = 0;
slouken@3434
  1300
    rect.y = 0;
slouken@3434
  1301
    rect.w = width;
slouken@3436
  1302
    rect.h = height;
slouken@3434
  1303
    return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
slouken@3434
  1304
}
slouken@3434
  1305
slouken@3434
  1306
/*
slouken@0
  1307
 * Free a surface created by the above function.
slouken@0
  1308
 */
slouken@1895
  1309
void
slouken@1895
  1310
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
  1311
{
slouken@1895
  1312
    if (surface == NULL) {
slouken@1895
  1313
        return;
slouken@1895
  1314
    }
slouken@5288
  1315
    if (surface->flags & SDL_DONTFREE) {
slouken@5288
  1316
        return;
slouken@5288
  1317
    }
slouken@11551
  1318
    SDL_InvalidateMap(surface->map);
slouken@11551
  1319
slouken@1895
  1320
    if (--surface->refcount > 0) {
slouken@1895
  1321
        return;
slouken@1895
  1322
    }
slouken@1895
  1323
    while (surface->locked > 0) {
slouken@1895
  1324
        SDL_UnlockSurface(surface);
slouken@1895
  1325
    }
sylvain@13433
  1326
#if SDL_HAVE_RLE
slouken@1895
  1327
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1328
        SDL_UnRLESurface(surface, 0);
slouken@1895
  1329
    }
sylvain@13433
  1330
#endif
slouken@1895
  1331
    if (surface->format) {
slouken@1895
  1332
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
  1333
        SDL_FreeFormat(surface->format);
slouken@1895
  1334
        surface->format = NULL;
slouken@1895
  1335
    }
sylvain@12587
  1336
    if (surface->flags & SDL_PREALLOC) {
sylvain@12587
  1337
        /* Don't free */
sylvain@12589
  1338
    } else if (surface->flags & SDL_SIMD_ALIGNED) {
sylvain@12587
  1339
        /* Free aligned */
sylvain@12587
  1340
        SDL_SIMDFree(surface->pixels);
sylvain@12587
  1341
    } else {
sylvain@12587
  1342
        /* Normal */
slouken@1895
  1343
        SDL_free(surface->pixels);
slouken@1895
  1344
    }
brandon@11563
  1345
    if (surface->map) {
brandon@11563
  1346
        SDL_FreeBlitMap(surface->map);
brandon@11563
  1347
    }
slouken@1895
  1348
    SDL_free(surface);
slouken@0
  1349
}
slouken@1895
  1350
slouken@1895
  1351
/* vi: set ts=4 sw=4 expandtab: */