SDL_rotate.c 22 KB

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