[v2,1/2] libcamera: geometry: Add Rectangle::croppedBy
diff mbox series

Message ID 20250724152936.99830-2-mzamazal@redhat.com
State New
Headers show
Series
  • Reduce statistics image area for GPU
Related show

Commit Message

Milan Zamazal July 24, 2025, 3:29 p.m. UTC
Let's add a new Rectangle method that returns the coordinates of the
central area of the original rectangle cropped by given
numerator/denominator fraction.  This is useful to specify a limited
area of the image to operate on, for example computing statistics only
from a reduced area for speedup.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 include/libcamera/geometry.h |  2 ++
 src/libcamera/geometry.cpp   | 19 +++++++++++++++++++
 test/geometry.cpp            |  9 +++++++++
 3 files changed, 30 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
index f322e3d5b..603c9a117 100644
--- a/include/libcamera/geometry.h
+++ b/include/libcamera/geometry.h
@@ -297,6 +297,8 @@  public:
 	[[nodiscard]] Rectangle scaledBy(const Size &numerator,
 					 const Size &denominator) const;
 	[[nodiscard]] Rectangle translatedBy(const Point &point) const;
+	[[nodiscard]] Rectangle croppedBy(const unsigned int numerator,
+					  const unsigned int denominator) const;
 
 	Rectangle transformedBetween(const Rectangle &source,
 				     const Rectangle &target) const;
diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp
index 81cc8cd53..8ceb75698 100644
--- a/src/libcamera/geometry.cpp
+++ b/src/libcamera/geometry.cpp
@@ -837,6 +837,25 @@  Rectangle Rectangle::translatedBy(const Point &point) const
 	return { x + point.x, y + point.y, width, height };
 }
 
+/**
+ * \brief Return a central area crop of the rectangle
+ * \param[in] numerator The numerator of the crop factor
+ * \param[in] denominator The denominator of the crop factor
+ *
+ * The rectangle of the central part of the original rectangle is computed, of
+ * numerator/denominator times the original width and height.
+ *
+ * \return The cropped Rectangle coordinates
+ */
+Rectangle Rectangle::croppedBy(const unsigned int numerator,
+			       const unsigned int denominator) const
+{
+	unsigned int w = static_cast<uint64_t>(width) * numerator / denominator;
+	unsigned int h = static_cast<uint64_t>(height) * numerator / denominator;
+
+	return Size(w, h).centeredTo(center());
+}
+
 /**
  * \brief Transform a Rectangle from one reference rectangle to another
  * \param[in] source The \a source reference rectangle
diff --git a/test/geometry.cpp b/test/geometry.cpp
index 11df043b7..9864774f4 100644
--- a/test/geometry.cpp
+++ b/test/geometry.cpp
@@ -466,6 +466,15 @@  protected:
 			return TestFail;
 		}
 
+		/* Rectangle::croppedBy() tests */
+		if (Rectangle(Size(300, 100)).croppedBy(2, 3) !=
+			    Rectangle(50, 17, 200, 66) ||
+		    Rectangle(-100, 100, 300, 700).croppedBy(3, 7) !=
+			    Rectangle(-14, 300, 128, 300)) {
+			cout << "Rectangle::croppedBy() test failed " << endl;
+			return TestFail;
+		}
+
 		/* Rectangle::translateBy() tests */
 		r = Rectangle(10, -20, 300, 400);
 		r.translateBy(Point(-30, 40));