|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "crop.h" |
| 19 | +#include <gxf/std/tensor.hpp> |
| 20 | + |
| 21 | +namespace holoscan::ops { |
| 22 | + |
| 23 | +void CropOp::setup(OperatorSpec& spec) { |
| 24 | + spec.input<holoscan::gxf::Entity>("input"); |
| 25 | + spec.output<holoscan::gxf::Entity>("output"); |
| 26 | + spec.param(x_, "x", "top left x", "top left x coordinate", 0); |
| 27 | + spec.param(y_, "y", "top left y", "top left y coordinate", 0); |
| 28 | + spec.param(width_, "width", "width", "width", 0); |
| 29 | + spec.param(height_, "height", "height", "height", 0); |
| 30 | +} |
| 31 | + |
| 32 | +void CropOp::compute(InputContext& op_input, OutputContext& op_output, ExecutionContext& context) { |
| 33 | + auto maybe_tensormap = op_input.receive<holoscan::TensorMap>("input"); |
| 34 | + const auto tensormap = maybe_tensormap.value(); |
| 35 | + |
| 36 | + if (tensormap.size() != 1) { throw std::runtime_error("Expecting single tensor input"); } |
| 37 | + |
| 38 | + auto tensor = tensormap.begin()->second; |
| 39 | + int orig_height = tensor->shape()[0]; |
| 40 | + int orig_width = tensor->shape()[1]; |
| 41 | + int nChannels = tensor->shape()[2]; |
| 42 | + |
| 43 | + nvidia::gxf::Tensor tensor_gxf(tensor->dl_ctx()); |
| 44 | + nvidia::gxf::PrimitiveType data_type = tensor_gxf.element_type(); |
| 45 | + int element_size = nvidia::gxf::PrimitiveTypeSize(data_type); |
| 46 | + |
| 47 | + if (x_ < 0 || y_ < 0 || width_ <= 0 || height_ <= 0) { |
| 48 | + throw std::runtime_error("Invalid crop dimensions"); |
| 49 | + } |
| 50 | + |
| 51 | + if ((x_ + width_) > orig_width || (y_ + height_) > orig_height) { |
| 52 | + throw std::runtime_error("Crop exceeds image boundaries"); |
| 53 | + } |
| 54 | + |
| 55 | + auto pointer = std::shared_ptr<void*>(new void*, [](void** pointer) { |
| 56 | + if (pointer != nullptr) { |
| 57 | + if (*pointer != nullptr) { cudaFree(*pointer); } |
| 58 | + delete pointer; |
| 59 | + } |
| 60 | + }); |
| 61 | + cudaMalloc(pointer.get(), width_ * height_ * element_size * nChannels); |
| 62 | + |
| 63 | + nvidia::gxf::Shape shape = nvidia::gxf::Shape{height_, width_, nChannels}; |
| 64 | + cudaMemcpy2D(*pointer, |
| 65 | + width_ * element_size * nChannels, |
| 66 | + static_cast<void*>((char*)tensor->data() + x_ * element_size * nChannels), |
| 67 | + orig_width * element_size * nChannels, |
| 68 | + width_ * element_size * nChannels, |
| 69 | + height_, |
| 70 | + cudaMemcpyDeviceToDevice); |
| 71 | + |
| 72 | + auto out_message = nvidia::gxf::Entity::New(context.context()); |
| 73 | + auto gxf_tensor = out_message.value().add<nvidia::gxf::Tensor>(""); |
| 74 | + |
| 75 | + gxf_tensor.value()->wrapMemory(shape, |
| 76 | + data_type, |
| 77 | + element_size, |
| 78 | + nvidia::gxf::ComputeTrivialStrides(shape, element_size), |
| 79 | + nvidia::gxf::MemoryStorageType::kDevice, |
| 80 | + *pointer, |
| 81 | + [orig_pointer = pointer](void*) mutable { |
| 82 | + orig_pointer.reset(); // decrement ref count |
| 83 | + return nvidia::gxf::Success; |
| 84 | + }); |
| 85 | + |
| 86 | + op_output.emit(out_message.value(), "output"); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace holoscan::ops |
0 commit comments