1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708 |
- #ifdef WIN32
- #include <windows.h>
- #endif
- #include "SDL_rotozoom.h"
- typedef struct tColorRGBA {
- Uint8 r;
- Uint8 g;
- Uint8 b;
- Uint8 a;
- } tColorRGBA;
- typedef struct tColorY {
- Uint8 y;
- } tColorY;
- #define MAX(a, b) (((a) > (b)) ? (a) : (b))
- #define GUARD_ROWS (2)
- #define VALUE_LIMIT 0.001
- Uint32 _colorkey(SDL_Surface *src) {
- Uint32 key = 0;
- #if (SDL_MINOR_VERSION == 3)
- SDL_GetColorKey(src, &key);
- #else
- if (src) {
- key = src->format->colorkey;
- }
- #endif
- return key;
- }
- int _shrinkSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int factorx, int factory) {
- int x, y, dx, dy, sgap, dgap, ra, ga, ba, aa;
- int n_average;
- tColorRGBA *sp, *osp, *oosp;
- tColorRGBA *dp;
-
-
- n_average = factorx * factory;
-
- sp = (tColorRGBA *) src->pixels;
- sgap = src->pitch - src->w * 4;
- dp = (tColorRGBA *) dst->pixels;
- dgap = dst->pitch - dst->w * 4;
- for (y = 0; y < dst->h; y++) {
- osp = sp;
- for (x = 0; x < dst->w; x++) {
-
- oosp = sp;
- ra = ga = ba = aa = 0;
- for (dy = 0; dy < factory; dy++) {
- for (dx = 0; dx < factorx; dx++) {
- ra += sp->r;
- ga += sp->g;
- ba += sp->b;
- aa += sp->a;
- sp++;
- }
-
- sp = (tColorRGBA *) ((Uint8 *) sp + (src->pitch - 4 * factorx));
- }
-
-
- sp = (tColorRGBA *) ((Uint8 *) oosp + 4 * factorx);
-
- dp->r = ra / n_average;
- dp->g = ga / n_average;
- dp->b = ba / n_average;
- dp->a = aa / n_average;
-
- dp++;
- }
-
-
- sp = (tColorRGBA *) ((Uint8 *) osp + src->pitch * factory);
-
- dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
- }
-
- return (0);
- }
- int _shrinkSurfaceY(SDL_Surface *src, SDL_Surface *dst, int factorx, int factory) {
- int x, y, dx, dy, sgap, dgap, a;
- int n_average;
- Uint8 *sp, *osp, *oosp;
- Uint8 *dp;
-
-
- n_average = factorx * factory;
-
- sp = (Uint8 *) src->pixels;
- sgap = src->pitch - src->w;
- dp = (Uint8 *) dst->pixels;
- dgap = dst->pitch - dst->w;
- for (y = 0; y < dst->h; y++) {
- osp = sp;
- for (x = 0; x < dst->w; x++) {
-
- oosp = sp;
- a = 0;
- for (dy = 0; dy < factory; dy++) {
- for (dx = 0; dx < factorx; dx++) {
- a += (*sp);
-
- sp++;
- }
-
-
- sp = (Uint8 *) ((Uint8 *) sp + (src->pitch - factorx));
- }
-
-
- sp = (Uint8 *) ((Uint8 *) oosp + factorx);
-
- *dp = a / n_average;
-
- dp++;
- }
-
-
- sp = (Uint8 *) ((Uint8 *) osp + src->pitch * factory);
-
- dp = (Uint8 *) ((Uint8 *) dp + dgap);
- }
-
- return (0);
- }
- int _zoomSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int flipx, int flipy, int smooth) {
- int x, y, sx, sy, ssx, ssy, *sax, *say, *csax, *csay, *salast, csx, csy, ex, ey, cx, cy, sstep, sstepx, sstepy;
- tColorRGBA *c00, *c01, *c10, *c11;
- tColorRGBA *sp, *csp, *dp;
- int spixelgap, spixelw, spixelh, dgap, t1, t2;
-
- 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);
- }
-
- spixelw = (src->w - 1);
- spixelh = (src->h - 1);
- if (smooth) {
- sx = (int) (65536.0 * (float) spixelw / (float) (dst->w - 1));
- sy = (int) (65536.0 * (float) spixelh / (float) (dst->h - 1));
- } else {
- sx = (int) (65536.0 * (float) (src->w) / (float) (dst->w));
- sy = (int) (65536.0 * (float) (src->h) / (float) (dst->h));
- }
-
- ssx = (src->w << 16) - 1;
- ssy = (src->h << 16) - 1;
-
- csx = 0;
- csax = sax;
- for (x = 0; x <= dst->w; x++) {
- *csax = csx;
- csax++;
- csx += sx;
-
- if (csx > ssx) {
- csx = ssx;
- }
- }
-
- csy = 0;
- csay = say;
- for (y = 0; y <= dst->h; y++) {
- *csay = csy;
- csay++;
- csy += sy;
-
- if (csy > ssy) {
- csy = ssy;
- }
- }
- sp = (tColorRGBA *) src->pixels;
- dp = (tColorRGBA *) dst->pixels;
- dgap = dst->pitch - dst->w * 4;
- spixelgap = src->pitch / 4;
- if (flipx) sp += spixelw;
- if (flipy) sp += (spixelgap * spixelh);
-
- if (smooth) {
-
- csay = say;
- for (y = 0; y < dst->h; y++) {
- csp = sp;
- csax = sax;
- for (x = 0; x < dst->w; x++) {
-
- ex = (*csax & 0xffff);
- ey = (*csay & 0xffff);
- cx = (*csax >> 16);
- cy = (*csay >> 16);
- sstepx = cx < spixelw;
- sstepy = cy < spixelh;
- c00 = sp;
- c01 = sp;
- c10 = sp;
- if (sstepy) {
- if (flipy) {
- c10 -= spixelgap;
- } else {
- c10 += spixelgap;
- }
- }
- c11 = c10;
- if (sstepx) {
- if (flipx) {
- c01--;
- c11--;
- } else {
- c01++;
- c11++;
- }
- }
-
- 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;
-
- salast = csax;
- csax++;
- sstep = (*csax >> 16) - (*salast >> 16);
- if (flipx) {
- sp -= sstep;
- } else {
- sp += sstep;
- }
-
- dp++;
- }
-
- salast = csay;
- csay++;
- sstep = (*csay >> 16) - (*salast >> 16);
- sstep *= spixelgap;
- if (flipy) {
- sp = csp - sstep;
- } else {
- sp = csp + sstep;
- }
-
- dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
- }
- } else {
-
- csay = say;
- for (y = 0; y < dst->h; y++) {
- csp = sp;
- csax = sax;
- for (x = 0; x < dst->w; x++) {
-
- *dp = *sp;
-
- salast = csax;
- csax++;
- sstep = (*csax >> 16) - (*salast >> 16);
- if (flipx) sstep = -sstep;
- sp += sstep;
-
- dp++;
- }
-
- salast = csay;
- csay++;
- sstep = (*csay >> 16) - (*salast >> 16);
- sstep *= spixelgap;
- if (flipy) sstep = -sstep;
- sp = csp + sstep;
-
- dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
- }
- }
-
- free(sax);
- free(say);
- return (0);
- }
- int _zoomSurfaceY(SDL_Surface *src, SDL_Surface *dst, int flipx, int flipy) {
- int x, y;
- Uint32 *sax, *say, *csax, *csay;
- int csx, csy;
- Uint8 *sp, *dp, *csp;
- int dgap;
-
- if ((sax = (Uint32 *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
- return (-1);
- }
- if ((say = (Uint32 *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
- free(sax);
- return (-1);
- }
-
- sp = csp = (Uint8 *) src->pixels;
- dp = (Uint8 *) dst->pixels;
- dgap = dst->pitch - dst->w;
- if (flipx) csp += (src->w - 1);
- if (flipy) csp = ((Uint8 *) csp + src->pitch * (src->h - 1));
-
- csx = 0;
- csax = sax;
- for (x = 0; x < dst->w; x++) {
- csx += src->w;
- *csax = 0;
- while (csx >= dst->w) {
- csx -= dst->w;
- (*csax)++;
- }
- (*csax) = (*csax) * (flipx ? -1 : 1);
- csax++;
- }
- csy = 0;
- csay = say;
- for (y = 0; y < dst->h; y++) {
- csy += src->h;
- *csay = 0;
- while (csy >= dst->h) {
- csy -= dst->h;
- (*csay)++;
- }
- (*csay) = (*csay) * (flipy ? -1 : 1);
- csay++;
- }
-
- 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 flipx, int flipy,
- int smooth) {
- 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;
-
- 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;
-
- 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 ((dx > -1) && (dy > -1) && (dx < (src->w - 1)) && (dy < (src->h - 1))) {
- sp = (tColorRGBA *) src->pixels;;
- sp += ((src->pitch / 4) * dy);
- sp += 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;
- }
-
- 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 (flipx) dx = (src->w - 1) - dx;
- if (flipy) dy = (src->h - 1) - dy;
- 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 flipx, int flipy) {
- 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 = (tColorY *) dst->pixels;
- gap = dst->pitch - dst->w;
-
- memset(pc, (int) (_colorkey(src) & 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 (flipx) dx = (src->w - 1) - dx;
- if (flipy) dy = (src->h - 1) - dy;
- 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;
- }
- }
- SDL_Surface *rotateSurface90Degrees(SDL_Surface *src, int numClockwiseTurns) {
- int row, col, newWidth, newHeight;
- int bpp, src_ipr, dst_ipr;
- SDL_Surface *dst;
- Uint32 *srcBuf;
- Uint32 *dstBuf;
-
- if (!src || src->format->BitsPerPixel != 32) { return NULL; }
-
- while (numClockwiseTurns < 0) { numClockwiseTurns += 4; }
- numClockwiseTurns = (numClockwiseTurns % 4);
-
- newWidth = (numClockwiseTurns % 2) ? (src->h) : (src->w);
- newHeight = (numClockwiseTurns % 2) ? (src->w) : (src->h);
- dst = SDL_CreateRGBSurface(src->flags, newWidth, newHeight, src->format->BitsPerPixel,
- src->format->Rmask,
- src->format->Gmask,
- src->format->Bmask,
- src->format->Amask);
- if (!dst) {
- return NULL;
- }
- if (SDL_MUSTLOCK(dst)) {
- SDL_LockSurface(dst);
- }
- if (SDL_MUSTLOCK(dst)) {
- SDL_LockSurface(dst);
- }
-
- bpp = src->format->BitsPerPixel / 8;
- src_ipr = src->pitch / bpp;
- dst_ipr = dst->pitch / bpp;
- switch (numClockwiseTurns) {
- case 0:
- {
-
- if (src->pitch == dst->pitch) {
-
- memcpy(dst->pixels, src->pixels, (src->h * src->pitch));
- } else {
-
- srcBuf = (Uint32 *) (src->pixels);
- dstBuf = (Uint32 *) (dst->pixels);
- for (row = 0; row < src->h; row++) {
- memcpy(dstBuf, srcBuf, dst->w * bpp);
- srcBuf += src_ipr;
- dstBuf += dst_ipr;
- }
- }
- }
- break;
-
- case 1:
- {
- for (row = 0; row < src->h; ++row) {
- srcBuf = (Uint32 *) (src->pixels) + (row * src_ipr);
- dstBuf = (Uint32 *) (dst->pixels) + (dst->w - row - 1);
- for (col = 0; col < src->w; ++col) {
- *dstBuf = *srcBuf;
- ++srcBuf;
- dstBuf += dst_ipr;
- }
-
- }
-
- }
- break;
- case 2:
- {
- for (row = 0; row < src->h; ++row) {
- srcBuf = (Uint32 *) (src->pixels) + (row * src_ipr);
- dstBuf = (Uint32 *) (dst->pixels) + ((dst->h - row - 1) * dst_ipr) + (dst->w - 1);
- for (col = 0; col < src->w; ++col) {
- *dstBuf = *srcBuf;
- ++srcBuf;
- --dstBuf;
- }
- }
- }
- break;
- case 3: {
- for (row = 0; row < src->h; ++row) {
- srcBuf = (Uint32 *) (src->pixels) + (row * src_ipr);
- dstBuf = (Uint32 *) (dst->pixels) + row + ((dst->h - 1) * dst_ipr);
- for (col = 0; col < src->w; ++col) {
- *dstBuf = *srcBuf;
- ++srcBuf;
- dstBuf -= dst_ipr;
- }
- }
- }
- break;
- }
-
- if (SDL_MUSTLOCK(src)) {
- SDL_UnlockSurface(src);
- }
- if (SDL_MUSTLOCK(dst)) {
- SDL_UnlockSurface(dst);
- }
- return dst;
- }
- void _rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoomx, double zoomy,
- 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 *= zoomx;
- *canglezoom *= zoomx;
- x = (double) (width / 2);
- y = (double) (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
- rotozoomSurfaceSizeXY(int width, int height, double angle, double zoomx, double zoomy, int *dstwidth, int *dstheight) {
- double dummy_sanglezoom, dummy_canglezoom;
- _rotozoomSurfaceSizeTrig(width, height, angle, zoomx, zoomy, dstwidth, dstheight, &dummy_sanglezoom,
- &dummy_canglezoom);
- }
- void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight) {
- double dummy_sanglezoom, dummy_canglezoom;
- _rotozoomSurfaceSizeTrig(width, height, angle, zoom, zoom, dstwidth, dstheight, &dummy_sanglezoom,
- &dummy_canglezoom);
- }
- SDL_Surface *rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth) {
- return rotozoomSurfaceXY(src, angle, zoom, zoom, smooth);
- }
- SDL_Surface *rotozoomSurfaceXY(SDL_Surface *src, double angle, double zoomx, double zoomy, 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;
- int flipx, flipy;
- Uint8 r, g, b;
- Uint32 colorkey = 0;
- int colorKeyAvailable = 0;
-
- if (src == NULL)
- return (NULL);
- if (src->flags & SDL_SRCCOLORKEY) {
- colorkey = _colorkey(src);
- SDL_GetRGB(colorkey, src->format, &r, &g, &b);
- colorKeyAvailable = 1;
- }
-
- 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,
- #if SDL_BYTEORDER == SDL_LIL_ENDIAN
- 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
- #else
- 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
- #endif
- );
- if (colorKeyAvailable)
- SDL_SetColorKey(src, 0, 0);
- SDL_BlitSurface(src, NULL, rz_src, NULL);
- if (colorKeyAvailable)
- SDL_SetColorKey(src, SDL_SRCCOLORKEY, colorkey);
- src_converted = 1;
- is32bit = 1;
- }
-
- flipx = (zoomx < 0.0);
- if (flipx) zoomx = -zoomx;
- flipy = (zoomy < 0.0);
- if (flipy) zoomy = -zoomy;
- if (zoomx < VALUE_LIMIT) zoomx = VALUE_LIMIT;
- if (zoomy < VALUE_LIMIT) zoomy = VALUE_LIMIT;
- zoominv = 65536.0 / (zoomx * zoomx);
-
- if (fabs(angle) > VALUE_LIMIT) {
-
-
-
- _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoomx, zoomy, &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 + GUARD_ROWS, 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 + GUARD_ROWS, 8, 0, 0, 0, 0);
- }
-
- if (rz_dst == NULL)
- return NULL;
-
- rz_dst->h = dstheight;
- if (colorKeyAvailable == 1) {
- colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
- SDL_FillRect(rz_dst, NULL, colorkey);
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_LockSurface(rz_src);
- }
-
- if (is32bit) {
-
- _transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
- (int) (sanglezoominv), (int) (canglezoominv),
- flipx, flipy,
- smooth);
-
- SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
- SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- } 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),
- flipx, flipy);
- SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_UnlockSurface(rz_src);
- }
- } else {
-
-
-
- zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
-
- rz_dst = NULL;
- if (is32bit) {
-
- 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 {
-
- rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
- }
-
- if (rz_dst == NULL)
- return NULL;
-
- rz_dst->h = dstheight;
- if (colorKeyAvailable == 1) {
- colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
- SDL_FillRect(rz_dst, NULL, colorkey);
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_LockSurface(rz_src);
- }
-
- if (is32bit) {
-
- _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, smooth);
-
- SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
- SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- } 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, flipx, flipy);
- SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_UnlockSurface(rz_src);
- }
- }
-
- if (src_converted) {
- SDL_FreeSurface(rz_src);
- }
-
- return (rz_dst);
- }
- void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight) {
-
- int flipx, flipy;
- flipx = (zoomx < 0.0);
- if (flipx) zoomx = -zoomx;
- flipy = (zoomy < 0.0);
- if (flipy) zoomy = -zoomy;
-
- if (zoomx < VALUE_LIMIT) {
- zoomx = VALUE_LIMIT;
- }
- if (zoomy < VALUE_LIMIT) {
- zoomy = VALUE_LIMIT;
- }
-
- *dstwidth = (int) floor(((double) width * zoomx) + 0.5);
- *dstheight = (int) floor(((double) height * zoomy) + 0.5);
- 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;
- int flipx, flipy;
-
- 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,
- #if SDL_BYTEORDER == SDL_LIL_ENDIAN
- 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
- #else
- 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
- #endif
- );
- if (rz_src == NULL) {
- return NULL;
- }
- SDL_BlitSurface(src, NULL, rz_src, NULL);
- src_converted = 1;
- is32bit = 1;
- }
- flipx = (zoomx < 0.0);
- if (flipx) zoomx = -zoomx;
- flipy = (zoomy < 0.0);
- if (flipy) zoomy = -zoomy;
-
- zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
-
- rz_dst = NULL;
- if (is32bit) {
-
- 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 {
-
- rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
- }
-
- if (rz_dst == NULL) {
-
- if (src_converted) {
- SDL_FreeSurface(rz_src);
- }
- return NULL;
- }
-
- rz_dst->h = dstheight;
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_LockSurface(rz_src);
- }
-
- if (is32bit) {
-
- _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, 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, flipx, flipy);
- SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_UnlockSurface(rz_src);
- }
-
- if (src_converted) {
- SDL_FreeSurface(rz_src);
- }
-
- return (rz_dst);
- }
- SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory) {
- int result;
- SDL_Surface *rz_src;
- SDL_Surface *rz_dst = NULL;
- int dstwidth, dstheight;
- int is32bit;
- int i, src_converted;
- int haveError = 0;
-
- 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,
- #if SDL_BYTEORDER == SDL_LIL_ENDIAN
- 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
- #else
- 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
- #endif
- );
- if (rz_src == NULL) {
- haveError = 1;
- goto exitShrinkSurface;
- }
- SDL_BlitSurface(src, NULL, rz_src, NULL);
- src_converted = 1;
- is32bit = 1;
- }
-
- if (SDL_MUSTLOCK(rz_src)) {
- if (SDL_LockSurface(rz_src) < 0) {
- haveError = 1;
- goto exitShrinkSurface;
- }
- }
-
- dstwidth = rz_src->w / factorx;
- while (dstwidth * factorx > rz_src->w) { dstwidth--; }
- dstheight = rz_src->h / factory;
- while (dstheight * factory > rz_src->h) { dstheight--; }
-
- if (is32bit == 1) {
-
- 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 {
-
- rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
- }
-
- if (rz_dst == NULL) {
- haveError = 1;
- goto exitShrinkSurface;
- }
-
- rz_dst->h = dstheight;
-
- if (is32bit == 1) {
-
- result = _shrinkSurfaceRGBA(rz_src, rz_dst, factorx, factory);
- if ((result != 0) || (rz_dst == NULL)) {
- haveError = 1;
- goto exitShrinkSurface;
- }
-
- result = SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
- if (result != 0) {
- haveError = 1;
- goto exitShrinkSurface;
- }
- } 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;
-
- result = _shrinkSurfaceY(rz_src, rz_dst, factorx, factory);
- if (result != 0) {
- haveError = 1;
- goto exitShrinkSurface;
- }
-
- result = SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
- if (result != 0) {
- haveError = 1;
- goto exitShrinkSurface;
- }
- }
- exitShrinkSurface:
- if (rz_src != NULL) {
-
- if (SDL_MUSTLOCK(rz_src)) {
- SDL_UnlockSurface(rz_src);
- }
-
- if (src_converted == 1) {
- SDL_FreeSurface(rz_src);
- }
- }
-
- if (haveError == 1) {
- if (rz_dst != NULL) {
- SDL_FreeSurface(rz_dst);
- }
- rz_dst = NULL;
- }
-
- return (rz_dst);
- }
|