blob: 32789cbe33c7cf8890a5148010f518be61a5d027 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
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
38using namespace arm_compute;
39
40NECumulativeDistributionKernel::NECumulativeDistributionKernel()
41 : _input(nullptr), _distribution(nullptr), _cumulative_sum(nullptr), _output(nullptr)
42{
43}
44
45bool NECumulativeDistributionKernel::is_parallelisable() const
46{
47 return false;
48}
49
50void 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
70void NECumulativeDistributionKernel::run(const Window &window)
71{
72 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
73 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
74 ARM_COMPUTE_ERROR_ON(_distribution->buffer() == nullptr);
75 ARM_COMPUTE_ERROR_ON(_cumulative_sum->buffer() == nullptr);
76 ARM_COMPUTE_ERROR_ON(_output->buffer() == nullptr);
77 ARM_COMPUTE_ERROR_ON_MSG(_distribution->num_bins() < 256, "Distribution must have 256 bins");
78
79 // Calculate the cumulative distribution (summed histogram).
80 const uint32_t *hist = _distribution->buffer();
81 uint32_t *cumulative_sum = _cumulative_sum->buffer();
82 uint8_t *output = _output->buffer();
83
84 // Calculate cumulative distribution
85 std::partial_sum(hist, hist + _histogram_size, cumulative_sum);
86
87 // Get the number of pixels that have the lowest value in the input image
88 const uint32_t cd_min = *std::find_if(hist, hist + _histogram_size, [](const uint32_t &v)
89 {
90 return v > 0;
91 });
92 const uint32_t image_size = cumulative_sum[_histogram_size - 1];
93
94 ARM_COMPUTE_ERROR_ON(cd_min > image_size);
95
96 // Create mapping lookup table
97 if(image_size == cd_min)
98 {
99 std::iota(output, output + _histogram_size, 0);
100 }
101 else
102 {
103 const float diff = image_size - cd_min;
104
105 for(unsigned int x = 0; x < _histogram_size; ++x)
106 {
107 output[x] = lround((cumulative_sum[x] - cd_min) / diff * 255.0f);
108 }
109 }
110}