src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 01 Oct 2012 00:40:26 -0700
changeset 6526 e9c3a540c52f
parent 6407 93f481657c6d
child 6527 c04879cd3b57
permissions -rw-r--r--
Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
*/
slouken@1402
    21
#include "SDL_config.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) {
slouken@1895
   146
        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   147
        return -1;
slouken@1895
   148
    }
slouken@5288
   149
    return SDL_SetPixelFormatPalette(surface->format, palette);
slouken@1895
   150
}
slouken@1895
   151
slouken@2267
   152
int
slouken@2267
   153
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
slouken@0
   154
{
slouken@2267
   155
    int flags;
slouken@0
   156
slouken@2266
   157
    if (!surface) {
slouken@2266
   158
        return -1;
slouken@1895
   159
    }
slouken@0
   160
slouken@2267
   161
    flags = surface->map->info.flags;
slouken@1895
   162
    if (flag) {
slouken@2267
   163
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@1895
   164
    } else {
slouken@2267
   165
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@1895
   166
    }
slouken@2267
   167
    if (surface->map->info.flags != flags) {
slouken@1895
   168
        SDL_InvalidateMap(surface->map);
slouken@1895
   169
    }
slouken@1895
   170
    return 0;
slouken@431
   171
}
slouken@0
   172
slouken@2267
   173
int
slouken@3560
   174
SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
slouken@2266
   175
{
slouken@2266
   176
    int flags;
slouken@2266
   177
slouken@2266
   178
    if (!surface) {
slouken@2266
   179
        return -1;
slouken@2266
   180
    }
slouken@2266
   181
slouken@2266
   182
    if (flag & SDL_RLEACCEL) {
slouken@2266
   183
        SDL_SetSurfaceRLE(surface, 1);
slouken@2266
   184
    }
slouken@2266
   185
slouken@2266
   186
    flags = surface->map->info.flags;
slouken@2266
   187
    if (flag) {
slouken@2266
   188
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   189
        surface->map->info.colorkey = key;
slouken@2266
   190
    } else {
slouken@2266
   191
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   192
    }
slouken@2266
   193
    if (surface->map->info.flags != flags) {
slouken@2266
   194
        SDL_InvalidateMap(surface->map);
slouken@2266
   195
    }
slouken@2267
   196
slouken@2266
   197
    return 0;
slouken@2266
   198
}
slouken@2266
   199
slouken@3103
   200
int
slouken@3103
   201
SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
slouken@3103
   202
{
slouken@3103
   203
    if (!surface) {
slouken@3103
   204
        return -1;
slouken@3103
   205
    }
slouken@3103
   206
slouken@3103
   207
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@3103
   208
        return -1;
slouken@3103
   209
    }
slouken@3103
   210
slouken@3103
   211
    if (key) {
slouken@3103
   212
        *key = surface->map->info.colorkey;
slouken@3103
   213
    }
slouken@3103
   214
    return 0;
slouken@3103
   215
}
slouken@3103
   216
slouken@2785
   217
/* This is a fairly slow function to switch from colorkey to alpha */
slouken@2787
   218
static void
slouken@2786
   219
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
slouken@2785
   220
{
slouken@2786
   221
    int x, y;
slouken@2785
   222
slouken@2786
   223
    if (!surface) {
slouken@2786
   224
        return;
slouken@2786
   225
    }
slouken@2785
   226
slouken@2786
   227
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
slouken@2786
   228
        !surface->format->Amask) {
slouken@2786
   229
        return;
slouken@2786
   230
    }
slouken@2785
   231
slouken@2786
   232
    SDL_LockSurface(surface);
slouken@2785
   233
slouken@2786
   234
    switch (surface->format->BytesPerPixel) {
slouken@2786
   235
    case 2:
slouken@2786
   236
        {
slouken@2786
   237
            Uint16 *row, *spot;
slouken@2786
   238
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
slouken@2786
   239
            Uint16 mask = (Uint16) (~surface->format->Amask);
slouken@2785
   240
slouken@2786
   241
            row = (Uint16 *) surface->pixels;
slouken@2786
   242
            for (y = surface->h; y--;) {
slouken@2786
   243
                spot = row;
slouken@2786
   244
                for (x = surface->w; x--;) {
slouken@2786
   245
                    if (*spot == ckey) {
slouken@2786
   246
                        *spot &= mask;
slouken@2786
   247
                    }
slouken@2786
   248
                    ++spot;
slouken@2786
   249
                }
slouken@2786
   250
                row += surface->pitch / 2;
slouken@2786
   251
            }
slouken@2786
   252
        }
slouken@2786
   253
        break;
slouken@2786
   254
    case 3:
slouken@2786
   255
        /* FIXME */
slouken@2786
   256
        break;
slouken@2786
   257
    case 4:
slouken@2786
   258
        {
slouken@2786
   259
            Uint32 *row, *spot;
slouken@2786
   260
            Uint32 ckey = surface->map->info.colorkey;
slouken@2786
   261
            Uint32 mask = ~surface->format->Amask;
slouken@2785
   262
slouken@2786
   263
            row = (Uint32 *) surface->pixels;
slouken@2786
   264
            for (y = surface->h; y--;) {
slouken@2786
   265
                spot = row;
slouken@2786
   266
                for (x = surface->w; x--;) {
slouken@2786
   267
                    if (*spot == ckey) {
slouken@2786
   268
                        *spot &= mask;
slouken@2786
   269
                    }
slouken@2786
   270
                    ++spot;
slouken@2786
   271
                }
slouken@2786
   272
                row += surface->pitch / 4;
slouken@2786
   273
            }
slouken@2786
   274
        }
slouken@2786
   275
        break;
slouken@2786
   276
    }
