src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Aug 2007 05:56:24 +0000
changeset 2249 5a58b57b6724
parent 2239 31835fd24b2b
child 2251 292bee385630
permissions -rw-r--r--
Added SSE and MMX optimization for SDL_FillRect()
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@2249
   515
#define SSE_BEGIN \
slouken@2249
   516
    DECLARE_ALIGNED(Uint32, cccc[4], 16); \
slouken@2249
   517
    cccc[0] = color; \
slouken@2249
   518
    cccc[1] = color; \
slouken@2249
   519
    cccc[2] = color; \
slouken@2249
   520
    cccc[3] = color; \
slouken@2249
   521
    __m128 c128 = *(__m128 *)cccc;
slouken@2249
   522
slouken@2249
   523
#define SSE_WORK \
slouken@2249
   524
    for (i = n / 64; i--;) { \
slouken@2249
   525
        _mm_stream_ps((float *)(p+0), c128); \
slouken@2249
   526
        _mm_stream_ps((float *)(p+16), c128); \
slouken@2249
   527
        _mm_stream_ps((float *)(p+32), c128); \
slouken@2249
   528
        _mm_stream_ps((float *)(p+48), c128); \
slouken@2249
   529
        p += 64; \
slouken@2249
   530
    }
slouken@2249
   531
slouken@2249
   532
#define SSE_END
slouken@2249
   533
slouken@2249
   534
#define DEFINE_SSE_FILLRECT(bpp, type) \
slouken@2249
   535
static void \
slouken@2249
   536
SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2249
   537
{ \
slouken@2249
   538
    SSE_BEGIN; \
slouken@2249
   539
 \
slouken@2249
   540
    while (h--) { \
slouken@2249
   541
        int i, n = w * bpp; \
slouken@2249
   542
        Uint8 *p = pixels; \
slouken@2249
   543
 \
slouken@2249
   544
        if (n > 15) { \
slouken@2249
   545
            int adjust = 16 - ((uintptr_t)p & 15); \
slouken@2249
   546
            if (adjust < 16) { \
slouken@2249
   547
                n -= adjust; \
slouken@2249
   548
                adjust /= bpp; \
slouken@2249
   549
                while(adjust--) { \
slouken@2249
   550
                    *((type *)p) = (type)color; \
slouken@2249
   551
                    p += bpp; \
slouken@2249
   552
                } \
slouken@2249
   553
            } \
slouken@2249
   554
            SSE_WORK; \
slouken@2249
   555
        } \
slouken@2249
   556
        if (n & 63) { \
slouken@2249
   557
            int remainder = (n & 63); \
slouken@2249
   558
            remainder /= bpp; \
slouken@2249
   559
            while(remainder--) { \
slouken@2249
   560
                *((type *)p) = (type)color; \
slouken@2249
   561
                p += bpp; \
slouken@2249
   562
            } \
slouken@2249
   563
        } \
slouken@2249
   564
        pixels += pitch; \
slouken@2249
   565
    } \
slouken@2249
   566
 \
slouken@2249
   567
    SSE_END; \
slouken@532
   568
}
slouken@532
   569
slouken@2249
   570
DEFINE_SSE_FILLRECT(1, Uint8)
slouken@2249
   571
DEFINE_SSE_FILLRECT(2, Uint16)
slouken@2249
   572
DEFINE_SSE_FILLRECT(4, Uint32)
slouken@2249
   573
slouken@2249
   574
/* *INDENT-ON* */
slouken@2249
   575
#endif /* __SSE__ */
slouken@2249
   576
slouken@2249
   577
#ifdef __MMX__
slouken@2249
   578
/* *INDENT-OFF* */
slouken@2249
   579
slouken@2249
   580
#define MMX_BEGIN \
slouken@2249
   581
    __m64 c64 = _mm_set_pi32(color, color)
slouken@2249
   582
slouken@2249
   583
