fw4spl
src/fwData/Histogram.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwData/registry/macros.hpp"
8 #include "fwData/Exception.hpp"
9 #include "fwData/Image.hpp"
10 #include "fwData/Histogram.hpp"
11 
12 
13 fwDataRegisterMacro( ::fwData::Histogram );
14 
15 
16 namespace fwData
17 {
18 
19 Histogram::Histogram(::fwData::Object::Key key) : m_binsWidth(1.f),
20  m_minValue(0.f),
21  m_maxValue(100.f)
22 {
23 }
24 
25 //------------------------------------------------------------------------------
26 
28 {
30 }
31 
32 //------------------------------------------------------------------------------
33 
34 void Histogram::shallowCopy(const Object::csptr &_source )
35 {
36  Histogram::csptr other = Histogram::dynamicConstCast(_source);
37  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
38  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
39  + " to " + this->getClassname()), !bool(other) );
40  this->fieldShallowCopy( _source );
41  m_values = other->m_values;
42  m_minValue = other->m_minValue;
43  m_maxValue = other->m_maxValue;
44  m_binsWidth = other->m_binsWidth;
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 void Histogram::cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache)
50 {
51  Histogram::csptr other = Histogram::dynamicConstCast(_source);
52  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
53  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
54  + " to " + this->getClassname()), !bool(other) );
55  this->fieldDeepCopy( _source, cache );
56 
57  m_minValue = other->m_minValue;
58  m_maxValue = other->m_maxValue;
59  m_binsWidth = other->m_binsWidth;
60 
61  m_values.clear();
62 
63  for(long value : other->m_values )
64  {
65  m_values.push_back( value );
66  }
67 }
68 
69 //------------------------------------------------------------------------------
70 
71 void Histogram::addPixel( float _pixel )
72 {
73  if( this->isInRange( _pixel ) )
74  {
75  int index = static_cast<int>(( _pixel - m_minValue ) / m_binsWidth);
76  m_values[ index ]++;
77  }
78 }
79 
80 //------------------------------------------------------------------------------
81 
82 void Histogram::initialize( float _min, float _max, float _binsWidth )
83 {
84  SLM_ASSERT("The minimum value can't be greater than the maximum value", _min <= _max);
85 
86  m_minValue = _min;
87  m_maxValue = _max;
88  m_binsWidth = _binsWidth;
89 
90  m_values.clear();
91 
92  if( m_binsWidth != 0 )
93  {
94  int newSize = static_cast<int>(( m_maxValue - m_minValue ) / m_binsWidth);
95  m_values.resize( newSize + 1, 0 );
96  }
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 long Histogram::getNbPixels( float _min, float _max )
102 {
103  SLM_ASSERT("The minimum value can't be greater than the maximum value", _min < _max);
104 
105  size_t indexMin = 0;
106  if( _min >= m_minValue )
107  {
108  indexMin = static_cast<size_t>(( _min - m_minValue ) / m_binsWidth);
109  }
110  size_t indexMax = m_values.size();
111  if( _max <= m_maxValue )
112  {
113  indexMax = static_cast<size_t>(( _max - m_minValue ) / m_binsWidth);
114  }
115  long nbPixels = 0;
116 
117  while( indexMin < indexMax )
118  {
119  nbPixels += m_values.at( indexMin++ );
120  }
121 
122  return nbPixels;
123 }
124 
125 //------------------------------------------------------------------------------
126 
127 bool Histogram::isInRange( float _pixel )
128 {
129  return ( _pixel >= m_minValue && _pixel <= m_maxValue );
130 }
131 
132 } // namespace fwData
133 
fwHistogramValues m_values
Histogram values.
Definition: Histogram.hpp:124
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
float m_minValue
The minimum pixel value within the histogram (a default value is set to 0).
Definition: Histogram.hpp:130
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
This class defines the histogram of an image.
Definition: Histogram.hpp:21
float m_maxValue
The maximum pixel value within the histogram (a default value is set to 100).
Definition: Histogram.hpp:133
float m_binsWidth
The pixel width of the bins (a default value is set to 1).
Definition: Histogram.hpp:127
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWDATA_API long getNbPixels(float _min, float _max)
Return the number of pixels of the histogram that are within the range defined by the given parameter...
virtual FWDATA_API ~Histogram()
Destructor.
FWDATA_API bool isInRange(float _pixel)
Return true if the given pixel value is set within histogram&#39;s boudaries.
FWDATA_API Histogram(::fwData::Object::Key key)
Constructor.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
FWDATA_API void initialize(float min, float max, float binsWidth)
Initialize the histogram.
FWDATA_API void addPixel(float pixel)
Add the given pixel value into the histogram.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
Definition: Histogram.hpp:26