load_untouch_header_only.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. % Load NIfTI / Analyze header without applying any appropriate affine
  2. % geometric transform or voxel intensity scaling. It is equivalent to
  3. % hdr field when using load_untouch_nii to load dataset. Support both
  4. % *.nii and *.hdr file extension. If file extension is not provided,
  5. % *.hdr will be used as default.
  6. %
  7. % Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename)
  8. %
  9. % filename - NIfTI / Analyze file name.
  10. %
  11. % Returned values:
  12. %
  13. % header - struct with NIfTI / Analyze header fields.
  14. %
  15. % ext - NIfTI extension if it is not empty.
  16. %
  17. % filetype - 0 for Analyze format (*.hdr/*.img);
  18. % 1 for NIFTI format in 2 files (*.hdr/*.img);
  19. % 2 for NIFTI format in 1 file (*.nii).
  20. %
  21. % machine - a string, see below for details. The default here is 'ieee-le'.
  22. %
  23. % 'native' or 'n' - local machine format - the default
  24. % 'ieee-le' or 'l' - IEEE floating point with little-endian
  25. % byte ordering
  26. % 'ieee-be' or 'b' - IEEE floating point with big-endian
  27. % byte ordering
  28. % 'vaxd' or 'd' - VAX D floating point and VAX ordering
  29. % 'vaxg' or 'g' - VAX G floating point and VAX ordering
  30. % 'cray' or 'c' - Cray floating point with big-endian
  31. % byte ordering
  32. % 'ieee-le.l64' or 'a' - IEEE floating point with little-endian
  33. % byte ordering and 64 bit long data type
  34. % 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte
  35. % ordering and 64 bit long data type.
  36. %
  37. % Part of this file is copied and modified from:
  38. % http://www.mathworks.com/matlabcentral/fileexchange/1878-mri-analyze-tools
  39. %
  40. % NIFTI data format can be found on: http://nifti.nimh.nih.gov
  41. %
  42. % - Jimmy Shen (jimmy@rotman-baycrest.on.ca)
  43. %
  44. function [hdr, ext, filetype, machine] = load_untouch_header_only(filename)
  45. if ~exist('filename','var')
  46. error('Usage: [header, ext, filetype, machine] = load_untouch_header_only(filename)');
  47. end
  48. v = version;
  49. % Check file extension. If .gz, unpack it into temp folder
  50. %
  51. if length(filename) > 2 & strcmp(filename(end-2:end), '.gz')
  52. if ~strcmp(filename(end-6:end), '.img.gz') & ...
  53. ~strcmp(filename(end-6:end), '.hdr.gz') & ...
  54. ~strcmp(filename(end-6:end), '.nii.gz')
  55. error('Please check filename.');
  56. end
  57. if str2num(v(1:3)) < 7.1 | ~usejava('jvm')
  58. error('Please use MATLAB 7.1 (with java) and above, or run gunzip outside MATLAB.');
  59. elseif strcmp(filename(end-6:end), '.img.gz')
  60. filename1 = filename;
  61. filename2 = filename;
  62. filename2(end-6:end) = '';
  63. filename2 = [filename2, '.hdr.gz'];
  64. tmpDir = tempname;
  65. mkdir(tmpDir);
  66. gzFileName = filename;
  67. filename1 = gunzip(filename1, tmpDir);
  68. filename2 = gunzip(filename2, tmpDir);
  69. filename = char(filename1); % convert from cell to string
  70. elseif strcmp(filename(end-6:end), '.hdr.gz')
  71. filename1 = filename;
  72. filename2 = filename;
  73. filename2(end-6:end) = '';
  74. filename2 = [filename2, '.img.gz'];
  75. tmpDir = tempname;
  76. mkdir(tmpDir);
  77. gzFileName = filename;
  78. filename1 = gunzip(filename1, tmpDir);
  79. filename2 = gunzip(filename2, tmpDir);
  80. filename = char(filename1); % convert from cell to string
  81. elseif strcmp(filename(end-6:end), '.nii.gz')
  82. tmpDir = tempname;
  83. mkdir(tmpDir);
  84. gzFileName = filename;
  85. filename = gunzip(filename, tmpDir);
  86. filename = char(filename); % convert from cell to string
  87. end
  88. end
  89. % Read the dataset header
  90. %
  91. [hdr, filetype, fileprefix, machine] = load_nii_hdr(filename);
  92. if filetype == 0
  93. hdr = load_untouch0_nii_hdr(fileprefix, machine);
  94. ext = [];
  95. else
  96. hdr = load_untouch_nii_hdr(fileprefix, machine, filetype);
  97. % Read the header extension
  98. %
  99. ext = load_nii_ext(filename);
  100. end
  101. % Set bitpix according to datatype
  102. %
  103. % /*Acceptable values for datatype are*/
  104. %
  105. % 0 None (Unknown bit per voxel) % DT_NONE, DT_UNKNOWN
  106. % 1 Binary (ubit1, bitpix=1) % DT_BINARY
  107. % 2 Unsigned char (uchar or uint8, bitpix=8) % DT_UINT8, NIFTI_TYPE_UINT8
  108. % 4 Signed short (int16, bitpix=16) % DT_INT16, NIFTI_TYPE_INT16
  109. % 8 Signed integer (int32, bitpix=32) % DT_INT32, NIFTI_TYPE_INT32
  110. % 16 Floating point (single or float32, bitpix=32) % DT_FLOAT32, NIFTI_TYPE_FLOAT32
  111. % 32 Complex, 2 float32 (Use float32, bitpix=64) % DT_COMPLEX64, NIFTI_TYPE_COMPLEX64
  112. % 64 Double precision (double or float64, bitpix=64) % DT_FLOAT64, NIFTI_TYPE_FLOAT64
  113. % 128 uint8 RGB (Use uint8, bitpix=24) % DT_RGB24, NIFTI_TYPE_RGB24
  114. % 256 Signed char (schar or int8, bitpix=8) % DT_INT8, NIFTI_TYPE_INT8
  115. % 511 Single RGB (Use float32, bitpix=96) % DT_RGB96, NIFTI_TYPE_RGB96
  116. % 512 Unsigned short (uint16, bitpix=16) % DT_UNINT16, NIFTI_TYPE_UNINT16
  117. % 768 Unsigned integer (uint32, bitpix=32) % DT_UNINT32, NIFTI_TYPE_UNINT32
  118. % 1024 Signed long long (int64, bitpix=64) % DT_INT64, NIFTI_TYPE_INT64
  119. % 1280 Unsigned long long (uint64, bitpix=64) % DT_UINT64, NIFTI_TYPE_UINT64
  120. % 1536 Long double, float128 (Unsupported, bitpix=128) % DT_FLOAT128, NIFTI_TYPE_FLOAT128
  121. % 1792 Complex128, 2 float64 (Use float64, bitpix=128) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
  122. % 2048 Complex256, 2 float128 (Unsupported, bitpix=256) % DT_COMPLEX128, NIFTI_TYPE_COMPLEX128
  123. %
  124. switch hdr.dime.datatype
  125. case 1,
  126. hdr.dime.bitpix = 1; precision = 'ubit1';
  127. case 2,
  128. hdr.dime.bitpix = 8; precision = 'uint8';
  129. case 4,
  130. hdr.dime.bitpix = 16; precision = 'int16';
  131. case 8,
  132. hdr.dime.bitpix = 32; precision = 'int32';
  133. case 16,
  134. hdr.dime.bitpix = 32; precision = 'float32';
  135. case 32,
  136. hdr.dime.bitpix = 64; precision = 'float32';
  137. case 64,
  138. hdr.dime.bitpix = 64; precision = 'float64';
  139. case 128,
  140. hdr.dime.bitpix = 24; precision = 'uint8';
  141. case 256
  142. hdr.dime.bitpix = 8; precision = 'int8';
  143. case 511
  144. hdr.dime.bitpix = 96; precision = 'float32';
  145. case 512
  146. hdr.dime.bitpix = 16; precision = 'uint16';
  147. case 768
  148. hdr.dime.bitpix = 32; precision = 'uint32';
  149. case 1024
  150. hdr.dime.bitpix = 64; precision = 'int64';
  151. case 1280
  152. hdr.dime.bitpix = 64; precision = 'uint64';
  153. case 1792,
  154. hdr.dime.bitpix = 128; precision = 'float64';
  155. otherwise
  156. error('This datatype is not supported');
  157. end
  158. tmp = hdr.dime.dim(2:end);
  159. tmp(find(tmp < 1)) = 1;
  160. hdr.dime.dim(2:end) = tmp;
  161. % Clean up after gunzip
  162. %
  163. if exist('gzFileName', 'var')
  164. rmdir(tmpDir,'s');
  165. end
  166. return % load_untouch_header_only