SDL_rotate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 Returns colorkey info for a surface
  61. */
  62. static Uint32
  63. _colorkey(SDL_Surface *src)
  64. {
  65. Uint32 key = 0;
  66. if (SDL_HasColorKey(src)) {
  67. SDL_GetColorKey(src, &key);
  68. }
  69. return key;
  70. }
  71. /* !
  72. \brief Internal target surface sizing function for rotations with trig result return.
  73. \param width The source surface width.
  74. \param height The source surface height.
  75. \param angle The angle to rotate in degrees.
  76. \param dstwidth The calculated width of the destination surface.
  77. \param dstheight The calculated height of the destination surface.
  78. \param cangle The sine of the angle
  79. \param sangle The cosine of the angle
  80. */
  81. void
  82. SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
  83. int *dstwidth, int *dstheight,
  84. double *cangle, double *sangle)
  85. {
  86. /* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
  87. int angle90 = (int)(angle/90);
  88. if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
  89. angle90 %= 4;
  90. if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  91. if(angle90 & 1) {
  92. *dstwidth = height;
  93. *dstheight = width;
  94. *cangle = 0;
  95. *sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
  96. } else {
  97. *dstwidth = width;
  98. *dstheight = height;
  99. *cangle = angle90 == 0 ? 1 : -1;
  100. *sangle = 0;
  101. }
  102. } else {
  103. double x, y, cx, cy, sx, sy;
  104. double radangle;
  105. int dstwidthhalf, dstheighthalf;
  106. /*
  107. * Determine destination width and height by rotating a centered source box
  108. */
  109. radangle = angle * (M_PI / -180.0); /* reverse the angle because our rotations are clockwise */
  110. *sangle = SDL_sin(radangle);
  111. *cangle = SDL_cos(radangle);
  112. x = (double)(width / 2);
  113. y = (double)(height / 2);
  114. cx = *cangle * x;
  115. cy = *cangle * y;
  116. sx = *sangle * x;
  117. sy = *sangle * y;
  118. dstwidthhalf = MAX((int)
  119. SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
  120. dstheighthalf = MAX((int)
  121. SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
  122. *dstwidth = 2 * dstwidthhalf;
  123. *dstheight = 2 * dstheighthalf;
  124. }
  125. }
  126. /* Computes source pointer X/Y increments for a rotation that's a multiple of 90 degrees. */
  127. static void
  128. computeSourceIncrements90(SDL_Surface * src, int bpp, int angle, int flipx, int flipy,
  129. int *sincx, int *sincy, int *signx, int *signy)
  130. {
  131. int pitch = flipy ? -src->pitch : src->pitch;
  132. if (flipx) {
  133. bpp = -bpp;
  134. }
  135. switch (angle) { /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  136. case 0: *sincx = bpp; *sincy = pitch - src->w * *sincx; *signx = *signy = 1; break;
  137. case 1: *sincx = -pitch; *sincy = bpp - *sincx * src->h; *signx = 1; *signy = -1; break;
  138. case 2: *sincx = -bpp; *sincy = -src->w * *sincx - pitch; *signx = *signy = -1; break;
  139. case 3: default: *sincx = pitch; *sincy = -*sincx * src->h - bpp; *signx = -1; *signy = 1; break;
  140. }
  141. if (flipx) {
  142. *signx = -*signx;
  143. }
  144. if (flipy) {
  145. *signy = -*signy;
  146. }
  147. }
  148. /* Performs a relatively fast rotation/flip when the angle is a multiple of 90 degrees. */
  149. #define TRANSFORM_SURFACE_90(pixelType) \
  150. int dy, dincy = dst->pitch - dst->w*sizeof(pixelType), sincx, sincy, signx, signy; \
  151. Uint8 *sp = (Uint8*)src->pixels, *dp = (Uint8*)dst->pixels, *de; \
  152. \
  153. computeSourceIncrements90(src, sizeof(pixelType), angle, flipx, flipy, &sincx, &sincy, &signx, &signy); \
  154. if (signx < 0) sp += (src->w-1)*sizeof(pixelType); \
  155. if (signy < 0) sp += (src->h-1)*src->pitch; \
  156. \
  157. for (dy = 0; dy < dst->h; sp += sincy, dp += dincy, dy++) { \
  158. if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use memcpy */ \
  159. SDL_memcpy(dp, sp, dst->w*sizeof(pixelType)); \
  160. sp += dst->w*sizeof(pixelType); \
  161. dp += dst->w*sizeof(pixelType); \
  162. } else { \
  163. for (de = dp + dst->w*sizeof(pixelType); dp != de; sp += sincx, dp += sizeof(pixelType)) { \
  164. *(pixelType*)dp = *(pixelType*)sp; \
  165. } \
  166. } \
  167. }
  168. static void
  169. transformSurfaceRGBA90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
  170. {
  171. TRANSFORM_SURFACE_90(tColorRGBA);
  172. }
  173. static void
  174. transformSurfaceY90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
  175. {
  176. TRANSFORM_SURFACE_90(tColorY);
  177. }
  178. #undef TRANSFORM_SURFACE_90
  179. /* !
  180. \brief Internal 32 bit rotozoomer with optional anti-aliasing.
  181. Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  182. parameters by scanning the destination surface and applying optionally anti-aliasing
  183. by bilinear interpolation.
  184. Assumes src and dst surfaces are of 32 bit depth.
  185. Assumes dst surface was allocated with the correct dimensions.
  186. \param src Source surface.
  187. \param dst Destination surface.
  188. \param cx Horizontal center coordinate.
  189. \param cy Vertical center coordinate.
  190. \param isin Integer version of sine of angle.
  191. \param icos Integer version of cosine of angle.
  192. \param flipx Flag indicating horizontal mirroring should be applied.
  193. \param flipy Flag indicating vertical mirroring should be applied.
  194. \param smooth Flag indicating anti-aliasing should be used.
  195. */
  196. static void
  197. _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
  198. {
  199. int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
  200. tColorRGBA c00, c01, c10, c11, cswap;
  201. tColorRGBA *pc, *sp;
  202. int gap;
  203. /*
  204. * Variable setup
  205. */
  206. xd = ((src->w - dst->w) << 15);
  207. yd = ((src->h - dst->h) << 15);
  208. ax = (cx << 16) - (icos * cx);
  209. ay = (cy << 16) - (isin * cx);
  210. sw = src->w - 1;
  211. sh = src->h - 1;
  212. pc = (tColorRGBA*) dst->pixels;
  213. gap = dst->pitch - dst->w * 4;
  214. /*
  215. * Switch between interpolating and non-interpolating code
  216. */
  217. if (smooth) {
  218. for (y = 0; y < dst->h; y++) {
  219. dy = cy - y;
  220. sdx = (ax + (isin * dy)) + xd;
  221. sdy = (ay - (icos * dy)) + yd;
  222. for (x = 0; x < dst->w; x++) {
  223. dx = (sdx >> 16);
  224. dy = (sdy >> 16);
  225. if (flipx) dx = sw - dx;
  226. if (flipy) dy = sh - dy;
  227. if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
  228. sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
  229. c00 = *sp;
  230. sp += 1;
  231. c01 = *sp;
  232. sp += (src->pitch/4);
  233. c11 = *sp;
  234. sp -= 1;
  235. c10 = *sp;
  236. if (flipx) {
  237. cswap = c00; c00=c01; c01=cswap;
  238. cswap = c10; c10=c11; c11=cswap;
  239. }
  240. if (flipy) {
  241. cswap = c00; c00=c10; c10=cswap;
  242. cswap = c01; c01=c11; c11=cswap;
  243. }
  244. /*
  245. * Interpolate colors
  246. */
  247. ex = (sdx & 0xffff);
  248. ey = (sdy & 0xffff);
  249. t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
  250. t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
  251. pc->r = (((t2 - t1) * ey) >> 16) + t1;
  252. t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
  253. t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
  254. pc->g = (((t2 - t1) * ey) >> 16) + t1;
  255. t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
  256. t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
  257. pc->b = (((t2 - t1) * ey) >> 16) + t1;
  258. t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
  259. t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
  260. pc->a = (((t2 - t1) * ey) >> 16) + t1;
  261. }
  262. sdx += icos;
  263. sdy += isin;
  264. pc++;
  265. }
  266. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  267. }
  268. } else {
  269. for (y = 0; y < dst->h; y++) {
  270. dy = cy - y;
  271. sdx = (ax + (isin * dy)) + xd;
  272. sdy = (ay - (icos * dy)) + yd;
  273. for (x = 0; x < dst->w; x++) {
  274. dx = (sdx >> 16);
  275. dy = (sdy >> 16);
  276. if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
  277. if(flipx) dx = sw - dx;
  278. if(flipy) dy = sh - dy;
  279. *pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
  280. }
  281. sdx += icos;
  282. sdy += isin;
  283. pc++;
  284. }
  285. pc = (tColorRGBA *) ((Uint8 *) pc + gap);
  286. }
  287. }
  288. }
  289. /* !
  290. \brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
  291. Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  292. parameters by scanning the destination surface.
  293. Assumes src and dst surfaces are of 8 bit depth.
  294. Assumes dst surface was allocated with the correct dimensions.
  295. \param src Source surface.
  296. \param dst Destination surface.
  297. \param cx Horizontal center coordinate.
  298. \param cy Vertical center coordinate.
  299. \param isin Integer version of sine of angle.
  300. \param icos Integer version of cosine of angle.
  301. \param flipx Flag indicating horizontal mirroring should be applied.
  302. \param flipy Flag indicating vertical mirroring should be applied.
  303. */
  304. static void
  305. transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
  306. {
  307. int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
  308. tColorY *pc;
  309. int gap;
  310. /*
  311. * Variable setup
  312. */
  313. xd = ((src->w - dst->w) << 15);
  314. yd = ((src->h - dst->h) << 15);
  315. ax = (cx << 16) - (icos * cx);
  316. ay = (cy << 16) - (isin * cx);
  317. pc = (tColorY*) dst->pixels;
  318. gap = dst->pitch - dst->w;
  319. /*
  320. * Clear surface to colorkey
  321. */
  322. SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
  323. /*
  324. * Iterate through destination surface
  325. */
  326. for (y = 0; y < dst->h; y++) {
  327. dy = cy - y;
  328. sdx = (ax + (isin * dy)) + xd;
  329. sdy = (ay - (icos * dy)) + yd;
  330. for (x = 0; x < dst->w; x++) {
  331. dx = (sdx >> 16);
  332. dy = (sdy >> 16);
  333. if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
  334. if (flipx) dx = (src->w-1)-dx;
  335. if (flipy) dy = (src->h-1)-dy;
  336. *pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
  337. }
  338. sdx += icos;
  339. sdy += isin;
  340. pc++;
  341. }
  342. pc += gap;
  343. }
  344. }
  345. /* !
  346. \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
  347. Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
  348. 'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
  349. then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
  350. surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
  351. The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
  352. mode will set the BLEND mode on the 'dst' surface. The MOD mode either generates a white 'dst'
  353. surface and sets the colorkey or fills the it with the colorkey before copying the pixels.
  354. When using the NONE and MOD modes, color and alpha modulation must be applied before using this function.
  355. \param src The surface to rotozoom.
  356. \param angle The angle to rotate in degrees.
  357. \param centerx The horizontal coordinate of the center of rotation
  358. \param zoomy The vertical coordinate of the center of rotation
  359. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  360. \param flipx Set to 1 to flip the image horizontally
  361. \param flipy Set to 1 to flip the image vertically
  362. \param dstwidth The destination surface width
  363. \param dstheight The destination surface height
  364. \param cangle The angle cosine
  365. \param sangle The angle sine
  366. \return The new rotated surface.
  367. */
  368. SDL_Surface *
  369. 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)
  370. {
  371. SDL_Surface *rz_dst;
  372. int is8bit, angle90;
  373. int i;
  374. SDL_BlendMode blendmode;
  375. Uint32 colorkey = 0;
  376. int colorKeyAvailable = SDL_FALSE;
  377. double sangleinv, cangleinv;
  378. /* Sanity check */
  379. if (src == NULL)
  380. return NULL;
  381. if (SDL_HasColorKey(src)) {
  382. if (SDL_GetColorKey(src, &colorkey) == 0) {
  383. colorKeyAvailable = SDL_TRUE;
  384. }
  385. }
  386. /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
  387. is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
  388. if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
  389. return NULL;
  390. /* Calculate target factors from sin/cos and zoom */
  391. sangleinv = sangle*65536.0;
  392. cangleinv = cangle*65536.0;
  393. /* Alloc space to completely contain the rotated surface */
  394. rz_dst = NULL;
  395. if (is8bit) {
  396. /* Target surface is 8 bit */
  397. rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
  398. if (rz_dst != NULL) {
  399. for (i = 0; i < src->format->palette->ncolors; i++) {
  400. rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
  401. }
  402. rz_dst->format->palette->ncolors = src->format->palette->ncolors;
  403. }
  404. } else {
  405. /* Target surface is 32 bit with source RGBA ordering */
  406. rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
  407. src->format->Rmask, src->format->Gmask,
  408. src->format->Bmask, src->format->Amask);
  409. }
  410. /* Check target */
  411. if (rz_dst == NULL)
  412. return NULL;
  413. /* Adjust for guard rows */
  414. rz_dst->h = dstheight;
  415. SDL_GetSurfaceBlendMode(src, &blendmode);
  416. if (colorKeyAvailable == SDL_TRUE) {
  417. /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
  418. SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
  419. SDL_FillRect(rz_dst, NULL, colorkey);
  420. } else if (blendmode == SDL_BLENDMODE_NONE) {
  421. blendmode = SDL_BLENDMODE_BLEND;
  422. } else if (blendmode == SDL_BLENDMODE_MOD || blendmode == SDL_BLENDMODE_MUL) {
  423. /* Without a colorkey, the target texture has to be white for the MOD and MUL blend mode so
  424. * that the pixels outside the rotated area don't affect the destination surface.
  425. */
  426. colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
  427. SDL_FillRect(rz_dst, NULL, colorkey);
  428. /* Setting a white colorkey for the destination surface makes the final blit discard
  429. * all pixels outside of the rotated area. This doesn't interfere with anything because
  430. * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
  431. */
  432. SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
  433. }
  434. SDL_SetSurfaceBlendMode(rz_dst, blendmode);
  435. /* Lock source surface */
  436. if (SDL_MUSTLOCK(src)) {
  437. SDL_LockSurface(src);
  438. }
  439. /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
  440. * the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
  441. * multiples of 90 degrees.
  442. */
  443. angle90 = (int)(angle/90);
  444. if (angle90 == angle/90) {
  445. angle90 %= 4;
  446. if (angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  447. } else {
  448. angle90 = -1;
  449. }
  450. if (is8bit) {
  451. /* Call the 8-bit transformation routine to do the rotation */
  452. if(angle90 >= 0) {
  453. transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
  454. } else {
  455. transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
  456. flipx, flipy);
  457. }
  458. } else {
  459. /* Call the 32-bit transformation routine to do the rotation */
  460. if (angle90 >= 0) {
  461. transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
  462. } else {
  463. _transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
  464. flipx, flipy, smooth);
  465. }
  466. }
  467. /* Unlock source surface */
  468. if (SDL_MUSTLOCK(src)) {
  469. SDL_UnlockSurface(src);
  470. }
  471. /* Return rotated surface */
  472. return rz_dst;
  473. }