load_untouch_nii_hdr.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. % internal function
  2. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  3. function hdr = load_nii_hdr(fileprefix, machine, filetype)
  4. if filetype == 2
  5. fn = sprintf('%s.nii',fileprefix);
  6. if ~exist(fn)
  7. msg = sprintf('Cannot find file "%s.nii".', fileprefix);
  8. error(msg);
  9. end
  10. else
  11. fn = sprintf('%s.hdr',fileprefix);
  12. if ~exist(fn)
  13. msg = sprintf('Cannot find file "%s.hdr".', fileprefix);
  14. error(msg);
  15. end
  16. end
  17. fid = fopen(fn,'r',machine);
  18. if fid < 0,
  19. msg = sprintf('Cannot open file %s.',fn);
  20. error(msg);
  21. else
  22. fseek(fid,0,'bof');
  23. hdr = read_header(fid);
  24. fclose(fid);
  25. end
  26. return % load_nii_hdr
  27. %---------------------------------------------------------------------
  28. function [ dsr ] = read_header(fid)
  29. % Original header structures
  30. % struct dsr
  31. % {
  32. % struct header_key hk; /* 0 + 40 */
  33. % struct image_dimension dime; /* 40 + 108 */
  34. % struct data_history hist; /* 148 + 200 */
  35. % }; /* total= 348 bytes*/
  36. dsr.hk = header_key(fid);
  37. dsr.dime = image_dimension(fid);
  38. dsr.hist = data_history(fid);
  39. % For Analyze data format
  40. %
  41. if ~strcmp(dsr.hist.magic, 'n+1') & ~strcmp(dsr.hist.magic, 'ni1')
  42. dsr.hist.qform_code = 0;
  43. dsr.hist.sform_code = 0;
  44. end
  45. return % read_header
  46. %---------------------------------------------------------------------
  47. function [ hk ] = header_key(fid)
  48. fseek(fid,0,'bof');
  49. % Original header structures
  50. % struct header_key /* header key */
  51. % { /* off + size */
  52. % int sizeof_hdr /* 0 + 4 */
  53. % char data_type[10]; /* 4 + 10 */
  54. % char db_name[18]; /* 14 + 18 */
  55. % int extents; /* 32 + 4 */
  56. % short int session_error; /* 36 + 2 */
  57. % char regular; /* 38 + 1 */
  58. % char dim_info; % char hkey_un0; /* 39 + 1 */
  59. % }; /* total=40 bytes */
  60. %
  61. % int sizeof_header Should be 348.
  62. % char regular Must be 'r' to indicate that all images and
  63. % volumes are the same size.
  64. v6 = version;
  65. if str2num(v6(1))<6
  66. directchar = '*char';
  67. else
  68. directchar = 'uchar=>char';
  69. end
  70. hk.sizeof_hdr = fread(fid, 1,'int32')'; % should be 348!
  71. hk.data_type = deblank(fread(fid,10,directchar)');
  72. hk.db_name = deblank(fread(fid,18,directchar)');
  73. hk.extents = fread(fid, 1,'int32')';
  74. hk.session_error = fread(fid, 1,'int16')';
  75. hk.regular = fread(fid, 1,directchar)';
  76. hk.dim_info = fread(fid, 1,'uchar')';
  77. return % header_key
  78. %---------------------------------------------------------------------
  79. function [ dime ] = image_dimension(fid)
  80. % Original header structures
  81. % struct image_dimension
  82. % { /* off + size */
  83. % short int dim[8]; /* 0 + 16 */
  84. % /*
  85. % dim[0] Number of dimensions in database; usually 4.
  86. % dim[1] Image X dimension; number of *pixels* in an image row.
  87. % dim[2] Image Y dimension; number of *pixel rows* in slice.
  88. % dim[3] Volume Z dimension; number of *slices* in a volume.
  89. % dim[4] Time points; number of volumes in database
  90. % */
  91. % float intent_p1; % char vox_units[4]; /* 16 + 4 */
  92. % float intent_p2; % char cal_units[8]; /* 20 + 4 */
  93. % float intent_p3; % char cal_units[8]; /* 24 + 4 */
  94. % short int intent_code; % short int unused1; /* 28 + 2 */
  95. % short int datatype; /* 30 + 2 */
  96. % short int bitpix; /* 32 + 2 */
  97. % short int slice_start; % short int dim_un0; /* 34 + 2 */
  98. % float pixdim[8]; /* 36 + 32 */
  99. % /*
  100. % pixdim[] specifies the voxel dimensions:
  101. % pixdim[1] - voxel width, mm
  102. % pixdim[2] - voxel height, mm
  103. % pixdim[3] - slice thickness, mm
  104. % pixdim[4] - volume timing, in msec
  105. % ..etc
  106. % */
  107. % float vox_offset; /* 68 + 4 */
  108. % float scl_slope; % float roi_scale; /* 72 + 4 */
  109. % float scl_inter; % float funused1; /* 76 + 4 */
  110. % short slice_end; % float funused2; /* 80 + 2 */
  111. % char slice_code; % float funused2; /* 82 + 1 */
  112. % char xyzt_units; % float funused2; /* 83 + 1 */
  113. % float cal_max; /* 84 + 4 */
  114. % float cal_min; /* 88 + 4 */
  115. % float slice_duration; % int compressed; /* 92 + 4 */
  116. % float toffset; % int verified; /* 96 + 4 */
  117. % int glmax; /* 100 + 4 */
  118. % int glmin; /* 104 + 4 */
  119. % }; /* total=108 bytes */
  120. dime.dim = fread(fid,8,'int16')';
  121. dime.intent_p1 = fread(fid,1,'float32')';
  122. dime.intent_p2 = fread(fid,1,'float32')';
  123. dime.intent_p3 = fread(fid,1,'float32')';
  124. dime.intent_code = fread(fid,1,'int16')';
  125. dime.datatype = fread(fid,1,'int16')';
  126. dime.bitpix = fread(fid,1,'int16')';
  127. dime.slice_start = fread(fid,1,'int16')';
  128. dime.pixdim = fread(fid,8,'float32')';
  129. dime.vox_offset = fread(fid,1,'float32')';
  130. dime.scl_slope = fread(fid,1,'float32')';
  131. dime.scl_inter = fread(fid,1,'float32')';
  132. dime.slice_end = fread(fid,1,'int16')';
  133. dime.slice_code = fread(fid,1,'uchar')';
  134. dime.xyzt_units = fread(fid,1,'uchar')';
  135. dime.cal_max = fread(fid,1,'float32')';
  136. dime.cal_min = fread(fid,1,'float32')';
  137. dime.slice_duration = fread(fid,1,'float32')';
  138. dime.toffset = fread(fid,1,'float32')';
  139. dime.glmax = fread(fid,1,'int32')';
  140. dime.glmin = fread(fid,1,'int32')';
  141. return % image_dimension
  142. %---------------------------------------------------------------------
  143. function [ hist ] = data_history(fid)
  144. % Original header structures
  145. % struct data_history
  146. % { /* off + size */
  147. % char descrip[80]; /* 0 + 80 */
  148. % char aux_file[24]; /* 80 + 24 */
  149. % short int qform_code; /* 104 + 2 */
  150. % short int sform_code; /* 106 + 2 */
  151. % float quatern_b; /* 108 + 4 */
  152. % float quatern_c; /* 112 + 4 */
  153. % float quatern_d; /* 116 + 4 */
  154. % float qoffset_x; /* 120 + 4 */
  155. % float qoffset_y; /* 124 + 4 */
  156. % float qoffset_z; /* 128 + 4 */
  157. % float srow_x[4]; /* 132 + 16 */
  158. % float srow_y[4]; /* 148 + 16 */
  159. % float srow_z[4]; /* 164 + 16 */
  160. % char intent_name[16]; /* 180 + 16 */
  161. % char magic[4]; % int smin; /* 196 + 4 */
  162. % }; /* total=200 bytes */
  163. v6 = version;
  164. if str2num(v6(1))<6
  165. directchar = '*char';
  166. else
  167. directchar = 'uchar=>char';
  168. end
  169. hist.descrip = deblank(fread(fid,80,directchar)');
  170. hist.aux_file = deblank(fread(fid,24,directchar)');
  171. hist.qform_code = fread(fid,1,'int16')';
  172. hist.sform_code = fread(fid,1,'int16')';
  173. hist.quatern_b = fread(fid,1,'float32')';
  174. hist.quatern_c = fread(fid,1,'float32')';
  175. hist.quatern_d = fread(fid,1,'float32')';
  176. hist.qoffset_x = fread(fid,1,'float32')';
  177. hist.qoffset_y = fread(fid,1,'float32')';
  178. hist.qoffset_z = fread(fid,1,'float32')';
  179. hist.srow_x = fread(fid,4,'float32')';
  180. hist.srow_y = fread(fid,4,'float32')';
  181. hist.srow_z = fread(fid,4,'float32')';
  182. hist.intent_name = deblank(fread(fid,16,directchar)');
  183. hist.magic = deblank(fread(fid,4,directchar)');
  184. return % data_history