src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 16 Aug 2014 23:25:02 -0700
changeset 9076 5e713281410c
parent 9046 c3ec7c3e6c24
child 9080 2e4e71ec140f
permissions -rw-r--r--
Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly

Patch from Benoit Pierre:

video: fix clipping handling in SDL_UpperBlitScaled

- honor destination clipping rectangle
- update both destination and source rectangles when clipping source
rectangle to source surface and destination rectangle to destination
clip rectangle
- don't change scaling factors when clipping

N.B.:

- when no scaling is involved (source and destination width/height are
the same), SDL_UpperBlit is used (so SDL_BlitScaled behaves like
SDL_BlitSurface)
- the final destination rectangle after all clipping is performed is
saved back to dstrect (like for SDL_UpperBlit)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
#include "SDL_video.h"
slouken@0
    24
#include "SDL_sysvideo.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
#include "SDL_RLEaccel_c.h"
slouken@0
    27
#include "SDL_pixels_c.h"
slouken@0
    28
slouken@0
    29
/* Public routines */
slouken@0
    30
/*
slouken@0
    31
 * Create an empty RGB surface of the appropriate depth
slouken@0
    32
 */
slouken@1895
    33
SDL_Surface *
slouken@1895
    34
SDL_CreateRGBSurface(Uint32 flags,
slouken@1895
    35
                     int width, int height, int depth,
slouken@1895
    36
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    37
{
slouken@1895
    38
    SDL_Surface *surface;
slouken@5288
    39
    Uint32 format;
slouken@0
    40
slouken@2807
    41
    /* The flags are no longer used, make the compiler happy */
slouken@4461
    42
    (void)flags;
slouken@2807
    43
slouken@5288
    44
    /* Get the pixel format */
slouken@5288
    45
    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
slouken@5288
    46
    if (format == SDL_PIXELFORMAT_UNKNOWN) {
slouken@5288
    47
        SDL_SetError("Unknown pixel format");
slouken@5288
    48
        return NULL;
slouken@5288
    49
    }
slouken@5288
    50
slouken@1895
    51
    /* Allocate the surface */
slouken@1920
    52
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
slouken@1895
    53
    if (surface == NULL) {
slouken@1895
    54
        SDL_OutOfMemory();
slouken@1895
    55
        return NULL;
slouken@1895
    56
    }
slouken@940
    57
slouken@5288
    58
    surface->format = SDL_AllocFormat(format);
slouken@1895
    59
    if (!surface->format) {
slouken@1895
    60
        SDL_FreeSurface(surface);
slouken@1895
    61
        return NULL;
slouken@1895
    62
    }
slouken@1895
    63
    surface->w = width;
slouken@1895
    64
    surface->h = height;
slouken@1895
    65
    surface->pitch = SDL_CalculatePitch(surface);
slouken@1895
    66
    SDL_SetClipRect(surface, NULL);
slouken@0
    67
slouken@5288
    68
    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
slouken@1895
    69
        SDL_Palette *palette =
slouken@1895
    70
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
slouken@1895
    71
        if (!palette) {
slouken@1895
    72
            SDL_FreeSurface(surface);
slouken@1895
    73
            return NULL;
slouken@1895
    74
        }
slouken@5438
    75
        if (palette->ncolors == 2) {
slouken@1895
    76
            /* Create a black and white bitmap palette */
slouken@1895
    77
            palette->colors[0].r = 0xFF;
slouken@1895
    78
            palette->colors[0].g = 0xFF;
slouken@1895
    79
            palette->colors[0].b = 0xFF;
slouken@1895
    80
            palette->colors[1].r = 0x00;
slouken@1895
    81
            palette->colors[1].g = 0x00;
slouken@1895
    82
            palette->colors[1].b = 0x00;
slouken@1895
    83
        }
slouken@1895
    84
        SDL_SetSurfacePalette(surface, palette);
slouken@1895
    85
        SDL_FreePalette(palette);
slouken@1895
    86
    }
slouken@1895
    87
slouken@1895
    88
    /* Get the pixels */
slouken@1895
    89
    if (surface->w && surface->h) {
slouken@1895
    90
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
    91
        if (!surface->pixels) {
slouken@1895
    92
            SDL_FreeSurface(surface);
slouken@1895
    93
            SDL_OutOfMemory();
slouken@1895
    94
            return NULL;
slouken@1895
    95
        }
slouken@1895
    96
        /* This is important for bitmaps */
slouken@1895
    97
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1895
    98
    }
slouken@1895
    99
slouken@1895
   100
    /* Allocate an empty mapping */
slouken@1895
   101
    surface->map = SDL_AllocBlitMap();
slouken@1895
   102
    if (!surface->map) {
slouken@1895
   103
        SDL_FreeSurface(surface);
slouken@1895
   104
        return NULL;
slouken@1895
   105
    }
slouken@1895
   106
slouken@2267
   107
    /* By default surface with an alpha mask are set up for blending */
slouken@2267
   108
    if (Amask) {
slouken@2884
   109
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2267
   110
    }
slouken@2267
   111
slouken@1895
   112
    /* The surface is ready to go */
slouken@1895
   113
    surface->refcount = 1;
slouken@1895
   114
    return surface;
slouken@0
   115
}
slouken@1895
   116
slouken@0
   117
/*
slouken@0
   118
 * Create an RGB surface from an existing memory buffer
slouken@0
   119
 */
slouken@1895
   120
SDL_Surface *
slouken@1895
   121
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   122
                         int width, int height, int depth, int pitch,
slouken@1895
   123
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   124
                         Uint32 Amask)
slouken@0
   125
{
slouken@1895
   126
    SDL_Surface *surface;
slouken@0
   127
slouken@1895
   128
    surface =
slouken@1895
   129
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
   130
    if (surface != NULL) {
slouken@1895
   131
        surface->flags |= SDL_PREALLOC;
slouken@1895
   132
        surface->pixels = pixels;
slouken@1895
   133
        surface->w = width;
slouken@1895
   134
        surface->h = height;
slouken@1895
   135
        surface->pitch = pitch;
slouken@1895
   136
        SDL_SetClipRect(surface, NULL);
slouken@1895
   137
    }
slouken@1895
   138
    return surface;
slouken@0
   139
}
slouken@1895
   140
slouken@1895
   141
int
slouken@1895
   142
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   143
{
slouken@5288
   144
    if (!surface) {
icculus@7037
   145
        return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   146
    }
slouken@8990
   147
    if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
slouken@8990
   148
        return -1;
slouken@8990
   149
    }
slouken@8990
   150
    SDL_InvalidateMap(surface->map);
slouken@8990
   151
slouken@8990
   152
    return 0;
