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

Message ID 20250718144456.58625-1-mzamazal@redhat.com
State New
Headers show
Series
  • [1/2] libcamera: geometry: Add Rectangle::croppedBy
Related show

Commit Message

Milan Zamazal July 18, 2025, 2:44 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   | 18 ++++++++++++++++++
 2 files changed, 20 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..8c1b156c5 100644
--- a/src/libcamera/geometry.cpp
+++ b/src/libcamera/geometry.cpp
@@ -837,6 +837,24 @@  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
+{
+	const unsigned int w = numerator * width / denominator;
+	const unsigned int h = numerator * height / denominator;
+	return Rectangle((width - w) / 2, (height - h) / 2, w, h);
+}
+
 /**
  * \brief Transform a Rectangle from one reference rectangle to another
  * \param[in] source The \a source reference rectangle