Procházet zdrojové kódy

Merge pull request #4168 from alfeg/perf/camera-capture-memcpy

perf(camera): memcpy/memset instead of per-pixel loops in capture path
SybexXx před 1 měsícem
rodič
revize
288d583ef7

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

@@ -710,8 +710,6 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
         return ESP_OK;
     }
 
-    stbi_uc *p_target;
-    stbi_uc *p_source;
     int channels = 3;
     int width = CCstatus.ImageWidth;
     int height = CCstatus.ImageHeight;
@@ -722,19 +720,9 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
     LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
 #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;
 

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

@@ -398,15 +398,8 @@ void CImageBasis::CreateEmptyImage(int _width, int _height, int _channels)
         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();
 }
@@ -418,17 +411,10 @@ void CImageBasis::EmptyImage()
         LogFile.WriteHeapInfo("EmptyImage");
     #endif
 
-    stbi_uc* p_source;
-
     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();
 }