From patchwork Mon Mar 4 15:58:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 19626 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 6183CBD160 for ; Mon, 4 Mar 2024 15:59:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id AEA2162871; Mon, 4 Mar 2024 16:59:52 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UPhvwLJP"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5E688627FC for ; Mon, 4 Mar 2024 16:59:51 +0100 (CET) Received: from jasper.fritz.box (unknown [IPv6:2a00:6020:448c:6c00:560e:6da1:ef9c:32d0]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D95B5BD1; Mon, 4 Mar 2024 16:59:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1709567975; bh=SglA9aiI2Su6hhoph3V88Ysn4XsICmsO7oEeQxlAmKo=; h=From:To:Cc:Subject:Date:From; b=UPhvwLJPUoBFx28nYCNTS1kyqtsUmFVrDM6SWSD3+iPnl7gD8KknNKW3pgY/L5/S3 YeJ5nGL5J0Er2bXmQ4p+JjwI3PgnhBpcGBXhNNA/fraVEu5eWsfltWazpQ70HipoOG 5fLj2i08GO/AqTow3zyz1Sm5gVsSwjVEHBjNvr9o= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2] libcamera: controls: Add overwriteExisting parameter to ControlList::merge Date: Mon, 4 Mar 2024 16:58:53 +0100 Message-Id: <20240304155852.105079-1-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.40.1 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" This is useful in many cases although not included in the stl. Signed-off-by: Stefan Klug Reviewed-by: Umang Jain --- This version adds a unittest and fixes a doxygen doc. include/libcamera/controls.h | 2 +- src/libcamera/controls.cpp | 9 ++++-- test/controls/control_list.cpp | 50 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/include/libcamera/controls.h b/include/libcamera/controls.h index cf942055..0bf778d8 100644 --- a/include/libcamera/controls.h +++ b/include/libcamera/controls.h @@ -368,7 +368,7 @@ public: std::size_t size() const { return controls_.size(); } void clear() { controls_.clear(); } - void merge(const ControlList &source); + void merge(const ControlList &source, bool overwriteExisting = false); bool contains(unsigned int id) const; diff --git a/src/libcamera/controls.cpp b/src/libcamera/controls.cpp index b808116c..93ce4efe 100644 --- a/src/libcamera/controls.cpp +++ b/src/libcamera/controls.cpp @@ -910,10 +910,13 @@ ControlList::ControlList(const ControlInfoMap &infoMap, /** * \brief Merge the \a source into the ControlList * \param[in] source The ControlList to merge into this object + * \param[in] overwriteExisting Control if existing elements in *this shall be + * overwritten * * Merging two control lists copies elements from the \a source and inserts * them in *this. If the \a source contains elements whose key is already - * present in *this, then those elements are not overwritten. + * present in *this, then those elements are only overwritten if + * \a overwriteExisting is true. * * Only control lists created from the same ControlIdMap or ControlInfoMap may * be merged. Attempting to do otherwise results in undefined behaviour. @@ -921,7 +924,7 @@ ControlList::ControlList(const ControlInfoMap &infoMap, * \todo Reimplement or implement an overloaded version which internally uses * std::unordered_map::merge() and accepts a non-const argument. */ -void ControlList::merge(const ControlList &source) +void ControlList::merge(const ControlList &source, bool overwriteExisting) { /** * \todo ASSERT that the current and source ControlList are derived @@ -936,7 +939,7 @@ void ControlList::merge(const ControlList &source) */ for (const auto &ctrl : source) { - if (contains(ctrl.first)) { + if (!overwriteExisting && contains(ctrl.first)) { const ControlId *id = idmap_->at(ctrl.first); LOG(Controls, Warning) << "Control " << id->name() << " not overwritten"; diff --git a/test/controls/control_list.cpp b/test/controls/control_list.cpp index c03f230e..304cabfc 100644 --- a/test/controls/control_list.cpp +++ b/test/controls/control_list.cpp @@ -196,6 +196,56 @@ protected: return TestFail; } + /* + * Create two lists with overlapping controls. Merge them with + * overwriteExisting = true, verifying that the existing control + * values *get* overwritten. + */ + mergeList.clear(); + mergeList.set(controls::Brightness, 0.7f); + mergeList.set(controls::Saturation, 0.4f); + + list.clear(); + list.set(controls::Brightness, 0.5f); + list.set(controls::Contrast, 1.1f); + + mergeList.merge(list, true); + if (mergeList.size() != 3) { + cout << "Merged list should contain three elements" << endl; + return TestFail; + } + + if (list.size() != 2) { + cout << "The list to merge should contain two elements" + << endl; + return TestFail; + } + + if (!mergeList.get(controls::Brightness) || + !mergeList.get(controls::Contrast) || + !mergeList.get(controls::Saturation)) { + cout << "Merged list does not contain all controls" << endl; + return TestFail; + } + + if (mergeList.get(controls::Brightness) != 0.5f) { + cout << "Brightness control value did not change after merging lists" + << endl; + return TestFail; + } + + if (mergeList.get(controls::Contrast) != 1.1f) { + cout << "Contrast control value did not change after merging lists" + << endl; + return TestFail; + } + + if (mergeList.get(controls::Saturation) != 0.4f) { + cout << "Saturation control value changed after merging lists" + << endl; + return TestFail; + } + return TestPass; } };