CImageBasis.cpp 16 KB

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