From a851fd393c837f38dd6625a89aa555851b90e728 Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Wed, 1 Feb 2023 19:49:48 +0100 Subject: [PATCH 1/6] Increase max JPG size (128k->160k) --- code/include/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/include/defines.h b/code/include/defines.h index c5a13d1d..432bc79a 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -130,7 +130,7 @@ //CImageBasis #define HTTP_BUFFER_SENT 1024 #define GET_MEMORY(X) heap_caps_malloc(X, MALLOC_CAP_SPIRAM) - #define MAX_JPG_SIZE 128000 + #define MAX_JPG_SIZE 160000 //CAlignAndCutImage + CImageBasis From d40d5ba0f6021938b7a3e04837cc3502e6adfab9 Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Wed, 1 Feb 2023 20:12:11 +0100 Subject: [PATCH 2/6] Disable USE_HIMEM_IF_AVAILABLE --- code/include/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/include/defines.h b/code/include/defines.h index 432bc79a..98db2bf7 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -21,7 +21,7 @@ // use himem //https://github.com/jomjol/AI-on-the-edge-device/issues/1842 - #define USE_HIMEM_IF_AVAILABLE + //#define USE_HIMEM_IF_AVAILABLE /* Uncomment this to generate task list with stack sizes using the /heap handler PLEASE BE AWARE: The following CONFIG parameters have to to be set in From 939271f2a0f62d8e5dc3b653abf1edfb5989a074 Mon Sep 17 00:00:00 2001 From: Slider0007 Date: Thu, 2 Feb 2023 00:01:41 +0100 Subject: [PATCH 3/6] buffer overflow check + error message --- .../jomjol_image_proc/CImageBasis.cpp | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/code/components/jomjol_image_proc/CImageBasis.cpp b/code/components/jomjol_image_proc/CImageBasis.cpp index 49f188e7..781bdd8f 100644 --- a/code/components/jomjol_image_proc/CImageBasis.cpp +++ b/code/components/jomjol_image_proc/CImageBasis.cpp @@ -18,6 +18,9 @@ using namespace std; static const char *TAG = "C IMG BASIS"; +bool jpgFileTooLarge = false; // JPG creation verfication + + //#define DEBUG_DETAIL_ON @@ -63,12 +66,18 @@ void writejpghelp(void *context, void *data, int size) ImageData* _zw = (ImageData*) context; uint8_t *voidstart = _zw->data; uint8_t *datastart = (uint8_t*) data; - voidstart += _zw->size; + + if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy -> no buffer anymore + voidstart += _zw->size; - for (int i = 0; i < size; ++i) - *(voidstart + i) = *(datastart + i); + for (int i = 0; i < size; ++i) + *(voidstart + i) = *(datastart + i); - _zw->size += size; + _zw->size += size; + } + else { + jpgFileTooLarge = true; + } } @@ -80,6 +89,10 @@ ImageData* CImageBasis::writeToMemoryAsJPG(const int quality) 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: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE)); + } return ii; } @@ -92,6 +105,10 @@ void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality) 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: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE)); + } memCopy((uint8_t*) ii, (uint8_t*) i, sizeof(ImageData)); delete ii; } @@ -364,7 +381,7 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels) RGBImageLock(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CreateEmptyImage"); + LogFile.WriteHeapInfo("CreateEmptyImage"); #endif int memsize = width * height * channels; @@ -372,9 +389,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels) if (rgb_image == NULL) { - //ESP_LOGE(TAG, "CImageBasis::CreateEmptyImage: No more free memory!! Needed: %d %d %d %d", width, height, channels, memsize); - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize)); - LogFile.WriteHeapInfo("CImageBasis::CreateEmptyImage"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CreateEmptyImage: Can't allocate enough memory: " + std::to_string(memsize)); + LogFile.WriteHeapInfo("CreateEmptyImage"); RGBImageRelease(); return; } @@ -396,7 +412,7 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels) void CImageBasis::EmptyImage() { #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::EmptyImage"); + LogFile.WriteHeapInfo("EmptyImage"); #endif stbi_uc* p_source; @@ -430,7 +446,7 @@ void CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len) { 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("CImageBasis::LoadFromMemory"); + LogFile.WriteHeapInfo("LoadFromMemory"); doReboot(); } @@ -450,7 +466,7 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom) RGBImageLock(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_copyfrom - Start"); + LogFile.WriteHeapInfo("CImageBasis_copyfrom - Start"); #endif int memsize = width * height * channels; @@ -458,8 +474,8 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom) if (rgb_image == NULL) { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize)); - LogFile.WriteHeapInfo("CImageBasis::CImageBasis-Copyfrom"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: Can't allocate enough memory: " + std::to_string(memsize)); + LogFile.WriteHeapInfo("CImageBasis-Copyfrom"); RGBImageRelease(); return; } @@ -468,7 +484,7 @@ CImageBasis::CImageBasis(CImageBasis *_copyfrom) RGBImageRelease(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_copyfrom - done"); + LogFile.WriteHeapInfo("CImageBasis_copyfrom - done"); #endif } @@ -485,7 +501,7 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels) RGBImageLock(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_width,height,ch - Start"); + LogFile.WriteHeapInfo("CImageBasis_width,height,ch - Start"); #endif int memsize = width * height * channels; @@ -493,8 +509,8 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels) if (rgb_image == NULL) { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis::CImageBasis-width,height,ch: Can't allocate enough memory: " + std::to_string(memsize)); - LogFile.WriteHeapInfo("CImageBasis::CImageBasis-width,height,ch"); + 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; } @@ -502,7 +518,7 @@ CImageBasis::CImageBasis(int _width, int _height, int _channels) RGBImageRelease(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_width,height,ch - done"); + LogFile.WriteHeapInfo("CImageBasis_width,height,ch - done"); #endif } @@ -522,14 +538,14 @@ CImageBasis::CImageBasis(std::string _image) RGBImageLock(); #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_image - Start"); + 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::CImageBasis-image: Failed to load " + _image + "! Is it corrupted?"); - LogFile.WriteHeapInfo("CImageBasis::CImageBasis-image"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-image: Failed to load " + _image + "! Is it corrupted?"); + LogFile.WriteHeapInfo("CImageBasis-image"); RGBImageRelease(); return; } @@ -543,7 +559,7 @@ CImageBasis::CImageBasis(std::string _image) #endif #ifdef DEBUG_DETAIL_ON - LogFile.WriteHeapInfo("CImageBasis::CImageBasis_image - done"); + LogFile.WriteHeapInfo("CImageBasis_image - done"); #endif } @@ -643,7 +659,7 @@ 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, "CImageBasis::Resize - Target image size does not fit!"); + ESP_LOGE(TAG, "Resize - Target image size does not fit!"); return; } From 4986b1936f2113a4c29b3931ad7da92573469df5 Mon Sep 17 00:00:00 2001 From: Slider0007 Date: Thu, 2 Feb 2023 16:45:26 +0100 Subject: [PATCH 4/6] Fix initialflip issue --- .../jomjol_flowcontroll/ClassFlowAlignment.cpp | 11 ++++------- .../jomjol_flowcontroll/ClassFlowControll.cpp | 3 ++- code/components/jomjol_image_proc/CImageBasis.cpp | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp b/code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp index 4d4038b8..1b686176 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp @@ -208,6 +208,10 @@ bool ClassFlowAlignment::doFlow(string time) int _zw = ImageBasis->height; ImageBasis->height = ImageBasis->width; ImageBasis->width = _zw; + + _zw = ImageTMP->width; + ImageTMP->width = ImageTMP->height; + ImageTMP->height = _zw; } if (initialmirror) @@ -246,13 +250,6 @@ bool ClassFlowAlignment::doFlow(string time) if (SaveAllFiles) { - if (initialflip) - { - int _zw = ImageTMP->width; - ImageTMP->width = ImageTMP->height; - ImageTMP->height = _zw; - } - AlignAndCutImage->SaveToFile(FormatFileName("/sdcard/img_tmp/alg.jpg")); ImageTMP->SaveToFile(FormatFileName("/sdcard/img_tmp/alg_roi.jpg")); } diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 3da1e39f..667ab683 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp @@ -755,7 +755,8 @@ esp_err_t ClassFlowControll::GetJPGStream(std::string _fn, httpd_req_t *req) fseek(file, 0, SEEK_SET); /* reset */ if (flowalignment->AlgROI->size > MAX_JPG_SIZE) { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_take_image.jpg too large: " + std::to_string(flowalignment->AlgROI->size)); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "File /sdcard/html/Flowstate_take_image.jpg (" + std::to_string(flowalignment->AlgROI->size) + + ") > allocated buffer (" + std::to_string(MAX_JPG_SIZE) + ")"); fclose(file); return ESP_FAIL; } diff --git a/code/components/jomjol_image_proc/CImageBasis.cpp b/code/components/jomjol_image_proc/CImageBasis.cpp index 781bdd8f..a8637e3f 100644 --- a/code/components/jomjol_image_proc/CImageBasis.cpp +++ b/code/components/jomjol_image_proc/CImageBasis.cpp @@ -67,7 +67,7 @@ void writejpghelp(void *context, void *data, int size) uint8_t *voidstart = _zw->data; uint8_t *datastart = (uint8_t*) data; - if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy -> no buffer anymore + if ((_zw->size < MAX_JPG_SIZE)) { // Abort copy to prevent buffer overflow voidstart += _zw->size; for (int i = 0; i < size; ++i) @@ -91,7 +91,7 @@ ImageData* CImageBasis::writeToMemoryAsJPG(const int quality) if (jpgFileTooLarge) { jpgFileTooLarge = false; - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE)); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: Creation aborted! JPG size > preallocated buffer: " + std::to_string(MAX_JPG_SIZE)); } return ii; } @@ -107,7 +107,7 @@ void CImageBasis::writeToMemoryAsJPG(ImageData* i, const int quality) if (jpgFileTooLarge) { jpgFileTooLarge = false; - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "writeToMemoryAsJPG: JPG creation aborted! Preallocated buffer not sufficient: " + std::to_string(MAX_JPG_SIZE)); + 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; From bbd60c9d19ff827960b315415ebc37222729e84a Mon Sep 17 00:00:00 2001 From: Slider0007 Date: Thu, 2 Feb 2023 21:46:00 +0100 Subject: [PATCH 5/6] Revert "Increase max JPG size (128k->160k)" This reverts commit a851fd393c837f38dd6625a89aa555851b90e728. --- code/include/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/include/defines.h b/code/include/defines.h index 98db2bf7..8f7faf2d 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -130,7 +130,7 @@ //CImageBasis #define HTTP_BUFFER_SENT 1024 #define GET_MEMORY(X) heap_caps_malloc(X, MALLOC_CAP_SPIRAM) - #define MAX_JPG_SIZE 160000 + #define MAX_JPG_SIZE 128000 //CAlignAndCutImage + CImageBasis From 0ddcbbc847902589fc349445f84c6e303495348e Mon Sep 17 00:00:00 2001 From: Slider0007 Date: Thu, 2 Feb 2023 21:48:22 +0100 Subject: [PATCH 6/6] Revert "Disable USE_HIMEM_IF_AVAILABLE" This reverts commit d40d5ba0f6021938b7a3e04837cc3502e6adfab9. --- code/include/defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/include/defines.h b/code/include/defines.h index 8f7faf2d..c5a13d1d 100644 --- a/code/include/defines.h +++ b/code/include/defines.h @@ -21,7 +21,7 @@ // use himem //https://github.com/jomjol/AI-on-the-edge-device/issues/1842 - //#define USE_HIMEM_IF_AVAILABLE + #define USE_HIMEM_IF_AVAILABLE /* Uncomment this to generate task list with stack sizes using the /heap handler PLEASE BE AWARE: The following CONFIG parameters have to to be set in