Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkToFImageCsvWriter.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 #include <mitkToFImageCsvWriter.h>
17 
18 namespace mitk
19 {
21  m_AmplitudeOutfile(NULL), m_IntensityOutfile(NULL)
22  {
23  this->m_Extension = std::string(".csv");
24  }
25 
27  {
28  }
29 
31  {
35 
37  this->m_ToFImageSizeInBytes = this->m_ToFPixelNumber * sizeof(float);
38 
39  if (this->m_DistanceImageSelected)
40  {
41  this->OpenCsvFile(&(this->m_DistanceOutfile), this->m_DistanceImageFileName);
42  }
43  if (this->m_AmplitudeImageSelected)
44  {
45  this->OpenCsvFile(&(this->m_AmplitudeOutfile), this->m_AmplitudeImageFileName);
46  }
47  if (this->m_IntensityImageSelected)
48  {
49  this->OpenCsvFile(&(this->m_IntensityOutfile), this->m_IntensityImageFileName);
50  }
51  this->m_NumOfFrames = 0;
52  }
53 
55  {
56  if (this->m_DistanceImageSelected)
57  {
58  this->CloseCsvFile(this->m_DistanceOutfile);
59  }
60  if (this->m_AmplitudeImageSelected)
61  {
62  this->CloseCsvFile(this->m_AmplitudeOutfile);
63  }
64  if (this->m_IntensityImageSelected)
65  {
66  this->CloseCsvFile(this->m_IntensityOutfile);
67  }
68  }
69 
70  void ToFImageCsvWriter::Add(float* distanceFloatData, float* amplitudeFloatData, float* intensityFloatData, unsigned char* rgbData)
71  {
72  if (this->m_DistanceImageSelected)
73  {
74  this->WriteCsvFile(this->m_DistanceOutfile, distanceFloatData);
75  }
76  if (this->m_AmplitudeImageSelected)
77  {
78  this->WriteCsvFile(this->m_AmplitudeOutfile, amplitudeFloatData);
79  }
80  if (this->m_IntensityImageSelected)
81  {
82  this->WriteCsvFile(this->m_IntensityOutfile, intensityFloatData);
83  }
84  this->m_NumOfFrames++;
85  }
86 
87  void ToFImageCsvWriter::WriteCsvFile(FILE* outfile, float* floatData)
88  {
89  for(int i=0; i<this->m_ToFPixelNumber; i++)
90  {
91  if (this->m_NumOfFrames==0 && i==0)
92  {
93  fprintf(outfile, "%f", floatData[i]);
94  }
95  else
96  {
97  fprintf(outfile, ",%f", floatData[i]);
98  }
99  }
100  }
101 
102  void ToFImageCsvWriter::OpenCsvFile(FILE** outfile, std::string outfileName)
103  {
104  (*outfile) = fopen( outfileName.c_str(), "w+" );
105  if( !outfile )
106  {
107  MITK_ERROR << "Error opening outfile: " << outfileName;
108  throw std::logic_error("Error opening outfile.");
109  return;
110  }
111  }
112 
113  void ToFImageCsvWriter::CloseCsvFile(FILE* outfile)
114  {
115  if (this->m_NumOfFrames == 0)
116  {
117  fclose(outfile);
118  throw std::logic_error("File is empty.");
119  return;
120  }
121  fclose(outfile);
122  }
123 
124 }
void Open() override
Checks for file extensions and opens the output files.
bool m_AmplitudeImageSelected
flag indicating if amplitude image should be recorded
int m_NumOfFrames
number of frames written to the image. Used for pic header.
bool m_IntensityImageSelected
flag indicating if intensity image should be recorded
void CheckForFileExtension(std::string &fileName)
Checks file name if file extension exists. If not an error message is returned.
#define MITK_ERROR
Definition: mitkLogMacros.h:24
bool m_DistanceImageSelected
flag indicating if distance image should be recorded
DataCollection - Class to facilitate loading/accessing structured data.
FILE * m_IntensityOutfile
file for intensity image
void Close() override
Closes the output files.
int m_ToFCaptureWidth
width (x-dimension) of the images to record.
int m_ToFCaptureHeight
height (y-dimension) of the images to record.
std::string m_IntensityImageFileName
file name for saving the intensity image
std::string m_AmplitudeImageFileName
file name for saving the amplitude image
int m_ToFPixelNumber
number of pixels (widht*height) of the images to record
void Add(float *distanceFloatData, float *amplitudeFloatData, float *intensityFloatData, unsigned char *rgbData=0) override
Pushes the image data to the output files.
std::string m_Extension
file extension used for saving images
Writer class for ToF images.
std::string m_DistanceImageFileName
file name for saving the distance image
FILE * m_DistanceOutfile
file for distance image
int m_ToFImageSizeInBytes
size of the image to save in bytes
FILE * m_AmplitudeOutfile
file for amplitude image