CRotateImage.cpp 9.7 KB

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