CRotateImage.cpp 9.2 KB

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