1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkImageToRGBVTKImageFilter_hxx
#define itkImageToRGBVTKImageFilter_hxx
#include "vtkVersion.h"
#include "itkImageToRGBVTKImageFilter.h"
namespace itk
{
/**
* Constructor
*/
template< typename TInputImage >
ImageToRGBVTKImageFilter< TInputImage >
::ImageToRGBVTKImageFilter()
{
m_Output = vtkSmartPointer< vtkImageData >::New();
}
/**
* Destructor
*/
template< typename TInputImage >
ImageToRGBVTKImageFilter< TInputImage >
::~ImageToRGBVTKImageFilter()
{}
/**
* Set an itk::Image as input
*/
template< typename TInputImage >
void
ImageToRGBVTKImageFilter< TInputImage >
::SetInput(const InputImageType *inputImage)
{
m_Input = inputImage;
this->Modified();
}
/**
* Get a vtkImage as output
*/
template< typename TInputImage >
vtkSmartPointer< vtkImageData >
ImageToRGBVTKImageFilter< TInputImage >
::GetOutput() const
{
return m_Output;
}
/**
* Delegate the Update to the importer
*/
template< typename TInputImage >
void
ImageToRGBVTKImageFilter< TInputImage >
::Update()
{
int dimension[3];
dimension[0] = 0;
dimension[1] = 0;
dimension[2] = 0;
double spacing[3];
spacing[0] = 0.;
spacing[1] = 0.;
spacing[2] = 0.;
InputRegionType region = m_Input->GetLargestPossibleRegion();
InputSizeType itk_size = region.GetSize();
InputSpacingType itk_spacing = m_Input->GetSpacing();
for( unsigned int i = 0; i < TInputImage::ImageDimension; i++ )
{
dimension[i] = static_cast< int >( itk_size[i] );
spacing[i] = static_cast< double >( itk_spacing[i] );
}
m_Output->SetDimensions( dimension );
m_Output->SetSpacing( spacing );
m_Output->SetExtent( 0, dimension[0],
0, dimension[1],
0, dimension[2] );
#if VTK_MAJOR_VERSION <= 5
m_Output->SetNumberOfScalarComponents( 3 );
// at first let's convert it to unsigned char
m_Output->SetScalarTypeToUnsignedChar();
m_Output->AllocateScalars();
#else
m_Output->AllocateScalars(VTK_UNSIGNED_CHAR,3);
#endif
//TODO: use itk iterators instead
for( int x = 0; x < dimension[0]; x++ )
{
for( int y = 0; y < dimension[1]; y++ )
{
if( TInputImage::ImageDimension == 3 )
{
for( int z = 0; z < dimension[2]; z++ )
{
InputPixelType* vtkpixel =
static_cast<InputPixelType*>(m_Output->GetScalarPointer(x,y,z));
InputIndexType index;
index[0] = x;
index[1] = y;
index[2] = z;
InputPixelType itkpixel = m_Input->GetPixel(index);
vtkpixel[0] = itkpixel;
vtkpixel[1] = itkpixel;
vtkpixel[2] = itkpixel;
}
}
else
{
InputPixelType* vtkpixel =
static_cast<InputPixelType*>(m_Output->GetScalarPointer(x,y,0));
InputIndexType index;
index[0] = x;
index[1] = y;
InputPixelType itkpixel = m_Input->GetPixel(index);
vtkpixel[0] = itkpixel;
vtkpixel[1] = itkpixel;
vtkpixel[2] = itkpixel;
}
}
}
}
} // end namespace itk
#endif
|