CASToR  3.0
Tomographic Reconstruction (PET/SPECT/CT)
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-2019 all CASToR contributors listed below:
18 
19  --> Didier BENOIT, Claude COMTAT, Marina FILIPOVIC, Thibaut MERLIN, Mael MILLARDET, Simon STUTE, Valentin VIELZEUF
20 
21 This is CASToR version 3.0.
22 */
23 
31 #ifndef OINTERFILEIO_HH
32 #define OINTERFILEIO_HH 1
33 
35 #include "sScannerManager.hh"
36 
37 // ---------------------------------------------------------------------
55 #define INTF_BIG_ENDIAN 0
56 
57 #define INTF_LITTLE_ENDIAN 1
58 
68 #define INTF_IMG_STATIC 0
69 
70 #define INTF_IMG_DYNAMIC 1
71 
72 #define INTF_IMG_PET 2
73 
74 #define INTF_IMG_SPECT 3
75 
76 #define INTF_IMG_GATED 4
77 
78 #define INTF_IMG_GSPECT 5
79 
80 #define INTF_IMG_UNKNOWN 6
81 
96 #define INTF_LERP_DISABLED false
97 
98 #define INTF_LERP_ENABLED true
99 
111 #define BIT_str "bit"
112 
113 #define UINT32_str "unsigned integer"
114 
115 #define INT32_str "signed integer"
116 
117 #define FLT32_str "short float"
118 #define FLT32_str2 "float"
119 
120 #define FLT64_str "long float"
121 
122 #define LONGDOUBLE_str "long long float"
123 
124 #define ASCII_str "ASCII"
125 
136 #define INTF_SUPINE_HEADIN_TRANSAXIAL 0
137 
138 #define INTF_SUPINE_HEADIN_SAGITTAL 1
139 
140 #define INTF_SUPINE_HEADIN_CORONAL 2
141 
142 #define INTF_SUPINE_FEETIN_TRANSAXIAL 3
143 
144 #define INTF_SUPINE_FEETIN_SAGITTAL 4
145 
146 #define INTF_SUPINE_FEETIN_CORONAL 5
147 
148 #define INTF_PRONE_HEADIN_TRANSAXIAL 6
149 
150 #define INTF_PRONE_HEADIN_SAGITTAL 7
151 
152 #define INTF_PRONE_HEADIN_CORONAL 8
153 
154 #define INTF_PRONE_FEETIN_TRANSAXIAL 9
155 
156 #define INTF_PRONE_FEETIN_SAGITTAL 10
157 
158 #define INTF_PRONE_FEETIN_CORONAL 11
159 
172 #define INTF_TRANSVERSE 0
173 
174 #define INTF_CORONAL 1
175 
176 #define INTF_SAGITTAL 2
177 
178 #define INTF_OTHER 3
179 
192 #define INTF_SUPINE 0
193 
194 #define INTF_PRONE 1
195 
208 #define INTF_HEADIN 0
209 
210 #define INTF_FEETIN 1
211 
222 struct Intf_key
223 {
224  string korig;
225  string kcase;
226  string klcase;
227  string kvalue;
228 };
232 // ---------------------------------------------------------------------
240 {
241  string path_to_image;
247  uint8_t endianness;
250  uint32_t data_offset;
253  string nb_format;
256  uint8_t nb_dims;
259  uint32_t mtx_size[7];
263  FLTNB vox_offset[3];
266 // float vox_size[3]; /*!< Voxel dimensions in mm.\n
267  FLTNB vox_size[3];
270 // float slice_thickness_mm ; /*!< Read from the key 'slice thickness (pixels)'.\n
281  uint16_t nb_time_frames ;
284  uint16_t nb_resp_gates ;
287  uint16_t nb_card_gates ;
290  uint32_t nb_total_imgs;
293  uint8_t nb_bytes_pixel;
300  int8_t pat_rotation;
317  uint32_t cmtx_size[3];
320  FLTNB cvox_size[3];
323  FLTNB cvox_offset[3];
330  int data_type;
334 // float study_duration; /*!< Acquisition duration.\n
338  vector<FLTNB> image_duration;
342  vector<FLTNB> image_start_time;
346  uint32_t nb_time_windows;
351  string process_status;
354  // --- Fields related to an image in a group --- // // todo
355 
359 // float image_pause; /*!< Pause between time windows ?\n
360  //FLTNB image_pause;
366  vector<FLTNB> frame_group_pause;
369  // --- SPECT and projection related data --- //
370  uint16_t nb_detector_heads;
373  uint32_t nb_energy_windows;
376  uint16_t nb_projections;
379 // float extent_rotation; /*!< Angular span ex: 180, 360.\n
386 // float first_angle; /*!< Angle of the first view.\n
393  string radius;
395 };
400 #include "gVariables.hh"
401 #include "sOutputManager.hh"
402 #include "gOptions.hh"
403 
404  // -------------------------------------------------------------------
405  // ----- "PUBLIC" FUNCTIONS WHICH COULD AND SHOULD BE ANYWHERE IN THE CODE WHERE INTERFILE IS REQUIRED -----
406 
419  template <typename T> int IntfKeyGetValueFromFile(const string& a_pathToHeader, const string& a_key, T* ap_return, int a_nbElts, int a_mandatoryFlag);
420 
435  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);
436 
448  int IntfReadProjectionImage( const string& a_pathToHeaderFile,
449  FLTNB* ap_ImgMatrix,
450  Intf_fields* ap_IF,
451  int vb,
452  bool a_lerpFlag);
453 
462  int IntfCheckDimensionsConsistency(Intf_fields ImgFields1, Intf_fields ImgFields2);
463 
473  FLTNB* IntfLoadImageFromScratch( const string& a_pathToHeaderFile,
474  Intf_fields* ap_ImgFields,
475  int vb );
486  int IntfWriteImageFromIntfFields( const string& a_pathToImg,
487  FLTNB* ap_ImgMatrix,
488  Intf_fields Img_fields,
489  int vb );
490 
502  int IntfReadImage( const string& a_pathToHeaderFile,
503  FLTNB* ap_ImgMatrix,
505  int vb,
506  bool a_lerpFlag);
507 
522  int IntfReadImage( const string& a_pathToHeaderFile,
523  FLTNB**** a4p_ImgMatrix,
525  int vb,
526  bool a_lerpFlag);
527 
542  int IntfReadImgDynCoeffFile(const string& a_pathToHeaderFile,
543  FLTNB** a2p_ImgMatrix,
545  int a_nbFbases,
546  int vb,
547  bool a_lerpFlag);
548 
549 
550  // -------------------------------------------------------------------
551  // ----- FUNCTIONS DEDICATED TO IMAGE DATA READING/WRITING -----
552 
566  int IntfCheckConsistency(Intf_fields* ap_IF, oImageDimensionsAndQuantification* ap_ID, int vb, int a_lerpFlag);
567 
583  ifstream* ap_iFile,
584  FLTNB* ap_outImgMatrix,
585  FLTNB* ap_inImgMatrix,
586  uint32_t* a_offset,
587  int a_nbVox,
588  int vb);
589 
607  template <class T>
608  int IntfReadData(Intf_fields a_IF,
609  ifstream* ap_iFile,
610  FLTNB* ap_outImgMatrix,
611  FLTNB* ap_inImgMatrix,
612  uint32_t* a_offset,
613  int a_nbVox,
614  int vb,
615  T* bytes);
616 
617 
631  int ImageInterpolation(FLTNB *ap_iImg, FLTNB *ap_oImg,
632  const uint32_t ap_iDimVox[3], const uint32_t ap_oDimVox[3],
633  const FLTNB ap_iSizeVox[3], const FLTNB ap_oSizeVox[3],
634  const FLTNB ap_iOffVox[3], const FLTNB ap_oOffVox[3] );
635 
636 
649  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, const Intf_fields& ap_IntfF, int vb);
650 
664  int IntfWriteImgFile(const string& a_pathToImg, FLTNB* ap_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
665 
682  int IntfWriteProjFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, Intf_fields a_Imgfields, int vb);
683 
702  int IntfWriteImgDynCoeffFile(const string& a_pathToImg, FLTNB** a2p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int a_nbParImgs, int vb, bool a_mergeDynImgFlag=false);
703 
719  int IntfWriteImgFile(const string& a_pathToImg, FLTNB**** a4p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
720 
732  int IntfWriteWholeDynBasisCoeffImgFile(const string& a_pathToImg, FLTNB**** a4p_ImgMatrix, oImageDimensionsAndQuantification* ap_ID, int vb);
733 
744  int IntfWriteImage(const string& a_pathToImg, FLTNB* ap_outImgMtx, uint32_t a_dim, int vb);
745 
756  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB** a2p_outImgMtx, uint32_t ap_dim[2], int vb);
757 
768  int IntfWriteImage(vector<string> ap_pathToImgs, FLTNB**** a4p_outImgMtx, uint32_t ap_dim[4], int vb);
769 
781  int IntfWriteData(ofstream* ap_oFile, FLTNB* ap_outImgMatrix, int a_nbVox, int vb);
782 
783 
784  // -------------------------------------------------------------------
785  // ----- FUNCTIONS DEDICATED TO INTERFILE HEADER KEYs DECODING/PARSING/READING -----
786 
799  int IntfIsMHD(string a_pathToFile, vector<string> &ap_lPathToImgs);
800 
811  int IntfWriteMHD(const string& a_pathToMhd, const vector<string> &ap_lPathToImgs, Intf_fields a_IntfF, oImageDimensionsAndQuantification* ap_ID, int vb);
812 
830  int IntfReadHeader(const string& a_pathToHeaderFile, Intf_fields* ap_IntfFields, int vb);
831 
843  int IntfWriteHeaderMainData(const string& a_path, const Intf_fields& ap_IntfF, int vb);
844 
855  int IntfWriteHeaderImgData(ofstream &ap_ofile, const Intf_fields& ap_IntfF, int vb);
856 
862  void IntfKeyInitFields(Intf_fields* ap_IF);
863 
873 
879  void IntfKeyPrintFields(Intf_fields a_IF);
880 
893  int IntfRecoverKey(Intf_key* ap_Key, const string& a_line);
894 
903  int IntfCheckKeyMatch(Intf_key a_Key, const string& a_field);
904 
911  int IntfKeyIsArray(Intf_key ap_Key);
912 
919  int IntfKeyGetArrayNbElts(Intf_key ap_Key);
920 
927  int IntfKeyGetMaxArrayKey(Intf_key ap_Key);
928 
937  template <typename T> int IntfKeyGetArrayElts(Intf_key a_Key, T* ap_return);
938 
947  string IntfKeyGetEndianStr(int a_val);
948 
957  string IntfKeyGetModalityStr(int a_modalityIdx);
958 
965  int IntfKeyGetInputImgDataType(const string& a_str);
966 
974 
980  string IntfKeyGetPixTypeStr();
981 
982 
983  // -------------------------------------------------------------------
984  // ----- NOT IMPLEMENTED KEY FUNCTION -----
985 
995 
1005  int IntfGetVoxIdxSHTOrientation(Intf_fields a_IF, int a_voxId);
1006 
1007 
1008  // -------------------------------------------------------------------
1009  // ----- SOME UTILITY FUNCTIONS -----
1010 
1017  void IntfAllocInterpImg(FLTNB** a2p_img, Intf_fields a_IF);
1018 
1025  void GetUserEndianness();
1026 
1032  void IntfEraseSpaces(string* input_str);
1033 
1040  void IntfToLowerCase(string* ap_str);
1041 
1046  string IntfKeyGetPatientNameTag();
1047 
1054  template <class Type> void SwapBytes(Type *ap_type);
1055 
1056 #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
int IntfWriteImgDynCoeffFile(const string &a_pathToImg, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int a_nbParImgs, int vb, bool a_mergeDynImgFlag=false)
FLTNB quant_units
string path_to_image
uint8_t nb_bytes_pixel
#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
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:61
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 &#39;!&#39; before and after the characters in the string passed...
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 &#39;{&#39; and &#39;}&#39; ) ...
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
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 &#39;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.
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.
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.
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...
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 &#39;{,,}&#39; )
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)