src/video/SDL_surface.c
author Sylvain Becker <sylvain.becker@gmail.com>
Mon, 04 Feb 2019 09:11:07 +0100
changeset 12591 66cd8731c3b1
parent 12590 b50bedb4bca0
permissions -rw-r--r--
Fixed bug 4484 - use SIMD aligned memory for SDL_Surface

Surfaces are allocated using SDL_SIMDAlloc()
They are marked with SDL_SIMD_ALIGNED flag to appropriatly free them with SDL_SIMDFree()
(Flag is cleared when pixels is free'd in RLE, in case user would hijack the pixels ptr)

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