load_nii_ext.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. % Load NIFTI header extension after its header is loaded using load_nii_hdr.
  2. %
  3. % Usage: ext = load_nii_ext(filename)
  4. %
  5. % filename - NIFTI file name.
  6. %
  7. % Returned values:
  8. %
  9. % ext - Structure of NIFTI header extension, which includes num_ext,
  10. % and all the extended header sections in the header extension.
  11. % Each extended header section will have its esize, ecode, and
  12. % edata, where edata can be plain text, xml, or any raw data
  13. % that was saved in the extended header section.
  14. %
  15. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  16. %
  17. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  18. %
  19. function ext = load_nii_ext(filename)
  20. if ~exist('filename','var'),
  21. error('Usage: ext = load_nii_ext(filename)');
  22. end
  23. v = version;
  24. % Check file extension. If .gz, unpack it into temp folder
  25. %
  26. if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
  27. if ~strcmp(filename(end-6:end), '.img.gz') & ...
  28. ~strcmp(filename(end-6:end), '.hdr.gz') & ...
  29. ~strcmp(filename(end-6:end), '.nii.gz')
  30. error('Please check filename.');
  31. end
  32. if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
  33. error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
  34. elseif strcmp(filename(end-6:end), '.img.gz')
  35. filename1 = filename;
  36. filename2 = filename;
  37. filename2(end-6:end) = '';
  38. filename2 = [filename2, '.hdr.gz'];
  39. tmpDir = tempname;
  40. mkdir(tmpDir);
  41. gzFileName = filename;
  42. filename1 = gunzip(filename1, tmpDir);
  43. filename2 = gunzip(filename2, tmpDir);
  44. filename = char(filename1); % convert from cell to string
  45. elseif strcmp(filename(end-6:end), '.hdr.gz')
  46. filename1 = filename;
  47. filename2 = filename;
  48. filename2(end-6:end) = '';
  49. filename2 = [filename2, '.img.gz'];
  50. tmpDir = tempname;
  51. mkdir(tmpDir);
  52. gzFileName = filename;
  53. filename1 = gunzip(filename1, tmpDir);
  54. filename2 = gunzip(filename2, tmpDir);
  55. filename = char(filename1); % convert from cell to string
  56. elseif strcmp(filename(end-6:end), '.nii.gz')
  57. tmpDir = tempname;
  58. mkdir(tmpDir);
  59. gzFileName = filename;
  60. filename = gunzip(filename, tmpDir);
  61. filename = char(filename); % convert from cell to string
  62. end
  63. end
  64. machine = 'ieee-le';
  65. new_ext = 0;
  66. if findstr('.nii',filename) & strcmp(filename(end-3:end), '.nii')
  67. new_ext = 1;
  68. filename(end-3:end)='';
  69. end
  70. if findstr('.hdr',filename) & strcmp(filename(end-3:end), '.hdr')
  71. filename(end-3:end)='';
  72. end
  73. if findstr('.img',filename) & strcmp(filename(end-3:end), '.img')
  74. filename(end-3:end)='';
  75. end
  76. if new_ext
  77. fn = sprintf('%s.nii',filename);
  78. if ~exist(fn)
  79. msg = sprintf('Cannot find file "%s.nii".', filename);
  80. error(msg);
  81. end
  82. else
  83. fn = sprintf('%s.hdr',filename);
  84. if ~exist(fn)
  85. msg = sprintf('Cannot find file "%s.hdr".', filename);
  86. error(msg);
  87. end
  88. end
  89. fid = fopen(fn,'r',machine);
  90. vox_offset = 0;
  91. if fid < 0,
  92. msg = sprintf('Cannot open file %s.',fn);
  93. error(msg);
  94. else
  95. fseek(fid,0,'bof');
  96. if fread(fid,1,'int32') == 348
  97. if new_ext
  98. fseek(fid,108,'bof');
  99. vox_offset = fread(fid,1,'float32');
  100. end
  101. ext = read_extension(fid, vox_offset);
  102. fclose(fid);
  103. else
  104. fclose(fid);
  105. % first try reading the opposite endian to 'machine'
  106. %
  107. switch machine,
  108. case 'ieee-le', machine = 'ieee-be';
  109. case 'ieee-be', machine = 'ieee-le';
  110. end
  111. fid = fopen(fn,'r',machine);
  112. if fid < 0,
  113. msg = sprintf('Cannot open file %s.',fn);
  114. error(msg);
  115. else
  116. fseek(fid,0,'bof');
  117. if fread(fid,1,'int32') ~= 348
  118. % Now throw an error
  119. %
  120. msg = sprintf('File "%s" is corrupted.',fn);
  121. error(msg);
  122. end
  123. if new_ext
  124. fseek(fid,108,'bof');
  125. vox_offset = fread(fid,1,'float32');
  126. end
  127. ext = read_extension(fid, vox_offset);
  128. fclose(fid);
  129. end
  130. end
  131. end
  132. % Clean up after gunzip
  133. %
  134. if exist('gzFileName', 'var')
  135. rmdir(tmpDir,'s');
  136. end
  137. return % load_nii_ext
  138. %---------------------------------------------------------------------
  139. function ext = read_extension(fid, vox_offset)
  140. ext = [];
  141. if vox_offset
  142. end_of_ext = vox_offset;
  143. else
  144. fseek(fid, 0, 'eof');
  145. end_of_ext = ftell(fid);
  146. end
  147. if end_of_ext > 352
  148. fseek(fid, 348, 'bof');
  149. ext.extension = fread(fid,4)';
  150. end
  151. if isempty(ext) | ext.extension(1) == 0
  152. ext = [];
  153. return;
  154. end
  155. i = 1;
  156. while(ftell(fid) < end_of_ext)
  157. ext.section(i).esize = fread(fid,1,'int32');
  158. ext.section(i).ecode = fread(fid,1,'int32');
  159. ext.section(i).edata = char(fread(fid,ext.section(i).esize-8)');
  160. i = i + 1;
  161. end
  162. ext.num_ext = length(ext.section);
  163. return % read_extension