CAlignAndCutImage.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "CAlignAndCutImage.h"
  2. #include "CRotateImage.h"
  3. #include "ClassLogFile.h"
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <cstring>
  7. #include <stdint.h>
  8. #include <limits.h>
  9. #include <new>
  10. #include <esp_log.h>
  11. #include "psram.h"
  12. #include "../../include/defines.h"
  13. #include "Helper.h"
  14. static const char* TAG = "c_align_and_cut_image";
  15. static bool getCutRectangle(int imageWidth, int imageHeight,
  16. int x, int y, int requestedWidth, int requestedHeight,
  17. int* right, int* bottom)
  18. {
  19. if (right == NULL || bottom == NULL || imageWidth <= 0 || imageHeight <= 0 ||
  20. x < 0 || y < 0 || requestedWidth <= 0 || requestedHeight <= 0 ||
  21. x >= imageWidth || y >= imageHeight) return false;
  22. const int64_t requestedRight = static_cast<int64_t>(x) + requestedWidth;
  23. const int64_t requestedBottom = static_cast<int64_t>(y) + requestedHeight;
  24. *right = static_cast<int>(std::min<int64_t>(requestedRight, imageWidth));
  25. *bottom = static_cast<int>(std::min<int64_t>(requestedBottom, imageHeight));
  26. return *right > x && *bottom > y;
  27. }
  28. static bool getCutBufferSize(int width, int height, int channels, size_t* size)
  29. {
  30. if (size == NULL || width <= 0 || height <= 0 || channels <= 0) return false;
  31. const size_t w = static_cast<size_t>(width);
  32. const size_t h = static_cast<size_t>(height);
  33. const size_t c = static_cast<size_t>(channels);
  34. if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return false;
  35. const size_t total = w * h * c;
  36. if (total > static_cast<size_t>(INT_MAX)) return false;
  37. *size = total;
  38. return true;
  39. }
  40. CAlignAndCutImage::CAlignAndCutImage(std::string _name, CImageBasis *_org, CImageBasis *_temp) : CImageBasis(_name)
  41. {
  42. name = _name;
  43. rgb_image = _org->rgb_image;
  44. channels = _org->channels;
  45. width = _org->width;
  46. height = _org->height;
  47. bpp = _org->bpp;
  48. externalImage = true;
  49. islocked = false;
  50. ImageTMP = _temp;
  51. }
  52. bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
  53. {
  54. int dx, dy;
  55. int r0_x, r0_y, r1_x, r1_y;
  56. bool isSimilar1, isSimilar2;
  57. if (_temp1 == NULL || _temp2 == NULL || rgb_image == NULL ||
  58. ImageTMP == NULL || !ImageTMP->ImageOkay() ||
  59. ImageTMP->width != width || ImageTMP->height != height ||
  60. ImageTMP->channels != channels) {
  61. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: source image or snapshot workspace is unavailable");
  62. return false;
  63. }
  64. // Copy the source while briefly holding the image lock. The template search
  65. // then reads only this immutable snapshot and does not block HTTP image access.
  66. size_t snapshotSize = 0;
  67. if (!getCutBufferSize(width, height, channels, &snapshotSize)) {
  68. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: invalid source image dimensions");
  69. return false;
  70. }
  71. uint8_t* source = RGBImageLock();
  72. if (source == NULL) {
  73. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock source image for snapshot");
  74. return false;
  75. }
  76. uint8_t* snapshot = ImageTMP->RGBImageLock();
  77. if (snapshot == NULL) {
  78. RGBImageRelease();
  79. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock snapshot workspace");
  80. return false;
  81. }
  82. std::memcpy(snapshot, source, snapshotSize);
  83. ImageTMP->RGBImageRelease();
  84. RGBImageRelease();
  85. CFindTemplate* ft = new (std::nothrow) CFindTemplate(
  86. "align", snapshot, channels, width, height, bpp);
  87. if (ft == NULL) {
  88. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to allocate template matcher");
  89. return false;
  90. }
  91. r0_x = _temp1->target_x;
  92. r0_y = _temp1->target_y;
  93. ESP_LOGD(TAG, "Before ft->FindTemplate(_temp1); %s", _temp1->image_file.c_str());
  94. isSimilar1 = ft->FindTemplate(_temp1, true);
  95. _temp1->width = ft->tpl_width;
  96. _temp1->height = ft->tpl_height;
  97. r1_x = _temp2->target_x;
  98. r1_y = _temp2->target_y;
  99. ESP_LOGD(TAG, "Before ft->FindTemplate(_temp2); %s", _temp2->image_file.c_str());
  100. isSimilar2 = ft->FindTemplate(_temp2, true);
  101. _temp2->width = ft->tpl_width;
  102. _temp2->height = ft->tpl_height;
  103. delete ft;
  104. dx = _temp1->target_x - _temp1->found_x;
  105. dy = _temp1->target_y - _temp1->found_y;
  106. r0_x += dx;
  107. r0_y += dy;
  108. r1_x += dx;
  109. r1_y += dy;
  110. float w_org, w_ist, d_winkel;
  111. w_org = atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
  112. w_ist = atan2((float)(r1_y - r0_y), (float)(r1_x - r0_x));
  113. d_winkel = (w_ist - w_org) * 180 / M_PI;
  114. CRotateImage rt("Align", this, ImageTMP);
  115. rt.Translate(dx, dy);
  116. rt.Rotate(d_winkel, _temp1->target_x, _temp1->target_y);
  117. ESP_LOGD(TAG, "Alignment: dx %d - dy %d - rot %f", dx, dy, d_winkel);
  118. return (isSimilar1 && isSimilar2);
  119. }
  120. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  121. {
  122. if (rgb_image == NULL || channels <= 0) {
  123. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: source image is not available");
  124. return;
  125. }
  126. int x2 = 0, y2 = 0;
  127. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) {
  128. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: invalid crop rectangle");
  129. return;
  130. }
  131. const int cropWidth = x2 - x1;
  132. const int cropHeight = y2 - y1;
  133. size_t bufferSize = 0;
  134. if (!getCutBufferSize(cropWidth, cropHeight, channels, &bufferSize)) {
  135. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: crop size overflow");
  136. return;
  137. }
  138. uint8_t* source = RGBImageLock();
  139. if (source == NULL) {
  140. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: source image is locked");
  141. return;
  142. }
  143. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  144. std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  145. if (output == NULL) {
  146. RGBImageRelease();
  147. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: PSRAM allocation failed");
  148. return;
  149. }
  150. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  151. for (int row = 0; row < cropHeight; ++row) {
  152. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  153. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  154. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  155. }
  156. #ifdef STBI_ONLY_JPEG
  157. stbi_write_jpg(_template1.c_str(), cropWidth, cropHeight, channels, output, 100);
  158. #else
  159. stbi_write_bmp(_template1.c_str(), cropWidth, cropHeight, channels, output);
  160. #endif
  161. RGBImageRelease();
  162. free_psram_heap(std::string(TAG) + "->odata", output);
  163. }
  164. bool CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
  165. {
  166. if (_target == NULL || rgb_image == NULL || channels <= 0) return false;
  167. int x2 = 0, y2 = 0;
  168. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) return false;
  169. const int cropWidth = x2 - x1;
  170. const int cropHeight = y2 - y1;
  171. size_t cropBufferSize = 0;
  172. if (!getCutBufferSize(cropWidth, cropHeight, channels, &cropBufferSize)) return false;
  173. if (_target->height != cropHeight || _target->width != cropWidth ||
  174. _target->channels != channels || !_target->ImageOkay()) {
  175. ESP_LOGD(TAG, "CAlignAndCutImage::CutAndSave - Image size does not match!");
  176. return false;
  177. }
  178. uint8_t* output = _target->RGBImageLock();
  179. if (output == NULL) return false;
  180. uint8_t* source = RGBImageLock();
  181. if (source == NULL) {
  182. _target->RGBImageRelease();
  183. return false;
  184. }
  185. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  186. for (int row = 0; row < cropHeight; ++row) {
  187. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  188. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  189. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  190. }
  191. RGBImageRelease();
  192. _target->RGBImageRelease();
  193. return true;
  194. }
  195. CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  196. {
  197. if (rgb_image == NULL || channels <= 0) return NULL;
  198. int x2 = 0, y2 = 0;
  199. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) return NULL;
  200. const int cropWidth = x2 - x1;
  201. const int cropHeight = y2 - y1;
  202. size_t bufferSize = 0;
  203. if (!getCutBufferSize(cropWidth, cropHeight, channels, &bufferSize)) return NULL;
  204. uint8_t* source = RGBImageLock();
  205. if (source == NULL) return NULL;
  206. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  207. std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  208. if (output == NULL) {
  209. RGBImageRelease();
  210. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: PSRAM allocation failed");
  211. return NULL;
  212. }
  213. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  214. for (int row = 0; row < cropHeight; ++row) {
  215. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  216. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  217. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  218. }
  219. RGBImageRelease();
  220. CImageBasis* result = new (std::nothrow) CImageBasis("CutAndSave", output, channels, cropWidth, cropHeight, bpp);
  221. if (result == NULL) {
  222. free_psram_heap(std::string(TAG) + "->odata (unowned)", output);
  223. return NULL;
  224. }
  225. if (!result->SetIndepended()) {
  226. delete result;
  227. free_psram_heap(std::string(TAG) + "->odata (invalid result)", output);
  228. return NULL;
  229. }
  230. // SetIndepended copied the externally-owned crop into its own managed PSRAM block.
  231. free_psram_heap(std::string(TAG) + "->odata (transferred)", output);
  232. return result;
  233. }