CASToR  2.0
Tomographic Reconstruction (PET/SPECT/CT)
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
oInterfileIO.hh
Go to the documentation of this file.
1 /*
2 This file is part of CASToR.
3 
4  CASToR is free software: you can redistribute it and/or modify it under the
5  terms of the GNU General Public License as published by the Free Software
6  Foundation, either version 3 of the License, or (at your option) any later
7  version.
8 
9  CASToR is distributed in the hope that it will be useful, but WITHOUT ANY
10  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  details.
13 
14  You should have received a copy of the GNU General Public License along with
15  CASToR (in file GNU_GPL.TXT). If not, see <http://www.gnu.org/licenses/>.
16 
17 Copyright 2017-2018 all CASToR contributors listed below:
18 
19  --> current contributors: Thibaut MERLIN, Simon STUTE, Didier BENOIT, Claude COMTAT, Marina FILIPOVIC, Mael MILLARDET
20  --> past contributors: Valentin VIELZEUF
21 
22 This is CASToR version 2.0.
23 */
24 
32 #ifndef OINTERFILEIO_HH
33 #define OINTERFILEIO_HH 1
34 
36 #include "sScannerManager.hh"
37 
38 // ---------------------------------------------------------------------
56 #define INTF_BIG_ENDIAN 0
57 
58 #define INTF_LITTLE_ENDIAN 1
59 
69 #define INTF_IMG_STATIC 0
70 
71 #define INTF_IMG_DYNAMIC 1
72 
73 #define INTF_IMG_PET 2
74 
75 #define INTF_IMG_SPECT 3
76 
77 #define INTF_IMG_GATED 4
78 
79 #define INTF_IMG_GSPECT 5
80 
81 #define INTF_IMG_UNKNOWN 6
82 
97 #define INTF_LERP_DISABLED false
98 
99 #define INTF_LERP_ENABLED true
100 
112 #define BIT_str "bit"
113 
114 #define UINT32_str "unsigned integer"
115 
116 #define INT32_str "signed integer"
117 
118 #define FLT32_str "short float"
119 #define FLT32_str2 "float"
120 
121 #define FLT64_str "long float"
122 
123 #define LONGDOUBLE_str "long long float"
124 
125 #define ASCII_str "ASCII"
126 
137 #define INTF_SUPINE_HEADIN_TRANSAXIAL 0
138 
139 #define INTF_SUPINE_HEADIN_SAGITTAL 1
140 
141 #define INTF_SUPINE_HEADIN_CORONAL 2
142 
143 #define INTF_SUPINE_FEETIN_TRANSAXIAL 3
144 
145 #define INTF_SUPINE_FEETIN_SAGITTAL 4
146 
147 #define INTF_SUPINE_FEETIN_CORONAL 5
148 
149 #define INTF_PRONE_HEADIN_TRANSAXIAL 6
150 
151 #define INTF_PRONE_HEADIN_SAGITTAL 7
152 
153 #define INTF_PRONE_HEADIN_CORONAL 8
154 
155 #define INTF_PRONE_FEETIN_TRANSAXIAL 9
156 
157 #define INTF_PRONE_FEETIN_SAGITTAL 10
158 
159 #define INTF_PRONE_FEETIN_CORONAL 11
160 
173 #define INTF_TRANSVERSE 0
174 
175 #define INTF_CORONAL 1
176 
177 #define INTF_SAGITTAL 2
178 
179 #define INTF_OTHER 3
180 
193 #define INTF_SUPINE 0
194 
195 #define INTF_PRONE 1
196 
209 #define INTF_HEADIN 0
210 
211 #define INTF_FEETIN 1
212 
223 struct Intf_key
224 {
225  string korig;
226  string kcase;
227  string klcase;
228  string kvalue;
229 };
233 // ---------------------------------------------------------------------
241 {
242  string path_to_image;
248  uint8_t endianness;
251  uint32_t data_offset;
254  string nb_format;
257  uint8_t nb_dims;
260  uint32_t mtx_size[7];
267 // float vox_size[3]; /*!< Voxel dimensions in mm.\n
271 // float slice_thickness_mm ; /*!< Read from the key 'slice thickness (pixels)'.\n
282  uint16_t nb_time_frames ;
285  uint16_t nb_resp_gates ;
288  uint16_t nb_card_gates ;
291  uint32_t nb_total_imgs;
294  uint8_t nb_bytes_pixel;
301  int8_t pat_rotation;
318  uint32_t cmtx_size[3];
331  int data_type;
335 // float study_duration; /*!< Acquisition duration.\n
339  vector<FLTNB> image_duration;
343  vector<FLTNB> image_start_time;
347  uint32_t nb_time_windows;
352  string process_status;
355  // --- Fields related to an image in a group --- // // todo
356 
360 // float image_pause; /*!< Pause between time windows ?\n
361  //FLTNB image_pause;
367  vector<FLTNB> frame_group_pause;
370  // --- SPECT and projection related data --- //
371  uint16_t nb_detector_heads;
374  uint32_t nb_energy_windows;
377  uint16_t nb_projections;
380 // float extent_rotation; /*!< Angular span ex: 180, 360.\n
387 // float first_angle; /*!< Angle of the first view.\n
394  string radius;
396 };
401 #include "gVariables.hh"
402 #include "sOutputManager.hh"
403 #include "gOptions.hh"
404 
405  // -------------------------------------------------------------------
406  // ----- "PUBLIC" FUNCTIONS WHICH COULD AND SHOULD BE ANYWHERE IN THE CODE WHERE INTERFILE IS REQUIRED -----
407 
420  template <typename T> int IntfKeyGetValueFromFile(const string& a_pathToHeader, const string& a_key, T* ap_return, int a_nbElts, int a_mandatoryFlag);
421 
436  template <typename T> int IntfKeyGetRecurringValueFromFile(const string& a_pathToHeader, const string& a_key, T* ap_return, int a_nbElts, int a_mandatoryFlag, uint16_t a_nbOccurrences);
437 
449  int IntfReadProjectionImage( const string& a_pathToHeaderFile,
450  FLTNB* ap_ImgMatrix,
451  Intf_fields* ap_IF,
452  int vb,
453  bool a_lerpFlag);
454 
463  int IntfCheckDimensionsConsistency(Intf_fields ImgFields1, Intf_fields ImgFields2);
464 
474  FLTNB* IntfLoadImageFromScratch( const string& a_pathToHeaderFile,
475  Intf_fields* ap_ImgFields,
476  int vb );
487  int IntfWriteImageFromIntfFields( const string& a_pathToImg,
488  FLTNB* ap_ImgMatrix,
489  Intf_fields Img_fields,
490  int vb );
491 
503  int IntfReadImage( const string& a_pathToHeaderFile,
504  FLTNB* ap_ImgMatrix,
506  int vb,
507  bool a_lerpFlag);
508 
523  int IntfReadImage( const string& a_pathToHeaderFile,
524  FLTNB**** a4p_ImgMatrix,
526  int vb,
527  bool a_lerpFlag);
528 
543  int IntfReadImgDynCoeffFile(const string& a_pathToHeaderFile,
544  FLTNB** a2p_ImgMatrix,
546  int a_nbFbases,
547  int vb,
548  bool a_lerpFlag);
549 
550 
551  // -------------------------------------------------------------------
552  // ----- FUNCTIONS DEDICATED TO IMAGE DATA READING/WRITING -----
553 
567  int IntfCheckConsistency(Intf_fields* ap_IF, oImageDimensionsAndQuantification* ap_ID, int vb, int a_lerpFlag);
568 
584  ifstream* ap_iFile,
585  FLTNB* ap_outImgMatrix,
586  FLTNB* ap_inImgMatrix,
587  uint32_t* a_offset,
588  int a_nbVox,
589  int vb);
590 
608  template <class T>
609  int IntfReadData(Intf_fields a_IF,
610  ifstream* ap_iFile,
611  FLTNB* ap_outImgMatrix,
612  FLTNB* ap_inImgMatrix,
613  uint32_t* a_offset,
614  int a_nbVox,
615  int vb,
616  T* bytes);
617 
618 
632  int ImageInterpolation(FLTNB *ap_iImg, FLTNB *ap_oImg,
633  const uint32_t ap_iDimVox[3], const uint32_t ap_oDimVox[3],
634  const FLTNB ap_iSizeVox[3], const FLTNB ap_oSizeVox[3],
635  const FLTNB ap_iOffVox[3], const FLTNB ap_oOffVox[3] );
636 
637 
650  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, const Intf_fields& ap_IntfF, int vb);
651 
665  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
666 
683  int IntfWriteProjFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, Intf_fields a_Imgfields, int vb);
684 
702  int IntfWriteImgDynCoeffFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int a_nbParImgs, int vb);
703 
719  int IntfWriteImgFile(const string& a_pathToImg, FLTNB**** a4p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
720 
731  int IntfWriteImage(const string& a_pathToImg, FLTNB* ap_outImgMtx, uint32_t a_dim, int vb);
732 
743  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB** a2p_outImgMtx, uint32_t ap_dim[2], int vb);
744 
755  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB**** a4p_outImgMtx, uint32_t ap_dim[4], int vb);
756 
768  int IntfWriteData(ofstream* ap_oFile, FLTNB* ap_outImgMatrix, int a_nbVox, int vb);
769 
770 
771  // -------------------------------------------------------------------
772  // ----- FUNCTIONS DEDICATED TO INTERFILE HEADER KEYs DECODING/PARSING/READING -----
773 
786  int IntfIsMHD(string a_pathToFile, vector<string> &ap_lPathToImgs);
787 
798  int IntfWriteMHD(const string& a_pathToMhd, const vector<string> &ap_lPathToImgs, Intf_fields a_IntfF, oImageDimensionsAndQuantification* ap_ID, int vb);
799 
817  int IntfReadHeader(const string& a_pathToHeaderFile, Intf_fields* ap_IntfFields, int vb);
818 
830  int IntfWriteHeaderMainData(const string& a_path, const Intf_fields& ap_IntfF, int vb);
831 
842  int IntfWriteHeaderImgData(ofstream &ap_ofile, const Intf_fields& ap_IntfF, int vb);
843 
849  void IntfKeyInitFields(Intf_fields* ap_IF);
850 
860 
866  void IntfKeyPrintFields(Intf_fields a_IF);
867 
880  int IntfRecoverKey(Intf_key* ap_Key, const string& a_line);
881 
890  int IntfCheckKeyMatch(Intf_key a_Key, const string& a_field);
891 
898  int IntfKeyIsArray(Intf_key ap_Key);
899 
906  int IntfKeyGetArrayNbElts(Intf_key ap_Key);
907 
914  int IntfKeyGetMaxArrayKey(Intf_key ap_Key);
915 
924  template <typename T> int IntfKeyGetArrayElts(Intf_key a_Key, T* ap_return);
925 
934  string IntfKeyGetEndianStr(int a_val);
935 
944  string IntfKeyGetModalityStr(int a_modalityIdx);
945 
952  int IntfKeyGetInputImgDataType(const string& a_str);
953 
961 
967  string IntfKeyGetPixTypeStr();
968 
969 
970  // -------------------------------------------------------------------
971  // ----- NOT IMPLEMENTED KEY FUNCTION -----
972 
982 
992  int IntfGetVoxIdxSHTOrientation(Intf_fields a_IF, int a_voxId);
993 
994 
995  // -------------------------------------------------------------------
996  // ----- SOME UTILITY FUNCTIONS -----
997 
1004  void IntfAllocInterpImg(FLTNB** a2p_img, Intf_fields a_IF);
1005 
1012  void GetUserEndianness();
1013 
1019  void IntfEraseSpaces(string* input_str);
1020 
1027  void IntfToLowerCase(string* ap_str);
1028 
1033  string IntfKeyGetPatientNameTag();
1034 
1041  template <class Type> void SwapBytes(Type *ap_type);
1042 
1043 #endif
vector< FLTNB > image_start_time
This header file is mainly used to declare some macro definitions and all includes needed from the st...
int IntfCheckKeyMatch(Intf_key a_Key, const string &a_field)
Check if the key matches the string passed in parameter.
FLTNB first_angle
Declaration of class oImageDimensionsAndQuantification.
string direction_rotation
FLTNB quant_units
string path_to_image
uint8_t nb_bytes_pixel
FLTNB vox_size[3]
#define FLTNB
Definition: gVariables.hh:81
int IntfWriteData(ofstream *ap_oFile, FLTNB *ap_outImgMatrix, int a_nbVox, int vb)
Write the content of the image matrix in the file pointed by ofstream.
int IntfKeyGetRecurringValueFromFile(const string &a_pathToHeader, const string &a_key, T *ap_return, int a_nbElts, int a_mandatoryFlag, uint16_t a_nbOccurrences)
uint32_t nb_total_imgs
string IntfKeyGetModalityStr(int a_modalityIdx)
Convert the integer provided in parameter to the string related to the corresponding modality as de...
int IntfReadData(Intf_fields a_IF, ifstream *ap_iFile, FLTNB *ap_outImgMatrix, FLTNB *ap_inImgMatrix, uint32_t *a_offset, int a_nbVox, int vb, T *bytes)
Templated function which read an image voxel by voxel and store it in the ap_outImgMtx image matrix...
FLTNB rescale_intercept
int8_t pat_rotation
FLTNB vox_offset[3]
string IntfKeyGetEndianStr(int a_val)
return the endian string corresponding to the value passed in parameter (see module INTF_ENDIANNESS)...
FLTNB extent_rotation
void IntfKeyPrintFields(Intf_fields a_IF)
Print all the keys of the Intf_fields structure passed in parameter, as well as their values for debu...
string kcase
uint8_t nb_dims
int IntfKeyGetValueFromFile(const string &a_pathToHeader, const string &a_key, T *ap_return, int a_nbElts, int a_mandatoryFlag)
Look for "a_nbElts" elts in the "a_pathToHeader" interfile header matching the "a_keyword" key passed...
Definition: oInterfileIO.cc:63
FLTNB cvox_offset[3]
int IntfCheckDimensionsConsistency(Intf_fields ImgFields1, Intf_fields ImgFields2)
FLTNB bed_relative_position
vector< FLTNB > frame_group_pause
void IntfEraseSpaces(string *input_str)
Erase space, blank characters ((t,r,n)), and '!' before and after the characters in the string passed...
uint32_t cmtx_size[3]
int ImageInterpolation(FLTNB *ap_iImg, FLTNB *ap_oImg, const uint32_t ap_iDimVox[3], const uint32_t ap_oDimVox[3], const FLTNB ap_iSizeVox[3], const FLTNB ap_oSizeVox[3], const FLTNB ap_iOffVox[3], const FLTNB ap_oOffVox[3])
string nb_format
int nb_bed_positions
void IntfToLowerCase(string *ap_str)
Set all characters of the string passed in parameter to lower case.
int IntfGetVoxIdxSHTOrientation(Intf_fields a_IF, int a_voxId)
Compute a voxel index corresponding to the default orientation (Sup/Hin/Trans) from the orientation...
uint16_t nb_resp_gates
uint16_t nb_card_gates
int IntfReadHeader(const string &a_pathToHeaderFile, Intf_fields *ap_IntfFields, int vb)
Read an Interfile header.
int8_t pat_orientation
int IntfIsMHD(string a_pathToFile, vector< string > &ap_lPathToImgs)
Check if the string in argument contains the path to a Interfile metaheader.
uint8_t endianness
int IntfKeyIsArray(Intf_key ap_Key)
Check if the key passed in parameter is an array (contains brackets '{' and '}' ) ...
int IntfWriteImage(const string &a_pathToImg, FLTNB *ap_outImgMtx, uint32_t a_dim, int vb)
Write Interfile raw data whose path is provided in parameter, using image matrix provided in paramete...
FLTNB rescale_slope
int8_t slice_orientation
int IntfKeyGetInputImgDataType(const string &a_str)
Get the image data type corresponding to the image metadata passed in parameter.
int IntfReadProjectionImage(const string &a_pathToHeaderFile, FLTNB *ap_ImgMatrix, Intf_fields *ap_IF, int vb, bool a_lerpFlag)
Main function which reads a projection Interfile 3D projection image and store its content in the pro...
FLTNB * IntfLoadImageFromScratch(const string &a_pathToHeaderFile, Intf_fields *ap_ImgFields, int vb)
int IntfWriteHeaderImgData(ofstream &ap_ofile, const Intf_fields &ap_IntfF, int vb)
Declaration of class sScannerManager.
int IntfKeyGetArrayNbElts(Intf_key ap_Key)
Return the number of elts in an Interfile array Key.
void IntfKeyInitFields(Intf_fields *ap_IF)
Init the file of an Interfile fields structure passed in parameter to their default values...
vector< FLTNB > image_duration
bool bed_position_provided
uint32_t mtx_size[7]
string korig
void IntfAllocInterpImg(FLTNB **a2p_img, Intf_fields a_IF)
Allocate memory for an image matrix to recover an image to interpolate.
uint16_t nb_projections
int IntfKeyGetPatOrientation(Intf_fields ap_IF)
Get the complete patient orientation from an Intf_fields structure according to the values of keys 's...
uint32_t data_offset
string klcase
uint16_t nb_time_frames
Interfile key elements. This structure is used to recover and process the elements of an Interfile ...
uint16_t ctr_to_ctr_separation
int IntfKeyGetArrayElts(Intf_key a_Key, T *ap_return)
Get all the elements in an array key in a templated array passed in parameter. It assumes the retur...
int IntfGetPixelTypeAndReadData(Intf_fields a_IF, ifstream *ap_iFile, FLTNB *ap_outImgMatrix, FLTNB *ap_inImgMatrix, uint32_t *a_offset, int a_nbVox, int vb)
The purpose of this function is to call the templated ReadData() function with the data type correspo...
int IntfWriteProjFile(const string &a_pathToImg, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, Intf_fields a_Imgfields, int vb)
Function dedicated to Interfile image writing for projected data.
int IntfWriteImgDynCoeffFile(const string &a_pathToImg, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int a_nbParImgs, int vb)
Function dedicated to Interfile image writing for dynamic coefficients images.
uint32_t nb_time_windows
Interfile fields. This structure contains all the Interfile keys currently managed by CASToR Decl...
Declaration of class sOutputManager.
uint16_t nb_detector_heads
string kvalue
string IntfKeyGetPatientNameTag()
Recover datafile name(s) stored in sOutputManager in one string.
FLTNB cvox_size[3]
int IntfWriteMHD(const string &a_pathToMhd, const vector< string > &ap_lPathToImgs, Intf_fields a_IntfF, oImageDimensionsAndQuantification *ap_ID, int vb)
Write an Interfile meta header at the path provided in parameter, using the field stack provided in p...
This class is designed to manage all dimensions and quantification related stuff. ...
FLTNB study_duration
This file is used for all kind of different functions designed for options parsing and ASCII file rea...
void IntfKeySetFieldsOutput(Intf_fields *ap_IF, oImageDimensionsAndQuantification *ap_ID)
Init the keys of the Interfile header of an image to be written on disk.
string projection_angles
int IntfRecoverKey(Intf_key *ap_Key, const string &a_line)
Process the line passed in parameter and write the key information in the ap_Key Intf_key member stru...
string process_status
uint32_t nb_energy_windows
int IntfKeyGetMaxArrayKey(Intf_key ap_Key)
Return the maximum value from an array key (key value contains brackets '{,,}' )
int IntfKeyGetOutputImgDataType(oImageDimensionsAndQuantification *ap_ID)
uint32_t nb_img_in_frame_groups
int IntfWriteHeaderMainData(const string &a_path, const Intf_fields &ap_IntfF, int vb)
void SwapBytes(Type *ap_type)
Use std::reverse to swap the bits of a variable of any type.
int IntfReadImgDynCoeffFile(const string &a_pathToHeaderFile, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int a_nbFbases, int vb, bool a_lerpFlag)
Function dedicated to Interfile image reading for dynamic coefficients images.
string IntfKeyGetPixTypeStr()
Return the string corresponding to the nb of bytes in the type FLTNB.
int IntfReadImage(const string &a_pathToHeaderFile, FLTNB *ap_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int vb, bool a_lerpFlag)
Main function dedicated to Interfile 3D image loading.
int IntfWriteImgFile(const string &a_pathToImg, FLTNB *ap_ImgMatrix, const Intf_fields &ap_IntfF, int vb)
Main function dedicated to Interfile 3D image writing. Recover image information from a provided In...
void GetUserEndianness()
Check user/host computer endianness and write it to the global variable User_Endianness.
FLTNB slice_thickness_mm
bool is_mtx_size_different
string originating_system
int IntfCheckConsistency(Intf_fields *ap_IF, oImageDimensionsAndQuantification *ap_ID, int vb, int a_lerpFlag)
Check if the mandatory fields have been initialize in the ap_IF structure, and check consistencies wi...
int IntfWriteImageFromIntfFields(const string &a_pathToImg, FLTNB *ap_ImgMatrix, Intf_fields Img_fields, int vb)