1 /*===================================================================
3 The Medical Imaging Interaction Toolkit (MITK)
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 See LICENSE.txt or http://www.mitk.org for details.
15 ===================================================================*/
17 #ifndef __mitkITKImageImport_txx
18 #define __mitkITKImageImport_txx
19 #include "mitkITKImageImport.h"
20 #include "mitkImageReadAccessor.h"
22 template <class TInputImage>
23 mitk::ITKImageImport<TInputImage>::ITKImageImport()
27 template <class TInputImage>
28 mitk::ITKImageImport<TInputImage>::~ITKImageImport()
32 template <class TInputImage>
33 typename mitk::ITKImageImport<TInputImage>::InputImageType *mitk::ITKImageImport<TInputImage>::GetInput(void)
35 return static_cast<TInputImage *>(this->ProcessObject::GetInput(0));
38 template <class TInputImage>
39 void mitk::ITKImageImport<TInputImage>::SetInput(const InputImageType *input)
41 this->ProcessObject::SetNthInput(0, const_cast<TInputImage *>(input));
44 template <class TInputImage>
45 void mitk::ITKImageImport<TInputImage>::SetGeometry(const BaseGeometry *geometry)
47 if (geometry != nullptr)
49 m_Geometry = static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer());
58 template <class TInputImage>
59 void mitk::ITKImageImport<TInputImage>::GenerateOutputInformation()
61 InputImageConstPointer input = this->GetInput();
62 mitk::Image::Pointer output = this->GetOutput();
64 itkDebugMacro(<< "GenerateOutputInformation()");
66 output->InitializeByItk(input.GetPointer());
68 if (m_Geometry.IsNotNull())
70 output->SetGeometry(m_Geometry);
74 template <class TInputImage>
75 void mitk::ITKImageImport<TInputImage>::GenerateData()
77 InputImageConstPointer input = this->GetInput();
78 mitk::Image::Pointer output = this->GetOutput();
80 output->SetImportChannel((void *)input->GetBufferPointer(), 0, mitk::Image::ReferenceMemory);
83 template <class TInputImage>
84 void mitk::ITKImageImport<TInputImage>::GenerateInputRequestedRegion()
86 Superclass::GenerateInputRequestedRegion();
88 // Input is an image, cast away the constness so we can set
89 // the requested region.
90 InputImagePointer input = const_cast<TInputImage *>(this->GetInput());
92 // Use the function object RegionCopier to copy the output region
93 // to the input. The default region copier has default implementations
94 // to handle the cases where the input and output are the same
95 // dimension, the input a higher dimension than the output, and the
96 // input a lower dimension than the output.
97 InputImageRegionType inputRegion;
98 OutputToInputRegionCopierType regionCopier;
99 regionCopier(inputRegion, this->GetOutput()->GetRequestedRegion());
100 input->SetRequestedRegion(inputRegion);
103 template <class TInputImage>
104 void mitk::ITKImageImport<TInputImage>::SetNthOutput(DataObjectPointerArraySizeType idx, itk::DataObject *output)
106 if ((output == nullptr) && (idx == 0))
108 // we are disconnected from our output:
109 // copy buffer of input to output, because we
110 // cannot guarantee that the input (to which our
111 // output is refering) will stay alive.
112 InputImageConstPointer input = this->GetInput();
113 mitk::Image::Pointer currentOutput = this->GetOutput();
114 if (input.IsNotNull() && currentOutput.IsNotNull())
115 currentOutput->SetChannel(input->GetBufferPointer());
117 Superclass::SetNthOutput(idx, output);
120 template <typename ItkOutputImageType>
121 mitk::Image::Pointer mitk::ImportItkImage(const itk::SmartPointer<ItkOutputImageType> &itkimage,
122 const BaseGeometry *geometry,
125 typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
126 importer->SetInput(itkimage);
127 importer->SetGeometry(geometry);
130 return importer->GetOutput();
133 template <typename ItkOutputImageType>
134 mitk::Image::Pointer mitk::ImportItkImage(const ItkOutputImageType *itkimage, const BaseGeometry *geometry, bool update)
136 typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
137 importer->SetInput(itkimage);
138 importer->SetGeometry(geometry);
141 return importer->GetOutput();
144 template <typename ItkOutputImageType>
145 mitk::Image::Pointer mitk::GrabItkImageMemory(itk::SmartPointer<ItkOutputImageType> &itkimage,
146 mitk::Image *mitkImage,
147 const BaseGeometry *geometry,
150 return GrabItkImageMemory(itkimage.GetPointer(), mitkImage, geometry, update);
153 template <typename ItkOutputImageType>
154 mitk::Image::Pointer mitk::GrabItkImageMemory(ItkOutputImageType *itkimage,
155 mitk::Image *mitkImage,
156 const BaseGeometry *geometry,
162 mitk::Image::Pointer resultImage;
163 if (mitkImage != nullptr)
165 resultImage = mitkImage;
167 // test the pointer equality with read accessor only if mitk Image is intialized, otherwise an Exception is thrown
168 // by the ReadAccessor
169 if (mitkImage->IsInitialized())
171 // check the data pointer, for that, we need to ignore the lock of the mitkImage
172 mitk::ImageReadAccessor read_probe(mitk::Image::Pointer(mitkImage), nullptr, mitk::ImageAccessorBase::IgnoreLock);
173 if (itkimage->GetBufferPointer() == read_probe.GetData())
179 resultImage = mitk::Image::New();
181 resultImage->InitializeByItk(itkimage);
182 resultImage->SetImportVolume(itkimage->GetBufferPointer(), 0, 0, Image::ManageMemory);
183 itkimage->GetPixelContainer()->ContainerManageMemoryOff();
185 if (geometry != nullptr)
186 resultImage->SetGeometry(static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer()));
191 #endif //__mitkITKImageImport_txx