VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grhistogram.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file grhistogram.h
19 ///
20 
21 #ifndef GR_HISTOGRAM__H
22 #define GR_HISTOGRAM__H
23 
24 #include "gdef.h"
25 #include "gstaticassert.h"
26 #include <vector>
27 #include <algorithm>
28 
29 namespace Gr
30 {
31  class Histogram ;
32 }
33 
34 /// \class Gr::Histogram
35 /// A class that does histogram equalisation for eight-bit samples.
36 ///
37 /// The implementation goes for speed over space by holding a complete
38 /// map of pixel values rather than just the pixel values that appear
39 /// in the image.
40 ///
42 {
43 public:
44  typedef unsigned long sum_type ;
45  typedef unsigned char value_type ;
46  enum { N = 256 } ;
47  G_STATIC_ASSERT( sizeof(value_type) == 1 ) ;
48 
49  Histogram() ;
50  ///< Constructor.
51  ///< Postcondition: active()
52 
53  explicit Histogram( bool active ) ;
54  ///< Constructor, optionally for an unusable, zero-cost object.
55 
56  void clear() ;
57  ///< Clears the map.
58 
59  void add( value_type ) ;
60  ///< Adds a pixel to the histogram.
61  ///< Precondition: active()
62 
63  void compute() ;
64  ///< Computes the equalisation map once all the pixels
65  ///< have been add()ed.
66 
67  value_type map( value_type ) const ;
68  ///< Does the equalisation mapping.
69 
70  bool active() const ;
71  ///< Returns true if constructed as active.
72 
73 private:
74  typedef std::vector<sum_type> Vector ;
75  Vector m_data ;
76 
77 private:
78  template <typename TValue,typename TSum>
79  struct Accumulator
80  {
81  typedef TValue value_type ;
82  typedef TSum sum_type ;
83  sum_type & n ;
84  Accumulator( sum_type & n_ ) : n(n_) {}
85  void operator()( sum_type & v ) { n += v ; v = n ; }
86  } ;
87  template <typename TValue,typename TSum>
88  struct Scaler
89  {
90  typedef TValue value_type ;
91  typedef TSum sum_type ;
92  sum_type min ;
93  sum_type divisor ;
94  Scaler( value_type min_ , sum_type total_ ) : min(min_) , divisor(total_-min_) {}
95  void operator()( sum_type & v )
96  {
97  const sum_type n = ( v - min ) * ( N - 1 ) ;
98  v = (value_type)( n / divisor ) ;
99  }
100  } ;
101 } ;
102 
103 inline
105  m_data(N)
106 {
107 }
108 
109 inline
111 {
112  if( active )
113  m_data.resize( N ) ;
114 }
115 
116 inline
117 void Gr::Histogram::add( value_type n )
118 {
119  m_data[n]++ ;
120 }
121 
122 inline
124 {
125  m_data.assign( N , 0 ) ;
126 }
127 
128 inline
130 {
131  const Vector::iterator end = m_data.end() ;
132  Vector::iterator min_p = std::find_if( m_data.begin() , end , std::bind2nd(std::not_equal_to<value_type>(),0) ) ;
133  if( min_p == end ) return ;
134  sum_type n = 0 ;
135  std::for_each( m_data.begin() , end , Accumulator<value_type,sum_type>(n) ) ;
136  std::for_each( m_data.begin() , end , Scaler<value_type,sum_type>(*min_p,m_data.back()) ) ;
137 }
138 
139 inline
140 Gr::Histogram::value_type Gr::Histogram::map( value_type n ) const
141 {
142  return m_data[n] ;
143 }
144 
145 inline
147 {
148  return !m_data.empty() ;
149 }
150 
151 #endif
void compute()
Computes the equalisation map once all the pixels have been add()ed.
Definition: grhistogram.h:129
A class that does histogram equalisation for eight-bit samples.
Definition: grhistogram.h:41
void add(value_type)
Adds a pixel to the histogram.
Definition: grhistogram.h:117
void clear()
Clears the map.
Definition: grhistogram.h:123
Histogram()
Constructor.
Definition: grhistogram.h:104
bool active() const
Returns true if constructed as active.
Definition: grhistogram.h:146
value_type map(value_type) const
Does the equalisation mapping.
Definition: grhistogram.h:140