src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Aug 2007 06:37:22 +0000
changeset 2251 292bee385630
parent 2249 5a58b57b6724
child 2253 6d99edd791bf
permissions -rw-r--r--
SSE and MMX intrinsics work with Visual Studio now...
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1895
    44
    /* Allocate the surface */
slouken@1920
    45
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
slouken@1895
    46
    if (surface == NULL) {
slouken@1895
    47
        SDL_OutOfMemory();
slouken@1895
    48
        return NULL;
slouken@1895
    49
    }
slouken@940
    50
slouken@1895
    51
    surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
    52
    if (!surface->format) {
slouken@1895
    53
        SDL_FreeSurface(surface);
slouken@1895
    54
        return NULL;
slouken@1895
    55
    }
slouken@1895
    56
    if (Amask) {
slouken@1895
    57
        surface->flags |= SDL_SRCALPHA;
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@1895
   141
    /* The surface is ready to go */
slouken@1895
   142
    surface->refcount = 1;
slouken@0
   143
#ifdef CHECK_LEAKS
slouken@1895
   144
    ++surfaces_allocated;
slouken@0
   145
#endif
slouken@1895
   146
    return surface;
slouken@0
   147
}
slouken@1895
   148
slouken@0
   149
/*
slouken@0
   150
 * Create an RGB surface from an existing memory buffer
slouken@0
   151
 */
slouken@1895
   152
SDL_Surface *
slouken@1895
   153
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   154
                         int width, int height, int depth, int pitch,
slouken@1895
   155
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   156
                         Uint32 Amask)
slouken@0
   157
{
slouken@1895
   158
    SDL_Surface *surface;
slouken@0
   159
slouken@1895
   160
    surface =
slouken@1895
   161
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
   162
    if (surface != NULL) {
slouken@1895
   163
        surface->flags |= SDL_PREALLOC;
slouken@1895
   164
        surface->pixels = pixels;
slouken@1895
   165
        surface->w = width;
slouken@1895
   166
        surface->h = height;
slouken@1895
   167
        surface->pitch = pitch;
slouken@1895
   168
        SDL_SetClipRect(surface, NULL);
slouken@1895
   169
    }
slouken@1895
   170
    return surface;
slouken@0
   171
}
slouken@1895
   172
slouken@1895
   173
static int
slouken@1895
   174
SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1895
   175
{
slouken@1895
   176
    SDL_Surface *surface = (SDL_Surface *) userdata;
slouken@1895
   177
slouken@1895
   178
    SDL_FormatChanged(surface);
slouken@1895
   179
slouken@1895
   180
    return 0;
slouken@1895
   181
}
slouken@1895
   182
slouken@1895
   183
int
slouken@1895
   184
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   185
{
slouken@1895
   186
    if (!surface || !surface->format) {
slouken@1895
   187
        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   188
        return -1;
slouken@1895
   189
    }
slouken@1895
   190
slouken@1895
   191
    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
slouken@1895
   192
        SDL_SetError
slouken@1895
   193
            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
slouken@1895
   194
        return -1;
slouken@1895
   195
    }
slouken@1895
   196
slouken@1895
   197
    if (surface->format->palette == palette) {
slouken@1895
   198
        return 0;
slouken@1895
   199
    }
slouken@1895
   200
slouken@1895
   201
    if (surface->format->palette) {
slouken@1895
   202
        SDL_DelPaletteWatch(surface->format->palette,
slouken@1895
   203
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   204
    }
slouken@1895
   205
slouken@1895
   206
    surface->format->palette = palette;
slouken@1895
   207
slouken@1895
   208
    if (surface->format->palette) {
slouken@1895
   209
        SDL_AddPaletteWatch(surface->format->palette,
slouken@1895
   210
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   211
    }
slouken@1895
   212
    return 0;
slouken@1895
   213
}
slouken@1895
   214
slouken@0
   215
/*
slouken@0
   216
 * Set the color key in a blittable surface
slouken@0
   217
 */
slouken@1895
   218
int
slouken@1895
   219
SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
slouken@0
   220
{
slouken@1895
   221
    /* Sanity check the flag as it gets passed in */
slouken@1895
   222
    if (flag & SDL_SRCCOLORKEY) {
slouken@1895
   223
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1895
   224
            flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   225
        } else {
slouken@1895
   226
            flag = SDL_SRCCOLORKEY;
slouken@1895
   227
        }
slouken@1895
   228
    } else {
slouken@1895
   229
        flag = 0;
slouken@1895
   230
    }
slouken@0
   231
slouken@1895
   232
    /* Optimize away operations that don't change anything */
slouken@1895
   233
    if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
slouken@1895
   234
        (key == surface->format->colorkey)) {
slouken@1895
   235
        return (0);
slouken@1895
   236
    }
slouken@0
   237
slouken@1895
   238
    /* UnRLE surfaces before we change the colorkey */
slouken@1895
   239
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   240
        SDL_UnRLESurface(surface, 1);
slouken@1895
   241
    }
slouken@0
   242
