Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef __mitkITKImageImport_txx
18 #define __mitkITKImageImport_txx
19 #include "mitkITKImageImport.h"
20 #include "mitkImageReadAccessor.h"
21 
22 template <class TInputImage>
23 mitk::ITKImageImport<TInputImage>::ITKImageImport()
24 {
25 }
26 
27 template <class TInputImage>
28 mitk::ITKImageImport<TInputImage>::~ITKImageImport()
29 {
30 }
31 
32 template <class TInputImage>
33 typename mitk::ITKImageImport<TInputImage>::InputImageType *mitk::ITKImageImport<TInputImage>::GetInput(void)
34 {
35  return static_cast<TInputImage *>(this->ProcessObject::GetInput(0));
36 }
37 
38 template <class TInputImage>
39 void mitk::ITKImageImport<TInputImage>::SetInput(const InputImageType *input)
40 {
41  this->ProcessObject::SetNthInput(0, const_cast<TInputImage *>(input));
42 }
43 
44 template <class TInputImage>
45 void mitk::ITKImageImport<TInputImage>::SetGeometry(const BaseGeometry *geometry)
46 {
47  if (geometry != nullptr)
48  {
49  m_Geometry = static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer());
50  }
51  else
52  {
53  m_Geometry = nullptr;
54  }
55  Modified();
56 }
57 
58 template <class TInputImage>
59 void mitk::ITKImageImport<TInputImage>::GenerateOutputInformation()
60 {
61  InputImageConstPointer input = this->GetInput();
62  mitk::Image::Pointer output = this->GetOutput();
63 
64  itkDebugMacro(<< "GenerateOutputInformation()");
65 
66  output->InitializeByItk(input.GetPointer());
67 
68  if (m_Geometry.IsNotNull())
69  {
70  output->SetGeometry(m_Geometry);
71  }
72 }
73 
74 template <class TInputImage>
75 void mitk::ITKImageImport<TInputImage>::GenerateData()
76 {
77  InputImageConstPointer input = this->GetInput();
78  mitk::Image::Pointer output = this->GetOutput();
79 
80  output->SetImportChannel((void *)input->GetBufferPointer(), 0, mitk::Image::ReferenceMemory);
81 }
82 
83 template <class TInputImage>
84 void mitk::ITKImageImport<TInputImage>::GenerateInputRequestedRegion()
85 {
86  Superclass::GenerateInputRequestedRegion();
87 
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());
91 
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);
101 }
102 
103 template <class TInputImage>
104 void mitk::ITKImageImport<TInputImage>::SetNthOutput(DataObjectPointerArraySizeType idx, itk::DataObject *output)
105 {
106  if ((output == nullptr) && (idx == 0))
107  {
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());
116  }
117  Superclass::SetNthOutput(idx, output);
118 }
119 
120 template <typename ItkOutputImageType>
121 mitk::Image::Pointer mitk::ImportItkImage(const itk::SmartPointer<ItkOutputImageType> &itkimage,
122  const BaseGeometry *geometry,
123  bool update)
124 {
125  typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
126  importer->SetInput(itkimage);
127  importer->SetGeometry(geometry);
128  if (update)
129  importer->Update();
130  return importer->GetOutput();
131 }
132 
133 template <typename ItkOutputImageType>
134 mitk::Image::Pointer mitk::ImportItkImage(const ItkOutputImageType *itkimage, const BaseGeometry *geometry, bool update)
135 {
136  typename mitk::ITKImageImport<ItkOutputImageType>::Pointer importer = mitk::ITKImageImport<ItkOutputImageType>::New();
137  importer->SetInput(itkimage);
138  importer->SetGeometry(geometry);
139  if (update)
140  importer->Update();
141  return importer->GetOutput();
142 }
143 
144 template <typename ItkOutputImageType>
145 mitk::Image::Pointer mitk::GrabItkImageMemory(itk::SmartPointer<ItkOutputImageType> &itkimage,
146  mitk::Image *mitkImage,
147  const BaseGeometry *geometry,
148  bool update)
149 {
150  return GrabItkImageMemory(itkimage.GetPointer(), mitkImage, geometry, update);
151 }
152 
153 template <typename ItkOutputImageType>
154 mitk::Image::Pointer mitk::GrabItkImageMemory(ItkOutputImageType *itkimage,
155  mitk::Image *mitkImage,
156  const BaseGeometry *geometry,
157  bool update)
158 {
159  if (update)
160  itkimage->Update();
161 
162  mitk::Image::Pointer resultImage;
163  if (mitkImage != nullptr)
164  {
165  resultImage = mitkImage;
166 
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())
170  {
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())
174  return resultImage;
175  }
176  }
177  else
178  {
179  resultImage = mitk::Image::New();
180  }
181  resultImage->InitializeByItk(itkimage);
182  resultImage->SetImportVolume(itkimage->GetBufferPointer(), 0, 0, Image::ManageMemory);
183  itkimage->GetPixelContainer()->ContainerManageMemoryOff();
184 
185  if (geometry != nullptr)
186  resultImage->SetGeometry(static_cast<mitk::BaseGeometry *>(geometry->Clone().GetPointer()));
187 
188  return resultImage;
189 }
190 
191 #endif //__mitkITKImageImport_txx