Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkRGBToRGBACastImageFilter.h"
00020 #include "mitkImageTimeSelector.h"
00021 #include "mitkProperties.h"
00022 #include "mitkImageAccessByItk.h"
00023 #include "mitkImageToItk.h"
00024
00025 #include <itkImageRegionConstIterator.h>
00026 #include <itkImageRegionIteratorWithIndex.h>
00027 #include <itkImageIOBase.h>
00028 #include <itkRGBAPixel.h>
00029
00030
00031 mitk::RGBToRGBACastImageFilter::RGBToRGBACastImageFilter()
00032 {
00033 this->SetNumberOfInputs(1);
00034 this->SetNumberOfRequiredInputs(1);
00035
00036 m_InputTimeSelector = mitk::ImageTimeSelector::New();
00037 m_OutputTimeSelector = mitk::ImageTimeSelector::New();
00038 }
00039
00040
00041 mitk::RGBToRGBACastImageFilter::~RGBToRGBACastImageFilter()
00042 {
00043 }
00044
00045
00046 bool mitk::RGBToRGBACastImageFilter::IsRGBImage( const mitk::Image *image )
00047 {
00048 const mitk::PixelType &inputPixelType = image->GetPixelType();
00049
00050 if ( (inputPixelType == typeid( UCRGBPixelType) )
00051 || (inputPixelType == typeid( USRGBPixelType) )
00052 || (inputPixelType == typeid( FloatRGBPixelType) )
00053 || (inputPixelType == typeid( DoubleRGBPixelType) ) )
00054 {
00055 return true;
00056 }
00057
00058 return false;
00059 }
00060
00061
00062 void mitk::RGBToRGBACastImageFilter::GenerateInputRequestedRegion()
00063 {
00064 Superclass::GenerateInputRequestedRegion();
00065
00066 mitk::Image* output = this->GetOutput();
00067 mitk::Image* input = const_cast< mitk::Image * > ( this->GetInput() );
00068 if ( !output->IsInitialized() )
00069 {
00070 return;
00071 }
00072
00073 input->SetRequestedRegionToLargestPossibleRegion();
00074
00075
00076 }
00077
00078 void mitk::RGBToRGBACastImageFilter::GenerateOutputInformation()
00079 {
00080 mitk::Image::ConstPointer input = this->GetInput();
00081 mitk::Image::Pointer output = this->GetOutput();
00082
00083 if ((output->IsInitialized()) && (this->GetMTime() <= m_TimeOfHeaderInitialization.GetMTime()))
00084 return;
00085
00086 itkDebugMacro(<<"GenerateOutputInformation()");
00087
00088
00089 const mitk::PixelType &inputPixelType = input->GetPixelType();
00090 if ( inputPixelType == typeid( UCRGBPixelType ) )
00091 {
00092 mitk::PixelType outputPixelType( typeid( unsigned char ), 4, itk::ImageIOBase::RGBA );
00093 output->Initialize( outputPixelType, *input->GetTimeSlicedGeometry() );
00094 }
00095 else if ( inputPixelType == typeid( USRGBPixelType ) )
00096 {
00097 mitk::PixelType outputPixelType( typeid( unsigned short ), 4, itk::ImageIOBase::RGBA );
00098 output->Initialize( outputPixelType, *input->GetTimeSlicedGeometry() );
00099 }
00100 else if ( inputPixelType == typeid( FloatRGBPixelType ) )
00101 {
00102 mitk::PixelType outputPixelType( typeid( float ), 4, itk::ImageIOBase::RGBA );
00103 output->Initialize( outputPixelType, *input->GetTimeSlicedGeometry() );
00104 }
00105 else if ( inputPixelType == typeid( DoubleRGBPixelType ) )
00106 {
00107 mitk::PixelType outputPixelType( typeid( double ), 4, itk::ImageIOBase::RGBA );
00108 output->Initialize( outputPixelType, *input->GetTimeSlicedGeometry() );
00109 }
00110
00111 output->SetPropertyList(input->GetPropertyList()->Clone());
00112
00113 m_TimeOfHeaderInitialization.Modified();
00114 }
00115
00116
00117 void mitk::RGBToRGBACastImageFilter::GenerateData()
00118 {
00119 mitk::Image::ConstPointer input = this->GetInput();
00120 mitk::Image::Pointer output = this->GetOutput();
00121
00122 if( !output->IsInitialized() )
00123 {
00124 return;
00125 }
00126
00127 m_InputTimeSelector->SetInput(input);
00128 m_OutputTimeSelector->SetInput(this->GetOutput());
00129
00130 mitk::Image::RegionType outputRegion = output->GetRequestedRegion();
00131 const mitk::TimeSlicedGeometry *outputTimeGeometry = output->GetTimeSlicedGeometry();
00132 const mitk::TimeSlicedGeometry *inputTimeGeometry = input->GetTimeSlicedGeometry();
00133 ScalarType timeInMS;
00134
00135 int timestep=0;
00136 int tstart=outputRegion.GetIndex(3);
00137 int tmax=tstart+outputRegion.GetSize(3);
00138
00139 int t;
00140 for(t=tstart;t<tmax;++t)
00141 {
00142 timeInMS = outputTimeGeometry->TimeStepToMS( t );
00143
00144 timestep = inputTimeGeometry->MSToTimeStep( timeInMS );
00145
00146 m_InputTimeSelector->SetTimeNr(timestep);
00147 m_InputTimeSelector->UpdateLargestPossibleRegion();
00148 m_OutputTimeSelector->SetTimeNr(t);
00149 m_OutputTimeSelector->UpdateLargestPossibleRegion();
00150
00151 mitk::Image *image = m_InputTimeSelector->GetOutput();
00152
00153 const mitk::PixelType &pixelType = image->GetPixelType();
00154
00155
00156 if ( pixelType == typeid( UCRGBPixelType ) )
00157 {
00158 AccessFixedPixelTypeByItk_2( image, InternalCast, UCRGBPixelType, this, 255 );
00159 }
00160 else if ( pixelType == typeid( USRGBPixelType ) )
00161 {
00162 AccessFixedPixelTypeByItk_2( image, InternalCast, USRGBPixelType, this, 65535 );
00163 }
00164 else if ( pixelType == typeid( FloatRGBPixelType ) )
00165 {
00166 AccessFixedPixelTypeByItk_2( image, InternalCast, FloatRGBPixelType, this, 1.0 );
00167 }
00168 else if ( pixelType == typeid( DoubleRGBPixelType ) )
00169 {
00170 AccessFixedPixelTypeByItk_2( image, InternalCast, DoubleRGBPixelType, this, 1.0 );
00171 }
00172 else
00173 {
00174
00175
00176
00177 }
00178
00179 }
00180
00181 m_TimeOfHeaderInitialization.Modified();
00182 }
00183
00184
00185 template < typename TPixel, unsigned int VImageDimension >
00186 void mitk::RGBToRGBACastImageFilter::InternalCast(
00187 itk::Image< TPixel, VImageDimension > *inputItkImage,
00188 mitk::RGBToRGBACastImageFilter *addComponentFilter,
00189 typename TPixel::ComponentType defaultAlpha )
00190 {
00191 typedef TPixel InputPixelType;
00192 typedef itk::RGBAPixel< typename TPixel::ComponentType > OutputPixelType;
00193 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
00194 typedef itk::Image< OutputPixelType, VImageDimension > OutputImageType;
00195
00196 typedef itk::ImageRegionConstIterator< InputImageType > InputImageIteratorType;
00197 typedef itk::ImageRegionIteratorWithIndex< OutputImageType > OutputImageIteratorType;
00198
00199 typename mitk::ImageToItk< OutputImageType >::Pointer outputimagetoitk =
00200 mitk::ImageToItk< OutputImageType >::New();
00201 outputimagetoitk->SetInput(addComponentFilter->m_OutputTimeSelector->GetOutput());
00202 outputimagetoitk->Update();
00203 typename OutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();
00204
00205
00206 typename InputImageType::RegionType inputRegionOfInterest =
00207 inputItkImage->GetLargestPossibleRegion();
00208 InputImageIteratorType inputIt( inputItkImage, inputRegionOfInterest );
00209 OutputImageIteratorType outputIt( outputItkImage, inputRegionOfInterest );
00210
00211 for ( inputIt.GoToBegin(), outputIt.GoToBegin();
00212 !inputIt.IsAtEnd();
00213 ++inputIt, ++outputIt )
00214 {
00215 typename InputPixelType::Iterator pixelInputIt = inputIt.Get().Begin();
00216 typename OutputPixelType::Iterator pixelOutputIt = outputIt.Get().Begin();
00217
00218 *pixelOutputIt++ = *pixelInputIt++;
00219 *pixelOutputIt++ = *pixelInputIt++;
00220 *pixelOutputIt++ = *pixelInputIt++;
00221 *pixelOutputIt = defaultAlpha;
00222 }
00223 }