slouken@1895
   243
    if (flag) {
slouken@1895
   244
        surface->flags |= SDL_SRCCOLORKEY;
slouken@1895
   245
        surface->format->colorkey = key;
slouken@1895
   246
        if (flag & SDL_RLEACCELOK) {
slouken@1895
   247
            surface->flags |= SDL_RLEACCELOK;
slouken@1895
   248
        } else {
slouken@1895
   249
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1895
   250
        }
slouken@1895
   251
    } else {
slouken@1895
   252
        surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   253
        surface->format->colorkey = 0;
slouken@1895
   254
    }
slouken@1895
   255
    SDL_InvalidateMap(surface->map);
slouken@1895
   256
    return (0);
slouken@1895
   257
}
slouken@0
   258
slouken@1895
   259
/* This function sets the alpha channel of a surface */
slouken@1895
   260
int
slouken@1895
   261
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
slouken@1895
   262
{
slouken@1895
   263
    Uint32 oldflags = surface->flags;
slouken@1895
   264
    Uint32 oldalpha = surface->format->alpha;
slouken@0
   265
slouken@1895
   266
    /* Sanity check the flag as it gets passed in */
slouken@1895
   267
    if (flag & SDL_SRCALPHA) {
slouken@1895
   268
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1895
   269
            flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   270
        } else {
slouken@1895
   271
            flag = SDL_SRCALPHA;
slouken@1895
   272
        }
slouken@1895
   273
    } else {
slouken@1895
   274
        flag = 0;
slouken@1895
   275
    }
slouken@1895
   276
slouken@1895
   277
    /* Optimize away operations that don't change anything */
slouken@1895
   278
    if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
slouken@1895
   279
        (!flag || value == oldalpha)) {
slouken@1895
   280
        return (0);
slouken@1895
   281
    }
slouken@1895
   282
slouken@1895
   283
    if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
slouken@1895
   284
        SDL_UnRLESurface(surface, 1);
slouken@1895
   285
slouken@1895
   286
    if (flag) {
slouken@1895
   287
        surface->flags |= SDL_SRCALPHA;
slouken@1895
   288
        surface->format->alpha = value;
slouken@1895
   289
        if (flag & SDL_RLEACCELOK) {
slouken@1895
   290
            surface->flags |= SDL_RLEACCELOK;
slouken@1895
   291
        } else {
slouken@1895
   292
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1895
   293
        }
slouken@1895
   294
    } else {
slouken@1895
   295
        surface->flags &= ~SDL_SRCALPHA;
slouken@1895
   296
        surface->format->alpha = SDL_ALPHA_OPAQUE;
slouken@1895
   297
    }
slouken@1895
   298
    /*
slouken@1895
   299
     * The representation for software surfaces is independent of
slouken@1895
   300
     * per-surface alpha, so no need to invalidate the blit mapping
slouken@1895
   301
     * if just the alpha value was changed. (If either is 255, we still
slouken@1895
   302
     * need to invalidate.)
slouken@1895
   303
     */
slouken@1895
   304
    if (oldflags != surface->flags
slouken@1895
   305
        || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
slouken@1895
   306
        SDL_InvalidateMap(surface->map);
slouken@1895
   307
    }
slouken@1895
   308
    return (0);
slouken@0
   309
}
slouken@1895
   310
slouken@1895
   311
int
slouken@1895
   312
SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
slouken@0
   313
{
slouken@1895
   314
    int row, col;
slouken@1895
   315
    int offset;
slouken@1895
   316
    Uint8 *buf;
slouken@0
   317
slouken@1895
   318
    if ((surface->format->Amask != 0xFF000000) &&
slouken@1895
   319
        (surface->format->Amask != 0x000000FF)) {
slouken@1895
   320
        SDL_SetError("Unsupported surface alpha mask format");
slouken@1895
   321
        return -1;
slouken@1895
   322
    }
slouken@431
   323
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1895
   324
    if (surface->format->Amask == 0xFF000000) {
slouken@1895
   325
        offset = 3;
slouken@1895
   326
    } else {
slouken@1895
   327
        offset = 0;
slouken@1895
   328
    }
slouken@431
   329
#else
slouken@1895
   330
    if (surface->format->Amask == 0xFF000000) {
slouken@1895
   331
        offset = 0;
slouken@1895
   332
    } else {
slouken@1895
   333
        offset = 3;
slouken@1895
   334
    }
slouken@431
   335
#endif /* Byte ordering */
slouken@431
   336
slouken@1895
   337
    /* Quickly set the alpha channel of an RGBA or ARGB surface */
slouken@1895
   338
    if (SDL_MUSTLOCK(surface)) {
slouken@1895
   339
        if (SDL_LockSurface(surface) < 0) {
slouken@1895
   340
            return -1;
slouken@1895
   341
        }
slouken@1895
   342
    }
slouken@1895
   343
    row = surface->h;
slouken@1895
   344
    while (row--) {
slouken@1895
   345
        col = surface->w;
slouken@1895
   346
        buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
slouken@1895
   347
        while (col--) {
slouken@1895
   348
            *buf = value;
slouken@1895
   349
            buf += 4;
slouken@1895
   350
        }
slouken@1895
   351
    }
slouken@1895
   352
    if (SDL_MUSTLOCK(surface)) {
slouken@1895
   353
        SDL_UnlockSurface(surface);
slouken@1895
   354
    }
slouken@1895
   355
    return 0;
slouken@431
   356
}
slouken@0
   357
