load_untouch0_nii_hdr.m 8.1 KB

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