SDL_rotate.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED
  23. #if defined(__WIN32__) || defined(__GDK__)
  24. #include "../../core/windows/SDL_windows.h"
  25. #endif
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "SDL_rotate.h"
  29. /* ---- Internally used structures */
  30. /* !
  31. \brief A 32 bit RGBA pixel.
  32. */
  33. typedef struct tColorRGBA
  34. {
  35. Uint8 r;
  36. Uint8 g;
  37. Uint8 b;
  38. Uint8 a;
  39. } tColorRGBA;
  40. /* !
  41. \brief A 8bit Y/palette pixel.
  42. */
  43. typedef struct tColorY
  44. {
  45. Uint8 y;
  46. } tColorY;
  47. /* !
  48. \brief Number of guard rows added to destination surfaces.
  49. This is a simple but effective workaround for observed issues.
  50. These rows allocate extra memory and are then hidden from the surface.
  51. Rows are added to the end of destination surfaces when they are allocated.
  52. This catches any potential overflows which seem to happen with
  53. just the right src image dimensions and scale/rotation and can lead
  54. to a situation where the program can segfault.
  55. */
  56. #define GUARD_ROWS (2)
  57. /* !
  58. \brief Returns colorkey info for a surface
  59. */
  60. static Uint32 get_colorkey(SDL_Surface *src)
  61. {
  62. Uint32 key = 0;
  63. if (SDL_SurfaceHasColorKey(src)) {
  64. SDL_GetSurfaceColorKey(src, &key);
  65. }
  66. return key;
  67. }
  68. /* rotate (sx, sy) by (angle, center) into (dx, dy) */
  69. static void rotate(double sx, double sy, double sinangle, double cosangle, const SDL_FPoint *center, double *dx, double *dy)
  70. {
  71. sx -= center->x;
  72. sy -= center->y;
  73. *dx = cosangle * sx - sinangle * sy;
  74. *dy = sinangle * sx + cosangle * sy;
  75. *dx += center->x;
  76. *dy += center->y;
  77. }
  78. /* !
  79. \brief Internal target surface sizing function for rotations with trig result return.
  80. \param width The source surface width.
  81. \param height The source surface height.
  82. \param angle The angle to rotate in degrees.
  83. \param dstwidth The calculated width of the destination surface.
  84. \param dstheight The calculated height of the destination surface.
  85. \param cangle The sine of the angle
  86. \param sangle The cosine of the angle
  87. */
  88. void SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle, const SDL_FPoint *center,
  89. SDL_Rect *rect_dest, double *cangle, double *sangle)
  90. {
  91. int minx, maxx, miny, maxy;
  92. double radangle;
  93. double x0, x1, x2, x3;
  94. double y0, y1, y2, y3;
  95. double sinangle;
  96. double cosangle;
  97. radangle = angle * (SDL_PI_D / 180.0);
  98. sinangle = SDL_sin(radangle);
  99. cosangle = SDL_cos(radangle);
  100. /*
  101. * Determine destination width and height by rotating a source box, at pixel center
  102. */
  103. rotate(0.5, 0.5, sinangle, cosangle, center, &x0, &y0);
  104. rotate(width - 0.5, 0.5, sinangle, cosangle, center, &x1, &y1);
  105. rotate(0.5, height - 0.5, sinangle, cosangle, center, &x2, &y2);
  106. rotate(width - 0.5, height - 0.5, sinangle, cosangle, center, &x3, &y3);
  107. minx = (int)SDL_floor(SDL_min(SDL_min(x0, x1), SDL_min(x2, x3)));
  108. maxx = (int)SDL_ceil(SDL_max(SDL_max(x0, x1), SDL_max(x2, x3)));
  109. miny = (int)SDL_floor(SDL_min(SDL_min(y0, y1), SDL_min(y2, y3)));
  110. maxy = (int)SDL_ceil(SDL_max(SDL_max(y0, y1), SDL_max(y2, y3)));
  111. rect_dest->w = maxx - minx;
  112. rect_dest->h = maxy - miny;
  113. rect_dest->x = minx;
  114. rect_dest->y = miny;
  115. /* reverse the angle because our rotations are clockwise */
  116. *sangle = -sinangle;
  117. *cangle = cosangle;
  118. {
  119. /* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
  120. int angle90 = (int)(angle / 90);
  121. if (angle90 == angle / 90) { /* if the angle is a multiple of 90 degrees */
  122. angle90 %= 4;
  123. if (angle90 < 0) {
  124. angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  125. }
  126. if (angle90 & 1) {
  127. rect_dest->w = height;
  128. rect_dest->h = width;
  129. *cangle = 0;
  130. *sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
  131. } else {
  132. rect_dest->w = width;
  133. rect_dest->h = height;
  134. *cangle = angle90 == 0 ? 1 : -1;
  135. *sangle = 0;
  136. }
  137. }
  138. }
  139. }
  140. /* Computes source pointer X/Y increments for a rotation that's a multiple of 90 degrees. */
  141. static void computeSourceIncrements90(SDL_Surface *src, int bpp, int angle, int flipx, int flipy,
  142. int *sincx, int *sincy, int *signx, int *signy)
  143. {
  144. int pitch = flipy ? -src->pitch : src->pitch;
  145. if (flipx) {
  146. bpp = -bpp;
  147. }
  148. switch (angle) { /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  149. case 0:
  150. *sincx = bpp;
  151. *sincy = pitch - src->w * *sincx;
  152. *signx = *signy = 1;
  153. break;
  154. case 1:
  155. *sincx = -pitch;
  156. *sincy = bpp - *sincx * src->h;
  157. *signx = 1;
  158. *signy = -1;
  159. break;
  160. case 2:
  161. *sincx = -bpp;
  162. *sincy = -src->w * *sincx - pitch;
  163. *signx = *signy = -1;
  164. break;
  165. case 3:
  166. default:
  167. *sincx = pitch;
  168. *sincy = -*sincx * src->h - bpp;
  169. *signx = -1;
  170. *signy = 1;
  171. break;
  172. }
  173. if (flipx) {
  174. *signx = -*signx;
  175. }
  176. if (flipy) {
  177. *signy = -*signy;
  178. }
  179. }
  180. /* Performs a relatively fast rotation/flip when the angle is a multiple of 90 degrees. */
  181. #define TRANSFORM_SURFACE_90(pixelType) \
  182. int dy, dincy = dst->pitch - dst->w * sizeof(pixelType), sincx, sincy, signx, signy; \
  183. Uint8 *sp = (Uint8 *)src->pixels, *dp = (Uint8 *)dst->pixels, *de; \
  184. \
  185. computeSourceIncrements90(src, sizeof(pixelType), angle, flipx, flipy, &sincx, &sincy, &signx, &signy); \
  186. if (signx < 0) \
  187. sp += (src->w - 1) * sizeof(pixelType); \
  188. if (signy < 0) \
  189. sp += (src->h - 1) * src->pitch; \
  190. \
  191. for (dy = 0; dy < dst->h; sp += sincy, dp += dincy, dy++) { \
  192. if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use SDL_memcpy */ \
  193. SDL_memcpy(dp, sp, dst->w * sizeof(pixelType)); \
  194. sp += dst->w * sizeof(pixelType); \
  195. dp += dst->w * sizeof(pixelType); \
  196. } else { \
  197. for (de = dp + dst->w * sizeof(pixelType); dp != de; sp += sincx, dp += sizeof(pixelType)) { \
  198. *(pixelType *)dp = *(pixelType *)sp; \
  199. } \
  200. } \
  201. }
  202. static void transformSurfaceRGBA90(SDL_Surface *src, SDL_Surface *dst, int angle, int flipx, int flipy)
  203. {
  204. TRANSFORM_SURFACE_90(tColorRGBA);
  205. }
  206. static void transformSurfaceY90(SDL_Surface *src, SDL_Surface *dst, int angle, int flipx, int flipy)
  207. {
  208. TRANSFORM_SURFACE_90(tColorY);
  209. }
  210. #undef TRANSFORM_SURFACE_90
  211. /* !
  212. \brief Internal 32 bit rotozoomer with optional anti-aliasing.
  213. Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  214. parameters by scanning the destination surface and applying optionally anti-aliasing
  215. by bilinear interpolation.
  216. Assumes src and dst surfaces are of 32 bit depth.
  217. Assumes dst surface was allocated with the correct dimensions.
  218. \param src Source surface.
  219. \param dst Destination surface.
  220. \param isin Integer version of sine of angle.
  221. \param icos Integer version of cosine of angle.
  222. \param flipx Flag indicating horizontal mirroring should be applied.
  223. \param flipy Flag indicating vertical mirroring should be applied.
  224. \param smooth Flag indicating anti-aliasing should be used.
  225. \param dst_rect destination coordinates
  226. \param center true center.
  227. */
  228. static void transformSurfaceRGBA(SDL_Surface *src, SDL_Surface *dst, int isin, int icos,
  229. int flipx, int flipy, int smooth,
  230. const SDL_Rect *rect_dest,
  231. const SDL_FPoint *center)
  232. {
  233. int sw, sh;
  234. int cx, cy;
  235. tColorRGBA c00, c01, c10, c11, cswap;
  236. tColorRGBA *pc, *sp;
  237. int gap;
  238. const int fp_half = (1 << 15);
  239. /*
  240. * Variable setup
  241. */
  242. sw = src->w - 1;
  243. sh = src->h - 1;
  244. pc = (tColorRGBA *)dst->pixels;
  245. gap = dst->pitch - dst->w * 4;
  246. cx = (int)(center->x * 65536.0);
  247. cy = (int)(center->y * 65536.0);
  248. /*
  249. * Switch between interpolating and non-interpolating code
  250. */
  251. if (smooth) {
  252. int y;
  253. for (y = 0; y < dst->h; y++) {
  254. int x;
  255. double src_x = (rect_dest->x + 0 + 0.5 - center->x);
  256. double src_y = (rect_dest->y + y + 0.5 - center->y);
  257. int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
  258. int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
  259. for (x = 0; x < dst->w; x++) {
  260. int dx = (sdx >> 16);
  261. int dy = (sdy >> 16);
  262. if (flipx) {
  263. dx = sw - dx;
  264. }
  265. if (flipy) {
  266. dy = sh - dy;
  267. }
  268. if ((dx > -1) && (dy > -1) && (dx < (src->w - 1)) && (dy < (src->h - 1))) {
  269. int ex, ey;
  270. int t1, t2;
  271. sp = (tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx;
  272. c00 = *sp;
  273. sp += 1;
  274. c01 = *sp;
  275. sp += (src->pitch / 4);
  276. c11 = *sp;
  277. sp -= 1;
  278. c10 = *sp;
  279. if (flipx) {
  280. cswap = c00;
  281. c00 = c01;
  282. c01 = cswap;
  283. cswap = c10;
  284. c10 = c11;
  285. c11 = cswap;
  286. }
  287. if (flipy) {
  288. cswap = c00;
  289. c00 = c10;
  290. c10 = cswap;
  291. cswap = c01;
  292. c01 = c11;
  293. c11 = cswap;
  294. }
  295. /*
  296. * Interpolate colors
  297. */
  298. ex = (sdx & 0xffff);
  299. ey = (sdy & 0xffff);
  300. t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
  301. t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
  302. pc->r = (((t2 - t1) * ey) >> 16) + t1;
  303. t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
  304. t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
  305. pc->g = (((t2 - t1) * ey) >> 16) + t1;
  306. t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
  307. t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
  308. pc->b = (((t2 - t1) * ey) >> 16) + t1;
  309. t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
  310. t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
  311. pc->a = (((t2 - t1) * ey) >> 16) + t1;
  312. }
  313. sdx += icos;
  314. sdy += isin;
  315. pc++;
  316. }
  317. pc = (tColorRGBA *)((Uint8 *)pc + gap);
  318. }
  319. } else {
  320. int y;
  321. for (y = 0; y < dst->h; y++) {
  322. int x;
  323. double src_x = (rect_dest->x + 0 + 0.5 - center->x);
  324. double src_y = (rect_dest->y + y + 0.5 - center->y);
  325. int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
  326. int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
  327. for (x = 0; x < dst->w; x++) {
  328. int dx = (sdx >> 16);
  329. int dy = (sdy >> 16);
  330. if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
  331. if (flipx) {
  332. dx = sw - dx;
  333. }
  334. if (flipy) {
  335. dy = sh - dy;
  336. }
  337. *pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
  338. }
  339. sdx += icos;
  340. sdy += isin;
  341. pc++;
  342. }
  343. pc = (tColorRGBA *)((Uint8 *)pc + gap);
  344. }
  345. }
  346. }
  347. /* !
  348. \brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
  349. Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  350. parameters by scanning the destination surface.
  351. Assumes src and dst surfaces are of 8 bit depth.
  352. Assumes dst surface was allocated with the correct dimensions.
  353. \param src Source surface.
  354. \param dst Destination surface.
  355. \param isin Integer version of sine of angle.
  356. \param icos Integer version of cosine of angle.
  357. \param flipx Flag indicating horizontal mirroring should be applied.
  358. \param flipy Flag indicating vertical mirroring should be applied.
  359. \param dst_rect destination coordinates
  360. \param center true center.
  361. */
  362. static void transformSurfaceY(SDL_Surface *src, SDL_Surface *dst, int isin, int icos, int flipx, int flipy,
  363. const SDL_Rect *rect_dest,
  364. const SDL_FPoint *center)
  365. {
  366. int sw, sh;
  367. int cx, cy;
  368. tColorY *pc;
  369. int gap;
  370. const int fp_half = (1 << 15);
  371. int y;
  372. /*
  373. * Variable setup
  374. */
  375. sw = src->w - 1;
  376. sh = src->h - 1;
  377. pc = (tColorY *)dst->pixels;
  378. gap = dst->pitch - dst->w;
  379. cx = (int)(center->x * 65536.0);
  380. cy = (int)(center->y * 65536.0);
  381. /*
  382. * Clear surface to colorkey
  383. */
  384. SDL_memset(pc, (int)(get_colorkey(src) & 0xff), (size_t)dst->pitch * dst->h);
  385. /*
  386. * Iterate through destination surface
  387. */
  388. for (y = 0; y < dst->h; y++) {
  389. int x;
  390. double src_x = (rect_dest->x + 0 + 0.5 - center->x);
  391. double src_y = (rect_dest->y + y + 0.5 - center->y);
  392. int sdx = (int)((icos * src_x - isin * src_y) + cx - fp_half);
  393. int sdy = (int)((isin * src_x + icos * src_y) + cy - fp_half);
  394. for (x = 0; x < dst->w; x++) {
  395. int dx = (sdx >> 16);
  396. int dy = (sdy >> 16);
  397. if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
  398. if (flipx) {
  399. dx = sw - dx;
  400. }
  401. if (flipy) {
  402. dy = sh - dy;
  403. }
  404. *pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
  405. }
  406. sdx += icos;
  407. sdy += isin;
  408. pc++;
  409. }
  410. pc += gap;
  411. }
  412. }
  413. /* !
  414. \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
  415. Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
  416. 'angle' is the rotation in degrees, 'center' the rotation center. If 'smooth' is set
  417. then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
  418. surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
  419. The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
  420. mode will set the BLEND mode on the 'dst' surface. The MOD mode either generates a white 'dst'
  421. surface and sets the colorkey or fills the it with the colorkey before copying the pixels.
  422. When using the NONE and MOD modes, color and alpha modulation must be applied before using this function.
  423. \param src The surface to rotozoom.
  424. \param angle The angle to rotate in degrees.
  425. \param zoomy The vertical coordinate of the center of rotation
  426. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  427. \param flipx Set to 1 to flip the image horizontally
  428. \param flipy Set to 1 to flip the image vertically
  429. \param rect_dest The destination rect bounding box
  430. \param cangle The angle cosine
  431. \param sangle The angle sine
  432. \param center The true coordinate of the center of rotation
  433. \return The new rotated surface.
  434. */
  435. SDL_Surface *
  436. SDLgfx_rotateSurface(SDL_Surface *src, double angle, int smooth, int flipx, int flipy,
  437. const SDL_Rect *rect_dest, double cangle, double sangle, const SDL_FPoint *center)
  438. {
  439. SDL_Surface *rz_dst;
  440. int is8bit, angle90;
  441. int i;
  442. SDL_BlendMode blendmode;
  443. Uint32 colorkey = 0;
  444. int colorKeyAvailable = SDL_FALSE;
  445. double sangleinv, cangleinv;
  446. /* Sanity check */
  447. if (src == NULL) {
  448. return NULL;
  449. }
  450. if (SDL_SurfaceHasColorKey(src)) {
  451. if (SDL_GetSurfaceColorKey(src, &colorkey) == 0) {
  452. colorKeyAvailable = SDL_TRUE;
  453. }
  454. }
  455. /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
  456. is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
  457. if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask))) {
  458. return NULL;
  459. }
  460. /* Calculate target factors from sine/cosine and zoom */
  461. sangleinv = sangle * 65536.0;
  462. cangleinv = cangle * 65536.0;
  463. /* Alloc space to completely contain the rotated surface */
  464. rz_dst = NULL;
  465. if (is8bit) {
  466. /* Target surface is 8 bit */
  467. rz_dst = SDL_CreateSurface(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format->format);
  468. if (rz_dst != NULL) {
  469. if (src->format->palette) {
  470. for (i = 0; i < src->format->palette->ncolors; i++) {
  471. rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
  472. }
  473. rz_dst->format->palette->ncolors = src->format->palette->ncolors;
  474. }
  475. }
  476. } else {
  477. /* Target surface is 32 bit with source RGBA ordering */
  478. rz_dst = SDL_CreateSurface(rect_dest->w, rect_dest->h + GUARD_ROWS, src->format->format);
  479. }
  480. /* Check target */
  481. if (rz_dst == NULL) {
  482. return NULL;
  483. }
  484. /* Adjust for guard rows */
  485. rz_dst->h = rect_dest->h;
  486. SDL_GetSurfaceBlendMode(src, &blendmode);
  487. if (colorKeyAvailable == SDL_TRUE) {
  488. /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
  489. SDL_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey);
  490. SDL_FillSurfaceRect(rz_dst, NULL, colorkey);
  491. } else if (blendmode == SDL_BLENDMODE_NONE) {
  492. blendmode = SDL_BLENDMODE_BLEND;
  493. } else if (blendmode == SDL_BLENDMODE_MOD || blendmode == SDL_BLENDMODE_MUL) {
  494. /* Without a colorkey, the target texture has to be white for the MOD and MUL blend mode so
  495. * that the pixels outside the rotated area don't affect the destination surface.
  496. */
  497. colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
  498. SDL_FillSurfaceRect(rz_dst, NULL, colorkey);
  499. /* Setting a white colorkey for the destination surface makes the final blit discard
  500. * all pixels outside of the rotated area. This doesn't interfere with anything because
  501. * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
  502. */
  503. SDL_SetSurfaceColorKey(rz_dst, SDL_TRUE, colorkey);
  504. }
  505. SDL_SetSurfaceBlendMode(rz_dst, blendmode);
  506. /* Lock source surface */
  507. if (SDL_MUSTLOCK(src)) {
  508. SDL_LockSurface(src);
  509. }
  510. /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
  511. * the off-by-one problem in transformSurfaceRGBA that expresses itself when the rotation is near
  512. * multiples of 90 degrees.
  513. */
  514. angle90 = (int)(angle / 90);
  515. if (angle90 == angle / 90) {
  516. angle90 %= 4;
  517. if (angle90 < 0) {
  518. angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
  519. }
  520. } else {
  521. angle90 = -1;
  522. }
  523. if (is8bit) {
  524. /* Call the 8-bit transformation routine to do the rotation */
  525. if (angle90 >= 0) {
  526. transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
  527. } else {
  528. transformSurfaceY(src, rz_dst, (int)sangleinv, (int)cangleinv,
  529. flipx, flipy, rect_dest, center);
  530. }
  531. } else {
  532. /* Call the 32-bit transformation routine to do the rotation */
  533. if (angle90 >= 0) {
  534. transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
  535. } else {
  536. transformSurfaceRGBA(src, rz_dst, (int)sangleinv, (int)cangleinv,
  537. flipx, flipy, smooth, rect_dest, center);
  538. }
  539. }
  540. /* Unlock source surface */
  541. if (SDL_MUSTLOCK(src)) {
  542. SDL_UnlockSurface(src);
  543. }
  544. /* Return rotated surface */
  545. return rz_dst;
  546. }
  547. #endif /* SDL_VIDEO_RENDER_SW && !SDL_RENDER_DISABLED */