src/render/software/SDL_rotate.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 30 Oct 2018 07:00:03 -0700
changeset 12372 bced4041fcc0
parent 12311 b19b09eb9e6d
permissions -rw-r--r--
Fixed bug 4188 - Software renderer SDL_RenderCopyEx blits corrupt image under certain cases

Sylvain

Re-opening this issue.

It fixes the test-case, but it introduces a regression with another bug (bug #4313).

So here's a new patch that activate cropping of the source surface to solve the issue.
It also reverts the wrong changeset.
It prevents unneeded colorkey error message.
gabomdq@6320
     1
/*
gabomdq@6320
     2
gabomdq@6320
     3
SDL_rotate.c: rotates 32bit or 8bit surfaces
gabomdq@6320
     4
gabomdq@6320
     5
Shamelessly stolen from SDL_gfx by Andreas Schiffler. Original copyright follows:
gabomdq@6320
     6
gabomdq@6320
     7
Copyright (C) 2001-2011  Andreas Schiffler
gabomdq@6320
     8
gabomdq@6320
     9
This software is provided 'as-is', without any express or implied
gabomdq@6320
    10
warranty. In no event will the authors be held liable for any damages
gabomdq@6320
    11
arising from the use of this software.
gabomdq@6320
    12
gabomdq@6320
    13
Permission is granted to anyone to use this software for any purpose,
gabomdq@6320
    14
including commercial applications, and to alter it and redistribute it
gabomdq@6320
    15
freely, subject to the following restrictions:
gabomdq@6320
    16
gabomdq@6320
    17
   1. The origin of this software must not be misrepresented; you must not
gabomdq@6320
    18
   claim that you wrote the original software. If you use this software
gabomdq@6320
    19
   in a product, an acknowledgment in the product documentation would be
gabomdq@6320
    20
   appreciated but is not required.
gabomdq@6320
    21
gabomdq@6320
    22
   2. Altered source versions must be plainly marked as such, and must not be
gabomdq@6320
    23
   misrepresented as being the original software.
gabomdq@6320
    24
gabomdq@6320
    25
   3. This notice may not be removed or altered from any source
gabomdq@6320
    26
   distribution.
gabomdq@6320
    27
gabomdq@6320
    28
Andreas Schiffler -- aschiffler at ferzkopp dot net
gabomdq@6320
    29
gabomdq@6320
    30
*/
icculus@8093
    31
#include "../../SDL_internal.h"
gabomdq@6320
    32
slouken@7828
    33
#if defined(__WIN32__)
slouken@7828
    34
#include "../../core/windows/SDL_windows.h"
gabomdq@6320
    35
#endif
gabomdq@6320
    36
gabomdq@6320
    37
#include <stdlib.h>
gabomdq@6320
    38
#include <string.h>
gabomdq@6320
    39
gabomdq@6320
    40
#include "SDL.h"
gabomdq@6320
    41
#include "SDL_rotate.h"
gabomdq@6320
    42
gabomdq@6320
    43
/* ---- Internally used structures */
gabomdq@6320
    44
gabomdq@7678
    45
/* !
gabomdq@6320
    46
\brief A 32 bit RGBA pixel.
gabomdq@6320
    47
*/
gabomdq@6320
    48
typedef struct tColorRGBA {
gabomdq@6320
    49
    Uint8 r;
gabomdq@6320
    50
    Uint8 g;
gabomdq@6320
    51
    Uint8 b;
gabomdq@6320
    52
    Uint8 a;
gabomdq@6320
    53
} tColorRGBA;
gabomdq@6320
    54
gabomdq@7678
    55
/* !
gabomdq@6320
    56
\brief A 8bit Y/palette pixel.
gabomdq@6320
    57
*/
gabomdq@6320
    58
typedef struct tColorY {
gabomdq@6320
    59
    Uint8 y;
gabomdq@6320
    60
} tColorY;
gabomdq@6320
    61
gabomdq@7678
    62
/* !
gabomdq@6320
    63
\brief Returns maximum of two numbers a and b.
gabomdq@6320
    64
*/
gabomdq@6320
    65
#define MAX(a,b)    (((a) > (b)) ? (a) : (b))
gabomdq@6320
    66
gabomdq@7678
    67
/* !
gabomdq@6320
    68
\brief Number of guard rows added to destination surfaces.
gabomdq@6320
    69
gabomdq@6320
    70
This is a simple but effective workaround for observed issues.
gabomdq@6320
    71
These rows allocate extra memory and are then hidden from the surface.
gabomdq@6320
    72
Rows are added to the end of destination surfaces when they are allocated.
gabomdq@6320
    73
This catches any potential overflows which seem to happen with
gabomdq@6320
    74
just the right src image dimensions and scale/rotation and can lead
gabomdq@6320
    75
to a situation where the program can segfault.
gabomdq@6320
    76
*/
gabomdq@6320
    77
#define GUARD_ROWS (2)
gabomdq@6320
    78
gabomdq@7678
    79
/* !
gabomdq@6320
    80
\brief Returns colorkey info for a surface
gabomdq@6320
    81
*/
slouken@7859
    82
static Uint32
slouken@7859
    83
_colorkey(SDL_Surface *src)
gabomdq@6320
    84
{
gabomdq@6320
    85
    Uint32 key = 0;
slouken@12372
    86
    if (SDL_HasColorKey(src)) {
slouken@12372
    87
        SDL_GetColorKey(src, &key);
slouken@12372
    88
    }
gabomdq@6320
    89
    return key;
gabomdq@6320
    90
}
gabomdq@6320
    91
gabomdq@6320
    92
gabomdq@7678
    93
/* !
gabomdq@6320
    94
\brief Internal target surface sizing function for rotations with trig result return.
gabomdq@6320
    95
gabomdq@6320
    96
\param width The source surface width.
gabomdq@6320
    97
\param height The source surface height.
gabomdq@6320
    98
\param angle The angle to rotate in degrees.
gabomdq@6320
    99
\param dstwidth The calculated width of the destination surface.
gabomdq@6320
   100
\param dstheight The calculated height of the destination surface.
gabomdq@6320
   101
\param cangle The sine of the angle
gabomdq@6320
   102
\param sangle The cosine of the angle
gabomdq@6320
   103
gabomdq@6320
   104
*/
slouken@7859
   105
void
slouken@7859
   106
SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
slouken@7859
   107
                               int *dstwidth, int *dstheight,
slouken@7859
   108
                               double *cangle, double *sangle)
