CRotateImage.cpp 13 KB

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