SDL_rotate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. SDL_rotate.c: rotates 32bit or 8bit surfaces
  3. Shamelessly stolen from SDL_gfx by Andreas Schiffler. Original copyright follows:
  4. Copyright (C) 2001-2011 Andreas Schiffler
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source
  18. distribution.
  19. Andreas Schiffler -- aschiffler at ferzkopp dot net
  20. */
  21. #include "../../SDL_internal.h"
  22. #if defined(__WIN32__)
  23. #include "../../core/windows/SDL_windows.h"
  24. #endif
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "SDL.h"
  28. #include "SDL_rotate.h"
  29. /* ---- Internally used structures */
  30. /* !
  31. \brief A 32 bit RGBA pixel.
  32. */
  33. typedef struct tColorRGBA {
  34. Uint8 r;
  35. Uint8 g;
  36. Uint8 b;
  37. Uint8 a;
  38. } tColorRGBA;
  39. /* !
  40. \brief A 8bit Y/palette pixel.
  41. */
  42. typedef struct tColorY {
  43. Uint8 y;
  44. } tColorY;
  45. /* !
  46. \brief Returns maximum of two numbers a and b.
  47. */
  48. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  49. /* !
  50. \brief Number of guard rows added to destination surfaces.
  51. This is a simple but effective workaround for observed issues.
  52. These rows allocate extra memory and are then hidden from the surface.
  53. Rows are added to the end of destination surfaces when they are allocated.
  54. This catches any potential overflows which seem to happen with
  55. just the right src image dimensions and scale/rotation and can lead
  56. to a situation where the program can segfault.
  57. */
  58. #define GUARD_ROWS (2)
  59. /* !
  60. \brief Lower limit of absolute zoom factor or rotation degrees.
  61. */
  62. #define VALUE_LIMIT 0.001
  63. /* !
  64. \brief Returns colorkey info for a surface
  65. */
  66. static Uint32
  67. _colorkey(SDL_Surface *src)
  68. {
  69. Uint32 key = 0;
  70. SDL_GetColorKey(src, &key);
  71. return key;
  72. }
  73. /* !
  74. \brief Internal target surface sizing function for rotations with trig result return.
  75. \param width The source surface width.
  76. \param height The source surface height.
  77. \param angle The angle to rotate in degrees.
  78. \param dstwidth The calculated width of the destination surface.
  79. \param dstheight The calculated height of the destination surface.
  80. \param cangle The sine of the angle
  81. \param sangle The cosine of the angle
  82. */
  83. void
  84. SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
  85. int *dstwidth, int *dstheight,
  86. double *cangle, double *sangle)
  87. {
  88. double x, y, cx, cy, sx, sy;
  89. double radangle;
  90. int dstwidthhalf, dstheighthalf;
  91. /*
  92. * Determine destination width and height by rotating a centered source box
  93. */
  94. radangle = angle * (M_PI / 180.0);
  95. *sangle = SDL_sin(radangle);
  96. *cangle = SDL_cos(radangle);
  97. x = (double)(width / 2);
  98. y = (double)(height / 2);
  99. cx = *cangle * x;
  100. cy = *cangle * y;
  101. sx = *sangle * x;
  102. sy = *sangle * y;
  103. dstwidthhalf = MAX((int)
  104. SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
  105. dstheighthalf = MAX((int)
  106. SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
  107. *dstwidth = 2 * dstwidthhalf;
  108. *dstheight = 2 * dstheighthalf;
  109. }
  110. /* !
  111. \brief Internal 32 bit rotozoomer with optional anti-aliasing.
  112. Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  113. parameters by scanning the destination surface and applying optionally anti-aliasing
  114. by bilinear interpolation.
  115. Assumes src and dst surfaces are of 32 bit depth.
  116. Assumes dst surface was allocated with the correct dimensions.
  117. \param src Source surface.
  118. \param dst Destination surface.
  119. \param cx Horizontal center coordinate.
  120. \param cy Vertical center coordinate.
  121. \param isin Integer version of sine of angle.
  122. \param icos Integer version of cosine of angle.
  123. \param flipx Flag indicating horizontal mirroring should be applied.
  124. \param flipy Flag indicating vertical mirroring should be applied.
  125. \param smooth Flag indicating anti-aliasing should be used.
  126. */
  127. static void
  128. _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
  129. {
  130. int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
  131. tColorRGBA c00, c01, c10, c11, cswap;
  132. tColorRGBA *pc, *sp;
  133. int gap;
  134. /*
  135. * Variable setup
  136. */
  137. xd = ((src->w - dst->w) << 15);
  138. yd = ((src->h - dst->h) << 15);
  139. ax = (cx << 16) - (icos * cx);
  140. ay = (cy << 16) - (isin * cx);
  141. sw = src->w - 1;
  142. sh = src->h - 1;
  143. pc = (tColorRGBA*) dst->pixels;
  144. gap = dst->pitch - dst->w * 4;
  145. /*
  146. * Switch between interpolating and non-interpolating code
  147. */
  148. if (smooth) {
  149. for (y = 0; y < dst->h; y++) {
  150. dy = cy - y;
  151. sdx = (ax + (isin * dy)) + xd;
  152. sdy = (ay - (icos * dy)) + yd;
  153. for (x = 0; x < dst->w; x++) {
  154. dx = (sdx >> 16);
  155. dy = (sdy >> 16);
  156. if (flipx) dx = sw - dx;
  157. if (flipy) dy = sh - dy;
  158. if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
  159. sp = (tColorRGBA *)src->pixels;;
  160. sp += ((src->pitch/4) * dy);
  161. sp += dx;
  162. c00 = *sp;
  163. sp += 1;
  164. c01 = *sp;
  165. sp += (src->pitch/4);
  166. c11 = *sp;
  167. sp -= 1;
  168. c10 = *sp;
  169. if (flipx) {
  170. cswap = c00; c00=c01; c01=cswap;
  171. cswap = c10; c10=c11; c11=cswap;
  172. }
  173. if (flipy) {
  174. cswap = c00; c00=c10; c10=cswap;
  175. cswap = c01; c01=c11; c11=cswap;
  176. }
  177. /*
  178. * Interpolate colors
  179. */
  180. ex = (sdx & 0xffff);
  181. ey = (sdy & 0xffff);
  182. t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
  183. t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
  184. pc->r = (((t2 - t1) * ey) >> 16) + t1;
  185. t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
  186. t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
  187. pc->g = (((t2 - t1) * ey) >> 16) + t1;
  188. t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
  189. t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
  190. pc->b = (((t2 - t1) * ey) >> 16) + t1;
  191. t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
  192. t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
  193. pc->a = (((t2 - t1) * ey) >> 16) + t1;
  194. }
  195. sdx += icos;
  196. sdy += isin;
  197. pc++;
  198. }
  199. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  200. }
  201. } else {
  202. for (y = 0; y < dst->h; y++) {
  203. dy = cy - y;
  204. sdx = (ax + (isin * dy)) + xd;
  205. sdy = (ay - (icos * dy)) + yd;
  206. for (x = 0; x < dst->w; x++) {
  207. dx = (short) (sdx >> 16);
  208. dy = (short) (sdy >> 16);
  209. if (flipx) dx = (src->w-1)-dx;
  210. if (flipy) dy = (src->h-1)-dy;
  211. if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
  212. sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
  213. sp += dx;
  214. *pc = *sp;
  215. }
  216. sdx += icos;
  217. sdy += isin;
  218. pc++;
  219. }
  220. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  221. }
  222. }
  223. }
  224. /* !
  225. \brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
  226. Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  227. parameters by scanning the destination surface.
  228. Assumes src and dst surfaces are of 8 bit depth.
  229. Assumes dst surface was allocated with the correct dimensions.
  230. \param src Source surface.
  231. \param dst Destination surface.
  232. \param cx Horizontal center coordinate.
  233. \param cy Vertical center coordinate.
  234. \param isin Integer version of sine of angle.
  235. \param icos Integer version of cosine of angle.
  236. \param flipx Flag indicating horizontal mirroring should be applied.
  237. \param flipy Flag indicating vertical mirroring should be applied.
  238. */
  239. static void
  240. transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
  241. {
  242. int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
  243. tColorY *pc, *sp;
  244. int gap;
  245. /*
  246. * Variable setup
  247. */
  248. xd = ((src->w - dst->w) << 15);
  249. yd = ((src->h - dst->h) << 15);
  250. ax = (cx << 16) - (icos * cx);
  251. ay = (cy << 16) - (isin * cx);
  252. pc = (tColorY*) dst->pixels;
  253. gap = dst->pitch - dst->w;
  254. /*
  255. * Clear surface to colorkey
  256. */
  257. SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
  258. /*
  259. * Iterate through destination surface
  260. */
  261. for (y = 0; y < dst->h; y++) {
  262. dy = cy - y;
  263. sdx = (ax + (isin * dy)) + xd;
  264. sdy = (ay - (icos * dy)) + yd;
  265. for (x = 0; x < dst->w; x++) {
  266. dx = (short) (sdx >> 16);
  267. dy = (short) (sdy >> 16);
  268. if (flipx) dx = (src->w-1)-dx;
  269. if (flipy) dy = (src->h-1)-dy;
  270. if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
  271. sp = (tColorY *) (src->pixels);
  272. sp += (src->pitch * dy + dx);
  273. *pc = *sp;
  274. }
  275. sdx += icos;
  276. sdy += isin;
  277. pc++;
  278. }
  279. pc += gap;
  280. }
  281. }
  282. /* !
  283. \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
  284. Rotates a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  285. 'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
  286. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  287. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  288. \param src The surface to rotozoom.
  289. \param angle The angle to rotate in degrees.
  290. \param centerx The horizontal coordinate of the center of rotation
  291. \param zoomy The vertical coordinate of the center of rotation
  292. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  293. \param flipx Set to 1 to flip the image horizontally
  294. \param flipy Set to 1 to flip the image vertically
  295. \param dstwidth The destination surface width
  296. \param dstheight The destination surface height
  297. \param cangle The angle cosine
  298. \param sangle The angle sine
  299. \return The new rotated surface.
  300. */
  301. SDL_Surface *
  302. SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
  303. {
  304. SDL_Surface *rz_src;
  305. SDL_Surface *rz_dst;
  306. int is32bit;
  307. int i, src_converted;
  308. Uint8 r,g,b;
  309. Uint32 colorkey = 0;
  310. int colorKeyAvailable = 0;
  311. double sangleinv, cangleinv;
  312. /*
  313. * Sanity check
  314. */
  315. if (src == NULL)
  316. return (NULL);
  317. if (src->flags & SDL_TRUE/* SDL_SRCCOLORKEY */)
  318. {
  319. colorkey = _colorkey(src);
  320. SDL_GetRGB(colorkey, src->format, &r, &g, &b);
  321. colorKeyAvailable = 1;
  322. }
  323. /*
  324. * Determine if source surface is 32bit or 8bit
  325. */
  326. is32bit = (src->format->BitsPerPixel == 32);
  327. if ((is32bit) || (src->format->BitsPerPixel == 8)) {
  328. /*
  329. * Use source surface 'as is'
  330. */
  331. rz_src = src;
  332. src_converted = 0;
  333. } else {
  334. /*
  335. * New source surface is 32bit with a defined RGBA ordering
  336. */
  337. rz_src =
  338. SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
  339. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  340. 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
  341. #else
  342. 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
  343. #endif
  344. );
  345. if(colorKeyAvailable)
  346. SDL_SetColorKey(src, 0, 0);
  347. SDL_BlitSurface(src, NULL, rz_src, NULL);
  348. if(colorKeyAvailable)
  349. SDL_SetColorKey(src, SDL_TRUE /* SDL_SRCCOLORKEY */, colorkey);
  350. src_converted = 1;
  351. is32bit = 1;
  352. }
  353. /* Determine target size */
  354. /* _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, &dstwidth, &dstheight, &cangle, &sangle); */
  355. /*
  356. * Calculate target factors from sin/cos and zoom
  357. */
  358. sangleinv = sangle*65536.0;
  359. cangleinv = cangle*65536.0;
  360. /*
  361. * Alloc space to completely contain the rotated surface
  362. */
  363. rz_dst = NULL;
  364. if (is32bit) {
  365. /*
  366. * Target surface is 32bit with source RGBA/ABGR ordering
  367. */
  368. rz_dst =
  369. SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
  370. rz_src->format->Rmask, rz_src->format->Gmask,
  371. rz_src->format->Bmask, rz_src->format->Amask);
  372. } else {
  373. /*
  374. * Target surface is 8bit
  375. */
  376. rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  377. }
  378. /* Check target */
  379. if (rz_dst == NULL)
  380. return NULL;
  381. /* Adjust for guard rows */
  382. rz_dst->h = dstheight;
  383. if (colorKeyAvailable == 1){
  384. colorkey = SDL_MapRGB(rz_dst->format, r, g, b);
  385. SDL_FillRect(rz_dst, NULL, colorkey );
  386. }
  387. /*
  388. * Lock source surface
  389. */
  390. if (SDL_MUSTLOCK(rz_src)) {
  391. SDL_LockSurface(rz_src);
  392. }
  393. /*
  394. * Check which kind of surface we have
  395. */
  396. if (is32bit) {
  397. /*
  398. * Call the 32bit transformation routine to do the rotation (using alpha)
  399. */
  400. _transformSurfaceRGBA(rz_src, rz_dst, centerx, centery,
  401. (int) (sangleinv), (int) (cangleinv),
  402. flipx, flipy,
  403. smooth);
  404. /*
  405. * Turn on source-alpha support
  406. */
  407. /* SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); */
  408. SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
  409. } else {
  410. /*
  411. * Copy palette and colorkey info
  412. */
  413. for (i = 0; i < rz_src->format->palette->ncolors; i++) {
  414. rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
  415. }
  416. rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
  417. /*
  418. * Call the 8bit transformation routine to do the rotation
  419. */
  420. transformSurfaceY(rz_src, rz_dst, centerx, centery,
  421. (int) (sangleinv), (int) (cangleinv),
  422. flipx, flipy);
  423. SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
  424. }
  425. /*
  426. * Unlock source surface
  427. */
  428. if (SDL_MUSTLOCK(rz_src)) {
  429. SDL_UnlockSurface(rz_src);
  430. }
  431. /*
  432. * Cleanup temp surface
  433. */
  434. if (src_converted) {
  435. SDL_FreeSurface(rz_src);
  436. }
  437. /*
  438. * Return destination surface
  439. */
  440. return (rz_dst);
  441. }