| 48 | } |
| 49 | |
| 50 | Tensor CustomCrop(Tensor &input, const NVCVRectI &rcCrop, std::optional<Stream> pstream) |
| 51 | { |
| 52 | auto info = nvcv::TensorLayoutInfoImage::Create(input.layout()); |
| 53 | if (!info) |
| 54 | { |
| 55 | throw std::invalid_argument("Non-supported tensor layout"); |
| 56 | } |
| 57 | |
| 58 | int iwidth = info->idxWidth(); |
| 59 | int iheight = info->idxHeight(); |
| 60 | |
| 61 | NVCV_ASSERT(iwidth >= 0 && "All images have width"); |
| 62 | |
| 63 | // If no height, we consider height==1, and this dimension can't be changed |
| 64 | // in order to keep the output layout the same as input's |
| 65 | if (iheight < 0 && rcCrop.height != 1) |
| 66 | { |
| 67 | throw std::invalid_argument("Non-supported tensor layout"); |
| 68 | } |
| 69 | |
| 70 | // Create the output shape based inputs, changing width/height to match rcCrop's size |
| 71 | nvcv::Shape shape = input.shape().shape(); |
| 72 | std::vector<int64_t> out_shape{shape.begin(), shape.end()}; |
| 73 | out_shape[iwidth] = rcCrop.width; |
| 74 | if (iheight >= 0) |
| 75 | { |
| 76 | out_shape[iheight] = rcCrop.height; |
| 77 | } |
| 78 | |
| 79 | Tensor output |
| 80 | = Tensor::Create({out_shape.data(), static_cast<int32_t>(out_shape.size()), input.layout()}, input.dtype()); |
| 81 | |
| 82 | return CustomCropInto(output, input, rcCrop, pstream); |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |