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"
24 #include "itkVariableLengthVector.h"
25 #include "itkVariableSizeMatrix.h"
26 
27 namespace itk
28 {
29 /** \class DefaultConvertPixelTraits
30  *  \brief Traits class used to by ConvertPixels to convert blocks of pixels.
31  *
32  *  TOutputPixelType is the destination type. The input type is inferred
33  *  by the templated static function Convert.
34  *
35  *  This implementaion, does a simple assignment operator, so if you are
36  *  going from from a higher bit representation to a lower bit one (int to
37  *  char), you may want to specialize and add some sort of transfer function.
38  * \ingroup ITKCommon
39  */
40 template< typename PixelType >
41 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits
42 {
43 public:
44   /** Determine the pixel data type. */
45   using ComponentType = typename PixelType::ComponentType;
46 
47   /** Return the number of components per pixel. */
GetNumberOfComponents()48   static unsigned int GetNumberOfComponents()
49   { return PixelType::GetNumberOfComponents(); }
50 
GetNumberOfComponents(const PixelType itkNotUsed (pixel))51   static unsigned int GetNumberOfComponents( const PixelType itkNotUsed(pixel) )
52   { return PixelType::GetNumberOfComponents( ); }
53 
54   /** Return the nth component of the pixel. */
GetNthComponent(int c,const PixelType & pixel)55   static ComponentType GetNthComponent(int c, const PixelType & pixel)
56   { return pixel.GetNthComponent(c); }
57 
58   /** Set the nth component of the pixel. */
SetNthComponent(int c,PixelType & pixel,const ComponentType & v)59   static void SetNthComponent(int c, PixelType & pixel, const ComponentType & v)
60   { pixel.SetNthComponent(c, v); }
61 
62   /** Return a single scalar value from this pixel. */
GetScalarValue(const PixelType & pixel)63   static ComponentType GetScalarValue(const PixelType & pixel)
64   { return pixel.GetScalarValue(); }
65 };
66 
67 #define ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(type)                      \
68   template< >                                                              \
69   class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< type >              \
70   {                                                                        \
71 public:                                                                    \
72     using ComponentType = type;                                            \
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 
95 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(float)
ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(double)96 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(double)
97 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(long double)
98 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(int)
99 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(char)
100 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(short)
101 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned int)
102 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(signed char)
103 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned char)
104 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned short)
105 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(long)
106 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long)
107 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(long long)
108 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(unsigned long long)
109 ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL(bool)
110 
111 #undef ITK_DEFAULTCONVERTTRAITS_NATIVE_SPECIAL
112 
113 //
114 //  Default traits for the Offset<> pixel type
115 //
116 
117 template<unsigned int VDimension>
118 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< Offset< VDimension > >
119 {
120 public:
121   using TargetType = Offset< VDimension >;
122   using ComponentType = typename TargetType::OffsetValueType;
123   static unsigned int GetNumberOfComponents()
124     {
125       return VDimension;
126     }
127   static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
128     {
129       pixel[i] = v;
130     }
131   static ComponentType GetScalarValue(const TargetType &pixel)
132     {
133       return pixel[0];
134     }
135 };
136 
137 //
138 //  Default traits for the pixel types deriving from FixedArray<>
139 //
140 
141 #define ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(type)                  \
142   template<typename TComponentType, unsigned VDimension >               \
143   class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< type< TComponentType, VDimension > > \
144   {                                                                     \
145   public:                                                               \
146     using TargetType = type< TComponentType, VDimension >;              \
147     using ComponentType = TComponentType;           \
148     static unsigned int GetNumberOfComponents()                         \
149     {                                                                   \
150       return VDimension;                                                \
151     }                                                                   \
152     static unsigned int GetNumberOfComponents( const TargetType )       \
153     {                                                                   \
154       return VDimension;                                                \
155     }                                                                   \
156     static void SetNthComponent(int i, TargetType & pixel,              \
157                                 const ComponentType &v)                 \
158     {                                                                   \
159       pixel[i] = v;                                                     \
160     }                                                                   \
161     static ComponentType GetNthComponent(int i, const TargetType pixel) \
162     {                                                                   \
163       return pixel[i];                                                  \
164     }                                                                   \
165     static ComponentType GetScalarValue(const TargetType &pixel)        \
166     {                                                                   \
167       return pixel[0];                                                  \
168     }                                                                   \
169   }                                                                     \
170 
171 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Vector);
172 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(CovariantVector);
173 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(Point);
174 ITK_DEFAULTCONVERTTRAITS_FIXEDARRAY_TYPE(FixedArray);
175 
176 //
177 //  End of Traits for the classes deriving from FixedArray.
178 //
179 //
180 
181 //
182 //  Default traits for pixel types deriving from VariableLengthVector<>
183 //
184 template<typename VComponent>
185 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< VariableLengthVector< VComponent > >
186 {
187 public:
188   using TargetType = VariableLengthVector< VComponent >;
189   using ComponentType = VComponent;
GetNumberOfComponents()190   static unsigned int GetNumberOfComponents()
191   {
192     return 0;
193   }
GetNumberOfComponents(const TargetType pixel)194   static unsigned int GetNumberOfComponents( const TargetType pixel )
195   {
196     return pixel.Size();
197   }
SetNthComponent(int i,TargetType & pixel,const ComponentType & v)198   static void SetNthComponent(int i, TargetType & pixel,
199                               const ComponentType &v)
200   {
201     pixel[i] = v;
202   }
GetNthComponent(int i,const TargetType & pixel)203   static ComponentType GetNthComponent(int i, const TargetType & pixel)
204   {
205     return pixel[i];
206   }
GetScalarValue(const TargetType & pixel)207   static ComponentType GetScalarValue(const TargetType &pixel)
208   {
209     return pixel.GetNorm();
210   }
211 };
212 
213 
214 //
215 //  Default traits for pixel types deriving from VariableSizeMatrix<>
216 //
217 template<typename VComponent>
218 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< VariableSizeMatrix< VComponent > >
219 {
220 public:
221   using TargetType = VariableSizeMatrix< VComponent >;
222   using ComponentType = VComponent;
GetNumberOfComponents()223   static unsigned int GetNumberOfComponents()
224   {
225     return 0;
226   }
GetNumberOfComponents(const TargetType pixel)227   static unsigned int GetNumberOfComponents( const TargetType pixel )
228   {
229     return pixel.Cols() * pixel.Rows();
230   }
SetNthComponent(int i,TargetType & pixel,const ComponentType & v)231   static void SetNthComponent(int i, TargetType & pixel,
232                               const ComponentType &v)
233   {
234     const unsigned int row = i / pixel.Cols();
235     const unsigned int col = i % pixel.Cols();
236     pixel(row,col) = v;
237   }
GetNthComponent(int i,const TargetType & pixel)238   static ComponentType GetNthComponent(int i, const TargetType & pixel)
239   {
240     const unsigned int row = i / pixel.Cols();
241     const unsigned int col = i % pixel.Cols();
242     return pixel(row,col);
243   }
GetScalarValue(const TargetType &)244   static ComponentType GetScalarValue(const TargetType &)
245   {
246     return 0.0;
247   }
248 };
249 
250 
251 //
252 //  End of Traits for the classes deriving from FixedArray.
253 //
254 //
255 
256 //
257 //  Default traits for the pixel types deriving from Matrix<>
258 //
259 
260 template<typename VComponent, unsigned VRows, unsigned VCols >
261 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< Matrix< VComponent, VRows, VCols > >
262 {
263 public:
264   using TargetType = Matrix< VComponent, VRows, VCols >;
265   using ComponentType = VComponent;
GetNumberOfComponents()266   static unsigned int GetNumberOfComponents()
267     {
268       return VRows * VCols;
269     }
SetNthComponent(int i,TargetType & pixel,const ComponentType & v)270   static void SetNthComponent(int i, TargetType & pixel,
271                               const ComponentType &v)
272     {
273       const unsigned int row = i / VCols;
274       const unsigned int col = i % VCols;
275       pixel[row][col] = v;
276     }
GetNthComponent(int i,const TargetType & pixel)277   static ComponentType GetNthComponent(int i, const TargetType & pixel)
278   {
279     const unsigned int row = i / VCols;
280     const unsigned int col = i % VCols;
281     return pixel[row][col];
282   }
GetScalarValue(const TargetType & pixel)283   static ComponentType GetScalarValue(const TargetType &pixel)
284     {
285       return pixel[0][0];
286     }
287 };
288 
289 //
290 //  Default traits for the pixel types deriving from std::complex<>
291 //
292 
293 template<typename TComponent >
294 class ITK_TEMPLATE_EXPORT DefaultConvertPixelTraits< ::std::complex< TComponent > >
295 {
296 public:
297   using TargetType = ::std::complex<TComponent>;
298   using ComponentType = TComponent;
GetNumberOfComponents()299   static unsigned int GetNumberOfComponents()
300     {
301       return 2;
302     }
SetNthComponent(int i,TargetType & pixel,const ComponentType & v)303   static void SetNthComponent(int i, TargetType & pixel, const ComponentType &v)
304     {
305       if ( i == 0 )
306         {
307         pixel = TargetType( v, pixel.imag() );
308         }
309       else
310         {
311         pixel = TargetType(pixel.real(), v);
312         }
313     }
GetScalarValue(const TargetType & pixel)314   static ComponentType GetScalarValue(const TargetType &pixel)
315     {
316       return std::norm(pixel);
317     }
318 };
319 
320 
321 //
322 //  End of Traits for the classes deriving from std::complex.
323 //
324 //
325 } // end namespace itk
326 #endif
327