From patchwork Tue Jan 28 08:35:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 22654 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 15A20BDB13 for ; Tue, 28 Jan 2025 08:35:43 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id EF0056855D; Tue, 28 Jan 2025 09:35:41 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=protonmail.com header.i=@protonmail.com header.b="eAWLKRhU"; dkim-atps=neutral Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4B4B860353 for ; Tue, 28 Jan 2025 09:35:40 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1738053339; x=1738312539; bh=SZzYsjbndAcH1bdiBs3C+saYfKuX+GxHVP5A8hhVPvs=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector: List-Unsubscribe:List-Unsubscribe-Post; b=eAWLKRhUD2P7xhTVD5cjGitwPAWt2HZ3fvg01Lr3GEy90xA/0FADiRdTjhC4aMoiL 46L1pmxLLIatx4oaHst5O5ro7kbspiGatgUuGxlaP6MDlmuf0Ec/PgpByGa7KcODMd BDWNBBP2t8H96oj333dQcpR2Dm3ohvuPwonyafyOgnM7RT2DzREmGrdV0HGQgQ4FXi /Hl5P8WrqZaowCOBm8zTCaZ9HcUju0ik7TJs9HZ8nHgkMn1gk7eKV0PcdgQoJnpBnB 5o67E966Np4DKtequo3rjH3TEwIIN3t5mFPK7Qdxi6zjO3EqOlSo5479UVjSnjgrpz iPklsa0mK7x1g== Date: Tue, 28 Jan 2025 08:35:33 +0000 To: libcamera-devel@lists.libcamera.org From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= Subject: [RFC PATCH v1] libcamera: {orientation, transform}FromRotation(): Return `std::optional` Message-ID: <20250128083530.351935-1-pobrn@protonmail.com> Feedback-ID: 20568564:user:proton X-Pm-Message-ID: 1fb590a451157d0c9ae7e6aa7a5af1caede813a0 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Return an empty `std::optional` on failure instead of using a separate out parameter for signalling success. Signed-off-by: Barnabás Pőcze --- include/libcamera/orientation.h | 3 ++- include/libcamera/transform.h | 4 +++- src/libcamera/orientation.cpp | 17 ++++------------- src/libcamera/sensor/camera_sensor_legacy.cpp | 8 ++++---- src/libcamera/sensor/camera_sensor_raw.cpp | 8 ++++---- src/libcamera/transform.cpp | 17 ++++------------- src/py/libcamera/py_transform.cpp | 15 +++++++-------- 7 files changed, 28 insertions(+), 44 deletions(-) diff --git a/include/libcamera/orientation.h b/include/libcamera/orientation.h index a3b40e636..4e5b0f570 100644 --- a/include/libcamera/orientation.h +++ b/include/libcamera/orientation.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace libcamera { @@ -23,7 +24,7 @@ enum class Orientation { Rotate90, }; -Orientation orientationFromRotation(int angle, bool *success = nullptr); +std::optional orientationFromRotation(int angle); std::ostream &operator<<(std::ostream &out, const Orientation &orientation); diff --git a/include/libcamera/transform.h b/include/libcamera/transform.h index 4517412a8..4769dc2e9 100644 --- a/include/libcamera/transform.h +++ b/include/libcamera/transform.h @@ -7,6 +7,8 @@ #pragma once +#include + namespace libcamera { enum class Orientation; @@ -68,7 +70,7 @@ constexpr Transform operator~(Transform t) return static_cast(~static_cast(t) & 7); } -Transform transformFromRotation(int angle, bool *success = nullptr); +std::optional transformFromRotation(int angle); Transform operator/(const Orientation &o1, const Orientation &o2); Orientation operator*(const Orientation &o, const Transform &t); diff --git a/src/libcamera/orientation.cpp b/src/libcamera/orientation.cpp index 7d7d21ae8..9f118f834 100644 --- a/src/libcamera/orientation.cpp +++ b/src/libcamera/orientation.cpp @@ -59,21 +59,15 @@ namespace libcamera { * \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 + * can be used to represent anticlockwise rotations. Must be a multiple of 90. + * \return The orientation corresponding to the rotation */ -Orientation orientationFromRotation(int angle, bool *success) +std::optional orientationFromRotation(int angle) { angle = angle % 360; if (angle < 0) angle += 360; - if (success != nullptr) - *success = true; - switch (angle) { case 0: return Orientation::Rotate0; @@ -85,10 +79,7 @@ Orientation orientationFromRotation(int angle, bool *success) return Orientation::Rotate270; } - if (success != nullptr) - *success = false; - - return Orientation::Rotate0; + return {}; } /** diff --git a/src/libcamera/sensor/camera_sensor_legacy.cpp b/src/libcamera/sensor/camera_sensor_legacy.cpp index b0c6abde4..d8c0bd41b 100644 --- a/src/libcamera/sensor/camera_sensor_legacy.cpp +++ b/src/libcamera/sensor/camera_sensor_legacy.cpp @@ -602,15 +602,15 @@ int CameraSensorLegacy::initProperties() * Cache the Transform associated with the camera mounting * rotation for later use in computeTransform(). */ - bool success; - mountingOrientation_ = orientationFromRotation(propertyValue, &success); - if (!success) { + auto mountingOrientation = orientationFromRotation(propertyValue); + if (!mountingOrientation) { LOG(CameraSensor, Warning) << "Invalid rotation of " << propertyValue << " degrees - ignoring"; - mountingOrientation_ = Orientation::Rotate0; } + mountingOrientation_ = mountingOrientation.value_or(Orientation::Rotate0); + properties_.set(properties::Rotation, propertyValue); } else { LOG(CameraSensor, Warning) diff --git a/src/libcamera/sensor/camera_sensor_raw.cpp b/src/libcamera/sensor/camera_sensor_raw.cpp index ab75b1f82..3a069ef9d 100644 --- a/src/libcamera/sensor/camera_sensor_raw.cpp +++ b/src/libcamera/sensor/camera_sensor_raw.cpp @@ -607,15 +607,15 @@ int CameraSensorRaw::initProperties() * Cache the Transform associated with the camera mounting * rotation for later use in computeTransform(). */ - bool success; - mountingOrientation_ = orientationFromRotation(propertyValue, &success); - if (!success) { + auto mountingOrientation = orientationFromRotation(propertyValue); + if (!mountingOrientation) { LOG(CameraSensor, Warning) << "Invalid rotation of " << propertyValue << " degrees - ignoring"; - mountingOrientation_ = Orientation::Rotate0; } + mountingOrientation_ = mountingOrientation.value_or(Orientation::Rotate0); + properties_.set(properties::Rotation, propertyValue); } else { LOG(CameraSensor, Warning) diff --git a/src/libcamera/transform.cpp b/src/libcamera/transform.cpp index 9fe8b5620..6636b23e4 100644 --- a/src/libcamera/transform.cpp +++ b/src/libcamera/transform.cpp @@ -269,21 +269,15 @@ Transform operator-(Transform t) * \brief Return the transform 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 transform corresponding to the rotation if \a success was set to - * `true`, otherwise the `Identity` transform + * can be used to represent anticlockwise rotations. Must be a multiple of 90. + * \return The transform corresponding to the rotation */ -Transform transformFromRotation(int angle, bool *success) +std::optional transformFromRotation(int angle) { angle = angle % 360; if (angle < 0) angle += 360; - if (success != nullptr) - *success = true; - switch (angle) { case 0: return Transform::Identity; @@ -295,10 +289,7 @@ Transform transformFromRotation(int angle, bool *success) return Transform::Rot270; } - if (success != nullptr) - *success = false; - - return Transform::Identity; + return {}; } namespace { diff --git a/src/py/libcamera/py_transform.cpp b/src/py/libcamera/py_transform.cpp index 768260ffc..fc25d5a32 100644 --- a/src/py/libcamera/py_transform.cpp +++ b/src/py/libcamera/py_transform.cpp @@ -24,19 +24,18 @@ void init_py_transform(py::module &m) pyTransform .def(py::init([](int rotation, bool hflip, bool vflip, bool transpose) { - bool ok; - - Transform t = transformFromRotation(rotation, &ok); - if (!ok) + auto t = transformFromRotation(rotation); + if (!t) throw std::invalid_argument("Invalid rotation"); if (hflip) - t ^= Transform::HFlip; + *t ^= Transform::HFlip; if (vflip) - t ^= Transform::VFlip; + *t ^= Transform::VFlip; if (transpose) - t ^= Transform::Transpose; - return t; + *t ^= Transform::Transpose; + + return *t; }), py::arg("rotation") = 0, py::arg("hflip") = false, py::arg("vflip") = false, py::arg("transpose") = false) .def(py::init([](Transform &other) { return other; }))