71 uint32_t oSize = buffers_.size();
73 buff->setFrame(shared_from_this());
74 buffers_.push_back(buff);
76 return (buffers_.begin() + oSize);
80ris::Frame::BufferIterator ris::Frame::appendFrame(
ris::FramePtr frame) {
81 uint32_t oSize = buffers_.size();
83 for (ris::Frame::BufferIterator it = frame->beginBuffer(); it != frame->endBuffer(); ++it) {
84 (*it)->setFrame(shared_from_this());
85 buffers_.push_back(*it);
89 return (buffers_.begin() + oSize);
175void ris::Frame::setPayload(uint32_t pSize) {
176 ris::Frame::BufferIterator it;
183 for (it = buffers_.begin(); it != buffers_.end(); ++it) {
184 loc = (*it)->getSize();
189 (*it)->setPayloadEmpty();
192 }
else if (lSize <= loc) {
193 (*it)->setPayload(lSize);
199 (*it)->setPayloadFull();
205 "Attempt to set payload to size %" PRIu32
" in frame with size %" PRIu32,
237void ris::Frame::setPayloadFull() {
238 ris::Frame::BufferIterator it;
243 for (it = buffers_.begin(); it != buffers_.end(); ++it) {
244 (*it)->setPayloadFull();
246 payload_ += (*it)->getPayload();
247 size_ += (*it)->getSize();
253void ris::Frame::setPayloadEmpty() {
254 ris::Frame::BufferIterator it;
259 for (it = buffers_.begin(); it != buffers_.end(); ++it) {
260 (*it)->setPayloadEmpty();
262 payload_ += (*it)->getPayload();
263 size_ += (*it)->getSize();
354void ris::Frame::readPy(boost::python::object p, uint32_t offset) {
357 if (PyObject_GetBuffer(p.ptr(), &pyBuf, PyBUF_CONTIG) < 0)
360 uint32_t size = getPayload();
361 uint32_t count = pyBuf.len;
363 if ((offset + count) > size) {
364 PyBuffer_Release(&pyBuf);
366 "Attempt to read %" PRIu32
" bytes from frame at offset %" PRIu32
367 " with size %" PRIu32,
374 ris::fromFrame(beg, count,
reinterpret_cast<uint8_t*
>(pyBuf.buf));
375 PyBuffer_Release(&pyBuf);
379bp::object ris::Frame::getBytearrayPy(uint32_t offset, uint32_t count) {
381 uint32_t size = getPayload();
384 count = size - offset;
388 bp::object byteArray(bp::handle<>(PyByteArray_FromStringAndSize(
nullptr, count)));
391 this->readPy(byteArray, offset);
397bp::object ris::Frame::getMemoryviewPy() {
399 uint32_t size = getPayload();
402 bp::object byteArray(bp::handle<>(PyByteArray_FromStringAndSize(
nullptr, size)));
404 this->readPy(byteArray, 0);
407 PyObject* memoryView = PyMemoryView_FromObject(byteArray.ptr());
413 return bp::object(bp::handle<>(memoryView));
417void ris::Frame::writePy(boost::python::object p, uint32_t offset) {
420 if (PyObject_GetBuffer(p.ptr(), &pyBuf, PyBUF_SIMPLE) < 0)
423 uint32_t size = getSize();
424 uint32_t count = pyBuf.len;
426 if ((offset + count) > size) {
427 PyBuffer_Release(&pyBuf);
429 "Attempt to write %" PRIu32
" bytes to frame at offset %" PRIu32
430 " with size %" PRIu32,
436 minPayload(offset + count);
438 ris::toFrame(beg, count,
reinterpret_cast<uint8_t*
>(pyBuf.buf));
439 PyBuffer_Release(&pyBuf);
446boost::python::object ris::Frame::getNumpy(uint32_t offset, uint32_t count) {
448 const npy_intp size_bytes = getPayload();
452 if (offset > size_bytes) {
455 "Offset %" PRIu32
" is past end of frame (size %" PRIuPTR
")",
456 offset,
static_cast<uintptr_t
>(size_bytes)));
458 count =
static_cast<uint32_t
>(size_bytes - offset);
462 if ((
static_cast<npy_intp
>(offset) +
static_cast<npy_intp
>(count)) > size_bytes) {
465 "Attempt to read %" PRIu32
" bytes from frame at offset %" PRIu32
466 " with size %" PRIuPTR,
467 count, offset,
static_cast<uintptr_t
>(size_bytes)));
471 npy_intp dims[1] = {
static_cast<npy_intp
>(count) };
472 PyObject* obj = PyArray_SimpleNew(1, dims, NPY_UINT8);
475 "Failed to allocate NumPy uint8 array."));
479 auto* arr =
reinterpret_cast<PyArrayObject*
>(obj);
480 auto* dst =
reinterpret_cast<uint8_t*
>(PyArray_DATA(arr));
486 return boost::python::object(boost::python::handle<>(obj));
492void ris::Frame::putNumpy(boost::python::object p, uint32_t offset) {
494 PyObject* obj = p.ptr();
497 if (!PyArray_Check(obj)) {
504 PyArrayObject* arr =
reinterpret_cast<decltype(arr)
>(obj);
505 int flags = PyArray_FLAGS(arr);
506 bool ctg = flags & (NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
508 arr = PyArray_GETCONTIGUOUS(arr);
512 uint32_t size = getSize();
513 uint32_t count = PyArray_NBYTES(arr);
514 uint32_t end = offset + count;
519 "Attempt to write %" PRIu32
" bytes to frame at offset %" PRIu32
520 " with size %" PRIu32,
526 uint8_t* src =
reinterpret_cast<uint8_t*
>(PyArray_DATA(arr));
544void ris::Frame::setup_python() {
548 PyObject* dtype_uint8 =
reinterpret_cast<PyObject*
>(PyArray_DescrFromType(NPY_UINT8));
554 bp::class_<ris::Frame, ris::FramePtr, boost::noncopyable>(
"Frame", bp::no_init)
555 .def(
"lock", &ris::Frame::lock)
556 .def(
"getSize", &ris::Frame::getSize)
557 .def(
"getAvailable", &ris::Frame::getAvailable)
558 .def(
"getPayload", &ris::Frame::getPayload)
559 .def(
"read", &ris::Frame::readPy, (bp::arg(
"offset") = 0))
560 .def(
"getBa", &ris::Frame::getBytearrayPy, (bp::arg(
"offset") = 0, bp::arg(
"count") = 0))
561 .def(
"getMemoryview", &ris::Frame::getMemoryviewPy)
562 .def(
"write", &ris::Frame::writePy, (bp::arg(
"offset") = 0))
563 .def(
"setError", &ris::Frame::setError)
564 .def(
"getError", &ris::Frame::getError)
565 .def(
"setFlags", &ris::Frame::setFlags)
566 .def(
"getFlags", &ris::Frame::getFlags)
567 .def(
"setFirstUser", &ris::Frame::setFirstUser)
568 .def(
"getFirstUser", &ris::Frame::getFirstUser)
569 .def(
"setLastUser", &ris::Frame::setLastUser)
570 .def(
"getLastUser", &ris::Frame::getLastUser)
571 .def(
"setChannel", &ris::Frame::setChannel)
572 .def(
"getChannel", &ris::Frame::getChannel)
574 &ris::Frame::getNumpy,
575 (bp::arg(
"offset") = 0,
576 bp::arg(
"count") = 0))
578 &ris::Frame::putNumpy,
579 (bp::arg(
"offset") = 0))
580 .def(
"_debug", &ris::Frame::debug);