slouken@1895
   153
}
slouken@1895
   154
slouken@2267
   155
int
slouken@2267
   156
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
slouken@0
   157
{
slouken@2267
   158
    int flags;
slouken@0
   159
slouken@2266
   160
    if (!surface) {
slouken@2266
   161
        return -1;
slouken@1895
   162
    }
slouken@0
   163
slouken@2267
   164
    flags = surface->map->info.flags;
slouken@1895
   165
    if (flag) {
slouken@2267
   166
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@1895
   167
    } else {
slouken@2267
   168
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@1895
   169
    }
slouken@2267
   170
    if (surface->map->info.flags != flags) {
slouken@1895
   171
        SDL_InvalidateMap(surface->map);
slouken@1895
   172
    }
slouken@1895
   173
    return 0;
slouken@431
   174
}
slouken@0
   175
slouken@2267
   176
int
slouken@3560
   177
SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
slouken@2266
   178
{
slouken@2266
   179
    int flags;
slouken@2266
   180
slouken@2266
   181
    if (!surface) {
slouken@7363
   182
        return SDL_InvalidParamError("surface");
slouken@7363
   183
    }
slouken@7363
   184
icculus@7482
   185
    if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
slouken@7363
   186
        return SDL_InvalidParamError("key");
slouken@2266
   187
    }
slouken@2266
   188
slouken@2266
   189
    if (flag & SDL_RLEACCEL) {
slouken@2266
   190
        SDL_SetSurfaceRLE(surface, 1);
slouken@2266
   191
    }
slouken@2266
   192
slouken@2266
   193
    flags = surface->map->info.flags;
slouken@2266
   194
    if (flag) {
slouken@2266
   195
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   196
        surface->map->info.colorkey = key;
slouken@7023
   197
        if (surface->format->palette) {
slouken@7024
   198
            surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
slouken@7025
   199
            ++surface->format->palette->version;
slouken@7025
   200
            if (!surface->format->palette->version) {
slouken@7025
   201
                surface->format->palette->version = 1;
slouken@7025
   202
            }
slouken@7023
   203
        }
slouken@2266
   204
    } else {
slouken@7023
   205
        if (surface->format->palette) {
slouken@7024
   206
            surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
slouken@7025
   207
            ++surface->format->palette->version;
slouken@7025
   208
            if (!surface->format->palette->version) {
slouken@7025
   209
                surface->format->palette->version = 1;
slouken@7025
   210
            }
slouken@7023
   211
        }
slouken@2266
   212
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   213
    }
slouken@2266
   214
    if (surface->map->info.flags != flags) {
slouken@2266
   215
        SDL_InvalidateMap(surface->map);
slouken@2266
   216
    }
slouken@2267
   217
slouken@2266
   218
    return 0;
slouken@2266
   219
}
slouken@2266
   220
slouken@3103
   221
int
slouken@3103
   222
SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
slouken@3103
   223
{
slouken@3103
   224
    if (!surface) {
slouken@3103
   225
        return -1;
slouken@3103
   226
    }
slouken@3103
   227
slouken@3103
   228
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@3103
   229
        return -1;
slouken@3103
   230
    }
slouken@3103
   231
slouken@3103
   232
    if (key) {
slouken@3103
   233
        *key = surface->map->info.colorkey;
slouken@3103
   234
    }
slouken@3103
   235
    return 0;
slouken@3103
   236
}
slouken@3103
   237
slouken@2785
   238
/* This is a fairly slow function to switch from colorkey to alpha */
slouken@2787
   239
static void
slouken@2786
   240
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
slouken@2785
   241
{
slouken@2786
   242
    int x, y;
slouken@2785
   243
slouken@2786
   244
    if (!surface) {
slouken@2786
   245
        return;
slouken@2786
   246
    }
slouken@2785
   247
slouken@2786
   248
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
slouken@2786
   249
        !surface->format->Amask) {
slouken@2786
   250
        return;
slouken@2786
   251
    }
slouken@2785
   252
slouken@2786
   253
    SDL_LockSurface(surface);
slouken@2785
   254
slouken@2786
   255
    switch (surface->format->BytesPerPixel) {
slouken@2786
   256
    case 2:
slouken@2786
   257
        {
slouken@2786
   258
            Uint16 *row, *spot;
slouken@2786
   259
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
slouken@2786
   260
            Uint16 mask = (Uint16) (~surface->format->Amask);
slouken@2785
   261
slouken@7601
   262
            /* Ignore alpha in colorkey comparison */
slouken@7601
   263
            ckey &= mask;
slouken@2786
   264
            row = (Uint16 *) surface->pixels;
slouken@2786
   265
            for (y = surface->h; y--;) {
slouken@2786
   266
                spot = row;
slouken@2786
   267
                for (x = surface->w; x--;) {
slouken@7601
   268
                    if ((*spot & mask) == ckey) {
slouken@2786
   269
                        *spot &= mask;
slouken@2786
   270
                    }
slouken@2786
   271
                    ++spot;
slouken@2786
   272
                }
slouken@2786
   273
                row += surface->pitch / 2;
slouken@2786
   274
            }
slouken@2786
   275
        }
slouken@2786
   276
        break;
slouken@2786
   277
    case 3:
slouken@2786
   278
        /* FIXME */
slouken@2786
   279
        break;
slouken@2786
   280
    case 4:
slouken@2786
   281
        {
slouken@2786
   282
            Uint32 *row, *spot;
slouken@2786
   283
            Uint32 ckey = surface->map->info.colorkey;
slouken@2786
   284
            Uint32 mask = ~surface->format->Amask;
slouken@2785
   285
slouken@7601
   286
            /* Ignore alpha in colorkey comparison */
slouken@7601
   287
            ckey &= mask;
slouken@2786
   288
            row = (Uint32 *) surface->pixels;
slouken@2786
   289
            for (y = surface->h; y--;) {
slouken@2786
   290
                spot = row;
slouken@2786
   291
                for (x = surface->w; x--;) {
slouken@7601
   292
                    if ((*spot & mask) == ckey) {
slouken@2786
   293
                        *spot &= mask;
slouken@2786
   294
                    }
slouken@2786
   295
                    ++spot;
slouken@2786
   296
                }
slouken@2786
   297
                row += surface->pitch / 4;
slouken@2786
   298
            }
slouken@2786
   299
        }
slouken@2786
   300
        break;
slouken@2786
   301
    }
slouken@2785
   302
slouken@2786
   303
    SDL_UnlockSurface(surface);
slouken@2785
   304
slouken@2786
   305
    SDL_SetColorKey(surface, 0, 0);
slouken@2884
   306
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2785
   307
}
slouken@2785
   308
slouken@2267
   309
int
slouken@2267
   310
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   311
{
slouken@2266
   312
    int flags;
slouken@2266
   313
slouken@2266
   314
    if (!surface) {
slouken@2266
   315
        return -1;
slouken@2266
   316
    }
slouken@2266
   317
slouken@2266
   318
    surface->map->info.r = r;
slouken@2266
   319
    surface->map->info.g = g;
slouken@2266
   320
    surface->map->info.b = b;
slouken@2266
   321
slouken@2266
   322
    flags = surface->map->info.flags;
slouken@2266
   323
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   324
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@2266
   325
    } else {
slouken@2266
   326
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@2266
   327
    }
slouken@2266
   328
    if (surface->map->info.flags != flags) {
slouken@2266
   329
        SDL_InvalidateMap(surface->map);
slouken@2266
   330
    }
slouken@2266
   331
    return 0;
slouken@2266
   332
}
slouken@2266
   333
slouken@2266
   334
slouken@2267
   335
int
slouken@2267
   336
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@2266
   337
{
slouken@2266
   338
    if (!surface) {
slouken@2266
   339
        return -1;
slouken@2266
   340
    }
slouken@2266
   341
slouken@2266
   342
    if (r) {
slouken@2266
   343
        *r = surface->map->info.r;
slouken@2266
   344
    }
slouken@2266
   345
    if (g) {
slouken@2266
   346
        *g = surface->map->info.g;
slouken@2266
   347
    }
slouken@2266
   348
    if (b) {
slouken@2266
   349
        *b = surface->map->info.b;
slouken@2266
   350
    }
slouken@2266
   351
    return 0;
slouken@2266
   352
}
slouken@2266
   353
slouken@2267
   354
int
slouken@2267
   355
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
slouken@2266
   356
{
slouken@2266
   357
    int flags;
slouken@2266
   358
slouken@2266
   359
    if (!surface) {
slouken@2266
   360
        return -1;
slouken@2266
   361
    }
slouken@2266
   362
slouken@2266
   363
    surface->map->info.a = alpha;
slouken@2266
   364
slouken@2266
   365
    flags = surface->map->info.flags;
slouken@2266
   366
    if (alpha != 0xFF) {
slouken@2266
   367
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   368
    } else {
slouken@2266
   369
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   370
    }
slouken@2266
   371
    if (surface->map->info.flags != flags) {
slouken@2266
   372
        SDL_InvalidateMap(surface->map);
slouken@2266
   373
    }
slouken@2266
   374
    return 0;
slouken@2266
   375
}
slouken@2266
   376
slouken@2267
   377
int
slouken@2267
   378
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
slouken@2266
   379
{
slouken@2266
   380
    if (!surface) {
slouken@2266
   381
        return -1;
slouken@2266
   382
    }
slouken@2266
   383
slouken@2266
   384
    if (alpha) {
slouken@2266
   385
        *alpha = surface->map->info.a;
slouken@2266
   386
    }
slouken@2266
   387
    return 0;
slouken@2266
   388
}
slouken@2266
   389
slouken@2267
   390
int
slouken@4929
   391
SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
slouken@2266
   392
{
slouken@2266
   393
    int flags, status;
slouken@2266
   394
slouken@2266
   395
    if (!surface) {
slouken@2266
   396
        return -1;
slouken@2266
   397
    }
slouken@2266
   398
slouken@2266
   399
    status = 0;
slouken@2266
   400
    flags = surface->map->info.flags;
slouken@5184
   401
    surface->map->info.flags &=
slouken@5184
   402
        ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
slouken@2266
   403
    switch (blendMode) {
slouken@2884
   404
    case SDL_BLENDMODE_NONE:
slouken@2266
   405
        break;
slouken@2884
   406
    case SDL_BLENDMODE_BLEND:
slouken@2266
   407
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   408
        break;
slouken@2884
   409
    case SDL_BLENDMODE_ADD:
slouken@2266
   410
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   411
        break;
slouken@5184
   412
    case SDL_BLENDMODE_MOD:
slouken@5184
   413
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@5184
   414
        break;
slouken@2266
   415
    default:
icculus@7037
   416
        status = SDL_Unsupported();
slouken@2266
   417
        break;
slouken@2266
   418
    }
slouken@2266
   419
slouken@2266
   420
    if (surface->map->info.flags != flags) {
slouken@2266
   421
        SDL_InvalidateMap(surface->map);
slouken@2266
   422
    }
slouken@2267
   423
slouken@2266
   424
    return status;
slouken@2266
   425
}
slouken@2266
   426
slouken@2267
   427
int
slouken@4929
   428
SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
slouken@2266
   429
{
slouken@2266
   430
    if (!surface) {
slouken@2266
   431
        return -1;
slouken@2266
   432
    }
slouken@2266
   433
slouken@2266
   434
    if (!blendMode) {
slouken@2266
   435
        return 0;
slouken@2266
   436
    }
slouken@2266
   437
slouken@5184
   438
    switch (surface->map->
slouken@5184
   439
            info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
slouken@2266
   440
    case SDL_COPY_BLEND:
slouken@2884
   441
        *blendMode = SDL_BLENDMODE_BLEND;
slouken@2266
   442
        break;
slouken@2266
   443
    case SDL_COPY_ADD:
slouken@2884
   444
        *blendMode = SDL_BLENDMODE_ADD;
slouken@2266
   445
        break;
slouken@5184
   446
    case SDL_COPY_MOD:
slouken@5184
   447
        *blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   448
        break;
slouken@2266
   449
    default:
slouken@2884
   450
        *blendMode = SDL_BLENDMODE_NONE;
slouken@2266
   451
        break;
slouken@2266
   452
    }
slouken@2266
   453
    return 0;
slouken@2266
   454
}
slouken@2266
   455
slouken@1895
   456
SDL_bool
slouken@1895
   457
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   458
{
slouken@1895
   459
    SDL_Rect full_rect;
slouken@0
   460
slouken@1895
   461
    /* Don't do anything if there's no surface to act on */
slouken@1895
   462
    if (!surface) {
slouken@1895
   463
        return SDL_FALSE;
slouken@1895
   464
    }
slouken@0
   465
slouken@1895
   466
    /* Set up the full surface rectangle */
slouken@1895
   467
    full_rect.x = 0;
slouken@1895
   468
    full_rect.y = 0;
slouken@1895
   469
    full_rect.w = surface->w;
slouken@1895
   470
    full_rect.h = surface->h;
slouken@0
   471
slouken@1895
   472
    /* Set the clipping rectangle */
slouken@1895
   473
    if (!rect) {
slouken@1895
   474
        surface->clip_rect = full_rect;
slouken@4966
   475
        return SDL_TRUE;
slouken@1895
   476
    }
slouken@1895
   477
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   478
}
slouken@1895
   479
