CImageBasis.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "CImageBasis.h"
  2. #include "Helper.h"
  3. #include "ClassLogFile.h"
  4. #include <esp_log.h>
  5. #include "esp_system.h"
  6. #include <cstring>
  7. #define _USE_MATH_DEFINES
  8. #include <math.h>
  9. #include <algorithm>
  10. #define _ESP32_PSRAM
  11. using namespace std;
  12. static const char *TAG = "CImageBasis";
  13. void writejpghelp(void *context, void *data, int size)
  14. {
  15. // printf("Size all: %d, size %d\n", ((ImageData*)context)->size, size);
  16. ImageData* _zw = (ImageData*) context;
  17. uint8_t *voidstart = _zw->data;
  18. uint8_t *datastart = (uint8_t*) data;
  19. voidstart += _zw->size;
  20. for (int i = 0; i < size; ++i)
  21. *(voidstart + i) = *(datastart + i);
  22. _zw->size += size;
  23. }
  24. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  25. {
  26. ImageData* ii = new ImageData;
  27. stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
  28. return ii;
  29. }
  30. #define HTTP_BUFFER_SENT 1024
  31. struct SendJPGHTTP
  32. {
  33. httpd_req_t *req;
  34. esp_err_t res;
  35. char buf[HTTP_BUFFER_SENT];
  36. int size = 0;
  37. };
  38. inline void writejpgtohttphelp(void *context, void *data, int size)
  39. {
  40. SendJPGHTTP* _send = (SendJPGHTTP*) context;
  41. if ((_send->size + size) >= HTTP_BUFFER_SENT) // data passt nich mehr in buffer
  42. {
  43. httpd_req_t *_req = _send->req;
  44. if (httpd_resp_send_chunk(_req, _send->buf, _send->size) != ESP_OK)
  45. {
  46. ESP_LOGE(TAG, "File sending failed!");
  47. _send->res = ESP_FAIL;
  48. }
  49. _send->size = 0;
  50. }
  51. std::memcpy((void*) (&(_send->buf[0]) + _send->size), data, size);
  52. _send->size+= size;
  53. }
  54. esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
  55. {
  56. SendJPGHTTP ii;
  57. ii.req = _req;
  58. ii.res = ESP_OK;
  59. ii.size = 0;
  60. stbi_write_jpg_to_func(writejpgtohttphelp, &ii, width, height, channels, rgb_image, quality);
  61. if (ii.size > 0)
  62. {
  63. if (httpd_resp_send_chunk(_req, (char*) ii.buf, ii.size) != ESP_OK) // verschicke noch den Rest
  64. {
  65. ESP_LOGE(TAG, "File sending failed!");
  66. ii.res = ESP_FAIL;
  67. }
  68. }
  69. return ii.res;
  70. }
  71. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  72. {
  73. int gr = height * width * channels;
  74. if (gr != _size) // Größe passt nicht
  75. {
  76. printf("Kann Bild nicht von Speicher kopierte - Größen passen nicht zusammen: soll %d, ist %d\n", _size, gr);
  77. return false;
  78. }
  79. memCopy(_source, rgb_image, _size);
  80. return true;
  81. }
  82. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  83. {
  84. stbi_uc* p_source;
  85. p_source = rgb_image + (channels * (y * width + x));
  86. return p_source[ch];
  87. }
  88. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  89. {
  90. #ifdef _ESP32_PSRAM
  91. for (int i = 0; i < _size; ++i)
  92. *(_target + i) = *(_source + i);
  93. #else
  94. memcpy(_target, _source, _size);
  95. #endif
  96. }
  97. bool CImageBasis::isInImage(int x, int y)
  98. {
  99. if ((x < 0) || (x > width - 1))
  100. return false;
  101. if ((y < 0) || (y > height- 1))
  102. return false;
  103. return true;
  104. }
  105. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  106. {
  107. stbi_uc* p_source;
  108. p_source = rgb_image + (channels * (y * width + x));
  109. p_source[0] = r;
  110. if ( channels > 2)
  111. {
  112. p_source[1] = g;
  113. p_source[2] = b;
  114. }
  115. }
  116. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  117. {
  118. int zwx1, zwx2, zwy1, zwy2;
  119. int _x, _y, _thick;
  120. zwx1 = x - thickness + 1;
  121. zwx2 = x + dx + thickness - 1;
  122. zwy1 = y;
  123. zwy2 = y;
  124. for (_thick = 0; _thick < thickness; _thick++)
  125. for (_x = zwx1; _x <= zwx2; ++_x)
  126. for (_y = zwy1; _y <= zwy2; _y++)
  127. if (isInImage(_x, _y))
  128. setPixelColor(_x, _y - _thick, r, g, b);
  129. zwx1 = x - thickness + 1;
  130. zwx2 = x + dx + thickness - 1;
  131. zwy1 = y + dy;
  132. zwy2 = y + dy;
  133. for (_thick = 0; _thick < thickness; _thick++)
  134. for (_x = zwx1; _x <= zwx2; ++_x)
  135. for (_y = zwy1; _y <= zwy2; _y++)
  136. if (isInImage(_x, _y))
  137. setPixelColor(_x, _y + _thick, r, g, b);
  138. zwx1 = x;
  139. zwx2 = x;
  140. zwy1 = y;
  141. zwy2 = y + dy;
  142. for (_thick = 0; _thick < thickness; _thick++)
  143. for (_x = zwx1; _x <= zwx2; ++_x)
  144. for (_y = zwy1; _y <= zwy2; _y++)
  145. if (isInImage(_x, _y))
  146. setPixelColor(_x - _thick, _y, r, g, b);
  147. zwx1 = x + dx;
  148. zwx2 = x + dx;
  149. zwy1 = y;
  150. zwy2 = y + dy;
  151. for (_thick = 0; _thick < thickness; _thick++)
  152. for (_x = zwx1; _x <= zwx2; ++_x)
  153. for (_y = zwy1; _y <= zwy2; _y++)
  154. if (isInImage(_x, _y))
  155. setPixelColor(_x + _thick, _y, r, g, b);
  156. }
  157. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  158. {
  159. int _x, _y, _thick;
  160. int _zwy1, _zwy2;
  161. thickness = (thickness-1) / 2;
  162. for (_thick = 0; _thick <= thickness; ++_thick)
  163. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  164. {
  165. if (x2 == x1)
  166. {
  167. _zwy1 = y1;
  168. _zwy2 = y2;
  169. }
  170. else
  171. {
  172. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  173. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  174. }
  175. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  176. if (isInImage(_x, _y))
  177. setPixelColor(_x, _y, r, g, b);
  178. }
  179. }
  180. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  181. {
  182. float deltarad, aktrad;
  183. int _thick, _x, _y;
  184. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  185. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  186. for (_thick = 0; _thick < thickness; ++_thick)
  187. {
  188. _x = sin(aktrad) * (rad + _thick) + x1;
  189. _y = cos(aktrad) * (rad + _thick) + y1;
  190. if (isInImage(_x, _y))
  191. setPixelColor(_x, _y, r, g, b);
  192. }
  193. }
  194. CImageBasis::CImageBasis()
  195. {
  196. externalImage = false;
  197. rgb_image = NULL;
  198. width = 0;
  199. height = 0;
  200. channels = 0;
  201. }
  202. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  203. {
  204. bpp = _channels;
  205. width = _width;
  206. height = _height;
  207. channels = _channels;
  208. int memsize = width * height * channels;
  209. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  210. stbi_uc* p_source;
  211. for (int x = 0; x < width; ++x)
  212. for (int y = 0; y < height; ++y)
  213. {
  214. p_source = rgb_image + (channels * (y * width + x));
  215. for (int _channels = 0; _channels < channels; ++_channels)
  216. p_source[_channels] = (uint8_t) 0;
  217. }
  218. }
  219. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  220. {
  221. if (rgb_image)
  222. stbi_image_free(rgb_image);
  223. rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
  224. bpp = channels;
  225. printf("Image loaded from memory: %d, %d, %d\n", width, height, channels);
  226. }
  227. CImageBasis::CImageBasis(CImageBasis *_copyfrom, int _anzrepeat)
  228. {
  229. externalImage = false;
  230. channels = _copyfrom->channels;
  231. width = _copyfrom->width;
  232. height = _copyfrom->height;
  233. bpp = _copyfrom->bpp;
  234. int memsize = width * height * channels;
  235. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  236. int anz = 1;
  237. TickType_t xDelay;
  238. while (!rgb_image && (anz < _anzrepeat))
  239. {
  240. printf("Create Image from Copy - Speicher ist voll - Versuche es erneut: %d.\n", anz);
  241. xDelay = 1000 / portTICK_PERIOD_MS;
  242. rgb_image = (unsigned char*) malloc(memsize);
  243. anz++;
  244. }
  245. if (!rgb_image)
  246. {
  247. printf(getESPHeapInfo().c_str());
  248. printf("\nKein freier Speicher mehr!!!! Benötigt: %d %d %d %d\n", width, height, channels, memsize);
  249. return;
  250. }
  251. memCopy(_copyfrom->rgb_image, rgb_image, memsize);
  252. }
  253. CImageBasis::CImageBasis(int _width, int _height, int _channels)
  254. {
  255. externalImage = false;
  256. channels = _channels;
  257. width = _width;
  258. height = _height;
  259. bpp = _channels;
  260. int memsize = width * height * channels;
  261. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  262. if (!rgb_image)
  263. {
  264. printf(getESPHeapInfo().c_str());
  265. printf("\nKein freier Speicher mehr!!!! Benötigt: %d %d %d %d\n", width, height, channels, memsize);
  266. return;
  267. }
  268. }
  269. CImageBasis::CImageBasis(std::string _image)
  270. {
  271. channels = 3;
  272. externalImage = false;
  273. filename = _image;
  274. long zwld = esp_get_free_heap_size();
  275. printf("freeheapsize before: %ld\n", zwld);
  276. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  277. zwld = esp_get_free_heap_size();
  278. printf("freeheapsize after : %ld\n", zwld);
  279. std::string zw = "Image Load failed:" + _image + "\n";
  280. if (rgb_image == NULL)
  281. printf(zw.c_str());
  282. zw = "CImageBasis after load " + _image + "\n";
  283. printf(zw.c_str());
  284. printf("w %d, h %d, b %d, c %d\n", width, height, bpp, channels);
  285. }
  286. bool CImageBasis::ImageOkay(){
  287. return rgb_image != NULL;
  288. }
  289. CImageBasis::CImageBasis(uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  290. {
  291. rgb_image = _rgb_image;
  292. channels = _channels;
  293. width = _width;
  294. height = _height;
  295. bpp = _bpp;
  296. externalImage = true;
  297. }
  298. void CImageBasis::Contrast(float _contrast) //input range [-100..100]
  299. {
  300. stbi_uc* p_source;
  301. float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
  302. float intercept = 128 * (1 - contrast);
  303. for (int x = 0; x < width; ++x)
  304. for (int y = 0; y < height; ++y)
  305. {
  306. p_source = rgb_image + (channels * (y * width + x));
  307. for (int _channels = 0; _channels < channels; ++_channels)
  308. p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
  309. }
  310. }
  311. CImageBasis::~CImageBasis()
  312. {
  313. if (!externalImage)
  314. stbi_image_free(rgb_image);
  315. }
  316. void CImageBasis::SaveToFile(std::string _imageout)
  317. {
  318. string typ = getFileType(_imageout);
  319. if ((typ == "jpg") || (typ == "JPG")) // ACHTUNG PROBLEMATISCH IM ESP32
  320. {
  321. stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
  322. }
  323. if ((typ == "bmp") || (typ == "BMP"))
  324. {
  325. stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
  326. }
  327. }
  328. void CImageBasis::Resize(int _new_dx, int _new_dy)
  329. {
  330. int memsize = _new_dx * _new_dy * channels;
  331. uint8_t* odata = (unsigned char*)GET_MEMORY(memsize);
  332. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  333. stbi_image_free(rgb_image);
  334. rgb_image = (unsigned char*)GET_MEMORY(memsize);
  335. memCopy(odata, rgb_image, memsize);
  336. width = _new_dx;
  337. height = _new_dy;
  338. stbi_image_free(odata);
  339. }
  340. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  341. {
  342. if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
  343. {
  344. printf("CImageBasis::Resize - Targetbildgröße passt nicht !!!!!!!!!");
  345. return;
  346. }
  347. uint8_t* odata = _target->rgb_image;
  348. stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
  349. }