gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
Utilities.inl
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2016-2023, Dr Franck P. Vidal, Bangor University, All rights reserved.
4 Copyright (c) 2023-present, Prof Franck P. Vidal (franck.vidal@stfc.ac.uk),
5 UK Research and Innovation, All rights reserved.
6 
7 
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
17 
18 3. Neither the name of Bangor University, UK Research and Innovation nor the
19 names of its contributors may be used to endorse or promote products derived
20 from this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 
63 //******************************************************************************
64 // Include
65 //******************************************************************************
66 #include <cstring>
67 #include <cmath>
68 
69 #ifndef __ConstantValues_h
71 #endif
72 
73 
74 //******************************************************************************
75 // namespace
76 //******************************************************************************
77 namespace gVirtualXRay {
78 
79 
80  //---------------------------------------
81  inline bool isMAT(const char* aFileName)
82  //---------------------------------------
83  {
84  if (checkExtension(aFileName, "mat") ||
85  checkExtension(aFileName, "MAT"))
86  {
87  return (true);
88  }
89  else
90  {
91  return (false);
92  }
93  }
94 
95 
96  //---------------------------------------
97  inline bool isDAT(const char* aFileName)
98  //---------------------------------------
99  {
100  if (checkExtension(aFileName, "dat") ||
101  checkExtension(aFileName, "DAT"))
102  {
103  return (true);
104  }
105  else
106  {
107  return (false);
108  }
109  }
110 
111 
112  //---------------------------------------
113  inline bool isTXT(const char* aFileName)
114  //---------------------------------------
115  {
116  if (checkExtension(aFileName, "txt") ||
117  checkExtension(aFileName, "TXT"))
118  {
119  return (true);
120  }
121  else
122  {
123  return (false);
124  }
125  }
126 
127 
128  //---------------------------------------
129  inline bool isPGM(const char* aFileName)
130  //---------------------------------------
131  {
132  if (checkExtension(aFileName, "pgm") ||
133  checkExtension(aFileName, "PGM"))
134  {
135  return (true);
136  }
137  else
138  {
139  return (false);
140  }
141  }
142 
143 
144  //---------------------------------------
145  inline bool isRAW(const char* aFileName)
146  //---------------------------------------
147  {
148  if (checkExtension(aFileName, "raw") ||
149  checkExtension(aFileName, "RAW"))
150  {
151  return (true);
152  }
153  else
154  {
155  return (false);
156  }
157  }
158 
159 
160  //---------------------------------------
161  inline bool isMHA(const char* aFileName)
162  //---------------------------------------
163  {
164  if (checkExtension(aFileName, "mha") ||
165  checkExtension(aFileName, "MHA"))
166  {
167  return (true);
168  }
169  else
170  {
171  return (false);
172  }
173  }
174 
175 
176  //---------------------------------------
177  inline bool isMHD(const char* aFileName)
178  //---------------------------------------
179  {
180  if (checkExtension(aFileName, "mhd") ||
181  checkExtension(aFileName, "MHD"))
182  {
183  return (true);
184  }
185  else
186  {
187  return (false);
188  }
189  }
190 
191 
192  //---------------------------------------
193  inline bool isDCM(const char* aFileName)
194  //---------------------------------------
195  {
196  if (checkExtension(aFileName, "dcm") ||
197  checkExtension(aFileName, "DCM"))
198  {
199  return (true);
200  }
201  else
202  {
203  return (false);
204  }
205  }
206 
207 
208  //---------------------------------------
209  inline bool isJPEG(const char* aFileName)
210  //---------------------------------------
211  {
212  if (checkExtension(aFileName, "jpg") ||
213  checkExtension(aFileName, "jpeg") ||
214  checkExtension(aFileName, "JPG") ||
215  checkExtension(aFileName, "JPEG"))
216  {
217  return (true);
218  }
219  else
220  {
221  return (false);
222  }
223  }
224 
225 
226  //---------------------------------------
227  inline bool isTIFF(const char* aFileName)
228  //---------------------------------------
229  {
230  if (checkExtension(aFileName, "tif") ||
231  checkExtension(aFileName, "tiff") ||
232  checkExtension(aFileName, "TIF") ||
233  checkExtension(aFileName, "TIFF"))
234  {
235  return (true);
236  }
237  else
238  {
239  return (false);
240  }
241  }
242 
243 
244  //---------------------------------------------
245  inline bool isMAT(const std::string& aFileName)
246  //---------------------------------------------
247  {
248  return (isMAT(aFileName.data()));
249  }
250 
251 
252  //---------------------------------------------
253  inline bool isDAT(const std::string& aFileName)
254  //---------------------------------------------
255  {
256  return (isDAT(aFileName.data()));
257  }
258 
259 
260  //---------------------------------------------
261  inline bool isTXT(const std::string& aFileName)
262  //---------------------------------------------
263  {
264  return (isTXT(aFileName.data()));
265  }
266 
267 
268  //---------------------------------------------
269  inline bool isPGM(const std::string& aFileName)
270  //---------------------------------------------
271  {
272  return (isPGM(aFileName.data()));
273  }
274 
275 
276  //---------------------------------------------
277  inline bool isRAW(const std::string& aFileName)
278  //---------------------------------------------
279  {
280  return (isRAW(aFileName.data()));
281  }
282 
283 
284 //---------------------------------------------
285 inline bool isMHA(const std::string& aFileName)
286 //---------------------------------------------
287 {
288  return (isMHA(aFileName.data()));
289 }
290 
291 
292 //---------------------------------------------
293 inline bool isMHD(const std::string& aFileName)
294 //---------------------------------------------
295 {
296  return (isMHD(aFileName.data()));
297 }
298 
299 
300 //---------------------------------------------
301 inline bool isDCM(const std::string& aFileName)
302 //---------------------------------------------
303 {
304  return (isDCM(aFileName.data()));
305 }
306 
307 
308 //----------------------------------------------
309 inline bool isJPEG(const std::string& aFileName)
310 //----------------------------------------------
311 {
312  return (isJPEG(aFileName.data()));
313 }
314 
315 
316 //----------------------------------------------
317 inline bool isTIFF(const std::string& aFileName)
318 //----------------------------------------------
319 {
320  return (isTIFF(aFileName.data()));
321 }
322 
323 
324 //-------------------------------------------------
325 inline bool checkExtension(const char* aFileName,
326  const char* anExtension)
327 //-------------------------------------------------
328 {
329  return (checkExtension(std::string(aFileName), std::string(anExtension)));
330 }
331 
332 
333 //-----------------------
334 inline bool isBigEndian()
335 //-----------------------
336 {
337  unsigned int x(1);
338  return (reinterpret_cast<unsigned char*>(&x)[0] == 0);
339 }
340 
341 
342 //--------------------------
343 inline bool isLittleEndian()
344 //--------------------------
345 {
346  return (!isBigEndian());
347 }
348 
349 
350 //-------------------------------
351 inline bool isLetter(char aValue)
352 //-------------------------------
353 {
354  return (('a' <= aValue && aValue <= 'z') ||
355  ('A' <= aValue && aValue <= 'Z'));
356 }
357 
358 
359 //----------------------------------
360 inline bool isUpperCase(char aValue)
361 //----------------------------------
362 {
363  return ('A' <= aValue && aValue <= 'Z');
364 }
365 
366 
367 //----------------------------------
368 inline bool isLowerCase(char aValue)
369 //----------------------------------
370 {
371  return ('a' <= aValue && aValue <= 'z');
372 }
373 
374 
375 //-------------------------------
376 inline bool isNumber(char aValue)
377 //-------------------------------
378 {
379  return ('0' <= aValue && aValue <= '9');
380 }
381 
382 
383 //--------------------------------------------
384 template<typename T> void swapBytes(T& aValue)
385 //--------------------------------------------
386 {
387  // There is more than one byte in T
388  if (sizeof(T) != 1)
389  {
390  // Swap bytes
391  char* p_casted_format(reinterpret_cast<char*>(&aValue));
392  for (unsigned int i(0); i < sizeof(T) / 2; ++i)
393  {
394  char temp(p_casted_format[i]);
395  p_casted_format[i] = p_casted_format[sizeof(T) - 1 - i];
396  p_casted_format[sizeof(T) - 1 - i] = temp;
397  }
398  }
399 }
400 
401 
402 //-------------------------------------------------------------
403 template<typename T> T degreeToRadian(const T& anAngleInDegree)
404 //-------------------------------------------------------------
405 {
406  return (anAngleInDegree * Pi / 180.0);
407 }
408 
409 
410 //-------------------------------------------------------------
411 template<typename T> T radianToDegree(const T& anAngleInRadian)
412 //-------------------------------------------------------------
413 {
414  return (anAngleInRadian * 180.0 / Pi);
415 }
416 
417 
418 //--------------------------------------------
419 template <typename T> int sgn(const T& aValue)
420 //--------------------------------------------
421 {
422  return (T(0) < aValue) - (aValue < T(0));
423 }
424 
425 
426 //---------------------------------------------
427 inline double interpolate(const double& a_low,
428  const double& a_high,
429  const double& a0,
430  const double& b_low,
431  const double& b_high)
432 //---------------------------------------------
433 {
434  return b_low + (b_high - b_low) * (a0 - a_low) / (a_high - a_low);
435 }
436 
437 
438 //-------------------------------------------
439 inline float interpolate(const float& a_low,
440  const float& a_high,
441  const float& a0,
442  const float& b_low,
443  const float& b_high)
444 //-------------------------------------------
445 {
446  return b_low + (b_high - b_low) * (a0 - a_low) / (a_high - a_low);
447 }
448 
449 
450 } // namespace gVirtualXRay
bool isRAW(const char *aFileName)
Check if the extension of a file name is RAW.
Definition: Utilities.inl:145
bool isMHD(const char *aFileName)
Check if the extension of a file name is MHD.
Definition: Utilities.inl:177
bool isLetter(char aValue)
Definition: Utilities.inl:351
bool isBigEndian()
Definition: Utilities.inl:334
bool isDAT(const char *aFileName)
Check if the extension of a file name is DAT.
Definition: Utilities.inl:97
bool isTIFF(const char *aFileName)
Check if the extension of a file name is TIFF.
Definition: Utilities.inl:227
bool isMHA(const char *aFileName)
Check if the extension of a file name is MHA.
Definition: Utilities.inl:161
T degreeToRadian(const T &aAngleInDegree)
Definition: Utilities.inl:403
bool isPGM(const char *aFileName)
Check if the extension of a file name is PGM.
Definition: Utilities.inl:129
T radianToDegree(const T &aAngleInRadian)
Definition: Utilities.inl:411
bool isTXT(const char *aFileName)
Check if the extension of a file name is TXT.
Definition: Utilities.inl:113
bool isJPEG(const char *aFileName)
Check if the extension of a file name is JPEG.
Definition: Utilities.inl:209
bool isLittleEndian()
Definition: Utilities.inl:343
bool isDCM(const char *aFileName)
Check if the extension of a file name is DCM.
Definition: Utilities.inl:193
bool isNumber(char aValue)
Definition: Utilities.inl:376
void swapBytes(T &aValue)
Definition: Utilities.inl:384
bool isUpperCase(char aValue)
Definition: Utilities.inl:360
bool checkExtension(const char *aFileName, const char *anExtension)
Definition: Utilities.inl:325
bool isLowerCase(char aValue)
Definition: Utilities.inl:368
const double Pi
Pi.
Constant values, such as the Z number of different atoms, etc.
int sgn(const T &aValue)
Definition: Utilities.inl:419
bool isMAT(const char *aFileName)
Check if the extension of a file name is MAT.
Definition: Utilities.inl:81
double interpolate(const double &a_low, const double &a_high, const double &a0, const double &b_low, const double &b_high)
Definition: Utilities.inl:427