gabomdq@6320
   109
{
slouken@10489
   110
    /* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
slouken@10489
   111
    int angle90 = (int)(angle/90);
slouken@10489
   112
    if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
slouken@10489
   113
        angle90 %= 4;
slouken@10489
   114
        if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
slouken@10489
   115
        if(angle90 & 1) {
slouken@10489
   116
            *dstwidth  = height;
slouken@10489
   117
            *dstheight = width;
slouken@10489
   118
            *cangle = 0;
slouken@10489
   119
            *sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
slouken@10489
   120
        } else {
slouken@10489
   121
            *dstwidth  = width;
slouken@10489
   122
            *dstheight = height;
slouken@10489
   123
            *cangle = angle90 == 0 ? 1 : -1;
slouken@10489
   124
            *sangle = 0;
slouken@10489
   125
        }
slouken@10489
   126
    } else {
slouken@10489
   127
        double x, y, cx, cy, sx, sy;
slouken@10489
   128
        double radangle;
slouken@10489
   129
        int dstwidthhalf, dstheighthalf;
slouken@10489
   130
        /*
slouken@10489
   131
        * Determine destination width and height by rotating a centered source box
slouken@10489
   132
        */
slouken@10489
   133
        radangle = angle * (M_PI / -180.0); /* reverse the angle because our rotations are clockwise */
slouken@10489
   134
        *sangle = SDL_sin(radangle);
slouken@10489
   135
        *cangle = SDL_cos(radangle);
slouken@10489
   136
        x = (double)(width / 2);
slouken@10489
   137
        y = (double)(height / 2);
slouken@10489
   138
        cx = *cangle * x;
slouken@10489
   139
        cy = *cangle * y;
slouken@10489
   140
        sx = *sangle * x;
slouken@10489
   141
        sy = *sangle * y;
slouken@10612
   142
slouken@10489
   143
        dstwidthhalf = MAX((int)
slouken@10489
   144
            SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
slouken@10489
   145
        dstheighthalf = MAX((int)
slouken@10489
   146
            SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
slouken@10489
   147
        *dstwidth = 2 * dstwidthhalf;
slouken@10489
   148
        *dstheight = 2 * dstheighthalf;
slouken@10489
   149
    }
gabomdq@6320
   150
}
gabomdq@6320
   151
slouken@10489
   152
/* Computes source pointer X/Y increments for a rotation that's a multiple of 90 degrees. */
slouken@10489
   153
static void
slouken@10489
   154
computeSourceIncrements90(SDL_Surface * src, int bpp, int angle, int flipx, int flipy,
slouken@12372
   155
                          int *sincx, int *sincy, int *signx, int *signy)
slouken@10489
   156
{
slouken@10489
   157
    int pitch = flipy ? -src->pitch : src->pitch;
slouken@10489
   158
    if (flipx) {
slouken@10489
   159
        bpp = -bpp;
slouken@10489
   160
    }
slouken@10489
   161
    switch (angle) { /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
slouken@12372
   162
    case 0: *sincx = bpp; *sincy = pitch - src->w * *sincx; *signx = *signy = 1; break;
slouken@12372
   163
    case 1: *sincx = -pitch; *sincy = bpp - *sincx * src->h; *signx = 1; *signy = -1; break;
slouken@12372
   164
    case 2: *sincx = -bpp; *sincy = -src->w * *sincx - pitch; *signx = *signy = -1; break;
slouken@12372
   165
    case 3: default: *sincx = pitch; *sincy = -*sincx * src->h - bpp; *signx = -1; *signy = 1; break;
slouken@10489
   166
    }
slouken@10489
   167
    if (flipx) {
slouken@10489
   168
        *signx = -*signx;
slouken@10489
   169
    }
slouken@10489
   170
    if (flipy) {
slouken@10489
   171
        *signy = -*signy;
slouken@10489
   172
    }
slouken@10489
   173
}
slouken@10489
   174
slouken@10489
   175
/* Performs a relatively fast rotation/flip when the angle is a multiple of 90 degrees. */
slouken@10489
   176
#define TRANSFORM_SURFACE_90(pixelType) \
slouken@10489
   177
    int dy, dincy = dst->pitch - dst->w*sizeof(pixelType), sincx, sincy, signx, signy;                      \
slouken@10489
   178
    Uint8 *sp = (Uint8*)src->pixels, *dp = (Uint8*)dst->pixels, *de;                                        \
slouken@10489
   179
                                                                                                            \
slouken@12372
   180
    computeSourceIncrements90(src, sizeof(pixelType), angle, flipx, flipy, &sincx, &sincy, &signx, &signy); \
slouken@12372
   181
    if (signx < 0) sp += (src->w-1)*sizeof(pixelType);                                                      \
slouken@12372
   182
    if (signy < 0) sp += (src->h-1)*src->pitch;                                                             \
slouken@12372
   183
                                                                                                            \
slouken@10489
   184
    for (dy = 0; dy < dst->h; sp += sincy, dp += dincy, dy++) {                                             \
slouken@10489
   185
        if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use memcpy */               \
slouken@10489
   186
            SDL_memcpy(dp, sp, dst->w*sizeof(pixelType));                                                   \
slouken@10489
   187
            sp += dst->w*sizeof(pixelType);                                                                 \
slouken@10489
   188
            dp += dst->w*sizeof(pixelType);                                                                 \
slouken@10489
   189
        } else {                                                                                            \
slouken@10489
   190
            for (de = dp + dst->w*sizeof(pixelType); dp != de; sp += sincx, dp += sizeof(pixelType)) {      \
slouken@10489
   191
                *(pixelType*)dp = *(pixelType*)sp;                                                          \
slouken@10489
   192
            }                                                                                               \
slouken@10489
   193
        }                                                                                                   \
slouken@10489
   194
    }
