CImageBasis.cpp 15 KB

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