slouken@1895
   480
void
slouken@1895
   481
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   482
{
slouken@1895
   483
    if (surface && rect) {
slouken@1895
   484
        *rect = surface->clip_rect;
slouken@1895
   485
    }
slouken@0
   486
}
slouken@1895
   487
slouken@7191
   488
/*
slouken@0
   489
 * Set up a blit between two surfaces -- split into three parts:
slouken@7191
   490
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle
slouken@0
   491
 * verification.  The lower part is a pointer to a low level
slouken@0
   492
 * accelerated blitting function.
slouken@0
   493
 *
slouken@7191
   494
 * These parts are separated out and each used internally by this
slouken@0
   495
 * library in the optimimum places.  They are exported so that if
slouken@0
   496
 * you know exactly what you are doing, you can optimize your code
slouken@0
   497
 * by calling the one(s) you need.
slouken@0
   498
 */
slouken@1895
   499
int
slouken@1895
   500
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   501
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   502
{
slouken@1895
   503
    /* Check to make sure the blit mapping is valid */
slouken@1895
   504
    if ((src->map->dst != dst) ||
slouken@5288
   505
        (dst->format->palette &&
slouken@6166
   506
         src->map->dst_palette_version != dst->format->palette->version) ||
slouken@6166
   507
        (src->format->palette &&
slouken@6166
   508
         src->map->src_palette_version != src->format->palette->version)) {
slouken@1895
   509
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   510
            return (-1);
slouken@1895
   511
        }
bob@2328
   512
        /* just here for debugging */
bob@2329
   513
/*         printf */
bob@2329
   514
/*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
bob@2329
   515
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
bob@2329
   516
/*              dst->map->info.flags, src->map->blit); */
slouken@1895
   517
    }
slouken@2257
   518
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   519
}
slouken@0
   520
slouken@0
   521
slouken@1895
   522
int
slouken@4949
   523
SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
slouken@1895
   524
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   525
{
slouken@1895
   526
    SDL_Rect fulldst;
slouken@1895
   527
    int srcx, srcy, w, h;
slouken@0
   528
slouken@1895
   529
    /* Make sure the surfaces aren't locked */
slouken@1895
   530
    if (!src || !dst) {
icculus@7037
   531
        return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   532
    }
slouken@1895
   533
    if (src->locked || dst->locked) {
icculus@7037
   534
        return SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   535
    }
slouken@0
   536
slouken@1895
   537
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   538
    if (dstrect == NULL) {
slouken@1895
   539
        fulldst.x = fulldst.y = 0;
slouken@7511
   540
        fulldst.w = dst->w;
slouken@7511
   541
        fulldst.h = dst->h;
slouken@1895
   542
        dstrect = &fulldst;
slouken@1895
   543
    }
slouken@0
   544
slouken@1895
   545
    /* clip the source rectangle to the source surface */
slouken@1895
   546
    if (srcrect) {
slouken@1895
   547
        int maxw, maxh;
slouken@0
   548
slouken@1895
   549
        srcx = srcrect->x;
slouken@1895
   550
        w = srcrect->w;
slouken@1895
   551
        if (srcx < 0) {
slouken@1895
   552
            w += srcx;
slouken@1895
   553
            dstrect->x -= srcx;
slouken@1895
   554
            srcx = 0;
slouken@1895
   555
        }
slouken@1895
   556
        maxw = src->w - srcx;
slouken@1895
   557
        if (maxw < w)
slouken@1895
   558
            w = maxw;
slouken@0
   559
slouken@1895
   560
        srcy = srcrect->y;
slouken@1895
   561
        h = srcrect->h;
slouken@1895
   562
        if (srcy < 0) {
slouken@1895
   563
            h += srcy;
slouken@1895
   564
            dstrect->y -= srcy;
slouken@1895
   565
            srcy = 0;
slouken@1895
   566
        }
slouken@1895
   567
        maxh = src->h - srcy;
slouken@1895
   568
        if (maxh < h)
slouken@1895
   569
            h = maxh;
slouken@0
   570
slouken@1895
   571
    } else {
slouken@1895
   572
        srcx = srcy = 0;
slouken@1895
   573
        w = src->w;
slouken@1895
   574
        h = src->h;
slouken@1895
   575
    }
slouken@0
   576
slouken@1895
   577
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   578
    {
slouken@1895
   579
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   580
        int dx, dy;
slouken@0
   581
slouken@1895
   582
        dx = clip->x - dstrect->x;
slouken@1895
   583
        if (dx > 0) {
slouken@1895
   584
            w -= dx;
slouken@1895
   585
            dstrect->x += dx;
slouken@1895
   586
            srcx += dx;
slouken@1895
   587
        }
slouken@1895
   588
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   589
        if (dx > 0)
slouken@1895
   590
            w -= dx;
slouken@1895
   591
slouken@1895
   592
        dy = clip->y - dstrect->y;
slouken@1895
   593
        if (dy > 0) {
slouken@1895
   594
            h -= dy;
slouken@1895
   595
            dstrect->y += dy;
slouken@1895
   596
            srcy += dy;
slouken@1895
   597
        }
slouken@1895
   598
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   599
        if (dy > 0)
slouken@1895
   600
            h -= dy;
slouken@1895
   601
    }
slouken@1895
   602
slouken@7834
   603
    /* Switch back to a fast blit if we were previously stretching */
slouken@7834
   604
    if (src->map->info.flags & SDL_COPY_NEAREST) {
slouken@7834
   605
        src->map->info.flags &= ~SDL_COPY_NEAREST;
slouken@7834
   606
        SDL_InvalidateMap(src->map);
slouken@7834
   607
    }
slouken@7834
   608
slouken@1895
   609
    if (w > 0 && h > 0) {
slouken@1895
   610
        SDL_Rect sr;
slouken@1895
   611
        sr.x = srcx;
slouken@1895
   612
        sr.y = srcy;
slouken@1895
   613
        sr.w = dstrect->w = w;
slouken@1895
   614
        sr.h = dstrect->h = h;
slouken@1895
   615
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   616
    }
slouken@1895
   617
    dstrect->w = dstrect->h = 0;
slouken@1895
   618
    return 0;
slouken@0
   619
}
slouken@0
   620
ken@5296
   621
int
ken@5499
   622
SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
ken@5499
   623
              SDL_Surface * dst, SDL_Rect * dstrect)
