CImageBasis.cpp 16 KB

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