src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Sep 2018 16:41:55 -0700
changeset 12204 5486557ca8d9
parent 11811 5d94cb6b24d3
child 12242 df7260f149f2
permissions -rw-r--r--
Fixed bug 4264 - SDL_CreateTextureFromSurface generates error message but returns ok

Anthony @ POW Games

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