[PATCH 2/3] libtuning: Gradient: Correct Linear distribution

Daniel Scally dan.scally at ideasonboard.com
Wed Mar 5 00:12:53 CET 2025


The Linear distributor attempts to distribute pixels into sectors
of even size. Where the image width doesn't allow this it attempts
to create all but one sector of full size, and allows the caller
to choose to have the stunted sector at the front or the back. The
implicit assumption is that

	domain == (sector_size * (n_sectors - 1)) + remainder

which does not necessarily hold true. For example with 32 sectors and
a domain of 648 the calculated sector size will be 21 pixels, which
leads to 31 * 21 = 651 which is larger than the domain size.

Correct the issue by checking if there's more one stunted sector to
be filled. If there is, rather than following the remainder hint for
distribution place one of the stunted sectors at the front and one at
the back.

Signed-off-by: Daniel Scally <dan.scally at ideasonboard.com>
---
 utils/tuning/libtuning/gradient.py | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/utils/tuning/libtuning/gradient.py b/utils/tuning/libtuning/gradient.py
index b643f502..bb2edef2 100644
--- a/utils/tuning/libtuning/gradient.py
+++ b/utils/tuning/libtuning/gradient.py
@@ -55,17 +55,23 @@ class Linear(Gradient):
 
         size = math.ceil(size)
         rem = domain % size
-        output_sectors = [int(size)] * (sectors - 1)
+        n_full_sectors = math.floor(domain / size)
+        output_sectors = [int(size)] * n_full_sectors
 
         if self.remainder == lt.Remainder.Float:
             size = domain / sectors
             output_sectors = [size] * sectors
-        elif self.remainder == lt.Remainder.DistributeFront:
+        elif ((sectors - n_full_sectors) == 1):
+            if self.remainder == lt.Remainder.DistributeFront:
+                output_sectors.append(int(rem))
+            elif self.remainder == lt.Remainder.DistributeBack:
+                output_sectors.insert(0, int(rem))
+            else:
+                raise ValueError
+        else:
+            rem = rem / 2
             output_sectors.append(int(rem))
-        elif self.remainder == lt.Remainder.DistributeBack:
             output_sectors.insert(0, int(rem))
-        else:
-            raise ValueError
 
         return output_sectors
 
-- 
2.34.1



More information about the libcamera-devel mailing list