slouken@0
   358
/*
slouken@0
   359
 * Set the clipping rectangle for a blittable surface
slouken@0
   360
 */
slouken@1895
   361
SDL_bool
slouken@1895
   362
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   363
{
slouken@1895
   364
    SDL_Rect full_rect;
slouken@0
   365
slouken@1895
   366
    /* Don't do anything if there's no surface to act on */
slouken@1895
   367
    if (!surface) {
slouken@1895
   368
        return SDL_FALSE;
slouken@1895
   369
    }
slouken@0
   370
slouken@1895
   371
    /* Set up the full surface rectangle */
slouken@1895
   372
    full_rect.x = 0;
slouken@1895
   373
    full_rect.y = 0;
slouken@1895
   374
    full_rect.w = surface->w;
slouken@1895
   375
    full_rect.h = surface->h;
slouken@0
   376
slouken@1895
   377
    /* Set the clipping rectangle */
slouken@1895
   378
    if (!rect) {
slouken@1895
   379
        surface->clip_rect = full_rect;
slouken@1895
   380
        return 1;
slouken@1895
   381
    }
slouken@1895
   382
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   383
}
slouken@1895
   384
slouken@1895
   385
void
slouken@1895
   386
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   387
{
slouken@1895
   388
    if (surface && rect) {
slouken@1895
   389
        *rect = surface->clip_rect;
slouken@1895
   390
    }
slouken@0
   391
}
slouken@1895
   392
slouken@0
   393
/* 
slouken@0
   394
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   395
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   396
 * verification.  The lower part is a pointer to a low level
slouken@0
   397
 * accelerated blitting function.
slouken@0
   398
 *
slouken@0
   399
 * These parts are separated out and each used internally by this 
slouken@0
   400
 * library in the optimimum places.  They are exported so that if
slouken@0
   401
 * you know exactly what you are doing, you can optimize your code
slouken@0
   402
 * by calling the one(s) you need.
slouken@0
   403
 */
slouken@1895
   404
int
slouken@1895
   405
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   406
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   407
{
slouken@1895
   408
    /* Check to make sure the blit mapping is valid */
slouken@1895
   409
    if ((src->map->dst != dst) ||
slouken@1895
   410
        (src->map->dst->format_version != src->map->format_version)) {
slouken@1895
   411
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   412
            return (-1);
slouken@1895
   413
        }
slouken@1895
   414
    }
slouken@1895
   415
    return (src->map->sw_blit(src, srcrect, dst, dstrect));
slouken@0
   416
}
slouken@0
   417
slouken@0
   418
slouken@1895
   419
int
slouken@1895
   420
SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   421
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   422
{
slouken@1895
   423
    SDL_Rect fulldst;
slouken@1895
   424
    int srcx, srcy, w, h;
slouken@0
   425
slouken@1895
   426
    /* Make sure the surfaces aren't locked */
slouken@1895
   427
    if (!src || !dst) {
slouken@1895
   428
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   429
        return (-1);
slouken@1895
   430
    }
slouken@1895
   431
    if (src->locked || dst->locked) {
slouken@1895
   432
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   433
        return (-1);
slouken@1895
   434
    }
slouken@0
   435
slouken@1895
   436
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   437
    if (dstrect == NULL) {
slouken@1895
   438
        fulldst.x = fulldst.y = 0;
slouken@1895
   439
        dstrect = &fulldst;
slouken@1895
   440
    }
slouken@0
   441
slouken@1895
   442
    /* clip the source rectangle to the source surface */
slouken@1895
   443
    if (srcrect) {
slouken@1895
   444
        int maxw, maxh;
slouken@0
   445
slouken@1895
   446
        srcx = srcrect->x;
slouken@1895
   447
        w = srcrect->w;
slouken@1895
   448
        if (srcx < 0) {
slouken@1895
   449
            w += srcx;
slouken@1895
   450
            dstrect->x -= srcx;
slouken@1895
   451
            srcx = 0;
slouken@1895
   452
        }
slouken@1895
   453
        maxw = src->w - srcx;
slouken@1895
   454
        if (maxw < w)
slouken@1895
   455
            w = maxw;
slouken@0
   456
slouken@1895
   457
        srcy = srcrect->y;
slouken@1895
   458
        h = srcrect->h;
slouken@1895
   459
        if (srcy < 0) {
slouken@1895
   460
            h += srcy;
slouken@1895
   461
            dstrect->y -= srcy;
slouken@1895
   462
            srcy = 0;
slouken@1895
   463
        }
slouken@1895
   464
        maxh = src->h - srcy;
slouken@1895
   465
        if (maxh < h)
slouken@1895
   466
            h = maxh;
slouken@0
   467
slouken@1895
   468
    } else {
slouken@1895
   469
        srcx = srcy = 0;
slouken@1895
   470
        w = src->w;
slouken@1895
   471
        h = src->h;
slouken@1895
   472
    }
slouken@0
   473
slouken@1895
   474
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   475
    {
slouken@1895
   476
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   477
        int dx, dy;
slouken@0
   478
slouken@1895
   479
        dx = clip->x - dstrect->x;
slouken@1895
   480
        if (dx > 0) {
slouken@1895
   481
            w -= dx;
slouken@1895
   482
            dstrect->x += dx;
slouken@1895
   483
            srcx += dx;
slouken@1895
   484
        }
slouken@1895
   485
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   486
        if (dx > 0)
slouken@1895
   487
            w -= dx;
slouken@1895
   488
slouken@1895
   489
        dy = clip->y - dstrect->y;
slouken@1895
   490
        if (dy > 0) {
slouken@1895
   491
            h -= dy;
slouken@1895
   492
            dstrect->y += dy;
slouken@1895
   493
            srcy += dy;
slouken@1895
   494
        }
slouken@1895
   495
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   496
        if (dy > 0)
slouken@1895
   497
            h -= dy;
slouken@1895
   498
    }
slouken@1895
   499
slouken@1895
   500
    if (w > 0 && h > 0) {
slouken@1895
   501
        SDL_Rect sr;
slouken@1895
   502
        sr.x = srcx;
slouken@1895
   503
        sr.y = srcy;
slouken@1895
   504
        sr.w = dstrect->w = w;
slouken@1895
   505
        sr.h = dstrect->h = h;
slouken@1895
   506
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   507
    }
slouken@1895
   508
    dstrect->w = dstrect->h = 0;
slouken@1895
   509
    return 0;
slouken@0
   510
}
slouken@0
   511
