extra_nii_hdr.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. % Decode extra NIFTI header information into hdr.extra
  2. %
  3. % Usage: hdr = extra_nii_hdr(hdr)
  4. %
  5. % hdr can be obtained from load_nii_hdr
  6. %
  7. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  8. %
  9. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  10. %
  11. function hdr = extra_nii_hdr(hdr)
  12. switch hdr.dime.datatype
  13. case 1
  14. extra.NIFTI_DATATYPES = 'DT_BINARY';
  15. case 2
  16. extra.NIFTI_DATATYPES = 'DT_UINT8';
  17. case 4
  18. extra.NIFTI_DATATYPES = 'DT_INT16';
  19. case 8
  20. extra.NIFTI_DATATYPES = 'DT_INT32';
  21. case 16
  22. extra.NIFTI_DATATYPES = 'DT_FLOAT32';
  23. case 32
  24. extra.NIFTI_DATATYPES = 'DT_COMPLEX64';
  25. case 64
  26. extra.NIFTI_DATATYPES = 'DT_FLOAT64';
  27. case 128
  28. extra.NIFTI_DATATYPES = 'DT_RGB24';
  29. case 256
  30. extra.NIFTI_DATATYPES = 'DT_INT8';
  31. case 512
  32. extra.NIFTI_DATATYPES = 'DT_UINT16';
  33. case 768
  34. extra.NIFTI_DATATYPES = 'DT_UINT32';
  35. case 1024
  36. extra.NIFTI_DATATYPES = 'DT_INT64';
  37. case 1280
  38. extra.NIFTI_DATATYPES = 'DT_UINT64';
  39. case 1536
  40. extra.NIFTI_DATATYPES = 'DT_FLOAT128';
  41. case 1792
  42. extra.NIFTI_DATATYPES = 'DT_COMPLEX128';
  43. case 2048
  44. extra.NIFTI_DATATYPES = 'DT_COMPLEX256';
  45. otherwise
  46. extra.NIFTI_DATATYPES = 'DT_UNKNOWN';
  47. end
  48. switch hdr.dime.intent_code
  49. case 2
  50. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CORREL';
  51. case 3
  52. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST';
  53. case 4
  54. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST';
  55. case 5
  56. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ZSCORE';
  57. case 6
  58. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ';
  59. case 7
  60. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BETA';
  61. case 8
  62. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_BINOM';
  63. case 9
  64. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GAMMA';
  65. case 10
  66. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POISSON';
  67. case 11
  68. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NORMAL';
  69. case 12
  70. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_FTEST_NONC';
  71. case 13
  72. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHISQ_NONC';
  73. case 14
  74. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGISTIC';
  75. case 15
  76. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LAPLACE';
  77. case 16
  78. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_UNIFORM';
  79. case 17
  80. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TTEST_NONC';
  81. case 18
  82. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_WEIBULL';
  83. case 19
  84. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_CHI';
  85. case 20
  86. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_INVGAUSS';
  87. case 21
  88. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_EXTVAL';
  89. case 22
  90. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_PVAL';
  91. case 23
  92. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOGPVAL';
  93. case 24
  94. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LOG10PVAL';
  95. case 1001
  96. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_ESTIMATE';
  97. case 1002
  98. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_LABEL';
  99. case 1003
  100. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NEURONAME';
  101. case 1004
  102. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_GENMATRIX';
  103. case 1005
  104. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_SYMMATRIX';
  105. case 1006
  106. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DISPVECT';
  107. case 1007
  108. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_VECTOR';
  109. case 1008
  110. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_POINTSET';
  111. case 1009
  112. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_TRIANGLE';
  113. case 1010
  114. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_QUATERNION';
  115. case 1011
  116. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_DIMLESS';
  117. otherwise
  118. extra.NIFTI_INTENT_CODES = 'NIFTI_INTENT_NONE';
  119. end
  120. extra.NIFTI_INTENT_NAMES = hdr.hist.intent_name;
  121. if hdr.hist.sform_code > 0
  122. switch hdr.hist.sform_code
  123. case 1
  124. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT';
  125. case 2
  126. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT';
  127. case 3
  128. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_TALAIRACH';
  129. case 4
  130. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_MNI_152';
  131. otherwise
  132. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  133. end
  134. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  135. elseif hdr.hist.qform_code > 0
  136. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  137. switch hdr.hist.qform_code
  138. case 1
  139. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_SCANNER_ANAT';
  140. case 2
  141. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_ALIGNED_ANAT';
  142. case 3
  143. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_TALAIRACH';
  144. case 4
  145. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_MNI_152';
  146. otherwise
  147. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  148. end
  149. else
  150. extra.NIFTI_SFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  151. extra.NIFTI_QFORM_CODES = 'NIFTI_XFORM_UNKNOWN';
  152. end
  153. switch bitand(hdr.dime.xyzt_units, 7) % mask with 0x07
  154. case 1
  155. extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_METER';
  156. case 2
  157. extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MM'; % millimeter
  158. case 3
  159. extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_MICRO';
  160. otherwise
  161. extra.NIFTI_SPACE_UNIT = 'NIFTI_UNITS_UNKNOWN';
  162. end
  163. switch bitand(hdr.dime.xyzt_units, 56) % mask with 0x38
  164. case 8
  165. extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_SEC';
  166. case 16
  167. extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_MSEC';
  168. case 24
  169. extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_USEC'; % microsecond
  170. otherwise
  171. extra.NIFTI_TIME_UNIT = 'NIFTI_UNITS_UNKNOWN';
  172. end
  173. switch hdr.dime.xyzt_units
  174. case 32
  175. extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_HZ';
  176. case 40
  177. extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_PPM'; % part per million
  178. case 48
  179. extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_RADS'; % radians per second
  180. otherwise
  181. extra.NIFTI_SPECTRAL_UNIT = 'NIFTI_UNITS_UNKNOWN';
  182. end
  183. % MRI-specific spatial and temporal information
  184. %
  185. dim_info = hdr.hk.dim_info;
  186. extra.NIFTI_FREQ_DIM = bitand(dim_info, 3);
  187. extra.NIFTI_PHASE_DIM = bitand(bitshift(dim_info, -2), 3);
  188. extra.NIFTI_SLICE_DIM = bitand(bitshift(dim_info, -4), 3);
  189. % Check slice code
  190. %
  191. switch hdr.dime.slice_code
  192. case 1
  193. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_INC'; % sequential increasing
  194. case 2
  195. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_SEQ_DEC'; % sequential decreasing
  196. case 3
  197. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC'; % alternating increasing
  198. case 4
  199. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC'; % alternating decreasing
  200. case 5
  201. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_INC2'; % ALT_INC # 2
  202. case 6
  203. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_ALT_DEC2'; % ALT_DEC # 2
  204. otherwise
  205. extra.NIFTI_SLICE_ORDER = 'NIFTI_SLICE_UNKNOWN';
  206. end
  207. % Check NIFTI version
  208. %
  209. if ~isempty(hdr.hist.magic) & strcmp(hdr.hist.magic(1),'n') & ...
  210. ( strcmp(hdr.hist.magic(2),'i') | strcmp(hdr.hist.magic(2),'+') ) & ...
  211. str2num(hdr.hist.magic(3)) >= 1 & str2num(hdr.hist.magic(3)) <= 9
  212. extra.NIFTI_VERSION = str2num(hdr.hist.magic(3));
  213. else
  214. extra.NIFTI_VERSION = 0;
  215. end
  216. % Check if data stored in the same file (*.nii) or separate
  217. % files (*.hdr/*.img)
  218. %
  219. if isempty(hdr.hist.magic)
  220. extra.NIFTI_ONEFILE = 0;
  221. else
  222. extra.NIFTI_ONEFILE = strcmp(hdr.hist.magic(2), '+');
  223. end
  224. % Swap has been taken care of by checking whether sizeof_hdr is
  225. % 348 (machine is 'ieee-le' or 'ieee-be' etc)
  226. %
  227. % extra.NIFTI_NEEDS_SWAP = (hdr.dime.dim(1) < 0 | hdr.dime.dim(1) > 7);
  228. % Check NIFTI header struct contains a 5th (vector) dimension
  229. %
  230. if hdr.dime.dim(1) > 4 & hdr.dime.dim(6) > 1
  231. extra.NIFTI_5TH_DIM = hdr.dime.dim(6);
  232. else
  233. extra.NIFTI_5TH_DIM = 0;
  234. end
  235. hdr.extra = extra;
  236. return; % extra_nii_hdr