slouken@10489
   195
slouken@10489
   196
static void
slouken@10489
   197
transformSurfaceRGBA90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
slouken@10489
   198
{
slouken@10489
   199
    TRANSFORM_SURFACE_90(tColorRGBA);
slouken@10489
   200
}
slouken@10489
   201
slouken@10489
   202
static void
slouken@10489
   203
transformSurfaceY90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
slouken@10489
   204
{
slouken@10489
   205
    TRANSFORM_SURFACE_90(tColorY);
slouken@10489
   206
}
slouken@10489
   207
slouken@10489
   208
#undef TRANSFORM_SURFACE_90
gabomdq@6320
   209
gabomdq@7678
   210
/* !
gabomdq@6320
   211
\brief Internal 32 bit rotozoomer with optional anti-aliasing.
gabomdq@6320
   212
gabomdq@6320
   213
Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
gabomdq@6320
   214
parameters by scanning the destination surface and applying optionally anti-aliasing
gabomdq@6320
   215
by bilinear interpolation.
gabomdq@6320
   216
Assumes src and dst surfaces are of 32 bit depth.
gabomdq@6320
   217
Assumes dst surface was allocated with the correct dimensions.
gabomdq@6320
   218
gabomdq@6320
   219
\param src Source surface.
gabomdq@6320
   220
\param dst Destination surface.
gabomdq@6320
   221
\param cx Horizontal center coordinate.
gabomdq@6320
   222
\param cy Vertical center coordinate.
gabomdq@6320
   223
\param isin Integer version of sine of angle.
gabomdq@6320
   224
\param icos Integer version of cosine of angle.
gabomdq@6320
   225
\param flipx Flag indicating horizontal mirroring should be applied.
gabomdq@6320
   226
\param flipy Flag indicating vertical mirroring should be applied.
gabomdq@6320
   227
\param smooth Flag indicating anti-aliasing should be used.
gabomdq@6320
   228
*/
slouken@7859
   229
