Currently, there are several high-level methods for converting MITK images to and from ITK images, with different semantics. The aim of this bug report is to consolidate the conversion methods and research an improved memory management mechanism which may be allows to reduce the number of conversion methods.
Methods for ITK --> MITK conversion
This always copies ITK memory:
template<ItkOutputImageType>
void CastToMitkImage(const itk::SmartPointer<ItkOutputImageType>&, mitk::Image::Pointer&)
This takes ownership of the ITK image memory:
template <typename ItkOutputImageType>
Image::Pointer GrabItkImageMemory(itk::SmartPointer<ItkOutputImageType>& itkimage, mitk::Image* mitkImage = NULL, const Geometry3D* geometry = NULL, bool update = true)
This references, but doesn't manage the memory pointed to by the ITK image:
template <typename ItkOutputImageType>
Image::Pointer ImportItkImage(const itk::SmartPointer<ItkOutputImageType>& itkimage, const Geometry3D* geometry = NULL, bool update = true)
Methods for MITK --> ITK conversion
This method references MITK image memory, but doesn't manage it (used by the AccessByItk macros):
template<typename TPixel, unsigned int VDimension>
typename itk::Image<TPixel, VDimension>::Pointer ImageToItkImage(mitk::Image* mitkImage)
This method references the MITK image memory (but doesn't manage it) if the ITK image pixel type matches the MITK image type, otherwise it casts the pixel types by copying memory (uses the above method internally via AccessByItk):
template <typename ItkOutputImageType>
void CastToItkImage(const mitk::Image * mitkImage, itk::SmartPointer<ItkOutputImageType>& itkOutputImage)