SDL_rotate.c File Reference
`#include "../../SDL_internal.h"`
`#include <stdlib.h>`
`#include <string.h>`
`#include "SDL.h"`
`#include "SDL_rotate.h"`
Include dependency graph for SDL_rotate.c:

Go to the source code of this file.

## Data Structures

struct | tColorRGBA
struct | tColorY

## Macros

#define | MAX(a, b) (((a) > (b)) ? (a) : (b))
#define | GUARD_ROWS (2)
#define | VALUE_LIMIT 0.001

## Functions

static Uint32 | _colorkey (SDL_Surface *src)
void | SDLgfx_rotozoomSurfaceSizeTrig (int width, int height, double angle, int *dstwidth, int *dstheight, double *cangle, double *sangle)
static void | _transformSurfaceRGBA (SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
static void | transformSurfaceY (SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
SDL_Surface * | 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)

## Macro Definition Documentation

#define GUARD_ROWS (2)

Definition at line 77 of file SDL_rotate.c.

Referenced by SDLgfx_rotateSurface().

#define MAX | ( |  | a,
|  |  | b
| ) |  | (((a) > (b)) ? (a) : (b))

Definition at line 65 of file SDL_rotate.c.

Referenced by SDLgfx_rotozoomSurfaceSizeTrig().

#define VALUE_LIMIT 0.001

Definition at line 82 of file SDL_rotate.c.

## Function Documentation

static Uint32 _colorkey | ( | SDL_Surface * | src | ) |

static

Definition at line 88 of file SDL_rotate.c.

References SDL_GetColorKey.

Referenced by SDLgfx_rotateSurface(), and transformSurfaceY().

{
Uint32 key = 0;
SDL_GetColorKey(src, &key);
return key;
}
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDL_GetColorKey
#define SDL_GetColorKey
Definition: SDL_dynapi_overrides.h:458

static void _transformSurfaceRGBA | ( | SDL_Surface * | src,
|  | SDL_Surface * | dst,
|  | int | cx,
|  | int | cy,
|  | int | isin,
|  | int | icos,
|  | int | flipx,
|  | int | flipy,
|  | int | smooth
| ) |  |

static

Definition at line 159 of file SDL_rotate.c.

References tColorRGBA::a, tColorRGBA::b, tColorRGBA::g, SDL_Surface::h, SDL_Surface::pitch, SDL_Surface::pixels, tColorRGBA::r, and SDL_Surface::w.

Referenced by SDLgfx_rotateSurface().

{
int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
tColorRGBA c00, c01, c10, c11, cswap;
tColorRGBA *pc, *sp;
int gap;
/*
* Variable setup
*/
xd = ((src->w - dst->w) << 15);
yd = ((src->h - dst->h) << 15);
ax = (cx << 16) - (icos * cx);
ay = (cy << 16) - (isin * cx);
sw = src->w - 1;
sh = src->h - 1;
pc = (tColorRGBA*) dst->pixels;
gap = dst->pitch - dst->w * 4;
/*
* Switch between interpolating and non-interpolating code
*/
if (smooth) {
for (y = 0; y < dst->h; y++) {
dy = cy - y;
sdx = (ax + (isin * dy)) + xd;
sdy = (ay - (icos * dy)) + yd;
for (x = 0; x < dst->w; x++) {
dx = (sdx >> 16);
dy = (sdy >> 16);
if (flipx) dx = sw - dx;
if (flipy) dy = sh - dy;
if ((unsigned)dx < (unsigned)sw && (unsigned)dy < (unsigned)sh) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
c00 = *sp;
sp += 1;
c01 = *sp;
sp += (src->pitch/4);
c11 = *sp;
sp -= 1;
c10 = *sp;
if (flipx) {
cswap = c00; c00=c01; c01=cswap;
cswap = c10; c10=c11; c11=cswap;
}
if (flipy) {
cswap = c00; c00=c10; c10=cswap;
cswap = c01; c01=c11; c11=cswap;
}
/*
* Interpolate colors
*/
ex = (sdx & 0xffff);
ey = (sdy & 0xffff);
t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
pc->r = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
pc->g = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
pc->b = (((t2 - t1) * ey) >> 16) + t1;
t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
pc->a = (((t2 - t1) * ey) >> 16) + t1;
}
sdx += icos;
sdy += isin;
pc++;
}
pc = (tColorRGBA *) ((Uint8 *) pc + gap);
}
} else {
for (y = 0; y < dst->h; y++) {
dy = cy - y;
sdx = (ax + (isin * dy)) + xd;
sdy = (ay - (icos * dy)) + yd;
for (x = 0; x < dst->w; x++) {
dx = (sdx >> 16);
dy = (sdy >> 16);
if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
if(flipx) dx = sw - dx;
if(flipy) dy = sh - dy;
*pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
}
sdx += icos;
sdy += isin;
pc++;
}
pc = (tColorRGBA *) ((Uint8 *) pc + gap);
}
}
}
SDL_Surface::h
int h
Definition: SDL_surface.h:73
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
tColorRGBA::a
Uint8 a
Definition: SDL_rotate.c:52
SDL_Surface::w
int w
Definition: SDL_surface.h:73
t1
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition: SDL_opengl_glext.h:8583
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
tColorRGBA::r
Uint8 r
Definition: SDL_rotate.c:49
tColorRGBA
Definition: SDL_rotate.c:48
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
tColorRGBA::b
Uint8 b
Definition: SDL_rotate.c:51
tColorRGBA::g
Uint8 g
Definition: SDL_rotate.c:50

