/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/image_wrapper.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/image_wrapper.hpp Source File#

7 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/image_wrapper.hpp Source File
image_wrapper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #pragma once
23 
24 #include <hip/hip_runtime.h>
25 
26 #include <vector>
27 
29 #include "core/tensor.hpp"
30 
31 namespace roccv {
32 
39 template <typename T>
40 class ImageWrapper {
41  public:
42  using ValueType = T;
44 
50  ImageWrapper(const Tensor& tensor) {
51  if (tensor.layout() != eTensorLayout::TENSOR_LAYOUT_NHWC &&
54  throw Exception("The given tensor layout is not supported for ImageWrapper", eStatusType::NOT_IMPLEMENTED);
55  }
56 
57  // Copy tensor data into image tensor descriptor
59  ImageShape indexes = {tensor.layout().batch_index(), tensor.layout().height_index(),
60  tensor.layout().width_index(), tensor.layout().channels_index()};
61 
62  // Handle HWC layout, which doesn't have shapes/strides for the batch dimension. We set the batch shape to 1 and
63  // the strides to 0.
64  int64_t num_batches = indexes.n < 0 ? 1 : tensor.shape(indexes.n);
65  int64_t batch_stride = indexes.n < 0 ? 0 : tdata.stride(indexes.n);
66 
67  shape = {num_batches, tdata.shape(indexes.h), tdata.shape(indexes.w), tdata.shape(indexes.c)};
68  stride = {batch_stride, tdata.stride(indexes.h), tdata.stride(indexes.w), tdata.stride(indexes.c)};
69  data = static_cast<unsigned char*>(tdata.basePtr());
70  }
71 
80  ImageWrapper(std::vector<BaseType>& input, int32_t batchSize, int32_t width, int32_t height) {
81  // Calculate strides based on input (byte-wise strides)
82  stride.c = sizeof(BaseType);
83  stride.w = stride.c * detail::NumElements<T>;
84  stride.h = stride.w * width;
85  stride.n = stride.h * height;
86 
87  // Copy shape information
88  shape.c = detail::NumElements<T>;
89  shape.w = width;
90  shape.h = height;
91  shape.n = batchSize;
92 
93  // Copy data pointer from input vector
94  data = reinterpret_cast<unsigned char*>(input.data());
95  }
96 
105  ImageWrapper(void* input, int32_t batchSize, int32_t width, int32_t height) {
106  // Calculate strides based on input (byte-wise strides)
107  stride.c = sizeof(BaseType);
108  stride.w = stride.c * detail::NumElements<T>;
109  stride.h = stride.w * width;
110  stride.n = stride.h * height;
111 
112  // Copy shape information
113  shape.c = detail::NumElements<T>;
114  shape.w = width;
115  shape.h = height;
116  shape.n = batchSize;
117 
118  data = reinterpret_cast<unsigned char*>(input);
119  }
120 
130  __device__ __host__ T& at(int64_t n, int64_t h, int64_t w, int64_t c) {
131  return *(reinterpret_cast<T*>(data + (stride.n * n) + (stride.h * h) + (stride.w * w) + (stride.c * c)));
132  }
133 
134  __device__ __host__ const T at(int64_t n, int64_t h, int64_t w, int64_t c) const {
135  return *(reinterpret_cast<T*>(data + (stride.n * n) + (stride.h * h) + (stride.w * w) + (stride.c * c)));
136  }
137 
143  __device__ __host__ inline int64_t height() const { return shape.h; }
144 
150  __device__ __host__ inline int64_t width() const { return shape.w; }
151 
157  __device__ __host__ inline int64_t batches() const { return shape.n; }
158 
164  __device__ __host__ inline int64_t channels() const { return shape.c; }
165 
166  private:
167  struct ImageShape {
168  int64_t n, h, w, c;
169  };
170 
171  ImageShape shape;
172 
181  ImageShape stride;
182 
183  unsigned char* data;
184 };
185 } // namespace roccv
Definition: exception.hpp:31
ImageWrapper is a non-owning wrapper for roccv::Tensors with a NHWC/NCHW/HWC layout....
Definition: image_wrapper.hpp:40
__device__ __host__ T & at(int64_t n, int64_t h, int64_t w, int64_t c)
Returns a reference to data given coordinates within an image tensor.
Definition: image_wrapper.hpp:130
ImageWrapper(void *input, int32_t batchSize, int32_t width, int32_t height)
Creates an ImageWrapper from a pointer.
Definition: image_wrapper.hpp:105
T ValueType
Definition: image_wrapper.hpp:42
__device__ __host__ int64_t height() const
Retrives the height of the images.
Definition: image_wrapper.hpp:143
__device__ __host__ int64_t batches() const
Retrieves the number of batches in the image tensor.
Definition: image_wrapper.hpp:157
detail::BaseType< T > BaseType
Definition: image_wrapper.hpp:43
__device__ __host__ int64_t width() const
Retrieves the width of the image.
Definition: image_wrapper.hpp:150
__device__ __host__ int64_t channels() const
Retries the number of channels in the image.
Definition: image_wrapper.hpp:164
__device__ __host__ const T at(int64_t n, int64_t h, int64_t w, int64_t c) const
Definition: image_wrapper.hpp:134
ImageWrapper(const Tensor &tensor)
Creates an ImageWrapper from a Tensor.
Definition: image_wrapper.hpp:50
ImageWrapper(std::vector< BaseType > &input, int32_t batchSize, int32_t width, int32_t height)
Creates an ImageWrapper from a vector.
Definition: image_wrapper.hpp:80
virtual void * basePtr() const
Returns the base pointer of the tensor data in memory.
virtual const TensorShape & shape() const &
Returns the shape of the tensor.
Holds the underlying tensor data alongside tensor metadata. This particular tensor data type is used ...
Definition: tensor_data.hpp:114
const int64_t stride(int d) const
Returns the stride at a given dimension.
Definition: tensor.hpp:37
TensorData exportData() const
Exports the tensor data of the tensor.
const TensorLayout & layout() const
Returns the layout of the tensor.
const TensorShape & shape() const
Returns the shape of the tensor.
int32_t batch_index() const
Index of the batch dimension specified by layout. E.g. returns 0 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:111
int32_t width_index() const
Index of the width dimension specified by layout. E.g. returns 2 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:125
int32_t height_index() const
Index of the height dimension specified by layout. E.g. returns 1 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:118
int32_t channels_index() const
Index of the channels dimension specified by layout. E.g. returns 3 for TENSOR_LAYOUT_NHWC.
Definition: tensor_layout.hpp:132
typename TypeTraits< T >::base_type BaseType
Returns the base type of a given HIP vectorized type.
Definition: type_traits.hpp:117
Definition: strided_data_wrap.hpp:33
@ NOT_IMPLEMENTED
invalid permutation of parameters.
@ TENSOR_LAYOUT_HWC
Definition: util_enums.h:40
@ TENSOR_LAYOUT_NCHW
Definition: util_enums.h:50
@ TENSOR_LAYOUT_NHWC
Definition: util_enums.h:39