ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkDefaultConvertPixelTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkDefaultConvertPixelTraits_h
19 #define __itkDefaultConvertPixelTraits_h
20 
21 #include "itkOffset.h"
22 #include "itkVector.h"
23 #include "itkMatrix.h"
25 #include "itkVariableSizeMatrix.h"
26 
27 namespace itk
28 {
40 template< typename PixelType >
42 {
43 public:
45  typedef typename PixelType::ComponentType ComponentType;
46 
48  static unsigned int GetNumberOfComponents()
49  { return PixelType::GetNumberOfComponents(); }
50 
51  static unsigned int GetNumberOfComponents( const PixelType itkNotUsed(pixel) )
52  { return PixelType::GetNumberOfComponents( ); }
53 
55  static ComponentType GetNthComponent(int c, const PixelType & pixel)
56  { return pixel.GetNthComponent(c); }
57 
59  static void SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
60  { pixel.SetNthComponent(c, v); }
61 
63  static ComponentType GetScalarValue(const PixelType & pixel)
64  { return pixel.GetScalarValue(); }
65 };
67 
68 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
69  template< > \
70  class DefaultConvertPixelTraits< type > \
71  { \
72 public: \
73  typedef type ComponentType; \
74  static unsigned int GetNumberOfComponents() \
75  { \
76  return 1; \
77  } \
78  static unsigned int GetNumberOfComponents(const type) \
79  { \
80  return 1; \
81  } \
82  static void SetNthComponent(int, type & pixel, const ComponentType &v) \
83  { \
84  pixel = v; \
85  } \
86  static type GetNthComponent(int, const type pixel) \
87  { \
88  return pixel; \
89  } \
90  static type GetScalarValue(const type &pixel) \
91  { \
92  return pixel; \
93  } \
94  };
95 
108 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long long)
110 
111 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
112 
113 //
114 // Default traits for the Offset<> pixel type
115 //
116 
117 template<unsigned int VDimension>
118 class DefaultConvertPixelTraits< Offset< VDimension > >
119 {
120 public:
123  static unsigned int GetNumberOfComponents()
124  {
125  return VDimension;
126  }
127  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
128  {
129  pixel[i] = v;
130  }
132  {
133  return pixel[0];
134  }
135 };
136 
137 //
138 // Default traits for the pixel types deriving from FixedArray<>
139 //
140 
141 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
142  template<typename TComponentType, unsigned VDimension > \
143  class DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
144  { \
145  public: \
146  typedef type< TComponentType, VDimension > TargetType; \
147  typedef TComponentType ComponentType; \
148  static unsigned int GetNumberOfComponents() \
149  { \
150  return VDimension; \
151  } \
152  static unsigned int GetNumberOfComponents( const TargetType ) \
153  { \
154  return VDimension; \
155  } \
156  static void SetNthComponent(int i, TargetType & pixel, \
157  const ComponentType &v) \
158  { \
159  pixel[i] = v; \
160  } \
161  static ComponentType GetNthComponent(int i, const TargetType pixel) \
162  { \
163  return pixel[i]; \
164  } \
165  static ComponentType GetScalarValue(const TargetType &pixel) \
166  { \
167  return pixel[0]; \
168  } \
169  } \
170 
175 
176 //
177 // End of Traits for the classes deriving from FixedArray.
178 //
179 //
180 
181 //
182 // Default traits for pixel types deriving from VariableLengthVector<>
183 //
184 template<typename VComponent>
186 {
187 public:
189  typedef VComponent ComponentType;
190  static unsigned int GetNumberOfComponents()
191  {
192  return 0;
193  }
194  static unsigned int GetNumberOfComponents( const TargetType pixel )
195  {
196  return pixel.Size();
197  }
198  static void SetNthComponent(int i, TargetType & pixel,
199  const ComponentType &v)
200  {
201  pixel[i] = v;
202  }
203  static ComponentType GetNthComponent(int i, const TargetType & pixel)
204  {
205  return pixel[i];
206  }
208  {
209  return pixel.GetNorm();
210  }
211 };
212 
213 
214 //
215 // Default traits for pixel types deriving from VariableSizeMatrix<>
216 //
217 template<typename VComponent>
219 {
220 public:
222  typedef VComponent ComponentType;
223  static unsigned int GetNumberOfComponents()
224  {
225  return 0;
226  }
227  static unsigned int GetNumberOfComponents( const TargetType pixel )
228  {
229  return pixel.Cols() * pixel.Rows();
230  }
231  static void SetNthComponent(int i, TargetType & pixel,
232  const ComponentType &v)
233  {
234  const unsigned int row = i / pixel.Cols();
235  const unsigned int col = i % pixel.Cols();
236  pixel(row,col) = v;
237  }
238  static ComponentType GetNthComponent(int i, const TargetType & pixel)
239  {
240  const unsigned int row = i / pixel.Cols();
241  const unsigned int col = i % pixel.Cols();
242  return pixel(row,col);
243  }
245  {
246  return 0.0;
247  }
248 };
249 
250 
251 //
252 // End of Traits for the classes deriving from FixedArray.
253 //
254 //
255 
256 //
257 // Default traits for the pixel types deriving from Matrix<>
258 //
259 
260 template<typename VComponent, unsigned VRows, unsigned VCols >
261 class DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
262 {
263 public:
265  typedef VComponent ComponentType;
266  static unsigned int GetNumberOfComponents()
267  {
268  return VRows * VCols;
269  }
270  static void SetNthComponent(int i, TargetType & pixel,
271  const ComponentType &v)
272  {
273  const unsigned int row = i / VCols;
274  const unsigned int col = i % VCols;
275  pixel[row][col] = v;
276  }
277  static ComponentType GetNthComponent(int i, const TargetType & pixel)
278  {
279  const unsigned int row = i / VCols;
280  const unsigned int col = i % VCols;
281  return pixel[row][col];
282  }
284  {
285  return pixel[0][0];
286  }
287 };
288 
289 //
290 // Default traits for the pixel types deriving from std::complex<>
291 //
292 
293 template<typename TComponent >
294 class DefaultConvertPixelTraits< ::std::complex< TComponent > >
295 {
296 public:
297  typedef::std::complex< TComponent > TargetType;
298  typedef TComponent ComponentType;
299  static unsigned int GetNumberOfComponents()
300  {
301  return 2;
302  }
303  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
304  {
305  if ( i == 0 )
306  {
307  pixel = TargetType( v, pixel.imag() );
308  }
309  else
310  {
311  pixel = TargetType(pixel.real(), v);
312  }
313  }
314  static ComponentType GetScalarValue(const TargetType &pixel)
315  {
316  return std::norm(pixel);
317  }
318 };
319 
320 
321 //
322 // End of Traits for the classes deriving from std::complex.
323 //
324 //
325 } // end namespace itk
326 #endif
327