src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Aug 2007 18:44:49 +0000
changeset 2235 55a8bebf9ba6
parent 2234 cf8c3b0117b3
child 2239 31835fd24b2b
permissions -rw-r--r--
indent
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@1895
   512
static int
slouken@1895
   513
SDL_FillRect1(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@532
   514
{
slouken@1895
   515
    /* FIXME: We have to worry about packing order.. *sigh* */
slouken@1895
   516
    SDL_SetError("1-bpp rect fill not yet implemented");
slouken@1895
   517
    return -1;
slouken@532
   518
}
slouken@532
   519
slouken@1895
   520
static int
slouken@1895
   521
SDL_FillRect4(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@532
   522
{
slouken@1895
   523
    /* FIXME: We have to worry about packing order.. *sigh* */
slouken@1895
   524
    SDL_SetError("4-bpp rect fill not yet implemented");
slouken@1895
   525
    return -1;
slouken@532
   526
}
slouken@532
   527
slouken@0
   528
/* 
slouken@0
   529
 * This function performs a fast fill of the given rectangle with 'color'
slouken@0
   530
 */
slouken@1895
   531
int
slouken@1895
   532
SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@0
   533
{
slouken@1895
   534
    int x, y;
slouken@1895
   535
    Uint8 *row;
slouken@0
   536
slouken@1895
   537
    /* This function doesn't work on surfaces < 8 bpp */
slouken@1895
   538
    if (dst->format->BitsPerPixel < 8) {
slouken@1895
   539
        switch (dst->format->BitsPerPixel) {
slouken@1895
   540
        case 1:
slouken@1895
   541
            return SDL_FillRect1(dst, dstrect, color);
slouken@1895
   542
            break;
slouken@1895
   543
        case 4:
slouken@1895
   544
            return SDL_FillRect4(dst, dstrect, color);
slouken@1895
   545
            break;
slouken@1895
   546
        default:
slouken@1895
   547
            SDL_SetError("Fill rect on unsupported surface format");
slouken@1895
   548
            return (-1);
slouken@1895
   549
            break;
slouken@1895
   550
        }
slouken@1895
   551
    }
slouken@532
   552
slouken@1895
   553
    /* If 'dstrect' == NULL, then fill the whole surface */
slouken@1895
   554
    if (dstrect) {
slouken@1895
   555
        /* Perform clipping */
slouken@1895
   556
        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@1895
   557
            return (0);
slouken@1895
   558
        }
slouken@1895
   559
    } else {
slouken@1895
   560
        dstrect = &dst->clip_rect;
slouken@1895
   561
    }
slouken@0
   562
slouken@1895
   563
    /* Perform software fill */
slouken@1895
   564
    if (SDL_LockSurface(dst) != 0) {
slouken@1895
   565
        return (-1);
slouken@1895
   566
    }
slouken@1895
   567
    row = (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
slouken@1895
   568
        dstrect->x * dst->format->BytesPerPixel;
slouken@1895
   569
    if (dst->format->palette || (color == 0)) {
slouken@1895
   570
        x = dstrect->w * dst->format->BytesPerPixel;
slouken@2235
   571
#ifndef __MACOSX__              /* memset() is optimized on Mac OS X */
slouken@1895
   572
        if (!color && !((uintptr_t) row & 3) && !(x & 3)
slouken@1895
   573
            && !(dst->pitch & 3)) {
slouken@1895
   574
            int n = x >> 2;
slouken@1895
   575
            for (y = dstrect->h; y; --y) {
slouken@1895
   576
                SDL_memset4(row, 0, n);
slouken@1895
   577
                row += dst->pitch;
slouken@1895
   578
            }
slouken@2234
   579
        } else
slouken@2234
   580
#endif /* !__MACOSX__ */
slouken@2234
   581
        {
slouken@2222
   582
            for (y = dstrect->h; y; y--) {
slouken@2222
   583
                SDL_memset(row, color, x);
slouken@2222
   584
                row += dst->pitch;
slouken@1895
   585
            }
slouken@1895
   586
        }
slouken@1895
   587
    } else {
slouken@1895
   588
        switch (dst->format->BytesPerPixel) {
slouken@1895
   589
        case 2:
slouken@1895
   590
            for (y = dstrect->h; y; --y) {
slouken@1895
   591
                Uint16 *pixels = (Uint16 *) row;
slouken@1895
   592
                Uint16 c = (Uint16) color;
slouken@1895
   593
                Uint32 cc = (Uint32) c << 16 | c;
slouken@1895
   594
                int n = dstrect->w;
slouken@1895
   595
                if ((uintptr_t) pixels & 3) {
slouken@1895
   596
                    *pixels++ = c;
slouken@1895
   597
                    n--;
slouken@1895
   598
                }
slouken@1895
   599
                if (n >> 1)
slouken@1895
   600
                    SDL_memset4(pixels, cc, n >> 1);
slouken@1895
   601
                if (n & 1)
slouken@1895
   602
                    pixels[n - 1] = c;
slouken@1895
   603
                row += dst->pitch;
slouken@1895
   604
            }
slouken@1895
   605
            break;
slouken@0
   606
slouken@1895
   607
        case 3:
slouken@1895
   608
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
   609
            color <<= 8;
slouken@1895
   610
#endif
slouken@1895
   611
            for (y = dstrect->h; y; --y) {
slouken@1895
   612
                Uint8 *pixels = row;
slouken@1895
   613
                for (x = dstrect->w; x; --x) {
slouken@1895
   614
                    SDL_memcpy(pixels, &color, 3);
slouken@1895
   615
                    pixels += 3;
slouken@1895
   616
                }
slouken@1895
   617
                row += dst->pitch;
slouken@1895
   618
            }
slouken@1895
   619
            break;
slouken@0
   620
slouken@1895
   621
        case 4:
slouken@1895
   622
            for (y = dstrect->h; y; --y) {
slouken@1895
   623
                SDL_memset4(row, color, dstrect->w);
slouken@1895
   624
                row += dst->pitch;
slouken@1895
   625
            }
slouken@1895
   626
            break;
slouken@1895
   627
        }
slouken@1895
   628
    }
slouken@1895
   629
    SDL_UnlockSurface(dst);
slouken@0
   630
slouken@1895
   631
    /* We're done! */
slouken@1895
   632
    return (0);
slouken@0
   633
}
slouken@0
   634
slouken@0
   635
/*
slouken@0
   636
 * Lock a surface to directly access the pixels
slouken@0
   637
 */
slouken@1895
   638
int
slouken@1895
   639
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   640
{
slouken@1895
   641
    if (!surface->locked) {
slouken@1895
   642
        /* Perform the lock */
slouken@1895
   643
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   644
            SDL_UnRLESurface(surface, 1);
slouken@1895
   645
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   646
        }
slouken@1895
   647
    }
slouken@0
   648
slouken@1895
   649
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   650
    ++surface->locked;
slouken@0
   651
slouken@1895
   652
    /* Ready to go.. */
slouken@1895
   653
    return (0);
slouken@0
   654
}
slouken@1895
   655
slouken@0
   656
/*
slouken@0
   657
 * Unlock a previously locked surface
slouken@0
   658
 */
slouken@1895
   659
void
slouken@1895
   660
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   661
{
slouken@1895
   662
    /* Only perform an unlock if we are locked */
slouken@1895
   663
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   664
        return;
slouken@1895
   665
    }
slouken@0
   666
slouken@1895
   667
    /* Update RLE encoded surface with new data */
slouken@1895
   668
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   669
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   670
        SDL_RLESurface(surface);
slouken@1895
   671
    }
