Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef IMAGETOITK_TXX_INCLUDED_C1C2FCD2
00020 #define IMAGETOITK_TXX_INCLUDED_C1C2FCD2
00021
00022 #include "mitkImageToItk.h"
00023 #include "mitkBaseProcess.h"
00024 #include "itkImportMitkImageContainer.h"
00025 #include <itkSmartPointerForwardReference.txx>
00026
00027
00028 template <class TOutputImage>
00029 void mitk::ImageToItk<TOutputImage>::SetInput(const mitk::Image *input)
00030 {
00031 if(input == NULL)
00032 itkExceptionMacro( << "image is null" );
00033 if(input->GetDimension()!=TOutputImage::GetImageDimension())
00034 itkExceptionMacro( << "image has dimension " << input->GetDimension() << " instead of " << TOutputImage::GetImageDimension() );
00035
00036
00037 if(!(input->GetPixelType() == typeid(PixelType)))
00038 itkExceptionMacro( << "image has wrong pixel type " );
00039
00040
00041 itk::ProcessObject::SetNthInput(0,
00042 const_cast< mitk::Image * >( input ) );
00043 }
00044
00045 template<class TOutputImage>
00046 void mitk::ImageToItk<TOutputImage>::SetInput( unsigned int index, const mitk::Image * input )
00047 {
00048 if( index+1 > this->GetNumberOfInputs() )
00049 {
00050 this->SetNumberOfRequiredInputs( index + 1 );
00051 }
00052
00053 if(input == NULL)
00054 itkExceptionMacro( << "image is null" );
00055 if(input->GetDimension()!=TOutputImage::GetImageDimension())
00056 itkExceptionMacro( << "image has dimension " << input->GetDimension() << " instead of " << TOutputImage::GetImageDimension() );
00057
00058
00059 if(!(input->GetPixelType() == typeid(PixelType)))
00060 itkExceptionMacro( << "image has wrong pixel type " );
00061
00062
00063 itk::ProcessObject::SetNthInput(index,
00064 const_cast< mitk::Image *>( input ) );
00065 }
00066
00067 template<class TOutputImage>
00068 const mitk::Image *mitk::ImageToItk<TOutputImage>::GetInput(void)
00069 {
00070 if (this->GetNumberOfInputs() < 1)
00071 {
00072 return 0;
00073 }
00074
00075 return static_cast< const mitk::Image * >
00076 (itk::ProcessObject::GetInput(0) );
00077 }
00078
00079 template<class TOutputImage>
00080 const mitk::Image *mitk::ImageToItk<TOutputImage>::GetInput(unsigned int idx)
00081 {
00082 return static_cast< mitk::Image * >
00083 (itk::ProcessObject::GetInput(idx));
00084 }
00085
00086 template<class TOutputImage>
00087 void mitk::ImageToItk<TOutputImage>
00088 ::GenerateData()
00089 {
00090
00091 mitk::Image::ConstPointer input = this->GetInput();
00092 typename Superclass::OutputImageType::Pointer output = this->GetOutput();
00093
00094
00095 unsigned long noBytes = input->GetDimension(0);
00096 for (unsigned int i=1; i<TOutputImage::GetImageDimension(); ++i)
00097 {
00098 noBytes = noBytes * input->GetDimension(i);
00099 }
00100
00101
00102 m_ImageDataItem = const_cast<mitk::Image*>(input.GetPointer())->GetChannelData( m_Channel );
00103 if(m_ImageDataItem.GetPointer() == NULL)
00104 {
00105 itkWarningMacro(<< "no image data to import in ITK image");
00106
00107 RegionType bufferedRegion;
00108 output->SetBufferedRegion(bufferedRegion);
00109 return;
00110 }
00111
00112 if (m_CopyMemFlag)
00113 {
00114 itkDebugMacro("copyMem ...");
00115
00116 output->Allocate();
00117
00118 memcpy( (PixelType *) output->GetBufferPointer(), m_ImageDataItem->GetData(), sizeof(PixelType)*noBytes);
00119
00120 }
00121 else
00122 {
00123 itkDebugMacro("do not copyMem ...");
00124 typedef itk::ImportMitkImageContainer< unsigned long, PixelType > ImportContainerType;
00125 typename ImportContainerType::Pointer import;
00126
00127 import = ImportContainerType::New();
00128 import->Initialize();
00129
00130 itkDebugMacro( << "size of container = " << import->Size() );
00131 import->SetImageDataItem(m_ImageDataItem);
00132
00133 output->SetPixelContainer(import);
00134 itkDebugMacro( << "size of container = " << import->Size() );
00135 }
00136 }
00137
00138 template<class TOutputImage>
00139 void mitk::ImageToItk<TOutputImage>
00140 ::UpdateOutputInformation()
00141 {
00142 mitk::Image::ConstPointer input = this->GetInput();
00143 if(input.IsNotNull() && (input->GetSource()!=NULL) && input->GetSource()->Updating())
00144 {
00145 typename Superclass::OutputImageType::Pointer output = this->GetOutput();
00146 unsigned long t1 = input->GetUpdateMTime()+1;
00147 if (t1 > this->m_OutputInformationMTime.GetMTime())
00148 {
00149 output->SetPipelineMTime(t1);
00150
00151 this->GenerateOutputInformation();
00152
00153 this->m_OutputInformationMTime.Modified();
00154 }
00155 return;
00156 }
00157 Superclass::UpdateOutputInformation();
00158 }
00159
00160 template<class TOutputImage>
00161 void mitk::ImageToItk<TOutputImage>
00162 ::GenerateOutputInformation()
00163 {
00164 mitk::Image::ConstPointer input = this->GetInput();
00165 typename Superclass::OutputImageType::Pointer output = this->GetOutput();
00166
00167
00168 SizeType size;
00169 const unsigned int itkDimMin3 = (TOutputImage::ImageDimension > 3 ? TOutputImage::ImageDimension : 3);
00170 const unsigned int itkDimMax3 = (TOutputImage::ImageDimension < 3 ? TOutputImage::ImageDimension : 3);
00171 typename Superclass::OutputImageType::PointType::ValueType origin[ itkDimMin3 ];
00172 typename Superclass::OutputImageType::SpacingType::ComponentType spacing[ itkDimMin3 ];
00173 typename Superclass::OutputImageType::DirectionType direction;
00174
00175
00176 unsigned int i;
00177 for ( i=0; i < itkDimMax3; ++i)
00178 {
00179 size[i] = input->GetDimension(i);
00180 spacing[i] = input->GetGeometry()->GetSpacing()[i];
00181 }
00182 for ( ; i < TOutputImage::ImageDimension; ++i)
00183 {
00184 origin[i] = 0.0;
00185 size[i] = input->GetDimension(i);
00186 spacing[i] = 1.0;
00187 }
00188
00189
00190 IndexType start;
00191 start.Fill( 0 );
00192 RegionType region;
00193 region.SetIndex( start );
00194 region.SetSize( size );
00195
00196
00197 const mitk::Point3D& mitkorigin = input->GetGeometry()->GetOrigin();
00198 itk2vtk(mitkorigin, origin);
00199
00200
00201 direction.SetIdentity();
00202 unsigned int j;
00203 const AffineTransform3D::MatrixType& matrix = input->GetGeometry()->GetIndexToWorldTransform()->GetMatrix();
00204
00207
00208
00209
00210 if ( itkDimMax3 >= 3)
00211 {
00212 for ( i=0; i < itkDimMax3; ++i)
00213 for( j=0; j < itkDimMax3; ++j )
00214 direction[i][j] = matrix[i][j]/spacing[j];
00215 }
00216
00217
00218 output->SetRegions( region );
00219 output->SetOrigin( origin );
00220 output->SetSpacing( spacing );
00221 output->SetDirection( direction );
00222 }
00223
00224 template<class TOutputImage>
00225 void
00226 mitk::ImageToItk<TOutputImage>
00227 ::PrintSelf(std::ostream& os, itk::Indent indent) const
00228 {
00229 Superclass::PrintSelf(os,indent);
00230 }
00231
00232 #endif //IMAGETOITK_TXX_INCLUDED_C1C2FCD2