#define MMX_WORK \
slouken@2249
   584
    for (i = n / 64; i--;) { \
slouken@2249
   585
        _mm_stream_pi((__m64 *)(p+0), c64); \
slouken@2249
   586
        _mm_stream_pi((__m64 *)(p+8), c64); \
slouken@2249
   587
        _mm_stream_pi((__m64 *)(p+16), c64); \
slouken@2249
   588
        _mm_stream_pi((__m64 *)(p+24), c64); \
slouken@2249
   589
        _mm_stream_pi((__m64 *)(p+32), c64); \
slouken@2249
   590
        _mm_stream_pi((__m64 *)(p+40), c64); \
slouken@2249
   591
        _mm_stream_pi((__m64 *)(p+48), c64); \
slouken@2249
   592
        _mm_stream_pi((__m64 *)(p+56), c64); \
slouken@2249
   593
        p += 64; \
slouken@2249
   594
    }
slouken@2249
   595
slouken@2249
   596
#define MMX_END \
slouken@2249
   597
    _mm_empty()
slouken@2249
   598
slouken@2249
   599
#define DEFINE_MMX_FILLRECT(bpp, type) \
slouken@2249
   600
static void \
slouken@2249
   601
SDL_FillRect##bpp##MMX(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
slouken@2249
   602
{ \
slouken@2249
   603
    MMX_BEGIN; \
slouken@2249
   604
 \
slouken@2249
   605
    while (h--) { \
slouken@2249
   606
        int i, n = w * bpp; \
slouken@2249
   607
        Uint8 *p = pixels; \
slouken@2249
   608
 \
slouken@2249
   609
        if (n > 7) { \
slouken@2249
   610
            int adjust = 8 - ((uintptr_t)p & 7); \
slouken@2249
   611
            if (adjust < 8) { \
slouken@2249
   612
                n -= adjust; \
slouken@2249
   613
                adjust /= bpp; \
slouken@2249
   614
                while(adjust--) { \
slouken@2249
   615
                    *((type *)p) = (type)color; \
slouken@2249
   616
                    p += bpp; \
slouken@2249
   617
                } \
slouken@2249
   618
            } \
slouken@2249
   619
            MMX_WORK; \
slouken@2249
   620
        } \
slouken@2249
   621
        if (n & 63) { \
slouken@2249
   622
            int remainder = (n & 63); \
slouken@2249
   623
            remainder /= bpp; \
slouken@2249
   624
            while(remainder--) { \
slouken@2249
   625
                *((type *)p) = (type)color; \
slouken@2249
   626
                p += bpp; \
slouken@2249
   627
            } \
slouken@2249
   628
        } \
slouken@2249
   629
        pixels += pitch; \
slouken@2249
   630
    } \
slouken@2249
   631
 \
slouken@2249
   632
    MMX_END; \
slouken@2249
   633
}
slouken@2249
   634
slouken@2249
   635
DEFINE_MMX_FILLRECT(1, Uint8)
slouken@2249
   636
DEFINE_MMX_FILLRECT(2, Uint16)
slouken@2249
   637
DEFINE_MMX_FILLRECT(4, Uint32)
slouken@2249
   638
slouken@2249
   639
/* *INDENT-ON* */
slouken@2249
   640
#endif /* __MMX__ */
slouken@2249
   641
slouken@2249
   642
static void
slouken@2249
   643
SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@532
   644
{
slouken@2249
   645
    while (h--) {
slouken@2249
   646
        int n = w;
slouken@2249
   647
        Uint8 *p = pixels;
slouken@2249
   648
slouken@2249
   649
        if (n > 3) {
slouken@2249
   650
            switch ((uintptr_t) p & 3) {
slouken@2249
   651
            case 1:
slouken@2249
   652
                *p++ = (Uint8) color;
slouken@2249
   653
                --n;
slouken@2249
   654
            case 2:
slouken@2249
   655
                *p++ = (Uint8) color;
slouken@2249
   656
                --n;
slouken@2249
   657
            case 3:
slouken@2249
   658
                *p++ = (Uint8) color;
slouken@2249
   659
                --n;
slouken@2249
   660
            }
slouken@2249
   661
            SDL_memset4(p, color, (n >> 2));
slouken@2249
   662
        }
slouken@2249
   663
        if (n & 3) {
slouken@2249
   664
            p += (n & ~3);
slouken@2249
   665
            switch (n & 3) {
slouken@2249
   666
            case 3:
slouken@2249
   667
                *p++ = (Uint8) color;
slouken@2249
   668
            case 2:
slouken@2249
   669
                *p++ = (Uint8) color;
slouken@2249
   670
            case 1:
slouken@2249
   671
                *p++ = (Uint8) color;
slouken@2249
   672
            }
slouken@2249
   673
        }
slouken@2249
   674
        pixels += pitch;
slouken@2249
   675
    }
slouken@2249
   676
}
slouken@2249
   677
slouken@2249
   678
static void
slouken@2249
   679
