/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/kernel_helpers.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/kernel_helpers.hpp Source File#

6 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/kernel_helpers.hpp Source File
kernel_helpers.hpp
Go to the documentation of this file.
1 
23 #pragma once
24 
25 #include <hip/hip_runtime.h>
26 #include <core/detail/casting.hpp>
28 #include "operator_types.h"
29 
30 using namespace roccv;
31 
32 namespace Kernels {
33 
34 static __host__ __device__ __forceinline__ bool pixel_in_box(float ix, float iy, float left, float right, float top, float bottom) {
35  return (ix > left) && (ix < right) && (iy > top) && (iy < bottom);
36 }
37 
38 template <typename T>
39 static __host__ __device__ void blend_single_color(T &color, const T &color_in) {
40  // Working type for internal pixel format, which is float and has 4 channels.
41  using WorkType = float4;
42  WorkType fgColor = detail::RangeCast<WorkType>(color_in);
43  WorkType bgColor = detail::RangeCast<WorkType>(color);
44  float fgAlpha = fgColor.w;
45  float bgAlpha = bgColor.w;
46  float blendAlpha = fgAlpha + bgAlpha * (1 - fgAlpha);
47 
48  WorkType blendColor = (fgColor * fgAlpha + bgColor * bgAlpha * (1 - fgAlpha)) / blendAlpha;
49  blendColor.w = blendAlpha;
50  color = detail::RangeCast<T>(blendColor);
51 }
52 
53 template <typename T>
54 inline static __host__ __device__ void shade_rectangle(const Rect_t &rect, int ix, int iy, T *out_color) {
55  if (rect.bordered) {
56  if (!pixel_in_box(ix, iy, rect.i_left, rect.i_right, rect.i_top, rect.i_bottom) &&
57  pixel_in_box(ix, iy, rect.o_left, rect.o_right, rect.o_top, rect.o_bottom)) {
58  blend_single_color<T>(out_color[0], detail::RangeCast<T>(rect.color));
59  }
60  } else {
61  if (pixel_in_box(ix, iy, rect.o_left, rect.o_right, rect.o_top, rect.o_bottom)) {
62  blend_single_color<T>(out_color[0], detail::RangeCast<T>(rect.color));
63  }
64  }
65 }
66 } // namespace Kernels
67 
68 // BT.601 Spec conversion factors
69 const float BT601A = 0.299F;
70 const float BT601B = 0.587F;
71 const float BT601C = 0.114F;
72 const float BT601D = 1.772F;
73 const float BT601E = 1.402F;
74 
75 // BT.709 Spec conversion factors
76 const float BT709A = 0.2126F;
77 const float BT709B = 0.7152F;
78 const float BT709C = 0.0722F;
79 const float BT709D = 1.8556F;
80 const float BT709E = 1.5748F;
81 
82 // BT.2020 Spec conversion factors
83 const float BT2020A = 0.2627F;
84 const float BT2020B = 0.6780F;
85 const float BT2020C = 0.0593F;
86 const float BT2020D = 1.8814F;
87 const float BT2020E = 1.4746F;
88 
90  float a;
91  float b;
92  float c;
93  float d;
94  float e;
95 } typedef ConversionFactors;
96 
97 namespace {
98 template <typename T>
99 inline __device__ __host__ float norm1(const T &a) {
100  using namespace roccv::detail;
101  if constexpr (NumElements<T> == 1)
102  return abs(a.x);
103  else if constexpr (NumElements<T> == 2)
104  return abs(a.x) + abs(a.y);
105  else if constexpr (NumElements<T> == 3)
106  return abs(a.x) + abs(a.y) + abs(a.z);
107  else if constexpr (NumElements<T> == 4)
108  return abs(a.x) + abs(a.y) + abs(a.z) + abs(a.w);
109 }
110 
111 // Advanced Color Conversion
112 inline ConversionFactors GetConversionFactorsForSpec(eColorSpec spec) {
113  ConversionFactors factors;
114  switch (spec) {
115  case BT601:
116  factors.a = BT601A;
117  factors.b = BT601B;
118  factors.c = BT601C;
119  factors.d = BT601D;
120  factors.e = BT601E;
121  break;
122  case BT709:
123  factors.a = BT709A;
124  factors.b = BT709B;
125  factors.c = BT709C;
126  factors.d = BT709D;
127  factors.e = BT709E;
128  break;
129  case BT2020:
130  factors.a = BT2020A;
131  factors.b = BT2020B;
132  factors.c = BT2020C;
133  factors.d = BT2020D;
134  factors.e = BT2020E;
135  break;
136  default:
137  throw roccv::Exception("Unexpected Color Specification", roccv::eStatusType::NOT_IMPLEMENTED);
138  }
139 
140  return factors;
141 }
142 
143 template <typename T>
144 inline __device__ __host__ void bgr2yuv_kernel(const T &r, const T &g, const T &b, float &y, float &u, float &v,
145  const ConversionFactors factors) {
146  y = r * factors.a + g * factors.b + b * factors.c;
147  v = (r - y) / factors.e;
148  u = (b - y) / factors.d;
149 }
150 
151 template <typename T>
152 inline __device__ __host__ void yuv2rgb_kernel(const T &y, const T &u, const T &v, float &r, float &g, float &b,
153  const ConversionFactors factors, float delta) {
154  r = y + factors.e * (v - delta);
155  g = y - (factors.a * factors.e / factors.b) * (v - delta) - (factors.c * factors.b / factors.b) * (u - delta);
156  b = y + factors.d * (u - delta);
157 }
158 } // namespace
Definition: exception.hpp:31
const float BT601E
Definition: kernel_helpers.hpp:73
const float BT2020A
Definition: kernel_helpers.hpp:83
const float BT709E
Definition: kernel_helpers.hpp:80
const float BT2020C
Definition: kernel_helpers.hpp:85
const float BT2020B
Definition: kernel_helpers.hpp:84
const float BT709A
Definition: kernel_helpers.hpp:76
const float BT601D
Definition: kernel_helpers.hpp:72
const float BT709C
Definition: kernel_helpers.hpp:78
const float BT601C
Definition: kernel_helpers.hpp:71
const float BT2020D
Definition: kernel_helpers.hpp:86
const float BT709D
Definition: kernel_helpers.hpp:79
const float BT601B
Definition: kernel_helpers.hpp:70
const float BT2020E
Definition: kernel_helpers.hpp:87
const float BT709B
Definition: kernel_helpers.hpp:77
const float BT601A
Definition: kernel_helpers.hpp:69
Definition: non_max_suppression_helpers.hpp:26
Definition: strided_data_wrap.hpp:33
Definition: strided_data_wrap.hpp:33
@ NOT_IMPLEMENTED
invalid permutation of parameters.
eColorSpec
Definition: operator_types.h:66
@ BT2020
Definition: operator_types.h:69
@ BT601
Definition: operator_types.h:67
@ BT709
Definition: operator_types.h:68
Definition: kernel_helpers.hpp:89
float e
Definition: kernel_helpers.hpp:94
float b
Definition: kernel_helpers.hpp:91
float d
Definition: kernel_helpers.hpp:93
float a
Definition: kernel_helpers.hpp:90
float c
Definition: kernel_helpers.hpp:92
Definition: operator_types.h:115
float i_right
Definition: operator_types.h:118
float o_left
Definition: operator_types.h:117
float i_left
Definition: operator_types.h:118
bool bordered
Definition: operator_types.h:120
float o_bottom
Definition: operator_types.h:117
float i_top
Definition: operator_types.h:118
float o_right
Definition: operator_types.h:117
float i_bottom
Definition: operator_types.h:118
float o_top
Definition: operator_types.h:117
uchar4 color
Definition: operator_types.h:119