CAlignAndCutImage.cpp 9.2 KB

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