ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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};
81
82#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type) \
83 template <> \
84 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<type> \
85 { \
86 public: \
87 using ComponentType = type; \
88 static unsigned int \
89 GetNumberOfComponents() \
90 { \
91 return 1; \
92 } \
93 static unsigned int \
94 GetNumberOfComponents(const type) \
95 { \
96 return 1; \
97 } \
98 static void \
99 SetNthComponent(int, type & pixel, const ComponentType & v) \
100 { \
101 pixel = v; \
102 } \
103 static type \
104 GetNthComponent(int, const type pixel) \
105 { \
106 return pixel; \
107 } \
108 static type \
109 GetScalarValue(const type & pixel) \
110 { \
111 return pixel; \
112 } \
113 };
114
130
131#undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
132
133//
134// Default traits for the Offset<> pixel type
135//
136
137template <unsigned int VDimension>
138class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Offset<VDimension>>
139{
140public:
143 static unsigned int
145 {
146 return VDimension;
147 }
148 static void
149 SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
150 {
151 pixel[i] = v;
152 }
153 static ComponentType
155 {
156 return pixel[0];
157 }
158};
159
160//
161// Default traits for the pixel types deriving from FixedArray<>
162//
163
164#define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type) \
165 template <typename TComponentType, unsigned int VDimension> \
166 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<type<TComponentType, VDimension>> \
167 { \
168 public: \
169 using TargetType = type<TComponentType, VDimension>; \
170 using ComponentType = TComponentType; \
171 static unsigned int \
172 GetNumberOfComponents() \
173 { \
174 return VDimension; \
175 } \
176 static unsigned int \
177 GetNumberOfComponents(const TargetType) \
178 { \
179 return VDimension; \
180 } \
181 static void \
182 SetNthComponent(int i, TargetType & pixel, const ComponentType & v) \
183 { \
184 pixel[i] = v; \
185 } \
186 static ComponentType \
187 GetNthComponent(int i, const TargetType pixel) \
188 { \
189 return pixel[i]; \
190 } \
191 static ComponentType \
192 GetScalarValue(const TargetType & pixel) \
193 { \
194 return pixel[0]; \
195 } \
196 }
197
202
203//
204// End of Traits for the classes deriving from FixedArray.
205//
206//
207
208//
209// Default traits for pixel types deriving from VariableLengthVector<>
210//
211template <typename VComponent>
212class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableLengthVector<VComponent>>
213{
214public:
216 using ComponentType = VComponent;
217 static unsigned int
219 {
220 return 0;
221 }
222 static unsigned int
224 {
225 return pixel.Size();
226 }
227 static void
228 SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
229 {
230 pixel[i] = v;
231 }
232 static ComponentType
233 GetNthComponent(int i, const TargetType & pixel)
234 {
235 return pixel[i];
236 }
237 static ComponentType
239 {
240 return pixel.GetNorm();
241 }
242};
243
244
245//
246// Default traits for pixel types deriving from VariableSizeMatrix<>
247//
248template <typename VComponent>
249class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<VariableSizeMatrix<VComponent>>
250{
251public:
253 using ComponentType = VComponent;
254 static unsigned int
256 {
257 return 0;
258 }
259 static unsigned int
261 {
262 return pixel.Cols() * pixel.Rows();
263 }
264 static void
265 SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
266 {
267 const unsigned int row = i / pixel.Cols();
268 const unsigned int col = i % pixel.Cols();
269 pixel(row, col) = v;
270 }
271 static ComponentType
272 GetNthComponent(int i, const TargetType & pixel)
273 {
274 const unsigned int row = i / pixel.Cols();
275 const unsigned int col = i % pixel.Cols();
276 return pixel(row, col);
277 }
278 static ComponentType
280 {
281 return 0.0;
282 }
283};
284
285
286//
287// End of Traits for the classes deriving from FixedArray.
288//
289//
290
291//
292// Default traits for the pixel types deriving from Matrix<>
293//
294
295template <typename VComponent, unsigned int VRows, unsigned int VCols>
296class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<Matrix<VComponent, VRows, VCols>>
297{
298public:
300 using ComponentType = VComponent;
301 static unsigned int
303 {
304 return VRows * VCols;
305 }
306 static void
307 SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
308 {
309 const unsigned int row = i / VCols;
310 const unsigned int col = i % VCols;
311 pixel[row][col] = v;
312 }
313 static ComponentType
314 GetNthComponent(int i, const TargetType & pixel)
315 {
316 const unsigned int row = i / VCols;
317 const unsigned int col = i % VCols;
318 return pixel[row][col];
319 }
320 static ComponentType
322 {
323 return pixel[0][0];
324 }
325};
326
327//
328// Default traits for the pixel types deriving from std::complex<>
329//
330
331template <typename TComponent>
332class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits<std::complex<TComponent>>
333{
334public:
335 using TargetType = std::complex<TComponent>;
336 using ComponentType = TComponent;
337 static unsigned int
339 {
340 return 2;
341 }
342 static void
343 SetNthComponent(int i, TargetType & pixel, const ComponentType & v)
344 {
345 if (i == 0)
346 {
347 pixel = TargetType(v, pixel.imag());
348 }
349 else
350 {
351 pixel = TargetType(pixel.real(), v);
352 }
353 }
354 static ComponentType
356 {
357 return std::norm(pixel);
358 }
359};
360
361
362//
363// End of Traits for the classes deriving from std::complex.
364//
365//
366} // end namespace itk
367#endif
A templated class holding a n-Dimensional covariant vector.
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
static ComponentType GetScalarValue(const TargetType &pixel)
static void SetNthComponent(int i, TargetType &pixel, const ComponentType &v)
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 unsigned int GetNumberOfComponents(const PixelType pixel)
static void SetNthComponent(int c, PixelType &pixel, const ComponentType &v)
typename PixelType::ComponentType ComponentType
Simulate a standard C array with copy semantics.
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
#define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type)
#define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
STL namespace.
Represent a n-dimensional offset between two n-dimensional indexes of n-dimensional image.
Definition itkOffset.h:67
itk::OffsetValueType OffsetValueType
Definition itkOffset.h:77