mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-08 20:46:52 +03:00
* Testcase for #2145 and debug-log (#2151) * new models ana-cont-11.0.5, ana-class100-1.5.7, dig-class100-1.6.0 * Testcase for #2145 Added debug log, if allowNegativeRates is handeled * Fix timezone config parser (#2169) * make sure to parse the whole config line * fix crash on empty timezone parameter --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * Enhance ROI pages (#2161) * Check if the ROIs are equidistant. Only if not, untick the checkbox * renaming * Check if the ROIs have same y, dy and dx. If so, tick the sync checkbox * only allow editing space when box is checked * fix sync check * show inner frame on all ROIs * cleanup * Check if the ROIs have same dy and dx. If so, tick the sync checkbox * checkbox position * renaming * renaming * show inner frame and cross hairs on all ROIs * update ROIs on ticking checkboxes * show timezone hint * fix deleting last ROI * cleanup --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * restart timeout on progress, catch error (#2170) * restart timeout on progress, catch error * . --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * BugFix #2167 * Release 15.1 preparations (#2171) * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update changelog * Fix links to PR * Formating * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md * Update Changelog.md --------- Co-authored-by: Slider0007 <jobbelle@gmx.net> Co-authored-by: Slider0007 <115730895+Slider0007@users.noreply.github.com> * fix typo * Replace relative documentation links with absolute ones pointing to the external documentation (#2180) Co-authored-by: CaCO3 <caco@ruinelli.ch> * Sort model files in configuration combobox (#2189) * new models ana-cont-11.0.5, ana-class100-1.5.7, dig-class100-1.6.0 * Testcase for #2145 Added debug log, if allowNegativeRates is handeled * Sort model files in combobox * reboot task - increase stack size (#2201) Avoid stack overflow * Update interface_influxdb.cpp * Update Changelog.md * Show PSRAM usage (#2206) * centralize PSRAM usage (application code only) * update logging * update logging * fix use after free * initialize buffer * free rgb_image before ussing it for new allocation * use wrapper function * switch log level to debug * . * undo adding free() calls * . * add names to all CImage instances * . * . * . * revert changes of stbi_image_free() with free_psram_heap() on the places where is is not in PSRAM * . * typos * typo * Added MQTT Outbox explanation/warning * added CONFIG_SPIRAM_USE_MEMMAP explanation --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * Disable custom MQTT Outbox. This also moves the MQTT Publishing memory usage back to the internal RAM! * log MQTT connection refused reasons (#2216) * Revert PSRAM usage as it lead to memory fragmentation. See https://github.com/jomjol/AI-on-the-edge-device/issues/2200 for details * fix missing value data * Revert PSRAM usage as it lead to memory fragmentation. (#2224) See https://github.com/jomjol/AI-on-the-edge-device/issues/2200 for details Co-authored-by: CaCO3 <caco@ruinelli.ch> * Fix missing value data in graph (#2230) * fix missing value data --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * Update Changelog.md (#2231) * Update interface_influxdb.cpp (#2233) * update copyright year * Cleanup * Set prevalue using MQTT + set prevalue to RAW value (REST+MQTT) (#2252) * Use double instead of float * Error handling + set to RAW if newvalue < 0 * REST SetPrevalue: Set to RAW if newvalue < 0 * set prevalue with MQTT * removed the stb_image files and re-add them as a submodule. (#2223) - stb_image.h: Version update 2.25 -> 2.28 - stb_resize.h: Version update 0.96 -> 0.97 - stb_write.h: Version update 1.14 -> 1.16 Co-authored-by: CaCO3 <caco@ruinelli.ch> * Remove obsolete ClassFlowWriteList (#2264) * Renaming & cleanup of some modules / functions in source code (#2265) * Rename module tag name * Rename server_tflite.cpp -> MainFlowControl.cpp * Remove redundandant MQTTMainTopic function * Update * Remove obsolete GetMQTTMainTopic * Fix last element missing in digit model drop down (#2282) * Debug influxdb (#2283) * Fix time offset issues in InfluxDB component. (#2278) Closes #2273 Closes #2150 * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Improve Logging * Implement TimeSync at beginning * Update time_sntp.cpp * Update time_sntp.cpp * Set Time After WLAN Init --------- Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu> * Implement a camera livestream handler (#2286) * fix leading NaN (#2310) * analogROI: Activate save button after ROI creation (#2326) * Migration of PlatformIO 5.2.0 to 6.1.0 (resp. ESP IDF from 4.4.2 to 5.0.1) (#2305) * Migration to PlatformIO 6.1.0 * Disable RMTMEM usage as it is no longer allowed -> Smart LEDs not functional! * moved miniz into subfolder of jomjol_fileserver_ota, else it does not build anymore. * cleanup * fix leading NaN (#2310) * Migration to PlatformIO 6.1.0 * Disable RMTMEM usage as it is no longer allowed -> Smart LEDs not functional! * moved miniz into subfolder of jomjol_fileserver_ota, else it does not build anymore. * cleanup * Task watchdog has new config name * Fix return value check. It must be something else than ESP_FAIL, but it does not need to be ESP_OK! * add missing strucures to work around new RMTMEM restriction (untested) --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * Keep MainFlowTask alive to handle reboot (#2325) * Shared PSRAM memory (#2285) * enable PSRAM logging * add extra functions for psram shared memroy handling * CImageBasis objects still should used dynamic memory (eg. rawImage), haw ever tmpImage must be placed inside the shared memory * Place all STBI allocs inside the shared memory * The models are placed in the shared PSRAM reagion and must be allocated through the dedicated functions * . * renaming * fix cast warning * add flag to switch STBI PSRAM usage * improve PSRAM shared handling * reserve shared PSRAM as early as possible * init logging eralier so we can use it in PSRAM shared alloc * move Wifi_LWIP, BSS_SEG and MQTT Outbox into PSRAM to ffree internal memory * Check if model fits into reserved shared memory * Update code/components/jomjol_tfliteclass/CTfLiteClass.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_flowcontroll/ClassFlowControll.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_image_proc/CImageBasis.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * . * . * . * . * Korrektur Merge Conflict in main.cpp --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> Co-authored-by: jomjol <30766535+jomjol@users.noreply.github.com> * fix PSRAM init return value check * Extend InfluxDBv1 with individual topic names (#2319) * Implement individual influx topic * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Update FieldName * analogROI: Activate save button after ROI creation (#2326) * Migration of PlatformIO 5.2.0 to 6.1.0 (resp. ESP IDF from 4.4.2 to 5.0.1) (#2305) * Migration to PlatformIO 6.1.0 * Disable RMTMEM usage as it is no longer allowed -> Smart LEDs not functional! * moved miniz into subfolder of jomjol_fileserver_ota, else it does not build anymore. * cleanup * fix leading NaN (#2310) * Migration to PlatformIO 6.1.0 * Disable RMTMEM usage as it is no longer allowed -> Smart LEDs not functional! * moved miniz into subfolder of jomjol_fileserver_ota, else it does not build anymore. * cleanup * Task watchdog has new config name * Fix return value check. It must be something else than ESP_FAIL, but it does not need to be ESP_OK! * add missing strucures to work around new RMTMEM restriction (untested) --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> * Keep MainFlowTask alive to handle reboot (#2325) * Shared PSRAM memory (#2285) * enable PSRAM logging * add extra functions for psram shared memroy handling * CImageBasis objects still should used dynamic memory (eg. rawImage), haw ever tmpImage must be placed inside the shared memory * Place all STBI allocs inside the shared memory * The models are placed in the shared PSRAM reagion and must be allocated through the dedicated functions * . * renaming * fix cast warning * add flag to switch STBI PSRAM usage * improve PSRAM shared handling * reserve shared PSRAM as early as possible * init logging eralier so we can use it in PSRAM shared alloc * move Wifi_LWIP, BSS_SEG and MQTT Outbox into PSRAM to ffree internal memory * Check if model fits into reserved shared memory * Update code/components/jomjol_tfliteclass/CTfLiteClass.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_flowcontroll/ClassFlowControll.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_image_proc/CImageBasis.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * Update code/components/jomjol_helper/psram.cpp * . * . * . * . * Korrektur Merge Conflict in main.cpp --------- Co-authored-by: CaCO3 <caco@ruinelli.ch> Co-authored-by: jomjol <30766535+jomjol@users.noreply.github.com> * fix PSRAM init return value check * Extend incl. indiv. Measurement * Implement UX * Update ClassFlowInfluxDBv2.cpp * Implement individual influx topic * Update interface_influxdb.cpp * Update interface_influxdb.cpp * Update FieldName * Extend incl. indiv. Measurement * Implement UX * Update ClassFlowInfluxDBv2.cpp * Update main.cpp --------- Co-authored-by: Slider0007 <115730895+Slider0007@users.noreply.github.com> Co-authored-by: CaCO3 <caco3@ruinelli.ch> Co-authored-by: CaCO3 <caco@ruinelli.ch> * Update Changelog.md (#2344) * Update Changelog.md * Update Changelog.md * merge conflicts * merge conflicts * update changelog --------- Co-authored-by: Frank Haverland <fspapaping@googlemail.com> Co-authored-by: CaCO3 <caco@ruinelli.ch> Co-authored-by: jomjol <30766535+jomjol@users.noreply.github.com> Co-authored-by: Slider0007 <jobbelle@gmx.net> Co-authored-by: Slider0007 <115730895+Slider0007@users.noreply.github.com> Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
704 lines
17 KiB
C++
704 lines
17 KiB
C++
#include "CImageBasis.h"
|
|
#include "Helper.h"
|
|
#include "psram.h"
|
|
#include "ClassLogFile.h"
|
|
#include "server_ota.h"
|
|
|
|
#include <esp_log.h>
|
|
#include "../../include/defines.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <math.h>
|
|
#include <algorithm>
|
|
|
|
|
|
using namespace std;
|
|
|
|
static const char *TAG = "C IMG BASIS";
|
|
|
|
bool jpgFileTooLarge = false; // JPG creation verfication
|
|
|
|
|
|
//#define DEBUG_DETAIL_ON
|
|
|
|
|
|
uint8_t * CImageBasis::RGBImageLock(int _waitmaxsec)
|
|
{
|
|
if (islocked)
|
|
{
|
|
#ifdef DEBUG_DETAIL_ON
|
|
ESP_LOGD(TAG, "Image is locked: sleep for: %ds", _waitmaxsec);
|
|
#endif
|
|
TickType_t xDelay;
|
|
xDelay = 1000 / portTICK_PERIOD_MS;
|
|
for (int i = 0; i <= _waitmaxsec; ++i)
|
|
{
|
|
vTaskDelay( xDelay );
|
|
if (!islocked)
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (islocked)
|
|
return NULL;
|
|
|
|
return rgb_image;
|
|
}
|
|
|
|
|
|
void CImageBasis::RGBImageRelease()
|
|
{
|
|
islocked = false;
|
|
}
|
|
|
|
|
|
uint8_t * CImageBasis::RGBImageGet()
|
|
{
|
|
return rgb_image;
|
|
}
|
|
|
|
|
|
void writejpghelp(void *context, void *data, int size)
|
|
{
|
|
// ESP_LOGD(TAG, "Size all: %d, size %d", ((ImageData*)context)->size, size);
|
|
ImageData* _zw = (ImageData*) context;
|
|
uint8_t *voidstart = _zw->data;
|
|
uint8_t *datastart = (uint8_t*) data;
|
|
|
|
if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy to prevent buffer overflow
|
|
voidstart += _zw->size;
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
*(voidstart + i) = *(datastart + i);
|
|
|
|
_zw->size += size;
|
|
}
|
|
else {
|
|
jpgFileTooLarge = true;
|
|
}
|
|
}
|
|
|
|
|
|
ImageData* CImageBasis::writeToMemoryAsJPG(const int quality)
|
|
{
|
|
ImageData* ii = new ImageData;
|
|
|
|
RGBImageLock();
|
|
stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
|
|
RGBImageRelease();
|
|
|
|
if (jpgFileTooLarge) {
|
|
jpgFileTooLarge = false;
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
|
|
}
|
|
return ii;
|
|
}
|
|
|
|
|
|
void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality)
|
|
{
|
|
ImageData* ii = new ImageData;
|
|
|
|
RGBImageLock();
|
|
stbi_write_jpg_to_func(writejpghelp, ii, width, height, channels, rgb_image, quality);
|
|
RGBImageRelease();
|
|
|
|
if (jpgFileTooLarge) {
|
|
jpgFileTooLarge = false;
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE));
|
|
}
|
|
memCopy((uint8_t*) ii, (uint8_t*) i, sizeof(ImageData));
|
|
delete ii;
|
|
}
|
|
|
|
|
|
struct SendJPGHTTP
|
|
{
|
|
httpd_req_t *req;
|
|
esp_err_t res;
|
|
char buf[HTTP_BUFFER_SENT];
|
|
int size = 0;
|
|
};
|
|
|
|
|
|
inline void writejpgtohttphelp(void *context, void *data, int size)
|
|
{
|
|
SendJPGHTTP* _send = (SendJPGHTTP*) context;
|
|
if ((_send->size + size) >= HTTP_BUFFER_SENT) // data no longer fits in buffer
|
|
{
|
|
if (httpd_resp_send_chunk(_send->req, _send->buf, _send->size) != ESP_OK)
|
|
{
|
|
ESP_LOGE(TAG, "File sending failed!");
|
|
_send->res = ESP_FAIL;
|
|
}
|
|
_send->size = 0;
|
|
}
|
|
std::memcpy((void*) (&(_send->buf[0]) + _send->size), data, size);
|
|
_send->size+= size;
|
|
}
|
|
|
|
|
|
esp_err_t CImageBasis::SendJPGtoHTTP(httpd_req_t *_req, const int quality)
|
|
{
|
|
SendJPGHTTP ii;
|
|
ii.req = _req;
|
|
ii.res = ESP_OK;
|
|
ii.size = 0;
|
|
|
|
RGBImageLock();
|
|
stbi_write_jpg_to_func(writejpgtohttphelp, &ii, width, height, channels, rgb_image, quality);
|
|
|
|
if (ii.size > 0)
|
|
{
|
|
if (httpd_resp_send_chunk(_req, (char*) ii.buf, ii.size) != ESP_OK) //still send the rest
|
|
{
|
|
ESP_LOGE(TAG, "File sending failed!");
|
|
ii.res = ESP_FAIL;
|
|
}
|
|
}
|
|
|
|
RGBImageRelease();
|
|
|
|
return ii.res;
|
|
}
|
|
|
|
|
|
bool CImageBasis::CopyFromMemory(uint8_t* _source, int _size)
|
|
{
|
|
int gr = height * width * channels;
|
|
if (gr != _size) // Size does not fit
|
|
{
|
|
ESP_LOGE(TAG, "Cannot copy image from memory - sizes do not match: should be %d, but is %d", _size, gr);
|
|
return false;
|
|
}
|
|
|
|
RGBImageLock();
|
|
memCopy(_source, rgb_image, _size);
|
|
RGBImageRelease();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
uint8_t CImageBasis::GetPixelColor(int x, int y, int ch)
|
|
{
|
|
stbi_uc* p_source;
|
|
p_source = rgb_image + (channels * (y * width + x));
|
|
return p_source[ch];
|
|
}
|
|
|
|
|
|
void CImageBasis::memCopy(uint8_t* _source, uint8_t* _target, int _size)
|
|
{
|
|
#ifdef _ESP32_PSRAM
|
|
for (int i = 0; i < _size; ++i)
|
|
*(_target + i) = *(_source + i);
|
|
#else
|
|
memcpy(_target, _source, _size);
|
|
#endif
|
|
}
|
|
|
|
|
|
bool CImageBasis::isInImage(int x, int y)
|
|
{
|
|
if ((x < 0) || (x > width - 1))
|
|
return false;
|
|
|
|
if ((y < 0) || (y > height- 1))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
void CImageBasis::setPixelColor(int x, int y, int r, int g, int b)
|
|
{
|
|
stbi_uc* p_source;
|
|
|
|
RGBImageLock();
|
|
p_source = rgb_image + (channels * (y * width + x));
|
|
p_source[0] = r;
|
|
if ( channels > 2)
|
|
{
|
|
p_source[1] = g;
|
|
p_source[2] = b;
|
|
}
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::drawRect(int x, int y, int dx, int dy, int r, int g, int b, int thickness)
|
|
{
|
|
int zwx1, zwx2, zwy1, zwy2;
|
|
int _x, _y, _thick;
|
|
|
|
zwx1 = x - thickness + 1;
|
|
zwx2 = x + dx + thickness - 1;
|
|
zwy1 = y;
|
|
zwy2 = y;
|
|
|
|
RGBImageLock();
|
|
|
|
for (_thick = 0; _thick < thickness; _thick++)
|
|
for (_x = zwx1; _x <= zwx2; ++_x)
|
|
for (_y = zwy1; _y <= zwy2; _y++)
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x, _y - _thick, r, g, b);
|
|
|
|
zwx1 = x - thickness + 1;
|
|
zwx2 = x + dx + thickness - 1;
|
|
zwy1 = y + dy;
|
|
zwy2 = y + dy;
|
|
for (_thick = 0; _thick < thickness; _thick++)
|
|
for (_x = zwx1; _x <= zwx2; ++_x)
|
|
for (_y = zwy1; _y <= zwy2; _y++)
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x, _y + _thick, r, g, b);
|
|
|
|
zwx1 = x;
|
|
zwx2 = x;
|
|
zwy1 = y;
|
|
zwy2 = y + dy;
|
|
for (_thick = 0; _thick < thickness; _thick++)
|
|
for (_x = zwx1; _x <= zwx2; ++_x)
|
|
for (_y = zwy1; _y <= zwy2; _y++)
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x - _thick, _y, r, g, b);
|
|
|
|
zwx1 = x + dx;
|
|
zwx2 = x + dx;
|
|
zwy1 = y;
|
|
zwy2 = y + dy;
|
|
for (_thick = 0; _thick < thickness; _thick++)
|
|
for (_x = zwx1; _x <= zwx2; ++_x)
|
|
for (_y = zwy1; _y <= zwy2; _y++)
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x + _thick, _y, r, g, b);
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::drawLine(int x1, int y1, int x2, int y2, int r, int g, int b, int thickness)
|
|
{
|
|
int _x, _y, _thick;
|
|
int _zwy1, _zwy2;
|
|
thickness = (thickness-1) / 2;
|
|
|
|
RGBImageLock();
|
|
|
|
for (_thick = 0; _thick <= thickness; ++_thick)
|
|
for (_x = x1 - _thick; _x <= x2 + _thick; ++_x)
|
|
{
|
|
if (x2 == x1)
|
|
{
|
|
_zwy1 = y1;
|
|
_zwy2 = y2;
|
|
}
|
|
else
|
|
{
|
|
_zwy1 = (y2 - y1) * (float)(_x - x1) / (float)(x2 - x1) + y1;
|
|
_zwy2 = (y2 - y1) * (float)(_x + 1 - x1) / (float)(x2 - x1) + y1;
|
|
}
|
|
|
|
for (_y = _zwy1 - _thick; _y <= _zwy2 + _thick; _y++)
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x, _y, r, g, b);
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::drawEllipse(int x1, int y1, int radx, int rady, int r, int g, int b, int thickness)
|
|
{
|
|
float deltarad, aktrad;
|
|
int _thick, _x, _y;
|
|
int rad = radx;
|
|
|
|
if (rady > radx)
|
|
rad = rady;
|
|
|
|
deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
|
|
|
|
RGBImageLock();
|
|
|
|
for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
|
|
for (_thick = 0; _thick < thickness; ++_thick)
|
|
{
|
|
_x = sin(aktrad) * (radx + _thick) + x1;
|
|
_y = cos(aktrad) * (rady + _thick) + y1;
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x, _y, r, g, b);
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::drawCircle(int x1, int y1, int rad, int r, int g, int b, int thickness)
|
|
{
|
|
float deltarad, aktrad;
|
|
int _thick, _x, _y;
|
|
|
|
deltarad = 1 / (4 * M_PI * (rad + thickness - 1));
|
|
|
|
RGBImageLock();
|
|
|
|
for (aktrad = 0; aktrad <= (2 * M_PI); aktrad += deltarad)
|
|
for (_thick = 0; _thick < thickness; ++_thick)
|
|
{
|
|
_x = sin(aktrad) * (rad + _thick) + x1;
|
|
_y = cos(aktrad) * (rad + _thick) + y1;
|
|
if (isInImage(_x, _y))
|
|
setPixelColor(_x, _y, r, g, b);
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
CImageBasis::CImageBasis(string _name)
|
|
{
|
|
name = _name;
|
|
externalImage = false;
|
|
rgb_image = NULL;
|
|
width = 0;
|
|
height = 0;
|
|
channels = 0;
|
|
islocked = false;
|
|
}
|
|
|
|
|
|
void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
|
|
{
|
|
bpp = _channels;
|
|
width = _width;
|
|
height = _height;
|
|
channels = _channels;
|
|
|
|
RGBImageLock();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CreateEmptyImage");
|
|
#endif
|
|
|
|
memsize = width * height * channels;
|
|
|
|
rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
|
|
|
|
if (rgb_image == NULL)
|
|
{
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize));
|
|
LogFile.WriteHeapInfo("CreateEmptyImage");
|
|
RGBImageRelease();
|
|
return;
|
|
}
|
|
|
|
stbi_uc* p_source;
|
|
|
|
for (int x = 0; x < width; ++x)
|
|
for (int y = 0; y < height; ++y)
|
|
{
|
|
p_source = rgb_image + (channels * (y * width + x));
|
|
for (int _channels = 0; _channels < channels; ++_channels)
|
|
p_source[_channels] = (uint8_t) 0;
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::EmptyImage()
|
|
{
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("EmptyImage");
|
|
#endif
|
|
|
|
stbi_uc* p_source;
|
|
|
|
RGBImageLock();
|
|
|
|
for (int x = 0; x < width; ++x)
|
|
for (int y = 0; y < height; ++y)
|
|
{
|
|
p_source = rgb_image + (channels * (y * width + x));
|
|
for (int _channels = 0; _channels < channels; ++_channels)
|
|
p_source[_channels] = (uint8_t) 0;
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
|
|
{
|
|
RGBImageLock();
|
|
|
|
if (rgb_image != NULL) {
|
|
stbi_image_free(rgb_image);
|
|
//free_psram_heap(std::string(TAG) + "->rgb_image (LoadFromMemory)", rgb_image);
|
|
}
|
|
|
|
rgb_image = stbi_load_from_memory(_buffer, len, &width, &height, &channels, 3);
|
|
bpp = channels;
|
|
ESP_LOGD(TAG, "Image loaded from memory: %d, %d, %d", width, height, channels);
|
|
|
|
if ((width * height * channels) == 0)
|
|
{
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Image with size 0 loaded --> reboot to be done! "
|
|
"Check that your camera module is working and connected properly.");
|
|
LogFile.WriteHeapInfo("LoadFromMemory");
|
|
|
|
doReboot();
|
|
}
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
|
|
{
|
|
name = _name;
|
|
islocked = false;
|
|
externalImage = false;
|
|
channels = _copyfrom->channels;
|
|
width = _copyfrom->width;
|
|
height = _copyfrom->height;
|
|
bpp = _copyfrom->bpp;
|
|
|
|
RGBImageLock();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start");
|
|
#endif
|
|
|
|
memsize = width * height * channels;
|
|
|
|
|
|
if (name == "tmpImage") {
|
|
rgb_image = (unsigned char*)psram_reserve_shared_tmp_image_memory();
|
|
}
|
|
else {
|
|
rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
|
|
}
|
|
|
|
if (rgb_image == NULL)
|
|
{
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize));
|
|
LogFile.WriteHeapInfo("CImageBasis-Copyfrom");
|
|
RGBImageRelease();
|
|
return;
|
|
}
|
|
|
|
memCopy(_copyfrom->rgb_image, rgb_image, memsize);
|
|
RGBImageRelease();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_copyfrom - done");
|
|
#endif
|
|
}
|
|
|
|
|
|
CImageBasis::CImageBasis(string _name, int _width, int _height, int _channels)
|
|
{
|
|
name = _name;
|
|
islocked = false;
|
|
externalImage = false;
|
|
channels = _channels;
|
|
width = _width;
|
|
height = _height;
|
|
bpp = _channels;
|
|
|
|
RGBImageLock();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start");
|
|
#endif
|
|
|
|
memsize = width * height * channels;
|
|
|
|
rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
|
|
|
|
if (rgb_image == NULL)
|
|
{
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize));
|
|
LogFile.WriteHeapInfo("CImageBasis-width,height,ch");
|
|
RGBImageRelease();
|
|
return;
|
|
}
|
|
|
|
RGBImageRelease();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done");
|
|
#endif
|
|
}
|
|
|
|
|
|
CImageBasis::CImageBasis(string _name, std::string _image)
|
|
{
|
|
name = _name;
|
|
islocked = false;
|
|
channels = 3;
|
|
externalImage = false;
|
|
filename = _image;
|
|
|
|
if (file_size(_image.c_str()) == 0) {
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, _image + " is empty!");
|
|
return;
|
|
}
|
|
|
|
RGBImageLock();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_image - Start");
|
|
#endif
|
|
|
|
rgb_image = stbi_load(_image.c_str(), &width, &height, &bpp, channels);
|
|
|
|
if (rgb_image == NULL) {
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?");
|
|
LogFile.WriteHeapInfo("CImageBasis-image");
|
|
RGBImageRelease();
|
|
return;
|
|
}
|
|
|
|
RGBImageRelease();
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
std::string zw = "CImageBasis after load " + _image;
|
|
ESP_LOGD(TAG, "%s", zw.c_str());
|
|
ESP_LOGD(TAG, "w %d, h %d, b %d, c %d", width, height, bpp, channels);
|
|
#endif
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
LogFile.WriteHeapInfo("CImageBasis_image - done");
|
|
#endif
|
|
}
|
|
|
|
|
|
bool CImageBasis::ImageOkay(){
|
|
return rgb_image != NULL;
|
|
}
|
|
|
|
|
|
CImageBasis::CImageBasis(string _name, uint8_t* _rgb_image, int _channels, int _width, int _height, int _bpp)
|
|
{
|
|
name = _name;
|
|
islocked = false;
|
|
rgb_image = _rgb_image;
|
|
channels = _channels;
|
|
width = _width;
|
|
height = _height;
|
|
bpp = _bpp;
|
|
externalImage = true;
|
|
}
|
|
|
|
|
|
void CImageBasis::Contrast(float _contrast) //input range [-100..100]
|
|
{
|
|
stbi_uc* p_source;
|
|
|
|
float contrast = (_contrast/100) + 1; //convert to decimal & shift range: [0..2]
|
|
float intercept = 128 * (1 - contrast);
|
|
|
|
RGBImageLock();
|
|
|
|
|
|
for (int x = 0; x < width; ++x)
|
|
for (int y = 0; y < height; ++y)
|
|
{
|
|
p_source = rgb_image + (channels * (y * width + x));
|
|
for (int _channels = 0; _channels < channels; ++_channels)
|
|
p_source[_channels] = (uint8_t) std::min(255, std::max(0, (int) (p_source[_channels] * contrast + intercept)));
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
CImageBasis::~CImageBasis()
|
|
{
|
|
RGBImageLock();
|
|
|
|
|
|
if (!externalImage) {
|
|
if (name == "tmpImage") { // This image should be placed in the shared part of PSRAM
|
|
psram_free_shared_temp_image_memory();
|
|
}
|
|
else { // All other images are much smaller and can go into the normal PSRAM region
|
|
//stbi_image_free(rgb_image);
|
|
if (memsize == 0) {
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Not freeing (" + name + " as there was never PSRAM allocated for it)");
|
|
}
|
|
else {
|
|
free_psram_heap(std::string(TAG) + "->CImageBasis (" + name + ", " + to_string(memsize) + ")", rgb_image);
|
|
}
|
|
}
|
|
}
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::SaveToFile(std::string _imageout)
|
|
{
|
|
string typ = getFileType(_imageout);
|
|
|
|
RGBImageLock();
|
|
|
|
if ((typ == "jpg") || (typ == "JPG")) // CAUTION PROBLEMATIC IN ESP32
|
|
{
|
|
stbi_write_jpg(_imageout.c_str(), width, height, channels, rgb_image, 0);
|
|
}
|
|
|
|
#ifndef STBI_ONLY_JPEG
|
|
if ((typ == "bmp") || (typ == "BMP"))
|
|
{
|
|
stbi_write_bmp(_imageout.c_str(), width, height, channels, rgb_image);
|
|
}
|
|
#endif
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::Resize(int _new_dx, int _new_dy)
|
|
{
|
|
memsize = _new_dx * _new_dy * channels;
|
|
uint8_t* odata = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->odata", memsize, MALLOC_CAP_SPIRAM);
|
|
|
|
RGBImageLock();
|
|
|
|
stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
|
|
|
|
rgb_image = (unsigned char*)malloc_psram_heap(std::string(TAG) + "->CImageBasis Resize (" + name + ")", memsize, MALLOC_CAP_SPIRAM);
|
|
memCopy(odata, rgb_image, memsize);
|
|
width = _new_dx;
|
|
height = _new_dy;
|
|
|
|
free_psram_heap(std::string(TAG) + "->odata", odata);
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|
|
|
|
void CImageBasis::Resize(int _new_dx, int _new_dy, CImageBasis *_target)
|
|
{
|
|
if ((_target->height != _new_dy) || (_target->width != _new_dx) || (_target->channels != channels))
|
|
{
|
|
ESP_LOGE(TAG, "Resize - Target image size does not fit!");
|
|
return;
|
|
}
|
|
|
|
RGBImageLock();
|
|
|
|
uint8_t* odata = _target->rgb_image;
|
|
stbir_resize_uint8(rgb_image, width, height, 0, odata, _new_dx, _new_dy, 0, channels);
|
|
|
|
RGBImageRelease();
|
|
}
|
|
|