ken@5499
   624
{
slouken@9076
   625
    double src_x0, src_y0, src_x1, src_y1;
slouken@9076
   626
    double dst_x0, dst_y0, dst_x1, dst_y1;
slouken@9076
   627
    SDL_Rect final_src, final_dst;
slouken@9076
   628
    double scaling_w, scaling_h;
slouken@9076
   629
    int src_w, src_h;
slouken@9076
   630
    int dst_w, dst_h;
ken@5499
   631
ken@5499
   632
    /* Make sure the surfaces aren't locked */
ken@5499
   633
    if (!src || !dst) {
icculus@7037
   634
        return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
ken@5499
   635
    }
ken@5499
   636
    if (src->locked || dst->locked) {
icculus@7037
   637
        return SDL_SetError("Surfaces must not be locked during blit");
ken@5499
   638
    }
ken@5499
   639
slouken@9076
   640
    if (NULL == srcrect) {
slouken@9076
   641
        src_w = src->w;
slouken@9076
   642
        src_h = src->h;
slouken@9076
   643
    } else {
slouken@9076
   644
        src_w = srcrect->w;
slouken@9076
   645
        src_h = srcrect->h;
ken@5499
   646
    }
ken@5499
   647
slouken@9076
   648
    if (NULL == dstrect) {
slouken@9076
   649
        dst_w = dst->w;
slouken@9076
   650
        dst_h = dst->h;
ken@5499
   651
    } else {
slouken@9076
   652
        dst_w = dstrect->w;
slouken@9076
   653
        dst_h = dstrect->h;
ken@5499
   654
    }
ken@5499
   655
slouken@9076
   656
    if (dst_w == src_w && dst_h == src_h) {
slouken@9076
   657
        /* No scaling, defer to regular blit */
slouken@9076
   658
        return SDL_BlitSurface(src, srcrect, dst, dstrect);
ken@5499
   659
    }
ken@5499
   660
slouken@9076
   661
    scaling_w = (double)dst_w / src_w;
slouken@9076
   662
    scaling_h = (double)dst_h / src_h;
slouken@9076
   663
slouken@9076
   664
    if (NULL == dstrect) {
slouken@9076
   665
        dst_x0 = 0;
slouken@9076
   666
        dst_y0 = 0;
slouken@9076
   667
        dst_x1 = dst_w - 1;
slouken@9076
   668
        dst_y1 = dst_h - 1;
slouken@9076
   669
    } else {
slouken@9076
   670
        dst_x0 = dstrect->x;
slouken@9076
   671
        dst_y0 = dstrect->y;
slouken@9076
   672
        dst_x1 = dst_x0 + dst_w - 1;
slouken@9076
   673
        dst_y1 = dst_y0 + dst_h - 1;
ken@5499
   674
    }
ken@5499
   675
slouken@9076
   676
    if (NULL == srcrect) {
slouken@9076
   677
        src_x0 = 0;
slouken@9076
   678
        src_y0 = 0;
slouken@9076
   679
        src_x1 = src_w - 1;
slouken@9076
   680
        src_y1 = src_h - 1;
slouken@9076
   681
    } else {
slouken@9076
   682
        src_x0 = srcrect->x;
slouken@9076
   683
        src_y0 = srcrect->y;
slouken@9076
   684
        src_x1 = src_x0 + src_w - 1;
slouken@9076
   685
        src_y1 = src_y0 + src_h - 1;
slouken@9076
   686
slouken@9076
   687
        /* Clip source rectangle to the source surface */
slouken@9076
   688
slouken@9076
   689
        if (src_x0 < 0) {
slouken@9076
   690
            dst_x0 -= src_x0 * scaling_w;
slouken@9076
   691
            src_x0 = 0;
slouken@9076
   692
        }
slouken@9076
   693
slouken@9076
   694
        if (src_x1 >= src->w) {
slouken@9076
   695
            dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
slouken@9076
   696
            src_x1 = src->w - 1;
slouken@9076
   697
        }
slouken@9076
   698
slouken@9076
   699
        if (src_y0 < 0) {
slouken@9076
   700
            dst_y0 -= src_y0 * scaling_h;
slouken@9076
   701
            src_y0 = 0;
slouken@9076
   702
        }
slouken@9076
   703
slouken@9076
   704
        if (src_y1 >= src->h) {
slouken@9076
   705
            dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
slouken@9076
   706
            src_y1 = src->h - 1;
slouken@9076
   707
        }
slouken@9076
   708
    }
slouken@9076
   709
slouken@9076
   710
    /* Clip destination rectangle to the clip rectangle */
slouken@9076
   711
slouken@9076
   712
    /* Translate to clip space for easier calculations */
slouken@9076
   713
    dst_x0 -= dst->clip_rect.x;
slouken@9076
   714
    dst_x1 -= dst->clip_rect.x;
slouken@9076
   715
    dst_y0 -= dst->clip_rect.y;
slouken@9076
   716
    dst_y1 -= dst->clip_rect.y;
slouken@9076
   717
slouken@9076
   718
    if (dst_x0 < 0) {
slouken@9076
   719
        src_x0 -= dst_x0 / scaling_w;
slouken@9076
   720
        dst_x0 = 0;
slouken@9076
   721
    }
slouken@9076
   722
slouken@9076
   723
    if (dst_x1 >= dst->clip_rect.w) {
slouken@9076
   724
        src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
slouken@9076
   725
        dst_x1 = dst->clip_rect.w - 1;
slouken@9076
   726
    }
slouken@9076
   727
slouken@9076
   728
    if (dst_y0 < 0) {
slouken@9076
   729
        src_y0 -= dst_y0 / scaling_h;
slouken@9076
   730
        dst_y0 = 0;
slouken@9076
   731
    }
slouken@9076
   732
slouken@9076
   733
    if (dst_y1 >= dst->clip_rect.h) {
slouken@9076
   734
        src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
slouken@9076
   735
        dst_y1 = dst->clip_rect.h - 1;
slouken@9076
   736
    }
slouken@9076
   737
slouken@9076
   738
    /* Translate back to surface coordinates */
slouken@9076
   739
    dst_x0 += dst->clip_rect.x;
slouken@9076
   740
    dst_x1 += dst->clip_rect.x;
slouken@9076
   741
    dst_y0 += dst->clip_rect.y;
slouken@9076
   742
    dst_y1 += dst->clip_rect.y;
slouken@9076
   743
slouken@9076
   744
    final_src.x = SDL_round(src_x0);
slouken@9076
   745
    final_src.y = SDL_round(src_y0);
slouken@9076
   746
    final_src.w = SDL_round(src_x1 - src_x0 + 1);
slouken@9076
   747
    final_src.h = SDL_round(src_y1 - src_y0 + 1);
slouken@9076
   748
slouken@9076
   749
    final_dst.x = SDL_round(dst_x0);
slouken@9076
   750
    final_dst.y = SDL_round(dst_y0);
slouken@9076
   751
    final_dst.w = SDL_round(dst_x1 - dst_x0 + 1);
slouken@9076
   752
    final_dst.h = SDL_round(dst_y1 - dst_y0 + 1);
slouken@9076
   753
slouken@9076
   754
    if (final_dst.w < 0)
slouken@9076
   755
        final_dst.w = 0;
slouken@9076
   756
    if (final_dst.h < 0)
slouken@9076
   757
        final_dst.h = 0;
slouken@9076
   758
slouken@9076
   759
    if (dstrect)
slouken@9076
   760
        *dstrect = final_dst;
slouken@9076
   761
slouken@9076
   762
    if (final_dst.w == 0 || final_dst.h == 0 ||
slouken@9076
   763
        final_src.w <= 0 || final_src.h <= 0) {
slouken@9076
   764
        /* No-op. */
slouken@9076
   765
        return 0;
slouken@9076
   766
    }
slouken@9076
   767
slouken@9076
   768
    return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
ken@5499
   769
}
ken@5499
   770
