CImageBasis.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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, 3);
  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. CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
  353. {
  354. name = _name;
  355. islocked = false;
  356. externalImage = false;
  357. channels = _copyfrom->channels;
  358. width = _copyfrom->width;
  359. height = _copyfrom->height;
  360. bpp = _copyfrom->bpp;
  361. RGBImageLock();
  362. #ifdef DEBUG_DETAIL_ON
  363. LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
  364. #endif
  365. memsize = width * height * channels;
  366. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  367. if (rgb_image == NULL)
  368. {
  369. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize));
  370. LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
  371. RGBImageRelease();
  372. return;
  373. }
  374. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  375. RGBImageRelease();
  376. #ifdef DEBUG_DETAIL_ON
  377. LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
  378. #endif
  379. }
  380. CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
  381. {
  382. name = _name;
  383. islocked = false;
  384. externalImage = false;
  385. channels = _channels;
  386. width = _width;
  387. height = _height;
  388. bpp = _channels;
  389. RGBImageLock();
  390. #ifdef DEBUG_DETAIL_ON
  391. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
  392. #endif
  393. memsize = width * height * channels;
  394. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  395. if (rgb_image == NULL)
  396. {
  397. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize));
  398. LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
  399. RGBImageRelease();
  400. return;
  401. }
  402. RGBImageRelease();
  403. #ifdef DEBUG_DETAIL_ON
  404. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
  405. #endif
  406. }
  407. CImageBasis::CImageBasis(string _name, std::string _image)
  408. {
  409. name = _name;
  410. islocked = false;
  411. channels = 3;
  412. externalImage = false;
  413. filename = _image;
  414. if (file_size(_image.c_str()) == 0) {
  415. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  416. return;
  417. }
  418. RGBImageLock();
  419. #ifdef DEBUG_DETAIL_ON
  420. LogFile.WriteHeapInfo("CImageBasis_image - Start");
  421. #endif
  422. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  423. if (rgb_image == NULL) {
  424. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
  425. LogFile.WriteHeapInfo("CImageBasis-image");
  426. RGBImageRelease();
  427. return;
  428. }
  429. RGBImageRelease();
  430. #ifdef DEBUG_DETAIL_ON
  431. std::string zw = "CImageBasis after load " + _image;
  432. ESP_LOGD(TAG, "%s", zw.c_str());
  433. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  434. #endif
  435. #ifdef DEBUG_DETAIL_ON
  436. LogFile.WriteHeapInfo("CImageBasis_image - done");
  437. #endif
  438. }
  439. bool CImageBasis::ImageOkay(){
  440. return rgb_image != NULL;
  441. }
  442. CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  443. {
  444. name = _name;
  445. islocked = false;
  446. rgb_image = _rgb_image;
  447. channels = _channels;
  448. width = _width;
  449. height = _height;
  450. bpp = _bpp;
  451. externalImage = true;
  452. }
  453. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  454. {
  455. stbi_uc* p_source;
  456. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  457. float intercept = 128 * (1 - contrast);
  458. RGBImageLock();
  459. for (int x = 0; x < width; ++x)
  460. for (int y = 0; y < height; ++y)
  461. {
  462. p_source = rgb_image + (channels * (y * width + x));
  463. for (int _channels = 0; _channels < channels; ++_channels)
  464. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  465. }
  466. RGBImageRelease();
  467. }
  468. CImageBasis::~CImageBasis()
  469. {
  470. RGBImageLock();
  471. if (!externalImage) {
  472. //stbi_image_free(rgb_image);
  473. free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + to_string(memsize) + ")", rgb_image);
  474. }
  475. RGBImageRelease();
  476. }
  477. void CImageBasis::SaveToFile(std::string _imageout)
  478. {
  479. string typ = getFileType(_imageout);
  480. RGBImageLock();
  481. if ((typ == "jpg") || (typ == "JPG")) // CAUTION PROBLEMATIC IN ESP32
  482. {
  483. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  484. }
  485. #ifndef STBI_ONLY_JPEG
  486. if ((typ == "bmp") || (typ == "BMP"))
  487. {
  488. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  489. }
  490. #endif
  491. RGBImageRelease();
  492. }
  493. void CImageBasis::Resize(int _new_dx, int _new_dy)
  494. {
  495. memsize = _new_dx * _new_dy * channels;
  496. uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
  497. RGBImageLock();
  498. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  499. rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
  500. memCopy(odata, rgb_image, memsize);
  501. width = _new_dx;
  502. height = _new_dy;
  503. free_psram_heap(std::string(TAG) + "->odata", odata);
  504. RGBImageRelease();
  505. }
  506. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  507. {
  508. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  509. {
  510. ESP_LOGE(TAG, "Resize - Target image size does not fit!");
  511. return;
  512. }
  513. RGBImageLock();
  514. uint8_t* odata = _target->rgb_image;
  515. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  516. RGBImageRelease();
  517. }