Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkToFImageRecorderFilter.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 <mitkImage.h>
19 #include <mitkImageDataItem.h>
20 #include "mitkToFNrrdImageWriter.h"
21 #include "mitkToFImageCsvWriter.h"
22 #include "mitkImageReadAccessor.h"
23 
24 // itk includes
25 #include "itksys/SystemTools.hxx"
26 
27 
28 mitk::ToFImageRecorderFilter::ToFImageRecorderFilter(): m_RecordingStarted(false), m_ToFImageWriter(0)
29 {
30  m_FileExtension = "";
31 }
32 
34 {
35 }
36 
38 {
39  std::string name = fileName;
40  m_FileExtension = itksys::SystemTools::GetFilenameLastExtension( fileName );
41  if(m_FileExtension == ".nrrd")
42  {
43  m_ToFImageWriter = mitk::ToFNrrdImageWriter::New();
44  }
45  else if(m_FileExtension == ".csv")
46  {
47  m_ToFImageWriter = mitk::ToFImageCsvWriter::New();
48  }
49  else
50  {
51  throw std::logic_error("The specified file type is not supported, standard file type is .nrrd!");
52  }
53  int pos = name.find_last_of(".");
54  name.insert(pos,"_DistanceImage");
55  m_ToFImageWriter->SetDistanceImageFileName(name);
56  name = fileName;
57  name.insert(pos,"_AmplitudeImage");
58  m_ToFImageWriter->SetAmplitudeImageFileName(name);
59  name = fileName;
60  name.insert(pos,"_IntensityImage");
61  m_ToFImageWriter->SetIntensityImageFileName(name);
62 }
63 
65 {
66  m_ToFImageWriter->SetToFImageType(tofImageType);
67 }
68 
70 {
71  mitk::Image::Pointer distanceImageInput = this->GetInput(0);
72  assert(distanceImageInput);
73  mitk::Image::Pointer amplitudeImageInput = this->GetInput(1);
74  assert(amplitudeImageInput);
75  mitk::Image::Pointer intensityImageInput = this->GetInput(2);
76  assert(intensityImageInput);
77  // add current data to file stream
78  ImageReadAccessor distAcc(distanceImageInput, distanceImageInput->GetSliceData(0,0,0));
79  ImageReadAccessor amplAcc(amplitudeImageInput, amplitudeImageInput->GetSliceData(0,0,0));
80  ImageReadAccessor intenAcc(intensityImageInput, intensityImageInput->GetSliceData(0,0,0));
81 
82  float* distanceFloatData = (float*) distAcc.GetData();
83  float* amplitudeFloatData = (float*) amplAcc.GetData();
84  float* intensityFloatData = (float*) intenAcc.GetData();
85  if (m_RecordingStarted)
86  {
87  m_ToFImageWriter->Add(distanceFloatData,amplitudeFloatData,intensityFloatData);
88  }
89 
90  // set outputs to inputs
91  this->SetNthOutput(0,distanceImageInput);
92  this->SetNthOutput(1,amplitudeImageInput);
93  this->SetNthOutput(2,intensityImageInput);
94 }
95 
97 {
98  if(m_ToFImageWriter.IsNull())
99  {
100  throw std::logic_error("ToFImageWriter is unitialized, set filename first!");
101  return;
102  }
103  m_ToFImageWriter->Open();
104  m_RecordingStarted = true;
105 }
106 
108 {
109  m_ToFImageWriter->Close();
110  m_RecordingStarted = false;
111 }
112 
114 {
115  return m_ToFImageWriter;
116 }
117 
119 {
120  m_ToFImageWriter = tofImageWriter;
121 }
122 
124 {
125  this->SetInput(0,input);
126 }
127 
128 void mitk::ToFImageRecorderFilter::SetInput( unsigned int idx, mitk::Image* input )
129 {
130  if ((input == NULL) && (idx == this->GetNumberOfInputs() - 1)) // if the last input is set to NULL, reduce the number of inputs by one
131  {
132  this->SetNumberOfInputs(this->GetNumberOfInputs() - 1);
133  }
134  else if(idx == 0 || idx == 1 || idx == 2)
135  {
136  this->ProcessObject::SetNthInput(idx, input); // Process object is not const-correct so the const_cast is required here
137  unsigned int xDim = input->GetDimension(0);
138  unsigned int yDim = input->GetDimension(1);
139  m_ToFImageWriter->SetToFCaptureWidth(xDim);
140  m_ToFImageWriter->SetToFCaptureWidth(yDim);
141  }
142 
143  this->CreateOutputsForAllInputs();
144 }
145 
147 {
148  return this->GetInput(0);
149 }
150 
152 {
153  if (this->GetNumberOfInputs() < 1)
154  return NULL;
155  return static_cast< mitk::Image*>(this->ProcessObject::GetInput(idx));
156 }
157 
159 {
160  this->SetNumberOfOutputs(this->GetNumberOfInputs()); // create outputs for all inputs
161  for (unsigned int idx = 0; idx < this->GetNumberOfOutputs(); ++idx)
162  if (this->GetOutput(idx) == NULL)
163  {
164  DataObjectPointer newOutput = this->MakeOutput(idx);
165  this->SetNthOutput(idx, newOutput);
166  }
167  this->Modified();
168 }
void SetImageType(ToFImageWriter::ToFImageType tofImageType)
Set image type for recording.
ToFImageRecorderFilter()
standard constructor
itk::SmartPointer< Self > Pointer
void StartRecording()
start recording of data
static Pointer New()
const void * GetData() const
Gives const access to the data.
void CreateOutputsForAllInputs()
Create an output for each input.
static Pointer New()
void StopRecording()
stop recording of data
virtual void GenerateData() override
method generating the output of this filter. Called in the updated process of the pipeline...
void SetFileName(std::string fileName)
Set file name for writing image files This filename will be appended by "_DistanceImage", "_AmplitudeImage", or "_IntensityImage" for the single images.
Image class for storing images.
Definition: mitkImage.h:76
void SetToFImageWriter(ToFImageWriter::Pointer tofImageWriter)
Sets a pointer to the ToFImageWriter internally used.
ToFImageWriter::Pointer GetToFImageWriter()
Returns a pointer to the ToFImageWriter internally used.
unsigned int GetDimension() const
Get dimension of the image.
Definition: mitkImage.cpp:110
ImageReadAccessor class to get locked read access for a particular image part.
Image * GetInput()
returns the input of this filter
virtual void SetInput(Image *input)
sets the input of this filter