SDL_FillRect2(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   680
{
slouken@2249
   681
    while (h--) {
slouken@2249
   682
        int n = w;
slouken@2249
   683
        Uint16 *p = (Uint16 *) pixels;
slouken@2249
   684
slouken@2249
   685
        if (n > 1) {
slouken@2249
   686
            if ((uintptr_t) p & 2) {
slouken@2249
   687
                *p++ = (Uint16) color;
slouken@2249
   688
                --n;
slouken@2249
   689
            }
slouken@2249
   690
            SDL_memset4(p, color, (n >> 1));
slouken@2249
   691
        }
slouken@2249
   692
        if (n & 1) {
slouken@2249
   693
            p[n - 1] = (Uint16) color;
slouken@2249
   694
        }
slouken@2249
   695
        pixels += pitch;
slouken@2249
   696
    }
slouken@2249
   697
}
slouken@2249
   698
slouken@2249
   699
static void
slouken@2249
   700
SDL_FillRect3(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   701
{
slouken@2249
   702
    Uint8 r = (Uint8) (color & 0xFF);
slouken@2249
   703
    Uint8 g = (Uint8) ((color >> 8) & 0xFF);
slouken@2249
   704
    Uint8 b = (Uint8) ((color >> 16) & 0xFF);
slouken@2249
   705
slouken@2249
   706
    while (h--) {
slouken@2249
   707
        int n = w;
slouken@2249
   708
        Uint8 *p = pixels;
slouken@2249
   709
slouken@2249
   710
        while (n--) {
slouken@2249
   711
            *p++ = r;
slouken@2249
   712
            *p++ = g;
slouken@2249
   713
            *p++ = b;
slouken@2249
   714
        }
slouken@2249
   715
        pixels += pitch;
slouken@2249
   716
    }
slouken@2249
   717
}
slouken@2249
   718
slouken@2249
   719
static void
slouken@2249
   720
SDL_FillRect4(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
slouken@2249
   721
{
slouken@2249
   722
    while (h--) {
slouken@2249
   723
        SDL_memset4(pixels, color, w);
slouken@2249
   724
        pixels += pitch;
slouken@2249
   725
    }
slouken@532
   726
}
slouken@532
   727
slouken@0
   728
/* 
slouken@0
   729
 * This function performs a fast fill of the given rectangle with 'color'
slouken@0
   730
 */
slouken@1895
   731
int
slouken@1895
   732
SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@0
   733
{
slouken@2249
   734
    Uint8 *pixels;
slouken@0
   735
slouken@1895
   736
    /* This function doesn't work on surfaces < 8 bpp */
slouken@1895
   737
    if (dst->format->BitsPerPixel < 8) {
slouken@2249
   738
        SDL_SetError("Fill rect on unsupported surface format");
slouken@2249
   739
        return (-1);
slouken@1895
   740
    }
slouken@532
   741
slouken@1895
   742
    /* If 'dstrect' == NULL, then fill the whole surface */
slouken@1895
   743
    if (dstrect) {
slouken@1895
   744
        /* Perform clipping */
slouken@1895
   745
        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@1895
   746
            return (0);
slouken@1895
   747
        }
slouken@1895
   748
    } else {
slouken@1895
   749
        dstrect = &dst->clip_rect;
slouken@1895
   750
    }
slouken@0
   751
slouken@1895
   752
    /* Perform software fill */
slouken@1895
   753
    if (SDL_LockSurface(dst) != 0) {
slouken@1895
   754
        return (-1);
slouken@1895
   755
    }
slouken@2249
   756
slouken@2249
   757
    pixels =
slouken@2249
   758
        (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
slouken@1895
   759
        dstrect->x * dst->format->BytesPerPixel;
slouken@2249
   760
slouken@2249
   761
    switch (dst->format->BytesPerPixel) {
slouken@2249
   762
    case 1:
slouken@2234
   763
        {
slouken@2249
   764
            color |= (color << 8);
slouken@2249
   765
            color |= (color << 16);
slouken@2249
   766
#ifdef __SSE__
slouken@2249
   767
            if (SDL_HasSSE()) {
slouken@2249
   768
                SDL_FillRect1SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   769
                                 dstrect->h);
slouken@2239
   770
                break;
slouken@2239
   771
            }
slouken@2239
   772
#endif
slouken@2249
   773
#ifdef __MMX__
slouken@2249
   774
            if (SDL_HasMMX()) {
slouken@2249
   775
                SDL_FillRect1MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   776
                                 dstrect->h);
slouken@2249
   777
                break;
slouken@1895
   778
            }
slouken@2249
   779
#endif
slouken@2249
   780
            SDL_FillRect1(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   781
            break;
slouken@2249
   782
        }
slouken@2249
   783
slouken@2249
   784
    case 2:
slouken@2249
   785
        {
slouken@2249
   786
            color |= (color << 16);
slouken@2249
   787
#ifdef __SSE__
slouken@2249
   788
            if (SDL_HasSSE()) {
slouken@2249
   789
                SDL_FillRect2SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   790
                                 dstrect->h);
slouken@2249
   791
                break;
slouken@2249
   792
            }
slouken@2249
   793
#endif
slouken@2249
   794
#ifdef __MMX__
slouken@2249
   795
            if (SDL_HasMMX()) {
slouken@2249
   796
                SDL_FillRect2MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   797
                                 dstrect->h);
slouken@2249
   798
                break;
slouken@2249
   799
            }
slouken@2249
   800
#endif
slouken@2249
   801
            SDL_FillRect2(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   802
            break;
slouken@2249
   803
        }
slouken@2249
   804
slouken@2249
   805
    case 3:
slouken@2249
   806
        /* 24-bit RGB is a slow path, at least for now. */
slouken@2249
   807
        {
slouken@2249
   808
            SDL_FillRect3(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@2249
   809
            break;
slouken@2249
   810
        }
slouken@2249
   811
slouken@2249
   812
    case 4:
slouken@2249
   813
        {
slouken@2249
   814
#ifdef __SSE__
slouken@2249
   815
            if (SDL_HasSSE()) {
slouken@2249
   816
                SDL_FillRect4SSE(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   817
                                 dstrect->h);
slouken@2249
   818
                break;
slouken@2249
   819
            }
slouken@2249
   820
#endif
slouken@2249
   821
#ifdef __MMX__
slouken@2249
   822
            if (SDL_HasMMX()) {
slouken@2249
   823
                SDL_FillRect4MMX(pixels, dst->pitch, color, dstrect->w,
slouken@2249
   824
                                 dstrect->h);
slouken@2249
   825
                break;
slouken@2249
   826
            }
slouken@2249
   827
#endif
slouken@2249
   828
            SDL_FillRect4(pixels, dst->pitch, color, dstrect->w, dstrect->h);
slouken@1895
   829
            break;
slouken@1895
   830
        }
slouken@1895
   831
    }
slouken@2249
   832
slouken@1895
   833
    SDL_UnlockSurface(dst);
slouken@0
   834
slouken@1895
   835
    /* We're done! */
slouken@1895
   836
    return (0);
slouken@0
   837
}
slouken@0
   838
slouken@0
   839
/*
slouken@0
   840
 * Lock a surface to directly access the pixels
slouken@0
   841
 */
slouken@1895
   842
int
slouken@1895
   843
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   844
{
slouken@1895
   845
    if (!surface->locked) {
slouken@1895
   846
        /* Perform the lock */
slouken@1895
   847
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   848
            SDL_UnRLESurface(surface, 1);
slouken@1895
   849
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   850
        }
slouken@1895
   851
    }
slouken@0
   852
slouken@1895
   853
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   854
    ++surface->locked;
slouken@0
   855
slouken@1895
   856
    /* Ready to go.. */
slouken@1895
   857
    return (0);
slouken@0
   858
}
slouken@1895
   859
slouken@0
   860
/*
slouken@0
   861
 * Unlock a previously locked surface
slouken@0
   862
 */
slouken@1895
   863
void
slouken@1895
   864
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   865
{
slouken@1895
   866
    /* Only perform an unlock if we are locked */
slouken@1895
   867
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   868
        return;
slouken@1895
   869
    }
slouken@0
   870
slouken@1895
   871
    /* Update RLE encoded surface with new data */
slouken@1895
   872
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   873
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   874
        SDL_RLESurface(surface);
slouken@1895
   875
    }
