CAlignAndCutImage.cpp 10 KB

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