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 itkNumericTraitsRGBPixel_h
19 #define itkNumericTraitsRGBPixel_h
20 
21 #include "itkNumericTraits.h"
22 #include "itkRGBPixel.h"
23 
24 namespace itk
25 {
26 /**
27  * \brief Define numeric traits for RGBPixel.
28  * \tparam T Component type of RGBPixel
29  *
30  * We provide here a generic implementation based on creating types of
31  * RGBPixel whose components are the types of the NumericTraits from
32  * the original RGBPixel components. This implementation require
33  * support for partial specializations, since it is based on the
34  * concept that:
35  *   NumericTraits<RGBPixel< T > >  is defined piecewise by
36  *   RGBPixel< NumericTraits< T > >
37  *
38  * \sa NumericTraits
39  * \ingroup DataRepresentation
40  * \ingroup ITKCommon
41  */
42 template< typename T >
43 class NumericTraits< RGBPixel< T > >
44 {
45 private:
46 
47   using ElementAbsType = typename NumericTraits< T >::AbsType;
48   using ElementAccumulateType = typename NumericTraits< T >::AccumulateType;
49   using ElementFloatType = typename NumericTraits< T >::FloatType;
50   using ElementPrintType = typename NumericTraits< T >::PrintType;
51   using ElementRealType = typename NumericTraits< T >::RealType;
52 
53 public:
54 
55   /** Return the type of the native component type. */
56   using ValueType = T;
57 
58   using Self = RGBPixel< T >;
59 
60   /** Unsigned component type */
61   using AbsType = RGBPixel< ElementAbsType >;
62 
63   /** Accumulation of addition and multiplication. */
64   using AccumulateType = RGBPixel< ElementAccumulateType >;
65 
66   /** Typedef for operations that use floating point instead of real precision
67     */
68   using FloatType = RGBPixel< ElementFloatType >;
69 
70   /** Return the type that can be printed. */
71   using PrintType = RGBPixel< ElementPrintType >;
72 
73   /** Type for real-valued scalar operations. */
74   using RealType = RGBPixel< ElementRealType >;
75 
76   /** Type for real-valued scalar operations. */
77   using ScalarRealType = ElementRealType;
78 
79   /** Measurement vector type */
80   using MeasurementVectorType = Self;
81 
82   /** Component wise defined element
83    *
84    * \note minimum value for floating pointer types is defined as
85    * minimum positive normalize value.
86    */
max(const Self &)87   static const Self max(const Self &)
88   {
89     return Self( NumericTraits< T >::max() );
90   }
91 
min(const Self &)92   static const Self min(const Self &)
93   {
94     return Self( NumericTraits< T >::min() );
95   }
96 
max()97   static const Self max()
98   {
99     return Self( NumericTraits< T >::max() );
100   }
101 
min()102   static const Self min()
103   {
104     return Self( NumericTraits< T >::min() );
105   }
106 
NonpositiveMin()107   static const Self NonpositiveMin()
108   {
109     return Self( NumericTraits< ValueType >::NonpositiveMin() );
110   }
111 
ZeroValue()112   static const Self ZeroValue()
113   {
114     return Self(NumericTraits< T >::ZeroValue());
115   }
116 
OneValue()117   static const Self OneValue()
118   {
119     return Self(NumericTraits< T >::OneValue());
120   }
121 
NonpositiveMin(const Self &)122   static const Self NonpositiveMin(const Self &)
123   {
124     return NonpositiveMin();
125   }
126 
ZeroValue(const Self &)127   static const Self ZeroValue(const Self &)
128   {
129     return ZeroValue();
130   }
131 
OneValue(const Self &)132   static const Self OneValue(const Self &)
133   {
134     return OneValue();
135   }
136 
137   static constexpr bool IsSigned = NumericTraits< ValueType >::IsSigned;
138   static constexpr bool IsInteger = NumericTraits< ValueType >::IsInteger;
139   static constexpr bool IsComplex = NumericTraits< ValueType >::IsComplex;
140 
141   /** RGB pixels must have 3 components, so the size cannot be
142    *  set to anything besides 3.  If called with size of 3, this
143    *  function will fill the pixel with zeros. */
SetLength(RGBPixel<T> & m,const unsigned int s)144   static void SetLength(RGBPixel< T > & m, const unsigned int s)
145   {
146     if ( s != 3 )
147       {
148       itkGenericExceptionMacro(<< "Cannot set the size of a RGBPixel to anything other "
149                                "than 3.");
150       }
151     m.Fill(NumericTraits< T >::ZeroValue());
152   }
153 
154   /** Return the dimensionality of the pixel. Always returns 3. */
GetLength(const RGBPixel<T> &)155   static unsigned int GetLength(const RGBPixel< T > &)
156   {
157     return 3;
158   }
159 
160   /** Return the dimensionality of the pixel. Always returns 3. */
GetLength()161   static unsigned int GetLength()
162   {
163     return 3;
164   }
165 
AssignToArray(const Self & v,MeasurementVectorType & mv)166   static void AssignToArray( const Self & v, MeasurementVectorType & mv )
167   {
168     mv = v;
169   }
170 
171   template<typename TArray>
AssignToArray(const Self & v,TArray & mv)172   static void AssignToArray( const Self & v, TArray & mv )
173   {
174     for( unsigned int i=0; i<3; i++ )
175       {
176       mv[i] = v[i];
177       }
178   }
179 
180   /** \note: the functions are preferred over the member variables as
181    * they are defined for all partial specialization
182    */
183   static const Self ITKCommon_EXPORT Zero;
184   static const Self ITKCommon_EXPORT One;
185 };
186 } // end namespace itk
187 
188 #endif // itkNumericTraitsRGBPixel_h
189