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