CImageBasis.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. size_t expected = 0;
  170. if (_source == nullptr || rgb_image == nullptr || _size < 0 ||
  171. !getImageBufferSize(width, height, channels, &expected) ||
  172. expected != static_cast<size_t>(_size)) {
  173. ESP_LOGE(TAG, "Cannot copy image from memory - sizes do not match: should be %u, but is %d",
  174. static_cast<unsigned int>(expected), _size);
  175. return false;
  176. }
  177. uint8_t* image = RGBImageLock();
  178. if (image == nullptr) return false;
  179. memCopy(_source, image, _size);
  180. RGBImageRelease();
  181. return true;
  182. }
  183. uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
  184. {
  185. size_t imageSize = 0;
  186. if (rgb_image == nullptr || !isInImage(x, y) || ch < 0 || ch >= channels ||
  187. !getImageBufferSize(width, height, channels, &imageSize)) return 0;
  188. const size_t offset = static_cast<size_t>(channels) *
  189. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  190. if (offset > imageSize || static_cast<size_t>(ch) >= imageSize - offset) return 0;
  191. return rgb_image[offset + static_cast<size_t>(ch)];
  192. }
  193. void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
  194. {
  195. #ifdef _ESP32_PSRAM
  196. for (int i = 0; i < _size; ++i)
  197. *(_target + i) = *(_source + i);
  198. #else
  199. memcpy(_target, _source, _size);
  200. #endif
  201. }
  202. bool CImageBasis::isInImage(int x, int y)
  203. {
  204. if ((x < 0) || (x > width - 1))
  205. return false;
  206. if ((y < 0) || (y > height- 1))
  207. return false;
  208. return true;
  209. }
  210. void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
  211. {
  212. size_t imageSize = 0;
  213. if (rgb_image == nullptr || !isInImage(x, y) ||
  214. !getImageBufferSize(width, height, channels, &imageSize)) return;
  215. uint8_t* image = RGBImageLock();
  216. if (image == nullptr) return;
  217. const size_t offset = static_cast<size_t>(channels) *
  218. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  219. if (offset >= imageSize) { RGBImageRelease(); return; }
  220. uint8_t* pixel = image + offset;
  221. pixel[0] = static_cast<uint8_t>(r);
  222. if (channels > 2) { pixel[1] = static_cast<uint8_t>(g); pixel[2] = static_cast<uint8_t>(b); }
  223. RGBImageRelease();
  224. }
  225. void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
  226. {
  227. int zwx1, zwx2, zwy1, zwy2;
  228. int _x, _y, _thick;
  229. zwx1 = x - thickness + 1;
  230. zwx2 = x + dx + thickness - 1;
  231. zwy1 = y;
  232. zwy2 = y;
  233. RGBImageLock();
  234. for (_thick = 0; _thick < thickness; _thick++)
  235. for (_x = zwx1; _x <= zwx2; ++_x)
  236. for (_y = zwy1; _y <= zwy2; _y++)
  237. if (isInImage(_x, _y))
  238. setPixelColor(_x, _y - _thick, r, g, b);
  239. zwx1 = x - thickness + 1;
  240. zwx2 = x + dx + thickness - 1;
  241. zwy1 = y + dy;
  242. zwy2 = y + dy;
  243. for (_thick = 0; _thick < thickness; _thick++)
  244. for (_x = zwx1; _x <= zwx2; ++_x)
  245. for (_y = zwy1; _y <= zwy2; _y++)
  246. if (isInImage(_x, _y))
  247. setPixelColor(_x, _y + _thick, r, g, b);
  248. zwx1 = x;
  249. zwx2 = x;
  250. zwy1 = y;
  251. zwy2 = y + dy;
  252. for (_thick = 0; _thick < thickness; _thick++)
  253. for (_x = zwx1; _x <= zwx2; ++_x)
  254. for (_y = zwy1; _y <= zwy2; _y++)
  255. if (isInImage(_x, _y))
  256. setPixelColor(_x - _thick, _y, r, g, b);
  257. zwx1 = x + dx;
  258. zwx2 = x + dx;
  259. zwy1 = y;
  260. zwy2 = y + dy;
  261. for (_thick = 0; _thick < thickness; _thick++)
  262. for (_x = zwx1; _x <= zwx2; ++_x)
  263. for (_y = zwy1; _y <= zwy2; _y++)
  264. if (isInImage(_x, _y))
  265. setPixelColor(_x + _thick, _y, r, g, b);
  266. RGBImageRelease();
  267. }
  268. void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
  269. {
  270. int _x, _y, _thick;
  271. int _zwy1, _zwy2;
  272. thickness = (thickness-1) / 2;
  273. RGBImageLock();
  274. for (_thick = 0; _thick <= thickness; ++_thick)
  275. for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
  276. {
  277. if (x2 == x1)
  278. {
  279. _zwy1 = y1;
  280. _zwy2 = y2;
  281. }
  282. else
  283. {
  284. _zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
  285. _zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
  286. }
  287. for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
  288. if (isInImage(_x, _y))
  289. setPixelColor(_x, _y, r, g, b);
  290. }
  291. RGBImageRelease();
  292. }
  293. void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
  294. {
  295. float deltarad, aktrad;
  296. int _thick, _x, _y;
  297. int rad = radx;
  298. if (rady > radx)
  299. rad = rady;
  300. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  301. RGBImageLock();
  302. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  303. for (_thick = 0; _thick < thickness; ++_thick)
  304. {
  305. _x = sin(aktrad) * (radx + _thick) + x1;
  306. _y = cos(aktrad) * (rady + _thick) + y1;
  307. if (isInImage(_x, _y))
  308. setPixelColor(_x, _y, r, g, b);
  309. }
  310. RGBImageRelease();
  311. }
  312. void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
  313. {
  314. float deltarad, aktrad;
  315. int _thick, _x, _y;
  316. deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
  317. RGBImageLock();
  318. for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
  319. for (_thick = 0; _thick < thickness; ++_thick)
  320. {
  321. _x = sin(aktrad) * (rad + _thick) + x1;
  322. _y = cos(aktrad) * (rad + _thick) + y1;
  323. if (isInImage(_x, _y))
  324. setPixelColor(_x, _y, r, g, b);
  325. }
  326. RGBImageRelease();
  327. }
  328. CImageBasis::CImageBasis(string _name)
  329. {
  330. name = _name;
  331. externalImage = false;
  332. rgb_image = NULL;
  333. width = 0;
  334. height = 0;
  335. channels = 0;
  336. islocked = false;
  337. }
  338. void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
  339. {
  340. if (_width <= 0 || _height <= 0 || _channels <= 0 || name == "tmpImage") return;
  341. const size_t w = static_cast<size_t>(_width), h = static_cast<size_t>(_height), c = static_cast<size_t>(_channels);
  342. if (w > SIZE_MAX / h || w * h > SIZE_MAX / c) return;
  343. const size_t newSize = w * h * c;
  344. if (newSize > INT_MAX) return;
  345. uint8_t* newImage = static_cast<uint8_t*>(malloc_psram_heap(
  346. std::string(TAG) + "->CImageBasis (" + name + ")", newSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  347. if (newImage == nullptr) {
  348. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: allocation failed");
  349. LogFile.WriteHeapInfo("CreateEmptyImage");
  350. return;
  351. }
  352. if (rgb_image != nullptr && !externalImage) {
  353. if (memsize == 0) {
  354. stbi_image_free(rgb_image);
  355. } else {
  356. free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + std::to_string(memsize) + ")", rgb_image);
  357. }
  358. }
  359. rgb_image = newImage;
  360. width = _width; height = _height; channels = _channels; bpp = _channels;
  361. memsize = static_cast<int>(newSize);
  362. externalImage = false;
  363. std::memset(rgb_image, 0, newSize);
  364. }
  365. void CImageBasis::EmptyImage()
  366. {
  367. size_t size = 0;
  368. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &size)) return;
  369. uint8_t* image = RGBImageLock();
  370. if (image == nullptr) return;
  371. std::memset(image, 0, size);
  372. RGBImageRelease();
  373. }
  374. void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
  375. {
  376. if (_buffer == NULL || len <= 0) {
  377. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "LoadFromMemory: invalid input buffer");
  378. return;
  379. }
  380. int newWidth = 0, newHeight = 0, sourceChannels = 0;
  381. uint8_t* decoded = stbi_load_from_memory(_buffer, len, &newWidth, &newHeight,
  382. &sourceChannels, STBI_rgb);
  383. size_t decodedSize = 0;
  384. if (decoded == NULL || !getImageBufferSize(newWidth, newHeight, STBI_rgb, &decodedSize)) {
  385. if (decoded != NULL) stbi_image_free(decoded);
  386. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "LoadFromMemory: image decode failed or returned invalid dimensions");
  387. LogFile.WriteHeapInfo("LoadFromMemory");
  388. return;
  389. }
  390. if (rgb_image != NULL && !externalImage) {
  391. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis", name,
  392. rgb_image, memsize, ownsSharedTempImageLease);
  393. }
  394. rgb_image = decoded;
  395. width = newWidth;
  396. height = newHeight;
  397. channels = STBI_rgb;
  398. bpp = sourceChannels;
  399. memsize = 0;
  400. externalImage = false;
  401. ownsSharedTempImageLease = false;
  402. ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
  403. }
  404. void CImageBasis::crop_image(unsigned short cropLeft, unsigned short cropRight, unsigned short cropTop, unsigned short cropBottom)
  405. {
  406. size_t inputSize = 0;
  407. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &inputSize) ||
  408. static_cast<unsigned int>(cropLeft) + cropRight >= static_cast<unsigned int>(width) ||
  409. static_cast<unsigned int>(cropTop) + cropBottom >= static_cast<unsigned int>(height)) return;
  410. const size_t rowBytes = static_cast<size_t>(width) * static_cast<size_t>(channels);
  411. const size_t firstRow = static_cast<size_t>(cropTop) * rowBytes;
  412. const size_t endRow = static_cast<size_t>(height - cropBottom) * rowBytes;
  413. const unsigned int maxX = static_cast<unsigned int>(width) - cropRight;
  414. const unsigned int newWidth = static_cast<unsigned int>(width) - cropLeft - cropRight;
  415. const unsigned int newHeight = static_cast<unsigned int>(height) - cropTop - cropBottom;
  416. size_t writeIndex = 0;
  417. for (size_t i = 0; i < inputSize; i += static_cast<size_t>(channels)) {
  418. const unsigned int x = static_cast<unsigned int>((i / static_cast<size_t>(channels)) % static_cast<size_t>(width));
  419. if (i < firstRow || i >= endRow || x < cropLeft || x >= maxX) continue;
  420. for (int channel = 0; channel < channels; ++channel) {
  421. if (i + static_cast<size_t>(channel) >= inputSize) return;
  422. rgb_image[writeIndex++] = rgb_image[i + static_cast<size_t>(channel)];
  423. }
  424. }
  425. width = static_cast<int>(newWidth);
  426. height = static_cast<int>(newHeight);
  427. }
  428. CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
  429. {
  430. name = _name;
  431. islocked = false;
  432. externalImage = false;
  433. rgb_image = NULL;
  434. width = height = channels = bpp = 0;
  435. memsize = 0;
  436. ownsSharedTempImageLease = false;
  437. if (_copyfrom == NULL || !_copyfrom->ImageOkay()) {
  438. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: source image is not available");
  439. return;
  440. }
  441. size_t imageSize = 0;
  442. if (!getImageBufferSize(_copyfrom->width, _copyfrom->height, _copyfrom->channels, &imageSize)) {
  443. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: invalid source dimensions");
  444. return;
  445. }
  446. width = _copyfrom->width;
  447. height = _copyfrom->height;
  448. channels = _copyfrom->channels;
  449. bpp = _copyfrom->bpp;
  450. #ifdef DEBUG_DETAIL_ON
  451. LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
  452. #endif
  453. uint8_t* source = _copyfrom->RGBImageLock();
  454. if (source == NULL) {
  455. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: source image is locked");
  456. return;
  457. }
  458. if (name == "tmpImage") {
  459. rgb_image = static_cast<uint8_t*>(psram_reserve_shared_tmp_image_memory());
  460. ownsSharedTempImageLease = (rgb_image != NULL);
  461. memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
  462. } else {
  463. rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
  464. std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,
  465. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  466. memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
  467. }
  468. if (rgb_image == NULL) {
  469. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: PSRAM allocation failed (" + std::to_string(imageSize) + " bytes)");
  470. LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
  471. _copyfrom->RGBImageRelease();
  472. return;
  473. }
  474. memCopy(source, rgb_image, static_cast<int>(imageSize));
  475. _copyfrom->RGBImageRelease();
  476. #ifdef DEBUG_DETAIL_ON
  477. LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
  478. #endif
  479. }
  480. CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
  481. {
  482. name = _name;
  483. islocked = false;
  484. externalImage = false;
  485. ownsSharedTempImageLease = false;
  486. rgb_image = NULL;
  487. width = height = channels = bpp = 0;
  488. memsize = 0;
  489. size_t imageSize = 0;
  490. if (!getImageBufferSize(_width, _height, _channels, &imageSize)) {
  491. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: invalid image dimensions");
  492. return;
  493. }
  494. #ifdef DEBUG_DETAIL_ON
  495. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
  496. #endif
  497. rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
  498. std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,
  499. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  500. if (rgb_image == NULL) {
  501. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: PSRAM allocation failed (" + std::to_string(imageSize) + " bytes)");
  502. LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
  503. return;
  504. }
  505. width = _width;
  506. height = _height;
  507. channels = _channels;
  508. bpp = _channels;
  509. memsize = static_cast<int>(imageSize);
  510. #ifdef DEBUG_DETAIL_ON
  511. LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
  512. #endif
  513. }
  514. CImageBasis::CImageBasis(string _name, std::string _image)
  515. {
  516. name = _name;
  517. islocked = false;
  518. channels = 3;
  519. externalImage = false;
  520. filename = _image;
  521. if (file_size(_image.c_str()) == 0) {
  522. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
  523. return;
  524. }
  525. RGBImageLock();
  526. #ifdef DEBUG_DETAIL_ON
  527. LogFile.WriteHeapInfo("CImageBasis_image - Start");
  528. #endif
  529. rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
  530. if (rgb_image == NULL) {
  531. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
  532. LogFile.WriteHeapInfo("CImageBasis-image");
  533. RGBImageRelease();
  534. return;
  535. }
  536. RGBImageRelease();
  537. #ifdef DEBUG_DETAIL_ON
  538. std::string zw = "CImageBasis after load " + _image;
  539. ESP_LOGD(TAG, "%s", zw.c_str());
  540. ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
  541. #endif
  542. #ifdef DEBUG_DETAIL_ON
  543. LogFile.WriteHeapInfo("CImageBasis_image - done");
  544. #endif
  545. }
  546. bool CImageBasis::ImageOkay(){
  547. size_t imageSize = 0;
  548. return rgb_image != NULL && getImageBufferSize(width, height, channels, &imageSize);
  549. }
  550. CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
  551. {
  552. name = _name;
  553. islocked = false;
  554. rgb_image = _rgb_image;
  555. channels = _channels;
  556. width = _width;
  557. height = _height;
  558. bpp = _bpp;
  559. externalImage = true;
  560. }
  561. void CImageBasis::Negative(void)
  562. {
  563. size_t size = 0;
  564. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &size)) return;
  565. uint8_t* image = RGBImageLock();
  566. if (image == nullptr) return;
  567. for (size_t i = 0; i < size; i += static_cast<size_t>(channels))
  568. for (int c = 0; c < channels; ++c) image[i + static_cast<size_t>(c)] = 255 - image[i + static_cast<size_t>(c)];
  569. RGBImageRelease();
  570. }
  571. void CImageBasis::Contrast(float _contrast)
  572. {
  573. size_t imageSize = 0;
  574. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &imageSize)) return;
  575. uint8_t* image = RGBImageLock();
  576. if (image == nullptr) return;
  577. const float contrast = (_contrast / 100) + 1;
  578. const float intercept = 128 * (1 - contrast);
  579. for (int y = 0; y < height; ++y) {
  580. for (int x = 0; x < width; ++x) {
  581. const size_t offset = static_cast<size_t>(channels) *
  582. (static_cast<size_t>(y) * static_cast<size_t>(width) + static_cast<size_t>(x));
  583. uint8_t* pixel = image + offset;
  584. for (int channel = 0; channel < channels; ++channel)
  585. pixel[channel] = static_cast<uint8_t>(std::min(255, std::max(0,
  586. static_cast<int>(pixel[channel] * contrast + intercept))));
  587. }
  588. }
  589. RGBImageRelease();
  590. }
  591. CImageBasis::~CImageBasis()
  592. {
  593. if (!externalImage && rgb_image != NULL) {
  594. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis", name,
  595. rgb_image, memsize, ownsSharedTempImageLease);
  596. }
  597. rgb_image = NULL;
  598. ownsSharedTempImageLease = false;
  599. }
  600. void CImageBasis::SaveToFile(std::string _imageout)
  601. {
  602. size_t imageSize = 0;
  603. if (rgb_image == nullptr || !getImageBufferSize(width, height, channels, &imageSize)) return;
  604. const string typ = getFileType(_imageout);
  605. uint8_t* image = RGBImageLock();
  606. if (image == nullptr) return;
  607. if ((typ == "jpg") || (typ == "JPG")) stbi_write_jpg(_imageout.c_str(), width, height, channels, image, 0);
  608. #ifndef STBI_ONLY_JPEG
  609. if ((typ == "bmp") || (typ == "BMP")) stbi_write_bmp(_imageout.c_str(), width, height, channels, image);
  610. #endif
  611. RGBImageRelease();
  612. }
  613. void CImageBasis::Resize(int _new_dx, int _new_dy)
  614. {
  615. if (rgb_image == NULL || name == "tmpImage") {
  616. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: not possible for image " + name);
  617. return;
  618. }
  619. size_t newSize = 0;
  620. if (!getImageBufferSize(_new_dx, _new_dy, channels, &newSize)) {
  621. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: invalid target dimensions");
  622. return;
  623. }
  624. uint8_t* source = RGBImageLock();
  625. if (source == NULL) return;
  626. uint8_t* output = static_cast<uint8_t*>(malloc_psram_heap(
  627. std::string(TAG) + "->CImageBasis Resize (" + name + ")", newSize,
  628. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  629. if (output == NULL) {
  630. RGBImageRelease();
  631. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: PSRAM allocation failed: " + std::to_string(newSize));
  632. return;
  633. }
  634. const int resized = stbir_resize_uint8(source, width, height, 0,
  635. output, _new_dx, _new_dy, 0, channels);
  636. RGBImageRelease();
  637. if (resized == 0) {
  638. free_psram_heap(std::string(TAG) + "->CImageBasis Resize (failed)", output);
  639. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Resize: stb resize failed");
  640. return;
  641. }
  642. if (!externalImage) {
  643. releaseOwnedImageBuffer(std::string(TAG) + "->CImageBasis Resize old", name,
  644. rgb_image, memsize, ownsSharedTempImageLease);
  645. }
  646. rgb_image = output;
  647. memsize = static_cast<int>(newSize);
  648. externalImage = false;
  649. ownsSharedTempImageLease = false;
  650. width = _new_dx;
  651. height = _new_dy;
  652. }
  653. void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
  654. {
  655. if (_target == NULL || rgb_image == NULL || !_target->ImageOkay() ||
  656. _target->height != _new_dy || _target->width != _new_dx ||
  657. _target->channels != channels) {
  658. ESP_LOGE(TAG, "Resize - Target image size does not fit or is unavailable!");
  659. return;
  660. }
  661. size_t sourceSize = 0, targetSize = 0;
  662. if (!getImageBufferSize(width, height, channels, &sourceSize) ||
  663. !getImageBufferSize(_new_dx, _new_dy, channels, &targetSize)) {
  664. ESP_LOGE(TAG, "Resize - Invalid source or target dimensions");
  665. return;
  666. }
  667. uint8_t* source = RGBImageLock();
  668. if (source == NULL) return;
  669. uint8_t* target = _target->RGBImageLock();
  670. if (target == NULL) {
  671. RGBImageRelease();
  672. return;
  673. }
  674. const int resized = stbir_resize_uint8(source, width, height, 0,
  675. target, _new_dx, _new_dy, 0, channels);
  676. _target->RGBImageRelease();
  677. RGBImageRelease();
  678. if (resized == 0) ESP_LOGE(TAG, "Resize - stb resize failed");
  679. }