Преглед изворни кода

Merge branch 'Increase-max-JPG-size'

jomjol пре 3 година
родитељ
комит
4424337a4e

+ 3 - 7
Changelog.md

@@ -2,8 +2,6 @@
 
 ## [Unreleased]
 
-## [14.0.0] - 2023-02-02
-
 **Stabilization and Improved User Experience**
 
 Thanks to over 80 Pull Requests from 6 contributors, we can anounce another great release with many many improvements and new features:
@@ -152,8 +150,8 @@ Improve **u**ser e**x**perience
 5.  Now you can reboot.
 
 If anything breaks you can try to
-1\. Call `http://<IP>/ota?task=update&file=firmware.bin` resp. `http://<IP>/ota?task=update&file=html.zip` if the upload successed but the extraction failed.
-1\. Use the initial_esp32_setup.zip ( <https://github.com/jomjol/AI-on-the-edge-device/wiki/Installation> ) as alternative.
+1. Call `http://<IP>/ota?task=update&file=firmware.bin` resp. `http://<IP>/ota?task=update&file=html.zip` if the upload successed but the extraction failed.
+1. Use the initial_esp32_setup.zip ( <https://github.com/jomjol/AI-on-the-edge-device/wiki/Installation> ) as alternative.
 
 ### Added
 
@@ -823,6 +821,4 @@ External Illumination
 
 [11.3.9]: https://github.com/haverland/AI-on-the-edge-device/compare/v10.6.2...v11.3.9
 
-[Unreleased]: https://github.com/jomjol/AI-on-the-edge-device/compare/14.0.0...HEAD
-
-[14.0.0]: https://github.com/jomjol/AI-on-the-edge-device/compare/13.0.8...14.0.0
+[Unreleased]: https://github.com/jomjol/AI-on-the-edge-device/compare/v14.0.0-RC8...HEAD

+ 4 - 7
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"));
     }

+ 2 - 1
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;
                     }

+ 39 - 23
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 to prevent buffer overflow
+        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: Creation aborted! JPG size > preallocated buffer: " + 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: Creation aborted! JPG size > preallocated buffer: " + 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;
     }