DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkRGBImageToVTKImageDataFilter.hxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: RGB Image to VTK Image Data 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 __itkRGBImageToVTKImageDataFilter_hxx
15 #define __itkRGBImageToVTKImageDataFilter_hxx
16 
18 
19 #include "vtkImageData.h"
20 #include "vtkDoubleArray.h"
21 #include "vtkUnsignedCharArray.h"
22 #include "vtkPointData.h"
23 
24 namespace itk
25 {
26 
30 template <class TInputImage>
33 {
34  m_Image = NULL;
35 }
36 
40 template <class TInputImage>
41 vtkImageData *
43 ::GetOutput() const
44 {
45  return m_Image;
46 }
47 
51 template <class TInputImage>
52 void
54 ::SetInput( const InputImageType *image )
55 {
56  // Process object is not const-correct so the const_cast is required here
57  this->ProcessObject::SetNthInput( 0,
58  const_cast< InputImageType * >( image ) );
59 }
60 
64 template <class TInputImage>
67 ::GetInput() const
68 {
69  return itkDynamicCastInDebugMode< const TInputImage * >(this->GetPrimaryInput());
70 }
71 
75 template <class TInputImage>
76 void
79 {
80  // Get input image
81  InputImageConstPointer inputImage = this->GetInput();
82 
83  // Image information
84  InputSizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
85  InputSpacingType inputSpacing = inputImage->GetSpacing();
86  InputPointType inputOrigin = inputImage->GetOrigin();
87  unsigned int numberOfComponentsPerPixel
88  = inputImage->GetNumberOfComponentsPerPixel();
89  unsigned int rgbaSize = 4;
90  unsigned int numberOfComponents
91  = vnl_math_min(rgbaSize, numberOfComponentsPerPixel);
92 
93  // Image data
94  m_Image = OutputImageType::New();
95 
96  int outputSize[Dimension];
97  double outputSpacing[Dimension];
98  double outputOrigin[Dimension];
99  vtkIdType numberOfVoxels = 1;
100 
101  for (unsigned int k=0; k<Dimension; k++)
102  {
103  outputSize[k] = inputSize[k];
104  outputSpacing[k] = inputSpacing[k];
105  outputOrigin[k] = inputOrigin[k];
106  numberOfVoxels *= outputSize[k];
107  }
108 
109  m_Image->SetDimensions(outputSize);
110  m_Image->SetSpacing(outputSpacing);
111  m_Image->SetOrigin(outputOrigin);
112 
113  // Data array
114  vtkSmartPointer<vtkUnsignedCharArray> data =
115  vtkSmartPointer<vtkUnsignedCharArray>::New();
116 
117  data->SetNumberOfComponents(numberOfComponents);
118  data->SetNumberOfTuples(numberOfVoxels);
119  data->SetName("RGB_Vectors");
120 
121  // Iterator
123  inputIt(inputImage, inputImage->GetLargestPossibleRegion());
124 
125  inputIt.GoToBegin();
126 
127  // Populate data
128  InputPixelType inputPixel;
129  vtkIdType i = 0;
130  double dataEntry [numberOfComponents];
131 
132  while(!inputIt.IsAtEnd())
133  {
134  inputPixel = inputIt.Get();
135  for (unsigned int k = 0; k < rgbaSize - 1; k++)
136  {
137  if ( k < numberOfComponents )
138  {
139  dataEntry[k] = static_cast<unsigned char>( inputPixel[k] );
140  }
141  else
142  {
143  dataEntry[k] = static_cast<unsigned char>( 0 );
144  }
145  }
146  if ( numberOfComponents == rgbaSize )
147  {
148  dataEntry[rgbaSize - 1]
149  = static_cast<unsigned char>( inputPixel[rgbaSize - 1] );
150  }
151  else
152  {
153  dataEntry[rgbaSize - 1] = 1.0;
154  }
155  data->SetTuple(i, dataEntry);
156  ++inputIt;
157  i++;
158  }
159 
160  m_Image->GetPointData()->AddArray(data);
161  m_Image->GetPointData()->SetActiveScalars(data->GetName());
162 }
163 
164 } // end namespace itk
165 
166 #endif
ImageRegionConstIterator< InputImageType > InputImageIteratorType
InputImageType::ConstPointer InputImageConstPointer
virtual void SetInput(const InputImageType *image)