WPOCT Software Developer's Kit (SDK)
SDK For using Wasatch Photonics OCT Spectrometers
FileUtils.h
Go to the documentation of this file.
1 
6 #ifndef FILEUTILS_H
7 #define FILEUTILS_H
8 
9 #define WIN32_LEAN_AND_MEAN
10 #include <string>
11 #include <vector>
12 #include <fstream>
13 #include <mutex>
14 
15 #include "Helper.h"
16 
17 using namespace std;
18 
19 
20 class FileUtils
21 {
22 public:
23  static __declspec(dllexport) bool WriteTiff (const char* fileName, int width, int height,
24  int stride, const unsigned short int * data,
25  bool rotate, const char* tags = nullptr);
26 
27  static __declspec(dllexport) bool WriteTiff (const char* fileName, int width, int height,
28  int stride, int bpp, const unsigned char * data,
29  bool rotate, const char* tags = nullptr);
30 
31  static __declspec(dllexport) bool OpenMultipageTifFileWriteBuffer(const char* qualifiedFilename,
32  bool create,
33  const unsigned short int* buffer,
34  int width, int height,
35  unsigned int bitsPerPixel,
36  bool rotate, int location,
37  const char* tags = nullptr);
38  static __declspec(dllexport) bool WriteBufferToMultipageTifFile (const unsigned short int* buffer,
39  int width, int height,
40  unsigned int bitsPerPixel,
41  bool rotate, int location,
42  const char* tags = nullptr);
43  static __declspec(dllexport) int GetNumPagesInMultipageTifFile (const char* qualifiedFilename);
44  static __declspec(dllexport) bool GetPageSizeInMultipageTifFile (const char* qualifiedFilename,
45  int pageNum, unsigned int* width,
46  unsigned int* height,
47  unsigned int* bpp);
48  static __declspec(dllexport) bool OpenMultipageTifFileReadPage (const char* qualifiedFilename,
49  int pageNum,
50  unsigned short int* data,
51  char* tags = nullptr);
52  static __declspec(dllexport) bool OpenMultipageTifFileReadPage (const char* qualifiedFilename,
53  int pageNum,
54  unsigned char* data,
55  char* tags = nullptr);
56  static __declspec(dllexport) bool ReadPageFromMultipageTifFile (int pageNum,
57  unsigned short int* data,
58  char* tags = nullptr);
59  static __declspec(dllexport) bool ReadPageFromMultipageTifFile (int pageNum,
60  unsigned char* data,
61  char* tags = nullptr);
62  static __declspec(dllexport) bool CloseMultipageTifFile();
63 
64  static __declspec(dllexport) void Cleanup ();
65 
66  static __declspec(dllexport) bool DirectoryExists (const char* dir);
67  static __declspec(dllexport) bool CreateADirectory (const char* dir);
68 
70  static __declspec(dllexport) bool FileExists (const char *filename);
71  static __declspec(dllexport) bool IsValidFilename (const char *filename);
72 
73  static bool WriteTiff (string fileName, int width, int height, int stride,
74  const vector<unsigned short> data, bool rotate);
75  static bool WriteTiff (string fileName, int width, int height, int stride, int bpp,
76  const vector<unsigned char> data, bool rotate);
77 
78  template <typename T>
79  static void SaveMemoryToTextFile(int w, int h, T* plot_data, const char* fname)
80  {
81  ofstream myfile;
82  string fullName(fname);
83  string fullName = fullName + ".txt";
84  myfile.open(fullName.c_str(), ios::out | ios::trunc);
85  for (int r = 0; r < h; r++)
86  {
87  for (int c = 0; c < w; c++)
88  {
89  int i = r * w + c;
90  myfile << *(plot_data + i) << " ";
91  }
92  myfile << endl;
93  }
94  myfile.close();
95  }
96 
97  template <typename T>
98  static bool SaveMemoryToBinFile(const char* fname, int w, int h, const T* data)
99  {
100  return SaveMemoryToBinFile(fname, w * h, data);
101  }
102 
103  template <typename T>
104  static bool SaveMemoryToBinFile(const char* fname, int size, const T* data)
105  {
106  bool result = false;
107  if (fname && data && size > 0)
108  {
109  ofstream fout;
110 
111  try
112  {
113  fout.open(fname, ios::out | ios::binary);
114  if (fout.good())
115  {
116  fout.write((char*)data, size * sizeof(T));
117  if (fout.good())
118  {
119  result = true;
120  }
121  fout.close();
122  }
123  }
124  catch (exception&)
125  {
126  result = false;
127  }
128  }
129  return result;
130  }
131 
132  template <typename T>
133  static bool LoadBinFileToMemory(const char* fname, int width, int height, T* data)
134  {
135  return LoadBinFileToMemory(fname, width * height, data);
136  }
137 
138  template <typename T>
139  static bool LoadBinFileToMemory(const char* fname, int size, T* data)
140  {
141  bool result = false;
142  if (fname && data && size > 0)
143  {
144  try
145  {
146  ifstream fin(fname, ios::binary | ios::ate);
147 
148  if (fin.good())
149  {
150  fin.seekg(ios::beg);
151  fin.read((char*)data, size * sizeof(T));
152  if (fin.good())
153  {
154  result = true;
155  }
156  fin.close();
157  }
158  }
159  catch (exception&)
160  {
161  result = false;
162  }
163  }
164  return result;
165  }
166 
167  template<typename T>
168  static void SaveVector(vector<T> dataVec, string fname)
169  {
170  ofstream myfile;
171  string s = fname + ".txt";
172  myfile.open(s.c_str(), ios::out | ios::trunc);
173  for (int i = 0; i < dataVec.size(); i++)
174  {
175  myfile << dataVec[i] << "\n";
176  }
177  myfile.close();
178  }
179 
180  template<typename T>
181  static ostream& BinaryWrite(ostream& stream, const T& value)
182  {
183  return stream.write(reinterpret_cast<const char*>(&value), sizeof(T));
184  }
185 
191  template<typename T>
192  static inline void SaveVector(int len, T* line_data, string fname)
193  {
194  ofstream myfile;
195  string s = fname + ".txt";
196 
197  myfile.open(s.c_str(), ios::out | ios::trunc);
198  for (int i = 0; i < len; i++)
199  {
200  myfile << *(line_data + i) << "\n";
201  }
202  myfile.close();
203  }
204 
208  template<typename T>
209  static inline void SaveVector(int start, int len, T* line_data, string fname)
210  {
211  ofstream myfile;
212  string s = fname + ".txt";
213 
214  myfile.open(s.c_str(), ios::out | ios::trunc);
215  for (int i = 0; i < len; i++)
216  {
217  myfile << *(line_data + start + i) << "\n";
218  }
219  myfile.close();
220  }
221 
222  static string GetFirstFileMatch (const string &directory, const string& partialFileName);
223  static size_t GetFileSize (const string& filename);
224 
225  //static void GetFilesInDirectory (const string &directory, vector<string> &files);
226  // Find file in directory specified.
227  //static const char* FindFile(const char* dir_path, const char* file_name);
228 
229 private:
230  static vector<unsigned short int> _transposedShortData;
231  static vector<unsigned char> _transposedCharData;
232  static mutex _mutex;
233  static HINSTANCE _hinstLib;
234 
235  static void LoadStorageUtils();
236 
237  static bool SubstituteFileExtension (string& filename, const char* extension);
238 
239  static bool WriteUsingTiffUtils (const char* fileName, int width, int height, int stride,
240  const unsigned short int * data,
241  const char* tags = nullptr);
242  static bool WriteUsingTiffUtils (const char* fileName, int width, int height, int stride,
243  int bpp, const unsigned char * data,
244  const char* tags = nullptr);
245  static bool OpenWritePageUsingTiffUtils(const char* qualifiedFilename, bool create,
246  const unsigned short int* buffer, int width,
247  int height, unsigned int bitsPerPixel, int location,
248  const char* tags);
249  static bool WritePageUsingTiffUtils (const unsigned short int* buffer, int width,
250  int height, unsigned int bitsPerPixel,
251  int location, const char* tags);
252  static int GetNumPagesUsingTiffUtils (const char* qualifiedFilename);
253  static bool GetPageSizeUsingTiffUtils (const char* qualifiedFilename, int pageNum,
254  unsigned int* width, unsigned int* height,
255  unsigned int* bpp);
256  static bool OpenReadPageUsingTiffUtils (const char* qualifiedFilename, int pageNum,
257  unsigned short int* data, char* tags = nullptr);
258  static bool ReadPageUsingTiffUtils (int pageNum, unsigned short int* data,
259  char* tags = nullptr);
260  static bool OpenReadPageUsingTiffUtils (const char* qualifiedFilename, int pageNum,
261  unsigned char* data, char* tags = nullptr);
262  static bool ReadPageUsingTiffUtils (int pageNum, unsigned char* data, char* tags = nullptr);
263  static bool CloseUsingTiffUtils ();
264 
265 };
266 
267 #endif //FILEUTILS_H
Utility template functions.
__declspec(dllexport) IWPOCTCamera *GetOCTCamera(IWPOCTCamera __declspec(dllexport) IWPOCTCamera __declspec(dllexport) bool DestroyOCTCamera(IWPOCTCamera *camera
Gets the instance of the camera object.
Definition: IWPOCTController.cpp:29
Definition: FileUtils.h:21
static __declspec(dllexport) bool FileExists(const char *filename)
Test if file exists.
static void SaveVector(int len, T *line_data, string fname)
save the array to file
Definition: FileUtils.h:192
static void SaveVector(int start, int len, T *line_data, string fname)
save the array[start] - array[start + len] to file
Definition: FileUtils.h:209