Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include "mitkImageAccessByItk.h"
19 #include "mitkImageTimeSelector.h"
20 #include "mitkImageToItk.h"
21 #include "mitkProperties.h"
22 
23 #include <itkImageIOBase.h>
24 #include <itkImageRegionConstIterator.h>
25 #include <itkImageRegionIteratorWithIndex.h>
26 #include <itkRGBAPixel.h>
27 
29 {
30  this->SetNumberOfIndexedInputs(1);
31  this->SetNumberOfRequiredInputs(1);
32 
35 }
36 
38 {
39 }
40 
42 {
43  const mitk::PixelType &inputPixelType = image->GetPixelType();
44 
45  if ((inputPixelType.GetPixelType() == itk::ImageIOBase::RGB) &&
46  ((inputPixelType.GetComponentType() == itk::ImageIOBase::UCHAR) ||
47  (inputPixelType.GetComponentType() == itk::ImageIOBase::USHORT) ||
48  (inputPixelType.GetComponentType() == itk::ImageIOBase::FLOAT) ||
49  (inputPixelType.GetComponentType() == itk::ImageIOBase::DOUBLE)))
50  {
51  return true;
52  }
53 
54  return false;
55 }
56 
58 {
59  Superclass::GenerateInputRequestedRegion();
60 
61  mitk::Image *output = this->GetOutput();
62  mitk::Image *input = const_cast<mitk::Image *>(this->GetInput());
63  if (!output->IsInitialized())
64  {
65  return;
66  }
67 
69 
70  // GenerateTimeInInputRegion(output, input);
71 }
72 
74 {
75  mitk::Image::ConstPointer input = this->GetInput();
76  mitk::Image::Pointer output = this->GetOutput();
77 
78  if ((output->IsInitialized()) && (this->GetMTime() <= m_TimeOfHeaderInitialization.GetMTime()))
79  return;
80 
81  itkDebugMacro(<< "GenerateOutputInformation()");
82 
83  // Initialize RGBA output with same pixel type as input image
84  const mitk::PixelType &inputPixelType = input->GetPixelType();
85 
86  typedef itk::Image<UCRGBPixelType> UCRGBItkImageType;
87  typedef itk::Image<USRGBPixelType> USRGBItkImageType;
88  typedef itk::Image<FloatRGBPixelType> FloatCRGBItkImageType;
89  typedef itk::Image<DoubleRGBPixelType> DoubleRGBItkImageType;
90 
91  if (inputPixelType == mitk::MakePixelType<UCRGBItkImageType>())
92  {
93  const mitk::PixelType refPtype = MakePixelType<UCRGBItkImageType>();
94  output->Initialize(refPtype, *input->GetTimeGeometry());
95  }
96  else if (inputPixelType == mitk::MakePixelType<USRGBItkImageType>())
97  {
98  const mitk::PixelType refPtype = MakePixelType<USRGBItkImageType>();
99  output->Initialize(refPtype, *input->GetTimeGeometry());
100  }
101  else if (inputPixelType == mitk::MakePixelType<FloatCRGBItkImageType>())
102  {
103  const mitk::PixelType refPtype = MakePixelType<FloatCRGBItkImageType>();
104  output->Initialize(refPtype, *input->GetTimeGeometry());
105  }
106  else if (inputPixelType == mitk::MakePixelType<DoubleRGBItkImageType>())
107  {
108  const mitk::PixelType refPtype = MakePixelType<DoubleRGBItkImageType>();
109  output->Initialize(refPtype, *input->GetTimeGeometry());
110  }
111 
112  output->SetPropertyList(input->GetPropertyList()->Clone());
113 
114  m_TimeOfHeaderInitialization.Modified();
115 }
116 
118 {
119  mitk::Image::ConstPointer input = this->GetInput();
120  mitk::Image::Pointer output = this->GetOutput();
121 
122  if (!output->IsInitialized())
123  {
124  return;
125  }
126 
127  m_InputTimeSelector->SetInput(input);
128  m_OutputTimeSelector->SetInput(this->GetOutput());
129 
130  mitk::Image::RegionType outputRegion = output->GetRequestedRegion();
131  const mitk::TimeGeometry *outputTimeGeometry = output->GetTimeGeometry();
132  const mitk::TimeGeometry *inputTimeGeometry = input->GetTimeGeometry();
133  TimePointType timeInMS;
134 
135  int timestep = 0;
136  int tstart = outputRegion.GetIndex(3);
137  int tmax = tstart + outputRegion.GetSize(3);
138 
139  int t;
140  for (t = tstart; t < tmax; ++t)
141  {
142  timeInMS = outputTimeGeometry->TimeStepToTimePoint(t);
143  timestep = inputTimeGeometry->TimePointToTimeStep(timeInMS);
144 
145  m_InputTimeSelector->SetTimeNr(timestep);
146  m_InputTimeSelector->UpdateLargestPossibleRegion();
147  m_OutputTimeSelector->SetTimeNr(t);
148  m_OutputTimeSelector->UpdateLargestPossibleRegion();
149 
150  mitk::Image *image = m_InputTimeSelector->GetOutput();
151 
152  const mitk::PixelType &pixelType = image->GetPixelType();
153 
154  // Check if the pixel type is supported
155  if (pixelType == MakePixelType<itk::Image<UCRGBPixelType>>())
156  {
157  AccessFixedPixelTypeByItk_2(image, InternalCast, (UCRGBPixelType), this, 255);
158  }
159  else if (pixelType == MakePixelType<itk::Image<USRGBPixelType>>())
160  {
161  AccessFixedPixelTypeByItk_2(image, InternalCast, (USRGBPixelType), this, 65535);
162  }
163  else if (pixelType == MakePixelType<itk::Image<FloatRGBPixelType>>())
164  {
165  AccessFixedPixelTypeByItk_2(image, InternalCast, (FloatRGBPixelType), this, 1.0);
166  }
167  else if (pixelType == MakePixelType<itk::Image<DoubleRGBPixelType>>())
168  {
169  AccessFixedPixelTypeByItk_2(image, InternalCast, (DoubleRGBPixelType), this, 1.0);
170  }
171  else
172  {
173  // Otherwise, write warning and graft input to output
174 
175  // ...TBD...
176  }
177  }
178 
179  m_TimeOfHeaderInitialization.Modified();
180 }
181 
182 template <typename TPixel, unsigned int VImageDimension>
183 void mitk::RGBToRGBACastImageFilter::InternalCast(itk::Image<TPixel, VImageDimension> *inputItkImage,
184  mitk::RGBToRGBACastImageFilter *addComponentFilter,
185  typename TPixel::ComponentType defaultAlpha)
186 {
187  typedef TPixel InputPixelType;
188  typedef itk::RGBAPixel<typename TPixel::ComponentType> OutputPixelType;
189  typedef itk::Image<InputPixelType, VImageDimension> InputImageType;
190  typedef itk::Image<OutputPixelType, VImageDimension> OutputImageType;
191 
192  typedef itk::ImageRegionConstIterator<InputImageType> InputImageIteratorType;
193  typedef itk::ImageRegionIteratorWithIndex<OutputImageType> OutputImageIteratorType;
194 
196  outputimagetoitk->SetInput(addComponentFilter->m_OutputTimeSelector->GetOutput());
197  outputimagetoitk->Update();
198  typename OutputImageType::Pointer outputItkImage = outputimagetoitk->GetOutput();
199 
200  // create the iterators
201  typename InputImageType::RegionType inputRegionOfInterest = inputItkImage->GetLargestPossibleRegion();
202  InputImageIteratorType inputIt(inputItkImage, inputRegionOfInterest);
203  OutputImageIteratorType outputIt(outputItkImage, inputRegionOfInterest);
204 
205  for (inputIt.GoToBegin(), outputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt, ++outputIt)
206  {
207  typename InputPixelType::Iterator pixelInputIt = inputIt.Get().Begin();
208  typename OutputPixelType::Iterator pixelOutputIt = outputIt.Get().Begin();
209 
210  *pixelOutputIt++ = *pixelInputIt++;
211  *pixelOutputIt++ = *pixelInputIt++;
212  *pixelOutputIt++ = *pixelInputIt++;
213  *pixelOutputIt = defaultAlpha;
214  }
215 }
virtual void SetRequestedRegionToLargestPossibleRegion() override
itk::RGBPixel< unsigned char > UCRGBPixelType
virtual void GenerateData() override
A version of GenerateData() specific for image processing filters.
mitk::ImageTimeSelector::Pointer m_InputTimeSelector
itk::ImageIOBase::IOPixelType GetPixelType() const
mitk::ImageTimeSelector::Pointer m_OutputTimeSelector
virtual void GenerateInputRequestedRegion() override
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
itk::RGBPixel< unsigned short > USRGBPixelType
itk::Image< double, 3 > InputImageType
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:76
virtual TimePointType TimeStepToTimePoint(TimeStepType timeStep) const =0
Converts a time step to a time point.
static bool IsRGBImage(const mitk::Image *image)
mitk::ScalarType TimePointType
int GetComponentType() const
Get the component type (the scalar (!) type). Each element may contain m_NumberOfComponents (more tha...
const mitk::PixelType GetPixelType(int n=0) const
Returns the PixelType of channel n.
Definition: mitkImage.cpp:105
static Pointer New()
#define AccessFixedPixelTypeByItk_2(mitkImage, itkImageTypeFunction, pixelTypeSeq, arg1, arg2)
virtual bool IsInitialized() const
Check whether the data has been initialized, i.e., at least the Geometry and other header data has be...
double InputPixelType
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:55