ken@5499
   771
/**
ken@5499
   772
 *  This is a semi-private blit function and it performs low-level surface
ken@5499
   773
 *  scaled blitting only.
ken@5499
   774
 */
ken@5499
   775
int
ken@5499
   776
SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
ken@5499
   777
                SDL_Surface * dst, SDL_Rect * dstrect)
ken@5296
   778
{
slouken@6526
   779
    static const Uint32 complex_copy_flags = (
slouken@6526
   780
        SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
slouken@6526
   781
        SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD |
slouken@6526
   782
        SDL_COPY_COLORKEY
slouken@6526
   783
    );
slouken@6526
   784
slouken@7834
   785
    if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
slouken@7834
   786
        src->map->info.flags |= SDL_COPY_NEAREST;
slouken@7834
   787
        SDL_InvalidateMap(src->map);
slouken@7834
   788
    }
ken@5296
   789
slouken@6526
   790
    if ( !(src->map->info.flags & complex_copy_flags) &&
slouken@7191
   791
         src->format->format == dst->format->format &&
slouken@6526
   792
         !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
slouken@9076
   793
        return SDL_SoftStretch( src, srcrect, dst, dstrect );
ken@5296
   794
    } else {
slouken@9076
   795
        return SDL_LowerBlit( src, srcrect, dst, dstrect );
ken@5296
   796
    }
ken@5296
   797
}
ken@5296
   798
ken@5296
   799
/*
slouken@0
   800
 * Lock a surface to directly access the pixels
slouken@0
   801
 */
slouken@1895
   802
int
slouken@1895
   803
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   804
{
slouken@1895
   805
    if (!surface->locked) {
slouken@1895
   806
        /* Perform the lock */
slouken@1895
   807
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   808
            SDL_UnRLESurface(surface, 1);
slouken@1895
   809
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   810
        }
slouken@1895
   811
    }
slouken@0
   812
slouken@1895
   813
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   814
    ++surface->locked;
slouken@0
   815
slouken@1895
   816
    /* Ready to go.. */
slouken@1895
   817
    return (0);
slouken@0
   818
}
slouken@1895
   819
slouken@0
   820
/*
slouken@0
   821
 * Unlock a previously locked surface
slouken@0
   822
 */
slouken@1895
   823
void
slouken@1895
   824
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   825
{
slouken@1895
   826
    /* Only perform an unlock if we are locked */
slouken@1895
   827
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   828
        return;
slouken@1895
   829
    }
slouken@0
   830
slouken@1895
   831
    /* Update RLE encoded surface with new data */
slouken@1895
   832
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   833
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   834
        SDL_RLESurface(surface);
slouken@1895
   835
    }
slouken@0
   836
}
slouken@0
   837
slouken@7191
   838
/*
slouken@0
   839
 * Convert a surface into the specified pixel format.
slouken@0
   840
 */
slouken@1895
   841
SDL_Surface *
icculus@7725
   842
SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
slouken@2807
   843
                   Uint32 flags)
slouken@0
   844
{
slouken@1895
   845
    SDL_Surface *convert;
slouken@2266
   846
    Uint32 copy_flags;
slouken@7601
   847
    SDL_Color copy_color;
slouken@1895
   848
    SDL_Rect bounds;
slouken@0
   849
slouken@1895
   850
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   851
    if (format->palette != NULL) {
slouken@1895
   852
        int i;
slouken@1895
   853
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   854
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   855
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   856
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   857
                break;
slouken@1895
   858
        }
slouken@1895
   859
        if (i == format->palette->ncolors) {
slouken@1895
   860
            SDL_SetError("Empty destination palette");
slouken@1895
   861
            return (NULL);
slouken@1895
   862
        }
slouken@1895
   863
    }
slouken@0
   864
slouken@1895
   865
    /* Create a new surface with the desired format */
slouken@2807
   866
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
slouken@1895
   867
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   868
                                   format->Gmask, format->Bmask,
slouken@1895
   869
                                   format->Amask);
slouken@1895
   870
    if (convert == NULL) {
slouken@1895
   871
        return (NULL);
slouken@1895
   872
    }
slouken@264
   873
slouken@1895
   874
    /* Copy the palette if any */
slouken@1895
   875
    if (format->palette && convert->format->palette) {
slouken@1895
   876
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   877
                   format->palette->colors,
slouken@1895
   878
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   879
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   880
    }
slouken@0
   881
slouken@2266
   882
    /* Save the original copy flags */
slouken@2266
   883
    copy_flags = surface->map->info.flags;
slouken@7601
   884
    copy_color.r = surface->map->info.r;
slouken@7601
   885
    copy_color.g = surface->map->info.g;
slouken@7601
   886
    copy_color.b = surface->map->info.b;
slouken@7601
   887
    copy_color.a = surface->map->info.a;
slouken@7601
   888
    surface->map->info.r = 0xFF;
slouken@7601
   889
    surface->map->info.g = 0xFF;
slouken@7601
   890
    surface->map->info.b = 0xFF;
slouken@7601
   891
    surface->map->info.a = 0xFF;
slouken@2266
   892
    surface->map->info.flags = 0;
slouken@7601
   893
    SDL_InvalidateMap(surface->map);
slouken@0
   894
slouken@1895
   895
    /* Copy over the image data */
slouken@1895
   896
    bounds.x = 0;
slouken@1895
   897
    bounds.y = 0;
slouken@1895
   898
    bounds.w = surface->w;
slouken@1895
   899
    bounds.h = surface->h;
slouken@1895
   900
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   901
slouken@1895
   902
    /* Clean up the original surface, and update converted surface */
