CImageBasis.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 <limits.h>
  11. #include <math.h>
  12. #include <algorithm>
  13. using namespace std;
  14. static const char *TAG = "C IMG BASIS";
  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. ImageData* output = static_cast<ImageData*>(context);
  47. if (output == nullptr || data == nullptr || size <= 0 || output->size > MAX_JPG_SIZE) return;
  48. if (static_cast<size_t>(size) > (MAX_JPG_SIZE - output->size)) {
  49. output->size = MAX_JPG_SIZE + 1U;
  50. return;
  51. }
  52. std::memcpy(output->data + output->size, data, static_cast<size_t>(size));
  53. output->size += static_cast<size_t>(size);
  54. }
  55. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  56. {
  57. ImageData* ii = new (std::nothrow) ImageData;
  58. if (ii == nullptr) {
  59. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: PSRAM allocation failed");
  60. return nullptr;
  61. }
  62. writeToMemoryAsJPG(ii, quality);
  63. return ii;
  64. }
  65. void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality)
  66. {
  67. if (i == nullptr) return;
  68. i->size = 0;
  69. uint8_t* image = RGBImageLock();
  70. if (image == nullptr) {
  71. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: image is unavailable or locked");
  72. return;
  73. }
  74. const int success = stbi_write_jpg_to_func(writejpghelp, i, width, height, channels, image, quality);
  75. RGBImageRelease();
  76. if (i->size > MAX_JPG_SIZE) {
  77. i->size = 0;
  78. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
  79. } else if (!success) {
  80. i->size = 0;
  81. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPEG encoder failed");
  82. }
  83. }
  84. struct SendJPGHTTP
  85. {
  86. httpd_req_t *req;
  87. esp_err_t res;
  88. char *buf;
  89. size_t size = 0;
  90. };
  91. static void flushJPGHTTPBuffer(SendJPGHTTP* send)
  92. {
  93. if (send == nullptr || send->res != ESP_OK || send->size == 0) return;
  94. if (httpd_resp_send_chunk(send->req, send->buf, send->size) != ESP_OK) {
  95. ESP_LOGE(TAG, "File sending failed!");
  96. send->res = ESP_FAIL;
  97. }
  98. send->size = 0;
  99. }
  100. inline void writejpgtohttphelp(void *context, void *data, int size)
  101. {
  102. SendJPGHTTP* send = static_cast<SendJPGHTTP*>(context);
  103. if (send == nullptr || data == nullptr || size <= 0 || send->res != ESP_OK) return;
  104. const uint8_t* source = static_cast<const uint8_t*>(data);
  105. size_t remaining = static_cast<size_t>(size);
  106. while (remaining > 0 && send->res == ESP_OK) {
  107. if (send->size >= HTTP_BUFFER_SENT) {
  108. flushJPGHTTPBuffer(send);
  109. if (send->res != ESP_OK) return;
  110. }
  111. const size_t available = HTTP_BUFFER_SENT - send->size;
  112. const size_t copySize = std::min(available, remaining);
  113. std::memcpy(send->buf + send->size, source, copySize);
  114. send->size += copySize;
  115. source += copySize;
  116. remaining -= copySize;
  117. }
  118. }
  119. esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
  120. {
  121. if (_req == nullptr) return ESP_FAIL;
  122. SendJPGHTTP send;
  123. send.req = _req; send.res = ESP_OK; send.size = 0;
  124. send.buf = static_cast<char*>(malloc_psram_heap("JPEG HTTP stream buffer", HTTP_BUFFER_SENT, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  125. if (send.buf == nullptr) return ESP_ERR_NO_MEM;
  126. uint8_t* image = RGBImageLock();
  127. if (image == nullptr) {
  128. free_psram_heap("JPEG HTTP stream buffer", send.buf);
  129. return ESP_FAIL;
  130. }
  131. const int encoded = stbi_write_jpg_to_func(writejpgtohttphelp, &send, width, height, channels, image, quality);
  132. if (!encoded && send.res == ESP_OK) send.res = ESP_FAIL;
  133. flushJPGHTTPBuffer(&send);
  134. RGBImageRelease();
  135. free_psram_heap("JPEG HTTP stream buffer", send.buf);
  136. return send.res;
  137. }
  138. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  139. {
  140. if (_source == nullptr || rgb_image == nullptr || _size < 0 || width <= 0 || height <= 0 || channels <= 0) return false;
  141. const size_t expected = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  142. if (expected > INT_MAX || expected != static_cast<size_t>(_size)) {
  143. ESP_LOGE(TAG, "Cannot copy image from memory - sizes do not match: should be %u, but is %d",
  144. static_cast<unsigned int>(expected), _size);
  145. return false;
  146. }
  147. uint8_t* image = RGBImageLock();
  148. if (image == nullptr) return false;
  149. memCopy(_source, image, _size);
  150. RGBImageRelease();
  151. return true;
  152. }
  153. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  154. {
  155. if (rgb_image == nullptr || !isInImage(x, y) || ch < 0 || ch >= channels) return 0;
  156. const size_t offset = static_cast<size_t>(channels) *
  157. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  158. return rgb_image[offset + static_cast<size_t>(ch)];
  159. }
  160. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  161. {
  162. #ifdef _ESP32_PSRAM
  163. for (int i = 0; i < _size; ++i)
  164. *(_target + i) = *(_source + i);
  165. #else
  166. memcpy(_target, _source, _size);
  167. #endif
  168. }
  169. bool CImageBasis::isInImage(int x, int y)
  170. {
  171. if ((x < 0) || (x > width - 1))
  172. return false;
  173. if ((y < 0) || (y > height- 1))
  174. return false;
  175. return true;
  176. }
  177. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  178. {
  179. if (rgb_image == nullptr || !isInImage(x, y) || channels <= 0) return;
  180. uint8_t* image = RGBImageLock();
  181. if (image == nullptr) return;
  182. const size_t offset = static_cast<size_t>(channels) *
  183. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  184. uint8_t* pixel = image + offset;
  185. pixel[0] = static_cast<uint8_t>(r);
  186. if (channels > 2) { pixel[1] = static_cast<uint8_t>(g); pixel[2] = static_cast<uint8_t>(b); }
  187. RGBImageRelease();
  188. }
  189. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  190. {
  191. int zwx1, zwx2, zwy1, zwy2;
  192. int _x, _y, _thick;
  193. zwx1 = x - thickness + 1;
  194. zwx2 = x + dx + thickness - 1;
  195. zwy1 = y;
  196. zwy2 = y;
  197. RGBImageLock();
  198. for (_thick = 0; _thick < thickness; _thick++)
  199. for (_x = zwx1; _x <= zwx2; ++_x)
  200. for (_y = zwy1; _y <= zwy2; _y++)
  201. if (isInImage(_x, _y))
  202. setPixelColor(_x, _y - _thick, r, g, b);
  203. zwx1 = x - thickness + 1;
  204. zwx2 = x + dx + thickness - 1;
  205. zwy1 = y + dy;
  206. zwy2 = y + dy;
  207. for (_thick = 0; _thick < thickness; _thick++)
  208. for (_x = zwx1; _x <= zwx2; ++_x)
  209. for (_y = zwy1; _y <= zwy2; _y++)
  210. if (isInImage(_x, _y))
  211. setPixelColor(_x, _y + _thick, r, g, b);
  212. zwx1 = x;
  213. zwx2 = x;
  214. zwy1 = y;
  215. zwy2 = y + dy;
  216. for (_thick = 0; _thick < thickness; _thick++)
  217. for (_x = zwx1; _x <= zwx2; ++_x)
  218. for (_y = zwy1; _y <= zwy2; _y++)
  219. if (isInImage(_x, _y))
  220. setPixelColor(_x - _thick, _y, r, g, b);
  221. zwx1 = x + dx;
  222. zwx2 = x + dx;
  223. zwy1 = y;
  224. zwy2 = y + dy;
  225. for (_thick = 0; _thick < thickness; _thick++)
  226. for (_x = zwx1; _x <= zwx2; ++_x)
  227. for (_y = zwy1; _y <= zwy2; _y++)
  228. if (isInImage(_x, _y))
  229. setPixelColor(_x + _thick, _y, r, g, b);
  230. RGBImageRelease();
  231. }
  232. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  233. {
  234. int _x, _y, _thick;
  235. int _zwy1, _zwy2;
  236. thickness = (thickness-1) / 2;
  237. RGBImageLock();
  238. for (_thick = 0; _thick <= thickness; ++_thick)
  239. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  240. {
  241. if (x2 == x1)
  242. {
  243. _zwy1 = y1;
  244. _zwy2 = y2;
  245. }
  246. else
  247. {
  248. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  249. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  250. }
  251. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  252. if (isInImage(_x, _y))
  253. setPixelColor(_x, _y, r, g, b);
  254. }
  255. RGBImageRelease();
  256. }
  257. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  258. {
  259. float deltarad, aktrad;
  260. int _thick, _x, _y;
  261. int rad = radx;
  262. if (rady > radx)
  263. rad = rady;
  264. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  265. RGBImageLock();
  266. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  267. for (_thick = 0; _thick < thickness; ++_thick)
  268. {
  269. _x = sin(aktrad) * (radx + _thick) + x1;
  270. _y = cos(aktrad) * (rady + _thick) + y1;
  271. if (isInImage(_x, _y))
  272. setPixelColor(_x, _y, r, g, b);
  273. }
  274. RGBImageRelease();
  275. }
  276. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  277. {
  278. float deltarad, aktrad;
  279. int _thick, _x, _y;
  280. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  281. RGBImageLock();
  282. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  283. for (_thick = 0; _thick < thickness; ++_thick)
  284. {
  285. _x = sin(aktrad) * (rad + _thick) + x1;
  286. _y = cos(aktrad) * (rad + _thick) + y1;
  287. if (isInImage(_x, _y))
  288. setPixelColor(_x, _y, r, g, b);
  289. }
  290. RGBImageRelease();
  291. }
  292. CImageBasis::CImageBasis(string _name)
  293. {
  294. name = _name;
  295. externalImage = false;
  296. rgb_image = NULL;
  297. width = 0;
  298. height = 0;
  299. channels = 0;
  300. islocked = false;
  301. }
  302. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  303. {
  304. if (_width <= 0 || _height <= 0 || _channels <= 0 || name == "tmpImage") return;
  305. const size_t w = static_cast<size_t>(_width), h = static_cast<size_t>(_height), c = static_cast<size_t>(_channels);
  306. if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return;
  307. const size_t newSize = w * h * c;
  308. if (newSize > INT_MAX) return;
  309. uint8_t* newImage = static_cast<uint8_t*>(malloc_psram_heap(
  310. std::string(TAG) + "->CImageBasis (" + name + ")", newSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  311. if (newImage == nullptr) {
  312. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: allocation failed");
  313. LogFile.WriteHeapInfo("CreateEmptyImage");
  314. return;
  315. }
  316. if (rgb_image != nullptr && !externalImage) stbi_image_free(rgb_image);
  317. rgb_image = newImage;
  318. width = _width; height = _height; channels = _channels; bpp = _channels;
  319. memsize = static_cast<int>(newSize);
  320. externalImage = false;
  321. std::memset(rgb_image, 0, newSize);
  322. }
  323. void CImageBasis::EmptyImage()
  324. {
  325. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  326. uint8_t* image = RGBImageLock();
  327. if (image == nullptr) return;
  328. const size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  329. std::memset(image, 0, size);
  330. RGBImageRelease();
  331. }
  332. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  333. {
  334. RGBImageLock();
  335. if (rgb_image != NULL && !externalImage) {
  336. if (name == "tmpImage" && memsize > 0) psram_free_shared_temp_image_memory();
  337. else stbi_image_free(rgb_image);
  338. }
  339. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, STBI_rgb);
  340. memsize = 0;
  341. externalImage = false;
  342. bpp = channels;
  343. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  344. if ((width * height * channels) == 0) {
  345. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! Check that your camera module is working and connected properly.");
  346. LogFile.WriteHeapInfo("LoadFromMemory");
  347. doReboot();
  348. }
  349. RGBImageRelease();
  350. }
  351. void CImageBasis::crop_image(unsigned short cropLeft, unsigned short cropRight, unsigned short cropTop, unsigned short cropBottom)
  352. {
  353. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0 ||
  354. static_cast<unsigned int>(cropLeft) + cropRight >= static_cast<unsigned int>(width) ||
  355. static_cast<unsigned int>(cropTop) + cropBottom >= static_cast<unsigned int>(height)) return;
  356. const unsigned int top = static_cast<unsigned int>(cropTop) * static_cast<unsigned int>(width) * static_cast<unsigned int>(channels);
  357. const unsigned int bottom = (static_cast<unsigned int>(width) * static_cast<unsigned int>(height) -
  358. static_cast<unsigned int>(cropBottom) * static_cast<unsigned int>(width)) * static_cast<unsigned int>(channels);
  359. const unsigned int maxX = static_cast<unsigned int>(width) - cropRight;
  360. const unsigned int newWidth = static_cast<unsigned int>(width) - cropLeft - cropRight;
  361. const unsigned int newHeight = static_cast<unsigned int>(height) - cropTop - cropBottom;
  362. size_t writeIndex = 0;
  363. const size_t inputSize = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  364. for (size_t i = 0; i < inputSize; i += static_cast<size_t>(channels)) {
  365. const unsigned int x = static_cast<unsigned int>((i / static_cast<size_t>(channels)) % static_cast<size_t>(width));
  366. if (i < top || i >= bottom || x < cropLeft || x >= maxX) continue;
  367. for (int channel = 0; channel < channels; ++channel) rgb_image[writeIndex++] = rgb_image[i + static_cast<size_t>(channel)];
  368. }
  369. width = static_cast<int>(newWidth);
  370. height = static_cast<int>(newHeight);
  371. }
  372. CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
  373. {
  374. name = _name;
  375. islocked = false;
  376. externalImage = false;
  377. channels = _copyfrom->channels;
  378. width = _copyfrom->width;
  379. height = _copyfrom->height;
  380. bpp = _copyfrom->bpp;
  381. RGBImageLock();
  382. #ifdef DEBUG_DETAIL_ON
  383. LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
  384. #endif
  385. memsize = width * height * channels;
  386. if (name == "tmpImage") {
  387. rgb_image = (unsigned char*)psram_reserve_shared_tmp_image_memory();
  388. }
  389. else {
  390. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  391. }
  392. if (rgb_image == NULL)
  393. {
  394. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize));
  395. LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
  396. RGBImageRelease();
  397. return;
  398. }
  399. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  400. RGBImageRelease();
  401. #ifdef DEBUG_DETAIL_ON
  402. LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
  403. #endif
  404. }
  405. CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
  406. {
  407. name = _name;
  408. islocked = false;
  409. externalImage = false;
  410. channels = _channels;
  411. width = _width;
  412. height = _height;
  413. bpp = _channels;
  414. RGBImageLock();
  415. #ifdef DEBUG_DETAIL_ON
  416. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
  417. #endif
  418. memsize = width * height * channels;
  419. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  420. if (rgb_image == NULL)
  421. {
  422. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize));
  423. LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
  424. RGBImageRelease();
  425. return;
  426. }
  427. RGBImageRelease();
  428. #ifdef DEBUG_DETAIL_ON
  429. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
  430. #endif
  431. }
  432. CImageBasis::CImageBasis(string _name, std::string _image)
  433. {
  434. name = _name;
  435. islocked = false;
  436. channels = 3;
  437. externalImage = false;
  438. filename = _image;
  439. if (file_size(_image.c_str()) == 0) {
  440. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  441. return;
  442. }
  443. RGBImageLock();
  444. #ifdef DEBUG_DETAIL_ON
  445. LogFile.WriteHeapInfo("CImageBasis_image - Start");
  446. #endif
  447. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  448. if (rgb_image == NULL) {
  449. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
  450. LogFile.WriteHeapInfo("CImageBasis-image");
  451. RGBImageRelease();
  452. return;
  453. }
  454. RGBImageRelease();
  455. #ifdef DEBUG_DETAIL_ON
  456. std::string zw = "CImageBasis after load " + _image;
  457. ESP_LOGD(TAG, "%s", zw.c_str());
  458. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  459. #endif
  460. #ifdef DEBUG_DETAIL_ON
  461. LogFile.WriteHeapInfo("CImageBasis_image - done");
  462. #endif
  463. }
  464. bool CImageBasis::ImageOkay(){
  465. return rgb_image != NULL;
  466. }
  467. CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  468. {
  469. name = _name;
  470. islocked = false;
  471. rgb_image = _rgb_image;
  472. channels = _channels;
  473. width = _width;
  474. height = _height;
  475. bpp = _bpp;
  476. externalImage = true;
  477. }
  478. void CImageBasis::Negative(void)
  479. {
  480. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  481. uint8_t* image = RGBImageLock();
  482. if (image == nullptr) return;
  483. const size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  484. for (size_t i = 0; i < size; i += static_cast<size_t>(channels))
  485. for (int c = 0; c < channels; ++c) image[i + static_cast<size_t>(c)] = 255 - image[i + static_cast<size_t>(c)];
  486. RGBImageRelease();
  487. }
  488. void CImageBasis::Contrast(float _contrast)
  489. {
  490. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  491. uint8_t* image = RGBImageLock();
  492. if (image == nullptr) return;
  493. const float contrast = (_contrast / 100) + 1;
  494. const float intercept = 128 * (1 - contrast);
  495. for (int y = 0; y < height; ++y) {
  496. for (int x = 0; x < width; ++x) {
  497. const size_t offset = static_cast<size_t>(channels) *
  498. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  499. uint8_t* pixel = image + offset;
  500. for (int channel = 0; channel < channels; ++channel)
  501. pixel[channel] = static_cast<uint8_t>(std::min(255, std::max(0,
  502. static_cast<int>(pixel[channel] * contrast + intercept))));
  503. }
  504. }
  505. RGBImageRelease();
  506. }
  507. CImageBasis::~CImageBasis()
  508. {
  509. RGBImageLock();
  510. if (!externalImage) {
  511. if (name == "tmpImage") {
  512. if (memsize == 0) stbi_image_free(rgb_image);
  513. else psram_free_shared_temp_image_memory();
  514. } else if (memsize == 0) stbi_image_free(rgb_image);
  515. else free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + to_string(memsize) + ")", rgb_image);
  516. }
  517. RGBImageRelease();
  518. }
  519. void CImageBasis::SaveToFile(std::string _imageout)
  520. {
  521. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  522. const string typ = getFileType(_imageout);
  523. uint8_t* image = RGBImageLock();
  524. if (image == nullptr) return;
  525. if ((typ == "jpg") || (typ == "JPG")) stbi_write_jpg(_imageout.c_str(), width, height, channels, image, 0);
  526. #ifndef STBI_ONLY_JPEG
  527. if ((typ == "bmp") || (typ == "BMP")) stbi_write_bmp(_imageout.c_str(), width, height, channels, image);
  528. #endif
  529. RGBImageRelease();
  530. }
  531. void CImageBasis::Resize(int _new_dx, int _new_dy)
  532. {
  533. // tmpImage lives in the shared PSRAM region and its destructor relies on that; never resize it in place
  534. if ((rgb_image == NULL) || (name == "tmpImage")) {
  535. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: not possible for image " + name);
  536. return;
  537. }
  538. int new_memsize = _new_dx * _new_dy * channels;
  539. uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", new_memsize, MALLOC_CAP_SPIRAM);
  540. if (odata == NULL) {
  541. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: Can't allocate enough memory: " + std::to_string(new_memsize));
  542. return;
  543. }
  544. RGBImageLock();
  545. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  546. // Release the old buffer with the allocator it came from (same rule as the destructor)
  547. if (!externalImage) {
  548. if (memsize == 0) { // Loaded via STBI (STBI_MALLOC)
  549. stbi_image_free(rgb_image);
  550. }
  551. else {
  552. free_psram_heap(std::string(TAG) + "->CImageBasis Resize old (" + name + ")", rgb_image);
  553. }
  554. }
  555. rgb_image = odata;
  556. memsize = new_memsize;
  557. externalImage = false; // The image now owns a normal PSRAM buffer
  558. width = _new_dx;
  559. height = _new_dy;
  560. RGBImageRelease();
  561. }
  562. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  563. {
  564. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  565. {
  566. ESP_LOGE(TAG, "Resize - Target image size does not fit!");
  567. return;
  568. }
  569. RGBImageLock();
  570. uint8_t* odata = _target->rgb_image;
  571. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  572. RGBImageRelease();
  573. }