CImageBasis.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. #include "CImageBasis.h"
  2. #include "Helper.h"
  3. #include "psram.h"
  4. #include "ClassLogFile.h"
  5. #include "server_ota.h"
  6. #include <esp_log.h>
  7. #include "../../include/defines.h"
  8. #include "esp_system.h"
  9. #include <cstring>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <algorithm>
  13. using namespace std;
  14. static const char *TAG = "C IMG BASIS";
  15. static bool getImageBufferSize(int width, int height, int channels, size_t* size)
  16. {
  17. if (size == nullptr || width <= 0 || height <= 0 || channels <= 0) return false;
  18. const size_t w = static_cast<size_t>(width);
  19. const size_t h = static_cast<size_t>(height);
  20. const size_t c = static_cast<size_t>(channels);
  21. if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return false;
  22. const size_t total = w * h * c;
  23. if (total > static_cast<size_t>(INT_MAX)) return false;
  24. *size = total;
  25. return true;
  26. }
  27. static void releaseOwnedImageBuffer(const std::string& tag, const std::string& name,
  28. uint8_t* image, int size, bool sharedLease)
  29. {
  30. if (image == nullptr) return;
  31. if (sharedLease) psram_free_shared_temp_image_memory();
  32. else if (size == 0) stbi_image_free(image);
  33. else free_psram_heap(tag + " (" + name + ", " + std::to_string(size) + ")", image);
  34. }
  35. //#define DEBUG_DETAIL_ON
  36. uint8_t * CImageBasis::RGBImageLock(int _waitmaxsec)
  37. {
  38. if (islocked)
  39. {
  40. #ifdef DEBUG_DETAIL_ON
  41. ESP_LOGD(TAG, "Image is locked: sleep for: %ds", _waitmaxsec);
  42. #endif
  43. TickType_t xDelay;
  44. xDelay = 1000 / portTICK_PERIOD_MS;
  45. for (int i = 0; i <= _waitmaxsec; ++i)
  46. {
  47. vTaskDelay( xDelay );
  48. if (!islocked)
  49. break;
  50. }
  51. }
  52. if (islocked)
  53. return NULL;
  54. return rgb_image;
  55. }
  56. void CImageBasis::RGBImageRelease()
  57. {
  58. islocked = false;
  59. }
  60. uint8_t * CImageBasis::RGBImageGet()
  61. {
  62. return rgb_image;
  63. }
  64. bool CImageBasis::SetIndepended()
  65. {
  66. size_t size = 0;
  67. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &size)) return false;
  68. externalImage = false;
  69. memsize = static_cast<int>(size);
  70. ownsSharedTempImageLease = false;
  71. return true;
  72. }
  73. void writejpghelp(void *context, void *data, int size)
  74. {
  75. ImageData* output = static_cast<ImageData*>(context);
  76. if (output == nullptr || data == nullptr || size <= 0 || output->size > MAX_JPG_SIZE) return;
  77. if (static_cast<size_t>(size) > (MAX_JPG_SIZE - output->size)) {
  78. output->size = MAX_JPG_SIZE + 1U;
  79. return;
  80. }
  81. std::memcpy(output->data + output->size, data, static_cast<size_t>(size));
  82. output->size += static_cast<size_t>(size);
  83. }
  84. ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
  85. {
  86. ImageData* ii = new (std::nothrow) ImageData;
  87. if (ii == nullptr) {
  88. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: PSRAM allocation failed");
  89. return nullptr;
  90. }
  91. writeToMemoryAsJPG(ii, quality);
  92. return ii;
  93. }
  94. void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality)
  95. {
  96. if (i == nullptr) return;
  97. i->size = 0;
  98. uint8_t* image = RGBImageLock();
  99. if (image == nullptr) {
  100. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: image is unavailable or locked");
  101. return;
  102. }
  103. const int success = stbi_write_jpg_to_func(writejpghelp, i, width, height, channels, image, quality);
  104. RGBImageRelease();
  105. if (i->size > MAX_JPG_SIZE) {
  106. i->size = 0;
  107. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
  108. } else if (!success) {
  109. i->size = 0;
  110. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPEG encoder failed");
  111. }
  112. }
  113. struct SendJPGHTTP
  114. {
  115. httpd_req_t *req;
  116. esp_err_t res;
  117. char *buf;
  118. size_t size = 0;
  119. };
  120. static void flushJPGHTTPBuffer(SendJPGHTTP* send)
  121. {
  122. if (send == nullptr || send->res != ESP_OK || send->size == 0) return;
  123. if (httpd_resp_send_chunk(send->req, send->buf, send->size) != ESP_OK) {
  124. ESP_LOGE(TAG, "File sending failed!");
  125. send->res = ESP_FAIL;
  126. }
  127. send->size = 0;
  128. }
  129. inline void writejpgtohttphelp(void *context, void *data, int size)
  130. {
  131. SendJPGHTTP* send = static_cast<SendJPGHTTP*>(context);
  132. if (send == nullptr || data == nullptr || size <= 0 || send->res != ESP_OK) return;
  133. const uint8_t* source = static_cast<const uint8_t*>(data);
  134. size_t remaining = static_cast<size_t>(size);
  135. while (remaining > 0 && send->res == ESP_OK) {
  136. if (send->size >= HTTP_BUFFER_SENT) {
  137. flushJPGHTTPBuffer(send);
  138. if (send->res != ESP_OK) return;
  139. }
  140. const size_t available = HTTP_BUFFER_SENT - send->size;
  141. const size_t copySize = std::min(available, remaining);
  142. std::memcpy(send->buf + send->size, source, copySize);
  143. send->size += copySize;
  144. source += copySize;
  145. remaining -= copySize;
  146. }
  147. }
  148. esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
  149. {
  150. if (_req == nullptr) return ESP_FAIL;
  151. SendJPGHTTP send;
  152. send.req = _req; send.res = ESP_OK; send.size = 0;
  153. send.buf = static_cast<char*>(malloc_psram_heap("JPEG HTTP stream buffer", HTTP_BUFFER_SENT, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  154. if (send.buf == nullptr) return ESP_ERR_NO_MEM;
  155. uint8_t* image = RGBImageLock();
  156. if (image == nullptr) {
  157. free_psram_heap("JPEG HTTP stream buffer", send.buf);
  158. return ESP_FAIL;
  159. }
  160. const int encoded = stbi_write_jpg_to_func(writejpgtohttphelp, &send, width, height, channels, image, quality);
  161. if (!encoded && send.res == ESP_OK) send.res = ESP_FAIL;
  162. flushJPGHTTPBuffer(&send);
  163. RGBImageRelease();
  164. free_psram_heap("JPEG HTTP stream buffer", send.buf);
  165. return send.res;
  166. }
  167. bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
  168. {
  169. if (_source == nullptr || rgb_image == nullptr || _size < 0 || width <= 0 || height <= 0 || channels <= 0) return false;
  170. const size_t expected = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  171. if (expected > INT_MAX || expected != static_cast<size_t>(_size)) {
  172. ESP_LOGE(TAG, "Cannot copy image from memory - sizes do not match: should be %u, but is %d",
  173. static_cast<unsigned int>(expected), _size);
  174. return false;
  175. }
  176. uint8_t* image = RGBImageLock();
  177. if (image == nullptr) return false;
  178. memCopy(_source, image, _size);
  179. RGBImageRelease();
  180. return true;
  181. }
  182. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  183. {
  184. if (rgb_image == nullptr || !isInImage(x, y) || ch < 0 || ch >= channels) return 0;
  185. const size_t offset = static_cast<size_t>(channels) *
  186. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  187. return rgb_image[offset + static_cast<size_t>(ch)];
  188. }
  189. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  190. {
  191. #ifdef _ESP32_PSRAM
  192. for (int i = 0; i < _size; ++i)
  193. *(_target + i) = *(_source + i);
  194. #else
  195. memcpy(_target, _source, _size);
  196. #endif
  197. }
  198. bool CImageBasis::isInImage(int x, int y)
  199. {
  200. if ((x < 0) || (x > width - 1))
  201. return false;
  202. if ((y < 0) || (y > height- 1))
  203. return false;
  204. return true;
  205. }
  206. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  207. {
  208. if (rgb_image == nullptr || !isInImage(x, y) || channels <= 0) return;
  209. uint8_t* image = RGBImageLock();
  210. if (image == nullptr) return;
  211. const size_t offset = static_cast<size_t>(channels) *
  212. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  213. uint8_t* pixel = image + offset;
  214. pixel[0] = static_cast<uint8_t>(r);
  215. if (channels > 2) { pixel[1] = static_cast<uint8_t>(g); pixel[2] = static_cast<uint8_t>(b); }
  216. RGBImageRelease();
  217. }
  218. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  219. {
  220. int zwx1, zwx2, zwy1, zwy2;
  221. int _x, _y, _thick;
  222. zwx1 = x - thickness + 1;
  223. zwx2 = x + dx + thickness - 1;
  224. zwy1 = y;
  225. zwy2 = y;
  226. RGBImageLock();
  227. for (_thick = 0; _thick < thickness; _thick++)
  228. for (_x = zwx1; _x <= zwx2; ++_x)
  229. for (_y = zwy1; _y <= zwy2; _y++)
  230. if (isInImage(_x, _y))
  231. setPixelColor(_x, _y - _thick, r, g, b);
  232. zwx1 = x - thickness + 1;
  233. zwx2 = x + dx + thickness - 1;
  234. zwy1 = y + dy;
  235. zwy2 = y + dy;
  236. for (_thick = 0; _thick < thickness; _thick++)
  237. for (_x = zwx1; _x <= zwx2; ++_x)
  238. for (_y = zwy1; _y <= zwy2; _y++)
  239. if (isInImage(_x, _y))
  240. setPixelColor(_x, _y + _thick, r, g, b);
  241. zwx1 = x;
  242. zwx2 = x;
  243. zwy1 = y;
  244. zwy2 = y + dy;
  245. for (_thick = 0; _thick < thickness; _thick++)
  246. for (_x = zwx1; _x <= zwx2; ++_x)
  247. for (_y = zwy1; _y <= zwy2; _y++)
  248. if (isInImage(_x, _y))
  249. setPixelColor(_x - _thick, _y, r, g, b);
  250. zwx1 = x + dx;
  251. zwx2 = x + dx;
  252. zwy1 = y;
  253. zwy2 = y + dy;
  254. for (_thick = 0; _thick < thickness; _thick++)
  255. for (_x = zwx1; _x <= zwx2; ++_x)
  256. for (_y = zwy1; _y <= zwy2; _y++)
  257. if (isInImage(_x, _y))
  258. setPixelColor(_x + _thick, _y, r, g, b);
  259. RGBImageRelease();
  260. }
  261. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  262. {
  263. int _x, _y, _thick;
  264. int _zwy1, _zwy2;
  265. thickness = (thickness-1) / 2;
  266. RGBImageLock();
  267. for (_thick = 0; _thick <= thickness; ++_thick)
  268. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  269. {
  270. if (x2 == x1)
  271. {
  272. _zwy1 = y1;
  273. _zwy2 = y2;
  274. }
  275. else
  276. {
  277. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  278. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  279. }
  280. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  281. if (isInImage(_x, _y))
  282. setPixelColor(_x, _y, r, g, b);
  283. }
  284. RGBImageRelease();
  285. }
  286. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  287. {
  288. float deltarad, aktrad;
  289. int _thick, _x, _y;
  290. int rad = radx;
  291. if (rady > radx)
  292. rad = rady;
  293. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  294. RGBImageLock();
  295. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  296. for (_thick = 0; _thick < thickness; ++_thick)
  297. {
  298. _x = sin(aktrad) * (radx + _thick) + x1;
  299. _y = cos(aktrad) * (rady + _thick) + y1;
  300. if (isInImage(_x, _y))
  301. setPixelColor(_x, _y, r, g, b);
  302. }
  303. RGBImageRelease();
  304. }
  305. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  306. {
  307. float deltarad, aktrad;
  308. int _thick, _x, _y;
  309. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  310. RGBImageLock();
  311. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  312. for (_thick = 0; _thick < thickness; ++_thick)
  313. {
  314. _x = sin(aktrad) * (rad + _thick) + x1;
  315. _y = cos(aktrad) * (rad + _thick) + y1;
  316. if (isInImage(_x, _y))
  317. setPixelColor(_x, _y, r, g, b);
  318. }
  319. RGBImageRelease();
  320. }
  321. CImageBasis::CImageBasis(string _name)
  322. {
  323. name = _name;
  324. externalImage = false;
  325. rgb_image = NULL;
  326. width = 0;
  327. height = 0;
  328. channels = 0;
  329. islocked = false;
  330. }
  331. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  332. {
  333. if (_width <= 0 || _height <= 0 || _channels <= 0 || name == "tmpImage") return;
  334. const size_t w = static_cast<size_t>(_width), h = static_cast<size_t>(_height), c = static_cast<size_t>(_channels);
  335. if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return;
  336. const size_t newSize = w * h * c;
  337. if (newSize > INT_MAX) return;
  338. uint8_t* newImage = static_cast<uint8_t*>(malloc_psram_heap(
  339. std::string(TAG) + "->CImageBasis (" + name + ")", newSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  340. if (newImage == nullptr) {
  341. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: allocation failed");
  342. LogFile.WriteHeapInfo("CreateEmptyImage");
  343. return;
  344. }
  345. if (rgb_image != nullptr && !externalImage) {
  346. if (memsize == 0) {
  347. stbi_image_free(rgb_image);
  348. } else {
  349. free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + std::to_string(memsize) + ")", rgb_image);
  350. }
  351. }
  352. rgb_image = newImage;
  353. width = _width; height = _height; channels = _channels; bpp = _channels;
  354. memsize = static_cast<int>(newSize);
  355. externalImage = false;
  356. std::memset(rgb_image, 0, newSize);
  357. }
  358. void CImageBasis::EmptyImage()
  359. {
  360. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  361. uint8_t* image = RGBImageLock();
  362. if (image == nullptr) return;
  363. const size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  364. std::memset(image, 0, size);
  365. RGBImageRelease();
  366. }
  367. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  368. {
  369. if (_buffer == NULL || len <= 0) {
  370. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "LoadFromMemory: invalid input buffer");
  371. return;
  372. }
  373. int newWidth = 0, newHeight = 0, sourceChannels = 0;
  374. uint8_t* decoded = stbi_load_from_memory(_buffer, len, &newWidth, &newHeight,
  375. &sourceChannels, STBI_rgb);
  376. size_t decodedSize = 0;
  377. if (decoded == NULL || !getImageBufferSize(newWidth, newHeight, STBI_rgb, &decodedSize)) {
  378. if (decoded != NULL) stbi_image_free(decoded);
  379. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "LoadFromMemory: image decode failed or returned invalid dimensions");
  380. LogFile.WriteHeapInfo("LoadFromMemory");
  381. return;
  382. }
  383. if (rgb_image != NULL && !externalImage) {
  384. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis", name,
  385. rgb_image, memsize, ownsSharedTempImageLease);
  386. }
  387. rgb_image = decoded;
  388. width = newWidth;
  389. height = newHeight;
  390. channels = STBI_rgb;
  391. bpp = sourceChannels;
  392. memsize = 0;
  393. externalImage = false;
  394. ownsSharedTempImageLease = false;
  395. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  396. }
  397. void CImageBasis::crop_image(unsigned short cropLeft, unsigned short cropRight, unsigned short cropTop, unsigned short cropBottom)
  398. {
  399. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0 ||
  400. static_cast<unsigned int>(cropLeft) + cropRight >= static_cast<unsigned int>(width) ||
  401. static_cast<unsigned int>(cropTop) + cropBottom >= static_cast<unsigned int>(height)) return;
  402. const unsigned int top = static_cast<unsigned int>(cropTop) * static_cast<unsigned int>(width) * static_cast<unsigned int>(channels);
  403. const unsigned int bottom = (static_cast<unsigned int>(width) * static_cast<unsigned int>(height) -
  404. static_cast<unsigned int>(cropBottom) * static_cast<unsigned int>(width)) * static_cast<unsigned int>(channels);
  405. const unsigned int maxX = static_cast<unsigned int>(width) - cropRight;
  406. const unsigned int newWidth = static_cast<unsigned int>(width) - cropLeft - cropRight;
  407. const unsigned int newHeight = static_cast<unsigned int>(height) - cropTop - cropBottom;
  408. size_t writeIndex = 0;
  409. const size_t inputSize = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  410. for (size_t i = 0; i < inputSize; i += static_cast<size_t>(channels)) {
  411. const unsigned int x = static_cast<unsigned int>((i / static_cast<size_t>(channels)) % static_cast<size_t>(width));
  412. if (i < top || i >= bottom || x < cropLeft || x >= maxX) continue;
  413. for (int channel = 0; channel < channels; ++channel) rgb_image[writeIndex++] = rgb_image[i + static_cast<size_t>(channel)];
  414. }
  415. width = static_cast<int>(newWidth);
  416. height = static_cast<int>(newHeight);
  417. }
  418. CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
  419. {
  420. name = _name;
  421. islocked = false;
  422. externalImage = false;
  423. rgb_image = NULL;
  424. width = height = channels = bpp = 0;
  425. memsize = 0;
  426. ownsSharedTempImageLease = false;
  427. if (_copyfrom == NULL || !_copyfrom->ImageOkay()) {
  428. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: source image is not available");
  429. return;
  430. }
  431. size_t imageSize = 0;
  432. if (!getImageBufferSize(_copyfrom->width, _copyfrom->height, _copyfrom->channels, &imageSize)) {
  433. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: invalid source dimensions");
  434. return;
  435. }
  436. width = _copyfrom->width;
  437. height = _copyfrom->height;
  438. channels = _copyfrom->channels;
  439. bpp = _copyfrom->bpp;
  440. #ifdef DEBUG_DETAIL_ON
  441. LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
  442. #endif
  443. uint8_t* source = _copyfrom->RGBImageLock();
  444. if (source == NULL) {
  445. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: source image is locked");
  446. return;
  447. }
  448. if (name == "tmpImage") {
  449. rgb_image = static_cast<uint8_t*>(psram_reserve_shared_tmp_image_memory());
  450. ownsSharedTempImageLease = (rgb_image != NULL);
  451. memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
  452. } else {
  453. rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
  454. std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,
  455. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  456. memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
  457. }
  458. if (rgb_image == NULL) {
  459. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: PSRAM allocation failed (" + std::to_string(imageSize) + " bytes)");
  460. LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
  461. _copyfrom->RGBImageRelease();
  462. return;
  463. }
  464. memCopy(source, rgb_image, static_cast<int>(imageSize));
  465. _copyfrom->RGBImageRelease();
  466. #ifdef DEBUG_DETAIL_ON
  467. LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
  468. #endif
  469. }
  470. CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
  471. {
  472. name = _name;
  473. islocked = false;
  474. externalImage = false;
  475. ownsSharedTempImageLease = false;
  476. rgb_image = NULL;
  477. width = height = channels = bpp = 0;
  478. memsize = 0;
  479. size_t imageSize = 0;
  480. if (!getImageBufferSize(_width, _height, _channels, &imageSize)) {
  481. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: invalid image dimensions");
  482. return;
  483. }
  484. #ifdef DEBUG_DETAIL_ON
  485. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
  486. #endif
  487. rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
  488. std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,
  489. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  490. if (rgb_image == NULL) {
  491. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: PSRAM allocation failed (" + std::to_string(imageSize) + " bytes)");
  492. LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
  493. return;
  494. }
  495. width = _width;
  496. height = _height;
  497. channels = _channels;
  498. bpp = _channels;
  499. memsize = static_cast<int>(imageSize);
  500. #ifdef DEBUG_DETAIL_ON
  501. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
  502. #endif
  503. }
  504. CImageBasis::CImageBasis(string _name, std::string _image)
  505. {
  506. name = _name;
  507. islocked = false;
  508. channels = 3;
  509. externalImage = false;
  510. filename = _image;
  511. if (file_size(_image.c_str()) == 0) {
  512. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  513. return;
  514. }
  515. RGBImageLock();
  516. #ifdef DEBUG_DETAIL_ON
  517. LogFile.WriteHeapInfo("CImageBasis_image - Start");
  518. #endif
  519. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  520. if (rgb_image == NULL) {
  521. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
  522. LogFile.WriteHeapInfo("CImageBasis-image");
  523. RGBImageRelease();
  524. return;
  525. }
  526. RGBImageRelease();
  527. #ifdef DEBUG_DETAIL_ON
  528. std::string zw = "CImageBasis after load " + _image;
  529. ESP_LOGD(TAG, "%s", zw.c_str());
  530. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  531. #endif
  532. #ifdef DEBUG_DETAIL_ON
  533. LogFile.WriteHeapInfo("CImageBasis_image - done");
  534. #endif
  535. }
  536. bool CImageBasis::ImageOkay(){
  537. return rgb_image != NULL;
  538. }
  539. CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  540. {
  541. name = _name;
  542. islocked = false;
  543. rgb_image = _rgb_image;
  544. channels = _channels;
  545. width = _width;
  546. height = _height;
  547. bpp = _bpp;
  548. externalImage = true;
  549. }
  550. void CImageBasis::Negative(void)
  551. {
  552. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  553. uint8_t* image = RGBImageLock();
  554. if (image == nullptr) return;
  555. const size_t size = static_cast<size_t>(width) * static_cast<size_t>(height) * static_cast<size_t>(channels);
  556. for (size_t i = 0; i < size; i += static_cast<size_t>(channels))
  557. for (int c = 0; c < channels; ++c) image[i + static_cast<size_t>(c)] = 255 - image[i + static_cast<size_t>(c)];
  558. RGBImageRelease();
  559. }
  560. void CImageBasis::Contrast(float _contrast)
  561. {
  562. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  563. uint8_t* image = RGBImageLock();
  564. if (image == nullptr) return;
  565. const float contrast = (_contrast / 100) + 1;
  566. const float intercept = 128 * (1 - contrast);
  567. for (int y = 0; y < height; ++y) {
  568. for (int x = 0; x < width; ++x) {
  569. const size_t offset = static_cast<size_t>(channels) *
  570. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  571. uint8_t* pixel = image + offset;
  572. for (int channel = 0; channel < channels; ++channel)
  573. pixel[channel] = static_cast<uint8_t>(std::min(255, std::max(0,
  574. static_cast<int>(pixel[channel] * contrast + intercept))));
  575. }
  576. }
  577. RGBImageRelease();
  578. }
  579. CImageBasis::~CImageBasis()
  580. {
  581. if (!externalImage && rgb_image != NULL) {
  582. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis", name,
  583. rgb_image, memsize, ownsSharedTempImageLease);
  584. }
  585. rgb_image = NULL;
  586. ownsSharedTempImageLease = false;
  587. }
  588. void CImageBasis::SaveToFile(std::string _imageout)
  589. {
  590. if (rgb_image == nullptr || width <= 0 || height <= 0 || channels <= 0) return;
  591. const string typ = getFileType(_imageout);
  592. uint8_t* image = RGBImageLock();
  593. if (image == nullptr) return;
  594. if ((typ == "jpg") || (typ == "JPG")) stbi_write_jpg(_imageout.c_str(), width, height, channels, image, 0);
  595. #ifndef STBI_ONLY_JPEG
  596. if ((typ == "bmp") || (typ == "BMP")) stbi_write_bmp(_imageout.c_str(), width, height, channels, image);
  597. #endif
  598. RGBImageRelease();
  599. }
  600. void CImageBasis::Resize(int _new_dx, int _new_dy)
  601. {
  602. if (rgb_image == NULL || name == "tmpImage") {
  603. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: not possible for image " + name);
  604. return;
  605. }
  606. size_t newSize = 0;
  607. if (!getImageBufferSize(_new_dx, _new_dy, channels, &newSize)) {
  608. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: invalid target dimensions");
  609. return;
  610. }
  611. uint8_t* source = RGBImageLock();
  612. if (source == NULL) return;
  613. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  614. std::string(TAG) + "->CImageBasis Resize (" + name + ")", newSize,
  615. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  616. if (output == NULL) {
  617. RGBImageRelease();
  618. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: PSRAM allocation failed: " + std::to_string(newSize));
  619. return;
  620. }
  621. const int resized = stbir_resize_uint8(source, width, height, 0,
  622. output, _new_dx, _new_dy, 0, channels);
  623. RGBImageRelease();
  624. if (resized == 0) {
  625. free_psram_heap(std::string(TAG) + "->CImageBasis Resize (failed)", output);
  626. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: stb resize failed");
  627. return;
  628. }
  629. if (!externalImage) {
  630. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis Resize old", name,
  631. rgb_image, memsize, ownsSharedTempImageLease);
  632. }
  633. rgb_image = output;
  634. memsize = static_cast<int>(newSize);
  635. externalImage = false;
  636. ownsSharedTempImageLease = false;
  637. width = _new_dx;
  638. height = _new_dy;
  639. }
  640. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  641. {
  642. if (_target == NULL || rgb_image == NULL || !_target->ImageOkay() ||
  643. _target->height != _new_dy || _target->width != _new_dx ||
  644. _target->channels != channels) {
  645. ESP_LOGE(TAG, "Resize - Target image size does not fit or is unavailable!");
  646. return;
  647. }
  648. size_t sourceSize = 0, targetSize = 0;
  649. if (!getImageBufferSize(width, height, channels, &sourceSize) ||
  650. !getImageBufferSize(_new_dx, _new_dy, channels, &targetSize)) {
  651. ESP_LOGE(TAG, "Resize - Invalid source or target dimensions");
  652. return;
  653. }
  654. uint8_t* source = RGBImageLock();
  655. if (source == NULL) return;
  656. uint8_t* target = _target->RGBImageLock();
  657. if (target == NULL) {
  658. RGBImageRelease();
  659. return;
  660. }
  661. const int resized = stbir_resize_uint8(source, width, height, 0,
  662. target, _new_dx, _new_dy, 0, channels);
  663. _target->RGBImageRelease();
  664. RGBImageRelease();
  665. if (resized == 0) ESP_LOGE(TAG, "Resize - stb resize failed");
  666. }