CImageBasis.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. #include "CImageBasis.h"
  2. #include "Helper.h"
  3. #include "ClassLogFile.h"
  4. #include "server_ota.h"
  5. #include <esp_log.h>
  6. #include "esp_system.h"
  7. #include <cstring>
  8. #define _USE_MATH_DEFINES
  9. #include <math.h>
  10. #include <algorithm>
  11. #define _ESP32_PSRAM
  12. using namespace std;
  13. static const char *TAG = "C IMG BASIS";
  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. voidstart += _zw->size;
  50. for (int i = 0; i < size; ++i)
  51. *(voidstart + i) = *(datastart + i);
  52. _zw->size += size;
  53. }
  54. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  55. {
  56. ImageData* ii = new ImageData;
  57. RGBImageLock();
  58. stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  59. RGBImageRelease();
  60. return ii;
  61. }
  62. #define HTTP_BUFFER_SENT 1024
  63. struct SendJPGHTTP
  64. {
  65. httpd_req_t *req;
  66. esp_err_t res;
  67. char buf[HTTP_BUFFER_SENT];
  68. int size = 0;
  69. };
  70. inline void writejpgtohttphelp(void *context, void *data, int size)
  71. {
  72. SendJPGHTTP* _send = (SendJPGHTTP*) context;
  73. if ((_send->size + size) >= HTTP_BUFFER_SENT) // data passt nich mehr in buffer
  74. {
  75. httpd_req_t *_req = _send->req;
  76. if (httpd_resp_send_chunk(_req, _send->buf, _send->size) != ESP_OK)
  77. {
  78. ESP_LOGE(TAG, "File sending failed!");
  79. _send->res = ESP_FAIL;
  80. }
  81. _send->size = 0;
  82. }
  83. std::memcpy((void*) (&(_send->buf[0]) + _send->size), data, size);
  84. _send->size+= size;
  85. }
  86. esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
  87. {
  88. SendJPGHTTP ii;
  89. ii.req = _req;
  90. ii.res = ESP_OK;
  91. ii.size = 0;
  92. RGBImageLock();
  93. stbi_write_jpg_to_func(writejpgtohttphelp, &ii, width, height, channels, rgb_image, quality);
  94. RGBImageRelease();
  95. if (ii.size > 0)
  96. {
  97. if (httpd_resp_send_chunk(_req, (char*) ii.buf, ii.size) != ESP_OK) // verschicke noch den Rest
  98. {
  99. ESP_LOGE(TAG, "File sending failed!");
  100. ii.res = ESP_FAIL;
  101. }
  102. }
  103. return ii.res;
  104. }
  105. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  106. {
  107. int gr = height * width * channels;
  108. if (gr != _size) // Größe passt nicht
  109. {
  110. ESP_LOGD(TAG, "Cannot copy image from memory - sizes do not match: should be %d, but is %d", _size, gr);
  111. return false;
  112. }
  113. RGBImageLock();
  114. memCopy(_source, rgb_image, _size);
  115. RGBImageRelease();
  116. return true;
  117. }
  118. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  119. {
  120. stbi_uc* p_source;
  121. p_source = rgb_image + (channels * (y * width + x));
  122. return p_source[ch];
  123. }
  124. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  125. {
  126. #ifdef _ESP32_PSRAM
  127. for (int i = 0; i < _size; ++i)
  128. *(_target + i) = *(_source + i);
  129. #else
  130. memcpy(_target, _source, _size);
  131. #endif
  132. }
  133. bool CImageBasis::isInImage(int x, int y)
  134. {
  135. if ((x < 0) || (x > width - 1))
  136. return false;
  137. if ((y < 0) || (y > height- 1))
  138. return false;
  139. return true;
  140. }
  141. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  142. {
  143. stbi_uc* p_source;
  144. RGBImageLock();
  145. p_source = rgb_image + (channels * (y * width + x));
  146. p_source[0] = r;
  147. if ( channels > 2)
  148. {
  149. p_source[1] = g;
  150. p_source[2] = b;
  151. }
  152. RGBImageRelease();
  153. }
  154. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  155. {
  156. int zwx1, zwx2, zwy1, zwy2;
  157. int _x, _y, _thick;
  158. zwx1 = x - thickness + 1;
  159. zwx2 = x + dx + thickness - 1;
  160. zwy1 = y;
  161. zwy2 = y;
  162. for (_thick = 0; _thick < thickness; _thick++)
  163. for (_x = zwx1; _x <= zwx2; ++_x)
  164. for (_y = zwy1; _y <= zwy2; _y++)
  165. if (isInImage(_x, _y))
  166. setPixelColor(_x, _y - _thick, r, g, b);
  167. zwx1 = x - thickness + 1;
  168. zwx2 = x + dx + thickness - 1;
  169. zwy1 = y + dy;
  170. zwy2 = y + dy;
  171. for (_thick = 0; _thick < thickness; _thick++)
  172. for (_x = zwx1; _x <= zwx2; ++_x)
  173. for (_y = zwy1; _y <= zwy2; _y++)
  174. if (isInImage(_x, _y))
  175. setPixelColor(_x, _y + _thick, r, g, b);
  176. zwx1 = x;
  177. zwx2 = x;
  178. zwy1 = y;
  179. zwy2 = y + dy;
  180. for (_thick = 0; _thick < thickness; _thick++)
  181. for (_x = zwx1; _x <= zwx2; ++_x)
  182. for (_y = zwy1; _y <= zwy2; _y++)
  183. if (isInImage(_x, _y))
  184. setPixelColor(_x - _thick, _y, r, g, b);
  185. zwx1 = x + dx;
  186. zwx2 = x + dx;
  187. zwy1 = y;
  188. zwy2 = y + dy;
  189. for (_thick = 0; _thick < thickness; _thick++)
  190. for (_x = zwx1; _x <= zwx2; ++_x)
  191. for (_y = zwy1; _y <= zwy2; _y++)
  192. if (isInImage(_x, _y))
  193. setPixelColor(_x + _thick, _y, r, g, b);
  194. }
  195. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  196. {
  197. int _x, _y, _thick;
  198. int _zwy1, _zwy2;
  199. thickness = (thickness-1) / 2;
  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. }
  218. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  219. {
  220. float deltarad, aktrad;
  221. int _thick, _x, _y;
  222. int rad = radx;
  223. if (rady > radx)
  224. rad = rady;
  225. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  226. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  227. for (_thick = 0; _thick < thickness; ++_thick)
  228. {
  229. _x = sin(aktrad) * (radx + _thick) + x1;
  230. _y = cos(aktrad) * (rady + _thick) + y1;
  231. if (isInImage(_x, _y))
  232. setPixelColor(_x, _y, r, g, b);
  233. }
  234. }
  235. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  236. {
  237. float deltarad, aktrad;
  238. int _thick, _x, _y;
  239. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  240. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  241. for (_thick = 0; _thick < thickness; ++_thick)
  242. {
  243. _x = sin(aktrad) * (rad + _thick) + x1;
  244. _y = cos(aktrad) * (rad + _thick) + y1;
  245. if (isInImage(_x, _y))
  246. setPixelColor(_x, _y, r, g, b);
  247. }
  248. }
  249. CImageBasis::CImageBasis()
  250. {
  251. externalImage = false;
  252. rgb_image = NULL;
  253. width = 0;
  254. height = 0;
  255. channels = 0;
  256. islocked = false;
  257. }
  258. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  259. {
  260. bpp = _channels;
  261. width = _width;
  262. height = _height;
  263. channels = _channels;
  264. RGBImageLock();
  265. int memsize = width * height * channels;
  266. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  267. stbi_uc* p_source;
  268. for (int x = 0; x < width; ++x)
  269. for (int y = 0; y < height; ++y)
  270. {
  271. p_source = rgb_image + (channels * (y * width + x));
  272. for (int _channels = 0; _channels < channels; ++_channels)
  273. p_source[_channels] = (uint8_t) 0;
  274. }
  275. RGBImageRelease();
  276. }
  277. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  278. {
  279. RGBImageLock();
  280. if (rgb_image)
  281. stbi_image_free(rgb_image);
  282. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
  283. bpp = channels;
  284. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  285. if ((width * height * channels) == 0)
  286. {
  287. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! "
  288. "Check that your camera module is working and connected properly.");
  289. doReboot();
  290. }
  291. RGBImageRelease();
  292. }
  293. CImageBasis::CImageBasis(CImageBasis *_copyfrom, int _anzrepeat)
  294. {
  295. islocked = false;
  296. externalImage = false;
  297. channels = _copyfrom->channels;
  298. width = _copyfrom->width;
  299. height = _copyfrom->height;
  300. bpp = _copyfrom->bpp;
  301. RGBImageLock();
  302. int memsize = width * height * channels;
  303. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  304. int anz = 1;
  305. while (!rgb_image && (anz < _anzrepeat))
  306. {
  307. ESP_LOGD(TAG, "Create Image from Copy - Memory is full - try again: %d", anz);
  308. rgb_image = (unsigned char*) malloc(memsize);
  309. anz++;
  310. }
  311. if (!rgb_image)
  312. {
  313. ESP_LOGD(TAG, "%s", getESPHeapInfo().c_str());
  314. ESP_LOGD(TAG, "No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize);
  315. RGBImageRelease();
  316. return;
  317. }
  318. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  319. RGBImageRelease();
  320. }
  321. CImageBasis::CImageBasis(int _width, int _height, int _channels)
  322. {
  323. islocked = false;
  324. externalImage = false;
  325. channels = _channels;
  326. width = _width;
  327. height = _height;
  328. bpp = _channels;
  329. int memsize = width * height * channels;
  330. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  331. if (!rgb_image)
  332. {
  333. ESP_LOGD(TAG, "%s", getESPHeapInfo().c_str());
  334. ESP_LOGD(TAG, "No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize);
  335. return;
  336. }
  337. }
  338. CImageBasis::CImageBasis(std::string _image)
  339. {
  340. islocked = false;
  341. channels = 3;
  342. externalImage = false;
  343. filename = _image;
  344. long zwld = esp_get_free_heap_size();
  345. ESP_LOGD(TAG, "freeheapsize before: %ld", zwld);
  346. if (file_size(_image.c_str()) == 0) {
  347. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  348. return;
  349. }
  350. RGBImageLock();
  351. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  352. if (rgb_image == NULL) {
  353. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to load " + _image + "! Is it corrupted?");
  354. RGBImageRelease();
  355. return;
  356. }
  357. RGBImageRelease();
  358. zwld = esp_get_free_heap_size();
  359. ESP_LOGD(TAG, "freeheapsize after: %ld", zwld);
  360. std::string zw = "Image Load failed:" + _image;
  361. if (rgb_image == NULL)
  362. ESP_LOGD(TAG, "%s", zw.c_str());
  363. zw = "CImageBasis after load " + _image + "\n";
  364. ESP_LOGD(TAG, "%s", zw.c_str());
  365. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  366. }
  367. bool CImageBasis::ImageOkay(){
  368. return rgb_image != NULL;
  369. }
  370. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  371. {
  372. islocked = false;
  373. rgb_image = _rgb_image;
  374. channels = _channels;
  375. width = _width;
  376. height = _height;
  377. bpp = _bpp;
  378. externalImage = true;
  379. }
  380. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  381. {
  382. stbi_uc* p_source;
  383. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  384. float intercept = 128 * (1 - contrast);
  385. RGBImageLock();
  386. for (int x = 0; x < width; ++x)
  387. for (int y = 0; y < height; ++y)
  388. {
  389. p_source = rgb_image + (channels * (y * width + x));
  390. for (int _channels = 0; _channels < channels; ++_channels)
  391. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  392. }
  393. RGBImageRelease();
  394. }
  395. CImageBasis::~CImageBasis()
  396. {
  397. RGBImageLock();
  398. if (!externalImage)
  399. stbi_image_free(rgb_image);
  400. }
  401. void CImageBasis::SaveToFile(std::string _imageout)
  402. {
  403. string typ = getFileType(_imageout);
  404. RGBImageLock();
  405. if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
  406. {
  407. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  408. }
  409. if ((typ == "bmp") || (typ == "BMP"))
  410. {
  411. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  412. }
  413. RGBImageRelease();
  414. }
  415. void CImageBasis::Resize(int _new_dx, int _new_dy)
  416. {
  417. int memsize = _new_dx * _new_dy * channels;
  418. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  419. RGBImageLock();
  420. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  421. stbi_image_free(rgb_image);
  422. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  423. memCopy(odata, rgb_image, memsize);
  424. RGBImageRelease();
  425. width = _new_dx;
  426. height = _new_dy;
  427. stbi_image_free(odata);
  428. }
  429. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  430. {
  431. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  432. {
  433. ESP_LOGD(TAG, "CImageBasis::Resize - Target image size does not fit!");
  434. return;
  435. }
  436. RGBImageLock();
  437. uint8_t* odata = _target->rgb_image;
  438. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  439. RGBImageRelease();
  440. }