save_untouch_nii_hdr.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 dim_info; % 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'); % might be uint8
  57. % fwrite(fid, hk.hkey_un0(1), 'uchar');
  58. % fwrite(fid, hk.hkey_un0(1), 'uint8');
  59. fwrite(fid, hk.dim_info(1), 'uchar');
  60. return; % header_key
  61. %---------------------------------------------------------------------
  62. function image_dimension(fid, dime)
  63. % Original header structures
  64. % struct image_dimension
  65. % { /* off + size */
  66. % short int dim[8]; /* 0 + 16 */
  67. % float intent_p1; % char vox_units[4]; /* 16 + 4 */
  68. % float intent_p2; % char cal_units[8]; /* 20 + 4 */
  69. % float intent_p3; % char cal_units[8]; /* 24 + 4 */
  70. % short int intent_code; % short int unused1; /* 28 + 2 */
  71. % short int datatype; /* 30 + 2 */
  72. % short int bitpix; /* 32 + 2 */
  73. % short int slice_start; % short int dim_un0; /* 34 + 2 */
  74. % float pixdim[8]; /* 36 + 32 */
  75. % /*
  76. % pixdim[] specifies the voxel dimensions:
  77. % pixdim[1] - voxel width
  78. % pixdim[2] - voxel height
  79. % pixdim[3] - interslice distance
  80. % pixdim[4] - volume timing, in msec
  81. % ..etc
  82. % */
  83. % float vox_offset; /* 68 + 4 */
  84. % float scl_slope; % float roi_scale; /* 72 + 4 */
  85. % float scl_inter; % float funused1; /* 76 + 4 */
  86. % short slice_end; % float funused2; /* 80 + 2 */
  87. % char slice_code; % float funused2; /* 82 + 1 */
  88. % char xyzt_units; % float funused2; /* 83 + 1 */
  89. % float cal_max; /* 84 + 4 */
  90. % float cal_min; /* 88 + 4 */
  91. % float slice_duration; % int compressed; /* 92 + 4 */
  92. % float toffset; % int verified; /* 96 + 4 */
  93. % int glmax; /* 100 + 4 */
  94. % int glmin; /* 104 + 4 */
  95. % }; /* total=108 bytes */
  96. fwrite(fid, dime.dim(1:8), 'int16');
  97. fwrite(fid, dime.intent_p1(1), 'float32');
  98. fwrite(fid, dime.intent_p2(1), 'float32');
  99. fwrite(fid, dime.intent_p3(1), 'float32');
  100. fwrite(fid, dime.intent_code(1), 'int16');
  101. fwrite(fid, dime.datatype(1), 'int16');
  102. fwrite(fid, dime.bitpix(1), 'int16');
  103. fwrite(fid, dime.slice_start(1), 'int16');
  104. fwrite(fid, dime.pixdim(1:8), 'float32');
  105. fwrite(fid, dime.vox_offset(1), 'float32');
  106. fwrite(fid, dime.scl_slope(1), 'float32');
  107. fwrite(fid, dime.scl_inter(1), 'float32');
  108. fwrite(fid, dime.slice_end(1), 'int16');
  109. fwrite(fid, dime.slice_code(1), 'uchar');
  110. fwrite(fid, dime.xyzt_units(1), 'uchar');
  111. fwrite(fid, dime.cal_max(1), 'float32');
  112. fwrite(fid, dime.cal_min(1), 'float32');
  113. fwrite(fid, dime.slice_duration(1), 'float32');
  114. fwrite(fid, dime.toffset(1), 'float32');
  115. fwrite(fid, dime.glmax(1), 'int32');
  116. fwrite(fid, dime.glmin(1), 'int32');
  117. return; % image_dimension
  118. %---------------------------------------------------------------------
  119. function data_history(fid, hist)
  120. % Original header structures
  121. %struct data_history
  122. % { /* off + size */
  123. % char descrip[80]; /* 0 + 80 */
  124. % char aux_file[24]; /* 80 + 24 */
  125. % short int qform_code; /* 104 + 2 */
  126. % short int sform_code; /* 106 + 2 */
  127. % float quatern_b; /* 108 + 4 */
  128. % float quatern_c; /* 112 + 4 */
  129. % float quatern_d; /* 116 + 4 */
  130. % float qoffset_x; /* 120 + 4 */
  131. % float qoffset_y; /* 124 + 4 */
  132. % float qoffset_z; /* 128 + 4 */
  133. % float srow_x[4]; /* 132 + 16 */
  134. % float srow_y[4]; /* 148 + 16 */
  135. % float srow_z[4]; /* 164 + 16 */
  136. % char intent_name[16]; /* 180 + 16 */
  137. % char magic[4]; % int smin; /* 196 + 4 */
  138. % }; /* total=200 bytes */
  139. % descrip = sprintf('%-80s', hist.descrip); % 80 chars from left
  140. % fwrite(fid, descrip(1:80), 'uchar');
  141. pad = zeros(1, 80-length(hist.descrip));
  142. hist.descrip = [hist.descrip char(pad)];
  143. fwrite(fid, hist.descrip(1:80), 'uchar');
  144. % aux_file = sprintf('%-24s', hist.aux_file); % 24 chars from left
  145. % fwrite(fid, aux_file(1:24), 'uchar');
  146. pad = zeros(1, 24-length(hist.aux_file));
  147. hist.aux_file = [hist.aux_file char(pad)];
  148. fwrite(fid, hist.aux_file(1:24), 'uchar');
  149. fwrite(fid, hist.qform_code, 'int16');
  150. fwrite(fid, hist.sform_code, 'int16');
  151. fwrite(fid, hist.quatern_b, 'float32');
  152. fwrite(fid, hist.quatern_c, 'float32');
  153. fwrite(fid, hist.quatern_d, 'float32');
  154. fwrite(fid, hist.qoffset_x, 'float32');
  155. fwrite(fid, hist.qoffset_y, 'float32');
  156. fwrite(fid, hist.qoffset_z, 'float32');
  157. fwrite(fid, hist.srow_x(1:4), 'float32');
  158. fwrite(fid, hist.srow_y(1:4), 'float32');
  159. fwrite(fid, hist.srow_z(1:4), 'float32');
  160. % intent_name = sprintf('%-16s', hist.intent_name); % 16 chars from left
  161. % fwrite(fid, intent_name(1:16), 'uchar');
  162. pad = zeros(1, 16-length(hist.intent_name));
  163. hist.intent_name = [hist.intent_name char(pad)];
  164. fwrite(fid, hist.intent_name(1:16), 'uchar');
  165. % magic = sprintf('%-4s', hist.magic); % 4 chars from left
  166. % fwrite(fid, magic(1:4), 'uchar');
  167. pad = zeros(1, 4-length(hist.magic));
  168. hist.magic = [hist.magic char(pad)];
  169. fwrite(fid, hist.magic(1:4), 'uchar');
  170. return; % data_history