slouken@2249
   512
#ifdef __SSE__
slouken@2249
   513
/* *INDENT-OFF* */
slouken@2249
   514
slouken@2251
   515
#ifdef _MSC_VER
slouken@2251
   516
#define SSE_BEGIN \
slouken@2251
   517
    __m128 c128; \
slouken@2251
   518
	c128.m128_u32[0] = color; \
slouken@2251
   519
	c128.m128_u32[1] = color; \
slouken@2251
   520
	c128.m128_u32[2] = color; \
slouken@2251
   521
	c128.m128_u32[3] = color;
slouken@2251
   522
#else
slouken@2249
   523
#define SSE_BEGIN \
slouken@2249
   524
    DECLARE_ALIGNED(Uint32, cccc[4], 16); \
slouken@2249
   525
    cccc[0] = color; \
slouken@2249
   526
    cccc[1] = color; \
slouken@2249
   527
    cccc[2] = color; \
slouken@2249
   528
    cccc[3] = color; \
slouken@2249
   529
    __m128 c128 = *(__m128 *)cccc;
slouken@2251
   530
#endif
slouken@2249
   531
slouken@2249
   532
#define SSE_WORK \
slouken@2249
   533
    for (i = n / 64; i--;) { \
slouken@2249
   534
        _mm_stream_ps((float *)(p+0), c128); \
slouken@2249
   535
        _mm_stream_ps((float *)(p+16), c128); \
slouken@2249
   536
        _mm_stream_ps((float *)(p+32), c128); \
slouken@2249
   537
        _mm_stream_ps((float *)(p+48), c128); \
slouken@2249
   538
        p += 64; \
slouken@2249
   539
    }
slouken@2249
   540
slouken@2249
   541
#define SSE_END
slouken@2249
   542
slouken@2249
   543
#define DEFINE_SSE_FILLRECT(bpp, type) \
slouken@2249
   544
static void \
slouken@2249
   545
SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2249
   546
{ \
slouken@2249
   547
    SSE_BEGIN; \
slouken@2249
   548
 \
slouken@2249
   549
    while (h--) { \
slouken@2249
   550
        int i, n = w * bpp; \
slouken@2249
   551
        Uint8 *p = pixels; \
slouken@2249
   552
 \
slouken@2249
   553
        if (n > 15) { \
slouken@2249
   554
            int adjust = 16 - ((uintptr_t)p & 15); \
slouken@2249
   555
            if (adjust < 16) { \
slouken@2249
   556
                n -= adjust; \
slouken@2249
   557
                adjust /= bpp; \
slouken@2249
   558
                while(adjust--) { \
slouken@2249
   559
                    *((type *)p) = (type)color; \
slouken@2249
   560
                    p += bpp; \
slouken@2249
   561
                } \
slouken@2249
   562
            } \
slouken@2249
   563
            SSE_WORK; \
slouken@2249
   564
        } \
slouken@2249
   565
        if (n & 63) { \
slouken@2249
   566
            int remainder = (n & 63); \
slouken@2249
   567
            remainder /= bpp; \
slouken@2249
   568
            while(remainder--) { \
slouken@2249
   569
                *((type *)p) = (type)color; \
slouken@2249
   570
                p += bpp; \
slouken@2249
   571
            } \
slouken@2249
   572
        } \
slouken@2249
   573
        pixels += pitch; \
slouken@2249
   574
    } \
slouken@2249
   575
 \
slouken@2249
   576
    SSE_END; \
slouken@532
   577
}
slouken@532
   578