slouken@0
   672
}
slouken@0
   673
slouken@0
   674
/* 
slouken@0
   675
 * Convert a surface into the specified pixel format.
slouken@0
   676
 */
slouken@1895
   677
SDL_Surface *
slouken@1895
   678
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   679
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   680
{
slouken@1895
   681
    SDL_Surface *convert;
slouken@1895
   682
    Uint32 colorkey = 0;
slouken@1895
   683
    Uint8 alpha = 0;
slouken@1895
   684
    Uint32 surface_flags;
slouken@1895
   685
    SDL_Rect bounds;
slouken@0
   686
slouken@1895
   687
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   688
    if (format->palette != NULL) {
slouken@1895
   689
        int i;
slouken@1895
   690
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   691
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   692
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   693
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   694
                break;
slouken@1895
   695
        }
slouken@1895
   696
        if (i == format->palette->ncolors) {
slouken@1895
   697
            SDL_SetError("Empty destination palette");
slouken@1895
   698
            return (NULL);
slouken@1895
   699
        }
slouken@1895
   700
    }
slouken@0
   701
slouken@1895
   702
    /* Create a new surface with the desired format */
slouken@1895
   703
    convert = SDL_CreateRGBSurface(flags,
slouken@1895
   704
                                   surface->w, surface->h,
slouken@1895
   705
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   706
                                   format->Gmask, format->Bmask,
slouken@1895
   707
                                   format->Amask);
