| 762 | namespace { |
| 763 | |
| 764 | std::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) |
no test coverage detected