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 itkVectorImageToImageAdaptor_h
19 #define itkVectorImageToImageAdaptor_h
20 
21 #include "itkImageAdaptor.h"
22 #include "itkVectorImage.h"
23 
24 namespace itk
25 {
26 namespace Accessor
27 {
28 /** \class VectorImageToImagePixelAccessor
29  * \brief Extract components from a VectorImage.
30  *
31  * This accessor is used to extract components from a VectorImage. It is used
32  * from VectorImageComponentExtractAdaptor. The component to extract is set
33  * using SetExtractComponentIdx.
34  *
35  * \note
36  * This work is part of the National Alliance for Medical Image Computing
37  * (NAMIC), funded by the National Institutes of Health through the NIH Roadmap
38  * for Medical Research, Grant U54 EB005149.
39  * \deprecated Please use the more generic NthElementImageAdaptor.
40  *
41  * \sa NthElementImageAdaptor
42  *
43  * \ingroup ImageAdaptors
44  * \ingroup ITKImageAdaptors
45  */
46 template< typename TType >
47 class VectorImageToImagePixelAccessor
48   : private DefaultVectorPixelAccessor< TType >
49 {
50 public:
51 
52   using VectorLengthType = unsigned int;
53 
54   /** External type alias. It defines the external aspect
55    * that this class will exhibit. */
56   using ExternalType = TType;
57 
58   /** Internal type alias used by the ImageAdaptor for the buffer pointer */
59   using InternalType = TType;
60 
61   using ActualPixelType = VariableLengthVector< TType >;
62 
Set(ActualPixelType output,const ExternalType & input)63   inline void Set(ActualPixelType output, const ExternalType & input) const
64   {
65     output[m_ComponentIdx] = input;
66   }
67 
Set(InternalType & output,const ExternalType & input,const unsigned long offset)68   inline void Set(InternalType &output, const ExternalType & input,
69                   const unsigned long offset) const
70   {
71     return Set( Superclass::Get( output, offset ), input );
72   }
73 
Get(const ActualPixelType & input)74   inline ExternalType Get(const ActualPixelType & input) const
75   {
76     ExternalType output;
77 
78     output = input[m_ComponentIdx];
79     return output;
80   }
81 
Get(const InternalType & input,const SizeValueType offset)82   inline ExternalType Get(const InternalType &input, const SizeValueType offset) const
83   {
84     return Get( Superclass::Get(input, offset) );
85   }
86 
SetExtractComponentIdx(VectorLengthType idx)87   void SetExtractComponentIdx(VectorLengthType idx)
88   {
89     m_ComponentIdx = idx;
90   }
91 
GetExtractComponentIdx()92   VectorLengthType GetExtractComponentIdx() const
93   {
94     return m_ComponentIdx;
95   }
96 
97   /** Set the length of each vector in the VectorImage */
SetVectorLength(VectorLengthType l)98   void SetVectorLength(VectorLengthType l)
99   {
100     Superclass::SetVectorLength( l );
101   }
102 
103   /** Get Vector lengths */
GetVectorLength()104   VectorLengthType GetVectorLength() const { return Superclass::GetVectorLength(); }
105 
106   VectorImageToImagePixelAccessor( unsigned int length = 1)
107 
108     {
109     Superclass::SetVectorLength( length );
110     }
111 
112 protected:
113   using Superclass = DefaultVectorPixelAccessor< TType >;
114 
115 private:
116   VectorLengthType m_ComponentIdx{0};
117 };
118 } // end namespace Accessor
119 
120 /** \class VectorImageToImageAdaptor
121  * \brief Presents a VectorImage and extracts a component from it into an image.
122  *
123  * The class is expected to be templated over a pixel type and dimension. These
124  * are the pixel types and dimension of the VectorImage.
125  *
126  * The component to extract is set with SetExtractComponentIdx() method.
127  *
128  * \note
129  * This work is part of the National Alliance for Medical Image Computing
130  * (NAMIC), funded by the National Institutes of Health through the NIH Roadmap
131  * for Medical Research, Grant U54 EB005149.
132  *
133  * \ingroup ImageAdaptors
134  *
135  * \ingroup ITKImageAdaptors
136  *
137  * \wiki
138  * \wikiexample{VectorImages/VectorImageToImageAdaptor,View a component of a vector image as if it were a scalar image}
139  * \endwiki
140  */
141 template< typename TPixelType, unsigned int Dimension >
142 class VectorImageToImageAdaptor:public
143   ImageAdaptor< VectorImage< TPixelType, Dimension >,
144                 Accessor::VectorImageToImagePixelAccessor< TPixelType > >
145 {
146 public:
147   ITK_DISALLOW_COPY_AND_ASSIGN(VectorImageToImageAdaptor);
148 
149   /** Standard class type aliases. */
150   using Self = VectorImageToImageAdaptor;
151   using VectorImageType = VectorImage< TPixelType, Dimension >;
152   using Superclass = ImageAdaptor< VectorImageType,
153                         Accessor::VectorImageToImagePixelAccessor< TPixelType >  >;
154 
155   using Pointer = SmartPointer< Self >;
156   using ConstPointer = SmartPointer< const Self >;
157 
158   /** Method for creation through the object factory. */
159   itkNewMacro(Self);
160 
161   /** Run-time type information (and related methods). */
162   itkTypeMacro(VectorImageToImageAdaptor, ImageAdaptor);
163 
164   /** PixelContainer type alias support Used to construct a container for
165    * the pixel data. */
166   using PixelContainer = typename Superclass::PixelContainer;
167   using PixelContainerPointer = typename Superclass::PixelContainerPointer;
168   using PixelContainerConstPointer = typename Superclass::PixelContainerConstPointer;
169   using IOPixelType = typename Superclass::IOPixelType;
170 
171   /** Typedef for the length of vectors in the VectorImage. */
172   using VectorLengthType = typename VectorImageType::VectorLengthType;
173 
174   // Set/GetMethods to set the component to be extracted.
SetExtractComponentIndex(VectorLengthType componentIdx)175   void SetExtractComponentIndex(VectorLengthType componentIdx)
176   {
177     this->GetPixelAccessor().SetExtractComponentIdx(componentIdx);
178   }
179 
180   // Set/GetMethods to set the component to be extracted.
GetExtractComponentIndex()181   VectorLengthType GetExtractComponentIndex() const
182   {
183     return this->GetPixelAccessor().GetExtractComponentIdx();
184   }
185 
186 protected:
187   VectorImageToImageAdaptor() = default;
188   ~VectorImageToImageAdaptor() override = default;
189 };
190 } // end namespace itk
191 
192 #endif
193