slouken@1895
   708
    if (convert == NULL) {
slouken@1895
   709
        return (NULL);
slouken@1895
   710
    }
slouken@264
   711
slouken@1895
   712
    /* Copy the palette if any */
slouken@1895
   713
    if (format->palette && convert->format->palette) {
slouken@1895
   714
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   715
                   format->palette->colors,
slouken@1895
   716
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   717
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   718
    }
slouken@0
   719
slouken@1895
   720
    /* Save the original surface color key and alpha */
slouken@1895
   721
    surface_flags = surface->flags;
slouken@1895
   722
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   723
        /* Convert colourkeyed surfaces to RGBA if requested */
slouken@1895
   724
        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
slouken@1895
   725
            surface_flags &= ~SDL_SRCCOLORKEY;
slouken@1895
   726
        } else {
slouken@1895
   727
            colorkey = surface->format->colorkey;
slouken@1895
   728
            SDL_SetColorKey(surface, 0, 0);
slouken@1895
   729
        }
slouken@1895
   730
    }
slouken@1895
   731
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   732
        /* Copy over the alpha channel to RGBA if requested */
slouken@1895
   733
        if (format->Amask) {
slouken@1895
   734
            surface->flags &= ~SDL_SRCALPHA;
slouken@1895
   735
        } else {
slouken@1895
   736
            alpha = surface->format->alpha;
slouken@1895
   737
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   738
        }
slouken@1895
   739
    }
slouken@0
   740
slouken@1895
   741
    /* Copy over the image data */
slouken@1895
   742
    bounds.x = 0;
slouken@1895
   743
    bounds.y = 0;
slouken@1895
   744
    bounds.w = surface->w;
slouken@1895
   745
    bounds.h = surface->h;
slouken@1895
   746
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   747
slouken@1895
   748
    /* Clean up the original surface, and update converted surface */
slouken@1895
   749
    if (convert != NULL) {
slouken@1895
   750
        SDL_SetClipRect(convert, &surface->clip_rect);
slouken@1895
   751
    }
slouken@1895
   752
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   753
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   754
        if (convert != NULL) {
slouken@1895
   755
            Uint8 keyR, keyG, keyB;
slouken@0
   756
slouken@1895
   757
            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
slouken@1895
   758
            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
slouken@1895
   759
                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@1895
   760
        }
slouken@1895
   761
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1895
   762
    }
slouken@1895
   763
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   764
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   765
        if (convert != NULL) {
slouken@1895
   766
            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
slouken@1895
   767
        }
slouken@1895
   768
        if (format->Amask) {
slouken@1895
   769
            surface->flags |= SDL_SRCALPHA;
slouken@1895
   770
        } else {
slouken@1895
   771
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1895
   772
        }
slouken@1895
   773
    }
slouken@0
   774
slouken@1895
   775
    /* We're ready to go! */
slouken@1895
   776
    return (convert);
slouken@0
   777
}
slouken@0
   778
slouken@0
   779
/*
slouken@0
   780
 * Free a surface created by the above function.
slouken@0
   781
 */
slouken@1895
   782
void
slouken@1895
   783
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   784
{
slouken@1895
   785
    if (surface == NULL) {
slouken@1895
   786
        return;
slouken@1895
   787
    }
slouken@1895
   788
    if (--surface->refcount > 0) {
slouken@1895
   789
        return;
slouken@1895
   790
    }
slouken@1895
   791
    while (surface->locked > 0) {
slouken@1895
   792
        SDL_UnlockSurface(surface);
slouken@1895
   793
    }
slouken@1895
   794
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   795
        SDL_UnRLESurface(surface, 0);
slouken@1895
   796
    }
slouken@1895
   797
    if (surface->format) {
slouken@1895
   798
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   799
        SDL_FreeFormat(surface->format);
slouken@1895
   800
        surface->format = NULL;
slouken@1895
   801
    }
slouken@1895
   802
    if (surface->map != NULL) {
slouken@1895
   803
        SDL_FreeBlitMap(surface->map);
slouken@1895
   804
        surface->map = NULL;
slouken@1895
   805
    }
slouken@1895
   806
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
   807
        SDL_free(surface->pixels);
slouken@1895
   808
    }
slouken@1895
   809
    SDL_free(surface);
slouken@0
   810
#ifdef CHECK_LEAKS
slouken@1895
   811
    --surfaces_allocated;
slouken@0
   812
#endif
slouken@0
   813
}
slouken@1895
   814
slouken@1895
   815
/* vi: set ts=4 sw=4 expandtab: */