CASToR  3.2
Tomographic Reconstruction (PET/SPECT/CT)
include/image/oInterfileIO.hh
Go to the documentation of this file.
1 
9 #ifndef OINTERFILEIO_HH
10 #define OINTERFILEIO_HH 1
11 
13 #include "sScannerManager.hh"
14 #include "vDataFile.hh"
15 
16 // ---------------------------------------------------------------------
34 #define INTF_BIG_ENDIAN 0
35 
36 #define INTF_LITTLE_ENDIAN 1
37 
47 #define INTF_IMG_STATIC 0
48 
49 #define INTF_IMG_DYNAMIC 1
50 
51 #define INTF_IMG_PET 2
52 
53 #define INTF_IMG_SPECT 3
54 
55 #define INTF_IMG_GATED 4
56 
57 #define INTF_IMG_GSPECT 5
58 
59 #define INTF_IMG_UNKNOWN 6
60 
75 #define INTF_LERP_DISABLED false
76 
77 #define INTF_LERP_ENABLED true
78 
90 #define BIT_str "bit"
91 
92 #define UINT32_str "unsigned integer"
93 
94 #define INT32_str "signed integer"
95 
96 #define FLT32_str "short float"
97 #define FLT32_str2 "float"
98 
99 #define FLT64_str "long float"
100 
101 #define LONGDOUBLE_str "long long float"
102 
103 #define ASCII_str "ASCII"
104 
115 #define INTF_SUPINE_HEADIN_TRANSAXIAL 0
116 
117 #define INTF_SUPINE_HEADIN_SAGITTAL 1
118 
119 #define INTF_SUPINE_HEADIN_CORONAL 2
120 
121 #define INTF_SUPINE_FEETIN_TRANSAXIAL 3
122 
123 #define INTF_SUPINE_FEETIN_SAGITTAL 4
124 
125 #define INTF_SUPINE_FEETIN_CORONAL 5
126 
127 #define INTF_PRONE_HEADIN_TRANSAXIAL 6
128 
129 #define INTF_PRONE_HEADIN_SAGITTAL 7
130 
131 #define INTF_PRONE_HEADIN_CORONAL 8
132 
133 #define INTF_PRONE_FEETIN_TRANSAXIAL 9
134 
135 #define INTF_PRONE_FEETIN_SAGITTAL 10
136 
137 #define INTF_PRONE_FEETIN_CORONAL 11
138 
151 #define INTF_TRANSVERSE 0
152 
153 #define INTF_CORONAL 1
154 
155 #define INTF_SAGITTAL 2
156 
157 #define INTF_OTHER 3
158 
171 #define INTF_SUPINE 0
172 
173 #define INTF_PRONE 1
174 
187 #define INTF_HEADIN 0
188 
189 #define INTF_FEETIN 1
190 
201 struct Intf_key
202 {
203  string korig;
204  string kcase;
205  string klcase;
206  string kvalue;
207 };
211 // ---------------------------------------------------------------------
218 struct Intf_fields
219 {
220  string path_to_image;
223  string originating_system;
226  uint8_t endianness;
229  uint32_t data_offset;
232  string nb_format;
235  uint8_t nb_dims;
238  uint32_t mtx_size[7];
242  FLTNB vox_offset[3];
245 // float vox_size[3]; /*!< Voxel dimensions in mm.\n
246  FLTNB vox_size[3];
249 // float slice_thickness_mm ; /*!< Read from the key 'slice thickness (pixels)'.\n
250  FLTNB slice_thickness_mm ;
253  int nb_bed_positions;
254  FLTNB bed_relative_position;
255  bool bed_position_provided;
257  uint16_t ctr_to_ctr_separation;
260  uint16_t nb_time_frames ;
263  uint16_t nb_resp_gates ;
266  uint16_t nb_card_gates ;
269  uint32_t nb_total_imgs;
272  uint8_t nb_bytes_pixel;
275  int8_t slice_orientation;
279  int8_t pat_rotation;
283  int8_t pat_orientation;
287  FLTNB rescale_slope;
290  FLTNB quant_units;
293  FLTNB rescale_intercept;
296  uint32_t cmtx_size[3];
299  FLTNB cvox_size[3];
302  FLTNB cvox_offset[3];
306  bool is_mtx_size_different;
309  int data_type;
313 // float study_duration; /*!< Acquisition duration.\n
314  FLTNB study_duration;
317  vector<FLTNB> image_duration;
321  vector<FLTNB> image_start_time;
325  uint32_t nb_time_windows;
330  string process_status;
333  // --- Fields related to an image in a group --- // // todo
334 
335  uint32_t nb_img_in_frame_groups;
338 // float image_pause; /*!< Pause between time windows ?\n
339  //FLTNB image_pause;
345  vector<FLTNB> frame_group_pause;
348  // --- SPECT and projection related data --- //
349  uint16_t nb_detector_heads;
352  uint32_t nb_energy_windows;
355  uint16_t nb_projections;
358 // float extent_rotation; /*!< Angular span ex: 180, 360.\n
359  FLTNB extent_rotation;
362  string direction_rotation;
365 // float first_angle; /*!< Angle of the first view.\n
366  FLTNB first_angle;
369  string projection_angles;
372  string radius;
374 };
379 #include "gVariables.hh"
380 #include "sOutputManager.hh"
381 #include "gOptions.hh"
382 
383  // -------------------------------------------------------------------
384  // ----- "PUBLIC" FUNCTIONS WHICH COULD AND SHOULD BE ANYWHERE IN THE CODE WHERE INTERFILE IS REQUIRED -----
385 
398  template <typename T> int IntfKeyGetValueFromFile(const string& a_pathToHeader, const string& a_key, T* ap_return, int a_nbElts, int a_mandatoryFlag);
399 
414  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);
415 
427  int IntfReadProjectionImage( const string& a_pathToHeaderFile,
428  FLTNB* ap_ImgMatrix,
429  Intf_fields* ap_IF,
430  int vb,
431  bool a_lerpFlag);
432 
441  int IntfCheckDimensionsConsistency(Intf_fields ImgFields1, Intf_fields ImgFields2);
442 
452  FLTNB* IntfLoadImageFromScratch( const string& a_pathToHeaderFile,
453  Intf_fields* ap_ImgFields,
454  int vb );
465  int IntfWriteImageFromIntfFields( const string& a_pathToImg,
466  FLTNB* ap_ImgMatrix,
467  Intf_fields Img_fields,
468  int vb );
469 
481  int IntfReadImage( const string& a_pathToHeaderFile,
482  FLTNB* ap_ImgMatrix,
484  int vb,
485  bool a_lerpFlag);
486 
501  int IntfReadImage( const string& a_pathToHeaderFile,
502  FLTNB**** a4p_ImgMatrix,
504  int vb,
505  bool a_lerpFlag);
516  int IntfReadAdditionalData( const string& a_pathToHeaderFile,
517  FLTNB* ap_DataMatrix,
518  int64_t a_matrixSize,
519  int vb);
534  int IntfReadImgDynCoeffFile(const string& a_pathToHeaderFile,
535  FLTNB** a2p_ImgMatrix,
537  int a_nbFbases,
538  int vb,
539  bool a_lerpFlag);
540 
541 
542  // -------------------------------------------------------------------
543  // ----- FUNCTIONS DEDICATED TO IMAGE DATA READING/WRITING -----
544 
558  int IntfCheckConsistency(Intf_fields* ap_IF, oImageDimensionsAndQuantification* ap_ID, int vb, int a_lerpFlag);
559 
575  ifstream* ap_iFile,
576  FLTNB* ap_outImgMatrix,
577  FLTNB* ap_inImgMatrix,
578  uint32_t* a_offset,
579  int64_t a_nbVox,
580  int vb);
581 
599  template <class T>
600  int IntfReadData(Intf_fields a_IF,
601  ifstream* ap_iFile,
602  FLTNB* ap_outImgMatrix,
603  FLTNB* ap_inImgMatrix,
604  uint32_t* a_offset,
605  int64_t a_nbVox,
606  int vb,
607  T* bytes);
608 
609 
623  int ImageInterpolation(FLTNB *ap_iImg, FLTNB *ap_oImg,
624  const uint32_t ap_iDimVox[3], const uint32_t ap_oDimVox[3],
625  const FLTNB ap_iSizeVox[3], const FLTNB ap_oSizeVox[3],
626  const FLTNB ap_iOffVox[3], const FLTNB ap_oOffVox[3] );
627 
628 
641  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, const Intf_fields& ap_IntfF, int vb);
642 
656  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
657 
674  int IntfWriteProjFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, Intf_fields a_Imgfields, int vb);
675 
694  int IntfWriteImgDynCoeffFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int a_nbParImgs, int vb, bool a_mergeDynImgFlag=false);
695 
711  int IntfWriteImgFile(const string& a_pathToImg, FLTNB**** a4p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
712 
724  int IntfWriteWholeDynBasisCoeffImgFile(const string& a_pathToImg, FLTNB**** a4p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
725 
736  int IntfWriteImage(const string& a_pathToImg, FLTNB* ap_outImgMtx, uint32_t a_dim, int vb);
737 
748  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB** a2p_outImgMtx, uint32_t ap_dim[2], int vb);
749 
760  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB**** a4p_outImgMtx, uint32_t ap_dim[4], int vb);
761 
773  int IntfWriteData(ofstream* ap_oFile, FLTNB* ap_outImgMatrix, int a_nbVox, int vb);
774 
782  int IntfWriteContentOfInputDataHeaderIntoInterfileHeader(ofstream &ap_ofile, int vb);
783 
784 
785  // -------------------------------------------------------------------
786  // ----- FUNCTIONS DEDICATED TO INTERFILE HEADER KEYs DECODING/PARSING/READING -----
787 
800  int IntfIsMHD(string a_pathToFile, vector<string> &ap_lPathToImgs);
801 
812  int IntfWriteMHD(const string& a_pathToMhd, const vector<string> &ap_lPathToImgs, Intf_fields a_IntfF, oImageDimensionsAndQuantification* ap_ID, int vb);
813 
831  int IntfReadHeader(const string& a_pathToHeaderFile, Intf_fields* ap_IntfFields, int vb);
832 
844  int IntfWriteHeaderMainData(const string& a_path, const Intf_fields& ap_IntfF, int vb);
845 
856  int IntfWriteHeaderImgData(ofstream &ap_ofile, const Intf_fields& ap_IntfF, int vb);
857 
863  void IntfKeyInitFields(Intf_fields* ap_IF);
864 
874 
880  void IntfKeyPrintFields(Intf_fields a_IF);
881 
894  int IntfRecoverKey(Intf_key* ap_Key, const string& a_line);
895 
904  int IntfCheckKeyMatch(Intf_key a_Key, const string& a_field);
905 
912  int IntfKeyIsArray(Intf_key ap_Key);
913 
920  int IntfKeyGetArrayNbElts(Intf_key ap_Key);
921 
928  int IntfKeyGetMaxArrayKey(Intf_key ap_Key);
929 
938  template <typename T> int IntfKeyGetArrayElts(Intf_key a_Key, T* ap_return);
939 
948  string IntfKeyGetEndianStr(int a_val);
949 
958  string IntfKeyGetModalityStr(int a_modalityIdx);
959 
966  int IntfKeyGetInputImgDataType(const string& a_str);
967 
975 
981  string IntfKeyGetPixTypeStr();
982 
983 
984  // -------------------------------------------------------------------
985  // ----- NOT IMPLEMENTED KEY FUNCTION -----
986 
996 
1006  int IntfGetVoxIdxSHTOrientation(Intf_fields a_IF, int a_voxId);
1007 
1008 
1009  // -------------------------------------------------------------------
1010  // ----- SOME UTILITY FUNCTIONS -----
1011 
1018  void IntfAllocInterpImg(FLTNB** a2p_img, Intf_fields a_IF);
1019 
1026  void GetUserEndianness();
1027 
1033  void IntfEraseSpaces(string* input_str);
1034 
1041  void IntfToLowerCase(string* ap_str);
1042 
1047  string IntfKeyGetPatientNameTag();
1048 
1055  template <class Type> void SwapBytes(Type *ap_type);
1056 
1057 #endif
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.
int IntfWriteImageFromIntfFields(const string &a_pathToImg, FLTNB *ap_ImgMatrix, Intf_fields Img_fields, int vb)
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 IntfKeyGetMaxArrayKey(Intf_key ap_Key)
Return the maximum value from an array key (key value contains brackets &#39;{,,}&#39; )
int IntfReadData(Intf_fields a_IF, ifstream *ap_iFile, FLTNB *ap_outImgMatrix, FLTNB *ap_inImgMatrix, uint32_t *a_offset, int64_t a_nbVox, int vb, T *bytes)
Templated function which read an image voxel by voxel and store it in the ap_outImgMtx image matrix...
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.
string IntfKeyGetModalityStr(int a_modalityIdx)
Convert the integer provided in parameter to the string related to the corresponding modality as de...
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 IntfKeyGetPatientNameTag()
Recover datafile name(s) stored in sOutputManager in one string.
int IntfReadAdditionalData(const string &a_pathToHeaderFile, FLTNB *ap_DataMatrix, int64_t a_matrixSize, int vb)
Main function dedicated to Interfile 3D additional data loading.
void IntfKeyInitFields(Intf_fields *ap_IF)
Init the file of an Interfile fields structure passed in parameter to their default values...
void SwapBytes(Type *ap_type)
void IntfEraseSpaces(string *input_str)
Erase space, blank characters ((t,r,n)), and &#39;!&#39; before and after the characters in the string passed...
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...
string IntfKeyGetPixTypeStr()
Return the string corresponding to the nb of bytes in the type FLTNB.
int IntfKeyGetOutputImgDataType(oImageDimensionsAndQuantification *ap_ID)
int IntfKeyGetArrayNbElts(Intf_key ap_Key)
Return the number of elts in an Interfile array Key.
void IntfAllocInterpImg(FLTNB **a2p_img, Intf_fields a_IF)
Allocate memory for an image matrix to recover an image to interpolate.
int IntfIsMHD(string a_pathToFile, vector< string > &ap_lPathToImgs)
Check if the string in argument contains the path to a Interfile metaheader.
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...
void IntfKeySetFieldsOutput(Intf_fields *ap_IF, oImageDimensionsAndQuantification *ap_ID)
Init the keys of the Interfile header of an image to be written on disk.
int IntfGetVoxIdxSHTOrientation(Intf_fields a_IF, int a_voxId)
Compute a voxel index corresponding to the default orientation (Sup/Hin/Trans) from the orientation...
int IntfWriteImgDynCoeffFile(const string &a_pathToImg, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int a_nbParImgs, int vb, bool a_mergeDynImgFlag=false)
int IntfCheckDimensionsConsistency(Intf_fields ImgFields1, Intf_fields ImgFields2)
int IntfGetPixelTypeAndReadData(Intf_fields a_IF, ifstream *ap_iFile, FLTNB *ap_outImgMatrix, FLTNB *ap_inImgMatrix, uint32_t *a_offset, int64_t a_nbVox, int vb)
The purpose of this function is to call the templated ReadData() function with the data type correspo...
int IntfWriteHeaderMainData(const string &a_path, const Intf_fields &ap_IntfF, int vb)
int IntfKeyGetRecurringValueFromFile(const string &a_pathToHeader, const string &a_key, T *ap_return, int a_nbElts, int a_mandatoryFlag, uint16_t a_nbOccurrences)
void GetUserEndianness()
Check user/host computer endianness and write it to the global variable User_Endianness.
int IntfReadHeader(const string &a_pathToHeaderFile, Intf_fields *ap_IntfFields, int vb)
Read an Interfile header.
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...
FLTNB * IntfLoadImageFromScratch(const string &a_pathToHeaderFile, Intf_fields *ap_ImgFields, int vb)
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...
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...
int IntfWriteWholeDynBasisCoeffImgFile(const string &a_pathToImg, FLTNB ****a4p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int vb)
Main function dedicated to Interfile 6D (dynamic dims + 3D ) image writing of basis function coeffici...
void IntfToLowerCase(string *ap_str)
Set all characters of the string passed in parameter to lower case.
int IntfWriteHeaderImgData(ofstream &ap_ofile, const Intf_fields &ap_IntfF, int vb)
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.
Interfile key elements. This structure is used to recover and process the elements of an Interfile ...
int IntfWriteContentOfInputDataHeaderIntoInterfileHeader(ofstream &ap_ofile, int vb)
int IntfKeyIsArray(Intf_key ap_Key)
Check if the key passed in parameter is an array (contains brackets &#39;{&#39; and &#39;}&#39; ) ...
int IntfKeyGetPatOrientation(Intf_fields ap_IF)
Get the complete patient orientation from an Intf_fields structure according to the values of keys &#39;s...
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...
Interfile fields. This structure contains all the Interfile keys currently managed by CASToR Decl...
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...
int IntfKeyGetInputImgDataType(const string &a_str)
Get the image data type corresponding to the image metadata passed in parameter.
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...
This class is designed to manage all dimensions and quantification related stuff. ...
int IntfCheckKeyMatch(Intf_key a_Key, const string &a_field)
Check if the key matches the string passed in parameter.
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.
string IntfKeyGetEndianStr(int a_val)
return the endian string corresponding to the value passed in parameter (see module INTF_ENDIANNESS)...
Declaration of class oImageDimensionsAndQuantification.