blob: 50040d9a92b7e2dd70cb4a3defe342e709431f10 [file] [log] [blame]
Sanghoon Lee1fad27a2018-04-05 10:57:57 +01001/*
2 * Copyright (c) 2017-2018 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *asymm_int_mult
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, asymm_int_multDAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H__
26
27#include "Utils.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace colorconvert_helper
34{
35namespace detail
36{
37constexpr float red_coef_bt709 = 1.5748F;
38constexpr float green_coef_bt709 = -0.1873f;
39constexpr float green_coef2_bt709 = -0.4681f;
40constexpr float blue_coef_bt709 = 1.8556f;
41
42template <typename T>
43inline void yuyv_to_rgb_calculation(const SimpleTensor<T> yvec, const SimpleTensor<T> vvec, const SimpleTensor<T> yyvec, const SimpleTensor<T> uvec, SimpleTensor<T> &dst)
44{
45 const int dst_width = dst.shape().x();
46 const int dst_height = dst.shape().y();
47
48 for(int y = 0; y < dst_height; ++y)
49 {
50 int x_coord = 0;
Georgios Pinitase8c18d42018-06-11 20:05:18 +010051 for(int x = 0; x < dst_width; x += 2, ++x_coord)
Sanghoon Lee1fad27a2018-04-05 10:57:57 +010052 {
53 Coordinates dst_coord{ x, y };
54 auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
55 float result = 0.f;
56
57 T border_value(0);
58 const int yvec_val = validation::tensor_elem_at(yvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
59 const int vvec_val = validation::tensor_elem_at(vvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
60 const int yyvec_val = validation::tensor_elem_at(yyvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
61 const int uvec_val = validation::tensor_elem_at(uvec, { x_coord, y }, BorderMode::CONSTANT, border_value);
62 const float red = (vvec_val - 128) * red_coef_bt709;
63 const float green = (uvec_val - 128) * green_coef_bt709 + (vvec_val - 128) * green_coef2_bt709;
64 const float blue = (uvec_val - 128) * blue_coef_bt709;
65
66 for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
67 {
68 if(channel_idx == 0)
69 {
70 // Channel 'R'
71 result = yvec_val + red;
72 }
73 else if(channel_idx == 1)
74 {
75 // Channel 'G'
76 result = yvec_val + green;
77 }
78 else if(channel_idx == 2)
79 {
80 // Channel 'B'
81 result = yvec_val + blue;
82 }
83 else
84 {
85 // Channel 'A'
86 result = 255;
87 }
88
89 if(result < 0)
90 {
91 result = 0;
92 }
93 else if(result > 255)
94 {
95 result = 255;
96 }
97 dst_pixel[channel_idx] = result;
98 }
99
100 dst_coord.set(0, x + 1);
101 dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
102 for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
103 {
104 if(channel_idx == 0)
105 {
106 // Channel 'R'
107 result = yyvec_val + red;
108 }
109 else if(channel_idx == 1)
110 {
111 // Channel 'G'
112 result = yyvec_val + green;
113 }
114 else if(channel_idx == 2)
115 {
116 // Channel 'B'
117 result = yyvec_val + blue;
118 }
119 else
120 {
121 // Channel 'A'
122 result = 255;
123 }
124
125 if(result < 0)
126 {
127 result = 0;
128 }
129 else if(result > 255)
130 {
131 result = 255;
132 }
133 dst_pixel[channel_idx] = result;
134 }
135 }
136 }
137}
138
139template <typename T>
140inline void colorconvert_rgb_to_rgbx(const SimpleTensor<T> src, SimpleTensor<T> &dst)
141{
142 for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
143 {
144 const int width = dst.shape().x();
145 const int height = dst.shape().y();
146
147 for(int y = 0; y < height; ++y)
148 {
149 for(int x = 0; x < width; ++x)
150 {
151 const Coordinates src_coord{ x, y };
152 const Coordinates dst_coord{ x, y };
153
154 const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
155 auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
156 if(channel_idx == 3)
157 {
158 dst_pixel[channel_idx] = 255;
159 continue;
160 }
161
162 dst_pixel[channel_idx] = src_pixel[channel_idx];
163 }
164 }
165 }
166}
167
168template <typename T>
169inline void colorconvert_rgbx_to_rgb(const SimpleTensor<T> src, SimpleTensor<T> &dst)
170{
171 for(int channel_idx = 0; channel_idx < dst.num_channels(); ++channel_idx)
172 {
173 const int width = dst.shape().x();
174 const int height = dst.shape().y();
175
176 for(int y = 0; y < height; ++y)
177 {
178 for(int x = 0; x < width; ++x)
179 {
180 const Coordinates src_coord{ x, y };
181 const Coordinates dst_coord{ x, y };
182
183 const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
184 auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
185
186 dst_pixel[channel_idx] = src_pixel[channel_idx];
187 }
188 }
189 }
190}
191
192template <typename T>
193inline void colorconvert_yuyv_to_rgb(const SimpleTensor<T> src, const Format format, SimpleTensor<T> &dst)
194{
Georgios Pinitase8c18d42018-06-11 20:05:18 +0100195 SimpleTensor<T> yvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
196 SimpleTensor<T> uvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
197 SimpleTensor<T> yyvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
198 SimpleTensor<T> vvec(TensorShape{ src.shape().x() / 2, src.shape().y() }, Format::U8);
Sanghoon Lee1fad27a2018-04-05 10:57:57 +0100199
200 const int step_x = (Format::YUYV422 == format || Format::UYVY422 == format) ? 2 : 1;
Georgios Pinitase8c18d42018-06-11 20:05:18 +0100201 const int offset = (Format::YUYV422 == format) ? 0 : 1;
Sanghoon Lee1fad27a2018-04-05 10:57:57 +0100202
Sanghoon Lee1fad27a2018-04-05 10:57:57 +0100203 Coordinates elem_coord{ 0, 0 };
Georgios Pinitase8c18d42018-06-11 20:05:18 +0100204 const int width = yvec.shape().x();
205 const int height = yvec.shape().y();
Sanghoon Lee1fad27a2018-04-05 10:57:57 +0100206
207 for(int y = 0; y < height; ++y)
208 {
209 for(int x = 0; x < width; ++x)
210 {
211 const Coordinates src_coord{ x * step_x, y };
212 const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
213 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
214 auto *uvec_pixel = reinterpret_cast<T *>(uvec(elem_coord));
215 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
216 auto *vvec_pixel = reinterpret_cast<T *>(vvec(elem_coord));
217 yvec_pixel[x] = src_pixel[0 + offset];
218 uvec_pixel[x] = src_pixel[1 - offset];
219 yyvec_pixel[x] = src_pixel[2 + offset];
220 vvec_pixel[x] = src_pixel[3 - offset];
221 }
222 elem_coord.set(1, y + 1);
223 }
224
225 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
226}
227
228template <typename T>
229inline void colorconvert_iyuv_to_rgb(const TensorShape &shape, const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
230{
231 SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
232 SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
233 SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
234 SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
235
236 Coordinates elem_coord{ 0, 0 };
237 const int yvec_width = yvec.shape().x();
238 const int yvec_height = yvec.shape().y();
239
240 for(int y = 0; y < yvec_height; ++y)
241 {
242 for(int x = 0; x < yvec_width; ++x)
243 {
244 const Coordinates src_coord{ x, y };
245 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
246 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
247 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
248 yvec_pixel[x] = src_pixel[x];
249 yyvec_pixel[x] = src_pixel[x + 1];
250 }
251 elem_coord.set(1, y + 1);
252 }
253
254 const int uvec_width = uvec.shape().x();
255 const int uvec_height = uvec.shape().y();
256
257 Coordinates top_elem_coord{ 0, 0 };
258 Coordinates bottom_elem_coord{ 0, 1 };
259 for(int y = 0; y < uvec_height; y += 2)
260 {
261 for(int x = 0; x < uvec_width; ++x)
262 {
263 const Coordinates src_coord{ x, y / 2 };
264 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
265 auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
266 auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
267
268 auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
269 auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
270 uvec_pixel_top[x] = src_pixel[0];
271 vvec_pixel_top[x] = src_pixel[0];
272 uvec_pixel_bottom[x] = src_pixel[0];
273 vvec_pixel_bottom[x] = src_pixel[0];
274 }
275 top_elem_coord.set(1, y + 2);
276 bottom_elem_coord.set(1, top_elem_coord.y() + 1);
277 }
278
279 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
280}
281
282template <typename T>
283inline void colorconvert_nv12_to_rgb(const TensorShape &shape, const Format format, const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
284{
285 SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
286 SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
287 SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
288 SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
289
290 const int offset = (Format::NV12 == format) ? 0 : 1;
291
292 Coordinates elem_coord{ 0, 0 };
293 const int yvec_width = yvec.shape().x();
294 const int yvec_height = yvec.shape().y();
295
296 for(int y = 0; y < yvec_height; ++y)
297 {
298 for(int x = 0; x < yvec_width; ++x)
299 {
300 const Coordinates src_coord{ x, y };
301 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
302 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
303 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
304 yvec_pixel[x] = src_pixel[x];
305 yyvec_pixel[x] = src_pixel[x + 1];
306 }
307 elem_coord.set(1, y + 1);
308 }
309
310 const int uvec_width = uvec.shape().x();
311 const int uvec_height = uvec.shape().y();
312
313 Coordinates top_elem_coord{ 0, 0 };
314 Coordinates bottom_elem_coord{ 0, 1 };
315 for(int y = 0; y < uvec_height; y += 2)
316 {
317 for(int x = 0; x < uvec_width; ++x)
318 {
319 const Coordinates src_coord{ x, y / 2 };
320 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
321 auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
322 auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
323
324 auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
325 auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
326 uvec_pixel_top[x] = src_pixel[0 + offset];
327 vvec_pixel_top[x] = src_pixel[1 - offset];
328 uvec_pixel_bottom[x] = src_pixel[0 + offset];
329 vvec_pixel_bottom[x] = src_pixel[1 - offset];
330 }
331 top_elem_coord.set(1, y + 2);
332 bottom_elem_coord.set(1, top_elem_coord.y() + 1);
333 }
334
335 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
336}
337
338} // namespace detail
339} // color_convert_helper
340} // namespace test
341} // namespace arm_compute
342#endif /*__ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H__ */