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

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

5 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/wrappers/interpolation_wrapper.hpp Source File
interpolation_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 "core/detail/casting.hpp"
27 #include "operator_types.h"
28 
29 namespace roccv {
30 
41 template <typename T, eBorderType B, eInterpolationType I>
43  public:
51  InterpolationWrapper(const Tensor& tensor, T border_value) : m_desc(tensor, border_value) {}
52 
59  InterpolationWrapper(BorderWrapper<T, B> borderWrapper) : m_desc(borderWrapper) {}
60 
69  inline __device__ __host__ const T at(int64_t n, float h, float w, int64_t c) const {
70  if constexpr (I == eInterpolationType::INTERP_TYPE_NEAREST) {
71  // Nearest neighbor interpolation implementation
72  return m_desc.at(n, lroundf(h), lroundf(w), c);
73  } else if constexpr (I == eInterpolationType::INTERP_TYPE_LINEAR) {
74  // Bilinear interpolation implementation
75  // v1 -- v2
76  // - -
77  // v3 -- v4
78 
80 
81  int64_t x0 = static_cast<int64_t>(floorf(w));
82  int64_t x1 = x0 + 1;
83  int64_t y0 = static_cast<int64_t>(floorf(h));
84  int64_t y1 = y0 + 1;
85 
86  auto v1 = detail::RangeCast<WorkType>(m_desc.at(n, y0, x0, c));
87  auto v2 = detail::RangeCast<WorkType>(m_desc.at(n, y0, x1, c));
88  auto v3 = detail::RangeCast<WorkType>(m_desc.at(n, y1, x0, c));
89  auto v4 = detail::RangeCast<WorkType>(m_desc.at(n, y1, x1, c));
90 
91  auto q1 = v1 * (x1 - w) + v2 * (w - x0);
92  auto q2 = v3 * (x1 - w) + v4 * (w - x0);
93  auto q = q1 * (y1 - h) + q2 * (h - y0);
94 
95  return detail::RangeCast<T>(q);
96  }
97 
98  // TODO: Support other interpolation methods.
99 
100  else {
101  static_assert(false, "Provided interpolation type is not supported.");
102  }
103  }
104 
105  __device__ __host__ inline int64_t height() const { return m_desc.height(); }
106  __device__ __host__ inline int64_t width() const { return m_desc.width(); }
107  __device__ __host__ inline int64_t batches() const { return m_desc.batches(); }
108  __device__ __host__ inline int64_t channels() const { return m_desc.channels(); }
109 
110  private:
111  BorderWrapper<T, B> m_desc;
112 };
113 } // namespace roccv
__device__ __host__ int64_t batches() const
Retrieves the number of batches in the image tensor.
Definition: border_wrapper.hpp:151
__device__ __host__ int64_t width() const
Retrieves the width of the image.
Definition: border_wrapper.hpp:144
__device__ __host__ int64_t height() const
Retrives the height of the images.
Definition: border_wrapper.hpp:137
__device__ __host__ int64_t channels() const
Retries the number of channels in the image.
Definition: border_wrapper.hpp:158
__device__ __host__ const T at(int64_t n, int64_t h, int64_t w, int64_t c) const
Returns a reference to the underlying data given image coordinates. If the coordinates fall out of bo...
Definition: border_wrapper.hpp:69
A kernel-friendly wrapper which provides interpolation logic based on the given coordinates....
Definition: interpolation_wrapper.hpp:42
InterpolationWrapper(const Tensor &tensor, T border_value)
Wraps a roccv::Tensor in an InterpolationWrapper to provide pixel interpolation when accessing non-in...
Definition: interpolation_wrapper.hpp:51
__device__ __host__ const T at(int64_t n, float h, float w, int64_t c) const
Retrieves an interpolated value at given image batch coordinates.
Definition: interpolation_wrapper.hpp:69
__device__ __host__ int64_t width() const
Definition: interpolation_wrapper.hpp:106
__device__ __host__ int64_t channels() const
Definition: interpolation_wrapper.hpp:108
InterpolationWrapper(BorderWrapper< T, B > borderWrapper)
Wraps a BorderWrapper in an Interpolation wrapper. Extends capabilities to interpolate pixel values w...
Definition: interpolation_wrapper.hpp:59
__device__ __host__ int64_t batches() const
Definition: interpolation_wrapper.hpp:107
__device__ __host__ int64_t height() const
Definition: interpolation_wrapper.hpp:105
Definition: tensor.hpp:37
MakeType_t< T, C >::type MakeType
Definition: type_traits.hpp:97
Definition: strided_data_wrap.hpp:33
@ INTERP_TYPE_LINEAR
Definition: operator_types.h:32
@ INTERP_TYPE_NEAREST
Definition: operator_types.h:31
This header defines common arithmetic operators for HIP vectorized types.