SDL_rotate.c 22 KB

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