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 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_NEHISTOGRAMKERNEL_H |
| 25 | #define ARM_COMPUTE_NEHISTOGRAMKERNEL_H |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 27 | #include "src/core/NEON/INEKernel.h" |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 28 | #include "support/Mutex.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | class IDistribution1D; |
| 36 | class ITensor; |
| 37 | using IImage = ITensor; |
| 38 | |
| 39 | /** Interface for the histogram kernel */ |
| 40 | class NEHistogramKernel : public INEKernel |
| 41 | { |
| 42 | public: |
Anthony Barbier | e8a4983 | 2018-01-18 10:04:05 +0000 | [diff] [blame] | 43 | const char *name() const override |
| 44 | { |
| 45 | return "NEHistogramKernel"; |
| 46 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | /** Default constructor */ |
| 48 | NEHistogramKernel(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 50 | NEHistogramKernel(const NEHistogramKernel &) = delete; |
| 51 | /** Prevent instances of this class from being copied (As this class contains pointers) */ |
| 52 | NEHistogramKernel &operator=(const NEHistogramKernel &) = delete; |
Georgios Pinitas | ca4111d | 2020-02-21 13:21:37 +0000 | [diff] [blame] | 53 | /** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 54 | NEHistogramKernel(NEHistogramKernel &&) = delete; |
| 55 | /** Prevent instances of this class from being moved (As this class contains non movable objects) */ |
| 56 | NEHistogramKernel &operator=(NEHistogramKernel &&) = delete; |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 57 | /** Default destructor */ |
| 58 | ~NEHistogramKernel() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 59 | |
| 60 | /** Set the input image and the distribution output. |
| 61 | * |
| 62 | * @param[in] input Source image. Data type supported: U8. |
| 63 | * @param[out] output Destination distribution. |
| 64 | * @param[in,out] local_hist Array that the threads use to save their local histograms. |
| 65 | * It's size should be equal to (number_of_threads * num_bins), |
| 66 | * and the Window::thread_id() is used to determine the part of the array |
| 67 | * used by each thread. |
| 68 | * @param[out] window_lut LUT with pre-calculated possible window values. |
| 69 | * The size of the LUT should be equal to max_range_size and it will be filled |
| 70 | * during the configure stage, while it re-used in every run, therefore can be |
| 71 | * safely shared among threads. |
| 72 | */ |
| 73 | void configure(const IImage *input, IDistribution1D *output, uint32_t *local_hist, uint32_t *window_lut); |
| 74 | /** Set the input image and the distribution output. |
| 75 | * |
| 76 | * @note Used for histogram of fixed size equal to 256 |
| 77 | * |
| 78 | * @param[in] input Source image. Data type supported: U8. |
| 79 | * @param[out] output Destination distribution which must be of 256 bins.. |
| 80 | */ |
| 81 | void configure(const IImage *input, IDistribution1D *output); |
| 82 | |
| 83 | // Inherited methods overridden: |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 84 | void run(const Window &window, const ThreadInfo &info) override; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | /** Function to merge multiple partial histograms. |
| 88 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 89 | * @param[out] global_hist Pointer to the final histogram. |
| 90 | * @param[in] local_hist Pointer to the partial histograms. |
| 91 | * @param[in] bins Number of bins. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | */ |
| 93 | void merge_histogram(uint32_t *global_hist, const uint32_t *local_hist, size_t bins); |
| 94 | /** Function to merge multiple minimum values of partial histograms. |
| 95 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 96 | * @param[out] global_min Pointer to the global min value. |
| 97 | * @param[in] local_min Local min value. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | */ |
| 99 | void merge_min(uint8_t *global_min, const uint8_t &local_min); |
| 100 | /** Function to perform histogram on the given window |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 101 | * |
| 102 | * @param[in] win Region on which to execute the kernel |
| 103 | * @param[in] info Info about the executing thread |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | */ |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 105 | void histogram_U8(Window win, const ThreadInfo &info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | /** Function to perform histogram on the given window where histogram is |
| 107 | * of fixed size 256 without ranges and offsets. |
| 108 | * |
Anthony Barbier | f202e50 | 2017-11-23 18:02:04 +0000 | [diff] [blame] | 109 | * @param[in] win Region on which to execute the kernel |
| 110 | * @param[in] info Info about the executing thread |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | */ |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 112 | void histogram_fixed_U8(Window win, const ThreadInfo &info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | /** Pre-calculate the pixel windowing for every possible pixel |
| 114 | * |
| 115 | * Calculate (V - offset) * numBins / range where V is every possible pixel value. |
| 116 | * |
| 117 | * @note We currently support U8 image thus possible pixel values are between 0 and 255 |
| 118 | */ |
| 119 | void calculate_window_lut() const; |
| 120 | /** Common signature for all the specialised Histogram functions |
| 121 | * |
| 122 | * @param[in] window Region on which to execute the kernel. |
| 123 | */ |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 124 | using HistogramFunctionPtr = void (NEHistogramKernel::*)(Window window, const ThreadInfo &info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | |
| 126 | HistogramFunctionPtr _func; ///< Histogram function to use for the particular image types passed to configure() |
| 127 | const IImage *_input; |
| 128 | IDistribution1D *_output; |
| 129 | uint32_t *_local_hist; |
| 130 | uint32_t *_window_lut; |
Michalis Spyrou | 07781ac | 2017-08-31 15:11:41 +0100 | [diff] [blame] | 131 | arm_compute::Mutex _hist_mtx; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | static constexpr unsigned int _max_range_size{ 256 }; ///< 256 possible pixel values as we handle only U8 images |
| 133 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 134 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 135 | #endif /*ARM_COMPUTE_NEHISTOGRAMKERNEL_H */ |