Compute Library
 21.02
ColorConvert.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 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  *
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, DAMAGES 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 #include "ColorConvert.h"
25 
26 #include "arm_compute/core/Types.h"
29 
30 namespace arm_compute
31 {
32 namespace test
33 {
34 namespace validation
35 {
36 namespace reference
37 {
38 namespace
39 {
40 template <typename T>
41 inline std::vector<SimpleTensor<T>> create_image_planes(const TensorShape &shape, Format format)
42 {
43  TensorShape image_shape = adjust_odd_shape(shape, format);
44 
45  std::vector<SimpleTensor<T>> image_planes;
46 
47  switch(format)
48  {
49  case Format::U8:
50  case Format::RGB888:
51  case Format::RGBA8888:
52  case Format::YUYV422:
53  case Format::UYVY422:
54  {
55  image_planes.emplace_back(image_shape, format);
56  break;
57  }
58  case Format::NV12:
59  case Format::NV21:
60  {
61  TensorShape shape_uv88 = calculate_subsampled_shape(image_shape, Format::UV88);
62 
63  image_planes.emplace_back(image_shape, Format::U8);
64  image_planes.emplace_back(shape_uv88, Format::UV88);
65  break;
66  }
67  case Format::IYUV:
68  {
69  TensorShape shape_sub2 = calculate_subsampled_shape(image_shape, Format::IYUV);
70 
71  image_planes.emplace_back(image_shape, Format::U8);
72  image_planes.emplace_back(shape_sub2, Format::U8);
73  image_planes.emplace_back(shape_sub2, Format::U8);
74  break;
75  }
76  case Format::YUV444:
77  {
78  image_planes.emplace_back(image_shape, Format::U8);
79  image_planes.emplace_back(image_shape, Format::U8);
80  image_planes.emplace_back(image_shape, Format::U8);
81  break;
82  }
83  default:
84  ARM_COMPUTE_ERROR("Not supported");
85  break;
86  }
87 
88  return image_planes;
89 }
90 } // namespace
91 
92 template <typename T>
93 std::vector<SimpleTensor<T>> color_convert(const TensorShape &shape, const std::vector<SimpleTensor<T>> &tensor_planes, Format src_format, Format dst_format)
94 {
95  std::vector<SimpleTensor<T>> dst = create_image_planes<T>(shape, dst_format);
96 
97  switch(src_format)
98  {
99  case Format::RGB888:
100  {
101  switch(dst_format)
102  {
103  case Format::RGBA8888:
105  break;
106  case Format::U8:
107  colorconvert_helper::detail::colorconvert_rgb_to_u8(tensor_planes[0], dst[0]);
108  break;
109  case Format::NV12:
111  break;
112  case Format::IYUV:
114  break;
115  case Format::YUV444:
117  break;
118  default:
119  ARM_COMPUTE_ERROR("Not Supported");
120  break;
121  }
122  break;
123  }
124  case Format::RGBA8888:
125  {
126  switch(dst_format)
127  {
128  case Format::RGB888:
130  break;
131  case Format::NV12:
133  break;
134  case Format::IYUV:
136  break;
137  case Format::YUV444:
139  break;
140  default:
141  ARM_COMPUTE_ERROR("Not Supported");
142  break;
143  }
144  break;
145  }
146  case Format::UYVY422:
147  case Format::YUYV422:
148  {
149  switch(dst_format)
150  {
151  case Format::RGB888:
152  case Format::RGBA8888:
153  colorconvert_helper::detail::colorconvert_yuyv_to_rgb(tensor_planes[0], src_format, dst[0]);
154  break;
155  case Format::NV12:
156  colorconvert_helper::detail::colorconvert_yuyv_to_nv12(tensor_planes[0], src_format, dst);
157  break;
158  case Format::IYUV:
159  colorconvert_helper::detail::colorconvert_yuyv_to_iyuv(tensor_planes[0], src_format, dst);
160  break;
161  default:
162  ARM_COMPUTE_ERROR("Not Supported");
163  break;
164  }
165  break;
166  }
167  case Format::IYUV:
168  {
169  switch(dst_format)
170  {
171  case Format::RGB888:
172  case Format::RGBA8888:
174  break;
175  default:
176  ARM_COMPUTE_ERROR("Not Supported");
177  break;
178  }
179  break;
180  }
181  case Format::NV12:
182  case Format::NV21:
183  {
184  switch(dst_format)
185  {
186  case Format::RGB888:
187  case Format::RGBA8888:
188  colorconvert_helper::detail::colorconvert_nv12_to_rgb(src_format, tensor_planes, dst[0]);
189  break;
190  case Format::IYUV:
191  colorconvert_helper::detail::colorconvert_nv_to_iyuv(tensor_planes, src_format, dst);
192  break;
193  case Format::YUV444:
194  colorconvert_helper::detail::colorconvert_nv_to_yuv4(tensor_planes, src_format, dst);
195  break;
196  default:
197  ARM_COMPUTE_ERROR("Not Supported");
198  break;
199  }
200  break;
201  }
202  default:
203  ARM_COMPUTE_ERROR("Not supported");
204  break;
205  }
206  return dst;
207 }
208 
209 template std::vector<SimpleTensor<uint8_t>> color_convert(const TensorShape &shape, const std::vector<SimpleTensor<uint8_t>> &tensor_planes, Format src_format, Format dst_format);
210 } // namespace reference
211 } // namespace validation
212 } // namespace test
213 } // namespace arm_compute
A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte.
Shape of a tensor.
Definition: TensorShape.h:39
void colorconvert_yuyv_to_iyuv(const SimpleTensor< T > src, const Format format, std::vector< SimpleTensor< T >> &dst)
TensorShape calculate_subsampled_shape(const TensorShape &shape, Format format, Channel channel=Channel::UNKNOWN)
Calculate subsampled shape for a given format and channel.
Definition: Utils.h:774
void colorconvert_nv_to_iyuv(const std::vector< SimpleTensor< T >> src, const Format src_format, std::vector< SimpleTensor< T >> &dst)
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void colorconvert_yuyv_to_nv12(const SimpleTensor< T > src, const Format format, std::vector< SimpleTensor< T >> &dst)
1 channel, 1 U8 per channel
void colorconvert_yuyv_to_rgb(const SimpleTensor< T > src, const Format format, SimpleTensor< T > &dst)
A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling.
void colorconvert_nv_to_yuv4(const std::vector< SimpleTensor< T >> src, const Format src_format, std::vector< SimpleTensor< T >> &dst)
void colorconvert_iyuv_to_rgb(const std::vector< SimpleTensor< T >> &tensor_planes, SimpleTensor< T > &dst)
A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling.
Copyright (c) 2017-2021 Arm Limited.
3 channels, 1 U8 per channel
void colorconvert_rgb_to_nv12(const SimpleTensor< T > src, std::vector< SimpleTensor< T >> &dst)
void colorconvert_rgbx_to_rgb(const SimpleTensor< T > src, SimpleTensor< T > &dst)
Format
Image colour formats.
Definition: Types.h:54
A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes.
TensorShape adjust_odd_shape(const TensorShape &shape, Format format)
Adjust tensor shape size if width or height are odd for a given multi-planar format.
Definition: Utils.h:747
std::vector< SimpleTensor< T > > color_convert(const TensorShape &shape, const std::vector< SimpleTensor< T >> &tensor_planes, Format src_format, Format dst_format)
A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes.
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
4 channels, 1 U8 per channel
void colorconvert_rgb_to_u8(const SimpleTensor< T > src, SimpleTensor< T > &dst)
2 channel, 1 U8 per channel
void colorconvert_rgb_to_rgbx(const SimpleTensor< T > src, SimpleTensor< T > &dst)
void colorconvert_rgb_to_iyuv(const SimpleTensor< T > src, std::vector< SimpleTensor< T >> &dst)
void colorconvert_nv12_to_rgb(const Format format, const std::vector< SimpleTensor< T >> &tensor_planes, SimpleTensor< T > &dst)
void colorconvert_rgb_to_yuv4(const SimpleTensor< T > src, std::vector< SimpleTensor< T >> &dst)
A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes.