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

perf(image): row-major loops in CRotateImage, skip identity transforms

Rotate(), RotateAntiAliasing() and Translate() iterated x in the outer
and y in the inner loop. Every inner step therefore jumped one image row
(width * 3 = 1920 bytes for VGA) in both the target and the source
buffer, which defeats the PSRAM cache. Each pass over a VGA frame took
about 2 seconds on an ESP32-CAM. Swapping the loops does not change the
result: every target pixel is computed independently of all others.

In addition, Translate(0, 0) and Rotate(0) without flip are identity
transforms, so the full-frame pass plus the 921 KB copy back are skipped.
Align() calls both on every round and the found offset is often (0, 0).
(RotateAntiAliasing(0) is not an identity - it whitens the last row and
column - so it is left untouched.) The temp image stays identical to the
image in all cases, as before.

Measured on an ESP32-CAM (VGA): Translate 2457 -> 527 ms, Rotate
1902 -> 574 ms, initial rotation 1814 -> 540 ms per round.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh пре 1 месец
родитељ
комит
6a8fa724d9
1 измењених фајлова са 17 додато и 6 уклоњено
  1. 17 6
      code/components/jomjol_image_proc/CRotateImage.cpp

+ 17 - 6
code/components/jomjol_image_proc/CRotateImage.cpp

@@ -20,6 +20,10 @@ CRotateImage::CRotateImage(std::string _name, CImageBasis *_org, CImageBasis *_t
 
 void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
 {
+    if ((_angle == 0) && !doflip) {  // identity transform, the copy would leave the image unchanged
+        return;
+    }
+
     int org_width, org_height;
     float m[2][3];
 
@@ -79,8 +83,9 @@ void CRotateImage::Rotate(float _angle, int _centerx, int _centery)
 
     RGBImageLock();
 
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
+    // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
+    for (int y = 0; y < height; ++y)
+        for (int x = 0; x < width; ++x)
         {
             p_target = odata + (channels * (y * width + x));
 
@@ -181,8 +186,9 @@ void CRotateImage::RotateAntiAliasing(float _angle, int _centerx, int _centery)
 
     RGBImageLock();
 
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
+    // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
+    for (int y = 0; y < height; ++y)
+        for (int x = 0; x < width; ++x)
         {
             p_target = odata + (channels * (y * width + x));
 
@@ -252,6 +258,10 @@ void CRotateImage::RotateAntiAliasing(float _angle)
 
 void CRotateImage::Translate(int _dx, int _dy)
 {
+    if ((_dx == 0) && (_dy == 0)) {  // identity transform, the copy would leave the image unchanged
+        return;
+    }
+
     int memsize = width * height * channels;
     uint8_t* odata;
     if (ImageTMP)
@@ -271,8 +281,9 @@ void CRotateImage::Translate(int _dx, int _dy)
 
     RGBImageLock();
 
-    for (int x = 0; x < width; ++x)
-        for (int y = 0; y < height; ++y)
+    // y outer / x inner: write the target (and mostly read the source) sequentially in PSRAM
+    for (int y = 0; y < height; ++y)
+        for (int x = 0; x < width; ++x)
         {
             p_target = odata + (channels * (y * width + x));