CRotateImage.cpp 14 KB

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