CRotateImage.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include <string>
  2. #include "CRotateImage.h"
  3. #include "psram.h"
  4. static const char *TAG = "C ROTATE IMG";
  5. CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_temp, bool _flip) : CImageBasis(_name)
  6. {
  7. rgb_image = _org->rgb_image;
  8. channels = _org->channels;
  9. width = _org->width;
  10. height = _org->height;
  11. bpp = _org->bpp;
  12. externalImage = true;
  13. ImageTMP = _temp;
  14. ImageOrg = _org;
  15. islocked = false;
  16. doflip = _flip;
  17. }
  18. void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
  19. {
  20. int org_width, org_height;
  21. float m[2][3];
  22. float x_center = _centerx;
  23. float y_center = _centery;
  24. _angle = _angle / 180 * M_PI;
  25. if (doflip)
  26. {
  27. org_width = width;
  28. org_height = height;
  29. height = org_width;
  30. width = org_height;
  31. x_center = x_center - (org_width/2) + (org_height/2);
  32. y_center = y_center + (org_width/2) - (org_height/2);
  33. if (ImageOrg)
  34. {
  35. ImageOrg->height = height;
  36. ImageOrg->width = width;
  37. }
  38. }
  39. else
  40. {
  41. org_width = width;
  42. org_height = height;
  43. }
  44. m[0][0] = cos(_angle);
  45. m[0][1] = sin(_angle);
  46. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  47. m[1][0] = -m[0][1];
  48. m[1][1] = m[0][0];
  49. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  50. if (doflip)
  51. {
  52. m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
  53. m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
  54. }
  55. int memsize = width * height * channels;
  56. uint8_t* odata;
  57. if (ImageTMP)
  58. {
  59. odata = ImageTMP->RGBImageLock();
  60. }
  61. else
  62. {
  63. odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  64. }
  65. int x_source, y_source;
  66. stbi_uc* p_target;
  67. stbi_uc* p_source;
  68. RGBImageLock();
  69. for (int x = 0; x < width; ++x)
  70. for (int y = 0; y < height; ++y)
  71. {
  72. p_target = odata + (channels * (y * width + x));
  73. x_source = int(m[0][0] * x + m[0][1] * y);
  74. y_source = int(m[1][0] * x + m[1][1] * y);
  75. x_source += int(m[0][2]);
  76. y_source += int(m[1][2]);
  77. if ((x_source >= 0) && (x_source < org_width) && (y_source >= 0) && (y_source < org_height))
  78. {
  79. p_source = rgb_image + (channels * (y_source * org_width + x_source));
  80. for (int _channels = 0; _channels < channels; ++_channels)
  81. p_target[_channels] = p_source[_channels];
  82. }
  83. else
  84. {
  85. for (int _channels = 0; _channels < channels; ++_channels)
  86. p_target[_channels] = 255;
  87. }
  88. }
  89. // memcpy(rgb_image, odata, memsize);
  90. memCopy(odata, rgb_image, memsize);
  91. if (!ImageTMP)
  92. {
  93. free_psram_heap(std::string(TAG) + "->odata", odata);
  94. }
  95. if (ImageTMP)
  96. ImageTMP->RGBImageRelease();
  97. RGBImageRelease();
  98. }
  99. void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
  100. {
  101. int org_width, org_height;
  102. float m[2][3];
  103. float x_center = _centerx;
  104. float y_center = _centery;
  105. _angle = _angle / 180 * M_PI;
  106. if (doflip)
  107. {
  108. org_width = width;
  109. org_height = height;
  110. height = org_width;
  111. width = org_height;
  112. x_center = x_center - (org_width/2) + (org_height/2);
  113. y_center = y_center + (org_width/2) - (org_height/2);
  114. if (ImageOrg)
  115. {
  116. ImageOrg->height = height;
  117. ImageOrg->width = width;
  118. }
  119. }
  120. else
  121. {
  122. org_width = width;
  123. org_height = height;
  124. }
  125. m[0][0] = cos(_angle);
  126. m[0][1] = sin(_angle);
  127. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  128. m[1][0] = -m[0][1];
  129. m[1][1] = m[0][0];
  130. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  131. if (doflip)
  132. {
  133. m[0][2] = m[0][2] + (org_width/2) - (org_height/2);
  134. m[1][2] = m[1][2] - (org_width/2) + (org_height/2);
  135. }
  136. int memsize = width * height * channels;
  137. uint8_t* odata;
  138. if (ImageTMP)
  139. {
  140. odata = ImageTMP->RGBImageLock();
  141. }
  142. else
  143. {
  144. odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  145. }
  146. int x_source_1, y_source_1, x_source_2, y_source_2;
  147. float x_source, y_source;
  148. float quad_ul, quad_ur, quad_ol, quad_or;
  149. stbi_uc* p_target;
  150. stbi_uc *p_source_ul, *p_source_ur, *p_source_ol, *p_source_or;
  151. RGBImageLock();
  152. for (int x = 0; x < width; ++x)
  153. for (int y = 0; y < height; ++y)
  154. {
  155. p_target = odata + (channels * (y * width + x));
  156. x_source = (m[0][0] * x + m[0][1] * y);
  157. y_source = (m[1][0] * x + m[1][1] * y);
  158. x_source += (m[0][2]);
  159. y_source += (m[1][2]);
  160. x_source_1 = (int)x_source;
  161. x_source_2 = x_source_1 + 1;
  162. y_source_1 = (int)y_source;
  163. y_source_2 = y_source_1 + 1;
  164. quad_ul = (x_source_2 - x_source) * (y_source_2 - y_source);
  165. quad_ur = (1- (x_source_2 - x_source)) * (y_source_2 - y_source);
  166. quad_or = (x_source_2 - x_source) * (1-(y_source_2 - y_source));
  167. quad_ol = (1- (x_source_2 - x_source)) * (1-(y_source_2 - y_source));
  168. if ((x_source_1 >= 0) && (x_source_2 < org_width) && (y_source_1 >= 0) && (y_source_2 < org_height))
  169. {
  170. p_source_ul = rgb_image + (channels * (y_source_1 * org_width + x_source_1));
  171. p_source_ur = rgb_image + (channels * (y_source_1 * org_width + x_source_2));
  172. p_source_or = rgb_image + (channels * (y_source_2 * org_width + x_source_1));
  173. p_source_ol = rgb_image + (channels * (y_source_2 * org_width + x_source_2));
  174. for (int _channels = 0; _channels < channels; ++_channels)
  175. {
  176. p_target[_channels] = (int)((float)p_source_ul[_channels] * quad_ul
  177. + (float)p_source_ur[_channels] * quad_ur
  178. + (float)p_source_or[_channels] * quad_or
  179. + (float)p_source_ol[_channels] * quad_ol);
  180. }
  181. }
  182. else
  183. {
  184. for (int _channels = 0; _channels < channels; ++_channels)
  185. p_target[_channels] = 255;
  186. }
  187. }
  188. // memcpy(rgb_image, odata, memsize);
  189. memCopy(odata, rgb_image, memsize);
  190. if (!ImageTMP)
  191. {
  192. free_psram_heap(std::string(TAG) + "->odata", odata);
  193. }
  194. if (ImageTMP)
  195. ImageTMP->RGBImageRelease();
  196. RGBImageRelease();
  197. }
  198. void CRotateImage::Rotate(float _angle)
  199. {
  200. // ESP_LOGD(TAG, "width %d, height %d", width, height);
  201. Rotate(_angle, width / 2, height / 2);
  202. }
  203. void CRotateImage::RotateAntiAliasing(float _angle)
  204. {
  205. // ESP_LOGD(TAG, "width %d, height %d", width, height);
  206. RotateAntiAliasing(_angle, width / 2, height / 2);
  207. }
  208. void CRotateImage::Translate(int _dx, int _dy)
  209. {
  210. int memsize = width * height * channels;
  211. uint8_t* odata;
  212. if (ImageTMP)
  213. {
  214. odata = ImageTMP->RGBImageLock();
  215. }
  216. else
  217. {
  218. odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  219. }
  220. int x_source, y_source;
  221. stbi_uc* p_target;
  222. stbi_uc* p_source;
  223. RGBImageLock();
  224. for (int x = 0; x < width; ++x)
  225. for (int y = 0; y < height; ++y)
  226. {
  227. p_target = odata + (channels * (y * width + x));
  228. x_source = x - _dx;
  229. y_source = y - _dy;
  230. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  231. {
  232. p_source = rgb_image + (channels * (y_source * width + x_source));
  233. for (int _channels = 0; _channels < channels; ++_channels)
  234. p_target[_channels] = p_source[_channels];
  235. }
  236. else
  237. {
  238. for (int _channels = 0; _channels < channels; ++_channels)
  239. p_target[_channels] = 255;
  240. }
  241. }
  242. // memcpy(rgb_image, odata, memsize);
  243. memCopy(odata, rgb_image, memsize);
  244. if (!ImageTMP)
  245. {
  246. free_psram_heap(std::string(TAG) + "->odata", odata);
  247. }
  248. if (ImageTMP)
  249. {
  250. ImageTMP->RGBImageRelease();
  251. }
  252. RGBImageRelease();
  253. }