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