CAlignAndCutImage.cpp 9.7 KB

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