CImageBasis.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. #include "CImageBasis.h"
  2. #include "Helper.h"
  3. #include "psram.h"
  4. #include "ClassLogFile.h"
  5. #include "server_ota.h"
  6. #include <esp_log.h>
  7. #include "../../include/defines.h"
  8. #include "esp_system.h"
  9. #include <cstring>
  10. #include <math.h>
  11. #include <algorithm>
  12. using namespace std;
  13. static const char *TAG = "C IMG BASIS";
  14. bool jpgFileTooLarge = false; // JPG creation verfication
  15. //#define DEBUG_DETAIL_ON
  16. uint8_t * CImageBasis::RGBImageLock(int _waitmaxsec)
  17. {
  18. if (islocked)
  19. {
  20. #ifdef DEBUG_DETAIL_ON
  21. ESP_LOGD(TAG, "Image is locked: sleep for: %ds", _waitmaxsec);
  22. #endif
  23. TickType_t xDelay;
  24. xDelay = 1000 / portTICK_PERIOD_MS;
  25. for (int i = 0; i <= _waitmaxsec; ++i)
  26. {
  27. vTaskDelay( xDelay );
  28. if (!islocked)
  29. break;
  30. }
  31. }
  32. if (islocked)
  33. return NULL;
  34. return rgb_image;
  35. }
  36. void CImageBasis::RGBImageRelease()
  37. {
  38. islocked = false;
  39. }
  40. uint8_t * CImageBasis::RGBImageGet()
  41. {
  42. return rgb_image;
  43. }
  44. void writejpghelp(void *context, void *data, int size)
  45. {
  46. // ESP_LOGD(TAG, "Size all: %d, size %d", ((ImageData*)context)->size, size);
  47. ImageData* _zw = (ImageData*) context;
  48. uint8_t *voidstart = _zw->data;
  49. uint8_t *datastart = (uint8_t*) data;
  50. if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy to prevent buffer overflow
  51. voidstart += _zw->size;
  52. for (int i = 0; i < size; ++i)
  53. *(voidstart + i) = *(datastart + i);
  54. _zw->size += size;
  55. }
  56. else {
  57. jpgFileTooLarge = true;
  58. }
  59. }
  60. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  61. {
  62. ImageData* ii = new ImageData;
  63. RGBImageLock();
  64. stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  65. RGBImageRelease();
  66. if (jpgFileTooLarge) {
  67. jpgFileTooLarge = false;
  68. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
  69. }
  70. return ii;
  71. }
  72. void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality)
  73. {
  74. ImageData* ii = new ImageData;
  75. RGBImageLock();
  76. stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  77. RGBImageRelease();
  78. if (jpgFileTooLarge) {
  79. jpgFileTooLarge = false;
  80. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
  81. }
  82. memCopy((uint8_t*) ii, (uint8_t*) i, sizeof(ImageData));
  83. delete ii;
  84. }
  85. struct SendJPGHTTP
  86. {
  87. httpd_req_t *req;
  88. esp_err_t res;
  89. char buf[HTTP_BUFFER_SENT];
  90. int size = 0;
  91. };
  92. inline void writejpgtohttphelp(void *context, void *data, int size)
  93. {
  94. SendJPGHTTP* _send = (SendJPGHTTP*) context;
  95. if ((_send->size + size) >= HTTP_BUFFER_SENT) // data no longer fits in buffer
  96. {
  97. if (httpd_resp_send_chunk(_send->req, _send->buf, _send->size) != ESP_OK)
  98. {
  99. ESP_LOGE(TAG, "File sending failed!");
  100. _send->res = ESP_FAIL;
  101. }
  102. _send->size = 0;
  103. }
  104. std::memcpy((void*) (&(_send->buf[0]) + _send->size), data, size);
  105. _send->size+= size;
  106. }
  107. esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
  108. {
  109. SendJPGHTTP ii;
  110. ii.req = _req;
  111. ii.res = ESP_OK;
  112. ii.size = 0;
  113. RGBImageLock();
  114. stbi_write_jpg_to_func(writejpgtohttphelp, &ii, width, height, channels, rgb_image, quality);
  115. if (ii.size > 0)
  116. {
  117. if (httpd_resp_send_chunk(_req, (char*) ii.buf, ii.size) != ESP_OK) //still send the rest
  118. {
  119. ESP_LOGE(TAG, "File sending failed!");
  120. ii.res = ESP_FAIL;
  121. }
  122. }
  123. RGBImageRelease();
  124. return ii.res;
  125. }
  126. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  127. {
  128. int gr = height * width * channels;
  129. if (gr != _size) // Size does not fit
  130. {
  131. ESP_LOGE(TAG, "Cannot copy image from memory - sizes do not match: should be %d, but is %d", _size, gr);
  132. return false;
  133. }
  134. RGBImageLock();
  135. memCopy(_source, rgb_image, _size);
  136. RGBImageRelease();
  137. return true;
  138. }
  139. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  140. {
  141. stbi_uc* p_source;
  142. p_source = rgb_image + (channels * (y * width + x));
  143. return p_source[ch];
  144. }
  145. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  146. {
  147. #ifdef _ESP32_PSRAM
  148. for (int i = 0; i < _size; ++i)
  149. *(_target + i) = *(_source + i);
  150. #else
  151. memcpy(_target, _source, _size);
  152. #endif
  153. }
  154. bool CImageBasis::isInImage(int x, int y)
  155. {
  156. if ((x < 0) || (x > width - 1))
  157. return false;
  158. if ((y < 0) || (y > height- 1))
  159. return false;
  160. return true;
  161. }
  162. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  163. {
  164. stbi_uc* p_source;
  165. RGBImageLock();
  166. p_source = rgb_image + (channels * (y * width + x));
  167. p_source[0] = r;
  168. if ( channels > 2)
  169. {
  170. p_source[1] = g;
  171. p_source[2] = b;
  172. }
  173. RGBImageRelease();
  174. }
  175. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  176. {
  177. int zwx1, zwx2, zwy1, zwy2;
  178. int _x, _y, _thick;
  179. zwx1 = x - thickness + 1;
  180. zwx2 = x + dx + thickness - 1;
  181. zwy1 = y;
  182. zwy2 = y;
  183. RGBImageLock();
  184. for (_thick = 0; _thick < thickness; _thick++)
  185. for (_x = zwx1; _x <= zwx2; ++_x)
  186. for (_y = zwy1; _y <= zwy2; _y++)
  187. if (isInImage(_x, _y))
  188. setPixelColor(_x, _y - _thick, r, g, b);
  189. zwx1 = x - thickness + 1;
  190. zwx2 = x + dx + thickness - 1;
  191. zwy1 = y + dy;
  192. zwy2 = y + dy;
  193. for (_thick = 0; _thick < thickness; _thick++)
  194. for (_x = zwx1; _x <= zwx2; ++_x)
  195. for (_y = zwy1; _y <= zwy2; _y++)
  196. if (isInImage(_x, _y))
  197. setPixelColor(_x, _y + _thick, r, g, b);
  198. zwx1 = x;
  199. zwx2 = x;
  200. zwy1 = y;
  201. zwy2 = y + dy;
  202. for (_thick = 0; _thick < thickness; _thick++)
  203. for (_x = zwx1; _x <= zwx2; ++_x)
  204. for (_y = zwy1; _y <= zwy2; _y++)
  205. if (isInImage(_x, _y))
  206. setPixelColor(_x - _thick, _y, r, g, b);
  207. zwx1 = x + dx;
  208. zwx2 = x + dx;
  209. zwy1 = y;
  210. zwy2 = y + dy;
  211. for (_thick = 0; _thick < thickness; _thick++)
  212. for (_x = zwx1; _x <= zwx2; ++_x)
  213. for (_y = zwy1; _y <= zwy2; _y++)
  214. if (isInImage(_x, _y))
  215. setPixelColor(_x + _thick, _y, r, g, b);
  216. RGBImageRelease();
  217. }
  218. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  219. {
  220. int _x, _y, _thick;
  221. int _zwy1, _zwy2;
  222. thickness = (thickness-1) / 2;
  223. RGBImageLock();
  224. for (_thick = 0; _thick <= thickness; ++_thick)
  225. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  226. {
  227. if (x2 == x1)
  228. {
  229. _zwy1 = y1;
  230. _zwy2 = y2;
  231. }
  232. else
  233. {
  234. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  235. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  236. }
  237. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  238. if (isInImage(_x, _y))
  239. setPixelColor(_x, _y, r, g, b);
  240. }
  241. RGBImageRelease();
  242. }
  243. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  244. {
  245. float deltarad, aktrad;
  246. int _thick, _x, _y;
  247. int rad = radx;
  248. if (rady > radx)
  249. rad = rady;
  250. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  251. RGBImageLock();
  252. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  253. for (_thick = 0; _thick < thickness; ++_thick)
  254. {
  255. _x = sin(aktrad) * (radx + _thick) + x1;
  256. _y = cos(aktrad) * (rady + _thick) + y1;
  257. if (isInImage(_x, _y))
  258. setPixelColor(_x, _y, r, g, b);
  259. }
  260. RGBImageRelease();
  261. }
  262. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  263. {
  264. float deltarad, aktrad;
  265. int _thick, _x, _y;
  266. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  267. RGBImageLock();
  268. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  269. for (_thick = 0; _thick < thickness; ++_thick)
  270. {
  271. _x = sin(aktrad) * (rad + _thick) + x1;
  272. _y = cos(aktrad) * (rad + _thick) + y1;
  273. if (isInImage(_x, _y))
  274. setPixelColor(_x, _y, r, g, b);
  275. }
  276. RGBImageRelease();
  277. }
  278. CImageBasis::CImageBasis(string _name)
  279. {
  280. name = _name;
  281. externalImage = false;
  282. rgb_image = NULL;
  283. width = 0;
  284. height = 0;
  285. channels = 0;
  286. islocked = false;
  287. }
  288. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  289. {
  290. bpp = _channels;
  291. width = _width;
  292. height = _height;
  293. channels = _channels;
  294. RGBImageLock();
  295. #ifdef DEBUG_DETAIL_ON
  296. LogFile.WriteHeapInfo("CreateEmptyImage");
  297. #endif
  298. memsize = width * height * channels;
  299. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  300. if (rgb_image == NULL)
  301. {
  302. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize));
  303. LogFile.WriteHeapInfo("CreateEmptyImage");
  304. RGBImageRelease();
  305. return;
  306. }
  307. stbi_uc* p_source;
  308. for (int x = 0; x < width; ++x)
  309. for (int y = 0; y < height; ++y)
  310. {
  311. p_source = rgb_image + (channels * (y * width + x));
  312. for (int _channels = 0; _channels < channels; ++_channels)
  313. p_source[_channels] = (uint8_t) 0;
  314. }
  315. RGBImageRelease();
  316. }
  317. void CImageBasis::EmptyImage()
  318. {
  319. #ifdef DEBUG_DETAIL_ON
  320. LogFile.WriteHeapInfo("EmptyImage");
  321. #endif
  322. stbi_uc* p_source;
  323. RGBImageLock();
  324. for (int x = 0; x < width; ++x)
  325. for (int y = 0; y < height; ++y)
  326. {
  327. p_source = rgb_image + (channels * (y * width + x));
  328. for (int _channels = 0; _channels < channels; ++_channels)
  329. p_source[_channels] = (uint8_t) 0;
  330. }
  331. RGBImageRelease();
  332. }
  333. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  334. {
  335. RGBImageLock();
  336. if (rgb_image != NULL) {
  337. stbi_image_free(rgb_image);
  338. //free_psram_heap(std::string(TAG) + "->rgb_image (LoadFromMemory)", rgb_image);
  339. }
  340. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, STBI_rgb);
  341. bpp = channels;
  342. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  343. if ((width * height * channels) == 0)
  344. {
  345. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! "
  346. "Check that your camera module is working and connected properly.");
  347. LogFile.WriteHeapInfo("LoadFromMemory");
  348. doReboot();
  349. }
  350. RGBImageRelease();
  351. }
  352. void CImageBasis::crop_image(unsigned short cropLeft, unsigned short cropRight, unsigned short cropTop, unsigned short cropBottom)
  353. {
  354. unsigned int maxTopIndex = cropTop * width * channels;
  355. unsigned int minBottomIndex = ((width*height) - (cropBottom * width)) * channels;
  356. unsigned short maxX = width - cropRight; // In pixels
  357. unsigned short newWidth = width - cropLeft - cropRight;
  358. unsigned short newHeight = height - cropTop - cropBottom;
  359. unsigned int writeIndex = 0;
  360. // Loop over all bytes
  361. for (int i = 0; i < width * height * channels; i += channels) {
  362. // Calculate current X, Y pixel position
  363. int x = (i/channels) % width;
  364. // Crop from the top
  365. if (i < maxTopIndex) { continue; }
  366. // Crop from the bottom
  367. if (i > minBottomIndex) { continue; }
  368. // Crop from the left
  369. if (x <= cropLeft) { continue; }
  370. // Crop from the right
  371. if (x > maxX) { continue; }
  372. // If we get here, keep the pixels
  373. for (int c = 0; c < channels; c++) {
  374. rgb_image[writeIndex++] = rgb_image[i+c];
  375. }
  376. }
  377. // Set the new dimensions of the framebuffer for further use.
  378. width = newWidth;
  379. height = newHeight;
  380. }
  381. CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
  382. {
  383. name = _name;
  384. islocked = false;
  385. externalImage = false;
  386. channels = _copyfrom->channels;
  387. width = _copyfrom->width;
  388. height = _copyfrom->height;
  389. bpp = _copyfrom->bpp;
  390. RGBImageLock();
  391. #ifdef DEBUG_DETAIL_ON
  392. LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
  393. #endif
  394. memsize = width * height * channels;
  395. if (name == "tmpImage") {
  396. rgb_image = (unsigned char*)psram_reserve_shared_tmp_image_memory();
  397. }
  398. else {
  399. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  400. }
  401. if (rgb_image == NULL)
  402. {
  403. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize));
  404. LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
  405. RGBImageRelease();
  406. return;
  407. }
  408. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  409. RGBImageRelease();
  410. #ifdef DEBUG_DETAIL_ON
  411. LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
  412. #endif
  413. }
  414. CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
  415. {
  416. name = _name;
  417. islocked = false;
  418. externalImage = false;
  419. channels = _channels;
  420. width = _width;
  421. height = _height;
  422. bpp = _channels;
  423. RGBImageLock();
  424. #ifdef DEBUG_DETAIL_ON
  425. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
  426. #endif
  427. memsize = width * height * channels;
  428. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  429. if (rgb_image == NULL)
  430. {
  431. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize));
  432. LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
  433. RGBImageRelease();
  434. return;
  435. }
  436. RGBImageRelease();
  437. #ifdef DEBUG_DETAIL_ON
  438. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
  439. #endif
  440. }
  441. CImageBasis::CImageBasis(string _name, std::string _image)
  442. {
  443. name = _name;
  444. islocked = false;
  445. channels = 3;
  446. externalImage = false;
  447. filename = _image;
  448. if (file_size(_image.c_str()) == 0) {
  449. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  450. return;
  451. }
  452. RGBImageLock();
  453. #ifdef DEBUG_DETAIL_ON
  454. LogFile.WriteHeapInfo("CImageBasis_image - Start");
  455. #endif
  456. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  457. if (rgb_image == NULL) {
  458. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
  459. LogFile.WriteHeapInfo("CImageBasis-image");
  460. RGBImageRelease();
  461. return;
  462. }
  463. RGBImageRelease();
  464. #ifdef DEBUG_DETAIL_ON
  465. std::string zw = "CImageBasis after load " + _image;
  466. ESP_LOGD(TAG, "%s", zw.c_str());
  467. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  468. #endif
  469. #ifdef DEBUG_DETAIL_ON
  470. LogFile.WriteHeapInfo("CImageBasis_image - done");
  471. #endif
  472. }
  473. bool CImageBasis::ImageOkay(){
  474. return rgb_image != NULL;
  475. }
  476. CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  477. {
  478. name = _name;
  479. islocked = false;
  480. rgb_image = _rgb_image;
  481. channels = _channels;
  482. width = _width;
  483. height = _height;
  484. bpp = _bpp;
  485. externalImage = true;
  486. }
  487. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  488. {
  489. stbi_uc* p_source;
  490. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  491. float intercept = 128 * (1 - contrast);
  492. RGBImageLock();
  493. for (int x = 0; x < width; ++x)
  494. for (int y = 0; y < height; ++y)
  495. {
  496. p_source = rgb_image + (channels * (y * width + x));
  497. for (int _channels = 0; _channels < channels; ++_channels)
  498. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  499. }
  500. RGBImageRelease();
  501. }
  502. CImageBasis::~CImageBasis()
  503. {
  504. RGBImageLock();
  505. if (!externalImage) {
  506. if (name == "tmpImage") { // This image should be placed in the shared part of PSRAM
  507. psram_free_shared_temp_image_memory();
  508. }
  509. else { // All other images are much smaller and can go into the normal PSRAM region
  510. //stbi_image_free(rgb_image);
  511. if (memsize == 0) {
  512. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Not freeing (" + name + " as there was never PSRAM allocated for it)");
  513. }
  514. else {
  515. free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + to_string(memsize) + ")", rgb_image);
  516. }
  517. }
  518. }
  519. RGBImageRelease();
  520. }
  521. void CImageBasis::SaveToFile(std::string _imageout)
  522. {
  523. string typ = getFileType(_imageout);
  524. RGBImageLock();
  525. if ((typ == "jpg") || (typ == "JPG")) // CAUTION PROBLEMATIC IN ESP32
  526. {
  527. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  528. }
  529. #ifndef STBI_ONLY_JPEG
  530. if ((typ == "bmp") || (typ == "BMP"))
  531. {
  532. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  533. }
  534. #endif
  535. RGBImageRelease();
  536. }
  537. void CImageBasis::Resize(int _new_dx, int _new_dy)
  538. {
  539. memsize = _new_dx * _new_dy * channels;
  540. uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  541. RGBImageLock();
  542. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  543. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  544. memCopy(odata, rgb_image, memsize);
  545. width = _new_dx;
  546. height = _new_dy;
  547. free_psram_heap(std::string(TAG) + "->odata", odata);
  548. RGBImageRelease();
  549. }
  550. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  551. {
  552. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  553. {
  554. ESP_LOGE(TAG, "Resize - Target image size does not fit!");
  555. return;
  556. }
  557. RGBImageLock();
  558. uint8_t* odata = _target->rgb_image;
  559. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  560. RGBImageRelease();
  561. }