@@ -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;
@@ -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
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(+)