slouken@2249
   579
DEFINE_SSE_FILLRECT(1, Uint8)
slouken@2249
   580
DEFINE_SSE_FILLRECT(2, Uint16)
slouken@2249
   581
DEFINE_SSE_FILLRECT(4, Uint32)
slouken@2249
   582
slouken@2249
   583
/* *INDENT-ON* */
slouken@2249
   584
#endif /* __SSE__ */
slouken@2249
   585
slouken@2249
   586
#ifdef __MMX__
slouken@2249
   587
/* *INDENT-OFF* */
slouken@2249
   588
slouken@2249
   589
#define MMX_BEGIN \
slouken@2249
   590
    __m64 c64 = _mm_set_pi32(color, color)
slouken@2249
   591
slouken@2249
   592
#define MMX_WORK \
slouken@2249
   593
    for (i = n / 64; i--;) { \
slouken@2249
   594
        _mm_stream_pi((__m64 *)(p+0), c64); \
slouken@2249
   595
        _mm_stream_pi((__m64 *)(p+8), c64); \
slouken@2249
   596
        _mm_stream_pi((__m64 *)(p+16), c64); \
slouken@2249
   597
        _mm_stream_pi((__m64 *)(p+24), c64); \
slouken@2249
   598
        _mm_stream_pi((__m64 *)(p+32), c64); \
slouken@2249
   599
        _mm_stream_pi((__m64 *)(p+40), c64); \
slouken@2249
   600
        _mm_stream_pi((__m64 *)(p+48), c64); \
slouken@2249
   601
        _mm_stream_pi((__m64 *)(p+56), c64); \
slouken@2249
   602
        p += 64; \
slouken@2249
   603
    }
slouken@2249
   604
slouken@2249
   605
#define MMX_END \
slouken@2249
   606
    _mm_empty()
slouken@2249
   607
slouken@2249
   608
#define DEFINE_MMX_FILLRECT(bpp, type) \
slouken@2249
   609
static void \
slouken@2249
   610
SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2249
   611
{ \
slouken@2249
   612
    MMX_BEGIN; \
slouken@2249
   613
 \
slouken@2249
   614
    while (h--) { \
slouken@2249
   615
        int i, n = w * bpp; \
slouken@2249
   616
        Uint8 *p = pixels; \
slouken@2249
   617
 \
slouken@2249
   618
        if (n > 7) { \
slouken@2249
   619
            int adjust = 8 - ((uintptr_t)p & 7); \
slouken@2249
   620
            if (adjust < 8) { \
slouken@2249
   621
                n -= adjust; \
slouken@2249
   622
                adjust /= bpp; \
slouken@2249
   623
                while(adjust--) { \
slouken@2249
   624
                    *((type *)p) = (type)color; \
slouken@2249
   625
                    p += bpp; \
slouken@2249
   626
                } \
slouken@2249
   627
            } \
slouken@2249
   628
            MMX_WORK; \
slouken@2249
   629
        } \
slouken@2249
   630
        if (n & 63) { \
slouken@2249
   631
            int remainder = (n & 63); \
slouken@2249
   632
            remainder /= bpp; \
slouken@2249
   633
            while(remainder--) { \
slouken@2249
   634
                *((type *)p) = (type)color; \
slouken@2249
   635
                p += bpp; \
slouken@2249
   636
            } \
slouken@2249
   637
        } \
slouken@2249
   638
        pixels += pitch; \
slouken@2249
   639
    } \
slouken@2249
   640
 \
slouken@2249
   641
    MMX_END; \
slouken@2249
   642
}
slouken@2249
   643
slouken@2249
   644
DEFINE_MMX_FILLRECT(1, Uint8)
slouken@2249
   645
DEFINE_MMX_FILLRECT(2, Uint16)
slouken@2249
   646
DEFINE_MMX_FILLRECT(4, Uint32)
slouken@2249
   647
slouken@2249
   648
/* *INDENT-ON* */
slouken@2249
   649
#endif /* __MMX__ */
slouken@2249
   650
slouken@2249
   651
static void
slouken@2249
   652
SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@532
   653
{
slouken@2249
   654
    while (h--) {
slouken@2249
   655
        int n = w;
slouken@2249
   656
        Uint8 *p = pixels;
slouken@2249
   657
slouken@2249
   658
        if (n > 3) {
slouken@2249
   659
            switch ((uintptr_t) p & 3) {
slouken@2249
   660
            case 1:
slouken@2249
   661
                *p++ = (Uint8) color;
slouken@2249
   662
                --n;
slouken@2249
   663
            case 2:
slouken@2249
   664
                *p++ = (Uint8) color;
slouken@2249
   665
                --n;
slouken@2249
   666
            case 3:
slouken@2249
   667
                *p++ = (Uint8) color;
slouken@2249
   668
                --n;
slouken@2249
   669
            }
slouken@2249
   670
            SDL_memset4(p, color, (n >> 2));
slouken@2249
   671
        }
slouken@2249
   672
        if (n & 3) {
slouken@2249
   673
            p += (n & ~3);
slouken@2249
   674
            switch (n & 3) {
slouken@2249
   675
            case 3:
slouken@2249
   676
                *p++ = (Uint8) color;
slouken@2249
   677
            case 2:
slouken@2249
   678
                *p++ = (Uint8) color;
slouken@2249
   679
            case 1:
slouken@2249
   680
                *p++ = (Uint8) color;
slouken@2249
   681
            }
slouken@2249
   682
        }
slouken@2249
   683
        pixels += pitch;
slouken@2249
   684
    }
slouken@2249
   685
}
slouken@2249
   686
slouken@2249
   687
static void
slouken@2249
   688
SDL_FillRect2(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   689
{
slouken@2249
   690
    while (h--) {
slouken@2249
   691
        int n = w;
slouken@2249
   692
        Uint16 *p = (Uint16 *) pixels;
slouken@2249
   693
slouken@2249
   694
        if (n > 1) {
slouken@2249
   695
            if ((uintptr_t) p & 2) {
slouken@2249
   696
                *p++ = (Uint16) color;
slouken@2249
   697
                --n;
slouken@2249
   698
            }
slouken@2249
   699
            SDL_memset4(p, color, (n >> 1));
slouken@2249
   700
        }
slouken@2249
   701
        if (n & 1) {
slouken@2249
   702
            p[n - 1] = (Uint16) color;
slouken@2249
   703
        }
slouken@2249
   704
        pixels += pitch;
slouken@2249
   705
    }
slouken@2249
   706
}
slouken@2249
   707
slouken@2249
   708
static void
slouken@2249
   709
SDL_FillRect3(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   710
{
slouken@2249
   711
    Uint8 r = (Uint8) (color & 0xFF);
slouken@2249
   712
    Uint8 g = (Uint8) ((color >> 8) & 0xFF);
slouken@2249
   713
    Uint8 b = (Uint8) ((color >> 16) & 0xFF);
slouken@2249
   714
slouken@2249
   715
    while (h--) {
slouken@2249
   716
        int n = w;
slouken@2249
   717
        Uint8 *p = pixels;
slouken@2249
   718
slouken@2249
   719
        while (n--) {
slouken@2249
   720
            *p++ = r;
slouken@2249
   721
            *p++ = g;
slouken@2249
   722
            *p++ = b;
slouken@2249
   723
        }
slouken@2249
   724
        pixels += pitch;
slouken@2249
   725
    }
slouken@2249
   726
}
slouken@2249
   727
slouken@2249
   728
static void
slouken@2249
   729
SDL_FillRect4(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   730
{
slouken@2249
   731
    while (h--) {
slouken@2249
   732
        SDL_memset4(pixels, color, w);
slouken@2249
   733
        pixels += pitch;
slouken@2249
   734
    }
slouken@532
   735
}
slouken@532
   736
slouken@0
   737
/* 
slouken@0
   738
 * This function performs a fast fill of the given rectangle with 'color'
slouken@0
   739
 */
slouken@1895
   740
int
slouken@1895
   741
SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@0
   742
{
slouken@2249
   743
    Uint8 *pixels;
slouken@0
   744
slouken@1895
   745
    /* This function doesn't work on surfaces < 8 bpp */
slouken@1895
   746
    if (dst->format->BitsPerPixel < 8) {
slouken@2249
   747
        SDL_SetError("Fill rect on unsupported surface format");
slouken@2249
   748
        return (-1);
slouken@1895
   749
    }
slouken@532
   750
slouken@1895
   751
    /* If 'dstrect' == NULL, then fill the whole surface */
slouken@1895
   752
    if (dstrect) {
slouken@1895
   753
        /* Perform clipping */
slouken@1895
   754
        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@1895
   755
            return (0);
slouken@1895
   756
        }
slouken@1895
   757
    } else {
slouken@1895
   758
        dstrect = &dst->clip_rect;
slouken@1895
   759
    }
slouken@0
   760
slouken@1895
   761
    /* Perform software fill */
slouken@1895
   762
    if (SDL_LockSurface(dst) != 0) {
slouken@1895
   763
        return (-1);
slouken@1895
   764
    }
slouken@2249
   765
slouken@2249
   766
    pixels =
slouken@2249
   767
        (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
slouken@1895
   768
        dstrect->x * dst->format->BytesPerPixel;
slouken@2249
   769
slouken@2249
   770
    switch (dst->format->BytesPerPixel) {
slouken@2249
   771
    case 1:
slouken@2234
   772
        {
slouken@2249
   773
            color |= (color << 8);
slouken@2249
   774
            color |= (color << 16);
slouken@2249
   775
#ifdef __SSE__
slouken@2249
   776
            if (SDL_HasSSE()) {
slouken@2249
   777
                SDL_FillRect1SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   778
                                 dstrect->h);
slouken@2239
   779
                break;
slouken@2239
   780
            }
slouken@2239
   781
#endif
slouken@2249
   782
#ifdef __MMX__
slouken@2249
   783
            if (SDL_HasMMX()) {
slouken@2249
   784
                SDL_FillRect1MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   785
                                 dstrect->h);
slouken@2249
   786
                break;
slouken@1895
   787
            }
slouken@2249
   788
#endif
slouken@2249
   789
            SDL_FillRect1(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   790
            break;
slouken@2249
   791
        }
slouken@2249
   792
slouken@2249
   793
    case 2:
slouken@2249
   794
        {
slouken@2249
   795
            color |= (color << 16);
slouken@2249
   796
#ifdef __SSE__
slouken@2249
   797
            if (SDL_HasSSE()) {
slouken@2249
   798
                SDL_FillRect2SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   799
                                 dstrect->h);
slouken@2249
   800
                break;
slouken@2249
   801
            }
slouken@2249
   802
#endif
slouken@2249
   803
#ifdef __MMX__
slouken@2249
   804
            if (SDL_HasMMX()) {
slouken@2249
   805
                SDL_FillRect2MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   806
                                 dstrect->h);
slouken@2249
   807
                break;
slouken@2249
   808
            }
slouken@2249
   809
#endif
slouken@2249
   810
            SDL_FillRect2(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   811
            break;
slouken@2249
   812
        }
slouken@2249
   813
slouken@2249
   814
    case 3:
slouken@2249
   815
        /* 24-bit RGB is a slow path, at least for now. */
slouken@2249
   816
        {
slouken@2249
   817
            SDL_FillRect3(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   818
            break;
slouken@2249
   819
        }
slouken@2249
   820
slouken@2249
   821
    case 4:
slouken@2249
   822
        {
slouken@2249
   823
#ifdef __SSE__
slouken@2249
   824
            if (SDL_HasSSE()) {
slouken@2249
   825
                SDL_FillRect4SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   826
                                 dstrect->h);
slouken@2249
   827
                break;
slouken@2249
   828
            }
slouken@2249
   829
#endif
slouken@2249
   830
#ifdef __MMX__
slouken@2249
   831
            if (SDL_HasMMX()) {
slouken@2249
   832
                SDL_FillRect4MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   833
                                 dstrect->h);
slouken@2249
   834
                break;
slouken@2249
   835
            }
slouken@2249
   836
#endif
slouken@2249
   837
            SDL_FillRect4(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@1895
   838
            break;
slouken@1895
   839
        }
slouken@1895
   840
    }
slouken@2249
   841
slouken@1895
   842
    SDL_UnlockSurface(dst);
slouken@0
   843
slouken@1895
   844
    /* We're done! */
slouken@1895
   845
    return (0);
slouken@0
   846
}
slouken@0
   847
slouken@0
   848
/*
slouken@0
   849
 * Lock a surface to directly access the pixels
slouken@0
   850
 */
slouken@1895
   851
int
slouken@1895
   852
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   853
{
slouken@1895
   854
    if (!surface->locked) {
slouken@1895
   855
        /* Perform the lock */
slouken@1895
   856
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   857
            SDL_UnRLESurface(surface, 1);
slouken@1895
   858
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   859
        }
slouken@1895
   860
    }
slouken@0
   861
slouken@1895
   862
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   863
    ++surface->locked;
slouken@0
   864
slouken@1895
   865
    /* Ready to go.. */
slouken@1895
   866
    return (0);
slouken@0
   867
}
slouken@1895
   868
