ITK  6.0.0
Insight Toolkit
itkDefaultConvertPixelTraits.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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>
41 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits
42 {
43 public:
45  using ComponentType = typename PixelType::ComponentType;
46 
48  static unsigned int
50  {
51  return PixelType::GetNumberOfComponents();
52  }
53 
54  static unsigned int
55  GetNumberOfComponents(const PixelType itkNotUsed(pixel))
56  {
57  return PixelType::GetNumberOfComponents();
58  }
59 
61  static ComponentType
62  GetNthComponent(int c, const PixelType & pixel)
63  {
64  return pixel.GetNthComponent(c);
65  }
66 
68  static void
69  SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
70  {
71  pixel.SetNthComponent(c, v);
72  }
73 
75  static ComponentType
76  GetScalarValue(const PixelType & pixel)
77  {
78  return pixel.GetScalarValue();
79  }
80 };
83 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
84  template <> \
85  class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<type> \
86  { \
87  public: \
88  using ComponentType = type; \
89  static unsigned int \
90  GetNumberOfComponents() \
91  { \
92  return 1; \
93  } \
94  static unsigned int \
95  GetNumberOfComponents(const type) \
96  { \
97  return 1; \
98  } \
99  static void \
100  SetNthComponent(int, type & pixel, const ComponentType & v) \
101  { \
102  pixel = v; \
103  } \
104  static type \
105  GetNthComponent(int, const type pixel) \
106  { \
107  return pixel; \
108  } \
109  static type \
110  GetScalarValue(const type & pixel) \
111  { \
112  return pixel; \
113  } \
114  };
115 
129 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long long)
131 
132 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
133 
134 //
135 // Default traits for the Offset<> pixel type
136 //
137 
138 template <unsigned int VDimension>
139 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Offset<VDimension>>
140 {
141 public:
144  static unsigned int
146  {
147  return VDimension;
148  }
149  static void
150  SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
151  {
152  pixel[i] = v;
153  }
154  static ComponentType
155  GetScalarValue(const TargetType & pixel)
156  {
157  return pixel[0];
158  }
159 };
160 
161 //
162 // Default traits for the pixel types deriving from FixedArray<>
163 //
164 
165 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
166  template <typename TComponentType, unsigned int VDimension> \
167  class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<type<TComponentType, VDimension>> \
168  { \
169  public: \
170  using TargetType = type<TComponentType, VDimension>; \
171  using ComponentType = TComponentType; \
172  static unsigned int \
173  GetNumberOfComponents() \
174  { \
175  return VDimension; \
176  } \
177  static unsigned int \
178  GetNumberOfComponents(const TargetType) \
179  { \
180  return VDimension; \
181  } \
182  static void \
183  SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
184  { \
185  pixel[i] = v; \
186  } \
187  static ComponentType \
188  GetNthComponent(int i, const TargetType pixel) \
189  { \
190  return pixel[i]; \
191  } \
192  static ComponentType \
193  GetScalarValue(const TargetType & pixel) \
194  { \
195  return pixel[0]; \
196  } \
197  }
198 
203 
204 //
205 // End of Traits for the classes deriving from FixedArray.
206 //
207 //
208 
209 //
210 // Default traits for pixel types deriving from VariableLengthVector<>
211 //
212 template <typename VComponent>
213 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableLengthVector<VComponent>>
214 {
215 public:
217  using ComponentType = VComponent;
218  static unsigned int
220  {
221  return 0;
222  }
223  static unsigned int
225  {
226  return pixel.Size();
227  }
228  static void
229  SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
230  {
231  pixel[i] = v;
232  }
233  static ComponentType
234  GetNthComponent(int i, const TargetType & pixel)
235  {
236  return pixel[i];
237  }
238  static ComponentType
239  GetScalarValue(const TargetType & pixel)
240  {
241  return pixel.GetNorm();
242  }
243 };
244 
245 
246 //
247 // Default traits for pixel types deriving from VariableSizeMatrix<>
248 //
249 template <typename VComponent>
250 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableSizeMatrix<VComponent>>
251 {
252 public:
254  using ComponentType = VComponent;
255  static unsigned int
257  {
258  return 0;
259  }
260  static unsigned int
262  {
263  return pixel.Cols() * pixel.Rows();
264  }
265  static void
266  SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
267  {
268  const unsigned int row = i / pixel.Cols();
269  const unsigned int col = i % pixel.Cols();
270  pixel(row, col) = v;
271  }
272  static ComponentType
273  GetNthComponent(int i, const TargetType & pixel)
274  {
275  const unsigned int row = i / pixel.Cols();
276  const unsigned int col = i % pixel.Cols();
277  return pixel(row, col);
278  }
279  static ComponentType
281  {
282  return 0.0;
283  }
284 };
285 
286 
287 //
288 // End of Traits for the classes deriving from FixedArray.
289 //
290 //
291 
292 //
293 // Default traits for the pixel types deriving from Matrix<>
294 //
295 
296 template <typename VComponent, unsigned int VRows, unsigned int VCols>
297 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Matrix<VComponent, VRows, VCols>>
298 {
299 public:
301  using ComponentType = VComponent;
302  static unsigned int
304  {
305  return VRows * VCols;
306  }
307  static void
308  SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
309  {
310  const unsigned int row = i / VCols;
311  const unsigned int col = i % VCols;
312  pixel[row][col] = v;
313  }
314  static ComponentType
315  GetNthComponent(int i, const TargetType & pixel)
316  {
317  const unsigned int row = i / VCols;
318  const unsigned int col = i % VCols;
319  return pixel[row][col];
320  }
321  static ComponentType
322  GetScalarValue(const TargetType & pixel)
323  {
324  return pixel[0][0];
325  }
326 };
327 
328 //
329 // Default traits for the pixel types deriving from std::complex<>
330 //
331 
332 template <typename TComponent>
333 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<std::complex<TComponent>>
334 {
335 public:
336  using TargetType = std::complex<TComponent>;
337  using ComponentType = TComponent;
338  static unsigned int
340  {
341  return 2;
342  }
343  static void
344  SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
345  {
346  if (i == 0)
347  {
348  pixel = TargetType(v, pixel.imag());
349  }
350  else
351  {
352  pixel = TargetType(pixel.real(), v);
353  }
354  }
355  static ComponentType
356  GetScalarValue(const TargetType & pixel)
357  {
358  return std::norm(pixel);
359  }
360 };
361 
362 
363 //
364 // End of Traits for the classes deriving from std::complex.
365 //
366 //
367 } // end namespace itk
368 #endif
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::SetNthComponent
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:229
itk::DefaultConvertPixelTraits< std::complex< TComponent > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:339
itk::DefaultConvertPixelTraits< Offset< VDimension > >::ComponentType
typename TargetType::OffsetValueType ComponentType
Definition: itkDefaultConvertPixelTraits.h:143
itk::DefaultConvertPixelTraits< std::complex< TComponent > >::SetNthComponent
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:344
itk::DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >::GetScalarValue
static ComponentType GetScalarValue(const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:322
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::SetNthComponent
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:266
itkVariableSizeMatrix.h
itkMatrix.h
itkOffset.h
itk::DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >::SetNthComponent
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:308
itkVariableLengthVector.h
itk::DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:303
itk::DefaultConvertPixelTraits::GetNumberOfComponents
static unsigned int GetNumberOfComponents(const PixelType)
Definition: itkDefaultConvertPixelTraits.h:55
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::GetNthComponent
static ComponentType GetNthComponent(int i, const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:273
itk::DefaultConvertPixelTraits< Offset< VDimension > >::GetScalarValue
static ComponentType GetScalarValue(const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:155
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:219
itk::VariableLengthVector::GetNorm
RealValueType GetNorm() const
itk::DefaultConvertPixelTraits< Offset< VDimension > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:145
itk::DefaultConvertPixelTraits< std::complex< TComponent > >::ComponentType
TComponent ComponentType
Definition: itkDefaultConvertPixelTraits.h:337
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::GetScalarValue
static ComponentType GetScalarValue(const TargetType &)
Definition: itkDefaultConvertPixelTraits.h:280
itk::DefaultConvertPixelTraits< std::complex< TComponent > >::TargetType
std::complex< TComponent > TargetType
Definition: itkDefaultConvertPixelTraits.h:336
itk::VariableSizeMatrix
A templated class holding a M x N size Matrix.
Definition: itkVariableSizeMatrix.h:45
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::GetScalarValue
static ComponentType GetScalarValue(const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:239
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents(const TargetType pixel)
Definition: itkDefaultConvertPixelTraits.h:261
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:256
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::GetNumberOfComponents
static unsigned int GetNumberOfComponents(const TargetType pixel)
Definition: itkDefaultConvertPixelTraits.h:224
itk::DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >::ComponentType
VComponent ComponentType
Definition: itkDefaultConvertPixelTraits.h:254
itk::DefaultConvertPixelTraits::GetScalarValue
static ComponentType GetScalarValue(const PixelType &pixel)
Definition: itkDefaultConvertPixelTraits.h:76
itk::DefaultConvertPixelTraits
Traits class used to by ConvertPixels to convert blocks of pixels.
Definition: itkDefaultConvertPixelTraits.h:41
itk::DefaultConvertPixelTraits::SetNthComponent
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:69
itk::VariableSizeMatrix::Cols
unsigned int Cols() const
Definition: itkVariableSizeMatrix.h:247
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::GetNthComponent
static ComponentType GetNthComponent(int i, const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:234
itk::VariableLengthVector::Size
unsigned int Size() const
Definition: itkVariableLengthVector.h:587
itk::ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE
ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Vector)
itk::OffsetValueType
long OffsetValueType
Definition: itkIntTypes.h:97
itk::VariableLengthVector
Represents an array whose length can be defined at run-time.
Definition: itkConstantBoundaryCondition.h:28
itk::DefaultConvertPixelTraits< VariableLengthVector< VComponent > >::ComponentType
VComponent ComponentType
Definition: itkDefaultConvertPixelTraits.h:217
itk::Matrix
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:52
itk::Offset
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:66
itk::DefaultConvertPixelTraits< Offset< VDimension > >::SetNthComponent
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Definition: itkDefaultConvertPixelTraits.h:150
itk::ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
itk::DefaultConvertPixelTraits::GetNumberOfComponents
static unsigned int GetNumberOfComponents()
Definition: itkDefaultConvertPixelTraits.h:49
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkVector.h
itk::DefaultConvertPixelTraits::GetNthComponent
static ComponentType GetNthComponent(int c, const PixelType &pixel)
Definition: itkDefaultConvertPixelTraits.h:62
itk::VariableSizeMatrix::Rows
unsigned int Rows() const
Definition: itkVariableSizeMatrix.h:240
itk::DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >::GetNthComponent
static ComponentType GetNthComponent(int i, const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:315
itk::DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >::ComponentType
VComponent ComponentType
Definition: itkDefaultConvertPixelTraits.h:301
itk::DefaultConvertPixelTraits< std::complex< TComponent > >::GetScalarValue
static ComponentType GetScalarValue(const TargetType &pixel)
Definition: itkDefaultConvertPixelTraits.h:356
itk::DefaultConvertPixelTraits::ComponentType
typename PixelType::ComponentType ComponentType
Definition: itkDefaultConvertPixelTraits.h:45