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