slouken@0
   869
/*
slouken@0
   870
 * Unlock a previously locked surface
slouken@0
   871
 */
slouken@1895
   872
void
slouken@1895
   873
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   874
{
slouken@1895
   875
    /* Only perform an unlock if we are locked */
slouken@1895
   876
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   877
        return;
slouken@1895
   878
    }
slouken@0
   879
slouken@1895
   880
    /* Update RLE encoded surface with new data */
slouken@1895
   881
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   882
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   883
        SDL_RLESurface(surface);
slouken@1895
   884
    }
slouken@0
   885
}
slouken@0
   886
slouken@0
   887
/* 
slouken@0
   888
 * Convert a surface into the specified pixel format.
slouken@0
   889
 */
slouken@1895
   890
SDL_Surface *
slouken@1895
   891
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   892
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   893
{
slouken@1895
   894
    SDL_Surface *convert;
slouken@1895
   895
    Uint32 colorkey = 0;
slouken@1895
   896
    Uint8 alpha = 0;
slouken@1895
   897
    Uint32 surface_flags;
slouken@1895
   898
    SDL_Rect bounds;
slouken@0
   899
slouken@1895
   900
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   901
    if (format->palette != NULL) {
slouken@1895
   902
        int i;
slouken@1895
   903
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   904
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   905
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   906
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   907
                break;
slouken@1895
   908
        }
slouken@1895
   909
        if (i == format->palette->ncolors) {
slouken@1895
   910
            SDL_SetError("Empty destination palette");
slouken@1895
   911
            return (NULL);
slouken@1895
   912
        }
slouken@1895
   913
    }