slouken@7601
   903
    convert->map->info.r = copy_color.r;
slouken@7601
   904
    convert->map->info.g = copy_color.g;
slouken@7601
   905
    convert->map->info.b = copy_color.b;
slouken@7601
   906
    convert->map->info.a = copy_color.a;
slouken@2824
   907
    convert->map->info.flags =
slouken@2824
   908
        (copy_flags &
slouken@2824
   909
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
slouken@2824
   910
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
slouken@2824
   911
           SDL_COPY_RLE_ALPHAKEY));
slouken@7601
   912
    surface->map->info.r = copy_color.r;
slouken@7601
   913
    surface->map->info.g = copy_color.g;
slouken@7601
   914
    surface->map->info.b = copy_color.b;
slouken@7601
   915
    surface->map->info.a = copy_color.a;
slouken@2824
   916
    surface->map->info.flags = copy_flags;
slouken@7601
   917
    SDL_InvalidateMap(surface->map);
slouken@2266
   918
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@7023
   919
        SDL_bool set_colorkey_by_color = SDL_FALSE;
slouken@2266
   920
slouken@7023
   921
        if (surface->format->palette) {
slouken@7191
   922
            if (format->palette &&
slouken@7023
   923
                surface->format->palette->ncolors <= format->palette->ncolors &&
slouken@7023
   924
                (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
slouken@7023
   925
                  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
slouken@7023
   926
                /* The palette is identical, just set the same colorkey */
slouken@7023
   927
                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
slouken@7023
   928
            } else if (format->Amask) {
slouken@7023
   929
                /* The alpha was set in the destination from the palette */
slouken@7023
   930
            } else {
slouken@7023
   931
                set_colorkey_by_color = SDL_TRUE;
slouken@7023
   932
            }
slouken@7023
   933
        } else {
slouken@7023
   934
            set_colorkey_by_color = SDL_TRUE;
slouken@7023
   935
        }
slouken@7023
   936
slouken@7023
   937
        if (set_colorkey_by_color) {
slouken@7023
   938
            /* Set the colorkey by color, which needs to be unique */
slouken@7023
   939
            Uint8 keyR, keyG, keyB, keyA;
slouken@7023
   940
slouken@7023
   941
            SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
slouken@7023
   942
                        &keyG, &keyB, &keyA);
slouken@7023
   943
            SDL_SetColorKey(convert, 1,
slouken@7023
   944
                            SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
slouken@7023
   945
            /* This is needed when converting for 3D texture upload */
slouken@7023
   946
            SDL_ConvertColorkeyToAlpha(convert);
slouken@7023
   947
        }
slouken@1895
   948
    }
slouken@2824
   949
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@0
   950
slouken@2266
   951
    /* Enable alpha blending by default if the new surface has an
slouken@2266
   952
     * alpha channel or alpha modulation */
slouken@2824
   953
    if ((surface->format->Amask && format->Amask) ||
slouken@4491
   954
        (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
slouken@2884
   955
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
slouken@1895
   956
    }
slouken@2824
   957
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
slouken@2824
   958
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
slouken@2824
   959
    }
slouken@0
   960
slouken@1895
   961
    /* We're ready to go! */
slouken@1895
   962
    return (convert);
slouken@0
   963
}
slouken@0
   964
slouken@5375
   965
SDL_Surface *
slouken@5375
   966
SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
slouken@5375
   967
                         Uint32 flags)
slouken@5375
   968
{
slouken@5375
   969
    SDL_PixelFormat *fmt;
slouken@5515
   970
    SDL_Surface *convert = NULL;
slouken@5375
   971
slouken@5375
   972
    fmt = SDL_AllocFormat(pixel_format);
slouken@5375
   973
    if (fmt) {
slouken@5375
   974
        convert = SDL_ConvertSurface(surface, fmt, flags);
slouken@5375
   975
        SDL_FreeFormat(fmt);
slouken@5375
   976
    }
slouken@5375
   977
    return convert;
slouken@5375
   978
}
slouken@5375
   979
slouken@0
   980
/*
slouken@3434
   981
 * Create a surface on the stack for quick blit operations
slouken@3434
   982
 */
slouken@7860
   983
static SDL_INLINE SDL_bool
slouken@3434
   984
SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
slouken@7191
   985
                         void * pixels, int pitch, SDL_Surface * surface,
slouken@3434
   986
                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
slouken@3434
   987
{
slouken@5288
   988
    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
   989
        SDL_SetError("Indexed pixel formats not supported");
slouken@3434
   990
        return SDL_FALSE;
slouken@3434
   991
    }
slouken@5288
   992
    if (SDL_InitFormat(format, pixel_format) < 0) {
slouken@3434
   993
        return SDL_FALSE;
slouken@3434
   994
    }
slouken@3434
   995
slouken@3434
   996
    SDL_zerop(surface);
slouken@3434
   997
    surface->flags = SDL_PREALLOC;
slouken@5288
   998
    surface->format = format;
slouken@3434
   999
    surface->pixels = pixels;
slouken@3434
  1000
    surface->w = width;
slouken@3434
  1001
    surface->h = height;
slouken@3434
  1002
    surface->pitch = pitch;
slouken@3434
  1003
    /* We don't actually need to set up the clip rect for our purposes */
gabomdq@7678
  1004
    /* SDL_SetClipRect(surface, NULL); */
slouken@3434
  1005
slouken@3434
  1006
    /* Allocate an empty mapping */
slouken@3434
  1007
    SDL_zerop(blitmap);
slouken@3434
  1008
    blitmap->info.r = 0xFF;
slouken@3434
  1009
    blitmap->info.g = 0xFF;
slouken@3434
  1010
    blitmap->info.b = 0xFF;
slouken@3434
  1011
    blitmap->info.a = 0xFF;
slouken@3434
  1012
    surface->map = blitmap;
slouken@3434
  1013
slouken@3434
  1014
    /* The surface is ready to go */
slouken@3434
  1015
    surface->refcount = 1;
slouken@3434
  1016
    return SDL_TRUE;
slouken@3434
  1017
}
slouken@3434
  1018
slouken@3434
  1019
/*
slouken@3434
  1020
 * Copy a block of pixels of one format to another format
slouken@3434
  1021
 */
slouken@3434
  1022
int SDL_ConvertPixels(int width, int height,
slouken@3434
  1023
                      Uint32 src_format, const void * src, int src_pitch,
slouken@3434
  1024
                      Uint32 dst_format, void * dst, int dst_pitch)
