1 /*
2  * Copyright (c) 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 #include "LaplacianPyramid.h"
25 
26 #include "tests/validation/reference/ArithmeticOperations.h"
27 #include "tests/validation/reference/DepthConvertLayer.h"
28 #include "tests/validation/reference/Gaussian5x5.h"
29 #include "tests/validation/reference/GaussianPyramidHalf.h"
30 
31 namespace arm_compute
32 {
33 namespace test
34 {
35 namespace validation
36 {
37 namespace reference
38 {
39 template <typename T, typename U>
laplacian_pyramid(const SimpleTensor<T> & src,SimpleTensor<U> & dst,size_t num_levels,BorderMode border_mode,uint8_t constant_border_value)40 std::vector<SimpleTensor<U>> laplacian_pyramid(const SimpleTensor<T> &src, SimpleTensor<U> &dst, size_t num_levels, BorderMode border_mode, uint8_t constant_border_value)
41 {
42     std::vector<SimpleTensor<T>> pyramid_conv;
43     std::vector<SimpleTensor<U>> pyramid_dst;
44 
45     // First, a Gaussian pyramid with SCALE_PYRAMID_HALF is created
46     std::vector<SimpleTensor<T>> gaussian_level_pyramid = reference::gaussian_pyramid_half(src, border_mode, constant_border_value, num_levels);
47 
48     // For each level i, the corresponding image Ii is blurred with Gaussian 5x5
49     // filter, and the difference between the two images is the corresponding
50     // level Li of the Laplacian pyramid
51     for(size_t i = 0; i < num_levels; ++i)
52     {
53         const SimpleTensor<T> level_filtered = reference::gaussian5x5(gaussian_level_pyramid[i], border_mode, constant_border_value);
54         pyramid_conv.push_back(level_filtered);
55 
56         const SimpleTensor<U> level_filtered_converted = depth_convert<T, U>(level_filtered, DataType::S16, ConvertPolicy::WRAP, 0);
57         const SimpleTensor<U> gaussian_level_converted = depth_convert<T, U>(gaussian_level_pyramid[i], DataType::S16, ConvertPolicy::WRAP, 0);
58 
59         const SimpleTensor<U> level_sub = reference::arithmetic_operation<U>(reference::ArithmeticOperation::SUB, gaussian_level_converted, level_filtered_converted, dst.data_type(), ConvertPolicy::WRAP);
60         pyramid_dst.push_back(level_sub);
61     }
62 
63     // Return the lowest resolution image and the pyramid
64     dst = depth_convert<T, U>(pyramid_conv[num_levels - 1], DataType::S16, ConvertPolicy::WRAP, 0);
65 
66     return pyramid_dst;
67 }
68 
69 template std::vector<SimpleTensor<int16_t>> laplacian_pyramid(const SimpleTensor<uint8_t> &src, SimpleTensor<int16_t> &dst, size_t num_levels, BorderMode border_mode, uint8_t constant_border_value);
70 } // namespace reference
71 } // namespace validation
72 } // namespace test
73 } // namespace arm_compute
74