MCPcopy Create free account
hub / github.com/CVCUDA/CV-CUDA / ToPyBufferInfo

Function ToPyBufferInfo

python/mod_cvcuda/nvcv/Image.cpp:764–959  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

762namespace {
763
764std::vector<std::pair<py::buffer_info, nvcv::TensorLayout>> ToPyBufferInfo(const nvcv::ImageDataStrided &imgData,
765 std::optional<nvcv::TensorLayout> userLayout)
766{
767 if (imgData.numPlanes() < 1)
768 {
769 return {};
770 }
771
772 const nvcv::ImagePlaneStrided &firstPlane = imgData.plane(0);
773
774 std::optional<nvcv::TensorLayoutInfoImage> infoLayout;
775 if (userLayout)
776 {
777 if (auto tmp = nvcv::TensorLayoutInfoImage::Create(*userLayout))
778 {
779 infoLayout.emplace(std::move(*tmp));
780 }
781 else
782 {
783 throw std::runtime_error("Layout can't represent the planar images needed");
784 }
785 }
786
787 bool singleBuffer = true;
788
789 // Let's check if we can return only one buffer, depending
790 // on the planes dimensions, pitch and data type.
791 for (int p = 1; p < imgData.numPlanes(); ++p)
792 {
793 const nvcv::ImagePlaneStrided &plane = imgData.plane(p);
794
795 if (plane.width != firstPlane.width || plane.height != firstPlane.height
796 || plane.rowStride != firstPlane.rowStride || imgData.format().planeDataType(0).numChannels() >= 2
797 || imgData.format().planeDataType(0) != imgData.format().planeDataType(p))
798 {
799 singleBuffer = false;
800 break;
801 }
802
803 // check if using the same plane pitch
804 if (p >= 2)
805 {
806 intptr_t goldPlaneStrided = imgData.plane(1).basePtr - imgData.plane(0).basePtr;
807 intptr_t curPlaneStrided = imgData.plane(p).basePtr - imgData.plane(p - 1).basePtr;
808 if (curPlaneStrided != goldPlaneStrided)
809 {
810 singleBuffer = false;
811 break;
812 }
813 }
814 }
815
816 std::vector<std::pair<py::buffer_info, nvcv::TensorLayout>> out;
817
818 // If not using a single buffer, we'll forcibly use one buffer per plane.
819 int numBuffers = singleBuffer ? 1 : imgData.numPlanes();
820
821 for (int p = 0; p < numBuffers; ++p)

Callers 2

ToPythonFunction · 0.85
cpuMethod · 0.85

Calls 15

castFunction · 0.85
FormatStringFunction · 0.85
make_tupleFunction · 0.85
findMethod · 0.80
numPlanesMethod · 0.45
planeMethod · 0.45
emplaceMethod · 0.45
numChannelsMethod · 0.45
planeDataTypeMethod · 0.45
formatMethod · 0.45
planeNumChannelsMethod · 0.45
strideBytesMethod · 0.45

Tested by

no test coverage detected