dc_rotozoom.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * File : dc_rotozoom.c
  3. * This file is part of RT-Thread GUI
  4. * COPYRIGHT (C) 2006 - 2014, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2014-03-15 Bernard porting SDL_gfx to RT-Thread GUI
  23. */
  24. /*
  25. SDL_rotozoom.c: rotozoomer, zoomer and shrinker for 32bit or 8bit surfaces
  26. Copyright (C) 2001-2012 Andreas Schiffler
  27. This software is provided 'as-is', without any express or implied
  28. warranty. In no event will the authors be held liable for any damages
  29. arising from the use of this software.
  30. Permission is granted to anyone to use this software for any purpose,
  31. including commercial applications, and to alter it and redistribute it
  32. freely, subject to the following restrictions:
  33. 1. The origin of this software must not be misrepresented; you must not
  34. claim that you wrote the original software. If you use this software
  35. in a product, an acknowledgment in the product documentation would be
  36. appreciated but is not required.
  37. 2. Altered source versions must be plainly marked as such, and must not be
  38. misrepresented as being the original software.
  39. 3. This notice may not be removed or altered from any source
  40. distribution.
  41. Andreas Schiffler -- aschiffler at ferzkopp dot net
  42. */
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <rtgui/rtgui.h>
  46. #include <rtgui/dc.h>
  47. #include <rtgui/dc_draw.h>
  48. #include <rtgui/color.h>
  49. #include <rtgui/rtgui_system.h>
  50. #include <math.h>
  51. /* ---- Internally used structures */
  52. /*!
  53. \brief A 32 bit RGBA pixel.
  54. */
  55. typedef struct tColorRGBA
  56. {
  57. rt_uint8_t r;
  58. rt_uint8_t g;
  59. rt_uint8_t b;
  60. rt_uint8_t a;
  61. } tColorRGBA;
  62. /*!
  63. \brief A 8bit Y/palette pixel.
  64. */
  65. typedef struct tColorY
  66. {
  67. rt_uint8_t y;
  68. } tColorY;
  69. /*!
  70. \brief Returns maximum of two numbers a and b.
  71. */
  72. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  73. /*!
  74. \brief Number of guard rows added to destination surfaces.
  75. This is a simple but effective workaround for observed issues.
  76. These rows allocate extra memory and are then hidden from the surface.
  77. Rows are added to the end of destination surfaces when they are allocated.
  78. This catches any potential overflows which seem to happen with
  79. just the right src image dimensions and scale/rotation and can lead
  80. to a situation where the program can segfault.
  81. */
  82. #define GUARD_ROWS (2)
  83. /*!
  84. \brief Lower limit of absolute zoom factor or rotation degrees.
  85. */
  86. #define VALUE_LIMIT 0.001
  87. void rtgui_dc_zoom_size(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight);
  88. /*!
  89. \brief Internal 32 bit integer-factor averaging Shrinker.
  90. Shrinks 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
  91. Averages color and alpha values values of src pixels to calculate dst pixels.
  92. Assumes src and dst surfaces are of 32 bit depth.
  93. Assumes dst surface was allocated with the correct dimensions.
  94. \param src The surface to shrink (input).
  95. \param dst The shrunken surface (output).
  96. \param factorx The horizontal shrinking ratio.
  97. \param factory The vertical shrinking ratio.
  98. \return 0 for success or -1 for error.
  99. */
  100. int _rtgui_dc_shrink_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst, int factorx, int factory)
  101. {
  102. int x, y, dx, dy, sgap, dgap, ra, ga, ba, aa;
  103. int n_average;
  104. tColorRGBA *sp, *osp, *oosp;
  105. tColorRGBA *dp;
  106. /*
  107. * Averaging integer shrink
  108. */
  109. /* Precalculate division factor */
  110. n_average = factorx*factory;
  111. /*
  112. * Scan destination
  113. */
  114. sp = (tColorRGBA *) src->pixel;
  115. sgap = src->pitch - src->width * 4;
  116. sgap = sgap;
  117. dp = (tColorRGBA *) dst->pixel;
  118. dgap = dst->pitch - dst->width * 4;
  119. for (y = 0; y < dst->height; y++)
  120. {
  121. osp=sp;
  122. for (x = 0; x < dst->width; x++)
  123. {
  124. /* Trace out source box and accumulate */
  125. oosp=sp;
  126. ra=ga=ba=aa=0;
  127. for (dy=0; dy < factory; dy++)
  128. {
  129. for (dx=0; dx < factorx; dx++)
  130. {
  131. ra += sp->r;
  132. ga += sp->g;
  133. ba += sp->b;
  134. aa += sp->a;
  135. sp++;
  136. }
  137. /* src dx loop */
  138. sp = (tColorRGBA *)((rt_uint8_t*)sp + (src->pitch - 4*factorx)); // next y
  139. }
  140. /* src dy loop */
  141. /* next box-x */
  142. sp = (tColorRGBA *)((rt_uint8_t*)oosp + 4*factorx);
  143. /* Store result in destination */
  144. dp->r = ra/n_average;
  145. dp->g = ga/n_average;
  146. dp->b = ba/n_average;
  147. dp->a = aa/n_average;
  148. /*
  149. * Advance destination pointer
  150. */
  151. dp++;
  152. }
  153. /* dst x loop */
  154. /* next box-y */
  155. sp = (tColorRGBA *)((rt_uint8_t*)osp + src->pitch*factory);
  156. /*
  157. * Advance destination pointers
  158. */
  159. dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
  160. }
  161. /* dst y loop */
  162. return (0);
  163. }
  164. /*!
  165. \brief Internal 32 bit Zoomer with optional anti-aliasing by bilinear interpolation.
  166. Zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface.
  167. Assumes src and dst surfaces are of 32 bit depth.
  168. Assumes dst surface was allocated with the correct dimensions.
  169. \param src The surface to zoom (input).
  170. \param dst The zoomed surface (output).
  171. \param flipx Flag indicating if the image should be horizontally flipped.
  172. \param flipy Flag indicating if the image should be vertically flipped.
  173. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  174. \return 0 for success or -1 for error.
  175. */
  176. int _rtgui_dc_zoom_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst, int flipx, int flipy, int smooth)
  177. {
  178. int x, y, sx, sy, ssx, ssy, *sax, *say, *csax, *csay, *salast, csx, csy, ex, ey, cx, cy, sstep, sstepx, sstepy;
  179. tColorRGBA *c00, *c01, *c10, *c11;
  180. tColorRGBA *sp, *csp, *dp;
  181. int spixelgap, spixelw, spixelh, dgap, t1, t2;
  182. /*
  183. * Allocate memory for row/column increments
  184. */
  185. if ((sax = (int *) rtgui_malloc((dst->width + 1) * sizeof(rt_uint32_t))) == RT_NULL)
  186. {
  187. return (-1);
  188. }
  189. if ((say = (int *) rtgui_malloc((dst->height + 1) * sizeof(rt_uint32_t))) == RT_NULL)
  190. {
  191. rtgui_free(sax);
  192. return (-1);
  193. }
  194. /*
  195. * Precalculate row increments
  196. */
  197. spixelw = (src->width - 1);
  198. spixelh = (src->height- 1);
  199. if (smooth)
  200. {
  201. sx = (int) (65536.0 * (double) spixelw / (double) (dst->width - 1));
  202. sy = (int) (65536.0 * (double) spixelh / (double) (dst->height - 1));
  203. }
  204. else
  205. {
  206. sx = (int) (65536.0 * (double) (src->width) / (double) (dst->width));
  207. sy = (int) (65536.0 * (double) (src->height) / (double) (dst->height));
  208. }
  209. /* Maximum scaled source size */
  210. ssx = (src->width << 16) - 1;
  211. ssy = (src->height << 16) - 1;
  212. /* Precalculate horizontal row increments */
  213. csx = 0;
  214. csax = sax;
  215. for (x = 0; x <= dst->width; x++)
  216. {
  217. *csax = csx;
  218. csax++;
  219. csx += sx;
  220. /* Guard from overflows */
  221. if (csx > ssx)
  222. {
  223. csx = ssx;
  224. }
  225. }
  226. /* Precalculate vertical row increments */
  227. csy = 0;
  228. csay = say;
  229. for (y = 0; y <= dst->height; y++)
  230. {
  231. *csay = csy;
  232. csay++;
  233. csy += sy;
  234. /* Guard from overflows */
  235. if (csy > ssy)
  236. {
  237. csy = ssy;
  238. }
  239. }
  240. sp = (tColorRGBA *) src->pixel;
  241. dp = (tColorRGBA *) dst->pixel;
  242. dgap = dst->pitch - dst->width * 4;
  243. spixelgap = src->pitch/4;
  244. if (flipx) sp += spixelw;
  245. if (flipy) sp += (spixelgap * spixelh);
  246. /*
  247. * Switch between interpolating and non-interpolating code
  248. */
  249. if (smooth)
  250. {
  251. /*
  252. * Interpolating Zoom
  253. */
  254. csay = say;
  255. for (y = 0; y < dst->height; y++)
  256. {
  257. csp = sp;
  258. csax = sax;
  259. for (x = 0; x < dst->width; x++)
  260. {
  261. /*
  262. * Setup color source pointers
  263. */
  264. ex = (*csax & 0xffff);
  265. ey = (*csay & 0xffff);
  266. cx = (*csax >> 16);
  267. cy = (*csay >> 16);
  268. sstepx = cx < spixelw;
  269. sstepy = cy < spixelh;
  270. c00 = sp;
  271. c01 = sp;
  272. c10 = sp;
  273. if (sstepy)
  274. {
  275. if (flipy)
  276. {
  277. c10 -= spixelgap;
  278. }
  279. else
  280. {
  281. c10 += spixelgap;
  282. }
  283. }
  284. c11 = c10;
  285. if (sstepx)
  286. {
  287. if (flipx)
  288. {
  289. c01--;
  290. c11--;
  291. }
  292. else
  293. {
  294. c01++;
  295. c11++;
  296. }
  297. }
  298. /*
  299. * Draw and interpolate colors
  300. */
  301. t1 = ((((c01->r - c00->r) * ex) >> 16) + c00->r) & 0xff;
  302. t2 = ((((c11->r - c10->r) * ex) >> 16) + c10->r) & 0xff;
  303. dp->r = (((t2 - t1) * ey) >> 16) + t1;
  304. t1 = ((((c01->g - c00->g) * ex) >> 16) + c00->g) & 0xff;
  305. t2 = ((((c11->g - c10->g) * ex) >> 16) + c10->g) & 0xff;
  306. dp->g = (((t2 - t1) * ey) >> 16) + t1;
  307. t1 = ((((c01->b - c00->b) * ex) >> 16) + c00->b) & 0xff;
  308. t2 = ((((c11->b - c10->b) * ex) >> 16) + c10->b) & 0xff;
  309. dp->b = (((t2 - t1) * ey) >> 16) + t1;
  310. t1 = ((((c01->a - c00->a) * ex) >> 16) + c00->a) & 0xff;
  311. t2 = ((((c11->a - c10->a) * ex) >> 16) + c10->a) & 0xff;
  312. dp->a = (((t2 - t1) * ey) >> 16) + t1;
  313. /*
  314. * Advance source pointer x
  315. */
  316. salast = csax;
  317. csax++;
  318. sstep = (*csax >> 16) - (*salast >> 16);
  319. if (flipx)
  320. {
  321. sp -= sstep;
  322. }
  323. else
  324. {
  325. sp += sstep;
  326. }
  327. /*
  328. * Advance destination pointer x
  329. */
  330. dp++;
  331. }
  332. /*
  333. * Advance source pointer y
  334. */
  335. salast = csay;
  336. csay++;
  337. sstep = (*csay >> 16) - (*salast >> 16);
  338. sstep *= spixelgap;
  339. if (flipy)
  340. {
  341. sp = csp - sstep;
  342. }
  343. else
  344. {
  345. sp = csp + sstep;
  346. }
  347. /*
  348. * Advance destination pointer y
  349. */
  350. dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
  351. }
  352. }
  353. else
  354. {
  355. /*
  356. * Non-Interpolating Zoom
  357. */
  358. csay = say;
  359. for (y = 0; y < dst->height; y++)
  360. {
  361. csp = sp;
  362. csax = sax;
  363. for (x = 0; x < dst->width; x++)
  364. {
  365. /*
  366. * Draw
  367. */
  368. *dp = *sp;
  369. /*
  370. * Advance source pointer x
  371. */
  372. salast = csax;
  373. csax++;
  374. sstep = (*csax >> 16) - (*salast >> 16);
  375. if (flipx) sstep = -sstep;
  376. sp += sstep;
  377. /*
  378. * Advance destination pointer x
  379. */
  380. dp++;
  381. }
  382. /*
  383. * Advance source pointer y
  384. */
  385. salast = csay;
  386. csay++;
  387. sstep = (*csay >> 16) - (*salast >> 16);
  388. sstep *= spixelgap;
  389. if (flipy) sstep = -sstep;
  390. sp = csp + sstep;
  391. /*
  392. * Advance destination pointer y
  393. */
  394. dp = (tColorRGBA *) ((rt_uint8_t *) dp + dgap);
  395. }
  396. }
  397. /*
  398. * Remove temp arrays
  399. */
  400. rtgui_free(sax);
  401. rtgui_free(say);
  402. return (0);
  403. }
  404. /*!
  405. \brief Internal 32 bit rotozoomer with optional anti-aliasing.
  406. Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
  407. parameters by scanning the destination surface and applying optionally anti-aliasing
  408. by bilinear interpolation.
  409. Assumes src and dst surfaces are of 32 bit depth.
  410. Assumes dst surface was allocated with the correct dimensions.
  411. \param src Source surface.
  412. \param dst Destination surface.
  413. \param cx Horizontal center coordinate.
  414. \param cy Vertical center coordinate.
  415. \param isin Integer version of sine of angle.
  416. \param icos Integer version of cosine of angle.
  417. \param flipx Flag indicating horizontal mirroring should be applied.
  418. \param flipy Flag indicating vertical mirroring should be applied.
  419. \param smooth Flag indicating anti-aliasing should be used.
  420. */
  421. int _rtgui_dc_transform_RGBA(struct rtgui_dc_buffer * src, struct rtgui_dc_buffer * dst,
  422. int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
  423. {
  424. int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
  425. tColorRGBA c00, c01, c10, c11, cswap;
  426. tColorRGBA *pc, *sp;
  427. int gap;
  428. /*
  429. * Variable setup
  430. */
  431. xd = ((src->width - dst->width) << 15);
  432. yd = ((src->height - dst->height) << 15);
  433. ax = (cx << 16) - (icos * cx);
  434. ay = (cy << 16) - (isin * cx);
  435. sw = src->width - 1;
  436. sh = src->height - 1;
  437. pc = (tColorRGBA*) dst->pixel;
  438. gap = dst->pitch - dst->width * 4;
  439. /*
  440. * Switch between interpolating and non-interpolating code
  441. */
  442. if (smooth)
  443. {
  444. for (y = 0; y < dst->height; y++)
  445. {
  446. dy = cy - y;
  447. sdx = (ax + (isin * dy)) + xd;
  448. sdy = (ay - (icos * dy)) + yd;
  449. for (x = 0; x < dst->width; x++)
  450. {
  451. dx = (sdx >> 16);
  452. dy = (sdy >> 16);
  453. if (flipx) dx = sw - dx;
  454. if (flipy) dy = sh - dy;
  455. if ((dx > -1) && (dy > -1) && (dx < (src->width-1)) && (dy < (src->height-1)))
  456. {
  457. sp = (tColorRGBA *)src->pixel;;
  458. sp += ((src->pitch/4) * dy);
  459. sp += dx;
  460. c00 = *sp;
  461. sp += 1;
  462. c01 = *sp;
  463. sp += (src->pitch/4);
  464. c11 = *sp;
  465. sp -= 1;
  466. c10 = *sp;
  467. if (flipx)
  468. {
  469. cswap = c00;
  470. c00=c01;
  471. c01=cswap;
  472. cswap = c10;
  473. c10=c11;
  474. c11=cswap;
  475. }
  476. if (flipy)
  477. {
  478. cswap = c00;
  479. c00=c10;
  480. c10=cswap;
  481. cswap = c01;
  482. c01=c11;
  483. c11=cswap;
  484. }
  485. /*
  486. * Interpolate colors
  487. */
  488. ex = (sdx & 0xffff);
  489. ey = (sdy & 0xffff);
  490. t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
  491. t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
  492. pc->r = (((t2 - t1) * ey) >> 16) + t1;
  493. t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
  494. t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
  495. pc->g = (((t2 - t1) * ey) >> 16) + t1;
  496. t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
  497. t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
  498. pc->b = (((t2 - t1) * ey) >> 16) + t1;
  499. t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
  500. t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
  501. pc->a = (((t2 - t1) * ey) >> 16) + t1;
  502. }
  503. sdx += icos;
  504. sdy += isin;
  505. pc++;
  506. }
  507. pc = (tColorRGBA *) ((rt_uint8_t *) pc + gap);
  508. }
  509. }
  510. else
  511. {
  512. for (y = 0; y < dst->height; y++)
  513. {
  514. dy = cy - y;
  515. sdx = (ax + (isin * dy)) + xd;
  516. sdy = (ay - (icos * dy)) + yd;
  517. for (x = 0; x < dst->width; x++)
  518. {
  519. dx = (short) (sdx >> 16);
  520. dy = (short) (sdy >> 16);
  521. if (flipx) dx = (src->width-1)-dx;
  522. if (flipy) dy = (src->height-1)-dy;
  523. if ((dx >= 0) && (dy >= 0) && (dx < src->width) && (dy < src->height))
  524. {
  525. sp = (tColorRGBA *) ((rt_uint8_t *) src->pixel + src->pitch * dy);
  526. sp += dx;
  527. *pc = *sp;
  528. }
  529. sdx += icos;
  530. sdy += isin;
  531. pc++;
  532. }
  533. pc = (tColorRGBA *) ((rt_uint8_t *) pc + gap);
  534. }
  535. }
  536. return 0;
  537. }
  538. /*!
  539. \brief Rotates a 32 bit surface in increments of 90 degrees.
  540. Specialized 90 degree rotator which rotates a 'src' surface in 90 degree
  541. increments clockwise returning a new surface. Faster than rotozoomer since
  542. not scanning or interpolation takes place. Input surface must be 32 bit.
  543. (code contributed by J. Schiller, improved by C. Allport and A. Schiffler)
  544. \param src Source surface to rotate.
  545. \param numClockwiseTurns Number of clockwise 90 degree turns to apply to the source.
  546. \returns The new, rotated surface; or RT_NULL for surfaces with incorrect input format.
  547. */
  548. struct rtgui_dc* rtgui_dc_rotate_90degrees(struct rtgui_dc_buffer* src, int numClockwiseTurns)
  549. {
  550. int row, col, newWidth, newHeight;
  551. int bpp, src_ipr, dst_ipr;
  552. struct rtgui_dc_buffer* dst;
  553. rt_uint32_t* srcBuf;
  554. rt_uint32_t* dstBuf;
  555. /* sanity check */
  556. if (src == RT_NULL) return RT_NULL;
  557. /* we only support 32bit */
  558. if (rtgui_color_get_bits(src->pixel_format) != 32) return RT_NULL;
  559. /* normalize numClockwiseTurns */
  560. while(numClockwiseTurns < 0)
  561. {
  562. numClockwiseTurns += 4;
  563. }
  564. numClockwiseTurns = (numClockwiseTurns % 4);
  565. /* if it's even, our new width will be the same as the source surface */
  566. newWidth = (numClockwiseTurns % 2) ? (src->height) : (src->width);
  567. newHeight = (numClockwiseTurns % 2) ? (src->width) : (src->height);
  568. dst = (struct rtgui_dc_buffer*) rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888, newWidth, newHeight);
  569. if(!dst) return RT_NULL;
  570. /* Calculate int-per-row */
  571. bpp = rtgui_color_get_bpp(src->pixel_format);
  572. src_ipr = src->pitch / bpp;
  573. dst_ipr = dst->pitch / bpp;
  574. switch(numClockwiseTurns)
  575. {
  576. case 0: /* Make a copy of the surface */
  577. {
  578. /* Unfortunately SDL_BlitSurface cannot be used to make a copy of the surface
  579. since it does not preserve alpha. */
  580. if (src->pitch == dst->pitch)
  581. {
  582. /* If the pitch is the same for both surfaces, the memory can be copied all at once. */
  583. memcpy(dst->pixel, src->pixel, (src->height * src->pitch));
  584. }
  585. else
  586. {
  587. /* If the pitch differs, copy each row separately */
  588. srcBuf = (rt_uint32_t*)(src->pixel);
  589. dstBuf = (rt_uint32_t*)(dst->pixel);
  590. for (row = 0; row < src->height; row++)
  591. {
  592. memcpy(dstBuf, srcBuf, dst->width * bpp);
  593. srcBuf += src_ipr;
  594. dstBuf += dst_ipr;
  595. } /* end for(col) */
  596. } /* end for(row) */
  597. }
  598. break;
  599. /* rotate clockwise */
  600. case 1: /* rotated 90 degrees clockwise */
  601. {
  602. for (row = 0; row < src->height; ++row)
  603. {
  604. srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
  605. dstBuf = (rt_uint32_t*)(dst->pixel) + (dst->width - row - 1);
  606. for (col = 0; col < src->width; ++col)
  607. {
  608. *dstBuf = *srcBuf;
  609. ++srcBuf;
  610. dstBuf += dst_ipr;
  611. }
  612. /* end for(col) */
  613. }
  614. /* end for(row) */
  615. }
  616. break;
  617. case 2: /* rotated 180 degrees clockwise */
  618. {
  619. for (row = 0; row < src->height; ++row)
  620. {
  621. srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
  622. dstBuf = (rt_uint32_t*)(dst->pixel) + ((dst->height - row - 1) * dst_ipr) + (dst->width - 1);
  623. for (col = 0; col < src->width; ++col)
  624. {
  625. *dstBuf = *srcBuf;
  626. ++srcBuf;
  627. --dstBuf;
  628. }
  629. }
  630. }
  631. break;
  632. case 3:
  633. {
  634. for (row = 0; row < src->height; ++row)
  635. {
  636. srcBuf = (rt_uint32_t*)(src->pixel) + (row * src_ipr);
  637. dstBuf = (rt_uint32_t*)(dst->pixel) + row + ((dst->height - 1) * dst_ipr);
  638. for (col = 0; col < src->width; ++col)
  639. {
  640. *dstBuf = *srcBuf;
  641. ++srcBuf;
  642. dstBuf -= dst_ipr;
  643. }
  644. }
  645. }
  646. break;
  647. }
  648. /* end switch */
  649. return RTGUI_DC(dst);
  650. }
  651. /*!
  652. \brief Internal target surface sizing function for rotozooms with trig result return.
  653. \param width The source surface width.
  654. \param height The source surface height.
  655. \param angle The angle to rotate in degrees.
  656. \param zoomx The horizontal scaling factor.
  657. \param zoomy The vertical scaling factor.
  658. \param dstwidth The calculated width of the destination surface.
  659. \param dstheight The calculated height of the destination surface.
  660. \param canglezoom The sine of the angle adjusted by the zoom factor.
  661. \param sanglezoom The cosine of the angle adjusted by the zoom factor.
  662. */
  663. void _rtgui_dc_rotozoom_size(int width, int height, double angle, double zoomx, double zoomy,
  664. int *dstwidth, int *dstheight,
  665. double *canglezoom, double *sanglezoom)
  666. {
  667. double x, y, cx, cy, sx, sy;
  668. double radangle;
  669. int dstwidthhalf, dstheighthalf;
  670. /*
  671. * Determine destination width and height by rotating a centered source box
  672. */
  673. radangle = angle * (M_PI / 180.0);
  674. *sanglezoom = sin(radangle);
  675. *canglezoom = cos(radangle);
  676. *sanglezoom *= zoomx;
  677. *canglezoom *= zoomx;
  678. x = (double)(width / 2);
  679. y = (double)(height / 2);
  680. cx = *canglezoom * x;
  681. cy = *canglezoom * y;
  682. sx = *sanglezoom * x;
  683. sy = *sanglezoom * y;
  684. dstwidthhalf = MAX((int)
  685. ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);
  686. dstheighthalf = MAX((int)
  687. ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);
  688. *dstwidth = 2 * dstwidthhalf;
  689. *dstheight = 2 * dstheighthalf;
  690. }
  691. /*!
  692. \brief Returns the size of the resulting target surface for a rotozoomSurface() call.
  693. \param width The source surface width.
  694. \param height The source surface height.
  695. \param angle The angle to rotate in degrees.
  696. \param zoom The scaling factor.
  697. \param dstwidth The calculated width of the rotozoomed destination surface.
  698. \param dstheight The calculated height of the rotozoomed destination surface.
  699. */
  700. void rtgui_dc_rotozoom_size(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight)
  701. {
  702. double dummy_sanglezoom, dummy_canglezoom;
  703. _rtgui_dc_rotozoom_size(width, height, angle, zoom, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);
  704. }
  705. /*!
  706. \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
  707. Rotates and zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  708. 'angle' is the rotation in degrees, 'zoomx and 'zoomy' scaling factors. If 'smooth' is set
  709. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  710. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  711. \param src The surface to rotozoom.
  712. \param angle The angle to rotate in degrees.
  713. \param zoomx The horizontal scaling factor.
  714. \param zoomy The vertical scaling factor.
  715. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  716. \return The new rotozoomed surface.
  717. */
  718. struct rtgui_dc *rtgui_dc_rotozoom(struct rtgui_dc *dc, double angle, double zoomx, double zoomy, int smooth)
  719. {
  720. struct rtgui_dc_buffer *rz_src;
  721. struct rtgui_dc_buffer *rz_dst;
  722. double zoominv;
  723. double sanglezoom, canglezoom, sanglezoominv, canglezoominv;
  724. int dstwidthhalf, dstwidth, dstheighthalf, dstheight;
  725. int flipx,flipy;
  726. int result;
  727. /*
  728. * Sanity check
  729. */
  730. rz_src = (struct rtgui_dc_buffer*)(dc);
  731. if (rz_src == RT_NULL) return (RT_NULL);
  732. /* we only support 32bit */
  733. if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;
  734. /*
  735. * Sanity check zoom factor
  736. */
  737. flipx = (zoomx<0.0);
  738. if (flipx) zoomx=-zoomx;
  739. flipy = (zoomy<0.0);
  740. if (flipy) zoomy=-zoomy;
  741. if (zoomx < VALUE_LIMIT) zoomx = VALUE_LIMIT;
  742. if (zoomy < VALUE_LIMIT) zoomy = VALUE_LIMIT;
  743. zoominv = 65536.0 / (zoomx * zoomx);
  744. /*
  745. * Check if we have a rotozoom or just a zoom
  746. */
  747. if (fabs(angle) > VALUE_LIMIT)
  748. {
  749. /*
  750. * Angle!=0: full rotozoom
  751. */
  752. /*
  753. * -----------------------
  754. */
  755. /* Determine target size */
  756. _rtgui_dc_rotozoom_size(rz_src->width, rz_src->height, angle, zoomx, zoomy, &dstwidth, &dstheight, &canglezoom, &sanglezoom);
  757. /*
  758. * Calculate target factors from sin/cos and zoom
  759. */
  760. sanglezoominv = sanglezoom;
  761. canglezoominv = canglezoom;
  762. sanglezoominv *= zoominv;
  763. canglezoominv *= zoominv;
  764. /* Calculate half size */
  765. dstwidthhalf = dstwidth / 2;
  766. dstheighthalf = dstheight / 2;
  767. /*
  768. * Alloc space to completely contain the rotated surface
  769. */
  770. rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
  771. dstwidth, dstheight + GUARD_ROWS);
  772. /* Check target */
  773. if (rz_dst == RT_NULL)return RT_NULL;
  774. /*
  775. * Call the 32bit transformation routine to do the rotation (using alpha)
  776. */
  777. result = _rtgui_dc_transform_RGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,
  778. (int) (sanglezoominv), (int) (canglezoominv),
  779. flipx, flipy,
  780. smooth);
  781. if (result != 0)
  782. {
  783. rtgui_dc_destory(RTGUI_DC(rz_dst));
  784. rz_dst = RT_NULL;
  785. }
  786. }
  787. else
  788. {
  789. /*
  790. * Angle=0: Just a zoom
  791. */
  792. /*
  793. * Calculate target size
  794. */
  795. rtgui_dc_zoom_size(rz_src->width, rz_src->height, zoomx, zoomy, &dstwidth, &dstheight);
  796. /*
  797. * Alloc space to completely contain the zoomed surface
  798. */
  799. rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
  800. dstwidth, dstheight + GUARD_ROWS);
  801. /* Check target */
  802. if (rz_dst == RT_NULL) return RT_NULL;
  803. /*
  804. * Call the 32bit transformation routine to do the zooming (using alpha)
  805. */
  806. result = _rtgui_dc_zoom_RGBA(rz_src, rz_dst, flipx, flipy, smooth);
  807. if (result != 0)
  808. {
  809. rtgui_dc_destory(RTGUI_DC(rz_dst));
  810. rz_dst = RT_NULL;
  811. }
  812. }
  813. /*
  814. * Return destination surface
  815. */
  816. return RTGUI_DC(rz_dst);
  817. }
  818. RTM_EXPORT(rtgui_dc_rotozoom);
  819. /*!
  820. \brief Calculates the size of the target surface for a rtgui_dc_zoom() call.
  821. The minimum size of the target surface is 1. The input factors can be positive or negative.
  822. \param width The width of the source surface to zoom.
  823. \param height The height of the source surface to zoom.
  824. \param zoomx The horizontal zoom factor.
  825. \param zoomy The vertical zoom factor.
  826. \param dstwidth Pointer to an integer to store the calculated width of the zoomed target surface.
  827. \param dstheight Pointer to an integer to store the calculated height of the zoomed target surface.
  828. */
  829. void rtgui_dc_zoom_size(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight)
  830. {
  831. /*
  832. * Make zoom factors positive
  833. */
  834. int flipx, flipy;
  835. flipx = (zoomx<0.0);
  836. if (flipx) zoomx = -zoomx;
  837. flipy = (zoomy<0.0);
  838. if (flipy) zoomy = -zoomy;
  839. /*
  840. * Sanity check zoom factors
  841. */
  842. if (zoomx < VALUE_LIMIT)
  843. {
  844. zoomx = VALUE_LIMIT;
  845. }
  846. if (zoomy < VALUE_LIMIT)
  847. {
  848. zoomy = VALUE_LIMIT;
  849. }
  850. /*
  851. * Calculate target size
  852. */
  853. *dstwidth = (int) floor(((double) width * zoomx) + 0.5);
  854. *dstheight = (int) floor(((double) height * zoomy) + 0.5);
  855. if (*dstwidth < 1)
  856. {
  857. *dstwidth = 1;
  858. }
  859. if (*dstheight < 1)
  860. {
  861. *dstheight = 1;
  862. }
  863. }
  864. /*!
  865. \brief Zoom a surface by independent horizontal and vertical factors with optional smoothing.
  866. Zooms a 32bit or 8bit 'src' surface to newly created 'dst' surface.
  867. 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is on
  868. then the destination 32bit surface is anti-aliased. If the surface is not 8bit
  869. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  870. If zoom factors are negative, the image is flipped on the axes.
  871. \param src The surface to zoom.
  872. \param zoomx The horizontal zoom factor.
  873. \param zoomy The vertical zoom factor.
  874. \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
  875. \return The new, zoomed surface.
  876. */
  877. struct rtgui_dc *rtgui_dc_zoom(struct rtgui_dc *dc, double zoomx, double zoomy, int smooth)
  878. {
  879. struct rtgui_dc_buffer *rz_src;
  880. struct rtgui_dc_buffer *rz_dst;
  881. int dstwidth, dstheight;
  882. int flipx, flipy;
  883. /* Sanity check */
  884. if (dc == RT_NULL) return dc;
  885. rz_src = (struct rtgui_dc_buffer*)(dc);
  886. /* we only support 32bit */
  887. if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;
  888. flipx = (zoomx<0.0);
  889. if (flipx) zoomx = -zoomx;
  890. flipy = (zoomy<0.0);
  891. if (flipy) zoomy = -zoomy;
  892. /* Get size if target */
  893. rtgui_dc_zoom_size(rz_src->width, rz_src->height, zoomx, zoomy, &dstwidth, &dstheight);
  894. /*
  895. * Alloc space to completely contain the zoomed surface
  896. */
  897. rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
  898. dstwidth, dstheight + GUARD_ROWS);
  899. /* Check target */
  900. if (rz_dst == RT_NULL) return RT_NULL;
  901. /*
  902. * Call the 32bit transformation routine to do the zooming (using alpha)
  903. */
  904. if (_rtgui_dc_zoom_RGBA(rz_src, rz_dst, flipx, flipy, smooth) != 0)
  905. {
  906. rtgui_dc_destory(RTGUI_DC(rz_dst));
  907. rz_dst = RT_NULL;
  908. }
  909. /*
  910. * Return destination surface
  911. */
  912. return RTGUI_DC(rz_dst);
  913. }
  914. RTM_EXPORT(rtgui_dc_zoom);
  915. /*!
  916. \brief Shrink a dc by an integer ratio using averaging.
  917. Shrinks a 32bit or 8bit 'src' buffer dc to a newly created 'dst' dc.
  918. 'factorx' and 'factory' are the shrinking ratios (i.e. 2=1/2 the size,
  919. 3=1/3 the size, etc.) The destination dc is antialiased by averaging
  920. the source box RGBA or Y information. If the surface is not 8bit
  921. or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
  922. The input surface is not modified. The output surface is newly allocated.
  923. \param src The surface to shrink.
  924. \param factorx The horizontal shrinking ratio.
  925. \param factory The vertical shrinking ratio.
  926. \return The new, shrunken surface.
  927. */
  928. struct rtgui_dc *rtgui_dc_shrink(struct rtgui_dc *dc, int factorx, int factory)
  929. {
  930. struct rtgui_dc_buffer *rz_src;
  931. struct rtgui_dc_buffer *rz_dst;
  932. int dstwidth, dstheight;
  933. /*
  934. * Sanity check
  935. */
  936. if (dc == RT_NULL) return dc;
  937. rz_src = (struct rtgui_dc_buffer*)(dc);
  938. /* we only support 32bit */
  939. if (rtgui_color_get_bits(rz_src->pixel_format) != 32) return RT_NULL;
  940. /* Get size for target */
  941. dstwidth=rz_src->width/factorx;
  942. while (dstwidth*factorx>rz_src->width)
  943. {
  944. dstwidth--;
  945. }
  946. dstheight=rz_src->height/factory;
  947. while (dstheight*factory>rz_src->height)
  948. {
  949. dstheight--;
  950. }
  951. /*
  952. * Target surface is 32bit with source RGBA/ABGR ordering
  953. */
  954. rz_dst = (struct rtgui_dc_buffer*)rtgui_dc_buffer_create_pixformat(RTGRAPHIC_PIXEL_FORMAT_ARGB888,
  955. dstwidth, dstheight + GUARD_ROWS);
  956. /* Check target */
  957. if (rz_dst == RT_NULL) return RT_NULL;
  958. /*
  959. * Call the 32bit transformation routine to do the shrinking (using alpha)
  960. */
  961. if (_rtgui_dc_shrink_RGBA(rz_src, rz_dst, factorx, factory) != 0)
  962. {
  963. rtgui_dc_destory(RTGUI_DC(rz_dst));
  964. rz_dst = RT_NULL;
  965. }
  966. /*
  967. * Return destination surface
  968. */
  969. return RTGUI_DC(rz_dst);
  970. }
  971. RTM_EXPORT(rtgui_dc_shrink);