1 /*============================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center (DKFZ)
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
11 ============================================================================*/
13 #ifndef __mitkITKImageImport_txx
14 #define __mitkITKImageImport_txx
15 #include "mitkITKImageImport.h"
16 #include "mitkImageReadAccessor.h"
18 template <class TInputImage>
19 mitk::ITKImageImport<TInputImage>::ITKImageImport()
23 template <class TInputImage>
24 mitk::ITKImageImport<TInputImage>::~ITKImageImport()
28 template <class TInputImage>
29 typename mitk::ITKImageImport<TInputImage>::InputImageType *mitk::ITKImageImport<TInputImage>::GetInput(void)
31 return static_cast<TInputImage *>(this->ProcessObject::GetInput(0));
34 template <class TInputImage>
35 void mitk::ITKImageImport<TInputImage>::SetInput(const InputImageType *input)
37 this->ProcessObject::SetNthInput(0, const_cast<TInputImage *>(input));
40 template <class TInputImage>
41 void mitk::ITKImageImport<TInputImage>::SetGeometry(const BaseGeometry *geometry)
43 if (geometry != nullptr)
45 m_Geometry = static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer());
54 template <class TInputImage>
55 void mitk::ITKImageImport<TInputImage>::GenerateOutputInformation()
57 InputImageConstPointer input = this->GetInput();
58 mitk::Image::Pointer output = this->GetOutput();
60 itkDebugMacro(<< "GenerateOutputInformation()");
62 output->InitializeByItk(input.GetPointer());
64 if (m_Geometry.IsNotNull())
66 output->SetGeometry(m_Geometry);
70 template <class TInputImage>
71 void mitk::ITKImageImport<TInputImage>::GenerateData()
73 InputImageConstPointer input = this->GetInput();
74 mitk::Image::Pointer output = this->GetOutput();
76 output->SetImportChannel((void *)input->GetBufferPointer(), 0, mitk::Image::ReferenceMemory);
79 template <class TInputImage>
80 void mitk::ITKImageImport<TInputImage>::GenerateInputRequestedRegion()
82 Superclass::GenerateInputRequestedRegion();
84 // Input is an image, cast away the constness so we can set
85 // the requested region.
86 InputImagePointer input = const_cast<TInputImage *>(this->GetInput());
88 // Use the function object RegionCopier to copy the output region
89 // to the input. The default region copier has default implementations
90 // to handle the cases where the input and output are the same
91 // dimension, the input a higher dimension than the output, and the
92 // input a lower dimension than the output.
93 InputImageRegionType inputRegion;
94 OutputToInputRegionCopierType regionCopier;
95 regionCopier(inputRegion, this->GetOutput()->GetRequestedRegion());
96 input->SetRequestedRegion(inputRegion);
99 template <class TInputImage>
100 void mitk::ITKImageImport<TInputImage>::SetNthOutput(DataObjectPointerArraySizeType idx, itk::DataObject *output)
102 if ((output == nullptr) && (idx == 0))
104 // we are disconnected from our output:
105 // copy buffer of input to output, because we
106 // cannot guarantee that the input (to which our
107 // output is refering) will stay alive.
108 InputImageConstPointer input = this->GetInput();
109 mitk::Image::Pointer currentOutput = this->GetOutput();
110 if (input.IsNotNull() && currentOutput.IsNotNull())
111 currentOutput->SetChannel(input->GetBufferPointer());
113 Superclass::SetNthOutput(idx, output);
116 template <typename ItkOutputImageType>
117 mitk::Image::Pointer mitk::ImportItkImage(const itk::SmartPointer<ItkOutputImageType> &itkimage,
118 const BaseGeometry *geometry,
121 typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
122 importer->SetInput(itkimage);
123 importer->SetGeometry(geometry);
126 return importer->GetOutput();
129 template <typename ItkOutputImageType>
130 mitk::Image::Pointer mitk::ImportItkImage(const ItkOutputImageType *itkimage, const BaseGeometry *geometry, bool update)
132 typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
133 importer->SetInput(itkimage);
134 importer->SetGeometry(geometry);
137 return importer->GetOutput();
140 template <typename ItkOutputImageType>
141 mitk::Image::Pointer mitk::GrabItkImageMemory(itk::SmartPointer<ItkOutputImageType> &itkimage,
142 mitk::Image *mitkImage,
143 const BaseGeometry *geometry,
146 return GrabItkImageMemory(itkimage.GetPointer(), mitkImage, geometry, update);
149 template <typename ItkOutputImageType>
150 mitk::Image::Pointer mitk::GrabItkImageMemory(ItkOutputImageType *itkimage,
151 mitk::Image *mitkImage,
152 const BaseGeometry *geometry,
158 mitk::Image::Pointer resultImage;
159 if (mitkImage != nullptr)
161 resultImage = mitkImage;
163 // test the pointer equality with read accessor only if mitk Image is intialized, otherwise an Exception is thrown
164 // by the ReadAccessor
165 if (mitkImage->IsInitialized())
167 // check the data pointer, for that, we need to ignore the lock of the mitkImage
168 mitk::ImageReadAccessor read_probe(mitk::Image::Pointer(mitkImage), nullptr, mitk::ImageAccessorBase::IgnoreLock);
169 if (itkimage->GetBufferPointer() == read_probe.GetData())
175 resultImage = mitk::Image::New();
177 resultImage->InitializeByItk(itkimage);
178 resultImage->SetImportVolume(itkimage->GetBufferPointer(), 0, 0, Image::ManageMemory);
179 itkimage->GetPixelContainer()->ContainerManageMemoryOff();
181 if (geometry != nullptr)
182 resultImage->SetGeometry(static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer()));
187 #endif //__mitkITKImageImport_txx