save_untouch0_nii_hdr.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. % internal function
  2. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  3. function save_nii_hdr(hdr, fid)
  4. if ~isequal(hdr.hk.sizeof_hdr,348),
  5. error('hdr.hk.sizeof_hdr must be 348.');
  6. end
  7. write_header(hdr, fid);
  8. return; % save_nii_hdr
  9. %---------------------------------------------------------------------
  10. function write_header(hdr, fid)
  11. % Original header structures
  12. % struct dsr /* dsr = hdr */
  13. % {
  14. % struct header_key hk; /* 0 + 40 */
  15. % struct image_dimension dime; /* 40 + 108 */
  16. % struct data_history hist; /* 148 + 200 */
  17. % }; /* total= 348 bytes*/
  18. header_key(fid, hdr.hk);
  19. image_dimension(fid, hdr.dime);
  20. data_history(fid, hdr.hist);
  21. % check the file size is 348 bytes
  22. %
  23. fbytes = ftell(fid);
  24. if ~isequal(fbytes,348),
  25. msg = sprintf('Header size is not 348 bytes.');
  26. warning(msg);
  27. end
  28. return; % write_header
  29. %---------------------------------------------------------------------
  30. function header_key(fid, hk)
  31. fseek(fid,0,'bof');
  32. % Original header structures
  33. % struct header_key /* header key */
  34. % { /* off + size */
  35. % int sizeof_hdr /* 0 + 4 */
  36. % char data_type[10]; /* 4 + 10 */
  37. % char db_name[18]; /* 14 + 18 */
  38. % int extents; /* 32 + 4 */
  39. % short int session_error; /* 36 + 2 */
  40. % char regular; /* 38 + 1 */
  41. % char hkey_un0; /* 39 + 1 */
  42. % }; /* total=40 bytes */
  43. fwrite(fid, hk.sizeof_hdr(1), 'int32'); % must be 348.
  44. % data_type = sprintf('%-10s',hk.data_type); % ensure it is 10 chars from left
  45. % fwrite(fid, data_type(1:10), 'uchar');
  46. pad = zeros(1, 10-length(hk.data_type));
  47. hk.data_type = [hk.data_type char(pad)];
  48. fwrite(fid, hk.data_type(1:10), 'uchar');
  49. % db_name = sprintf('%-18s', hk.db_name); % ensure it is 18 chars from left
  50. % fwrite(fid, db_name(1:18), 'uchar');
  51. pad = zeros(1, 18-length(hk.db_name));
  52. hk.db_name = [hk.db_name char(pad)];
  53. fwrite(fid, hk.db_name(1:18), 'uchar');
  54. fwrite(fid, hk.extents(1), 'int32');
  55. fwrite(fid, hk.session_error(1), 'int16');
  56. fwrite(fid, hk.regular(1), 'uchar');
  57. fwrite(fid, hk.hkey_un0(1), 'uchar');
  58. return; % header_key
  59. %---------------------------------------------------------------------
  60. function image_dimension(fid, dime)
  61. %struct image_dimension
  62. % { /* off + size */
  63. % short int dim[8]; /* 0 + 16 */
  64. % char vox_units[4]; /* 16 + 4 */
  65. % char cal_units[8]; /* 20 + 8 */
  66. % short int unused1; /* 28 + 2 */
  67. % short int datatype; /* 30 + 2 */
  68. % short int bitpix; /* 32 + 2 */
  69. % short int dim_un0; /* 34 + 2 */
  70. % float pixdim[8]; /* 36 + 32 */
  71. % /*
  72. % pixdim[] specifies the voxel dimensions:
  73. % pixdim[1] - voxel width
  74. % pixdim[2] - voxel height
  75. % pixdim[3] - interslice distance
  76. % ..etc
  77. % */
  78. % float vox_offset; /* 68 + 4 */
  79. % float roi_scale; /* 72 + 4 */
  80. % float funused1; /* 76 + 4 */
  81. % float funused2; /* 80 + 4 */
  82. % float cal_max; /* 84 + 4 */
  83. % float cal_min; /* 88 + 4 */
  84. % int compressed; /* 92 + 4 */
  85. % int verified; /* 96 + 4 */
  86. % int glmax; /* 100 + 4 */
  87. % int glmin; /* 104 + 4 */
  88. % }; /* total=108 bytes */
  89. fwrite(fid, dime.dim(1:8), 'int16');
  90. pad = zeros(1, 4-length(dime.vox_units));
  91. dime.vox_units = [dime.vox_units char(pad)];
  92. fwrite(fid, dime.vox_units(1:4), 'uchar');
  93. pad = zeros(1, 8-length(dime.cal_units));
  94. dime.cal_units = [dime.cal_units char(pad)];
  95. fwrite(fid, dime.cal_units(1:8), 'uchar');
  96. fwrite(fid, dime.unused1(1), 'int16');
  97. fwrite(fid, dime.datatype(1), 'int16');
  98. fwrite(fid, dime.bitpix(1), 'int16');
  99. fwrite(fid, dime.dim_un0(1), 'int16');
  100. fwrite(fid, dime.pixdim(1:8), 'float32');
  101. fwrite(fid, dime.vox_offset(1), 'float32');
  102. fwrite(fid, dime.roi_scale(1), 'float32');
  103. fwrite(fid, dime.funused1(1), 'float32');
  104. fwrite(fid, dime.funused2(1), 'float32');
  105. fwrite(fid, dime.cal_max(1), 'float32');
  106. fwrite(fid, dime.cal_min(1), 'float32');
  107. fwrite(fid, dime.compressed(1), 'int32');
  108. fwrite(fid, dime.verified(1), 'int32');
  109. fwrite(fid, dime.glmax(1), 'int32');
  110. fwrite(fid, dime.glmin(1), 'int32');
  111. return; % image_dimension
  112. %---------------------------------------------------------------------
  113. function data_history(fid, hist)
  114. % Original header structures - ANALYZE 7.5
  115. %struct data_history
  116. % { /* off + size */
  117. % char descrip[80]; /* 0 + 80 */
  118. % char aux_file[24]; /* 80 + 24 */
  119. % char orient; /* 104 + 1 */
  120. % char originator[10]; /* 105 + 10 */
  121. % char generated[10]; /* 115 + 10 */
  122. % char scannum[10]; /* 125 + 10 */
  123. % char patient_id[10]; /* 135 + 10 */
  124. % char exp_date[10]; /* 145 + 10 */
  125. % char exp_time[10]; /* 155 + 10 */
  126. % char hist_un0[3]; /* 165 + 3 */
  127. % int views /* 168 + 4 */
  128. % int vols_added; /* 172 + 4 */
  129. % int start_field; /* 176 + 4 */
  130. % int field_skip; /* 180 + 4 */
  131. % int omax; /* 184 + 4 */
  132. % int omin; /* 188 + 4 */
  133. % int smax; /* 192 + 4 */
  134. % int smin; /* 196 + 4 */
  135. % }; /* total=200 bytes */
  136. % descrip = sprintf('%-80s', hist.descrip); % 80 chars from left
  137. % fwrite(fid, descrip(1:80), 'uchar');
  138. pad = zeros(1, 80-length(hist.descrip));
  139. hist.descrip = [hist.descrip char(pad)];
  140. fwrite(fid, hist.descrip(1:80), 'uchar');
  141. % aux_file = sprintf('%-24s', hist.aux_file); % 24 chars from left
  142. % fwrite(fid, aux_file(1:24), 'uchar');
  143. pad = zeros(1, 24-length(hist.aux_file));
  144. hist.aux_file = [hist.aux_file char(pad)];
  145. fwrite(fid, hist.aux_file(1:24), 'uchar');
  146. fwrite(fid, hist.orient(1), 'uchar');
  147. fwrite(fid, hist.originator(1:5), 'int16');
  148. pad = zeros(1, 10-length(hist.generated));
  149. hist.generated = [hist.generated char(pad)];
  150. fwrite(fid, hist.generated(1:10), 'uchar');
  151. pad = zeros(1, 10-length(hist.scannum));
  152. hist.scannum = [hist.scannum char(pad)];
  153. fwrite(fid, hist.scannum(1:10), 'uchar');
  154. pad = zeros(1, 10-length(hist.patient_id));
  155. hist.patient_id = [hist.patient_id char(pad)];
  156. fwrite(fid, hist.patient_id(1:10), 'uchar');
  157. pad = zeros(1, 10-length(hist.exp_date));
  158. hist.exp_date = [hist.exp_date char(pad)];
  159. fwrite(fid, hist.exp_date(1:10), 'uchar');
  160. pad = zeros(1, 10-length(hist.exp_time));
  161. hist.exp_time = [hist.exp_time char(pad)];
  162. fwrite(fid, hist.exp_time(1:10), 'uchar');
  163. pad = zeros(1, 3-length(hist.hist_un0));
  164. hist.hist_un0 = [hist.hist_un0 char(pad)];
  165. fwrite(fid, hist.hist_un0(1:3), 'uchar');
  166. fwrite(fid, hist.views(1), 'int32');
  167. fwrite(fid, hist.vols_added(1), 'int32');
  168. fwrite(fid, hist.start_field(1),'int32');
  169. fwrite(fid, hist.field_skip(1), 'int32');
  170. fwrite(fid, hist.omax(1), 'int32');
  171. fwrite(fid, hist.omin(1), 'int32');
  172. fwrite(fid, hist.smax(1), 'int32');
  173. fwrite(fid, hist.smin(1), 'int32');
  174. return; % data_history