SDL_rotozoom.c 50 KB

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