CRotateImage.cpp 13 KB

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