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

perf(camera): memcpy/memset instead of per-pixel loops in capture path

CCamera::CaptureToBasisImage() first blanks the raw image with
CImageBasis::EmptyImage() and, after decoding the JPEG frame, copies the
decoded image into it pixel by pixel. Both loops ran x in the outer and
y in the inner loop, i.e. column by column through a 921 KB PSRAM
buffer, one byte at a time. Measured on an ESP32-CAM (VGA):

  EmptyImage   ~1540 ms
  copy         ~2285 ms

Both buffers are packed RGB888 of the same size, so the copy is a plain
memcpy() and the blanking a plain memset(). CreateEmptyImage() gets the
same treatment. The resulting image data is byte-for-byte identical.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh пре 1 месец
родитељ
комит
67bc8babd1

+ 3 - 15
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -710,8 +710,6 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
         return ESP_OK;
         return ESP_OK;
     }
     }
 
 
-    stbi_uc *p_target;
-    stbi_uc *p_source;
     int channels = 3;
     int channels = 3;
     int width = CCstatus.ImageWidth;
     int width = CCstatus.ImageWidth;
     int height = CCstatus.ImageHeight;
     int height = CCstatus.ImageHeight;
@@ -722,19 +720,9 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
 #endif
 #endif
 
 
-    for (int x = 0; x < width; ++x)
-    {
-        for (int y = 0; y < height; ++y)
-        {
-            p_target = _Image->rgb_image + (channels * (y * width + x));
-            p_source = _zwImage->rgb_image + (channels * (y * width + x));
-
-            for (int c = 0; c < channels; c++)
-            {
-                p_target[c] = p_source[c];
-            }
-        }
-    }
+    // Both images are packed RGB888 with the same dimensions: copy the whole frame at once
+    // (the former per-pixel, column-by-column loop took ~2 s on an ESP32-CAM)
+    memcpy(_Image->rgb_image, _zwImage->rgb_image, width * height * channels);
 
 
     delete _zwImage;
     delete _zwImage;
 
 

+ 4 - 18
code/components/jomjol_image_proc/CImageBasis.cpp

@@ -398,15 +398,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
         return;
         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;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
 
     RGBImageRelease();
     RGBImageRelease();
 }
 }
@@ -418,17 +411,10 @@ void CImageBasis::EmptyImage()
         LogFile.WriteHeapInfo("EmptyImage");
         LogFile.WriteHeapInfo("EmptyImage");
     #endif
     #endif
 
 
-    stbi_uc* p_source;
-
     RGBImageLock();
     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;
-        }
+    // One contiguous memset instead of a per-pixel, column-by-column loop through PSRAM
+    memset(rgb_image, 0, width * height * channels);
 
 
     RGBImageRelease();
     RGBImageRelease();
 }
 }