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 
19 #ifndef itkImageVectorOptimizerParametersHelper_hxx
20 #define itkImageVectorOptimizerParametersHelper_hxx
21 
22 #include "itkImageVectorOptimizerParametersHelper.h"
23 
24 namespace itk
25 {
26 /** Default contstructor */
27 template< typename TValue,
28           unsigned int NVectorDimension,
29           unsigned int VImageDimension >
30 ImageVectorOptimizerParametersHelper< TValue, NVectorDimension, VImageDimension >
ImageVectorOptimizerParametersHelper()31 ::ImageVectorOptimizerParametersHelper()
32 {
33   m_ParameterImage = nullptr;
34 }
35 
36 /** Move the data pointer */
37 template< typename TValue,
38           unsigned int NVectorDimension,
39           unsigned int VImageDimension >
40 void
41 ImageVectorOptimizerParametersHelper< TValue, NVectorDimension, VImageDimension >
MoveDataPointer(CommonContainerType * container,TValue * pointer)42 ::MoveDataPointer( CommonContainerType* container, TValue * pointer )
43 {
44   if( m_ParameterImage.IsNull() )
45     {
46     itkGenericExceptionMacro("ImageVectorOptimizerParametersHelper::"
47       "MoveDataPointer: m_ParameterImage must be defined.");
48     }
49   // The buffer for Image<Vector> points to Vector type, not TValue, so
50   // have to cast.
51   using vectorElement = typename ParameterImageType::PixelContainer::Element;
52   auto * vectorPointer = reinterpret_cast<vectorElement *>(pointer);
53   // We're expecting the new memory buffer t be of same size.
54   unsigned int sizeInVectors = m_ParameterImage->GetPixelContainer()->Size();
55   // After this call, PixelContainer will *not* manage its memory.
56   this->m_ParameterImage->GetPixelContainer()->SetImportPointer( vectorPointer,
57                                                               sizeInVectors );
58   Superclass::MoveDataPointer( container, pointer );
59 }
60 
61 /** Set parameter image */
62 template< typename TValue,
63           unsigned int NVectorDimension,
64           unsigned int VImageDimension >
65 void
66 ImageVectorOptimizerParametersHelper< TValue, NVectorDimension, VImageDimension >
SetParametersObject(CommonContainerType * container,LightObject * object)67 ::SetParametersObject(CommonContainerType * container, LightObject * object)
68 {
69   if( object == nullptr )
70     {
71     m_ParameterImage = nullptr;
72     return;
73     }
74   else
75     {
76     auto * image = dynamic_cast<ParameterImageType *>( object );
77     if( image == nullptr )
78       {
79       itkGenericExceptionMacro(
80         "ImageVectorOptimizerParametersHelper::SetParametersObject: object is "
81         "not of proper image type. Expected VectorImage, received "
82         << object->GetNameOfClass() )
83       }
84     m_ParameterImage = image;
85     //The PixelContainer for Image<Vector> points to type Vector, so we have
86     // to determine the number of raw elements of type TValue in the buffer
87     // and cast a pointer to it for assignment to the Array data pointer.
88     typename CommonContainerType::SizeValueType sz = image->GetPixelContainer()->Size() * NVectorDimension;
89     auto * valuePointer = reinterpret_cast<TValue *>
90                               ( image->GetPixelContainer()->GetBufferPointer() );
91     //Set the Array's pointer to the image data buffer. By default it will
92     // not manage the memory.
93     container->SetData( valuePointer, sz, false );
94     }
95 }
96 
97 }//namespace itk
98 #endif
99