static void
slouken@7859
   230
_transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
gabomdq@6320
   231
{
gabomdq@6320
   232
    int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
gabomdq@6320
   233
    tColorRGBA c00, c01, c10, c11, cswap;
gabomdq@6320
   234
    tColorRGBA *pc, *sp;
gabomdq@6320
   235
    int gap;
gabomdq@6320
   236
gabomdq@6320
   237
    /*
gabomdq@6320
   238
    * Variable setup
gabomdq@6320
   239
    */
gabomdq@6320
   240
    xd = ((src->w - dst->w) << 15);
gabomdq@6320
   241
    yd = ((src->h - dst->h) << 15);
gabomdq@6320
   242
    ax = (cx << 16) - (icos * cx);
gabomdq@6320
   243
    ay = (cy << 16) - (isin * cx);
gabomdq@6320
   244
    sw = src->w - 1;
gabomdq@6320
   245
    sh = src->h - 1;
gabomdq@6320
   246
    pc = (tColorRGBA*) dst->pixels;
gabomdq@6320
   247
    gap = dst->pitch - dst->w * 4;
gabomdq@6320
   248
gabomdq@6320
   249
    /*
gabomdq@6320
   250
    * Switch between interpolating and non-interpolating code
gabomdq@6320
   251
    */
gabomdq@6320
   252
    if (smooth) {
gabomdq@6320
   253
        for (y = 0; y < dst->h; y++) {
gabomdq@6320
   254
            dy = cy - y;
gabomdq@6320
   255
            sdx = (ax + (isin * dy)) + xd;
gabomdq@6320
   256
            sdy = (ay - (icos * dy)) + yd;
gabomdq@6320
   257
            for (x = 0; x < dst->w; x++) {
gabomdq@6320
   258
                dx = (sdx >> 16);
gabomdq@6320
   259
                dy = (sdy >> 16);
gabomdq@6320
   260
                if (flipx) dx = sw - dx;
gabomdq@6320
   261
                if (flipy) dy = sh - dy;
slouken@11511
   262
                if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
slouken@9762
   263
                    sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
gabomdq@6320
   264
                    c00 = *sp;
gabomdq@6320
   265
                    sp += 1;
gabomdq@6320
   266
                    c01 = *sp;
gabomdq@6320
   267
                    sp += (src->pitch/4);
gabomdq@6320
   268
                    c11 = *sp;
gabomdq@6320
   269
                    sp -= 1;
gabomdq@6320
   270
                    c10 = *sp;
gabomdq@6320
   271
                    if (flipx) {
gabomdq@6320
   272
                        cswap = c00; c00=c01; c01=cswap;
gabomdq@6320
   273
                        cswap = c10; c10=c11; c11=cswap;
gabomdq@6320
   274
                    }
gabomdq@6320
   275
                    if (flipy) {
gabomdq@6320
   276
                        cswap = c00; c00=c10; c10=cswap;
gabomdq@6320
   277
                        cswap = c01; c01=c11; c11=cswap;
gabomdq@6320
   278
                    }
gabomdq@6320
   279
                    /*
gabomdq@6320
   280
                    * Interpolate colors
gabomdq@6320
   281
                    */
gabomdq@6320
   282
                    ex = (sdx & 0xffff);
gabomdq@6320
   283
                    ey = (sdy & 0xffff);
gabomdq@6320
   284
                    t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
gabomdq@6320
   285
                    t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
gabomdq@6320
   286
                    pc->r = (((t2 - t1) * ey) >> 16) + t1;
gabomdq@6320
   287
                    t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
gabomdq@6320
   288
                    t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
gabomdq@6320
   289
                    pc->g = (((t2 - t1) * ey) >> 16) + t1;
gabomdq@6320
   290
                    t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
gabomdq@6320
   291
                    t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
gabomdq@6320
   292
                    pc->b = (((t2 - t1) * ey) >> 16) + t1;
gabomdq@6320
   293
                    t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
gabomdq@6320
   294
                    t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
gabomdq@6320
   295
                    pc->a = (((t2 - t1) * ey) >> 16) + t1;
gabomdq@6320
   296
                }
gabomdq@6320
   297
                sdx += icos;
gabomdq@6320
   298
                sdy += isin;
gabomdq@6320
   299
                pc++;
gabomdq@6320
   300
            }
gabomdq@6320
   301
            pc = (tColorRGBA *) ((Uint8 *) pc + gap);
gabomdq@6320
   302
        }
gabomdq@6320
   303
    } else {
gabomdq@6320
   304
        for (y = 0; y < dst->h; y++) {
gabomdq@6320
   305
            dy = cy - y;
gabomdq@6320
   306
            sdx = (ax + (isin * dy)) + xd;
gabomdq@6320
   307
            sdy = (ay - (icos * dy)) + yd;
gabomdq@6320
   308
            for (x = 0; x < dst->w; x++) {
slouken@9762
   309
                dx = (sdx >> 16);
slouken@9762
   310
                dy = (sdy >> 16);
slouken@9762
   311
                if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
slouken@9762
   312
                    if(flipx) dx = sw - dx;
slouken@9762
   313
                    if(flipy) dy = sh - dy;
slouken@9762
   314
                    *pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
gabomdq@6320
   315
                }
gabomdq@6320
   316
                sdx += icos;
gabomdq@6320
   317
                sdy += isin;
gabomdq@6320
   318
                pc++;
gabomdq@6320
   319
            }
gabomdq@6320
   320
            pc = (tColorRGBA *) ((Uint8 *) pc + gap);
gabomdq@6320
   321
        }
gabomdq@6320
   322
    }
gabomdq@6320
   323
}
gabomdq@6320
   324
gabomdq@7678
   325
/* !
gabomdq@6320
   326
gabomdq@6320
   327
\brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
gabomdq@6320
   328
gabomdq@6320
   329
Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
gabomdq@6320
   330
parameters by scanning the destination surface.
gabomdq@6320
   331
Assumes src and dst surfaces are of 8 bit depth.
gabomdq@6320
   332
Assumes dst surface was allocated with the correct dimensions.
gabomdq@6320
   333
gabomdq@6320
   334
\param src Source surface.
gabomdq@6320
   335
\param dst Destination surface.
gabomdq@6320
   336
\param cx Horizontal center coordinate.
gabomdq@6320
   337
\param cy Vertical center coordinate.
gabomdq@6320
   338
\param isin Integer version of sine of angle.
gabomdq@6320
   339
\param icos Integer version of cosine of angle.
gabomdq@6320
   340
\param flipx Flag indicating horizontal mirroring should be applied.
gabomdq@6320
   341
\param flipy Flag indicating vertical mirroring should be applied.
gabomdq@6320
   342
*/
slouken@7859
   343
static void
slouken@7859
   344
transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
gabomdq@6320
   345
{
icculus@6389
   346
    int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
slouken@9762
   347
    tColorY *pc;
gabomdq@6320
   348
    int gap;
gabomdq@6320
   349
gabomdq@6320
   350
    /*
gabomdq@6320
   351
    * Variable setup
gabomdq@6320
   352
    */
gabomdq@6320
   353
    xd = ((src->w - dst->w) << 15);
gabomdq@6320
   354
    yd = ((src->h - dst->h) << 15);
gabomdq@6320
   355
    ax = (cx << 16) - (icos * cx);
gabomdq@6320
   356
    ay = (cy << 16) - (isin * cx);
gabomdq@6320
   357
    pc = (tColorY*) dst->pixels;
gabomdq@6320
   358
    gap = dst->pitch - dst->w;
gabomdq@6320
   359
    /*
gabomdq@6320
   360
    * Clear surface to colorkey
gabomdq@6320
   361
    */
gabomdq@6320
   362
    SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
gabomdq@6320
   363
    /*
gabomdq@6320
   364
    * Iterate through destination surface
gabomdq@6320
   365
    */
gabomdq@6320
   366
    for (y = 0; y < dst->h; y++) {
gabomdq@6320
   367
        dy = cy - y;
gabomdq@6320
   368
        sdx = (ax + (isin * dy)) + xd;
gabomdq@6320
   369
        sdy = (ay - (icos * dy)) + yd;
gabomdq@6320
   370
        for (x = 0; x < dst->w; x++) {
slouken@9762
   371
            dx = (sdx >> 16);
slouken@9762
   372
            dy = (sdy >> 16);
slouken@9762
   373
            if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
slouken@9762
   374
                if (flipx) dx = (src->w-1)-dx;
slouken@9762
   375
                if (flipy) dy = (src->h-1)-dy;
slouken@9762
   376
                *pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
gabomdq@6320
   377
            }
gabomdq@6320
   378
            sdx += icos;
gabomdq@6320
   379
            sdy += isin;
gabomdq@6320
   380
            pc++;
gabomdq@6320
   381
        }
gabomdq@6320
   382
        pc += gap;
gabomdq@6320
   383
    }