slouken@3434
  1025
{
slouken@3434
  1026
    SDL_Surface src_surface, dst_surface;
slouken@3434
  1027
    SDL_PixelFormat src_fmt, dst_fmt;
slouken@3434
  1028
    SDL_BlitMap src_blitmap, dst_blitmap;
slouken@3434
  1029
    SDL_Rect rect;
icculus@6407
  1030
    void *nonconst_src = (void *) src;
slouken@3434
  1031
slouken@9046
  1032
    /* Check to make sure we are blitting somewhere, so we don't crash */
slouken@6920
  1033
    if (!dst) {
icculus@7037
  1034
        return SDL_InvalidParamError("dst");
slouken@6920
  1035
    }
slouken@6920
  1036
    if (!dst_pitch) {
icculus@7037
  1037
        return SDL_InvalidParamError("dst_pitch");
slouken@6920
  1038
    }
slouken@6920
  1039
slouken@3434
  1040
    /* Fast path for same format copy */
slouken@3434
  1041
    if (src_format == dst_format) {
slouken@9046
  1042
        int bpp, i;
slouken@3434
  1043
slouken@3434
  1044
        if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
slouken@3434
  1045
            switch (src_format) {
slouken@3434
  1046
            case SDL_PIXELFORMAT_YUY2:
slouken@3434
  1047
            case SDL_PIXELFORMAT_UYVY:
slouken@3434
  1048
            case SDL_PIXELFORMAT_YVYU:
slouken@3434
  1049
                bpp = 2;
slouken@6663
  1050
                break;
slouken@9046
  1051
            case SDL_PIXELFORMAT_YV12:
slouken@9046
  1052
            case SDL_PIXELFORMAT_IYUV:
slouken@9046
  1053
            case SDL_PIXELFORMAT_NV12:
slouken@9046
  1054
            case SDL_PIXELFORMAT_NV21:
slouken@9046
  1055
                bpp = 1;
slouken@9046
  1056
                break;
slouken@3434
  1057
            default:
icculus@7037
  1058
                return SDL_SetError("Unknown FOURCC pixel format");
slouken@3434
  1059
            }
slouken@3434
  1060
        } else {
slouken@3434
  1061
            bpp = SDL_BYTESPERPIXEL(src_format);
slouken@3434
  1062
        }
slouken@3434
  1063
        width *= bpp;
slouken@3434
  1064
slouken@9046
  1065
        for (i = height; i--;) {
slouken@3434
  1066
            SDL_memcpy(dst, src, width);
slouken@3434
  1067
            src = (Uint8*)src + src_pitch;
slouken@3434
  1068
            dst = (Uint8*)dst + dst_pitch;
slouken@3434
  1069
        }
slouken@9046
  1070
slouken@9046
  1071
        if (src_format == SDL_PIXELFORMAT_YV12 || src_format == SDL_PIXELFORMAT_IYUV) {
slouken@9046
  1072
            /* U and V planes are a quarter the size of the Y plane */
slouken@9046
  1073
            width /= 2;
slouken@9046
  1074
            height /= 2;
slouken@9046
  1075
            src_pitch /= 2;
slouken@9046
  1076
            dst_pitch /= 2;
slouken@9046
  1077
            for (i = height * 2; i--;) {
slouken@9046
  1078
                SDL_memcpy(dst, src, width);
slouken@9046
  1079
                src = (Uint8*)src + src_pitch;
slouken@9046
  1080
                dst = (Uint8*)dst + dst_pitch;
slouken@9046
  1081
            }
slouken@9046
  1082
        } else if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
slouken@9046
  1083
            /* U/V plane is half the height of the Y plane */
slouken@9046
  1084
            height /= 2;
slouken@9046
  1085
            for (i = height; i--;) {
slouken@9046
  1086
                SDL_memcpy(dst, src, width);
slouken@9046
  1087
                src = (Uint8*)src + src_pitch;
slouken@9046
  1088
                dst = (Uint8*)dst + dst_pitch;
slouken@9046
  1089
            }
slouken@9046
  1090
        }
slouken@6041
  1091
        return 0;
slouken@3434
  1092
    }
slouken@3434
  1093
icculus@6407
  1094
    if (!SDL_CreateSurfaceOnStack(width, height, src_format, nonconst_src,
slouken@3434
  1095
                                  src_pitch,
slouken@3434
  1096
                                  &src_surface, &src_fmt, &src_blitmap)) {
slouken@3434
  1097
        return -1;
slouken@3434
  1098
    }
slouken@3434
  1099
    if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
slouken@3434
  1100
                                  &dst_surface, &dst_fmt, &dst_blitmap)) {
slouken@3434
  1101
        return -1;
slouken@3434
  1102
    }
slouken@3434
  1103
slouken@3434
  1104
    /* Set up the rect and go! */
slouken@3434
  1105
    rect.x = 0;
slouken@3434
  1106
    rect.y = 0;
slouken@3434
  1107
    rect.w = width;
slouken@3436
  1108
    rect.h = height;
slouken@3434
  1109
    return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
slouken@3434
  1110
}
slouken@3434
  1111
slouken@3434
  1112
/*
slouken@0
  1113
 * Free a surface created by the above function.
slouken@0
  1114
 */
slouken@1895
  1115
void
slouken@1895
  1116
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
  1117
{
slouken@1895
  1118
    if (surface == NULL) {
slouken@1895
  1119
        return;
slouken@1895
  1120
    }
slouken@5288
  1121
    if (surface->flags & SDL_DONTFREE) {
slouken@5288
  1122
        return;
slouken@5288
  1123
    }
slouken@1895
  1124
    if (--surface->refcount > 0) {
slouken@1895
  1125
        return;
slouken@1895
  1126
    }
slouken@1895
  1127
    while (surface->locked > 0) {
slouken@1895
  1128
        SDL_UnlockSurface(surface);
slouken@1895
  1129
    }
slouken@1895
  1130
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
  1131
        SDL_UnRLESurface(surface, 0);
slouken@1895
  1132
    }
slouken@1895
  1133
    if (surface->format) {
slouken@1895
  1134
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
  1135
        SDL_FreeFormat(surface->format);
slouken@1895
  1136
        surface->format = NULL;
slouken@1895
  1137
    }
slouken@1895
  1138
    if (surface->map != NULL) {
slouken@1895
  1139
        SDL_FreeBlitMap(surface->map);
slouken@1895
  1140
        surface->map = NULL;
slouken@1895
  1141
    }
slouken@7719
  1142
    if (!(surface->flags & SDL_PREALLOC)) {
slouken@1895
  1143
        SDL_free(surface->pixels);
slouken@1895
  1144
    }
slouken@1895
  1145
    SDL_free(surface);
slouken@0
  1146
}
slouken@1895
  1147
slouken@1895
  1148
/* vi: set ts=4 sw=4 expandtab: */