Просмотр исходного кода

Check camera file conversion and ROI crop buffer sizes

SybexXx 2 дней назад
Родитель
Сommit
186cf6dd83

+ 28 - 35
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -774,49 +774,42 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
     uint8_t *buf = NULL;
     size_t buf_len = 0;
     bool converted = false;
+    bool captureOkay = false;
 
-    if (ftype.compare("BMP") == 0)
-    {
-        frame2bmp(fb, &buf, &buf_len);
+    if (ftype == "BMP") {
         converted = true;
-    }
-
-    if (ftype.compare("JPG") == 0)
-    {
-        if (fb->format != PIXFORMAT_JPEG)
-        {
-            bool jpeg_converted = frame2jpg(fb, CCstatus.ImageQuality, &buf, &buf_len);
-            converted = true;
-
-            if (!jpeg_converted)
-            {
-                ESP_LOGE(TAG, "JPEG compression failed");
-            }
-        }
-        else
-        {
+        captureOkay = frame2bmp(fb, &buf, &buf_len) && buf != NULL && buf_len > 0;
+    } else if (ftype == "JPG") {
+        if (fb->format == PIXFORMAT_JPEG) {
             buf_len = fb->len;
             buf = fb->buf;
+            captureOkay = (buf != NULL && buf_len > 0);
+        } else {
+            converted = true;
+            captureOkay = frame2jpg(fb, CCstatus.ImageQuality, &buf, &buf_len) &&
+                          buf != NULL && buf_len > 0;
+            if (!captureOkay) ESP_LOGE(TAG, "JPEG compression failed");
         }
     }
 
-    FILE *fp = fopen(nm.c_str(), "wb");
-
-    if (fp == NULL)
-    {
-        // If an error occurs during the file creation
-        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
-    }
-    else
-    {
-        fwrite(buf, sizeof(uint8_t), buf_len, fp);
-        fclose(fp);
+    if (captureOkay) {
+        FILE *fp = fopen(nm.c_str(), "wb");
+        if (fp == NULL) {
+            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
+            captureOkay = false;
+        } else {
+            const size_t written = fwrite(buf, 1, buf_len, fp);
+            const int closeResult = fclose(fp);
+            if (written != buf_len || closeResult != 0) {
+                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: incomplete write to " + nm);
+                captureOkay = false;
+            }
+        }
+    } else {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: unsupported image format or conversion failed");
     }
 
-    if (converted)
-    {
-        free(buf);
-    }
+    if (converted && buf != NULL) free(buf);
 
     esp_camera_fb_return(fb);
 
@@ -825,7 +818,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
         LightOnOff(false); // Flash-LED off
     }
 
-    return ESP_OK;
+    return captureOkay ? ESP_OK : ESP_FAIL;
 }
 
 esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)

+ 2 - 0
code/components/jomjol_image_proc/CAlignAndCutImage.cpp

@@ -211,6 +211,8 @@ bool CAlignAndCutImage::CutAndSave(int x1, int y1, int dx, int dy, CImageBasis *
     if (!getCutRectangle(width, height, x1, y1, dx, dy, &x2, &y2)) return false;
     const int cropWidth = x2 - x1;
     const int cropHeight = y2 - y1;
+    size_t cropBufferSize = 0;
+    if (!getCutBufferSize(cropWidth, cropHeight, channels, &cropBufferSize)) return false;
     if (_target->height != cropHeight || _target->width != cropWidth ||
         _target->channels != channels || !_target->ImageOkay()) {
         ESP_LOGD(TAG, "CAlignAndCutImage::CutAndSave - Image size does not match!");