gabomdq@6320
   384
}
gabomdq@6320
   385
gabomdq@6320
   386
gabomdq@7678
   387
/* !
gabomdq@6320
   388
\brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
gabomdq@6320
   389
slouken@10612
   390
Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
gabomdq@6320
   391
'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
slouken@10612
   392
then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
slouken@10612
   393
surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
slouken@10612
   394
The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
slouken@10612
   395
mode will set the BLEND mode on the 'dst' surface. The MOD mode either generates a white 'dst'
slouken@10612
   396
surface and sets the colorkey or fills the it with the colorkey before copying the pixels.
slouken@10612
   397
When using the NONE and MOD modes, color and alpha modulation must be applied before using this function.
gabomdq@6320
   398
gabomdq@6320
   399
\param src The surface to rotozoom.
gabomdq@6320
   400
\param angle The angle to rotate in degrees.
gabomdq@6320
   401
\param centerx The horizontal coordinate of the center of rotation
gabomdq@6320
   402
\param zoomy The vertical coordinate of the center of rotation
gabomdq@6320
   403
\param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
gabomdq@6320
   404
\param flipx Set to 1 to flip the image horizontally
gabomdq@6320
   405
\param flipy Set to 1 to flip the image vertically
gabomdq@6320
   406
\param dstwidth The destination surface width
gabomdq@6320
   407
\param dstheight The destination surface height
gabomdq@6320
   408
\param cangle The angle cosine
gabomdq@6320
   409
\param sangle The angle sine
gabomdq@6320
   410
\return The new rotated surface.
gabomdq@6320
   411
gabomdq@6320
   412
*/
gabomdq@6320
   413
slouken@7859
   414
SDL_Surface *
slouken@7859
   415
SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
gabomdq@6320
   416
{
gabomdq@6320
   417
    SDL_Surface *rz_dst;
slouken@10612
   418
    int is8bit, angle90;
slouken@9763
   419
    int i;
slouken@10612
   420
    SDL_BlendMode blendmode;
gabomdq@6320
   421
    Uint32 colorkey = 0;
slouken@10612
   422
    int colorKeyAvailable = SDL_FALSE;
gabomdq@6320
   423
    double sangleinv, cangleinv;
gabomdq@6320
   424
slouken@10612
   425
    /* Sanity check */
gabomdq@6320
   426
    if (src == NULL)
slouken@10612
   427
        return NULL;
gabomdq@6320
   428
slouken@12372
   429
    if (SDL_HasColorKey(src)) {
slouken@12372
   430
        if (SDL_GetColorKey(src, &colorkey) == 0) {
slouken@12372
   431
            colorKeyAvailable = SDL_TRUE;
slouken@12372
   432
        }
gabomdq@6320
   433
    }
slouken@10606
   434
slouken@10612
   435
    /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
slouken@10612
   436
    is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
slouken@10612
   437
    if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
slouken@10612
   438
        return NULL;
gabomdq@6320
   439
slouken@10612
   440
    /* Calculate target factors from sin/cos and zoom */
gabomdq@6320
   441
    sangleinv = sangle*65536.0;
gabomdq@6320
   442
    cangleinv = cangle*65536.0;
gabomdq@6320
   443
slouken@10612
   444
    /* Alloc space to completely contain the rotated surface */
slouken@10612
   445
    rz_dst = NULL;
slouken@10612
   446
    if (is8bit) {
slouken@10612
   447
        /* Target surface is 8 bit */
slouken@10612
   448
        rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
slouken@10612
   449
        if (rz_dst != NULL) {
slouken@10612
   450
            for (i = 0; i < src->format->palette->ncolors; i++) {
slouken@10612
   451
                rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
slouken@10612
   452
            }
slouken@10612
   453
            rz_dst->format->palette->ncolors = src->format->palette->ncolors;
slouken@10612
   454
        }
gabomdq@6320
   455
    } else {
slouken@10612
   456
        /* Target surface is 32 bit with source RGBA ordering */
slouken@10612
   457
        rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
slouken@10612
   458
                                      src->format->Rmask, src->format->Gmask,
slouken@10612
   459
                                      src->format->Bmask, src->format->Amask);
gabomdq@6320
   460
    }
