ITK  4.2.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 
26 namespace itk
27 {
39 template< typename PixelType >
41 {
42 public:
44  typedef typename PixelType::ComponentType ComponentType;
45 
47  static unsigned int GetNumberOfComponents()
49 
50  static unsigned int GetNumberOfComponents( const PixelType itkNotUsed(pixel) )
52 
54  static ComponentType GetNthComponent(int c, const PixelType & pixel)
55  { return pixel.GetNthComponent(c); }
56 
58  static void SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
59  { pixel.SetNthComponent(c, v); }
60 
62  static ComponentType GetScalarValue(const PixelType & pixel)
63  { return pixel.GetScalarValue(); }
64 };
66 
67 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
68  template< > \
69  class DefaultConvertPixelTraits< type > \
70  { \
71 public: \
72  typedef type ComponentType; \
73  static unsigned int GetNumberOfComponents() \
74  { \
75  return 1; \
76  } \
77  static unsigned int GetNumberOfComponents(const type) \
78  { \
79  return 1; \
80  } \
81  static void SetNthComponent(int, type & pixel, const ComponentType &v) \
82  { \
83  pixel = v; \
84  } \
85  static type GetNthComponent(int, const type pixel) \
86  { \
87  return pixel; \
88  } \
89  static type GetScalarValue(const type &pixel) \
90  { \
91  return pixel; \
92  } \
93  };
94 
107 
108 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
109 
110 //
111 // Default traits for the Offset<> pixel type
112 //
113 
114 template<unsigned int VDimension>
115 class DefaultConvertPixelTraits< Offset< VDimension > >
116 {
117 public:
120  static unsigned int GetNumberOfComponents()
121  {
122  return VDimension;
123  }
124  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
125  {
126  pixel[i] = v;
127  }
129  {
130  return pixel[0];
131  }
132 };
133 
134 //
135 // Default traits for the pixel types deriving from FixedArray<>
136 //
137 
138 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
139  template<typename TComponentType, unsigned VDimension > \
140  class DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
141  { \
142  public: \
143  typedef type< TComponentType, VDimension > TargetType; \
144  typedef TComponentType ComponentType; \
145  static unsigned int GetNumberOfComponents() \
146  { \
147  return VDimension; \
148  } \
149  static unsigned int GetNumberOfComponents( const TargetType ) \
150  { \
151  return VDimension; \
152  } \
153  static void SetNthComponent(int i, TargetType & pixel, \
154  const ComponentType &v) \
155  { \
156  pixel[i] = v; \
157  } \
158  static ComponentType GetNthComponent(int i, const TargetType pixel) \
159  { \
160  return pixel[i]; \
161  } \
162  static ComponentType GetScalarValue(const TargetType &pixel) \
163  { \
164  return pixel[0]; \
165  } \
166  } \
167 
172 
173 //
174 // End of Traits for the classes deriving from FixedArray.
175 //
176 //
177 
178 //
179 // Default traits for pixel types deriving from VariableLengthVector<>
180 //
181 template<typename VComponent>
183 {
184 public:
186  typedef VComponent ComponentType;
187  static unsigned int GetNumberOfComponents()
188  {
189  return 0;
190  }
191  static unsigned int GetNumberOfComponents( const TargetType pixel )
192  {
193  return pixel.Size();
194  }
195  static void SetNthComponent(int i, TargetType & pixel,
196  const ComponentType &v)
197  {
198  pixel[i] = v;
199  }
200  static ComponentType GetNthComponent(int i, const TargetType & pixel)
201  {
202  return pixel[i];
203  }
205  {
206  return pixel.GetNorm();
207  }
208 };
209 
210 
211 //
212 // End of Traits for the classes deriving from FixedArray.
213 //
214 //
215 
216 //
217 // Default traits for the pixel types deriving from Matrix<>
218 //
219 
220 template<typename VComponent, unsigned VRows, unsigned VCols >
221 class DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
222 {
223 public:
225  typedef VComponent ComponentType;
226  static unsigned int GetNumberOfComponents()
227  {
228  return VRows * VCols;
229  }
230  static void SetNthComponent(int i, TargetType & pixel,
231  const ComponentType &v)
232  {
233  const unsigned int row = i / VCols;
234  const unsigned int col = i % VCols;
235  pixel[row][col] = v;
236  }
238  {
239  return pixel[0][0];
240  }
241 };
242 
243 //
244 // Default traits for the pixel types deriving from std::complex<>
245 //
246 
247 template<typename TComponent >
248 class DefaultConvertPixelTraits< ::std::complex< TComponent > >
249 {
250 public:
251  typedef::std::complex< TComponent > TargetType;
252  typedef TComponent ComponentType;
253  static unsigned int GetNumberOfComponents()
254  {
255  return 2;
256  }
257  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
258  {
259  if ( i == 0 )
260  {
261  pixel = TargetType( v, pixel.imag() );
262  }
263  else
264  {
265  pixel = TargetType(pixel.real(), v);
266  }
267  }
268  static ComponentType GetScalarValue(const TargetType &pixel)
269  {
270  return std::norm(pixel);
271  }
272 };
273 
274 
275 //
276 // End of Traits for the classes deriving from std::complex.
277 //
278 //
279 } // end namespace itk
280 #endif
281