Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkITKImageImport.txx
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef __mitkITKImageImport_txx
14 #define __mitkITKImageImport_txx
15 #include "mitkITKImageImport.h"
16 #include "mitkImageReadAccessor.h"
17 
18 template <class TInputImage>
19 mitk::ITKImageImport<TInputImage>::ITKImageImport()
20 {
21 }
22 
23 template <class TInputImage>
24 mitk::ITKImageImport<TInputImage>::~ITKImageImport()
25 {
26 }
27 
28 template <class TInputImage>
29 typename mitk::ITKImageImport<TInputImage>::InputImageType *mitk::ITKImageImport<TInputImage>::GetInput(void)
30 {
31  return static_cast<TInputImage *>(this->ProcessObject::GetInput(0));
32 }
33 
34 template <class TInputImage>
35 void mitk::ITKImageImport<TInputImage>::SetInput(const InputImageType *input)
36 {
37  this->ProcessObject::SetNthInput(0, const_cast<TInputImage *>(input));
38 }
39 
40 template <class TInputImage>
41 void mitk::ITKImageImport<TInputImage>::SetGeometry(const BaseGeometry *geometry)
42 {
43  if (geometry != nullptr)
44  {
45  m_Geometry = static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer());
46  }
47  else
48  {
49  m_Geometry = nullptr;
50  }
51  Modified();
52 }
53 
54 template <class TInputImage>
55 void mitk::ITKImageImport<TInputImage>::GenerateOutputInformation()
56 {
57  InputImageConstPointer input = this->GetInput();
58  mitk::Image::Pointer output = this->GetOutput();
59 
60  itkDebugMacro(<< "GenerateOutputInformation()");
61 
62  output->InitializeByItk(input.GetPointer());
63 
64  if (m_Geometry.IsNotNull())
65  {
66  output->SetGeometry(m_Geometry);
67  }
68 }
69 
70 template <class TInputImage>
71 void mitk::ITKImageImport<TInputImage>::GenerateData()
72 {
73  InputImageConstPointer input = this->GetInput();
74  mitk::Image::Pointer output = this->GetOutput();
75 
76  output->SetImportChannel((void *)input->GetBufferPointer(), 0, mitk::Image::ReferenceMemory);
77 }
78 
79 template <class TInputImage>
80 void mitk::ITKImageImport<TInputImage>::GenerateInputRequestedRegion()
81 {
82  Superclass::GenerateInputRequestedRegion();
83 
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());
87 
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);
97 }
98 
99 template <class TInputImage>
100 void mitk::ITKImageImport<TInputImage>::SetNthOutput(DataObjectPointerArraySizeType idx, itk::DataObject *output)
101 {
102  if ((output == nullptr) && (idx == 0))
103  {
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());
112  }
113  Superclass::SetNthOutput(idx, output);
114 }
115 
116 template <typename ItkOutputImageType>
117 mitk::Image::Pointer mitk::ImportItkImage(const itk::SmartPointer<ItkOutputImageType> &itkimage,
118  const BaseGeometry *geometry,
119  bool update)
120 {
121  typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
122  importer->SetInput(itkimage);
123  importer->SetGeometry(geometry);
124  if (update)
125  importer->Update();
126  return importer->GetOutput();
127 }
128 
129 template <typename ItkOutputImageType>
130 mitk::Image::Pointer mitk::ImportItkImage(const ItkOutputImageType *itkimage, const BaseGeometry *geometry, bool update)
131 {
132  typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
133  importer->SetInput(itkimage);
134  importer->SetGeometry(geometry);
135  if (update)
136  importer->Update();
137  return importer->GetOutput();
138 }
139 
140 template <typename ItkOutputImageType>
141 mitk::Image::Pointer mitk::GrabItkImageMemory(itk::SmartPointer<ItkOutputImageType> &itkimage,
142  mitk::Image *mitkImage,
143  const BaseGeometry *geometry,
144  bool update)
145 {
146  return GrabItkImageMemory(itkimage.GetPointer(), mitkImage, geometry, update);
147 }
148 
149 template <typename ItkOutputImageType>
150 mitk::Image::Pointer mitk::GrabItkImageMemory(ItkOutputImageType *itkimage,
151  mitk::Image *mitkImage,
152  const BaseGeometry *geometry,
153  bool update)
154 {
155  if (update)
156  itkimage->Update();
157 
158  mitk::Image::Pointer resultImage;
159  if (mitkImage != nullptr)
160  {
161  resultImage = mitkImage;
162 
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())
166  {
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())
170  return resultImage;
171  }
172  }
173  else
174  {
175  resultImage = mitk::Image::New();
176  }
177  resultImage->InitializeByItk(itkimage);
178  resultImage->SetImportVolume(itkimage->GetBufferPointer(), 0, 0, Image::ManageMemory);
179  itkimage->GetPixelContainer()->ContainerManageMemoryOff();
180 
181  if (geometry != nullptr)
182  resultImage->SetGeometry(static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer()));
183 
184  return resultImage;
185 }
186 
187 #endif //__mitkITKImageImport_txx