get_nii_frame.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. % Return time frame of a NIFTI dataset. Support both *.nii and
  2. % *.hdr/*.img file extension. If file extension is not provided,
  3. % *.hdr/*.img will be used as default.
  4. %
  5. % It is a lightweighted "load_nii_hdr", and is equivalent to
  6. % hdr.dime.dim(5)
  7. %
  8. % Usage: [ total_scan ] = get_nii_frame(filename)
  9. %
  10. % filename - NIFTI file name.
  11. %
  12. % Returned values:
  13. %
  14. % total_scan - total number of image scans for the time frame
  15. %
  16. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  17. %
  18. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  19. %
  20. function [ total_scan ] = get_nii_frame(filename)
  21. if ~exist('filename','var'),
  22. error('Usage: [ total_scan ] = get_nii_frame(filename)');
  23. end
  24. v = version;
  25. % Check file extension. If .gz, unpack it into temp folder
  26. %
  27. if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
  28. if ~strcmp(filename(end-6:end), '.img.gz') & ...
  29. ~strcmp(filename(end-6:end), '.hdr.gz') & ...
  30. ~strcmp(filename(end-6:end), '.nii.gz')
  31. error('Please check filename.');
  32. end
  33. if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
  34. error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
  35. elseif strcmp(filename(end-6:end), '.img.gz')
  36. filename1 = filename;
  37. filename2 = filename;
  38. filename2(end-6:end) = '';
  39. filename2 = [filename2, '.hdr.gz'];
  40. tmpDir = tempname;
  41. mkdir(tmpDir);
  42. gzFileName = filename;
  43. filename1 = gunzip(filename1, tmpDir);
  44. filename2 = gunzip(filename2, tmpDir);
  45. filename = char(filename1); % convert from cell to string
  46. elseif strcmp(filename(end-6:end), '.hdr.gz')
  47. filename1 = filename;
  48. filename2 = filename;
  49. filename2(end-6:end) = '';
  50. filename2 = [filename2, '.img.gz'];
  51. tmpDir = tempname;
  52. mkdir(tmpDir);
  53. gzFileName = filename;
  54. filename1 = gunzip(filename1, tmpDir);
  55. filename2 = gunzip(filename2, tmpDir);
  56. filename = char(filename1); % convert from cell to string
  57. elseif strcmp(filename(end-6:end), '.nii.gz')
  58. tmpDir = tempname;
  59. mkdir(tmpDir);
  60. gzFileName = filename;
  61. filename = gunzip(filename, tmpDir);
  62. filename = char(filename); % convert from cell to string
  63. end
  64. end
  65. fileprefix = filename;
  66. machine = 'ieee-le';
  67. new_ext = 0;
  68. if findstr('.nii',fileprefix) & strcmp(fileprefix(end-3:end), '.nii')
  69. new_ext = 1;
  70. fileprefix(end-3:end)='';
  71. end
  72. if findstr('.hdr',fileprefix) & strcmp(fileprefix(end-3:end), '.hdr')
  73. fileprefix(end-3:end)='';
  74. end
  75. if findstr('.img',fileprefix) & strcmp(fileprefix(end-3:end), '.img')
  76. fileprefix(end-3:end)='';
  77. end
  78. if new_ext
  79. fn = sprintf('%s.nii',fileprefix);
  80. if ~exist(fn)
  81. msg = sprintf('Cannot find file "%s.nii".', fileprefix);
  82. error(msg);
  83. end
  84. else
  85. fn = sprintf('%s.hdr',fileprefix);
  86. if ~exist(fn)
  87. msg = sprintf('Cannot find file "%s.hdr".', fileprefix);
  88. error(msg);
  89. end
  90. end
  91. fid = fopen(fn,'r',machine);
  92. if fid < 0,
  93. msg = sprintf('Cannot open file %s.',fn);
  94. error(msg);
  95. else
  96. hdr = read_header(fid);
  97. fclose(fid);
  98. end
  99. if hdr.sizeof_hdr ~= 348
  100. % first try reading the opposite endian to 'machine'
  101. switch machine,
  102. case 'ieee-le', machine = 'ieee-be';
  103. case 'ieee-be', machine = 'ieee-le';
  104. end
  105. fid = fopen(fn,'r',machine);
  106. if fid < 0,
  107. msg = sprintf('Cannot open file %s.',fn);
  108. error(msg);
  109. else
  110. hdr = read_header(fid);
  111. fclose(fid);
  112. end
  113. end
  114. if hdr.sizeof_hdr ~= 348
  115. % Now throw an error
  116. msg = sprintf('File "%s" is corrupted.',fn);
  117. error(msg);
  118. end
  119. total_scan = hdr.dim(5);
  120. % Clean up after gunzip
  121. %
  122. if exist('gzFileName', 'var')
  123. rmdir(tmpDir,'s');
  124. end
  125. return; % get_nii_frame
  126. %---------------------------------------------------------------------
  127. function [ dsr ] = read_header(fid)
  128. fseek(fid,0,'bof');
  129. dsr.sizeof_hdr = fread(fid,1,'int32')'; % should be 348!
  130. fseek(fid,40,'bof');
  131. dsr.dim = fread(fid,8,'int16')';
  132. return; % read_header