CFindTemplate.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. #include "CFindTemplate.h"
  2. #include "Helper.h"
  3. #include "ClassLogFile.h"
  4. #include "esp_system.h"
  5. #define _USE_MATH_DEFINES
  6. #include <math.h>
  7. #include <algorithm>
  8. #define _ESP32_PSRAM
  9. using namespace std;
  10. #define GET_MEMORY malloc
  11. void writejpghelp(void *context, void *data, int size)
  12. {
  13. // printf("Size all: %d, size %d\n", ((ImageData*)context)->size, size);
  14. ImageData* _zw = (ImageData*) context;
  15. uint8_t *voidstart = _zw->data;
  16. uint8_t *datastart = (uint8_t*) data;
  17. voidstart += _zw->size;
  18. for (int i = 0; i < size; ++i)
  19. *(voidstart + i) = *(datastart + i);
  20. _zw->size += size;
  21. }
  22. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  23. {
  24. ImageData* ii = new ImageData;
  25. auto rv2 = stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  26. return ii;
  27. }
  28. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  29. {
  30. int gr = height * width * channels;
  31. if (gr != _size) // Größe passt nicht
  32. {
  33. printf("Kann Bild nicht von Speicher kopierte - Größen passen nicht zusammen: soll %d, ist %d\n", _size, gr);
  34. return false;
  35. }
  36. memCopy(_source, rgb_image, _size);
  37. return true;
  38. }
  39. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  40. {
  41. stbi_uc* p_source;
  42. p_source = rgb_image + (channels * (y * width + x));
  43. return p_source[ch];
  44. }
  45. void CResizeImage::Resize(int _new_dx, int _new_dy)
  46. {
  47. int memsize = _new_dx * _new_dy * channels;
  48. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  49. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  50. stbi_image_free(rgb_image);
  51. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  52. memCopy(odata, rgb_image, memsize);
  53. width = _new_dx;
  54. height = _new_dy;
  55. stbi_image_free(odata);
  56. }
  57. CRotate::CRotate(CImageBasis *_org, CImageBasis *_temp)
  58. {
  59. rgb_image = _org->rgb_image;
  60. channels = _org->channels;
  61. width = _org->width;
  62. height = _org->height;
  63. bpp = _org->bpp;
  64. externalImage = true;
  65. ImageTMP = _temp;
  66. }
  67. void CRotate::Mirror(){
  68. int memsize = width * height * channels;
  69. uint8_t* odata;
  70. if (ImageTMP)
  71. {
  72. odata = ImageTMP->rgb_image;
  73. }
  74. else
  75. {
  76. odata = (unsigned char*)GET_MEMORY(memsize);
  77. }
  78. int x_source, y_source;
  79. stbi_uc* p_target;
  80. stbi_uc* p_source;
  81. for (int x = 0; x < width; ++x)
  82. for (int y = 0; y < height; ++y)
  83. {
  84. p_target = odata + (channels * (y * width + x));
  85. x_source = width - x;
  86. y_source = y;
  87. p_source = rgb_image + (channels * (y_source * width + x_source));
  88. for (int _channels = 0; _channels < channels; ++_channels)
  89. p_target[_channels] = p_source[_channels];
  90. }
  91. // memcpy(rgb_image, odata, memsize);
  92. memCopy(odata, rgb_image, memsize);
  93. if (!ImageTMP)
  94. {
  95. stbi_image_free(odata);
  96. }
  97. }
  98. void CRotate::Rotate(float _angle, int _centerx, int _centery)
  99. {
  100. float m[2][3];
  101. float x_center = _centerx;
  102. float y_center = _centery;
  103. _angle = _angle / 180 * M_PI;
  104. m[0][0] = cos(_angle);
  105. m[0][1] = sin(_angle);
  106. m[0][2] = (1 - m[0][0]) * x_center - m[0][1] * y_center;
  107. m[1][0] = -m[0][1];
  108. m[1][1] = m[0][0];
  109. m[1][2] = m[0][1] * x_center + (1 - m[0][0]) * y_center;
  110. int memsize = width * height * channels;
  111. uint8_t* odata;
  112. if (ImageTMP)
  113. {
  114. odata = ImageTMP->rgb_image;
  115. }
  116. else
  117. {
  118. odata = (unsigned char*)GET_MEMORY(memsize);
  119. }
  120. int x_source, y_source;
  121. stbi_uc* p_target;
  122. stbi_uc* p_source;
  123. for (int x = 0; x < width; ++x)
  124. for (int y = 0; y < height; ++y)
  125. {
  126. p_target = odata + (channels * (y * width + x));
  127. x_source = int(m[0][0] * x + m[0][1] * y);
  128. y_source = int(m[1][0] * x + m[1][1] * y);
  129. x_source += int(m[0][2]);
  130. y_source += int(m[1][2]);
  131. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  132. {
  133. p_source = rgb_image + (channels * (y_source * width + x_source));
  134. for (int _channels = 0; _channels < channels; ++_channels)
  135. p_target[_channels] = p_source[_channels];
  136. }
  137. else
  138. {
  139. for (int _channels = 0; _channels < channels; ++_channels)
  140. p_target[_channels] = 255;
  141. }
  142. }
  143. // memcpy(rgb_image, odata, memsize);
  144. memCopy(odata, rgb_image, memsize);
  145. if (!ImageTMP)
  146. {
  147. stbi_image_free(odata);
  148. }
  149. }
  150. void CRotate::Rotate(float _angle)
  151. {
  152. // printf("width %d, height %d\n", width, height);
  153. Rotate(_angle, width / 2, height / 2);
  154. }
  155. void CRotate::Translate(int _dx, int _dy)
  156. {
  157. int memsize = width * height * channels;
  158. uint8_t* odata;
  159. if (ImageTMP)
  160. {
  161. odata = ImageTMP->rgb_image;
  162. }
  163. else
  164. {
  165. odata = (unsigned char*)GET_MEMORY(memsize);
  166. }
  167. int x_source, y_source;
  168. stbi_uc* p_target;
  169. stbi_uc* p_source;
  170. for (int x = 0; x < width; ++x)
  171. for (int y = 0; y < height; ++y)
  172. {
  173. p_target = odata + (channels * (y * width + x));
  174. x_source = x - _dx;
  175. y_source = y - _dy;
  176. if ((x_source >= 0) && (x_source < width) && (y_source >= 0) && (y_source < height))
  177. {
  178. p_source = rgb_image + (channels * (y_source * width + x_source));
  179. for (int _channels = 0; _channels < channels; ++_channels)
  180. p_target[_channels] = p_source[_channels];
  181. }
  182. else
  183. {
  184. for (int _channels = 0; _channels < channels; ++_channels)
  185. p_target[_channels] = 255;
  186. }
  187. }
  188. // memcpy(rgb_image, odata, memsize);
  189. memCopy(odata, rgb_image, memsize);
  190. if (!ImageTMP)
  191. {
  192. stbi_image_free(odata);
  193. }
  194. }
  195. /*
  196. CFindTemplate::CFindTemplate(std::string _image)
  197. {
  198. channels = 1;
  199. rgb_image = stbi_load(_image.c_str(), &(width), &(height), &(bpp), channels);
  200. }
  201. */
  202. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y)
  203. {
  204. FindTemplate(_template, found_x, found_y, 0, 0);
  205. }
  206. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy)
  207. {
  208. uint8_t* rgb_template = stbi_load(_template.c_str(), &tpl_width, &tpl_height, &tpl_bpp, channels);
  209. int ow, ow_start, ow_stop;
  210. int oh, oh_start, oh_stop;
  211. if (_dx == 0)
  212. {
  213. _dx = width;
  214. *found_x = 0;
  215. }
  216. if (_dy == 0)
  217. {
  218. _dy = height;
  219. *found_y = 0;
  220. }
  221. ow_start = *found_x - _dx;
  222. ow_start = std::max(ow_start, 0);
  223. ow_stop = *found_x + _dx;
  224. if ((ow_stop + tpl_width) > width)
  225. ow_stop = width - tpl_width;
  226. ow = ow_stop - ow_start + 1;
  227. oh_start = *found_y - _dy;
  228. oh_start = std::max(oh_start, 0);
  229. oh_stop = *found_y + _dy;
  230. if ((oh_stop + tpl_height) > height)
  231. oh_stop = height - tpl_height;
  232. oh = oh_stop - oh_start + 1;
  233. uint8_t* odata = (unsigned char*)GET_MEMORY(ow * oh * channels);
  234. double aktSAD;
  235. double minSAD = pow(tpl_width * tpl_height * 255, 2);
  236. for (int xouter = ow_start; xouter <= ow_stop; xouter++)
  237. for (int youter = oh_start; youter <= oh_stop; ++youter)
  238. {
  239. aktSAD = 0;
  240. for (int tpl_x = 0; tpl_x < tpl_width; tpl_x++)
  241. for (int tpl_y = 0; tpl_y < tpl_height; tpl_y++)
  242. {
  243. stbi_uc* p_org = rgb_image + (channels * ((youter + tpl_y) * width + (xouter + tpl_x)));
  244. stbi_uc* p_tpl = rgb_template + (channels * (tpl_y * tpl_width + tpl_x));
  245. aktSAD += pow(p_tpl[0] - p_org[0], 2);
  246. }
  247. stbi_uc* p_out = odata + (channels * ((youter - oh_start) * ow + (xouter - ow_start)));
  248. p_out[0] = int(sqrt(aktSAD / (tpl_width * tpl_height)));
  249. if (aktSAD < minSAD)
  250. {
  251. minSAD = aktSAD;
  252. *found_x = xouter;
  253. *found_y = youter;
  254. }
  255. }
  256. stbi_write_bmp("sdcard\\find.bmp", ow, oh, channels, odata);
  257. stbi_image_free(odata);
  258. stbi_image_free(rgb_template);
  259. }
  260. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, std::string _imageout)
  261. {
  262. FindTemplate(_template, found_x, found_y);
  263. SaveToFile(_imageout);
  264. }
  265. void CFindTemplate::FindTemplate(std::string _template, int* found_x, int* found_y, int _dx, int _dy, std::string _imageout)
  266. {
  267. FindTemplate(_template, found_x, found_y, _dx, _dy);
  268. SaveToFile(_imageout);
  269. }
  270. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  271. {
  272. #ifdef _ESP32_PSRAM
  273. for (int i = 0; i < _size; ++i)
  274. *(_target + i) = *(_source + i);
  275. #else
  276. memcpy(_target, _source, _size);
  277. #endif
  278. }
  279. bool CImageBasis::isInImage(int x, int y)
  280. {
  281. if ((x < 0) || (x > width - 1))
  282. return false;
  283. if ((y < 0) || (y > height- 1))
  284. return false;
  285. return true;
  286. }
  287. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  288. {
  289. stbi_uc* p_source;
  290. p_source = rgb_image + (channels * (y * width + x));
  291. p_source[0] = r;
  292. if ( channels > 2)
  293. {
  294. p_source[1] = g;
  295. p_source[2] = b;
  296. }
  297. }
  298. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  299. {
  300. int zwx1, zwx2, zwy1, zwy2;
  301. int _x, _y, _thick;
  302. zwx1 = x - thickness + 1;
  303. zwx2 = x + dx + thickness - 1;
  304. zwy1 = y;
  305. zwy2 = y;
  306. for (_thick = 0; _thick < thickness; _thick++)
  307. for (_x = zwx1; _x <= zwx2; ++_x)
  308. for (_y = zwy1; _y <= zwy2; _y++)
  309. if (isInImage(_x, _y))
  310. setPixelColor(_x, _y - _thick, r, g, b);
  311. zwx1 = x - thickness + 1;
  312. zwx2 = x + dx + thickness - 1;
  313. zwy1 = y + dy;
  314. zwy2 = y + dy;
  315. for (_thick = 0; _thick < thickness; _thick++)
  316. for (_x = zwx1; _x <= zwx2; ++_x)
  317. for (_y = zwy1; _y <= zwy2; _y++)
  318. if (isInImage(_x, _y))
  319. setPixelColor(_x, _y + _thick, r, g, b);
  320. zwx1 = x;
  321. zwx2 = x;
  322. zwy1 = y;
  323. zwy2 = y + dy;
  324. for (_thick = 0; _thick < thickness; _thick++)
  325. for (_x = zwx1; _x <= zwx2; ++_x)
  326. for (_y = zwy1; _y <= zwy2; _y++)
  327. if (isInImage(_x, _y))
  328. setPixelColor(_x - _thick, _y, r, g, b);
  329. zwx1 = x + dx;
  330. zwx2 = x + dx;
  331. zwy1 = y;
  332. zwy2 = y + dy;
  333. for (_thick = 0; _thick < thickness; _thick++)
  334. for (_x = zwx1; _x <= zwx2; ++_x)
  335. for (_y = zwy1; _y <= zwy2; _y++)
  336. if (isInImage(_x, _y))
  337. setPixelColor(_x + _thick, _y, r, g, b);
  338. }
  339. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  340. {
  341. int _x, _y, _thick;
  342. int _zwy1, _zwy2;
  343. thickness = (thickness-1) / 2;
  344. for (_thick = 0; _thick <= thickness; ++_thick)
  345. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  346. {
  347. if (x2 == x1)
  348. {
  349. _zwy1 = y1;
  350. _zwy2 = y2;
  351. }
  352. else
  353. {
  354. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  355. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  356. }
  357. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  358. if (isInImage(_x, _y))
  359. setPixelColor(_x, _y, r, g, b);
  360. }
  361. }
  362. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  363. {
  364. float deltarad, aktrad;
  365. int _thick, _x, _y;
  366. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  367. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  368. for (_thick = 0; _thick < thickness; ++_thick)
  369. {
  370. _x = sin(aktrad) * (rad + _thick) + x1;
  371. _y = cos(aktrad) * (rad + _thick) + y1;
  372. if (isInImage(_x, _y))
  373. setPixelColor(_x, _y, r, g, b);
  374. }
  375. }
  376. CImageBasis::CImageBasis()
  377. {
  378. externalImage = false;
  379. }
  380. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  381. {
  382. bpp = _channels;
  383. width = _width;
  384. height = _height;
  385. channels = _channels;
  386. int memsize = width * height * channels;
  387. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  388. stbi_uc* p_source;
  389. for (int x = 0; x < width; ++x)
  390. for (int y = 0; y < height; ++y)
  391. {
  392. p_source = rgb_image + (channels * (y * width + x));
  393. for (int _channels = 0; _channels < channels; ++_channels)
  394. p_source[_channels] = (uint8_t) 0;
  395. }
  396. }
  397. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  398. {
  399. // if (rgb_image)
  400. // free(rgb_image);
  401. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
  402. bpp = channels;
  403. // STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
  404. }
  405. CImageBasis::CImageBasis(CImageBasis *_copyfrom)
  406. {
  407. externalImage = false;
  408. channels = _copyfrom->channels;
  409. width = _copyfrom->width;
  410. height = _copyfrom->height;
  411. bpp = _copyfrom->bpp;
  412. int memsize = width * height * channels;
  413. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  414. if (!rgb_image)
  415. {
  416. printf(getESPHeapInfo().c_str());
  417. printf("\nKein freier Speicher mehr!!!! Benötigt: %d %d %d %d\n", width, height, channels, memsize);
  418. return;
  419. }
  420. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  421. }
  422. CImageBasis::CImageBasis(std::string _image)
  423. {
  424. channels = 3;
  425. externalImage = false;
  426. filename = _image;
  427. long zwld = esp_get_free_heap_size();
  428. printf("freeheapsize before: %ld\n", zwld);
  429. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  430. zwld = esp_get_free_heap_size();
  431. printf("freeheapsize after : %ld\n", zwld);
  432. std::string zw = "Image Load failed:" + _image + "\n";
  433. if (rgb_image == NULL)
  434. printf(zw.c_str());
  435. zw = "CImageBasis after load " + _image + "\n";
  436. printf(zw.c_str());
  437. printf("w %d, h %d, b %d, c %d\n", width, height, bpp, channels);
  438. }
  439. bool CImageBasis::ImageOkay(){
  440. return rgb_image != NULL;
  441. }
  442. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  443. {
  444. rgb_image = _rgb_image;
  445. channels = _channels;
  446. width = _width;
  447. height = _height;
  448. bpp = _bpp;
  449. externalImage = true;
  450. }
  451. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  452. {
  453. stbi_uc* p_source;
  454. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  455. float intercept = 128 * (1 - contrast);
  456. for (int x = 0; x < width; ++x)
  457. for (int y = 0; y < height; ++y)
  458. {
  459. p_source = rgb_image + (channels * (y * width + x));
  460. for (int _channels = 0; _channels < channels; ++_channels)
  461. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  462. }
  463. }
  464. CImageBasis::~CImageBasis()
  465. {
  466. if (!externalImage)
  467. stbi_image_free(rgb_image);
  468. }
  469. void CImageBasis::SaveToFile(std::string _imageout)
  470. {
  471. string typ = getFileType(_imageout);
  472. if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
  473. {
  474. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  475. }
  476. if ((typ == "bmp") || (typ == "BMP"))
  477. {
  478. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  479. }
  480. }
  481. CAlignAndCutImage::CAlignAndCutImage(CImageBasis *_org, CImageBasis *_temp)
  482. {
  483. rgb_image = _org->rgb_image;
  484. channels = _org->channels;
  485. width = _org->width;
  486. height = _org->height;
  487. bpp = _org->bpp;
  488. externalImage = true;
  489. ImageTMP = _temp;
  490. }
  491. void CAlignAndCutImage::GetRefSize(int *ref_dx, int *ref_dy)
  492. {
  493. ref_dx[0] = t0_dx;
  494. ref_dy[0] = t0_dy;
  495. ref_dx[1] = t1_dx;
  496. ref_dy[1] = t1_dy;
  497. }
  498. void CAlignAndCutImage::Align(std::string _template0, int ref0_x, int ref0_y, std::string _template1, int ref1_x, int ref1_y, int deltax, int deltay, std::string imageROI)
  499. {
  500. int dx, dy;
  501. int r0_x, r0_y, r1_x, r1_y;
  502. // CFindTemplate* ft = new CFindTemplate(filename);
  503. CFindTemplate* ft = new CFindTemplate(rgb_image, channels, width, height, bpp);
  504. r0_x = ref0_x;
  505. r0_y = ref0_y;
  506. ft->FindTemplate(_template0, &r0_x, &r0_y, deltax, deltay);
  507. t0_dx = ft->tpl_width;
  508. t0_dy = ft->tpl_height;
  509. r1_x = ref1_x;
  510. r1_y = ref1_y;
  511. ft->FindTemplate(_template1, &r1_x, &r1_y, deltax, deltay);
  512. t1_dx = ft->tpl_width;
  513. t1_dy = ft->tpl_height;
  514. delete ft;
  515. dx = ref0_x - r0_x;
  516. dy = ref0_y - r0_y;
  517. r0_x += dx;
  518. r0_y += dy;
  519. r1_x += dx;
  520. r1_y += dy;
  521. float w_org, w_ist, d_winkel;
  522. w_org = atan2(ref1_y - ref0_y, ref1_x - ref0_x);
  523. w_ist = atan2(r1_y - r0_y, r1_x - r0_x);
  524. d_winkel = (w_org - w_ist) * 180 / M_PI;
  525. if (imageROI.length() > 0)
  526. {
  527. CImageBasis* imgzw = new CImageBasis(this);
  528. imgzw->drawRect(r0_x, r0_y, t0_dx, t0_dy, 255, 0, 0, 2);
  529. imgzw->drawRect(r1_x, r1_y, t1_dx, t1_dy, 255, 0, 0, 2);
  530. imgzw->SaveToFile(imageROI);
  531. printf("Alignment: alignment ROI created: %s\n", imageROI.c_str());
  532. delete imgzw;
  533. }
  534. string zw = "\tdx:\t" + to_string(dx) + "\tdy:\t" + to_string(dy) + "\td_winkel:\t" + to_string(d_winkel);
  535. // LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
  536. CRotate rt(this, ImageTMP);
  537. rt.Translate(dx, dy);
  538. rt.Rotate(d_winkel, ref0_x, ref0_y);
  539. printf("Alignment: dx %d - dy %d - rot %f\n", dx, dy, d_winkel);
  540. }
  541. void CAlignAndCutImage::CutAndSave(std::string _template1, int x1, int y1, int dx, int dy)
  542. {
  543. int x2, y2;
  544. x2 = x1 + dx;
  545. y2 = y1 + dy;
  546. x2 = min(x2, width - 1);
  547. y2 = min(y2, height - 1);
  548. dx = x2 - x1;
  549. dy = y2 - y1;
  550. int memsize = dx * dy * channels;
  551. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  552. stbi_uc* p_target;
  553. stbi_uc* p_source;
  554. for (int x = x1; x < x2; ++x)
  555. for (int y = y1; y < y2; ++y)
  556. {
  557. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  558. p_source = rgb_image + (channels * (y * width + x));
  559. for (int _channels = 0; _channels < channels; ++_channels)
  560. p_target[_channels] = p_source[_channels];
  561. }
  562. // stbi_write_jpg(_template1.c_str(), dx, dy, channels, odata, 0);
  563. stbi_write_bmp(_template1.c_str(), dx, dy, channels, odata);
  564. stbi_image_free(odata);
  565. }
  566. CResizeImage* CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy)
  567. {
  568. int x2, y2;
  569. x2 = x1 + dx;
  570. y2 = y1 + dy;
  571. x2 = min(x2, width - 1);
  572. y2 = min(y2, height - 1);
  573. dx = x2 - x1;
  574. dy = y2 - y1;
  575. int memsize = dx * dy * channels;
  576. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  577. stbi_uc* p_target;
  578. stbi_uc* p_source;
  579. for (int x = x1; x < x2; ++x)
  580. for (int y = y1; y < y2; ++y)
  581. {
  582. p_target = odata + (channels * ((y - y1) * dx + (x - x1)));
  583. p_source = rgb_image + (channels * (y * width + x));
  584. for (int _channels = 0; _channels < channels; ++_channels)
  585. p_target[_channels] = p_source[_channels];
  586. }
  587. CResizeImage* rs = new CResizeImage(odata, channels, dx, dy, bpp);
  588. rs->SetIndepended();
  589. return rs;
  590. }