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