py: Use geometry classes

Now that we have proper geometry classes in the Python bindings, change
the existing bindings and the .py files accordingly.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Tomi Valkeinen 2022-05-18 16:13:25 +03:00 committed by Laurent Pinchart
parent e0a8517b27
commit 2bb6c9fbd2
5 changed files with 22 additions and 39 deletions

View file

@ -160,8 +160,11 @@ def configure(ctx):
for idx, stream_opts in enumerate(streams): for idx, stream_opts in enumerate(streams):
stream_config = camconfig.at(idx) stream_config = camconfig.at(idx)
if 'width' in stream_opts and 'height' in stream_opts: if 'width' in stream_opts:
stream_config.size = (stream_opts['width'], stream_opts['height']) stream_config.size.width = stream_opts['width']
if 'height' in stream_opts:
stream_config.size.height = stream_opts['height']
if 'pixelformat' in stream_opts: if 'pixelformat' in stream_opts:
stream_config.pixel_format = libcam.PixelFormat(stream_opts['pixelformat']) stream_config.pixel_format = libcam.PixelFormat(stream_opts['pixelformat'])

View file

@ -126,7 +126,8 @@ class KMSRenderer:
}) })
for fb in ctx['allocator'].buffers(stream): for fb in ctx['allocator'].buffers(stream):
w, h = cfg.size w = cfg.size.width
h = cfg.size.height
fds = [] fds = []
strides = [] strides = []
offsets = [] offsets = []

View file

@ -84,8 +84,8 @@ def demosaic(data, r0, g0, g1, b0):
def to_rgb(fmt, size, data): def to_rgb(fmt, size, data):
w = size[0] w = size.width
h = size[1] h = size.height
fmt = str(fmt) fmt = str(fmt)
@ -292,7 +292,8 @@ class MainWindow(QtWidgets.QWidget):
def buf_to_qpixmap(self, stream, fb): def buf_to_qpixmap(self, stream, fb):
with fb.mmap() as mfb: with fb.mmap() as mfb:
cfg = stream.configuration cfg = stream.configuration
w, h = cfg.size w = cfg.size.width
h = cfg.size.height
pitch = cfg.stride pitch = cfg.stride
if str(cfg.pixel_format) == 'MJPEG': if str(cfg.pixel_format) == 'MJPEG':

View file

@ -268,7 +268,8 @@ class MainWindow(QtWidgets.QWidget):
def create_texture(self, stream, fb): def create_texture(self, stream, fb):
cfg = stream.configuration cfg = stream.configuration
fmt = cfg.pixel_format.fourcc fmt = cfg.pixel_format.fourcc
w, h = cfg.size w = cfg.size.width
h = cfg.size.height
attribs = [ attribs = [
EGL_WIDTH, w, EGL_WIDTH, w,

View file

@ -6,7 +6,6 @@
*/ */
/* /*
* \todo Add geometry classes (Point, Rectangle...)
* \todo Add bindings for the ControlInfo class * \todo Add bindings for the ControlInfo class
*/ */
@ -61,11 +60,11 @@ static py::object controlValueToPy(const ControlValue &cv)
return py::cast(cv.get<std::string>()); return py::cast(cv.get<std::string>());
case ControlTypeRectangle: { case ControlTypeRectangle: {
const Rectangle *v = reinterpret_cast<const Rectangle *>(cv.data().data()); const Rectangle *v = reinterpret_cast<const Rectangle *>(cv.data().data());
return py::make_tuple(v->x, v->y, v->width, v->height); return py::cast(v);
} }
case ControlTypeSize: { case ControlTypeSize: {
const Size *v = reinterpret_cast<const Size *>(cv.data().data()); const Size *v = reinterpret_cast<const Size *>(cv.data().data());
return py::make_tuple(v->width, v->height); return py::cast(v);
} }
case ControlTypeNone: case ControlTypeNone:
default: default:
@ -99,14 +98,10 @@ static ControlValue pyToControlValue(const py::object &ob, ControlType type)
return controlValueMaybeArray<float>(ob); return controlValueMaybeArray<float>(ob);
case ControlTypeString: case ControlTypeString:
return ControlValue(ob.cast<std::string>()); return ControlValue(ob.cast<std::string>());
case ControlTypeRectangle: { case ControlTypeRectangle:
auto array = ob.cast<std::array<int32_t, 4>>(); return ControlValue(ob.cast<Rectangle>());
return ControlValue(Rectangle(array[0], array[1], array[2], array[3])); case ControlTypeSize:
} return ControlValue(ob.cast<Size>());
case ControlTypeSize: {
auto array = ob.cast<std::array<int32_t, 2>>();
return ControlValue(Size(array[0], array[1]));
}
case ControlTypeNone: case ControlTypeNone:
default: default:
throw std::runtime_error("Control type not implemented"); throw std::runtime_error("Control type not implemented");
@ -397,15 +392,7 @@ PYBIND11_MODULE(_libcamera, m)
.def("__str__", &StreamConfiguration::toString) .def("__str__", &StreamConfiguration::toString)
.def_property_readonly("stream", &StreamConfiguration::stream, .def_property_readonly("stream", &StreamConfiguration::stream,
py::return_value_policy::reference_internal) py::return_value_policy::reference_internal)
.def_property( .def_readwrite("size", &StreamConfiguration::size)
"size",
[](StreamConfiguration &self) {
return std::make_tuple(self.size.width, self.size.height);
},
[](StreamConfiguration &self, std::tuple<uint32_t, uint32_t> size) {
self.size.width = std::get<0>(size);
self.size.height = std::get<1>(size);
})
.def_readwrite("pixel_format", &StreamConfiguration::pixelFormat) .def_readwrite("pixel_format", &StreamConfiguration::pixelFormat)
.def_readwrite("stride", &StreamConfiguration::stride) .def_readwrite("stride", &StreamConfiguration::stride)
.def_readwrite("frame_size", &StreamConfiguration::frameSize) .def_readwrite("frame_size", &StreamConfiguration::frameSize)
@ -416,18 +403,8 @@ PYBIND11_MODULE(_libcamera, m)
pyStreamFormats pyStreamFormats
.def_property_readonly("pixel_formats", &StreamFormats::pixelformats) .def_property_readonly("pixel_formats", &StreamFormats::pixelformats)
.def("sizes", [](StreamFormats &self, const PixelFormat &pixelFormat) { .def("sizes", &StreamFormats::sizes)
std::vector<std::tuple<uint32_t, uint32_t>> fmts; .def("range", &StreamFormats::range);
for (const auto &s : self.sizes(pixelFormat))
fmts.push_back(std::make_tuple(s.width, s.height));
return fmts;
})
.def("range", [](StreamFormats &self, const PixelFormat &pixelFormat) {
const auto &range = self.range(pixelFormat);
return make_tuple(std::make_tuple(range.hStep, range.vStep),
std::make_tuple(range.min.width, range.min.height),
std::make_tuple(range.max.width, range.max.height));
});
pyFrameBufferAllocator pyFrameBufferAllocator
.def(py::init<std::shared_ptr<Camera>>(), py::keep_alive<1, 2>()) .def(py::init<std::shared_ptr<Camera>>(), py::keep_alive<1, 2>())