blob: fc7ae7d6cb2e4d0d02af038561746e53b3e83aee [file] [log] [blame]
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +01001/*
Michalis Spyrou6260e192019-06-06 13:47:38 +01002 * Copyright (c) 2017-2019 ARM Limited.
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +01003 *
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 "ChannelExtract.h"
25
26#include "arm_compute/core/Types.h"
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010027#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37template <typename T>
38SimpleTensor<uint8_t> channel_extract(const TensorShape &shape, const std::vector<SimpleTensor<T>> &tensor_planes, Format format, Channel channel)
39{
40 // Find plane and channel index
41 const unsigned int plane_idx = plane_idx_from_channel(format, channel);
42 const unsigned int channel_idx = channel_idx_from_format(format, channel);
43
44 // Create dst and get src tensor
45 SimpleTensor<T> src = tensor_planes[plane_idx];
46 SimpleTensor<T> dst{ calculate_subsampled_shape(shape, format, channel), Format::U8 };
47
48 // Single planar formats with subsampling require a double horizontal step
49 const int step_x = ((Format::YUYV422 == format || Format::UYVY422 == format) && Channel::Y != channel) ? 2 : 1;
50 const int width = dst.shape().x();
51 const int height = dst.shape().y();
52
53 // Loop over each pixel and extract channel
54 for(int y = 0; y < height; ++y)
55 {
56 for(int x = 0; x < width; ++x)
57 {
58 const Coordinates src_coord{ x * step_x, y };
59 const Coordinates dst_coord{ x, y };
60
61 const auto *src_pixel = reinterpret_cast<const T *>(src(src_coord));
62 auto *dst_pixel = reinterpret_cast<T *>(dst(dst_coord));
63
Michalis Spyrou6260e192019-06-06 13:47:38 +010064 dst_pixel[0] = src_pixel[channel_idx]; // NOLINT
Ioan-Cristian Szabo9414f642017-10-27 17:35:40 +010065 }
66 }
67
68 return dst;
69}
70
71template SimpleTensor<uint8_t> channel_extract(const TensorShape &shape, const std::vector<SimpleTensor<uint8_t>> &tensor_planes, Format format, Channel channel);
72} // namespace reference
73} // namespace validation
74} // namespace test
75} // namespace arm_compute