gabomdq@6320
   461
gabomdq@6320
   462
    /* Check target */
gabomdq@6320
   463
    if (rz_dst == NULL)
gabomdq@6320
   464
        return NULL;
gabomdq@6320
   465
gabomdq@6320
   466
    /* Adjust for guard rows */
gabomdq@6320
   467
    rz_dst->h = dstheight;
gabomdq@6320
   468
slouken@10612
   469
    SDL_GetSurfaceBlendMode(src, &blendmode);
gabomdq@6320
   470
slouken@10612
   471
    if (colorKeyAvailable == SDL_TRUE) {
slouken@10612
   472
        /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
slouken@10612
   473
        SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
slouken@10606
   474
        SDL_FillRect(rz_dst, NULL, colorkey);
slouken@10612
   475
    } else if (blendmode == SDL_BLENDMODE_NONE) {
slouken@10612
   476
        blendmode = SDL_BLENDMODE_BLEND;
slouken@10612
   477
    } else if (blendmode == SDL_BLENDMODE_MOD) {
slouken@10612
   478
        /* Without a colorkey, the target texture has to be white for the MOD blend mode so
slouken@10612
   479
         * that the pixels outside the rotated area don't affect the destination surface.
slouken@10612
   480
         */
slouken@10612
   481
        colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
slouken@10612
   482
        SDL_FillRect(rz_dst, NULL, colorkey);
slouken@10612
   483
        /* Setting a white colorkey for the destination surface makes the final blit discard
slouken@10612
   484
         * all pixels outside of the rotated area. This doesn't interfere with anything because
slouken@10612
   485
         * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
slouken@10612
   486
         */
slouken@10612
   487
        SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
gabomdq@6320
   488
    }
gabomdq@6320
   489
slouken@10612
   490
    SDL_SetSurfaceBlendMode(rz_dst, blendmode);
slouken@10612
   491
slouken@10612
   492
    /* Lock source surface */
slouken@10612
   493
    if (SDL_MUSTLOCK(src)) {
slouken@10612
   494
        SDL_LockSurface(src);
gabomdq@6320
   495
    }
gabomdq@6320
   496
slouken@10489
   497
    /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
slouken@10489
   498
     * the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
slouken@10489
   499
     * multiples of 90 degrees.
slouken@10489
   500
     */
slouken@10489
   501
    angle90 = (int)(angle/90);
slouken@10489
   502
    if (angle90 == angle/90) {
slouken@10489
   503
        angle90 %= 4;
slouken@10489
   504
        if (angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
slouken@10489
   505
    } else {
slouken@10489
   506
        angle90 = -1;
slouken@10489
   507
    }
slouken@10489
   508
slouken@10612
   509
    if (is8bit) {
slouken@10612
   510
        /* Call the 8-bit transformation routine to do the rotation */
slouken@10612
   511
        if(angle90 >= 0) {
slouken@10612
   512
            transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
slouken@10612
   513
        } else {
slouken@10612
   514
            transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
slouken@10612
   515
                              flipx, flipy);
slouken@10612
   516
        }
slouken@10612
   517
    } else {
slouken@10612
   518
        /* Call the 32-bit transformation routine to do the rotation */
slouken@10489
   519
        if (angle90 >= 0) {
slouken@10612
   520
            transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
slouken@10489
   521
        } else {
slouken@10612
   522
            _transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
slouken@10612
   523
                                  flipx, flipy, smooth);
slouken@10489
   524
        }
slouken@10606
   525
    }
slouken@10606
   526
slouken@10612
   527
    /* Unlock source surface */
slouken@10612
   528
    if (SDL_MUSTLOCK(src)) {
slouken@10612
   529
        SDL_UnlockSurface(src);
gabomdq@6320
   530
    }
slouken@9763
   531
slouken@10612
   532
    /* Return rotated surface */
slouken@10612
   533
    return rz_dst;
gabomdq@6320
   534
}