DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkVTKImageDataToImageFilter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: VTK Image Data to Image Converter
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 #ifndef __itkVTKImageDataToImageFilter_hxx
15 #define __itkVTKImageDataToImageFilter_hxx
16 
18 
19 #include "vtkDoubleArray.h"
20 #include "vtkPointData.h"
21 #include "vtkVersion.h"
22 
23 namespace itk
24 {
25 
29 template <class TOutputImage>
32 {
33  m_Image = NULL;
34 }
35 
39 template <class TOutputImage>
40 void
42 ::SetInputData( vtkImageData * inputImage )
43 {
44  if ( inputImage != m_Image )
45  {
46  m_Image = inputImage;
47 // m_Image->Register(this);
48  this->Modified();
49  }
50 }
51 
55 template <class TOutputImage>
56 void
59 {
60  // Data array
61  vtkSmartPointer<vtkDoubleArray> data
62  = vtkSmartPointer<vtkDoubleArray>::New();
63  data = static_cast<vtkDoubleArray *>(
64  m_Image->GetPointData()->GetScalars() );
65 
66  // Image information
67  int inputSize[Dimension];
68  double inputSpacing[Dimension];
69  double inputOrigin[Dimension];
70  m_Image->GetDimensions(inputSize);
71  m_Image->GetSpacing(inputSpacing);
72  m_Image->GetOrigin(inputOrigin);
73  int numberOfComponentsPerPixel
74  = m_Image->GetPointData()->GetNumberOfComponents();
75 
76  OutputImagePointer outputImage = this->GetOutput();
77 
78  // Setup output image
79  OutputSizeType outputSize;
80  OutputSpacingType outputSpacing;
81  OutputPointType outputOrigin;
82  OutputIndexType outputIndex;
83  OutputPixelType outputPixel;
84  outputPixel.SetSize( numberOfComponentsPerPixel );
85 
86  for (unsigned int k=0; k<Dimension; k++)
87  {
88  outputSize[k] = inputSize[k];
89  outputSpacing[k] = inputSpacing[k];
90  outputOrigin[k] = inputOrigin[k];
91  outputIndex[k] = 0;
92  }
93 
94  OutputRegionType outputRegion;
95  outputRegion.SetSize(outputSize);
96  outputRegion.SetIndex(outputIndex);
97 
98  outputImage->SetSpacing(outputSpacing);
99  outputImage->SetRegions(outputRegion);
100  outputImage->SetOrigin(outputOrigin);
101  outputImage->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel);
102 
103  outputImage->Allocate();
104  outputPixel.Fill(0);
105  outputImage->FillBuffer(outputPixel);
106 
107  // Iterator
109  outputIt(outputImage, outputImage->GetLargestPossibleRegion());
110 
111  outputIt.GoToBegin();
112 
113  // Populate data
114  vtkIdType i = 0;
115  double *dataEntry;
116 
117  while( !outputIt.IsAtEnd() )
118  {
119  dataEntry = data->GetTuple(i);
120  for (unsigned int k = 0; k < numberOfComponentsPerPixel; k++)
121  {
122  outputPixel[k] = dataEntry[k];
123  }
124  outputIt.Set( outputPixel );
125  ++outputIt;
126  i++;
127  }
128 }
129 
130 } // end namespace itk
131 
132 #endif
ImageRegionIterator< OutputImageType > OutputImageIteratorType
OutputImageType::RegionType OutputRegionType
OutputImageType::SpacingType OutputSpacingType
OutputImageType::PixelType OutputPixelType
OutputImageType::IndexType OutputIndexType
OutputImageType::PointType OutputPointType