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/core/NEON/kernels/NECumulativeDistributionKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/IDistribution1D.h" |
| 29 | #include "arm_compute/core/ILut.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cmath> |
| 36 | #include <numeric> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
| 40 | NECumulativeDistributionKernel::NECumulativeDistributionKernel() |
| 41 | : _input(nullptr), _distribution(nullptr), _cumulative_sum(nullptr), _output(nullptr) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | bool NECumulativeDistributionKernel::is_parallelisable() const |
| 46 | { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | void NECumulativeDistributionKernel::configure(const IImage *input, const IDistribution1D *distribution, IDistribution1D *cumulative_sum, ILut *output) |
| 51 | { |
| 52 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, distribution, cumulative_sum, output); |
| 53 | ARM_COMPUTE_ERROR_ON_TENSOR_NOT_2D(input); |
| 54 | |
| 55 | set_format_if_unknown(*input->info(), Format::U8); |
| 56 | |
| 57 | ARM_COMPUTE_ERROR_ON(distribution->num_bins() != cumulative_sum->num_bins()); |
| 58 | ARM_COMPUTE_ERROR_ON(distribution->num_bins() != output->num_elements()); |
| 59 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 60 | ARM_COMPUTE_ERROR_ON(input->info()->data_type() != output->type()); |
| 61 | |
| 62 | _input = input; |
| 63 | _distribution = distribution; |
| 64 | _cumulative_sum = cumulative_sum; |
| 65 | _output = output; |
| 66 | |
| 67 | INEKernel::configure(calculate_max_window(*input->info())); |
| 68 | } |
| 69 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 70 | void NECumulativeDistributionKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 74 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 75 | ARM_COMPUTE_ERROR_ON(_distribution->buffer() == nullptr); |
| 76 | ARM_COMPUTE_ERROR_ON(_cumulative_sum->buffer() == nullptr); |
| 77 | ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr); |
| 78 | ARM_COMPUTE_ERROR_ON_MSG(_distribution->num_bins() < 256, "Distribution must have 256 bins"); |
| 79 | |
| 80 | // Calculate the cumulative distribution (summed histogram). |
| 81 | const uint32_t *hist = _distribution->buffer(); |
| 82 | uint32_t *cumulative_sum = _cumulative_sum->buffer(); |
| 83 | uint8_t *output = _output->buffer(); |
| 84 | |
| 85 | // Calculate cumulative distribution |
| 86 | std::partial_sum(hist, hist + _histogram_size, cumulative_sum); |
| 87 | |
| 88 | // Get the number of pixels that have the lowest value in the input image |
| 89 | const uint32_t cd_min = *std::find_if(hist, hist + _histogram_size, [](const uint32_t &v) |
| 90 | { |
| 91 | return v > 0; |
| 92 | }); |
| 93 | const uint32_t image_size = cumulative_sum[_histogram_size - 1]; |
| 94 | |
| 95 | ARM_COMPUTE_ERROR_ON(cd_min > image_size); |
| 96 | |
| 97 | // Create mapping lookup table |
| 98 | if(image_size == cd_min) |
| 99 | { |
| 100 | std::iota(output, output + _histogram_size, 0); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | const float diff = image_size - cd_min; |
| 105 | |
| 106 | for(unsigned int x = 0; x < _histogram_size; ++x) |
| 107 | { |
| 108 | output[x] = lround((cumulative_sum[x] - cd_min) / diff * 255.0f); |
| 109 | } |
| 110 | } |
| 111 | } |