Selaa lähdekoodia

Enforce VGA camera output and retain sensor-side zoom

SybexXx 1 päivä sitten
vanhempi
sitoutus
82d972761c
1 muutettua tiedostoa jossa 24 lisäystä ja 10 poistoa
  1. 24 10
      code/components/jomjol_image_proc/CImageBasis.cpp

+ 24 - 10
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -513,6 +513,19 @@ bool CImageBasis::LoadFromMemory(stbi_uc *_buffer, int len)
         return false;
         return false;
     }
     }
 
 
+    // tmpImage is used for camera frames. Inspect dimensions before decoding so
+    // an unexpected high-resolution JPEG cannot allocate a large temporary buffer.
+    if (name == "tmpImage") {
+        int inputWidth = 0, inputHeight = 0, inputChannels = 0;
+        if (!stbi_info_from_memory(_buffer, len, &inputWidth, &inputHeight, &inputChannels) ||
+            inputWidth <= 0 || inputHeight <= 0 ||
+            inputWidth > CAMERA_MAX_FRAME_WIDTH || inputHeight > CAMERA_MAX_FRAME_HEIGHT) {
+            LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
+                "LoadFromMemory: tmpImage JPEG exceeds the VGA limit or has invalid dimensions");
+            return false;
+        }
+    }
+
     int newWidth = 0, newHeight = 0, sourceChannels = 0;
     int newWidth = 0, newHeight = 0, sourceChannels = 0;
     uint8_t* decoded = stbi_load_from_memory(_buffer, len, &newWidth, &newHeight,
     uint8_t* decoded = stbi_load_from_memory(_buffer, len, &newWidth, &newHeight,
                                               &sourceChannels, STBI_rgb);
                                               &sourceChannels, STBI_rgb);
@@ -604,6 +617,14 @@ CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: invalid source dimensions");
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CImageBasis-Copyfrom: invalid source dimensions");
         return;
         return;
     }
     }
+    if (name == "tmpImage" &&
+        (_copyfrom->width > CAMERA_MAX_FRAME_WIDTH ||
+         _copyfrom->height > CAMERA_MAX_FRAME_HEIGHT)) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
+            "CImageBasis-Copyfrom: tmpImage exceeds the VGA limit (" +
+            std::to_string(_copyfrom->width) + "x" + std::to_string(_copyfrom->height) + ")");
+        return;
+    }
     width = _copyfrom->width;
     width = _copyfrom->width;
     height = _copyfrom->height;
     height = _copyfrom->height;
     channels = _copyfrom->channels;
     channels = _copyfrom->channels;
@@ -620,18 +641,11 @@ CImageBasis::CImageBasis(string _name, CImageBasis *_copyfrom)
     }
     }
 
 
     if (name == "tmpImage") {
     if (name == "tmpImage") {
+        // tmpImage is an in-place VGA workspace. Do not silently add a second,
+        // larger PSRAM allocation when the shared workspace cannot be leased.
         rgb_image = static_cast<uint8_t*>(psram_reserve_shared_tmp_image_memory(imageSize));
         rgb_image = static_cast<uint8_t*>(psram_reserve_shared_tmp_image_memory(imageSize));
         ownsSharedTempImageLease = (rgb_image != NULL);
         ownsSharedTempImageLease = (rgb_image != NULL);
-        if (ownsSharedTempImageLease) {
-            memsize = static_cast<int>(imageSize);
-        } else {
-            // High-resolution camera frames can exceed the fixed shared workspace.
-            // Keep the frame in PSRAM heap rather than writing past the shared region.
-            rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
-                std::string(TAG) + "->CImageBasis (" + name + " fallback)", imageSize,
-                MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
-            memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
-        }
+        memsize = (rgb_image != NULL) ? static_cast<int>(imageSize) : 0;
     } else {
     } else {
         rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
         rgb_image = static_cast<uint8_t*>(malloc_psram_heap(
             std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,
             std::string(TAG) + "->CImageBasis (" + name + ")", imageSize,