slouken@2785
   277
slouken@2786
   278
    SDL_UnlockSurface(surface);
slouken@2785
   279
slouken@2786
   280
    SDL_SetColorKey(surface, 0, 0);
slouken@2884
   281
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2785
   282
}
slouken@2785
   283
slouken@2267
   284
int
slouken@2267
   285
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   286
{
slouken@2266
   287
    int flags;
slouken@2266
   288
slouken@2266
   289
    if (!surface) {
slouken@2266
   290
        return -1;
slouken@2266
   291
    }
slouken@2266
   292
slouken@2266
   293
    surface->map->info.r = r;
slouken@2266
   294
    surface->map->info.g = g;
slouken@2266
   295
    surface->map->info.b = b;
slouken@2266
   296
slouken@2266
   297
    flags = surface->map->info.flags;
slouken@2266
   298
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   299
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@2266
   300
    } else {
slouken@2266
   301
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@2266
   302
    }
slouken@2266
   303
    if (surface->map->info.flags != flags) {
slouken@2266
   304
        SDL_InvalidateMap(surface->map);
slouken@2266
   305
    }
slouken@2266
   306
    return 0;
slouken@2266
   307
}
slouken@2266
   308
slouken@2266
   309
slouken@2267
   310
int
slouken@2267
   311
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@2266
   312
{
slouken@2266
   313
    if (!surface) {
slouken@2266
   314
        return -1;
slouken@2266
   315
    }
slouken@2266
   316
slouken@2266
   317
    if (r) {
slouken@2266
   318
        *r = surface->map->info.r;
slouken@2266
   319
    }
slouken@2266
   320
    if (g) {
slouken@2266
   321
        *g = surface->map->info.g;
slouken@2266
   322
    }
slouken@2266
   323
    if (b) {
slouken@2266
   324
        *b = surface->map->info.b;
slouken@2266
   325
    }
slouken@2266
   326
    return 0;
slouken@2266
   327
}
slouken@2266
   328
slouken@2267
   329
int
slouken@2267
   330
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
slouken@2266
   331
{
slouken@2266
   332
    int flags;
slouken@2266
   333
slouken@2266
   334
    if (!surface) {
slouken@2266
   335
        return -1;
slouken@2266
   336
    }
slouken@2266
   337
slouken@2266
   338
    surface->map->info.a = alpha;
slouken@2266
   339
slouken@2266
   340
    flags = surface->map->info.flags;
slouken@2266
   341
    if (alpha != 0xFF) {
slouken@2266
   342
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   343
    } else {
slouken@2266
   344
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   345
    }
slouken@2266
   346
    if (surface->map->info.flags != flags) {
slouken@2266
   347
        SDL_InvalidateMap(surface->map);
slouken@2266
   348
    }
slouken@2266
   349
    return 0;
slouken@2266
   350
}
slouken@2266
   351
slouken@2267
   352
int
slouken@2267
   353
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
slouken@2266
   354
{
slouken@2266
   355
    if (!surface) {
slouken@2266
   356
        return -1;
slouken@2266
   357
    }
slouken@2266
   358
slouken@2266
   359
    if (alpha) {
slouken@2266
   360
        *alpha = surface->map->info.a;
slouken@2266
   361
    }
slouken@2266
   362
    return 0;
slouken@2266
   363
}
slouken@2266
   364
slouken@2267
   365
int
slouken@4929
   366
SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
slouken@2266
   367
{
slouken@2266
   368
    int flags, status;
slouken@2266
   369
slouken@2266
   370
    if (!surface) {
slouken@2266
   371
        return -1;
slouken@2266
   372
    }
slouken@2266
   373
slouken@2266
   374
    status = 0;
slouken@2266
   375
    flags = surface->map->info.flags;
slouken@5184
   376
    surface->map->info.flags &=
slouken@5184
   377
        ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
slouken@2266
   378
    switch (blendMode) {
slouken@2884
   379
    case SDL_BLENDMODE_NONE:
slouken@2266
   380
        break;
slouken@2884
   381
    case SDL_BLENDMODE_BLEND:
slouken@2266
   382
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   383
        break;
slouken@2884
   384
    case SDL_BLENDMODE_ADD:
slouken@2266
   385
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   386
        break;
slouken@5184
   387
    case SDL_BLENDMODE_MOD:
slouken@5184
   388
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@5184
   389
        break;
slouken@2266
   390
    default:
slouken@2266
   391
        SDL_Unsupported();
slouken@2266
   392
        status = -1;
slouken@2266
   393
        break;
slouken@2266
   394
    }
slouken@2266
   395
slouken@2266
   396
    if (surface->map->info.flags != flags) {
slouken@2266
   397
        SDL_InvalidateMap(surface->map);
slouken@2266
   398
    }
slouken@2267
   399
slouken@2266
   400
    return status;
slouken@2266
   401
}
slouken@2266
   402
slouken@2267
   403
int
slouken@4929
   404
SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
slouken@2266
   405
{
slouken@2266
   406
    if (!surface) {
slouken@2266
   407
        return -1;
slouken@2266
   408
    }
slouken@2266
   409
slouken@2266
   410
    if (!blendMode) {
slouken@2266
   411
        return 0;
slouken@2266
   412
    }
slouken@2266
   413
slouken@5184
   414
    switch (surface->map->
slouken@5184
   415
            info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
slouken@2266
   416
    case SDL_COPY_BLEND:
slouken@2884
   417
        *blendMode = SDL_BLENDMODE_BLEND;
slouken@2266
   418
        break;
slouken@2266
   419
    case SDL_COPY_ADD:
slouken@2884
   420
        *blendMode = SDL_BLENDMODE_ADD;
slouken@2266
   421
        break;
slouken@5184
   422
    case SDL_COPY_MOD:
slouken@5184
   423
        *blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   424
        break;
slouken@2266
   425
    default:
slouken@2884
   426
        *blendMode = SDL_BLENDMODE_NONE;
slouken@2266
   427
        break;
slouken@2266
   428
    }
slouken@2266
   429
    return 0;
slouken@2266
   430
}
slouken@2266
   431
slouken@1895
   432
SDL_bool
slouken@1895
   433
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   434
{
slouken@1895
   435
    SDL_Rect full_rect;
slouken@0
   436
slouken@1895
   437
    /* Don't do anything if there's no surface to act on */
slouken@1895
   438
    if (!surface) {
slouken@1895
   439
        return SDL_FALSE;
slouken@1895
   440
    }
slouken@0
   441
slouken@1895
   442
    /* Set up the full surface rectangle */
slouken@1895
   443
    full_rect.x = 0;
slouken@1895
   444
    full_rect.y = 0;
slouken@1895
   445
    full_rect.w = surface->w;
slouken@1895
   446
    full_rect.h = surface->h;
slouken@0
   447
slouken@1895
   448
    /* Set the clipping rectangle */
slouken@1895
   449
    if (!rect) {
slouken@1895
   450
        surface->clip_rect = full_rect;
slouken@4966
   451
        return SDL_TRUE;
slouken@1895
   452
    }
slouken@1895
   453
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   454
}
slouken@1895
   455
slouken@1895
   456
void
slouken@1895
   457
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   458
{
slouken@1895
   459
    if (surface && rect) {
slouken@1895
   460
        *rect = surface->clip_rect;
slouken@1895
   461
    }
slouken@0
   462
}
slouken@1895
   463
slouken@0
   464
/* 
slouken@0
   465
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   466
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   467
 * verification.  The lower part is a pointer to a low level
slouken@0
   468
 * accelerated blitting function.
slouken@0
   469
 *
slouken@0
   470
 * These parts are separated out and each used internally by this 
slouken@0
   471
 * library in the optimimum places.  They are exported so that if
slouken@0
   472
 * you know exactly what you are doing, you can optimize your code
slouken@0
   473
 * by calling the one(s) you need.
slouken@0
   474
 */
slouken@1895
   475
int
slouken@1895
   476
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   477
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   478
{
slouken@1895
   479
    /* Check to make sure the blit mapping is valid */
slouken@1895
   480
    if ((src->map->dst != dst) ||
slouken@5288
   481
        (dst->format->palette &&
slouken@6166
   482
         src->map->dst_palette_version != dst->format->palette->version) ||
slouken@6166
   483
        (src->format->palette &&
slouken@6166
   484
         src->map->src_palette_version != src->format->palette->version)) {
slouken@1895
   485
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   486
            return (-1);
slouken@1895
   487
        }
bob@2328
   488
        /* just here for debugging */
bob@2329
   489
/*         printf */
bob@2329
   490
/*             ("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
   491
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
bob@2329
   492
/*              dst->map->info.flags, src->map->blit); */
slouken@1895
   493
    }
