SDL_rotozoom.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. /*
  2. SDL_rotozoom.c: rotozoomer, zoomer and shrinker for 32bit or 8bit surfaces
  3. Copyright (C) 2001-2012 Andreas Schiffler
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. Andreas Schiffler -- aschiffler at ferzkopp dot net
  19. */
  20. #ifdef WIN32
  21. #include <windows.h>
  22. #endif
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "SDL_rotozoom.h"
  26. /* ---- Internally used structures */
  27. /*!
  28. \brief A 32 bit RGBA pixel.
  29. */
  30. typedef struct tColorRGBA {
  31. Uint8 r;
  32. Uint8 g;
  33. Uint8 b;
  34. Uint8 a;
  35. } tColorRGBA;
  36. /*!
  37. \brief A 8bit Y/palette pixel.
  38. */
  39. typedef struct tColorY {
  40. Uint8 y;
  41. } tColorY;
  42. /*!
  43. \brief Returns maximum of two numbers a and b.
  44. */
  45. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  46. /*!
  47. \brief Number of guard rows added to destination surfaces.
  48. This is a simple but effective workaround for observed issues.
  49. These rows allocate extra memory and are then hidden from the surface.
  50. Rows are added to the end of destination surfaces when they are allocated.
  51. This catches any potential overflows which seem to happen with
  52. just the right src image dimensions and scale/rotation and can lead
  53. to a situation where the program can segfault.
  54. */
  55. #define GUARD_ROWS (2)
  56. /*!
  57. \brief Lower limit of absolute zoom factor or rotation degrees.
  58. */
  59. #define VALUE_LIMIT 0.001
  60. /*!
  61. \brief Returns colorkey info for a surface
  62. */
  63. Uint32 _colorkey(SDL_Surface *src)
  64. {
  65. Uint32 key = 0;
  66. #if (SDL_MINOR_VERSION == 3)
  67. SDL_GetColorKey(src, &key);
  68. #else
  69. if (src)
  70. {
  71. key = src->format->colorkey;
  72. }
  73. #endif
  74. return key;
  75. }
  76. /*!
  77. \brief Internal 32 bit integer-factor averaging Shrinker.
  78. Shrinks 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
  79. Averages color and alpha values values of src pixels to calculate dst pixels.
  80. Assumes src and dst surfaces are of 32 bit depth.
  81. Assumes dst surface was allocated with the correct dimensions.
  82. \param src The surface to shrink (input).
  83. \param dst The shrunken surface (output).
  84. \param factorx The horizontal shrinking ratio.
  85. \param factory The vertical shrinking ratio.
  86. \return 0 for success or -1 for error.
  87. */
  88. int _shrinkSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory)
  89. {
  90. int x, y, dx, dy, sgap, dgap, ra, ga, ba, aa;
  91. int n_average;
  92. tColorRGBA *sp, *osp, *oosp;
  93. tColorRGBA *dp;
  94. /*
  95. * Averaging integer shrink
  96. */
  97. /* Precalculate division factor */
  98. n_average = factorx*factory;
  99. /*
  100. * Scan destination
  101. */
  102. sp = (tColorRGBA *) src->pixels;
  103. sgap = src->pitch - src->w * 4;
  104. dp = (tColorRGBA *) dst->pixels;
  105. dgap = dst->pitch - dst->w * 4;
  106. for (y = 0; y < dst->h; y++) {
  107. osp=sp;
  108. for (x = 0; x < dst->w; x++) {
  109. /* Trace out source box and accumulate */
  110. oosp=sp;
  111. ra=ga=ba=aa=0;
  112. for (dy=0; dy < factory; dy++) {
  113. for (dx=0; dx < factorx; dx++) {
  114. ra += sp->r;
  115. ga += sp->g;
  116. ba += sp->b;
  117. aa += sp->a;
  118. sp++;
  119. }
  120. /* src dx loop */
  121. sp = (tColorRGBA *)((Uint8*)sp + (src->pitch - 4*factorx)); // next y
  122. }
  123. /* src dy loop */
  124. /* next box-x */
  125. sp = (tColorRGBA *)((Uint8*)oosp + 4*factorx);
  126. /* Store result in destination */
  127. dp->r = ra/n_average;
  128. dp->g = ga/n_average;
  129. dp->b = ba/n_average;
  130. dp->a = aa/n_average;
  131. /*
  132. * Advance destination pointer
  133. */
  134. dp++;
  135. }
  136. /* dst x loop */
  137. /* next box-y */
  138. sp = (tColorRGBA *)((Uint8*)osp + src->pitch*factory);
  139. /*
  140. * Advance destination pointers
  141. */
  142. dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
  143. }
  144. /* dst y loop */
  145. return (0);
  146. }
  147. /*!
  148. \brief Internal 8 bit integer-factor averaging shrinker.
  149. Shrinks 8bit Y 'src' surface to 'dst' surface.
  150. Averages color (brightness) values values of src pixels to calculate dst pixels.
  151. Assumes src and dst surfaces are of 8 bit depth.
  152. Assumes dst surface was allocated with the correct dimensions.
  153. \param src The surface to shrink (input).
  154. \param dst The shrunken surface (output).
  155. \param factorx The horizontal shrinking ratio.
  156. \param factory The vertical shrinking ratio.
  157. \return 0 for success or -1 for error.
  158. */
  159. int _shrinkSurfaceY(SDL_Surface * src, SDL_Surface * dst, int factorx, int factory)
  160. {
  161. int x, y, dx, dy, sgap, dgap, a;
  162. int n_average;
  163. Uint8 *sp, *osp, *oosp;
  164. Uint8 *dp;
  165. /*
  166. * Averaging integer shrink
  167. */
  168. /* Precalculate division factor */
  169. n_average = factorx*factory;
  170. /*
  171. * Scan destination
  172. */
  173. sp = (Uint8 *) src->pixels;
  174. sgap = src->pitch - src->w;
  175. dp = (Uint8 *) dst->pixels;
  176. dgap = dst->pitch - dst->w;
  177. for (y = 0; y < dst->h; y++) {
  178. osp=sp;
  179. for (x = 0; x < dst->w; x++) {
  180. /* Trace out source box and accumulate */
  181. oosp=sp;
  182. a=0;
  183. for (dy=0; dy < factory; dy++) {
  184. for (dx=0; dx < factorx; dx++) {
  185. a += (*sp);
  186. /* next x */
  187. sp++;
  188. }
  189. /* end src dx loop */
  190. /* next y */
  191. sp = (Uint8 *)((Uint8*)sp + (src->pitch - factorx));
  192. }
  193. /* end src dy loop */
  194. /* next box-x */
  195. sp = (Uint8 *)((Uint8*)oosp + factorx);
  196. /* Store result in destination */
  197. *dp = a/n_average;
  198. /*
  199. * Advance destination pointer
  200. */
  201. dp++;
  202. }
  203. /* end dst x loop */
  204. /* next box-y */
  205. sp = (Uint8 *)((Uint8*)osp + src->pitch*factory);
  206. /*
  207. * Advance destination pointers
  208. */
  209. dp = (Uint8 *)((Uint8 *)dp + dgap);
  210. }
  211. /* end dst y loop */
  212. return (0);
  213. }
  214. /*!
  215. \brief Internal 32 bit Zoomer with optional anti-aliasing by bilinear interpolation.
  216. Zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
  217. Assumes src and dst surfaces are of 32 bit depth.
  218. Assumes dst surface was allocated with the correct dimensions.
  219. \param src The surface to zoom (input).
  220. \param dst The zoomed surface (output).
  221. \param flipx Flag indicating if the image should be horizontally flipped.
  222. \param flipy Flag indicating if the image should be vertically flipped.
  223. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  224. \return 0 for success or -1 for error.
  225. */
  226. int _zoomSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy, int smooth)
  227. {
  228. int x, y, sx, sy, ssx, ssy, *sax, *say, *csax, *csay, *salast, csx, csy, ex, ey, cx, cy, sstep, sstepx, sstepy;
  229. tColorRGBA *c00, *c01, *c10, *c11;
  230. tColorRGBA *sp, *csp, *dp;
  231. int spixelgap, spixelw, spixelh, dgap, t1, t2;
  232. /*
  233. * Allocate memory for row/column increments
  234. */
  235. if ((sax = (int *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
  236. return (-1);
  237. }
  238. if ((say = (int *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
  239. free(sax);
  240. return (-1);
  241. }
  242. /*
  243. * Precalculate row increments
  244. */
  245. spixelw = (src->w - 1);
  246. spixelh = (src->h - 1);
  247. if (smooth) {
  248. sx = (int) (65536.0 * (float) spixelw / (float) (dst->w - 1));
  249. sy = (int) (65536.0 * (float) spixelh / (float) (dst->h - 1));
  250. } else {
  251. sx = (int) (65536.0 * (float) (src->w) / (float) (dst->w));
  252. sy = (int) (65536.0 * (float) (src->h) / (float) (dst->h));
  253. }
  254. /* Maximum scaled source size */
  255. ssx = (src->w << 16) - 1;
  256. ssy = (src->h << 16) - 1;
  257. /* Precalculate horizontal row increments */
  258. csx = 0;
  259. csax = sax;
  260. for (x = 0; x <= dst->w; x++) {
  261. *csax = csx;
  262. csax++;
  263. csx += sx;
  264. /* Guard from overflows */
  265. if (csx > ssx) {
  266. csx = ssx;
  267. }
  268. }
  269. /* Precalculate vertical row increments */
  270. csy = 0;
  271. csay = say;
  272. for (y = 0; y <= dst->h; y++) {
  273. *csay = csy;
  274. csay++;
  275. csy += sy;
  276. /* Guard from overflows */
  277. if (csy > ssy) {
  278. csy = ssy;
  279. }
  280. }
  281. sp = (tColorRGBA *) src->pixels;
  282. dp = (tColorRGBA *) dst->pixels;
  283. dgap = dst->pitch - dst->w * 4;
  284. spixelgap = src->pitch/4;
  285. if (flipx) sp += spixelw;
  286. if (flipy) sp += (spixelgap * spixelh);
  287. /*
  288. * Switch between interpolating and non-interpolating code
  289. */
  290. if (smooth) {
  291. /*
  292. * Interpolating Zoom
  293. */
  294. csay = say;
  295. for (y = 0; y < dst->h; y++) {
  296. csp = sp;
  297. csax = sax;
  298. for (x = 0; x < dst->w; x++) {
  299. /*
  300. * Setup color source pointers
  301. */
  302. ex = (*csax & 0xffff);
  303. ey = (*csay & 0xffff);
  304. cx = (*csax >> 16);
  305. cy = (*csay >> 16);
  306. sstepx = cx < spixelw;
  307. sstepy = cy < spixelh;
  308. c00 = sp;
  309. c01 = sp;
  310. c10 = sp;
  311. if (sstepy) {
  312. if (flipy) {
  313. c10 -= spixelgap;
  314. } else {
  315. c10 += spixelgap;
  316. }
  317. }
  318. c11 = c10;
  319. if (sstepx) {
  320. if (flipx) {
  321. c01--;
  322. c11--;
  323. } else {
  324. c01++;
  325. c11++;
  326. }
  327. }
  328. /*
  329. * Draw and interpolate colors
  330. */
  331. t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
  332. t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
  333. dp->r = (((t2 - t1) * ey) >> 16) + t1;
  334. t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff;
  335. t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff;
  336. dp->g = (((t2 - t1) * ey) >> 16) + t1;
  337. t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff;
  338. t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff;
  339. dp->b = (((t2 - t1) * ey) >> 16) + t1;
  340. t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff;
  341. t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff;
  342. dp->a = (((t2 - t1) * ey) >> 16) + t1;
  343. /*
  344. * Advance source pointer x
  345. */
  346. salast = csax;
  347. csax++;
  348. sstep = (*csax >> 16) - (*salast >> 16);
  349. if (flipx) {
  350. sp -= sstep;
  351. } else {
  352. sp += sstep;
  353. }
  354. /*
  355. * Advance destination pointer x
  356. */
  357. dp++;
  358. }
  359. /*
  360. * Advance source pointer y
  361. */
  362. salast = csay;
  363. csay++;
  364. sstep = (*csay >> 16) - (*salast >> 16);
  365. sstep *= spixelgap;
  366. if (flipy) {
  367. sp = csp - sstep;
  368. } else {
  369. sp = csp + sstep;
  370. }
  371. /*
  372. * Advance destination pointer y
  373. */
  374. dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
  375. }
  376. } else {
  377. /*
  378. * Non-Interpolating Zoom
  379. */
  380. csay = say;
  381. for (y = 0; y < dst->h; y++) {
  382. csp = sp;
  383. csax = sax;
  384. for (x = 0; x < dst->w; x++) {
  385. /*
  386. * Draw
  387. */
  388. *dp = *sp;
  389. /*
  390. * Advance source pointer x
  391. */
  392. salast = csax;
  393. csax++;
  394. sstep = (*csax >> 16) - (*salast >> 16);
  395. if (flipx) sstep = -sstep;
  396. sp += sstep;
  397. /*
  398. * Advance destination pointer x
  399. */
  400. dp++;
  401. }
  402. /*
  403. * Advance source pointer y
  404. */
  405. salast = csay;
  406. csay++;
  407. sstep = (*csay >> 16) - (*salast >> 16);
  408. sstep *= spixelgap;
  409. if (flipy) sstep = -sstep;
  410. sp = csp + sstep;
  411. /*
  412. * Advance destination pointer y
  413. */
  414. dp = (tColorRGBA *) ((Uint8 *) dp + dgap);
  415. }
  416. }
  417. /*
  418. * Remove temp arrays
  419. */
  420. free(sax);
  421. free(say);
  422. return (0);
  423. }
  424. /*!
  425. \brief Internal 8 bit Zoomer without smoothing.
  426. Zooms 8bit palette/Y 'src' surface to 'dst' surface.
  427. Assumes src and dst surfaces are of 8 bit depth.
  428. Assumes dst surface was allocated with the correct dimensions.
  429. \param src The surface to zoom (input).
  430. \param dst The zoomed surface (output).
  431. \param flipx Flag indicating if the image should be horizontally flipped.
  432. \param flipy Flag indicating if the image should be vertically flipped.
  433. \return 0 for success or -1 for error.
  434. */
  435. int _zoomSurfaceY(SDL_Surface * src, SDL_Surface * dst, int flipx, int flipy)
  436. {
  437. int x, y;
  438. Uint32 *sax, *say, *csax, *csay;
  439. int csx, csy;
  440. Uint8 *sp, *dp, *csp;
  441. int dgap;
  442. /*
  443. * Allocate memory for row increments
  444. */
  445. if ((sax = (Uint32 *) malloc((dst->w + 1) * sizeof(Uint32))) == NULL) {
  446. return (-1);
  447. }
  448. if ((say = (Uint32 *) malloc((dst->h + 1) * sizeof(Uint32))) == NULL) {
  449. free(sax);
  450. return (-1);
  451. }
  452. /*
  453. * Pointer setup
  454. */
  455. sp = csp = (Uint8 *) src->pixels;
  456. dp = (Uint8 *) dst->pixels;
  457. dgap = dst->pitch - dst->w;
  458. if (flipx) csp += (src->w-1);
  459. if (flipy) csp = ( (Uint8*)csp + src->pitch*(src->h-1) );
  460. /*
  461. * Precalculate row increments
  462. */
  463. csx = 0;
  464. csax = sax;
  465. for (x = 0; x < dst->w; x++) {
  466. csx += src->w;
  467. *csax = 0;
  468. while (csx >= dst->w) {
  469. csx -= dst->w;
  470. (*csax)++;
  471. }
  472. (*csax) = (*csax) * (flipx ? -1 : 1);
  473. csax++;
  474. }
  475. csy = 0;
  476. csay = say;
  477. for (y = 0; y < dst->h; y++) {
  478. csy += src->h;
  479. *csay = 0;
  480. while (csy >= dst->h) {
  481. csy -= dst->h;
  482. (*csay)++;
  483. }
  484. (*csay) = (*csay) * (flipy ? -1 : 1);
  485. csay++;
  486. }
  487. /*
  488. * Draw
  489. */
  490. csay = say;
  491. for (y = 0; y < dst->h; y++) {
  492. csax = sax;
  493. sp = csp;
  494. for (x = 0; x < dst->w; x++) {
  495. /*
  496. * Draw
  497. */
  498. *dp = *sp;
  499. /*
  500. * Advance source pointers
  501. */
  502. sp += (*csax);
  503. csax++;
  504. /*
  505. * Advance destination pointer
  506. */
  507. dp++;
  508. }
  509. /*
  510. * Advance source pointer (for row)
  511. */
  512. csp += ((*csay) * src->pitch);
  513. csay++;
  514. /*
  515. * Advance destination pointers
  516. */
  517. dp += dgap;
  518. }
  519. /*
  520. * Remove temp arrays
  521. */
  522. free(sax);
  523. free(say);
  524. return (0);
  525. }
  526. /*!
  527. \brief Internal 32 bit rotozoomer with optional anti-aliasing.
  528. Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  529. parameters by scanning the destination surface and applying optionally anti-aliasing
  530. by bilinear interpolation.
  531. Assumes src and dst surfaces are of 32 bit depth.
  532. Assumes dst surface was allocated with the correct dimensions.
  533. \param src Source surface.
  534. \param dst Destination surface.
  535. \param cx Horizontal center coordinate.
  536. \param cy Vertical center coordinate.
  537. \param isin Integer version of sine of angle.
  538. \param icos Integer version of cosine of angle.
  539. \param flipx Flag indicating horizontal mirroring should be applied.
  540. \param flipy Flag indicating vertical mirroring should be applied.
  541. \param smooth Flag indicating anti-aliasing should be used.
  542. */
  543. void _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
  544. {
  545. int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
  546. tColorRGBA c00, c01, c10, c11, cswap;
  547. tColorRGBA *pc, *sp;
  548. int gap;
  549. /*
  550. * Variable setup
  551. */
  552. xd = ((src->w - dst->w) << 15);
  553. yd = ((src->h - dst->h) << 15);
  554. ax = (cx << 16) - (icos * cx);
  555. ay = (cy << 16) - (isin * cx);
  556. sw = src->w - 1;
  557. sh = src->h - 1;
  558. pc = (tColorRGBA*) dst->pixels;
  559. gap = dst->pitch - dst->w * 4;
  560. /*
  561. * Switch between interpolating and non-interpolating code
  562. */
  563. if (smooth) {
  564. for (y = 0; y < dst->h; y++) {
  565. dy = cy - y;
  566. sdx = (ax + (isin * dy)) + xd;
  567. sdy = (ay - (icos * dy)) + yd;
  568. for (x = 0; x < dst->w; x++) {
  569. dx = (sdx >> 16);
  570. dy = (sdy >> 16);
  571. if (flipx) dx = sw - dx;
  572. if (flipy) dy = sh - dy;
  573. if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
  574. sp = (tColorRGBA *)src->pixels;;
  575. sp += ((src->pitch/4) * dy);
  576. sp += dx;
  577. c00 = *sp;
  578. sp += 1;
  579. c01 = *sp;
  580. sp += (src->pitch/4);
  581. c11 = *sp;
  582. sp -= 1;
  583. c10 = *sp;
  584. if (flipx) {
  585. cswap = c00; c00=c01; c01=cswap;
  586. cswap = c10; c10=c11; c11=cswap;
  587. }
  588. if (flipy) {
  589. cswap = c00; c00=c10; c10=cswap;
  590. cswap = c01; c01=c11; c11=cswap;
  591. }
  592. /*
  593. * Interpolate colors
  594. */
  595. ex = (sdx & 0xffff);
  596. ey = (sdy & 0xffff);
  597. t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
  598. t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
  599. pc->r = (((t2 - t1) * ey) >> 16) + t1;
  600. t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
  601. t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
  602. pc->g = (((t2 - t1) * ey) >> 16) + t1;
  603. t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
  604. t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
  605. pc->b = (((t2 - t1) * ey) >> 16) + t1;
  606. t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
  607. t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
  608. pc->a = (((t2 - t1) * ey) >> 16) + t1;
  609. }
  610. sdx += icos;
  611. sdy += isin;
  612. pc++;
  613. }
  614. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  615. }
  616. } else {
  617. for (y = 0; y < dst->h; y++) {
  618. dy = cy - y;
  619. sdx = (ax + (isin * dy)) + xd;
  620. sdy = (ay - (icos * dy)) + yd;
  621. for (x = 0; x < dst->w; x++) {
  622. dx = (short) (sdx >> 16);
  623. dy = (short) (sdy >> 16);
  624. if (flipx) dx = (src->w-1)-dx;
  625. if (flipy) dy = (src->h-1)-dy;
  626. if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
  627. sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
  628. sp += dx;
  629. *pc = *sp;
  630. }
  631. sdx += icos;
  632. sdy += isin;
  633. pc++;
  634. }
  635. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  636. }
  637. }
  638. }
  639. /*!
  640. \brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
  641. Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  642. parameters by scanning the destination surface.
  643. Assumes src and dst surfaces are of 8 bit depth.
  644. Assumes dst surface was allocated with the correct dimensions.
  645. \param src Source surface.
  646. \param dst Destination surface.
  647. \param cx Horizontal center coordinate.
  648. \param cy Vertical center coordinate.
  649. \param isin Integer version of sine of angle.
  650. \param icos Integer version of cosine of angle.
  651. \param flipx Flag indicating horizontal mirroring should be applied.
  652. \param flipy Flag indicating vertical mirroring should be applied.
  653. */
  654. void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
  655. {
  656. int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh;
  657. tColorY *pc, *sp;
  658. int gap;
  659. /*
  660. * Variable setup
  661. */
  662. xd = ((src->w - dst->w) << 15);
  663. yd = ((src->h - dst->h) << 15);
  664. ax = (cx << 16) - (icos * cx);
  665. ay = (cy << 16) - (isin * cx);
  666. sw = src->w - 1;
  667. sh = src->h - 1;
  668. pc = (tColorY*) dst->pixels;
  669. gap = dst->pitch - dst->w;
  670. /*
  671. * Clear surface to colorkey
  672. */
  673. memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
  674. /*
  675. * Iterate through destination surface
  676. */
  677. for (y = 0; y < dst->h; y++) {
  678. dy = cy - y;
  679. sdx = (ax + (isin * dy)) + xd;
  680. sdy = (ay - (icos * dy)) + yd;
  681. for (x = 0; x < dst->w; x++) {
  682. dx = (short) (sdx >> 16);
  683. dy = (short) (sdy >> 16);
  684. if (flipx) dx = (src->w-1)-dx;
  685. if (flipy) dy = (src->h-1)-dy;
  686. if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
  687. sp = (tColorY *) (src->pixels);
  688. sp += (src->pitch * dy + dx);
  689. *pc = *sp;
  690. }
  691. sdx += icos;
  692. sdy += isin;
  693. pc++;
  694. }
  695. pc += gap;
  696. }
  697. }
  698. /*!
  699. \brief Rotates a 32 bit surface in increments of 90 degrees.
  700. Specialized 90 degree rotator which rotates a 'src' surface in 90 degree
  701. increments clockwise returning a new surface. Faster than rotozoomer since
  702. not scanning or interpolation takes place. Input surface must be 32 bit.
  703. (code contributed by J. Schiller, improved by C. Allport and A. Schiffler)
  704. \param src Source surface to rotate.
  705. \param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source.
  706. \returns The new, rotated surface; or NULL for surfaces with incorrect input format.
  707. */
  708. SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns)
  709. {
  710. int row, col, newWidth, newHeight;
  711. int bpp, src_ipr, dst_ipr;
  712. SDL_Surface* dst;
  713. Uint32* srcBuf;
  714. Uint32* dstBuf;
  715. /* Has to be a valid surface pointer and only 32-bit surfaces (for now) */
  716. if (!src || src->format->BitsPerPixel != 32) { return NULL; }
  717. /* normalize numClockwiseTurns */
  718. while(numClockwiseTurns < 0) { numClockwiseTurns += 4; }
  719. numClockwiseTurns = (numClockwiseTurns % 4);
  720. /* if it's even, our new width will be the same as the source surface */
  721. newWidth = (numClockwiseTurns % 2) ? (src->h) : (src->w);
  722. newHeight = (numClockwiseTurns % 2) ? (src->w) : (src->h);
  723. dst = SDL_CreateRGBSurface( src->flags, newWidth, newHeight, src->format->BitsPerPixel,
  724. src->format->Rmask,
  725. src->format->Gmask,
  726. src->format->Bmask,
  727. src->format->Amask);
  728. if(!dst) {
  729. return NULL;
  730. }
  731. if (SDL_MUSTLOCK(dst)) {
  732. SDL_LockSurface(dst);
  733. }
  734. if (SDL_MUSTLOCK(dst)) {
  735. SDL_LockSurface(dst);
  736. }
  737. /* Calculate int-per-row */
  738. bpp = src->format->BitsPerPixel / 8;
  739. src_ipr = src->pitch / bpp;
  740. dst_ipr = dst->pitch / bpp;
  741. switch(numClockwiseTurns) {
  742. case 0: /* Make a copy of the surface */
  743. {
  744. /* Unfortunately SDL_BlitSurface cannot be used to make a copy of the surface
  745. since it does not preserve alpha. */
  746. if (src->pitch == dst->pitch) {
  747. /* If the pitch is the same for both surfaces, the memory can be copied all at once. */
  748. memcpy(dst->pixels, src->pixels, (src->h * src->pitch));
  749. }
  750. else
  751. {
  752. /* If the pitch differs, copy each row separately */
  753. srcBuf = (Uint32*)(src->pixels);
  754. dstBuf = (Uint32*)(dst->pixels);
  755. for (row = 0; row < src->h; row++) {
  756. memcpy(dstBuf, srcBuf, dst->w * bpp);
  757. srcBuf += src_ipr;
  758. dstBuf += dst_ipr;
  759. } /* end for(col) */
  760. } /* end for(row) */
  761. }
  762. break;
  763. /* rotate clockwise */
  764. case 1: /* rotated 90 degrees clockwise */
  765. {
  766. for (row = 0; row < src->h; ++row) {
  767. srcBuf = (Uint32*)(src->pixels) + (row * src_ipr);
  768. dstBuf = (Uint32*)(dst->pixels) + (dst->w - row - 1);
  769. for (col = 0; col < src->w; ++col) {
  770. *dstBuf = *srcBuf;
  771. ++srcBuf;
  772. dstBuf += dst_ipr;
  773. }
  774. /* end for(col) */
  775. }
  776. /* end for(row) */
  777. }
  778. break;
  779. case 2: /* rotated 180 degrees clockwise */
  780. {
  781. for (row = 0; row < src->h; ++row) {
  782. srcBuf = (Uint32*)(src->pixels) + (row * src_ipr);
  783. dstBuf = (Uint32*)(dst->pixels) + ((dst->h - row - 1) * dst_ipr) + (dst->w - 1);
  784. for (col = 0; col < src->w; ++col) {
  785. *dstBuf = *srcBuf;
  786. ++srcBuf;
  787. --dstBuf;
  788. }
  789. }
  790. }
  791. break;
  792. case 3:
  793. {
  794. for (row = 0; row < src->h; ++row) {
  795. srcBuf = (Uint32*)(src->pixels) + (row * src_ipr);
  796. dstBuf = (Uint32*)(dst->pixels) + row + ((dst->h - 1) * dst_ipr);
  797. for (col = 0; col < src->w; ++col) {
  798. *dstBuf = *srcBuf;
  799. ++srcBuf;
  800. dstBuf -= dst_ipr;
  801. }
  802. }
  803. }
  804. break;
  805. }
  806. /* end switch */
  807. if (SDL_MUSTLOCK(src)) {
  808. SDL_UnlockSurface(src);
  809. }
  810. if (SDL_MUSTLOCK(dst)) {
  811. SDL_UnlockSurface(dst);
  812. }
  813. return dst;
  814. }
  815. /*!
  816. \brief Internal target surface sizing function for rotozooms with trig result return.
  817. \param width The source surface width.
  818. \param height The source surface height.
  819. \param angle The angle to rotate in degrees.
  820. \param zoomx The horizontal scaling factor.
  821. \param zoomy The vertical scaling factor.
  822. \param dstwidth The calculated width of the destination surface.
  823. \param dstheight The calculated height of the destination surface.
  824. \param canglezoom The sine of the angle adjusted by the zoom factor.
  825. \param sanglezoom The cosine of the angle adjusted by the zoom factor.
  826. */
  827. void _rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoomx, double zoomy,
  828. int *dstwidth, int *dstheight,
  829. double *canglezoom, double *sanglezoom)
  830. {
  831. double x, y, cx, cy, sx, sy;
  832. double radangle;
  833. int dstwidthhalf, dstheighthalf;
  834. /*
  835. * Determine destination width and height by rotating a centered source box
  836. */
  837. radangle = angle * (M_PI / 180.0);
  838. *sanglezoom = sin(radangle);
  839. *canglezoom = cos(radangle);
  840. *sanglezoom *= zoomx;
  841. *canglezoom *= zoomx;
  842. x = (double)(width / 2);
  843. y = (double)(height / 2);
  844. cx = *canglezoom * x;
  845. cy = *canglezoom * y;
  846. sx = *sanglezoom * x;
  847. sy = *sanglezoom * y;
  848. dstwidthhalf = MAX((int)
  849. ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);
  850. dstheighthalf = MAX((int)
  851. ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);
  852. *dstwidth = 2 * dstwidthhalf;
  853. *dstheight = 2 * dstheighthalf;
  854. }
  855. /*!
  856. \brief Returns the size of the resulting target surface for a rotozoomSurfaceXY() call.
  857. \param width The source surface width.
  858. \param height The source surface height.
  859. \param angle The angle to rotate in degrees.
  860. \param zoomx The horizontal scaling factor.
  861. \param zoomy The vertical scaling factor.
  862. \param dstwidth The calculated width of the rotozoomed destination surface.
  863. \param dstheight The calculated height of the rotozoomed destination surface.
  864. */
  865. void rotozoomSurfaceSizeXY(int width, int height, double angle, double zoomx, double zoomy, int *dstwidth, int *dstheight)
  866. {
  867. double dummy_sanglezoom, dummy_canglezoom;
  868. _rotozoomSurfaceSizeTrig(width, height, angle, zoomx, zoomy, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
  869. }
  870. /*!
  871. \brief Returns the size of the resulting target surface for a rotozoomSurface() call.
  872. \param width The source surface width.
  873. \param height The source surface height.
  874. \param angle The angle to rotate in degrees.
  875. \param zoom The scaling factor.
  876. \param dstwidth The calculated width of the rotozoomed destination surface.
  877. \param dstheight The calculated height of the rotozoomed destination surface.
  878. */
  879. void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight)
  880. {
  881. double dummy_sanglezoom, dummy_canglezoom;
  882. _rotozoomSurfaceSizeTrig(width, height, angle, zoom, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
  883. }
  884. /*!
  885. \brief Rotates and zooms a surface and optional anti-aliasing.
  886. Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  887. 'angle' is the rotation in degrees and 'zoom' a scaling factor. If 'smooth' is set
  888. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  889. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  890. \param src The surface to rotozoom.
  891. \param angle The angle to rotate in degrees.
  892. \param zoom The scaling factor.
  893. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  894. \return The new rotozoomed surface.
  895. */
  896. SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth)
  897. {
  898. return rotozoomSurfaceXY(src, angle, zoom, zoom, smooth);
  899. }
  900. /*!
  901. \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
  902. Rotates and zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  903. 'angle' is the rotation in degrees, 'zoomx and 'zoomy' scaling factors. If 'smooth' is set
  904. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  905. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  906. \param src The surface to rotozoom.
  907. \param angle The angle to rotate in degrees.
  908. \param zoomx The horizontal scaling factor.
  909. \param zoomy The vertical scaling factor.
  910. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  911. \return The new rotozoomed surface.
  912. */
  913. SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, double zoomy, int smooth)
  914. {
  915. SDL_Surface *rz_src;
  916. SDL_Surface *rz_dst;
  917. double zoominv;
  918. double sanglezoom, canglezoom, sanglezoominv, canglezoominv;
  919. int dstwidthhalf, dstwidth, dstheighthalf, dstheight;
  920. int is32bit;
  921. int i, src_converted;
  922. int flipx,flipy;
  923. Uint8 r,g,b;
  924. Uint32 colorkey = 0;
  925. int colorKeyAvailable = 0;
  926. /*
  927. * Sanity check
  928. */
  929. if (src == NULL)
  930. return (NULL);
  931. if (src->flags & SDL_SRCCOLORKEY)
  932. {
  933. colorkey = _colorkey(src);
  934. SDL_GetRGB(colorkey, src->format, &r, &g, &b);
  935. colorKeyAvailable = 1;
  936. }
  937. /*
  938. * Determine if source surface is 32bit or 8bit
  939. */
  940. is32bit = (src->format->BitsPerPixel == 32);
  941. if ((is32bit) || (src->format->BitsPerPixel == 8)) {
  942. /*
  943. * Use source surface 'as is'
  944. */
  945. rz_src = src;
  946. src_converted = 0;
  947. } else {
  948. /*
  949. * New source surface is 32bit with a defined RGBA ordering
  950. */
  951. rz_src =
  952. SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
  953. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  954. 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
  955. #else
  956. 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
  957. #endif
  958. );
  959. if(colorKeyAvailable)
  960. SDL_SetColorKey(src, 0, 0);
  961. SDL_BlitSurface(src, NULL, rz_src, NULL);
  962. if(colorKeyAvailable)
  963. SDL_SetColorKey(src, SDL_SRCCOLORKEY, colorkey);
  964. src_converted = 1;
  965. is32bit = 1;
  966. }
  967. /*
  968. * Sanity check zoom factor
  969. */
  970. flipx = (zoomx<0.0);
  971. if (flipx) zoomx=-zoomx;
  972. flipy = (zoomy<0.0);
  973. if (flipy) zoomy=-zoomy;
  974. if (zoomx < VALUE_LIMIT) zoomx = VALUE_LIMIT;
  975. if (zoomy < VALUE_LIMIT) zoomy = VALUE_LIMIT;
  976. zoominv = 65536.0 / (zoomx * zoomx);
  977. /*
  978. * Check if we have a rotozoom or just a zoom
  979. */
  980. if (fabs(angle) > VALUE_LIMIT) {
  981. /*
  982. * Angle!=0: full rotozoom
  983. */
  984. /*
  985. * -----------------------
  986. */
  987. /* Determine target size */
  988. _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoomx, zoomy, &dstwidth, &dstheight, &canglezoom, &sanglezoom);
  989. /*
  990. * Calculate target factors from sin/cos and zoom
  991. */
  992. sanglezoominv = sanglezoom;
  993. canglezoominv = canglezoom;
  994. sanglezoominv *= zoominv;
  995. canglezoominv *= zoominv;
  996. /* Calculate half size */
  997. dstwidthhalf = dstwidth / 2;
  998. dstheighthalf = dstheight / 2;
  999. /*
  1000. * Alloc space to completely contain the rotated surface
  1001. */
  1002. rz_dst = NULL;
  1003. if (is32bit) {
  1004. /*
  1005. * Target surface is 32bit with source RGBA/ABGR ordering
  1006. */
  1007. rz_dst =
  1008. SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
  1009. rz_src->format->Rmask, rz_src->format->Gmask,
  1010. rz_src->format->Bmask, rz_src->format->Amask);
  1011. } else {
  1012. /*
  1013. * Target surface is 8bit
  1014. */
  1015. rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  1016. }
  1017. /* Check target */
  1018. if (rz_dst == NULL)
  1019. return NULL;
  1020. /* Adjust for guard rows */
  1021. rz_dst->h = dstheight;
  1022. if (colorKeyAvailable == 1){
  1023. colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
  1024. SDL_FillRect(rz_dst, NULL, colorkey );
  1025. }
  1026. /*
  1027. * Lock source surface
  1028. */
  1029. if (SDL_MUSTLOCK(rz_src)) {
  1030. SDL_LockSurface(rz_src);
  1031. }
  1032. /*
  1033. * Check which kind of surface we have
  1034. */
  1035. if (is32bit) {
  1036. /*
  1037. * Call the 32bit transformation routine to do the rotation (using alpha)
  1038. */
  1039. _transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
  1040. (int) (sanglezoominv), (int) (canglezoominv),
  1041. flipx, flipy,
  1042. smooth);
  1043. /*
  1044. * Turn on source-alpha support
  1045. */
  1046. SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
  1047. SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1048. } else {
  1049. /*
  1050. * Copy palette and colorkey info
  1051. */
  1052. for (i = 0; i < rz_src->format->palette->ncolors; i++) {
  1053. rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
  1054. }
  1055. rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
  1056. /*
  1057. * Call the 8bit transformation routine to do the rotation
  1058. */
  1059. transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
  1060. (int) (sanglezoominv), (int) (canglezoominv),
  1061. flipx, flipy);
  1062. SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1063. }
  1064. /*
  1065. * Unlock source surface
  1066. */
  1067. if (SDL_MUSTLOCK(rz_src)) {
  1068. SDL_UnlockSurface(rz_src);
  1069. }
  1070. } else {
  1071. /*
  1072. * Angle=0: Just a zoom
  1073. */
  1074. /*
  1075. * --------------------
  1076. */
  1077. /*
  1078. * Calculate target size
  1079. */
  1080. zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
  1081. /*
  1082. * Alloc space to completely contain the zoomed surface
  1083. */
  1084. rz_dst = NULL;
  1085. if (is32bit) {
  1086. /*
  1087. * Target surface is 32bit with source RGBA/ABGR ordering
  1088. */
  1089. rz_dst =
  1090. SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
  1091. rz_src->format->Rmask, rz_src->format->Gmask,
  1092. rz_src->format->Bmask, rz_src->format->Amask);
  1093. } else {
  1094. /*
  1095. * Target surface is 8bit
  1096. */
  1097. rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  1098. }
  1099. /* Check target */
  1100. if (rz_dst == NULL)
  1101. return NULL;
  1102. /* Adjust for guard rows */
  1103. rz_dst->h = dstheight;
  1104. if (colorKeyAvailable == 1){
  1105. colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
  1106. SDL_FillRect(rz_dst, NULL, colorkey );
  1107. }
  1108. /*
  1109. * Lock source surface
  1110. */
  1111. if (SDL_MUSTLOCK(rz_src)) {
  1112. SDL_LockSurface(rz_src);
  1113. }
  1114. /*
  1115. * Check which kind of surface we have
  1116. */
  1117. if (is32bit) {
  1118. /*
  1119. * Call the 32bit transformation routine to do the zooming (using alpha)
  1120. */
  1121. _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, smooth);
  1122. /*
  1123. * Turn on source-alpha support
  1124. */
  1125. SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
  1126. SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1127. } else {
  1128. /*
  1129. * Copy palette and colorkey info
  1130. */
  1131. for (i = 0; i < rz_src->format->palette->ncolors; i++) {
  1132. rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
  1133. }
  1134. rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
  1135. /*
  1136. * Call the 8bit transformation routine to do the zooming
  1137. */
  1138. _zoomSurfaceY(rz_src, rz_dst, flipx, flipy);
  1139. SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1140. }
  1141. /*
  1142. * Unlock source surface
  1143. */
  1144. if (SDL_MUSTLOCK(rz_src)) {
  1145. SDL_UnlockSurface(rz_src);
  1146. }
  1147. }
  1148. /*
  1149. * Cleanup temp surface
  1150. */
  1151. if (src_converted) {
  1152. SDL_FreeSurface(rz_src);
  1153. }
  1154. /*
  1155. * Return destination surface
  1156. */
  1157. return (rz_dst);
  1158. }
  1159. /*!
  1160. \brief Calculates the size of the target surface for a zoomSurface() call.
  1161. The minimum size of the target surface is 1. The input factors can be positive or negative.
  1162. \param width The width of the source surface to zoom.
  1163. \param height The height of the source surface to zoom.
  1164. \param zoomx The horizontal zoom factor.
  1165. \param zoomy The vertical zoom factor.
  1166. \param dstwidth Pointer to an integer to store the calculated width of the zoomed target surface.
  1167. \param dstheight Pointer to an integer to store the calculated height of the zoomed target surface.
  1168. */
  1169. void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight)
  1170. {
  1171. /*
  1172. * Make zoom factors positive
  1173. */
  1174. int flipx, flipy;
  1175. flipx = (zoomx<0.0);
  1176. if (flipx) zoomx = -zoomx;
  1177. flipy = (zoomy<0.0);
  1178. if (flipy) zoomy = -zoomy;
  1179. /*
  1180. * Sanity check zoom factors
  1181. */
  1182. if (zoomx < VALUE_LIMIT) {
  1183. zoomx = VALUE_LIMIT;
  1184. }
  1185. if (zoomy < VALUE_LIMIT) {
  1186. zoomy = VALUE_LIMIT;
  1187. }
  1188. /*
  1189. * Calculate target size
  1190. */
  1191. *dstwidth = (int) floor(((double) width * zoomx) + 0.5);
  1192. *dstheight = (int) floor(((double) height * zoomy) + 0.5);
  1193. if (*dstwidth < 1) {
  1194. *dstwidth = 1;
  1195. }
  1196. if (*dstheight < 1) {
  1197. *dstheight = 1;
  1198. }
  1199. }
  1200. /*!
  1201. \brief Zoom a surface by independent horizontal and vertical factors with optional smoothing.
  1202. Zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  1203. 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is on
  1204. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  1205. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  1206. If zoom factors are negative, the image is flipped on the axes.
  1207. \param src The surface to zoom.
  1208. \param zoomx The horizontal zoom factor.
  1209. \param zoomy The vertical zoom factor.
  1210. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  1211. \return The new, zoomed surface.
  1212. */
  1213. SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth)
  1214. {
  1215. SDL_Surface *rz_src;
  1216. SDL_Surface *rz_dst;
  1217. int dstwidth, dstheight;
  1218. int is32bit;
  1219. int i, src_converted;
  1220. int flipx, flipy;
  1221. /*
  1222. * Sanity check
  1223. */
  1224. if (src == NULL)
  1225. return (NULL);
  1226. /*
  1227. * Determine if source surface is 32bit or 8bit
  1228. */
  1229. is32bit = (src->format->BitsPerPixel == 32);
  1230. if ((is32bit) || (src->format->BitsPerPixel == 8)) {
  1231. /*
  1232. * Use source surface 'as is'
  1233. */
  1234. rz_src = src;
  1235. src_converted = 0;
  1236. } else {
  1237. /*
  1238. * New source surface is 32bit with a defined RGBA ordering
  1239. */
  1240. rz_src =
  1241. SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
  1242. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1243. 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
  1244. #else
  1245. 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
  1246. #endif
  1247. );
  1248. if (rz_src == NULL) {
  1249. return NULL;
  1250. }
  1251. SDL_BlitSurface(src, NULL, rz_src, NULL);
  1252. src_converted = 1;
  1253. is32bit = 1;
  1254. }
  1255. flipx = (zoomx<0.0);
  1256. if (flipx) zoomx = -zoomx;
  1257. flipy = (zoomy<0.0);
  1258. if (flipy) zoomy = -zoomy;
  1259. /* Get size if target */
  1260. zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);
  1261. /*
  1262. * Alloc space to completely contain the zoomed surface
  1263. */
  1264. rz_dst = NULL;
  1265. if (is32bit) {
  1266. /*
  1267. * Target surface is 32bit with source RGBA/ABGR ordering
  1268. */
  1269. rz_dst =
  1270. SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
  1271. rz_src->format->Rmask, rz_src->format->Gmask,
  1272. rz_src->format->Bmask, rz_src->format->Amask);
  1273. } else {
  1274. /*
  1275. * Target surface is 8bit
  1276. */
  1277. rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  1278. }
  1279. /* Check target */
  1280. if (rz_dst == NULL) {
  1281. /*
  1282. * Cleanup temp surface
  1283. */
  1284. if (src_converted) {
  1285. SDL_FreeSurface(rz_src);
  1286. }
  1287. return NULL;
  1288. }
  1289. /* Adjust for guard rows */
  1290. rz_dst->h = dstheight;
  1291. /*
  1292. * Lock source surface
  1293. */
  1294. if (SDL_MUSTLOCK(rz_src)) {
  1295. SDL_LockSurface(rz_src);
  1296. }
  1297. /*
  1298. * Check which kind of surface we have
  1299. */
  1300. if (is32bit) {
  1301. /*
  1302. * Call the 32bit transformation routine to do the zooming (using alpha)
  1303. */
  1304. _zoomSurfaceRGBA(rz_src, rz_dst, flipx, flipy, smooth);
  1305. /*
  1306. * Turn on source-alpha support
  1307. */
  1308. SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
  1309. } else {
  1310. /*
  1311. * Copy palette and colorkey info
  1312. */
  1313. for (i = 0; i < rz_src->format->palette->ncolors; i++) {
  1314. rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
  1315. }
  1316. rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
  1317. /*
  1318. * Call the 8bit transformation routine to do the zooming
  1319. */
  1320. _zoomSurfaceY(rz_src, rz_dst, flipx, flipy);
  1321. SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1322. }
  1323. /*
  1324. * Unlock source surface
  1325. */
  1326. if (SDL_MUSTLOCK(rz_src)) {
  1327. SDL_UnlockSurface(rz_src);
  1328. }
  1329. /*
  1330. * Cleanup temp surface
  1331. */
  1332. if (src_converted) {
  1333. SDL_FreeSurface(rz_src);
  1334. }
  1335. /*
  1336. * Return destination surface
  1337. */
  1338. return (rz_dst);
  1339. }
  1340. /*!
  1341. \brief Shrink a surface by an integer ratio using averaging.
  1342. Shrinks a 32bit or 8bit 'src' surface to a newly created 'dst' surface.
  1343. 'factorx' and 'factory' are the shrinking ratios (i.e. 2=1/2 the size,
  1344. 3=1/3 the size, etc.) The destination surface is antialiased by averaging
  1345. the source box RGBA or Y information. If the surface is not 8bit
  1346. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  1347. The input surface is not modified. The output surface is newly allocated.
  1348. \param src The surface to shrink.
  1349. \param factorx The horizontal shrinking ratio.
  1350. \param factory The vertical shrinking ratio.
  1351. \return The new, shrunken surface.
  1352. */
  1353. /*@null@*/
  1354. SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory)
  1355. {
  1356. int result;
  1357. SDL_Surface *rz_src;
  1358. SDL_Surface *rz_dst = NULL;
  1359. int dstwidth, dstheight;
  1360. int is32bit;
  1361. int i, src_converted;
  1362. int haveError = 0;
  1363. /*
  1364. * Sanity check
  1365. */
  1366. if (src == NULL) {
  1367. return (NULL);
  1368. }
  1369. /*
  1370. * Determine if source surface is 32bit or 8bit
  1371. */
  1372. is32bit = (src->format->BitsPerPixel == 32);
  1373. if ((is32bit) || (src->format->BitsPerPixel == 8)) {
  1374. /*
  1375. * Use source surface 'as is'
  1376. */
  1377. rz_src = src;
  1378. src_converted = 0;
  1379. } else {
  1380. /*
  1381. * New source surface is 32bit with a defined RGBA ordering
  1382. */
  1383. rz_src = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
  1384. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  1385. 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
  1386. #else
  1387. 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
  1388. #endif
  1389. );
  1390. if (rz_src==NULL) {
  1391. haveError = 1;
  1392. goto exitShrinkSurface;
  1393. }
  1394. SDL_BlitSurface(src, NULL, rz_src, NULL);
  1395. src_converted = 1;
  1396. is32bit = 1;
  1397. }
  1398. /*
  1399. * Lock the surface
  1400. */
  1401. if (SDL_MUSTLOCK(rz_src)) {
  1402. if (SDL_LockSurface(rz_src) < 0) {
  1403. haveError = 1;
  1404. goto exitShrinkSurface;
  1405. }
  1406. }
  1407. /* Get size for target */
  1408. dstwidth=rz_src->w/factorx;
  1409. while (dstwidth*factorx>rz_src->w) { dstwidth--; }
  1410. dstheight=rz_src->h/factory;
  1411. while (dstheight*factory>rz_src->h) { dstheight--; }
  1412. /*
  1413. * Alloc space to completely contain the shrunken surface
  1414. * (with added guard rows)
  1415. */
  1416. if (is32bit==1) {
  1417. /*
  1418. * Target surface is 32bit with source RGBA/ABGR ordering
  1419. */
  1420. rz_dst =
  1421. SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
  1422. rz_src->format->Rmask, rz_src->format->Gmask,
  1423. rz_src->format->Bmask, rz_src->format->Amask);
  1424. } else {
  1425. /*
  1426. * Target surface is 8bit
  1427. */
  1428. rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  1429. }
  1430. /* Check target */
  1431. if (rz_dst == NULL) {
  1432. haveError = 1;
  1433. goto exitShrinkSurface;
  1434. }
  1435. /* Adjust for guard rows */
  1436. rz_dst->h = dstheight;
  1437. /*
  1438. * Check which kind of surface we have
  1439. */
  1440. if (is32bit==1) {
  1441. /*
  1442. * Call the 32bit transformation routine to do the shrinking (using alpha)
  1443. */
  1444. result = _shrinkSurfaceRGBA(rz_src, rz_dst, factorx, factory);
  1445. if ((result!=0) || (rz_dst==NULL)) {
  1446. haveError = 1;
  1447. goto exitShrinkSurface;
  1448. }
  1449. /*
  1450. * Turn on source-alpha support
  1451. */
  1452. result = SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);
  1453. if (result!=0) {
  1454. haveError = 1;
  1455. goto exitShrinkSurface;
  1456. }
  1457. } else {
  1458. /*
  1459. * Copy palette and colorkey info
  1460. */
  1461. for (i = 0; i < rz_src->format->palette->ncolors; i++) {
  1462. rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
  1463. }
  1464. rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
  1465. /*
  1466. * Call the 8bit transformation routine to do the shrinking
  1467. */
  1468. result = _shrinkSurfaceY(rz_src, rz_dst, factorx, factory);
  1469. if (result!=0) {
  1470. haveError = 1;
  1471. goto exitShrinkSurface;
  1472. }
  1473. /*
  1474. * Set colorkey on target
  1475. */
  1476. result = SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, _colorkey(rz_src));
  1477. if (result!=0) {
  1478. haveError = 1;
  1479. goto exitShrinkSurface;
  1480. }
  1481. }
  1482. exitShrinkSurface:
  1483. if (rz_src!=NULL) {
  1484. /*
  1485. * Unlock source surface
  1486. */
  1487. if (SDL_MUSTLOCK(rz_src)) {
  1488. SDL_UnlockSurface(rz_src);
  1489. }
  1490. /*
  1491. * Cleanup temp surface
  1492. */
  1493. if (src_converted==1) {
  1494. SDL_FreeSurface(rz_src);
  1495. }
  1496. }
  1497. /* Check error state; maybe need to cleanup destination */
  1498. if (haveError==1) {
  1499. if (rz_dst!=NULL) {
  1500. SDL_FreeSurface(rz_dst);
  1501. }
  1502. rz_dst=NULL;
  1503. }
  1504. /*
  1505. * Return destination surface
  1506. */
  1507. return (rz_dst);
  1508. }