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