ITK  4.13.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 
109 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long long)
111 
112 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
113 
114 //
115 // Default traits for the Offset<> pixel type
116 //
117 
118 template<unsigned int VDimension>
119 class DefaultConvertPixelTraits< Offset< VDimension > >
120 {
121 public:
124  static unsigned int GetNumberOfComponents()
125  {
126  return VDimension;
127  }
128  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
129  {
130  pixel[i] = v;
131  }
133  {
134  return pixel[0];
135  }
136 };
137 
138 //
139 // Default traits for the pixel types deriving from FixedArray<>
140 //
141 
142 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
143  template<typename TComponentType, unsigned VDimension > \
144  class DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
145  { \
146  public: \
147  typedef type< TComponentType, VDimension > TargetType; \
148  typedef TComponentType ComponentType; \
149  static unsigned int GetNumberOfComponents() \
150  { \
151  return VDimension; \
152  } \
153  static unsigned int GetNumberOfComponents( const TargetType ) \
154  { \
155  return VDimension; \
156  } \
157  static void SetNthComponent(int i, TargetType & pixel, \
158  const ComponentType &v) \
159  { \
160  pixel[i] = v; \
161  } \
162  static ComponentType GetNthComponent(int i, const TargetType pixel) \
163  { \
164  return pixel[i]; \
165  } \
166  static ComponentType GetScalarValue(const TargetType &pixel) \
167  { \
168  return pixel[0]; \
169  } \
170  } \
171 
176 
177 //
178 // End of Traits for the classes deriving from FixedArray.
179 //
180 //
181 
182 //
183 // Default traits for pixel types deriving from VariableLengthVector<>
184 //
185 template<typename VComponent>
187 {
188 public:
190  typedef VComponent ComponentType;
191  static unsigned int GetNumberOfComponents()
192  {
193  return 0;
194  }
195  static unsigned int GetNumberOfComponents( const TargetType pixel )
196  {
197  return pixel.Size();
198  }
199  static void SetNthComponent(int i, TargetType & pixel,
200  const ComponentType &v)
201  {
202  pixel[i] = v;
203  }
204  static ComponentType GetNthComponent(int i, const TargetType & pixel)
205  {
206  return pixel[i];
207  }
209  {
210  return pixel.GetNorm();
211  }
212 };
213 
214 
215 //
216 // Default traits for pixel types deriving from VariableSizeMatrix<>
217 //
218 template<typename VComponent>
220 {
221 public:
223  typedef VComponent ComponentType;
224  static unsigned int GetNumberOfComponents()
225  {
226  return 0;
227  }
228  static unsigned int GetNumberOfComponents( const TargetType pixel )
229  {
230  return pixel.Cols() * pixel.Rows();
231  }
232  static void SetNthComponent(int i, TargetType & pixel,
233  const ComponentType &v)
234  {
235  const unsigned int row = i / pixel.Cols();
236  const unsigned int col = i % pixel.Cols();
237  pixel(row,col) = v;
238  }
239  static ComponentType GetNthComponent(int i, const TargetType & pixel)
240  {
241  const unsigned int row = i / pixel.Cols();
242  const unsigned int col = i % pixel.Cols();
243  return pixel(row,col);
244  }
246  {
247  return 0.0;
248  }
249 };
250 
251 
252 //
253 // End of Traits for the classes deriving from FixedArray.
254 //
255 //
256 
257 //
258 // Default traits for the pixel types deriving from Matrix<>
259 //
260 
261 template<typename VComponent, unsigned VRows, unsigned VCols >
262 class DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
263 {
264 public:
266  typedef VComponent ComponentType;
267  static unsigned int GetNumberOfComponents()
268  {
269  return VRows * VCols;
270  }
271  static void SetNthComponent(int i, TargetType & pixel,
272  const ComponentType &v)
273  {
274  const unsigned int row = i / VCols;
275  const unsigned int col = i % VCols;
276  pixel[row][col] = v;
277  }
278  static ComponentType GetNthComponent(int i, const TargetType & pixel)
279  {
280  const unsigned int row = i / VCols;
281  const unsigned int col = i % VCols;
282  return pixel[row][col];
283  }
285  {
286  return pixel[0][0];
287  }
288 };
289 
290 //
291 // Default traits for the pixel types deriving from std::complex<>
292 //
293 
294 template<typename TComponent >
295 class DefaultConvertPixelTraits< ::std::complex< TComponent > >
296 {
297 public:
298  typedef::std::complex< TComponent > TargetType;
299  typedef TComponent ComponentType;
300  static unsigned int GetNumberOfComponents()
301  {
302  return 2;
303  }
304  static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
305  {
306  if ( i == 0 )
307  {
308  pixel = TargetType( v, pixel.imag() );
309  }
310  else
311  {
312  pixel = TargetType(pixel.real(), v);
313  }
314  }
315  static ComponentType GetScalarValue(const TargetType &pixel)
316  {
317  return std::norm(pixel);
318  }
319 };
320 
321 
322 //
323 // End of Traits for the classes deriving from std::complex.
324 //
325 //
326 } // end namespace itk
327 #endif
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
unsigned int Rows() const
Represent the offset between two n-dimensional indexes in a n-dimensional image.
Definition: itkOffset.h:56
static ComponentType GetScalarValue(const PixelType &pixel)
static unsigned int GetNumberOfComponents(const PixelType)
unsigned int Size(void) const
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
A templated class holding a M x N size Matrix.
Traits class used to by ConvertPixels to convert blocks of pixels.
static ComponentType GetNthComponent(int i, const TargetType &pixel)
RealValueType GetNorm() const
unsigned int Cols() const
static ComponentType GetNthComponent(int c, const PixelType &pixel)
static ComponentType GetScalarValue(const TargetType &pixel)
Represents an array whose length can be defined at run-time.
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Vector)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
static ComponentType GetNthComponent(int i, const TargetType &pixel)
#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)
static ComponentType GetNthComponent(int i, const TargetType &pixel)
itk::OffsetValueType OffsetValueType
Definition: itkOffset.h:70
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)