14 #ifndef __itkRGBImageToVTKImageDataFilter_hxx 15 #define __itkRGBImageToVTKImageDataFilter_hxx 19 #include "vtkImageData.h" 20 #include "vtkDoubleArray.h" 21 #include "vtkUnsignedCharArray.h" 22 #include "vtkPointData.h" 30 template <
class TInputImage>
40 template <
class TInputImage>
51 template <
class TInputImage>
57 this->ProcessObject::SetNthInput( 0,
58 const_cast< InputImageType * >( image ) );
64 template <
class TInputImage>
69 return itkDynamicCastInDebugMode< const TInputImage * >(this->GetPrimaryInput());
75 template <
class TInputImage>
84 InputSizeType inputSize = inputImage->GetLargestPossibleRegion().GetSize();
87 unsigned int numberOfComponentsPerPixel
88 = inputImage->GetNumberOfComponentsPerPixel();
89 unsigned int rgbaSize = 4;
90 unsigned int numberOfComponents
91 = vnl_math_min(rgbaSize, numberOfComponentsPerPixel);
94 m_Image = OutputImageType::New();
96 int outputSize[Dimension];
97 double outputSpacing[Dimension];
98 double outputOrigin[Dimension];
99 vtkIdType numberOfVoxels = 1;
101 for (
unsigned int k=0; k<Dimension; k++)
103 outputSize[k] = inputSize[k];
104 outputSpacing[k] = inputSpacing[k];
105 outputOrigin[k] = inputOrigin[k];
106 numberOfVoxels *= outputSize[k];
109 m_Image->SetDimensions(outputSize);
110 m_Image->SetSpacing(outputSpacing);
111 m_Image->SetOrigin(outputOrigin);
114 vtkSmartPointer<vtkUnsignedCharArray> data =
115 vtkSmartPointer<vtkUnsignedCharArray>::New();
117 data->SetNumberOfComponents(numberOfComponents);
118 data->SetNumberOfTuples(numberOfVoxels);
119 data->SetName(
"RGB_Vectors");
123 inputIt(inputImage, inputImage->GetLargestPossibleRegion());
130 double dataEntry [numberOfComponents];
132 while(!inputIt.IsAtEnd())
134 inputPixel = inputIt.Get();
135 for (
unsigned int k = 0; k < rgbaSize - 1; k++)
137 if ( k < numberOfComponents )
139 dataEntry[k] =
static_cast<unsigned char>( inputPixel[k] );
143 dataEntry[k] =
static_cast<unsigned char>( 0 );
146 if ( numberOfComponents == rgbaSize )
148 dataEntry[rgbaSize - 1]
149 =
static_cast<unsigned char>( inputPixel[rgbaSize - 1] );
153 dataEntry[rgbaSize - 1] = 1.0;
155 data->SetTuple(i, dataEntry);
160 m_Image->GetPointData()->AddArray(data);
161 m_Image->GetPointData()->SetActiveScalars(data->GetName());
const InputImageType * GetInput() const
ImageRegionConstIterator< InputImageType > InputImageIteratorType
InputImageType::SizeType InputSizeType
InputImageType::PointType InputPointType
vtkImageData * GetOutput() const
InputImageType::PixelType InputPixelType
virtual void GenerateData() ITK_OVERRIDE
InputImageType::ConstPointer InputImageConstPointer
RGBImageToVTKImageDataFilter()
virtual void SetInput(const InputImageType *image)
TInputImage InputImageType
InputImageType::SpacingType InputSpacingType