DGtal  1.4.beta
GrayscaleColorMap.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file GrayscaleColorMap.ih
19  * @author Sebastien Fourey (\c Sebastien.Fourey@greyc.ensicaen.fr )
20  * Groupe de Recherche en Informatique, Image, Automatique et Instrumentation de Caen - GREYC (CNRS, UMR 6072), ENSICAEN, France
21  *
22  * @date 2010/07/19
23  *
24  * Implementation of inline methods defined in GrayscaleColorMap.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 template <typename Value>
42 inline
43 DGtal::GrayscaleColorMap<Value>::GrayscaleColorMap( const Value & aMin,
44  const Value & aMax )
45  : myMin( aMin ), myMax( aMax )
46 {
47  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
48 }
49 
50 template <typename Value>
51 inline
52 DGtal::GrayscaleColorMap<Value>::GrayscaleColorMap
53 ( const GrayscaleColorMap<Value> & other )
54  : myMin( other.myMin ), myMax( other.myMax )
55 {
56  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
57 }
58 
59 template <typename Value>
60 inline
61 DGtal::GrayscaleColorMap<Value>::~GrayscaleColorMap()
62 {
63 }
64 
65 template <typename Value>
66 DGtal::GrayscaleColorMap<Value> &
67 DGtal::GrayscaleColorMap<Value>::operator=
68 ( const GrayscaleColorMap<Value> & other )
69 {
70  if ( &other != this ) {
71  myMin = other.myMin;
72  myMax = other.myMax;
73  ASSERT_MSG(myMin < myMax, "Max should be strictly greather than Min in a colormap.");
74  }
75  return *this;
76 }
77 
78 ///////////////////////////////////////////////////////////////////////////////
79 // Interface - public :
80 
81 
82 template<typename Value>
83 inline
84 const Value &
85 DGtal::GrayscaleColorMap<Value>::min() const
86 {
87  return myMin;
88 }
89 
90 template<typename Value>
91 inline
92 const Value &
93 DGtal::GrayscaleColorMap<Value>::max() const
94 {
95  return myMax;
96 }
97 
98 template<typename Value>
99 inline
100 DGtal::Color
101 DGtal::GrayscaleColorMap<Value>::operator()( const Value & value ) const
102 {
103  return GrayscaleColorMap<Value>::getColor( myMin, myMax, value );
104 }
105 
106 /**
107  * Writes/Displays the object on an output stream.
108  * @param out the output stream where the object is written.
109  */
110 template <typename Value>
111 inline
112 void
113 DGtal::GrayscaleColorMap<Value>::selfDisplay ( std::ostream & out ) const
114 {
115  out << "[GrayscaleColorMap "
116  << " min=" << myMin
117  << " max=" << myMax
118  << " ]";
119 }
120 
121 /**
122  * Checks the validity/consistency of the object.
123  * @return 'true' if the object is valid, 'false' otherwise.
124  */
125 template <typename Value>
126 inline
127 bool
128 DGtal::GrayscaleColorMap<Value>::isValid() const
129 {
130  return true;
131 }
132 
133 
134 template <typename Value>
135 inline
136 DGtal::Color
137 DGtal::GrayscaleColorMap<Value>::getColor( const Value & min,
138  const Value & max,
139  const Value & value )
140 {
141  ASSERT_MSG(min < max, "Max should be strictly greather than Min in a colormap.");
142  double range = static_cast<double>( max - min );
143  double scale = static_cast<double>( value - min );
144  unsigned char gray = static_cast<unsigned char>( 255.0 * ( scale / range ) );
145  return Color( gray );
146 }
147 
148 ///////////////////////////////////////////////////////////////////////////////
149 // Implementation of inline functions //
150 
151 template <typename Value>
152 inline
153 std::ostream&
154 DGtal::operator<< ( std::ostream & out,
155  const GrayscaleColorMap<Value> & object )
156 {
157  object.selfDisplay( out );
158  return out;
159 }
160 
161 // //
162 ///////////////////////////////////////////////////////////////////////////////
163 
164