CImageBasis.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. uint8_t * CImageBasis::RGBImageLock(int _waitmaxsec)
  14. {
  15. if (islocked)
  16. {
  17. #ifdef DEBUG_DETAIL_ON
  18. ESP_LOGD(TAG, "Image is locked: sleep for: %ds", _waitmaxsec);
  19. #endif
  20. TickType_t xDelay;
  21. xDelay = 1000 / portTICK_PERIOD_MS;
  22. for (int i = 0; i <= _waitmaxsec; ++i)
  23. {
  24. vTaskDelay( xDelay );
  25. if (!islocked)
  26. break;
  27. }
  28. }
  29. if (islocked)
  30. return NULL;
  31. return rgb_image;
  32. }
  33. void CImageBasis::RGBImageRelease()
  34. {
  35. islocked = false;
  36. }
  37. uint8_t * CImageBasis::RGBImageGet()
  38. {
  39. return rgb_image;
  40. }
  41. void writejpghelp(void *context, void *data, int size)
  42. {
  43. // ESP_LOGD(TAG, "Size all: %d, size %d", ((ImageData*)context)->size, size);
  44. ImageData* _zw = (ImageData*) context;
  45. uint8_t *voidstart = _zw->data;
  46. uint8_t *datastart = (uint8_t*) data;
  47. voidstart += _zw->size;
  48. for (int i = 0; i < size; ++i)
  49. *(voidstart + i) = *(datastart + i);
  50. _zw->size += size;
  51. }
  52. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  53. {
  54. ImageData* ii = new ImageData;
  55. RGBImageLock();
  56. stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  57. RGBImageRelease();
  58. return ii;
  59. }
  60. struct SendJPGHTTP
  61. {
  62. httpd_req_t *req;
  63. esp_err_t res;
  64. char buf[HTTP_BUFFER_SENT];
  65. int size = 0;
  66. };
  67. inline void writejpgtohttphelp(void *context, void *data, int size)
  68. {
  69. SendJPGHTTP* _send = (SendJPGHTTP*) context;
  70. if ((_send->size + size) >= HTTP_BUFFER_SENT) // data no longer fits in buffer
  71. {
  72. httpd_req_t *_req = _send->req;
  73. if (httpd_resp_send_chunk(_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. RGBImageRelease();
  92. if (ii.size > 0)
  93. {
  94. if (httpd_resp_send_chunk(_req, (char*) ii.buf, ii.size) != ESP_OK) //still send the rest
  95. {
  96. ESP_LOGE(TAG, "File sending failed!");
  97. ii.res = ESP_FAIL;
  98. }
  99. }
  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_LOGD(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. for (_thick = 0; _thick < thickness; _thick++)
  160. for (_x = zwx1; _x <= zwx2; ++_x)
  161. for (_y = zwy1; _y <= zwy2; _y++)
  162. if (isInImage(_x, _y))
  163. setPixelColor(_x, _y - _thick, r, g, b);
  164. zwx1 = x - thickness + 1;
  165. zwx2 = x + dx + thickness - 1;
  166. zwy1 = y + dy;
  167. zwy2 = y + dy;
  168. for (_thick = 0; _thick < thickness; _thick++)
  169. for (_x = zwx1; _x <= zwx2; ++_x)
  170. for (_y = zwy1; _y <= zwy2; _y++)
  171. if (isInImage(_x, _y))
  172. setPixelColor(_x, _y + _thick, r, g, b);
  173. zwx1 = x;
  174. zwx2 = x;
  175. zwy1 = y;
  176. zwy2 = y + dy;
  177. for (_thick = 0; _thick < thickness; _thick++)
  178. for (_x = zwx1; _x <= zwx2; ++_x)
  179. for (_y = zwy1; _y <= zwy2; _y++)
  180. if (isInImage(_x, _y))
  181. setPixelColor(_x - _thick, _y, r, g, b);
  182. zwx1 = x + dx;
  183. zwx2 = x + dx;
  184. zwy1 = y;
  185. zwy2 = y + dy;
  186. for (_thick = 0; _thick < thickness; _thick++)
  187. for (_x = zwx1; _x <= zwx2; ++_x)
  188. for (_y = zwy1; _y <= zwy2; _y++)
  189. if (isInImage(_x, _y))
  190. setPixelColor(_x + _thick, _y, r, g, b);
  191. }
  192. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  193. {
  194. int _x, _y, _thick;
  195. int _zwy1, _zwy2;
  196. thickness = (thickness-1) / 2;
  197. for (_thick = 0; _thick <= thickness; ++_thick)
  198. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  199. {
  200. if (x2 == x1)
  201. {
  202. _zwy1 = y1;
  203. _zwy2 = y2;
  204. }
  205. else
  206. {
  207. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  208. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  209. }
  210. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  211. if (isInImage(_x, _y))
  212. setPixelColor(_x, _y, r, g, b);
  213. }
  214. }
  215. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  216. {
  217. float deltarad, aktrad;
  218. int _thick, _x, _y;
  219. int rad = radx;
  220. if (rady > radx)
  221. rad = rady;
  222. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  223. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  224. for (_thick = 0; _thick < thickness; ++_thick)
  225. {
  226. _x = sin(aktrad) * (radx + _thick) + x1;
  227. _y = cos(aktrad) * (rady + _thick) + y1;
  228. if (isInImage(_x, _y))
  229. setPixelColor(_x, _y, r, g, b);
  230. }
  231. }
  232. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  233. {
  234. float deltarad, aktrad;
  235. int _thick, _x, _y;
  236. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  237. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  238. for (_thick = 0; _thick < thickness; ++_thick)
  239. {
  240. _x = sin(aktrad) * (rad + _thick) + x1;
  241. _y = cos(aktrad) * (rad + _thick) + y1;
  242. if (isInImage(_x, _y))
  243. setPixelColor(_x, _y, r, g, b);
  244. }
  245. }
  246. CImageBasis::CImageBasis()
  247. {
  248. externalImage = false;
  249. rgb_image = NULL;
  250. width = 0;
  251. height = 0;
  252. channels = 0;
  253. islocked = false;
  254. }
  255. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  256. {
  257. bpp = _channels;
  258. width = _width;
  259. height = _height;
  260. channels = _channels;
  261. RGBImageLock();
  262. int memsize = width * height * channels;
  263. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  264. stbi_uc* p_source;
  265. for (int x = 0; x < width; ++x)
  266. for (int y = 0; y < height; ++y)
  267. {
  268. p_source = rgb_image + (channels * (y * width + x));
  269. for (int _channels = 0; _channels < channels; ++_channels)
  270. p_source[_channels] = (uint8_t) 0;
  271. }
  272. RGBImageRelease();
  273. }
  274. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  275. {
  276. RGBImageLock();
  277. if (rgb_image)
  278. stbi_image_free(rgb_image);
  279. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
  280. bpp = channels;
  281. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  282. if ((width * height * channels) == 0)
  283. {
  284. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! "
  285. "Check that your camera module is working and connected properly.");
  286. doReboot();
  287. }
  288. RGBImageRelease();
  289. }
  290. CImageBasis::CImageBasis(CImageBasis *_copyfrom)
  291. {
  292. islocked = false;
  293. externalImage = false;
  294. channels = _copyfrom->channels;
  295. width = _copyfrom->width;
  296. height = _copyfrom->height;
  297. bpp = _copyfrom->bpp;
  298. RGBImageLock();
  299. int memsize = width * height * channels;
  300. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  301. if (!rgb_image)
  302. {
  303. ESP_LOGD(TAG, "%s", getESPHeapInfo().c_str());
  304. ESP_LOGD(TAG, "No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize);
  305. RGBImageRelease();
  306. return;
  307. }
  308. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  309. RGBImageRelease();
  310. }
  311. CImageBasis::CImageBasis(int _width, int _height, int _channels)
  312. {
  313. islocked = false;
  314. externalImage = false;
  315. channels = _channels;
  316. width = _width;
  317. height = _height;
  318. bpp = _channels;
  319. int memsize = width * height * channels;
  320. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  321. if (!rgb_image)
  322. {
  323. ESP_LOGD(TAG, "%s", getESPHeapInfo().c_str());
  324. ESP_LOGD(TAG, "No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize);
  325. return;
  326. }
  327. }
  328. CImageBasis::CImageBasis(std::string _image)
  329. {
  330. islocked = false;
  331. channels = 3;
  332. externalImage = false;
  333. filename = _image;
  334. long zwld = esp_get_free_heap_size();
  335. ESP_LOGD(TAG, "freeheapsize before: %ld", zwld);
  336. if (file_size(_image.c_str()) == 0) {
  337. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  338. return;
  339. }
  340. RGBImageLock();
  341. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  342. if (rgb_image == NULL) {
  343. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to load " + _image + "! Is it corrupted?");
  344. RGBImageRelease();
  345. return;
  346. }
  347. RGBImageRelease();
  348. zwld = esp_get_free_heap_size();
  349. ESP_LOGD(TAG, "freeheapsize after: %ld", zwld);
  350. std::string zw = "Image Load failed:" + _image;
  351. if (rgb_image == NULL)
  352. ESP_LOGD(TAG, "%s", zw.c_str());
  353. zw = "CImageBasis after load " + _image + "\n";
  354. ESP_LOGD(TAG, "%s", zw.c_str());
  355. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  356. }
  357. bool CImageBasis::ImageOkay(){
  358. return rgb_image != NULL;
  359. }
  360. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  361. {
  362. islocked = false;
  363. rgb_image = _rgb_image;
  364. channels = _channels;
  365. width = _width;
  366. height = _height;
  367. bpp = _bpp;
  368. externalImage = true;
  369. }
  370. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  371. {
  372. stbi_uc* p_source;
  373. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  374. float intercept = 128 * (1 - contrast);
  375. RGBImageLock();
  376. for (int x = 0; x < width; ++x)
  377. for (int y = 0; y < height; ++y)
  378. {
  379. p_source = rgb_image + (channels * (y * width + x));
  380. for (int _channels = 0; _channels < channels; ++_channels)
  381. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  382. }
  383. RGBImageRelease();
  384. }
  385. CImageBasis::~CImageBasis()
  386. {
  387. RGBImageLock();
  388. if (!externalImage)
  389. stbi_image_free(rgb_image);
  390. }
  391. void CImageBasis::SaveToFile(std::string _imageout)
  392. {
  393. string typ = getFileType(_imageout);
  394. RGBImageLock();
  395. if ((typ == "jpg") || (typ == "JPG")) // CAUTION PROBLEMATIC IN ESP32
  396. {
  397. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  398. }
  399. #ifndef STBI_ONLY_JPEG
  400. if ((typ == "bmp") || (typ == "BMP"))
  401. {
  402. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  403. }
  404. #endif
  405. RGBImageRelease();
  406. }
  407. void CImageBasis::Resize(int _new_dx, int _new_dy)
  408. {
  409. int memsize = _new_dx * _new_dy * channels;
  410. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  411. RGBImageLock();
  412. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  413. stbi_image_free(rgb_image);
  414. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  415. memCopy(odata, rgb_image, memsize);
  416. RGBImageRelease();
  417. width = _new_dx;
  418. height = _new_dy;
  419. stbi_image_free(odata);
  420. }
  421. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  422. {
  423. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  424. {
  425. ESP_LOGD(TAG, "CImageBasis::Resize - Target image size does not fit!");
  426. return;
  427. }
  428. RGBImageLock();
  429. uint8_t* odata = _target->rgb_image;
  430. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  431. RGBImageRelease();
  432. }