• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  *
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 #ifndef ARM_COMPUTE_TEST_COLOR_CONVERT_FIXTURE
25 #define ARM_COMPUTE_TEST_COLOR_CONVERT_FIXTURE
26 
27 #include "arm_compute/core/TensorShape.h"
28 #include "arm_compute/core/Types.h"
29 #include "tests/AssetsLibrary.h"
30 #include "tests/Globals.h"
31 #include "tests/IAccessor.h"
32 #include "tests/framework/Asserts.h"
33 #include "tests/framework/Fixture.h"
34 #include "tests/validation/Helpers.h"
35 #include "tests/validation/reference/ColorConvert.h"
36 
37 namespace arm_compute
38 {
39 namespace test
40 {
41 namespace validation
42 {
43 namespace
44 {
45 }
46 template <typename MultiImageType, typename TensorType, typename AccessorType, typename FunctionType, typename T>
47 class ColorConvertValidationFixture : public framework::Fixture
48 {
49 public:
50     template <typename...>
setup(TensorShape shape,Format src_format,Format dst_format)51     void setup(TensorShape shape, Format src_format, Format dst_format)
52     {
53         shape = adjust_odd_shape(shape, src_format);
54         shape = adjust_odd_shape(shape, dst_format);
55 
56         _target    = compute_target(shape, src_format, dst_format);
57         _reference = compute_reference(shape, src_format, dst_format);
58     }
59 
60 protected:
61     template <typename U>
fill(U && tensor,int i)62     void fill(U &&tensor, int i)
63     {
64         library->fill_tensor_uniform(tensor, i);
65     }
66 
create_tensor_planes_reference(const TensorShape & shape,Format format)67     std::vector<SimpleTensor<T>> create_tensor_planes_reference(const TensorShape &shape, Format format)
68     {
69         std::vector<SimpleTensor<T>> tensor_planes;
70 
71         switch(format)
72         {
73             case Format::RGB888:
74             case Format::RGBA8888:
75             case Format::YUYV422:
76             case Format::UYVY422:
77             {
78                 tensor_planes.emplace_back(shape, format);
79                 break;
80             }
81             case Format::NV12:
82             case Format::NV21:
83             {
84                 const TensorShape shape_uv88 = calculate_subsampled_shape(shape, Format::UV88);
85 
86                 tensor_planes.emplace_back(shape, Format::U8);
87                 tensor_planes.emplace_back(shape_uv88, Format::UV88);
88                 break;
89             }
90             case Format::IYUV:
91             {
92                 const TensorShape shape_sub2 = calculate_subsampled_shape(shape, Format::IYUV);
93 
94                 tensor_planes.emplace_back(shape, Format::U8);
95                 tensor_planes.emplace_back(shape_sub2, Format::U8);
96                 tensor_planes.emplace_back(shape_sub2, Format::U8);
97                 break;
98             }
99             case Format::YUV444:
100             {
101                 tensor_planes.emplace_back(shape, Format::U8);
102                 tensor_planes.emplace_back(shape, Format::U8);
103                 tensor_planes.emplace_back(shape, Format::U8);
104                 break;
105             }
106             default:
107                 ARM_COMPUTE_ERROR("Not supported");
108                 break;
109         }
110 
111         return tensor_planes;
112     }
113 
compute_target(const TensorShape & shape,Format src_format,Format dst_format)114     MultiImageType compute_target(const TensorShape &shape, Format src_format, Format dst_format)
115     {
116         _src_num_planes = num_planes_from_format(src_format);
117         _dst_num_planes = num_planes_from_format(dst_format);
118 
119         // Create tensors
120         MultiImageType ref_src = create_multi_image<MultiImageType>(shape, src_format);
121         MultiImageType ref_dst = create_multi_image<MultiImageType>(shape, dst_format);
122 
123         // Create and configure function
124         FunctionType color_convert;
125 
126         if(1U == _src_num_planes)
127         {
128             const TensorType *plane_src = static_cast<TensorType *>(ref_src.plane(0));
129 
130             if(1U == _dst_num_planes)
131             {
132                 TensorType *plane_dst = static_cast<TensorType *>(ref_dst.plane(0));
133                 color_convert.configure(plane_src, plane_dst);
134             }
135             else
136             {
137                 color_convert.configure(plane_src, &ref_dst);
138             }
139         }
140         else
141         {
142             if(1U == _dst_num_planes)
143             {
144                 TensorType *plane_dst = static_cast<TensorType *>(ref_dst.plane(0));
145                 color_convert.configure(&ref_src, plane_dst);
146             }
147             else
148             {
149                 color_convert.configure(&ref_src, &ref_dst);
150             }
151         }
152 
153         for(unsigned int plane_idx = 0; plane_idx < _src_num_planes; ++plane_idx)
154         {
155             const TensorType *src_plane = static_cast<const TensorType *>(ref_src.plane(plane_idx));
156 
157             ARM_COMPUTE_EXPECT(src_plane->info()->is_resizable(), framework::LogLevel::ERRORS);
158         }
159         for(unsigned int plane_idx = 0; plane_idx < _dst_num_planes; ++plane_idx)
160         {
161             const TensorType *dst_plane = static_cast<const TensorType *>(ref_dst.plane(plane_idx));
162 
163             ARM_COMPUTE_EXPECT(dst_plane->info()->is_resizable(), framework::LogLevel::ERRORS);
164         }
165 
166         // Allocate tensors
167         ref_src.allocate();
168         ref_dst.allocate();
169 
170         for(unsigned int plane_idx = 0; plane_idx < _src_num_planes; ++plane_idx)
171         {
172             const TensorType *src_plane = static_cast<const TensorType *>(ref_src.plane(plane_idx));
173             ARM_COMPUTE_EXPECT(!src_plane->info()->is_resizable(), framework::LogLevel::ERRORS);
174         }
175 
176         for(unsigned int plane_idx = 0; plane_idx < _dst_num_planes; ++plane_idx)
177         {
178             const TensorType *dst_plane = static_cast<const TensorType *>(ref_dst.plane(plane_idx));
179             ARM_COMPUTE_EXPECT(!dst_plane->info()->is_resizable(), framework::LogLevel::ERRORS);
180         }
181 
182         // Fill tensor planes
183         for(unsigned int plane_idx = 0; plane_idx < _src_num_planes; ++plane_idx)
184         {
185             TensorType *src_plane = static_cast<TensorType *>(ref_src.plane(plane_idx));
186 
187             fill(AccessorType(*src_plane), plane_idx);
188         }
189 
190         // Compute function
191         color_convert.run();
192 
193         return ref_dst;
194     }
195 
compute_reference(const TensorShape & shape,Format src_format,Format dst_format)196     std::vector<SimpleTensor<T>> compute_reference(const TensorShape &shape, Format src_format, Format dst_format)
197     {
198         // Create reference
199         std::vector<SimpleTensor<T>> ref_src = create_tensor_planes_reference(shape, src_format);
200 
201         // Fill references
202         for(unsigned int plane_idx = 0; plane_idx < ref_src.size(); ++plane_idx)
203         {
204             fill(ref_src[plane_idx], plane_idx);
205         }
206 
207         return reference::color_convert<T>(shape, ref_src, src_format, dst_format);
208     }
209 
210     unsigned int                 _src_num_planes{};
211     unsigned int                 _dst_num_planes{};
212     MultiImageType               _target{};
213     std::vector<SimpleTensor<T>> _reference{};
214 };
215 } // namespace validation
216 } // namespace test
217 } // namespace arm_compute
218 #endif /* ARM_COMPUTE_TEST_COLOR_CONVERT_FIXTURE */
219