SDL_Surface* 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
| ) |  |

Definition at line 340 of file SDL_rotate.c.

References _colorkey(), _transformSurfaceRGBA(), SDL_PixelFormat::Amask, tColorRGBA::b, SDL_PixelFormat::BitsPerPixel, blendMode, SDL_PixelFormat::Bmask, SDL_Palette::colors, SDL_Surface::flags, SDL_Surface::format, tColorRGBA::g, SDL_PixelFormat::Gmask, GUARD_ROWS, SDL_Surface::h, i, SDL_Palette::ncolors, NULL, SDL_PixelFormat::palette, tColorRGBA::r, SDL_PixelFormat::Rmask, SDL_BYTEORDER, SDL_ConvertSurfaceFormat, SDL_CreateRGBSurface, SDL_FillRect, SDL_FreeSurface, SDL_GetRGB, SDL_GetSurfaceAlphaMod, SDL_GetSurfaceBlendMode, SDL_GetSurfaceColorMod, SDL_LIL_ENDIAN, SDL_LockSurface, SDL_MapRGB, SDL_MasksToPixelFormatEnum, SDL_MUSTLOCK, SDL_RLEACCEL, SDL_SetColorKey, SDL_SetSurfaceAlphaMod, SDL_SetSurfaceBlendMode, SDL_SetSurfaceColorMod, SDL_SWSURFACE, SDL_TRUE, SDL_UnlockSurface, and transformSurfaceY().

Referenced by SW_RenderCopyEx().

