You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1663 lines
40 KiB

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