CImageBasis.cpp 27 KB

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