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"
26
27namespace itk
28{
40template <typename PixelType>
41class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits
42{
43public:
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
131
132#undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
133
134//
135// Default traits for the Offset<> pixel type
136//
137
138template <unsigned int VDimension>
139class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Offset<VDimension>>
140{
141public:
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
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//
212template <typename VComponent>
213class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableLengthVector<VComponent>>
214{
215public:
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
240 {
241 return pixel.GetNorm();
242 }
243};
244
245
246//
247// Default traits for pixel types deriving from VariableSizeMatrix<>
248//
249template <typename VComponent>
250class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableSizeMatrix<VComponent>>
251{
252public:
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
296template <typename VComponent, unsigned int VRows, unsigned int VCols>
297class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Matrix<VComponent, VRows, VCols>>
298{
299public:
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
323 {
324 return pixel[0][0];
325 }
326};
327
328//
329// Default traits for the pixel types deriving from std::complex<>
330//
331
332template <typename TComponent>
333class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<std::complex<TComponent>>
334{
335public:
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
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
A templated class holding a n-Dimensional covariant vector.
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
static ComponentType GetNthComponent(int i, const TargetType &pixel)
static ComponentType GetScalarValue(const TargetType &pixel)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
static ComponentType GetNthComponent(int i, const TargetType &pixel)
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)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
Traits class used to by ConvertPixels to convert blocks of pixels.
static ComponentType GetNthComponent(int c, const PixelType &pixel)
static ComponentType GetScalarValue(const PixelType &pixel)
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
typename PixelType::ComponentType ComponentType
static unsigned int GetNumberOfComponents(const PixelType)
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
A templated class holding a M x N size Matrix.
Definition: itkMatrix.h:53
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:54
A templated class holding a M x N size Matrix.
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:63
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
long OffsetValueType
Definition: itkIntTypes.h:97
ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Vector)
STL namespace.
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition: itkOffset.h:67