slouken@2257
   494
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   495
}
slouken@0
   496
slouken@0
   497
slouken@1895
   498
int
slouken@4949
   499
SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
slouken@1895
   500
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   501
{
slouken@1895
   502
    SDL_Rect fulldst;
slouken@1895
   503
    int srcx, srcy, w, h;
slouken@0
   504
slouken@1895
   505
    /* Make sure the surfaces aren't locked */
slouken@1895
   506
    if (!src || !dst) {
slouken@1895
   507
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   508
        return (-1);
slouken@1895
   509
    }
slouken@1895
   510
    if (src->locked || dst->locked) {
slouken@1895
   511
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   512
        return (-1);
slouken@1895
   513
    }
slouken@0
   514
slouken@1895
   515
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   516
    if (dstrect == NULL) {
slouken@1895
   517
        fulldst.x = fulldst.y = 0;
slouken@1895
   518
        dstrect = &fulldst;
slouken@1895
   519
    }
slouken@0
   520
slouken@1895
   521
    /* clip the source rectangle to the source surface */
slouken@1895
   522
    if (srcrect) {
slouken@1895
   523
        int maxw, maxh;
slouken@0
   524
slouken@1895
   525
        srcx = srcrect->x;
slouken@1895
   526
        w = srcrect->w;
slouken@1895
   527
        if (srcx < 0) {
slouken@1895
   528
            w += srcx;
slouken@1895
   529
            dstrect->x -= srcx;
slouken@1895
   530
            srcx = 0;
slouken@1895
   531
        }
slouken@1895
   532
        maxw = src->w - srcx;
slouken@1895
   533
        if (maxw < w)
slouken@1895
   534
            w = maxw;
slouken@0
   535
slouken@1895
   536
        srcy = srcrect->y;
slouken@1895
   537
        h = srcrect->h;
slouken@1895
   538
        if (srcy < 0) {
slouken@1895
   539
            h += srcy;
slouken@1895
   540
            dstrect->y -= srcy;
slouken@1895
   541
            srcy = 0;
slouken@1895
   542
        }
slouken@1895
   543
        maxh = src->h - srcy;
slouken@1895
   544
        if (maxh < h)
slouken@1895
   545
            h = maxh;
slouken@0
   546
slouken@1895
   547
    } else {
slouken@1895
   548
        srcx = srcy = 0;
slouken@1895
   549
        w = src->w;
slouken@1895
   550
        h = src->h;
slouken@1895
   551
    }
slouken@0
   552
slouken@1895
   553
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   554
    {
slouken@1895
   555
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   556
        int dx, dy;
slouken@0
   557
slouken@1895
   558
        dx = clip->x - dstrect->x;
slouken@1895
   559
        if (dx > 0) {
slouken@1895
   560
            w -= dx;
slouken@1895
   561
            dstrect->x += dx;
slouken@1895
   562
            srcx += dx;
slouken@1895
   563
        }
slouken@1895
   564
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   565
        if (dx > 0)
slouken@1895
   566
            w -= dx;
slouken@1895
   567
slouken@1895
   568
        dy = clip->y - dstrect->y;
slouken@1895
   569
        if (dy > 0) {
slouken@1895
   570
            h -= dy;
slouken@1895
   571
            dstrect->y += dy;
slouken@1895
   572
            srcy += dy;
slouken@1895
   573
        }
slouken@1895
   574
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   575
        if (dy > 0)
slouken@1895
   576
            h -= dy;
slouken@1895
   577
    }
slouken@1895
   578
slouken@1895
   579
    if (w > 0 && h > 0) {
slouken@1895
   580
        SDL_Rect sr;
slouken@1895
   581
        sr.x = srcx;
slouken@1895
   582
        sr.y = srcy;
slouken@1895
   583
        sr.w = dstrect->w = w;
slouken@1895
   584
        sr.h = dstrect->h = h;
slouken@1895
   585
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   586
    }
slouken@1895
   587
    dstrect->w = dstrect->h = 0;
slouken@1895
   588
    return 0;
slouken@0
   589
}
slouken@0
   590
ken@5296
   591
int
ken@5499
   592
SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
ken@5499
   593
              SDL_Surface * dst, SDL_Rect * dstrect)
