Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 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 "arm_compute/runtime/NEON/functions/NELaplacianReconstruct.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IPyramid.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
| 33 | #include <cstddef> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | NELaplacianReconstruct::NELaplacianReconstruct() |
| 38 | : _tmp_pyr(), _addf(), _scalef(), _depthf() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void NELaplacianReconstruct::configure(const IPyramid *pyramid, const ITensor *input, ITensor *output, BorderMode border_mode, uint8_t constant_border_value) |
| 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON(nullptr == pyramid); |
| 45 | ARM_COMPUTE_ERROR_ON(input == output); |
| 46 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S16); |
| 47 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 48 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 49 | ARM_COMPUTE_ERROR_ON(output->info()->num_dimensions() != pyramid->get_pyramid_level(0)->info()->num_dimensions()); |
| 50 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != pyramid->get_pyramid_level(0)->info()->dimension(0)); |
| 51 | ARM_COMPUTE_ERROR_ON(output->info()->dimension(1) != pyramid->get_pyramid_level(0)->info()->dimension(1)); |
| 52 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(0)); |
| 53 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != pyramid->get_pyramid_level(pyramid->info()->num_levels() - 1)->info()->dimension(1)); |
| 54 | |
| 55 | const size_t num_levels = pyramid->info()->num_levels(); |
| 56 | |
| 57 | // Create and initialize the tmp pyramid: I(n-2) = upsample( input + Laplace(n-1) ) |
| 58 | PyramidInfo pyramid_info; |
| 59 | pyramid_info.init(num_levels, 0.5f, output->info()->tensor_shape(), arm_compute::Format::S16); |
| 60 | |
| 61 | _tmp_pyr.init(pyramid_info); |
| 62 | |
| 63 | // Allocate add and scale functions. Level 0 does not need to be scaled. |
| 64 | _addf = arm_compute::cpp14::make_unique<NEArithmeticAddition[]>(num_levels); |
| 65 | _scalef = arm_compute::cpp14::make_unique<NEScale[]>(num_levels - 1); |
| 66 | |
| 67 | const size_t last_level = num_levels - 1; |
| 68 | |
| 69 | _addf[last_level].configure(input, pyramid->get_pyramid_level(last_level), _tmp_pyr.get_pyramid_level(last_level), ConvertPolicy::SATURATE); |
| 70 | |
| 71 | // Scale levels n-1 to 1, and add levels n-2 to 0 |
| 72 | for(size_t l = 0; l < last_level; ++l) |
| 73 | { |
| 74 | _scalef[l].configure(_tmp_pyr.get_pyramid_level(l + 1), _tmp_pyr.get_pyramid_level(l), arm_compute::InterpolationPolicy::NEAREST_NEIGHBOR, border_mode, constant_border_value); |
| 75 | _addf[l].configure(_tmp_pyr.get_pyramid_level(l), pyramid->get_pyramid_level(l), _tmp_pyr.get_pyramid_level(l), ConvertPolicy::SATURATE); |
| 76 | } |
| 77 | |
| 78 | // Convert level 0 from S16 to U8 |
| 79 | _depthf.configure(_tmp_pyr.get_pyramid_level(0), output, ConvertPolicy::SATURATE, 0); |
| 80 | |
| 81 | _tmp_pyr.allocate(); |
| 82 | } |
| 83 | |
| 84 | void NELaplacianReconstruct::run() |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR_ON_MSG(_addf == nullptr, "Unconfigured function"); |
| 87 | |
| 88 | const size_t last_level = _tmp_pyr.info()->num_levels() - 1; |
| 89 | |
| 90 | _addf[last_level].run(); |
| 91 | |
| 92 | // Run l = [last_level - 1, 0] |
| 93 | for(size_t l = last_level; l-- > 0;) |
| 94 | { |
| 95 | _scalef[l].run(); |
| 96 | _addf[l].run(); |
| 97 | } |
| 98 | |
| 99 | _depthf.run(); |
| 100 | } |