slouken@0
   876
}
slouken@0
   877
slouken@0
   878
/* 
slouken@0
   879
 * Convert a surface into the specified pixel format.
slouken@0
   880
 */
slouken@1895
   881
SDL_Surface *
slouken@1895
   882
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   883
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   884
{
slouken@1895
   885
    SDL_Surface *convert;
slouken@1895
   886
    Uint32 colorkey = 0;
slouken@1895
   887
    Uint8 alpha = 0;
slouken@1895
   888
    Uint32 surface_flags;
slouken@1895
   889
    SDL_Rect bounds;
slouken@0
   890
slouken@1895
   891
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   892
    if (format->palette != NULL) {
slouken@1895
   893
        int i;
slouken@1895
   894
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   895
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   896
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   897
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   898
                break;
slouken@1895
   899
        }
slouken@1895
   900
        if (i == format->palette->ncolors) {
slouken@1895
   901
            SDL_SetError("Empty destination palette");
slouken@1895
   902
            return (NULL);
slouken@1895
   903
        }
slouken@1895
   904
    }
slouken@0
   905
slouken@1895
   906
    /* Create a new surface with the desired format */
slouken@1895
   907
    convert = SDL_CreateRGBSurface(flags,
slouken@1895
   908
                                   surface->w, surface->h,
slouken@1895
   909
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   910
                                   format->Gmask, format->Bmask,
slouken@1895
   911
                                   format->Amask);
slouken@1895
   912
    if (convert == NULL) {
slouken@1895
   913
        return (NULL);
slouken@1895
   914
    }
slouken@264
   915
slouken@1895
   916
    /* Copy the palette if any */
slouken@1895
   917
    if (format->palette && convert->format->palette) {
slouken@1895
   918
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   919
                   format->palette->colors,
slouken@1895
   920
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   921
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   922
    }
slouken@0
   923
slouken@1895
   924
    /* Save the original surface color key and alpha */
slouken@1895
   925
    surface_flags = surface->flags;
slouken@1895
   926
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   927
        /* Convert colourkeyed surfaces to RGBA if requested */
slouken@1895
   928
        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
slouken@1895
   929
            surface_flags &= ~SDL_SRCCOLORKEY;
slouken@1895
   930
        } else {
slouken@1895
   931
            colorkey = surface->format->colorkey;
slouken@1895
   932
            SDL_SetColorKey(surface, 0, 0);
slouken@1895
   933
        }
slouken@1895
   934
    }
slouken@1895
   935
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   936
        /* Copy over the alpha channel to RGBA if requested */
slouken@1895
   937
        if (format->Amask) {
slouken@1895
   938
            surface->flags &= ~SDL_SRCALPHA;
slouken@1895
   939
        } else {
slouken@1895
   940
            alpha = surface->format->alpha;
slouken@1895
   941
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   942
        }
slouken@1895
   943
    }
slouken@0
   944
slouken@1895
   945
    /* Copy over the image data */
slouken@1895
   946
    bounds.x = 0;
slouken@1895
   947
    bounds.y = 0;
slouken@1895
   948
    bounds.w = surface->w;
slouken@1895
   949
    bounds.h = surface->h;
slouken@1895
   950
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   951
slouken@1895
   952
    /* Clean up the original surface, and update converted surface */
slouken@1895
   953
    if (convert != NULL) {
slouken@1895
   954
        SDL_SetClipRect(convert, &surface->clip_rect);
slouken@1895
   955
    }
slouken@1895
   956
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   957
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   958
        if (convert != NULL) {
slouken@1895
   959
            Uint8 keyR, keyG, keyB;
slouken@0
   960
slouken@1895
   961
            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
slouken@1895
   962
            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
slouken@1895
   963
                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@1895
   964
        }
slouken@1895
   965
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1895
   966
    }
slouken@1895
   967
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   968
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   969
        if (convert != NULL) {
slouken@1895
   970
            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
slouken@1895
   971
        }
slouken@1895
   972
        if (format->Amask) {
slouken@1895
   973
            surface->flags |= SDL_SRCALPHA;
slouken@1895
   974
        } else {
slouken@1895
   975
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1895
   976
        }
slouken@1895
   977
    }
slouken@0
   978
slouken@1895
   979
    /* We're ready to go! */
slouken@1895
   980
    return (convert);
slouken@0
   981
}
slouken@0
   982
slouken@0
   983
/*
slouken@0
   984
 * Free a surface created by the above function.
slouken@0
   985
 */
slouken@1895
   986
void
slouken@1895
   987
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   988
{
slouken@1895
   989
    if (surface == NULL) {
slouken@1895
   990
        return;
slouken@1895
   991
    }
slouken@1895
   992
    if (--surface->refcount > 0) {
slouken@1895
   993
        return;
slouken@1895
   994
    }
slouken@1895
   995
    while (surface->locked > 0) {
slouken@1895
   996
        SDL_UnlockSurface(surface);
slouken@1895
   997
    }
slouken@1895
   998
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   999
        SDL_UnRLESurface(surface, 0);
slouken@1895
  1000
    }
slouken@1895
  1001
    if (surface->format) {
slouken@1895
  1002
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
  1003
        SDL_FreeFormat(surface->format);
slouken@1895
  1004
        surface->format = NULL;
slouken@1895
  1005
    }
slouken@1895
  1006
    if (surface->map != NULL) {
slouken@1895
  1007
        SDL_FreeBlitMap(surface->map);
slouken@1895
  1008
        surface->map = NULL;
slouken@1895
  1009
    }
slouken@1895
  1010
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
  1011
        SDL_free(surface->pixels);
slouken@1895
  1012
    }
slouken@1895
  1013
    SDL_free(surface);
slouken@0
  1014
#ifdef CHECK_LEAKS
slouken@1895
  1015
    --surfaces_allocated;
slouken@0
  1016
#endif
slouken@0
  1017
}
slouken@1895
  1018
slouken@1895
  1019
/* vi: set ts=4 sw=4 expandtab: */