sw_math.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2020 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Utilities of sw math API
  9. */
  10. #ifndef ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_
  11. #define ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. /**
  15. * @defgroup display-util Display Utilities
  16. * @ingroup display_libraries
  17. * @{
  18. */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* fixedpoint with 16-bit fraction */
  23. typedef int32_t fixedpoint16_t;
  24. /* convert integer to fixedpoint-1 */
  25. #define FIXEDPOINT1(x) to_fixedpoint(x, 1)
  26. #define FIXEDPOINT1_0_5 (FIXEDPOINT1(1) >> 1)
  27. /* convert pixel coordinate to fixedpoint-1 coordinate */
  28. #define PX_FIXEDPOINT1(x) (FIXEDPOINT1(x) + (FIXEDPOINT1(1) >> 1))
  29. /* convert fixedpoint-1 to integer */
  30. #define FLOOR_FIXEDPOINT1(x) floor_fixedpoint(x, 1)
  31. #define ROUND_FIXEDPOINT1(x) round_fixedpoint(x, 1)
  32. #define CEIL_FIXEDPOINT1(x) ceil_fixedpoint(x, 1)
  33. /* convert integer to fixedpoint-12 */
  34. #define FIXEDPOINT12(x) to_fixedpoint(x, 12)
  35. #define FIXEDPOINT12_0_5 (FIXEDPOINT12(1) >> 1)
  36. /* convert pixel coordinate to fixedpoint-12 coordinate */
  37. #define PX_FIXEDPOINT12(x) (FIXEDPOINT12(x) + (FIXEDPOINT12(1) >> 1))
  38. /* convert fixedpoint-12 to integer */
  39. #define FLOOR_FIXEDPOINT12(x) floor_fixedpoint(x, 12)
  40. #define ROUND_FIXEDPOINT12(x) round_fixedpoint(x, 12)
  41. /* convert integer to fixedpoint-16 */
  42. #define FIXEDPOINT16(x) to_fixedpoint(x, 16)
  43. #define FIXEDPOINT16_0_5 (FIXEDPOINT16(1) >> 1)
  44. /* convert pixel coordinate to fixedpoint-16 coordinate */
  45. #define PX_FIXEDPOINT16(x) (FIXEDPOINT16(x) + (FIXEDPOINT16(1) >> 1))
  46. /* convert fixedpoint-16 to integer */
  47. #define FLOOR_FIXEDPOINT16(x) floor_fixedpoint(x, 16)
  48. #define ROUND_FIXEDPOINT16(x) round_fixedpoint(x, 16)
  49. /* Convert to fixedpoint */
  50. static inline int32_t to_fixedpoint(int32_t x, int fraction_bits)
  51. {
  52. return x << fraction_bits;
  53. }
  54. /* Round fixedpoint towards nearest integer (round to positive infinity). */
  55. static inline int32_t round_fixedpoint(int32_t x, int fraction_bits)
  56. {
  57. return (x + (1 << (fraction_bits - 1))) >> fraction_bits;
  58. }
  59. /* Round fixedpoint towards negative infinity. */
  60. static inline int32_t floor_fixedpoint(int32_t x, int fraction_bits)
  61. {
  62. return x >> fraction_bits;
  63. }
  64. /* Round fixedpoint towards positive infinity. */
  65. static inline int32_t ceil_fixedpoint(int32_t x, int fraction_bits)
  66. {
  67. return (x + ((1 << fraction_bits) - 1)) >> fraction_bits;
  68. }
  69. /* Round fixedpoint towards zero. */
  70. static inline int32_t fix_fixedpoint(int32_t x, int fraction_bits)
  71. {
  72. if (x >= 0)
  73. return floor_fixedpoint(x, fraction_bits);
  74. else
  75. return ceil_fixedpoint(x, fraction_bits);
  76. }
  77. /* Integer part of fixedpoint */
  78. static inline int32_t fixedpoint_int(int32_t x, int fraction_bits)
  79. {
  80. //return to_fixedpoint(floor_fixedpoint(x, fraction_bits));
  81. return x & ~((1 << fraction_bits) - 1);
  82. }
  83. /* Fraction part of fixedpoint */
  84. static inline int32_t fixedpoint_frac(int32_t x, int fraction_bits)
  85. {
  86. return x - fixedpoint_int(x, fraction_bits);
  87. }
  88. /*
  89. * @brief compute cos
  90. *
  91. * @param angle angle in 0.1 degree [0, 3600]
  92. *
  93. * @retval result in fixedpoint-30
  94. */
  95. int32_t sw_cos30(uint16_t angle);
  96. /*
  97. * @brief compute sin
  98. *
  99. * @param angle angle in 0.1 degree [0, 3600]
  100. *
  101. * @retval result in fixedpoint-30
  102. */
  103. int32_t sw_sin30(uint16_t angle);
  104. /*
  105. * @brief compute cos
  106. *
  107. * @param angle angle in 0.1 degree [0, 3600]
  108. *
  109. * @retval result in fixedpoint-14
  110. */
  111. int16_t sw_cos14(uint16_t angle);
  112. /*
  113. * @brief compute sin
  114. *
  115. * @param angle angle in 0.1 degree [0, 3600]
  116. *
  117. * @retval result in fixedpoint-14
  118. */
  119. int16_t sw_sin14(uint16_t angle);
  120. /*
  121. * @brief transform a 32-bit point with scaling first
  122. *
  123. * @param dest_x x coord of the point after rotation
  124. * @param dest_y y coord of the point after rotation
  125. * @param src_x x coord of the point before rotation
  126. * @param src_y y coord of the point image before rotation
  127. * @param pivot_x x coord of rotation pivot
  128. * @param pivot_y y coord of rotation pivot
  129. * @param angle rotation angle in 0.1 degree [0, 3600)
  130. * @param scale_x scale factor X, equal to "dest_w /src_w"
  131. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  132. * @param scale_bits scale factor bits of scale_x and scale_y
  133. *
  134. * @retval N/A
  135. */
  136. void sw_transform_point32(int32_t *dest_x, int32_t *dest_y,
  137. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y,
  138. uint16_t angle, uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  139. /*
  140. * @brief transform a 32-bit point with rotation first
  141. *
  142. * @param dest_x x coord of the point after rotation
  143. * @param dest_y y coord of the point after rotation
  144. * @param src_x x coord of the point before rotation
  145. * @param src_y y coord of the point image before rotation
  146. * @param pivot_x x coord of rotation pivot
  147. * @param pivot_y y coord of rotation pivot
  148. * @param angle rotation angle in 0.1 degree [0, 3600)
  149. * @param scale_x scale factor X, equal to "dest_w /src_w"
  150. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  151. * @param scale_bits scale factor bits of scale_x and scale_y
  152. *
  153. * @retval N/A
  154. */
  155. void sw_transform_point32_rot_first(int32_t *dest_x, int32_t *dest_y,
  156. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y,
  157. uint16_t angle, uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  158. /*
  159. * @brief rotate a 32-bit point
  160. *
  161. * @param dest_x x coord of the point after rotation
  162. * @param dest_y y coord of the point after rotation
  163. * @param src_x x coord of the point before rotation
  164. * @param src_y y coord of the point image before rotation
  165. * @param pivot_x x coord of rotation pivot
  166. * @param pivot_y y coord of rotation pivot
  167. * @param angle rotation angle in 0.1 degree [0, 360)
  168. *
  169. * @retval N/A
  170. */
  171. static inline void sw_rotate_point32(int32_t *dest_x, int32_t *dest_y,
  172. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y, uint16_t angle)
  173. {
  174. sw_transform_point32(dest_x, dest_y, src_x, src_y, pivot_x, pivot_y, angle, 1, 1, 0);
  175. }
  176. /*
  177. * @brief scale a 32-bit point
  178. *
  179. * @param dest_x x coord of the point after rotation
  180. * @param dest_y y coord of the point after rotation
  181. * @param src_x x coord of the point before rotation
  182. * @param src_y y coord of the point image before rotation
  183. * @param pivot_x x coord of rotation pivot
  184. * @param pivot_y y coord of rotation pivot
  185. * @param scale_x scale factor X, equal to "dest_w /src_w"
  186. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  187. * @param scale_bits scale factor bits of scale_x and scale_y
  188. *
  189. * @retval N/A
  190. */
  191. void sw_scale_point32(int32_t *dest_x, int32_t *dest_y,
  192. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y,
  193. uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  194. /*
  195. * @brief translate a 32-bit point
  196. *
  197. * @param dest_x x coord of the point after rotation
  198. * @param dest_y y coord of the point after rotation
  199. * @param src_x x coord of the point before rotation
  200. * @param src_y y coord of the point image before rotation
  201. * @param tx x translate
  202. * @param ty y translate
  203. *
  204. * @retval N/A
  205. */
  206. static inline void sw_translate_point32(int32_t *dest_x, int32_t *dest_y,
  207. int32_t src_x, int32_t src_y, int32_t tx, int32_t ty)
  208. {
  209. *dest_x = src_x + tx;
  210. *dest_y = src_y + ty;
  211. }
  212. /*
  213. * @brief transform a 16-bit point with scaling first
  214. *
  215. * @param dest_x x coord of the point after rotation
  216. * @param dest_y y coord of the point after rotation
  217. * @param src_x x coord of the point before rotation
  218. * @param src_y y coord of the point image before rotation
  219. * @param pivot_x x coord of rotation pivot
  220. * @param pivot_y y coord of rotation pivot
  221. * @param angle rotation angle in 0.1 degree [0, 3600)
  222. * @param scale_x scale factor X, equal to "dest_w /src_w"
  223. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  224. * @param scale_bits scale factor bits of scale_x and scale_y
  225. *
  226. * @retval N/A
  227. */
  228. void sw_transform_point16(int16_t *dest_x, int16_t *dest_y,
  229. int16_t src_x, int16_t src_y, int16_t pivot_x, int16_t pivot_y,
  230. uint16_t angle, uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  231. /*
  232. * @brief transform a 16-bit point with rotation first
  233. *
  234. * @param dest_x x coord of the point after rotation
  235. * @param dest_y y coord of the point after rotation
  236. * @param src_x x coord of the point before rotation
  237. * @param src_y y coord of the point image before rotation
  238. * @param pivot_x x coord of rotation pivot
  239. * @param pivot_y y coord of rotation pivot
  240. * @param angle rotation angle in 0.1 degree [0, 3600)
  241. * @param scale_x scale factor X, equal to "dest_w /src_w"
  242. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  243. * @param scale_bits scale factor bits of scale_x and scale_y
  244. *
  245. * @retval N/A
  246. */
  247. void sw_transform_point16_rot_first(int32_t *dest_x, int32_t *dest_y,
  248. int32_t src_x, int32_t src_y, int32_t pivot_x, int32_t pivot_y,
  249. uint16_t angle, uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  250. /*
  251. * @brief rotate a 16-bit point
  252. *
  253. * @param dest_x x coord of the point after rotation
  254. * @param dest_y y coord of the point after rotation
  255. * @param src_x x coord of the point before rotation
  256. * @param src_y y coord of the point image before rotation
  257. * @param pivot_x x coord of rotation pivot
  258. * @param pivot_y y coord of rotation pivot
  259. * @param angle rotation angle in 0.1 degree [0, 3600]
  260. *
  261. * @retval N/A
  262. */
  263. static inline void sw_rotate_point16(int16_t *dest_x, int16_t *dest_y,
  264. int16_t src_x, int16_t src_y, int16_t pivot_x, int16_t pivot_y, uint16_t angle)
  265. {
  266. sw_transform_point16(dest_x, dest_y, src_x, src_y, pivot_x, pivot_y, angle, 1, 1, 0);
  267. }
  268. /*
  269. * @brief scale a 16-bit point
  270. *
  271. * @param dest_x x coord of the point after rotation
  272. * @param dest_y y coord of the point after rotation
  273. * @param src_x x coord of the point before rotation
  274. * @param src_y y coord of the point image before rotation
  275. * @param pivot_x x coord of rotation pivot
  276. * @param pivot_y y coord of rotation pivot
  277. * @param scale_x scale factor X, equal to "dest_w /src_w"
  278. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  279. * @param scale_bits scale factor bits of scale_x and scale_y
  280. *
  281. * @retval N/A
  282. */
  283. void sw_scale_point16(int16_t *dest_x, int16_t *dest_y,
  284. int16_t src_x, int16_t src_y, int16_t pivot_x, int16_t pivot_y,
  285. uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  286. /*
  287. * @brief translate a 16-bit point
  288. *
  289. * @param dest_x x coord of the point after rotation
  290. * @param dest_y y coord of the point after rotation
  291. * @param src_x x coord of the point before rotation
  292. * @param src_y y coord of the point image before rotation
  293. * @param tx x translate
  294. * @param ty y translate
  295. *
  296. * @retval N/A
  297. */
  298. static inline void sw_translate_point16(int16_t *dest_x, int16_t *dest_y,
  299. int16_t src_x, int16_t src_y, int16_t tx, int16_t ty)
  300. {
  301. *dest_x = src_x + tx;
  302. *dest_y = src_y + ty;
  303. }
  304. /*
  305. * @brief rotate a 16-bit area
  306. *
  307. * [x1, x2] x [y1, y2] define the pixel coordinates of a area
  308. *
  309. * @param dest_x1 x1 coord of rect after rotation
  310. * @param dest_y1 y1 coord of rect after rotation
  311. * @param dest_x2 x2 coord of rect after rotation
  312. * @param dest_y2 y2 coord of rect after rotation
  313. * @param src_x1 x1 coord of rect before rotation
  314. * @param src_y1 y1 coord of rect before rotation
  315. * @param src_x2 x2 coord of rect before rotation
  316. * @param src_y2 y2 coord of rect before rotation
  317. * @param pivot_x x coord of rotation pivot
  318. * @param pivot_y y coord of rotation pivot
  319. * @param angle rotation angle in 0.1 degree [0, 3600]
  320. * @param scale_x scale factor X, equal to "dest_w /src_w"
  321. * @param scale_y scale factor Y, equal to "dest_h /src_h"
  322. * @param scale_bits scale factor bits of scale_x and scale_y
  323. *
  324. * @retval N/A
  325. */
  326. void sw_transform_area16(
  327. int16_t *dest_x1, int16_t *dest_y1, int16_t *dest_x2, int16_t *dest_y2,
  328. int16_t src_x1, int16_t src_y1, int16_t src_x2, int16_t src_y2,
  329. int16_t pivot_x, int16_t pivot_y, uint16_t angle,
  330. uint16_t scale_x, uint16_t scale_y, uint16_t scale_bits);
  331. /*
  332. * @brief rotate a 16-bit area
  333. *
  334. * [x1, x2] x [y1, y2] define the pixel coordinates of a area
  335. *
  336. * @param dest_x1 x1 coord of rect after rotation
  337. * @param dest_y1 y1 coord of rect after rotation
  338. * @param dest_x2 x2 coord of rect after rotation
  339. * @param dest_y2 y2 coord of rect after rotation
  340. * @param src_x1 x1 coord of rect before rotation
  341. * @param src_y1 y1 coord of rect before rotation
  342. * @param src_x2 x2 coord of rect before rotation
  343. * @param src_y2 y2 coord of rect before rotation
  344. * @param pivot_x x coord of rotation pivot
  345. * @param pivot_y y coord of rotation pivot
  346. * @param angle rotation angle in 0.1 degree [0, 3600]
  347. *
  348. * @retval N/A
  349. */
  350. static inline void sw_rotate_area16(
  351. int16_t *dest_x1, int16_t *dest_y1, int16_t *dest_x2, int16_t *dest_y2,
  352. int16_t src_x1, int16_t src_y1, int16_t src_x2, int16_t src_y2,
  353. int16_t pivot_x, int16_t pivot_y, uint16_t angle)
  354. {
  355. sw_transform_area16(dest_x1, dest_y1, dest_x2, dest_y2,
  356. src_x1, src_y1, src_x2, src_y2, pivot_x, pivot_y, angle, 1, 1, 0);
  357. }
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. /**
  362. * @}
  363. */
  364. #endif /* ZEPHYR_FRAMEWORK_INCLUDE_DISPLAY_SW_MATH_H_ */