From patchwork Tue May 17 14:33:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomi Valkeinen X-Patchwork-Id: 15936 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 AB43CC3256 for ; Tue, 17 May 2022 14:34:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 25D5B65663; Tue, 17 May 2022 16:34:04 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1652798044; bh=lJxPhYu1Q9PAKcijXta3z7JGTZfAb86TDDJn4rPYuAQ=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=P2DaFzIKuhEUndxG6t97BD+wT/dw4zV8HigttWcqK7xMpwjAGUeNR6FHxt5hmRJgW rWKS1X7IlCwrPS7OhDECh8VIZFnekmu4dENAUAJXQPyFMVfz3C4QteLH3gLT4y8yuL GsSvDqHKF88RE3CfnAXXiuqtKApngDh3ocYBijhmW7PjoiBVwQkWUsoB9klYrGn9S9 dEZKjqlNgu1g1xRYprECqSo8qH417dsE7oDMacDVR8bH79aoCOxNBkpP6/34U64QXM QVttsuOO/a3rJ7AnNIa71zMoEJLfs4IfTDNHP+nveNp1479K5M/MlByr2h+GiQbJzF crjU9jzNrNfTQ== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id D314265676 for ; Tue, 17 May 2022 16:33:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="YD0Wxpyp"; dkim-atps=neutral Received: from deskari.lan (91-156-85-209.elisa-laajakaista.fi [91.156.85.209]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 3E1B74A8; Tue, 17 May 2022 16:33:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1652798037; bh=lJxPhYu1Q9PAKcijXta3z7JGTZfAb86TDDJn4rPYuAQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YD0WxpypdNVjfaWqdwkRE6lVbNCWD+r2AP0GDXf9Jhm1QTHWdMy7/UtMeV8y8DCBx bEuGn3IKuEB+PdjEaxV3rBYHysXZlQ8jtoFigS6PdM2KgoP2UPdnnKPSJBOCzoFPDo abu7S9hb/b9A2PfO8UZ9q4qNtGrpwItuc7fo7s6A= To: libcamera-devel@lists.libcamera.org, David Plowman , Kieran Bingham , Laurent Pinchart , Jacopo Mondi Date: Tue, 17 May 2022 17:33:24 +0300 Message-Id: <20220517143325.71784-13-tomi.valkeinen@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220517143325.71784-1-tomi.valkeinen@ideasonboard.com> References: <20220517143325.71784-1-tomi.valkeinen@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 12/13] py: add geometry classes 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: , X-Patchwork-Original-From: Tomi Valkeinen via libcamera-devel From: Tomi Valkeinen Reply-To: Tomi Valkeinen Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add libcamera's geometry classes to the Python bindings. Note that this commit only adds the classes, but they are not used anywhere yet. Signed-off-by: Tomi Valkeinen Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/py/libcamera/meson.build | 1 + src/py/libcamera/pygeometry.cpp | 119 ++++++++++++++++++++++++++++++++ src/py/libcamera/pymain.cpp | 2 + 3 files changed, 122 insertions(+) create mode 100644 src/py/libcamera/pygeometry.cpp diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build index 32e74504..0bef52c7 100644 --- a/src/py/libcamera/meson.build +++ b/src/py/libcamera/meson.build @@ -14,6 +14,7 @@ pybind11_dep = pybind11_proj.get_variable('pybind11_dep') pycamera_sources = files([ 'pyenums.cpp', + 'pygeometry.cpp', 'pymain.cpp', ]) diff --git a/src/py/libcamera/pygeometry.cpp b/src/py/libcamera/pygeometry.cpp new file mode 100644 index 00000000..d77de144 --- /dev/null +++ b/src/py/libcamera/pygeometry.cpp @@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2022, Tomi Valkeinen + * + * Python bindings - Geometry classes + */ + +#include + +#include +#include + +#include +#include +#include + +namespace py = pybind11; + +using namespace libcamera; + +void init_pygeometry(py::module &m) +{ + auto pyPoint = py::class_(m, "Point"); + auto pySize = py::class_(m, "Size"); + auto pySizeRange = py::class_(m, "SizeRange"); + auto pyRectangle = py::class_(m, "Rectangle"); + + pyPoint + .def(py::init<>()) + .def(py::init()) + .def_readwrite("x", &Point::x) + .def_readwrite("y", &Point::y) + .def(py::self == py::self) + .def(-py::self) + .def("__str__", &Point::toString) + .def("__repr__", [](const Point &self) { + return py::str("libcamera.Point({}, {})") + .format(self.x, self.y); + }); + + pySize + .def(py::init<>()) + .def(py::init()) + .def_readwrite("width", &Size::width) + .def_readwrite("height", &Size::height) + .def_property_readonly("is_null", &Size::isNull) + .def("align_down_to", &Size::alignDownTo) + .def("align_up_to", &Size::alignUpTo) + .def("bound_to", &Size::boundTo) + .def("expand_to", &Size::expandTo) + .def("grow_by", &Size::growBy) + .def("shrink_by", &Size::shrinkBy) + .def("aligned_up_to", &Size::alignedUpTo) + .def("aligned_up_to", &Size::alignedUpTo) + .def("bounded_to", &Size::boundedTo) + .def("expanded_to", &Size::expandedTo) + .def("grown_by", &Size::grownBy) + .def("shrunk_by", &Size::shrunkBy) + .def("bounded_to_aspect_ratio", &Size::boundedToAspectRatio) + .def("expanded_to_aspect_ratio", &Size::expandedToAspectRatio) + .def("centered_to", &Size::centeredTo) + .def(py::self == py::self) + .def(py::self < py::self) + .def(py::self <= py::self) + .def(py::self * float()) + .def(py::self / float()) + .def(py::self *= float()) + .def(py::self /= float()) + .def("__str__", &Size::toString) + .def("__repr__", [](const Size &self) { + return py::str("libcamera.Size({}, {})") + .format(self.width, self.height); + }); + + pySizeRange + .def(py::init<>()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def_readwrite("min", &SizeRange::min) + .def_readwrite("max", &SizeRange::max) + .def_readwrite("hStep", &SizeRange::hStep) + .def_readwrite("vStep", &SizeRange::vStep) + .def("contains", &SizeRange::contains) + .def(py::self == py::self) + .def("__str__", &SizeRange::toString) + .def("__repr__", [](const SizeRange &self) { + return py::str("libcamera.SizeRange(({}, {}), ({}, {}), {}, {})") + .format(self.min.width, self.min.height, + self.max.width, self.max.height, + self.hStep, self.vStep); + }); + + pyRectangle + .def(py::init<>()) + .def(py::init()) + .def(py::init()) + .def(py::init()) + .def_readwrite("x", &Rectangle::x) + .def_readwrite("y", &Rectangle::y) + .def_readwrite("width", &Rectangle::width) + .def_readwrite("height", &Rectangle::height) + .def_property_readonly("is_null", &Rectangle::isNull) + .def_property_readonly("center", &Rectangle::center) + .def_property_readonly("size", &Rectangle::size) + .def_property_readonly("topLeft", &Rectangle::topLeft) + .def("scale_by", &Rectangle::scaleBy) + .def("translate_by", &Rectangle::translateBy) + .def("bounded_to", &Rectangle::boundedTo) + .def("enclosed_in", &Rectangle::enclosedIn) + .def("scaled_by", &Rectangle::scaledBy) + .def("translated_by", &Rectangle::translatedBy) + .def(py::self == py::self) + .def("__str__", &Rectangle::toString) + .def("__repr__", [](const Rectangle &self) { + return py::str("libcamera.Rectangle({}, {}, {}, {})") + .format(self.x, self.y, self.width, self.height); + }); +} diff --git a/src/py/libcamera/pymain.cpp b/src/py/libcamera/pymain.cpp index 97b05903..96333ebc 100644 --- a/src/py/libcamera/pymain.cpp +++ b/src/py/libcamera/pymain.cpp @@ -137,11 +137,13 @@ static void handleRequestCompleted(Request *req) void init_pyenums(py::module &m); void init_pyenums_generated(py::module &m); +void init_pygeometry(py::module &m); PYBIND11_MODULE(_libcamera, m) { init_pyenums(m); init_pyenums_generated(m); + init_pygeometry(m); /* Forward declarations */