Medical Imaging Interaction Toolkit  2018.4.99-07c45cb1
Medical Imaging Interaction Toolkit
mitkRGBToRGBACastImageFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 #include "mitkImageAccessByItk.h"
15 #include "mitkImageTimeSelector.h"
16 #include "mitkImageToItk.h"
17 #include "mitkProperties.h"
18 
19 #include <itkImageIOBase.h>
20 #include <itkImageRegionConstIterator.h>
21 #include <itkImageRegionIteratorWithIndex.h>
22 #include <itkRGBAPixel.h>
23 
25 {
26  this->SetNumberOfIndexedInputs(1);
27  this->SetNumberOfRequiredInputs(1);
28 
31 }
32 
34 {
35 }
36 
38 {
39  const mitk::PixelType &inputPixelType = image->GetPixelType();
40 
41  if ((inputPixelType.GetPixelType() == itk::ImageIOBase::RGB) &&
42  ((inputPixelType.GetComponentType() == itk::ImageIOBase::UCHAR) ||
43  (inputPixelType.GetComponentType() == itk::ImageIOBase::USHORT) ||
44  (inputPixelType.GetComponentType() == itk::ImageIOBase::FLOAT) ||
45  (inputPixelType.GetComponentType() == itk::ImageIOBase::DOUBLE)))
46  {
47  return true;
48  }
49 
50  return false;
51 }
52 
54 {
56 
57  mitk::Image *output = this->GetOutput();
58  mitk::Image *input = this->GetInput();
59  if (!output->IsInitialized())
60  {
61  return;
62  }
63 
65 
66  // GenerateTimeInInputRegion(output, input);
67 }
68 
70 {
71  mitk::Image::ConstPointer input = this->GetInput();
72  mitk::Image::Pointer output = this->GetOutput();
73 
74  if ((output->IsInitialized()) && (this->GetMTime() <= m_TimeOfHeaderInitialization.GetMTime()))
75  return;
76 
77  itkDebugMacro(<< "GenerateOutputInformation()");
78 
79  // Initialize RGBA output with same pixel type as input image
80  const mitk::PixelType &inputPixelType = input->GetPixelType();
81 
82  typedef itk::Image<UCRGBPixelType> UCRGBItkImageType;
83  typedef itk::Image<USRGBPixelType> USRGBItkImageType;
84  typedef itk::Image<FloatRGBPixelType> FloatCRGBItkImageType;
85  typedef itk::Image<DoubleRGBPixelType> DoubleRGBItkImageType;
86 
87  if (inputPixelType == mitk::MakePixelType<UCRGBItkImageType>())
88  {
89  const mitk::PixelType refPtype = MakePixelType<UCRGBItkImageType>();
90  output->Initialize(refPtype, *input->GetTimeGeometry());
91  }
92  else if (inputPixelType == mitk::MakePixelType<USRGBItkImageType>())
93  {
94  const mitk::PixelType refPtype = MakePixelType<USRGBItkImageType>();
95  output->Initialize(refPtype, *input->GetTimeGeometry());
96  }
97  else if (inputPixelType == mitk::MakePixelType<FloatCRGBItkImageType>())
98  {
99  const mitk::PixelType refPtype = MakePixelType<FloatCRGBItkImageType>();
100  output->Initialize(refPtype, *input->GetTimeGeometry());
101  }
102  else if (inputPixelType == mitk::MakePixelType<DoubleRGBItkImageType>())
103  {
104  const mitk::PixelType refPtype = MakePixelType<DoubleRGBItkImageType>();
105  output->Initialize(refPtype, *input->GetTimeGeometry());
106  }
107 
108  output->SetPropertyList(input->GetPropertyList()->Clone());
109 
110  m_TimeOfHeaderInitialization.Modified();
111 }
112 
114 {
115  mitk::Image::ConstPointer input = this->GetInput();
116  mitk::Image::Pointer output = this->GetOutput();
117 
118  if (!output->IsInitialized())
119  {
120  return;
121  }
122 
123  m_InputTimeSelector->SetInput(input);
124  m_OutputTimeSelector->SetInput(this->GetOutput());
125 
126  mitk::Image::RegionType outputRegion = output->GetRequestedRegion();
127  const mitk::TimeGeometry *outputTimeGeometry = output->GetTimeGeometry();
128  const mitk::TimeGeometry *inputTimeGeometry = input->GetTimeGeometry();
129  TimePointType timeInMS;
130 
131  int timestep = 0;
132  int tstart = outputRegion.GetIndex(3);
133  int tmax = tstart + outputRegion.GetSize(3);
134 
135  int t;
136  for (t = tstart; t < tmax; ++t)
137  {
138  timeInMS = outputTimeGeometry->TimeStepToTimePoint(t);
139  timestep = inputTimeGeometry->TimePointToTimeStep(timeInMS);
140 
141  m_InputTimeSelector->SetTimeNr(timestep);
142  m_InputTimeSelector->UpdateLargestPossibleRegion();
143  m_OutputTimeSelector->SetTimeNr(t);
144  m_OutputTimeSelector->UpdateLargestPossibleRegion();
145 
146  mitk::Image *image = m_InputTimeSelector->GetOutput();
147 
148  const mitk::PixelType &pixelType = image->GetPixelType();
149 
150  // Check if the pixel type is supported
151  if (pixelType == MakePixelType<itk::Image<UCRGBPixelType>>())
152  {
154  }
155  else if (pixelType == MakePixelType<itk::Image<USRGBPixelType>>())
156  {
158  }
159  else if (pixelType == MakePixelType<itk::Image<FloatRGBPixelType>>())
160  {
162  }
163  else if (pixelType == MakePixelType<itk::Image<DoubleRGBPixelType>>())
164  {
166  }
167  else
168  {
169  // Otherwise, write warning and graft input to output
170 
171  // ...TBD...
172  }
173  }
174 
175  m_TimeOfHeaderInitialization.Modified();
176 }
177 
178 template <typename TPixel, unsigned int VImageDimension>
179 void mitk::RGBToRGBACastImageFilter::InternalCast(itk::Image<TPixel, VImageDimension> *inputItkImage,
180  mitk::RGBToRGBACastImageFilter *addComponentFilter,
181  typename TPixel::ComponentType defaultAlpha)
182 {
183  typedef TPixel InputPixelType;
184  typedef itk::RGBAPixel<typename TPixel::ComponentType> OutputPixelType;
185  typedef itk::Image<InputPixelType, VImageDimension> InputImageType;
186  typedef itk::Image<OutputPixelType, VImageDimension> OutputImageType;
187 
188  typedef itk::ImageRegionConstIterator<InputImageType> InputImageIteratorType;
189  typedef itk::ImageRegionIteratorWithIndex<OutputImageType> OutputImageIteratorType;
190 
192  outputimagetoitk->SetInput(addComponentFilter->m_OutputTimeSelector->GetOutput());
193  outputimagetoitk->Update();
194  typename OutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();
195 
196  // create the iterators
197  typename InputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
198  InputImageIteratorType inputIt(inputItkImage, inputRegionOfInterest);
199  OutputImageIteratorType outputIt(outputItkImage, inputRegionOfInterest);
200 
201  for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
202  {
203  typename InputPixelType::Iterator pixelInputIt = inputIt.Get().Begin();
204  typename OutputPixelType::Iterator pixelOutputIt = outputIt.Get().Begin();
205 
206  *pixelOutputIt++ = *pixelInputIt++;
207  *pixelOutputIt++ = *pixelInputIt++;
208  *pixelOutputIt++ = *pixelInputIt++;
209  *pixelOutputIt = defaultAlpha;
210  }
211 }
void SetRequestedRegionToLargestPossibleRegion() override
itk::RGBPixel< unsigned char > UCRGBPixelType
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:101
mitk::ImageTimeSelector::Pointer m_InputTimeSelector
mitk::ImageTimeSelector::Pointer m_OutputTimeSelector
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
itk::RGBPixel< unsigned short > USRGBPixelType
itk::ImageRegion< RegionDimension > RegionType
virtual TimeStepType TimePointToTimeStep(TimePointType timePoint) const =0
Converts a time point to the corresponding time step.
Image class for storing images.
Definition: mitkImage.h:72
virtual TimePointType TimeStepToTimePoint(TimeStepType timeStep) const =0
Converts a time step to a time point.
mitk::Image OutputImageType
Some convenient typedefs.
static bool IsRGBImage(const mitk::Image *image)
mitk::ScalarType TimePointType
itk::ImageIOBase::IOPixelType GetPixelType() const
mitk::Image::Pointer image
static Pointer New()
#define AccessFixedPixelTypeByItk_2(mitkImage, itkImageTypeFunction, pixelTypeSeq, arg1, arg2)
InputImageType * GetInput(void)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
OutputType * GetOutput()
Get the output data of this image source object.
void GenerateInputRequestedRegion() override
void InternalCast(itk::Image< TPixel, VImageDimension > *itkImage, mitk::RGBToRGBACastImageFilter *addComponentFilter, typename TPixel::ComponentType defaultAlpha)
static Pointer New()
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51