blob: 11607cf71db33f116b4e5faacfbf3e6d1d089310 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 "arm_compute/runtime/CL/functions/CLEqualizeHistogram.h"
25
26#include "arm_compute/core/CL/ICLDistribution1D.h"
27#include "arm_compute/core/CL/ICLLut.h"
28#include "arm_compute/core/CL/OpenCL.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010031#include "src/core/CL/kernels/CLHistogramKernel.h"
32#include "src/core/CL/kernels/CLTableLookupKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34#include <algorithm>
35#include <cmath>
36#include <cstddef>
37#include <numeric>
38
39using namespace arm_compute;
40
41namespace
42{
43void calculate_cum_dist_and_lut(CLDistribution1D &dist, CLDistribution1D &cum_dist, CLLut &lut)
44{
45 dist.map(true);
46 cum_dist.map(true);
47 lut.map(true);
48
49 const uint32_t *dist_ptr = dist.buffer();
50 uint32_t *cum_dist_ptr = cum_dist.buffer();
51 uint8_t *lut_ptr = lut.buffer();
52
53 ARM_COMPUTE_ERROR_ON(dist_ptr == nullptr);
54 ARM_COMPUTE_ERROR_ON(cum_dist_ptr == nullptr);
55 ARM_COMPUTE_ERROR_ON(lut_ptr == nullptr);
56
57 // Calculate cumulative distribution
58 std::partial_sum(dist_ptr, dist_ptr + 256, cum_dist_ptr);
59
60 // Get the number of pixels that have the lowest value in the input image
61 const uint32_t num_lowest_pixels = *std::find_if(dist_ptr, dist_ptr + 256, [](const uint32_t &v)
62 {
63 return v > 0;
64 });
65 const size_t image_size = cum_dist_ptr[255];
66
67 if(image_size == num_lowest_pixels)
68 {
69 std::iota(lut_ptr, lut_ptr + 256, 0);
70 }
71 else
72 {
Michele Di Giorgio7cd70212018-09-05 16:29:28 +010073 const float diff = image_size - num_lowest_pixels;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074
75 for(size_t i = 0; i < 256; ++i)
76 {
77 lut_ptr[i] = lround((cum_dist_ptr[i] - num_lowest_pixels) / diff * 255.f);
78 }
79 }
80
81 dist.unmap();
82 cum_dist.unmap();
83 lut.unmap();
84}
85} // namespace
86
87CLEqualizeHistogram::CLEqualizeHistogram()
Georgios Pinitas40f51a62020-11-21 03:04:18 +000088 : _histogram_kernel(std::make_unique<CLHistogramKernel>()),
89 _border_histogram_kernel(std::make_unique<CLHistogramBorderKernel>()),
90 _map_histogram_kernel(std::make_unique<CLTableLookupKernel>()),
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010091 _hist(nr_bins, 0, max_range),
92 _cum_dist(nr_bins, 0, max_range),
93 _cd_lut(nr_bins, DataType::U8)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
95}
96
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010097CLEqualizeHistogram::~CLEqualizeHistogram() = default;
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099void CLEqualizeHistogram::configure(const ICLImage *input, ICLImage *output)
100{
Manuel Bottini2b84be52020-04-08 10:15:51 +0100101 configure(CLKernelLibrary::get().get_compile_context(), input, output);
102}
103
104void CLEqualizeHistogram::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLImage *output)
105{
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100106 _histogram_kernel->configure(compile_context, input, &_hist);
107 _border_histogram_kernel->configure(compile_context, input, &_hist);
108 _map_histogram_kernel->configure(compile_context, input, &_cd_lut, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109}
110
111void CLEqualizeHistogram::run()
112{
113 // Calculate histogram of input.
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100114 CLScheduler::get().enqueue(*_histogram_kernel, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115
116 // Calculate remaining pixels when image is not multiple of the elements of histogram kernel
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100117 CLScheduler::get().enqueue(*_border_histogram_kernel, false);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
119 // Calculate cumulative distribution of histogram and create LUT.
120 calculate_cum_dist_and_lut(_hist, _cum_dist, _cd_lut);
121
122 // Map input to output using created LUT.
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +0100123 CLScheduler::get().enqueue(*_map_histogram_kernel);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124}