#ifdef WIN32
#include <windows.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "SDL_rotozoom.h"
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
int zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int smooth)
{
int x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy, ex, ey, t1, t2, sstep;
tColorRGBA *c00, *c01, *c10, *c11;
tColorRGBA *sp, *csp, *dp;
int sgap, dgap;
if (smooth) {
sx = (int) (65536.0 * (float) (src->w - 1) / (float) dst->w);
sy = (int) (65536.0 * (float) (src->h - 1) / (float) dst->h);
} else {
sx = (int) (65536.0 * (float) src->w / (float) dst->w);
sy = (int) (65536.0 * (float) src->h / (float) dst->h);
}
if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
return (-1);
}
if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
free(sax);
return (-1);
}
csx = 0;
csax = sax;
for (x = 0; x <= dst->w; x++) {
*csax = csx;
csax++;
csx &= 0xffff;
csx += sx;
}
csy = 0;
csay = say;
for (y = 0; y <= dst->h; y++) {
*csay = csy;
csay++;
csy &= 0xffff;
csy += sy;
}
sp = csp = (tColorRGBA *) src->pixels;
dp = (tColorRGBA *) dst->pixels;
sgap = src->pitch - src->w * 4;
dgap = dst->pitch - dst->w * 4;
if (smooth) {
csay = say;
for (y = 0; y < dst->h; y++) {
c00 = csp;
c01 = csp;
c01++;
c10 = (tColorRGBA *) ((Uint8 *) csp + src->pitch);
c11 = c10;
c11++;
csax = sax;
for (x = 0; x < dst->w; x++) {
ex = (*csax & 0xffff);
ey = (*csay & 0xffff);
t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
dp->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;
dp->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;
dp->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;
dp->a = (((t2 - t1) * ey) >> 16) + t1;
csax++;
sstep = (*csax >> 16);
c00 += sstep;
c01 += sstep;
c10 += sstep;
c11 += sstep;
dp++;
}
csay++;
csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch);
dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
}
} else {
csay = say;
for (y = 0; y < dst->h; y++) {
sp = csp;
csax = sax;
for (x = 0; x < dst->w; x++) {
*dp = *sp;
csax++;
sp += (*csax >> 16);
dp++;
}
csay++;
csp = (tColorRGBA *) ((Uint8 *) csp + (*csay >> 16) * src->pitch);
dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
}
}
free(sax);
free(say);
return (0);
}
int zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst)
{
Uint32 x, y, sx, sy, *sax, *say, *csax, *csay, csx, csy;
Uint8 *sp, *dp, *csp;
int dgap;
sx = (Uint32) (65536.0 * (float) src->w / (float) dst->w);
sy = (Uint32) (65536.0 * (float) src->h / (float) dst->h);
if ((sax = (Uint32 *) malloc(dst->w * sizeof(Uint32))) == NULL) {
return (-1);
}
if ((say = (Uint32 *) malloc(dst->h * sizeof(Uint32))) == NULL) {
if (sax != NULL) {
free(sax);
}
return (-1);
}
csx = 0;
csax = sax;
for (x = 0; x < dst->w; x++) {
csx += sx;
*csax = (csx >> 16);
csx &= 0xffff;
csax++;
}
csy = 0;
csay = say;
for (y = 0; y < dst->h; y++) {
csy += sy;
*csay = (csy >> 16);
csy &= 0xffff;
csay++;
}
csx = 0;
csax = sax;
for (x = 0; x < dst->w; x++) {
csx += (*csax);
csax++;
}
csy = 0;
csay = say;
for (y = 0; y < dst->h; y++) {
csy += (*csay);
csay++;
}
sp = csp = (Uint8 *) src->pixels;
dp = (Uint8 *) dst->pixels;
dgap = dst->pitch - dst->w;
csay = say;
for (y = 0; y < dst->h; y++) {
csax = sax;
sp = csp;
for (x = 0; x < dst->w; x++) {
*dp = *sp;
sp += (*csax);
csax++;
dp++;
}
csp += ((*csay) * src->pitch);
csay++;
dp += dgap;
}
free(sax);
free(say);
return (0);
}
void transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int smooth)
{
int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
tColorRGBA c00, c01, c10, c11;
tColorRGBA *pc, *sp;
int gap;
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 = dst->pixels;
gap = dst->pitch - dst->w * 4;
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 ((dx >= -1) && (dy >= -1) && (dx < src->w) && (dy < src->h)) {
if ((dx >= 0) && (dy >= 0) && (dx < sw) && (dy < sh)) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
sp += dx;
c00 = *sp;
sp += 1;
c01 = *sp;
sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
sp -= 1;
c10 = *sp;
sp += 1;
c11 = *sp;
} else if ((dx == sw) && (dy == sh)) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
sp += dx;
c00 = *sp;
c01 = *sp;
c10 = *sp;
c11 = *sp;
} else if ((dx == -1) && (dy == -1)) {
sp = (tColorRGBA *) (src->pixels);
c00 = *sp;
c01 = *sp;
c10 = *sp;
c11 = *sp;
} else if ((dx == -1) && (dy == sh)) {
sp = (tColorRGBA *) (src->pixels);
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
c00 = *sp;
c01 = *sp;
c10 = *sp;
c11 = *sp;
} else if ((dx == sw) && (dy == -1)) {
sp = (tColorRGBA *) (src->pixels);
sp += dx;
c00 = *sp;
c01 = *sp;
c10 = *sp;
c11 = *sp;
} else if (dx == -1) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
c00 = *sp;
c01 = *sp;
c10 = *sp;
sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
c11 = *sp;
} else if (dy == -1) {
sp = (tColorRGBA *) (src->pixels);
sp += dx;
c00 = *sp;
c01 = *sp;
c10 = *sp;
sp += 1;
c11 = *sp;
} else if (dx == sw) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
sp += dx;
c00 = *sp;
c01 = *sp;
sp = (tColorRGBA *) ((Uint8 *) sp + src->pitch);
c10 = *sp;
c11 = *sp;
} else if (dy == sh) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
sp += dx;
c00 = *sp;
sp += 1;
c01 = *sp;
c10 = *sp;
c11 = *sp;
}
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 = (short) (sdx >> 16);
dy = (short) (sdy >> 16);
if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
sp += dx;
*pc = *sp;
}
sdx += icos;
sdy += isin;
pc++;
}
pc = (tColorRGBA *) ((Uint8 *) pc + gap);
}
}
}
void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos)
{
int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh;
tColorY *pc, *sp;
int gap;
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 = dst->pixels;
gap = dst->pitch - dst->w;
memset(pc, (unsigned char) (src->format->colorkey & 0xff), dst->pitch * dst->h);
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 = (short) (sdx >> 16);
dy = (short) (sdy >> 16);
if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
sp = (tColorY *) (src->pixels);
sp += (src->pitch * dy + dx);
*pc = *sp;
}
sdx += icos;
sdy += isin;
pc++;
}
pc += gap;
}
}
#define VALUE_LIMIT 0.001
void rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight,
double *canglezoom, double *sanglezoom)
{
double x, y, cx, cy, sx, sy;
double radangle;
int dstwidthhalf, dstheighthalf;
radangle = angle * (M_PI / 180.0);
*sanglezoom = sin(radangle);
*canglezoom = cos(radangle);
*sanglezoom *= zoom;
*canglezoom *= zoom;
x = width / 2;
y = height / 2;
cx = *canglezoom * x;
cy = *canglezoom * y;
sx = *sanglezoom * x;
sy = *sanglezoom * y;
dstwidthhalf = MAX((int)
ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);
dstheighthalf = MAX((int)
ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);
*dstwidth = 2 * dstwidthhalf;
*dstheight = 2 * dstheighthalf;
}
void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight)
{
double dummy_sanglezoom, dummy_canglezoom;
rotozoomSurfaceSizeTrig(width, height, angle, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
}
SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth)
{
SDL_Surface *rz_src;
SDL_Surface *rz_dst;
double zoominv;
double sanglezoom, canglezoom, sanglezoominv, canglezoominv;
int dstwidthhalf, dstwidth, dstheighthalf, dstheight;
int is32bit;
int i, src_converted;
smooth = 1;
if (src == NULL)
return (NULL);
is32bit = (src->format->BitsPerPixel == 32);
if ( (src->format->BitsPerPixel == 8)) {
rz_src = src;
src_converted = 0;
} else {
rz_src =
SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_BlitSurface(src, NULL, rz_src, NULL);
src_converted = 1;
is32bit = 1;
}
if (zoom < VALUE_LIMIT) {
zoom = VALUE_LIMIT;
}
zoominv = 65536.0 / (zoom * zoom);
if (fabs(angle) > VALUE_LIMIT) {
rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoom, &dstwidth, &dstheight, &canglezoom, &sanglezoom);
sanglezoominv = sanglezoom;
canglezoominv = canglezoom;
sanglezoominv *= zoominv;
canglezoominv *= zoominv;
dstwidthhalf = dstwidth / 2;
dstheighthalf = dstheight / 2;
rz_dst = NULL;
if (is32bit) {
rz_dst =
SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
rz_src->format->Rmask, rz_src->format->Gmask,
rz_src->format->Bmask, rz_src->format->Amask);
} else {
rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
}
SDL_LockSurface(rz_src);
if (is32bit) {
transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
(int) (sanglezoominv), (int) (canglezoominv), smooth);
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
} else {
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;
transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
(int) (sanglezoominv), (int) (canglezoominv));
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
}
SDL_UnlockSurface(rz_src);
} else {
zoomSurfaceSize(rz_src->w, rz_src->h, zoom, zoom, &dstwidth, &dstheight);
rz_dst = NULL;
if (is32bit) {
rz_dst =
SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
rz_src->format->Rmask, rz_src->format->Gmask,
rz_src->format->Bmask, rz_src->format->Amask);
} else {
rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
}
SDL_LockSurface(rz_src);
if (is32bit) {
zoomSurfaceRGBA(rz_src, rz_dst, smooth);
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
} else {
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;
zoomSurfaceY(rz_src, rz_dst);
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
}
SDL_UnlockSurface(rz_src);
}
if (src_converted) {
SDL_FreeSurface(rz_src);
}
return (rz_dst);
}
#define VALUE_LIMIT 0.001
void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight)
{
if (zoomx < VALUE_LIMIT) {
zoomx = VALUE_LIMIT;
}
if (zoomy < VALUE_LIMIT) {
zoomy = VALUE_LIMIT;
}
*dstwidth = (int) ((double) width * zoomx);
*dstheight = (int) ((double) height * zoomy);
if (*dstwidth < 1) {
*dstwidth = 1;
}
if (*dstheight < 1) {
*dstheight = 1;
}
}
SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth)
{
SDL_Surface *rz_src;
SDL_Surface *rz_dst;
int dstwidth, dstheight;
int is32bit;
int i, src_converted;
if (src == NULL)
return (NULL);
is32bit = (src->format->BitsPerPixel == 32);
if ((is32bit) || (src->format->BitsPerPixel == 8)) {
rz_src = src;
src_converted = 0;
} else {
rz_src =
SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_BlitSurface(src, NULL, rz_src, NULL);
src_converted = 1;
is32bit = 1;
}
zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
rz_dst = NULL;
if (is32bit) {
rz_dst =
SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,
rz_src->format->Rmask, rz_src->format->Gmask,
rz_src->format->Bmask, rz_src->format->Amask);
} else {
rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);
}
SDL_LockSurface(rz_src);
if (is32bit) {
zoomSurfaceRGBA(rz_src, rz_dst, smooth);
SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
} else {
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;
zoomSurfaceY(rz_src, rz_dst);
SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);
}
SDL_UnlockSurface(rz_src);
if (src_converted) {
SDL_FreeSurface(rz_src);
}
return (rz_dst);
}