blob: 4daa7025cb9c3e4e087b8d67645104ae2ce351da [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;
51 for(int x = 0; x < dst_width; x += 2, x_coord++)
52 {
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{
195 SimpleTensor<T> yvec(TensorShape{ src.shape().x(), src.shape().y() }, Format::U8);
196 SimpleTensor<T> uvec(TensorShape{ src.shape().x(), src.shape().y() }, Format::U8);
197 SimpleTensor<T> yyvec(TensorShape{ src.shape().x(), src.shape().y() }, Format::U8);
198 SimpleTensor<T> vvec(TensorShape{ src.shape().x(), src.shape().y() }, Format::U8);
199
200 const int step_x = (Format::YUYV422 == format || Format::UYVY422 == format) ? 2 : 1;
201
202 const int offset = (Format::YUYV422 == format) ? 0 : 1;
203 Coordinates elem_coord{ 0, 0 };
204
205 const int width = vvec.shape().x();
206 const int height = vvec.shape().y();
207
208 for(int y = 0; y < height; ++y)
209 {
210 for(int x = 0; x < width; ++x)
211 {
212 const Coordinates src_coord{ x * step_x, y };
213 const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
214 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
215 auto *uvec_pixel = reinterpret_cast<T *>(uvec(elem_coord));
216 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
217 auto *vvec_pixel = reinterpret_cast<T *>(vvec(elem_coord));
218 yvec_pixel[x] = src_pixel[0 + offset];
219 uvec_pixel[x] = src_pixel[1 - offset];
220 yyvec_pixel[x] = src_pixel[2 + offset];
221 vvec_pixel[x] = src_pixel[3 - offset];
222 }
223 elem_coord.set(1, y + 1);
224 }
225
226 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
227}
228
229template <typename T>
230inline void colorconvert_iyuv_to_rgb(const TensorShape &shape, const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
231{
232 SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
233 SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
234 SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
235 SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
236
237 Coordinates elem_coord{ 0, 0 };
238 const int yvec_width = yvec.shape().x();
239 const int yvec_height = yvec.shape().y();
240
241 for(int y = 0; y < yvec_height; ++y)
242 {
243 for(int x = 0; x < yvec_width; ++x)
244 {
245 const Coordinates src_coord{ x, y };
246 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
247 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
248 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
249 yvec_pixel[x] = src_pixel[x];
250 yyvec_pixel[x] = src_pixel[x + 1];
251 }
252 elem_coord.set(1, y + 1);
253 }
254
255 const int uvec_width = uvec.shape().x();
256 const int uvec_height = uvec.shape().y();
257
258 Coordinates top_elem_coord{ 0, 0 };
259 Coordinates bottom_elem_coord{ 0, 1 };
260 for(int y = 0; y < uvec_height; y += 2)
261 {
262 for(int x = 0; x < uvec_width; ++x)
263 {
264 const Coordinates src_coord{ x, y / 2 };
265 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
266 auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
267 auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
268
269 auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
270 auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
271 uvec_pixel_top[x] = src_pixel[0];
272 vvec_pixel_top[x] = src_pixel[0];
273 uvec_pixel_bottom[x] = src_pixel[0];
274 vvec_pixel_bottom[x] = src_pixel[0];
275 }
276 top_elem_coord.set(1, y + 2);
277 bottom_elem_coord.set(1, top_elem_coord.y() + 1);
278 }
279
280 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
281}
282
283template <typename T>
284inline void colorconvert_nv12_to_rgb(const TensorShape &shape, const Format format, const std::vector<SimpleTensor<T>> &tensor_planes, SimpleTensor<T> &dst)
285{
286 SimpleTensor<T> yvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
287 SimpleTensor<T> uvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
288 SimpleTensor<T> yyvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
289 SimpleTensor<T> vvec(TensorShape{ tensor_planes[0].shape().x(), tensor_planes[0].shape().y() }, Format::U8);
290
291 const int offset = (Format::NV12 == format) ? 0 : 1;
292
293 Coordinates elem_coord{ 0, 0 };
294 const int yvec_width = yvec.shape().x();
295 const int yvec_height = yvec.shape().y();
296
297 for(int y = 0; y < yvec_height; ++y)
298 {
299 for(int x = 0; x < yvec_width; ++x)
300 {
301 const Coordinates src_coord{ x, y };
302 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[0](src_coord));
303 auto *yvec_pixel = reinterpret_cast<T *>(yvec(elem_coord));
304 auto *yyvec_pixel = reinterpret_cast<T *>(yyvec(elem_coord));
305 yvec_pixel[x] = src_pixel[x];
306 yyvec_pixel[x] = src_pixel[x + 1];
307 }
308 elem_coord.set(1, y + 1);
309 }
310
311 const int uvec_width = uvec.shape().x();
312 const int uvec_height = uvec.shape().y();
313
314 Coordinates top_elem_coord{ 0, 0 };
315 Coordinates bottom_elem_coord{ 0, 1 };
316 for(int y = 0; y < uvec_height; y += 2)
317 {
318 for(int x = 0; x < uvec_width; ++x)
319 {
320 const Coordinates src_coord{ x, y / 2 };
321 const auto *src_pixel = reinterpret_cast<const T *>(tensor_planes[1](src_coord));
322 auto *uvec_pixel_top = reinterpret_cast<T *>(uvec(top_elem_coord));
323 auto *vvec_pixel_top = reinterpret_cast<T *>(vvec(top_elem_coord));
324
325 auto *uvec_pixel_bottom = reinterpret_cast<T *>(uvec(bottom_elem_coord));
326 auto *vvec_pixel_bottom = reinterpret_cast<T *>(vvec(bottom_elem_coord));
327 uvec_pixel_top[x] = src_pixel[0 + offset];
328 vvec_pixel_top[x] = src_pixel[1 - offset];
329 uvec_pixel_bottom[x] = src_pixel[0 + offset];
330 vvec_pixel_bottom[x] = src_pixel[1 - offset];
331 }
332 top_elem_coord.set(1, y + 2);
333 bottom_elem_coord.set(1, top_elem_coord.y() + 1);
334 }
335
336 yuyv_to_rgb_calculation(yvec, vvec, yyvec, uvec, dst);
337}
338
339} // namespace detail
340} // color_convert_helper
341} // namespace test
342} // namespace arm_compute
343#endif /*__ARM_COMPUTE_TEST_VALIDATION_COLOR_CONVERT_H__ */