/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2022, Tomi Valkeinen */ #include "py_helpers.h" #include #include #include #include namespace py = pybind11; using namespace libcamera; template static py::object valueOrTuple(const ControlValue &cv) { if (cv.isArray()) { const T *v = reinterpret_cast(cv.data().data()); auto t = py::tuple(cv.numElements()); for (size_t i = 0; i < cv.numElements(); ++i) t[i] = v[i]; return std::move(t); } return py::cast(cv.get()); } py::object controlValueToPy(const ControlValue &cv) { switch (cv.type()) { case ControlTypeBool: return valueOrTuple(cv); case ControlTypeByte: return valueOrTuple(cv); case ControlTypeInteger32: return valueOrTuple(cv); case ControlTypeInteger64: return valueOrTuple(cv); case ControlTypeFloat: return valueOrTuple(cv); case ControlTypeString: return py::cast(cv.get()); case ControlTypeRectangle: return valueOrTuple(cv); case ControlTypeSize: { const Size *v = reinterpret_cast(cv.data().data()); return py::cast(v); } case ControlTypeNone: return py::none(); default: throw std::runtime_error("Unsupported ControlValue type"); } } template static ControlValue controlValueMaybeArray(const py::object &ob) { if (py::isinstance(ob) || py::isinstance(ob)) { std::vector vec = ob.cast>(); return ControlValue(Span(vec)); } return ControlValue(ob.cast()); } ControlValue pyToControlValue(const py::object &ob, ControlType type) { switch (type) { case ControlTypeBool: return ControlValue(ob.cast()); case ControlTypeByte: return controlValueMaybeArray(ob); case ControlTypeInteger32: return controlValueMaybeArray(ob); case ControlTypeInteger64: return controlValueMaybeArray(ob); case ControlTypeFloat: return controlValueMaybeArray(ob); case ControlTypeString: return ControlValue(ob.cast()); case ControlTypeRectangle: return controlValueMaybeArray(ob); case ControlTypeSize: return ControlValue(ob.cast()); case ControlTypeNone: return ControlValue(); default: throw std::runtime_error("Control type not implemented"); } }