CAlignAndCutImage.cpp 10 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. 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. bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
  68. {
  69. int dx, dy;
  70. int r0_x, r0_y, r1_x, r1_y;
  71. bool isSimilar1, isSimilar2;
  72. if (_temp1 == NULL || _temp2 == NULL || rgb_image == NULL) {
  73. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: source image or reference is unavailable");
  74. return false;
  75. }
  76. ScopedImageAccess sourceAccess(this);
  77. if (!sourceAccess.isLocked()) {
  78. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock source image");
  79. return false;
  80. }
  81. CFindTemplate* ft = new (std::nothrow) CFindTemplate("align", rgb_image, channels, width, height, bpp);
  82. if (ft == NULL) {
  83. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to allocate template matcher");
  84. return false;
  85. }
  86. r0_x = _temp1->target_x;
  87. r0_y = _temp1->target_y;
  88. ESP_LOGD(TAG, "Before ft->FindTemplate(_temp1); %s", _temp1->image_file.c_str());
  89. isSimilar1 = ft->FindTemplate(_temp1);
  90. _temp1->width = ft->tpl_width;
  91. _temp1->height = ft->tpl_height;
  92. r1_x = _temp2->target_x;
  93. r1_y = _temp2->target_y;
  94. ESP_LOGD(TAG, "Before ft->FindTemplate(_temp2); %s", _temp2->image_file.c_str());
  95. isSimilar2 = ft->FindTemplate(_temp2);
  96. _temp2->width = ft->tpl_width;
  97. _temp2->height = ft->tpl_height;
  98. delete ft;
  99. dx = _temp1->target_x - _temp1->found_x;
  100. dy = _temp1->target_y - _temp1->found_y;
  101. r0_x += dx;
  102. r0_y += dy;
  103. r1_x += dx;
  104. r1_y += dy;
  105. float w_org, w_ist, d_winkel;
  106. w_org = atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
  107. w_ist = atan2((float)(r1_y - r0_y), (float)(r1_x - r0_x));
  108. d_winkel = (w_ist - w_org) * 180 / M_PI;
  109. /*#ifdef DEBUG_DETAIL_ON
  110. std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
  111. zw = zw + "\tt1_x_y:\t" + std::to_string(_temp1->found_x) + "\t" + std::to_string(_temp1->found_y);
  112. 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);
  113. zw = zw + "\tt2_x_y:\t" + std::to_string(_temp2->found_x) + "\t" + std::to_string(_temp2->found_y);
  114. 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);
  115. LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  116. #endif*/
  117. CRotateImage rt("Align", this, ImageTMP);
  118. rt.Translate(dx, dy);
  119. rt.Rotate(d_winkel, _temp1->target_x, _temp1->target_y);
  120. ESP_LOGD(TAG, "Alignment: dx %d - dy %d - rot %f", dx, dy, d_winkel);
  121. return (isSimilar1 && isSimilar2);
  122. }
  123. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  124. {
  125. if (rgb_image == NULL || channels <= 0) {
  126. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: source image is not available");
  127. return;
  128. }
  129. int x2 = 0, y2 = 0;
  130. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) {
  131. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: invalid crop rectangle");
  132. return;
  133. }
  134. const int cropWidth = x2 - x1;
  135. const int cropHeight = y2 - y1;
  136. size_t bufferSize = 0;
  137. if (!getCutBufferSize(cropWidth, cropHeight, channels, &bufferSize)) {
  138. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: crop size overflow");
  139. return;
  140. }
  141. uint8_t* source = RGBImageLock();
  142. if (source == NULL) {
  143. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: source image is locked");
  144. return;
  145. }
  146. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  147. std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  148. if (output == NULL) {
  149. RGBImageRelease();
  150. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: PSRAM allocation failed");
  151. return;
  152. }
  153. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  154. for (int row = 0; row < cropHeight; ++row) {
  155. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  156. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  157. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  158. }
  159. #ifdef STBI_ONLY_JPEG
  160. stbi_write_jpg(_template1.c_str(), cropWidth, cropHeight, channels, output, 100);
  161. #else
  162. stbi_write_bmp(_template1.c_str(), cropWidth, cropHeight, channels, output);
  163. #endif
  164. RGBImageRelease();
  165. free_psram_heap(std::string(TAG) + "->odata", output);
  166. }
  167. bool CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *_target)
  168. {
  169. if (_target == NULL || rgb_image == NULL || channels <= 0) return false;
  170. int x2 = 0, y2 = 0;
  171. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) return false;
  172. const int cropWidth = x2 - x1;
  173. const int cropHeight = y2 - y1;
  174. size_t cropBufferSize = 0;
  175. if (!getCutBufferSize(cropWidth, cropHeight, channels, &cropBufferSize)) return false;
  176. if (_target->height != cropHeight || _target->width != cropWidth ||
  177. _target->channels != channels || !_target->ImageOkay()) {
  178. ESP_LOGD(TAG, "CAlignAndCutImage::CutAndSave - Image size does not match!");
  179. return false;
  180. }
  181. uint8_t* output = _target->RGBImageLock();
  182. if (output == NULL) return false;
  183. uint8_t* source = RGBImageLock();
  184. if (source == NULL) {
  185. _target->RGBImageRelease();
  186. return false;
  187. }
  188. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  189. for (int row = 0; row < cropHeight; ++row) {
  190. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  191. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  192. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  193. }
  194. RGBImageRelease();
  195. _target->RGBImageRelease();
  196. return true;
  197. }
  198. CImageBasis* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  199. {
  200. if (rgb_image == NULL || channels <= 0) return NULL;
  201. int x2 = 0, y2 = 0;
  202. if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) return NULL;
  203. const int cropWidth = x2 - x1;
  204. const int cropHeight = y2 - y1;
  205. size_t bufferSize = 0;
  206. if (!getCutBufferSize(cropWidth, cropHeight, channels, &bufferSize)) return NULL;
  207. uint8_t* source = RGBImageLock();
  208. if (source == NULL) return NULL;
  209. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  210. std::string(TAG) + "->odata", bufferSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  211. if (output == NULL) {
  212. RGBImageRelease();
  213. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CutAndSave: PSRAM allocation failed");
  214. return NULL;
  215. }
  216. const size_t rowBytes = static_cast<size_t>(cropWidth) * static_cast<size_t>(channels);
  217. for (int row = 0; row < cropHeight; ++row) {
  218. const size_t sourceOffset = (static_cast<size_t>(y1 + row) * static_cast<size_t>(width) +
  219. static_cast<size_t>(x1)) * static_cast<size_t>(channels);
  220. std::memcpy(output + static_cast<size_t>(row) * rowBytes, source + sourceOffset, rowBytes);
  221. }
  222. RGBImageRelease();
  223. CImageBasis* result = new (std::nothrow) CImageBasis("CutAndSave", output, channels, cropWidth, cropHeight, bpp);
  224. if (result == NULL) {
  225. free_psram_heap(std::string(TAG) + "->odata (unowned)", output);
  226. return NULL;
  227. }
  228. if (!result->SetIndepended()) {
  229. delete result;
  230. free_psram_heap(std::string(TAG) + "->odata (invalid result)", output);
  231. return NULL;
  232. }
  233. // SetIndepended copied the externally-owned crop into its own managed PSRAM block.
  234. free_psram_heap(std::string(TAG) + "->odata (transferred)", output);
  235. return result;
  236. }