slouken@0
   914
slouken@1895
   915
    /* Create a new surface with the desired format */
slouken@1895
   916
    convert = SDL_CreateRGBSurface(flags,
slouken@1895
   917
                                   surface->w, surface->h,
slouken@1895
   918
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   919
                                   format->Gmask, format->Bmask,
slouken@1895
   920
                                   format->Amask);
slouken@1895
   921
    if (convert == NULL) {
slouken@1895
   922
        return (NULL);
slouken@1895
   923
    }
slouken@264
   924
slouken@1895
   925
    /* Copy the palette if any */
slouken@1895
   926
    if (format->palette && convert->format->palette) {
slouken@1895
   927
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   928
                   format->palette->colors,
slouken@1895
   929
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   930
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   931
    }
slouken@0
   932
slouken@1895
   933
    /* Save the original surface color key and alpha */
slouken@1895
   934
    surface_flags = surface->flags;
slouken@1895
   935
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   936
        /* Convert colourkeyed surfaces to RGBA if requested */
slouken@1895
   937
        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
slouken@1895
   938
            surface_flags &= ~SDL_SRCCOLORKEY;
slouken@1895
   939
        } else {
slouken@1895
   940
            colorkey = surface->format->colorkey;
slouken@1895
   941
            SDL_SetColorKey(surface, 0, 0);
slouken@1895
   942
        }
slouken@1895
   943
    }
slouken@1895
   944
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   945
        /* Copy over the alpha channel to RGBA if requested */
slouken@1895
   946
        if (format->Amask) {
slouken@1895
   947
            surface->flags &= ~SDL_SRCALPHA;
slouken@1895
   948
        } else {
slouken@1895
   949
            alpha = surface->format->alpha;
slouken@1895
   950
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   951
        }
slouken@1895
   952
    }
slouken@0
   953
slouken@1895
   954
    /* Copy over the image data */
slouken@1895
   955
    bounds.x = 0;
slouken@1895
   956
    bounds.y = 0;
slouken@1895
   957
    bounds.w = surface->w;
slouken@1895
   958
    bounds.h = surface->h;
slouken@1895
   959
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   960
slouken@1895
   961
    /* Clean up the original surface, and update converted surface */
slouken@1895
   962
    if (convert != NULL) {
slouken@1895
   963
        SDL_SetClipRect(convert, &surface->clip_rect);
slouken@1895
   964
    }
slouken@1895
   965
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   966
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   967
        if (convert != NULL) {
slouken@1895
   968
            Uint8 keyR, keyG, keyB;
slouken@0
   969
slouken@1895
   970
            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
slouken@1895
   971
            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
slouken@1895
   972
                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@1895
   973
        }
slouken@1895
   974
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1895
   975
    }
slouken@1895
   976
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   977
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   978
        if (convert != NULL) {
slouken@1895
   979
            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
slouken@1895
   980
        }
slouken@1895
   981
        if (format->Amask) {
slouken@1895
   982
            surface->flags |= SDL_SRCALPHA;
slouken@1895
   983
        } else {
slouken@1895
   984
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1895
   985
        }
slouken@1895
   986
    }
slouken@0
   987
slouken@1895
   988
    /* We're ready to go! */
slouken@1895
   989
    return (convert);
slouken@0
   990
}
slouken@0
   991
slouken@0
   992
/*
slouken@0
   993
 * Free a surface created by the above function.
slouken@0
   994
 */
slouken@1895
   995
void
slouken@1895
   996
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   997
{
slouken@1895
   998
    if (surface == NULL) {
slouken@1895
   999
        return;
slouken@1895
  1000
    }
slouken@1895
  1001
    if (--surface->refcount > 0) {
slouken@1895
  1002
        return;
slouken@1895
  1003
    }
slouken@1895
  1004
    while (surface->locked > 0) {
slouken@1895
  1005
        SDL_UnlockSurface(surface);
slouken@1895
  1006
    }
slouken@1895
  1007
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1008
        SDL_UnRLESurface(surface, 0);
slouken@1895
  1009
    }
slouken@1895
  1010
    if (surface->format) {
slouken@1895
  1011
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
  1012
        SDL_FreeFormat(surface->format);
slouken@1895
  1013
        surface->format = NULL;
slouken@1895
  1014
    }
slouken@1895
  1015
    if (surface->map != NULL) {
slouken@1895
  1016
        SDL_FreeBlitMap(surface->map);
slouken@1895
  1017
        surface->map = NULL;
slouken@1895
  1018
    }
slouken@1895
  1019
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
  1020
        SDL_free(surface->pixels);
slouken@1895
  1021
    }
slouken@1895
  1022
    SDL_free(surface);
slouken@0
  1023
#ifdef CHECK_LEAKS
slouken@1895
  1024
    --surfaces_allocated;
slouken@0
  1025
#endif
slouken@0
  1026
}
slouken@1895
  1027
slouken@1895
  1028
/* vi: set ts=4 sw=4 expandtab: */