Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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/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 Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 31 | #include "src/core/CL/kernels/CLHistogramKernel.h" |
| 32 | #include "src/core/CL/kernels/CLTableLookupKernel.h" |
| 33 | #include "support/MemorySupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <algorithm> |
| 36 | #include <cmath> |
| 37 | #include <cstddef> |
| 38 | #include <numeric> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | void calculate_cum_dist_and_lut(CLDistribution1D &dist, CLDistribution1D &cum_dist, CLLut &lut) |
| 45 | { |
| 46 | dist.map(true); |
| 47 | cum_dist.map(true); |
| 48 | lut.map(true); |
| 49 | |
| 50 | const uint32_t *dist_ptr = dist.buffer(); |
| 51 | uint32_t *cum_dist_ptr = cum_dist.buffer(); |
| 52 | uint8_t *lut_ptr = lut.buffer(); |
| 53 | |
| 54 | ARM_COMPUTE_ERROR_ON(dist_ptr == nullptr); |
| 55 | ARM_COMPUTE_ERROR_ON(cum_dist_ptr == nullptr); |
| 56 | ARM_COMPUTE_ERROR_ON(lut_ptr == nullptr); |
| 57 | |
| 58 | // Calculate cumulative distribution |
| 59 | std::partial_sum(dist_ptr, dist_ptr + 256, cum_dist_ptr); |
| 60 | |
| 61 | // Get the number of pixels that have the lowest value in the input image |
| 62 | const uint32_t num_lowest_pixels = *std::find_if(dist_ptr, dist_ptr + 256, [](const uint32_t &v) |
| 63 | { |
| 64 | return v > 0; |
| 65 | }); |
| 66 | const size_t image_size = cum_dist_ptr[255]; |
| 67 | |
| 68 | if(image_size == num_lowest_pixels) |
| 69 | { |
| 70 | std::iota(lut_ptr, lut_ptr + 256, 0); |
| 71 | } |
| 72 | else |
| 73 | { |
Michele Di Giorgio | 7cd7021 | 2018-09-05 16:29:28 +0100 | [diff] [blame] | 74 | const float diff = image_size - num_lowest_pixels; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
| 76 | for(size_t i = 0; i < 256; ++i) |
| 77 | { |
| 78 | lut_ptr[i] = lround((cum_dist_ptr[i] - num_lowest_pixels) / diff * 255.f); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | dist.unmap(); |
| 83 | cum_dist.unmap(); |
| 84 | lut.unmap(); |
| 85 | } |
| 86 | } // namespace |
| 87 | |
| 88 | CLEqualizeHistogram::CLEqualizeHistogram() |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 89 | : _histogram_kernel(support::cpp14::make_unique<CLHistogramKernel>()), |
| 90 | _border_histogram_kernel(support::cpp14::make_unique<CLHistogramBorderKernel>()), |
| 91 | _map_histogram_kernel(support::cpp14::make_unique<CLTableLookupKernel>()), |
| 92 | _hist(nr_bins, 0, max_range), |
| 93 | _cum_dist(nr_bins, 0, max_range), |
| 94 | _cd_lut(nr_bins, DataType::U8) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | { |
| 96 | } |
| 97 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 98 | CLEqualizeHistogram::~CLEqualizeHistogram() = default; |
| 99 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | void CLEqualizeHistogram::configure(const ICLImage *input, ICLImage *output) |
| 101 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 102 | configure(CLKernelLibrary::get().get_compile_context(), input, output); |
| 103 | } |
| 104 | |
| 105 | void CLEqualizeHistogram::configure(const CLCompileContext &compile_context, const ICLImage *input, ICLImage *output) |
| 106 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 107 | _histogram_kernel->configure(compile_context, input, &_hist); |
| 108 | _border_histogram_kernel->configure(compile_context, input, &_hist); |
| 109 | _map_histogram_kernel->configure(compile_context, input, &_cd_lut, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void CLEqualizeHistogram::run() |
| 113 | { |
| 114 | // Calculate histogram of input. |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 115 | CLScheduler::get().enqueue(*_histogram_kernel, false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | |
| 117 | // Calculate remaining pixels when image is not multiple of the elements of histogram kernel |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 118 | CLScheduler::get().enqueue(*_border_histogram_kernel, false); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | |
| 120 | // Calculate cumulative distribution of histogram and create LUT. |
| 121 | calculate_cum_dist_and_lut(_hist, _cum_dist, _cd_lut); |
| 122 | |
| 123 | // Map input to output using created LUT. |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame^] | 124 | CLScheduler::get().enqueue(*_map_histogram_kernel); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | } |