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