WPOCT Software Developer's Kit (SDK)
SDK For using Wasatch Photonics OCT Spectrometers
WPScanConverter.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <boost/signals2.hpp>
9 #include <boost/bind.hpp>
10 
11 #include <string>
12 #include <vector>
13 #include <mutex>
14 #include <atomic>
15 #include <iostream>
16 #include <fstream>
17 
18 #include "OCTTypeDefs.h"
19 #include "IWPOCTConverter.h"
20 #include "ConversionInOutData.h"
21 #include "ScanProcessor.h"
22 
23 using namespace std;
24 
28 {
29 public:
30  const int InitialNumScanProcessors = 1;
31  const int MaxAScanSize = 8192;
32  const int MaxAScansPerBScan = 65000;
33  const int MaxNumScanProcessors = 8;
34 
35  // Constructor
37 
38  // Destructor
39  virtual ~WPScanConverter();
40 
41  // Events
42  boost::signals2::signal<void(const char* )> SignalCalibrationFileChanged;
43 
44  // Getters
45 
47  const char* GetQualifiedInputFileName() const override { return _qualifiedInputFileName.c_str(); }
48 
50  const char* GetOutputIntensityName () const override { return _outputIntensityName.c_str(); }
51 
53  const char* GetOutputPhaseName () const override { return _outputPhaseName.c_str(); }
54 
56  const char* GetOutputDirectory () const override { return _outputDirectory.c_str(); }
57 
59  const char* GetCalibrationFringeFile () const override { return _calibrationFringeFile.c_str(); }
60 
62  bool GetOutputIntensity () const override { return _outputIntensity; }
63 
65  bool GetOutputPhase () const override { return _outputPhase; }
66 
67  // Get the flag that determines if GPU is used in calculations.
68  //bool GetUseGPU () const override { return _useGPU; }
69 
72  bool GetDeleteInputFileAfterConversion () const override { return _deleteInputFile; }
73 
75  bool GetOutputTiffFormat () const override { return _outputTiffFormat; }
76 
78  bool GetOutputLogOfData () const override { return ScanProcessor::GetOutputLogValues(); }
79 
81  bool GetUseContrast () const override { return ScanProcessor::GetUseAutoContrast(); }
82 
84  bool GetUseScaling () const override { return ScanProcessor::GetUseScaling(); }
85 
86  // Get the flag that determines if apodization is used.
87  //bool GetUseApodization () const override { return ScanProcessor::GetUseApodization(); }
88 
90  int GetAScansPerBScan () const override { return _scanProcessors[0]->GetWidth(); }
92  int GetAScanSize () const override { return _scanProcessors[0]->GetHeight(); }
94  bool GetScanDimensions (int* width, int* height) const override;
95 
97  bool GetDispersionCoefficients(float* coefficients, int length) const override;
98 
100  bool GetNumberOfLinesToIgnore(int* top, int* bottom) const override;
101 
103  bool GetMinMaxScaling (float* min, float* max) const override;
104 
106  int GetNumScanProcessors () const override { return (int)_scanProcessors.size(); }
107 
109  int GetAScanAverage () const override { return _scanProcessors[0]->GetAScanAverage(); }
110 
112  virtual bool GetUseMovingAverage () const override { return ScanProcessor::GetUseMovingAverage(); }
113  virtual int GetMovingAverageWindowSize () const override { return ScanProcessor::GetMovingAverageWindowSize(); }
114 
115  // Setters
116 
118  bool SetQualifiedInputFileName(const char* name) override;
119 
121  bool SetOutputIntensityName (const char* name) override;
122 
124  bool SetOutputPhaseName (const char* name) override;
125 
127  bool SetOutputDirectory (const char* name) override;
128 
130  bool SetCalibrationFringeFile(const char* name) override;
131 
133  bool SetOutputIntensity (bool value) override { _outputIntensity = value; return true; }
134 
136  bool SetOutputPhase (bool value) override { _outputPhase = value; return true; }
137 
138  // Set the flag that determines if GPU is used in calculations.
139  //bool SetUseGPU (bool value) override { _useGPU = value; return true; }
140 
142  bool SetDeleteInputFileAfterConversion (bool value) override { _deleteInputFile = value; return true; }
143 
145  bool SetOutputTiffFormat (bool value) override { _outputTiffFormat = value; return true; }
146 
148  bool SetOutputLogOfData (bool value) override { ScanProcessor::SetOutputLogValues(value); return true; }
149 
152  bool SetUseScaling (bool value) override
153  {
154  if (!value)
155  {
156  ScanProcessor::SetUseAutoContrast(false);
157  }
158  return ScanProcessor::SetUseScaling(value);
159  }
160 
162  bool SetUseContrast (bool value) override { return ScanProcessor::SetUseAutoContrast(value); }
163 
164  // Set the flag that determines if apodization is used.
165  //bool SetUseApodization (bool value) override { ScanProcessor::SetUseApodization(value); }
166 
168  bool SetDispersionCoefficients (const float* dispersionCoeffients, int length) override;
169 
170  bool SetDispersionOptimizationParameters(int numCoefficients, int maxIterations,
171  float* initialGuesses, int startLine,
172  int stopLine, int offset) override;
174  bool CalcDispersionCoefficients(const unsigned short* inData, int width, int height,
175  float* coefficients) override;
176 
178  bool SetNumberOfLinesToIgnore (int top, int bottom) override;
179 
181  bool SetMinMaxScaling (float min, float max) override
182  {
183  if (min > max || min < 0 || max > 1)
184  {
185  _lastError = InvalidArgumentValue;
186  return false;
187  }
188 
189  ScanProcessor::SetAutoScaleUserMin(min);
190  ScanProcessor::SetAutoScaleUserMax(max);
191  _lastError = NoError;
192  return true;
193  }
194 
196  bool SetAScansPerBScan (int value) override
197  {
198  if (value <= 0 || value > MaxAScansPerBScan)
199  {
200  _lastError = InvalidArgumentValue;
201  return false;
202  }
203 
204  _scanWidth = value;
205  _resizeDataContainers = true;
206  for (size_t i = 0; i < _scanProcessors.size(); i++)
207  {
208  _scanProcessors[i]->SetWidth(value);
209  }
210  _lastError = NoError;
211  return true;
212  }
213 
215  bool SetAScanSize (int value) override
216  {
217  if (value <= 0 || value > MaxAScanSize)
218  {
219  _lastError = InvalidArgumentValue;
220  return false;
221  }
222 
223  _scanHeight = value;
224  _resizeDataContainers = true;
225  // Set height of all the scan processors.
226  for (size_t i = 0; i < _scanProcessors.size(); i++)
227  {
228  _scanProcessors[i]->SetHeight(value);
229  }
230  _lastError = NoError;
231  return true;
232  }
233 
235  bool SetScanDimensions (int aScansPerBScan, int aScanSize) override
236  {
237  if (aScansPerBScan <= 0 || aScansPerBScan > MaxAScansPerBScan ||
238  aScanSize <= 0 || aScanSize > MaxAScanSize)
239  {
240  _lastError = InvalidArgumentValue;
241  return false;
242  }
243 
244  if (_scanWidth != aScansPerBScan || _scanHeight != aScanSize)
245  {
246  _scanWidth = aScansPerBScan;
247  _scanHeight = aScanSize;
248  _resizeDataContainers = true;
249  // Set height of all the scan processors.
250  for (size_t i = 0; i < _scanProcessors.size(); i++)
251  {
252  _scanProcessors[i]->SetWidthAndHeight(aScansPerBScan, aScanSize);
253  }
254  }
255  _lastError = NoError;
256  return true;
257  }
258 
260  bool SetAScanAverage (int value) override
261  { return _scanProcessors[0]->SetAScanAverage(value); }
262 
264  bool SetNumScanProcessors(int value) override;
265 
267  bool SetUseMovingAverage (bool value) override
268  { return ScanProcessor::SetUseMovingAverage(value); }
269  bool SetMovingAverageWindowSize (int value) override
270  { return ScanProcessor::SetMovingAverageWindowSize(value); }
271  bool ResetMovingAverage () override
272  { return ScanProcessor::ResetMovingAverage(); }
273 
276  bool UpdateBackgroundImage () override;
278  bool GetBackgroundImage (float* buffer, int size) override;
280  bool SetBackgroundImage (float* buffer, int size) override;
281 
282  // Conversion routines
283 
284  // Convert raw spectral data to intensity array. Returns true if successful.
285  bool ConvertSpectralDataToIntensity (IWPOCTInOutData* inOutData) override;
286  bool ConvertSpectralDataToIntensity (const unsigned short* inData, float* outIntensity) override;
287 
288  // Convert raw spectral data to phase and intensity arrays. Returns true if successful.
289  bool ConvertSpectralDataToPhaseIntensity (IWPOCTInOutData* inOutData) override;
290  bool ConvertSpectralDataToPhaseIntensity (const unsigned short* inData, float* outIntensity,
291  float* outPhase) override;
292 
295  bool ConvertSpectralFileToPhaseIntensity () override;
296 
299  bool ConvertSpectralFileToPhaseIntensity (const char* qualifiedFileName) override;
300 
303  bool ConvertSpectralDirToPhaseIntensity () override;
304 
306  bool ConvertFloatToUnsignedChar (const float* inData, unsigned char* outData,
307  int size) const override;
308 
310  bool ConvertFloatToUnsignedShort (const float* inData, unsigned short* outData,
311  int size) const override;
312 
313  bool TransposeArray (const float* src, float* dst, int inWidth, int inHeight) const override;
314  bool TransposeArray (const unsigned short* src, unsigned short* dst, int inWidth, int inHeight) const override;
315  bool TransposeArray (const unsigned char* src, unsigned char* dst, int inWidth, int inHeight) const override;
316 
317  bool FlipVertical (unsigned short* data, int width, int height) const override;
318  bool FlipHorizontal (unsigned short* data, int width, int height) const override;
319  bool RotateImage (const unsigned short* inData, unsigned short* outData, int width, int height,
320  unsigned int cw90Multiplier) const override;
321 
322  virtual IWPOCTConverter::ErrorCode GetLastError() const override;
323 
324 protected:
325  void SignalProgress ();
326  // File routines
327  bool ReadBinFileToMemory (const char* qualifiedFileName, unsigned short* outData);
328  bool WriteMemoryToFile (const char* qualifiedFileName, const U16* inData);
329  bool WriteMemoryToFile (const char* qualifiedFileName, const float* inData);
330  string GetFileName (const string& fullName);
331 
332 
333 private:
334  std::mutex _mutex;
335 
337  int _scanWidth;
339  int _scanHeight;
341  int _currScanProcessor;
342 
344  string _qualifiedInputFileName;
345 
347  string _outputPhaseName;
348  string _outputPhaseIndexedName;
349 
351  string _outputIntensityName;
352  string _outputIntensityIndexedName;
353 
355  string _outputDirectory;
356 
358  string _calibrationFringeFile;
359 
360  clock_t _startTime;
361 
363  std::atomic<bool> _outputIntensity;
364 
366  std::atomic<bool> _outputPhase;
367 
369  std::atomic<bool> _useGPU;
370 
372  std::atomic<bool> _deleteInputFile;
373 
375  std::atomic<bool> _outputTiffFormat;
376 
378  std::atomic<long> _dongleVal;
380  std::atomic<ErrorCode> _lastError;
381 
383  std::vector<ScanProcessor*> _scanProcessors;
384 
386  IWPOCTInOutData* _inOutData;
387 
389  std::vector<U16> _shortData;
390 
392  bool _resizeDataContainers;
393 
395  std::vector<std::string> _filesInDir;
396 
398  void Init();
399 
401  void OnCalibrationFileChanged(const char* name);
402 
403  // Resize containers.
404  void ResizeContainers ();
405  void ResizeShortData (int size);
406  template <typename T>
407  void ResizeData (std::vector<T>& vec, int size);
408 
409  std::vector<std::string> GetFilesInDir (std::string& myDir);
410 
411  string FormOutputName (string& path, bool intensity);
412  string FormOutputName (string& path, string& name);
413  string GetBaseFileName (string& inName, bool& numsAtEnd);
414  string GetIndexFromFile (string& currFile, bool numsAtEnd);
415 
416  ScanProcessor* GetNextScanProcessor();
417 
418  void GenerateOutputIntensityIndexedName (string& index, bool numsAtEnd);
419  void GenerateOutputPhaseIndexedName (string& index, bool numsAtEnd);
420 
423  bool ConvertSpectralFileToPhaseIntensity (std::string& qualifiedFileName);
424 };
425 
426 // Implementation
427 template <typename T>
428 void WPScanConverter::ResizeData(std::vector<T>& vec, int size)
429 {
430  if (vec.size() != size)
431  {
432  vec.clear();
433  vec.resize(size);
434  }
435 }
Interface of the ConversionInOutData class.
Interface of the IWPOCTConverter struct.
typedefs used across UtensilConverter
Interface of the ScanProcessor class.
Class that processes scans.
Definition: ScanProcessor.h:32
Class that converts a scanned image from one format to another.
Definition: WPScanConverter.h:28
const char * GetOutputDirectory() const override
Get the output directory.
Definition: WPScanConverter.h:56
bool GetDeleteInputFileAfterConversion() const override
Get the flag that determines if the RAW input file is deleted after data is converted.
Definition: WPScanConverter.h:72
const char * GetOutputPhaseName() const override
Get the phase file name (no path).
Definition: WPScanConverter.h:53
bool SetMinMaxScaling(float min, float max) override
Set the min and max scaling values.
Definition: WPScanConverter.h:181
int GetAScansPerBScan() const override
Get the number of A-scans in a B-scan.
Definition: WPScanConverter.h:90
int GetAScanSize() const override
Get the size of an A-scan.
Definition: WPScanConverter.h:92
const char * GetCalibrationFringeFile() const override
Get the calibration fringe file name.
Definition: WPScanConverter.h:59
bool SetUseContrast(bool value) override
Set the flag that determines if contrast is used on the output.
Definition: WPScanConverter.h:162
bool SetOutputLogOfData(bool value) override
Set the flag that determines if the log of the output data is taken/used.
Definition: WPScanConverter.h:148
bool SetAScansPerBScan(int value) override
Get the number of A-scans in a B-scan.
Definition: WPScanConverter.h:196
bool GetOutputPhase() const override
Get the flag that determines if phase is output.
Definition: WPScanConverter.h:65
bool ResetMovingAverage() override
Resets the moving average by clearing the buffers.
Definition: WPScanConverter.h:271
const char * GetQualifiedInputFileName() const override
Get the fully qualified file name (includes path).
Definition: WPScanConverter.h:47
virtual bool GetUseMovingAverage() const override
Getters for displaying a moving average.
Definition: WPScanConverter.h:112
bool SetUseScaling(bool value) override
Set the flag that determines if scaling is used on the output.
Definition: WPScanConverter.h:152
int GetNumScanProcessors() const override
Get the number of scan processors currently in use.
Definition: WPScanConverter.h:106
virtual int GetMovingAverageWindowSize() const override
Gets the window size of the moving average.
Definition: WPScanConverter.h:113
int GetAScanAverage() const override
Get the number of A-scan averages.
Definition: WPScanConverter.h:109
bool SetAScanSize(int value) override
Set the size of an A-scan.
Definition: WPScanConverter.h:215
bool SetMovingAverageWindowSize(int value) override
Sets the window size of the moving average.
Definition: WPScanConverter.h:269
bool SetOutputIntensity(bool value) override
Set the flag that determines if intensity is output.
Definition: WPScanConverter.h:133
bool SetScanDimensions(int aScansPerBScan, int aScanSize) override
Set the width and height of a scan.
Definition: WPScanConverter.h:235
bool SetAScanAverage(int value) override
Set the A-scan average. Returns true if successful.
Definition: WPScanConverter.h:260
bool SetOutputTiffFormat(bool value) override
Get the flag that determines if the output files will be in Tiff format.
Definition: WPScanConverter.h:145
bool GetOutputLogOfData() const override
Get the flag that determines if the log of the output data is taken/used.
Definition: WPScanConverter.h:78
bool GetUseContrast() const override
Get the flag that determines if contrast is used on the output.
Definition: WPScanConverter.h:81
bool SetDeleteInputFileAfterConversion(bool value) override
Set the flag that determines if the RAW input file is deleted after data is converted.
Definition: WPScanConverter.h:142
bool SetUseMovingAverage(bool value) override
Setters for displaying a moving average.
Definition: WPScanConverter.h:267
const char * GetOutputIntensityName() const override
Get the intensity file name (no path).
Definition: WPScanConverter.h:50
bool GetOutputIntensity() const override
Get the flag that determines if intensity is output.
Definition: WPScanConverter.h:62
bool GetUseScaling() const override
Get the flag that determines if scaling is used on the output.
Definition: WPScanConverter.h:84
bool SetOutputPhase(bool value) override
Set the flag that determines if phase is output.
Definition: WPScanConverter.h:136
bool GetOutputTiffFormat() const override
Get the flag that determines if the output files will be in Tiff format.
Definition: WPScanConverter.h:75
Class to convert data obtained from a line scan camera into meaningful 2D OCT image data.
Definition: IWPOCTConverter.h:56
ErrorCode
The following are error codes that are used within the converter object.
Definition: IWPOCTConverter.h:61
The data object contains pointers to input and output data.
Definition: IWPOCTInOutData.h:14