FAST  3.2.0
Framework for Heterogeneous Medical Image Computing and Visualization
ITKImageImporter.hpp
Go to the documentation of this file.
1 #ifndef ITKIMAGEIMPORTER_HPP_
2 #define ITKIMAGEIMPORTER_HPP_
3 
4 #include "FAST/ProcessObject.hpp"
5 #include "FAST/Data/Image.hpp"
6 
7 #include <itkImageSource.h>
8 #include <itkImageRegionIterator.h>
9 
10 namespace fast {
11 
12 template <class TImage>
13 class FAST_EXPORT ITKImageImporter : public ProcessObject {
15  public:
16  void setInput(typename TImage::Pointer image);
17  private:
19  void execute();
20  void* getITKDataPointer();
21 
22  typename TImage::Pointer mInput;
23 };
24 
25 } // end namespace fast
26 
27 template<class TImage>
29  createOutputPort<fast::Image>(0);
30 }
31 
32 template<class TImage>
34  typename TImage::Pointer image) {
35 
36  mInput = image;
37  mIsModified = true;
38 }
39 
40 template<class TImage>
42  typename TImage::PixelType* data;
43  itk::ImageRegionIterator<TImage> imageIterator(mInput,
44  mInput->GetLargestPossibleRegion());
45 
46  typename TImage::RegionType region = mInput->GetLargestPossibleRegion();
47  unsigned int width = region.GetSize()[0];
48  unsigned int height = region.GetSize()[1];
49  if(TImage::ImageDimension == 2) {
50  data = new typename TImage::PixelType[width*height];
51  while (!imageIterator.IsAtEnd()) {
52  unsigned int x = imageIterator.GetIndex()[0];
53  unsigned int y = imageIterator.GetIndex()[1];
54  data[x + y*width] = imageIterator.Get();
55 
56  ++imageIterator;
57  }
58  } else {
59  unsigned int depth = region.GetSize()[2];
60  data = new typename TImage::PixelType[width*height*depth];
61 
62  while (!imageIterator.IsAtEnd()) {
63  unsigned int x = imageIterator.GetIndex()[0];
64  unsigned int y = imageIterator.GetIndex()[1];
65  unsigned int z = imageIterator.GetIndex()[2];
66  data[x + y*width + z*width*height] = imageIterator.Get();
67 
68  ++imageIterator;
69  }
70  }
71 
72  return (void*)data;
73 }
74 
75 
76 
77 template<class TImage>
79 
80  // Make sure ITK input is updated
81  mInput->Update();
82 
83  DataType type;
84  if(typeid(typename TImage::PixelType) == typeid(float)) {
85  type = TYPE_FLOAT;
86  } else if(typeid(typename TImage::PixelType) == typeid(char)) {
87  type = TYPE_INT8;
88  } else if(typeid(typename TImage::PixelType) == typeid(unsigned char)) {
89  type = TYPE_UINT8;
90  } else if(typeid(typename TImage::PixelType) == typeid(short)) {
91  type = TYPE_INT16;
92  } else if(typeid(typename TImage::PixelType) == typeid(unsigned short)) {
93  type = TYPE_UINT16;
94  } else {
95  throw Exception("Unsupported pixel type sent to ITKImageImporter");
96  }
97  void* data = getITKDataPointer();
98  typename TImage::RegionType region = mInput->GetLargestPossibleRegion();
99  unsigned int width = region.GetSize()[0];
100  unsigned int height = region.GetSize()[1];
101  Image::pointer output = getOutputData<Image>();
102  if(TImage::ImageDimension == 2) {
103  output->create(width, height, type, 1, Host::getInstance(), data);
104  } else if(TImage::ImageDimension == 3) {
105  unsigned int depth = region.GetSize()[3];
106  output->create(width, height, depth, type, 1, Host::getInstance(), data);
107  } else {
108  throw Exception("The ITKImageImporter only supports 2D and 3D images.");
109  }
110 }
111 
112 #endif /* ITKIMAGEIMPORTER_HPP_ */
Image.hpp
fast::DataType
DataType
Definition: DataTypes.hpp:38
fast::TYPE_UINT8
@ TYPE_UINT8
Definition: DataTypes.hpp:40
fast
Definition: AffineTransformation.hpp:7
FAST_OBJECT
#define FAST_OBJECT(className)
Definition: Object.hpp:9
fast::TYPE_FLOAT
@ TYPE_FLOAT
Definition: DataTypes.hpp:39
fast::TYPE_INT8
@ TYPE_INT8
Definition: DataTypes.hpp:41
fast::ITKImageImporter::setInput
void setInput(typename TImage::Pointer image)
Definition: ITKImageImporter.hpp:33
ProcessObject.hpp
fast::TYPE_INT16
@ TYPE_INT16
Definition: DataTypes.hpp:43
fast::ITKImageImporter
Definition: ITKImageImporter.hpp:13
fast::ProcessObject
Definition: ProcessObject.hpp:22
fast::TYPE_UINT16
@ TYPE_UINT16
Definition: DataTypes.hpp:42