ken@5499
   594
{
ken@5499
   595
    SDL_Rect final_src, final_dst, fulldst;
ken@5499
   596
ken@5499
   597
    /* Make sure the surfaces aren't locked */
ken@5499
   598
    if (!src || !dst) {
ken@5499
   599
        SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
ken@5499
   600
        return (-1);
ken@5499
   601
    }
ken@5499
   602
    if (src->locked || dst->locked) {
ken@5499
   603
        SDL_SetError("Surfaces must not be locked during blit");
ken@5499
   604
        return (-1);
ken@5499
   605
    }
ken@5499
   606
ken@5499
   607
    /* If the destination rectangle is NULL, use the entire dest surface */
ken@5499
   608
    if (dstrect == NULL) {
ken@5499
   609
        fulldst.x = fulldst.y = 0;
ken@5499
   610
        dstrect = &fulldst;
ken@5499
   611
    }
ken@5499
   612
ken@5499
   613
    /* clip the source rectangle to the source surface */
ken@5499
   614
    if (srcrect) {
ken@5499
   615
        int maxw, maxh;
ken@5499
   616
ken@5499
   617
        final_src.x = srcrect->x;
ken@5499
   618
        final_src.w = srcrect->w;
ken@5499
   619
        if (final_src.x < 0) {
ken@5499
   620
            final_src.w += final_src.x;
ken@5499
   621
            final_src.x = 0;
ken@5499
   622
        }
ken@5499
   623
        maxw = src->w - final_src.x;
ken@5499
   624
        if (maxw < final_src.w)
ken@5499
   625
            final_src.w = maxw;
ken@5499
   626
ken@5499
   627
        final_src.y = srcrect->y;
ken@5499
   628
        final_src.h = srcrect->h;
ken@5499
   629
        if (final_src.y < 0) {
ken@5499
   630
            final_src.h += final_src.y;
ken@5499
   631
            final_src.y = 0;
ken@5499
   632
        }
ken@5499
   633
        maxh = src->h - final_src.y;
ken@5499
   634
        if (maxh < final_src.h)
ken@5499
   635
            final_src.h = maxh;
ken@5499
   636
ken@5499
   637
    } else {
ken@5499
   638
        final_src.x = final_src.y = 0;
ken@5499
   639
        final_src.w = src->w;
ken@5499
   640
        final_src.h = src->h;
ken@5499
   641
    }
ken@5499
   642
ken@5499
   643
    /* clip the destination rectangle against the clip rectangle */
ken@5499
   644
    if (dstrect) {
ken@5499
   645
        int maxw, maxh;
ken@5499
   646
ken@5499
   647
        final_dst.x = dstrect->x;
ken@5499
   648
        final_dst.w = dstrect->w;
ken@5499
   649
        if (final_dst.x < 0) {
ken@5499
   650
            final_dst.w += final_dst.x;
ken@5499
   651
            final_dst.x = 0;
ken@5499
   652
        }
ken@5499
   653
        maxw = dst->w - final_dst.x;
ken@5499
   654
        if (maxw < final_dst.w)
ken@5499
   655
            final_dst.w = maxw;
ken@5499
   656
ken@5499
   657
        final_dst.y = dstrect->y;
ken@5499
   658
        final_dst.h = dstrect->h;
ken@5499
   659
        if (final_dst.y < 0) {
ken@5499
   660
            final_dst.h += final_dst.y;
ken@5499
   661
            final_dst.y = 0;
ken@5499
   662
        }
ken@5499
   663
        maxh = dst->h - final_dst.y;
ken@5499
   664
        if (maxh < final_dst.h)
ken@5499
   665
            final_dst.h = maxh;
ken@5499
   666
    } else {
ken@5499
   667
        final_dst.x = final_dst.y = 0;
ken@5499
   668
        final_dst.w = dst->w;
ken@5499
   669
        final_dst.h = dst->h;
ken@5499
   670
    }
ken@5499
   671
ken@5499
   672
    if (final_dst.w > 0 && final_dst.h > 0) {
ken@5499
   673
        return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
ken@5499
   674
    }
ken@5499
   675
ken@5499
   676
    return 0;
ken@5499
   677
}
ken@5499
   678
ken@5499
   679
/**
ken@5499
   680
 *  This is a semi-private blit function and it performs low-level surface
ken@5499
   681
 *  scaled blitting only.
ken@5499
   682
 */
ken@5499
   683
int
ken@5499
   684
SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
ken@5499
   685
                SDL_Surface * dst, SDL_Rect * dstrect)