{
SDL_Surface *rz_src;
SDL_Surface *rz_dst;
int is32bit;
int i;
Uint8 r,g,b;
Uint32 colorkey = 0;
int colorKeyAvailable = 0;
double sangleinv, cangleinv;
/*
* Sanity check
*/
if (src == NULL)
return (NULL);
if (src->flags & SDL_TRUE/* SDL_SRCCOLORKEY */)
{
colorkey = _colorkey(src);
SDL_GetRGB(colorkey, src->format, &r, &g, &b);
colorKeyAvailable = 1;
}
/*
* Determine if source surface is 32bit or 8bit
*/
is32bit = (src->format->BitsPerPixel == 32);
if ((is32bit) || (src->format->BitsPerPixel == 8)) {
/*
* Use source surface 'as is'
*/
rz_src = src;
} else {
Uint32 format = SDL_MasksToPixelFormatEnum(32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
#else
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
#endif
);
rz_src = SDL_ConvertSurfaceFormat(src, format, src->flags);
is32bit = 1;
}
/* Determine target size */
/* _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, &dstwidth, &dstheight, &cangle, &sangle); */
/*
* Calculate target factors from sin/cos and zoom
*/
sangleinv = sangle*65536.0;
cangleinv = cangle*65536.0;
/*
* Alloc space to completely contain the rotated surface
*/
rz_dst = NULL;
if (is32bit) {
/*
* Target surface is 32bit with source RGBA/ABGR ordering
*/
rz_dst =
SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
rz_src->format->Rmask, rz_src->format->Gmask,
rz_src->format->Bmask, rz_src->format->Amask);
} else {
/*
* Target surface is 8bit
*/
rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
}
/* Check target */
if (rz_dst == NULL)
return NULL;
/* Adjust for guard rows */
rz_dst->h = dstheight;
if (colorKeyAvailable == 1){
colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
SDL_FillRect(rz_dst, NULL, colorkey );
}
/*
* Lock source surface
*/
if (SDL_MUSTLOCK(rz_src)) {
SDL_LockSurface(rz_src);
}
/*
* Check which kind of surface we have
*/
if (is32bit) {
/*
* Call the 32bit transformation routine to do the rotation (using alpha)
*/
_transformSurfaceRGBA(rz_src, rz_dst, centerx, centery,
(int) (sangleinv), (int) (cangleinv),
flipx, flipy,
smooth);
/*
* Turn on source-alpha support
*/
/* SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); */
SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
} else {
/*
* Copy palette and colorkey info
*/
for (i = 0; i < rz_src->format->palette->ncolors; i++) {
rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
}
rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
/*
* Call the 8bit transformation routine to do the rotation
*/
transformSurfaceY(rz_src, rz_dst, centerx, centery,
(int) (sangleinv), (int) (cangleinv),
flipx, flipy);
SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
}
/* copy alpha mod, color mod, and blend mode */
{
SDL_BlendMode blendMode;
Uint8 alphaMod, cr, cg, cb;
SDL_GetSurfaceAlphaMod(src, &alphaMod);
SDL_GetSurfaceBlendMode(src, &blendMode);
SDL_GetSurfaceColorMod(src, &cr, &cg, &cb);
SDL_SetSurfaceAlphaMod(rz_dst, alphaMod);
SDL_SetSurfaceBlendMode(rz_dst, blendMode);
SDL_SetSurfaceColorMod(rz_dst, cr, cg, cb);
}
/*
* Unlock source surface
*/
if (SDL_MUSTLOCK(rz_src)) {
SDL_UnlockSurface(rz_src);
}
/*
* Cleanup temp surface
*/
if (rz_src != src) {
SDL_FreeSurface(rz_src);
}
/*
* Return destination surface
*/
return (rz_dst);
}
SDL_GetRGB
#define SDL_GetRGB
Definition: SDL_dynapi_overrides.h:294
SDL_ConvertSurfaceFormat
#define SDL_ConvertSurfaceFormat
Definition: SDL_dynapi_overrides.h:468
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
SDL_UnlockSurface
#define SDL_UnlockSurface
Definition: SDL_dynapi_overrides.h:453
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDL_PixelFormat::Gmask
Uint32 Gmask
Definition: SDL_pixels.h:307
SDL_Surface::h
int h
Definition: SDL_surface.h:73
SDL_SWSURFACE
#define SDL_SWSURFACE
Definition: SDL_surface.h:52
SDL_BlendMode
SDL_BlendMode
The blend mode used in SDL_RenderCopy() and drawing operations.
Definition: SDL_blendmode.h:40
SDL_MasksToPixelFormatEnum
#define SDL_MasksToPixelFormatEnum
Definition: SDL_dynapi_overrides.h:285
GUARD_ROWS
#define GUARD_ROWS
Definition: SDL_rotate.c:77
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_PixelFormat::Bmask
Uint32 Bmask
Definition: SDL_pixels.h:308
SDL_LIL_ENDIAN
#define SDL_LIL_ENDIAN
Definition: SDL_endian.h:37
SDL_Surface::flags
Uint32 flags
Definition: SDL_surface.h:71
SDL_GetSurfaceBlendMode
#define SDL_GetSurfaceBlendMode
Definition: SDL_dynapi_overrides.h:464
blendMode
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1109
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:450
SDL_SetSurfaceColorMod
#define SDL_SetSurfaceColorMod
Definition: SDL_dynapi_overrides.h:459
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
SDL_PixelFormat::BitsPerPixel
Uint8 BitsPerPixel
Definition: SDL_pixels.h:303
transformSurfaceY
static void transformSurfaceY(SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
Definition: SDL_rotate.c:273
SDL_Palette::ncolors
int ncolors
Definition: SDL_pixels.h:290
SDL_SetColorKey
#define SDL_SetColorKey
Definition: SDL_dynapi_overrides.h:457
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_GetSurfaceAlphaMod
#define SDL_GetSurfaceAlphaMod
Definition: SDL_dynapi_overrides.h:462
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
NULL
#define NULL
Definition: begin_code.h:143
SDL_Palette::colors
SDL_Color * colors
Definition: SDL_pixels.h:291
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
SDL_LockSurface
#define SDL_LockSurface
Definition: SDL_dynapi_overrides.h:452
SDL_PixelFormat::Rmask
Uint32 Rmask
Definition: SDL_pixels.h:306
SDL_CreateRGBSurface
#define SDL_CreateRGBSurface
Definition: SDL_dynapi_overrides.h:448
SDL_GetSurfaceColorMod
#define SDL_GetSurfaceColorMod
Definition: SDL_dynapi_overrides.h:460
SDL_MUSTLOCK
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:61
SDL_SetSurfaceBlendMode
#define SDL_SetSurfaceBlendMode
Definition: SDL_dynapi_overrides.h:463
SDL_FillRect
#define SDL_FillRect
Definition: SDL_dynapi_overrides.h:470
SDL_MapRGB
#define SDL_MapRGB
Definition: SDL_dynapi_overrides.h:292
_colorkey
static Uint32 _colorkey(SDL_Surface *src)
Definition: SDL_rotate.c:88
SDL_PixelFormat::Amask
Uint32 Amask
Definition: SDL_pixels.h:309
SDL_PixelFormat::palette
SDL_Palette * palette
Definition: SDL_pixels.h:302
src
GLenum src
Definition: SDL_opengl_glext.h:1737
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
SDL_BYTEORDER
#define SDL_BYTEORDER
Definition: SDL_config_pandora.h:36
SDL_SetSurfaceAlphaMod
#define SDL_SetSurfaceAlphaMod
Definition: SDL_dynapi_overrides.h:461
SDL_RLEACCEL
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
_transformSurfaceRGBA
static void _transformSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
Definition: SDL_rotate.c:159

void SDLgfx_rotozoomSurfaceSizeTrig | ( | int | width,
|  | int | height,
|  | double | angle,
|  | int * | dstwidth,
|  | int * | dstheight,
|  | double * | cangle,
|  | double * | sangle
| ) |  |

Definition at line 109 of file SDL_rotate.c.

References MAX, SDL_ceil, SDL_cos, SDL_fabs, and SDL_sin.

Referenced by SW_RenderCopyEx().

{
double x, y, cx, cy, sx, sy;
double radangle;
int dstwidthhalf, dstheighthalf;
/*
* Determine destination width and height by rotating a centered source box
*/
radangle = angle * (M_PI / 180.0);
*sangle = SDL_sin(radangle);
*cangle = SDL_cos(radangle);
x = (double)(width / 2);
y = (double)(height / 2);
cx = *cangle * x;
cy = *cangle * y;
sx = *sangle * x;
sy = *sangle * y;
dstwidthhalf = MAX((int)
SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
dstheighthalf = MAX((int)
SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
*dstwidth = 2 * dstwidthhalf;
*dstheight = 2 * dstheighthalf;
}
SDL_ceil
#define SDL_ceil
Definition: SDL_dynapi_overrides.h:432
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1565
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_fabs
#define SDL_fabs
Definition: SDL_dynapi_overrides.h:436
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1565
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_cos
#define SDL_cos
Definition: SDL_dynapi_overrides.h:434
angle
GLfloat angle
Definition: SDL_opengl_glext.h:6097
MAX
#define MAX(a, b)
Definition: SDL_rotate.c:65
SDL_sin
#define SDL_sin
Definition: SDL_dynapi_overrides.h:441

static void transformSurfaceY | ( | SDL_Surface * | src,
|  | SDL_Surface * | dst,
|  | int | cx,
|  | int | cy,
|  | int | isin,
|  | int | icos,
|  | int | flipx,
|  | int | flipy
| ) |  |

static

Definition at line 273 of file SDL_rotate.c.

References _colorkey(), SDL_Surface::h, SDL_Surface::pitch, SDL_Surface::pixels, SDL_memset, and SDL_Surface::w.

Referenced by SDLgfx_rotateSurface().

{
int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
tColorY *pc;
int gap;
/*
* Variable setup
*/
xd = ((src->w - dst->w) << 15);
yd = ((src->h - dst->h) << 15);
ax = (cx << 16) - (icos * cx);
ay = (cy << 16) - (isin * cx);
pc = (tColorY*) dst->pixels;
gap = dst->pitch - dst->w;
/*
* Clear surface to colorkey
*/
SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
/*
* Iterate through destination surface
*/
for (y = 0; y < dst->h; y++) {
dy = cy - y;
sdx = (ax + (isin * dy)) + xd;
sdy = (ay - (icos * dy)) + yd;
for (x = 0; x < dst->w; x++) {
dx = (sdx >> 16);
dy = (sdy >> 16);
if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
if (flipx) dx = (src->w-1)-dx;
if (flipy) dy = (src->h-1)-dy;
*pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
}
sdx += icos;
sdy += isin;
pc++;
}
pc += gap;
}
}
SDL_Surface::h
int h
Definition: SDL_surface.h:73
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_Surface::w
int w
Definition: SDL_surface.h:73
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
_colorkey
static Uint32 _colorkey(SDL_Surface *src)
Definition: SDL_rotate.c:88
tColorY
Definition: SDL_rotate.c:58
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:392