save_nii_hdr.m 9.3 KB

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