blob: 75d00e9d313211fb3e7042dc9790fdcd899da9d6 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
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#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:
43 /** Default constructor */
44 NEHistogramKernel();
45 /** Default destructor */
46 ~NEHistogramKernel() = default;
47 /** Prevent instances of this class from being copied (As this class contains pointers) */
48 NEHistogramKernel(const NEHistogramKernel &) = delete;
49 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 NEHistogramKernel &operator=(const NEHistogramKernel &) = delete;
51 /** Allow instances of this class to be moved */
52 NEHistogramKernel(NEHistogramKernel &&) = default;
53 /** Allow instances of this class to be moved */
54 NEHistogramKernel &operator=(NEHistogramKernel &&) = default;
55
56 /** Set the input image and the distribution output.
57 *
58 * @param[in] input Source image. Data type supported: U8.
59 * @param[out] output Destination distribution.
60 * @param[in,out] local_hist Array that the threads use to save their local histograms.
61 * It's size should be equal to (number_of_threads * num_bins),
62 * and the Window::thread_id() is used to determine the part of the array
63 * used by each thread.
64 * @param[out] window_lut LUT with pre-calculated possible window values.
65 * The size of the LUT should be equal to max_range_size and it will be filled
66 * during the configure stage, while it re-used in every run, therefore can be
67 * safely shared among threads.
68 */
69 void configure(const IImage *input, IDistribution1D *output, uint32_t *local_hist, uint32_t *window_lut);
70 /** Set the input image and the distribution output.
71 *
72 * @note Used for histogram of fixed size equal to 256
73 *
74 * @param[in] input Source image. Data type supported: U8.
75 * @param[out] output Destination distribution which must be of 256 bins..
76 */
77 void configure(const IImage *input, IDistribution1D *output);
78
79 // Inherited methods overridden:
Moritz Pflanzerc186b572017-09-07 09:48:04 +010080 void run(const Window &window, const ThreadInfo &info) override;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010081
82private:
83 /** Function to merge multiple partial histograms.
84 *
85 * @param[out] global_hist Pointer to the final histogram.
86 * @param[in] local_hist Pointer to the partial histograms.
87 * @param[in] bins Number of bins.
88 */
89 void merge_histogram(uint32_t *global_hist, const uint32_t *local_hist, size_t bins);
90 /** Function to merge multiple minimum values of partial histograms.
91 *
92 * @param[out] global_min Pointer to the global min value.
93 * @param[in] local_min Local min value.
94 */
95 void merge_min(uint8_t *global_min, const uint8_t &local_min);
96 /** Function to perform histogram on the given window
97 *
Moritz Pflanzerc186b572017-09-07 09:48:04 +010098 * @param[in] win Region on which to execute the kernel
99 * @param[in] info Info about the executing thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100101 void histogram_U8(Window win, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102 /** Function to perform histogram on the given window where histogram is
103 * of fixed size 256 without ranges and offsets.
104 *
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100105 * @param[in] win Region on which to execute the kernel
106 * @param[in] info Info about the executing thread
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100108 void histogram_fixed_U8(Window win, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109 /** Pre-calculate the pixel windowing for every possible pixel
110 *
111 * Calculate (V - offset) * numBins / range where V is every possible pixel value.
112 *
113 * @note We currently support U8 image thus possible pixel values are between 0 and 255
114 */
115 void calculate_window_lut() const;
116 /** Common signature for all the specialised Histogram functions
117 *
118 * @param[in] window Region on which to execute the kernel.
119 */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100120 using HistogramFunctionPtr = void (NEHistogramKernel::*)(Window window, const ThreadInfo &info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121
122 HistogramFunctionPtr _func; ///< Histogram function to use for the particular image types passed to configure()
123 const IImage *_input;
124 IDistribution1D *_output;
125 uint32_t *_local_hist;
126 uint32_t *_window_lut;
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100127 arm_compute::Mutex _hist_mtx;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 static constexpr unsigned int _max_range_size{ 256 }; ///< 256 possible pixel values as we handle only U8 images
129};
130}
131#endif /*__ARM_COMPUTE_NEHISTOGRAMKERNEL_H__ */