ken@5296
   686
{
slouken@6526
   687
    static const Uint32 complex_copy_flags = (
slouken@6526
   688
        SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
slouken@6526
   689
        SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD |
slouken@6526
   690
        SDL_COPY_COLORKEY
slouken@6526
   691
    );
slouken@6526
   692
ken@5296
   693
    /* Save off the original dst width, height */
ken@5296
   694
    int dstW = dstrect->w;
ken@5296
   695
    int dstH = dstrect->h;
ken@5296
   696
    SDL_Rect final_dst = *dstrect;
ken@5296
   697
    SDL_Rect final_src = *srcrect;
ken@5296
   698
ken@5296
   699
    /* Clip the dst surface to the dstrect */
ken@5296
   700
    SDL_SetClipRect( dst, &final_dst );
ken@5296
   701
ken@5296
   702
    /* Did the dst width change? */
ken@5296
   703
    if ( dstW != dst->clip_rect.w ) {
ken@5296
   704
        /* scale the src width appropriately */
ken@5296
   705
        final_src.w = final_src.w * dst->clip_rect.w / dstW;
ken@5296
   706
    }
ken@5296
   707
ken@5296
   708
    /* Did the dst height change? */
ken@5296
   709
    if ( dstH != dst->clip_rect.h ) {
ken@5296
   710
        /* scale the src width appropriately */
ken@5296
   711
        final_src.h = final_src.h * dst->clip_rect.h / dstH;
ken@5296
   712
    }
ken@5296
   713
ken@5296
   714
    /* Clip the src surface to the srcrect */
ken@5296
   715
    SDL_SetClipRect( src, &final_src );
ken@5296
   716
ken@5296
   717
    src->map->info.flags |= SDL_COPY_NEAREST;
ken@5296
   718
slouken@6526
   719
    if ( !(src->map->info.flags & complex_copy_flags) &&
slouken@6526
   720
         src->format->format == dst->format->format && 
slouken@6526
   721
         !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
ken@5296
   722
        return SDL_SoftStretch( src, &final_src, dst, &final_dst );
ken@5296
   723
    } else {
ken@5296
   724
        return SDL_LowerBlit( src, &final_src, dst, &final_dst );
ken@5296
   725
    }
ken@5296
   726
}
ken@5296
   727
ken@5296
   728
/*
slouken@0
   729
 * Lock a surface to directly access the pixels
slouken@0
   730
 */
slouken@1895
   731
int
slouken@1895
   732
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   733
{
slouken@1895
   734
    if (!surface->locked) {
slouken@1895
   735
        /* Perform the lock */
slouken@1895
   736
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   737
            SDL_UnRLESurface(surface, 1);
slouken@1895
   738
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   739
        }
slouken@1895
   740
    }
slouken@0
   741
slouken@1895
   742
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   743
    ++surface->locked;
slouken@0
   744
slouken@1895
   745
    /* Ready to go.. */
slouken@1895
   746
    return (0);
slouken@0
   747
}
slouken@1895
   748
slouken@0
   749
/*
slouken@0
   750
 * Unlock a previously locked surface
slouken@0
   751
 */
slouken@1895
   752
void
slouken@1895
   753
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   754
{
slouken@1895
   755
    /* Only perform an unlock if we are locked */
slouken@1895
   756
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   757
        return;
slouken@1895
   758
    }
slouken@0
   759
slouken@1895
   760
    /* Update RLE encoded surface with new data */
slouken@1895
   761
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   762
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   763
        SDL_RLESurface(surface);
slouken@1895
   764
    }
slouken@0
   765
}
slouken@0
   766
slouken@0
   767
/* 
slouken@0
   768
 * Convert a surface into the specified pixel format.
slouken@0
   769
 */
slouken@1895
   770
SDL_Surface *
slouken@2807
   771
SDL_ConvertSurface(SDL_Surface * surface, SDL_PixelFormat * format,
slouken@2807
   772
                   Uint32 flags)
slouken@0
   773
{
slouken@1895
   774
    SDL_Surface *convert;
slouken@2266
   775
    Uint32 copy_flags;
slouken@1895
   776
    SDL_Rect bounds;
slouken@0
   777
slouken@1895
   778
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   779
    if (format->palette != NULL) {
slouken@1895
   780
        int i;
slouken@1895
   781
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   782
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   783
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   784
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   785
                break;
slouken@1895
   786
        }
slouken@1895
   787
        if (i == format->palette->ncolors) {
slouken@1895
   788
            SDL_SetError("Empty destination palette");
slouken@1895
   789
            return (NULL);
slouken@1895
   790
        }
slouken@1895
   791
    }
slouken@0
   792
slouken@1895
   793
    /* Create a new surface with the desired format */
slouken@2807
   794
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
slouken@1895
   795
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   796
                                   format->Gmask, format->Bmask,
slouken@1895
   797
                                   format->Amask);
slouken@1895
   798
    if (convert == NULL) {
slouken@1895
   799
        return (NULL);
slouken@1895
   800
    }
slouken@264
   801
slouken@1895
   802
    /* Copy the palette if any */
slouken@1895
   803
    if (format->palette && convert->format->palette) {
slouken@1895
   804
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   805
                   format->palette->colors,
slouken@1895
   806
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   807
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   808
    }
slouken@0
   809
slouken@2266
   810
    /* Save the original copy flags */
slouken@2266
   811
    copy_flags = surface->map->info.flags;
slouken@2266
   812
    surface->map->info.flags = 0;
slouken@0
   813
slouken@1895
   814
    /* Copy over the image data */
slouken@1895
   815
    bounds.x = 0;
slouken@1895
   816
    bounds.y = 0;
slouken@1895
   817
    bounds.w = surface->w;
slouken@1895
   818
    bounds.h = surface->h;
slouken@1895
   819
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   820
slouken@1895
   821
    /* Clean up the original surface, and update converted surface */
slouken@2824
   822
    convert->map->info.r = surface->map->info.r;
slouken@2824
   823
    convert->map->info.g = surface->map->info.g;
slouken@2824
   824
    convert->map->info.b = surface->map->info.b;
slouken@2824
   825
    convert->map->info.a = surface->map->info.a;
slouken@2824
   826
    convert->map->info.flags =
slouken@2824
   827
        (copy_flags &
slouken@2824
   828
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
slouken@2824
   829
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
slouken@2824
   830
           SDL_COPY_RLE_ALPHAKEY));
slouken@2824
   831
    surface->map->info.flags = copy_flags;
slouken@2266
   832
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@2266
   833
        Uint8 keyR, keyG, keyB, keyA;
slouken@2266
   834
slouken@2267
   835
        SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
slouken@2267
   836
                    &keyG, &keyB, &keyA);
slouken@2266
   837
        SDL_SetColorKey(convert, 1,
slouken@2266
   838
                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
slouken@2824
   839
        /* This is needed when converting for 3D texture upload */
slouken@2787
   840
        SDL_ConvertColorkeyToAlpha(convert);
slouken@1895
   841
    }
slouken@2824
   842
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@0
   843
slouken@2266
   844
    /* Enable alpha blending by default if the new surface has an
slouken@2266
   845
     * alpha channel or alpha modulation */
slouken@2824
   846
    if ((surface->format->Amask && format->Amask) ||
slouken@4491
   847
        (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
slouken@2884
   848
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
slouken@1895
   849
    }
slouken@2824
   850
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
slouken@2824
   851
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
slouken@2824
   852
    }
slouken@0
   853
slouken@1895
   854
    /* We're ready to go! */
slouken@1895
   855
    return (convert);
slouken@0
   856
}
slouken@0
   857
slouken@5375
   858
SDL_Surface *
slouken@5375
   859
SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
slouken@5375
   860
                         Uint32 flags)
slouken@5375
   861
{
slouken@5375
   862
    SDL_PixelFormat *fmt;
slouken@5515
   863
    SDL_Surface *convert = NULL;
slouken@5375
   864
slouken@5375
   865
    fmt = SDL_AllocFormat(pixel_format);
slouken@5375
   866
    if (fmt) {
slouken@5375
   867
        convert = SDL_ConvertSurface(surface, fmt, flags);
slouken@5375
   868
        SDL_FreeFormat(fmt);
slouken@5375
   869
    }
slouken@5375
   870
    return convert;
slouken@5375
   871
}
slouken@5375
   872
slouken@0
   873
/*
slouken@3434
   874
 * Create a surface on the stack for quick blit operations
slouken@3434
   875
 */
slouken@3434
   876
static __inline__ SDL_bool
slouken@3434
   877
SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
slouken@3434
   878
                         void * pixels, int pitch, SDL_Surface * surface, 
slouken@3434
   879
                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
slouken@3434
   880
{
slouken@5288
   881
    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
   882
        SDL_SetError("Indexed pixel formats not supported");
slouken@3434
   883
        return SDL_FALSE;
slouken@3434
   884
    }
slouken@5288
   885
    if (SDL_InitFormat(format, pixel_format) < 0) {
slouken@3434
   886
        return SDL_FALSE;
slouken@3434
   887
    }
slouken@3434
   888
slouken@3434
   889
    SDL_zerop(surface);
slouken@3434
   890
    surface->flags = SDL_PREALLOC;
slouken@5288
   891
    surface->format = format;
slouken@3434
   892
    surface->pixels = pixels;
slouken@3434
   893
    surface->w = width;
slouken@3434
   894
    surface->h = height;
slouken@3434
   895
    surface->pitch = pitch;
slouken@3434
   896
    /* We don't actually need to set up the clip rect for our purposes */
slouken@3434
   897
    /*SDL_SetClipRect(surface, NULL);*/
slouken@3434
   898
slouken@3434
   899
    /* Allocate an empty mapping */
slouken@3434
   900
    SDL_zerop(blitmap);
slouken@3434
   901
    blitmap->info.r = 0xFF;
slouken@3434
   902
    blitmap->info.g = 0xFF;
slouken@3434
   903
    blitmap->info.b = 0xFF;
slouken@3434
   904
    blitmap->info.a = 0xFF;
slouken@3434
   905
    surface->map = blitmap;
slouken@3434
   906
slouken@3434
   907
    /* The surface is ready to go */
slouken@3434
   908
    surface->refcount = 1;
slouken@3434
   909
    return SDL_TRUE;
slouken@3434
   910
}
slouken@3434
   911
slouken@3434
   912
