[libcamera-devel,v6,13/12] fixup! libcamera: camera: Introduce Orientation
diff mbox series

Message ID 20231022224159.30298-1-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • libcamera: Replace CameraConfiguration::transform
Related show

Commit Message

Laurent Pinchart Oct. 22, 2023, 10:41 p.m. UTC
- Add an orientationFromRotation() function

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 include/libcamera/orientation.h |  2 ++
 src/libcamera/orientation.cpp   | 36 +++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/orientation.h b/include/libcamera/orientation.h
index ae19d4c44a55..9a2c2fb2fbb1 100644
--- a/include/libcamera/orientation.h
+++ b/include/libcamera/orientation.h
@@ -23,6 +23,8 @@  enum class Orientation {
 	Rotate90,
 };
 
+Orientation orientationFromRotation(int angle, bool *success = nullptr);
+
 std::ostream &operator<<(std::ostream &out, const Orientation &orientation);
 
 } /* namespace libcamera */
diff --git a/src/libcamera/orientation.cpp b/src/libcamera/orientation.cpp
index 5caf76f73865..a6363df30861 100644
--- a/src/libcamera/orientation.cpp
+++ b/src/libcamera/orientation.cpp
@@ -56,6 +56,42 @@  namespace libcamera {
  * \image html rotation/rotate90.svg
  */
 
+/**
+ * \brief Return the orientation representing a rotation of the given angle
+ * clockwise
+ * \param[in] angle The angle of rotation in a clockwise sense. Negative values
+ * can be used to represent anticlockwise rotations
+ * \param[out] success Set to `true` if the angle is a multiple of 90 degrees,
+ * otherwise `false`
+ * \return The orientation corresponding to the rotation if \a success was set
+ * to `true`, otherwise the `Rotate0` orientation
+ */
+Orientation orientationFromRotation(int angle, bool *success)
+{
+	angle = angle % 360;
+	if (angle < 0)
+		angle += 360;
+
+	if (success != nullptr)
+		*success = true;
+
+	switch (angle) {
+	case 0:
+		return Orientation::Rotate0;
+	case 90:
+		return Orientation::Rotate90;
+	case 180:
+		return Orientation::Rotate180;
+	case 270:
+		return Orientation::Rotate270;
+	}
+
+	if (success != nullptr)
+		*success = false;
+
+	return Orientation::Rotate0;
+}
+
 /**
  * \brief Prints human-friendly names for Orientation items
  * \param[in] out The output stream