blob: c4dbbeae836e74a1e36cd2114b49ac06c539b02d [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"
28
29#include <cstddef>
30#include <cstdint>
31#include <mutex>
32
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:
80 void run(const Window &window) override;
81
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 *
98 * @param[in] win Region on which to execute the kernel
99 */
100 void histogram_U8(Window win);
101 /** Function to perform histogram on the given window where histogram is
102 * of fixed size 256 without ranges and offsets.
103 *
104 * @param[in] win Region on which to execute the kernel
105 */
106 void histogram_fixed_U8(Window win);
107 /** Pre-calculate the pixel windowing for every possible pixel
108 *
109 * Calculate (V - offset) * numBins / range where V is every possible pixel value.
110 *
111 * @note We currently support U8 image thus possible pixel values are between 0 and 255
112 */
113 void calculate_window_lut() const;
114 /** Common signature for all the specialised Histogram functions
115 *
116 * @param[in] window Region on which to execute the kernel.
117 */
118 using HistogramFunctionPtr = void (NEHistogramKernel::*)(Window window);
119
120 HistogramFunctionPtr _func; ///< Histogram function to use for the particular image types passed to configure()
121 const IImage *_input;
122 IDistribution1D *_output;
123 uint32_t *_local_hist;
124 uint32_t *_window_lut;
125 std::mutex _hist_mtx;
126 static constexpr unsigned int _max_range_size{ 256 }; ///< 256 possible pixel values as we handle only U8 images
127};
128}
129#endif /*__ARM_COMPUTE_NEHISTOGRAMKERNEL_H__ */