/*
slouken@3434
   913
 * Copy a block of pixels of one format to another format
slouken@3434
   914
 */
slouken@3434
   915
int SDL_ConvertPixels(int width, int height,
slouken@3434
   916
                      Uint32 src_format, const void * src, int src_pitch,
slouken@3434
   917
                      Uint32 dst_format, void * dst, int dst_pitch)
slouken@3434
   918
{
slouken@3434
   919
    SDL_Surface src_surface, dst_surface;
slouken@3434
   920
    SDL_PixelFormat src_fmt, dst_fmt;
slouken@3434
   921
    SDL_BlitMap src_blitmap, dst_blitmap;
slouken@3434
   922
    SDL_Rect rect;
icculus@6407
   923
    void *nonconst_src = (void *) src;
slouken@3434
   924
slouken@3434
   925
    /* Fast path for same format copy */
slouken@3434
   926
    if (src_format == dst_format) {
slouken@3434
   927
        int bpp;
slouken@3434
   928
slouken@3434
   929
        if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
slouken@3434
   930
            switch (src_format) {
slouken@3434
   931
            case SDL_PIXELFORMAT_YV12:
slouken@3434
   932
            case SDL_PIXELFORMAT_IYUV:
slouken@3434
   933
            case SDL_PIXELFORMAT_YUY2:
slouken@3434
   934
            case SDL_PIXELFORMAT_UYVY:
slouken@3434
   935
            case SDL_PIXELFORMAT_YVYU:
slouken@3434
   936
                bpp = 2;
slouken@3434
   937
            default:
slouken@3434
   938
                SDL_SetError("Unknown FOURCC pixel format");
slouken@3434
   939
                return -1;
slouken@3434
   940
            }
slouken@3434
   941
        } else {
slouken@3434
   942
            bpp = SDL_BYTESPERPIXEL(src_format);
slouken@3434
   943
        }
slouken@3434
   944
        width *= bpp;
slouken@3434
   945
slouken@3434
   946
        while (height-- > 0) {
slouken@3434
   947
            SDL_memcpy(dst, src, width);
slouken@3434
   948
            src = (Uint8*)src + src_pitch;
slouken@3434
   949
            dst = (Uint8*)dst + dst_pitch;
slouken@3434
   950
        }
slouken@6041
   951
        return 0;
slouken@3434
   952
    }
slouken@3434
   953
icculus@6407
   954
    if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
slouken@3434
   955
                                  src_pitch,
slouken@3434
   956
                                  &src_surface, &src_fmt, &src_blitmap)) {
slouken@3434
   957
        return -1;
slouken@3434
   958
    }
slouken@3434
   959
    if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
slouken@3434
   960
                                  &dst_surface, &dst_fmt, &dst_blitmap)) {
slouken@3434
   961
        return -1;
slouken@3434
   962
    }
slouken@3434
   963
slouken@3434
   964
    /* Set up the rect and go! */
slouken@3434
   965
    rect.x = 0;
slouken@3434
   966
    rect.y = 0;
slouken@3434
   967
    rect.w = width;
slouken@3436
   968
    rect.h = height;
slouken@3434
   969
    return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
slouken@3434
   970
}
slouken@3434
   971
slouken@3434
   972
/*
slouken@0
   973
 * Free a surface created by the above function.
slouken@0
   974
 */
slouken@1895
   975
void
slouken@1895
   976
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   977
{
slouken@1895
   978
    if (surface == NULL) {
slouken@1895
   979
        return;
slouken@1895
   980
    }
slouken@5288
   981
    if (surface->flags & SDL_DONTFREE) {
slouken@5288
   982
        return;
slouken@5288
   983
    }
slouken@1895
   984
    if (--surface->refcount > 0) {
slouken@1895
   985
        return;
slouken@1895
   986
    }
slouken@1895
   987
    while (surface->locked > 0) {
slouken@1895
   988
        SDL_UnlockSurface(surface);
slouken@1895
   989
    }
slouken@1895
   990
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   991
        SDL_UnRLESurface(surface, 0);
slouken@1895
   992
    }
slouken@1895
   993
    if (surface->format) {
slouken@1895
   994
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   995
        SDL_FreeFormat(surface->format);
slouken@1895
   996
        surface->format = NULL;
slouken@1895
   997
    }
slouken@1895
   998
    if (surface->map != NULL) {
slouken@1895
   999
        SDL_FreeBlitMap(surface->map);
slouken@1895
  1000
        surface->map = NULL;
slouken@1895
  1001
    }
slouken@1895
  1002
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
  1003
        SDL_free(surface->pixels);
slouken@1895
  1004
    }
slouken@1895
  1005
    SDL_free(surface);
slouken@0
  1006
}
slouken@1895
  1007
slouken@1895
  1008
/* vi: set ts=4 sw=4 expandtab: */