blob: 086353bee257a6d2e428ce0f97ac46638fba6a31 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Anthony Barbiere8a49832018-01-18 10:04:05 +00002 * Copyright (c) 2016-2018 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#ifndef __ARM_COMPUTE_NEHISTOGRAMKERNEL_H__
25#define __ARM_COMPUTE_NEHISTOGRAMKERNEL_H__
26
27#include "arm_compute/core/NEON/INEKernel.h"
Michalis Spyrou07781ac2017-08-31 15:11:41 +010028#include "support/Mutex.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30#include <cstddef>
31#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032
33namespace arm_compute
34{
35class IDistribution1D;
36class ITensor;
37using IImage = ITensor;
38
39/** Interface for the histogram kernel */
40class NEHistogramKernel : public INEKernel
41{
42public:
Anthony Barbiere8a49832018-01-18 10:04:05 +000043 const char *name() const override
44 {
45 return "NEHistogramKernel";
46 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +010047 /** Default constructor */
48 NEHistogramKernel();
49 /** Default destructor */
50 ~NEHistogramKernel() = default;
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 NEHistogramKernel(const NEHistogramKernel &) = delete;
53 /** Prevent instances of this class from being copied (As this class contains pointers) */
54 NEHistogramKernel &operator=(const NEHistogramKernel &) = delete;
55 /** Allow instances of this class to be moved */
56 NEHistogramKernel(NEHistogramKernel &&) = default;
57 /** Allow instances of this class to be moved */
58 NEHistogramKernel &operator=(NEHistogramKernel &&) = default;
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 Pflanzerc186b572017-09-07 09:48:04 +010084 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085
86private:
87 /** Function to merge multiple partial histograms.
88 *
Anthony Barbierf202e502017-11-23 18:02:04 +000089 * @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 Barbier6ff3b192017-09-04 18:44:23 +010092 */
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 Barbierf202e502017-11-23 18:02:04 +000096 * @param[out] global_min Pointer to the global min value.
97 * @param[in] local_min Local min value.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098 */
99 void merge_min(uint8_t *global_min, const uint8_t &local_min);
100 /** Function to perform histogram on the given window
Anthony Barbierf202e502017-11-23 18:02:04 +0000101 *
102 * @param[in] win Region on which to execute the kernel
103 * @param[in] info Info about the executing thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100105 void histogram_U8(Window win, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 /** Function to perform histogram on the given window where histogram is
107 * of fixed size 256 without ranges and offsets.
108 *
Anthony Barbierf202e502017-11-23 18:02:04 +0000109 * @param[in] win Region on which to execute the kernel
110 * @param[in] info Info about the executing thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100111 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100112 void histogram_fixed_U8(Window win, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 /** 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 Pflanzerc186b572017-09-07 09:48:04 +0100124 using HistogramFunctionPtr = void (NEHistogramKernel::*)(Window window, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125
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 Spyrou07781ac2017-08-31 15:11:41 +0100131 arm_compute::Mutex _hist_mtx;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 static constexpr unsigned int _max_range_size{ 256 }; ///< 256 possible pixel values as we handle only U8 images
133};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100134} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135#endif /*__ARM_COMPUTE_NEHISTOGRAMKERNEL_H__ */