Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkImageCast.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 #include <mitkImageAccessByItk.h>
14 #include <mitkImageCast.h>
15 
16 #include <itkCastImageFilter.h>
17 #include <itkImage.h>
18 
19 namespace mitk
20 {
21 #ifndef DOXYGEN_SKIP
22 
23  template <typename ItkOutputImageType>
24  void CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer<ItkOutputImageType> &itkOutputImage)
25  {
26  AccessFixedDimensionByItk_1(mitkImage, _CastToItkImage2Access, ItkOutputImageType::ImageDimension, itkOutputImage);
27  }
28 
29  template <typename TPixelType, unsigned int VImageDimension>
30  void CastToItkImage(const mitk::Image *mitkImage,
31  itk::SmartPointer<itk::VectorImage<TPixelType, VImageDimension>> &itkOutputImage)
32  {
33  AccessVectorFixedDimensionByItk_n(mitkImage, _CastToItkImage2Access, VImageDimension, (itkOutputImage));
34  }
35 
36  template <typename TPixel, unsigned int VImageDimension, class ItkOutputImageType>
37  void _CastToItkImage2Access(const itk::Image<TPixel, VImageDimension> *itkInputImage,
38  itk::SmartPointer<ItkOutputImageType> &itkOutputImage)
39  {
40  typedef itk::Image<TPixel, VImageDimension> ItkInputImageType;
41  if (typeid(ItkInputImageType) == typeid(ItkOutputImageType))
42  {
43  itkOutputImage = const_cast<ItkOutputImageType *>(reinterpret_cast<const ItkOutputImageType *>(itkInputImage));
44  return;
45  }
46  typedef itk::CastImageFilter<ItkInputImageType, ItkOutputImageType> CastImageFilterType;
47  typename CastImageFilterType::Pointer castImageFilter = CastImageFilterType::New();
48  castImageFilter->SetInput(itkInputImage);
49  castImageFilter->Update();
50  itkOutputImage = castImageFilter->GetOutput();
51  }
52 
53  template <typename TPixel, unsigned int VImageDimension, class ItkOutputImageType>
54  void _CastToItkImage2Access(const itk::VectorImage<TPixel, VImageDimension> *itkInputImage,
55  itk::SmartPointer<ItkOutputImageType> &itkOutputImage)
56  {
57  typedef itk::VectorImage<TPixel, VImageDimension> ItkInputImageType;
58  if (typeid(ItkInputImageType) == typeid(ItkOutputImageType))
59  {
60  itkOutputImage = const_cast<ItkOutputImageType *>(reinterpret_cast<const ItkOutputImageType *>(itkInputImage));
61  return;
62  }
63  typedef itk::CastImageFilter<ItkInputImageType, ItkOutputImageType> CastImageFilterType;
64  typename CastImageFilterType::Pointer castImageFilter = CastImageFilterType::New();
65  castImageFilter->SetInput(itkInputImage);
66  castImageFilter->Update();
67  itkOutputImage = castImageFilter->GetOutput();
68  }
69 
70 #endif // DOXYGEN_SKIP
71 }