Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 0434d75 | 2017-10-30 13:02:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 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" |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <cmath> |
| 34 | #include <cstddef> |
| 35 | #include <numeric> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace |
| 40 | { |
| 41 | void calculate_cum_dist_and_lut(CLDistribution1D &dist, CLDistribution1D &cum_dist, CLLut &lut) |
| 42 | { |
| 43 | dist.map(true); |
| 44 | cum_dist.map(true); |
| 45 | lut.map(true); |
| 46 | |
| 47 | const uint32_t *dist_ptr = dist.buffer(); |
| 48 | uint32_t *cum_dist_ptr = cum_dist.buffer(); |
| 49 | uint8_t *lut_ptr = lut.buffer(); |
| 50 | |
| 51 | ARM_COMPUTE_ERROR_ON(dist_ptr == nullptr); |
| 52 | ARM_COMPUTE_ERROR_ON(cum_dist_ptr == nullptr); |
| 53 | ARM_COMPUTE_ERROR_ON(lut_ptr == nullptr); |
| 54 | |
| 55 | // Calculate cumulative distribution |
| 56 | std::partial_sum(dist_ptr, dist_ptr + 256, cum_dist_ptr); |
| 57 | |
| 58 | // Get the number of pixels that have the lowest value in the input image |
| 59 | const uint32_t num_lowest_pixels = *std::find_if(dist_ptr, dist_ptr + 256, [](const uint32_t &v) |
| 60 | { |
| 61 | return v > 0; |
| 62 | }); |
| 63 | const size_t image_size = cum_dist_ptr[255]; |
| 64 | |
| 65 | if(image_size == num_lowest_pixels) |
| 66 | { |
| 67 | std::iota(lut_ptr, lut_ptr + 256, 0); |
| 68 | } |
| 69 | else |
| 70 | { |
Michele Di Giorgio | 7cd7021 | 2018-09-05 16:29:28 +0100 | [diff] [blame] | 71 | const float diff = image_size - num_lowest_pixels; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 72 | |
| 73 | for(size_t i = 0; i < 256; ++i) |
| 74 | { |
| 75 | lut_ptr[i] = lround((cum_dist_ptr[i] - num_lowest_pixels) / diff * 255.f); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | dist.unmap(); |
| 80 | cum_dist.unmap(); |
| 81 | lut.unmap(); |
| 82 | } |
| 83 | } // namespace |
| 84 | |
| 85 | CLEqualizeHistogram::CLEqualizeHistogram() |
| 86 | : _histogram_kernel(), _border_histogram_kernel(), _map_histogram_kernel(), _hist(nr_bins, 0, max_range), _cum_dist(nr_bins, 0, max_range), _cd_lut(nr_bins, DataType::U8) |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void CLEqualizeHistogram::configure(const ICLImage *input, ICLImage *output) |
| 91 | { |
| 92 | _histogram_kernel.configure(input, &_hist); |
| 93 | _border_histogram_kernel.configure(input, &_hist); |
| 94 | _map_histogram_kernel.configure(input, &_cd_lut, output); |
| 95 | } |
| 96 | |
| 97 | void CLEqualizeHistogram::run() |
| 98 | { |
| 99 | // Calculate histogram of input. |
| 100 | CLScheduler::get().enqueue(_histogram_kernel, false); |
| 101 | |
| 102 | // Calculate remaining pixels when image is not multiple of the elements of histogram kernel |
| 103 | CLScheduler::get().enqueue(_border_histogram_kernel, false); |
| 104 | |
| 105 | // Calculate cumulative distribution of histogram and create LUT. |
| 106 | calculate_cum_dist_and_lut(_hist, _cum_dist, _cd_lut); |
| 107 | |
| 108 | // Map input to output using created LUT. |
| 109 | CLScheduler::get().enqueue(_map_histogram_kernel); |
| 110 | } |